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