]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - usr.sbin/pc-sysinstall/backend/functions-networking.sh
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / usr.sbin / pc-sysinstall / backend / functions-networking.sh
1 #!/bin/sh
2 #-
3 # Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4 # Copyright (c) 2011 The FreeBSD Foundation
5 # All rights reserved.
6 #
7 # Portions of this software were developed by Bjoern Zeeb
8 # under sponsorship from the FreeBSD Foundation.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
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.
18 #
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
29 # SUCH DAMAGE.
30 #
31 # $FreeBSD$
32
33 # Functions which perform our networking setup
34
35 # Function which creates a kde4 .desktop file for the PC-BSD net tray
36 create_desktop_nettray()
37 {
38   NIC="${1}"
39   echo "#!/usr/bin/env xdg-open
40 [Desktop Entry]
41 Exec=/usr/local/kde4/bin/pc-nettray ${NIC}
42 Icon=network
43 StartupNotify=false
44 Type=Application" > ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop
45   chmod 744 ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop
46
47 };
48
49 # Function which checks is a nic is wifi or not
50 check_is_wifi()
51 {
52   NIC="$1"
53   ifconfig ${NIC} | grep -q "802.11" 2>/dev/null
54   if [ $? -eq 0 ]
55   then
56     return 0
57   else 
58     return 1
59   fi
60 };
61
62 # Function to get the first available wired nic, used for setup
63 get_first_wired_nic()
64 {
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" ]
69   then
70     while read line
71     do
72       NIC="`echo $line | cut -d ':' -f 1`"
73       check_is_wifi ${NIC}
74       if [ $? -ne 0 ]
75       then
76         export VAL="${NIC}"
77         return
78       fi
79     done < ${TMPDIR}/.niclist
80   fi
81
82   export VAL=""
83   return
84 };
85
86
87 # Function which simply enables plain dhcp on all detected nics
88 enable_dhcp_all()
89 {
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" ]
94   then
95     echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
96     WLANCOUNT="0"
97     while read line
98     do
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."
102       check_is_wifi ${NIC}
103       if [ $? -eq 0 ]
104       then
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
109         CNIC="${WLAN}"
110         WLANCOUNT=$((WLANCOUNT+1))
111       else
112         echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
113         CNIC="${NIC}"
114       fi
115  
116     done < ${TMPDIR}/.niclist 
117   fi
118 };
119
120
121 # Function which detects available nics, and enables dhcp on them
122 save_auto_dhcp()
123 {
124   enable_dhcp_all
125 };
126
127 # Function which simply enables iPv6 SLAAC on all detected nics
128 enable_slaac_all()
129 {
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" ]
134   then
135     echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
136     WLANCOUNT="0"
137     while read line
138     do
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."
142       check_is_wifi ${NIC}
143       if [ $? -eq 0 ]
144       then
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
152         CNIC="${WLAN}"
153         WLANCOUNT=$((WLANCOUNT+1))
154       else
155         #echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf
156         echo "ifconfig_${NIC}_ipv6=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf
157         CNIC="${NIC}"
158       fi
159  
160     done < ${TMPDIR}/.niclist 
161   fi
162
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
167   NAMESERVER="${VAL}"
168   if [ -n "${NAMESERVER}" ]
169   then
170     echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
171   fi
172
173 };
174
175
176 # Function which detects available nics, and enables IPv6 SLAAC on them
177 save_auto_slaac()
178 {
179   enable_slaac_all
180 };
181
182
183 # Function which saves a manual nic setup to the installed system
184 save_manual_nic()
185 {
186   # Get the target nic
187   NIC="$1"
188
189   get_value_from_cfg netSaveIP
190   NETIP="${VAL}"
191  
192   if [ "$NETIP" = "DHCP" ]
193   then
194     echo_log "Setting $NIC to DHCP on the system."
195     echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
196     return 0
197   fi
198
199   # If we get here, we have a manual setup, lets do so now
200   IFARGS=""
201   IF6ARGS=""
202
203   # Set the manual IP
204   if [ -n "${NETIP}" ]
205   then
206     IFARGS="inet ${NETIP}"
207
208     # Check if we have a netmask to set
209     get_value_from_cfg netSaveMask
210     NETMASK="${VAL}"
211     if [ -n "${NETMASK}" ]
212     then
213       IFARGS="${IFARGS} netmask ${NETMASK}"
214     fi
215   fi
216
217   get_value_from_cfg netSaveIPv6
218   NETIP6="${VAL}"
219   if [ -n "${NETIP6}" ]
220   then
221     # Make sure we have one inet6 prefix.
222     IF6ARGS=`echo "${NETIP6}" | awk '{ if ("^inet6 ") { print $0; } else
223       { printf "inet6 %s", $0; } }'`
224   fi
225
226   echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
227   if [ -n "${IFARGS}" ]
228   then
229     echo "ifconfig_${NIC}=\"${IFARGS}\"" >>${FSMNT}/etc/rc.conf
230   fi
231   if [ -n "${IF6ARGS}" ]
232   then
233     echo "ifconfig_${NIC}_ipv6=\"${IF6ARGS}\"" >>${FSMNT}/etc/rc.conf
234   fi
235
236   # Check if we have a default router to set
237   get_value_from_cfg netSaveDefaultRouter
238   NETROUTE="${VAL}"
239   if [ -n "${NETROUTE}" ]
240   then
241     echo "defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
242   fi
243   get_value_from_cfg netSaveIPv6DefaultRouter
244   NETROUTE="${VAL}"
245   if [ -n "${NETROUTE}" ]
246   then
247     echo "ipv6_defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
248   fi
249
250   # Check if we have a nameserver to enable
251   : > ${FSMNT}/etc/resolv.conf
252   get_value_from_cfg netSaveNameServer
253   NAMESERVER="${VAL}"
254   if [ -n "${NAMESERVER}" ]
255   then
256     echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
257   fi
258   get_value_from_cfg netSaveIPv6NameServer
259   NAMESERVER="${VAL}"
260   if [ -n "${NAMESERVER}" ]
261   then
262     echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
263   fi
264
265 };
266
267 # Function which determines if a nic is active / up
268 is_nic_active()
269 {
270   ifconfig ${1} | grep -q "status: active" 2>/dev/null
271   if [ $? -eq 0 ] ; then
272     return 0
273   else
274     return 1
275   fi
276 };
277
278
279 # Function which detects available nics, and runs DHCP on them until
280 # a success is found
281 enable_auto_dhcp()
282 {
283   # start by getting a list of nics on this system
284   ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
285   while read line
286   do
287     NIC="`echo $line | cut -d ':' -f 1`"
288     DESC="`echo $line | cut -d ':' -f 2`"
289
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
296             export WRKNIC="$NIC"
297             return 0
298           fi
299     fi
300   done < ${TMPDIR}/.niclist 
301
302 };
303
304 # Function which detects available nics, and runs rtsol on them.
305 enable_auto_slaac()
306 {
307
308   # start by getting a list of nics on this system
309   ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
310   ALLNICS=""
311   while read line
312   do
313     NIC="`echo $line | cut -d ':' -f 1`"
314     DESC="`echo $line | cut -d ':' -f 2`"
315
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}"
321     fi
322   done < ${TMPDIR}/.niclist 
323
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
327 }
328
329 # Get the mac address of a target NIC
330 get_nic_mac()
331 {
332   FOUNDMAC="`ifconfig ${1} | grep 'ether' | tr -d '\t' | cut -d ' ' -f 2`"
333   export FOUNDMAC
334 }
335
336 # Function which performs the manual setup of a target nic in the cfg
337 enable_manual_nic()
338 {
339   # Get the target nic
340   NIC="$1"
341
342   # Check that this NIC exists
343   rc_halt "ifconfig ${NIC}"
344
345   get_value_from_cfg netIP
346   NETIP="${VAL}"
347   
348   if [ "$NETIP" = "DHCP" ]
349   then
350     echo_log "Enabling DHCP on $NIC"
351     rc_halt "dhclient ${NIC}"
352     return 0
353   fi
354
355   # If we get here, we have a manual setup, lets do so now
356
357   # IPv4:
358
359   # Set the manual IP
360   if [ -n "${NETIP}" ]
361   then
362     # Check if we have a netmask to set
363     get_value_from_cfg netMask
364     NETMASK="${VAL}"
365     if [ -n "${NETMASK}" ]
366     then
367       rc_halt "ifconfig inet ${NIC} netmask ${NETMASK}"
368     else
369       rc_halt "ifconfig inet ${NIC} ${NETIP}"
370     fi
371   fi
372
373   # Check if we have a default router to set
374   get_value_from_cfg netDefaultRouter
375   NETROUTE="${VAL}"
376   if [ -n "${NETROUTE}" ]
377   then
378     rc_halt "route add -inet default ${NETROUTE}"
379   fi
380
381   # IPv6:
382
383   # Set static IPv6 address
384   get_value_from_cfg netIPv6
385   NETIP="${VAL}"
386   if [ -n ${NETIP} ]
387   then
388       rc_halt "ifconfig inet6 ${NIC} ${NETIP} -ifdisabled up"
389   fi
390
391   # Default router
392   get_value_from_cfg netIPv6DefaultRouter
393   NETROUTE="${VAL}"
394   if [ -n "${NETROUTE}" ]
395   then
396     rc_halt "route add -inet6 default ${NETROUTE}"
397   fi
398
399   # Check if we have a nameserver to enable
400   : >/etc/resolv.conf
401   get_value_from_cfg netNameServer
402   NAMESERVER="${VAL}"
403   if [ -n "${NAMESERVER}" ]
404   then
405     echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf
406   fi
407   get_value_from_cfg netIPv6NameServer
408   NAMESERVER="${VAL}"
409   if [ -n "${NAMESERVER}" ]
410   then
411     echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf
412   fi
413
414 };
415
416
417 # Function which parses the cfg and enables networking per specified
418 start_networking()
419 {
420   # Check if we have any networking requested
421   get_value_from_cfg netDev
422   if [ -z "${VAL}" ]
423   then
424     return 0
425   fi
426
427   NETDEV="${VAL}"
428   if [ "$NETDEV" = "AUTO-DHCP" ]
429   then
430     enable_auto_dhcp
431   elif [ "$NETDEV" = "IPv6-SLAAC" ]
432   then
433     enable_auto_slaac
434   elif [ "$NETDEV" = "AUTO-DHCP-SLAAC" ]
435   then
436     enable_auto_dhcp
437     enable_auto_slaac
438   else
439     enable_manual_nic ${NETDEV}
440   fi
441
442 };
443
444
445 # Function which checks the cfg and enables the specified networking on
446 # the installed system
447 save_networking_install()
448 {
449
450   # Check if we have any networking requested to save
451   get_value_from_cfg netSaveDev
452   if [ -z "${VAL}" ]
453   then
454     return 0
455   fi
456
457   NETDEV="${VAL}"
458   if [ "$NETDEV" = "AUTO-DHCP" ]
459   then
460     save_auto_dhcp
461   elif [ "$NETDEV" = "IPv6-SLAAC" ]
462   then
463     save_auto_slaac
464   elif [ "$NETDEV" = "AUTO-DHCP-SLAAC" ]
465   then
466     save_auto_dhcp
467     save_auto_slaac
468   else
469     save_manual_nic ${NETDEV}
470   fi
471
472 };