Annotation of src/etc/netstart, Revision 1.173
1.1 deraadt 1: #!/bin/sh -
2: #
1.173 ! rpe 3: # $OpenBSD: netstart,v 1.172 2016/12/06 14:01:43 mpi 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.160 rpe 22: # Start a single interface.
23: # Usage: ifstart if1
1.83 miod 24: ifstart() {
1.84 deraadt 25: if=$1
1.83 miod 26: # Interface names must be alphanumeric only. We check to avoid
27: # configuring backup or temp files, and to catch the "*" case.
1.137 rpe 28: [[ $if != +([[:alpha:]])+([[:digit:]]) ]] && return
1.83 miod 29:
1.119 deraadt 30: file=/etc/hostname.$if
1.121 todd 31: if ! [ -f $file ]; then
32: echo "netstart: $file: No such file or directory"
33: return
34: fi
1.146 rpe 35: # Not using stat(1), we can't rely on having /usr yet.
1.166 rpe 36: set -A stat -- $(ls -nL $file)
1.123 sthen 37: if [ "${stat[0]#???????} ${stat[2]} ${stat[3]}" != "--- 0 0" ]; then
1.119 deraadt 38: echo "WARNING: $file is insecure, fixing permissions"
1.122 sthen 39: chmod -LR o-rwx $file
40: chown -LR root.wheel $file
1.119 deraadt 41: fi
1.136 rpe 42: # Check for ifconfig'able interface.
43: (ifconfig $if || ifconfig $if create) >/dev/null 2>&1 || return
1.83 miod 44:
1.146 rpe 45: # Now parse the hostname.* file.
1.83 miod 46: while :; do
47: if [ "$cmd2" ]; then
48: # We are carrying over from the 'read dt dtaddr'
49: # last time.
50: set -- $cmd2
51: af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2=
52: # Make sure and get any remaining args in ext2,
1.146 rpe 53: # like the read below.
1.83 miod 54: i=1
1.126 simon 55: while [ $i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done
1.83 miod 56: ext2="$@"
57: else
58: # Read the next line or exit the while loop.
59: read af name mask bcaddr ext1 ext2 || break
60: fi
1.146 rpe 61: # $af can be "dhcp", "up", "rtsol", an address family, commands,
62: # or a comment.
1.83 miod 63: case "$af" in
1.146 rpe 64: "#"*|"") # Skip comments and empty lines.
1.83 miod 65: continue
66: ;;
1.146 rpe 67: "!"*) # Parse commands.
1.83 miod 68: cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}"
69: ;;
70: "dhcp")
71: [ "$name" = "NONE" ] && name=
72: [ "$mask" = "NONE" ] && mask=
73: [ "$bcaddr" = "NONE" ] && bcaddr=
1.143 claudio 74: cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 down"
1.106 todd 75: cmd="$cmd;dhclient $if"
1.110 todd 76: dhcpif="$dhcpif $if"
1.83 miod 77: ;;
78: "rtsol")
1.84 deraadt 79: rtsolif="$rtsolif $if"
1.106 todd 80: cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
1.83 miod 81: ;;
82: *)
83: read dt dtaddr
84: if [ "$name" = "alias" ]; then
1.146 rpe 85: # Perform a 'shift' of sorts.
1.83 miod 86: alias=$name
87: name=$mask
88: mask=$bcaddr
89: bcaddr=$ext1
90: ext1=$ext2
91: ext2=
92: else
93: alias=
94: fi
1.114 todd 95: cmd="ifconfig $if $af $alias $name"
1.83 miod 96: case "$dt" in
97: dest)
98: cmd="$cmd $dtaddr"
99: ;;
1.130 todd 100: *)
1.83 miod 101: cmd2="$dt $dtaddr"
102: ;;
103: esac
104: case $af in
105: inet)
1.128 todd 106: if [ ! -n "$name" ]; then
107: echo "/etc/hostname.$if: inet alone is invalid"
108: return
109: fi
1.83 miod 110: [ "$mask" ] && cmd="$cmd netmask $mask"
111: if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
112: cmd="$cmd broadcast $bcaddr"
113: fi
114: ;;
1.128 todd 115: inet6)
116: if [ ! -n "$name" ]; then
117: echo "/etc/hostname.$if: inet6 alone is invalid"
118: return
119: fi
120: [ "$mask" ] && cmd="$cmd prefixlen $mask"
1.83 miod 121: cmd="$cmd $bcaddr"
122: ;;
123: *)
124: cmd="$cmd $mask $bcaddr"
125: ;;
126: esac
1.139 mpi 127: cmd="$cmd $ext1 $ext2"
1.83 miod 128: ;;
129: esac
130: eval "$cmd"
1.147 rpe 131: done </etc/hostname.$if
1.83 miod 132: }
133:
1.161 rpe 134: # Start multiple interfaces by driver name.
135: # Usage: ifmstart "em iwm" "trunk vlan"
1.146 rpe 136: # Start "$1" interfaces in order or all interfaces if empty.
1.161 rpe 137: # Don't start "$2" interfaces. "$2" is optional.
1.105 todd 138: ifmstart() {
1.161 rpe 139: local _sifs=$1 _xifs=$2 _hn _if _sif _xif
140:
141: for _sif in ${_sifs:-ALL}; do
142: for _hn in /etc/hostname.*; do
143: _if=${_hn#/etc/hostname.}
144: [[ $_if == '*' ]] && continue
1.113 david 145:
1.146 rpe 146: # Skip unwanted ifs.
1.161 rpe 147: for _xif in $_xifs; do
148: [[ $_xif == ${_if%%[0-9]*} ]] && continue 2
1.105 todd 149: done
150:
1.146 rpe 151: # Start wanted ifs.
1.161 rpe 152: [[ $_sif == @(ALL|${_if%%[0-9]*}) ]] && ifstart $_if
1.105 todd 153: done
154: done
155: }
156:
1.162 rpe 157: # IPv6 autoconf the interfaces in the $rtsolif list.
158: # Usage: ifv6autoconf
159: ifv6autoconf() {
160: local _if
161:
1.156 sthen 162: # $ip6kernel will not have been set if we were invoked with a
163: # list of interface names
1.162 rpe 164: ifconfig lo0 inet6 >/dev/null 2>&1 || return 0
165:
166: for _if in $rtsolif; do
167: ifconfig $_if inet6 autoconf
168: done
1.154 sthen 169: }
1.170 jasper 170:
1.172 mpi 171: # Parse /etc/mygate and add default routes for IPv4 and IPv6
172: # Usage: defaultroute
173: defaultroute() {
174: [[ -z $dhcpif ]] && stripcom /etc/mygate | while read gw; do
175: [[ $gw == @(*:*) ]] && continue
176: route -qn delete default >/dev/null 2>&1
177: route -qn add -host default $gw && break
178: done
179: [[ -z $rtsolif ]] && stripcom /etc/mygate | while read gw; do
180: [[ $gw == !(*:*) ]] && continue
181: route -qn delete -inet6 default >/dev/null 2>&1
182: route -qn add -host -inet6 default $gw && break
183: done
184: }
185:
1.170 jasper 186: # Make sure the invoking user has the right privileges.
187: if (($(id -u) != 0)); then
188: echo "${0##*/}: need root privileges"
189: exit 1
190: fi
1.154 sthen 191:
1.151 rpe 192: # Get network related vars from rc.conf using the parsing routine from rc.subr.
193: FUNCS_ONLY=1 . /etc/rc.d/rc.subr
1.150 ajacouto 194: _rc_parse_conf
1.1 deraadt 195:
1.83 miod 196: # If we were invoked with a list of interface names, just reconfigure these
1.172 mpi 197: # interfaces (or bridges), add default routes and return.
1.159 rpe 198: if (($# > 0)); then
199: for _if; do ifstart $_if; done
1.162 rpe 200: ifv6autoconf
1.172 mpi 201: defaultroute
1.83 miod 202: return
203: fi
204:
205: # Otherwise, process with the complete network initialization.
206:
1.146 rpe 207: # /etc/myname contains my symbolic name.
1.159 rpe 208: [[ -f /etc/myname ]] && hostname "$(stripcom /etc/myname)"
1.4 dm 209:
1.131 sobrado 210: # Set the address for the loopback interface. Bringing the interface up,
211: # automatically invokes the IPv6 address ::1.
1.129 henning 212: ifconfig lo0 inet 127.0.0.1/8
1.24 kstailey 213:
1.54 itojun 214: if ifconfig lo0 inet6 >/dev/null 2>&1; then
215: # IPv6 configurations.
216: ip6kernel=YES
217:
1.83 miod 218: # Disallow link-local unicast dest without outgoing scope identifiers.
1.147 rpe 219: route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject >/dev/null
1.66 itojun 220:
1.83 miod 221: # Disallow site-local unicast dest without outgoing scope identifiers.
1.66 itojun 222: # If you configure site-locals without scope id (it is permissible
223: # config for routers that are not on scope boundary), you may want
224: # to comment the line out.
1.147 rpe 225: route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject >/dev/null
1.66 itojun 226:
1.83 miod 227: # Disallow "internal" addresses to appear on the wire.
1.147 rpe 228: route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject >/dev/null
1.66 itojun 229:
1.83 miod 230: # Disallow packets to malicious IPv4 compatible prefix.
1.147 rpe 231: route -qn add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject >/dev/null
232: route -qn add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject >/dev/null
233: route -qn add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject >/dev/null
234: route -qn add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject >/dev/null
1.66 itojun 235:
1.83 miod 236: # Disallow packets to malicious 6to4 prefix.
1.147 rpe 237: route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject >/dev/null
238: route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject >/dev/null
239: route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject >/dev/null
240: route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject >/dev/null
1.115 itojun 241:
242: # Disallow packets without scope identifier.
1.147 rpe 243: route -qn add -inet6 ff01:: -prefixlen 16 ::1 -reject >/dev/null
244: route -qn add -inet6 ff02:: -prefixlen 16 ::1 -reject >/dev/null
1.66 itojun 245:
246: # Completely disallow packets to IPv4 compatible prefix.
1.146 rpe 247: #
1.66 itojun 248: # This may conflict with RFC1933 under following circumstances:
249: # (1) An IPv6-only KAME node tries to originate packets to IPv4
1.77 deraadt 250: # compatible destination. The KAME node has no IPv4 compatible
1.66 itojun 251: # support. Under RFC1933, it should transmit native IPv6
252: # packets toward IPv4 compatible destination, hoping it would
253: # reach a router that forwards the packet toward auto-tunnel
254: # interface.
1.77 deraadt 255: # (2) An IPv6-only node originates a packet to an IPv4 compatible
1.66 itojun 256: # destination. A KAME node is acting as an IPv6 router, and
257: # asked to forward it.
1.146 rpe 258: #
1.77 deraadt 259: # Due to rare use of IPv4 compatible addresses, and security issues
1.66 itojun 260: # with it, we disable it by default.
1.147 rpe 261: route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject >/dev/null
1.56 itojun 262:
263: rtsolif=""
1.54 itojun 264: else
265: ip6kernel=NO
266: fi
267:
1.105 todd 268:
269: # Configure all the non-loopback interfaces which we know about, but
1.127 deraadt 270: # do not start interfaces which must be delayed. Refer to hostname.if(5)
1.171 rzalamen 271: ifmstart "" "trunk svlan vlan carp gif gre pfsync pppoe tun bridge switch pflow"
1.56 itojun 272:
1.118 brad 273: # The trunk interfaces need to come up first in this list.
1.132 mpf 274: # The (s)vlan interfaces need to come up after trunk.
1.118 brad 275: # Configure all the carp interfaces which we know about before default route.
1.132 mpf 276: ifmstart "trunk svlan vlan carp"
1.118 brad 277:
1.154 sthen 278: # Now that $rtsolif has been populated, IPv6 autoconf those interfaces
1.162 rpe 279: ifv6autoconf
1.102 mcbride 280:
1.138 todd 281: # Look for default routes in /etc/mygate.
1.172 mpi 282: defaultroute
1.44 deraadt 283:
1.48 niklas 284: # Multicast routing.
1.168 sthen 285: if [[ $multicast != YES ]]; then
286: route -qn delete 224.0.0.0/4 >/dev/null 2>&1
1.147 rpe 287: route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject >/dev/null
1.168 sthen 288: fi
1.105 todd 289:
1.152 florian 290: # Configure PPPoE, GIF, GRE, TUN and PFLOW interfaces, delayed because they
291: # require routes to be set. TUN might depend on PPPoE, and GIF or GRE may
292: # depend on either of them. PFLOW might bind to ip addresses configured
293: # on either of them.
1.171 rzalamen 294: ifmstart "pppoe tun gif gre bridge switch pflow"
1.92 deraadt 295:
1.146 rpe 296: # Reject 127/8 other than 127.0.0.1.
1.147 rpe 297: route -qn add -net 127 127.0.0.1 -reject >/dev/null
1.116 david 298:
1.159 rpe 299: if [[ $ip6kernel == YES ]]; then
1.146 rpe 300: # This is to make sure DAD is completed before going further.
1.124 markus 301: count=0
1.159 rpe 302: while ((count++ < 10 && $(sysctl -n net.inet6.ip6.dad_pending) != 0)); do
1.124 markus 303: sleep 1
304: done
1.116 david 305: fi