1#!/bin/sh - 2# 3# $OpenBSD: netstart,v 1.126 2009/09/17 08:22:22 simon Exp $ 4 5# Strip comments (and leading/trailing whitespace if IFS is set) 6# from a file and spew to stdout 7stripcom() { 8 local _l 9 [[ -f $1 ]] || return 10 while read _l; do 11 [[ -n ${_l%%#*} ]] && echo $_l 12 done<$1 13} 14 15# Returns true if $1 contains only alphanumerics 16isalphanumeric() { 17 local _n 18 _n=$1 19 while [ ${#_n} != 0 ]; do 20 case $_n in 21 [A-Za-z0-9]*) ;; 22 *) return 1;; 23 esac 24 _n=${_n#?} 25 done 26 return 0 27} 28 29# Start the $1 interface 30ifstart() { 31 if=$1 32 # Interface names must be alphanumeric only. We check to avoid 33 # configuring backup or temp files, and to catch the "*" case. 34 if ! isalphanumeric "$if"; then 35 return 36 fi 37 38 file=/etc/hostname.$if 39 if ! [ -f $file ]; then 40 echo "netstart: $file: No such file or directory" 41 return 42 fi 43 # Not using stat(1), we can't rely on having /usr yet 44 set -A stat -- `ls -nL $file` 45 if [ "${stat[0]#???????} ${stat[2]} ${stat[3]}" != "--- 0 0" ]; then 46 echo "WARNING: $file is insecure, fixing permissions" 47 chmod -LR o-rwx $file 48 chown -LR root.wheel $file 49 fi 50 ifconfig $if > /dev/null 2>&1 51 if [ "$?" != "0" ]; then 52 # Try to create interface if it does not exist 53 ifconfig $if create > /dev/null 2>&1 54 if [ "$?" != "0" ]; then 55 return 56 fi 57 fi 58 59 # Now parse the hostname.* file 60 while :; do 61 if [ "$cmd2" ]; then 62 # We are carrying over from the 'read dt dtaddr' 63 # last time. 64 set -- $cmd2 65 af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2= 66 # Make sure and get any remaining args in ext2, 67 # like the read below 68 i=1 69 while [ $i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done 70 ext2="$@" 71 else 72 # Read the next line or exit the while loop. 73 read af name mask bcaddr ext1 ext2 || break 74 fi 75 # $af can be "dhcp", "up", "rtsol", an address family, 76 # commands, or a comment. 77 case "$af" in 78 "#"*|"") # skip comments and empty lines 79 continue 80 ;; 81 "!"*) # parse commands 82 cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}" 83 ;; 84 "bridge") 85 cmd="echo /etc/hostname.$if: bridges now supported via bridgename.* files" 86 ;; 87 "dhcp") 88 [ "$name" = "NONE" ] && name= 89 [ "$mask" = "NONE" ] && mask= 90 [ "$bcaddr" = "NONE" ] && bcaddr= 91 cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 down" 92 cmd="$cmd;dhclient $if" 93 dhcpif="$dhcpif $if" 94 ;; 95 "rtsol") 96 rtsolif="$rtsolif $if" 97 cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up" 98 ;; 99 "up") 100 # The only one of these guaranteed to be set is $if. 101 # The remaining ones exist so that media controls work. 102 cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up" 103 ;; 104 *) 105 read dt dtaddr 106 if [ "$name" = "alias" ]; then 107 # perform a 'shift' of sorts 108 alias=$name 109 name=$mask 110 mask=$bcaddr 111 bcaddr=$ext1 112 ext1=$ext2 113 ext2= 114 else 115 alias= 116 fi 117 cmd="ifconfig $if $af $alias $name" 118 case "$dt" in 119 dest) 120 cmd="$cmd $dtaddr" 121 ;; 122 [a-z!]*) 123 cmd2="$dt $dtaddr" 124 ;; 125 esac 126 if [ ! -n "$name" ]; then 127 echo "/etc/hostname.$if: invalid network configuration file" 128 return 129 fi 130 case $af in 131 inet) 132 [ "$mask" ] && cmd="$cmd netmask $mask" 133 if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then 134 cmd="$cmd broadcast $bcaddr" 135 fi 136 [ "$alias" ] && rtcmd=";route -qn add -host $name 127.0.0.1" 137 ;; 138 inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask" 139 cmd="$cmd $bcaddr" 140 ;; 141 *) 142 cmd="$cmd $mask $bcaddr" 143 ;; 144 esac 145 cmd="$cmd $ext1 $ext2$rtcmd" rtcmd= 146 ;; 147 esac 148 eval "$cmd" 149 done < /etc/hostname.$if 150} 151 152# Start multiple: 153# start "$1" interfaces in order or all interfaces if empty 154# don't start "$2" interfaces 155ifmstart() { 156 for sif in ${1:-ALL}; do 157 for hn in /etc/hostname.*; do 158 # Strip off /etc/hostname. prefix 159 if=${hn#/etc/hostname.} 160 test "$if" = "*" && continue 161 162 # Skip unwanted ifs 163 s="" 164 for xf in $2; do 165 test "$xf" = "${if%%[0-9]*}" && s="1" && break 166 done 167 test "$s" = "1" && continue 168 169 # Start wanted ifs 170 test "$sif" = "ALL" -o \ 171 "$sif" = "${if%%[0-9]*}" \ 172 && ifstart $if 173 done 174 done 175} 176 177# Start the $1 bridge 178bridgestart() { 179 # Interface names must be alphanumeric only. We check to avoid 180 # configuring backup or temp files, and to catch the "*" case. 181 if ! isalphanumeric "$1"; then 182 return 183 fi 184 brconfig $1 > /dev/null 2>&1 185 if [ "$?" != "0" ]; then 186 # Try to create interface if it does not exist 187 ifconfig $1 create > /dev/null 2>&1 188 if [ "$?" != "0" ]; then 189 return 190 fi 191 fi 192 193 # Now parse the bridgename.* file 194 # All lines are run as brconfig(8) commands. 195 while read line ; do 196 line=${line%%#*} # strip comments 197 test -z "$line" && continue 198 case "$line" in 199 "!"*) 200 cmd="${line#*!}" 201 ;; 202 *) 203 cmd="brconfig $1 $line" 204 ;; 205 esac 206 eval "$cmd" 207 done < /etc/bridgename.$1 208} 209 210# Re-read /etc/rc.conf 211. /etc/rc.conf 212 213# If we were invoked with a list of interface names, just reconfigure these 214# interfaces (or bridges) and return. 215if [ $1x = autobootx ]; then 216 shift 217fi 218if [ $# -gt 0 ]; then 219 while [ $# -gt 0 ]; do 220 if [ -f /etc/bridgename.$1 ]; then 221 bridgestart $1 222 else 223 ifstart $1 224 fi 225 shift 226 done 227 return 228fi 229 230# Otherwise, process with the complete network initialization. 231 232# /etc/myname contains my symbolic name 233if [ -f /etc/myname ]; then 234 hostname=`stripcom /etc/myname` 235 hostname $hostname 236else 237 hostname=`hostname` 238fi 239 240if [ -f /etc/defaultdomain ]; then 241 domainname `stripcom /etc/defaultdomain` 242fi 243 244# Set the address for the loopback interface. Bringing the 245# interface up, automatically invokes the IPv6 address ::1) 246ifconfig lo0 inet 127.0.0.1 247 248if ifconfig lo0 inet6 >/dev/null 2>&1; then 249 # IPv6 configurations. 250 ip6kernel=YES 251 252 # Disallow link-local unicast dest without outgoing scope identifiers. 253 route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null 254 255 # Disallow site-local unicast dest without outgoing scope identifiers. 256 # If you configure site-locals without scope id (it is permissible 257 # config for routers that are not on scope boundary), you may want 258 # to comment the line out. 259 route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null 260 261 # Disallow "internal" addresses to appear on the wire. 262 route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null 263 264 # Disallow packets to malicious IPv4 compatible prefix. 265 route -qn add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null 266 route -qn add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null 267 route -qn add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null 268 route -qn add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null 269 270 # Disallow packets to malicious 6to4 prefix. 271 route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null 272 route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null 273 route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null 274 route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null 275 276 # Disallow packets without scope identifier. 277 route -qn add -inet6 ff01:: -prefixlen 16 ::1 -reject > /dev/null 278 route -qn add -inet6 ff02:: -prefixlen 16 ::1 -reject > /dev/null 279 280 # Completely disallow packets to IPv4 compatible prefix. 281 # This may conflict with RFC1933 under following circumstances: 282 # (1) An IPv6-only KAME node tries to originate packets to IPv4 283 # compatible destination. The KAME node has no IPv4 compatible 284 # support. Under RFC1933, it should transmit native IPv6 285 # packets toward IPv4 compatible destination, hoping it would 286 # reach a router that forwards the packet toward auto-tunnel 287 # interface. 288 # (2) An IPv6-only node originates a packet to an IPv4 compatible 289 # destination. A KAME node is acting as an IPv6 router, and 290 # asked to forward it. 291 # Due to rare use of IPv4 compatible addresses, and security issues 292 # with it, we disable it by default. 293 route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null 294 295 rtsolif="" 296else 297 ip6kernel=NO 298fi 299 300 301# Configure all the non-loopback interfaces which we know about, but 302# do not start interfaces which must be delayed. 303# Refer to hostname.if(5) and bridgename.if(5) 304ifmstart "" "trunk vlan carp gif gre pfsync pppoe tun" 305 306# The trunk interfaces need to come up first in this list. 307# The vlan interfaces need to come up after trunk. 308# Configure all the carp interfaces which we know about before default route. 309ifmstart "trunk vlan carp" 310 311if [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then 312 fw=`sysctl -n net.inet6.ip6.forwarding` 313 ra=`sysctl -n net.inet6.ip6.accept_rtadv` 314 if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then 315 echo "IPv6 autoconf:$rtsolif" 316 rtsol $rtsolif 317 else 318 echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf" 319 fi 320fi 321 322# /etc/mygate, if it exists, contains the name of my gateway host 323# that name must be in /etc/hosts. 324[[ -z $dhcpif ]] && stripcom /etc/mygate | while read gw; do 325 [[ $gw == @(*:*) ]] && continue 326 route -qn delete default > /dev/null 2>&1 327 route -qn add -host default $gw && break 328done 329[[ -z $rtsolif ]] && stripcom /etc/mygate | while read gw; do 330 [[ $gw == !(*:*) ]] && continue 331 route -qn delete -inet6 default > /dev/null 2>&1 332 route -qn add -host -inet6 default $gw && break 333done 334 335# Multicast routing. 336# 337# The routing to the 224.0.0.0/4 net is setup according to these rules: 338# multicast_host multicast_router route comment 339# NO NO -reject no multicast 340# NO YES none installed daemon will run 341# YES/interface NO -interface YES=def. iface 342# Any other combination -reject config error 343route -qn delete 224.0.0.0/4 > /dev/null 2>&1 344case "$multicast_host:$multicast_router" in 345NO:NO) 346 route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null 347 ;; 348NO:YES) 349 ;; 350*:NO) 351 maddr=`if [ "$multicast_host" = "YES" ]; then 352 ed -s '!route -qn show -inet' <<EOF 353/^default/p 354EOF 355 else 356 ed -s "!ifconfig $multicast_host" <<EOF 357/^ inet /p 358EOF 359 fi 2> /dev/null` 360 if [ "X${maddr}" != "X" ]; then 361 set $maddr 362 route -qn add -net 224.0.0.0/4 -interface $2 > /dev/null 363 else 364 route -qn add -net 224.0.0.0/4 -interface \ 365 127.0.0.1 -reject > /dev/null 366 fi 367 ;; 368*:*) 369 echo 'config error, multicasting disabled until rc.conf is fixed' 370 route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null 371 ;; 372esac 373 374 375# Configure PPPoE, GIF, GRE and TUN interfaces, delayed because they require 376# routes to be set. TUN might depend on PPPoE, and GIF or GRE may depend on 377# either of them. 378ifmstart "pppoe tun gif gre" 379 380# reject 127/8 other than 127.0.0.1 381route -qn add -net 127 127.0.0.1 -reject > /dev/null 382 383# Configure all the bridges. 384for bn in /etc/bridgename.*; do 385 # Strip off /etc/bridgename. prefix 386 if=${bn#/etc/bridgename.} 387 test "$if" = "*" && continue 388 389 bridgestart $if 390done 391 392if [ "$ip6kernel" = "YES" ]; then 393 # this is to make sure DAD is completed before going further. 394 count=0 395 while [ $((count++)) -lt 10 -a "x"`sysctl -n net.inet6.ip6.dad_pending` != "x0" ]; do 396 sleep 1 397 done 398fi 399