dhclient - Fix interval handling.
[dragonfly.git] / sbin / dhclient / dhclient-script
CommitLineData
846204b6
HT
1#!/bin/sh
2#
944f2750 3# $OpenBSD: src/sbin/dhclient/dhclient-script,v 1.18 2011/03/27 12:15:46 krw Exp $
846204b6
HT
4#
5# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
6#
7# Permission to use, copy, modify, and distribute this software for any
8# purpose with or without fee is hereby granted, provided that the above
9# copyright notice and this permission notice appear in all copies.
10#
11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18#
19#
20
21#
22# Helper functions that implement common actions.
23#
24
25delete_old_address() {
26 if [ -n "$old_ip_address" ]; then
27 ifconfig $interface inet $old_ip_address delete $medium
34910103 28 #route delete "$old_ip_address" 127.0.0.1 >/dev/null 2>&1
846204b6
HT
29 fi
30}
31
32add_new_address() {
33 ifconfig $interface \
34 inet $new_ip_address \
35 netmask $new_subnet_mask \
36 broadcast $new_broadcast_address \
37 $medium
38
39 # XXX Original TIMEOUT code did not do this unless $new_routers was set?
34910103 40 #route add $new_ip_address 127.0.0.1 >/dev/null 2>&1
846204b6
HT
41}
42
43delete_old_alias() {
44 if [ -n "$alias_ip_address" ]; then
45 ifconfig $interface inet $alias_ip_address delete > /dev/null 2>&1
34910103 46# #route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
846204b6
HT
47 fi
48}
49
50add_new_alias() {
51 if [ -n "$alias_ip_address" ]; then
52 ifconfig $interface inet $alias_ip_address alias netmask \
53 $alias_subnet_mask
34910103 54 #route add $alias_ip_address 127.0.0.1
846204b6
HT
55 fi
56}
57
58delete_old_routes() {
846204b6
HT
59 if [ -n "$old_static_routes" ]; then
60 set $old_static_routes
61 while [ $# -gt 1 ]; do
62 route delete "$1" "$2"
63 shift; shift
64 done
65 fi
66
67 arp -dan
68}
69
70add_new_routes() {
846204b6 71 for router in $new_routers; do
3a961bf3 72 route -q delete default
846204b6 73 if [ "$new_ip_address" = "$router" ]; then
3a961bf3 74 route -q add default -iface $router
846204b6 75 else
3a961bf3 76 route -q add default $router
846204b6
HT
77 fi
78 # 2nd and subsequent default routers error out, so explicitly
79 # stop processing the list after the first one.
80 break
81 done
82
83 if [ -n "$new_static_routes" ]; then
84 set $new_static_routes
85 while [ $# -gt 1 ]; do
86 route add $1 $2
87 shift; shift
88 done
89 fi
90}
91
92add_new_resolv_conf() {
34afd952
AHJ
93 # Create resolv.conf when either $new_domain_name_servers or
94 # $new_domain_name are provided. As reported in PR#3135, some ISPs
95 # provide only $new_domain_name_servers.
846204b6
HT
96
97 rm -f /etc/resolv.conf.std
98
99 if [ -n "$new_domain_name" ]; then
100 echo "search $new_domain_name" >>/etc/resolv.conf.std
101 fi
102
103 if [ -n "$new_domain_name_servers" ]; then
104 for nameserver in $new_domain_name_servers; do
105 echo "nameserver $nameserver" >>/etc/resolv.conf.std
106 done
107 fi
108
109 if [ -f /etc/resolv.conf.std ]; then
110 if [ -f /etc/resolv.conf.tail ]; then
111 cat /etc/resolv.conf.tail >>/etc/resolv.conf.std
112 fi
113
114 # In case (e.g. during OpenBSD installs) /etc/resolv.conf
115 # is a symbolic link, take care to preserve the link and write
116 # the new data in the correct location.
117
118 if [ -f /etc/resolv.conf ]; then
119 cat /etc/resolv.conf > /etc/resolv.conf.save
120 fi
121 cat /etc/resolv.conf.std > /etc/resolv.conf
122 rm -f /etc/resolv.conf.std
123
124 # Try to ensure correct ownership and permissions.
125 chown -RL root:wheel /etc/resolv.conf
126 chmod -RL 644 /etc/resolv.conf
127
128 return 0
129 fi
130
131 return 1
132}
133
134#
135# Start of active code.
136#
137
138case $reason in
139MEDIUM)
140 ifconfig $interface $medium
141 sleep 1
142 ;;
143
144PREINIT)
145 delete_old_alias
146 ifconfig $interface up
147 ;;
148
944f2750
AHJ
149ARPCHECK)
150 # Always succeed. i.e. accept lease.
151 ;;
152
153ARPSEND)
154 # Always fail. i.e. don't wait for ARP packet here.
155 exit 1
846204b6
HT
156 ;;
157
158BOUND|RENEW|REBIND|REBOOT)
159 if [ -n "$old_ip_address" ]; then
160 if [ "$old_ip_address" != "$alias_ip_address" ]; then
161 delete_old_alias
162 fi
163 if [ "$old_ip_address" != "$new_ip_address" ]; then
164 delete_old_address
165 delete_old_routes
166 fi
167 fi
168 if [ "$reason" = BOUND ] ||
169 [ "$reason" = REBOOT ] ||
170 [ -z "$old_ip_address" ] ||
171 [ "$old_ip_address" != "$new_ip_address" ]; then
172 add_new_address
173 add_new_routes
174 fi
175 if [ "$new_ip_address" != "$alias_ip_address" ]; then
176 add_new_alias
177 fi
178 add_new_resolv_conf
179 ;;
180
181EXPIRE|FAIL)
182 delete_old_alias
183 if [ -n "$old_ip_address" ]; then
184 delete_old_address
185 delete_old_routes
186 fi
187 # XXX Why add alias we just deleted above?
188 add_new_alias
189 if [ -f /etc/resolv.conf.save ]; then
190 cat /etc/resolv.conf.save > /etc/resolv.conf
191 fi
192 ;;
193
194TIMEOUT)
195 delete_old_alias
196 add_new_address
197 sleep 1
198 if [ -n "$new_routers" ]; then
199 set "$new_routers"
200 if ping -q -c 1 -w 1 "$1"; then
201 if [ "$new_ip_address" != "$alias_ip_address" ]; then
202 add_new_alias
203 fi
204 add_new_routes
205 if add_new_resolv_conf; then
206 exit 0
207 fi
208 fi
209 fi
210 ifconfig $interface inet $new_ip_address delete $medium
211 # XXX Why not a delete_old_address as before all other invocations of
212 # delete_old_routes?
213 delete_old_routes
214 exit 1
215 ;;
216esac
217
218exit 0