3 # dhclient-script for Linux.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version
8 # 2 of the License, or (at your option) any later version.
10 # Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 # Probably, I did not understand, what this funny feature as "alias"
13 # means exactly. For now I suppose, that it is a static address, which
14 # we should install and preserve.
17 exec >> /tmp/DHS.log 2>&1
19 echo dhc-script $* reason=$reason
20 set | grep "^\(old_\|new_\|check_\)"
26 # convert 8bit mask to length
32 while [ $l -le 7 ]; do
33 if [ $[ ( 1 << $l ) + $1 ] -eq 256 ]; then
41 # convert inet dotted quad mask to length
42 # arg: $1 = dotquad mask
59 mask8=${mask8#255.255.}
64 mask8=${mask8#255.255.255.}
71 return $[ $? + $masklen ]
74 # calculate ABC "natural" mask
75 # arg: $1 = dotquad address
82 if [ "$1" = "255.255.255.255" ]; then
84 elif [ "$1" = "0.0.0.0" ]; then
86 elif [ $class -ge 224 ]; then
88 elif [ $class -ge 192 ]; then
90 elif [ $class -ge 128 ]; then
97 # calculate ABC "natural" mask length
98 # arg: $1 = dotquad address
105 if [ "$1" = "255.255.255.255" ]; then
107 elif [ "$1" = "0.0.0.0" ]; then
109 elif [ $class -ge 224 ]; then
111 elif [ $class -ge 192 ]; then
113 elif [ $class -ge 128 ]; then
121 # args: $1 = interface
136 LOG ifconfig $addrid down
137 ifconfig $addrid down
141 # args: $1 = interface
158 mask_arg="netmask $3"
161 brd_arg="broadcast $4"
164 LOG ifconfig $addrid $2 $mask_arg $brd_arg up
165 ifconfig $addrid $2 $mask_arg $brd_arg up
169 # args: $1 = routers list
175 LOG AddDefaultRoutes $*
177 LOG route add default gw $router
178 route add default gw $router
183 # Delete default routes
184 # args: $1 = routers list
190 LOG DelDefaultRoutes $*
193 LOG route del default gw $router
194 route del default gw $router
200 # args: $1 = dotquad address of the host
204 if ping -q -c 1 -w 2 $1 ; then
210 # Check (and add route, if alive) default routers
211 # args: $1 = routers list
212 # returns: 0 if at least one router is alive.
218 LOG CheckRouterList $*
221 if PingNode $router ; then
223 route add default gw $router
229 # Delete/create static routes.
230 # args: $1 = operation (del/add)
231 # $2 = routes list in format "dst1 nexthop1 dst2 ..."
233 # BEWARE: this feature of DHCP is obsolete, because does not
234 # support subnetting.
236 X-StaticRouteList() {
241 LOG X-StaticRouteList $*
245 while [ $# -gt 1 ]; do
246 route $op -net $1 netmask `ABCMask "$1"` gw $2
252 # Create static routes.
253 # arg: $1 = routes list in format "dst1 nexthop1 dst2 ..."
255 AddStaticRouteList() {
256 LOG AddStaticRouteList $*
257 X-StaticRouteList add "$1"
260 # Delete static routes.
261 # arg: $1 = routes list in format "dst1 nexthop1 dst2 ..."
263 DelStaticRouteList() {
264 LOG DelStaticRouteList $*
265 X-StaticRouteList del "$1"
268 # Broadcast unsolicited ARP to update neighbours' caches.
269 # args: $1 = interface
273 if [ -f /sbin/arping ]; then
274 /sbin/arping -A -c 1 -I "$1" "$2" &
275 (sleep 2 ; /sbin/arping -U -c 1 -I "$1" "$2" ) &
279 # Duplicate address detection.
280 # args: $1 = interface
282 # returns: 0, if DAD succeeded.
284 if [ -f /sbin/arping ]; then
285 /sbin/arping -c 2 -w 3 -D -I "$1" "$2"
294 # domain and nameserver list are passed in global variables.
296 # NOTE: we try to be careful and not to break user supplied resolv.conf.
297 # The script mangles it, only if it has dhcp magic signature.
301 local idstring="#### Generated by DHCPCD"
305 if [ "$new_domain_name" = "" -a "$new_domain_name_servers" = "" ]; then
309 echo $idstring > /etc/resolv.conf.dhcp
310 if [ "$new_domain_name" ]; then
311 echo search $new_domain_name >> /etc/resolv.conf.dhcp
313 echo options ndots:1 >> /etc/resolv.conf.dhcp
315 if [ "$new_domain_name_servers" ]; then
316 for nameserver in $new_domain_name_servers; do
317 echo nameserver $nameserver >> /etc/resolv.conf.dhcp
320 echo nameserver 127.0.0.1 >> /etc/resolv.conf.dhcp
323 if [ -f /etc/resolv.conf ]; then
324 if [ "`head -1 /etc/resolv.conf`" != "$idstring" ]; then
327 if [ "$old_domain_name" = "$new_domain_name" -a
328 "$new_domain_name_servers" = "$old_domain_name_servers" ]; then
332 mv /etc/resolv.conf.dhcp /etc/resolv.conf
345 ifconfig $interface:dhcp down
346 ifconfig $interface:dhcp1 down
347 if [ -d /proc/sys/net/ipv4/conf/$interface ]; then
348 ifconfig $interface:dhcp 10.10.10.10 netmask 255.255.255.255
349 ifconfig $interface:dhcp down
350 if [ -d /proc/sys/net/ipv4/conf/$interface ]; then
351 LOG The interface $interface already configured.
354 ifconfig $interface:dhcp up
363 if DAD "$interface" "$check_ip_address" ; then
369 BOUND|RENEW|REBIND|REBOOT)
370 if [ "$old_ip_address" -a "$alias_ip_address" -a \
371 "$alias_ip_address" != "$old_ip_address" ]; then
372 DelINETAddr "$interface" "$alias_ip_address" "$alias_subnet_mask" "$alias_broadcast_address" dhcp1
374 if [ "$old_ip_address" -a "$old_ip_address" != "$new_ip_address" ]; then
375 DelINETAddr "$interface" "$old_ip_address" "$old_subnet_mask" "$old_broadcast_address" dhcp
376 DelDefaultRoutes "$old_routers"
377 DelStaticRouteList "$old_static_routes"
379 if [ "$old_ip_address" = "" -o "$old_ip_address" != "$new_ip_address" -o \
380 "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then
381 AddINETAddr "$interface" "$new_ip_address" "$new_subnet_mask" "$new_broadcast_address" dhcp
382 AddStaticRouteList "$new_static_routes"
383 AddDefaultRoutes "$new_routers"
384 UnsolicitedARP "$interface" "$new_ip_address"
386 if [ "$new_ip_address" != "$alias_ip_address" -a "$alias_ip_address" ]; then
387 AddINETAddr "$interface" "$alias_ip_address" "$alias_subnet_mask" "$alias_broadcast_address" dhcp1
394 if [ "$alias_ip_address" ]; then
395 DelINETAddr "$interface" "$alias_ip_address" "$alias_subnet_mask" "$alias_broadcast_address" dhcp1
397 if [ "$old_ip_address" ]; then
398 DelINETAddr "$interface" "$old_ip_address" "$old_subnet_mask" "$old_broadcast_address" dhcp
399 DelDefaultRoutes "$old_routers"
400 DelStaticRouteList "$old_static_routes"
402 if [ "$alias_ip_address" ]; then
403 AddINETAddr "$interface" "$alias_ip_address" "$alias_subnet_mask" "$alias_broadcast_address" dhcp1
409 if [ "$alias_ip_address" ]; then
410 DelINETAddr "$interface" "$alias_ip_address" "$alias_subnet_mask" "$alias_broadcast_address" dhcp1
412 # Seems, <null address> means, that no more old leases found.
413 # Or does it mean bug in dhcpcd? 8) Fail for now.
414 if [ "$new_ip_address" = "<null address>" ]; then
415 if [ "$old_ip_address" ]; then
416 DelINETAddr "$interface" "$old_ip_address" "$old_subnet_mask" "$old_broadcast_address" dhcp
418 if [ "$alias_ip_address" ]; then
419 AddINETAddr "$interface" "$alias_ip_address" "$alias_subnet_mask" "$alias_broadcast_address" dhcp1
423 if DAD "$interface" "$new_ip_address" ; then
424 AddINETAddr "$interface" "$new_ip_address" "$new_subnet_mask" "$new_broadcast_address" dhcp
425 UnsolicitedARP "$interface" "$new_ip_address"
426 if [ "$alias_ip_address" -a "$alias_ip_address" != "$new_ip_address" ]; then
427 AddINETAddr "$interface" "$alias_ip_address" "$alias_subnet_mask" "$alias_broadcast_address" dhcp1
428 UnsolicitedARP "$interface" "$alias_ip_address"
430 if CheckRouterList "$new_routers" ; then
431 AddStaticRouteList "$new_static_routes"
436 DelINETAddr "$interface" "$new_ip_address" "$new_subnet_mask" "$new_broadcast_address" dhcp
437 DelDefaultRoutes "$old_routers"
438 DelStaticRouteList "$old_static_routes"
439 if [ "$alias_ip_address" ]; then
440 AddINETAddr "$interface" "$alias_ip_address" "$alias_subnet_mask" "$alias_broadcast_address" dhcp1