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