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