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