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