Annotation of src/etc/netstart, Revision 1.123
1.1 deraadt 1: #!/bin/sh -
2: #
1.123 ! sthen 3: # $OpenBSD: netstart,v 1.122 2008/07/23 16:05:47 sthen Exp $
1.101 millert 4:
5: # Strip comments (and leading/trailing whitespace if IFS is set)
6: # from a file and spew to stdout
7: stripcom() {
1.108 todd 8: local _l
1.110 todd 9: [[ -f $1 ]] || return
1.108 todd 10: while read _l; do
11: [[ -n ${_l%%#*} ]] && echo $_l
12: done<$1
1.101 millert 13: }
1.45 millert 14:
15: # Returns true if $1 contains only alphanumerics
16: isalphanumeric() {
17: local _n
18: _n=$1
19: while [ ${#_n} != 0 ]; do
20: case $_n in
21: [A-Za-z0-9]*) ;;
22: *) return 1;;
23: esac
24: _n=${_n#?}
25: done
26: return 0
27: }
1.81 angelos 28:
1.83 miod 29: # Start the $1 interface
30: ifstart() {
1.84 deraadt 31: if=$1
1.83 miod 32: # Interface names must be alphanumeric only. We check to avoid
33: # configuring backup or temp files, and to catch the "*" case.
1.84 deraadt 34: if ! isalphanumeric "$if"; then
1.83 miod 35: return
36: fi
37:
1.119 deraadt 38: file=/etc/hostname.$if
1.121 todd 39: if ! [ -f $file ]; then
40: echo "netstart: $file: No such file or directory"
41: return
42: fi
1.123 ! sthen 43: # Not using stat(1), we can't rely on having /usr yet
! 44: set -A stat -- `ls -nL $file`
! 45: if [ "${stat[0]#???????} ${stat[2]} ${stat[3]}" != "--- 0 0" ]; then
1.119 deraadt 46: echo "WARNING: $file is insecure, fixing permissions"
1.122 sthen 47: chmod -LR o-rwx $file
48: chown -LR root.wheel $file
1.119 deraadt 49: fi
1.84 deraadt 50: ifconfig $if > /dev/null 2>&1
1.83 miod 51: if [ "$?" != "0" ]; then
1.89 markus 52: # Try to create interface if it does not exist
53: ifconfig $if create > /dev/null 2>&1
54: if [ "$?" != "0" ]; then
55: return
56: fi
1.83 miod 57: fi
58:
59: # Now parse the hostname.* file
60: while :; do
61: if [ "$cmd2" ]; then
62: # We are carrying over from the 'read dt dtaddr'
63: # last time.
64: set -- $cmd2
65: af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2=
66: # Make sure and get any remaining args in ext2,
67: # like the read below
68: i=1
69: while [ i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done
70: ext2="$@"
71: else
72: # Read the next line or exit the while loop.
73: read af name mask bcaddr ext1 ext2 || break
74: fi
75: # $af can be "dhcp", "up", "rtsol", an address family,
76: # commands, or a comment.
77: case "$af" in
78: "#"*|"") # skip comments and empty lines
79: continue
80: ;;
81: "!"*) # parse commands
82: cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}"
83: ;;
84: "bridge")
1.84 deraadt 85: cmd="echo /etc/hostname.$if: bridges now supported via bridgename.* files"
1.83 miod 86: ;;
87: "dhcp")
88: [ "$name" = "NONE" ] && name=
89: [ "$mask" = "NONE" ] && mask=
90: [ "$bcaddr" = "NONE" ] && bcaddr=
1.106 todd 91: cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 down"
92: cmd="$cmd;dhclient $if"
1.110 todd 93: dhcpif="$dhcpif $if"
1.83 miod 94: ;;
95: "rtsol")
1.84 deraadt 96: rtsolif="$rtsolif $if"
1.106 todd 97: cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
1.83 miod 98: ;;
99: "up")
1.84 deraadt 100: # The only one of these guaranteed to be set is $if.
1.83 miod 101: # The remaining ones exist so that media controls work.
1.84 deraadt 102: cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
1.83 miod 103: ;;
104: *)
105: read dt dtaddr
106: if [ "$name" = "alias" ]; then
107: # perform a 'shift' of sorts
108: alias=$name
109: name=$mask
110: mask=$bcaddr
111: bcaddr=$ext1
112: ext1=$ext2
113: ext2=
114: else
115: alias=
116: fi
1.114 todd 117: cmd="ifconfig $if $af $alias $name"
1.83 miod 118: case "$dt" in
119: dest)
120: cmd="$cmd $dtaddr"
121: ;;
122: [a-z!]*)
123: cmd2="$dt $dtaddr"
124: ;;
125: esac
126: if [ ! -n "$name" ]; then
1.84 deraadt 127: echo "/etc/hostname.$if: invalid network configuration file"
1.83 miod 128: return
129: fi
130: case $af in
131: inet)
132: [ "$mask" ] && cmd="$cmd netmask $mask"
133: if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
134: cmd="$cmd broadcast $bcaddr"
135: fi
1.91 deraadt 136: [ "$alias" ] && rtcmd=";route -qn add -host $name 127.0.0.1"
1.83 miod 137: ;;
138: inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask"
139: cmd="$cmd $bcaddr"
140: ;;
141: *)
142: cmd="$cmd $mask $bcaddr"
143: ;;
144: esac
145: cmd="$cmd $ext1 $ext2$rtcmd" rtcmd=
146: ;;
147: esac
148: eval "$cmd"
1.84 deraadt 149: done < /etc/hostname.$if
1.83 miod 150: }
151:
1.105 todd 152: # Start multiple:
153: # start "$1" interfaces in order or all interfaces if empty
154: # don't start "$2" interfaces
155: ifmstart() {
156: for sif in ${1:-ALL}; do
157: for hn in /etc/hostname.*; do
158: # Strip off /etc/hostname. prefix
159: if=${hn#/etc/hostname.}
160: test "$if" = "*" && continue
1.113 david 161:
1.105 todd 162: # Skip unwanted ifs
1.113 david 163: s=""
1.105 todd 164: for xf in $2; do
165: test "$xf" = "${if%%[0-9]*}" && s="1" && break
166: done
167: test "$s" = "1" && continue
168:
169: # Start wanted ifs
170: test "$sif" = "ALL" -o \
171: "$sif" = "${if%%[0-9]*}" \
172: && ifstart $if
173: done
174: done
175: }
176:
1.83 miod 177: # Start the $1 bridge
178: bridgestart() {
179: # Interface names must be alphanumeric only. We check to avoid
180: # configuring backup or temp files, and to catch the "*" case.
181: if ! isalphanumeric "$1"; then
182: return
183: fi
184: brconfig $1 > /dev/null 2>&1
185: if [ "$?" != "0" ]; then
1.90 millert 186: # Try to create interface if it does not exist
1.111 todd 187: ifconfig $1 create > /dev/null 2>&1
1.90 millert 188: if [ "$?" != "0" ]; then
189: return
190: fi
1.83 miod 191: fi
192:
193: # Now parse the bridgename.* file
194: # All lines are run as brconfig(8) commands.
195: while read line ; do
196: line=${line%%#*} # strip comments
197: test -z "$line" && continue
198: case "$line" in
199: "!"*)
200: cmd="${line#*!}"
201: ;;
202: *)
203: cmd="brconfig $1 $line"
204: ;;
205: esac
206: eval "$cmd"
207: done < /etc/bridgename.$1
208: }
209:
1.81 angelos 210: # Re-read /etc/rc.conf
211: . /etc/rc.conf
1.1 deraadt 212:
1.83 miod 213: # If we were invoked with a list of interface names, just reconfigure these
214: # interfaces (or bridges) and return.
215: if [ $1x = autobootx ]; then
216: shift
217: fi
218: if [ $# -gt 0 ]; then
219: while [ $# -gt 0 ]; do
220: if [ -f /etc/bridgename.$1 ]; then
221: bridgestart $1
222: else
223: ifstart $1
224: fi
225: shift
226: done
227: return
228: fi
229:
230: # Otherwise, process with the complete network initialization.
231:
1.1 deraadt 232: # /etc/myname contains my symbolic name
1.87 henning 233: if [ -f /etc/myname ]; then
1.100 millert 234: hostname=`stripcom /etc/myname`
1.87 henning 235: hostname $hostname
236: else
237: hostname=`hostname`
238: fi
239:
1.1 deraadt 240: if [ -f /etc/defaultdomain ]; then
1.100 millert 241: domainname `stripcom /etc/defaultdomain`
1.4 dm 242: fi
243:
1.93 deraadt 244: # Set the address for the loopback interface. Bringing the
245: # interface up, automatically invokes the IPv6 address ::1)
1.92 deraadt 246: ifconfig lo0 inet 127.0.0.1
1.24 kstailey 247:
1.54 itojun 248: if ifconfig lo0 inet6 >/dev/null 2>&1; then
249: # IPv6 configurations.
250: ip6kernel=YES
251:
1.83 miod 252: # Disallow link-local unicast dest without outgoing scope identifiers.
1.98 deraadt 253: route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null
1.66 itojun 254:
1.83 miod 255: # Disallow site-local unicast dest without outgoing scope identifiers.
1.66 itojun 256: # If you configure site-locals without scope id (it is permissible
257: # config for routers that are not on scope boundary), you may want
258: # to comment the line out.
1.98 deraadt 259: route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null
1.66 itojun 260:
1.83 miod 261: # Disallow "internal" addresses to appear on the wire.
1.98 deraadt 262: route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
1.66 itojun 263:
1.83 miod 264: # Disallow packets to malicious IPv4 compatible prefix.
1.98 deraadt 265: route -qn add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null
266: route -qn add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
267: route -qn add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
268: route -qn add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
1.66 itojun 269:
1.83 miod 270: # Disallow packets to malicious 6to4 prefix.
1.98 deraadt 271: route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null
272: route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null
273: route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null
274: route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null
1.115 itojun 275:
276: # Disallow packets without scope identifier.
277: route -qn add -inet6 ff01:: -prefixlen 16 ::1 -reject > /dev/null
278: route -qn add -inet6 ff02:: -prefixlen 16 ::1 -reject > /dev/null
1.66 itojun 279:
280: # Completely disallow packets to IPv4 compatible prefix.
281: # This may conflict with RFC1933 under following circumstances:
282: # (1) An IPv6-only KAME node tries to originate packets to IPv4
1.77 deraadt 283: # compatible destination. The KAME node has no IPv4 compatible
1.66 itojun 284: # support. Under RFC1933, it should transmit native IPv6
285: # packets toward IPv4 compatible destination, hoping it would
286: # reach a router that forwards the packet toward auto-tunnel
287: # interface.
1.77 deraadt 288: # (2) An IPv6-only node originates a packet to an IPv4 compatible
1.66 itojun 289: # destination. A KAME node is acting as an IPv6 router, and
290: # asked to forward it.
1.77 deraadt 291: # Due to rare use of IPv4 compatible addresses, and security issues
1.66 itojun 292: # with it, we disable it by default.
1.98 deraadt 293: route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
1.56 itojun 294:
295: rtsolif=""
1.54 itojun 296: else
297: ip6kernel=NO
298: fi
299:
1.105 todd 300:
301: # Configure all the non-loopback interfaces which we know about, but
302: # do not start interfaces which must be delayed.
1.83 miod 303: # Refer to hostname.if(5) and bridgename.if(5)
1.107 todd 304: ifmstart "" "trunk vlan carp gif gre pfsync pppoe"
1.56 itojun 305:
1.118 brad 306: # The trunk interfaces need to come up first in this list.
307: # The vlan interfaces need to come up after trunk.
308: # Configure all the carp interfaces which we know about before default route.
309: ifmstart "trunk vlan carp"
310:
1.56 itojun 311: if [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
312: fw=`sysctl -n net.inet6.ip6.forwarding`
313: ra=`sysctl -n net.inet6.ip6.accept_rtadv`
314: if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
1.57 itojun 315: echo "IPv6 autoconf:$rtsolif"
1.56 itojun 316: rtsol $rtsolif
317: else
318: echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
319: fi
1.57 itojun 320: fi
1.102 mcbride 321:
1.14 deraadt 322: # /etc/mygate, if it exists, contains the name of my gateway host
323: # that name must be in /etc/hosts.
1.110 todd 324: [[ -z $dhcpif ]] && stripcom /etc/mygate | while read gw; do
325: [[ $gw == @(*:*) ]] && continue
326: route -qn delete default > /dev/null 2>&1
327: route -qn add -host default $gw && break
328: done
329: [[ -z $rtsolif ]] && stripcom /etc/mygate | while read gw; do
330: [[ $gw == !(*:*) ]] && continue
331: route -qn delete -inet6 default > /dev/null 2>&1
332: route -qn add -host -inet6 default $gw && break
333: done
1.44 deraadt 334:
1.48 niklas 335: # Multicast routing.
336: #
337: # The routing to the 224.0.0.0/4 net is setup according to these rules:
338: # multicast_host multicast_router route comment
339: # NO NO -reject no multicast
340: # NO YES none installed daemon will run
341: # YES/interface NO -interface YES=def. iface
342: # Any other combination -reject config error
1.112 reyk 343: route -qn delete 224.0.0.0/4 > /dev/null 2>&1
1.48 niklas 344: case "$multicast_host:$multicast_router" in
345: NO:NO)
1.91 deraadt 346: route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
1.67 deraadt 347: ;;
1.48 niklas 348: NO:YES)
349: ;;
350: *:NO)
1.112 reyk 351: maddr=`if [ "$multicast_host" = "YES" ]; then
1.91 deraadt 352: ed -s '!route -qn show -inet' <<EOF
1.48 niklas 353: /^default/p
354: EOF
355: else
356: ed -s "!ifconfig $multicast_host" <<EOF
357: /^ inet /p
358: EOF
1.112 reyk 359: fi 2> /dev/null`
360: if [ "X${maddr}" != "X" ]; then
361: set $maddr
362: route -qn add -net 224.0.0.0/4 -interface $2 > /dev/null
363: else
364: route -qn add -net 224.0.0.0/4 -interface \
365: 127.0.0.1 -reject > /dev/null
366: fi
1.67 deraadt 367: ;;
1.48 niklas 368: *:*)
369: echo 'config error, multicasting disabled until rc.conf is fixed'
1.91 deraadt 370: route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
1.67 deraadt 371: ;;
1.48 niklas 372: esac
1.83 miod 373:
1.105 todd 374:
375: # Configure PPPoE, GIF, GRE interfaces, delayed because they require routes
376: # to be set. PPPoE must be first, as GIF and GRE may depend on it.
377: ifmstart "pppoe gif gre"
1.92 deraadt 378:
1.99 itojun 379: # reject 127/8 other than 127.0.0.1
1.92 deraadt 380: route -qn add -net 127 127.0.0.1 -reject > /dev/null
1.83 miod 381:
382: # Configure all the bridges.
383: for bn in /etc/bridgename.*; do
384: # Strip off /etc/bridgename. prefix
385: if=${bn#/etc/bridgename.}
1.85 todd 386: test "$if" = "*" && continue
1.83 miod 387:
388: bridgestart $if
389: done
1.116 david 390:
391: if [ "$ip6kernel" = "YES" ]; then
392: # this is to make sure DAD is completed before going further.
393: sleep `sysctl -n net.inet6.ip6.dad_count`
394: fi