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