network revision 1.58
11.1Slukem#!/bin/sh 21.1Slukem# 31.58Sroy# $NetBSD: network,v 1.58 2009/05/14 15:33:36 roy Exp $ 41.1Slukem# 51.1Slukem 61.1Slukem# PROVIDE: network 71.19Slukem# REQUIRE: ipfilter ipsec mountcritlocal root tty sysctl 81.34Sthorpej# BEFORE: NETWORKING 91.1Slukem 101.45Smycroft$_rc_subr_loaded . /etc/rc.subr 111.1Slukem 121.1Slukemname="network" 131.1Slukemstart_cmd="network_start" 141.14Slukemstop_cmd="network_stop" 151.1Slukem 161.54Sapbnl=' 171.54Sapb' # a newline 181.54Sapb 191.1Slukemnetwork_start() 201.1Slukem{ 211.1Slukem # set hostname, turn on network 221.1Slukem # 231.1Slukem echo "Starting network." 241.1Slukem 251.1Slukem # If $hostname is set, use it for my Internet name, 261.1Slukem # otherwise use /etc/myname 271.1Slukem # 281.20Snisimura if [ -z "$hostname" ] && [ -f /etc/myname ]; then 291.46Schristos hostname=$(cat /etc/myname) 301.1Slukem fi 311.1Slukem if [ -n "$hostname" ]; then 321.1Slukem echo "Hostname: $hostname" 331.1Slukem hostname $hostname 341.1Slukem else 351.8Sthorpej # Don't warn about it if we're going to run 361.8Sthorpej # DHCP later, as we will probably get the 371.8Sthorpej # hostname at that time. 381.8Sthorpej # 391.58Sroy if ! checkyesno dhclient && ! checkyesno dhcpcd && \ 401.58Sroy [ -z "$(hostname)" ] 411.58Sroy then 421.8Sthorpej warn "\$hostname not set." 431.8Sthorpej fi 441.1Slukem fi 451.1Slukem 461.1Slukem # Check $domainname first, then /etc/defaultdomain, 471.1Slukem # for NIS/YP domain name 481.1Slukem # 491.20Snisimura if [ -z "$domainname" ] && [ -f /etc/defaultdomain ]; then 501.46Schristos domainname=$(cat /etc/defaultdomain) 511.1Slukem fi 521.1Slukem if [ -n "$domainname" ]; then 531.1Slukem echo "NIS domainname: $domainname" 541.1Slukem domainname $domainname 551.1Slukem fi 561.1Slukem 571.1Slukem # Flush all routes just to make sure it is clean 581.1Slukem if checkyesno flushroutes; then 591.53Sreed /sbin/route -qn flush 601.1Slukem fi 611.1Slukem 621.1Slukem # Set the address for the first loopback interface, so that the 631.1Slukem # auto-route from a newly configured interface's address to lo0 641.1Slukem # works correctly. 651.1Slukem # 661.32Slukem # NOTE: obscure networking problems will occur if lo0 isn't configured. 671.1Slukem # 681.53Sreed /sbin/ifconfig lo0 inet 127.0.0.1 691.10Sitojun 701.31Sitojun # According to RFC1122, 127.0.0.0/8 must not leave the node. 711.10Sitojun # 721.53Sreed /sbin/route -q add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject 731.1Slukem 741.30Sitojun # IPv6 routing setups, and host/router mode selection. 751.30Sitojun # 761.53Sreed if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then 771.30Sitojun # We have IPv6 support in kernel. 781.30Sitojun 791.30Sitojun # disallow link-local unicast dest without outgoing scope 801.30Sitojun # identifiers. 811.30Sitojun # 821.53Sreed /sbin/route -q add -inet6 fe80:: -prefixlen 10 ::1 -reject 831.30Sitojun 841.50Srpaulo # disallow the use of the RFC3849 documentation address 851.50Srpaulo # 861.53Sreed /sbin/route -q add -inet6 2001:db8:: -prefixlen 32 ::1 -reject 871.50Srpaulo 881.50Srpaulo # IPv6 site-local scoped address prefix (fec0::/10) 891.50Srpaulo # has been deprecated by RFC3879. 901.30Sitojun # 911.50Srpaulo if [ -n "$ip6sitelocal" ]; then 921.50Srpaulo warn "\$ip6sitelocal is no longer valid" 931.30Sitojun fi 941.30Sitojun 951.30Sitojun # disallow "internal" addresses to appear on the wire. 961.30Sitojun # 971.53Sreed /sbin/route -q add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject 981.30Sitojun 991.30Sitojun # disallow packets to malicious IPv4 compatible prefix 1001.30Sitojun # 1011.53Sreed /sbin/route -q add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject 1021.53Sreed /sbin/route -q add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject 1031.53Sreed /sbin/route -q add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject 1041.53Sreed /sbin/route -q add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject 1051.30Sitojun 1061.30Sitojun # disallow packets to malicious 6to4 prefix 1071.30Sitojun # 1081.53Sreed /sbin/route -q add -inet6 2002:e000:: -prefixlen 20 ::1 -reject 1091.53Sreed /sbin/route -q add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject 1101.53Sreed /sbin/route -q add -inet6 2002:0000:: -prefixlen 24 ::1 -reject 1111.53Sreed /sbin/route -q add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject 1121.30Sitojun 1131.30Sitojun # Completely disallow packets to IPv4 compatible prefix. 1141.30Sitojun # This may conflict with RFC1933 under following circumstances: 1151.30Sitojun # (1) An IPv6-only KAME node tries to originate packets to IPv4 1161.51Sreed # compatible destination. The KAME node has no IPv4 1171.30Sitojun # compatible support. Under RFC1933, it should transmit 1181.30Sitojun # native IPv6 packets toward IPv4 compatible destination, 1191.30Sitojun # hoping it would reach a router that forwards the packet 1201.30Sitojun # toward auto-tunnel interface. 1211.30Sitojun # (2) An IPv6-only node originates a packet to IPv4 compatible 1221.30Sitojun # destination. A KAME node is acting as an IPv6 router, and 1231.30Sitojun # asked to forward it. 1241.30Sitojun # Due to rare use of IPv4 compatible address, and security 1251.30Sitojun # issues with it, we disable it by default. 1261.30Sitojun # 1271.53Sreed /sbin/route -q add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject 1281.30Sitojun 1291.53Sreed /sbin/sysctl -qw net.inet6.ip6.forwarding=0 1301.53Sreed /sbin/sysctl -qw net.inet6.ip6.accept_rtadv=0 1311.30Sitojun 1321.30Sitojun case $ip6mode in 1331.30Sitojun router) 1341.30Sitojun echo 'IPv6 mode: router' 1351.53Sreed /sbin/sysctl -qw net.inet6.ip6.forwarding=1 1361.50Srpaulo 1371.50Srpaulo # disallow unique-local unicast forwarding without 1381.50Srpaulo # explicit configuration. 1391.50Srpaulo if ! checkyesno ip6uniquelocal; then 1401.53Sreed /sbin/route -q add -inet6 fc00:: -prefixlen 7 \ 1411.50Srpaulo ::1 -reject 1421.50Srpaulo fi 1431.30Sitojun ;; 1441.30Sitojun 1451.30Sitojun autohost) 1461.30Sitojun echo 'IPv6 mode: autoconfigured host' 1471.53Sreed /sbin/sysctl -qw net.inet6.ip6.accept_rtadv=1 1481.30Sitojun ;; 1491.30Sitojun 1501.30Sitojun host) 1511.30Sitojun echo 'IPv6 mode: host' 1521.30Sitojun ;; 1531.30Sitojun 1541.36Slukem *) warn "invalid \$ip6mode value "\"$ip6mode\" 1551.30Sitojun ;; 1561.30Sitojun 1571.30Sitojun esac 1581.30Sitojun fi 1591.30Sitojun 1601.1Slukem # Configure all of the network interfaces listed in $net_interfaces; 1611.1Slukem # if $auto_ifconfig is YES, grab all interfaces from ifconfig. 1621.1Slukem # In the following, "xxN" stands in for interface names, like "le0". 1631.54Sapb # 1641.54Sapb # For any interfaces that has an $ifconfig_xxN variable 1651.54Sapb # associated, we break it into lines using ';' as a separator, 1661.54Sapb # then process it just like the contents of an /etc/ifconfig.xxN 1671.54Sapb # file. 1681.54Sapb # 1691.54Sapb # For each line from the $ifconfig_xxN variable or the 1701.54Sapb # /etc/ifconfig.xxN file, we ignore comments and blank lines, 1711.54Sapb # treat lines beginning with "!" as commands to execute, treat 1721.54Sapb # "dhcp" as a special case to invoke dhcpcd, and for any other 1731.54Sapb # line we run "ifconfig xxN", using each line of the file as the 1741.54Sapb # arguments for a separate "ifconfig" invocation. 1751.1Slukem # 1761.1Slukem # In order to configure an interface reasonably, you at the very least 1771.1Slukem # need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"), 1781.1Slukem # and probably a netmask (as in "netmask 0xffffffe0"). You will 1791.1Slukem # frequently need to specify a media type, as in "media UTP", for 1801.1Slukem # interface cards with multiple media connections that do not 1811.1Slukem # autoconfigure. See the ifconfig manual page for details. 1821.1Slukem # 1831.1Slukem # Note that /etc/ifconfig.xxN takes multiple lines. The following 1841.1Slukem # configuration is possible: 1851.1Slukem # inet 10.1.1.1 netmask 0xffffff00 1861.1Slukem # inet 10.1.1.2 netmask 0xffffff00 alias 1871.50Srpaulo # inet6 2001:db8::1 prefixlen 64 alias 1881.1Slukem # 1891.29Sitojun # You can put shell script fragment into /etc/ifconfig.xxN by 1901.29Sitojun # starting a line with "!". Refer to ifconfig.if(5) for details. 1911.29Sitojun # 1921.1Slukem if [ "$net_interfaces" != NO ]; then 1931.1Slukem if checkyesno auto_ifconfig; then 1941.53Sreed tmp=$(/sbin/ifconfig -l) 1951.53Sreed for cloner in $(/sbin/ifconfig -C 2>/dev/null); do 1961.25Swiz for int in /etc/ifconfig.${cloner}[0-9]*; do 1971.23Snisimura [ ! -f $int ] && break 1981.21Slukem tmp="$tmp ${int##*.}" 1991.15Sthorpej done 2001.15Sthorpej done 2011.1Slukem else 2021.1Slukem tmp="$net_interfaces" 2031.1Slukem fi 2041.1Slukem echo -n 'Configuring network interfaces:' 2051.1Slukem for int in $tmp; do 2061.54Sapb eval argslist=\$ifconfig_$int 2071.54Sapb 2081.54Sapb # Skip interfaces that do not have explicit 2091.54Sapb # configuration information. If auto_ifconfig is 2101.54Sapb # false then also warn about such interfaces. 2111.54Sapb # 2121.54Sapb if [ -z "$argslist" ] && ! [ -f /etc/ifconfig.$int ] 2131.54Sapb then 2141.1Slukem if ! checkyesno auto_ifconfig; then 2151.1Slukem echo 2161.1Slukem warn \ 2171.1Slukem "/etc/ifconfig.$int missing and ifconfig_$int not set;" 2181.1Slukem warn "interface $int not configured." 2191.1Slukem fi 2201.1Slukem continue 2211.1Slukem fi 2221.54Sapb 2231.54Sapb echo -n " $int" 2241.54Sapb 2251.54Sapb # Create the interface if necessary. 2261.54Sapb # If the interface did not exist before, 2271.54Sapb # then also resync ipf(4). 2281.54Sapb # 2291.54Sapb if /sbin/ifconfig $int create 2>/dev/null && \ 2301.54Sapb checkyesno ipfilter; then 2311.54Sapb /sbin/ipf -y >/dev/null 2321.54Sapb fi 2331.54Sapb 2341.54Sapb # If $ifconfig_xxN is empty, then use 2351.54Sapb # /etc/ifconfig.xxN, which we know exists due to 2361.54Sapb # an earlier test. 2371.54Sapb # 2381.54Sapb # If $ifconfig_xxN is non-empty and contains a 2391.54Sapb # newline, then just use it as is. (This allows 2401.54Sapb # semicolons through unmolested.) 2411.54Sapb # 2421.54Sapb # If $ifconfig_xxN is non-empty and does not 2431.54Sapb # contain a newline, then convert all semicolons 2441.54Sapb # to newlines. 2451.54Sapb # 2461.54Sapb case "$argslist" in 2471.54Sapb '') 2481.54Sapb cat /etc/ifconfig.$int 2491.54Sapb ;; 2501.54Sapb *"${nl}"*) 2511.54Sapb echo "$argslist" 2521.54Sapb ;; 2531.54Sapb *) 2541.54Sapb ( 2551.54Sapb set -o noglob 2561.54Sapb IFS=';'; set -- $argslist 2571.54Sapb #echo >&2 "[$#] [$1] [$2] [$3] [$4]" 2581.54Sapb IFS="$nl"; echo "$*" 2591.54Sapb ) 2601.54Sapb ;; 2611.54Sapb esac | 2621.54Sapb while read -r args; do 2631.54Sapb case "$args" in 2641.54Sapb ''|"#"*|create) 2651.54Sapb ;; 2661.54Sapb "!"*) 2671.54Sapb # Run arbitrary command in a subshell. 2681.54Sapb ( eval "${args#*!}" ) 2691.54Sapb ;; 2701.54Sapb dhcp) 2711.58Sroy if ! checkyesno dhcpcd; then 2721.58Sroy /sbin/dhcpcd -n \ 2731.58Sroy ${dhcpcd_flags} $int 2741.58Sroy fi 2751.54Sapb ;; 2761.54Sapb *) 2771.54Sapb # Pass args to ifconfig. Note 2781.54Sapb # that args may contain embedded 2791.54Sapb # shell metacharacters, such as 2801.57Schristos # "ssid 'foo;*>bar'". We eval 2811.57Schristos # one more time so that things 2821.57Schristos # like ssid "Columbia University" work. 2831.54Sapb ( 2841.54Sapb set -o noglob 2851.57Schristos eval set -- $args 2861.54Sapb #echo >&2 "[$#] [$1] [$2] [$3]" 2871.54Sapb /sbin/ifconfig $int "$@" 2881.54Sapb ) 2891.54Sapb ;; 2901.54Sapb esac 2911.54Sapb done 2921.1Slukem configured_interfaces="$configured_interfaces $int" 2931.1Slukem done 2941.1Slukem echo "." 2951.1Slukem fi 2961.1Slukem 2971.48Scjs echo -n "Adding interface aliases:" 2981.48Scjs 2991.1Slukem # Check if each configured interface xxN has an $ifaliases_xxN variable 3001.1Slukem # associated, then configure additional IP addresses for that interface. 3011.1Slukem # The variable contains a list of "address netmask" pairs, with 3021.1Slukem # "netmask" set to "-" if the interface default netmask is to be used. 3031.1Slukem # 3041.54Sapb # Note that $ifaliases_xxN works only in certain cases and its 3051.54Sapb # use is not recommended. Use /etc/ifconfig.xxN or multiple 3061.54Sapb # commands in $ifconfig_xxN instead. 3071.1Slukem # 3081.48Scjs for int in lo0 $configured_interfaces; do 3091.20Snisimura eval args=\$ifaliases_$int 3101.1Slukem if [ -n "$args" ]; then 3111.1Slukem set -- $args 3121.1Slukem while [ $# -ge 2 ]; do 3131.1Slukem addr=$1 ; net=$2 ; shift 2 3141.1Slukem if [ "$net" = "-" ]; then 3151.16Sjdolecek # for compatibility only, obsolete 3161.53Sreed /sbin/ifconfig $int inet alias $addr 3171.1Slukem else 3181.53Sreed /sbin/ifconfig $int inet alias $addr \ 3191.1Slukem netmask $net 3201.1Slukem fi 3211.48Scjs echo -n " $int:$addr" 3221.1Slukem done 3231.1Slukem fi 3241.1Slukem done 3251.1Slukem 3261.1Slukem # /etc/ifaliases, if it exists, contains the names of additional IP 3271.1Slukem # addresses for each interface. It is formatted as a series of lines 3281.1Slukem # that contain 3291.1Slukem # address interface netmask 3301.1Slukem # 3311.54Sapb # Note that /etc/ifaliases works only in certain cases and its 3321.54Sapb # use is not recommended. Use /etc/ifconfig.xxN or multiple 3331.54Sapb # commands in $ifconfig_xxN instead. 3341.1Slukem # 3351.1Slukem if [ -f /etc/ifaliases ]; then 3361.1Slukem while read addr int net; do 3371.1Slukem if [ -z "$net" ]; then 3381.16Sjdolecek # for compatibility only, obsolete 3391.53Sreed /sbin/ifconfig $int inet alias $addr 3401.1Slukem else 3411.53Sreed /sbin/ifconfig $int inet alias $addr netmask $net 3421.1Slukem fi 3431.20Snisimura done < /etc/ifaliases 3441.1Slukem fi 3451.1Slukem 3461.56Sapb echo "." # for "Adding interface aliases:" 3471.56Sapb 3481.56Sapb # Check $defaultroute, then /etc/mygate, for the name or address 3491.56Sapb # of my IPv4 gateway host. If using a name, that name must be in 3501.56Sapb # /etc/hosts. 3511.56Sapb # 3521.56Sapb if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then 3531.56Sapb defaultroute=$(cat /etc/mygate) 3541.56Sapb fi 3551.56Sapb if [ -n "$defaultroute" ]; then 3561.56Sapb /sbin/route add default $defaultroute 3571.56Sapb fi 3581.56Sapb 3591.56Sapb # Check $defaultroute6, then /etc/mygate6, for the name or address 3601.56Sapb # of my IPv6 gateway host. If using a name, that name must be in 3611.56Sapb # /etc/hosts. Note that the gateway host address must be a link-local 3621.56Sapb # address if it is not using an stf* interface. 3631.56Sapb # 3641.56Sapb if [ -z "$defaultroute6" ] && [ -f /etc/mygate6 ]; then 3651.56Sapb defaultroute6=$(cat /etc/mygate6) 3661.56Sapb fi 3671.56Sapb if [ -n "$defaultroute6" ]; then 3681.56Sapb if [ "$ip6mode" = "autohost" ]; then 3691.56Sapb echo 3701.56Sapb warn \ 3711.56Sapb "ip6mode is set to 'autohost' and a v6 default route is also set." 3721.56Sapb fi 3731.56Sapb /sbin/route add -inet6 default $defaultroute6 3741.56Sapb fi 3751.48Scjs 3761.30Sitojun # IPv6 interface autoconfiguration. 3771.1Slukem # 3781.53Sreed if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then 3791.17Sitojun # wait till DAD is completed. always invoke it in case 3801.17Sitojun # if are configured manually by ifconfig 3811.17Sitojun # 3821.53Sreed dadcount=$(/sbin/sysctl -n net.inet6.ip6.dad_count 2>/dev/null) 3831.17Sitojun sleep $dadcount 3841.17Sitojun sleep 1 3851.17Sitojun 3861.1Slukem if checkyesno rtsol; then 3871.1Slukem if [ "$ip6mode" = "autohost" ]; then 3881.1Slukem echo 'Sending router solicitation...' 3891.53Sreed /sbin/rtsol $rtsol_flags 3901.1Slukem else 3911.1Slukem echo 3921.1Slukem warn \ 3931.1Slukem "ip6mode must be set to 'autohost' to use rtsol." 3941.1Slukem fi 3951.17Sitojun 3961.18Sitojun # wait till DAD is completed, for global addresses 3971.18Sitojun # configured by router advert message. 3981.17Sitojun # 3991.17Sitojun sleep $dadcount 4001.17Sitojun sleep 1 4011.1Slukem fi 4021.42Schristos fi 4031.42Schristos 4041.1Slukem # XXX this must die 4051.1Slukem if [ -s /etc/netstart.local ]; then 4061.1Slukem sh /etc/netstart.local start 4071.1Slukem fi 4081.1Slukem} 4091.1Slukem 4101.1Slukemnetwork_stop() 4111.1Slukem{ 4121.1Slukem echo "Stopping network." 4131.1Slukem 4141.1Slukem # XXX this must die 4151.1Slukem if [ -s /etc/netstart.local ]; then 4161.1Slukem sh /etc/netstart.local stop 4171.1Slukem fi 4181.1Slukem 4191.1Slukem echo "Deleting aliases." 4201.1Slukem if [ -f /etc/ifaliases ]; then 4211.1Slukem while read addr int net; do 4221.53Sreed /sbin/ifconfig $int inet delete $addr 4231.20Snisimura done < /etc/ifaliases 4241.1Slukem fi 4251.1Slukem 4261.53Sreed for int in $(/sbin/ifconfig -lu); do 4271.20Snisimura eval args=\$ifaliases_$int 4281.1Slukem if [ -n "$args" ]; then 4291.1Slukem set -- $args 4301.1Slukem while [ $# -ge 2 ]; do 4311.1Slukem addr=$1 ; net=$2 ; shift 2 4321.53Sreed /sbin/ifconfig $int inet delete $addr 4331.1Slukem done 4341.1Slukem fi 4351.1Slukem done 4361.1Slukem 4371.1Slukem # down interfaces 4381.1Slukem # 4391.1Slukem echo -n 'Downing network interfaces:' 4401.1Slukem if [ "$net_interfaces" != NO ]; then 4411.1Slukem if checkyesno auto_ifconfig; then 4421.53Sreed tmp=$(/sbin/ifconfig -l) 4431.1Slukem else 4441.1Slukem tmp="$net_interfaces" 4451.1Slukem fi 4461.1Slukem for int in $tmp; do 4471.20Snisimura eval args=\$ifconfig_$int 4481.2Sveego if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then 4491.1Slukem echo -n " $int" 4501.58Sroy if [ -f /var/run/dhcpcd-$int.pid ]; then 4511.58Sroy /sbin/dhcpcd -k $int 2> /dev/null 4521.58Sroy fi 4531.53Sreed /sbin/ifconfig $int down 4541.53Sreed if /sbin/ifconfig $int destroy 2>/dev/null && \ 4551.39Stron checkyesno ipfilter; then 4561.39Stron # resync ipf(4) 4571.53Sreed /sbin/ipf -y >/dev/null 4581.39Stron fi 4591.1Slukem fi 4601.1Slukem done 4611.1Slukem echo "." 4621.1Slukem fi 4631.1Slukem 4641.1Slukem # flush routes 4651.1Slukem # 4661.53Sreed /sbin/route -qn flush 4671.38Stron 4681.1Slukem} 4691.1Slukem 4701.47Slukemload_rc_config $name 4711.47Slukemload_rc_config_var dhclient dhclient 4721.58Sroyload_rc_config_var dhcpcd dhcpcd 4731.47Slukemload_rc_config_var ipfilter ipfilter 4741.1Slukemrun_rc_command "$1" 475