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