dhclient-script 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. #!/bin/bash
  2. #
  3. # dhclient-script: Network interface configuration script run by
  4. # dhclient based on DHCP client communication
  5. #
  6. # Copyright (C) 2008, 2009, 2010 Red Hat, Inc.
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. # Author(s): David Cantrell <dcantrell@redhat.com>
  22. #
  23. # ----------
  24. # This script is a rewrite/reworking on dhclient-script originally
  25. # included as part of dhcp-970306:
  26. # dhclient-script for Linux. Dan Halbert, March, 1997.
  27. # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
  28. # Modified by David Cantrell <dcantrell@redhat.com> for Fedora and RHEL
  29. # ----------
  30. #
  31. PATH=/bin:/usr/bin:/sbin
  32. SAVEDIR=/var/lib/dhclient
  33. LOGFACILITY="local7"
  34. LOGLEVEL="notice"
  35. ETCDIR="/etc/dhcp"
  36. logmessage() {
  37. msg="${1}"
  38. logger -p ${LOGFACILITY}.${LOGLEVEL} -t "NET" "dhclient: ${msg}"
  39. }
  40. fix_context() {
  41. if [ -x /sbin/restorecon ]; then
  42. /sbin/restorecon ${1} >/dev/null 2>&1
  43. fi
  44. }
  45. save_previous() {
  46. origfile="${1}"
  47. savefile="${SAVEDIR}/${origfile##*/}.predhclient.${interface}"
  48. if [ ! -d ${SAVEDIR} ]; then
  49. mkdir -p ${SAVEDIR}
  50. fi
  51. if [ -e ${origfile} ]; then
  52. contents="$(< ${origfile})"
  53. echo "${contents}" > ${savefile}
  54. rm -f ${origfile}
  55. else
  56. echo > ${savefile}
  57. fi
  58. fix_context ${savefile}
  59. }
  60. make_resolv_conf() {
  61. [ "${PEERDNS}" = "no" ] && return
  62. if [ "${reason}" = "RENEW" ] &&
  63. [ "${new_domain_name}" = "${old_domain_name}" ] &&
  64. [ "${new_domain_name_servers}" = "${old_domain_name_servers}" ]; then
  65. return
  66. fi
  67. if [ -n "${new_domain_name}" ] ||
  68. [ -n "${new_domain_name_servers}" ] ||
  69. [ -n "${new_domain_search}" ]; then
  70. save_previous /etc/resolv.conf
  71. rscf="$(mktemp /tmp/XXXXXX)"
  72. echo "; generated by /sbin/dhclient-script" > ${rscf}
  73. if [ -n "${SEARCH}" ]; then
  74. echo "search ${SEARCH}" >> $rscf
  75. else
  76. if [ -n "${new_domain_search}" ]; then
  77. echo "search ${new_domain_search//\\032/ }" >> ${rscf}
  78. elif [ -n "${new_domain_name}" ]; then
  79. # Note that the DHCP 'Domain Name Option' is really just a domain
  80. # name, and that this practice of using the domain name option as
  81. # a search path is both nonstandard and deprecated.
  82. echo "search ${new_domain_name//\\032/ }" >> ${rscf}
  83. fi
  84. fi
  85. if [ -n "${RES_OPTIONS}" ]; then
  86. echo "options ${RES_OPTIONS}" >> ${rscf}
  87. fi
  88. for nameserver in ${new_domain_name_servers} ; do
  89. echo "nameserver ${nameserver}" >> ${rscf}
  90. done
  91. change_resolv_conf ${rscf}
  92. rm -f ${rscf}
  93. fix_context /etc/resolv.conf
  94. elif [ -n "${new_dhcp6_name_servers}" ] ||
  95. [ -n "${new_dhcp6_domain_search}" ]; then
  96. save_previous /etc/resolv.conf
  97. rscf="$(mktemp /tmp/XXXXXX)"
  98. echo "; generated by /sbin/dhclient-script" > ${rscf}
  99. if [ -n "${SEARCH}" ]; then
  100. echo "search ${SEARCH}" >> $rscf
  101. else
  102. if [ -n "${new_dhcp6_domain_search}" ]; then
  103. echo "search ${new_dhcp6_domain_search//\\032/ }" >> ${rscf}
  104. fi
  105. fi
  106. if [ -n "${RES_OPTIONS}" ]; then
  107. echo "options ${RES_OPTIONS}" >> ${rscf}
  108. fi
  109. for nameserver in ${new_dhcp6_name_servers} ; do
  110. echo "nameserver ${nameserver}" >> ${rscf}
  111. done
  112. change_resolv_conf ${rscf}
  113. rm -f ${rscf}
  114. fix_context /etc/resolv.conf
  115. fi
  116. }
  117. exit_with_hooks() {
  118. exit_status="${1}"
  119. if [ -x ${ETCDIR}/dhclient-exit-hooks ]; then
  120. . ${ETCDIR}/dhclient-exit-hooks
  121. fi
  122. exit ${exit_status}
  123. }
  124. quad2num() {
  125. if [ $# -eq 4 ]; then
  126. let n="${1} << 24 | ${2} << 16 | ${3} << 8 | ${4}"
  127. echo "${n}"
  128. return 0
  129. else
  130. echo "0"
  131. return 1
  132. fi
  133. }
  134. ip2num() {
  135. IFS="." quad2num ${1}
  136. }
  137. num2ip() {
  138. let n="${1}"
  139. let o1="(n >> 24) & 0xff"
  140. let o2="(n >> 16) & 0xff"
  141. let o3="(n >> 8) & 0xff"
  142. let o4="n & 0xff"
  143. echo "${o1}.${o2}.${o3}.${o4}"
  144. }
  145. get_network_address() {
  146. # get network address for the given IP address and (netmask or prefix)
  147. ip="${1}"
  148. nm="${2}"
  149. if [ -n "${ip}" -a -n "${nm}" ]; then
  150. if [[ "${nm}" = *.* ]]; then
  151. ipcalc -s -n ${ip} ${nm} | cut -d '=' -f 2
  152. else
  153. ipcalc -s -n ${ip}/${nm} | cut -d '=' -f 2
  154. fi
  155. fi
  156. }
  157. get_prefix() {
  158. # get prefix for the given IP address and mask
  159. ip="${1}"
  160. nm="${2}"
  161. if [ -n "${ip}" -a -n "${nm}" ]; then
  162. ipcalc -s -p ${ip} ${nm} | cut -d '=' -f 2
  163. fi
  164. }
  165. class_bits() {
  166. let ip=$(IFS='.' ip2num $1)
  167. let bits=32
  168. let mask='255'
  169. for ((i=0; i <= 3; i++, 'mask<<=8')); do
  170. let v='ip&mask'
  171. if [ "$v" -eq 0 ] ; then
  172. let bits-=8
  173. else
  174. break
  175. fi
  176. done
  177. echo $bits
  178. }
  179. is_router_reachable() {
  180. # handle DHCP servers that give us a router not on our subnet
  181. router="${1}"
  182. routersubnet="$(get_network_address ${router} ${new_subnet_mask})"
  183. mysubnet="$(get_network_address ${new_ip_address} ${new_subnet_mask})"
  184. unreachable=0
  185. if [ ! "${routersubnet}" = "${mysubnet}" ]; then
  186. unreachable=1
  187. if arping -f -q -I ${interface} -w2 ${router}; then
  188. ip -4 route add ${router}/32 dev ${interface}
  189. if [ $? -eq 0 ]; then
  190. unreachable=0
  191. else
  192. logmessage "failed to create host router for unreachable router ${router} not on subnet ${mysubnet}"
  193. fi
  194. else
  195. unreachable=1
  196. logmessage "DHCP router ${router} is unreachable on DHCP subnet ${mysubnet} router subnet ${routersubnet}"
  197. fi
  198. fi
  199. return ${unreachable}
  200. }
  201. add_default_gateway() {
  202. router="${1}"
  203. metric=""
  204. if [ $# -gt 1 ] && [ ${2} -gt 0 ]; then
  205. metric="metric ${2}"
  206. fi
  207. if is_router_reachable ${router} ; then
  208. ip -4 route replace default via ${router} dev ${interface} ${metric}
  209. if [ $? -ne 0 ]; then
  210. logmessage "failed to create default route: ${router} dev ${interface} ${metric}"
  211. return 1
  212. else
  213. return 0
  214. fi
  215. fi
  216. return 1
  217. }
  218. dhconfig() {
  219. if [ -n "${old_ip_address}" ] && [ -n "${alias_ip_address}" ] &&
  220. [ ! "${alias_ip_address}" = "${old_ip_address}" ]; then
  221. # possible new alias, remove old alias first
  222. ip -4 addr del ${old_ip_address} dev ${interface}:0
  223. fi
  224. if [ -n "${old_ip_address}" ] &&
  225. [ ! "${old_ip_address}" = "${new_ip_address}" ]; then
  226. # IP address changed. Bringing down the interface will delete all
  227. # routes, and clear the ARP cache.
  228. ip -4 addr flush dev ${interface} >/dev/null 2>&1
  229. fi
  230. if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
  231. [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
  232. [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
  233. [ ! "${old_network_number}" = "${new_network_number}" ] ||
  234. [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
  235. [ ! "${old_routers}" = "${new_routers}" ] ||
  236. [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
  237. ip -4 addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
  238. ip link set dev ${interface} up
  239. # The 576 MTU is only used for X.25 and dialup connections
  240. # where the admin wants low latency. Such a low MTU can cause
  241. # problems with UDP traffic, among other things. As such,
  242. # disallow MTUs from 576 and below by default, so that broken
  243. # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
  244. if [ -n "${new_interface_mtu}" ] && [ ${new_interface_mtu} -gt 576 ]; then
  245. ip link set ${interface} mtu ${new_interface_mtu}
  246. fi
  247. if [ -x ${ETCDIR}/dhclient-${interface}-up-hooks ]; then
  248. . ${ETCDIR}/dhclient-${interface}-up-hooks
  249. elif [ -x ${ETCDIR}/dhclient-up-hooks ]; then
  250. . ${ETCDIR}/dhclient-up-hooks
  251. fi
  252. # static routes
  253. if [ -n "${new_classless_static_routes}" ] ||
  254. [ -n "${new_static_routes}" ]; then
  255. if [ -n "${new_classless_static_routes}" ]; then
  256. IFS=', |' static_routes=(${new_classless_static_routes})
  257. # If the DHCP server returns both a Classless Static Routes option and
  258. # a Router option, the DHCP client MUST ignore the Router option. (RFC3442)
  259. new_routers=""
  260. else
  261. IFS=', |' static_routes=(${new_static_routes})
  262. fi
  263. route_targets=()
  264. for((i=0; i<${#static_routes[@]}; i+=2)); do
  265. target=${static_routes[$i]}
  266. if [ -n "${new_classless_static_routes}" ]; then
  267. prefix=$(echo ${target} | cut -d "." -f 1)
  268. target=$(echo ${target} | cut -d "." -f 2-)
  269. IFS="." target_arr=(${target})
  270. unset IFS
  271. ((pads=4-${#target_arr[@]}))
  272. for j in $(seq $pads); do
  273. target=${target}".0"
  274. done
  275. # Client MUST zero any bits in the subnet number where the corresponding bit in the mask is zero.
  276. # In other words, the subnet number installed in the routing table is the logical AND of
  277. # the subnet number and subnet mask given in the Classless Static Routes option. (RFC3442)
  278. target="$(get_network_address ${target} ${prefix})"
  279. else
  280. prefix=$(class_bits ${target})
  281. fi
  282. gateway=${static_routes[$i+1]}
  283. metric=''
  284. for t in ${route_targets[@]}; do
  285. if [ ${t} = ${target} ]; then
  286. if [ -z "${metric}" ]; then
  287. metric=1
  288. else
  289. ((metric=metric+1))
  290. fi
  291. fi
  292. done
  293. if [ -n "${metric}" ]; then
  294. metric="metric ${metric}"
  295. fi
  296. if is_router_reachable ${gateway}; then
  297. ip -4 route replace ${target}/${prefix} via ${gateway} dev ${interface} ${metric}
  298. if [ $? -ne 0 ]; then
  299. logmessage "failed to create static route: ${target}/${prefix} via ${gateway} dev ${interface} ${metric}"
  300. else
  301. route_targets=(${route_targets[@]} ${target})
  302. fi
  303. fi
  304. done
  305. fi
  306. # gateways
  307. if [[ ( "${DEFROUTE}" != "no") &&
  308. (( -z "${GATEWAYDEV}" ) ||
  309. ( "${GATEWAYDEV}" = "${interface}" )) ]]; then
  310. if [[ ( -z "$GATEWAY" ) ||
  311. (( -n "$DHCLIENT_IGNORE_GATEWAY" ) &&
  312. ( "$DHCLIENT_IGNORE_GATEWAY" = [Yy]* )) ]]; then
  313. metric="${METRIC:-}"
  314. let i="${METRIC:-0}"
  315. default_routers=()
  316. for router in ${new_routers} ; do
  317. added_router=-
  318. for r in ${default_routers[@]} ; do
  319. if [ "${r}" = "${router}" ]; then
  320. added_router=1
  321. fi
  322. done
  323. if [ -z "${router}" ] ||
  324. [ "${added_router}" = "1" ] ||
  325. [ $(IFS=. ip2num ${router}) -le 0 ] ||
  326. [[ ( "${router}" = "${new_broadcast_address}" ) &&
  327. ( "${new_subnet_mask}" != "255.255.255.255" ) ]]; then
  328. continue
  329. fi
  330. default_routers=(${default_routers[@]} ${router})
  331. add_default_gateway ${router} ${metric}
  332. let i=i+1
  333. metric=${i}
  334. done
  335. elif [ -n "${GATEWAY}" ]; then
  336. routersubnet=$(get_network_address ${GATEWAY} ${new_subnet_mask})
  337. mysubnet=$(get_network_address ${new_ip_address} ${new_subnet_mask})
  338. if [ "${routersubnet}" = "${mysubnet}" ]; then
  339. ip -4 route replace default via ${GATEWAY} dev ${interface}
  340. fi
  341. fi
  342. fi
  343. fi
  344. if [ ! "${new_ip_address}" = "${alias_ip_address}" ] &&
  345. [ -n "${alias_ip_address}" ]; then
  346. ip -4 addr flush dev ${interface}:0 >/dev/null 2>&1
  347. ip -4 addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
  348. ip -4 route replace ${alias_ip_address}/32 dev ${interface}:0
  349. fi
  350. make_resolv_conf
  351. if [ -n "${new_host_name}" ] && need_hostname; then
  352. hostname ${new_host_name} || echo "See -nc option in dhclient(8) man page."
  353. fi
  354. if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
  355. [[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
  356. if [ -n "${new_time_offset}" ]; then
  357. # DHCP option "time-offset" is requested by default and should be
  358. # handled. The geographical zone abbreviation cannot be determined
  359. # from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be
  360. # used - note: this disables DST.
  361. ((z=new_time_offset/3600))
  362. ((hoursWest=$(printf '%+d' $z)))
  363. if (( $hoursWest < 0 )); then
  364. # tzdata treats negative 'hours west' as positive 'gmtoff'!
  365. ((hoursWest*=-1))
  366. fi
  367. tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' ${hoursWest})
  368. if [ -e ${tzfile} ]; then
  369. save_previous /etc/localtime
  370. cp -fp ${tzfile} /etc/localtime
  371. touch /etc/localtime
  372. fix_context /etc/localtime
  373. fi
  374. fi
  375. fi
  376. # execute any additional client side configuration scripts we have
  377. if [ -d ${ETCDIR}/dhclient.d ]; then
  378. for f in ${ETCDIR}/dhclient.d/*.sh ; do
  379. if [ -x ${f} ]; then
  380. subsystem="${f%.sh}"
  381. subsystem="${subsystem##*/}"
  382. . ${f}
  383. "${subsystem}_config"
  384. fi
  385. done
  386. fi
  387. }
  388. # Section 18.1.8. (Receipt of Reply Messages) of RFC 3315 says:
  389. # The client SHOULD perform duplicate address detection on each of
  390. # the addresses in any IAs it receives in the Reply message before
  391. # using that address for traffic.
  392. add_ipv6_addr_with_DAD() {
  393. ip -6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \
  394. dev ${interface} scope global
  395. # repeatedly test whether newly added address passed
  396. # duplicate address detection (DAD)
  397. for i in $(seq 5); do
  398. sleep 1 # give the DAD some time
  399. # tentative flag = DAD is still not complete or failed
  400. duplicate=$(ip -6 addr show dev ${interface} tentative \
  401. | grep ${new_ip6_address}/${new_ip6_prefixlen})
  402. # if there's no tentative flag, address passed DAD
  403. if [ -z "${duplicate}" ]; then
  404. break
  405. fi
  406. done
  407. # if there's still tentative flag = address didn't pass DAD =
  408. # = it's duplicate = remove it
  409. if [ -n "${duplicate}" ]; then
  410. ip -6 addr del ${new_ip6_address}/${new_ip6_prefixlen} dev ${interface}
  411. exit_with_hooks 3
  412. fi
  413. }
  414. dh6config() {
  415. case "${reason}" in
  416. BOUND6)
  417. if [ -z "${new_ip6_address}" ] &&
  418. [ -z "${new_ip6_prefixlen}" ]; then
  419. exit_with_hooks 2
  420. fi
  421. add_ipv6_addr_with_DAD
  422. make_resolv_conf
  423. ;;
  424. RENEW6|REBIND6)
  425. if [ -n "${new_ip6_prefixlen}" ] &&
  426. [ -n "${new_ip6_address}" ] &&
  427. [ ! "${new_ip6_address}" = "${old_ip6_address}" ]; then
  428. add_ipv6_addr_with_DAD
  429. fi
  430. if [ ! "${new_dhcp6_name_servers}" = "${old_dhcp6_name_servers}" ] ||
  431. [ ! "${new_dhcp6_domain_search}" = "${old_dhcp6_domain_search}" ]; then
  432. make_resolv_conf
  433. fi
  434. ;;
  435. DEPREF6)
  436. if [ -z "${new_ip6_prefixlen}" ]; then
  437. exit_with_hooks 2
  438. fi
  439. ip -6 addr change ${new_ip6_address}/${new_ip6_prefixlen} \
  440. dev ${interface} scope global preferred_lft 0
  441. ;;
  442. esac
  443. # execute any additional client side configuration scripts we have
  444. if [ -d ${ETCDIR}/dhclient.d ]; then
  445. for f in ${ETCDIR}/dhclient.d/*.sh ; do
  446. if [ -x ${f} ]; then
  447. subsystem="${f%.sh}"
  448. subsystem="${subsystem##*/}"
  449. . ${f}
  450. "${subsystem}_config"
  451. fi
  452. done
  453. fi
  454. }
  455. #
  456. # ### MAIN
  457. #
  458. if [ -x ${ETCDIR}/dhclient-enter-hooks ]; then
  459. exit_status=0
  460. # dhclient-enter-hooks can abort dhclient-script by setting
  461. # the exit_status variable to a non-zero value
  462. . ${ETCDIR}/dhclient-enter-hooks
  463. if [ ${exit_status} -ne 0 ]; then
  464. exit ${exit_status}
  465. fi
  466. fi
  467. if [ ! -r /etc/sysconfig/network-scripts/network-functions ]; then
  468. echo "Missing /etc/sysconfig/network-scripts/network-functions, exiting." >&2
  469. exit 1
  470. fi
  471. if [ ! -r /etc/rc.d/init.d/functions ]; then
  472. echo "Missing /etc/rc.d/init.d/functions, exiting." >&2
  473. exit 1
  474. fi
  475. . /etc/sysconfig/network-scripts/network-functions
  476. . /etc/rc.d/init.d/functions
  477. if [ -f /etc/sysconfig/network ]; then
  478. . /etc/sysconfig/network
  479. fi
  480. if [ -f /etc/sysconfig/networking/network ]; then
  481. . /etc/sysconfig/networking/network
  482. fi
  483. cd /etc/sysconfig/network-scripts
  484. CONFIG="ifcfg-${interface}"
  485. need_config ${CONFIG}
  486. source_config >/dev/null 2>&1
  487. new_prefix="$(get_prefix ${new_ip_address} ${new_subnet_mask})"
  488. old_prefix="$(get_prefix ${old_ip_address} ${old_subnet_mask})"
  489. alias_prefix="$(get_prefix ${alias_ip_address} ${alias_subnet_mask})"
  490. case "${reason}" in
  491. MEDIUM)
  492. # Linux doesn't handle mediums (media)
  493. exit_with_hooks 0
  494. ;;
  495. PREINIT)
  496. if [ -n "${alias_ip_address}" ]; then
  497. # Bring down alias interface, its routes will disappear too.
  498. ip link set ${interface}:0 down
  499. fi
  500. if [ "${keep_old_ip}" = "yes" ]; then
  501. ip link set ${interface} up
  502. else
  503. ip -4 addr flush dev ${interface} >/dev/null 2>&1
  504. ip link set ${interface} up
  505. fi
  506. if [ -n "${DHCLIENT_DELAY}" ] && [ ${DHCLIENT_DELAY} -gt 0 ]; then
  507. sleep ${DHCLIENT_DELAY}
  508. fi
  509. exit_with_hooks 0
  510. ;;
  511. PREINIT6)
  512. # ensure interface is up
  513. ip link set ${interface} up
  514. # remove any stale addresses from aborted clients
  515. ip -6 addr flush dev ${interface} scope global permanent
  516. exit_with_hooks 0
  517. ;;
  518. ARPCHECK|ARPSEND)
  519. if [ -z "${new_ip_address}" ] || [ -z "${interface}" ] ||
  520. arping -q -f -c 2 -w 3 -D -I ${interface} ${new_ip_address}; then
  521. exit_with_hooks 0
  522. else
  523. exit_with_hooks 1
  524. fi
  525. ;;
  526. BOUND|RENEW|REBIND|REBOOT)
  527. dhconfig
  528. exit_with_hooks 0
  529. ;;
  530. BOUND6|RENEW6|REBIND6|DEPREF6)
  531. dh6config
  532. exit_with_hooks 0
  533. ;;
  534. EXPIRE6|RELEASE6|STOP6)
  535. if [ -z "${old_ip6_address}" ] || [ -z "${old_ip6_prefixlen}" ]; then
  536. exit_with_hooks 2
  537. fi
  538. ip -6 addr del ${old_ip6_address}/${old_ip6_prefixlen} \
  539. dev ${interface}
  540. # execute any additional client side configuration scripts we have
  541. if [ -d ${ETCDIR}/dhclient.d ]; then
  542. for f in ${ETCDIR}/dhclient.d/*.sh ; do
  543. if [ -x ${f} ]; then
  544. subsystem="${f%.sh}"
  545. subsystem="${subsystem##*/}"
  546. . ${f}
  547. "${subsystem}_restore"
  548. fi
  549. done
  550. fi
  551. if [ -x ${ETCDIR}/dhclient-${interface}-down-hooks ]; then
  552. . ${ETCDIR}/dhclient-${interface}-down-hooks
  553. elif [ -x ${ETCDIR}/dhclient-down-hooks ]; then
  554. . ${ETCDIR}/dhclient-down-hooks
  555. fi
  556. exit_with_hooks 0
  557. ;;
  558. EXPIRE|FAIL|RELEASE|STOP)
  559. # only restore config files if there are no other dhclient processes
  560. # running (#306381)
  561. any_other_clients="$(ps -eo pid,ppid,comm | grep dhclient | grep -v ${PPID})"
  562. if [ -n "${any_other_clients}" ]; then
  563. if [ -f ${SAVEDIR}/resolv.conf.predhclient.${interface} ]; then
  564. change_resolv_conf ${SAVEDIR}/resolv.conf.predhclient.${interface}
  565. rm -f ${SAVEDIR}/resolv.conf.predhclient.${interface}
  566. fi
  567. if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
  568. [[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
  569. if [ -e ${SAVEDIR}/localtime.predhclient.${interface} ]; then
  570. rm -f /etc/localtime
  571. contents="$(< ${SAVEDIR}/localtime.predhclient.${interface})"
  572. echo "${contents}" > /etc/localtime
  573. rm -f ${SAVEDIR}/localtime.predhclient.${interface}
  574. touch /etc/localtime
  575. fix_context /etc/localtime
  576. fi
  577. fi
  578. fi
  579. # execute any additional client side configuration scripts we have
  580. if [ -d ${ETCDIR}/dhclient.d ]; then
  581. for f in ${ETCDIR}/dhclient.d/*.sh ; do
  582. if [ -x ${f} ]; then
  583. subsystem="${f%.sh}"
  584. subsystem="${subsystem##*/}"
  585. . ${f}
  586. "${subsystem}_restore"
  587. fi
  588. done
  589. fi
  590. if [ -x ${ETCDIR}/dhclient-${interface}-down-hooks ]; then
  591. . ${ETCDIR}/dhclient-${interface}-down-hooks
  592. elif [ -x ${ETCDIR}/dhclient-down-hooks ]; then
  593. . ${ETCDIR}/dhclient-down-hooks
  594. fi
  595. if [ -n "${alias_ip_address}" ]; then
  596. # Turn off alias interface
  597. ip link set ${interface}:0 down
  598. fi
  599. if [ -n "${old_ip_address}" ]; then
  600. # Shut down interface, which will delete routes and clear arp cache.
  601. ip -4 addr flush dev ${interface} >/dev/null 2>&1
  602. ip link set ${interface} down
  603. fi
  604. if [ -n "${alias_ip_address}" ]; then
  605. ip -4 addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
  606. ip -4 route replace ${alias_ip_address}/32 ${interface}:0
  607. fi
  608. exit_with_hooks 0
  609. ;;
  610. TIMEOUT)
  611. if [ -n "${new_routers}" ]; then
  612. if [ -n "${alias_ip_address}" ]; then
  613. ip -4 addr flush dev ${interface}:0 >/dev/null 2>&1
  614. fi
  615. ip -4 addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
  616. set ${new_routers}
  617. if ping -q -c 1 -w 10 -I ${interface} ${1}; then
  618. dhconfig
  619. exit_with_hooks 0
  620. fi
  621. ip -4 addr flush dev ${interface} >/dev/null 2>&1
  622. ip link set ${interface} down
  623. exit_with_hooks 1
  624. else
  625. exit_with_hooks 1
  626. fi
  627. ;;
  628. *)
  629. logmessage "unhandled state: ${reason}"
  630. exit_with_hooks 1
  631. ;;
  632. esac
  633. exit_with_hooks 0