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 cat ${FSMNT}/etc/rc.conf | grep -q "wlans_${NIC}="
108 if [ $? -ne 0 ] ; then
109 echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf
111 echo "ifconfig_${WLAN}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
113 WLANCOUNT=$((WLANCOUNT+1))
115 echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
119 done < ${TMPDIR}/.niclist
124 # Function which detects available nics, and enables dhcp on them
130 # Function which simply enables iPv6 SLAAC on all detected nics
133 rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
134 # start by getting a list of nics on this system
135 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
136 if [ -e "${TMPDIR}/.niclist" ]
138 echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
142 NIC="`echo $line | cut -d ':' -f 1`"
143 DESC="`echo $line | cut -d ':' -f 2`"
144 echo_log "Setting $NIC to accepting RAs on the system."
148 # We have a wifi device, setup a wlan* entry for it
149 # Given we cannot have DHCP and SLAAC the same time currently
150 # it's save to just duplicate.
151 WLAN="wlan${WLANCOUNT}"
152 cat ${FSMNT}/etc/rc.conf | grep -q "wlans_${NIC}="
153 if [ $? -ne 0 ] ; then
154 echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf
156 #echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf
157 echo "ifconfig_${WLAN}_ipv6=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf
159 WLANCOUNT=$((WLANCOUNT+1))
161 #echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf
162 echo "ifconfig_${NIC}_ipv6=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf
166 done < ${TMPDIR}/.niclist
169 # Given we cannot yet rely on RAs to provide DNS information as much
170 # as we can in the DHCP world, we should append a given nameserver.
171 : > ${FSMNT}/etc/resolv.conf
172 get_value_from_cfg netSaveIPv6NameServer
174 if [ -n "${NAMESERVER}" ]
176 echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
182 # Function which detects available nics, and enables IPv6 SLAAC on them
189 # Function which saves a manual nic setup to the installed system
195 get_value_from_cfg netSaveIP
198 if [ "$NETIP" = "DHCP" ]
200 echo_log "Setting $NIC to DHCP on the system."
201 echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
205 # If we get here, we have a manual setup, lets do so now
212 IFARGS="inet ${NETIP}"
214 # Check if we have a netmask to set
215 get_value_from_cfg netSaveMask
217 if [ -n "${NETMASK}" ]
219 IFARGS="${IFARGS} netmask ${NETMASK}"
223 get_value_from_cfg netSaveIPv6
225 if [ -n "${NETIP6}" ]
227 # Make sure we have one inet6 prefix.
228 IF6ARGS=`echo "${NETIP6}" | awk '{ if ("^inet6 ") { print $0; } else
229 { printf "inet6 %s", $0; } }'`
232 echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
233 if [ -n "${IFARGS}" ]
235 echo "ifconfig_${NIC}=\"${IFARGS}\"" >>${FSMNT}/etc/rc.conf
237 if [ -n "${IF6ARGS}" ]
239 echo "ifconfig_${NIC}_ipv6=\"${IF6ARGS}\"" >>${FSMNT}/etc/rc.conf
242 # Check if we have a default router to set
243 get_value_from_cfg netSaveDefaultRouter
245 if [ -n "${NETROUTE}" ]
247 echo "defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
249 get_value_from_cfg netSaveIPv6DefaultRouter
251 if [ -n "${NETROUTE}" ]
253 echo "ipv6_defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
256 # Check if we have a nameserver to enable
257 : > ${FSMNT}/etc/resolv.conf
258 get_value_from_cfg netSaveNameServer
260 if [ -n "${NAMESERVER}" ]
262 echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
264 get_value_from_cfg netSaveIPv6NameServer
266 if [ -n "${NAMESERVER}" ]
268 echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
273 # Function which determines if a nic is active / up
276 ifconfig ${1} | grep -q "status: active" 2>/dev/null
277 if [ $? -eq 0 ] ; then
285 # Function which detects available nics, and runs DHCP on them until
289 # start by getting a list of nics on this system
290 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
293 NIC="`echo $line | cut -d ':' -f 1`"
294 DESC="`echo $line | cut -d ':' -f 2`"
296 is_nic_active "${NIC}"
297 if [ $? -eq 0 ] ; then
298 echo_log "Trying DHCP on $NIC $DESC"
299 dhclient ${NIC} >/dev/null 2>/dev/null
300 if [ $? -eq 0 ] ; then
301 # Got a valid DHCP IP, we can return now
306 done < ${TMPDIR}/.niclist
310 # Function which detects available nics, and runs rtsol on them.
314 # start by getting a list of nics on this system
315 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
319 NIC="`echo $line | cut -d ':' -f 1`"
320 DESC="`echo $line | cut -d ':' -f 2`"
322 is_nic_active "${NIC}"
323 if [ $? -eq 0 ] ; then
324 echo_log "Will try IPv6 SLAAC on $NIC $DESC"
325 ifconfig ${NIC} inet6 -ifdisabled accept_rtadv up
326 ALLNICS="${ALLNICS} ${NIC}"
328 done < ${TMPDIR}/.niclist
330 # XXX once we support it in-tree call /sbin/resovconf here.
331 echo_log "Running rtsol on ${ALLNICS}"
332 rtsol -F ${ALLNICS} >/dev/null 2>/dev/null
335 # Get the mac address of a target NIC
338 FOUNDMAC="`ifconfig ${1} | grep 'ether' | tr -d '\t' | cut -d ' ' -f 2`"
342 # Function which performs the manual setup of a target nic in the cfg
348 # Check that this NIC exists
349 rc_halt "ifconfig ${NIC}"
351 get_value_from_cfg netIP
354 if [ "$NETIP" = "DHCP" ]
356 echo_log "Enabling DHCP on $NIC"
357 rc_halt "dhclient ${NIC}"
361 # If we get here, we have a manual setup, lets do so now
368 # Check if we have a netmask to set
369 get_value_from_cfg netMask
371 if [ -n "${NETMASK}" ]
373 rc_halt "ifconfig inet ${NIC} netmask ${NETMASK}"
375 rc_halt "ifconfig inet ${NIC} ${NETIP}"
379 # Check if we have a default router to set
380 get_value_from_cfg netDefaultRouter
382 if [ -n "${NETROUTE}" ]
384 rc_halt "route add -inet default ${NETROUTE}"
389 # Set static IPv6 address
390 get_value_from_cfg netIPv6
394 rc_halt "ifconfig inet6 ${NIC} ${NETIP} -ifdisabled up"
398 get_value_from_cfg netIPv6DefaultRouter
400 if [ -n "${NETROUTE}" ]
402 rc_halt "route add -inet6 default ${NETROUTE}"
405 # Check if we have a nameserver to enable
407 get_value_from_cfg netNameServer
409 if [ -n "${NAMESERVER}" ]
411 echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf
413 get_value_from_cfg netIPv6NameServer
415 if [ -n "${NAMESERVER}" ]
417 echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf
423 # Function which parses the cfg and enables networking per specified
426 # Check if we have any networking requested
427 get_value_from_cfg netDev
434 if [ "$NETDEV" = "AUTO-DHCP" ]
437 elif [ "$NETDEV" = "IPv6-SLAAC" ]
440 elif [ "$NETDEV" = "AUTO-DHCP-SLAAC" ]
445 enable_manual_nic ${NETDEV}
451 # Function which checks the cfg and enables the specified networking on
452 # the installed system
453 save_networking_install()
456 # Check if we have any networking requested to save
457 get_value_from_cfg netSaveDev
464 if [ "$NETDEV" = "AUTO-DHCP" ]
467 elif [ "$NETDEV" = "IPv6-SLAAC" ]
470 elif [ "$NETDEV" = "AUTO-DHCP-SLAAC" ]
475 save_manual_nic ${NETDEV}