Project

General

Profile

Download (11.1 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
			#  delete local route to the router ip address.
176
			#  cleans up our route to a gateway possibly outside of the assigned subnet
177
			$ROUTE delete -host $router -iface $interface
178

    
179
			$ROUTE delete default $router >/dev/null 2>&1
180
			/bin/rm -f /tmp/${interface}_router
181
		done
182
	fi
183

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

    
193
	arp_flush
194
}
195

    
196
add_new_routes() {
197
	$LOGGER "Adding new routes to interface: $interface"
198

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

    
222
	ADDED_ROUTE=no
223
	# Only allow the default route to be overridden if it's on our own interface
224
	if [ -f "/tmp/${interface}_defaultgw" ]; then
225
		$ROUTE delete default
226
		for router in $new_routers; do
227
			if [ "$new_ip_address" = "$router" -o "$router" = "255.255.255.255" ]; then
228
				$ROUTE add default -iface $interface
229
				echo $ROUTE add default -iface $interface | $LOGGER
230
				# NOTE: Do not activate this for all ones address since pf(4) will try to forward packets to it.
231
				if [ "$new_ip_address" = "$router" ]; then
232
					echo $router > /tmp/${interface}_router
233
				fi
234
			else
235
				#  add local route to the router ip address.
236
				#  this will not cause any harm if the router is within the subnet
237
				#  but it will prevent route troubles if the router is outside of the subnet
238
				#  this is useful for captive subnets or similar gateway out-of-subnet situations
239
				$ROUTE add -host $router -iface $interface
240
				echo $ROUTE add -host $router -iface $interface | $LOGGER
241

    
242
				$ROUTE add default $router
243
				echo $ROUTE add default $router | $LOGGER
244
				echo $router > /tmp/${interface}_router
245
			fi
246
			ADDED_ROUTE=yes
247
			# 2nd and subsequent default routers error out, so explicitly
248
			# stop processing the list after the first one.
249
			break
250
		done
251
	fi
252

    
253
	if [ -n "$new_static_routes" ]; then
254
		$LOGGER "New Static Routes ($interface): $new_static_routes"
255
		set $new_static_routes
256
		while [ $# -gt 1 ]; do
257
			$ROUTE add $1 $2
258
			if [ "$ADDED_ROUTE" = "no" ]; then
259
				echo $2 > /tmp/${interface}_router
260
			fi
261
			shift; shift
262
		done
263
	fi
264
}
265

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

    
289
	return 0
290
}
291

    
292
# Notify rc.newwanip of changes to an interface
293
notify_rc_newwanip() {
294
	/usr/local/sbin/pfSctl -c "interface newip $interface"
295
}
296

    
297
#
298
# Start of active code.
299
#
300

    
301
# Invoke the local dhcp client enter hooks, if they exist.
302
if [ -f /etc/dhclient-enter-hooks ]; then
303
	$LOGGER "dhclient-enter-hooks"
304
	exit_status=0
305
	. /etc/dhclient-enter-hooks
306
	# allow the local script to abort processing of this state
307
	# local script must set exit_status variable to nonzero.
308
	if [ $exit_status -ne 0 ]; then
309
		exit $exit_status
310
	fi
311
fi
312

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

    
321
PREINIT)
322
	delete_old_alias
323
	$IFCONFIG $interface inet 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
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 [ -n "$old_ip_address" ]; then
334
		if [ -n "$alias_ip_address" ] && \
335
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
336
			delete_old_alias
337
			changes="yes"
338
		fi
339
		if [ "$old_ip_address" != "$new_ip_address" ]; then
340
			delete_old_address
341
			delete_old_routes
342
			changes="yes"
343
		fi
344
	fi
345
	if [ "$reason" = BOUND ] || \
346
	   [ "$reason" = REBOOT ] || \
347
	   [ -z "$old_ip_address" ] || \
348
	   [ "$old_ip_address" != "$new_ip_address" ]; then
349
		add_new_address
350
		add_new_routes
351
		changes="yes"
352
	fi
353
	if [ -n "$alias_ip_address" ] && \
354
       [ "$new_ip_address" != "$alias_ip_address" ]; then
355
		add_new_alias
356
		changes="yes"
357
	fi
358
	add_new_resolv_conf
359
	if [ "$changes" = "yes" ] ; then
360
		notify_rc_newwanip
361
	fi
362
	;;
363

    
364
EXPIRE|FAIL|RELEASE)
365
	delete_old_alias
366
	if [ -n "$old_ip_address" ]; then
367
		delete_old_address
368
		delete_old_routes
369
	fi
370
	;;
371

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

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