PageRenderTime 45ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/cmd/svc/milestone/net-routing-setup

https://bitbucket.org/illumos/illumos-gate/
Shell | 214 lines | 74 code | 16 blank | 124 comment | 18 complexity | e74a0762e64b8482233551298ee8bace MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, AGPL-1.0, AGPL-3.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, BSD-2-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, 0BSD
  1. #!/sbin/sh
  2. #
  3. # CDDL HEADER START
  4. #
  5. # The contents of this file are subject to the terms of the
  6. # Common Development and Distribution License (the "License").
  7. # You may not use this file except in compliance with the License.
  8. #
  9. # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10. # or http://www.opensolaris.org/os/licensing.
  11. # See the License for the specific language governing permissions
  12. # and limitations under the License.
  13. #
  14. # When distributing Covered Code, include this CDDL HEADER in each
  15. # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16. # If applicable, add the following below this CDDL HEADER, with the
  17. # fields enclosed by brackets "[]" replaced with your own identifying
  18. # information: Portions Copyright [yyyy] [name of copyright owner]
  19. #
  20. # CDDL HEADER END
  21. #
  22. #
  23. # Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
  24. # This script configures IP routing.
  25. . /lib/svc/share/smf_include.sh
  26. #
  27. # In a shared-IP zone we need this service to be up, but all of the work
  28. # it tries to do is irrelevant (and will actually lead to the service
  29. # failing if we try to do it), so just bail out.
  30. # In the global zone and exclusive-IP zones we proceed.
  31. #
  32. smf_configure_ip || exit $SMF_EXIT_OK
  33. #
  34. # If routing.conf file is in place, and has not already been read in
  35. # by previous invokation of routeadm, legacy configuration is upgraded
  36. # by this call to "routeadm -u". This call is also needed when
  37. # a /var/svc/profile/upgrade file is found, as it may contain routeadm commands
  38. # which need to be applied. Finally, routeadm starts in.ndpd by
  39. # enabling the ndp service (in.ndpd), which is required for IPv6 address
  40. # autoconfiguration. It would be nice if we could do this in
  41. # network/loopback, but since the SMF backend is read-only at that
  42. # point in boot, we cannot.
  43. #
  44. /sbin/routeadm -u
  45. #
  46. # Are we routing dynamically? routeadm(1M) reports this in the
  47. # "current" values of ipv4/6-routing - if either are true, we are running
  48. # routing daemons (or at least they are enabled to run).
  49. #
  50. dynamic_routing_test=`/sbin/routeadm -p | \
  51. nawk '/^ipv[46]-routing [.]*/ { print $2 }' | /usr/bin/grep "current=enabled"`
  52. if [ -n "$dynamic_routing_test" ]; then
  53. dynamic_routing="true"
  54. fi
  55. #
  56. # Configure default IPv4 routers using the local "/etc/defaultrouter"
  57. # configuration file. The file can contain the hostnames or IP
  58. # addresses of one or more default routers. If hostnames are used,
  59. # each hostname must also be listed in the local "/etc/hosts" file
  60. # because NIS is not running at the time that this script is
  61. # run. Each router name or address is listed on a single line by
  62. # itself in the file. Anything else on that line after the router's
  63. # name or address is ignored. Lines that begin with "#" are
  64. # considered comments and ignored.
  65. #
  66. # The default routes listed in the "/etc/defaultrouter" file will
  67. # replace those added by the kernel during diskless booting. An
  68. # empty "/etc/defaultrouter" file will cause the default route
  69. # added by the kernel to be deleted.
  70. #
  71. # Note that the default router file is ignored if we received routes
  72. # from a DHCP server. Our policy is to always trust DHCP over local
  73. # administration.
  74. #
  75. smf_netstrategy
  76. if [ "$_INIT_NET_STRATEGY" = "dhcp" ] && \
  77. [ -n "`/sbin/dhcpinfo Router`" ]; then
  78. defrouters=`/sbin/dhcpinfo Router`
  79. elif [ -f /etc/defaultrouter ]; then
  80. defrouters=`/usr/bin/grep -v \^\# /etc/defaultrouter | \
  81. /usr/bin/awk '{print $1}'`
  82. if [ -n "$defrouters" ]; then
  83. #
  84. # We want the default router(s) listed in
  85. # /etc/defaultrouter to replace the one added from the
  86. # BOOTPARAMS WHOAMI response but we must avoid flushing
  87. # the last route between the running system and its
  88. # /usr file system.
  89. #
  90. # First, remember the original route.
  91. shift $#
  92. set -- `/usr/bin/netstat -rn -f inet | \
  93. /usr/bin/grep '^default'`
  94. route_IP="$2"
  95. #
  96. # Next, add those from /etc/defaultrouter. While doing
  97. # this, if one of the routes we add is for the route
  98. # previously added as a result of the BOOTPARAMS
  99. # response, we will see a message of the form:
  100. # "add net default: gateway a.b.c.d: entry exists"
  101. #
  102. do_delete=yes
  103. for router in $defrouters; do
  104. route_added=`/usr/sbin/route -n add default \
  105. -gateway $router`
  106. res=$?
  107. set -- $route_added
  108. [ $res -ne 0 -a "$5" = "$route_IP:" ] && do_delete=no
  109. done
  110. #
  111. # Finally, delete the original default route unless it
  112. # was also listed in the defaultrouter file.
  113. #
  114. if [ -n "$route_IP" -a $do_delete = yes ]; then
  115. /usr/sbin/route -n delete default \
  116. -gateway $route_IP >/dev/null
  117. fi
  118. else
  119. /usr/sbin/route -fn > /dev/null
  120. fi
  121. else
  122. defrouters=
  123. fi
  124. #
  125. # Use routeadm(1M) to configure forwarding and launch routing daemons
  126. # for IPv4 and IPv6 based on preset values. These settings only apply
  127. # to the global zone. For IPv4 dynamic routing, the system will default
  128. # to disabled if a default route was previously added via BOOTP, DHCP,
  129. # or the /etc/defaultrouter file. routeadm also starts in.ndpd.
  130. #
  131. if [ "$dynamic_routing" != "true" ] && [ -z "$defrouters" ]; then
  132. #
  133. # No default routes were setup by "route" command above.
  134. # Check the kernel routing table for any other default
  135. # routes.
  136. #
  137. /usr/bin/netstat -rn -f inet | \
  138. /usr/bin/grep default >/dev/null 2>&1 && defrouters=yes
  139. fi
  140. #
  141. # The routeadm/ipv4-routing-set property is true if the administrator
  142. # has run "routeadm -e/-d ipv4-routing". If not, we revert to the
  143. # appropriate defaults. We no longer run "routeadm -u" on every boot
  144. # however, as persistent daemon state is now controlled by SMF.
  145. #
  146. ipv4_routing_set=`/usr/bin/svcprop -p routeadm/ipv4-routing-set $SMF_FMRI`
  147. if [ -z "$defrouters" ]; then
  148. #
  149. # Set default value for ipv4-routing to enabled. If routeadm -e/-d
  150. # has not yet been run by the administrator, we apply this default.
  151. # The -b option is project-private and informs routeadm not
  152. # to treat the enable as administrator-driven.
  153. #
  154. /usr/sbin/svccfg -s $SMF_FMRI \
  155. setprop routeadm/default-ipv4-routing = true
  156. if [ "$ipv4_routing_set" = "false" ]; then
  157. /sbin/routeadm -b -e ipv4-routing -u
  158. fi
  159. else
  160. #
  161. # Default router(s) have been found, so ipv4-routing default value
  162. # should be disabled. If routaedm -e/d has not yet been run by
  163. # the administrator, we apply this default. The -b option is
  164. # project-private and informs routeadm not to treat the disable as
  165. # administrator-driven.
  166. #
  167. /usr/sbin/svccfg -s $SMF_FMRI \
  168. setprop routeadm/default-ipv4-routing = false
  169. if [ "$ipv4_routing_set" = "false" ]; then
  170. /sbin/routeadm -b -d ipv4-routing -u
  171. fi
  172. fi
  173. #
  174. # See if static routes were created by install. If so, they were created
  175. # under /etc/svc/volatile. Copy them into their proper place.
  176. #
  177. if [ -f /etc/svc/volatile/etc/inet/static_routes ]; then
  178. echo "Installing persistent routes"
  179. if [ -f /etc/inet/static_routes ]; then
  180. cat /etc/svc/volatile/etc/inet/static_routes | grep -v '^#' \
  181. >> /etc/inet/static_routes
  182. else
  183. cp /etc/svc/volatile/etc/inet/static_routes \
  184. /etc/inet/static_routes
  185. fi
  186. /usr/bin/rm /etc/svc/volatile/etc/inet/static_routes
  187. fi
  188. #
  189. # Read /etc/inet/static_routes and add each route.
  190. #
  191. if [ -f /etc/inet/static_routes ]; then
  192. echo "Adding persistent routes:"
  193. /usr/bin/egrep -v "^(#|$)" /etc/inet/static_routes | while read line; do
  194. /usr/sbin/route add $line
  195. done
  196. fi
  197. # Clear exit status.
  198. exit $SMF_EXIT_OK