1209513Simp#!/bin/sh 2209513Simp#- 3209552Simp# Copyright (c) 2010 iXsystems, Inc. All rights reserved. 4222528Sbz# Copyright (c) 2011 The FreeBSD Foundation 5222528Sbz# All rights reserved. 6209513Simp# 7222528Sbz# Portions of this software were developed by Bjoern Zeeb 8222528Sbz# under sponsorship from the FreeBSD Foundation. 9222528Sbz# 10209513Simp# Redistribution and use in source and binary forms, with or without 11209513Simp# modification, are permitted provided that the following conditions 12209513Simp# are met: 13209513Simp# 1. Redistributions of source code must retain the above copyright 14209513Simp# notice, this list of conditions and the following disclaimer. 15209513Simp# 2. Redistributions in binary form must reproduce the above copyright 16209513Simp# notice, this list of conditions and the following disclaimer in the 17209513Simp# documentation and/or other materials provided with the distribution. 18209513Simp# 19209513Simp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20209513Simp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21209513Simp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22209513Simp# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23209513Simp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24209513Simp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25209513Simp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26209513Simp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27209513Simp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28209513Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29209513Simp# SUCH DAMAGE. 30209513Simp# 31209513Simp# $FreeBSD: releng/10.3/usr.sbin/pc-sysinstall/backend/functions-networking.sh 234987 2012-05-03 21:21:45Z jpaetzel $ 32209513Simp 33209513Simp# Functions which perform our networking setup 34209513Simp 35209513Simp# Function which creates a kde4 .desktop file for the PC-BSD net tray 36209513Simpcreate_desktop_nettray() 37209513Simp{ 38209513Simp NIC="${1}" 39209513Simp echo "#!/usr/bin/env xdg-open 40209513Simp[Desktop Entry] 41209513SimpExec=/usr/local/kde4/bin/pc-nettray ${NIC} 42209513SimpIcon=network 43209513SimpStartupNotify=false 44209513SimpType=Application" > ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop 45209513Simp chmod 744 ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop 46209513Simp 47209513Simp}; 48209513Simp 49209513Simp# Function which checks is a nic is wifi or not 50209513Simpcheck_is_wifi() 51209513Simp{ 52209513Simp NIC="$1" 53220059Sjpaetzel ifconfig ${NIC} | grep -q "802.11" 2>/dev/null 54220059Sjpaetzel if [ $? -eq 0 ] 55209513Simp then 56209513Simp return 0 57209513Simp else 58209513Simp return 1 59209513Simp fi 60209513Simp}; 61209513Simp 62217170Sjpaetzel# Function to get the first available wired nic, used for setup 63209513Simpget_first_wired_nic() 64209513Simp{ 65209513Simp rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null 66209513Simp # start by getting a list of nics on this system 67209513Simp ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist 68209513Simp if [ -e "${TMPDIR}/.niclist" ] 69209513Simp then 70209513Simp while read line 71209513Simp do 72209513Simp NIC="`echo $line | cut -d ':' -f 1`" 73209513Simp check_is_wifi ${NIC} 74220059Sjpaetzel if [ $? -ne 0 ] 75209513Simp then 76220059Sjpaetzel export VAL="${NIC}" 77211730Simp return 78209513Simp fi 79209513Simp done < ${TMPDIR}/.niclist 80209513Simp fi 81209513Simp 82220059Sjpaetzel export VAL="" 83209513Simp return 84209513Simp}; 85209513Simp 86217170Sjpaetzel 87217170Sjpaetzel# Function which simply enables plain dhcp on all detected nics 88217170Sjpaetzelenable_dhcp_all() 89209513Simp{ 90209513Simp rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null 91209513Simp # start by getting a list of nics on this system 92209513Simp ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist 93209513Simp if [ -e "${TMPDIR}/.niclist" ] 94209513Simp then 95209513Simp echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf 96209513Simp WLANCOUNT="0" 97209513Simp while read line 98209513Simp do 99209513Simp NIC="`echo $line | cut -d ':' -f 1`" 100209513Simp DESC="`echo $line | cut -d ':' -f 2`" 101209513Simp echo_log "Setting $NIC to DHCP on the system." 102209513Simp check_is_wifi ${NIC} 103220059Sjpaetzel if [ $? -eq 0 ] 104209513Simp then 105209513Simp # We have a wifi device, setup a wlan* entry for it 106209513Simp WLAN="wlan${WLANCOUNT}" 107232890Sjpaetzel cat ${FSMNT}/etc/rc.conf | grep -q "wlans_${NIC}=" 108232890Sjpaetzel if [ $? -ne 0 ] ; then 109232890Sjpaetzel echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf 110232890Sjpaetzel fi 111209513Simp echo "ifconfig_${WLAN}=\"DHCP\"" >>${FSMNT}/etc/rc.conf 112209513Simp CNIC="${WLAN}" 113220059Sjpaetzel WLANCOUNT=$((WLANCOUNT+1)) 114209513Simp else 115209513Simp echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf 116209513Simp CNIC="${NIC}" 117209513Simp fi 118209513Simp 119209513Simp done < ${TMPDIR}/.niclist 120209513Simp fi 121209513Simp}; 122209513Simp 123209513Simp 124217170Sjpaetzel# Function which detects available nics, and enables dhcp on them 125209513Simpsave_auto_dhcp() 126209513Simp{ 127217170Sjpaetzel enable_dhcp_all 128209513Simp}; 129209513Simp 130222528Sbz# Function which simply enables iPv6 SLAAC on all detected nics 131222528Sbzenable_slaac_all() 132222528Sbz{ 133222528Sbz rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null 134222528Sbz # start by getting a list of nics on this system 135222528Sbz ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist 136222528Sbz if [ -e "${TMPDIR}/.niclist" ] 137222528Sbz then 138222528Sbz echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf 139222528Sbz WLANCOUNT="0" 140222528Sbz while read line 141222528Sbz do 142222528Sbz NIC="`echo $line | cut -d ':' -f 1`" 143222528Sbz DESC="`echo $line | cut -d ':' -f 2`" 144232890Sjpaetzel echo_log "Setting $NIC to accepting RAs on the system." 145222528Sbz check_is_wifi ${NIC} 146222528Sbz if [ $? -eq 0 ] 147222528Sbz then 148222528Sbz # We have a wifi device, setup a wlan* entry for it 149222528Sbz # Given we cannot have DHCP and SLAAC the same time currently 150222528Sbz # it's save to just duplicate. 151222528Sbz WLAN="wlan${WLANCOUNT}" 152232890Sjpaetzel cat ${FSMNT}/etc/rc.conf | grep -q "wlans_${NIC}=" 153232890Sjpaetzel if [ $? -ne 0 ] ; then 154232890Sjpaetzel echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf 155232890Sjpaetzel fi 156222528Sbz #echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf 157232890Sjpaetzel echo "ifconfig_${WLAN}_ipv6=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf 158222528Sbz CNIC="${WLAN}" 159222528Sbz WLANCOUNT=$((WLANCOUNT+1)) 160222528Sbz else 161222528Sbz #echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf 162222528Sbz echo "ifconfig_${NIC}_ipv6=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf 163222528Sbz CNIC="${NIC}" 164222528Sbz fi 165222528Sbz 166222528Sbz done < ${TMPDIR}/.niclist 167222528Sbz fi 168209513Simp 169222528Sbz # Given we cannot yet rely on RAs to provide DNS information as much 170222528Sbz # as we can in the DHCP world, we should append a given nameserver. 171222528Sbz : > ${FSMNT}/etc/resolv.conf 172222528Sbz get_value_from_cfg netSaveIPv6NameServer 173222528Sbz NAMESERVER="${VAL}" 174222528Sbz if [ -n "${NAMESERVER}" ] 175222528Sbz then 176222528Sbz echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf 177222528Sbz fi 178222528Sbz 179222528Sbz}; 180222528Sbz 181222528Sbz 182222528Sbz# Function which detects available nics, and enables IPv6 SLAAC on them 183222528Sbzsave_auto_slaac() 184222528Sbz{ 185222528Sbz enable_slaac_all 186222528Sbz}; 187222528Sbz 188222528Sbz 189209513Simp# Function which saves a manual nic setup to the installed system 190209513Simpsave_manual_nic() 191209513Simp{ 192209513Simp # Get the target nic 193209513Simp NIC="$1" 194209513Simp 195234987Sjpaetzel get_value_from_cfg netSaveIP_${NIC} 196209513Simp NETIP="${VAL}" 197209513Simp 198209513Simp if [ "$NETIP" = "DHCP" ] 199209513Simp then 200209513Simp echo_log "Setting $NIC to DHCP on the system." 201209513Simp echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf 202209513Simp return 0 203209513Simp fi 204209513Simp 205209513Simp # If we get here, we have a manual setup, lets do so now 206222528Sbz IFARGS="" 207222528Sbz IF6ARGS="" 208209513Simp 209209513Simp # Set the manual IP 210222528Sbz if [ -n "${NETIP}" ] 211222528Sbz then 212222528Sbz IFARGS="inet ${NETIP}" 213209513Simp 214222528Sbz # Check if we have a netmask to set 215234987Sjpaetzel get_value_from_cfg netSaveMask_${NIC} 216222528Sbz NETMASK="${VAL}" 217222528Sbz if [ -n "${NETMASK}" ] 218222528Sbz then 219222528Sbz IFARGS="${IFARGS} netmask ${NETMASK}" 220222528Sbz fi 221222528Sbz fi 222222528Sbz 223234987Sjpaetzel get_value_from_cfg netSaveIPv6_${NIC} 224222528Sbz NETIP6="${VAL}" 225222528Sbz if [ -n "${NETIP6}" ] 226209513Simp then 227222528Sbz # Make sure we have one inet6 prefix. 228222528Sbz IF6ARGS=`echo "${NETIP6}" | awk '{ if ("^inet6 ") { print $0; } else 229222528Sbz { printf "inet6 %s", $0; } }'` 230209513Simp fi 231209513Simp 232209513Simp echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf 233222528Sbz if [ -n "${IFARGS}" ] 234222528Sbz then 235222528Sbz echo "ifconfig_${NIC}=\"${IFARGS}\"" >>${FSMNT}/etc/rc.conf 236222528Sbz fi 237222528Sbz if [ -n "${IF6ARGS}" ] 238222528Sbz then 239222528Sbz echo "ifconfig_${NIC}_ipv6=\"${IF6ARGS}\"" >>${FSMNT}/etc/rc.conf 240222528Sbz fi 241209513Simp 242234987Sjpaetzel}; 243234987Sjpaetzel 244234987Sjpaetzel# Function which saves a manual gateway router setup to the installed system 245234987Sjpaetzelsave_manual_router() 246234987Sjpaetzel{ 247234987Sjpaetzel 248209513Simp # Check if we have a default router to set 249209513Simp get_value_from_cfg netSaveDefaultRouter 250209513Simp NETROUTE="${VAL}" 251220059Sjpaetzel if [ -n "${NETROUTE}" ] 252209513Simp then 253209513Simp echo "defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf 254209513Simp fi 255222528Sbz get_value_from_cfg netSaveIPv6DefaultRouter 256222528Sbz NETROUTE="${VAL}" 257222528Sbz if [ -n "${NETROUTE}" ] 258222528Sbz then 259222528Sbz echo "ipv6_defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf 260222528Sbz fi 261209513Simp 262234987Sjpaetzel}; 263234987Sjpaetzel 264234987Sjpaetzelsave_manual_nameserver() 265234987Sjpaetzel{ 266209513Simp # Check if we have a nameserver to enable 267222528Sbz : > ${FSMNT}/etc/resolv.conf 268234987Sjpaetzel get_value_from_cfg_with_spaces netSaveNameServer 269234987Sjpaetzel NAMESERVERLIST="${VAL}" 270234987Sjpaetzel if [ ! -z "${NAMESERVERLIST}" ] 271209513Simp then 272234987Sjpaetzel for NAMESERVER in ${NAMESERVERLIST} 273234987Sjpaetzel do 274234987Sjpaetzel echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf 275234987Sjpaetzel done 276209513Simp fi 277234987Sjpaetzel 278234987Sjpaetzel get_value_from_cfg_with_spaces netSaveIPv6NameServer 279234987Sjpaetzel NAMESERVERLIST="${VAL}" 280234987Sjpaetzel if [ ! -z "${NAMESERVERLIST}" ] 281222528Sbz then 282234987Sjpaetzel for NAMESERVER in ${NAMESERVERLIST} 283234987Sjpaetzel do 284234987Sjpaetzel echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf 285234987Sjpaetzel done 286222528Sbz fi 287222528Sbz 288209513Simp}; 289209513Simp 290209513Simp# Function which determines if a nic is active / up 291209513Simpis_nic_active() 292209513Simp{ 293220059Sjpaetzel ifconfig ${1} | grep -q "status: active" 2>/dev/null 294220059Sjpaetzel if [ $? -eq 0 ] ; then 295209513Simp return 0 296209513Simp else 297209513Simp return 1 298209513Simp fi 299209513Simp}; 300209513Simp 301209513Simp 302209513Simp# Function which detects available nics, and runs DHCP on them until 303209513Simp# a success is found 304209513Simpenable_auto_dhcp() 305209513Simp{ 306209513Simp # start by getting a list of nics on this system 307209513Simp ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist 308209513Simp while read line 309209513Simp do 310209513Simp NIC="`echo $line | cut -d ':' -f 1`" 311209513Simp DESC="`echo $line | cut -d ':' -f 2`" 312209513Simp 313209513Simp is_nic_active "${NIC}" 314220059Sjpaetzel if [ $? -eq 0 ] ; then 315211730Simp echo_log "Trying DHCP on $NIC $DESC" 316211730Simp dhclient ${NIC} >/dev/null 2>/dev/null 317220059Sjpaetzel if [ $? -eq 0 ] ; then 318211730Simp # Got a valid DHCP IP, we can return now 319220059Sjpaetzel export WRKNIC="$NIC" 320211730Simp return 0 321211730Simp fi 322209513Simp fi 323209513Simp done < ${TMPDIR}/.niclist 324209513Simp 325209513Simp}; 326209513Simp 327222528Sbz# Function which detects available nics, and runs rtsol on them. 328222528Sbzenable_auto_slaac() 329222528Sbz{ 330222528Sbz 331222528Sbz # start by getting a list of nics on this system 332222528Sbz ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist 333222528Sbz ALLNICS="" 334222528Sbz while read line 335222528Sbz do 336222528Sbz NIC="`echo $line | cut -d ':' -f 1`" 337222528Sbz DESC="`echo $line | cut -d ':' -f 2`" 338222528Sbz 339222528Sbz is_nic_active "${NIC}" 340222528Sbz if [ $? -eq 0 ] ; then 341222528Sbz echo_log "Will try IPv6 SLAAC on $NIC $DESC" 342222528Sbz ifconfig ${NIC} inet6 -ifdisabled accept_rtadv up 343222528Sbz ALLNICS="${ALLNICS} ${NIC}" 344222528Sbz fi 345222528Sbz done < ${TMPDIR}/.niclist 346222528Sbz 347222528Sbz # XXX once we support it in-tree call /sbin/resovconf here. 348222528Sbz echo_log "Running rtsol on ${ALLNICS}" 349222528Sbz rtsol -F ${ALLNICS} >/dev/null 2>/dev/null 350222528Sbz} 351222528Sbz 352209513Simp# Get the mac address of a target NIC 353211730Simpget_nic_mac() 354211730Simp{ 355211730Simp FOUNDMAC="`ifconfig ${1} | grep 'ether' | tr -d '\t' | cut -d ' ' -f 2`" 356211730Simp export FOUNDMAC 357209513Simp} 358209513Simp 359209513Simp# Function which performs the manual setup of a target nic in the cfg 360209513Simpenable_manual_nic() 361209513Simp{ 362209513Simp # Get the target nic 363209513Simp NIC="$1" 364209513Simp 365209513Simp # Check that this NIC exists 366209513Simp rc_halt "ifconfig ${NIC}" 367209513Simp 368209513Simp get_value_from_cfg netIP 369209513Simp NETIP="${VAL}" 370209513Simp 371209513Simp if [ "$NETIP" = "DHCP" ] 372209513Simp then 373209513Simp echo_log "Enabling DHCP on $NIC" 374209513Simp rc_halt "dhclient ${NIC}" 375209513Simp return 0 376209513Simp fi 377209513Simp 378209513Simp # If we get here, we have a manual setup, lets do so now 379209513Simp 380222528Sbz # IPv4: 381222528Sbz 382209513Simp # Set the manual IP 383222528Sbz if [ -n "${NETIP}" ] 384209513Simp then 385222528Sbz # Check if we have a netmask to set 386222528Sbz get_value_from_cfg netMask 387222528Sbz NETMASK="${VAL}" 388222528Sbz if [ -n "${NETMASK}" ] 389222528Sbz then 390222528Sbz rc_halt "ifconfig inet ${NIC} netmask ${NETMASK}" 391222528Sbz else 392222528Sbz rc_halt "ifconfig inet ${NIC} ${NETIP}" 393222528Sbz fi 394209513Simp fi 395209513Simp 396209513Simp # Check if we have a default router to set 397209513Simp get_value_from_cfg netDefaultRouter 398209513Simp NETROUTE="${VAL}" 399220059Sjpaetzel if [ -n "${NETROUTE}" ] 400209513Simp then 401222528Sbz rc_halt "route add -inet default ${NETROUTE}" 402209513Simp fi 403209513Simp 404222528Sbz # IPv6: 405222528Sbz 406222528Sbz # Set static IPv6 address 407222528Sbz get_value_from_cfg netIPv6 408222528Sbz NETIP="${VAL}" 409222528Sbz if [ -n ${NETIP} ] 410222528Sbz then 411222528Sbz rc_halt "ifconfig inet6 ${NIC} ${NETIP} -ifdisabled up" 412222528Sbz fi 413222528Sbz 414222528Sbz # Default router 415222528Sbz get_value_from_cfg netIPv6DefaultRouter 416222528Sbz NETROUTE="${VAL}" 417222528Sbz if [ -n "${NETROUTE}" ] 418222528Sbz then 419222528Sbz rc_halt "route add -inet6 default ${NETROUTE}" 420222528Sbz fi 421222528Sbz 422209513Simp # Check if we have a nameserver to enable 423222528Sbz : >/etc/resolv.conf 424209513Simp get_value_from_cfg netNameServer 425209513Simp NAMESERVER="${VAL}" 426220059Sjpaetzel if [ -n "${NAMESERVER}" ] 427209513Simp then 428222528Sbz echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf 429209513Simp fi 430222528Sbz get_value_from_cfg netIPv6NameServer 431222528Sbz NAMESERVER="${VAL}" 432222528Sbz if [ -n "${NAMESERVER}" ] 433222528Sbz then 434222528Sbz echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf 435222528Sbz fi 436222528Sbz 437209513Simp}; 438209513Simp 439209513Simp 440209513Simp# Function which parses the cfg and enables networking per specified 441209513Simpstart_networking() 442209513Simp{ 443209513Simp # Check if we have any networking requested 444209513Simp get_value_from_cfg netDev 445209513Simp if [ -z "${VAL}" ] 446209513Simp then 447209513Simp return 0 448209513Simp fi 449209513Simp 450209513Simp NETDEV="${VAL}" 451209513Simp if [ "$NETDEV" = "AUTO-DHCP" ] 452209513Simp then 453209513Simp enable_auto_dhcp 454222528Sbz elif [ "$NETDEV" = "IPv6-SLAAC" ] 455222528Sbz then 456222528Sbz enable_auto_slaac 457227118Sjpaetzel elif [ "$NETDEV" = "AUTO-DHCP-SLAAC" ] 458227118Sjpaetzel then 459227118Sjpaetzel enable_auto_dhcp 460227118Sjpaetzel enable_auto_slaac 461209513Simp else 462209513Simp enable_manual_nic ${NETDEV} 463209513Simp fi 464209513Simp 465209513Simp}; 466209513Simp 467209513Simp 468209513Simp# Function which checks the cfg and enables the specified networking on 469209513Simp# the installed system 470209513Simpsave_networking_install() 471209513Simp{ 472209513Simp 473209513Simp # Check if we have any networking requested to save 474234987Sjpaetzel get_value_from_cfg_with_spaces netSaveDev 475209513Simp if [ -z "${VAL}" ] 476209513Simp then 477209513Simp return 0 478209513Simp fi 479209513Simp 480234987Sjpaetzel NETDEVLIST="${VAL}" 481234987Sjpaetzel if [ "$NETDEVLIST" = "AUTO-DHCP" ] 482209513Simp then 483209513Simp save_auto_dhcp 484234987Sjpaetzel elif [ "$NETDEVLIST" = "IPv6-SLAAC" ] 485222528Sbz then 486222528Sbz save_auto_slaac 487234987Sjpaetzel elif [ "$NETDEVLIST" = "AUTO-DHCP-SLAAC" ] 488227118Sjpaetzel then 489227118Sjpaetzel save_auto_dhcp 490227118Sjpaetzel save_auto_slaac 491209513Simp else 492234987Sjpaetzel for NETDEV in ${NETDEVLIST} 493234987Sjpaetzel do 494234987Sjpaetzel save_manual_nic ${NETDEV} 495234987Sjpaetzel done 496234987Sjpaetzel save_manual_router 497234987Sjpaetzel save_manual_nameserver 498209513Simp fi 499209513Simp 500209513Simp}; 501