Annotation of src/etc/netstart, Revision 1.196
1.1 deraadt 1: #!/bin/sh -
2: #
1.196 ! landry 3: # $OpenBSD: netstart,v 1.195 2018/02/21 19:57:21 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[@]}"
1.180 rpe 45: V6_AUTOCONF=true
1.177 rpe 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"
1.180 rpe 63: V4_DHCPCONF=true
1.177 rpe 64: ;;
65: '!'*) _cmd=$(print -- "${_c[@]}" | sed 's/\$if/'$_if'/g')
66: _cmds[${#_cmds[*]}]="${_cmd#!}"
67: ;;
68: *) _cmds[${#_cmds[*]}]="ifconfig $_if ${_c[@]}"
69: ;;
70: esac
71: unset _c
1.178 rpe 72: set +o noglob
1.177 rpe 73: }
74:
1.192 rpe 75: # Create interface $1 if it does not yet exist.
1.195 rpe 76: # Usage: ifcreate if1
1.191 dlg 77: ifcreate() {
78: local _if=$1
79:
1.192 rpe 80: { ifconfig $_if || ifconfig $_if create; } >/dev/null 2>&1
1.191 dlg 81: }
82:
1.192 rpe 83: # Create interfaces for network pseudo-devices referred to by hostname.if files.
1.195 rpe 84: # Usage: vifscreate
1.191 dlg 85: vifscreate() {
1.192 rpe 86: local _vif _hn _if
1.191 dlg 87:
1.192 rpe 88: for _vif in $(ifconfig -C); do
1.193 rpe 89: for _hn in /etc/hostname.${_vif}+([[:digit:]]); do
1.191 dlg 90: [[ -f $_hn ]] || continue
91: _if=${_hn#/etc/hostname.}
92:
1.193 rpe 93: if ! ifcreate $_if; then
1.194 rpe 94: print -u2 "${0##*/}: create for '$_if' failed."
1.193 rpe 95: fi
1.191 dlg 96: done
97: done
98: }
99:
1.160 rpe 100: # Start a single interface.
101: # Usage: ifstart if1
1.83 miod 102: ifstart() {
1.187 tb 103: local _if=$1 _hn=/etc/hostname.$1 _cmds _i=0 _line _stat
1.177 rpe 104: set -A _cmds
1.174 rpe 105:
1.83 miod 106: # Interface names must be alphanumeric only. We check to avoid
107: # configuring backup or temp files, and to catch the "*" case.
1.177 rpe 108: [[ $_if != +([[:alpha:]])+([[:digit:]]) ]] && return
1.83 miod 109:
1.185 rpe 110: if [[ ! -f $_hn ]]; then
1.194 rpe 111: print -u2 "${0##*/}: $_hn: No such file or directory."
1.121 todd 112: return
113: fi
1.177 rpe 114:
1.146 rpe 115: # Not using stat(1), we can't rely on having /usr yet.
1.185 rpe 116: set -A _stat -- $(ls -nL $_hn)
1.183 rpe 117: if [[ "${_stat[0]}${_stat[2]}${_stat[3]}" != *---00 ]]; then
1.194 rpe 118: print -u2 "WARNING: $_hn is insecure, fixing permissions."
1.185 rpe 119: chmod -LR o-rwx $_hn
120: chown -LR root:wheel $_hn
1.119 deraadt 121: fi
1.83 miod 122:
1.177 rpe 123: # Check for ifconfig'able interface, except if -n option is specified.
124: if ! $PRINT_ONLY; then
1.191 dlg 125: ifcreate $_if || return
1.177 rpe 126: fi
127:
128: # Parse the hostname.if(5) file and fill _cmds array with interface
129: # configuration commands.
130: set -o noglob
131: while IFS= read -- _line; do
132: parse_hn_line $_line
1.185 rpe 133: done <$_hn
1.177 rpe 134:
135: # Apply the interface configuration commands stored in _cmds array.
136: while ((_i < ${#_cmds[*]})); do
137: if $PRINT_ONLY; then
138: print -r -- "${_cmds[_i]}"
1.83 miod 139: else
1.177 rpe 140: eval "${_cmds[_i]}"
1.83 miod 141: fi
1.177 rpe 142: ((_i++))
143: done
144: unset _cmds
1.178 rpe 145: set +o noglob
1.83 miod 146: }
147:
1.161 rpe 148: # Start multiple interfaces by driver name.
149: # Usage: ifmstart "em iwm" "trunk vlan"
1.146 rpe 150: # Start "$1" interfaces in order or all interfaces if empty.
1.161 rpe 151: # Don't start "$2" interfaces. "$2" is optional.
1.105 todd 152: ifmstart() {
1.161 rpe 153: local _sifs=$1 _xifs=$2 _hn _if _sif _xif
154:
155: for _sif in ${_sifs:-ALL}; do
1.193 rpe 156: for _hn in /etc/hostname.+([[:alpha:]])+([[:digit:]]); do
157: [[ -f $_hn ]] || continue
1.161 rpe 158: _if=${_hn#/etc/hostname.}
1.113 david 159:
1.146 rpe 160: # Skip unwanted ifs.
1.161 rpe 161: for _xif in $_xifs; do
162: [[ $_xif == ${_if%%[0-9]*} ]] && continue 2
1.105 todd 163: done
164:
1.146 rpe 165: # Start wanted ifs.
1.161 rpe 166: [[ $_sif == @(ALL|${_if%%[0-9]*}) ]] && ifstart $_if
1.105 todd 167: done
168: done
169: }
170:
1.195 rpe 171: # Parse /etc/mygate and add default routes for IPv4 and IPv6.
1.172 mpi 172: # Usage: defaultroute
173: defaultroute() {
1.188 tb 174: local _cmd;
175:
1.180 rpe 176: ! $V4_DHCPCONF && stripcom /etc/mygate |
177: while read gw; do
178: [[ $gw == @(*:*) ]] && continue
1.188 tb 179: _cmd="route -qn add -host default $gw"
180: if $PRINT_ONLY; then
181: print -r -- "$_cmd" && break
182: else
183: $_cmd && break
184: fi
1.172 mpi 185: done
1.180 rpe 186: ! $V6_AUTOCONF && stripcom /etc/mygate |
187: while read gw; do
188: [[ $gw == !(*:*) ]] && continue
1.188 tb 189: _cmd="route -qn add -host -inet6 default $gw"
190: if $PRINT_ONLY; then
191: print -r -- "$_cmd" && break
192: else
193: $_cmd && break
194: fi
1.172 mpi 195: done
196: }
1.154 sthen 197:
1.151 rpe 198: # Get network related vars from rc.conf using the parsing routine from rc.subr.
199: FUNCS_ONLY=1 . /etc/rc.d/rc.subr
1.150 ajacouto 200: _rc_parse_conf
1.177 rpe 201:
202: PRINT_ONLY=false
1.196 ! landry 203: USAGE="usage: ${0##*/} [[-n] interface ...]"
1.180 rpe 204: V4_DHCPCONF=false
205: V6_AUTOCONF=false
206:
1.177 rpe 207: while getopts ":n" opt; do
208: case $opt in
209: n) PRINT_ONLY=true;;
210: *) print -u2 "$USAGE"; exit 1;;
211: esac
212: done
213: shift $((OPTIND-1))
214:
215: # Option -n is only supported if interface names are specified as parameters.
216: if $PRINT_ONLY && (($# == 0)); then
217: print -u2 "Missing parameters.\n$USAGE"
218: exit 1
219: fi
1.189 florian 220:
1.195 rpe 221: # Load key material for the generation of IPv6 Semantically Opaque Interface
222: # Identifiers (SOII) used for link local and SLAAC addresses.
1.189 florian 223: $PRINT_ONLY || [[ ! -f /etc/soii.key ]] ||
224: sysctl -q "net.inet6.ip6.soiikey=$(</etc/soii.key)"
1.1 deraadt 225:
1.83 miod 226: # If we were invoked with a list of interface names, just reconfigure these
1.172 mpi 227: # interfaces (or bridges), add default routes and return.
1.159 rpe 228: if (($# > 0)); then
229: for _if; do ifstart $_if; done
1.172 mpi 230: defaultroute
1.83 miod 231: return
232: fi
233:
234: # Otherwise, process with the complete network initialization.
235:
1.146 rpe 236: # /etc/myname contains my symbolic name.
1.159 rpe 237: [[ -f /etc/myname ]] && hostname "$(stripcom /etc/myname)"
1.4 dm 238:
1.131 sobrado 239: # Set the address for the loopback interface. Bringing the interface up,
240: # automatically invokes the IPv6 address ::1.
1.129 henning 241: ifconfig lo0 inet 127.0.0.1/8
1.24 kstailey 242:
1.195 rpe 243: # IPv6 configuration.
1.54 itojun 244: if ifconfig lo0 inet6 >/dev/null 2>&1; then
245: ip6kernel=YES
246:
1.83 miod 247: # Disallow link-local unicast dest without outgoing scope identifiers.
1.147 rpe 248: route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject >/dev/null
1.66 itojun 249:
1.83 miod 250: # Disallow site-local unicast dest without outgoing scope identifiers.
1.66 itojun 251: # If you configure site-locals without scope id (it is permissible
252: # config for routers that are not on scope boundary), you may want
253: # to comment the line out.
1.147 rpe 254: route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject >/dev/null
1.66 itojun 255:
1.83 miod 256: # Disallow "internal" addresses to appear on the wire.
1.147 rpe 257: route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject >/dev/null
1.66 itojun 258:
1.83 miod 259: # Disallow packets to malicious 6to4 prefix.
1.147 rpe 260: route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject >/dev/null
261: route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject >/dev/null
262: route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject >/dev/null
263: route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject >/dev/null
1.115 itojun 264:
265: # Disallow packets without scope identifier.
1.147 rpe 266: route -qn add -inet6 ff01:: -prefixlen 16 ::1 -reject >/dev/null
267: route -qn add -inet6 ff02:: -prefixlen 16 ::1 -reject >/dev/null
1.66 itojun 268:
269: # Completely disallow packets to IPv4 compatible prefix.
1.146 rpe 270: #
1.66 itojun 271: # This may conflict with RFC1933 under following circumstances:
272: # (1) An IPv6-only KAME node tries to originate packets to IPv4
1.77 deraadt 273: # compatible destination. The KAME node has no IPv4 compatible
1.66 itojun 274: # support. Under RFC1933, it should transmit native IPv6
275: # packets toward IPv4 compatible destination, hoping it would
276: # reach a router that forwards the packet toward auto-tunnel
277: # interface.
1.77 deraadt 278: # (2) An IPv6-only node originates a packet to an IPv4 compatible
1.66 itojun 279: # destination. A KAME node is acting as an IPv6 router, and
280: # asked to forward it.
1.146 rpe 281: #
1.77 deraadt 282: # Due to rare use of IPv4 compatible addresses, and security issues
1.66 itojun 283: # with it, we disable it by default.
1.147 rpe 284: route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject >/dev/null
1.54 itojun 285: else
286: ip6kernel=NO
287: fi
288:
1.195 rpe 289: # Create all the pseudo interfaces up front.
1.191 dlg 290: vifscreate
1.105 todd 291:
292: # Configure all the non-loopback interfaces which we know about, but
1.127 deraadt 293: # do not start interfaces which must be delayed. Refer to hostname.if(5)
1.191 dlg 294: ifmstart "" "trunk svlan vlan carp pppoe tun tap gif etherip gre egre mobileip pflow"
1.56 itojun 295:
1.118 brad 296: # The trunk interfaces need to come up first in this list.
1.132 mpf 297: # The (s)vlan interfaces need to come up after trunk.
1.118 brad 298: # Configure all the carp interfaces which we know about before default route.
1.191 dlg 299: ifmstart "trunk svlan vlan carp pppoe"
1.102 mcbride 300:
1.195 rpe 301: # Set default routes for IPv4 and IPv6.
1.172 mpi 302: defaultroute
1.44 deraadt 303:
1.48 niklas 304: # Multicast routing.
1.168 sthen 305: if [[ $multicast != YES ]]; then
306: route -qn delete 224.0.0.0/4 >/dev/null 2>&1
1.147 rpe 307: route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject >/dev/null
1.168 sthen 308: fi
1.105 todd 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.191 dlg 312:
313: # Configure interfaces that rely on routing
314: ifmstart "tun tap gif etherip gre egre mobileip pflow"
1.116 david 315:
1.159 rpe 316: if [[ $ip6kernel == YES ]]; then
1.195 rpe 317: # Ensure IPv6 Duplicate Address Detection (DAD) is completed.
1.124 markus 318: count=0
1.159 rpe 319: while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) != 0)); do
1.124 markus 320: sleep 1
321: done
1.116 david 322: fi