Annotation of src/etc/netstart, Revision 1.161
1.1 deraadt 1: #!/bin/sh -
2: #
1.161 ! rpe 3: # $OpenBSD: netstart,v 1.160 2015/11/12 22:50:46 rpe Exp $
1.153 rpe 4:
5: # Turn off Strict Bourne shell mode.
6: set +o sh
1.101 millert 7:
1.160 rpe 8: # Strip comment lines from a file.
9: # Strip leading and trailing whitespace if IFS is set.
10: # Usage: stripcom /path/to/file
1.101 millert 11: stripcom() {
1.160 rpe 12: local _file=$1 _line
13:
14: [[ -f $_file ]] || return
15:
16: while read _line; do
17: [[ -n ${_line%%#*} ]] && print -r -- "$_line"
18: done <$_file
1.101 millert 19: }
1.45 millert 20:
1.160 rpe 21: # Start a single interface.
22: # Usage: ifstart if1
1.83 miod 23: ifstart() {
1.84 deraadt 24: if=$1
1.83 miod 25: # Interface names must be alphanumeric only. We check to avoid
26: # configuring backup or temp files, and to catch the "*" case.
1.137 rpe 27: [[ $if != +([[:alpha:]])+([[:digit:]]) ]] && return
1.83 miod 28:
1.119 deraadt 29: file=/etc/hostname.$if
1.121 todd 30: if ! [ -f $file ]; then
31: echo "netstart: $file: No such file or directory"
32: return
33: fi
1.146 rpe 34: # Not using stat(1), we can't rely on having /usr yet.
1.123 sthen 35: set -A stat -- `ls -nL $file`
36: if [ "${stat[0]#???????} ${stat[2]} ${stat[3]}" != "--- 0 0" ]; then
1.119 deraadt 37: echo "WARNING: $file is insecure, fixing permissions"
1.122 sthen 38: chmod -LR o-rwx $file
39: chown -LR root.wheel $file
1.119 deraadt 40: fi
1.136 rpe 41: # Check for ifconfig'able interface.
42: (ifconfig $if || ifconfig $if create) >/dev/null 2>&1 || return
1.83 miod 43:
1.146 rpe 44: # Now parse the hostname.* file.
1.83 miod 45: while :; do
46: if [ "$cmd2" ]; then
47: # We are carrying over from the 'read dt dtaddr'
48: # last time.
49: set -- $cmd2
50: af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2=
51: # Make sure and get any remaining args in ext2,
1.146 rpe 52: # like the read below.
1.83 miod 53: i=1
1.126 simon 54: while [ $i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done
1.83 miod 55: ext2="$@"
56: else
57: # Read the next line or exit the while loop.
58: read af name mask bcaddr ext1 ext2 || break
59: fi
1.146 rpe 60: # $af can be "dhcp", "up", "rtsol", an address family, commands,
61: # or a comment.
1.83 miod 62: case "$af" in
1.146 rpe 63: "#"*|"") # Skip comments and empty lines.
1.83 miod 64: continue
65: ;;
1.146 rpe 66: "!"*) # Parse commands.
1.83 miod 67: cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}"
68: ;;
69: "dhcp")
70: [ "$name" = "NONE" ] && name=
71: [ "$mask" = "NONE" ] && mask=
72: [ "$bcaddr" = "NONE" ] && bcaddr=
1.143 claudio 73: cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 down"
1.106 todd 74: cmd="$cmd;dhclient $if"
1.110 todd 75: dhcpif="$dhcpif $if"
1.83 miod 76: ;;
77: "rtsol")
1.84 deraadt 78: rtsolif="$rtsolif $if"
1.106 todd 79: cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
1.83 miod 80: ;;
81: *)
82: read dt dtaddr
83: if [ "$name" = "alias" ]; then
1.146 rpe 84: # Perform a 'shift' of sorts.
1.83 miod 85: alias=$name
86: name=$mask
87: mask=$bcaddr
88: bcaddr=$ext1
89: ext1=$ext2
90: ext2=
91: else
92: alias=
93: fi
1.114 todd 94: cmd="ifconfig $if $af $alias $name"
1.83 miod 95: case "$dt" in
96: dest)
97: cmd="$cmd $dtaddr"
98: ;;
1.130 todd 99: *)
1.83 miod 100: cmd2="$dt $dtaddr"
101: ;;
102: esac
103: case $af in
104: inet)
1.128 todd 105: if [ ! -n "$name" ]; then
106: echo "/etc/hostname.$if: inet alone is invalid"
107: return
108: fi
1.83 miod 109: [ "$mask" ] && cmd="$cmd netmask $mask"
110: if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
111: cmd="$cmd broadcast $bcaddr"
112: fi
113: ;;
1.128 todd 114: inet6)
115: if [ ! -n "$name" ]; then
116: echo "/etc/hostname.$if: inet6 alone is invalid"
117: return
118: fi
119: [ "$mask" ] && cmd="$cmd prefixlen $mask"
1.83 miod 120: cmd="$cmd $bcaddr"
121: ;;
122: *)
123: cmd="$cmd $mask $bcaddr"
124: ;;
125: esac
1.139 mpi 126: cmd="$cmd $ext1 $ext2"
1.83 miod 127: ;;
128: esac
129: eval "$cmd"
1.147 rpe 130: done </etc/hostname.$if
1.83 miod 131: }
132:
1.161 ! rpe 133: # Start multiple interfaces by driver name.
! 134: # Usage: ifmstart "em iwm" "trunk vlan"
1.146 rpe 135: # Start "$1" interfaces in order or all interfaces if empty.
1.161 ! rpe 136: # Don't start "$2" interfaces. "$2" is optional.
1.105 todd 137: ifmstart() {
1.161 ! rpe 138: local _sifs=$1 _xifs=$2 _hn _if _sif _xif
! 139:
! 140: for _sif in ${_sifs:-ALL}; do
! 141: for _hn in /etc/hostname.*; do
! 142: _if=${_hn#/etc/hostname.}
! 143: [[ $_if == '*' ]] && continue
1.113 david 144:
1.146 rpe 145: # Skip unwanted ifs.
1.161 ! rpe 146: for _xif in $_xifs; do
! 147: [[ $_xif == ${_if%%[0-9]*} ]] && continue 2
1.105 todd 148: done
149:
1.146 rpe 150: # Start wanted ifs.
1.161 ! rpe 151: [[ $_sif == @(ALL|${_if%%[0-9]*}) ]] && ifstart $_if
1.105 todd 152: done
153: done
154: }
155:
1.154 sthen 156: # IPv6 autoconf the interfaces in the list at $rtsolif
157: # Usage: ifautoconf
158: ifautoconf() {
1.156 sthen 159: # $ip6kernel will not have been set if we were invoked with a
160: # list of interface names
161: if ifconfig lo0 inet6 >/dev/null 2>&1; then
162: for curif in $rtsolif; do
163: ifconfig $curif inet6 autoconf
164: done
1.154 sthen 165: fi
166: }
167:
1.151 rpe 168: # Get network related vars from rc.conf using the parsing routine from rc.subr.
169: FUNCS_ONLY=1 . /etc/rc.d/rc.subr
1.150 ajacouto 170: _rc_parse_conf
1.1 deraadt 171:
1.83 miod 172: # If we were invoked with a list of interface names, just reconfigure these
173: # interfaces (or bridges) and return.
1.159 rpe 174: if (($# > 0)); then
175: for _if; do ifstart $_if; done
1.154 sthen 176: ifautoconf
1.83 miod 177: return
178: fi
179:
180: # Otherwise, process with the complete network initialization.
181:
1.146 rpe 182: # /etc/myname contains my symbolic name.
1.159 rpe 183: [[ -f /etc/myname ]] && hostname "$(stripcom /etc/myname)"
1.4 dm 184:
1.131 sobrado 185: # Set the address for the loopback interface. Bringing the interface up,
186: # automatically invokes the IPv6 address ::1.
1.129 henning 187: ifconfig lo0 inet 127.0.0.1/8
1.24 kstailey 188:
1.54 itojun 189: if ifconfig lo0 inet6 >/dev/null 2>&1; then
190: # IPv6 configurations.
191: ip6kernel=YES
192:
1.83 miod 193: # Disallow link-local unicast dest without outgoing scope identifiers.
1.147 rpe 194: route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject >/dev/null
1.66 itojun 195:
1.83 miod 196: # Disallow site-local unicast dest without outgoing scope identifiers.
1.66 itojun 197: # If you configure site-locals without scope id (it is permissible
198: # config for routers that are not on scope boundary), you may want
199: # to comment the line out.
1.147 rpe 200: route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject >/dev/null
1.66 itojun 201:
1.83 miod 202: # Disallow "internal" addresses to appear on the wire.
1.147 rpe 203: route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject >/dev/null
1.66 itojun 204:
1.83 miod 205: # Disallow packets to malicious IPv4 compatible prefix.
1.147 rpe 206: route -qn add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject >/dev/null
207: route -qn add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject >/dev/null
208: route -qn add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject >/dev/null
209: route -qn add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject >/dev/null
1.66 itojun 210:
1.83 miod 211: # Disallow packets to malicious 6to4 prefix.
1.147 rpe 212: route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject >/dev/null
213: route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject >/dev/null
214: route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject >/dev/null
215: route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject >/dev/null
1.115 itojun 216:
217: # Disallow packets without scope identifier.
1.147 rpe 218: route -qn add -inet6 ff01:: -prefixlen 16 ::1 -reject >/dev/null
219: route -qn add -inet6 ff02:: -prefixlen 16 ::1 -reject >/dev/null
1.66 itojun 220:
221: # Completely disallow packets to IPv4 compatible prefix.
1.146 rpe 222: #
1.66 itojun 223: # This may conflict with RFC1933 under following circumstances:
224: # (1) An IPv6-only KAME node tries to originate packets to IPv4
1.77 deraadt 225: # compatible destination. The KAME node has no IPv4 compatible
1.66 itojun 226: # support. Under RFC1933, it should transmit native IPv6
227: # packets toward IPv4 compatible destination, hoping it would
228: # reach a router that forwards the packet toward auto-tunnel
229: # interface.
1.77 deraadt 230: # (2) An IPv6-only node originates a packet to an IPv4 compatible
1.66 itojun 231: # destination. A KAME node is acting as an IPv6 router, and
232: # asked to forward it.
1.146 rpe 233: #
1.77 deraadt 234: # Due to rare use of IPv4 compatible addresses, and security issues
1.66 itojun 235: # with it, we disable it by default.
1.147 rpe 236: route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject >/dev/null
1.56 itojun 237:
238: rtsolif=""
1.54 itojun 239: else
240: ip6kernel=NO
241: fi
242:
1.105 todd 243:
244: # Configure all the non-loopback interfaces which we know about, but
1.127 deraadt 245: # do not start interfaces which must be delayed. Refer to hostname.if(5)
1.157 claudio 246: ifmstart "" "trunk svlan vlan carp gif gre pfsync pppoe tun tap bridge pflow"
1.56 itojun 247:
1.118 brad 248: # The trunk interfaces need to come up first in this list.
1.132 mpf 249: # The (s)vlan interfaces need to come up after trunk.
1.118 brad 250: # Configure all the carp interfaces which we know about before default route.
1.132 mpf 251: ifmstart "trunk svlan vlan carp"
1.118 brad 252:
1.154 sthen 253: # Now that $rtsolif has been populated, IPv6 autoconf those interfaces
254: ifautoconf
1.102 mcbride 255:
1.138 todd 256: # Look for default routes in /etc/mygate.
1.110 todd 257: [[ -z $dhcpif ]] && stripcom /etc/mygate | while read gw; do
258: [[ $gw == @(*:*) ]] && continue
1.147 rpe 259: route -qn delete default >/dev/null 2>&1
1.110 todd 260: route -qn add -host default $gw && break
261: done
262: [[ -z $rtsolif ]] && stripcom /etc/mygate | while read gw; do
263: [[ $gw == !(*:*) ]] && continue
1.147 rpe 264: route -qn delete -inet6 default >/dev/null 2>&1
1.110 todd 265: route -qn add -host -inet6 default $gw && break
266: done
1.44 deraadt 267:
1.48 niklas 268: # Multicast routing.
269: #
270: # The routing to the 224.0.0.0/4 net is setup according to these rules:
271: # multicast_host multicast_router route comment
272: # NO NO -reject no multicast
273: # NO YES none installed daemon will run
274: # YES/interface NO -interface YES=def. iface
275: # Any other combination -reject config error
1.147 rpe 276: route -qn delete 224.0.0.0/4 >/dev/null 2>&1
1.48 niklas 277: case "$multicast_host:$multicast_router" in
278: NO:NO)
1.147 rpe 279: route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject >/dev/null
1.67 deraadt 280: ;;
1.48 niklas 281: NO:YES)
282: ;;
283: *:NO)
1.159 rpe 284: maddr=$(if [[ $multicast_host == YES ]]; then
1.91 deraadt 285: ed -s '!route -qn show -inet' <<EOF
1.48 niklas 286: /^default/p
287: EOF
288: else
289: ed -s "!ifconfig $multicast_host" <<EOF
290: /^ inet /p
291: EOF
1.159 rpe 292: fi 2>/dev/null)
293: if [[ -n $maddr ]]; then
1.112 reyk 294: set $maddr
1.147 rpe 295: route -qn add -net 224.0.0.0/4 -interface $2 >/dev/null
1.112 reyk 296: else
297: route -qn add -net 224.0.0.0/4 -interface \
1.147 rpe 298: 127.0.0.1 -reject >/dev/null
1.112 reyk 299: fi
1.67 deraadt 300: ;;
1.48 niklas 301: *:*)
302: echo 'config error, multicasting disabled until rc.conf is fixed'
1.147 rpe 303: route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject >/dev/null
1.67 deraadt 304: ;;
1.48 niklas 305: esac
1.83 miod 306:
1.105 todd 307:
1.152 florian 308: # Configure PPPoE, GIF, GRE, TUN and PFLOW interfaces, delayed because they
309: # require routes to be set. TUN might depend on PPPoE, and GIF or GRE may
310: # depend on either of them. PFLOW might bind to ip addresses configured
311: # on either of them.
1.157 claudio 312: ifmstart "pppoe tun tap gif gre bridge pflow"
1.92 deraadt 313:
1.146 rpe 314: # Reject 127/8 other than 127.0.0.1.
1.147 rpe 315: route -qn add -net 127 127.0.0.1 -reject >/dev/null
1.116 david 316:
1.159 rpe 317: if [[ $ip6kernel == YES ]]; then
1.146 rpe 318: # This is to make sure DAD is completed before going further.
1.124 markus 319: count=0
1.159 rpe 320: while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) != 0)); do
1.124 markus 321: sleep 1
322: done
1.116 david 323: fi