network revision 1.39 1 #!/bin/sh
2 #
3 # $NetBSD: network,v 1.39 2002/10/05 05:24:40 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" ] || [ -f /etc/ifconfig.$int ]; then
189 if ifconfig $int create 2>/dev/null && \
190 checkyesno ipfilter; then
191 # resync ipf(4)
192 ipf -y >/dev/null
193 fi
194 fi
195 if [ -n "$args" ]; then
196 echo -n " $int"
197 ifconfig $int $args
198 elif [ -f /etc/ifconfig.$int ]; then
199 echo -n " $int"
200 while read args; do
201 [ -z "$args" ] && continue
202 case "$args" in
203 "#"*|create)
204 ;;
205 "!"*)
206 eval ${args#*!}
207 ;;
208 *)
209 ifconfig $int $args
210 ;;
211 esac
212 done < /etc/ifconfig.$int
213 else
214 if ! checkyesno auto_ifconfig; then
215 echo
216 warn \
217 "/etc/ifconfig.$int missing and ifconfig_$int not set;"
218 warn "interface $int not configured."
219 fi
220 continue
221 fi
222 configured_interfaces="$configured_interfaces $int"
223 done
224 echo "."
225 fi
226
227 # Check $defaultroute, then /etc/mygate, for the name of my gateway
228 # host. That name must be in /etc/hosts.
229 #
230 if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
231 defaultroute=`cat /etc/mygate`
232 fi
233 if [ -n "$defaultroute" ]; then
234 route add default $defaultroute
235 fi
236
237 # Check if each configured interface xxN has an $ifaliases_xxN variable
238 # associated, then configure additional IP addresses for that interface.
239 # The variable contains a list of "address netmask" pairs, with
240 # "netmask" set to "-" if the interface default netmask is to be used.
241 #
242 # Note that $ifaliases_xxN works only with certain configurations and
243 # considered not recommended. Use /etc/ifconfig.xxN if possible.
244 #
245 #
246 if [ -n "$configured_interfaces" ]; then
247 echo "Adding interface aliases:"
248 done_aliases_message=yes
249 fi
250 for int in $configured_interfaces; do
251 eval args=\$ifaliases_$int
252 if [ -n "$args" ]; then
253 set -- $args
254 while [ $# -ge 2 ]; do
255 addr=$1 ; net=$2 ; shift 2
256 if [ "$net" = "-" ]; then
257 # for compatibility only, obsolete
258 ifconfig $int inet alias $addr
259 else
260 ifconfig $int inet alias $addr \
261 netmask $net
262 fi
263 done
264 fi
265 done
266
267 # /etc/ifaliases, if it exists, contains the names of additional IP
268 # addresses for each interface. It is formatted as a series of lines
269 # that contain
270 # address interface netmask
271 #
272 # Note that /etc/ifaliases works only with certain cases only and its
273 # use is not recommended. Use /etc/ifconfig.xxN instead.
274 #
275 #
276 if [ -f /etc/ifaliases ]; then
277 if [ "$done_aliases_message" != yes ]; then
278 echo "Adding interface aliases:"
279 fi
280 while read addr int net; do
281 if [ -z "$net" ]; then
282 # for compatibility only, obsolete
283 ifconfig $int inet alias $addr
284 else
285 ifconfig $int inet alias $addr netmask $net
286 fi
287 done < /etc/ifaliases
288 fi
289
290 # IPv6 interface autoconfiguration.
291 #
292 if ifconfig lo0 inet6 >/dev/null 2>&1; then
293 # wait till DAD is completed. always invoke it in case
294 # if are configured manually by ifconfig
295 #
296 dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
297 sleep $dadcount
298 sleep 1
299
300 if checkyesno rtsol; then
301 if [ "$ip6mode" = "autohost" ]; then
302 echo 'Sending router solicitation...'
303 rtsol $rtsol_flags
304 else
305 echo
306 warn \
307 "ip6mode must be set to 'autohost' to use rtsol."
308 fi
309
310 # wait till DAD is completed, for global addresses
311 # configured by router advert message.
312 #
313 sleep $dadcount
314 sleep 1
315 fi
316 fi
317
318 # XXX this must die
319 if [ -s /etc/netstart.local ]; then
320 sh /etc/netstart.local start
321 fi
322 }
323
324 network_stop()
325 {
326 echo "Stopping network."
327
328 # XXX this must die
329 if [ -s /etc/netstart.local ]; then
330 sh /etc/netstart.local stop
331 fi
332
333 echo "Deleting aliases."
334 if [ -f /etc/ifaliases ]; then
335 while read addr int net; do
336 ifconfig $int inet delete $addr
337 done < /etc/ifaliases
338 fi
339
340 for int in `ifconfig -lu`; do
341 eval args=\$ifaliases_$int
342 if [ -n "$args" ]; then
343 set -- $args
344 while [ $# -ge 2 ]; do
345 addr=$1 ; net=$2 ; shift 2
346 ifconfig $int inet delete $addr
347 done
348 fi
349 done
350
351 # down interfaces
352 #
353 echo -n 'Downing network interfaces:'
354 if [ "$net_interfaces" != NO ]; then
355 if checkyesno auto_ifconfig; then
356 tmp=`ifconfig -l`
357 else
358 tmp="$net_interfaces"
359 fi
360 for int in $tmp; do
361 eval args=\$ifconfig_$int
362 if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
363 echo -n " $int"
364 ifconfig $int down
365 if ifconfig $int destroy 2>/dev/null && \
366 checkyesno ipfilter; then
367 # resync ipf(4)
368 ipf -y >/dev/null
369 fi
370 fi
371 done
372 echo "."
373 fi
374
375 # flush routes
376 #
377 route -n flush
378
379 }
380
381 load_rc_config $name ipfilter
382 run_rc_command "$1"
383