Project

General

Profile

Download (9.6 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" -o "$router" = "255.255.255.255" ]; then
193
				$ROUTE add default -iface $interface
194
				echo $ROUTE add default -iface $interface | $LOGGER
195
				# 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
			else
200
				$ROUTE add default $router
201
				echo $ROUTE add default $router | $LOGGER
202
                       		echo $router > /tmp/${interface}_router
203
			fi
204
			ADDED_ROUTE=yes
205
			# 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

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

    
224
add_new_resolv_conf() {
225
	$LOGGER "Creating resolv.conf"
226
	if [ -f "/var/etc/nameserver_$interface" ]; then
227
		# Remove old entries
228
		for nameserver in `cat /var/etc/nameserver_$interface`; do
229
			$ROUTE delete $nameserver
230
		done
231
	fi
232
	if [ -n "$new_domain_name_servers" ]; then 
233
		/bin/rm -f /var/etc/nameserver_$interface
234
		for nameserver in $new_domain_name_servers; do
235
			nameserver_sanitized=`echo '$nameserver' | egrep -o '([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[0-9a-f]+:)'`
236
			# 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
			echo '$nameserver_sanitized' >>/var/etc/nameserver_$interface
240
			$ROUTE add $nameserver_sanitized -iface $interface
241
		done
242
		echo $new_domain_name | /usr/bin/egrep -o "[0-9\.]+" >/var/etc/searchdomain_$interface
243
	fi
244

    
245
	return 0
246
}
247

    
248
# Notify rc.newwanip of changes to an interface
249
notify_rc_newwanip() {
250
	/usr/local/sbin/pfSctl -c "interface newip $interface"
251
}
252

    
253
#
254
# Start of active code.
255
#
256

    
257
# Invoke the local dhcp client enter hooks, if they exist.
258
if [ -f /etc/dhclient-enter-hooks ]; then
259
	$LOGGER "dhclient-enter-hooks"
260
	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
fi
268

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

    
276
$LOGGER $reason
277
case $reason in
278
MEDIUM)
279
	$IFCONFIG $interface $medium
280
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
281
	/bin/sleep 1
282
	;;
283

    
284
PREINIT)
285
	delete_old_alias
286
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
287
	/bin/rm -f /tmp/${interface}_router
288
	;;
289

    
290
ARPCHECK|ARPSEND)
291
	;;
292

    
293
BOUND|RENEW|REBIND|REBOOT)
294
	check_hostname
295
	changes="no"
296
	if [ -n "$old_ip_address" ]; then
297
		if [ -n "$alias_ip_address" ] && \
298
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
299
			delete_old_alias
300
			changes="yes"
301
		fi
302
		if [ "$old_ip_address" != "$new_ip_address" ]; then
303
			delete_old_address
304
			delete_old_routes
305
			changes="yes"
306
		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
		changes="yes"
315
	fi
316
	if [ -n "$alias_ip_address" ] && \
317
       [ "$new_ip_address" != "$alias_ip_address" ]; then
318
		add_new_alias
319
		changes="yes"
320
	fi
321
	add_new_resolv_conf
322
	if [ "$changes" = "yes" ] ; then
323
		notify_rc_newwanip
324
	fi
325
	;;
326

    
327
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

    
335
TIMEOUT)
336
	delete_old_alias
337
	add_new_address
338
	/bin/sleep 1
339
	if [ -n "$new_routers" ]; then
340
		$LOGGER "New Routers ($interface): $new_routers"
341
		set "$new_routers"
342
		if /sbin/ping -q -c 1 -t 1 "$1"; then
343
			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
				notify_rc_newwanip
349
			fi
350
		fi
351
	fi
352
	$IFCONFIG $interface inet -alias $new_ip_address $medium
353
	delete_old_routes
354
	;;
355
esac
356

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