]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/rc.d/routing
Merge bmake-20120831 from vendor/NetBSD/bmake/dist.
[FreeBSD/FreeBSD.git] / etc / rc.d / routing
1 #!/bin/sh
2 #
3 # Configure routing and miscellaneous network tunables
4 #
5 # $FreeBSD$
6 #
7
8 # PROVIDE: routing
9 # REQUIRE: faith netif ppp stf
10 # KEYWORD: nojail
11
12 . /etc/rc.subr
13 . /etc/network.subr
14
15 name="routing"
16 start_cmd="routing_start doall"
17 stop_cmd="routing_stop"
18 extra_commands="options static"
19 static_cmd="routing_start static"
20 options_cmd="routing_start options"
21
22 afcheck()
23 {
24         case $_af in
25         ""|inet|inet6|ipx|atm)
26                 ;;
27         *)
28                 err 1 "Unsupported address family: $_af."
29                 ;;
30         esac
31 }
32
33 routing_start()
34 {
35         local _cmd _af _a
36         _cmd=$1
37         _af=$2
38
39         afcheck
40
41         case $_af in
42         inet|inet6|ipx|atm)
43                 setroutes $_cmd $_af
44                 ;;
45         "")
46                 for _a in inet inet6 ipx atm; do
47                         afexists $_a && setroutes $_cmd $_a
48                 done
49                 ;;
50         esac
51 }
52
53 routing_stop()
54 {
55         local _af _a
56         _af=$1
57
58         afcheck
59
60         case $_af in
61         inet|inet6|ipx|atm)
62                 eval static_${_af} delete
63                 eval routing_stop_${_af}
64                 ;;
65         "")
66                 for _a in inet inet6 ipx atm; do
67                         afexists $_a || continue
68                         eval static_${_a} delete
69                         eval routing_stop_${_a}
70                 done
71                 ;;
72         esac
73 }
74
75 setroutes()
76 {
77         case $1 in
78         static)
79                 static_$2 add
80                 ;;
81         options)
82                 options_$2
83                 ;;
84         doall)
85                 static_$2 add
86                 options_$2
87                 ;;
88         esac
89 }
90
91 routing_stop_inet()
92 {
93         route -n flush -inet
94 }
95
96 routing_stop_inet6()
97 {
98         local i
99
100         route -n flush -inet6
101         for i in `list_net_interfaces`; do
102                 if ipv6if $i; then
103                         ifconfig $i inet6 -defaultif
104                 fi
105         done
106 }
107
108 routing_stop_atm()
109 {
110         return 0
111 }
112
113 routing_stop_ipx()
114 {
115         return 0
116 }
117
118 static_inet()
119 {
120         local _action
121         _action=$1
122
123         case ${defaultrouter} in
124         [Nn][Oo] | '')
125                 ;;
126         *)
127                 static_routes="default ${static_routes}"
128                 route_default="default ${defaultrouter}"
129                 ;;
130         esac
131
132         if [ -n "${static_routes}" ]; then
133                 for i in ${static_routes}; do
134                         route_args=`get_if_var $i route_IF`
135                         route ${_action} ${route_args}
136                 done
137         fi
138 }
139
140 static_inet6()
141 {
142         local _action i fibs
143         _action=$1
144
145         # get the number of FIBs supported.
146         fibs=`sysctl -n net.fibs`
147         : ${fibs:=1}
148
149         # disallow "internal" addresses to appear on the wire
150         route ${_action} -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
151         route ${_action} -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
152         i=1
153         if test ${i} -lt ${fibs}; then
154                 printf "Also installing reject routes for FIBs"
155                 while test ${i} -lt ${fibs}; do
156                         setfib -F ${i} route -q ${_action} \
157                             -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
158                         setfib -F ${i} route -q ${_action} \
159                             -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
160                         printf " %d" ${i}
161                         i=$((i + 1))
162                 done
163                 printf "\n"
164         fi
165
166         case ${ipv6_defaultrouter} in
167         [Nn][Oo] | '')
168                 ;;
169         *)
170                 ipv6_static_routes="default ${ipv6_static_routes}"
171                 ipv6_route_default="default ${ipv6_defaultrouter}"
172                 ;;
173         esac
174
175         if [ -n "${ipv6_static_routes}" ]; then
176                 for i in ${ipv6_static_routes}; do
177                         ipv6_route_args=`get_if_var $i ipv6_route_IF`
178                         route ${_action} -inet6 ${ipv6_route_args}
179                 done
180         fi
181
182         # Fixup $ipv6_network_interfaces
183         case ${ipv6_network_interfaces} in
184         [Nn][Oo][Nn][Ee])
185                 ipv6_network_interfaces=''
186                 ;;
187         esac
188
189         if checkyesno ipv6_gateway_enable; then
190                 for i in ${ipv6_network_interfaces}; do
191
192                         laddr=`network6_getladdr $i exclude_tentative`
193                         case ${laddr} in
194                         '')
195                                 ;;
196                         *)
197                                 ipv6_working_interfaces="$i \
198                                     ${ipv6_working_interfaces}"
199                                 ;;
200                         esac
201                 done
202                 ipv6_network_interfaces=${ipv6_working_interfaces}
203         fi
204
205         # Install the "default interface" to kernel, which will be used
206         # as the default route when there's no router.
207         case "${ipv6_default_interface}" in
208         [Nn][Oo] | [Nn][Oo][Nn][Ee])
209                 ipv6_default_interface=""
210                 ;;
211         [Aa][Uu][Tt][Oo] | "")
212                 for i in ${ipv6_network_interfaces}; do
213                         case $i in
214                         lo0|faith[0-9]*)
215                                 continue
216                                 ;;
217                         esac
218                         laddr=`network6_getladdr $i exclude_tentative`
219                         case ${laddr} in
220                         '')
221                                 ;;
222                         *)
223                                 ipv6_default_interface=$i
224                                 break
225                                 ;;
226                         esac
227                 done
228                 ;;
229         esac
230
231         # Disallow link-local unicast packets without outgoing scope
232         # identifiers.  However, if you set "ipv6_default_interface",
233         # for the host case, you will allow to omit the identifiers.
234         # Under this configuration, the packets will go to the default
235         # interface.
236         route ${_action} -inet6 fe80:: -prefixlen 10 ::1 -reject
237         route ${_action} -inet6 ff02:: -prefixlen 16 ::1 -reject
238         i=1
239         if test ${i} -lt ${fibs}; then
240                 printf "Also installing reject routes for FIBs"
241                 while test ${i} -lt ${fibs}; do
242                         setfib -F ${i} route -q ${_action} \
243                             -inet6 fe80:: -prefixlen 10 ::1 -reject
244                         setfib -F ${i} route -q ${_action} \
245                             -inet6 ff02:: -prefixlen 16 ::1 -reject
246                         printf " %d" ${i}
247                         i=$((i + 1))
248                 done
249                 printf "\n"
250         fi
251
252         case ${ipv6_default_interface} in
253         '')
254                 ;;
255         *)
256                 # Disable installing the default interface when we act
257                 # as router to avoid conflict between the default
258                 # router list and the manual configured default route.
259                 if ! checkyesno ipv6_gateway_enable; then
260                         ifconfig ${ipv6_default_interface} inet6 defaultif
261                         sysctl net.inet6.ip6.use_defaultzone=1
262                 fi
263                 ;;
264         esac
265 }
266
267 static_atm()
268 {
269         local _action i route_args
270         _action=$1
271
272         if [ -n "${natm_static_routes}" ]; then
273                 for i in ${natm_static_routes}; do
274                         route_args=`get_if_var $i route_IF`
275                         atmconfig natm ${_action} ${route_args}
276                 done
277         fi
278 }
279
280 static_ipx()
281 {
282         :
283 }
284
285 ropts_init()
286 {
287         if [ -z "${_ropts_initdone}" ]; then
288                 echo -n "Additional $1 routing options:"
289                 _ropts_initdone=yes
290         fi
291 }
292
293 options_inet()
294 {
295         _ropts_initdone=
296         if checkyesno icmp_bmcastecho; then
297                 ropts_init inet
298                 echo -n ' broadcast ping responses=YES'
299                 ${SYSCTL} net.inet.icmp.bmcastecho=1 > /dev/null
300         else
301                 ${SYSCTL} net.inet.icmp.bmcastecho=0 > /dev/null
302         fi
303
304         if checkyesno icmp_drop_redirect; then
305                 ropts_init inet
306                 echo -n ' ignore ICMP redirect=YES'
307                 ${SYSCTL} net.inet.icmp.drop_redirect=1 > /dev/null
308         else
309                 ${SYSCTL} net.inet.icmp.drop_redirect=0 > /dev/null
310         fi
311
312         if checkyesno icmp_log_redirect; then
313                 ropts_init inet
314                 echo -n ' log ICMP redirect=YES'
315                 ${SYSCTL} net.inet.icmp.log_redirect=1 > /dev/null
316         else
317                 ${SYSCTL} net.inet.icmp.log_redirect=0 > /dev/null
318         fi
319
320         if checkyesno gateway_enable; then
321                 ropts_init inet
322                 echo -n ' gateway=YES'
323                 ${SYSCTL} net.inet.ip.forwarding=1 > /dev/null
324         else
325                 ${SYSCTL} net.inet.ip.forwarding=0 > /dev/null
326         fi
327
328         if checkyesno forward_sourceroute; then
329                 ropts_init inet
330                 echo -n ' do source routing=YES'
331                 ${SYSCTL} net.inet.ip.sourceroute=1 > /dev/null
332         else
333                 ${SYSCTL} net.inet.ip.sourceroute=0 > /dev/null
334         fi
335
336         if checkyesno accept_sourceroute; then
337                 ropts_init inet
338                 echo -n ' accept source routing=YES'
339                 ${SYSCTL} net.inet.ip.accept_sourceroute=1 > /dev/null
340         else
341                 ${SYSCTL} net.inet.ip.accept_sourceroute=0 > /dev/null
342         fi
343
344         if checkyesno arpproxy_all; then
345                 ropts_init inet
346                 echo -n ' ARP proxyall=YES'
347                 ${SYSCTL} net.link.ether.inet.proxyall=1 > /dev/null
348         else
349                 ${SYSCTL} net.link.ether.inet.proxyall=0 > /dev/null
350         fi
351
352         [ -n "${_ropts_initdone}" ] && echo '.'
353 }
354
355 options_inet6()
356 {
357         _ropts_initdone=
358
359         if checkyesno ipv6_gateway_enable; then
360                 ropts_init inet6
361                 echo -n ' gateway=YES'
362                 ${SYSCTL} net.inet6.ip6.forwarding=1 > /dev/null
363         else
364                 ${SYSCTL} net.inet6.ip6.forwarding=0 > /dev/null
365         fi
366
367         [ -n "${_ropts_initdone}" ] && echo '.'
368 }
369
370 options_atm()
371 {
372         _ropts_initdone=
373
374         [ -n "${_ropts_initdone}" ] && echo '.'
375 }
376
377 options_ipx()
378 {
379         _ropts_initdone=
380
381         if checkyesno ipxgateway_enable; then
382                 ropts_init ipx
383                 echo -n ' gateway=YES'
384                 ${SYSCTL} net.ipx.ipx.ipxforwarding=1 > /dev/null
385         else
386                 ${SYSCTL} net.ipx.ipx.ipxforwarding=0 > /dev/null
387         fi
388
389         [ -n "${_ropts_initdone}" ] && echo '.'
390 }
391
392 load_rc_config $name
393 run_rc_command "$@"