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