Home | History | Annotate | Line # | Download | only in rc.d
network revision 1.1
      1  1.1  lukem #!/bin/sh
      2  1.1  lukem #
      3  1.1  lukem # $NetBSD: network,v 1.1 2000/03/10 11:53:24 lukem Exp $
      4  1.1  lukem #
      5  1.1  lukem 
      6  1.1  lukem # PROVIDE: network
      7  1.1  lukem # REQUIRE: root mountcritlocal tty ipfilter
      8  1.1  lukem 
      9  1.1  lukem . /etc/rc.subr
     10  1.1  lukem . /etc/rc.conf
     11  1.1  lukem 
     12  1.1  lukem name="network"
     13  1.1  lukem start_cmd="network_start"
     14  1.1  lukem stop_cmd="network_stop"
     15  1.1  lukem 
     16  1.1  lukem network_start()
     17  1.1  lukem {
     18  1.1  lukem 	# set hostname, turn on network
     19  1.1  lukem 	#
     20  1.1  lukem 	echo "Starting network."
     21  1.1  lukem 
     22  1.1  lukem 	# If $hostname is set, use it for my Internet name,
     23  1.1  lukem 	# otherwise use /etc/myname
     24  1.1  lukem 	#
     25  1.1  lukem 	if [ -z "$hostname" -a -f /etc/myname ]; then
     26  1.1  lukem 		hostname=`cat /etc/myname`
     27  1.1  lukem 	fi
     28  1.1  lukem 	if [ -n "$hostname" ]; then
     29  1.1  lukem 		echo "Hostname: $hostname"
     30  1.1  lukem 		hostname $hostname
     31  1.1  lukem 	else
     32  1.1  lukem 		warn "\$hostname not set."
     33  1.1  lukem 	fi
     34  1.1  lukem 
     35  1.1  lukem 	# Check $domainname first, then /etc/defaultdomain,
     36  1.1  lukem 	# for NIS/YP domain name
     37  1.1  lukem 	#
     38  1.1  lukem 	if [ -z "$domainname" -a -f /etc/defaultdomain ]; then
     39  1.1  lukem 		domainname=`cat /etc/defaultdomain`
     40  1.1  lukem 	fi
     41  1.1  lukem 	if [ -n "$domainname" ]; then
     42  1.1  lukem 		echo "NIS domainname: $domainname"
     43  1.1  lukem 		domainname $domainname
     44  1.1  lukem 	fi
     45  1.1  lukem 
     46  1.1  lukem 	# Flush all routes just to make sure it is clean
     47  1.1  lukem 	if checkyesno flushroutes; then
     48  1.1  lukem 		route -n flush
     49  1.1  lukem 	fi
     50  1.1  lukem 
     51  1.1  lukem 	# Set the address for the first loopback interface, so that the
     52  1.1  lukem 	# auto-route from a newly configured interface's address to lo0
     53  1.1  lukem 	# works correctly.
     54  1.1  lukem 	#
     55  1.1  lukem 	# NOTE: obscure networking problems may occur if lo0 isn't configured...
     56  1.1  lukem 	#
     57  1.1  lukem 	ifconfig lo0 inet 127.0.0.1
     58  1.1  lukem 
     59  1.1  lukem 	# Configure all of the network interfaces listed in $net_interfaces;
     60  1.1  lukem 	# if $auto_ifconfig is YES, grab all interfaces from ifconfig.
     61  1.1  lukem 	# In the following, "xxN" stands in for interface names, like "le0".
     62  1.1  lukem 	# For any interfaces that has an $ifconfig_xxN variable associated,
     63  1.1  lukem 	# we do "ifconfig xxN $ifconfig_xxN".
     64  1.1  lukem 	# If there is no such variable, we take the contents of the file
     65  1.1  lukem 	# /etc/ifconfig.xxN, and run "ifconfig xxN" repeatedly, using each
     66  1.1  lukem 	# line of the file as the arguments for a seperate "ifconfig"
     67  1.1  lukem 	# invocation.
     68  1.1  lukem 	#
     69  1.1  lukem 	# In order to configure an interface reasonably, you at the very least
     70  1.1  lukem 	# need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
     71  1.1  lukem 	# and probably a netmask (as in "netmask 0xffffffe0"). You will
     72  1.1  lukem 	# frequently need to specify a media type, as in "media UTP", for
     73  1.1  lukem 	# interface cards with multiple media connections that do not
     74  1.1  lukem 	# autoconfigure. See the ifconfig manual page for details.
     75  1.1  lukem 	#
     76  1.1  lukem 	# Note that /etc/ifconfig.xxN takes multiple lines.  The following
     77  1.1  lukem 	# configuration is possible:
     78  1.1  lukem 	#	inet 10.1.1.1 netmask 0xffffff00
     79  1.1  lukem 	#	inet 10.1.1.2 netmask 0xffffff00 alias
     80  1.1  lukem 	#	inet6 fec0::1 prefixlen 64 alias
     81  1.1  lukem 	#
     82  1.1  lukem 	if [ "$net_interfaces" != NO ]; then
     83  1.1  lukem 		if checkyesno auto_ifconfig; then
     84  1.1  lukem 			tmp="`ifconfig -l`"
     85  1.1  lukem 		else
     86  1.1  lukem 			tmp="$net_interfaces"
     87  1.1  lukem 		fi
     88  1.1  lukem 		echo -n 'Configuring network interfaces:'
     89  1.1  lukem 		for int in $tmp; do
     90  1.1  lukem 			eval `echo 'args=$ifconfig_'$int`
     91  1.1  lukem 			if [ -n "$args" ]; then
     92  1.1  lukem 				echo -n " $int"
     93  1.1  lukem 				ifconfig $int $args
     94  1.1  lukem 			elif [ -f /etc/ifconfig.$int ]; then
     95  1.1  lukem 				echo -n " $int"
     96  1.1  lukem 				(while read args; do
     97  1.1  lukem 					if [ -n "`eval echo '$args'`" ] ; then
     98  1.1  lukem 						ifconfig $int $args
     99  1.1  lukem 					fi
    100  1.1  lukem 				done) < /etc/ifconfig.$int
    101  1.1  lukem 			else
    102  1.1  lukem 				if ! checkyesno auto_ifconfig; then
    103  1.1  lukem 					echo
    104  1.1  lukem 					warn \
    105  1.1  lukem 			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
    106  1.1  lukem 					warn "interface $int not configured."
    107  1.1  lukem 				fi
    108  1.1  lukem 				continue
    109  1.1  lukem 			fi
    110  1.1  lukem 			configured_interfaces="$configured_interfaces $int"
    111  1.1  lukem 		done
    112  1.1  lukem 		echo "."
    113  1.1  lukem 	fi
    114  1.1  lukem 
    115  1.1  lukem 	# Check $defaultroute, then /etc/mygate, for the name of my gateway
    116  1.1  lukem 	# host. That name must be in /etc/hosts.
    117  1.1  lukem 	#
    118  1.1  lukem 	if [ -z "$defaultroute" -a -f /etc/mygate ]; then
    119  1.1  lukem 		defaultroute=`cat /etc/mygate`
    120  1.1  lukem 	fi
    121  1.1  lukem 	if [ -n "$defaultroute" ]; then
    122  1.1  lukem 		route add default $defaultroute
    123  1.1  lukem 	fi
    124  1.1  lukem 
    125  1.1  lukem 	# Check if each configured interface xxN has an $ifaliases_xxN variable
    126  1.1  lukem 	# associated, then configure additional IP addresses for that interface.
    127  1.1  lukem 	# The variable contains a list of "address netmask" pairs, with
    128  1.1  lukem 	# "netmask" set to "-" if the interface default netmask is to be used.
    129  1.1  lukem 	#
    130  1.1  lukem 	# Note that $ifaliases_xxN works only with certain configurations and
    131  1.1  lukem 	# considered not recommended.  Use /etc/ifconfig.xxN if possible.
    132  1.1  lukem 	# 
    133  1.1  lukem 	#
    134  1.1  lukem 	if [ -n "$configured_interfaces" ]; then
    135  1.1  lukem 		echo "Adding interface aliases:"
    136  1.1  lukem 		done_aliases_message=yes
    137  1.1  lukem 	fi
    138  1.1  lukem 	for int in $configured_interfaces; do
    139  1.1  lukem 		eval `echo 'args=$ifaliases_'$int`
    140  1.1  lukem 		if [ -n "$args" ]; then
    141  1.1  lukem 			set -- $args
    142  1.1  lukem 			while [ $# -ge 2 ]; do
    143  1.1  lukem 				addr=$1 ; net=$2 ; shift 2
    144  1.1  lukem 				if [ "$net" = "-" ]; then
    145  1.1  lukem 					ifconfig $int inet alias $addr
    146  1.1  lukem 				else
    147  1.1  lukem 					ifconfig $int inet alias $addr \
    148  1.1  lukem 					    netmask $net
    149  1.1  lukem 				fi
    150  1.1  lukem 				# Use loopback, not the wire
    151  1.1  lukem 				route add $addr 127.0.0.1
    152  1.1  lukem 			done
    153  1.1  lukem 		fi
    154  1.1  lukem 	done
    155  1.1  lukem 
    156  1.1  lukem 	# /etc/ifaliases, if it exists, contains the names of additional IP
    157  1.1  lukem 	# addresses for each interface. It is formatted as a series of lines
    158  1.1  lukem 	# that contain
    159  1.1  lukem 	#	address interface netmask
    160  1.1  lukem 	#
    161  1.1  lukem 	# Note that /etc/ifaliases works only with certain cases only and its
    162  1.1  lukem 	# use is not recommended.  Use /etc/ifconfig.xxN instead.
    163  1.1  lukem 	#
    164  1.1  lukem 	#
    165  1.1  lukem 	if [ -f /etc/ifaliases ]; then
    166  1.1  lukem 	(
    167  1.1  lukem 		if [ "$done_aliases_message" != yes ]; then
    168  1.1  lukem 			echo "Adding interface aliases:"
    169  1.1  lukem 		fi
    170  1.1  lukem 		while read addr int net; do
    171  1.1  lukem 			if [ -z "$net" ]; then
    172  1.1  lukem 				ifconfig $int inet alias $addr
    173  1.1  lukem 			else
    174  1.1  lukem 				ifconfig $int inet alias $addr netmask $net
    175  1.1  lukem 			fi
    176  1.1  lukem 			# use loopback, not the wire
    177  1.1  lukem 			route add $addr 127.0.0.1
    178  1.1  lukem 		done
    179  1.1  lukem 	) < /etc/ifaliases
    180  1.1  lukem 	fi
    181  1.1  lukem 
    182  1.1  lukem 	# IPv6
    183  1.1  lukem 	# Note that manual configuration can be done in the above, using
    184  1.1  lukem 	# ifconfig.
    185  1.1  lukem 	#
    186  1.1  lukem 	if ifconfig lo0 inet6 >/dev/null 2>&1; then
    187  1.1  lukem 		# We have IPv6 support in kernel.
    188  1.1  lukem 
    189  1.1  lukem 		# disallow scoped unicast dest without outgoing scope
    190  1.1  lukem 		# identifiers.
    191  1.1  lukem 		#
    192  1.1  lukem 		route add -inet6 fe80:: -prefixlen 10 ::1 -reject
    193  1.1  lukem 		route add -inet6 fc80:: -prefixlen 10 ::1 -reject
    194  1.1  lukem 
    195  1.1  lukem 		# disallow "internal" addresses to appear on the wire.
    196  1.1  lukem 		#
    197  1.1  lukem 		route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
    198  1.1  lukem 		route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
    199  1.1  lukem 
    200  1.1  lukem 		sysctl -w net.inet6.ip6.forwarding=0 >/dev/null
    201  1.1  lukem 		sysctl -w net.inet6.ip6.accept_rtadv=0 >/dev/null
    202  1.1  lukem 
    203  1.1  lukem 		# backward compatibility
    204  1.1  lukem 		#
    205  1.1  lukem 		if [ -z "$ip6mode" -a -n "$ip6forwarding" ]; then
    206  1.1  lukem 			warn 'Please migrate to newer rc.conf' \
    207  1.1  lukem 			    '(use ip6mode, not ip6forwarding)'
    208  1.1  lukem 			if checkyesno ip6forwarding; then
    209  1.1  lukem 				ip6mode=router
    210  1.1  lukem 			else
    211  1.1  lukem 				if checkyesno rtsol; then
    212  1.1  lukem 					ip6mode=autohost
    213  1.1  lukem 				else
    214  1.1  lukem 					ip6mode=host
    215  1.1  lukem 				fi
    216  1.1  lukem 			fi
    217  1.1  lukem 		fi
    218  1.1  lukem 
    219  1.1  lukem 		case $ip6mode in
    220  1.1  lukem 		router)
    221  1.1  lukem 			echo 'IPv6 mode: router'
    222  1.1  lukem 			sysctl -w net.inet6.ip6.forwarding=1 >/dev/null
    223  1.1  lukem 			;;
    224  1.1  lukem 
    225  1.1  lukem 		autohost)
    226  1.1  lukem 			echo 'IPv6 mode: autoconfigured host'
    227  1.1  lukem 			sysctl -w net.inet6.ip6.accept_rtadv=1 >/dev/null
    228  1.1  lukem 			if [ -n "$ip6defaultif" ]; then
    229  1.1  lukem 				ndp -I $ip6defaultif
    230  1.1  lukem 			fi
    231  1.1  lukem 			;;
    232  1.1  lukem 
    233  1.1  lukem 		host)	
    234  1.1  lukem 			echo 'IPv6 mode: host'
    235  1.1  lukem 			if [ -n "$ip6defaultif" ]; then
    236  1.1  lukem 				ndp -I $ip6defaultif
    237  1.1  lukem 			fi
    238  1.1  lukem 			;;
    239  1.1  lukem 
    240  1.1  lukem 		*)	echo 'WARNING: invalid value in ip6mode'
    241  1.1  lukem 			;;
    242  1.1  lukem 
    243  1.1  lukem 		esac
    244  1.1  lukem 
    245  1.1  lukem 		if checkyesno rtsol; then
    246  1.1  lukem 			if [ "$ip6mode" = "autohost" ]; then
    247  1.1  lukem 				echo 'Sending router solicitation...'
    248  1.1  lukem 				rtsol $rtsol_flags
    249  1.1  lukem 			else
    250  1.1  lukem 				echo
    251  1.1  lukem 				warn \
    252  1.1  lukem 			    "ip6mode must be set to 'autohost' to use rtsol."
    253  1.1  lukem 			fi
    254  1.1  lukem 		fi
    255  1.1  lukem 
    256  1.1  lukem 		# wait till DAD is completed. always invoke it in case if are
    257  1.1  lukem 		# configured manually by ifconfig
    258  1.1  lukem 		#
    259  1.1  lukem 		dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
    260  1.1  lukem 		sleep $dadcount
    261  1.1  lukem 		sleep 1
    262  1.1  lukem 	fi
    263  1.1  lukem 
    264  1.1  lukem 	# XXX this must die
    265  1.1  lukem 	if [ -s /etc/netstart.local ]; then
    266  1.1  lukem 		sh /etc/netstart.local start
    267  1.1  lukem 	fi
    268  1.1  lukem }
    269  1.1  lukem 
    270  1.1  lukem network_stop()
    271  1.1  lukem {
    272  1.1  lukem 	echo "Stopping network."
    273  1.1  lukem 
    274  1.1  lukem 	# XXX this must die
    275  1.1  lukem 	if [ -s /etc/netstart.local ]; then
    276  1.1  lukem 		sh /etc/netstart.local stop
    277  1.1  lukem 	fi
    278  1.1  lukem 
    279  1.1  lukem 	rtsolpid=`check_process rtsol`
    280  1.1  lukem 	if [ -n "$rtsolpid" ]; then
    281  1.1  lukem 		echo "Stopping rtsol (IPv6 router solicitation daemon)."
    282  1.1  lukem 		kill -TERM $rtsolpid
    283  1.1  lukem 	fi
    284  1.1  lukem 
    285  1.1  lukem 	echo "Deleting aliases."
    286  1.1  lukem 	if [ -f /etc/ifaliases ]; then
    287  1.1  lukem 	(
    288  1.1  lukem 		while read addr int net; do
    289  1.1  lukem 			ifconfig $int inet delete $addr
    290  1.1  lukem 		done
    291  1.1  lukem 	) < /etc/ifaliases
    292  1.1  lukem 	fi
    293  1.1  lukem 
    294  1.1  lukem 	for int in $configured_interfaces; do
    295  1.1  lukem 		eval `echo 'args=$ifaliases_'$int`
    296  1.1  lukem 		if [ -n "$args" ]; then
    297  1.1  lukem 			set -- $args
    298  1.1  lukem 			while [ $# -ge 2 ]; do
    299  1.1  lukem 				addr=$1 ; net=$2 ; shift 2
    300  1.1  lukem 				ifconfig $int inet delete $addr
    301  1.1  lukem 			done
    302  1.1  lukem 		fi
    303  1.1  lukem 	done
    304  1.1  lukem 
    305  1.1  lukem 	# down interfaces
    306  1.1  lukem 	#
    307  1.1  lukem 	echo -n 'Downing network interfaces:'
    308  1.1  lukem 	if [ "$net_interfaces" != NO ]; then
    309  1.1  lukem 		if checkyesno auto_ifconfig; then
    310  1.1  lukem 			tmp="`ifconfig -l`"
    311  1.1  lukem 		else
    312  1.1  lukem 			tmp="$net_interfaces"
    313  1.1  lukem 		fi
    314  1.1  lukem 		for int in $tmp; do
    315  1.1  lukem 			eval `echo 'args=$ifconfig_'$int`
    316  1.1  lukem 			if [ -n "$args" || -f /etc/ifconfig.$int ]; then
    317  1.1  lukem 				echo -n " $int"
    318  1.1  lukem 				ifconfig $int down
    319  1.1  lukem 			fi
    320  1.1  lukem 		done
    321  1.1  lukem 		echo "."
    322  1.1  lukem 	fi
    323  1.1  lukem 
    324  1.1  lukem 	# flush routes
    325  1.1  lukem 	#
    326  1.1  lukem 	route -n flush
    327  1.1  lukem 
    328  1.1  lukem }
    329  1.1  lukem 
    330  1.1  lukem run_rc_command "$1"
    331