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
36create_desktop_nettray()
37{
38  NIC="${1}"
39  echo "#!/usr/bin/env xdg-open
40[Desktop Entry]
41Exec=/usr/local/kde4/bin/pc-nettray ${NIC}
42Icon=network
43StartupNotify=false
44Type=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
50check_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
63get_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
88enable_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	cat ${FSMNT}/etc/rc.conf | grep -q "wlans_${NIC}="
108	if [ $? -ne 0 ] ; then
109          echo "wlans_${NIC}=\"${WLAN}\"" >>${FSMNT}/etc/rc.conf
110	fi
111        echo "ifconfig_${WLAN}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
112        CNIC="${WLAN}"
113        WLANCOUNT=$((WLANCOUNT+1))
114      else
115        echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
116        CNIC="${NIC}"
117      fi
118 
119    done < ${TMPDIR}/.niclist 
120  fi
121};
122
123
124# Function which detects available nics, and enables dhcp on them
125save_auto_dhcp()
126{
127  enable_dhcp_all
128};
129
130# Function which simply enables iPv6 SLAAC on all detected nics
131enable_slaac_all()
132{
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" ]
137  then
138    echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
139    WLANCOUNT="0"
140    while read line
141    do
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."
145      check_is_wifi ${NIC}
146      if [ $? -eq 0 ]
147      then
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
155	fi
156	#echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf
157        echo "ifconfig_${WLAN}_ipv6=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf
158        CNIC="${WLAN}"
159        WLANCOUNT=$((WLANCOUNT+1))
160      else
161	#echo "ifconfig_${NIC}=\"up\"" >>${FSMNT}/etc/rc.conf
162        echo "ifconfig_${NIC}_ipv6=\"inet6 accept_rtadv\"" >>${FSMNT}/etc/rc.conf
163        CNIC="${NIC}"
164      fi
165 
166    done < ${TMPDIR}/.niclist 
167  fi
168
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
173  NAMESERVER="${VAL}"
174  if [ -n "${NAMESERVER}" ]
175  then
176    echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
177  fi
178
179};
180
181
182# Function which detects available nics, and enables IPv6 SLAAC on them
183save_auto_slaac()
184{
185  enable_slaac_all
186};
187
188
189# Function which saves a manual nic setup to the installed system
190save_manual_nic()
191{
192  # Get the target nic
193  NIC="$1"
194
195  get_value_from_cfg netSaveIP_${NIC}
196  NETIP="${VAL}"
197 
198  if [ "$NETIP" = "DHCP" ]
199  then
200    echo_log "Setting $NIC to DHCP on the system."
201    echo "ifconfig_${NIC}=\"DHCP\"" >>${FSMNT}/etc/rc.conf
202    return 0
203  fi
204
205  # If we get here, we have a manual setup, lets do so now
206  IFARGS=""
207  IF6ARGS=""
208
209  # Set the manual IP
210  if [ -n "${NETIP}" ]
211  then
212    IFARGS="inet ${NETIP}"
213
214    # Check if we have a netmask to set
215    get_value_from_cfg netSaveMask_${NIC}
216    NETMASK="${VAL}"
217    if [ -n "${NETMASK}" ]
218    then
219      IFARGS="${IFARGS} netmask ${NETMASK}"
220    fi
221  fi
222
223  get_value_from_cfg netSaveIPv6_${NIC}
224  NETIP6="${VAL}"
225  if [ -n "${NETIP6}" ]
226  then
227    # Make sure we have one inet6 prefix.
228    IF6ARGS=`echo "${NETIP6}" | awk '{ if ("^inet6 ") { print $0; } else
229      { printf "inet6 %s", $0; } }'`
230  fi
231
232  echo "# Auto-Enabled NICs from pc-sysinstall" >>${FSMNT}/etc/rc.conf
233  if [ -n "${IFARGS}" ]
234  then
235    echo "ifconfig_${NIC}=\"${IFARGS}\"" >>${FSMNT}/etc/rc.conf
236  fi
237  if [ -n "${IF6ARGS}" ]
238  then
239    echo "ifconfig_${NIC}_ipv6=\"${IF6ARGS}\"" >>${FSMNT}/etc/rc.conf
240  fi
241
242};
243
244# Function which saves a manual gateway router setup to the installed system
245save_manual_router()
246{
247
248  # Check if we have a default router to set
249  get_value_from_cfg netSaveDefaultRouter
250  NETROUTE="${VAL}"
251  if [ -n "${NETROUTE}" ]
252  then
253    echo "defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
254  fi
255  get_value_from_cfg netSaveIPv6DefaultRouter
256  NETROUTE="${VAL}"
257  if [ -n "${NETROUTE}" ]
258  then
259    echo "ipv6_defaultrouter=\"${NETROUTE}\"" >>${FSMNT}/etc/rc.conf
260  fi
261
262};
263
264save_manual_nameserver()
265{
266  # Check if we have a nameserver to enable
267  : > ${FSMNT}/etc/resolv.conf
268  get_value_from_cfg_with_spaces netSaveNameServer
269  NAMESERVERLIST="${VAL}"
270  if [ ! -z "${NAMESERVERLIST}" ]
271  then
272    for NAMESERVER in ${NAMESERVERLIST}
273    do
274      echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
275    done
276  fi
277
278  get_value_from_cfg_with_spaces netSaveIPv6NameServer
279  NAMESERVERLIST="${VAL}"
280  if [ ! -z "${NAMESERVERLIST}" ]
281  then
282    for NAMESERVER in ${NAMESERVERLIST}
283    do
284      echo "nameserver ${NAMESERVER}" >>${FSMNT}/etc/resolv.conf
285    done
286  fi
287
288};
289
290# Function which determines if a nic is active / up
291is_nic_active()
292{
293  ifconfig ${1} | grep -q "status: active" 2>/dev/null
294  if [ $? -eq 0 ] ; then
295    return 0
296  else
297    return 1
298  fi
299};
300
301
302# Function which detects available nics, and runs DHCP on them until
303# a success is found
304enable_auto_dhcp()
305{
306  # start by getting a list of nics on this system
307  ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
308  while read line
309  do
310    NIC="`echo $line | cut -d ':' -f 1`"
311    DESC="`echo $line | cut -d ':' -f 2`"
312
313    is_nic_active "${NIC}"
314    if [ $? -eq 0 ] ; then
315      echo_log "Trying DHCP on $NIC $DESC"
316      dhclient ${NIC} >/dev/null 2>/dev/null
317      if [ $? -eq 0 ] ; then
318        # Got a valid DHCP IP, we can return now
319	    export WRKNIC="$NIC"
320   	    return 0
321	  fi
322    fi
323  done < ${TMPDIR}/.niclist 
324
325};
326
327# Function which detects available nics, and runs rtsol on them.
328enable_auto_slaac()
329{
330
331  # start by getting a list of nics on this system
332  ${QUERYDIR}/detect-nics.sh > ${TMPDIR}/.niclist
333  ALLNICS=""
334  while read line
335  do
336    NIC="`echo $line | cut -d ':' -f 1`"
337    DESC="`echo $line | cut -d ':' -f 2`"
338
339    is_nic_active "${NIC}"
340    if [ $? -eq 0 ] ; then
341      echo_log "Will try IPv6 SLAAC on $NIC $DESC"
342      ifconfig ${NIC} inet6 -ifdisabled accept_rtadv up
343      ALLNICS="${ALLNICS} ${NIC}"
344    fi
345  done < ${TMPDIR}/.niclist 
346
347  # XXX once we support it in-tree call /sbin/resovconf here.
348  echo_log "Running rtsol on ${ALLNICS}"
349  rtsol -F ${ALLNICS} >/dev/null 2>/dev/null
350}
351
352# Get the mac address of a target NIC
353get_nic_mac()
354{
355  FOUNDMAC="`ifconfig ${1} | grep 'ether' | tr -d '\t' | cut -d ' ' -f 2`"
356  export FOUNDMAC
357}
358
359# Function which performs the manual setup of a target nic in the cfg
360enable_manual_nic()
361{
362  # Get the target nic
363  NIC="$1"
364
365  # Check that this NIC exists
366  rc_halt "ifconfig ${NIC}"
367
368  get_value_from_cfg netIP
369  NETIP="${VAL}"
370  
371  if [ "$NETIP" = "DHCP" ]
372  then
373    echo_log "Enabling DHCP on $NIC"
374    rc_halt "dhclient ${NIC}"
375    return 0
376  fi
377
378  # If we get here, we have a manual setup, lets do so now
379
380  # IPv4:
381
382  # Set the manual IP
383  if [ -n "${NETIP}" ]
384  then
385    # Check if we have a netmask to set
386    get_value_from_cfg netMask
387    NETMASK="${VAL}"
388    if [ -n "${NETMASK}" ]
389    then
390      rc_halt "ifconfig inet ${NIC} netmask ${NETMASK}"
391    else
392      rc_halt "ifconfig inet ${NIC} ${NETIP}"
393    fi
394  fi
395
396  # Check if we have a default router to set
397  get_value_from_cfg netDefaultRouter
398  NETROUTE="${VAL}"
399  if [ -n "${NETROUTE}" ]
400  then
401    rc_halt "route add -inet default ${NETROUTE}"
402  fi
403
404  # IPv6:
405
406  # Set static IPv6 address
407  get_value_from_cfg netIPv6
408  NETIP="${VAL}"
409  if [ -n ${NETIP} ]
410  then
411      rc_halt "ifconfig inet6 ${NIC} ${NETIP} -ifdisabled up"
412  fi
413
414  # Default router
415  get_value_from_cfg netIPv6DefaultRouter
416  NETROUTE="${VAL}"
417  if [ -n "${NETROUTE}" ]
418  then
419    rc_halt "route add -inet6 default ${NETROUTE}"
420  fi
421
422  # Check if we have a nameserver to enable
423  : >/etc/resolv.conf
424  get_value_from_cfg netNameServer
425  NAMESERVER="${VAL}"
426  if [ -n "${NAMESERVER}" ]
427  then
428    echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf
429  fi
430  get_value_from_cfg netIPv6NameServer
431  NAMESERVER="${VAL}"
432  if [ -n "${NAMESERVER}" ]
433  then
434    echo "nameserver ${NAMESERVER}" >>/etc/resolv.conf
435  fi
436
437};
438
439
440# Function which parses the cfg and enables networking per specified
441start_networking()
442{
443  # Check if we have any networking requested
444  get_value_from_cfg netDev
445  if [ -z "${VAL}" ]
446  then
447    return 0
448  fi
449
450  NETDEV="${VAL}"
451  if [ "$NETDEV" = "AUTO-DHCP" ]
452  then
453    enable_auto_dhcp
454  elif [ "$NETDEV" = "IPv6-SLAAC" ]
455  then
456    enable_auto_slaac
457  elif [ "$NETDEV" = "AUTO-DHCP-SLAAC" ]
458  then
459    enable_auto_dhcp
460    enable_auto_slaac
461  else
462    enable_manual_nic ${NETDEV}
463  fi
464
465};
466
467
468# Function which checks the cfg and enables the specified networking on
469# the installed system
470save_networking_install()
471{
472
473  # Check if we have any networking requested to save
474  get_value_from_cfg_with_spaces netSaveDev
475  if [ -z "${VAL}" ]
476  then
477    return 0
478  fi
479
480  NETDEVLIST="${VAL}"
481  if [ "$NETDEVLIST" = "AUTO-DHCP" ]
482  then
483    save_auto_dhcp
484  elif [ "$NETDEVLIST" = "IPv6-SLAAC" ]
485  then
486    save_auto_slaac
487  elif [ "$NETDEVLIST" = "AUTO-DHCP-SLAAC" ]
488  then
489    save_auto_dhcp
490    save_auto_slaac
491  else
492    for NETDEV in ${NETDEVLIST}
493    do
494      save_manual_nic ${NETDEV}
495    done
496    save_manual_router
497    save_manual_nameserver
498  fi
499
500};
501