Project

General

Profile

Download (9.26 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
	EXISTSGW=`/bin/ls -l /tmp/*_defaultgw | /usr/bin/wc -l`
188
	# Only allow the default route to be overridden if it's on our own interface
189
	if [ -f "/tmp/${interface}_defaultgw" -o $EXISTSGW -eq 0 ]; then
190
		$ROUTE delete default
191
		for router in $new_routers; do
192
			if [ "$new_ip_address" = "$router" ]; then
193
				$ROUTE add default -iface $interface
194
				echo $ROUTE add default -iface $interface | $LOGGER
195
				echo $router > /tmp/${interface}_router
196
			else
197
				$ROUTE add default $router
198
				echo $ROUTE add default $router | $LOGGER
199
                       		echo $router > /tmp/${interface}_router
200
			fi
201
			ADDED_ROUTE=yes
202
			# 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

    
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
			$ROUTE add $1 $2
213
			if [ "$ADDED_ROUTE" = "no" ]; then
214
                        	echo $2 > /tmp/${interface}_router
215
			fi
216
			shift; shift
217
		done
218
	fi
219
}
220

    
221
add_new_resolv_conf() {
222
	$LOGGER "Creating resolv.conf"
223
	if [ -f "/var/etc/nameserver_$interface" ]; then
224
		# Remove old entries
225
		for nameserver in `cat /var/etc/nameserver_$interface`; do
226
			$ROUTE delete $nameserver
227
		done
228
	fi
229
	if [ -n "$new_domain_name_servers" ]; then 
230
		/bin/rm -f /var/etc/nameserver_$interface
231
		for nameserver in $new_domain_name_servers; do
232
			# 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
			$ROUTE add $nameserver -iface $interface
237
		done
238
		echo $new_domain_name >/var/etc/searchdomain_$interface
239
	fi
240

    
241
	return 0
242
}
243

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

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

    
253
# Invoke the local dhcp client enter hooks, if they exist.
254
if [ -f /etc/dhclient-enter-hooks ]; then
255
	$LOGGER "dhclient-enter-hooks"
256
	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
fi
264

    
265
#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

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

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

    
286
ARPCHECK|ARPSEND)
287
	;;
288

    
289
BOUND|RENEW|REBIND|REBOOT)
290
	check_hostname
291
	changes="no"
292
	if [ -n "$old_ip_address" ]; then
293
		if [ -n "$alias_ip_address" ] && \
294
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
295
			delete_old_alias
296
			changes="yes"
297
		fi
298
		if [ "$old_ip_address" != "$new_ip_address" ]; then
299
			delete_old_address
300
			delete_old_routes
301
			changes="yes"
302
		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
		changes="yes"
311
	fi
312
	if [ -n "$alias_ip_address" ] && \
313
       [ "$new_ip_address" != "$alias_ip_address" ]; then
314
		add_new_alias
315
		changes="yes"
316
	fi
317
	add_new_resolv_conf
318
	if [ "$changes" = "yes" ] ; then
319
		notify_rc_newwanip
320
	fi
321
	;;
322

    
323
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

    
331
TIMEOUT)
332
	delete_old_alias
333
	add_new_address
334
	/bin/sleep 1
335
	if [ -n "$new_routers" ]; then
336
		$LOGGER "New Routers ($interface): $new_routers"
337
		set "$new_routers"
338
		if /sbin/ping -q -c 1 -t 1 "$1"; then
339
			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
				notify_rc_newwanip
345
			fi
346
		fi
347
	fi
348
	$IFCONFIG $interface inet -alias $new_ip_address $medium
349
	delete_old_routes
350
	;;
351
esac
352

    
353
# 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
(2-2/3)