Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Tag: | Revision:
1 8e4ab9fe Scott Ullrich
#!/bin/sh
2 4671d198 Scott Ullrich
# $Id$
3 b43ba51b Scott Ullrich
# $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 f968bb86 Scott Ullrich
ROUTE=/sbin/route
23 24003009 Scott Ullrich
SED=/usr/bin/sed
24
ARP=/usr/sbin/arp
25 4671d198 Scott Ullrich
IFCONFIG=/sbin/ifconfig
26 ff3da5db Renato Botelho
PFCTL=/sbin/pfctl
27 24003009 Scott Ullrich
28 b43ba51b Scott Ullrich
LOCALHOST=127.0.0.1
29 8e4ab9fe Scott Ullrich
30
if [ -x /usr/bin/logger ]; then
31 b43ba51b Scott Ullrich
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
32 8e4ab9fe Scott Ullrich
else
33 ff3da5db Renato Botelho
	LOGGER="echo"
34 8e4ab9fe Scott Ullrich
fi
35
36 b43ba51b Scott Ullrich
#
37
# Helper functions that implement common actions.
38
#
39
40
check_hostname() {
41 a63ce784 Scott Ullrich
	current_hostname=`$HOSTNAME`
42 2d34e81a Renato Botelho
	if [ -z "$current_hostname" ] || \
43
	   [ "$current_hostname" = "$old_host_name" -a \
44
	     "$new_hostname" != "$old_host_name" ]; then
45 a63ce784 Scott Ullrich
		$LOGGER "New Hostname ($interface): $new_host_name"
46
		$HOSTNAME $new_host_name
47
	fi
48 b43ba51b Scott Ullrich
}
49
50
arp_flush() {
51 24003009 Scott Ullrich
	$ARP -an -i $interface | \
52
		$SED -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
53
		/bin/sh >/dev/null 2>&1
54 b43ba51b Scott Ullrich
}
55
56 c75e8aed Chris Buechler
# 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 06498591 jim-p
delete_old_states() {
60 96f7a687 jim-p
	$LOGGER "Starting delete_old_states()"
61 ff3da5db Renato Botelho
	_FLUSHED=0
62 06498591 jim-p
	# If the IP changed, remove states from the old one
63
	if [ -f /var/db/${interface}_ip ]; then
64 ff3da5db Renato Botelho
		OLD_IP=`cat /var/db/${interface}_ip`
65 96f7a687 jim-p
		$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 ff3da5db Renato Botelho
			${PFCTL} -i $interface -Fs
69
			${PFCTL} -K ${OLD_IP}/32
70
			_FLUSHED=1
71 06498591 jim-p
		fi
72
	fi
73
	# Delete states through old gateway if it's not the same
74 ff3da5db Renato Botelho
	OLD_ROUTER=""
75 96f7a687 jim-p
	if [ -n "${old_routers}" ]; then
76 ff3da5db Renato Botelho
		OLD_ROUTER=$old_routers
77
	elif [ -f /tmp/${interface}_router ]; then
78
		OLD_ROUTER=`cat /tmp/${interface}_router`
79 96f7a687 jim-p
	fi
80 ff3da5db Renato Botelho
	if [ ${_FLUSHED} -eq 0 -a -n "${OLD_ROUTER}" ]; then
81 96f7a687 jim-p
		$LOGGER "Comparing Routers: Old: ${OLD_ROUTER} New: ${new_routers}"
82 06498591 jim-p
		if [ "${OLD_ROUTER}" != "${new_routers}" ]; then
83 96f7a687 jim-p
			$LOGGER "Removing states through old gateway '${OLD_ROUTER}' (new gateway '${new_routers}')"
84 ff3da5db Renato Botelho
			${PFCTL} -i $interface -Fs
85 06498591 jim-p
		fi
86
	fi
87
}
88
89 b43ba51b Scott Ullrich
delete_old_address() {
90 7afd6325 Ermal
	/bin/rm -f /var/db/${interface}_ip
91 4671d198 Scott Ullrich
	$IFCONFIG $interface inet -alias $old_ip_address $medium
92 b43ba51b Scott Ullrich
}
93
94
add_new_address() {
95 d6ee4ccf Scott Ullrich
96 b4a69e17 Scott Ullrich
	$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 4671d198 Scott Ullrich
	$IFCONFIG $interface \
101 b43ba51b Scott Ullrich
		inet $new_ip_address \
102
		netmask $new_subnet_mask \
103
		broadcast $new_broadcast_address \
104
		$medium
105 378574a3 jim-p
106 0b1f9973 Ermal
	$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 fe5869e4 Scott Ullrich
111 bd17f93d Ermal
112 69eefb50 Renato Botelho
	# This is necessary otherwise dpinger will try to ping all 1s address
113 96f7a687 jim-p
	if [ -n "$new_routers" ] && [ "$new_routers" != "255.255.255.255" ]; then
114 bd17f93d Ermal
		echo $new_routers > /tmp/${interface}_router
115
	fi
116 0c452870 Ermal
	echo $new_ip_address > /var/db/${interface}_ip
117 b43ba51b Scott Ullrich
}
118
119
delete_old_alias() {
120
	if [ -n "$alias_ip_address" ]; then
121 4671d198 Scott Ullrich
		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
122 f968bb86 Scott Ullrich
		$ROUTE delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
123 b43ba51b Scott Ullrich
	fi
124
}
125
126
add_new_alias() {
127
	if [ -n "$alias_ip_address" ]; then
128 4671d198 Scott Ullrich
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
129 b43ba51b Scott Ullrich
		    $alias_subnet_mask
130 f968bb86 Scott Ullrich
		$ROUTE add $alias_ip_address $LOCALHOST
131 b43ba51b Scott Ullrich
	fi
132
}
133
134 bacd881e Ermal
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 b43ba51b Scott Ullrich
delete_old_routes() {
160 b4a69e17 Scott Ullrich
	$LOGGER "Deleting old routes"
161 bacd881e Ermal
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 e1c8cdf5 Scott Ullrich
	# Only allow the default route to be overridden if it's on our own interface
173 04c528e7 Ermal
	if [ -f "/tmp/${interface}_defaultgw" ]; then
174 e1c8cdf5 Scott Ullrich
		for router in $old_routers; do
175 9fde2b2b Viktor G
			#  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 04c528e7 Ermal
			$ROUTE delete default $router >/dev/null 2>&1
180
			/bin/rm -f /tmp/${interface}_router
181 e1c8cdf5 Scott Ullrich
		done
182
	fi
183 b43ba51b Scott Ullrich
184
	if [ -n "$old_static_routes" ]; then
185
		set $old_static_routes
186
		while [ $# -gt 1 ]; do
187 f968bb86 Scott Ullrich
			$ROUTE delete "$1" "$2"
188 b43ba51b Scott Ullrich
			shift; shift
189 7d61beba Phil Davis
			/bin/rm -f /tmp/${interface}_router
190 b43ba51b Scott Ullrich
		done
191
	fi
192
193
	arp_flush
194
}
195
196
add_new_routes() {
197 924f202e Ermal
	$LOGGER "Adding new routes to interface: $interface"
198 e1c8cdf5 Scott Ullrich
199 bacd881e Ermal
	# 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 6cf1cc61 Ermal
	ADDED_ROUTE=no
223 e1c8cdf5 Scott Ullrich
	# Only allow the default route to be overridden if it's on our own interface
224 e311cb79 PiBa-NL
	if [ -f "/tmp/${interface}_defaultgw" ]; then
225 924f202e Ermal
		$ROUTE delete default
226 e1c8cdf5 Scott Ullrich
		for router in $new_routers; do
227 06d30ce7 Ermal
			if [ "$new_ip_address" = "$router" -o "$router" = "255.255.255.255" ]; then
228 924f202e Ermal
				$ROUTE add default -iface $interface
229
				echo $ROUTE add default -iface $interface | $LOGGER
230 5766add8 Ermal
				# 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 e1c8cdf5 Scott Ullrich
			else
235 9fde2b2b Viktor G
				#  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 e1c8cdf5 Scott Ullrich
				$ROUTE add default $router
243
				echo $ROUTE add default $router | $LOGGER
244 7d61beba Phil Davis
				echo $router > /tmp/${interface}_router
245 e1c8cdf5 Scott Ullrich
			fi
246 6cf1cc61 Ermal
			ADDED_ROUTE=yes
247 e1c8cdf5 Scott Ullrich
			# 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 b43ba51b Scott Ullrich
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 f968bb86 Scott Ullrich
			$ROUTE add $1 $2
258 6cf1cc61 Ermal
			if [ "$ADDED_ROUTE" = "no" ]; then
259 7d61beba Phil Davis
				echo $2 > /tmp/${interface}_router
260 6cf1cc61 Ermal
			fi
261 b43ba51b Scott Ullrich
			shift; shift
262
		done
263
	fi
264
}
265
266
add_new_resolv_conf() {
267 b4a69e17 Scott Ullrich
	$LOGGER "Creating resolv.conf"
268 0c452870 Ermal
	if [ -f "/var/etc/nameserver_$interface" ]; then
269 d6ee4ccf Scott Ullrich
		# Remove old entries
270
		for nameserver in `cat /var/etc/nameserver_$interface`; do
271 e5f3359c Ermal
			$ROUTE delete $nameserver >/dev/null 2>&1
272 d6ee4ccf Scott Ullrich
		done
273 0c452870 Ermal
	fi
274 7d61beba Phil Davis
	if [ -n "$new_domain_name_servers" ]; then
275 7afd6325 Ermal
		/bin/rm -f /var/etc/nameserver_$interface
276 a5c36eb2 Renato Botelho
		ALLOWOVERRIDE=$(/usr/local/sbin/read_xml_tag.sh boolean system/dnsallowoverride)
277 b43ba51b Scott Ullrich
		for nameserver in $new_domain_name_servers; do
278 d6ee4ccf Scott Ullrich
			# 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 a5c36eb2 Renato Botelho
			if [ "$ALLOWOVERRIDE" = "true" ]; then
282 cfe92577 Ermal
				echo $nameserver >>/var/etc/nameserver_$interface
283 978cf4fd Ermal
				$ROUTE add $nameserver -iface $interface
284 cfe92577 Ermal
			fi
285 b43ba51b Scott Ullrich
		done
286 4eb4b18a jim-p
		echo $new_domain_name >/var/etc/searchdomain_$interface
287 b43ba51b Scott Ullrich
	fi
288 fe5869e4 Scott Ullrich
289 aad37fd2 Jeb Campbell
	return 0
290 8e4ab9fe Scott Ullrich
}
291
292 d0d7f09a Scott Ullrich
# Notify rc.newwanip of changes to an interface
293
notify_rc_newwanip() {
294 7afd6325 Ermal
	/usr/local/sbin/pfSctl -c "interface newip $interface"
295 8e4ab9fe Scott Ullrich
}
296
297 b43ba51b Scott Ullrich
#
298
# Start of active code.
299
#
300
301 8e4ab9fe Scott Ullrich
# Invoke the local dhcp client enter hooks, if they exist.
302
if [ -f /etc/dhclient-enter-hooks ]; then
303 b4a69e17 Scott Ullrich
	$LOGGER "dhclient-enter-hooks"
304 b43ba51b Scott Ullrich
	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 8e4ab9fe Scott Ullrich
fi
312
313 d0d7f09a Scott Ullrich
$LOGGER $reason
314 b43ba51b Scott Ullrich
case $reason in
315
MEDIUM)
316 4671d198 Scott Ullrich
	$IFCONFIG $interface $medium
317
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
318 24003009 Scott Ullrich
	/bin/sleep 1
319 b43ba51b Scott Ullrich
	;;
320 8e4ab9fe Scott Ullrich
321 b43ba51b Scott Ullrich
PREINIT)
322
	delete_old_alias
323 a9b305a8 Chris Buechler
	$IFCONFIG $interface inet 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
324 b4a69e17 Scott Ullrich
	/bin/rm -f /tmp/${interface}_router
325 b43ba51b Scott Ullrich
	;;
326 8e4ab9fe Scott Ullrich
327 b43ba51b Scott Ullrich
ARPCHECK|ARPSEND)
328
	;;
329 8e4ab9fe Scott Ullrich
330 b43ba51b Scott Ullrich
BOUND|RENEW|REBIND|REBOOT)
331
	check_hostname
332 7afd6325 Ermal
	changes="no"
333 b43ba51b Scott Ullrich
	if [ -n "$old_ip_address" ]; then
334 d0d7f09a Scott Ullrich
		if [ -n "$alias_ip_address" ] && \
335
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
336 b43ba51b Scott Ullrich
			delete_old_alias
337 7afd6325 Ermal
			changes="yes"
338 b43ba51b Scott Ullrich
		fi
339
		if [ "$old_ip_address" != "$new_ip_address" ]; then
340 80c043fa jim-p
			delete_old_address
341 b43ba51b Scott Ullrich
			delete_old_routes
342 7afd6325 Ermal
			changes="yes"
343 b43ba51b Scott Ullrich
		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 7afd6325 Ermal
		changes="yes"
352 b43ba51b Scott Ullrich
	fi
353 d0d7f09a Scott Ullrich
	if [ -n "$alias_ip_address" ] && \
354
       [ "$new_ip_address" != "$alias_ip_address" ]; then
355 b43ba51b Scott Ullrich
		add_new_alias
356 7afd6325 Ermal
		changes="yes"
357 b43ba51b Scott Ullrich
	fi
358
	add_new_resolv_conf
359 d0d7f09a Scott Ullrich
	if [ "$changes" = "yes" ] ; then
360
		notify_rc_newwanip
361
	fi
362 b43ba51b Scott Ullrich
	;;
363 8e4ab9fe Scott Ullrich
364 718432f1 NOYB
EXPIRE|FAIL|RELEASE)
365 b43ba51b Scott Ullrich
	delete_old_alias
366
	if [ -n "$old_ip_address" ]; then
367 80c043fa jim-p
		delete_old_address
368 b43ba51b Scott Ullrich
		delete_old_routes
369
	fi
370
	;;
371 8e4ab9fe Scott Ullrich
372 b43ba51b Scott Ullrich
TIMEOUT)
373
	delete_old_alias
374
	add_new_address
375 24003009 Scott Ullrich
	/bin/sleep 1
376 b43ba51b Scott Ullrich
	if [ -n "$new_routers" ]; then
377
		$LOGGER "New Routers ($interface): $new_routers"
378
		set "$new_routers"
379 bb92b70f Ermal
		if /sbin/ping -q -c 1 -t 1 "$1"; then
380 b43ba51b Scott Ullrich
			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 d0d7f09a Scott Ullrich
				notify_rc_newwanip
386 b43ba51b Scott Ullrich
			fi
387
		fi
388
	fi
389 4671d198 Scott Ullrich
	$IFCONFIG $interface inet -alias $new_ip_address $medium
390 b43ba51b Scott Ullrich
	delete_old_routes
391
	;;
392
esac
393 8e4ab9fe Scott Ullrich
394 d0d7f09a Scott Ullrich
# 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