mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
8.6 KiB

  1. #!/bin/bash
  2. # dhclient-script for Linux. Dan Halbert, March, 1997.
  3. # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
  4. # No guarantees about this. I'm a novice at the details of Linux
  5. # networking.
  6. export PATH=/bin:/sbin:/usr/bin:/usr/sbin
  7. # Notes:
  8. # 0. This script is based on the netbsd script supplied with dhcp-970306.
  9. # 1. ifconfig down apparently deletes all relevant routes and flushes
  10. # the arp cache, so this doesn't need to be done explicitly.
  11. # 2. The alias address handling here has not been tested AT ALL.
  12. # I'm just going by the doc of modern Linux ip aliasing, which uses
  13. # notations like eth0:0, eth0:1, for each alias.
  14. # 3. I have to calculate the network address, and calculate the broadcast
  15. # address if it is not supplied. This might be much more easily done
  16. # by the dhclient C code, and passed on.
  17. # 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
  18. # of the $1 in its args.
  19. # 'ip' just looks too weird. /sbin/ip looks less weird.
  20. ip=/sbin/ip
  21. make_resolv_conf() {
  22. if [ x"$new_domain_name_servers" != x ]; then
  23. cat /dev/null > /etc/resolv.conf.dhclient
  24. chmod 644 /etc/resolv.conf.dhclient
  25. if [ x"$new_domain_search" != x ]; then
  26. echo search $new_domain_search >> /etc/resolv.conf.dhclient
  27. elif [ x"$new_domain_name" != x ]; then
  28. # Note that the DHCP 'Domain Name Option' is really just a domain
  29. # name, and that this practice of using the domain name option as
  30. # a search path is both nonstandard and deprecated.
  31. echo search $new_domain_name >> /etc/resolv.conf.dhclient
  32. fi
  33. for nameserver in $new_domain_name_servers; do
  34. echo nameserver $nameserver >>/etc/resolv.conf.dhclient
  35. done
  36. mv /etc/resolv.conf.dhclient /etc/resolv.conf
  37. elif [ "x${new_dhcp6_name_servers}" != x ] ; then
  38. cat /dev/null > /etc/resolv.conf.dhclient6
  39. chmod 644 /etc/resolv.conf.dhclient6
  40. if [ "x${new_dhcp6_domain_search}" != x ] ; then
  41. echo search ${new_dhcp6_domain_search} >> /etc/resolv.conf.dhclient6
  42. fi
  43. for nameserver in ${new_dhcp6_name_servers} ; do
  44. echo nameserver ${nameserver} >> /etc/resolv.conf.dhclient6
  45. done
  46. mv /etc/resolv.conf.dhclient6 /etc/resolv.conf
  47. fi
  48. }
  49. # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
  50. exit_with_hooks() {
  51. exit_status=$1
  52. if [ -f /etc/dhclient-exit-hooks ]; then
  53. . /etc/dhclient-exit-hooks
  54. fi
  55. # probably should do something with exit status of the local script
  56. exit $exit_status
  57. }
  58. # Invoke the local dhcp client enter hooks, if they exist.
  59. if [ -f /etc/dhclient-enter-hooks ]; then
  60. exit_status=0
  61. . /etc/dhclient-enter-hooks
  62. # allow the local script to abort processing of this state
  63. # local script must set exit_status variable to nonzero.
  64. if [ $exit_status -ne 0 ]; then
  65. exit $exit_status
  66. fi
  67. fi
  68. release=`uname -r`
  69. release=`expr $release : '\(.*\)\..*'`
  70. relminor=`echo $release |sed -e 's/[0-9]*\.\([0-9][0-9]*\)\(\..*\)*$/\1/'`
  71. relmajor=`echo $release |sed -e 's/\([0-9][0-9]*\)\..*$/\1/'`
  72. ###
  73. ### DHCPv4 Handlers
  74. ###
  75. if [ x$new_broadcast_address != x ]; then
  76. new_broadcast_arg="broadcast $new_broadcast_address"
  77. fi
  78. if [ x$old_broadcast_address != x ]; then
  79. old_broadcast_arg="broadcast $old_broadcast_address"
  80. fi
  81. if [ x$new_subnet_mask != x ]; then
  82. new_subnet_arg="netmask $new_subnet_mask"
  83. fi
  84. if [ x$old_subnet_mask != x ]; then
  85. old_subnet_arg="netmask $old_subnet_mask"
  86. fi
  87. if [ x$alias_subnet_mask != x ]; then
  88. alias_subnet_arg="netmask $alias_subnet_mask"
  89. fi
  90. if [ x$reason = xMEDIUM ]; then
  91. # Linux doesn't do mediums (ok, ok, media).
  92. exit_with_hooks 0
  93. fi
  94. if [ x$reason = xPREINIT ]; then
  95. if [ x$alias_ip_address != x ]; then
  96. # Bring down alias interface. Its routes will disappear too.
  97. ifconfig $interface:0- inet 0
  98. fi
  99. if [ $relmajor -lt 2 ] || ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] )
  100. then
  101. ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
  102. broadcast 255.255.255.255 up
  103. # Add route to make broadcast work. Do not omit netmask.
  104. route add default dev $interface netmask 0.0.0.0
  105. else
  106. ifconfig $interface 0 up
  107. fi
  108. # We need to give the kernel some time to get the interface up.
  109. sleep 1
  110. exit_with_hooks 0
  111. fi
  112. if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
  113. exit_with_hooks 0
  114. fi
  115. if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
  116. [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
  117. current_hostname=`hostname`
  118. if [ x$current_hostname = x ] || \
  119. [ x$current_hostname = x$old_host_name ]; then
  120. if [ x$current_hostname = x ] || \
  121. [ x$new_host_name != x$old_host_name ]; then
  122. hostname $new_host_name
  123. fi
  124. fi
  125. if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
  126. [ x$alias_ip_address != x$old_ip_address ]; then
  127. # Possible new alias. Remove old alias.
  128. ifconfig $interface:0- inet 0
  129. fi
  130. if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
  131. # IP address changed. Bringing down the interface will delete all routes,
  132. # and clear the ARP cache.
  133. ifconfig $interface inet 0 down
  134. fi
  135. if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
  136. [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
  137. ifconfig $interface inet $new_ip_address $new_subnet_arg \
  138. $new_broadcast_arg
  139. # Add a network route to the computed network address.
  140. if [ $relmajor -lt 2 ] || \
  141. ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
  142. route add -net $new_network_number $new_subnet_arg dev $interface
  143. fi
  144. for router in $new_routers; do
  145. route add default gw $router
  146. done
  147. fi
  148. if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
  149. then
  150. ifconfig $interface:0- inet 0
  151. ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  152. route add -host $alias_ip_address $interface:0
  153. fi
  154. make_resolv_conf
  155. exit_with_hooks 0
  156. fi
  157. if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
  158. || [ x$reason = xSTOP ]; then
  159. if [ x$alias_ip_address != x ]; then
  160. # Turn off alias interface.
  161. ifconfig $interface:0- inet 0
  162. fi
  163. if [ x$old_ip_address != x ]; then
  164. # Shut down interface, which will delete routes and clear arp cache.
  165. ifconfig $interface inet 0 down
  166. fi
  167. if [ x$alias_ip_address != x ]; then
  168. ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  169. route add -host $alias_ip_address $interface:0
  170. fi
  171. exit_with_hooks 0
  172. fi
  173. if [ x$reason = xTIMEOUT ]; then
  174. if [ x$alias_ip_address != x ]; then
  175. ifconfig $interface:0- inet 0
  176. fi
  177. ifconfig $interface inet $new_ip_address $new_subnet_arg \
  178. $new_broadcast_arg
  179. set $new_routers
  180. if ping -q -c 1 $1; then
  181. if [ x$new_ip_address != x$alias_ip_address ] && \
  182. [ x$alias_ip_address != x ]; then
  183. ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  184. route add -host $alias_ip_address dev $interface:0
  185. fi
  186. if [ $relmajor -lt 2 ] || \
  187. ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
  188. route add -net $new_network_number
  189. fi
  190. for router in $new_routers; do
  191. route add default gw $router
  192. done
  193. make_resolv_conf
  194. exit_with_hooks 0
  195. fi
  196. ifconfig $interface inet 0 down
  197. exit_with_hooks 1
  198. fi
  199. ###
  200. ### DHCPv6 Handlers
  201. ###
  202. if [ ${reason} = PREINIT6 ] ; then
  203. # Ensure interface is up.
  204. ${ip} link set ${interface} up
  205. # Remove any stale addresses from aborted clients.
  206. ${ip} -f inet6 addr flush dev ${interface} scope global permanent
  207. exit_with_hooks 0
  208. fi
  209. if [ ${reason} = BOUND6 ] ; then
  210. if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then
  211. exit_with_hooks 2;
  212. fi
  213. ${ip} -f inet6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \
  214. dev ${interface} scope global
  215. # Check for nameserver options.
  216. make_resolv_conf
  217. exit_with_hooks 0
  218. fi
  219. if [ ${reason} = RENEW6 ] || [ ${reason} = REBIND6 ] ; then
  220. # Make sure nothing has moved around on us.
  221. # Nameservers/domains/etc.
  222. if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
  223. [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then
  224. make_resolv_conf
  225. fi
  226. exit_with_hooks 0
  227. fi
  228. if [ ${reason} = DEPREF6 ] ; then
  229. if [ x${new_ip6_prefixlen} = x ] ; then
  230. exit_with_hooks 2;
  231. fi
  232. # There doesn't appear to be a way to update an addr to indicate
  233. # preference.
  234. # ${ip} -f inet6 addr ??? ${new_ip6_address}/${new_ip6_prefixlen} \
  235. # dev ${interface} scope global deprecated?
  236. exit_with_hooks 0
  237. fi
  238. if [ ${reason} = EXPIRE6 ] ; then
  239. if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then
  240. exit_with_hooks 2;
  241. fi
  242. ${ip} -f inet6 addr del ${old_ip6_address}/${old_ip6_prefixlen} \
  243. dev ${interface}
  244. exit_with_hooks 0
  245. fi
  246. exit_with_hooks 0