]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pc-sysinstall/backend/functions-networking.sh
Upgrade of base gcc and libstdc++ to the last GPLv2-licensed revision
[FreeBSD/FreeBSD.git] / usr.sbin / pc-sysinstall / backend / functions-networking.sh
1 #!/bin/sh
2 #-
3 # Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 #    notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 #    notice, this list of conditions and the following disclaimer in the
12 #    documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 # SUCH DAMAGE.
25 #
26 # $FreeBSD$
27
28 # Functions which perform our networking setup
29
30 # Function which creates a kde4 .desktop file for the PC-BSD net tray
31 create_desktop_nettray()
32 {
33   NIC="${1}"
34   echo "#!/usr/bin/env xdg-open
35 [Desktop Entry]
36 Exec=/usr/local/kde4/bin/pc-nettray ${NIC}
37 Icon=network
38 StartupNotify=false
39 Type=Application" > ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop
40   chmod 744 ${FSMNT}/usr/share/skel/.kde4/Autostart/tray-${NIC}.desktop
41
42 };
43
44 # Function which checks is a nic is wifi or not
45 check_is_wifi()
46 {
47   NIC="$1"
48   ifconfig ${NIC} | grep -q "802.11" 2>/dev/null
49   if [ $? -eq 0 ]
50   then
51     return 0
52   else 
53     return 1
54   fi
55 };
56
57 # Function to get the first available wired nic, used for setup
58 get_first_wired_nic()
59 {
60   rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
61   # start by getting a list of nics on this system
62   ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
63   if [ -e "${TMPDIR}/.niclist" ]
64   then
65     while read line
66     do
67       NIC="`echo $line | cut -d ':' -f 1`"
68       check_is_wifi ${NIC}
69       if [ $? -ne 0 ]
70       then
71         export VAL="${NIC}"
72         return
73       fi
74     done < ${TMPDIR}/.niclist
75   fi
76
77   export VAL=""
78   return
79 };
80
81
82 # Function which simply enables plain dhcp on all detected nics
83 enable_dhcp_all()
84 {
85   rm ${TMPDIR}/.niclist >/dev/null 2>/dev/null
86   # start by getting a list of nics on this system
87   ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
88   if [ -e "${TMPDIR}/.niclist" ]
89   then
90     echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
91     WLANCOUNT="0"
92     while read line
93     do
94       NIC="`echo $line | cut -d ':' -f 1`"
95       DESC="`echo $line | cut -d ':' -f 2`"
96       echo_log "Setting $NIC to DHCP on the system."
97       check_is_wifi ${NIC}
98       if [ $? -eq 0 ]
99       then
100         # We have a wifi device, setup a wlan* entry for it
101         WLAN="wlan${WLANCOUNT}"
102         echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf
103         echo "ifconfig_${WLAN}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
104         CNIC="${WLAN}"
105         WLANCOUNT=$((WLANCOUNT+1))
106       else
107         echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
108         CNIC="${NIC}"
109       fi
110  
111     done < ${TMPDIR}/.niclist 
112   fi
113 };
114
115
116 # Function which detects available nics, and enables dhcp on them
117 save_auto_dhcp()
118 {
119   enable_dhcp_all
120 };
121
122
123 # Function which saves a manual nic setup to the installed system
124 save_manual_nic()
125 {
126   # Get the target nic
127   NIC="$1"
128
129   get_value_from_cfg netSaveIP
130   NETIP="${VAL}"
131  
132   if [ "$NETIP" = "DHCP" ]
133   then
134     echo_log "Setting $NIC to DHCP on the system."
135     echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
136     return 0
137   fi
138
139   # If we get here, we have a manual setup, lets do so now
140
141   # Set the manual IP
142   IFARGS="inet ${NETIP}"
143
144   # Check if we have a netmask to set
145   get_value_from_cfg netSaveMask
146   NETMASK="${VAL}"
147   if [ -n "${NETMASK}" ]
148   then
149     IFARGS="${IFARGS} netmask ${NETMASK}"
150   fi
151
152
153   echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
154   echo "ifconfig_${NIC}=\"${IFARGS}\"" >>${FSMNT}/etc/rc.conf
155
156   # Check if we have a default router to set
157   get_value_from_cfg netSaveDefaultRouter
158   NETROUTE="${VAL}"
159   if [ -n "${NETROUTE}" ]
160   then
161     echo "defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
162   fi
163
164   # Check if we have a nameserver to enable
165   get_value_from_cfg netSaveNameServer
166   NAMESERVER="${VAL}"
167   if [ -n "${NAMESERVER}" ]
168   then
169     echo "nameserver ${NAMESERVER}" >${FSMNT}/etc/resolv.conf
170   fi
171  
172 };
173
174 # Function which determines if a nic is active / up
175 is_nic_active()
176 {
177   ifconfig ${1} | grep -q "status: active" 2>/dev/null
178   if [ $? -eq 0 ] ; then
179     return 0
180   else
181     return 1
182   fi
183 };
184
185
186 # Function which detects available nics, and runs DHCP on them until
187 # a success is found
188 enable_auto_dhcp()
189 {
190   # start by getting a list of nics on this system
191   ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
192   while read line
193   do
194     NIC="`echo $line | cut -d ':' -f 1`"
195     DESC="`echo $line | cut -d ':' -f 2`"
196
197     is_nic_active "${NIC}"
198     if [ $? -eq 0 ] ; then
199       echo_log "Trying DHCP on $NIC $DESC"
200       dhclient ${NIC} >/dev/null 2>/dev/null
201       if [ $? -eq 0 ] ; then
202         # Got a valid DHCP IP, we can return now
203             export WRKNIC="$NIC"
204             return 0
205           fi
206     fi
207   done < ${TMPDIR}/.niclist 
208
209 };
210
211 # Get the mac address of a target NIC
212 get_nic_mac()
213 {
214   FOUNDMAC="`ifconfig ${1} | grep 'ether' | tr -d '\t' | cut -d ' ' -f 2`"
215   export FOUNDMAC
216 }
217
218 # Function which performs the manual setup of a target nic in the cfg
219 enable_manual_nic()
220 {
221   # Get the target nic
222   NIC="$1"
223
224   # Check that this NIC exists
225   rc_halt "ifconfig ${NIC}"
226
227   get_value_from_cfg netIP
228   NETIP="${VAL}"
229   
230   if [ "$NETIP" = "DHCP" ]
231   then
232     echo_log "Enabling DHCP on $NIC"
233     rc_halt "dhclient ${NIC}"
234     return 0
235   fi
236
237   # If we get here, we have a manual setup, lets do so now
238
239   # Set the manual IP
240   rc_halt "ifconfig ${NIC} ${NETIP}"
241
242   # Check if we have a netmask to set
243   get_value_from_cfg netMask
244   NETMASK="${VAL}"
245   if [ -n "${NETMASK}" ]
246   then
247     rc_halt "ifconfig ${NIC} netmask ${NETMASK}"
248   fi
249
250   # Check if we have a default router to set
251   get_value_from_cfg netDefaultRouter
252   NETROUTE="${VAL}"
253   if [ -n "${NETROUTE}" ]
254   then
255     rc_halt "route add default ${NETROUTE}"
256   fi
257
258   # Check if we have a nameserver to enable
259   get_value_from_cfg netNameServer
260   NAMESERVER="${VAL}"
261   if [ -n "${NAMESERVER}" ]
262   then
263     echo "nameserver ${NAMESERVER}" >/etc/resolv.conf
264   fi
265   
266   
267 };
268
269
270 # Function which parses the cfg and enables networking per specified
271 start_networking()
272 {
273   # Check if we have any networking requested
274   get_value_from_cfg netDev
275   if [ -z "${VAL}" ]
276   then
277     return 0
278   fi
279
280   NETDEV="${VAL}"
281   if [ "$NETDEV" = "AUTO-DHCP" ]
282   then
283     enable_auto_dhcp
284   else
285     enable_manual_nic ${NETDEV}
286   fi
287
288 };
289
290
291 # Function which checks the cfg and enables the specified networking on
292 # the installed system
293 save_networking_install()
294 {
295
296   # Check if we have any networking requested to save
297   get_value_from_cfg netSaveDev
298   if [ -z "${VAL}" ]
299   then
300     return 0
301   fi
302
303   NETDEV="${VAL}"
304   if [ "$NETDEV" = "AUTO-DHCP" ]
305   then
306     save_auto_dhcp
307   else
308     save_manual_nic ${NETDEV}
309   fi
310
311 };