Hi guys,
I'd like to commit the following patch, which is phase 1 of the network script
rewrite. It creates a new file /etc/rc_network.subr that contains some generic
network subroutines. I've split up our network1 script into
1) hostname - Sets the hostname and NIS domain name
2) netif - Sets up network interfaces, modulo dhcp interfaces
3) dhclient - Configures the dhcp interfaces (previously unused NetBSD script)
After this goes in I'll start working on phase 2, which will include the bits to
bring up/take down individual interfaces or protocols. I'll happily entertain
suggestions on what kinds of functionality to include.
FYI: filesystem mounting and network interface configuration differ the most
between FreeBSD and NetBSD, so maintaining compatibility doesn't
matter in this case.
Cheers.
--
Mike Makonnen | GPG-KEY:
http://www.identd.net/~mtm/mtm.asc
mtm@... | Fingerprint: D228 1A6F C64E 120A A1C9 A3AA DAE1 E2AF DBCC 68B9
Index: etc/rc_network.subr
===================================================================
RCS file: etc/rc_network.subr
diff -N etc/rc_network.subr
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ etc/rc_network.subr 19 Jan 2003 06:37:04 -0000
@@ -0,0 +1,214 @@
+#
+# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+
+#
+# Subroutines commonly used from network startup scripts.
+# Requires that rc.conf be loaded first.
+#
+
+# ifconfig_up if
+# Evaluate ifconfig(8) arguments for interface $if and
+# run ifconfig(8) with those arguments. It returns 0 if
+# arguments were found and executed or 1 if the interface
+# had no arguments.
+#
+ifconfig_up()
+{
+ eval ifconfig_args=\$ifconfig_$1
+ if [ -n "${ifconfig_args}" ]; then
+ ifconfig $1 ${ifconfig_args}
+ return 0
+ fi
+ return 1
+}
+
+# ifalias_up if
+# Configure aliases for network interface $if.
+# It returns 0 if at least one alias was configured or
+# 1 if there were none.
+#
+ifalias_up()
+{
+ _ret=1
+ alias=0
+ while : ; do
+ eval ifconfig_args=\$ifconfig_$1_alias${alias}
+ if [ -n "${ifconfig_args}" ]; then
+ ifconfig $1 ${ifconfig_args} alias
+ alias=$((${alias} + 1))
+ _ret=0
+ else
+ break
+ fi
+ done
+ return $_ret
+}
+
+# ifscript_up if
+# Evaluate a startup script for the $if interface.
+# It returns 0 if a script was found and processed or
+# 1 if no script was found.
+#
+ifscript_up()
+{
+ if [ -r /etc/start_if.$1 ]; then
+ . /etc/start_if.$1
+ return 0
+ fi
+ return 1
+}
+
+# Create cloneable interfaces.
+#
+clone_up()
+{
+ _prefix=
+ _list=
+ for ifn in ${cloned_interfaces}; do
+ ifconfig ${ifn} create
+ if $? ; then
+ _list="${_list}${_prefix}${ifn}"
+ [ -z "$_prefix" ] && _prefix=' '
+ fi
+ done
+ debug "Cloned: ${_list}"
+}
+
+# Destroy cloned interfaces. Destroyed interfaces are echoed
+# to standard output.
+#
+clone_down()
+{
+ _prefix=
+ _list=
+ for ifn in ${cloned_interfaces}; do
+ ifconfig ${ifn} destroy
+ if $? ; then
+ _list="${_list}${_prefix}${ifn}"
+ [ -z "$_prefix" ] && _prefix=' '
+ fi
+ done
+ debug "Destroyed clones: ${_list}"
+}
+
+gif_up() {
+ case ${gif_interfaces} in
+ [Nn][Oo] | '')
+ ;;
+ *)
+ for i in ${gif_interfaces}; do
+ eval peers=\$gifconfig_$i
+ case ${peers} in
+ '')
+ continue
+ ;;
+ *)
+ ifconfig $i create >/dev/null 2>&1
+ ifconfig $i tunnel ${peers}
+ ifconfig $i up
+ ;;
+ esac
+ done
+ ;;
+ esac
+}
+
+#
+# ipx_up ifn
+# Configure any IPX addresses for interface $ifn. Returns 0 if IPX
+# arguments were found and configured; returns 1 otherwise.
+#
+ipx_up()
+{
+ ifn="$1"
+ eval ifconfig_args=\$ifconfig_${ifn}_ipx
+ if [ -n "${ifconfig_args}" ]; then
+ ifconfig ${ifn} ${ifconfig_args}
+ return 0
+ fi
+ return 1
+}
+
+#
+# list_net_interfaces type
+# List all network interfaces. The type of interface returned
+# can be controlled by the type argument. The type
+# argument can be any of the following:
+# nodhcp - all interfaces, excluding DHCP configured interfaces
+# dhcp - list only DHCP configured interfaces
+# If no argument is specified all network interfaces are output.
+# Note that the list always includes cloned interfaces.
+#
+list_net_interfaces()
+{
+ type=$1
+
+ # Get a list of ALL the interfaces
+ #
+ case ${network_interfaces} in
+ [Aa][Uu][Tt][Oo])
+ _tmplist="`ifconfig -l`"
+ ;;
+ *)
+ _tmplist="${network_interfaces}"
+ ;;
+ esac
+ _tmplist="${_tmplist} ${cloned_interfaces}"
+
+ if [ -z "$type" ]; then
+ echo $_tmplist
+ return 0
+ fi
+
+ # Separate out dhcp and non-dhcp intefraces
+ #
+ _aprefix=
+ _brefix=
+ for _if in ${_tmplist} ; do
+ eval _ifarg="\$ifconfig_${_if}"
+ case "$_ifarg" in
+ [Dd][Hh][Cc][Pp])
+ _dhcplist="${_dhcplist}${_aprefix}${_if}"
+ [ -z "$_aprefix" ] && _aprefix=' '
+ ;;
+ ''|*)
+ _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
+ [ -z "$_bprefix" ] && _bprefix=' '
+ ;;
+ esac
+ done
+
+ case "$type" in
+ nodhcp)
+ echo $_nodhcplist
+ ;;
+ dhcp)
+ echo $_dhcplist
+ ;;
+ esac
+ return 0
+}
Index: etc/rc.d/Makefile
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/Makefile,v
retrieving revision 1.10
diff -u -r1.10 Makefile
--- etc/rc.d/Makefile 22 Dec 2002 22:25:53 -0000 1.10
+++ etc/rc.d/Makefile 19 Jan 2003 06:45:49 -0000
@@ -5,11 +5,11 @@
FILES= DAEMON LOGIN NETWORKING SERVERS abi accounting addswap adjkerntz amd \
apm apmd atm1 atm2.sh atm3.sh archdep bgfsck bootparams ccd cleanvar \
- cleartmp cron devd devdb devfs diskless dmesg dumpon fsck inetd \
- initdiskless initrandom ip6fw ipfilter ipfw ipmon ipnat ipsec \
- ipxrouted isdnd kadmind kerberos keyserv ldconfig local \
+ cleartmp cron devd devdb devfs dhclient diskless dmesg dumpon fsck \
+ hostname inetd initdiskless initrandom ip6fw ipfilter ipfw ipmon ipnat \
+ ipsec ipxrouted isdnd kadmind kerberos keyserv ldconfig local \
localdaemons lomac lpd motd mountcritlocal mountcritremote \
- mountd moused mroute6d mrouted msgs named network1 network2 \
+ mountd moused mroute6d mrouted msgs named netif network2 \
network3 network_ipv6 nfsclient nfsd nfslocking nfsserver ntpd \
ntpdate othermta pccard pcvt ppp-user pppoed pwcheck quota random \
rarpd rcconf.sh root route6d routed rpcbind rtadvd rwho savecore \
Index: etc/rc.d/NETWORKING
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/NETWORKING,v
retrieving revision 1.2
diff -u -r1.2 NETWORKING
--- etc/rc.d/NETWORKING 12 Oct 2002 13:49:21 -0000 1.2
+++ etc/rc.d/NETWORKING 10 Jan 2003 00:04:30 -0000
@@ -5,7 +5,7 @@
#
# PROVIDE: NETWORKING NETWORK
-# REQUIRE: network dhclient altqd network1 network2 network_ipv6 ppp-user
+# REQUIRE: network dhclient altqd netif network2 network_ipv6 ppp-user
# KEYWORD: FreeBSD NetBSD
# This is a dummy dependency, for services which require networking
Index: etc/rc.d/adjkerntz
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/adjkerntz,v
retrieving revision 1.1
diff -u -r1.1 adjkerntz
--- etc/rc.d/adjkerntz 13 Jun 2002 22:14:36 -0000 1.1
+++ etc/rc.d/adjkerntz 10 Jan 2003 00:04:58 -0000
@@ -5,7 +5,7 @@
# PROVIDE: adjkerntz
# REQUIRE: diskless mountcritlocal random
-# BEFORE: network1
+# BEFORE: netif
# KEYWORD: FreeBSD
. /etc/rc.subr
Index: etc/rc.d/atm1
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/atm1,v
retrieving revision 1.11
diff -u -r1.11 atm1
--- etc/rc.d/atm1 30 Sep 2002 08:01:43 -0000 1.11
+++ etc/rc.d/atm1 10 Jan 2003 00:05:42 -0000
@@ -29,7 +29,7 @@
# PROVIDE: atm1
# REQUIRE: root
-# BEFORE: network1
+# BEFORE: netif
# KEYWORD: FreeBSD
. /etc/rc.subr
Index: etc/rc.d/atm2.sh
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/atm2.sh,v
retrieving revision 1.11
diff -u -r1.11 atm2.sh
--- etc/rc.d/atm2.sh 12 Oct 2002 10:31:31 -0000 1.11
+++ etc/rc.d/atm2.sh 10 Jan 2003 00:05:59 -0000
@@ -28,7 +28,7 @@
#
# PROVIDE: atm2
-# REQUIRE: atm1 network1
+# REQUIRE: atm1 netif
# BEFORE: network2
# KEYWORD: FreeBSD
Index: etc/rc.d/cleanvar
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/cleanvar,v
retrieving revision 1.2
diff -u -r1.2 cleanvar
--- etc/rc.d/cleanvar 12 Oct 2002 10:31:31 -0000 1.2
+++ etc/rc.d/cleanvar 10 Jan 2003 00:06:27 -0000
@@ -5,7 +5,7 @@
# PROVIDE: cleanvar
# REQUIRE: adjkerntz mountcritlocal
-# BEFORE: network1
+# BEFORE: netif
# KEYWORD: FreeBSD
purgedir()
Index: etc/rc.d/dhclient
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/dhclient,v
retrieving revision 1.2
diff -u -r1.2 dhclient
--- etc/rc.d/dhclient 13 Jun 2002 22:14:36 -0000 1.2
+++ etc/rc.d/dhclient 17 Jan 2003 05:30:16 -0000
@@ -5,19 +5,49 @@
#
# PROVIDE: dhclient
-# REQUIRE: network mountcritlocal
+# REQUIRE: network netif mountcritlocal
# BEFORE: NETWORKING
+# KEYWORD: FreeBSD NetBSD
#
# Note that there no syslog logging of dhclient messages at boot because
# dhclient needs to start before services that syslog depends upon do.
#
. /etc/rc.subr
+. /etc/rc_network.subr
name="dhclient"
-rcvar=$name
command="/sbin/${name}"
pidfile="/var/run/${name}.pid"
+case "${OSTYPE}" in
+FreeBSD)
+ rcvar=
+ start_precmd="dhclient_prestart"
+ start_postcmd="dhclient_poststart"
+ ;;
+NetBSD)
+ rcvar=$name
+ ;;
+esac
+
+dhclient_prestart()
+{
+ dhcp_list="`list_net_interfaces dhcp`"
+ if [ -z "$dhcp_list" ]; then
+ return 1
+ fi
+ rc_flags="${rc_flags} ${dhcp_flags} ${dhcp_list}"
+ return 0
+}
+
+dhclient_poststart()
+{
+ for ifn in ${dhcp_list}; do
+ ifalias_up ${ifn}
+ ipx_up ${ifn}
+ ifconfig ${ifn}
+ done
+}
load_rc_config $name
run_rc_command "$1"
Index: etc/rc.d/hostname
===================================================================
RCS file: etc/rc.d/hostname
diff -N etc/rc.d/hostname
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ etc/rc.d/hostname 17 Jan 2003 03:46:55 -0000
@@ -0,0 +1,109 @@
+#!/bin/sh
+#
+# $FreeBSD$
+#
+
+# PROVIDE: hostname
+# REQUIRE: mountcritlocal sysctl tty
+# BEFORE: netif
+# KEYWORD: FreeBSD
+
+. /etc/rc.subr
+
+name="hostname"
+start_cmd="hostname_start"
+stop_cmd=":"
+
+convert_host_conf()
+{
+ host_conf=$1; shift;
+ nsswitch_conf=$1; shift;
+ awk ' \
+ /^[:blank:]*#/ { next } \
+ /(hosts|local|file)/ { nsswitch[c] = "files"; c++; next } \
+ /(dns|bind)/ { nsswitch[c] = "dns"; c++; next } \
+ /nis/ { nsswitch[c] = "nis"; c++; next } \
+ { printf "Warning: unrecognized line [%s]", $0 > "/dev/stderr" } \
+ END { \
+ printf "hosts: "; \
+ for (i in nsswitch) printf "%s ", nsswitch[i]; \
+ printf "\n"; \
+ }' < $host_conf > $nsswitch_conf
+}
+
+generate_host_conf()
+{
+ nsswitch_conf=$1; shift;
+ host_conf=$1; shift;
+
+ awk '
+BEGIN {
+ xlat["files"] = "hosts";
+ xlat["dns"] = "bind";
+ xlat["nis"] = "nis";
+ cont = 0;
+}
+sub(/^[\t ]*hosts:/, "") || cont {
+ if (!cont)
+ srcs = ""
+ sub(/#.*/, "")
+ gsub(/[][]/, " & ")
+ cont = sub(/\\$/, "")
+ srcs = srcs " " $0
+}
+END {
+ print "# Auto-generated from nsswitch.conf, do not edit"
+ ns = split(srcs, s)
+ for (n = 1; n <= ns; ++n) {
+ if (s[n] in xlat)
+ print xlat[s[n]]
+ }
+}
+' <$nsswitch_conf >$host_conf
+}
+
+hostname_start()
+{
+ # set hostname
+ #
+ echo -n "Setting hostname:"
+
+ # Generate host.conf for compatibility
+ #
+ if [ -f "/etc/nsswitch.conf" ]; then
+ echo -n ' host.conf'
+ generate_host_conf /etc/nsswitch.conf /etc/host.conf
+ fi
+
+ # Convert host.conf to nsswitch.conf if necessary
+ #
+ if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
+ echo ''
+ echo 'Warning: /etc/host.conf is no longer used'
+ echo ' /etc/nsswitch.conf will be created for you'
+ convert_host_conf /etc/host.conf /etc/nsswitch.conf
+ fi
+
+ # Set the host name if it is not already set
+ #
+ if [ -z "`hostname -s`" ]; then
+ hostname ${hostname}
+ echo -n " `hostname`"
+ fi
+
+ # Set the domainname if we're using NIS
+ #
+ case ${nisdomainname} in
+ [Nn][Oo]|'')
+ ;;
+ *)
+ domainname ${nisdomainname}
+ echo -n " nisdomain=`domainname`"
+ ;;
+ esac
+
+ echo '.'
+}
+
+load_rc_config $name
+run_rc_command "$1"
Index: etc/rc.d/netif
===================================================================
RCS file: etc/rc.d/netif
diff -N etc/rc.d/netif
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ etc/rc.d/netif 17 Jan 2003 03:49:14 -0000
@@ -0,0 +1,76 @@
+#!/bin/sh
+#
+# $FreeBSD$
+#
+
+# PROVIDE: netif
+# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl tty
+# KEYWORD: FreeBSD
+
+. /etc/rc.subr
+. /etc/rc_network.subr
+
+name="network"
+start_cmd="network_start"
+stop_cmd="network_stop"
+cloneup_cmd="clone_up"
+clonedown_cmd="clone_down"
+extra_commands="cloneup clonedown"
+
+network_start()
+{
+ # Create cloned interfaces
+ clone_up
+
+ # Create IPv6<-->IPv4 tunnels
+ gif_up
+
+ # Get a list of network interfaces. Do not include dhcp interfaces.
+ _ifn_list="`list_net_interfaces nodhcp`"
+
+ # Setup the supplied network interfaces including startup
+ # scripts, if they exist.
+ #
+ for ifn in ${_ifn_list}; do
+ _up=`ifconfig ${ifn} | head -1 | grep -v LOOPBACK | grep UP,`
+ if [ "$_up" != "" ]; then
+ # Interface is already up, so ignore it, but
+ # make sure to configure any aliases or ipx first.
+ ifalias_up ${ifn} && eval showstat_$ifn=1
+ ipx_up ${ifn} && eval showstat_$ifn=1
+ continue
+ fi
+
+ ifscript_up ${ifn} && eval showstat_$ifn=1
+
+ ifconfig_up ${ifn} && eval showstat_$ifn=1
+
+ ifalias_up ${ifn} && eval showstat_$ifn=1
+
+ ipx_up ${ifn} && eval showstat_$ifn=1
+ done
+
+ # Display interfaces configured by this script
+ #
+ for ifn in ${_ifn_list}; do
+ eval showstat=\$showstat_${ifn}
+ if [ ! -z ${showstat} ]; then
+ ifconfig ${ifn}
+ fi
+ done
+
+ # Resync ipfilter
+ /etc/rc.d/ipfilter resync
+}
+
+network_stop()
+{
+ echo -n "Stopping network:"
+
+ # flush routes
+ route -n flush
+ echo '.'
+}
+
+load_rc_config $name
+run_rc_command "$1"
Index: etc/rc.d/network1
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/network1,v
retrieving revision 1.144
diff -u -r1.144 network1
--- etc/rc.d/network1 13 Dec 2002 23:36:31 -0000 1.144
+++ etc/rc.d/network1 9 Jan 2003 16:08:55 -0000
@@ -5,7 +5,7 @@
# PROVIDE: network1
# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl tty
-# KEYWORD: FreeBSD
+# KEYWORD: FreeBSD nostart
. /etc/rc.subr
Index: etc/rc.d/network2
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/network2,v
retrieving revision 1.135
diff -u -r1.135 network2
--- etc/rc.d/network2 13 Jun 2002 22:14:36 -0000 1.135
+++ etc/rc.d/network2 10 Jan 2003 00:07:36 -0000
@@ -6,7 +6,7 @@
#
# PROVIDE: network2
-# REQUIRE: network1 ppp-user
+# REQUIRE: netif ppp-user
# KEYWORD: FreeBSD
. /etc/rc.subr
Index: etc/rc.d/ppp-user
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/ppp-user,v
retrieving revision 1.2
diff -u -r1.2 ppp-user
--- etc/rc.d/ppp-user 12 Oct 2002 10:31:31 -0000 1.2
+++ etc/rc.d/ppp-user 10 Jan 2003 00:07:54 -0000
@@ -4,7 +4,7 @@
#
# PROVIDE: ppp-user
-# REQUIRE: network1
+# REQUIRE: netif
# KEYWORD: FreeBSD
. /etc/rc.subr
Index: etc/rc.d/random
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/random,v
retrieving revision 1.2
diff -u -r1.2 random
--- etc/rc.d/random 12 Oct 2002 10:31:31 -0000 1.2
+++ etc/rc.d/random 10 Jan 2003 00:08:16 -0000
@@ -5,7 +5,7 @@
# PROVIDE: random
# REQUIRE: diskless mountcritlocal initrandom
-# BEFORE: network1
+# BEFORE: netif
# KEYWORD: FreeBSD shutdown
. /etc/rc.subr
Index: etc/rc.d/sppp
===================================================================
RCS file: /home/ncvs/src/etc/rc.d/sppp,v
retrieving revision 1.1
diff -u -r1.1 sppp
--- etc/rc.d/sppp 13 Jun 2002 22:14:36 -0000 1.1
+++ etc/rc.d/sppp 10 Jan 2003 00:08:44 -0000
@@ -5,7 +5,7 @@
# PROVIDE: sppp
# REQUIRE: root
-# BEFORE: network1
+# BEFORE: netif
# KEYWORD: FreeBSD
. /etc/rc.subr