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