network revision 1.63
11.1Slukem#!/bin/sh 21.1Slukem# 31.63Schristos# $NetBSD: network,v 1.63 2013/04/20 18:24:18 christos 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.63Schristosintmissing() { 201.63Schristos local int="$1" 211.63Schristos shift 221.63Schristos for i 231.63Schristos do 241.63Schristos if [ "$int" = "$i" ]; then 251.63Schristos return 1 261.63Schristos fi 271.63Schristos done 281.63Schristos return 0 291.63Schristos} 301.63Schristos 311.1Slukemnetwork_start() 321.1Slukem{ 331.1Slukem # set hostname, turn on network 341.1Slukem # 351.1Slukem echo "Starting network." 361.1Slukem 371.1Slukem # If $hostname is set, use it for my Internet name, 381.1Slukem # otherwise use /etc/myname 391.1Slukem # 401.20Snisimura if [ -z "$hostname" ] && [ -f /etc/myname ]; then 411.46Schristos hostname=$(cat /etc/myname) 421.1Slukem fi 431.1Slukem if [ -n "$hostname" ]; then 441.1Slukem echo "Hostname: $hostname" 451.1Slukem hostname $hostname 461.1Slukem else 471.8Sthorpej # Don't warn about it if we're going to run 481.8Sthorpej # DHCP later, as we will probably get the 491.8Sthorpej # hostname at that time. 501.8Sthorpej # 511.58Sroy if ! checkyesno dhclient && ! checkyesno dhcpcd && \ 521.58Sroy [ -z "$(hostname)" ] 531.58Sroy then 541.8Sthorpej warn "\$hostname not set." 551.8Sthorpej fi 561.1Slukem fi 571.1Slukem 581.1Slukem # Check $domainname first, then /etc/defaultdomain, 591.1Slukem # for NIS/YP domain name 601.1Slukem # 611.20Snisimura if [ -z "$domainname" ] && [ -f /etc/defaultdomain ]; then 621.46Schristos domainname=$(cat /etc/defaultdomain) 631.1Slukem fi 641.1Slukem if [ -n "$domainname" ]; then 651.1Slukem echo "NIS domainname: $domainname" 661.1Slukem domainname $domainname 671.1Slukem fi 681.1Slukem 691.1Slukem # Flush all routes just to make sure it is clean 701.1Slukem if checkyesno flushroutes; then 711.53Sreed /sbin/route -qn flush 721.1Slukem fi 731.1Slukem 741.1Slukem # Set the address for the first loopback interface, so that the 751.1Slukem # auto-route from a newly configured interface's address to lo0 761.1Slukem # works correctly. 771.1Slukem # 781.32Slukem # NOTE: obscure networking problems will occur if lo0 isn't configured. 791.1Slukem # 801.53Sreed /sbin/ifconfig lo0 inet 127.0.0.1 811.10Sitojun 821.31Sitojun # According to RFC1122, 127.0.0.0/8 must not leave the node. 831.10Sitojun # 841.53Sreed /sbin/route -q add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject 851.1Slukem 861.30Sitojun # IPv6 routing setups, and host/router mode selection. 871.30Sitojun # 881.53Sreed if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then 891.30Sitojun # We have IPv6 support in kernel. 901.30Sitojun 911.30Sitojun # disallow link-local unicast dest without outgoing scope 921.30Sitojun # identifiers. 931.30Sitojun # 941.53Sreed /sbin/route -q add -inet6 fe80:: -prefixlen 10 ::1 -reject 951.30Sitojun 961.50Srpaulo # disallow the use of the RFC3849 documentation address 971.50Srpaulo # 981.53Sreed /sbin/route -q add -inet6 2001:db8:: -prefixlen 32 ::1 -reject 991.50Srpaulo 1001.50Srpaulo # IPv6 site-local scoped address prefix (fec0::/10) 1011.50Srpaulo # has been deprecated by RFC3879. 1021.30Sitojun # 1031.50Srpaulo if [ -n "$ip6sitelocal" ]; then 1041.50Srpaulo warn "\$ip6sitelocal is no longer valid" 1051.30Sitojun fi 1061.30Sitojun 1071.30Sitojun # disallow "internal" addresses to appear on the wire. 1081.30Sitojun # 1091.53Sreed /sbin/route -q add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject 1101.30Sitojun 1111.30Sitojun # disallow packets to malicious IPv4 compatible prefix 1121.30Sitojun # 1131.53Sreed /sbin/route -q add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject 1141.53Sreed /sbin/route -q add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject 1151.53Sreed /sbin/route -q add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject 1161.53Sreed /sbin/route -q add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject 1171.30Sitojun 1181.30Sitojun # disallow packets to malicious 6to4 prefix 1191.30Sitojun # 1201.53Sreed /sbin/route -q add -inet6 2002:e000:: -prefixlen 20 ::1 -reject 1211.53Sreed /sbin/route -q add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject 1221.53Sreed /sbin/route -q add -inet6 2002:0000:: -prefixlen 24 ::1 -reject 1231.53Sreed /sbin/route -q add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject 1241.30Sitojun 1251.30Sitojun # Completely disallow packets to IPv4 compatible prefix. 1261.30Sitojun # This may conflict with RFC1933 under following circumstances: 1271.30Sitojun # (1) An IPv6-only KAME node tries to originate packets to IPv4 1281.51Sreed # compatible destination. The KAME node has no IPv4 1291.30Sitojun # compatible support. Under RFC1933, it should transmit 1301.30Sitojun # native IPv6 packets toward IPv4 compatible destination, 1311.30Sitojun # hoping it would reach a router that forwards the packet 1321.30Sitojun # toward auto-tunnel interface. 1331.30Sitojun # (2) An IPv6-only node originates a packet to IPv4 compatible 1341.30Sitojun # destination. A KAME node is acting as an IPv6 router, and 1351.30Sitojun # asked to forward it. 1361.30Sitojun # Due to rare use of IPv4 compatible address, and security 1371.30Sitojun # issues with it, we disable it by default. 1381.30Sitojun # 1391.53Sreed /sbin/route -q add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject 1401.30Sitojun 1411.53Sreed /sbin/sysctl -qw net.inet6.ip6.forwarding=0 1421.53Sreed /sbin/sysctl -qw net.inet6.ip6.accept_rtadv=0 1431.30Sitojun 1441.30Sitojun case $ip6mode in 1451.30Sitojun router) 1461.30Sitojun echo 'IPv6 mode: router' 1471.53Sreed /sbin/sysctl -qw net.inet6.ip6.forwarding=1 1481.50Srpaulo 1491.50Srpaulo # disallow unique-local unicast forwarding without 1501.50Srpaulo # explicit configuration. 1511.50Srpaulo if ! checkyesno ip6uniquelocal; then 1521.53Sreed /sbin/route -q add -inet6 fc00:: -prefixlen 7 \ 1531.50Srpaulo ::1 -reject 1541.50Srpaulo fi 1551.30Sitojun ;; 1561.30Sitojun 1571.30Sitojun autohost) 1581.30Sitojun echo 'IPv6 mode: autoconfigured host' 1591.53Sreed /sbin/sysctl -qw net.inet6.ip6.accept_rtadv=1 1601.30Sitojun ;; 1611.30Sitojun 1621.30Sitojun host) 1631.30Sitojun echo 'IPv6 mode: host' 1641.30Sitojun ;; 1651.30Sitojun 1661.36Slukem *) warn "invalid \$ip6mode value "\"$ip6mode\" 1671.30Sitojun ;; 1681.30Sitojun 1691.30Sitojun esac 1701.30Sitojun fi 1711.30Sitojun 1721.1Slukem # Configure all of the network interfaces listed in $net_interfaces; 1731.1Slukem # if $auto_ifconfig is YES, grab all interfaces from ifconfig. 1741.1Slukem # In the following, "xxN" stands in for interface names, like "le0". 1751.54Sapb # 1761.54Sapb # For any interfaces that has an $ifconfig_xxN variable 1771.54Sapb # associated, we break it into lines using ';' as a separator, 1781.54Sapb # then process it just like the contents of an /etc/ifconfig.xxN 1791.54Sapb # file. 1801.54Sapb # 1811.54Sapb # For each line from the $ifconfig_xxN variable or the 1821.54Sapb # /etc/ifconfig.xxN file, we ignore comments and blank lines, 1831.54Sapb # treat lines beginning with "!" as commands to execute, treat 1841.54Sapb # "dhcp" as a special case to invoke dhcpcd, and for any other 1851.54Sapb # line we run "ifconfig xxN", using each line of the file as the 1861.54Sapb # arguments for a separate "ifconfig" invocation. 1871.1Slukem # 1881.1Slukem # In order to configure an interface reasonably, you at the very least 1891.1Slukem # need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"), 1901.1Slukem # and probably a netmask (as in "netmask 0xffffffe0"). You will 1911.1Slukem # frequently need to specify a media type, as in "media UTP", for 1921.1Slukem # interface cards with multiple media connections that do not 1931.1Slukem # autoconfigure. See the ifconfig manual page for details. 1941.1Slukem # 1951.1Slukem # Note that /etc/ifconfig.xxN takes multiple lines. The following 1961.1Slukem # configuration is possible: 1971.1Slukem # inet 10.1.1.1 netmask 0xffffff00 1981.1Slukem # inet 10.1.1.2 netmask 0xffffff00 alias 1991.50Srpaulo # inet6 2001:db8::1 prefixlen 64 alias 2001.1Slukem # 2011.29Sitojun # You can put shell script fragment into /etc/ifconfig.xxN by 2021.29Sitojun # starting a line with "!". Refer to ifconfig.if(5) for details. 2031.29Sitojun # 2041.1Slukem if [ "$net_interfaces" != NO ]; then 2051.63Schristos ifaces="$(/sbin/ifconfig -l)" 2061.1Slukem if checkyesno auto_ifconfig; then 2071.63Schristos tmp="$ifaces" 2081.63Schristos for cloner in $(/sbin/ifconfig -C); do 2091.25Swiz for int in /etc/ifconfig.${cloner}[0-9]*; do 2101.23Snisimura [ ! -f $int ] && break 2111.21Slukem tmp="$tmp ${int##*.}" 2121.15Sthorpej done 2131.15Sthorpej done 2141.1Slukem else 2151.1Slukem tmp="$net_interfaces" 2161.1Slukem fi 2171.1Slukem echo -n 'Configuring network interfaces:' 2181.1Slukem for int in $tmp; do 2191.54Sapb eval argslist=\$ifconfig_$int 2201.54Sapb 2211.54Sapb # Skip interfaces that do not have explicit 2221.54Sapb # configuration information. If auto_ifconfig is 2231.54Sapb # false then also warn about such interfaces. 2241.54Sapb # 2251.54Sapb if [ -z "$argslist" ] && ! [ -f /etc/ifconfig.$int ] 2261.54Sapb then 2271.1Slukem if ! checkyesno auto_ifconfig; then 2281.1Slukem echo 2291.1Slukem warn \ 2301.1Slukem "/etc/ifconfig.$int missing and ifconfig_$int not set;" 2311.1Slukem warn "interface $int not configured." 2321.1Slukem fi 2331.1Slukem continue 2341.1Slukem fi 2351.54Sapb 2361.54Sapb echo -n " $int" 2371.54Sapb 2381.54Sapb # Create the interface if necessary. 2391.54Sapb # If the interface did not exist before, 2401.54Sapb # then also resync ipf(4). 2411.54Sapb # 2421.63Schristos if intmissing $int $ifaces; then 2431.63Schristos if /sbin/ifconfig $int create && \ 2441.63Schristos checkyesno ipfilter; then 2451.63Schristos /sbin/ipf -y >/dev/null 2461.63Schristos fi 2471.54Sapb fi 2481.54Sapb 2491.54Sapb # If $ifconfig_xxN is empty, then use 2501.54Sapb # /etc/ifconfig.xxN, which we know exists due to 2511.54Sapb # an earlier test. 2521.54Sapb # 2531.54Sapb # If $ifconfig_xxN is non-empty and contains a 2541.54Sapb # newline, then just use it as is. (This allows 2551.54Sapb # semicolons through unmolested.) 2561.54Sapb # 2571.54Sapb # If $ifconfig_xxN is non-empty and does not 2581.54Sapb # contain a newline, then convert all semicolons 2591.54Sapb # to newlines. 2601.54Sapb # 2611.54Sapb case "$argslist" in 2621.54Sapb '') 2631.54Sapb cat /etc/ifconfig.$int 2641.54Sapb ;; 2651.54Sapb *"${nl}"*) 2661.54Sapb echo "$argslist" 2671.54Sapb ;; 2681.54Sapb *) 2691.54Sapb ( 2701.54Sapb set -o noglob 2711.54Sapb IFS=';'; set -- $argslist 2721.54Sapb #echo >&2 "[$#] [$1] [$2] [$3] [$4]" 2731.54Sapb IFS="$nl"; echo "$*" 2741.54Sapb ) 2751.54Sapb ;; 2761.54Sapb esac | 2771.61Sapb collapse_backslash_newline | 2781.54Sapb while read -r args; do 2791.54Sapb case "$args" in 2801.54Sapb ''|"#"*|create) 2811.54Sapb ;; 2821.54Sapb "!"*) 2831.54Sapb # Run arbitrary command in a subshell. 2841.54Sapb ( eval "${args#*!}" ) 2851.54Sapb ;; 2861.54Sapb dhcp) 2871.58Sroy if ! checkyesno dhcpcd; then 2881.58Sroy /sbin/dhcpcd -n \ 2891.58Sroy ${dhcpcd_flags} $int 2901.58Sroy fi 2911.54Sapb ;; 2921.54Sapb *) 2931.54Sapb # Pass args to ifconfig. Note 2941.54Sapb # that args may contain embedded 2951.54Sapb # shell metacharacters, such as 2961.57Schristos # "ssid 'foo;*>bar'". We eval 2971.57Schristos # one more time so that things 2981.57Schristos # like ssid "Columbia University" work. 2991.54Sapb ( 3001.54Sapb set -o noglob 3011.57Schristos eval set -- $args 3021.54Sapb #echo >&2 "[$#] [$1] [$2] [$3]" 3031.54Sapb /sbin/ifconfig $int "$@" 3041.54Sapb ) 3051.54Sapb ;; 3061.54Sapb esac 3071.54Sapb done 3081.1Slukem configured_interfaces="$configured_interfaces $int" 3091.1Slukem done 3101.1Slukem echo "." 3111.1Slukem fi 3121.1Slukem 3131.48Scjs echo -n "Adding interface aliases:" 3141.48Scjs 3151.1Slukem # Check if each configured interface xxN has an $ifaliases_xxN variable 3161.1Slukem # associated, then configure additional IP addresses for that interface. 3171.1Slukem # The variable contains a list of "address netmask" pairs, with 3181.1Slukem # "netmask" set to "-" if the interface default netmask is to be used. 3191.1Slukem # 3201.54Sapb # Note that $ifaliases_xxN works only in certain cases and its 3211.54Sapb # use is not recommended. Use /etc/ifconfig.xxN or multiple 3221.54Sapb # commands in $ifconfig_xxN instead. 3231.1Slukem # 3241.48Scjs for int in lo0 $configured_interfaces; do 3251.20Snisimura eval args=\$ifaliases_$int 3261.1Slukem if [ -n "$args" ]; then 3271.1Slukem set -- $args 3281.1Slukem while [ $# -ge 2 ]; do 3291.1Slukem addr=$1 ; net=$2 ; shift 2 3301.1Slukem if [ "$net" = "-" ]; then 3311.16Sjdolecek # for compatibility only, obsolete 3321.53Sreed /sbin/ifconfig $int inet alias $addr 3331.1Slukem else 3341.53Sreed /sbin/ifconfig $int inet alias $addr \ 3351.1Slukem netmask $net 3361.1Slukem fi 3371.48Scjs echo -n " $int:$addr" 3381.1Slukem done 3391.1Slukem fi 3401.1Slukem done 3411.1Slukem 3421.1Slukem # /etc/ifaliases, if it exists, contains the names of additional IP 3431.1Slukem # addresses for each interface. It is formatted as a series of lines 3441.1Slukem # that contain 3451.1Slukem # address interface netmask 3461.1Slukem # 3471.54Sapb # Note that /etc/ifaliases works only in certain cases and its 3481.54Sapb # use is not recommended. Use /etc/ifconfig.xxN or multiple 3491.54Sapb # commands in $ifconfig_xxN instead. 3501.1Slukem # 3511.1Slukem if [ -f /etc/ifaliases ]; then 3521.1Slukem while read addr int net; do 3531.1Slukem if [ -z "$net" ]; then 3541.16Sjdolecek # for compatibility only, obsolete 3551.53Sreed /sbin/ifconfig $int inet alias $addr 3561.1Slukem else 3571.53Sreed /sbin/ifconfig $int inet alias $addr netmask $net 3581.1Slukem fi 3591.20Snisimura done < /etc/ifaliases 3601.1Slukem fi 3611.1Slukem 3621.56Sapb echo "." # for "Adding interface aliases:" 3631.56Sapb 3641.56Sapb # Check $defaultroute, then /etc/mygate, for the name or address 3651.56Sapb # of my IPv4 gateway host. If using a name, that name must be in 3661.56Sapb # /etc/hosts. 3671.56Sapb # 3681.56Sapb if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then 3691.56Sapb defaultroute=$(cat /etc/mygate) 3701.56Sapb fi 3711.56Sapb if [ -n "$defaultroute" ]; then 3721.56Sapb /sbin/route add default $defaultroute 3731.56Sapb fi 3741.56Sapb 3751.56Sapb # Check $defaultroute6, then /etc/mygate6, for the name or address 3761.56Sapb # of my IPv6 gateway host. If using a name, that name must be in 3771.56Sapb # /etc/hosts. Note that the gateway host address must be a link-local 3781.56Sapb # address if it is not using an stf* interface. 3791.56Sapb # 3801.56Sapb if [ -z "$defaultroute6" ] && [ -f /etc/mygate6 ]; then 3811.56Sapb defaultroute6=$(cat /etc/mygate6) 3821.56Sapb fi 3831.56Sapb if [ -n "$defaultroute6" ]; then 3841.56Sapb if [ "$ip6mode" = "autohost" ]; then 3851.56Sapb echo 3861.56Sapb warn \ 3871.56Sapb "ip6mode is set to 'autohost' and a v6 default route is also set." 3881.56Sapb fi 3891.56Sapb /sbin/route add -inet6 default $defaultroute6 3901.56Sapb fi 3911.48Scjs 3921.30Sitojun # IPv6 interface autoconfiguration. 3931.1Slukem # 3941.53Sreed if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then 3951.17Sitojun # wait till DAD is completed. always invoke it in case 3961.17Sitojun # if are configured manually by ifconfig 3971.17Sitojun # 3981.62Syamt echo 'Waiting for DAD completion for' \ 3991.62Syamt 'statically configured addresses...' 4001.53Sreed dadcount=$(/sbin/sysctl -n net.inet6.ip6.dad_count 2>/dev/null) 4011.17Sitojun sleep $dadcount 4021.17Sitojun sleep 1 4031.17Sitojun 4041.1Slukem if checkyesno rtsol; then 4051.1Slukem if [ "$ip6mode" = "autohost" ]; then 4061.1Slukem echo 'Sending router solicitation...' 4071.53Sreed /sbin/rtsol $rtsol_flags 4081.1Slukem else 4091.1Slukem echo 4101.1Slukem warn \ 4111.1Slukem "ip6mode must be set to 'autohost' to use rtsol." 4121.1Slukem fi 4131.17Sitojun 4141.18Sitojun # wait till DAD is completed, for global addresses 4151.18Sitojun # configured by router advert message. 4161.17Sitojun # 4171.62Syamt echo 'Waiting for DAD completion for' \ 4181.62Syamt 'addresses configured by router advert message...' 4191.17Sitojun sleep $dadcount 4201.17Sitojun sleep 1 4211.1Slukem fi 4221.42Schristos fi 4231.42Schristos 4241.1Slukem # XXX this must die 4251.1Slukem if [ -s /etc/netstart.local ]; then 4261.1Slukem sh /etc/netstart.local start 4271.1Slukem fi 4281.1Slukem} 4291.1Slukem 4301.1Slukemnetwork_stop() 4311.1Slukem{ 4321.1Slukem echo "Stopping network." 4331.1Slukem 4341.1Slukem # XXX this must die 4351.1Slukem if [ -s /etc/netstart.local ]; then 4361.1Slukem sh /etc/netstart.local stop 4371.1Slukem fi 4381.1Slukem 4391.1Slukem echo "Deleting aliases." 4401.1Slukem if [ -f /etc/ifaliases ]; then 4411.1Slukem while read addr int net; do 4421.53Sreed /sbin/ifconfig $int inet delete $addr 4431.20Snisimura done < /etc/ifaliases 4441.1Slukem fi 4451.1Slukem 4461.53Sreed for int in $(/sbin/ifconfig -lu); do 4471.20Snisimura eval args=\$ifaliases_$int 4481.1Slukem if [ -n "$args" ]; then 4491.1Slukem set -- $args 4501.1Slukem while [ $# -ge 2 ]; do 4511.1Slukem addr=$1 ; net=$2 ; shift 2 4521.53Sreed /sbin/ifconfig $int inet delete $addr 4531.1Slukem done 4541.1Slukem fi 4551.1Slukem done 4561.1Slukem 4571.1Slukem # down interfaces 4581.1Slukem # 4591.1Slukem echo -n 'Downing network interfaces:' 4601.1Slukem if [ "$net_interfaces" != NO ]; then 4611.1Slukem if checkyesno auto_ifconfig; then 4621.53Sreed tmp=$(/sbin/ifconfig -l) 4631.1Slukem else 4641.1Slukem tmp="$net_interfaces" 4651.1Slukem fi 4661.1Slukem for int in $tmp; do 4671.20Snisimura eval args=\$ifconfig_$int 4681.2Sveego if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then 4691.1Slukem echo -n " $int" 4701.58Sroy if [ -f /var/run/dhcpcd-$int.pid ]; then 4711.58Sroy /sbin/dhcpcd -k $int 2> /dev/null 4721.58Sroy fi 4731.53Sreed /sbin/ifconfig $int down 4741.53Sreed if /sbin/ifconfig $int destroy 2>/dev/null && \ 4751.39Stron checkyesno ipfilter; then 4761.39Stron # resync ipf(4) 4771.53Sreed /sbin/ipf -y >/dev/null 4781.39Stron fi 4791.1Slukem fi 4801.1Slukem done 4811.1Slukem echo "." 4821.1Slukem fi 4831.1Slukem 4841.1Slukem # flush routes 4851.1Slukem # 4861.53Sreed /sbin/route -qn flush 4871.38Stron 4881.1Slukem} 4891.1Slukem 4901.47Slukemload_rc_config $name 4911.47Slukemload_rc_config_var dhclient dhclient 4921.58Sroyload_rc_config_var dhcpcd dhcpcd 4931.47Slukemload_rc_config_var ipfilter ipfilter 4941.1Slukemrun_rc_command "$1" 495