]>
Commit | Line | Data |
---|---|---|
893aa178 JR |
1 | --- dhcp-3.1.0a3/client/scripts/linux.orig 2006-07-22 04:24:16.000000000 +0200 |
2 | +++ dhcp-3.1.0a3/client/scripts/linux 2007-04-16 17:25:06.000000000 +0200 | |
e8fa690e | 3 | @@ -1,208 +1,227 @@ |
893aa178 JR |
4 | -#!/bin/bash |
5 | +#!/bin/sh | |
6 | # dhclient-script for Linux. Dan Halbert, March, 1997. | |
7 | # Updated for Linux 2.[12] by Brian J. Murrell, January 1999. | |
8 | -# No guarantees about this. I'm a novice at the details of Linux | |
9 | -# networking. | |
10 | ||
11 | # Notes: | |
12 | ||
13 | # 0. This script is based on the netbsd script supplied with dhcp-970306. | |
14 | ||
15 | -# 1. ifconfig down apparently deletes all relevant routes and flushes | |
16 | -# the arp cache, so this doesn't need to be done explicitly. | |
17 | +# 1. This script was modified to work with iproute2 | |
18 | ||
19 | -# 2. The alias address handling here has not been tested AT ALL. | |
20 | -# I'm just going by the doc of modern Linux ip aliasing, which uses | |
21 | -# notations like eth0:0, eth0:1, for each alias. | |
22 | - | |
23 | -# 3. I have to calculate the network address, and calculate the broadcast | |
24 | -# address if it is not supplied. This might be much more easily done | |
25 | -# by the dhclient C code, and passed on. | |
26 | - | |
27 | -# 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious | |
28 | -# of the $1 in its args. | |
29 | +calc_prefix() | |
30 | +{ | |
31 | + local old_IFS=$IFS | |
32 | + IFS='.' | |
33 | + local netmask=$(echo $1) | |
34 | + IFS=$old_IFS | |
35 | + | |
36 | + local ret=0 | |
37 | + local endp=0 | |
893aa178 JR |
38 | + for n in $netmask ; do |
39 | + for i in 128 64 32 16 8 4 2 1 ; do | |
40 | + if [ $(($n & $i)) -ne 0 ]; then | |
41 | + if [ $endp -eq 0 ]; then | |
42 | + ret=$(($ret + 1)) | |
43 | + else | |
44 | + echo "32" | |
45 | + return | |
46 | + fi | |
47 | + else | |
48 | + endp=1 | |
49 | + fi | |
50 | + done | |
51 | + done | |
52 | + echo $ret | |
53 | +} | |
54 | ||
55 | make_resolv_conf() { | |
56 | - if [ x"$new_domain_name_servers" != x ]; then | |
57 | - cat /dev/null > /etc/resolv.conf.dhclient | |
58 | - chmod 644 /etc/resolv.conf.dhclient | |
59 | - if [ x"$new_domain_search" != x ]; then | |
60 | - echo search $new_domain_search >> /etc/resolv.conf.dhclient | |
61 | - elif [ x"$new_domain_name" != x ]; then | |
62 | - # Note that the DHCP 'Domain Name Option' is really just a domain | |
63 | - # name, and that this practice of using the domain name option as | |
64 | - # a search path is both nonstandard and deprecated. | |
65 | - echo search $new_domain_name >> /etc/resolv.conf.dhclient | |
66 | - fi | |
67 | - for nameserver in $new_domain_name_servers; do | |
68 | - echo nameserver $nameserver >>/etc/resolv.conf.dhclient | |
69 | - done | |
70 | - | |
71 | - mv /etc/resolv.conf.dhclient /etc/resolv.conf | |
72 | - fi | |
73 | + if [ -n "$new_domain_name_servers" ]; then | |
74 | + :> /etc/resolv.conf.dhclient | |
75 | + chmod 644 /etc/resolv.conf.dhclient | |
76 | + if [ -n "$new_domain_search" ]; then | |
77 | + echo search $new_domain_search >> /etc/resolv.conf.dhclient | |
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 >> /etc/resolv.conf.dhclient | |
83 | + fi | |
84 | + for nameserver in $new_domain_name_servers; do | |
85 | + echo nameserver $nameserver >>/etc/resolv.conf.dhclient | |
86 | + done | |
87 | + mv /etc/resolv.conf.dhclient /etc/resolv.conf | |
88 | + fi | |
89 | } | |
90 | ||
91 | -# Must be used on exit. Invokes the local dhcp client exit hooks, if any. | |
92 | +# Must be used on exit. | |
93 | +# Invokes the local dhcp client exit hooks, if any. | |
94 | exit_with_hooks() { | |
95 | - exit_status=$1 | |
96 | - if [ -f /etc/dhclient-exit-hooks ]; then | |
97 | - . /etc/dhclient-exit-hooks | |
98 | - fi | |
99 | -# probably should do something with exit status of the local script | |
100 | - exit $exit_status | |
101 | + exit_status=$1 | |
102 | + if [ -f /etc/dhclient-exit-hooks ]; then | |
103 | + . /etc/dhclient-exit-hooks | |
104 | + fi | |
105 | + # probably should do something with exit status of the local script | |
106 | + exit $exit_status | |
107 | } | |
108 | ||
109 | # Invoke the local dhcp client enter hooks, if they exist. | |
110 | if [ -f /etc/dhclient-enter-hooks ]; then | |
111 | - exit_status=0 | |
112 | - . /etc/dhclient-enter-hooks | |
113 | - # allow the local script to abort processing of this state | |
114 | - # local script must set exit_status variable to nonzero. | |
115 | - if [ $exit_status -ne 0 ]; then | |
116 | - exit $exit_status | |
117 | - fi | |
118 | -fi | |
119 | - | |
120 | -release=`uname -r` | |
121 | -release=`expr $release : '\(.*\)\..*'` | |
122 | -relminor=`echo $release |sed -e 's/[0-9]*\.\([0-9][0-9]*\)\(\..*\)*$/\1/'` | |
123 | -relmajor=`echo $release |sed -e 's/\([0-9][0-9]*\)\..*$/\1/'` | |
124 | - | |
125 | -if [ x$new_broadcast_address != x ]; then | |
126 | - new_broadcast_arg="broadcast $new_broadcast_address" | |
127 | -fi | |
128 | -if [ x$old_broadcast_address != x ]; then | |
129 | - old_broadcast_arg="broadcast $old_broadcast_address" | |
130 | -fi | |
131 | -if [ x$new_subnet_mask != x ]; then | |
132 | - new_subnet_arg="netmask $new_subnet_mask" | |
133 | -fi | |
134 | -if [ x$old_subnet_mask != x ]; then | |
135 | - old_subnet_arg="netmask $old_subnet_mask" | |
136 | -fi | |
137 | -if [ x$alias_subnet_mask != x ]; then | |
138 | - alias_subnet_arg="netmask $alias_subnet_mask" | |
139 | -fi | |
140 | - | |
141 | -if [ x$reason = xMEDIUM ]; then | |
142 | - # Linux doesn't do mediums (ok, ok, media). | |
143 | - exit_with_hooks 0 | |
144 | -fi | |
145 | - | |
146 | -if [ x$reason = xPREINIT ]; then | |
147 | - if [ x$alias_ip_address != x ]; then | |
148 | - # Bring down alias interface. Its routes will disappear too. | |
149 | - ifconfig $interface:0- inet 0 | |
150 | - fi | |
151 | - if [ $relmajor -lt 2 ] || ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ) | |
152 | - then | |
153 | - ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \ | |
154 | - broadcast 255.255.255.255 up | |
155 | - # Add route to make broadcast work. Do not omit netmask. | |
156 | - route add default dev $interface netmask 0.0.0.0 | |
157 | - else | |
158 | - ifconfig $interface 0 up | |
159 | - fi | |
160 | - | |
161 | - # We need to give the kernel some time to get the interface up. | |
162 | - sleep 1 | |
163 | - | |
164 | - exit_with_hooks 0 | |
165 | -fi | |
166 | - | |
167 | -if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then | |
168 | - exit_with_hooks 0 | |
169 | -fi | |
170 | - | |
171 | -if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \ | |
172 | - [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then | |
173 | - current_hostname=`hostname` | |
174 | - if [ x$current_hostname = x ] || \ | |
175 | - [ x$current_hostname = x$old_host_name ]; then | |
176 | - if [ x$current_hostname = x ] || \ | |
177 | - [ x$new_host_name != x$old_host_name ]; then | |
178 | - hostname $new_host_name | |
179 | - fi | |
180 | - fi | |
181 | - | |
182 | - if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \ | |
183 | - [ x$alias_ip_address != x$old_ip_address ]; then | |
184 | - # Possible new alias. Remove old alias. | |
185 | - ifconfig $interface:0- inet 0 | |
186 | - fi | |
187 | - if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then | |
188 | - # IP address changed. Bringing down the interface will delete all routes, | |
189 | - # and clear the ARP cache. | |
190 | - ifconfig $interface inet 0 down | |
191 | - | |
192 | - fi | |
193 | - if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \ | |
194 | - [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then | |
195 | - | |
196 | - ifconfig $interface inet $new_ip_address $new_subnet_arg \ | |
197 | - $new_broadcast_arg | |
198 | - # Add a network route to the computed network address. | |
199 | - if [ $relmajor -lt 2 ] || \ | |
200 | - ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then | |
201 | - route add -net $new_network_number $new_subnet_arg dev $interface | |
202 | - fi | |
203 | - for router in $new_routers; do | |
204 | - route add default gw $router | |
205 | - done | |
206 | - fi | |
207 | - if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ]; | |
208 | - then | |
209 | - ifconfig $interface:0- inet 0 | |
210 | - ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg | |
211 | - route add -host $alias_ip_address $interface:0 | |
212 | - fi | |
213 | - make_resolv_conf | |
214 | - exit_with_hooks 0 | |
215 | -fi | |
216 | - | |
217 | -if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \ | |
218 | - || [ x$reason = xSTOP ]; then | |
219 | - if [ x$alias_ip_address != x ]; then | |
220 | - # Turn off alias interface. | |
221 | - ifconfig $interface:0- inet 0 | |
222 | - fi | |
223 | - if [ x$old_ip_address != x ]; then | |
224 | - # Shut down interface, which will delete routes and clear arp cache. | |
225 | - ifconfig $interface inet 0 down | |
226 | - fi | |
227 | - if [ x$alias_ip_address != x ]; then | |
228 | - ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg | |
229 | - route add -host $alias_ip_address $interface:0 | |
230 | - fi | |
231 | - exit_with_hooks 0 | |
232 | -fi | |
233 | - | |
234 | -if [ x$reason = xTIMEOUT ]; then | |
235 | - if [ x$alias_ip_address != x ]; then | |
236 | - ifconfig $interface:0- inet 0 | |
237 | - fi | |
238 | - ifconfig $interface inet $new_ip_address $new_subnet_arg \ | |
239 | - $new_broadcast_arg | |
240 | - set $new_routers | |
241 | - ############## what is -w in ping? | |
242 | - if ping -q -c 1 $1; then | |
243 | - if [ x$new_ip_address != x$alias_ip_address ] && \ | |
244 | - [ x$alias_ip_address != x ]; then | |
245 | - ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg | |
246 | - route add -host $alias_ip_address dev $interface:0 | |
247 | - fi | |
248 | - if [ $relmajor -lt 2 ] || \ | |
249 | - ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then | |
250 | - route add -net $new_network_number | |
251 | - fi | |
252 | - for router in $new_routers; do | |
253 | - route add default gw $router | |
254 | - done | |
255 | - make_resolv_conf | |
256 | - exit_with_hooks 0 | |
257 | - fi | |
258 | - ifconfig $interface inet 0 down | |
259 | - exit_with_hooks 1 | |
260 | -fi | |
261 | + exit_status=0 | |
262 | + . /etc/dhclient-enter-hooks | |
263 | + # allow the local script to abort processing of this state | |
264 | + # local script must set exit_status variable to nonzero. | |
265 | + if [ $exit_status -ne 0 ]; then | |
266 | + exit $exit_status | |
267 | + fi | |
268 | +fi | |
269 | + | |
270 | +if [ -n "$new_broadcast_address" ]; then | |
271 | + new_broadcast_arg="broadcast $new_broadcast_address" | |
272 | +fi | |
273 | +if [ -n "$old_broadcast_address" ]; then | |
274 | + old_broadcast_arg="broadcast $old_broadcast_address" | |
275 | +fi | |
276 | +if [ -n "$new_subnet_mask" ]; then | |
277 | + new_subnet_arg="$(calc_prefix $new_subnet_mask)" | |
278 | +fi | |
279 | +if [ -n "$old_subnet_mask" ]; then | |
280 | + old_subnet_arg="$(calc_prefix $old_subnet_mask)" | |
281 | +fi | |
282 | +if [ -n "$alias_subnet_mask" ]; then | |
283 | + alias_subnet_arg="$(calc_prefix $alias_subnet_mask)" | |
284 | +fi | |
285 | +[ -z "new_subnet_arg" ] && $new_subnet_arg="32" | |
286 | +[ -z "old_subnet_arg" ] && $old_subnet_arg="32" | |
287 | +[ -z "alias_subnet_arg" ] && $alias_subnet_arg="32" | |
288 | + | |
289 | +case "$reason" in | |
290 | + MEDIUM) | |
291 | + # Linux doesn't do mediums (ok, ok, media). | |
292 | + ;; | |
293 | + | |
294 | + PREINIT) | |
295 | + if [ -n "$alias_ip_address" ]; then | |
296 | + /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
297 | + fi | |
298 | + /sbin/ip link set $interface up | |
299 | + | |
300 | + # We need to give the kernel some time to get the interface up. | |
301 | + sleep 1 | |
302 | + ;; | |
303 | + | |
304 | + BOUND|RENEW|REBIND|REBOOT) | |
305 | + current_hostname=`hostname` | |
306 | + if [ -z "$current_hostname" ]; then | |
307 | + hostname $new_host_name | |
308 | + elif [ "$current_hostname" = "$old_host_name" -a \ | |
309 | + "$new_host_name" != "$old_host_name" ]; then | |
310 | + hostname $new_host_name | |
311 | + fi | |
312 | + | |
313 | + if [ -n "$old_ip_address" -a -n "$alias_ip_address" -a \ | |
314 | + "$alias_ip_address" != "$old_ip_address" ]; then | |
315 | + # Possible new alias. Remove old alias. | |
316 | + /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
317 | + fi | |
318 | + if [ -n "$old_ip_address" -a "$old_ip_address" != "$new_ip_address" ]; then | |
319 | + # IP address changed. | |
320 | + /sbin/ip addr del $old_ip_address/$old_subnet_arg dev $interface | |
321 | + for router in $old_routers; do | |
322 | + /sbin/ip route del default via $router | |
323 | + done | |
324 | + if [ -n "$old_static_routes" ]; then | |
325 | + set -- $old_static_routes | |
326 | + while [ $# -gt 1 ]; do | |
327 | + /sbin/ip route del $1 via $2 | |
328 | + shift; shift | |
329 | + done | |
330 | + fi | |
331 | + fi | |
332 | + if [ -z "$old_ip_address" -o "$old_ip_address" != "$new_ip_address" -o \ | |
333 | + "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then | |
334 | + /sbin/ip addr add $new_ip_address/$new_subnet_arg $new_broadcast_arg dev $interface | |
335 | + # Add a network route to the computed network address. | |
336 | + for router in $new_routers; do | |
337 | + /sbin/ip route add default via $router | |
338 | + done | |
339 | + if [ -n "$new_static_routes" ]; then | |
340 | + set -- $new_static_routes | |
341 | + while [ $# -gt 1 ]; do | |
342 | + /sbin/ip route add $1 via $2 | |
343 | + shift; shift | |
344 | + done | |
345 | + fi | |
346 | + fi | |
347 | + if [ -n "$alias_ip_address" -a "$new_ip_address" != "$alias_ip_address" ]; then | |
348 | + /sbin/ip addr add $alias_ip_address/$alias_subnet_arg dev $interface | |
349 | + fi | |
350 | + make_resolv_conf | |
351 | + ;; | |
352 | + | |
353 | + EXPIRE|FAIL|RELEASE|STOP) | |
354 | + if [ -n "$alias_ip_address" ]; then | |
355 | + # Turn off alias interface. | |
356 | + /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
357 | + fi | |
358 | + if [ -n "$old_ip_address" ]; then | |
359 | + /sbin/ip addr del $old_ip_address/$old_subnet_arg dev $interface | |
360 | + for router in $old_routers; do | |
361 | + /sbin/ip route del default via $router | |
362 | + done | |
363 | + if [ -n "$old_static_routes" ]; then | |
364 | + set -- $old_static_routes | |
365 | + while [ $# -gt 1 ]; do | |
366 | + /sbin/ip route del $1 via $2 | |
367 | + shift; shift | |
368 | + done | |
369 | + fi | |
370 | + fi | |
371 | + if [ -n "$alias_ip_address" ]; then | |
372 | + /sbin/ip addr add $alias_ip_address/$alias_subnet_arg dev $interface | |
373 | + fi | |
374 | + ;; | |
375 | + | |
376 | + TIMEOUT) | |
377 | + if [ -n "$alias_ip_address" ]; then | |
378 | + /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
379 | + fi | |
380 | + /sbin/ip addr add $new_ip_address/$new_subnet_arg $new_broadcast_arg dev $interface | |
381 | + | |
382 | + set $new_routers | |
383 | + if ping -q -c 1 $1; then | |
384 | + if [ "$new_ip_address" != "$alias_ip_address" ] && \ | |
385 | + [ -n "$alias_ip_address" ]; then | |
386 | + /sbin/ip addr add $alias_ip_address/$alias_subnet_arg dev $interface | |
387 | + fi | |
388 | + for router in $new_routers; do | |
389 | + /sbin/ip route add default via $router | |
390 | + done | |
391 | + if [ -n "$new_static_routes" ]; then | |
392 | + set -- $new_static_routes | |
393 | + while [ $# -gt 1 ]; do | |
394 | + /sbin/ip route add $1 via $2 | |
395 | + shift; shift | |
396 | + done | |
397 | + fi | |
398 | + make_resolv_conf | |
399 | + exit_with_hooks 0 | |
400 | + fi | |
401 | + | |
402 | + /sbin/ip addr del $new_ip_address/$new_subnet_arg dev $interface | |
403 | + for router in $old_routers; do | |
404 | + /sbin/ip route del default via $router | |
405 | + done | |
406 | + if [ -n "$old_static_routes" ]; then | |
407 | + set -- $old_static_routes | |
408 | + while [ $# -gt 1 ]; do | |
409 | + /sbin/ip route del $1 via $2 | |
410 | + shift; shift | |
411 | + done | |
412 | + fi | |
413 | + exit_with_hooks 1 | |
414 | + ;; | |
415 | + *) | |
416 | + ;; | |
417 | +esac | |
418 | ||
419 | exit_with_hooks 0 |