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