3 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
4 # Copyright (c) 2011 The FreeBSD Foundation
7 # Portions of this software were developed by Bjoern Zeeb
8 # under sponsorship from the FreeBSD Foundation.
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 # Functions which perform our networking setup
35 # Function which creates a kde4 .desktop file for the PC-BSD net tray
36 create_desktop_nettray()
39 echo "#!/usr/bin/env xdg-open
41 Exec=/usr/local/kde4/bin/pc-nettray ${NIC}
44 Type=Application" > ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop
45 chmod 744 ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop
49 # Function which checks is a nic is wifi or not
53 ifconfig ${NIC} | grep -q "802.11" 2>/dev/null
62 # Function to get the first available wired nic, used for setup
65 rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
66 # start by getting a list of nics on this system
67 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
68 if [ -e "${TMPDIR}/.niclist" ]
72 NIC="`echo $line | cut -d ':' -f 1`"
79 done < ${TMPDIR}/.niclist
87 # Function which simply enables plain dhcp on all detected nics
90 rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
91 # start by getting a list of nics on this system
92 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
93 if [ -e "${TMPDIR}/.niclist" ]
95 echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
99 NIC="`echo $line | cut -d ':' -f 1`"
100 DESC="`echo $line | cut -d ':' -f 2`"
101 echo_log "Setting $NIC to DHCP on the system."
105 # We have a wifi device, setup a wlan* entry for it
106 WLAN="wlan${WLANCOUNT}"
107 echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf
108 echo "ifconfig_${WLAN}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
110 WLANCOUNT=$((WLANCOUNT+1))
112 echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
116 done < ${TMPDIR}/.niclist
121 # Function which detects available nics, and enables dhcp on them
127 # Function which simply enables iPv6 SLAAC on all detected nics
130 rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
131 # start by getting a list of nics on this system
132 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
133 if [ -e "${TMPDIR}/.niclist" ]
135 echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
139 NIC="`echo $line | cut -d ':' -f 1`"
140 DESC="`echo $line | cut -d ':' -f 2`"
141 echo_log "Setting $NIC to acceptign RAs on the system."
145 # We have a wifi device, setup a wlan* entry for it
146 # Given we cannot have DHCP and SLAAC the same time currently
147 # it's save to just duplicate.
148 WLAN="wlan${WLANCOUNT}"
149 echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf
150 #echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf
151 echo "ifconfig_${WLAN}=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf
153 WLANCOUNT=$((WLANCOUNT+1))
155 #echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf
156 echo "ifconfig_${NIC}_ipv6=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf
160 done < ${TMPDIR}/.niclist
163 # Given we cannot yet rely on RAs to provide DNS information as much
164 # as we can in the DHCP world, we should append a given nameserver.
165 : > ${FSMNT}/etc/resolv.conf
166 get_value_from_cfg netSaveIPv6NameServer
168 if [ -n "${NAMESERVER}" ]
170 echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
176 # Function which detects available nics, and enables IPv6 SLAAC on them
183 # Function which saves a manual nic setup to the installed system
189 get_value_from_cfg netSaveIP
192 if [ "$NETIP" = "DHCP" ]
194 echo_log "Setting $NIC to DHCP on the system."
195 echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
199 # If we get here, we have a manual setup, lets do so now
206 IFARGS="inet ${NETIP}"
208 # Check if we have a netmask to set
209 get_value_from_cfg netSaveMask
211 if [ -n "${NETMASK}" ]
213 IFARGS="${IFARGS} netmask ${NETMASK}"
217 get_value_from_cfg netSaveIPv6
219 if [ -n "${NETIP6}" ]
221 # Make sure we have one inet6 prefix.
222 IF6ARGS=`echo "${NETIP6}" | awk '{ if ("^inet6 ") { print $0; } else
223 { printf "inet6 %s", $0; } }'`
226 echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
227 if [ -n "${IFARGS}" ]
229 echo "ifconfig_${NIC}=\"${IFARGS}\"" >>${FSMNT}/etc/rc.conf
231 if [ -n "${IF6ARGS}" ]
233 echo "ifconfig_${NIC}_ipv6=\"${IF6ARGS}\"" >>${FSMNT}/etc/rc.conf
236 # Check if we have a default router to set
237 get_value_from_cfg netSaveDefaultRouter
239 if [ -n "${NETROUTE}" ]
241 echo "defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
243 get_value_from_cfg netSaveIPv6DefaultRouter
245 if [ -n "${NETROUTE}" ]
247 echo "ipv6_defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
250 # Check if we have a nameserver to enable
251 : > ${FSMNT}/etc/resolv.conf
252 get_value_from_cfg netSaveNameServer
254 if [ -n "${NAMESERVER}" ]
256 echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
258 get_value_from_cfg netSaveIPv6NameServer
260 if [ -n "${NAMESERVER}" ]
262 echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
267 # Function which determines if a nic is active / up
270 ifconfig ${1} | grep -q "status: active" 2>/dev/null
271 if [ $? -eq 0 ] ; then
279 # Function which detects available nics, and runs DHCP on them until
283 # start by getting a list of nics on this system
284 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
287 NIC="`echo $line | cut -d ':' -f 1`"
288 DESC="`echo $line | cut -d ':' -f 2`"
290 is_nic_active "${NIC}"
291 if [ $? -eq 0 ] ; then
292 echo_log "Trying DHCP on $NIC $DESC"
293 dhclient ${NIC} >/dev/null 2>/dev/null
294 if [ $? -eq 0 ] ; then
295 # Got a valid DHCP IP, we can return now
300 done < ${TMPDIR}/.niclist
304 # Function which detects available nics, and runs rtsol on them.
308 # start by getting a list of nics on this system
309 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
313 NIC="`echo $line | cut -d ':' -f 1`"
314 DESC="`echo $line | cut -d ':' -f 2`"
316 is_nic_active "${NIC}"
317 if [ $? -eq 0 ] ; then
318 echo_log "Will try IPv6 SLAAC on $NIC $DESC"
319 ifconfig ${NIC} inet6 -ifdisabled accept_rtadv up
320 ALLNICS="${ALLNICS} ${NIC}"
322 done < ${TMPDIR}/.niclist
324 # XXX once we support it in-tree call /sbin/resovconf here.
325 echo_log "Running rtsol on ${ALLNICS}"
326 rtsol -F ${ALLNICS} >/dev/null 2>/dev/null
329 # Get the mac address of a target NIC
332 FOUNDMAC="`ifconfig ${1} | grep 'ether' | tr -d '\t' | cut -d ' ' -f 2`"
336 # Function which performs the manual setup of a target nic in the cfg
342 # Check that this NIC exists
343 rc_halt "ifconfig ${NIC}"
345 get_value_from_cfg netIP
348 if [ "$NETIP" = "DHCP" ]
350 echo_log "Enabling DHCP on $NIC"
351 rc_halt "dhclient ${NIC}"
355 # If we get here, we have a manual setup, lets do so now
362 # Check if we have a netmask to set
363 get_value_from_cfg netMask
365 if [ -n "${NETMASK}" ]
367 rc_halt "ifconfig inet ${NIC} netmask ${NETMASK}"
369 rc_halt "ifconfig inet ${NIC} ${NETIP}"
373 # Check if we have a default router to set
374 get_value_from_cfg netDefaultRouter
376 if [ -n "${NETROUTE}" ]
378 rc_halt "route add -inet default ${NETROUTE}"
383 # Set static IPv6 address
384 get_value_from_cfg netIPv6
388 rc_halt "ifconfig inet6 ${NIC} ${NETIP} -ifdisabled up"
392 get_value_from_cfg netIPv6DefaultRouter
394 if [ -n "${NETROUTE}" ]
396 rc_halt "route add -inet6 default ${NETROUTE}"
399 # Check if we have a nameserver to enable
401 get_value_from_cfg netNameServer
403 if [ -n "${NAMESERVER}" ]
405 echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf
407 get_value_from_cfg netIPv6NameServer
409 if [ -n "${NAMESERVER}" ]
411 echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf
417 # Function which parses the cfg and enables networking per specified
420 # Check if we have any networking requested
421 get_value_from_cfg netDev
428 if [ "$NETDEV" = "AUTO-DHCP" ]
431 elif [ "$NETDEV" = "IPv6-SLAAC" ]
434 elif [ "$NETDEV" = "AUTO-DHCP-SLAAC" ]
439 enable_manual_nic ${NETDEV}
445 # Function which checks the cfg and enables the specified networking on
446 # the installed system
447 save_networking_install()
450 # Check if we have any networking requested to save
451 get_value_from_cfg netSaveDev
458 if [ "$NETDEV" = "AUTO-DHCP" ]
461 elif [ "$NETDEV" = "IPv6-SLAAC" ]
464 elif [ "$NETDEV" = "AUTO-DHCP-SLAAC" ]
469 save_manual_nic ${NETDEV}