Project

General

Profile

Download (10.5 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
HOSTNAME=/bin/hostname
22
ROUTE=/sbin/route
23
SED=/usr/bin/sed
24
ARP=/usr/sbin/arp
25
IFCONFIG=/sbin/ifconfig
26
PFCTL=/sbin/pfctl
27

    
28
LOCALHOST=127.0.0.1
29

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

    
36
#
37
# Helper functions that implement common actions.
38
#
39

    
40
check_hostname() {
41
	current_hostname=`$HOSTNAME`
42
	if [ -z "$current_hostname" ] || \
43
	   [ "$current_hostname" = "$old_host_name" -a \
44
	     "$new_hostname" != "$old_host_name" ]; then
45
		$LOGGER "New Hostname ($interface): $new_host_name"
46
		$HOSTNAME $new_host_name
47
	fi
48
}
49

    
50
arp_flush() {
51
	$ARP -an -i $interface | \
52
		$SED -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
53
		/bin/sh >/dev/null 2>&1
54
}
55

    
56
# NOTE: use of the below has been disabled because rc.newwanip handles this correctly and this
57
# unnecessarily killed states in multiple circumstances. Leaving here for now, should be safe
58
# to remove later.  -cmb 20141105
59
delete_old_states() {
60
	$LOGGER "Starting delete_old_states()"
61
	_FLUSHED=0
62
	# If the IP changed, remove states from the old one
63
	if [ -f /var/db/${interface}_ip ]; then
64
		OLD_IP=`cat /var/db/${interface}_ip`
65
		$LOGGER "Comparing IPs: Old: ${OLD_IP} New: ${new_ip_address}"
66
		if [ -n "${OLD_IP}" ] && [ "${OLD_IP}" != "${new_ip_address}" ]; then
67
			$LOGGER "Removing states from old IP '${OLD_IP}' (new IP '${new_ip_address}')"
68
			${PFCTL} -i $interface -Fs
69
			${PFCTL} -K ${OLD_IP}/32
70
			_FLUSHED=1
71
		fi
72
	fi
73
	# Delete states through old gateway if it's not the same
74
	OLD_ROUTER=""
75
	if [ -n "${old_routers}" ]; then
76
		OLD_ROUTER=$old_routers
77
	elif [ -f /tmp/${interface}_router ]; then
78
		OLD_ROUTER=`cat /tmp/${interface}_router`
79
	fi
80
	if [ ${_FLUSHED} -eq 0 -a -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
			${PFCTL} -i $interface -Fs
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 dpinger 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
	# Only allow the default route to be overridden if it's on our own interface
220
	if [ -f "/tmp/${interface}_defaultgw" ]; then
221
		$ROUTE delete default
222
		for router in $new_routers; do
223
			if [ "$new_ip_address" = "$router" -o "$router" = "255.255.255.255" ]; then
224
				$ROUTE add default -iface $interface
225
				echo $ROUTE add default -iface $interface | $LOGGER
226
				# NOTE: Do not activate this for all ones address since pf(4) will try to forward packets to it.
227
				if [ "$new_ip_address" = "$router" ]; then
228
					echo $router > /tmp/${interface}_router
229
				fi
230
			else
231
				$ROUTE add default $router
232
				echo $ROUTE add default $router | $LOGGER
233
				echo $router > /tmp/${interface}_router
234
			fi
235
			ADDED_ROUTE=yes
236
			# 2nd and subsequent default routers error out, so explicitly
237
			# stop processing the list after the first one.
238
			break
239
		done
240
	fi
241

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

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

    
278
	return 0
279
}
280

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

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

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

    
302
$LOGGER $reason
303
case $reason in
304
MEDIUM)
305
	$IFCONFIG $interface $medium
306
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
307
	/bin/sleep 1
308
	;;
309

    
310
PREINIT)
311
	delete_old_alias
312
	$IFCONFIG $interface inet 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
313
	/bin/rm -f /tmp/${interface}_router
314
	;;
315

    
316
ARPCHECK|ARPSEND)
317
	;;
318

    
319
BOUND|RENEW|REBIND|REBOOT)
320
	check_hostname
321
	changes="no"
322
	if [ -n "$old_ip_address" ]; then
323
		if [ -n "$alias_ip_address" ] && \
324
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
325
			delete_old_alias
326
			changes="yes"
327
		fi
328
		if [ "$old_ip_address" != "$new_ip_address" ]; then
329
			delete_old_address
330
			delete_old_routes
331
			changes="yes"
332
		fi
333
	fi
334
	if [ "$reason" = BOUND ] || \
335
	   [ "$reason" = REBOOT ] || \
336
	   [ -z "$old_ip_address" ] || \
337
	   [ "$old_ip_address" != "$new_ip_address" ]; then
338
		add_new_address
339
		add_new_routes
340
		changes="yes"
341
	fi
342
	if [ -n "$alias_ip_address" ] && \
343
       [ "$new_ip_address" != "$alias_ip_address" ]; then
344
		add_new_alias
345
		changes="yes"
346
	fi
347
	add_new_resolv_conf
348
	if [ "$changes" = "yes" ] ; then
349
		notify_rc_newwanip
350
	fi
351
	;;
352

    
353
EXPIRE|FAIL|RELEASE)
354
	delete_old_alias
355
	if [ -n "$old_ip_address" ]; then
356
		delete_old_address
357
		delete_old_routes
358
	fi
359
	;;
360

    
361
TIMEOUT)
362
	delete_old_alias
363
	add_new_address
364
	/bin/sleep 1
365
	if [ -n "$new_routers" ]; then
366
		$LOGGER "New Routers ($interface): $new_routers"
367
		set "$new_routers"
368
		if /sbin/ping -q -c 1 -t 1 "$1"; then
369
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
370
				add_new_alias
371
			fi
372
			add_new_routes
373
			if add_new_resolv_conf; then
374
				notify_rc_newwanip
375
			fi
376
		fi
377
	fi
378
	$IFCONFIG $interface inet -alias $new_ip_address $medium
379
	delete_old_routes
380
	;;
381
esac
382

    
383
# Invoke the local dhcp client exit hooks, if they exist.
384
if [ -f /etc/dhclient-exit-hooks ]; then
385
	$LOGGER "dhclient-exit-hooks"
386
	exit_status=0
387
	. /etc/dhclient-exit-hooks
388
	# allow the local script to abort processing of this state
389
	# local script must set exit_status variable to nonzero.
390
	exit $exit_status
391
fi
(12-12/29)