Project

General

Profile

Download (10.9 KB) Statistics
| Branch: | Tag: | Revision:
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

    
31
LOCALHOST=127.0.0.1
32

    
33
if [ -x /usr/bin/logger ]; then
34
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
35
else
36
	LOGGER=echo
37
fi
38

    
39
#
40
# Helper functions that implement common actions.
41
#
42

    
43
check_hostname() {
44
	current_hostname=`$HOSTNAME`
45
	if [ -z "$current_hostname" ]; then
46
		$LOGGER "New Hostname ($interface): $new_host_name"
47
		$HOSTNAME $new_host_name
48
	elif [ "$current_hostname" = "$old_host_name" -a \
49
	       "$new_host_name" != "$old_host_name" ]; then
50
		$LOGGER "New Hostname ($interface): $new_host_name"
51
		$HOSTNAME $new_host_name
52
	fi
53
}
54

    
55
arp_flush() {
56
	$ARP -an -i $interface | \
57
		$SED -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
58
		/bin/sh >/dev/null 2>&1
59
}
60

    
61
delete_old_states() {
62
	$LOGGER "Starting delete_old_states()"
63
	# If the IP changed, remove states from the old one
64
	if [ -f /var/db/${interface}_ip ]; then
65
		OLD_IP = `cat /var/db/${interface}_ip`
66
		$LOGGER "Comparing IPs: Old: ${OLD_IP} New: ${new_ip_address}"
67
		if [ -n "${OLD_IP}" ] && [ "${OLD_IP}" != "${new_ip_address}" ]; then
68
			$LOGGER "Removing states from old IP '${OLD_IP}' (new IP '${new_ip_address}')"
69
			/sbin/pfctl -i $interface -Fs
70
			pfctl -K ${OLD_IP}/32
71
		fi
72
	fi
73
	# Delete states through old gateway if it's not the same
74
	if [ -n "${old_routers}" ]; then
75
		OLD_ROUTER = $old_routers
76
	fi
77
	if [ -z "${OLD_ROUTER}" ] && [ -f /tmp/${interface}_router ]; then
78
		OLD_ROUTER = `cat /tmp/${interface}_router`
79
	fi
80
	if [ -n "${OLD_ROUTER}" ]; then
81
		$LOGGER "Comparing Routers: Old: ${OLD_ROUTER} New: ${new_routers}"
82
		if [ "${OLD_ROUTER}" != "${new_routers}" ]; then
83
			$LOGGER "Removing states through old gateway '${OLD_ROUTER}' (new gateway '${new_routers}')"
84
			/sbin/pfctl -i $interface -Fs -G ${OLD_ROUTER}
85
		fi
86
	fi
87
}
88

    
89
delete_old_address() {
90
	/bin/rm -f /var/db/${interface}_ip
91
	$IFCONFIG $interface inet -alias $old_ip_address $medium
92
}
93

    
94
add_new_address() {
95

    
96
	$LOGGER "Starting add_new_address()"
97

    
98
	$LOGGER "ifconfig $interface inet $new_ip_address netmask $new_subnet_mask broadcast $new_broadcast_address $medium"
99

    
100
	$IFCONFIG $interface \
101
		inet $new_ip_address \
102
		netmask $new_subnet_mask \
103
		broadcast $new_broadcast_address \
104
		$medium
105

    
106
		$LOGGER "New IP Address ($interface): $new_ip_address"
107
		$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
108
		$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
109
		$LOGGER "New Routers ($interface): $new_routers"
110

    
111

    
112
	# This is necessary otherwise apinger will try to ping all 1s address
113
	if [ -n "$new_routers" ] && [ "$new_routers" != "255.255.255.255" ]; then
114
		echo $new_routers > /tmp/${interface}_router
115
	fi
116
	echo $new_ip_address > /var/db/${interface}_ip
117
}
118

    
119
delete_old_alias() {
120
	if [ -n "$alias_ip_address" ]; then
121
		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
122
		$ROUTE delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
123
	fi
124
}
125

    
126
add_new_alias() {
127
	if [ -n "$alias_ip_address" ]; then
128
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
129
		    $alias_subnet_mask
130
		$ROUTE add $alias_ip_address $LOCALHOST
131
	fi
132
}
133

    
134
fill_classless_routes() {
135
	set $1
136
	while [ $# -ge 5 ]; do
137
		if [ $1 -eq 0 ]; then
138
			route="default"
139
		elif [ $1 -le 8 ]; then
140
			route="$2.0.0.0/$1"
141
			shift
142
		elif [ $1 -le 16 ]; then
143
			route="$2.$3.0.0/$1"
144
			shift; shift
145
		elif [ $1 -le 24 ]; then
146
			route="$2.$3.$4.0/$1"
147
			shift; shift; shift
148
		else
149
			route="$2.$3.$4.$5/$1"
150
			shift; shift; shift; shift
151
		fi
152
		shift
153
		router="$1.$2.$3.$4"
154
		classless_routes="$classless_routes $route $router"
155
		shift; shift; shift; shift
156
	done
157
}
158

    
159
delete_old_routes() {
160
	$LOGGER "Deleting old routes"
161

    
162
	if [ -n "$old_classless_routes" ]; then
163
		fill_classless_routes "$old_classless_routes"
164
		set $classless_routes
165
		while [ $# -gt 1 ]; do
166
			route delete "$1" "$2"
167
			shift; shift
168
		done
169
		return 0;
170
	fi
171

    
172
	# Only allow the default route to be overridden if it's on our own interface
173
	if [ -f "/tmp/${interface}_defaultgw" ]; then
174
		for router in $old_routers; do
175
			$ROUTE delete default $router >/dev/null 2>&1
176
			/bin/rm -f /tmp/${interface}_router
177
		done
178
	fi
179

    
180
	if [ -n "$old_static_routes" ]; then
181
		set $old_static_routes
182
		while [ $# -gt 1 ]; do
183
			$ROUTE delete "$1" "$2"
184
			shift; shift
185
                        /bin/rm -f /tmp/${interface}_router
186
		done
187
	fi
188

    
189
	arp_flush
190
}
191

    
192
add_new_routes() {
193
	$LOGGER "Adding new routes to interface: $interface"
194

    
195
	# RFC 3442: If the DHCP server returns both a Classless Static
196
	# Routes option and a Router option, the DHCP client MUST ignore
197
	# the Router option.
198
	#
199
	# DHCP clients that support this option (Classless Static Routes)
200
	# MUST NOT install the routes specified in the Static Routes
201
	# option (option code 33) if both a Static Routes option and the
202
	# Classless Static Routes option are provided.
203
	if [ -n "$new_classless_routes" ]; then
204
		fill_classless_routes "$new_classless_routes"
205
		$LOGGER "New Classless Static Routes ($interface): $classless_routes"
206
		set $classless_routes
207
		while [ $# -gt 1 ]; do
208
			if [ "0.0.0.0" = "$2" ]; then
209
				route add "$1" -iface "$interface"
210
			else
211
				route add "$1" "$2"
212
			fi
213
			shift; shift
214
		done
215
		return
216
	fi
217

    
218
	ADDED_ROUTE=no
219
	EXISTSGW=`/bin/ls -l /tmp/*_defaultgw | /usr/bin/wc -l`
220
	# Only allow the default route to be overridden if it's on our own interface
221
	if [ -f "/tmp/${interface}_defaultgw" -o $EXISTSGW -eq 0 ]; then
222
		$ROUTE delete default
223
		for router in $new_routers; do
224
			if [ "$new_ip_address" = "$router" -o "$router" = "255.255.255.255" ]; then
225
				$ROUTE add default -iface $interface
226
				echo $ROUTE add default -iface $interface | $LOGGER
227
				# NOTE: Do not activate this for all ones address since pf(4) will try to forward packets to it.
228
				if [ "$new_ip_address" = "$router" ]; then
229
					echo $router > /tmp/${interface}_router
230
				fi
231
			else
232
				$ROUTE add default $router
233
				echo $ROUTE add default $router | $LOGGER
234
                       		echo $router > /tmp/${interface}_router
235
			fi
236
			ADDED_ROUTE=yes
237
			# 2nd and subsequent default routers error out, so explicitly
238
			# stop processing the list after the first one.
239
			break
240
		done
241
	fi
242

    
243
	if [ -n "$new_static_routes" ]; then
244
		$LOGGER "New Static Routes ($interface): $new_static_routes"
245
		set $new_static_routes
246
		while [ $# -gt 1 ]; do
247
			$ROUTE add $1 $2
248
			if [ "$ADDED_ROUTE" = "no" ]; then
249
                        	echo $2 > /tmp/${interface}_router
250
			fi
251
			shift; shift
252
		done
253
	fi
254
}
255

    
256
add_new_resolv_conf() {
257
	$LOGGER "Creating resolv.conf"
258
	if [ -f "/var/etc/nameserver_$interface" ]; then
259
		# Remove old entries
260
		for nameserver in `cat /var/etc/nameserver_$interface`; do
261
			$ROUTE delete $nameserver >/dev/null 2>&1
262
		done
263
	fi
264
	if [ -n "$new_domain_name_servers" ]; then 
265
		/bin/rm -f /var/etc/nameserver_$interface
266
		ALLOWOVERRIDE=`/usr/bin/grep dnsallowoverride /conf/config.xml | /usr/bin/wc -l`
267
		for nameserver in $new_domain_name_servers; do
268
			# Add a route to the nameserver out the correct interface
269
			# so that mulitple wans work correctly with multiple dns
270
			# also backup the nameserver for later route removal
271
			if [ $ALLOWOVERRIDE -gt 0 ]; then
272
				echo $nameserver >>/var/etc/nameserver_$interface
273
				$ROUTE add $nameserver -iface $interface
274
			fi
275
		done
276
		echo $new_domain_name >/var/etc/searchdomain_$interface
277
	fi
278

    
279
	return 0
280
}
281

    
282
# Notify rc.newwanip of changes to an interface
283
notify_rc_newwanip() {
284
	/usr/local/sbin/pfSctl -c "interface newip $interface"
285
}
286

    
287
#
288
# Start of active code.
289
#
290

    
291
# Invoke the local dhcp client enter hooks, if they exist.
292
if [ -f /etc/dhclient-enter-hooks ]; then
293
	$LOGGER "dhclient-enter-hooks"
294
	exit_status=0
295
	. /etc/dhclient-enter-hooks
296
	# allow the local script to abort processing of this state
297
	# local script must set exit_status variable to nonzero.
298
	if [ $exit_status -ne 0 ]; then
299
		exit $exit_status
300
	fi
301
fi
302

    
303
#if [ -x $ROUTE ]; then
304
#	if_defaultroute=`$ROUTE -n get -inet default | $GREP interface | $AWK '{print $2}'`
305
#else
306
#	$LOGGER "if_defaultroute"
307
#	if_defaultroute="x"
308
#fi
309

    
310
$LOGGER $reason
311
case $reason in
312
MEDIUM)
313
	$IFCONFIG $interface $medium
314
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
315
	/bin/sleep 1
316
	;;
317

    
318
PREINIT)
319
	delete_old_alias
320
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
321
	delete_old_states
322
	/bin/rm -f /tmp/${interface}_router
323
	;;
324

    
325
ARPCHECK|ARPSEND)
326
	;;
327

    
328
BOUND|RENEW|REBIND|REBOOT)
329
	check_hostname
330
	changes="no"
331
	if [ "$old_ip_address" != "$new_ip_address" ]; then
332
		delete_old_states
333
	fi
334
	if [ -n "$old_ip_address" ]; then
335
		if [ -n "$alias_ip_address" ] && \
336
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
337
			delete_old_alias
338
			changes="yes"
339
		fi
340
		if [ "$old_ip_address" != "$new_ip_address" ]; then
341
			delete_old_address
342
			delete_old_routes
343
			changes="yes"
344
		fi
345
	fi
346
	if [ "$reason" = BOUND ] || \
347
	   [ "$reason" = REBOOT ] || \
348
	   [ -z "$old_ip_address" ] || \
349
	   [ "$old_ip_address" != "$new_ip_address" ]; then
350
		add_new_address
351
		add_new_routes
352
		changes="yes"
353
	fi
354
	if [ -n "$alias_ip_address" ] && \
355
       [ "$new_ip_address" != "$alias_ip_address" ]; then
356
		add_new_alias
357
		changes="yes"
358
	fi
359
	add_new_resolv_conf
360
	if [ "$changes" = "yes" ] ; then
361
		notify_rc_newwanip
362
	fi
363
	;;
364

    
365
EXPIRE|FAIL)
366
	delete_old_alias
367
	delete_old_states
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_states
393
	delete_old_routes
394
	;;
395
esac
396

    
397
# Invoke the local dhcp client exit hooks, if they exist.
398
if [ -f /etc/dhclient-exit-hooks ]; then
399
	$LOGGER "dhclient-exit-hooks"
400
	exit_status=0
401
	. /etc/dhclient-exit-hooks
402
	# allow the local script to abort processing of this state
403
	# local script must set exit_status variable to nonzero.
404
	exit $exit_status
405
fi
(2-2/3)