PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/etc/rc.d/routing

https://bitbucket.org/freebsd/freebsd-head/
Shell | 393 lines | 320 code | 51 blank | 22 comment | 36 complexity | 297c0d58143f485b40d40e11a58be021 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. #!/bin/sh
  2. #
  3. # Configure routing and miscellaneous network tunables
  4. #
  5. # $FreeBSD$
  6. #
  7. # PROVIDE: routing
  8. # REQUIRE: faith netif ppp stf
  9. # KEYWORD: nojail
  10. . /etc/rc.subr
  11. . /etc/network.subr
  12. name="routing"
  13. start_cmd="routing_start doall"
  14. stop_cmd="routing_stop"
  15. extra_commands="options static"
  16. static_cmd="routing_start static"
  17. options_cmd="routing_start options"
  18. afcheck()
  19. {
  20. case $_af in
  21. ""|inet|inet6|ipx|atm)
  22. ;;
  23. *)
  24. err 1 "Unsupported address family: $_af."
  25. ;;
  26. esac
  27. }
  28. routing_start()
  29. {
  30. local _cmd _af _a
  31. _cmd=$1
  32. _af=$2
  33. afcheck
  34. case $_af in
  35. inet|inet6|ipx|atm)
  36. setroutes $_cmd $_af
  37. ;;
  38. "")
  39. for _a in inet inet6 ipx atm; do
  40. afexists $_a && setroutes $_cmd $_a
  41. done
  42. ;;
  43. esac
  44. }
  45. routing_stop()
  46. {
  47. local _af _a
  48. _af=$1
  49. afcheck
  50. case $_af in
  51. inet|inet6|ipx|atm)
  52. eval static_${_af} delete
  53. eval routing_stop_${_af}
  54. ;;
  55. "")
  56. for _a in inet inet6 ipx atm; do
  57. afexists $_a || continue
  58. eval static_${_a} delete
  59. eval routing_stop_${_a}
  60. done
  61. ;;
  62. esac
  63. }
  64. setroutes()
  65. {
  66. case $1 in
  67. static)
  68. static_$2 add
  69. ;;
  70. options)
  71. options_$2
  72. ;;
  73. doall)
  74. static_$2 add
  75. options_$2
  76. ;;
  77. esac
  78. }
  79. routing_stop_inet()
  80. {
  81. route -n flush -inet
  82. }
  83. routing_stop_inet6()
  84. {
  85. local i
  86. route -n flush -inet6
  87. for i in `list_net_interfaces`; do
  88. if ipv6if $i; then
  89. ifconfig $i inet6 -defaultif
  90. fi
  91. done
  92. }
  93. routing_stop_atm()
  94. {
  95. return 0
  96. }
  97. routing_stop_ipx()
  98. {
  99. return 0
  100. }
  101. static_inet()
  102. {
  103. local _action
  104. _action=$1
  105. case ${defaultrouter} in
  106. [Nn][Oo] | '')
  107. ;;
  108. *)
  109. static_routes="default ${static_routes}"
  110. route_default="default ${defaultrouter}"
  111. ;;
  112. esac
  113. if [ -n "${static_routes}" ]; then
  114. for i in ${static_routes}; do
  115. route_args=`get_if_var $i route_IF`
  116. route ${_action} ${route_args}
  117. done
  118. fi
  119. }
  120. static_inet6()
  121. {
  122. local _action i fibs
  123. _action=$1
  124. # get the number of FIBs supported.
  125. fibs=`sysctl -n net.fibs`
  126. : ${fibs:=1}
  127. # disallow "internal" addresses to appear on the wire
  128. route ${_action} -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
  129. route ${_action} -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
  130. i=1
  131. if test ${i} -lt ${fibs}; then
  132. printf "Also installing reject routes for FIBs"
  133. while test ${i} -lt ${fibs}; do
  134. setfib -F ${i} route -q ${_action} \
  135. -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
  136. setfib -F ${i} route -q ${_action} \
  137. -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
  138. printf " %d" ${i}
  139. i=$((i + 1))
  140. done
  141. printf "\n"
  142. fi
  143. case ${ipv6_defaultrouter} in
  144. [Nn][Oo] | '')
  145. ;;
  146. *)
  147. ipv6_static_routes="default ${ipv6_static_routes}"
  148. ipv6_route_default="default ${ipv6_defaultrouter}"
  149. ;;
  150. esac
  151. if [ -n "${ipv6_static_routes}" ]; then
  152. for i in ${ipv6_static_routes}; do
  153. ipv6_route_args=`get_if_var $i ipv6_route_IF`
  154. route ${_action} -inet6 ${ipv6_route_args}
  155. done
  156. fi
  157. # Fixup $ipv6_network_interfaces
  158. case ${ipv6_network_interfaces} in
  159. [Nn][Oo][Nn][Ee])
  160. ipv6_network_interfaces=''
  161. ;;
  162. esac
  163. if checkyesno ipv6_gateway_enable; then
  164. for i in ${ipv6_network_interfaces}; do
  165. laddr=`network6_getladdr $i exclude_tentative`
  166. case ${laddr} in
  167. '')
  168. ;;
  169. *)
  170. ipv6_working_interfaces="$i \
  171. ${ipv6_working_interfaces}"
  172. ;;
  173. esac
  174. done
  175. ipv6_network_interfaces=${ipv6_working_interfaces}
  176. fi
  177. # Install the "default interface" to kernel, which will be used
  178. # as the default route when there's no router.
  179. case "${ipv6_default_interface}" in
  180. [Nn][Oo] | [Nn][Oo][Nn][Ee])
  181. ipv6_default_interface=""
  182. ;;
  183. [Aa][Uu][Tt][Oo] | "")
  184. for i in ${ipv6_network_interfaces}; do
  185. case $i in
  186. lo0|faith[0-9]*)
  187. continue
  188. ;;
  189. esac
  190. laddr=`network6_getladdr $i exclude_tentative`
  191. case ${laddr} in
  192. '')
  193. ;;
  194. *)
  195. ipv6_default_interface=$i
  196. break
  197. ;;
  198. esac
  199. done
  200. ;;
  201. esac
  202. # Disallow link-local unicast packets without outgoing scope
  203. # identifiers. However, if you set "ipv6_default_interface",
  204. # for the host case, you will allow to omit the identifiers.
  205. # Under this configuration, the packets will go to the default
  206. # interface.
  207. route ${_action} -inet6 fe80:: -prefixlen 10 ::1 -reject
  208. route ${_action} -inet6 ff02:: -prefixlen 16 ::1 -reject
  209. i=1
  210. if test ${i} -lt ${fibs}; then
  211. printf "Also installing reject routes for FIBs"
  212. while test ${i} -lt ${fibs}; do
  213. setfib -F ${i} route -q ${_action} \
  214. -inet6 fe80:: -prefixlen 10 ::1 -reject
  215. setfib -F ${i} route -q ${_action} \
  216. -inet6 ff02:: -prefixlen 16 ::1 -reject
  217. printf " %d" ${i}
  218. i=$((i + 1))
  219. done
  220. printf "\n"
  221. fi
  222. case ${ipv6_default_interface} in
  223. '')
  224. ;;
  225. *)
  226. # Disable installing the default interface when we act
  227. # as router to avoid conflict between the default
  228. # router list and the manual configured default route.
  229. if ! checkyesno ipv6_gateway_enable; then
  230. ifconfig ${ipv6_default_interface} inet6 defaultif
  231. sysctl net.inet6.ip6.use_defaultzone=1
  232. fi
  233. ;;
  234. esac
  235. }
  236. static_atm()
  237. {
  238. local _action i route_args
  239. _action=$1
  240. if [ -n "${natm_static_routes}" ]; then
  241. for i in ${natm_static_routes}; do
  242. route_args=`get_if_var $i route_IF`
  243. atmconfig natm ${_action} ${route_args}
  244. done
  245. fi
  246. }
  247. static_ipx()
  248. {
  249. :
  250. }
  251. ropts_init()
  252. {
  253. if [ -z "${_ropts_initdone}" ]; then
  254. echo -n "Additional $1 routing options:"
  255. _ropts_initdone=yes
  256. fi
  257. }
  258. options_inet()
  259. {
  260. _ropts_initdone=
  261. if checkyesno icmp_bmcastecho; then
  262. ropts_init inet
  263. echo -n ' broadcast ping responses=YES'
  264. ${SYSCTL} net.inet.icmp.bmcastecho=1 > /dev/null
  265. else
  266. ${SYSCTL} net.inet.icmp.bmcastecho=0 > /dev/null
  267. fi
  268. if checkyesno icmp_drop_redirect; then
  269. ropts_init inet
  270. echo -n ' ignore ICMP redirect=YES'
  271. ${SYSCTL} net.inet.icmp.drop_redirect=1 > /dev/null
  272. else
  273. ${SYSCTL} net.inet.icmp.drop_redirect=0 > /dev/null
  274. fi
  275. if checkyesno icmp_log_redirect; then
  276. ropts_init inet
  277. echo -n ' log ICMP redirect=YES'
  278. ${SYSCTL} net.inet.icmp.log_redirect=1 > /dev/null
  279. else
  280. ${SYSCTL} net.inet.icmp.log_redirect=0 > /dev/null
  281. fi
  282. if checkyesno gateway_enable; then
  283. ropts_init inet
  284. echo -n ' gateway=YES'
  285. ${SYSCTL} net.inet.ip.forwarding=1 > /dev/null
  286. else
  287. ${SYSCTL} net.inet.ip.forwarding=0 > /dev/null
  288. fi
  289. if checkyesno forward_sourceroute; then
  290. ropts_init inet
  291. echo -n ' do source routing=YES'
  292. ${SYSCTL} net.inet.ip.sourceroute=1 > /dev/null
  293. else
  294. ${SYSCTL} net.inet.ip.sourceroute=0 > /dev/null
  295. fi
  296. if checkyesno accept_sourceroute; then
  297. ropts_init inet
  298. echo -n ' accept source routing=YES'
  299. ${SYSCTL} net.inet.ip.accept_sourceroute=1 > /dev/null
  300. else
  301. ${SYSCTL} net.inet.ip.accept_sourceroute=0 > /dev/null
  302. fi
  303. if checkyesno arpproxy_all; then
  304. ropts_init inet
  305. echo -n ' ARP proxyall=YES'
  306. ${SYSCTL} net.link.ether.inet.proxyall=1 > /dev/null
  307. else
  308. ${SYSCTL} net.link.ether.inet.proxyall=0 > /dev/null
  309. fi
  310. [ -n "${_ropts_initdone}" ] && echo '.'
  311. }
  312. options_inet6()
  313. {
  314. _ropts_initdone=
  315. if checkyesno ipv6_gateway_enable; then
  316. ropts_init inet6
  317. echo -n ' gateway=YES'
  318. ${SYSCTL} net.inet6.ip6.forwarding=1 > /dev/null
  319. else
  320. ${SYSCTL} net.inet6.ip6.forwarding=0 > /dev/null
  321. fi
  322. [ -n "${_ropts_initdone}" ] && echo '.'
  323. }
  324. options_atm()
  325. {
  326. _ropts_initdone=
  327. [ -n "${_ropts_initdone}" ] && echo '.'
  328. }
  329. options_ipx()
  330. {
  331. _ropts_initdone=
  332. if checkyesno ipxgateway_enable; then
  333. ropts_init ipx
  334. echo -n ' gateway=YES'
  335. ${SYSCTL} net.ipx.ipx.ipxforwarding=1 > /dev/null
  336. else
  337. ${SYSCTL} net.ipx.ipx.ipxforwarding=0 > /dev/null
  338. fi
  339. [ -n "${_ropts_initdone}" ] && echo '.'
  340. }
  341. load_rc_config $name
  342. run_rc_command "$@"