Project

General

Profile

Download (9.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
NETSTAT=/usr/bin/netstat
22
GREP=/usr/bin/grep
23
AWK=/usr/bin/awk
24
HOSTNAME=/bin/hostname
25 ea0c6522 Scott Ullrich
GREP=/usr/bin/grep
26 f968bb86 Scott Ullrich
ROUTE=/sbin/route
27 24003009 Scott Ullrich
SED=/usr/bin/sed
28
ARP=/usr/sbin/arp
29 4671d198 Scott Ullrich
IFCONFIG=/sbin/ifconfig
30 24003009 Scott Ullrich
31 b43ba51b Scott Ullrich
LOCALHOST=127.0.0.1
32 8e4ab9fe Scott Ullrich
33
if [ -x /usr/bin/logger ]; then
34 b43ba51b Scott Ullrich
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
35 8e4ab9fe Scott Ullrich
else
36 b43ba51b Scott Ullrich
	LOGGER=echo
37 8e4ab9fe Scott Ullrich
fi
38
39 b43ba51b Scott Ullrich
#
40
# Helper functions that implement common actions.
41
#
42
43
check_hostname() {
44 a63ce784 Scott Ullrich
	current_hostname=`$HOSTNAME`
45
	if [ -z "$current_hostname" ]; then
46
		$LOGGER "New Hostname ($interface): $new_host_name"
47
		$HOSTNAME $new_host_name
48
	elif [ "$current_hostname" = "$old_host_name" -a \
49
	       "$new_host_name" != "$old_host_name" ]; then
50
		$LOGGER "New Hostname ($interface): $new_host_name"
51
		$HOSTNAME $new_host_name
52
	fi
53 b43ba51b Scott Ullrich
}
54
55
arp_flush() {
56 24003009 Scott Ullrich
	$ARP -an -i $interface | \
57
		$SED -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
58
		/bin/sh >/dev/null 2>&1
59 b43ba51b Scott Ullrich
}
60
61
delete_old_address() {
62 7afd6325 Ermal
	/bin/rm -f /var/db/${interface}_ip
63 4671d198 Scott Ullrich
	$IFCONFIG $interface inet -alias $old_ip_address $medium
64 b43ba51b Scott Ullrich
}
65
66
add_new_address() {
67 d6ee4ccf Scott Ullrich
68 b4a69e17 Scott Ullrich
	$LOGGER "Starting add_new_address()"
69
70
	$LOGGER "ifconfig $interface inet $new_ip_address netmask $new_subnet_mask broadcast $new_broadcast_address $medium"
71
72 4671d198 Scott Ullrich
	$IFCONFIG $interface \
73 b43ba51b Scott Ullrich
		inet $new_ip_address \
74
		netmask $new_subnet_mask \
75
		broadcast $new_broadcast_address \
76
		$medium
77
78 b4a69e17 Scott Ullrich
		$LOGGER "New IP Address ($interface): $new_ip_address"
79
		$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
80
		$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
81
		$LOGGER "New Routers ($interface): $new_routers"
82 fe5869e4 Scott Ullrich
83 b43ba51b Scott Ullrich
        echo $new_routers > /tmp/${interface}_router
84 0c452870 Ermal
	echo $new_ip_address > /var/db/${interface}_ip
85 b43ba51b Scott Ullrich
}
86
87
delete_old_alias() {
88
	if [ -n "$alias_ip_address" ]; then
89 4671d198 Scott Ullrich
		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
90 f968bb86 Scott Ullrich
		$ROUTE delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
91 b43ba51b Scott Ullrich
	fi
92
}
93
94
add_new_alias() {
95
	if [ -n "$alias_ip_address" ]; then
96 4671d198 Scott Ullrich
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
97 b43ba51b Scott Ullrich
		    $alias_subnet_mask
98 f968bb86 Scott Ullrich
		$ROUTE add $alias_ip_address $LOCALHOST
99 b43ba51b Scott Ullrich
	fi
100
}
101
102 bacd881e Ermal
fill_classless_routes() {
103
	set $1
104
	while [ $# -ge 5 ]; do
105
		if [ $1 -eq 0 ]; then
106
			route="default"
107
		elif [ $1 -le 8 ]; then
108
			route="$2.0.0.0/$1"
109
			shift
110
		elif [ $1 -le 16 ]; then
111
			route="$2.$3.0.0/$1"
112
			shift; shift
113
		elif [ $1 -le 24 ]; then
114
			route="$2.$3.$4.0/$1"
115
			shift; shift; shift
116
		else
117
			route="$2.$3.$4.$5/$1"
118
			shift; shift; shift; shift
119
		fi
120
		shift
121
		router="$1.$2.$3.$4"
122
		classless_routes="$classless_routes $route $router"
123
		shift; shift; shift; shift
124
	done
125
}
126
127 b43ba51b Scott Ullrich
delete_old_routes() {
128 b4a69e17 Scott Ullrich
	$LOGGER "Deleting old routes"
129 bacd881e Ermal
130
	if [ -n "$old_classless_routes" ]; then
131
		fill_classless_routes "$old_classless_routes"
132
		set $classless_routes
133
		while [ $# -gt 1 ]; do
134
			route delete "$1" "$2"
135
			shift; shift
136
		done
137
		return 0;
138
	fi
139
140 e1c8cdf5 Scott Ullrich
	# Only allow the default route to be overridden if it's on our own interface
141 04c528e7 Ermal
	if [ -f "/tmp/${interface}_defaultgw" ]; then
142 e1c8cdf5 Scott Ullrich
		for router in $old_routers; do
143 04c528e7 Ermal
			$ROUTE delete default $router >/dev/null 2>&1
144
			/bin/rm -f /tmp/${interface}_router
145 e1c8cdf5 Scott Ullrich
		done
146
	fi
147 b43ba51b Scott Ullrich
148
	if [ -n "$old_static_routes" ]; then
149
		set $old_static_routes
150
		while [ $# -gt 1 ]; do
151 f968bb86 Scott Ullrich
			$ROUTE delete "$1" "$2"
152 b43ba51b Scott Ullrich
			shift; shift
153 4671d198 Scott Ullrich
                        /bin/rm -f /tmp/${interface}_router
154 b43ba51b Scott Ullrich
		done
155
	fi
156
157
	arp_flush
158
}
159
160
add_new_routes() {
161 924f202e Ermal
	$LOGGER "Adding new routes to interface: $interface"
162 e1c8cdf5 Scott Ullrich
163 bacd881e Ermal
	# RFC 3442: If the DHCP server returns both a Classless Static
164
	# Routes option and a Router option, the DHCP client MUST ignore
165
	# the Router option.
166
	#
167
	# DHCP clients that support this option (Classless Static Routes)
168
	# MUST NOT install the routes specified in the Static Routes
169
	# option (option code 33) if both a Static Routes option and the
170
	# Classless Static Routes option are provided.
171
	if [ -n "$new_classless_routes" ]; then
172
		fill_classless_routes "$new_classless_routes"
173
		$LOGGER "New Classless Static Routes ($interface): $classless_routes"
174
		set $classless_routes
175
		while [ $# -gt 1 ]; do
176
			if [ "0.0.0.0" = "$2" ]; then
177
				route add "$1" -iface "$interface"
178
			else
179
				route add "$1" "$2"
180
			fi
181
			shift; shift
182
		done
183
		return
184
	fi
185
186 6cf1cc61 Ermal
	ADDED_ROUTE=no
187 c90ba62d Ermal
	EXISTSGW=`/bin/ls -l /tmp/*_defaultgw | /usr/bin/wc -l`
188 e1c8cdf5 Scott Ullrich
	# Only allow the default route to be overridden if it's on our own interface
189 c90ba62d Ermal
	if [ -f "/tmp/${interface}_defaultgw" -o $EXISTSGW -eq 0 ]; then
190 924f202e Ermal
		$ROUTE delete default
191 e1c8cdf5 Scott Ullrich
		for router in $new_routers; do
192 06d30ce7 Ermal
			if [ "$new_ip_address" = "$router" -o "$router" = "255.255.255.255" ]; then
193 924f202e Ermal
				$ROUTE add default -iface $interface
194
				echo $ROUTE add default -iface $interface | $LOGGER
195 5766add8 Ermal
				# NOTE: Do not activate this for all ones address since pf(4) will try to forward packets to it.
196
				if [ "$new_ip_address" = "$router" ]; then
197
					echo $router > /tmp/${interface}_router
198
				fi
199 e1c8cdf5 Scott Ullrich
			else
200
				$ROUTE add default $router
201
				echo $ROUTE add default $router | $LOGGER
202 924f202e Ermal
                       		echo $router > /tmp/${interface}_router
203 e1c8cdf5 Scott Ullrich
			fi
204 6cf1cc61 Ermal
			ADDED_ROUTE=yes
205 e1c8cdf5 Scott Ullrich
			# 2nd and subsequent default routers error out, so explicitly
206
			# stop processing the list after the first one.
207
			break
208
		done
209
	fi
210 b43ba51b Scott Ullrich
211
	if [ -n "$new_static_routes" ]; then
212
		$LOGGER "New Static Routes ($interface): $new_static_routes"
213
		set $new_static_routes
214
		while [ $# -gt 1 ]; do
215 f968bb86 Scott Ullrich
			$ROUTE add $1 $2
216 6cf1cc61 Ermal
			if [ "$ADDED_ROUTE" = "no" ]; then
217
                        	echo $2 > /tmp/${interface}_router
218
			fi
219 b43ba51b Scott Ullrich
			shift; shift
220
		done
221
	fi
222
}
223
224
add_new_resolv_conf() {
225 b4a69e17 Scott Ullrich
	$LOGGER "Creating resolv.conf"
226 0c452870 Ermal
	if [ -f "/var/etc/nameserver_$interface" ]; then
227 d6ee4ccf Scott Ullrich
		# Remove old entries
228
		for nameserver in `cat /var/etc/nameserver_$interface`; do
229 1fb05961 Ermal
			$ROUTE delete $nameserver
230 d6ee4ccf Scott Ullrich
		done
231 0c452870 Ermal
	fi
232 9d1f614c Ermal
	if [ -n "$new_domain_name_servers" ]; then 
233 7afd6325 Ermal
		/bin/rm -f /var/etc/nameserver_$interface
234 b43ba51b Scott Ullrich
		for nameserver in $new_domain_name_servers; do
235 955f2d78 Scott Ullrich
			nameserver_sanitized=`echo '$nameserver' | egrep -o '([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[0-9a-f]+:)'`
236 d6ee4ccf Scott Ullrich
			# Add a route to the nameserver out the correct interface
237
			# so that mulitple wans work correctly with multiple dns
238
			# also backup the nameserver for later route removal
239 955f2d78 Scott Ullrich
			echo '$nameserver_sanitized' >>/var/etc/nameserver_$interface
240
			$ROUTE add $nameserver_sanitized -iface $interface
241 b43ba51b Scott Ullrich
		done
242 d0cc727e Scott Ullrich
		echo $new_domain_name | /usr/bin/egrep -o "[0-9\.]+" >/var/etc/searchdomain_$interface
243 b43ba51b Scott Ullrich
	fi
244 fe5869e4 Scott Ullrich
245 aad37fd2 Jeb Campbell
	return 0
246 8e4ab9fe Scott Ullrich
}
247
248 d0d7f09a Scott Ullrich
# Notify rc.newwanip of changes to an interface
249
notify_rc_newwanip() {
250 7afd6325 Ermal
	/usr/local/sbin/pfSctl -c "interface newip $interface"
251 8e4ab9fe Scott Ullrich
}
252
253 b43ba51b Scott Ullrich
#
254
# Start of active code.
255
#
256
257 8e4ab9fe Scott Ullrich
# Invoke the local dhcp client enter hooks, if they exist.
258
if [ -f /etc/dhclient-enter-hooks ]; then
259 b4a69e17 Scott Ullrich
	$LOGGER "dhclient-enter-hooks"
260 b43ba51b Scott Ullrich
	exit_status=0
261
	. /etc/dhclient-enter-hooks
262
	# allow the local script to abort processing of this state
263
	# local script must set exit_status variable to nonzero.
264
	if [ $exit_status -ne 0 ]; then
265
		exit $exit_status
266
	fi
267 8e4ab9fe Scott Ullrich
fi
268
269 409dc2e1 Ermal
#if [ -x $ROUTE ]; then
270
#	if_defaultroute=`$ROUTE -n get -inet default | $GREP interface | $AWK '{print $2}'`
271
#else
272
#	$LOGGER "if_defaultroute"
273
#	if_defaultroute="x"
274
#fi
275 8e4ab9fe Scott Ullrich
276 d0d7f09a Scott Ullrich
$LOGGER $reason
277 b43ba51b Scott Ullrich
case $reason in
278
MEDIUM)
279 4671d198 Scott Ullrich
	$IFCONFIG $interface $medium
280
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
281 24003009 Scott Ullrich
	/bin/sleep 1
282 b43ba51b Scott Ullrich
	;;
283 8e4ab9fe Scott Ullrich
284 b43ba51b Scott Ullrich
PREINIT)
285
	delete_old_alias
286 4671d198 Scott Ullrich
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
287 b4a69e17 Scott Ullrich
	/bin/rm -f /tmp/${interface}_router
288 b43ba51b Scott Ullrich
	;;
289 8e4ab9fe Scott Ullrich
290 b43ba51b Scott Ullrich
ARPCHECK|ARPSEND)
291
	;;
292 8e4ab9fe Scott Ullrich
293 b43ba51b Scott Ullrich
BOUND|RENEW|REBIND|REBOOT)
294
	check_hostname
295 7afd6325 Ermal
	changes="no"
296 b43ba51b Scott Ullrich
	if [ -n "$old_ip_address" ]; then
297 d0d7f09a Scott Ullrich
		if [ -n "$alias_ip_address" ] && \
298
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
299 b43ba51b Scott Ullrich
			delete_old_alias
300 7afd6325 Ermal
			changes="yes"
301 b43ba51b Scott Ullrich
		fi
302
		if [ "$old_ip_address" != "$new_ip_address" ]; then
303
			delete_old_address
304
			delete_old_routes
305 7afd6325 Ermal
			changes="yes"
306 b43ba51b Scott Ullrich
		fi
307
	fi
308
	if [ "$reason" = BOUND ] || \
309
	   [ "$reason" = REBOOT ] || \
310
	   [ -z "$old_ip_address" ] || \
311
	   [ "$old_ip_address" != "$new_ip_address" ]; then
312
		add_new_address
313
		add_new_routes
314 7afd6325 Ermal
		changes="yes"
315 b43ba51b Scott Ullrich
	fi
316 d0d7f09a Scott Ullrich
	if [ -n "$alias_ip_address" ] && \
317
       [ "$new_ip_address" != "$alias_ip_address" ]; then
318 b43ba51b Scott Ullrich
		add_new_alias
319 7afd6325 Ermal
		changes="yes"
320 b43ba51b Scott Ullrich
	fi
321
	add_new_resolv_conf
322 d0d7f09a Scott Ullrich
	if [ "$changes" = "yes" ] ; then
323
		notify_rc_newwanip
324
	fi
325 b43ba51b Scott Ullrich
	;;
326 8e4ab9fe Scott Ullrich
327 b43ba51b Scott Ullrich
EXPIRE|FAIL)
328
	delete_old_alias
329
	if [ -n "$old_ip_address" ]; then
330
		delete_old_address
331
		delete_old_routes
332
	fi
333
	;;
334 8e4ab9fe Scott Ullrich
335 b43ba51b Scott Ullrich
TIMEOUT)
336
	delete_old_alias
337
	add_new_address
338 24003009 Scott Ullrich
	/bin/sleep 1
339 b43ba51b Scott Ullrich
	if [ -n "$new_routers" ]; then
340
		$LOGGER "New Routers ($interface): $new_routers"
341
		set "$new_routers"
342 bb92b70f Ermal
		if /sbin/ping -q -c 1 -t 1 "$1"; then
343 b43ba51b Scott Ullrich
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
344
				add_new_alias
345
			fi
346
			add_new_routes
347
			if add_new_resolv_conf; then
348 d0d7f09a Scott Ullrich
				notify_rc_newwanip
349 b43ba51b Scott Ullrich
			fi
350
		fi
351
	fi
352 4671d198 Scott Ullrich
	$IFCONFIG $interface inet -alias $new_ip_address $medium
353 b43ba51b Scott Ullrich
	delete_old_routes
354
	;;
355
esac
356 8e4ab9fe Scott Ullrich
357 d0d7f09a Scott Ullrich
# Invoke the local dhcp client exit hooks, if they exist.
358
if [ -f /etc/dhclient-exit-hooks ]; then
359
	$LOGGER "dhclient-exit-hooks"
360
	exit_status=0
361
	. /etc/dhclient-exit-hooks
362
	# allow the local script to abort processing of this state
363
	# local script must set exit_status variable to nonzero.
364
	exit $exit_status
365
fi