Project

General

Profile

Download (9.19 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
NETSTAT=/usr/bin/netstat
22
GREP=/usr/bin/grep
23
AWK=/usr/bin/awk
24
HOSTNAME=/bin/hostname
25
GREP=/usr/bin/grep
26
ROUTE=/sbin/route
27
SED=/usr/bin/sed
28
ARP=/usr/sbin/arp
29
IFCONFIG=/sbin/ifconfig
30

    
31
LOCALHOST=127.0.0.1
32

    
33
if [ -x /usr/bin/logger ]; then
34
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
35
else
36
	LOGGER=echo
37
fi
38

    
39
#
40
# Helper functions that implement common actions.
41
#
42

    
43
check_hostname() {
44
	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
}
54

    
55
arp_flush() {
56
	$ARP -an -i $interface | \
57
		$SED -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
58
		/bin/sh >/dev/null 2>&1
59
}
60

    
61
delete_old_address() {
62
	/bin/rm -f /var/db/${interface}_ip
63
	$IFCONFIG $interface inet -alias $old_ip_address $medium
64
}
65

    
66
add_new_address() {
67

    
68
	$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
	$IFCONFIG $interface \
73
		inet $new_ip_address \
74
		netmask $new_subnet_mask \
75
		broadcast $new_broadcast_address \
76
		$medium
77

    
78
		$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

    
83
        echo $new_routers > /tmp/${interface}_router
84
	echo $new_ip_address > /var/db/${interface}_ip
85
}
86

    
87
delete_old_alias() {
88
	if [ -n "$alias_ip_address" ]; then
89
		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
90
		$ROUTE delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
91
	fi
92
}
93

    
94
add_new_alias() {
95
	if [ -n "$alias_ip_address" ]; then
96
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
97
		    $alias_subnet_mask
98
		$ROUTE add $alias_ip_address $LOCALHOST
99
	fi
100
}
101

    
102
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
delete_old_routes() {
128
	$LOGGER "Deleting old routes"
129

    
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
	# Only allow the default route to be overridden if it's on our own interface
141
	if [ -f "/tmp/${interface}_defaultgw" ]; then
142
		for router in $old_routers; do
143
			$ROUTE delete default $router >/dev/null 2>&1
144
			/bin/rm -f /tmp/${interface}_router
145
		done
146
	fi
147

    
148
	if [ -n "$old_static_routes" ]; then
149
		set $old_static_routes
150
		while [ $# -gt 1 ]; do
151
			$ROUTE delete "$1" "$2"
152
			shift; shift
153
                        /bin/rm -f /tmp/${interface}_router
154
		done
155
	fi
156

    
157
	arp_flush
158
}
159

    
160
add_new_routes() {
161
	$LOGGER "Adding new routes to interface: $interface"
162

    
163
	# 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
	ADDED_ROUTE=no
187
	# Only allow the default route to be overridden if it's on our own interface
188
	if [ -f "/tmp/${interface}_defaultgw" ]; then
189
		$ROUTE delete default
190
		for router in $new_routers; do
191
			if [ "$new_ip_address" = "$router" ]; then
192
				$ROUTE add default -iface $interface
193
				echo $ROUTE add default -iface $interface | $LOGGER
194
				echo $router > /tmp/${interface}_router
195
			else
196
				$ROUTE add default $router
197
				echo $ROUTE add default $router | $LOGGER
198
                       		echo $router > /tmp/${interface}_router
199
			fi
200
			ADDED_ROUTE=yes
201
			# 2nd and subsequent default routers error out, so explicitly
202
			# stop processing the list after the first one.
203
			break
204
		done
205
	fi
206

    
207
	if [ -n "$new_static_routes" ]; then
208
		$LOGGER "New Static Routes ($interface): $new_static_routes"
209
		set $new_static_routes
210
		while [ $# -gt 1 ]; do
211
			$ROUTE add $1 $2
212
			if [ "$ADDED_ROUTE" = "no" ]; then
213
                        	echo $2 > /tmp/${interface}_router
214
			fi
215
			shift; shift
216
		done
217
	fi
218
}
219

    
220
add_new_resolv_conf() {
221
	$LOGGER "Creating resolv.conf"
222
	if [ -f "/var/etc/nameserver_$interface" ]; then
223
		# Remove old entries
224
		for nameserver in `cat /var/etc/nameserver_$interface`; do
225
			$ROUTE delete $nameserver
226
		done
227
	fi
228
	if [ -n "$new_domain_name_servers" ]; then 
229
		/bin/rm -f /var/etc/nameserver_$interface
230
		for nameserver in $new_domain_name_servers; do
231
			# Add a route to the nameserver out the correct interface
232
			# so that mulitple wans work correctly with multiple dns
233
			# also backup the nameserver for later route removal
234
			echo $nameserver >>/var/etc/nameserver_$interface
235
			$ROUTE add $nameserver -iface $interface
236
		done
237
		echo $new_domain_name >/var/etc/searchdomain_$interface
238
	fi
239

    
240
	return 0
241
}
242

    
243
# Notify rc.newwanip of changes to an interface
244
notify_rc_newwanip() {
245
	/usr/local/sbin/pfSctl -c "interface newip $interface"
246
}
247

    
248
#
249
# Start of active code.
250
#
251

    
252
# Invoke the local dhcp client enter hooks, if they exist.
253
if [ -f /etc/dhclient-enter-hooks ]; then
254
	$LOGGER "dhclient-enter-hooks"
255
	exit_status=0
256
	. /etc/dhclient-enter-hooks
257
	# allow the local script to abort processing of this state
258
	# local script must set exit_status variable to nonzero.
259
	if [ $exit_status -ne 0 ]; then
260
		exit $exit_status
261
	fi
262
fi
263

    
264
#if [ -x $ROUTE ]; then
265
#	if_defaultroute=`$ROUTE -n get -inet default | $GREP interface | $AWK '{print $2}'`
266
#else
267
#	$LOGGER "if_defaultroute"
268
#	if_defaultroute="x"
269
#fi
270

    
271
$LOGGER $reason
272
case $reason in
273
MEDIUM)
274
	$IFCONFIG $interface $medium
275
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
276
	/bin/sleep 1
277
	;;
278

    
279
PREINIT)
280
	delete_old_alias
281
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
282
	/bin/rm -f /tmp/${interface}_router
283
	;;
284

    
285
ARPCHECK|ARPSEND)
286
	;;
287

    
288
BOUND|RENEW|REBIND|REBOOT)
289
	check_hostname
290
	changes="no"
291
	if [ -n "$old_ip_address" ]; then
292
		if [ -n "$alias_ip_address" ] && \
293
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
294
			delete_old_alias
295
			changes="yes"
296
		fi
297
		if [ "$old_ip_address" != "$new_ip_address" ]; then
298
			delete_old_address
299
			delete_old_routes
300
			changes="yes"
301
		fi
302
	fi
303
	if [ "$reason" = BOUND ] || \
304
	   [ "$reason" = REBOOT ] || \
305
	   [ -z "$old_ip_address" ] || \
306
	   [ "$old_ip_address" != "$new_ip_address" ]; then
307
		add_new_address
308
		add_new_routes
309
		changes="yes"
310
	fi
311
	if [ -n "$alias_ip_address" ] && \
312
       [ "$new_ip_address" != "$alias_ip_address" ]; then
313
		add_new_alias
314
		changes="yes"
315
	fi
316
	add_new_resolv_conf
317
	if [ "$changes" = "yes" ] ; then
318
		notify_rc_newwanip
319
	fi
320
	;;
321

    
322
EXPIRE|FAIL)
323
	delete_old_alias
324
	if [ -n "$old_ip_address" ]; then
325
		delete_old_address
326
		delete_old_routes
327
	fi
328
	;;
329

    
330
TIMEOUT)
331
	delete_old_alias
332
	add_new_address
333
	/bin/sleep 1
334
	if [ -n "$new_routers" ]; then
335
		$LOGGER "New Routers ($interface): $new_routers"
336
		set "$new_routers"
337
		if /sbin/ping -q -c 1 -t 1 "$1"; then
338
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
339
				add_new_alias
340
			fi
341
			add_new_routes
342
			if add_new_resolv_conf; then
343
				notify_rc_newwanip
344
			fi
345
		fi
346
	fi
347
	$IFCONFIG $interface inet -alias $new_ip_address $medium
348
	delete_old_routes
349
	;;
350
esac
351

    
352
# Invoke the local dhcp client exit hooks, if they exist.
353
if [ -f /etc/dhclient-exit-hooks ]; then
354
	$LOGGER "dhclient-exit-hooks"
355
	exit_status=0
356
	. /etc/dhclient-exit-hooks
357
	# allow the local script to abort processing of this state
358
	# local script must set exit_status variable to nonzero.
359
	exit $exit_status
360
fi
(2-2/3)