Annotation of src/etc/netstart, Revision 1.225
1.1 deraadt 1: #!/bin/sh -
2: #
1.225 ! kn 3: # $OpenBSD: netstart,v 1.224 2022/10/31 19:48:50 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.221 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.83 miod 138: # Interface names must be alphanumeric only. We check to avoid
139: # configuring backup or temp files, and to catch the "*" case.
1.177 rpe 140: [[ $_if != +([[:alpha:]])+([[:digit:]]) ]] && return
1.83 miod 141:
1.185 rpe 142: if [[ ! -f $_hn ]]; then
1.194 rpe 143: print -u2 "${0##*/}: $_hn: No such file or directory."
1.121 todd 144: return
145: fi
1.177 rpe 146:
1.146 rpe 147: # Not using stat(1), we can't rely on having /usr yet.
1.185 rpe 148: set -A _stat -- $(ls -nL $_hn)
1.183 rpe 149: if [[ "${_stat[0]}${_stat[2]}${_stat[3]}" != *---00 ]]; then
1.194 rpe 150: print -u2 "WARNING: $_hn is insecure, fixing permissions."
1.185 rpe 151: chmod -LR o-rwx $_hn
152: chown -LR root:wheel $_hn
1.119 deraadt 153: fi
1.83 miod 154:
1.177 rpe 155: # Check for ifconfig'able interface, except if -n option is specified.
1.215 bluhm 156: ifcreate $_if || return
1.177 rpe 157:
158: # Parse the hostname.if(5) file and fill _cmds array with interface
159: # configuration commands.
160: set -o noglob
1.205 kn 161: while IFS= read -- _line; do
1.177 rpe 162: parse_hn_line $_line
1.185 rpe 163: done <$_hn
1.177 rpe 164:
165: # Apply the interface configuration commands stored in _cmds array.
166: while ((_i < ${#_cmds[*]})); do
167: if $PRINT_ONLY; then
168: print -r -- "${_cmds[_i]}"
1.83 miod 169: else
1.177 rpe 170: eval "${_cmds[_i]}"
1.83 miod 171: fi
1.177 rpe 172: ((_i++))
173: done
174: unset _cmds
1.178 rpe 175: set +o noglob
1.83 miod 176: }
177:
1.161 rpe 178: # Start multiple interfaces by driver name.
179: # Usage: ifmstart "em iwm" "trunk vlan"
1.146 rpe 180: # Start "$1" interfaces in order or all interfaces if empty.
1.161 rpe 181: # Don't start "$2" interfaces. "$2" is optional.
1.105 todd 182: ifmstart() {
1.161 rpe 183: local _sifs=$1 _xifs=$2 _hn _if _sif _xif
184:
185: for _sif in ${_sifs:-ALL}; do
1.193 rpe 186: for _hn in /etc/hostname.+([[:alpha:]])+([[:digit:]]); do
187: [[ -f $_hn ]] || continue
1.161 rpe 188: _if=${_hn#/etc/hostname.}
1.113 david 189:
1.146 rpe 190: # Skip unwanted ifs.
1.161 rpe 191: for _xif in $_xifs; do
192: [[ $_xif == ${_if%%[0-9]*} ]] && continue 2
1.105 todd 193: done
194:
1.146 rpe 195: # Start wanted ifs.
1.161 rpe 196: [[ $_sif == @(ALL|${_if%%[0-9]*}) ]] && ifstart $_if
1.105 todd 197: done
198: done
199: }
200:
1.195 rpe 201: # Parse /etc/mygate and add default routes for IPv4 and IPv6.
1.172 mpi 202: # Usage: defaultroute
203: defaultroute() {
1.209 tb 204: local _cmd _v4set=false _v6set=false;
1.208 deraadt 205: set -o noglob
1.188 tb 206:
1.208 deraadt 207: stripcom /etc/mygate |
1.180 rpe 208: while read gw; do
1.208 deraadt 209: case $gw in
210: '!'*)
1.209 tb 211: _cmd=$(print -- "$gw")
1.208 deraadt 212: _cmd="${_cmd#!}"
213: ;;
1.209 tb 214: !(*:*))
1.214 sthen 215: ($_v4set || $V4_AUTOCONF) && continue
1.209 tb 216: _cmd="route -qn add -host default $gw"
217: _v4set=true
218: ;;
1.208 deraadt 219: *)
1.209 tb 220: ($_v6set || $V6_AUTOCONF) && continue
221: _cmd="route -qn add -host -inet6 default $gw"
222: _v6set=true
1.208 deraadt 223: ;;
224: esac
1.188 tb 225: if $PRINT_ONLY; then
1.208 deraadt 226: print -r -- "$_cmd"
1.188 tb 227: else
1.208 deraadt 228: $_cmd
1.188 tb 229: fi
1.172 mpi 230: done
1.208 deraadt 231: set +o noglob
1.172 mpi 232: }
1.199 tb 233:
1.215 bluhm 234: # add all the routes needed for IPv6
235: ip6routes() {
236: local _i=0
237: set -A _cmds
238:
239: # Disallow link-local unicast dest without outgoing scope identifiers.
240: _cmds[_i++]="route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject"
241:
242: # Disallow site-local unicast dest without outgoing scope identifiers.
243: # If you configure site-locals without scope id (it is permissible
244: # config for routers that are not on scope boundary), you may want
245: # to comment the line out.
246: _cmds[_i++]="route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject"
247:
248: # Disallow "internal" addresses to appear on the wire.
249: _cmds[_i++]="route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject"
250:
251: # Disallow packets to malicious 6to4 prefix.
252: _cmds[_i++]="route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject"
253: _cmds[_i++]="route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject"
254: _cmds[_i++]="route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject"
255: _cmds[_i++]="route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject"
256:
257: # Disallow packets without scope identifier.
258: _cmds[_i++]="route -qn add -inet6 ff01:: -prefixlen 16 ::1 -reject"
259: _cmds[_i++]="route -qn add -inet6 ff02:: -prefixlen 16 ::1 -reject"
260:
261: # Completely disallow packets to IPv4 compatible prefix.
262: #
263: # This may conflict with RFC1933 under following circumstances:
264: # (1) An IPv6-only KAME node tries to originate packets to IPv4
265: # compatible destination. The KAME node has no IPv4 compatible
266: # support. Under RFC1933, it should transmit native IPv6
267: # packets toward IPv4 compatible destination, hoping it would
268: # reach a router that forwards the packet toward auto-tunnel
269: # interface.
270: # (2) An IPv6-only node originates a packet to an IPv4 compatible
271: # destination. A KAME node is acting as an IPv6 router, and
272: # asked to forward it.
273: #
274: # Due to rare use of IPv4 compatible addresses, and security issues
275: # with it, we disable it by default.
276: _cmds[_i++]="route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject"
277:
278: # Apply the interface configuration commands stored in _cmds array.
279: _i=0
280: while ((_i < ${#_cmds[*]})); do
281: if $PRINT_ONLY; then
282: print -r -- "${_cmds[_i]}"
283: else
284: eval "${_cmds[_i]}"
285: fi
286: ((_i++))
287: done
288: unset _cmds
289: }
290:
1.218 florian 291: # wait for autoconf interfaces
292: wait_autoconf_default() {
293: if ifconfig | grep -q ': flags=.*<.*AUTOCONF.*>'; then
294: count=0
295: while ((count++ < 20)); do
296: route -n show | grep -q ^default && break
297: sleep .5
298: done
299: fi
300: }
301:
1.199 tb 302: # Make sure the invoking user has the right privileges. Check for presence of
303: # id(1) to avoid problems with diskless setups.
304: if [[ -x /usr/bin/id ]] && (($(id -u) != 0)); then
305: echo "${0##*/}: need root privileges"
306: exit 1
307: fi
1.154 sthen 308:
1.151 rpe 309: # Get network related vars from rc.conf using the parsing routine from rc.subr.
310: FUNCS_ONLY=1 . /etc/rc.d/rc.subr
1.150 ajacouto 311: _rc_parse_conf
1.177 rpe 312:
313: PRINT_ONLY=false
1.214 sthen 314: V4_AUTOCONF=false
1.180 rpe 315: V6_AUTOCONF=false
1.223 kn 316: IP6KERNEL=false
1.180 rpe 317:
1.177 rpe 318: while getopts ":n" opt; do
319: case $opt in
320: n) PRINT_ONLY=true;;
1.198 rpe 321: *) usage;;
1.177 rpe 322: esac
323: done
324: shift $((OPTIND-1))
325:
1.223 kn 326: if ifconfig lo0 inet6 >/dev/null 2>&1; then
327: IP6KERNEL=true
328: fi
329:
1.195 rpe 330: # Load key material for the generation of IPv6 Semantically Opaque Interface
1.224 kn 331: # Identifiers (SOII) used for SLAAC addresses.
1.189 florian 332: $PRINT_ONLY || [[ ! -f /etc/soii.key ]] ||
333: sysctl -q "net.inet6.ip6.soiikey=$(</etc/soii.key)"
1.1 deraadt 334:
1.83 miod 335: # If we were invoked with a list of interface names, just reconfigure these
1.172 mpi 336: # interfaces (or bridges), add default routes and return.
1.219 kn 337: # Create virtual interfaces upfront to make ifconfig commands depending on
338: # other interfaces, e.g. "patch", work regardless of in which order interface
339: # names were specified.
1.159 rpe 340: if (($# > 0)); then
1.219 kn 341: vifscreate "$@"
1.159 rpe 342: for _if; do ifstart $_if; done
1.172 mpi 343: defaultroute
1.83 miod 344: return
345: fi
346:
347: # Otherwise, process with the complete network initialization.
1.4 dm 348:
1.131 sobrado 349: # Set the address for the loopback interface. Bringing the interface up,
350: # automatically invokes the IPv6 address ::1.
1.215 bluhm 351: if $PRINT_ONLY; then
352: print -r -- "ifconfig lo0 inet 127.0.0.1/8"
353: else
354: ifconfig lo0 inet 127.0.0.1/8
1.225 ! kn 355: fi
! 356:
! 357: if $IP6KERNEL && ! $PRINT_ONLY; then
! 358: ip6routes
1.215 bluhm 359: fi
1.24 kstailey 360:
1.195 rpe 361: # Create all the pseudo interfaces up front.
1.191 dlg 362: vifscreate
1.105 todd 363:
364: # Configure all the non-loopback interfaces which we know about, but
1.127 deraadt 365: # do not start interfaces which must be delayed. Refer to hostname.if(5)
1.217 dlg 366: ifmstart "" "aggr trunk svlan vlan carp pppoe tun tap gif etherip gre egre nvgre eoip vxlan pflow wg"
1.56 itojun 367:
1.201 dlg 368: # The aggr and trunk interfaces need to come up first in this list.
1.132 mpf 369: # The (s)vlan interfaces need to come up after trunk.
1.118 brad 370: # Configure all the carp interfaces which we know about before default route.
1.201 dlg 371: ifmstart "aggr trunk svlan vlan carp pppoe"
1.102 mcbride 372:
1.195 rpe 373: # Set default routes for IPv4 and IPv6.
1.172 mpi 374: defaultroute
1.44 deraadt 375:
1.48 niklas 376: # Multicast routing.
1.168 sthen 377: if [[ $multicast != YES ]]; then
1.215 bluhm 378: if $PRINT_ONLY; then
379: print -r -- "route -qn delete 224.0.0.0/4"
380: print -r -- "route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject"
381: else
382: route -qn delete 224.0.0.0/4
383: route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject
384: fi
1.168 sthen 385: fi
1.105 todd 386:
1.146 rpe 387: # Reject 127/8 other than 127.0.0.1.
1.215 bluhm 388: if $PRINT_ONLY; then
389: print -r -- "route -qn add -net 127 127.0.0.1 -reject"
390: else
391: route -qn add -net 127 127.0.0.1 -reject
392: fi
1.218 florian 393:
394: # If interface autoconf exists, pause a little for at least one default route
1.222 kn 395: $PRINT_ONLY || wait_autoconf_default
1.191 dlg 396:
397: # Configure interfaces that rely on routing
1.217 dlg 398: ifmstart "tun tap gif etherip gre egre nvgre eoip vxlan pflow wg"
1.116 david 399:
1.223 kn 400: if $IP6KERNEL; then
1.195 rpe 401: # Ensure IPv6 Duplicate Address Detection (DAD) is completed.
1.124 markus 402: count=0
1.159 rpe 403: while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) != 0)); do
1.124 markus 404: sleep 1
405: done
1.116 david 406: fi