network revision 1.38 1 #!/bin/sh
2 #
3 # $NetBSD: network,v 1.38 2002/05/04 13:53:02 tron Exp $
4 #
5
6 # PROVIDE: network
7 # REQUIRE: ipfilter ipsec mountcritlocal root tty sysctl
8 # BEFORE: NETWORKING
9
10 . /etc/rc.subr
11
12 name="network"
13 start_cmd="network_start"
14 stop_cmd="network_stop"
15
16 network_start()
17 {
18 # set hostname, turn on network
19 #
20 echo "Starting network."
21
22 # If $hostname is set, use it for my Internet name,
23 # otherwise use /etc/myname
24 #
25 if [ -z "$hostname" ] && [ -f /etc/myname ]; then
26 hostname=`cat /etc/myname`
27 fi
28 if [ -n "$hostname" ]; then
29 echo "Hostname: $hostname"
30 hostname $hostname
31 else
32 # Don't warn about it if we're going to run
33 # DHCP later, as we will probably get the
34 # hostname at that time.
35 #
36 if ! checkyesno dhclient && [ -z "`hostname`" ]; then
37 warn "\$hostname not set."
38 fi
39 fi
40
41 # Check $domainname first, then /etc/defaultdomain,
42 # for NIS/YP domain name
43 #
44 if [ -z "$domainname" ] && [ -f /etc/defaultdomain ]; then
45 domainname=`cat /etc/defaultdomain`
46 fi
47 if [ -n "$domainname" ]; then
48 echo "NIS domainname: $domainname"
49 domainname $domainname
50 fi
51
52 # Flush all routes just to make sure it is clean
53 if checkyesno flushroutes; then
54 route -n flush
55 fi
56
57 # Set the address for the first loopback interface, so that the
58 # auto-route from a newly configured interface's address to lo0
59 # works correctly.
60 #
61 # NOTE: obscure networking problems will occur if lo0 isn't configured.
62 #
63 ifconfig lo0 inet 127.0.0.1
64
65 # According to RFC1122, 127.0.0.0/8 must not leave the node.
66 #
67 route -q add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject
68
69 # IPv6 routing setups, and host/router mode selection.
70 #
71 if ifconfig lo0 inet6 >/dev/null 2>&1; then
72 # We have IPv6 support in kernel.
73
74 # disallow link-local unicast dest without outgoing scope
75 # identifiers.
76 #
77 route -q add -inet6 fe80:: -prefixlen 10 ::1 -reject
78
79 # disallow site-local unicast dest without outgoing scope
80 # identifiers.
81 # If you configure site-locals without scope id (it is
82 # permissible config for routers that are not on scope
83 # boundary), you may want to comment the following one out.
84 #
85 if ! checkyesno ip6sitelocal; then
86 route -q add -inet6 fec0:: -prefixlen 10 ::1 -reject
87 fi
88
89 # disallow "internal" addresses to appear on the wire.
90 #
91 route -q add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
92
93 # disallow packets to malicious IPv4 compatible prefix
94 #
95 route -q add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject
96 route -q add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject
97 route -q add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject
98 route -q add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject
99
100 # disallow packets to malicious 6to4 prefix
101 #
102 route -q add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
103 route -q add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
104 route -q add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
105 route -q add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
106
107 # Completely disallow packets to IPv4 compatible prefix.
108 # This may conflict with RFC1933 under following circumstances:
109 # (1) An IPv6-only KAME node tries to originate packets to IPv4
110 # comatible destination. The KAME node has no IPv4
111 # compatible support. Under RFC1933, it should transmit
112 # native IPv6 packets toward IPv4 compatible destination,
113 # hoping it would reach a router that forwards the packet
114 # toward auto-tunnel interface.
115 # (2) An IPv6-only node originates a packet to IPv4 compatible
116 # destination. A KAME node is acting as an IPv6 router, and
117 # asked to forward it.
118 # Due to rare use of IPv4 compatible address, and security
119 # issues with it, we disable it by default.
120 #
121 route -q add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
122
123 sysctl -qw net.inet6.ip6.forwarding=0
124 sysctl -qw net.inet6.ip6.accept_rtadv=0
125
126 case $ip6mode in
127 router)
128 echo 'IPv6 mode: router'
129 sysctl -qw net.inet6.ip6.forwarding=1
130 ;;
131
132 autohost)
133 echo 'IPv6 mode: autoconfigured host'
134 sysctl -qw net.inet6.ip6.accept_rtadv=1
135 ;;
136
137 host)
138 echo 'IPv6 mode: host'
139 ;;
140
141 *) warn "invalid \$ip6mode value "\"$ip6mode\"
142 ;;
143
144 esac
145 fi
146
147 # Configure all of the network interfaces listed in $net_interfaces;
148 # if $auto_ifconfig is YES, grab all interfaces from ifconfig.
149 # In the following, "xxN" stands in for interface names, like "le0".
150 # For any interfaces that has an $ifconfig_xxN variable associated,
151 # we do "ifconfig xxN $ifconfig_xxN".
152 # If there is no such variable, we take the contents of the file
153 # /etc/ifconfig.xxN, and run "ifconfig xxN" repeatedly, using each
154 # line of the file as the arguments for a separate "ifconfig"
155 # invocation.
156 #
157 # In order to configure an interface reasonably, you at the very least
158 # need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
159 # and probably a netmask (as in "netmask 0xffffffe0"). You will
160 # frequently need to specify a media type, as in "media UTP", for
161 # interface cards with multiple media connections that do not
162 # autoconfigure. See the ifconfig manual page for details.
163 #
164 # Note that /etc/ifconfig.xxN takes multiple lines. The following
165 # configuration is possible:
166 # inet 10.1.1.1 netmask 0xffffff00
167 # inet 10.1.1.2 netmask 0xffffff00 alias
168 # inet6 fec0::1 prefixlen 64 alias
169 #
170 # You can put shell script fragment into /etc/ifconfig.xxN by
171 # starting a line with "!". Refer to ifconfig.if(5) for details.
172 #
173 if [ "$net_interfaces" != NO ]; then
174 if checkyesno auto_ifconfig; then
175 tmp=`ifconfig -l`
176 for cloner in `ifconfig -C 2>/dev/null`; do
177 for int in /etc/ifconfig.${cloner}[0-9]*; do
178 [ ! -f $int ] && break
179 tmp="$tmp ${int##*.}"
180 done
181 done
182 else
183 tmp="$net_interfaces"
184 fi
185 echo -n 'Configuring network interfaces:'
186 for int in $tmp; do
187 eval args=\$ifconfig_$int
188 if [ -n "$args" ]; then
189 echo -n " $int"
190 ifconfig $int $args
191 elif [ -f /etc/ifconfig.$int ]; then
192 echo -n " $int"
193 while read args; do
194 [ -z "$args" ] && continue
195 case "$args" in
196 "#"*)
197 ;;
198 "!"*)
199 eval ${args#*!}
200 ;;
201 *)
202 ifconfig $int $args
203 ;;
204 esac
205 done < /etc/ifconfig.$int
206 else
207 if ! checkyesno auto_ifconfig; then
208 echo
209 warn \
210 "/etc/ifconfig.$int missing and ifconfig_$int not set;"
211 warn "interface $int not configured."
212 fi
213 continue
214 fi
215 configured_interfaces="$configured_interfaces $int"
216 done
217 echo "."
218 fi
219
220 # Check $defaultroute, then /etc/mygate, for the name of my gateway
221 # host. That name must be in /etc/hosts.
222 #
223 if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
224 defaultroute=`cat /etc/mygate`
225 fi
226 if [ -n "$defaultroute" ]; then
227 route add default $defaultroute
228 fi
229
230 # Check if each configured interface xxN has an $ifaliases_xxN variable
231 # associated, then configure additional IP addresses for that interface.
232 # The variable contains a list of "address netmask" pairs, with
233 # "netmask" set to "-" if the interface default netmask is to be used.
234 #
235 # Note that $ifaliases_xxN works only with certain configurations and
236 # considered not recommended. Use /etc/ifconfig.xxN if possible.
237 #
238 #
239 if [ -n "$configured_interfaces" ]; then
240 echo "Adding interface aliases:"
241 done_aliases_message=yes
242 fi
243 for int in $configured_interfaces; do
244 eval args=\$ifaliases_$int
245 if [ -n "$args" ]; then
246 set -- $args
247 while [ $# -ge 2 ]; do
248 addr=$1 ; net=$2 ; shift 2
249 if [ "$net" = "-" ]; then
250 # for compatibility only, obsolete
251 ifconfig $int inet alias $addr
252 else
253 ifconfig $int inet alias $addr \
254 netmask $net
255 fi
256 done
257 fi
258 done
259
260 # /etc/ifaliases, if it exists, contains the names of additional IP
261 # addresses for each interface. It is formatted as a series of lines
262 # that contain
263 # address interface netmask
264 #
265 # Note that /etc/ifaliases works only with certain cases only and its
266 # use is not recommended. Use /etc/ifconfig.xxN instead.
267 #
268 #
269 if [ -f /etc/ifaliases ]; then
270 if [ "$done_aliases_message" != yes ]; then
271 echo "Adding interface aliases:"
272 fi
273 while read addr int net; do
274 if [ -z "$net" ]; then
275 # for compatibility only, obsolete
276 ifconfig $int inet alias $addr
277 else
278 ifconfig $int inet alias $addr netmask $net
279 fi
280 done < /etc/ifaliases
281 fi
282
283 # IPv6 interface autoconfiguration.
284 #
285 if ifconfig lo0 inet6 >/dev/null 2>&1; then
286 # wait till DAD is completed. always invoke it in case
287 # if are configured manually by ifconfig
288 #
289 dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
290 sleep $dadcount
291 sleep 1
292
293 if checkyesno rtsol; then
294 if [ "$ip6mode" = "autohost" ]; then
295 echo 'Sending router solicitation...'
296 rtsol $rtsol_flags
297 else
298 echo
299 warn \
300 "ip6mode must be set to 'autohost' to use rtsol."
301 fi
302
303 # wait till DAD is completed, for global addresses
304 # configured by router advert message.
305 #
306 sleep $dadcount
307 sleep 1
308 fi
309 fi
310
311 # resync ipf(4)
312 if checkyesno ipfilter; then
313 /sbin/ipf -y
314 fi
315
316 # XXX this must die
317 if [ -s /etc/netstart.local ]; then
318 sh /etc/netstart.local start
319 fi
320 }
321
322 network_stop()
323 {
324 echo "Stopping network."
325
326 # XXX this must die
327 if [ -s /etc/netstart.local ]; then
328 sh /etc/netstart.local stop
329 fi
330
331 echo "Deleting aliases."
332 if [ -f /etc/ifaliases ]; then
333 while read addr int net; do
334 ifconfig $int inet delete $addr
335 done < /etc/ifaliases
336 fi
337
338 for int in `ifconfig -lu`; do
339 eval args=\$ifaliases_$int
340 if [ -n "$args" ]; then
341 set -- $args
342 while [ $# -ge 2 ]; do
343 addr=$1 ; net=$2 ; shift 2
344 ifconfig $int inet delete $addr
345 done
346 fi
347 done
348
349 # down interfaces
350 #
351 echo -n 'Downing network interfaces:'
352 if [ "$net_interfaces" != NO ]; then
353 if checkyesno auto_ifconfig; then
354 tmp=`ifconfig -l`
355 else
356 tmp="$net_interfaces"
357 fi
358 for int in $tmp; do
359 eval args=\$ifconfig_$int
360 if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
361 echo -n " $int"
362 ifconfig $int down
363 fi
364 done
365 echo "."
366 fi
367
368 # flush routes
369 #
370 route -n flush
371
372 # resync ipf(4)
373 if checkyesno ipfilter; then
374 /sbin/ipf -y
375 fi
376
377 }
378
379 load_rc_config $name
380 run_rc_command "$1"
381