dhclient-script 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #!/bin/sh
  2. # dhclient-script from OpenWRT project
  3. # http://git.openwrt.org/?p=packages.git;a=blob;f=net/isc-dhcp/files/dhclient-script;h=4afebc0ad20ebac51c5baae5ed01c6713e3a0fd0;hb=HEAD
  4. make_resolv_conf() {
  5. if [ x"$new_domain_name_servers" != x ]; then
  6. cat /dev/null > /etc/resolv.conf.dhclient
  7. chmod 644 /etc/resolv.conf.dhclient
  8. if [ x"$new_domain_search" != x ]; then
  9. echo search $new_domain_search >> /etc/resolv.conf.dhclient
  10. elif [ x"$new_domain_name" != x ]; then
  11. # Note that the DHCP 'Domain Name Option' is really just a domain
  12. # name, and that this practice of using the domain name option as
  13. # a search path is both nonstandard and deprecated.
  14. echo search $new_domain_name >> /etc/resolv.conf.dhclient
  15. fi
  16. for nameserver in $new_domain_name_servers; do
  17. echo nameserver $nameserver >>/etc/resolv.conf.dhclient
  18. done
  19. elif [ "x${new_dhcp6_name_servers}" != x ] ; then
  20. cat /dev/null > /etc/resolv.conf.dhclient6
  21. chmod 644 /etc/resolv.conf.dhclient6
  22. if [ "x${new_dhcp6_domain_search}" != x ] ; then
  23. echo search ${new_dhcp6_domain_search} >> /etc/resolv.conf.dhclient6
  24. fi
  25. for nameserver in ${new_dhcp6_name_servers} ; do
  26. echo nameserver ${nameserver} >> /etc/resolv.conf.dhclient6
  27. done
  28. fi
  29. # if both v4 and v6 clients are running, concatenate results
  30. cat /etc/resolv.conf.* > /etc/resolv.conf
  31. }
  32. # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
  33. exit_with_hooks() {
  34. exit_status=$1
  35. if [ -f /etc/dhclient-exit-hooks ]; then
  36. . /etc/dhclient-exit-hooks
  37. fi
  38. # probably should do something with exit status of the local script
  39. exit $exit_status
  40. }
  41. # Invoke the local dhcp client enter hooks, if they exist.
  42. if [ -f /etc/dhclient-enter-hooks ]; then
  43. exit_status=0
  44. . /etc/dhclient-enter-hooks
  45. # allow the local script to abort processing of this state
  46. # local script must set exit_status variable to nonzero.
  47. if [ $exit_status -ne 0 ]; then
  48. exit $exit_status
  49. fi
  50. fi
  51. ###
  52. ### DHCPv4 Handlers
  53. ###
  54. if [ x$new_broadcast_address != x ]; then
  55. new_broadcast_arg="broadcast $new_broadcast_address"
  56. fi
  57. if [ x$new_subnet_mask != x ]; then
  58. new_subnet_arg="netmask $new_subnet_mask"
  59. fi
  60. if [ x$alias_subnet_mask != x ]; then
  61. alias_subnet_arg="netmask $alias_subnet_mask"
  62. fi
  63. if [ x$reason = xMEDIUM ]; then
  64. # Linux doesn't do mediums (ok, ok, media).
  65. exit_with_hooks 0
  66. fi
  67. if [ x$reason = xPREINIT ]; then
  68. if [ x$alias_ip_address != x ]; then
  69. # Bring down alias interface. Its routes will disappear too.
  70. ifconfig $interface:0- 0.0.0.0
  71. fi
  72. ifconfig $interface 0.0.0.0 up
  73. # We need to give the kernel some time to get the interface up.
  74. sleep 1
  75. exit_with_hooks 0
  76. fi
  77. if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
  78. exit_with_hooks 0
  79. fi
  80. if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
  81. [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
  82. current_hostname=`hostname`
  83. if [ x$current_hostname = x ] || \
  84. [ x$current_hostname = x$old_host_name ]; then
  85. if [ x$current_hostname = x ] || \
  86. [ x$new_host_name != x$old_host_name ]; then
  87. hostname $new_host_name
  88. fi
  89. fi
  90. if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
  91. [ x$alias_ip_address != x$old_ip_address ]; then
  92. # Possible new alias. Remove old alias.
  93. ifconfig $interface:0- 0.0.0.0
  94. fi
  95. if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
  96. # IP address changed. Bringing down the interface will delete all routes,
  97. # and clear the ARP cache.
  98. ifconfig $interface 0.0.0.0 down
  99. fi
  100. if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
  101. [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
  102. ifconfig $interface $new_ip_address $new_subnet_arg \
  103. $new_broadcast_arg
  104. for router in $new_routers; do
  105. if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
  106. route add -host $router dev $interface
  107. fi
  108. route add default gw $router
  109. done
  110. fi
  111. if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
  112. then
  113. ifconfig $interface:0- 0.0.0.0
  114. ifconfig $interface:0 $alias_ip_address $alias_subnet_arg
  115. route add -host $alias_ip_address $interface:0
  116. fi
  117. make_resolv_conf
  118. exit_with_hooks 0
  119. fi
  120. if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
  121. || [ x$reason = xSTOP ]; then
  122. if [ x$alias_ip_address != x ]; then
  123. # Turn off alias interface.
  124. ifconfig $interface:0- 0.0.0.0
  125. fi
  126. if [ x$old_ip_address != x ]; then
  127. # Shut down interface, which will delete routes and clear arp cache.
  128. ifconfig $interface 0.0.0.0 down
  129. fi
  130. if [ x$alias_ip_address != x ]; then
  131. ifconfig $interface:0 $alias_ip_address $alias_subnet_arg
  132. route add -host $alias_ip_address $interface:0
  133. fi
  134. # remove v4 dns configuration for this interface
  135. rm /etc/resolv.conf.dhclient
  136. cat /etc/resolv.conf.* > /etc/resolv.conf
  137. exit_with_hooks 0
  138. fi
  139. if [ x$reason = xTIMEOUT ]; then
  140. if [ x$alias_ip_address != x ]; then
  141. ifconfig $interface:0- 0.0.0.0
  142. fi
  143. ifconfig $interface $new_ip_address $new_subnet_arg \
  144. $new_broadcast_arg
  145. set $new_routers
  146. if ping -q -c 1 $1; then
  147. if [ x$new_ip_address != x$alias_ip_address ] && \
  148. [ x$alias_ip_address != x ]; then
  149. ifconfig $interface:0 $alias_ip_address $alias_subnet_arg
  150. route add -host $alias_ip_address dev $interface:0
  151. fi
  152. for router in $new_routers; do
  153. if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
  154. route add -host $router dev $interface
  155. fi
  156. route add default gw $router
  157. done
  158. make_resolv_conf
  159. exit_with_hooks 0
  160. fi
  161. ifconfig $interface 0.0.0.0 down
  162. exit_with_hooks 1
  163. fi
  164. ###
  165. ### DHCPv6 Handlers
  166. ###
  167. if [ x$reason = xPREINIT6 ]; then
  168. # Ensure interface is up.
  169. ifconfig ${interface} up
  170. # Remove any stale addresses from aborted clients.
  171. ip -f inet6 addr flush dev ${interface} scope global
  172. exit_with_hooks 0
  173. fi
  174. if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ] ; then
  175. echo Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix}
  176. exit_with_hooks 0
  177. fi
  178. if [ x$reason = xBOUND6 ]; then
  179. if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then
  180. exit_with_hooks 2;
  181. fi
  182. ifconfig ${interface} add ${new_ip6_address}/${new_ip6_prefixlen}
  183. # Check for nameserver options.
  184. make_resolv_conf
  185. ### <<
  186. # Set up softwire tunnel
  187. if [ x${new_dhcp6_softwire} != x ] ; then
  188. /etc/init.d/dhclient stop
  189. ifconfig ${interface} 0.0.0.0
  190. ip -6 tunnel add tun0 mode ipip6 \
  191. remote ${new_dhcp6_softwire} \
  192. local ${new_ip6_address} \
  193. dev ${interface} encaplimit none
  194. ip link set tun0 up
  195. ip route add default dev tun0
  196. fi
  197. ### >>
  198. exit_with_hooks 0
  199. fi
  200. if [ x$reason = xRENEW6 ] || [ x$reason = xREBIND6 ]; then
  201. if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then
  202. exit_with_hooks 2;
  203. fi
  204. ifconfig ${interface} add ${new_ip6_address}/${new_ip6_prefixlen}
  205. # Make sure nothing has moved around on us.
  206. # Nameservers/domains/etc.
  207. if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
  208. [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then
  209. make_resolv_conf
  210. fi
  211. exit_with_hooks 0
  212. fi
  213. if [ x$reason = xDEPREF6 ]; then
  214. if [ x${new_ip6_address} = x ] ; then
  215. exit_with_hooks 2;
  216. fi
  217. # Busybox ifconfig has no way to communicate this to the kernel, so ignore it
  218. exit_with_hooks 0
  219. fi
  220. if [ x$reason = xEXPIRE6 -o x$reason = xRELEASE6 -o x$reason = xSTOP6 ]; then
  221. if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then
  222. exit_with_hooks 2;
  223. fi
  224. ifconfig ${interface} del ${old_ip6_address}/${old_ip6_prefixlen}
  225. # remove v6 dns configuration for this interface
  226. rm /etc/resolv.conf.dhclient6
  227. cat /etc/resolv.conf.* > /etc/resolv.conf
  228. ### <<
  229. # Tear down softwire tunnel
  230. if [ x${old_dhcp6_softwire} != x ] ; then
  231. ip link set tun0 down
  232. ip tunnel del tun0
  233. fi
  234. ### >>
  235. exit_with_hooks 0
  236. fi
  237. exit_with_hooks 0