Sync usage with man page.
[netbsd-mini2440.git] / usr.sbin / dhcp / clientscript / dhclient-script
blob2f86f2453c2a088541d68d1535932284ef477a8c
1 #!/bin/sh
3 # $NetBSD: dhclient-script,v 1.12 2006/10/08 17:09:32 riz Exp $
5 ENTERHOOKS=/etc/dhclient-enter-hooks
6 EXITHOOKS=/etc/dhclient-exit-hooks
7 RESOLV=/etc/resolv.conf
8 SIGNATURE="# Created by dhclient from"
9 INTSIG="$SIGNATURE $interface"
11 echo_resolv_conf() {
12 echo "$INTSIG"
13 echo search $new_domain_name
14 for nameserver in $new_domain_name_servers; do
15 echo nameserver $nameserver
16 done
19 make_resolv_conf() {
20 if [ ! -z "$new_domain_name_servers" ]; then
21 if [ -x /sbin/resolvconf ]; then
22 echo_resolv_conf | /sbin/resolvconf -a $interface
23 else
24 if [ -f $RESOLV ]; then
25 while read line; do
26 case $line in
27 "$SIGNATURE"*)
30 mv $RESOLV $RESOLV.save;;
31 esac
32 break
33 done < $RESOLV
35 echo_resolv_conf > $RESOLV
40 restore_resolv_conf() {
41 if [ -x /sbin/resolvconf ]; then
42 /sbin/resolvconf -d $interface
43 elif [ -f $RESOLV.save -a -f $RESOLV ]; then
44 while read line; do
45 case $line in
46 "$INTSIG"*)
47 mv $RESOLV.save $RESOLV;;
48 esac
49 break
50 done < $RESOLV
54 # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
55 exit_with_hooks() {
56 exit_status=$1
57 if [ -f "$EXITHOOKS" ]; then
58 . "$EXITHOOKS"
60 # probably should do something with exit status of the local script
61 exit $exit_status
65 add_new_routes() {
66 for router in $new_routers; do
67 route add default $router
68 done >/dev/null 2>&1
70 set -- $new_static_routes
71 while [ $# -gt 1 ]; do
72 route add $1 $2
73 shift; shift
74 done
77 delete_old_routes() {
78 for router in $old_routers; do
79 route delete default $router
80 done >/dev/null 2>&1
82 set -- $old_static_routes
83 while [ $# -gt 1 ]; do
84 route delete $1 $2
85 shift; shift
86 done
88 route -n flush -inet -llinfo -host
91 # Invoke the local dhcp client enter hooks, if they exist.
92 if [ -f $ENTERHOOKS ]; then
93 exit_status=0
94 . $ENTERHOOKS
95 # allow the local script to abort processing of this state
96 # local script must set exit_status variable to nonzero.
97 if [ $exit_status -ne 0 ]; then
98 exit $exit_status
102 #if [ ! -z "$new_host_name" ]; then
103 # echo New Host Name: $new_host_name
106 #if [ ! -z "$new_nis_domain" ]; then
107 # echo New NIS Domain: $new_nis_domain
110 #if [ ! -z "$new_network_number" ]; then
111 # echo New Network Number: $new_network_number
114 if [ ! -z "$new_broadcast_address" ]; then
115 # echo New Broadcast Address: $new_broadcast_address
116 new_broadcast_arg="broadcast $new_broadcast_address"
119 if [ ! -z "$old_broadcast_address" ]; then
120 old_broadcast_arg="broadcast $old_broadcast_address"
123 if [ ! -z "$new_subnet_mask" ]; then
124 new_netmask_arg="netmask $new_subnet_mask"
127 if [ ! -z "$old_subnet_mask" ]; then
128 old_netmask_arg="netmask $old_subnet_mask"
131 if [ ! -z "$alias_subnet_mask" ]; then
132 alias_subnet_arg="netmask $alias_subnet_mask"
135 case "$reason" in
136 MEDIUM)
137 test -z "$medium" && exit_with_hooks 0
138 eval "ifconfig $interface $medium"
139 eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
141 sleep 1
143 exit_with_hooks 0
146 PREINIT)
147 if [ ! -z "$alias_ip_address" ]; then
148 ifconfig $interface inet \
149 -alias $alias_ip_address >/dev/null 2>&1
150 route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
153 ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
154 broadcast 255.255.255.255 up
156 exit_with_hooks 0
159 ARPCHECK|ARPSEND)
160 exit_with_hooks 0
163 BOUND|RENEW|REBIND|REBOOT)
164 if [ ! -z "$new_nis_domain" ]; then
165 if type domainname > /dev/null 2>&1; then
166 domainname $new_nis_domain
170 if type hostname > /dev/null 2>&1; then
171 if [ \( -z "$current_hostname" \) -o \
172 \( "x$new_host_name" = "x$old_hostname" \) ]; then
173 current_hostname=`hostname`
175 if [ \( -z "$current_hostname" \) -o \
176 \( "x$new_host_name" != "x$old_hostname" \) ]; then
177 hostname $new_host_name
181 if [ \( ! -z "$old_ip_address" \) -a \( ! -z "$alias_ip_address" \) -a \
182 \( "x$alias_ip_address" != "x$old_ip_address" \) ]; then
183 ifconfig $interface inet \
184 -alias $alias_ip_address > /dev/null 2>&1
185 route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
188 if [ \( ! -z "$old_ip_address" \) -a \
189 \( "x$old_ip_address" != "x$new_ip_address" \) ]; then
190 eval "ifconfig $interface inet -alias $old_ip_address $medium"
191 route delete $old_ip_address 127.0.0.1 >/dev/null 2>&1
193 delete_old_routes
196 if [ \( -z "$old_ip_address" \) -o \
197 \( "x$old_ip_address" != "x$new_ip_address" \) -o \
198 \( "x$reason" = "xBOUND" \) -o \( "x$reason" = "xREBOOT" \) ]; then
199 eval "ifconfig $interface inet $new_ip_address \
200 $new_netmask_arg $new_broadcast_arg $medium"
201 route add $new_ip_address 127.0.0.1 >/dev/null 2>&1
203 add_new_routes
206 if [ \( ! -z "$alias_ip_address" \) -a \
207 \( "x$new_ip_address" != "x$alias_ip_address" \) ]; then
208 ifconfig $interface inet alias $alias_ip_address \
209 $alias_subnet_arg
210 route add $alias_ip_address 127.0.0.1
212 make_resolv_conf
213 exit_with_hooks 0
216 EXPIRE|FAIL|RELEASE|STOP)
217 if [ ! -z "$old_nis_domain" ]; then
218 if type domainname > /dev/null 2>&1; then
219 # delete the old nis domain name
220 domainname ""
224 if [ ! -z "$alias_ip_address" ]; then
225 ifconfig $interface inet -alias $alias_ip_address
226 route delete $alias_ip_address 127.0.0.1
227 fi > /dev/null 2>&1
229 if [ ! -z "$old_ip_address" ]; then
230 eval "ifconfig $interface inet -alias $old_ip_address $medium"
231 route delete $old_ip_address 127.0.0.1 >/dev/null 2>&1
232 delete_old_routes
236 if [ ! -z "$alias_ip_address" ]; then
237 ifconfig $interface inet alias $alias_ip_address \
238 $alias_subnet_arg
239 route add $alias_ip_address 127.0.0.1
242 restore_resolv_conf
243 exit_with_hooks 0
246 TIMEOUT)
247 if [ ! -z "$alias_ip_address" ]; then
248 ifconfig $interface inet -alias $alias_ip_address
249 route delete $alias_ip_address 127.0.0.1
250 fi > /dev/null 2>&1
252 if [ ! -z "$new_host_name" ]; then
253 if type hostname > /dev/null 2>&1; then
254 hostname $new_host_name
258 if [ ! -z "$new_nis_domain" ]; then
259 if type domainname > /dev/null 2>&1; then
260 domainname $new_nis_domain
264 eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \
265 $new_broadcast_arg $medium"
266 sleep 1
268 if [ ! -z "$new_routers" ]; then
269 set -- $new_routers
270 if ping -n -q -c 1 -w 1 $1; then
271 if [ \( ! -z "$alias_ip_address" \) -a \
272 \( "x$new_ip_address" != "x$alias_ip_address" \) ]
273 then
274 ifconfig $interface inet alias \
275 $alias_ip_address $alias_subnet_arg
276 route add $alias_ip_address 127.0.0.1
279 route add $new_ip_address 127.0.0.1 >/dev/null 2>&1
281 add_new_routes
282 make_resolv_conf
283 exit_with_hooks 0
287 eval "ifconfig $interface inet -alias $new_ip_address $medium"
289 delete_old_routes
291 exit_with_hooks 1
294 exit_with_hooks 0
296 esac