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
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
delete_old_states() {
61
	$LOGGER "Starting delete_old_states()"
62
	_FLUSHED=0
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
			${PFCTL} -i $interface -Fs
70
			${PFCTL} -K ${OLD_IP}/32
71
			_FLUSHED=1
72
		fi
73
	fi
74
	# Delete states through old gateway if it's not the same
75
	OLD_ROUTER=""
76
	if [ -n "${old_routers}" ]; then
77
		OLD_ROUTER=$old_routers
78
	elif [ -f /tmp/${interface}_router ]; then
79
		OLD_ROUTER=`cat /tmp/${interface}_router`
80
	fi
81
	if [ ${_FLUSHED} -eq 0 -a -n "${OLD_ROUTER}" ]; then
82
		$LOGGER "Comparing Routers: Old: ${OLD_ROUTER} New: ${new_routers}"
83
		if [ "${OLD_ROUTER}" != "${new_routers}" ]; then
84
			$LOGGER "Removing states through old gateway '${OLD_ROUTER}' (new gateway '${new_routers}')"
85
			${PFCTL} -i $interface -Fs
86
		fi
87
	fi
88
}
89

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

    
95
add_new_address() {
96

    
97
	$LOGGER "Starting add_new_address()"
98

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

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

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

    
113

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

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

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

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

    
161
delete_old_routes() {
162
	$LOGGER "Deleting old routes"
163

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

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

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

    
191
	arp_flush
192
}
193

    
194
add_new_routes() {
195
	$LOGGER "Adding new routes to interface: $interface"
196

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

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

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

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

    
281
	return 0
282
}
283

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

    
289
#
290
# Start of active code.
291
#
292

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

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

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

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

    
327
ARPCHECK|ARPSEND)
328
	;;
329

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

    
367
EXPIRE|FAIL)
368
	delete_old_alias
369
	delete_old_states
370
	if [ -n "$old_ip_address" ]; then
371
		delete_old_address
372
		delete_old_routes
373
	fi
374
	;;
375

    
376
TIMEOUT)
377
	delete_old_alias
378
	add_new_address
379
	/bin/sleep 1
380
	if [ -n "$new_routers" ]; then
381
		$LOGGER "New Routers ($interface): $new_routers"
382
		set "$new_routers"
383
		if /sbin/ping -q -c 1 -t 1 "$1"; then
384
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
385
				add_new_alias
386
			fi
387
			add_new_routes
388
			if add_new_resolv_conf; then
389
				notify_rc_newwanip
390
			fi
391
		fi
392
	fi
393
	$IFCONFIG $interface inet -alias $new_ip_address $medium
394
	delete_old_states
395
	delete_old_routes
396
	;;
397
esac
398

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