1
|
#!/bin/sh
|
2
|
# $Id$
|
3
|
# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
|
4
|
# $FreeBSD: src/sbin/dhclient/dhclient-script,v 1.4 2005/06/10 03:41:18 brooks Exp $
|
5
|
#
|
6
|
# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
|
7
|
#
|
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.
|
11
|
#
|
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.
|
19
|
#
|
20
|
|
21
|
NETSTAT=/usr/bin/netstat
|
22
|
GREP=/usr/bin/grep
|
23
|
AWK=/usr/bin/awk
|
24
|
HOSTNAME=/bin/hostname
|
25
|
GREP=/usr/bin/grep
|
26
|
ROUTE=/sbin/route
|
27
|
SED=/usr/bin/sed
|
28
|
ARP=/usr/sbin/arp
|
29
|
IFCONFIG=/sbin/ifconfig
|
30
|
PFCTL=/sbin/pfctl
|
31
|
|
32
|
LOCALHOST=127.0.0.1
|
33
|
|
34
|
if [ -x /usr/bin/logger ]; then
|
35
|
LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
|
36
|
else
|
37
|
LOGGER="echo"
|
38
|
fi
|
39
|
|
40
|
#
|
41
|
# Helper functions that implement common actions.
|
42
|
#
|
43
|
|
44
|
check_hostname() {
|
45
|
current_hostname=`$HOSTNAME`
|
46
|
if [ -z "$current_hostname" ] || \
|
47
|
[ "$current_hostname" = "$old_host_name" -a \
|
48
|
"$new_hostname" != "$old_host_name" ]; then
|
49
|
$LOGGER "New Hostname ($interface): $new_host_name"
|
50
|
$HOSTNAME $new_host_name
|
51
|
fi
|
52
|
}
|
53
|
|
54
|
arp_flush() {
|
55
|
$ARP -an -i $interface | \
|
56
|
$SED -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
|
57
|
/bin/sh >/dev/null 2>&1
|
58
|
}
|
59
|
|
60
|
# NOTE: use of the below has been disabled because rc.newwanip handles this correctly and this
|
61
|
# unnecessarily killed states in multiple circumstances. Leaving here for now, should be safe
|
62
|
# to remove later. -cmb 20141105
|
63
|
delete_old_states() {
|
64
|
$LOGGER "Starting delete_old_states()"
|
65
|
_FLUSHED=0
|
66
|
# If the IP changed, remove states from the old one
|
67
|
if [ -f /var/db/${interface}_ip ]; then
|
68
|
OLD_IP=`cat /var/db/${interface}_ip`
|
69
|
$LOGGER "Comparing IPs: Old: ${OLD_IP} New: ${new_ip_address}"
|
70
|
if [ -n "${OLD_IP}" ] && [ "${OLD_IP}" != "${new_ip_address}" ]; then
|
71
|
$LOGGER "Removing states from old IP '${OLD_IP}' (new IP '${new_ip_address}')"
|
72
|
${PFCTL} -i $interface -Fs
|
73
|
${PFCTL} -K ${OLD_IP}/32
|
74
|
_FLUSHED=1
|
75
|
fi
|
76
|
fi
|
77
|
# Delete states through old gateway if it's not the same
|
78
|
OLD_ROUTER=""
|
79
|
if [ -n "${old_routers}" ]; then
|
80
|
OLD_ROUTER=$old_routers
|
81
|
elif [ -f /tmp/${interface}_router ]; then
|
82
|
OLD_ROUTER=`cat /tmp/${interface}_router`
|
83
|
fi
|
84
|
if [ ${_FLUSHED} -eq 0 -a -n "${OLD_ROUTER}" ]; then
|
85
|
$LOGGER "Comparing Routers: Old: ${OLD_ROUTER} New: ${new_routers}"
|
86
|
if [ "${OLD_ROUTER}" != "${new_routers}" ]; then
|
87
|
$LOGGER "Removing states through old gateway '${OLD_ROUTER}' (new gateway '${new_routers}')"
|
88
|
${PFCTL} -i $interface -Fs
|
89
|
fi
|
90
|
fi
|
91
|
}
|
92
|
|
93
|
delete_old_address() {
|
94
|
/bin/rm -f /var/db/${interface}_ip
|
95
|
$IFCONFIG $interface inet -alias $old_ip_address $medium
|
96
|
}
|
97
|
|
98
|
add_new_address() {
|
99
|
|
100
|
$LOGGER "Starting add_new_address()"
|
101
|
|
102
|
$LOGGER "ifconfig $interface inet $new_ip_address netmask $new_subnet_mask broadcast $new_broadcast_address $medium"
|
103
|
|
104
|
$IFCONFIG $interface \
|
105
|
inet $new_ip_address \
|
106
|
netmask $new_subnet_mask \
|
107
|
broadcast $new_broadcast_address \
|
108
|
$medium
|
109
|
$IFCONFIG $interface setfirst $new_ip_address
|
110
|
|
111
|
$LOGGER "New IP Address ($interface): $new_ip_address"
|
112
|
$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
|
113
|
$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
|
114
|
$LOGGER "New Routers ($interface): $new_routers"
|
115
|
|
116
|
|
117
|
# This is necessary otherwise apinger will try to ping all 1s address
|
118
|
if [ -n "$new_routers" ] && [ "$new_routers" != "255.255.255.255" ]; then
|
119
|
echo $new_routers > /tmp/${interface}_router
|
120
|
fi
|
121
|
echo $new_ip_address > /var/db/${interface}_ip
|
122
|
}
|
123
|
|
124
|
delete_old_alias() {
|
125
|
if [ -n "$alias_ip_address" ]; then
|
126
|
$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
|
127
|
$ROUTE delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
|
128
|
fi
|
129
|
}
|
130
|
|
131
|
add_new_alias() {
|
132
|
if [ -n "$alias_ip_address" ]; then
|
133
|
$IFCONFIG $interface inet alias $alias_ip_address netmask \
|
134
|
$alias_subnet_mask
|
135
|
$ROUTE add $alias_ip_address $LOCALHOST
|
136
|
fi
|
137
|
}
|
138
|
|
139
|
fill_classless_routes() {
|
140
|
set $1
|
141
|
while [ $# -ge 5 ]; do
|
142
|
if [ $1 -eq 0 ]; then
|
143
|
route="default"
|
144
|
elif [ $1 -le 8 ]; then
|
145
|
route="$2.0.0.0/$1"
|
146
|
shift
|
147
|
elif [ $1 -le 16 ]; then
|
148
|
route="$2.$3.0.0/$1"
|
149
|
shift; shift
|
150
|
elif [ $1 -le 24 ]; then
|
151
|
route="$2.$3.$4.0/$1"
|
152
|
shift; shift; shift
|
153
|
else
|
154
|
route="$2.$3.$4.$5/$1"
|
155
|
shift; shift; shift; shift
|
156
|
fi
|
157
|
shift
|
158
|
router="$1.$2.$3.$4"
|
159
|
classless_routes="$classless_routes $route $router"
|
160
|
shift; shift; shift; shift
|
161
|
done
|
162
|
}
|
163
|
|
164
|
delete_old_routes() {
|
165
|
$LOGGER "Deleting old routes"
|
166
|
|
167
|
if [ -n "$old_classless_routes" ]; then
|
168
|
fill_classless_routes "$old_classless_routes"
|
169
|
set $classless_routes
|
170
|
while [ $# -gt 1 ]; do
|
171
|
route delete "$1" "$2"
|
172
|
shift; shift
|
173
|
done
|
174
|
return 0;
|
175
|
fi
|
176
|
|
177
|
# Only allow the default route to be overridden if it's on our own interface
|
178
|
if [ -f "/tmp/${interface}_defaultgw" ]; then
|
179
|
for router in $old_routers; do
|
180
|
$ROUTE delete default $router >/dev/null 2>&1
|
181
|
/bin/rm -f /tmp/${interface}_router
|
182
|
done
|
183
|
fi
|
184
|
|
185
|
if [ -n "$old_static_routes" ]; then
|
186
|
set $old_static_routes
|
187
|
while [ $# -gt 1 ]; do
|
188
|
$ROUTE delete "$1" "$2"
|
189
|
shift; shift
|
190
|
/bin/rm -f /tmp/${interface}_router
|
191
|
done
|
192
|
fi
|
193
|
|
194
|
arp_flush
|
195
|
}
|
196
|
|
197
|
add_new_routes() {
|
198
|
$LOGGER "Adding new routes to interface: $interface"
|
199
|
|
200
|
# RFC 3442: If the DHCP server returns both a Classless Static
|
201
|
# Routes option and a Router option, the DHCP client MUST ignore
|
202
|
# the Router option.
|
203
|
#
|
204
|
# DHCP clients that support this option (Classless Static Routes)
|
205
|
# MUST NOT install the routes specified in the Static Routes
|
206
|
# option (option code 33) if both a Static Routes option and the
|
207
|
# Classless Static Routes option are provided.
|
208
|
if [ -n "$new_classless_routes" ]; then
|
209
|
fill_classless_routes "$new_classless_routes"
|
210
|
$LOGGER "New Classless Static Routes ($interface): $classless_routes"
|
211
|
set $classless_routes
|
212
|
while [ $# -gt 1 ]; do
|
213
|
if [ "0.0.0.0" = "$2" ]; then
|
214
|
route add "$1" -iface "$interface"
|
215
|
else
|
216
|
route add "$1" "$2"
|
217
|
fi
|
218
|
shift; shift
|
219
|
done
|
220
|
return
|
221
|
fi
|
222
|
|
223
|
ADDED_ROUTE=no
|
224
|
EXISTSGW=`/bin/ls -l /tmp/*_defaultgw | /usr/bin/wc -l`
|
225
|
# Only allow the default route to be overridden if it's on our own interface
|
226
|
if [ -f "/tmp/${interface}_defaultgw" -o $EXISTSGW -eq 0 ]; then
|
227
|
$ROUTE delete default
|
228
|
for router in $new_routers; do
|
229
|
if [ "$new_ip_address" = "$router" -o "$router" = "255.255.255.255" ]; then
|
230
|
$ROUTE add default -iface $interface
|
231
|
echo $ROUTE add default -iface $interface | $LOGGER
|
232
|
# NOTE: Do not activate this for all ones address since pf(4) will try to forward packets to it.
|
233
|
if [ "$new_ip_address" = "$router" ]; then
|
234
|
echo $router > /tmp/${interface}_router
|
235
|
fi
|
236
|
else
|
237
|
$ROUTE add default $router
|
238
|
echo $ROUTE add default $router | $LOGGER
|
239
|
echo $router > /tmp/${interface}_router
|
240
|
fi
|
241
|
ADDED_ROUTE=yes
|
242
|
# 2nd and subsequent default routers error out, so explicitly
|
243
|
# stop processing the list after the first one.
|
244
|
break
|
245
|
done
|
246
|
fi
|
247
|
|
248
|
if [ -n "$new_static_routes" ]; then
|
249
|
$LOGGER "New Static Routes ($interface): $new_static_routes"
|
250
|
set $new_static_routes
|
251
|
while [ $# -gt 1 ]; do
|
252
|
$ROUTE add $1 $2
|
253
|
if [ "$ADDED_ROUTE" = "no" ]; then
|
254
|
echo $2 > /tmp/${interface}_router
|
255
|
fi
|
256
|
shift; shift
|
257
|
done
|
258
|
fi
|
259
|
}
|
260
|
|
261
|
add_new_resolv_conf() {
|
262
|
$LOGGER "Creating resolv.conf"
|
263
|
if [ -f "/var/etc/nameserver_$interface" ]; then
|
264
|
# Remove old entries
|
265
|
for nameserver in `cat /var/etc/nameserver_$interface`; do
|
266
|
$ROUTE delete $nameserver >/dev/null 2>&1
|
267
|
done
|
268
|
fi
|
269
|
if [ -n "$new_domain_name_servers" ]; then
|
270
|
/bin/rm -f /var/etc/nameserver_$interface
|
271
|
ALLOWOVERRIDE=`/usr/bin/grep dnsallowoverride /conf/config.xml | /usr/bin/wc -l`
|
272
|
for nameserver in $new_domain_name_servers; do
|
273
|
# Add a route to the nameserver out the correct interface
|
274
|
# so that mulitple wans work correctly with multiple dns
|
275
|
# also backup the nameserver for later route removal
|
276
|
if [ $ALLOWOVERRIDE -gt 0 ]; then
|
277
|
echo $nameserver >>/var/etc/nameserver_$interface
|
278
|
$ROUTE add $nameserver -iface $interface
|
279
|
fi
|
280
|
done
|
281
|
echo $new_domain_name >/var/etc/searchdomain_$interface
|
282
|
fi
|
283
|
|
284
|
return 0
|
285
|
}
|
286
|
|
287
|
# Notify rc.newwanip of changes to an interface
|
288
|
notify_rc_newwanip() {
|
289
|
/usr/local/sbin/pfSctl -c "interface newip $interface"
|
290
|
}
|
291
|
|
292
|
#
|
293
|
# Start of active code.
|
294
|
#
|
295
|
|
296
|
# Invoke the local dhcp client enter hooks, if they exist.
|
297
|
if [ -f /etc/dhclient-enter-hooks ]; then
|
298
|
$LOGGER "dhclient-enter-hooks"
|
299
|
exit_status=0
|
300
|
. /etc/dhclient-enter-hooks
|
301
|
# allow the local script to abort processing of this state
|
302
|
# local script must set exit_status variable to nonzero.
|
303
|
if [ $exit_status -ne 0 ]; then
|
304
|
exit $exit_status
|
305
|
fi
|
306
|
fi
|
307
|
|
308
|
#if [ -x $ROUTE ]; then
|
309
|
# if_defaultroute=`$ROUTE -n get -inet default | $GREP interface | $AWK '{print $2}'`
|
310
|
#else
|
311
|
# $LOGGER "if_defaultroute"
|
312
|
# if_defaultroute="x"
|
313
|
#fi
|
314
|
|
315
|
$LOGGER $reason
|
316
|
case $reason in
|
317
|
MEDIUM)
|
318
|
$IFCONFIG $interface $medium
|
319
|
$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
|
320
|
/bin/sleep 1
|
321
|
;;
|
322
|
|
323
|
PREINIT)
|
324
|
delete_old_alias
|
325
|
$IFCONFIG $interface inet 0.0.0.0 netmask 255.255.255.255 broadcast 255.255.255.255 up
|
326
|
/bin/rm -f /tmp/${interface}_router
|
327
|
;;
|
328
|
|
329
|
ARPCHECK|ARPSEND)
|
330
|
;;
|
331
|
|
332
|
BOUND|RENEW|REBIND|REBOOT)
|
333
|
check_hostname
|
334
|
changes="no"
|
335
|
if [ -n "$old_ip_address" ]; then
|
336
|
if [ -n "$alias_ip_address" ] && \
|
337
|
[ "$old_ip_address" != "$alias_ip_address" ]; then
|
338
|
delete_old_alias
|
339
|
changes="yes"
|
340
|
fi
|
341
|
if [ "$old_ip_address" != "$new_ip_address" ]; then
|
342
|
delete_old_address
|
343
|
delete_old_routes
|
344
|
changes="yes"
|
345
|
fi
|
346
|
fi
|
347
|
if [ "$reason" = BOUND ] || \
|
348
|
[ "$reason" = REBOOT ] || \
|
349
|
[ -z "$old_ip_address" ] || \
|
350
|
[ "$old_ip_address" != "$new_ip_address" ]; then
|
351
|
add_new_address
|
352
|
add_new_routes
|
353
|
changes="yes"
|
354
|
fi
|
355
|
if [ -n "$alias_ip_address" ] && \
|
356
|
[ "$new_ip_address" != "$alias_ip_address" ]; then
|
357
|
add_new_alias
|
358
|
changes="yes"
|
359
|
fi
|
360
|
add_new_resolv_conf
|
361
|
if [ "$changes" = "yes" ] ; then
|
362
|
notify_rc_newwanip
|
363
|
fi
|
364
|
;;
|
365
|
|
366
|
EXPIRE|FAIL)
|
367
|
delete_old_alias
|
368
|
if [ -n "$old_ip_address" ]; then
|
369
|
delete_old_address
|
370
|
delete_old_routes
|
371
|
fi
|
372
|
;;
|
373
|
|
374
|
TIMEOUT)
|
375
|
delete_old_alias
|
376
|
add_new_address
|
377
|
/bin/sleep 1
|
378
|
if [ -n "$new_routers" ]; then
|
379
|
$LOGGER "New Routers ($interface): $new_routers"
|
380
|
set "$new_routers"
|
381
|
if /sbin/ping -q -c 1 -t 1 "$1"; then
|
382
|
if [ "$new_ip_address" != "$alias_ip_address" ]; then
|
383
|
add_new_alias
|
384
|
fi
|
385
|
add_new_routes
|
386
|
if add_new_resolv_conf; then
|
387
|
notify_rc_newwanip
|
388
|
fi
|
389
|
fi
|
390
|
fi
|
391
|
$IFCONFIG $interface inet -alias $new_ip_address $medium
|
392
|
delete_old_routes
|
393
|
;;
|
394
|
esac
|
395
|
|
396
|
# Invoke the local dhcp client exit hooks, if they exist.
|
397
|
if [ -f /etc/dhclient-exit-hooks ]; then
|
398
|
$LOGGER "dhclient-exit-hooks"
|
399
|
exit_status=0
|
400
|
. /etc/dhclient-exit-hooks
|
401
|
# allow the local script to abort processing of this state
|
402
|
# local script must set exit_status variable to nonzero.
|
403
|
exit $exit_status
|
404
|
fi
|