Annotation of src/etc/netstart, Revision 1.230
1.1 deraadt 1: #!/bin/sh -
2: #
1.230 ! afresh1 3: # $OpenBSD: netstart,v 1.229 2022/11/05 12:06:05 kn Exp $
1.153 rpe 4:
5: # Turn off Strict Bourne shell mode.
6: set +o sh
1.101 millert 7:
1.198 rpe 8: # Show usage of the netstart script and exit.
9: usage() {
1.228 kn 10: print -u2 "usage: sh $0 [-n] [interface ...]"
1.198 rpe 11: exit 1
12: }
13:
1.219 kn 14: # Test the first argument against the remaining ones, return success on a match.
15: isin() {
16: local _a=$1 _b
17:
18: shift
19: for _b; do
20: [[ $_a == "$_b" ]] && return 0
21: done
22: return 1
23: }
24:
1.204 kn 25: # Echo file $1 to stdout. Skip comment lines. Strip leading and trailing
1.173 rpe 26: # whitespace if IFS is set.
1.160 rpe 27: # Usage: stripcom /path/to/file
1.101 millert 28: stripcom() {
1.160 rpe 29: local _file=$1 _line
30:
31: [[ -f $_file ]] || return
32:
33: while read _line; do
34: [[ -n ${_line%%#*} ]] && print -r -- "$_line"
35: done <$_file
1.101 millert 36: }
1.45 millert 37:
1.177 rpe 38: # Parse and "unpack" a hostname.if(5) line given as positional parameters.
39: # Fill the _cmds array with the resulting interface configuration commands.
40: parse_hn_line() {
1.211 krw 41: local _af=0 _name=1 _mask=2 _bc=3 _prefix=2 _c _cmd _prev _daddr _dhcp _i
1.177 rpe 42: set -A _c -- "$@"
43: set -o noglob
44:
45: case ${_c[_af]} in
46: ''|*([[:blank:]])'#'*)
47: return
48: ;;
49: inet) ((${#_c[*]} > 1)) || return
1.212 florian 50: if [[ ${_c[_name]} == autoconf ]]; then
51: _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
1.214 sthen 52: V4_AUTOCONF=true
1.212 florian 53: return
54: fi
1.177 rpe 55: [[ ${_c[_name]} == alias ]] && _mask=3 _bc=4
56: [[ -n ${_c[_mask]} ]] && _c[_mask]="netmask ${_c[_mask]}"
57: if [[ -n ${_c[_bc]} ]]; then
58: _c[_bc]="broadcast ${_c[_bc]}"
59: [[ ${_c[_bc]} == *NONE ]] && _c[_bc]=
60: fi
61: _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
62: ;;
63: inet6) ((${#_c[*]} > 1)) || return
64: if [[ ${_c[_name]} == autoconf ]]; then
65: _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
1.180 rpe 66: V6_AUTOCONF=true
1.177 rpe 67: return
68: fi
69: [[ ${_c[_name]} == alias ]] && _prefix=3
70: [[ -n ${_c[_prefix]} ]] && _c[_prefix]="prefixlen ${_c[_prefix]}"
71: _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
72: ;;
73: dest) ((${#_c[*]} == 2)) && _daddr=${_c[1]} || return
74: _prev=$((${#_cmds[*]} - 1))
75: ((_prev >= 0)) || return
76: set -A _c -- ${_cmds[_prev]}
77: _name=3
78: [[ ${_c[_name]} == alias ]] && _name=4
79: _c[_name]="${_c[_name]} $_daddr"
80: _cmds[$_prev]="${_c[@]}"
81: ;;
1.213 florian 82: dhcp) _cmds[${#_cmds[*]}]="ifconfig $_if inet autoconf"
83: V4_AUTOCONF=true
1.177 rpe 84: ;;
85: '!'*) _cmd=$(print -- "${_c[@]}" | sed 's/\$if/'$_if'/g')
86: _cmds[${#_cmds[*]}]="${_cmd#!}"
87: ;;
88: *) _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
89: ;;
90: esac
91: unset _c
1.178 rpe 92: set +o noglob
1.177 rpe 93: }
94:
1.192 rpe 95: # Create interface $1 if it does not yet exist.
1.195 rpe 96: # Usage: ifcreate if1
1.191 dlg 97: ifcreate() {
98: local _if=$1
99:
1.215 bluhm 100: if $PRINT_ONLY; then
101: print -r -- "{ ifconfig $_if || ifconfig $_if create; }"
102: else
103: { ifconfig $_if || ifconfig $_if create; } >/dev/null 2>&1
104: fi
1.191 dlg 105: }
106:
1.192 rpe 107: # Create interfaces for network pseudo-devices referred to by hostname.if files.
1.219 kn 108: # Optionally, limit creation to given interfaces only.
109: # Usage: vifscreate [if ...]
1.191 dlg 110: vifscreate() {
1.192 rpe 111: local _vif _hn _if
1.191 dlg 112:
1.192 rpe 113: for _vif in $(ifconfig -C); do
1.193 rpe 114: for _hn in /etc/hostname.${_vif}+([[:digit:]]); do
1.191 dlg 115: [[ -f $_hn ]] || continue
116: _if=${_hn#/etc/hostname.}
1.216 bluhm 117:
118: # loopback for routing domain is created by kernel
119: [[ -n ${_if##lo[1-9]*} ]] || continue
1.191 dlg 120:
1.219 kn 121: if (($# > 0)) && ! isin $_if "$@"; then
122: continue
123: fi
124:
1.193 rpe 125: if ! ifcreate $_if; then
1.194 rpe 126: print -u2 "${0##*/}: create for '$_if' failed."
1.193 rpe 127: fi
1.191 dlg 128: done
129: done
130: }
131:
1.160 rpe 132: # Start a single interface.
133: # Usage: ifstart if1
1.83 miod 134: ifstart() {
1.187 tb 135: local _if=$1 _hn=/etc/hostname.$1 _cmds _i=0 _line _stat
1.177 rpe 136: set -A _cmds
1.174 rpe 137:
1.230 ! afresh1 138: if [[ $_if == ??:??:??:??:??:?? ]]; then
! 139: if ! _line=$( ifconfig -M $_if ); then
! 140: print -u2 "${0##*/}: $_if is not unique."
! 141: return
! 142: fi
! 143:
! 144: [[ -z $_line ]] && return
! 145: _if=$_line
! 146: _line=
! 147:
! 148: if [[ -e /etc/hostname.$_if ]]; then
! 149: print -u2 "${0##*/}: $_hn: /etc/hostname.$_if overrides"
! 150: return
! 151: fi
! 152: fi
! 153:
1.83 miod 154: # Interface names must be alphanumeric only. We check to avoid
155: # configuring backup or temp files, and to catch the "*" case.
1.177 rpe 156: [[ $_if != +([[:alpha:]])+([[:digit:]]) ]] && return
1.83 miod 157:
1.185 rpe 158: if [[ ! -f $_hn ]]; then
1.194 rpe 159: print -u2 "${0##*/}: $_hn: No such file or directory."
1.121 todd 160: return
161: fi
1.177 rpe 162:
1.146 rpe 163: # Not using stat(1), we can't rely on having /usr yet.
1.185 rpe 164: set -A _stat -- $(ls -nL $_hn)
1.183 rpe 165: if [[ "${_stat[0]}${_stat[2]}${_stat[3]}" != *---00 ]]; then
1.194 rpe 166: print -u2 "WARNING: $_hn is insecure, fixing permissions."
1.185 rpe 167: chmod -LR o-rwx $_hn
168: chown -LR root:wheel $_hn
1.119 deraadt 169: fi
1.83 miod 170:
1.177 rpe 171: # Check for ifconfig'able interface, except if -n option is specified.
1.215 bluhm 172: ifcreate $_if || return
1.177 rpe 173:
174: # Parse the hostname.if(5) file and fill _cmds array with interface
175: # configuration commands.
176: set -o noglob
1.205 kn 177: while IFS= read -- _line; do
1.177 rpe 178: parse_hn_line $_line
1.185 rpe 179: done <$_hn
1.177 rpe 180:
181: # Apply the interface configuration commands stored in _cmds array.
182: while ((_i < ${#_cmds[*]})); do
183: if $PRINT_ONLY; then
184: print -r -- "${_cmds[_i]}"
1.83 miod 185: else
1.177 rpe 186: eval "${_cmds[_i]}"
1.83 miod 187: fi
1.177 rpe 188: ((_i++))
189: done
190: unset _cmds
1.178 rpe 191: set +o noglob
1.83 miod 192: }
193:
1.161 rpe 194: # Start multiple interfaces by driver name.
195: # Usage: ifmstart "em iwm" "trunk vlan"
1.146 rpe 196: # Start "$1" interfaces in order or all interfaces if empty.
1.161 rpe 197: # Don't start "$2" interfaces. "$2" is optional.
1.105 todd 198: ifmstart() {
1.161 rpe 199: local _sifs=$1 _xifs=$2 _hn _if _sif _xif
200:
201: for _sif in ${_sifs:-ALL}; do
1.230 ! afresh1 202: for _hn in /etc/hostname.@(+([[:alpha:]])+([[:digit:]])|??:??:??:??:??:??); do
1.193 rpe 203: [[ -f $_hn ]] || continue
1.161 rpe 204: _if=${_hn#/etc/hostname.}
1.113 david 205:
1.230 ! afresh1 206: if [[ $_if == +([[:alpha:]])+([[:digit:]]) ]]; then
! 207: # Skip unwanted ifs.
! 208: for _xif in $_xifs; do
! 209: [[ $_xif == ${_if%%[0-9]*} ]] && continue 2
! 210: done
! 211: fi
1.105 todd 212:
1.146 rpe 213: # Start wanted ifs.
1.161 rpe 214: [[ $_sif == @(ALL|${_if%%[0-9]*}) ]] && ifstart $_if
1.105 todd 215: done
216: done
217: }
218:
1.195 rpe 219: # Parse /etc/mygate and add default routes for IPv4 and IPv6.
1.172 mpi 220: # Usage: defaultroute
221: defaultroute() {
1.209 tb 222: local _cmd _v4set=false _v6set=false;
1.208 deraadt 223: set -o noglob
1.188 tb 224:
1.208 deraadt 225: stripcom /etc/mygate |
1.180 rpe 226: while read gw; do
1.208 deraadt 227: case $gw in
228: '!'*)
1.209 tb 229: _cmd=$(print -- "$gw")
1.208 deraadt 230: _cmd="${_cmd#!}"
231: ;;
1.209 tb 232: !(*:*))
1.214 sthen 233: ($_v4set || $V4_AUTOCONF) && continue
1.209 tb 234: _cmd="route -qn add -host default $gw"
235: _v4set=true
236: ;;
1.208 deraadt 237: *)
1.209 tb 238: ($_v6set || $V6_AUTOCONF) && continue
239: _cmd="route -qn add -host -inet6 default $gw"
240: _v6set=true
1.208 deraadt 241: ;;
242: esac
1.188 tb 243: if $PRINT_ONLY; then
1.208 deraadt 244: print -r -- "$_cmd"
1.188 tb 245: else
1.208 deraadt 246: $_cmd
1.188 tb 247: fi
1.172 mpi 248: done
1.208 deraadt 249: set +o noglob
1.172 mpi 250: }
1.199 tb 251:
1.215 bluhm 252: # add all the routes needed for IPv6
253: ip6routes() {
254: local _i=0
255: set -A _cmds
256:
257: # Disallow link-local unicast dest without outgoing scope identifiers.
258: _cmds[_i++]="route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject"
259:
260: # Disallow site-local unicast dest without outgoing scope identifiers.
261: # If you configure site-locals without scope id (it is permissible
262: # config for routers that are not on scope boundary), you may want
263: # to comment the line out.
264: _cmds[_i++]="route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject"
265:
266: # Disallow "internal" addresses to appear on the wire.
267: _cmds[_i++]="route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject"
268:
269: # Disallow packets to malicious 6to4 prefix.
270: _cmds[_i++]="route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject"
271: _cmds[_i++]="route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject"
272: _cmds[_i++]="route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject"
273: _cmds[_i++]="route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject"
274:
275: # Disallow packets without scope identifier.
276: _cmds[_i++]="route -qn add -inet6 ff01:: -prefixlen 16 ::1 -reject"
277: _cmds[_i++]="route -qn add -inet6 ff02:: -prefixlen 16 ::1 -reject"
278:
279: # Completely disallow packets to IPv4 compatible prefix.
280: #
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: #
292: # Due to rare use of IPv4 compatible addresses, and security issues
293: # with it, we disable it by default.
294: _cmds[_i++]="route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject"
295:
296: # Apply the interface configuration commands stored in _cmds array.
297: _i=0
298: while ((_i < ${#_cmds[*]})); do
299: if $PRINT_ONLY; then
300: print -r -- "${_cmds[_i]}"
301: else
302: eval "${_cmds[_i]}"
303: fi
304: ((_i++))
305: done
306: unset _cmds
307: }
308:
1.218 florian 309: # wait for autoconf interfaces
310: wait_autoconf_default() {
1.226 kn 311: local _count=0
312:
1.218 florian 313: if ifconfig | grep -q ': flags=.*<.*AUTOCONF.*>'; then
1.226 kn 314: while ((_count++ < 20)); do
1.218 florian 315: route -n show | grep -q ^default && break
316: sleep .5
317: done
318: fi
319: }
320:
1.226 kn 321: # Ensure IPv6 Duplicate Address Detection (DAD) is completed.
322: wait_dad() {
323: local _count=0
324:
325: while ((_count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) != 0)); do
326: sleep 1
327: done
328: }
329:
1.199 tb 330: # Make sure the invoking user has the right privileges. Check for presence of
331: # id(1) to avoid problems with diskless setups.
332: if [[ -x /usr/bin/id ]] && (($(id -u) != 0)); then
1.229 kn 333: print -u2 "${0##*/}: need root privileges"
1.199 tb 334: exit 1
335: fi
1.154 sthen 336:
1.151 rpe 337: # Get network related vars from rc.conf using the parsing routine from rc.subr.
338: FUNCS_ONLY=1 . /etc/rc.d/rc.subr
1.150 ajacouto 339: _rc_parse_conf
1.177 rpe 340:
341: PRINT_ONLY=false
1.214 sthen 342: V4_AUTOCONF=false
1.180 rpe 343: V6_AUTOCONF=false
1.223 kn 344: IP6KERNEL=false
1.180 rpe 345:
1.177 rpe 346: while getopts ":n" opt; do
347: case $opt in
348: n) PRINT_ONLY=true;;
1.198 rpe 349: *) usage;;
1.177 rpe 350: esac
351: done
352: shift $((OPTIND-1))
353:
1.223 kn 354: if ifconfig lo0 inet6 >/dev/null 2>&1; then
355: IP6KERNEL=true
356: fi
357:
1.195 rpe 358: # Load key material for the generation of IPv6 Semantically Opaque Interface
1.224 kn 359: # Identifiers (SOII) used for SLAAC addresses.
1.227 kn 360: if $IP6KERNEL && ! $PRINT_ONLY; then
361: [[ -f /etc/soii.key ]] &&
362: sysctl -q "net.inet6.ip6.soiikey=$(</etc/soii.key)"
363: fi
1.1 deraadt 364:
1.83 miod 365: # If we were invoked with a list of interface names, just reconfigure these
1.172 mpi 366: # interfaces (or bridges), add default routes and return.
1.219 kn 367: # Create virtual interfaces upfront to make ifconfig commands depending on
368: # other interfaces, e.g. "patch", work regardless of in which order interface
369: # names were specified.
1.159 rpe 370: if (($# > 0)); then
1.219 kn 371: vifscreate "$@"
1.159 rpe 372: for _if; do ifstart $_if; done
1.172 mpi 373: defaultroute
1.83 miod 374: return
375: fi
376:
377: # Otherwise, process with the complete network initialization.
1.4 dm 378:
1.131 sobrado 379: # Set the address for the loopback interface. Bringing the interface up,
380: # automatically invokes the IPv6 address ::1.
1.215 bluhm 381: if $PRINT_ONLY; then
382: print -r -- "ifconfig lo0 inet 127.0.0.1/8"
383: else
384: ifconfig lo0 inet 127.0.0.1/8
1.225 kn 385: fi
386:
387: if $IP6KERNEL && ! $PRINT_ONLY; then
388: ip6routes
1.215 bluhm 389: fi
1.24 kstailey 390:
1.195 rpe 391: # Create all the pseudo interfaces up front.
1.191 dlg 392: vifscreate
1.105 todd 393:
394: # Configure all the non-loopback interfaces which we know about, but
1.127 deraadt 395: # do not start interfaces which must be delayed. Refer to hostname.if(5)
1.217 dlg 396: ifmstart "" "aggr trunk svlan vlan carp pppoe tun tap gif etherip gre egre nvgre eoip vxlan pflow wg"
1.56 itojun 397:
1.201 dlg 398: # The aggr and trunk interfaces need to come up first in this list.
1.132 mpf 399: # The (s)vlan interfaces need to come up after trunk.
1.118 brad 400: # Configure all the carp interfaces which we know about before default route.
1.201 dlg 401: ifmstart "aggr trunk svlan vlan carp pppoe"
1.102 mcbride 402:
1.195 rpe 403: # Set default routes for IPv4 and IPv6.
1.172 mpi 404: defaultroute
1.44 deraadt 405:
1.48 niklas 406: # Multicast routing.
1.168 sthen 407: if [[ $multicast != YES ]]; then
1.215 bluhm 408: if $PRINT_ONLY; then
409: print -r -- "route -qn delete 224.0.0.0/4"
410: print -r -- "route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject"
411: else
412: route -qn delete 224.0.0.0/4
413: route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject
414: fi
1.168 sthen 415: fi
1.105 todd 416:
1.146 rpe 417: # Reject 127/8 other than 127.0.0.1.
1.215 bluhm 418: if $PRINT_ONLY; then
419: print -r -- "route -qn add -net 127 127.0.0.1 -reject"
420: else
421: route -qn add -net 127 127.0.0.1 -reject
422: fi
1.218 florian 423:
424: # If interface autoconf exists, pause a little for at least one default route
1.222 kn 425: $PRINT_ONLY || wait_autoconf_default
1.191 dlg 426:
427: # Configure interfaces that rely on routing
1.217 dlg 428: ifmstart "tun tap gif etherip gre egre nvgre eoip vxlan pflow wg"
1.116 david 429:
1.226 kn 430: if $IP6KERNEL && ! $PRINT_ONLY; then
431: wait_dad
1.116 david 432: fi