Project

General

Profile

Download (9.26 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
			if [ "$new_ip_address" = "$router" ]; then
193 924f202e Ermal
				$ROUTE add default -iface $interface
194
				echo $ROUTE add default -iface $interface | $LOGGER
195
				echo $router > /tmp/${interface}_router
196 e1c8cdf5 Scott Ullrich
			else
197
				$ROUTE add default $router
198
				echo $ROUTE add default $router | $LOGGER
199 924f202e Ermal
                       		echo $router > /tmp/${interface}_router
200 e1c8cdf5 Scott Ullrich
			fi
201 6cf1cc61 Ermal
			ADDED_ROUTE=yes
202 e1c8cdf5 Scott Ullrich
			# 2nd and subsequent default routers error out, so explicitly
203
			# stop processing the list after the first one.
204
			break
205
		done
206
	fi
207 b43ba51b Scott Ullrich
208
	if [ -n "$new_static_routes" ]; then
209
		$LOGGER "New Static Routes ($interface): $new_static_routes"
210
		set $new_static_routes
211
		while [ $# -gt 1 ]; do
212 f968bb86 Scott Ullrich
			$ROUTE add $1 $2
213 6cf1cc61 Ermal
			if [ "$ADDED_ROUTE" = "no" ]; then
214
                        	echo $2 > /tmp/${interface}_router
215
			fi
216 b43ba51b Scott Ullrich
			shift; shift
217
		done
218
	fi
219
}
220
221
add_new_resolv_conf() {
222 b4a69e17 Scott Ullrich
	$LOGGER "Creating resolv.conf"
223 0c452870 Ermal
	if [ -f "/var/etc/nameserver_$interface" ]; then
224 d6ee4ccf Scott Ullrich
		# Remove old entries
225
		for nameserver in `cat /var/etc/nameserver_$interface`; do
226 1fb05961 Ermal
			$ROUTE delete $nameserver
227 d6ee4ccf Scott Ullrich
		done
228 0c452870 Ermal
	fi
229 9d1f614c Ermal
	if [ -n "$new_domain_name_servers" ]; then 
230 7afd6325 Ermal
		/bin/rm -f /var/etc/nameserver_$interface
231 b43ba51b Scott Ullrich
		for nameserver in $new_domain_name_servers; do
232 d6ee4ccf Scott Ullrich
			# Add a route to the nameserver out the correct interface
233
			# so that mulitple wans work correctly with multiple dns
234
			# also backup the nameserver for later route removal
235
			echo $nameserver >>/var/etc/nameserver_$interface
236 1fb05961 Ermal
			$ROUTE add $nameserver -iface $interface
237 b43ba51b Scott Ullrich
		done
238 86dcdfc9 Ermal
		echo $new_domain_name >/var/etc/searchdomain_$interface
239 b43ba51b Scott Ullrich
	fi
240 fe5869e4 Scott Ullrich
241 aad37fd2 Jeb Campbell
	return 0
242 8e4ab9fe Scott Ullrich
}
243
244 d0d7f09a Scott Ullrich
# Notify rc.newwanip of changes to an interface
245
notify_rc_newwanip() {
246 7afd6325 Ermal
	/usr/local/sbin/pfSctl -c "interface newip $interface"
247 8e4ab9fe Scott Ullrich
}
248
249 b43ba51b Scott Ullrich
#
250
# Start of active code.
251
#
252
253 8e4ab9fe Scott Ullrich
# Invoke the local dhcp client enter hooks, if they exist.
254
if [ -f /etc/dhclient-enter-hooks ]; then
255 b4a69e17 Scott Ullrich
	$LOGGER "dhclient-enter-hooks"
256 b43ba51b Scott Ullrich
	exit_status=0
257
	. /etc/dhclient-enter-hooks
258
	# allow the local script to abort processing of this state
259
	# local script must set exit_status variable to nonzero.
260
	if [ $exit_status -ne 0 ]; then
261
		exit $exit_status
262
	fi
263 8e4ab9fe Scott Ullrich
fi
264
265 409dc2e1 Ermal
#if [ -x $ROUTE ]; then
266
#	if_defaultroute=`$ROUTE -n get -inet default | $GREP interface | $AWK '{print $2}'`
267
#else
268
#	$LOGGER "if_defaultroute"
269
#	if_defaultroute="x"
270
#fi
271 8e4ab9fe Scott Ullrich
272 d0d7f09a Scott Ullrich
$LOGGER $reason
273 b43ba51b Scott Ullrich
case $reason in
274
MEDIUM)
275 4671d198 Scott Ullrich
	$IFCONFIG $interface $medium
276
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
277 24003009 Scott Ullrich
	/bin/sleep 1
278 b43ba51b Scott Ullrich
	;;
279 8e4ab9fe Scott Ullrich
280 b43ba51b Scott Ullrich
PREINIT)
281
	delete_old_alias
282 4671d198 Scott Ullrich
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
283 b4a69e17 Scott Ullrich
	/bin/rm -f /tmp/${interface}_router
284 b43ba51b Scott Ullrich
	;;
285 8e4ab9fe Scott Ullrich
286 b43ba51b Scott Ullrich
ARPCHECK|ARPSEND)
287
	;;
288 8e4ab9fe Scott Ullrich
289 b43ba51b Scott Ullrich
BOUND|RENEW|REBIND|REBOOT)
290
	check_hostname
291 7afd6325 Ermal
	changes="no"
292 b43ba51b Scott Ullrich
	if [ -n "$old_ip_address" ]; then
293 d0d7f09a Scott Ullrich
		if [ -n "$alias_ip_address" ] && \
294
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
295 b43ba51b Scott Ullrich
			delete_old_alias
296 7afd6325 Ermal
			changes="yes"
297 b43ba51b Scott Ullrich
		fi
298
		if [ "$old_ip_address" != "$new_ip_address" ]; then
299
			delete_old_address
300
			delete_old_routes
301 7afd6325 Ermal
			changes="yes"
302 b43ba51b Scott Ullrich
		fi
303
	fi
304
	if [ "$reason" = BOUND ] || \
305
	   [ "$reason" = REBOOT ] || \
306
	   [ -z "$old_ip_address" ] || \
307
	   [ "$old_ip_address" != "$new_ip_address" ]; then
308
		add_new_address
309
		add_new_routes
310 7afd6325 Ermal
		changes="yes"
311 b43ba51b Scott Ullrich
	fi
312 d0d7f09a Scott Ullrich
	if [ -n "$alias_ip_address" ] && \
313
       [ "$new_ip_address" != "$alias_ip_address" ]; then
314 b43ba51b Scott Ullrich
		add_new_alias
315 7afd6325 Ermal
		changes="yes"
316 b43ba51b Scott Ullrich
	fi
317
	add_new_resolv_conf
318 d0d7f09a Scott Ullrich
	if [ "$changes" = "yes" ] ; then
319
		notify_rc_newwanip
320
	fi
321 b43ba51b Scott Ullrich
	;;
322 8e4ab9fe Scott Ullrich
323 b43ba51b Scott Ullrich
EXPIRE|FAIL)
324
	delete_old_alias
325
	if [ -n "$old_ip_address" ]; then
326
		delete_old_address
327
		delete_old_routes
328
	fi
329
	;;
330 8e4ab9fe Scott Ullrich
331 b43ba51b Scott Ullrich
TIMEOUT)
332
	delete_old_alias
333
	add_new_address
334 24003009 Scott Ullrich
	/bin/sleep 1
335 b43ba51b Scott Ullrich
	if [ -n "$new_routers" ]; then
336
		$LOGGER "New Routers ($interface): $new_routers"
337
		set "$new_routers"
338 bb92b70f Ermal
		if /sbin/ping -q -c 1 -t 1 "$1"; then
339 b43ba51b Scott Ullrich
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
340
				add_new_alias
341
			fi
342
			add_new_routes
343
			if add_new_resolv_conf; then
344 d0d7f09a Scott Ullrich
				notify_rc_newwanip
345 b43ba51b Scott Ullrich
			fi
346
		fi
347
	fi
348 4671d198 Scott Ullrich
	$IFCONFIG $interface inet -alias $new_ip_address $medium
349 b43ba51b Scott Ullrich
	delete_old_routes
350
	;;
351
esac
352 8e4ab9fe Scott Ullrich
353 d0d7f09a Scott Ullrich
# Invoke the local dhcp client exit hooks, if they exist.
354
if [ -f /etc/dhclient-exit-hooks ]; then
355
	$LOGGER "dhclient-exit-hooks"
356
	exit_status=0
357
	. /etc/dhclient-exit-hooks
358
	# allow the local script to abort processing of this state
359
	# local script must set exit_status variable to nonzero.
360
	exit $exit_status
361
fi