]> git.ipfire.org Git - people/ms/ipfire-3.x.git/blame - dhcp/dhclient-script
Merge remote-tracking branch 'stevee/gettext'
[people/ms/ipfire-3.x.git] / dhcp / dhclient-script
CommitLineData
12573d1f
MT
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 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
32PATH=/bin:/usr/bin:/sbin
33SAVEDIR=/var/lib/dhclient
34
35LOGFACILITY="local7"
36LOGLEVEL="notice"
37
38logmessage() {
39 msg="${1}"
40 logger -p ${LOGFACILITY}.${LOGLEVEL} -t "NET" "dhclient: ${msg}"
41}
42
43save_previous() {
44 origfile="${1}"
45 savefile="${SAVEDIR}/${origfile##*/}.predhclient.${interface}"
46
47 if [ ! -d ${SAVEDIR} ]; then
48 mkdir -p ${SAVEDIR}
49 fi
50
51 if [ -e ${origfile} ]; then
52 mv ${origfile} ${savefile}
53 else
54 echo > ${savefile}
55 fi
12573d1f
MT
56}
57
58make_resolv_conf() {
59 [ "${PEERDNS}" = "no" ] && return
60
61 if [ "${reason}" = "RENEW" ] &&
62 [ "${new_domain_name}" = "${old_domain_name}" ] &&
63 [ "${new_domain_name_servers}" = "${old_domain_name_servers}" ]; then
64 return
65 fi
66
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
74 if [ -n "${SEARCH}" ]; then
75 echo "search ${SEARCH}" >> $rscf
76 else
77 if [ -n "${new_domain_search}" ]; then
78 echo "search ${new_domain_search//\\032/ }" >> ${rscf}
79 elif [ -n "${new_domain_name}" ]; then
80 echo "search ${new_domain_name//\\032/ }" >> ${rscf}
81 fi
82 fi
83
84 if [ -n "${RES_OPTIONS}" ]; then
85 echo "options ${RES_OPTIONS}" >> ${rscf}
86 fi
87
88 for nameserver in ${new_domain_name_servers} ; do
89 echo "nameserver ${nameserver}" >> ${rscf}
90 done
91
92 change_resolv_conf ${rscf}
93 rm -f ${rscf}
12573d1f
MT
94 fi
95}
96
97exit_with_hooks() {
98 exit_status="${1}"
99
100 if [ -x /etc/dhclient-exit-hooks ]; then
101 . /etc/dhclient-exit-hooks
102 fi
103
104 exit ${exit_status}
105}
106
107quad2num() {
108 if [ $# -eq 4 ]; then
109 let n="${1} << 24 | ${2} << 16 | ${3} << 8 | ${4}"
110 echo "${n}"
111 return 0
112 else
113 echo "0"
114 return 1
115 fi
116}
117
118ip2num() {
119 IFS="." quad2num ${1}
120}
121
122num2ip() {
123 let n="${1}"
124 let o1="(n >> 24) & 0xff"
125 let o2="(n >> 16) & 0xff"
126 let o3="(n >> 8) & 0xff"
127 let o4="n & 0xff"
128 echo "${o1}.${o2}.${o3}.${o4}"
129}
130
131mask() {
132 ip="${1}"
133 m="${2}"
134 let ip="$(IFS="." ip2num ${ip})"
135 let m="$(IFS="." ip2num ${m})"
136 let n="ip & m"
137 num2ip ${n}
138}
139
140class_bits() {
141 let ip=$(IFS='.' ip2num $1)
142 let bits=32
143 let mask='255'
144 for ((i=0; i <= 3; i++, 'mask<<=8')); do
145 let v='ip&mask'
146 if [ "$v" -eq 0 ] ; then
147 let bits-=8
148 else
149 break
150 fi
151 done
152 echo $bits
153}
154
155is_router_reachable() {
156 # handle DHCP servers that give us a router not on our subnet
157 router="${1}"
158 routersubnet="$(mask ${router} ${new_subnet_mask})"
159 mysubnet="$(mask ${new_ip_address} ${new_subnet_mask})"
160 unreachable=0
161
162 if [ ! "${routersubnet}" = "${mysubnet}" ]; then
163 unreachable=1
164 if arping -f -q -I ${interface} -w2 ${router}; then
165 ip route add ${router}/32 dev ${interface}
166 if [ $? -eq 0 ]; then
167 unreachable=0
168 else
169 logmessage "failed to create host router for unreachable router ${router} not on subnet ${mysubnet}"
170 fi
171 else
172 unreachable=1
173 logmessage "DHCP router ${router} is unreachable on DHCP subnet ${mysubnet} router subnet ${routersubnet}"
174 fi
175 fi
176
177 return ${unreachable}
178}
179
180add_default_gateway() {
181 router="${1}"
182 metric=""
183
184 if [ $# -gt 1 ] && [ ${2} -gt 0 ]; then
185 metric="metric ${2}"
186 fi
187
188 if is_router_reachable ${router} ; then
189 ip route replace default via ${router} dev ${interface} ${metric}
190 if [ $? -ne 0 ]; then
191 logmessage "failed to create default route: ${router} dev ${interface} ${metric}"
192 return 1
193 else
194 return 0
195 fi
196 fi
197
198 return 1
199}
200
201dhconfig() {
202 if [ -n "${old_ip_address}" ] && [ -n "${alias_ip_address}" ] &&
203 [ ! "${alias_ip_address}" = "${old_ip_address}" ]; then
204 # possible new alias, remove old alias first
205 ip -family inet addr del ${old_ip_address} dev ${interface}:0
206 fi
207
208 if [ -n "${old_ip_address}" ] &&
209 [ ! "${old_ip_address}" = "${new_ip_address}" ]; then
210 # IP address changed. Bringing down the interface will delete all
211 # routes, and clear the ARP cache.
212 ip -family inet addr flush dev ${interface} >/dev/null 2>&1
213 ip -family inet link set dev ${interface} down
214 fi
215
216 if [ "${reason}" = "BOUND" ] || [ "${reason}" = "REBOOT" ] ||
217 [ ! "${old_ip_address}" = "${new_ip_address}" ] ||
218 [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] ||
219 [ ! "${old_network_number}" = "${new_network_number}" ] ||
220 [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] ||
221 [ ! "${old_routers}" = "${new_routers}" ] ||
222 [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
223 ip -family inet addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
224
225 if [ -n "${new_interface_mtu}" ]; then
226 ip link set ${interface} mtu ${new_interface_mtu}
227 fi
228
229 if [ -x /etc/dhclient-${interface}-up-hooks ]; then
230 . /etc/dhclient-${interface}-up-hooks
231 elif [ -x /etc/dhclient-up-hooks ]; then
232 . /etc/dhclient-up-hooks
233 fi
234
235 if [[ (( -z "${GATEWAYDEV}" ) ||
236 ( "${GATEWAYDEV}" = "${interface}" )) &&
237 (( -z "$GATEWAY" ) ||
238 (( -n "$DHCLIENT_IGNORE_GATEWAY" ) &&
239 ( "$DHCLIENT_IGNORE_GATEWAY" = [Yy]* ))) ]]; then
240 metric="${METRIC:-}"
241 let i="${METRIC:-0}"
242 default_routers=()
243
244 for router in ${new_routers} ; do
245 added_router=-
246
247 for r in ${default_routers[@]} ; do
248 if [ "${r}" = "${router}" ]; then
249 added_router=1
250 fi
251 done
252
253 if [ -z "${router}" ] ||
254 [ "${added_router}" = "1" ] ||
255 [ $(IFS=. ip2num ${router}) -le 0 ] ||
256 [[ ( "${router}" = "${new_broadcast_address}" ) &&
257 ( "${new_subnet_mask}" != "255.255.255.255" ) ]]; then
258 continue
259 fi
260
261 default_routers=(${default_routers[@]} ${router})
262 add_default_gateway ${router} ${metric}
263 let i=i+1
264 metric=${i}
265 done
266 elif [[ (( -z "${GATEWAYDEV}" ) ||
267 ( "${GATEWAYDEV}" = "${interface}" )) &&
268 ( -n "${GATEWAY}" ) ]]; then
269 routersubnet=$(mask ${GATEWAY} ${new_subnet_mask})
270 mysubnet=$(mask ${new_ip_address} ${new_subnet_mask})
271
272 if [ "${routersubnet}" = "${mysubnet}" ]; then
273 ip route replace default via ${GATEWAY} dev ${interface}
274 fi
275 fi
276
277 # static routes
278 if [ -n "${new_static_routes}" ]; then
279 IFS=', |' static_routes=(${new_static_routes})
280 route_targets=()
281
282 for((i=0; i<${#static_routes[@]}; i+=2)); do
283 target=${static_routes[$i]}
284 gateway=${static_routes[$i+1]}
285 metric=''
286
287 for t in ${route_targets[@]}; do
288 if [ ${t} = ${target} ]; then
289 if [ -z "${metric}" ]; then
290 metric=1
291 else
292 ((metric=metric+1))
293 fi
294 fi
295 done
296
297 if [ -n "${metric}" ]; then
298 metric="metric ${metric}"
299 fi
300
301 if is_router_reachable ${gateway}; then
302 ip route replace ${target}/$(class_bits ${target}) via ${gateway} dev ${interface} ${metric}
303
304 if [ $? -ne 0 ]; then
305 logmessage "failed to create static route: ${target}/$(class_bits ${target}) via ${gateway} dev ${interface} ${metric}"
306 else
307 route_targets=(${route_targets[@]} ${target})
308 fi
309 fi
310 done
311 fi
312 fi
313
314 if [ ! "${new_ip_address}" = "${alias_ip_address}" ] &&
315 [ -n "${alias_ip_address}" ]; then
316 ip -family inet addr flush dev ${interface}:0 >/dev/null 2>&1
317 ip -family inet addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
318 ip route replace ${alias_ip_address}/32 dev ${interface}:0
319 fi
320
321 make_resolv_conf
322
323 if [ -n "${new_host_name}" ] && need_hostname; then
324 hostname ${new_host_name}
325 fi
326
327 if [ ! "${PEERNIS}" = "no" ]; then
328 if [ -n "${new_nis_domain}" ]; then
329 domainname "${new_nis_domain}"
330 save_previous /etc/yp.conf
331 let contents=0
332 echo '# generated by /sbin/dhclient-script' > /etc/yp.conf
333
334 if [ -n "${new_nis_servers}" ]; then
335 for i in ${new_nis_servers} ; do
336 echo "domain ${new_nis_domain} server ${i}" >> /etc/yp.conf
337 let contents=contents+1
338 done
339 else
340 echo "domain ${new_nis_domain} broadcast" >> /etc/yp.conf
341 let contents=contents+1
342 fi
343
344 if [ ${contents} -gt 0 ]; then
345 if [ -x /etc/rc.d/init.d/ypbind ] &&
346 [ -r /var/run/ypbind.pid ]; then
347 service ypbind restart >/dev/null 2>&1
348 fi
349 fi
350 elif [ -n "${new_nis_servers}" ]; then
351 save_previous /etc/yp.conf
352 echo '# generated by /sbin/dhclient-script' > /etc/yp.conf
353 let contents=0
354
355 for i in ${new_nis_servers} ; do
356 echo "ypserver ${i}" >> /etc/yp.conf
357 let contents=contents+1
358 done
359
360 if [ $contents -gt 0 ]; then
361 if [ -x /etc/rc.d/init.d/ypbind ] &&
362 [ -r /var/run/ypbind.pid ]; then
363 service ypbind restart >/dev/null 2>&1
364 fi
365 fi
366 fi
367 fi
368
369 if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
370 [[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
371 if [ -n "${new_time_offset}" ]; then
372 # DHCP option "time-offset" is requested by default and should be
373 # handled. The geographical zone abbreviation cannot be determined
374 # from the GMT offset, but the $ZONEINFO/Etc/GMT$offset file can be
375 # used - note: this disables DST.
376 ((z=new_time_offset/3600))
377 ((hoursWest=$(printf '%+d' $z)))
378
379 if (( $hoursWest < 0 )); then
380 # tzdata treats negative 'hours west' as positive 'gmtoff'!
381 ((hoursWest*=-1))
382 fi
383
384 tzfile=/usr/share/zoneinfo/Etc/GMT$(printf '%+d' ${hoursWest})
385 if [ -e ${tzfile} ]; then
386 save_previous /etc/localtime
387 cp -fp ${tzfile} /etc/localtime
388 touch /etc/localtime
389 fi
390 fi
391 fi
392
393 if [ ! "${PEERNTP}" = "no" ] &&
394 [ -n "${new_ntp_servers}" ] && [ -e /etc/ntp.conf ]; then
395 save_previous /etc/ntp.conf
396 egrep -v '^server .* # added by /sbin/dhclient-script$'< ${SAVEDIR}/ntp.conf.predhclient.${interface} > /etc/ntp.conf
12573d1f
MT
397
398 for s in ${new_ntp_servers} ; do
399 echo "server ${s} # added by /sbin/dhclient-script" >> /etc/ntp.conf
400 done
401
402 diff -q /etc/ntp.conf ${SAVEDIR}/ntp.conf.predhclient.${interface} >/dev/null 2>&1
403 if [ $? -eq 1 ]; then
404 service ntpd condrestart >/dev/null 2>&1
405 fi
406 fi
407}
408
409get_prefix() {
410 ip="${1}"
411 nm="${2}"
412
413 if [ -n "${ip}" -a -n "${nm}" ]; then
414 ipcalc -s -p ${ip} ${nm} | cut -d '=' -f 2
415 fi
416}
417
418
419#
420# ### MAIN
421#
422
423if [ -x /etc/dhclient-enter-hooks ]; then
424 exit_status=0
425
426 # dhclient-enter-hooks can abort dhclient-script by setting
427 # the exit_status variable to a non-zero value
428 . /etc/dhclient-enter-hooks
429 if [ ${exit_status} -ne 0 ]; then
430 exit ${exit_status}
431 fi
432fi
433
434if [ ! -r /etc/sysconfig/network-scripts/network-functions ]; then
435 echo "Missing /etc/sysconfig/network-scripts/network-functions, exiting." >&2
436 exit 1
437fi
438
439if [ ! -r /etc/rc.d/init.d/functions ]; then
440 echo "Missing /etc/rc.d/init.d/functions, exiting." >&2
441 exit 1
442fi
443
444. /etc/sysconfig/network-scripts/network-functions
445. /etc/rc.d/init.d/functions
446
447if [ -f /etc/sysconfig/network ]; then
448 . /etc/sysconfig/network
449fi
450
451if [ -f /etc/sysconfig/networking/network ]; then
452 . /etc/sysconfig/networking/network
453fi
454
455CONFIG="/etc/sysconfig/network-scripts/ifcfg-${interface}"
456need_config ${CONFIG}
457source_config >/dev/null 2>&1
458
459new_prefix="$(get_prefix ${new_ip_address} ${new_subnet_mask})"
460old_prefix="$(get_prefix ${old_ip_address} ${new_subnet_mask})"
461alias_prefix="$(get_prefix ${alias_ip_address} ${alias_subnet_mask})"
462
463case "${reason}" in
464 MEDIUM)
465 # Linux doesn't handle mediums (media)
466 exit_with_hooks 0
467 ;;
468
469 PREINIT)
470 if [ -n "${alias_ip_address}" ]; then
471 # Bring down alias interface, its routes will disappear too.
472 ip -family inet link set ${interface}:0 down
473 fi
474
475 if [ "${keep_old_ip}" = "yes" ]; then
476 ip -family inet link set ${interface} up
477 else
478 ip -family inet addr flush dev ${interface} >/dev/null 2>&1
479 ip -family inet link set ${interface} up
480 fi
481
482 if [ -n "${DHCLIENT_DELAY}" ] && [ ${DHCLIENT_DELAY} -gt 0 ]; then
483 sleep ${DHCLIENT_DELAY}
484 fi
485
486 exit_with_hooks 0
487 ;;
488
489 ARPCHECK|ARPSEND)
490 if [ -z "${new_ip_address}" ] || [ -z "${interface}" ] ||
491 arping -q -f -c 2 -w 3 -D -I ${interface} ${new_ip_address}; then
492 exit_with_hooks 0
493 else
494 exit_with_hooks 1
495 fi
496 ;;
497
498 BOUND|RENEW|REBIND|REBOOT)
499 dhconfig
500 exit_with_hooks 0
501 ;;
502
503 EXPIRE|FAIL|RELEASE|STOP)
504 # only restore config files if there are no other dhclient processes
505 # running (#306381)
506 any_other_clients="$(ps -eo pid,ppid,comm | grep dhclient | grep -v ${PPID})"
507 if [ -n "${any_other_clients}" ]; then
508 if [ -f ${SAVEDIR}/resolv.conf.predhclient.${interface} ]; then
509 change_resolv_conf ${SAVEDIR}/resolv.conf.predhclient.${interface}
510 rm -f ${SAVEDIR}/resolv.conf.predhclient.${interface}
511 fi
512
513 if [ -n "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" ] &&
514 [[ "${DHCP_TIME_OFFSET_SETS_TIMEZONE}" = [yY1]* ]]; then
515 if [ -e ${SAVEDIR}/localtime.predhclient.${interface} ]; then
516 rm -f /etc/localtime
517 mv ${SAVEDIR}/localtime.predhclient.${interface} /etc/localtime
518 touch /etc/localtime
519 fi
520 fi
521
522 if [ -f ${SAVEDIR}/ntp.conf.predhclient.${interface} ]; then
523 rm -f /etc/ntp.conf
524 mv -f ${SAVEDIR}/ntp.conf.predhclient.${interface} /etc/ntp.conf
525 service ntpd condrestart >/dev/null 2>&1
526 fi
527
528 if [ -f ${SAVEDIR}/yp.conf.predhclient.${interface} ]; then
529 rm -f /etc/yp.conf
530 mv -f ${SAVEDIR}/yp.conf.predhclient.${interface} /etc/yp.conf
531 if [ -x /etc/rc.d/init.d/ypbind ] && [ -r /var/run/ypbind.pid ]; then
532 service ypbind restart >/dev/null 2>&1
533 fi
534 fi
535 fi
536
537 if [ -x /etc/dhclient-${interface}-down-hooks ]; then
538 . /etc/dhclient-${interface}-down-hooks
539 elif [ -x /etc/dhclient-down-hooks ]; then
540 . /etc/dhclient-down-hooks
541 fi
542
543 if [ -n "${alias_ip_address}" ]; then
544 # Turn off alias interface
545 ip -family inet link set ${interface}:0 down
546 fi
547
548 if [ -n "${old_ip_address}" ]; then
549 # Shut down interface, which will delete routes and clear arp cache.
550 ip -family inet addr flush dev ${interface} >/dev/null 2>&1
551 ip -family inet link set ${interface} down
552 fi
553
554 if [ -n "${alias_ip_address}" ]; then
555 ip -family inet addr add ${alias_ip_address}/${alias_prefix} dev ${interface}:0
556 ip -family inet route replace ${alias_ip_address}/32 ${interface}:0
557 fi
558
559 exit_with_hooks 0
560 ;;
561
562 TIMEOUT)
563 if [ -n "${new_routers}" ]; then
564 if [ -n "${alias_ip_address}" ]; then
565 ip -family inet addr flush dev ${interface}:0 >/dev/null 2>&1
566 fi
567
568 ip -family inet addr add ${new_ip_address}/${new_prefix} broadcast ${new_broadcast_address} dev ${interface}
569 set ${new_routers}
570
571 if ping -q -c 1 -w 10 -I ${interface} ${1}; then
572 dhconfig
573 exit_with_hooks 0
574 fi
575
576 ip -family inet addr flush dev ${interface} >/dev/null 2>&1
577 ip -family inet link set ${interface} down
578 exit_with_hooks 1
579 else
580 exit_with_hooks 1
581 fi
582 ;;
583
584 *)
585 logmessage "unhandled state: ${reason}"
586 exit_with_hooks 1
587 ;;
588esac
589
590exit_with_hooks 0