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.
24 HOSTNAME=/bin/hostname
25 IFCONFIG='/sbin/ifconfig -n'
26 NETSTAT=/usr/bin/netstat
30 if [ -x /usr/bin/logger ]; then
31 LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
37 # Helper functions that implement common actions.
41 current_hostname=`$HOSTNAME`
42 if [ -z "$current_hostname" ]; then
43 $LOGGER "New Hostname ($interface): $new_host_name"
44 $HOSTNAME $new_host_name
45 elif [ "$current_hostname" = "$old_host_name" -a \
46 "$new_host_name" != "$old_host_name" ]; then
47 $LOGGER "New Hostname ($interface): $new_host_name"
48 $HOSTNAME $new_host_name
53 arp -an -i $interface | \
54 sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
58 delete_old_address() {
59 eval "$IFCONFIG $interface inet -alias $old_ip_address $medium"
63 eval "$IFCONFIG $interface \
64 inet $new_ip_address \
65 netmask $new_subnet_mask \
66 broadcast $new_broadcast_address \
69 $LOGGER "New IP Address ($interface): $new_ip_address"
70 $LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
71 $LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
72 $LOGGER "New Routers ($interface): $new_routers"
76 if [ -n "$alias_ip_address" ]; then
77 $IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
78 #route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
83 if [ -n "$alias_ip_address" ]; then
84 $IFCONFIG $interface inet alias $alias_ip_address netmask \
86 #route add $alias_ip_address $LOCALHOST
90 fill_classless_routes() {
92 while [ $# -ge 5 ]; do
95 elif [ $1 -le 8 ]; then
98 elif [ $1 -le 16 ]; then
101 elif [ $1 -le 24 ]; then
102 route="$2.$3.$4.0/$1"
105 route="$2.$3.$4.$5/$1"
106 shift; shift; shift; shift
110 classless_routes="$classless_routes $route $router"
111 shift; shift; shift; shift
115 delete_old_routes() {
116 #route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
117 if [ -n "$old_classless_routes" ]; then
118 fill_classless_routes "$old_classless_routes"
119 set $classless_routes
120 while [ $# -gt 1 ]; do
121 route delete "$1" "$2"
127 for router in $old_routers; do
128 if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then
129 route delete default $route >/dev/null 2>&1
133 if [ -n "$old_static_routes" ]; then
134 set $old_static_routes
135 while [ $# -gt 1 ]; do
136 route delete "$1" "$2"
145 #route add $new_ip_address $LOCALHOST >/dev/null 2>&1
147 # RFC 3442: If the DHCP server returns both a Classless Static
148 # Routes option and a Router option, the DHCP client MUST ignore
151 # DHCP clients that support this option (Classless Static Routes)
152 # MUST NOT install the routes specified in the Static Routes
153 # option (option code 33) if both a Static Routes option and the
154 # Classless Static Routes option are provided.
156 if [ -n "$new_classless_routes" ]; then
157 fill_classless_routes "$new_classless_routes"
158 $LOGGER "New Classless Static Routes ($interface): $classless_routes"
159 set $classless_routes
160 while [ $# -gt 1 ]; do
161 if [ "0.0.0.0" = "$2" ]; then
162 route add "$1" -iface "$interface"
171 for router in $new_routers; do
172 if [ "$new_ip_address" = "$router" ]; then
173 route add default -iface $router >/dev/null 2>&1
175 route add default $router >/dev/null 2>&1
177 # 2nd and subsequent default routers error out, so explicitly
178 # stop processing the list after the first one.
182 if [ -n "$new_static_routes" ]; then
183 $LOGGER "New Static Routes ($interface): $new_static_routes"
184 set $new_static_routes
185 while [ $# -gt 1 ]; do
192 add_new_resolv_conf() {
193 # XXX Old code did not create/update resolv.conf unless both
194 # $new_domain_name and $new_domain_name_servers were provided. PR
195 # #3135 reported some ISP's only provide $new_domain_name_servers and
196 # thus broke the script. This code creates the resolv.conf if either
199 local tmpres=/var/run/resolv.conf.${interface}
202 if [ -n "$new_domain_name" ]; then
203 echo "search $new_domain_name" >>$tmpres
206 if [ -n "$new_domain_name_servers" ]; then
207 for nameserver in $new_domain_name_servers; do
208 echo "nameserver $nameserver" >>$tmpres
212 if [ -f $tmpres ]; then
213 if [ -f /etc/resolv.conf.tail ]; then
214 cat /etc/resolv.conf.tail >>$tmpres
217 # When resolv.conf is not changed actually, we don't
219 # If /usr is not mounted yet, we cannot use cmp, then
220 # the following test fails. In such case, we simply
221 # ignore an error and do update resolv.conf.
222 if cmp -s $tmpres /etc/resolv.conf; then
227 # In case (e.g. during OpenBSD installs) /etc/resolv.conf
228 # is a symbolic link, take care to preserve the link and write
229 # the new data in the correct location.
231 if [ -f /etc/resolv.conf ]; then
232 cat /etc/resolv.conf > /etc/resolv.conf.save
234 cat $tmpres > /etc/resolv.conf
237 # Try to ensure correct ownership and permissions.
238 chown -RL root:wheel /etc/resolv.conf
239 chmod -RL 644 /etc/resolv.conf
247 # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
250 if [ -f /etc/dhclient-exit-hooks ]; then
251 . /etc/dhclient-exit-hooks
253 # probably should do something with exit status of the local script
258 # Start of active code.
261 # Invoke the local dhcp client enter hooks, if they exist.
262 if [ -f /etc/dhclient-enter-hooks ]; then
264 . /etc/dhclient-enter-hooks
265 # allow the local script to abort processing of this state
266 # local script must set exit_status variable to nonzero.
267 if [ $exit_status -ne 0 ]; then
272 if [ -x $NETSTAT ]; then
273 if_defaultroute=`$NETSTAT -rnf inet | $AWK '{if ($1=="default") printf $6}'`
280 eval "$IFCONFIG $interface $medium"
281 eval "$IFCONFIG $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
287 $IFCONFIG $interface inet alias 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
293 BOUND|RENEW|REBIND|REBOOT)
295 if [ -n "$old_ip_address" ]; then
296 if [ "$old_ip_address" != "$alias_ip_address" ]; then
299 if [ "$old_ip_address" != "$new_ip_address" ]; then
304 if [ "$reason" = BOUND ] || \
305 [ "$reason" = REBOOT ] || \
306 [ -z "$old_ip_address" ] || \
307 [ "$old_ip_address" != "$new_ip_address" ]; then
311 if [ "$new_ip_address" != "$alias_ip_address" ]; then
319 if [ -n "$old_ip_address" ]; then
324 $ARP -d -a -i $interface
326 # XXX Why add alias we just deleted above?
328 if [ -f /etc/resolv.conf.save ]; then
329 cat /etc/resolv.conf.save > /etc/resolv.conf
337 if [ -n "$new_routers" ]; then
338 $LOGGER "New Routers ($interface): $new_routers"
340 if ping -q -c 1 -t 1 "$1"; then
341 if [ "$new_ip_address" != "$alias_ip_address" ]; then
345 if add_new_resolv_conf; then
350 eval "$IFCONFIG $interface inet -alias $new_ip_address $medium"