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