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