Project

General

Profile

Download (10.6 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 04c528e7 Ermal
			$ROUTE delete default $router >/dev/null 2>&1
176
			/bin/rm -f /tmp/${interface}_router
177 e1c8cdf5 Scott Ullrich
		done
178
	fi
179 b43ba51b Scott Ullrich
180
	if [ -n "$old_static_routes" ]; then
181
		set $old_static_routes
182
		while [ $# -gt 1 ]; do
183 f968bb86 Scott Ullrich
			$ROUTE delete "$1" "$2"
184 b43ba51b Scott Ullrich
			shift; shift
185 7d61beba Phil Davis
			/bin/rm -f /tmp/${interface}_router
186 b43ba51b Scott Ullrich
		done
187
	fi
188
189
	arp_flush
190
}
191
192
add_new_routes() {
193 924f202e Ermal
	$LOGGER "Adding new routes to interface: $interface"
194 e1c8cdf5 Scott Ullrich
195 bacd881e Ermal
	# 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 6cf1cc61 Ermal
	ADDED_ROUTE=no
219 c90ba62d Ermal
	EXISTSGW=`/bin/ls -l /tmp/*_defaultgw | /usr/bin/wc -l`
220 e1c8cdf5 Scott Ullrich
	# Only allow the default route to be overridden if it's on our own interface
221 c90ba62d Ermal
	if [ -f "/tmp/${interface}_defaultgw" -o $EXISTSGW -eq 0 ]; then
222 924f202e Ermal
		$ROUTE delete default
223 e1c8cdf5 Scott Ullrich
		for router in $new_routers; do
224 06d30ce7 Ermal
			if [ "$new_ip_address" = "$router" -o "$router" = "255.255.255.255" ]; then
225 924f202e Ermal
				$ROUTE add default -iface $interface
226
				echo $ROUTE add default -iface $interface | $LOGGER
227 5766add8 Ermal
				# 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 e1c8cdf5 Scott Ullrich
			else
232
				$ROUTE add default $router
233
				echo $ROUTE add default $router | $LOGGER
234 7d61beba Phil Davis
				echo $router > /tmp/${interface}_router
235 e1c8cdf5 Scott Ullrich
			fi
236 6cf1cc61 Ermal
			ADDED_ROUTE=yes
237 e1c8cdf5 Scott Ullrich
			# 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 b43ba51b Scott Ullrich
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 f968bb86 Scott Ullrich
			$ROUTE add $1 $2
248 6cf1cc61 Ermal
			if [ "$ADDED_ROUTE" = "no" ]; then
249 7d61beba Phil Davis
				echo $2 > /tmp/${interface}_router
250 6cf1cc61 Ermal
			fi
251 b43ba51b Scott Ullrich
			shift; shift
252
		done
253
	fi
254
}
255
256
add_new_resolv_conf() {
257 b4a69e17 Scott Ullrich
	$LOGGER "Creating resolv.conf"
258 0c452870 Ermal
	if [ -f "/var/etc/nameserver_$interface" ]; then
259 d6ee4ccf Scott Ullrich
		# Remove old entries
260
		for nameserver in `cat /var/etc/nameserver_$interface`; do
261 e5f3359c Ermal
			$ROUTE delete $nameserver >/dev/null 2>&1
262 d6ee4ccf Scott Ullrich
		done
263 0c452870 Ermal
	fi
264 7d61beba Phil Davis
	if [ -n "$new_domain_name_servers" ]; then
265 7afd6325 Ermal
		/bin/rm -f /var/etc/nameserver_$interface
266 a5c36eb2 Renato Botelho
		ALLOWOVERRIDE=$(/usr/local/sbin/read_xml_tag.sh boolean system/dnsallowoverride)
267 b43ba51b Scott Ullrich
		for nameserver in $new_domain_name_servers; do
268 d6ee4ccf Scott Ullrich
			# 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 a5c36eb2 Renato Botelho
			if [ "$ALLOWOVERRIDE" = "true" ]; then
272 cfe92577 Ermal
				echo $nameserver >>/var/etc/nameserver_$interface
273 978cf4fd Ermal
				$ROUTE add $nameserver -iface $interface
274 cfe92577 Ermal
			fi
275 b43ba51b Scott Ullrich
		done
276 4eb4b18a jim-p
		echo $new_domain_name >/var/etc/searchdomain_$interface
277 b43ba51b Scott Ullrich
	fi
278 fe5869e4 Scott Ullrich
279 aad37fd2 Jeb Campbell
	return 0
280 8e4ab9fe Scott Ullrich
}
281
282 d0d7f09a Scott Ullrich
# Notify rc.newwanip of changes to an interface
283
notify_rc_newwanip() {
284 7afd6325 Ermal
	/usr/local/sbin/pfSctl -c "interface newip $interface"
285 8e4ab9fe Scott Ullrich
}
286
287 b43ba51b Scott Ullrich
#
288
# Start of active code.
289
#
290
291 8e4ab9fe Scott Ullrich
# Invoke the local dhcp client enter hooks, if they exist.
292
if [ -f /etc/dhclient-enter-hooks ]; then
293 b4a69e17 Scott Ullrich
	$LOGGER "dhclient-enter-hooks"
294 b43ba51b Scott Ullrich
	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 8e4ab9fe Scott Ullrich
fi
302
303 d0d7f09a Scott Ullrich
$LOGGER $reason
304 b43ba51b Scott Ullrich
case $reason in
305
MEDIUM)
306 4671d198 Scott Ullrich
	$IFCONFIG $interface $medium
307
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
308 24003009 Scott Ullrich
	/bin/sleep 1
309 b43ba51b Scott Ullrich
	;;
310 8e4ab9fe Scott Ullrich
311 b43ba51b Scott Ullrich
PREINIT)
312
	delete_old_alias
313 a9b305a8 Chris Buechler
	$IFCONFIG $interface inet 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
314 b4a69e17 Scott Ullrich
	/bin/rm -f /tmp/${interface}_router
315 b43ba51b Scott Ullrich
	;;
316 8e4ab9fe Scott Ullrich
317 b43ba51b Scott Ullrich
ARPCHECK|ARPSEND)
318
	;;
319 8e4ab9fe Scott Ullrich
320 b43ba51b Scott Ullrich
BOUND|RENEW|REBIND|REBOOT)
321
	check_hostname
322 7afd6325 Ermal
	changes="no"
323 b43ba51b Scott Ullrich
	if [ -n "$old_ip_address" ]; then
324 d0d7f09a Scott Ullrich
		if [ -n "$alias_ip_address" ] && \
325
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
326 b43ba51b Scott Ullrich
			delete_old_alias
327 7afd6325 Ermal
			changes="yes"
328 b43ba51b Scott Ullrich
		fi
329
		if [ "$old_ip_address" != "$new_ip_address" ]; then
330 80c043fa jim-p
			delete_old_address
331 b43ba51b Scott Ullrich
			delete_old_routes
332 7afd6325 Ermal
			changes="yes"
333 b43ba51b Scott Ullrich
		fi
334
	fi
335
	if [ "$reason" = BOUND ] || \
336
	   [ "$reason" = REBOOT ] || \
337
	   [ -z "$old_ip_address" ] || \
338
	   [ "$old_ip_address" != "$new_ip_address" ]; then
339
		add_new_address
340
		add_new_routes
341 7afd6325 Ermal
		changes="yes"
342 b43ba51b Scott Ullrich
	fi
343 d0d7f09a Scott Ullrich
	if [ -n "$alias_ip_address" ] && \
344
       [ "$new_ip_address" != "$alias_ip_address" ]; then
345 b43ba51b Scott Ullrich
		add_new_alias
346 7afd6325 Ermal
		changes="yes"
347 b43ba51b Scott Ullrich
	fi
348
	add_new_resolv_conf
349 d0d7f09a Scott Ullrich
	if [ "$changes" = "yes" ] ; then
350
		notify_rc_newwanip
351
	fi
352 b43ba51b Scott Ullrich
	;;
353 8e4ab9fe Scott Ullrich
354 718432f1 NOYB
EXPIRE|FAIL|RELEASE)
355 b43ba51b Scott Ullrich
	delete_old_alias
356
	if [ -n "$old_ip_address" ]; then
357 80c043fa jim-p
		delete_old_address
358 b43ba51b Scott Ullrich
		delete_old_routes
359
	fi
360
	;;
361 8e4ab9fe Scott Ullrich
362 b43ba51b Scott Ullrich
TIMEOUT)
363
	delete_old_alias
364
	add_new_address
365 24003009 Scott Ullrich
	/bin/sleep 1
366 b43ba51b Scott Ullrich
	if [ -n "$new_routers" ]; then
367
		$LOGGER "New Routers ($interface): $new_routers"
368
		set "$new_routers"
369 bb92b70f Ermal
		if /sbin/ping -q -c 1 -t 1 "$1"; then
370 b43ba51b Scott Ullrich
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
371
				add_new_alias
372
			fi
373
			add_new_routes
374
			if add_new_resolv_conf; then
375 d0d7f09a Scott Ullrich
				notify_rc_newwanip
376 b43ba51b Scott Ullrich
			fi
377
		fi
378
	fi
379 4671d198 Scott Ullrich
	$IFCONFIG $interface inet -alias $new_ip_address $medium
380 b43ba51b Scott Ullrich
	delete_old_routes
381
	;;
382
esac
383 8e4ab9fe Scott Ullrich
384 d0d7f09a Scott Ullrich
# Invoke the local dhcp client exit hooks, if they exist.
385
if [ -f /etc/dhclient-exit-hooks ]; then
386
	$LOGGER "dhclient-exit-hooks"
387
	exit_status=0
388
	. /etc/dhclient-exit-hooks
389
	# allow the local script to abort processing of this state
390
	# local script must set exit_status variable to nonzero.
391
	exit $exit_status
392
fi