3 # $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
6 # Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
8 # Permission to use, copy, modify, and distribute this software for any
9 # purpose with or without fee is hereby granted, provided that the above
10 # copyright notice and this permission notice appear in all copies.
12 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 HOSTNAME=/bin/hostname
24 IFCONFIG='/sbin/ifconfig -n'
28 if [ -x /usr/bin/logger ]; then
29 LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
35 # Helper functions that implement common actions.
39 current_hostname=`$HOSTNAME`
40 if [ -z "$current_hostname" ]; then
41 $LOGGER "New Hostname ($interface): $new_host_name"
42 $HOSTNAME $new_host_name
43 elif [ "$current_hostname" = "$old_host_name" -a \
44 "$new_host_name" != "$old_host_name" ]; then
45 $LOGGER "New Hostname ($interface): $new_host_name"
46 $HOSTNAME $new_host_name
51 arp -an -i $interface | \
52 sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
56 delete_old_address() {
57 eval "$IFCONFIG $interface inet -alias $old_ip_address $medium"
61 eval "$IFCONFIG $interface \
62 inet $new_ip_address \
63 netmask $new_subnet_mask \
64 broadcast $new_broadcast_address \
67 $LOGGER "New IP Address ($interface): $new_ip_address"
68 $LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
69 $LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
70 $LOGGER "New Routers ($interface): $new_routers"
74 if [ -n "$alias_ip_address" ]; then
75 $IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
76 #route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
81 if [ -n "$alias_ip_address" ]; then
82 $IFCONFIG $interface inet alias $alias_ip_address netmask \
84 #route add $alias_ip_address $LOCALHOST
88 fill_classless_routes() {
90 while [ $# -ge 5 ]; do
93 elif [ $1 -le 8 ]; then
96 elif [ $1 -le 16 ]; then
99 elif [ $1 -le 24 ]; then
100 route="$2.$3.$4.0/$1"
103 route="$2.$3.$4.$5/$1"
104 shift; shift; shift; shift
108 classless_routes="$classless_routes $route $router"
109 shift; shift; shift; shift
113 delete_old_routes() {
114 #route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
115 if [ -n "$old_classless_routes" ]; then
116 fill_classless_routes "$old_classless_routes"
117 set $classless_routes
118 while [ $# -gt 1 ]; do
119 route delete "$1" "$2"
125 # If we supported multiple default routes, we'd be removing each
126 # one here. We don't so just delete the default route if it's
127 # through our interface.
128 if is_default_interface; then
129 route delete default >/dev/null 2>&1
132 if [ -n "$old_static_routes" ]; then
133 set $old_static_routes
134 while [ $# -gt 1 ]; do
135 route delete "$1" "$2"
144 #route add $new_ip_address $LOCALHOST >/dev/null 2>&1
146 # RFC 3442: If the DHCP server returns both a Classless Static
147 # Routes option and a Router option, the DHCP client MUST ignore
150 # DHCP clients that support this option (Classless Static Routes)
151 # MUST NOT install the routes specified in the Static Routes
152 # option (option code 33) if both a Static Routes option and the
153 # Classless Static Routes option are provided.
155 if [ -n "$new_classless_routes" ]; then
156 fill_classless_routes "$new_classless_routes"
157 $LOGGER "New Classless Static Routes ($interface): $classless_routes"
158 set $classless_routes
159 while [ $# -gt 1 ]; do
160 if [ "0.0.0.0" = "$2" ]; then
161 route add "$1" -iface "$interface"
170 for router in $new_routers; do
171 if is_default_interface; then
173 if [ "$new_ip_address" = "$router" ]; then
174 route add default -iface $router >/dev/null 2>&1
176 route add default $router >/dev/null 2>&1
179 # 2nd and subsequent default routers error out, so explicitly
180 # stop processing the list after the first one.
184 if [ -n "$new_static_routes" ]; then
185 $LOGGER "New Static Routes ($interface): $new_static_routes"
186 set $new_static_routes
187 while [ $# -gt 1 ]; do
194 add_new_resolv_conf() {
195 # XXX Old code did not create/update resolv.conf unless both
196 # $new_domain_name and $new_domain_name_servers were provided. PR
197 # #3135 reported some ISP's only provide $new_domain_name_servers and
198 # thus broke the script. This code creates the resolv.conf if either
201 local tmpres=/var/run/resolv.conf.${interface}
204 if [ -n "$new_domain_search" ]; then
205 echo "search $new_domain_search" >>$tmpres
206 elif [ -n "$new_domain_name" ]; then
207 echo "search $new_domain_name" >>$tmpres
210 if [ -n "$new_domain_name_servers" ]; then
211 for nameserver in $new_domain_name_servers; do
212 echo "nameserver $nameserver" >>$tmpres
216 if [ -f $tmpres ]; then
217 if [ -f /etc/resolv.conf.tail ]; then
218 cat /etc/resolv.conf.tail >>$tmpres
221 case $resolvconf_enable in
222 # "no", "false", "off", or "0"
223 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
224 # When resolv.conf is not changed actually, we don't
226 # If /usr is not mounted yet, we cannot use cmp, then
227 # the following test fails. In such case, we simply
228 # ignore an error and do update resolv.conf.
229 if cmp -s $tmpres /etc/resolv.conf; then
234 # In case (e.g. during OpenBSD installs)
235 # /etc/resolv.conf is a symbolic link, take
236 # care to preserve the link and write the new
237 # data in the correct location.
239 if [ -f /etc/resolv.conf ]; then
240 cat /etc/resolv.conf > /etc/resolv.conf.save
242 cat $tmpres > /etc/resolv.conf
244 # Try to ensure correct ownership and permissions.
245 chown -RL root:wheel /etc/resolv.conf
246 chmod -RL 644 /etc/resolv.conf
250 /sbin/resolvconf -a ${interface} < $tmpres
262 # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
265 if [ -f /etc/dhclient-exit-hooks ]; then
266 . /etc/dhclient-exit-hooks
268 # probably should do something with exit status of the local script
272 # Get the interface with the current ipv4 default route on it using only
273 # commands that are available prior to /usr being mounted.
274 is_default_interface()
276 routeget="`route -n get -inet default`"
281 for line in $routeget ; do
290 if [ -z "$defif" -o "$defif" = "$interface" ]; then
298 # Start of active code.
301 # Invoke the local dhcp client enter hooks, if they exist.
302 if [ -f /etc/dhclient-enter-hooks ]; then
304 . /etc/dhclient-enter-hooks
305 # allow the local script to abort processing of this state
306 # local script must set exit_status variable to nonzero.
307 if [ $exit_status -ne 0 ]; then
312 : ${resolvconf_enable="YES"}
316 eval "$IFCONFIG $interface $medium"
317 eval "$IFCONFIG $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
323 $IFCONFIG $interface inet alias 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
329 BOUND|RENEW|REBIND|REBOOT)
331 if [ -n "$old_ip_address" ]; then
332 if [ "$old_ip_address" != "$alias_ip_address" ]; then
335 if [ "$old_ip_address" != "$new_ip_address" ]; then
340 if [ "$reason" = BOUND ] || \
341 [ "$reason" = REBOOT ] || \
342 [ -z "$old_ip_address" ] || \
343 [ "$old_ip_address" != "$new_ip_address" ]; then
347 if [ "$new_ip_address" != "$alias_ip_address" ]; then
350 if is_default_interface; then
357 if [ -n "$old_ip_address" ]; then
362 $ARP -d -a -i $interface
364 # XXX Why add alias we just deleted above?
366 if is_default_interface; then
367 case $resolvconf_enable in
368 # "no", "false", "off", or "0"
369 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
370 if [ -f /etc/resolv.conf.save ]; then
371 cat /etc/resolv.conf.save > /etc/resolv.conf
375 /sbin/resolvconf -d ${interface}
385 if [ -n "$new_routers" ]; then
386 $LOGGER "New Routers ($interface): $new_routers"
388 if ping -q -c 1 -t 1 "$1"; then
389 if [ "$new_ip_address" != "$alias_ip_address" ]; then
393 if ! is_default_interface; then
396 if add_new_resolv_conf; then
401 eval "$IFCONFIG $interface inet -alias $new_ip_address $medium"