]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - etc/rc.d/routing
MFC r231852,232127:
[FreeBSD/stable/9.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 ${ipv6_network_interfaces}; do
102                 ifconfig $i inet6 -defaultif
103         done
104 }
105
106 routing_stop_atm()
107 {
108         return 0
109 }
110
111 routing_stop_ipx()
112 {
113         return 0
114 }
115
116 static_inet()
117 {
118         local _action
119         _action=$1
120
121         case ${defaultrouter} in
122         [Nn][Oo] | '')
123                 ;;
124         *)
125                 static_routes="default ${static_routes}"
126                 route_default="default ${defaultrouter}"
127                 ;;
128         esac
129
130         if [ -n "${static_routes}" ]; then
131                 for i in ${static_routes}; do
132                         route_args=`get_if_var $i route_IF`
133                         route ${_action} ${route_args}
134                 done
135         fi
136 }
137
138 static_inet6()
139 {
140         local _action i fibs
141         _action=$1
142
143         # get the number of FIBs supported.
144         fibs=`sysctl -n net.fibs`
145         : ${fibs:=1}
146
147         # disallow "internal" addresses to appear on the wire
148         i=0
149         while test ${i} -lt ${fibs}; do
150                 setfib -F ${i} route ${_action} \
151                     -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
152                 setfib -F ${i} route ${_action} \
153                     -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
154                 i=$((i + 1))
155         done
156
157         case ${ipv6_defaultrouter} in
158         [Nn][Oo] | '')
159                 ;;
160         *)
161                 ipv6_static_routes="default ${ipv6_static_routes}"
162                 ipv6_route_default="default ${ipv6_defaultrouter}"
163                 ;;
164         esac
165
166         if [ -n "${ipv6_static_routes}" ]; then
167                 for i in ${ipv6_static_routes}; do
168                         ipv6_route_args=`get_if_var $i ipv6_route_IF`
169                         route ${_action} -inet6 ${ipv6_route_args}
170                 done
171         fi
172
173         # Fixup $ipv6_network_interfaces
174         case ${ipv6_network_interfaces} in
175         [Nn][Oo][Nn][Ee])
176                 ipv6_network_interfaces=''
177                 ;;
178         esac
179
180         if checkyesno ipv6_gateway_enable; then
181                 for i in ${ipv6_network_interfaces}; do
182
183                         laddr=`network6_getladdr $i exclude_tentative`
184                         case ${laddr} in
185                         '')
186                                 ;;
187                         *)
188                                 ipv6_working_interfaces="$i \
189                                     ${ipv6_working_interfaces}"
190                                 ;;
191                         esac
192                 done
193                 ipv6_network_interfaces=${ipv6_working_interfaces}
194         fi
195
196         # Install the "default interface" to kernel, which will be used
197         # as the default route when there's no router.
198         case "${ipv6_default_interface}" in
199         [Nn][Oo] | [Nn][Oo][Nn][Ee])
200                 ipv6_default_interface=""
201                 ;;
202         [Aa][Uu][Tt][Oo] | "")
203                 for i in ${ipv6_network_interfaces}; do
204                         case $i in
205                         lo0|faith[0-9]*)
206                                 continue
207                                 ;;
208                         esac
209                         laddr=`network6_getladdr $i exclude_tentative`
210                         case ${laddr} in
211                         '')
212                                 ;;
213                         *)
214                                 ipv6_default_interface=$i
215                                 break
216                                 ;;
217                         esac
218                 done
219                 ;;
220         esac
221
222         # Disallow link-local unicast packets without outgoing scope
223         # identifiers.  However, if you set "ipv6_default_interface",
224         # for the host case, you will allow to omit the identifiers.
225         # Under this configuration, the packets will go to the default
226         # interface.
227         i=0
228         while test ${i} -lt ${fibs}; do
229                 setfib -F ${i} route ${_action} \
230                     -inet6 fe80:: -prefixlen 10 ::1 -reject
231                 setfib -F ${i} route ${_action} \
232                     -inet6 ff02:: -prefixlen 16 ::1 -reject
233                 i=$((i + 1))
234         done
235
236         case ${ipv6_default_interface} in
237         '')
238                 ;;
239         *)
240                 # Disable installing the default interface when we act
241                 # as router to avoid conflict between the default
242                 # router list and the manual configured default route.
243                 if ! checkyesno ipv6_gateway_enable; then
244                         ifconfig ${ipv6_default_interface} inet6 defaultif
245                         sysctl net.inet6.ip6.use_defaultzone=1
246                 fi
247                 ;;
248         esac
249 }
250
251 static_atm()
252 {
253         local _action i route_args
254         _action=$1
255
256         if [ -n "${natm_static_routes}" ]; then
257                 for i in ${natm_static_routes}; do
258                         route_args=`get_if_var $i route_IF`
259                         atmconfig natm ${_action} ${route_args}
260                 done
261         fi
262 }
263
264 static_ipx()
265 {
266 }
267
268 ropts_init()
269 {
270         if [ -z "${_ropts_initdone}" ]; then
271                 echo -n "Additional $1 routing options:"
272                 _ropts_initdone=yes
273         fi
274 }
275
276 options_inet()
277 {
278         _ropts_initdone=
279         if checkyesno icmp_bmcastecho; then
280                 ropts_init inet
281                 echo -n ' broadcast ping responses=YES'
282                 ${SYSCTL} net.inet.icmp.bmcastecho=1 > /dev/null
283         else
284                 ${SYSCTL} net.inet.icmp.bmcastecho=0 > /dev/null
285         fi
286
287         if checkyesno icmp_drop_redirect; then
288                 ropts_init inet
289                 echo -n ' ignore ICMP redirect=YES'
290                 ${SYSCTL} net.inet.icmp.drop_redirect=1 > /dev/null
291         else
292                 ${SYSCTL} net.inet.icmp.drop_redirect=0 > /dev/null
293         fi
294
295         if checkyesno icmp_log_redirect; then
296                 ropts_init inet
297                 echo -n ' log ICMP redirect=YES'
298                 ${SYSCTL} net.inet.icmp.log_redirect=1 > /dev/null
299         else
300                 ${SYSCTL} net.inet.icmp.log_redirect=0 > /dev/null
301         fi
302
303         if checkyesno gateway_enable; then
304                 ropts_init inet
305                 echo -n ' gateway=YES'
306                 ${SYSCTL} net.inet.ip.forwarding=1 > /dev/null
307         else
308                 ${SYSCTL} net.inet.ip.forwarding=0 > /dev/null
309         fi
310
311         if checkyesno forward_sourceroute; then
312                 ropts_init inet
313                 echo -n ' do source routing=YES'
314                 ${SYSCTL} net.inet.ip.sourceroute=1 > /dev/null
315         else
316                 ${SYSCTL} net.inet.ip.sourceroute=0 > /dev/null
317         fi
318
319         if checkyesno accept_sourceroute; then
320                 ropts_init inet
321                 echo -n ' accept source routing=YES'
322                 ${SYSCTL} net.inet.ip.accept_sourceroute=1 > /dev/null
323         else
324                 ${SYSCTL} net.inet.ip.accept_sourceroute=0 > /dev/null
325         fi
326
327         if checkyesno arpproxy_all; then
328                 ropts_init inet
329                 echo -n ' ARP proxyall=YES'
330                 ${SYSCTL} net.link.ether.inet.proxyall=1 > /dev/null
331         else
332                 ${SYSCTL} net.link.ether.inet.proxyall=0 > /dev/null
333         fi
334
335         [ -n "${_ropts_initdone}" ] && echo '.'
336 }
337
338 options_inet6()
339 {
340         _ropts_initdone=
341
342         if checkyesno ipv6_gateway_enable; then
343                 ropts_init inet6
344                 echo -n ' gateway=YES'
345                 ${SYSCTL} net.inet6.ip6.forwarding=1 > /dev/null
346         else
347                 ${SYSCTL} net.inet6.ip6.forwarding=0 > /dev/null
348         fi
349
350         [ -n "${_ropts_initdone}" ] && echo '.'
351 }
352
353 options_atm()
354 {
355         _ropts_initdone=
356
357         [ -n "${_ropts_initdone}" ] && echo '.'
358 }
359
360 options_ipx()
361 {
362         _ropts_initdone=
363
364         if checkyesno ipxgateway_enable; then
365                 ropts_init ipx
366                 echo -n ' gateway=YES'
367                 ${SYSCTL} net.ipx.ipx.ipxforwarding=1 > /dev/null
368         else
369                 ${SYSCTL} net.ipx.ipx.ipxforwarding=0 > /dev/null
370         fi
371
372         [ -n "${_ropts_initdone}" ] && echo '.'
373 }
374
375 load_rc_config $name
376 run_rc_command "$@"