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