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