3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
6 # Copyright (c) 2011 The FreeBSD Foundation
9 # Portions of this software were developed by Bjoern Zeeb
10 # under sponsorship from the FreeBSD Foundation.
12 # Redistribution and use in source and binary forms, with or without
13 # modification, are permitted provided that the following conditions
15 # 1. Redistributions of source code must retain the above copyright
16 # notice, this list of conditions and the following disclaimer.
17 # 2. Redistributions in binary form must reproduce the above copyright
18 # notice, this list of conditions and the following disclaimer in the
19 # documentation and/or other materials provided with the distribution.
21 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 # Functions which perform our networking setup
37 # Function which creates a kde4 .desktop file for the PC-BSD net tray
38 create_desktop_nettray()
41 echo "#!/usr/bin/env xdg-open
43 Exec=/usr/local/kde4/bin/pc-nettray ${NIC}
46 Type=Application" > ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop
47 chmod 744 ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop
51 # Function which checks is a nic is wifi or not
55 ifconfig ${NIC} | grep -q "802.11" 2>/dev/null
64 # Function to get the first available wired nic, used for setup
67 rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
68 # start by getting a list of nics on this system
69 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
70 if [ -e "${TMPDIR}/.niclist" ]
74 NIC="`echo $line | cut -d ':' -f 1`"
81 done < ${TMPDIR}/.niclist
89 # Function which simply enables plain dhcp on all detected nics
92 rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
93 # start by getting a list of nics on this system
94 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
95 if [ -e "${TMPDIR}/.niclist" ]
97 echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
101 NIC="`echo $line | cut -d ':' -f 1`"
102 DESC="`echo $line | cut -d ':' -f 2`"
103 echo_log "Setting $NIC to DHCP on the system."
107 # We have a wifi device, setup a wlan* entry for it
108 WLAN="wlan${WLANCOUNT}"
109 cat ${FSMNT}/etc/rc.conf | grep -q "wlans_${NIC}="
110 if [ $? -ne 0 ] ; then
111 echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf
113 echo "ifconfig_${WLAN}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
115 WLANCOUNT=$((WLANCOUNT+1))
117 echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
121 done < ${TMPDIR}/.niclist
126 # Function which detects available nics, and enables dhcp on them
132 # Function which simply enables iPv6 SLAAC on all detected nics
135 rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
136 # start by getting a list of nics on this system
137 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
138 if [ -e "${TMPDIR}/.niclist" ]
140 echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
144 NIC="`echo $line | cut -d ':' -f 1`"
145 DESC="`echo $line | cut -d ':' -f 2`"
146 echo_log "Setting $NIC to accepting RAs on the system."
150 # We have a wifi device, setup a wlan* entry for it
151 # Given we cannot have DHCP and SLAAC the same time currently
152 # it's save to just duplicate.
153 WLAN="wlan${WLANCOUNT}"
154 cat ${FSMNT}/etc/rc.conf | grep -q "wlans_${NIC}="
155 if [ $? -ne 0 ] ; then
156 echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf
158 #echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf
159 echo "ifconfig_${WLAN}_ipv6=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf
161 WLANCOUNT=$((WLANCOUNT+1))
163 #echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf
164 echo "ifconfig_${NIC}_ipv6=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf
168 done < ${TMPDIR}/.niclist
171 # Given we cannot yet rely on RAs to provide DNS information as much
172 # as we can in the DHCP world, we should append a given nameserver.
173 : > ${FSMNT}/etc/resolv.conf
174 get_value_from_cfg netSaveIPv6NameServer
176 if [ -n "${NAMESERVER}" ]
178 echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
184 # Function which detects available nics, and enables IPv6 SLAAC on them
191 # Function which saves a manual nic setup to the installed system
197 get_value_from_cfg netSaveIP_${NIC}
200 if [ "$NETIP" = "DHCP" ]
202 echo_log "Setting $NIC to DHCP on the system."
203 echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
207 # If we get here, we have a manual setup, lets do so now
214 IFARGS="inet ${NETIP}"
216 # Check if we have a netmask to set
217 get_value_from_cfg netSaveMask_${NIC}
219 if [ -n "${NETMASK}" ]
221 IFARGS="${IFARGS} netmask ${NETMASK}"
225 get_value_from_cfg netSaveIPv6_${NIC}
227 if [ -n "${NETIP6}" ]
229 # Make sure we have one inet6 prefix.
230 IF6ARGS=`echo "${NETIP6}" | awk '{ if ("^inet6 ") { print $0; } else
231 { printf "inet6 %s", $0; } }'`
234 echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
235 if [ -n "${IFARGS}" ]
237 echo "ifconfig_${NIC}=\"${IFARGS}\"" >>${FSMNT}/etc/rc.conf
239 if [ -n "${IF6ARGS}" ]
241 echo "ifconfig_${NIC}_ipv6=\"${IF6ARGS}\"" >>${FSMNT}/etc/rc.conf
246 # Function which saves a manual gateway router setup to the installed system
250 # Check if we have a default router to set
251 get_value_from_cfg netSaveDefaultRouter
253 if [ -n "${NETROUTE}" ]
255 echo "defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
257 get_value_from_cfg netSaveIPv6DefaultRouter
259 if [ -n "${NETROUTE}" ]
261 echo "ipv6_defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
266 save_manual_nameserver()
268 # Check if we have a nameserver to enable
269 : > ${FSMNT}/etc/resolv.conf
270 get_value_from_cfg_with_spaces netSaveNameServer
271 NAMESERVERLIST="${VAL}"
272 if [ ! -z "${NAMESERVERLIST}" ]
274 for NAMESERVER in ${NAMESERVERLIST}
276 echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
280 get_value_from_cfg_with_spaces netSaveIPv6NameServer
281 NAMESERVERLIST="${VAL}"
282 if [ ! -z "${NAMESERVERLIST}" ]
284 for NAMESERVER in ${NAMESERVERLIST}
286 echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
292 # Function which determines if a nic is active / up
295 ifconfig ${1} | grep -q "status: active" 2>/dev/null
296 if [ $? -eq 0 ] ; then
304 # Function which detects available nics, and runs DHCP on them until
308 # start by getting a list of nics on this system
309 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
312 NIC="`echo $line | cut -d ':' -f 1`"
313 DESC="`echo $line | cut -d ':' -f 2`"
315 is_nic_active "${NIC}"
316 if [ $? -eq 0 ] ; then
317 echo_log "Trying DHCP on $NIC $DESC"
318 dhclient ${NIC} >/dev/null 2>/dev/null
319 if [ $? -eq 0 ] ; then
320 # Got a valid DHCP IP, we can return now
325 done < ${TMPDIR}/.niclist
329 # Function which detects available nics, and runs rtsol on them.
333 # start by getting a list of nics on this system
334 ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
338 NIC="`echo $line | cut -d ':' -f 1`"
339 DESC="`echo $line | cut -d ':' -f 2`"
341 is_nic_active "${NIC}"
342 if [ $? -eq 0 ] ; then
343 echo_log "Will try IPv6 SLAAC on $NIC $DESC"
344 ifconfig ${NIC} inet6 -ifdisabled accept_rtadv up
345 ALLNICS="${ALLNICS} ${NIC}"
347 done < ${TMPDIR}/.niclist
349 # XXX once we support it in-tree call /sbin/resovconf here.
350 echo_log "Running rtsol on ${ALLNICS}"
351 rtsol -F ${ALLNICS} >/dev/null 2>/dev/null
354 # Get the mac address of a target NIC
357 FOUNDMAC="`ifconfig ${1} | grep 'ether' | tr -d '\t' | cut -d ' ' -f 2`"
361 # Function which performs the manual setup of a target nic in the cfg
367 # Check that this NIC exists
368 rc_halt "ifconfig ${NIC}"
370 get_value_from_cfg netIP
373 if [ "$NETIP" = "DHCP" ]
375 echo_log "Enabling DHCP on $NIC"
376 rc_halt "dhclient ${NIC}"
380 # If we get here, we have a manual setup, lets do so now
387 # Check if we have a netmask to set
388 get_value_from_cfg netMask
390 if [ -n "${NETMASK}" ]
392 rc_halt "ifconfig inet ${NIC} netmask ${NETMASK}"
394 rc_halt "ifconfig inet ${NIC} ${NETIP}"
398 # Check if we have a default router to set
399 get_value_from_cfg netDefaultRouter
401 if [ -n "${NETROUTE}" ]
403 rc_halt "route add -inet default ${NETROUTE}"
408 # Set static IPv6 address
409 get_value_from_cfg netIPv6
413 rc_halt "ifconfig inet6 ${NIC} ${NETIP} -ifdisabled up"
417 get_value_from_cfg netIPv6DefaultRouter
419 if [ -n "${NETROUTE}" ]
421 rc_halt "route add -inet6 default ${NETROUTE}"
424 # Check if we have a nameserver to enable
426 get_value_from_cfg netNameServer
428 if [ -n "${NAMESERVER}" ]
430 echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf
432 get_value_from_cfg netIPv6NameServer
434 if [ -n "${NAMESERVER}" ]
436 echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf
442 # Function which parses the cfg and enables networking per specified
445 # Check if we have any networking requested
446 get_value_from_cfg netDev
453 if [ "$NETDEV" = "AUTO-DHCP" ]
456 elif [ "$NETDEV" = "IPv6-SLAAC" ]
459 elif [ "$NETDEV" = "AUTO-DHCP-SLAAC" ]
464 enable_manual_nic ${NETDEV}
470 # Function which checks the cfg and enables the specified networking on
471 # the installed system
472 save_networking_install()
475 # Check if we have any networking requested to save
476 get_value_from_cfg_with_spaces netSaveDev
483 if [ "$NETDEVLIST" = "AUTO-DHCP" ]
486 elif [ "$NETDEVLIST" = "IPv6-SLAAC" ]
489 elif [ "$NETDEVLIST" = "AUTO-DHCP-SLAAC" ]
494 for NETDEV in ${NETDEVLIST}
496 save_manual_nic ${NETDEV}
499 save_manual_nameserver