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