Project

General

Profile

Download (8.53 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
	$IFCONFIG $interface inet -alias $old_ip_address $medium
63
}
64

    
65
add_new_address() {
66

    
67
	$LOGGER "Starting add_new_address()"
68

    
69
	$LOGGER "ifconfig $interface inet $new_ip_address netmask $new_subnet_mask broadcast $new_broadcast_address $medium"
70

    
71
	$IFCONFIG $interface \
72
		inet $new_ip_address \
73
		netmask $new_subnet_mask \
74
		broadcast $new_broadcast_address \
75
		$medium
76

    
77
		$LOGGER "New IP Address ($interface): $new_ip_address"
78
		$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
79
		$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
80
		$LOGGER "New Routers ($interface): $new_routers"
81

    
82
        echo $new_routers > /tmp/${interface}_router
83
	cp /var/db/${interface}_ip /tmp/rc.newwanip_oldip 2>/dev/null
84
#	echo $new_ip_address > /var/db/${interface}_ip
85

    
86
	echo $interface > /tmp/rc.newwanip
87
#	echo $new_ip_address >>  /tmp/rc.newwanip
88
				
89
}
90

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

    
98
add_new_alias() {
99
	if [ -n "$alias_ip_address" ]; then
100
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
101
		    $alias_subnet_mask
102
		$ROUTE add $alias_ip_address $LOCALHOST
103
	fi
104
}
105

    
106
delete_old_routes() {
107
	$LOGGER "Deleting old routes"
108
	$ROUTE delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
109
	# Only allow the default route to be overridden if it's on our own interface
110
	DEFAULTROUTE_IFACE=`route get default | grep interface | awk '{print $2};'`
111
	if [ -z "${DEFAULTROUTE_IFACE}" -o "{$interface}" = "${DEFAULTROUTE_IFACE}" ]; then
112
		for router in $old_routers; do
113
			if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then
114
				$ROUTE delete default $route >/dev/null 2>&1
115
                        	/bin/rm -f /tmp/${interface}_router
116
			fi
117
		done
118
	fi
119

    
120
	if [ -n "$old_static_routes" ]; then
121
		set $old_static_routes
122
		while [ $# -gt 1 ]; do
123
			$ROUTE delete "$1" "$2"
124
			shift; shift
125
                        /bin/rm -f /tmp/${interface}_router
126
		done
127
	fi
128

    
129
	arp_flush
130
}
131

    
132
add_new_routes() {
133
	$LOGGER "Adding new routes"
134
	# $ROUTE add $new_ip_address $LOCALHOST >/dev/null 2>&1
135

    
136
	# Only allow the default route to be overridden if it's on our own interface
137
	DEFAULTROUTE_IFACE=`route get default | grep interface | awk '{print $2};'`
138
	if [ -z "${DEFAULTROUTE_IFACE}" -o "{$interface}" = "${DEFAULTROUTE_IFACE}" ]; then
139
		for router in $new_routers; do
140
			if [ "$new_ip_address" = "$router" ]; then
141
				$ROUTE add default -iface $router
142
				#>/dev/null 2>&1
143
				echo $ROUTE add default -iface $router | $LOGGER
144
				echo $new_routers > /tmp/${interface}_router
145
			else
146
				$ROUTE add default $router
147
				echo $ROUTE add default $router | $LOGGER
148
				#>/dev/null 2>&1
149
                       		echo $new_routers > /tmp/${interface}_router
150
			fi
151
			# 2nd and subsequent default routers error out, so explicitly
152
			# stop processing the list after the first one.
153
			break
154
		done
155
	fi
156

    
157
	if [ -n "$new_static_routes" ]; then
158
		$LOGGER "New Static Routes ($interface): $new_static_routes"
159
		set $new_static_routes
160
		while [ $# -gt 1 ]; do
161
			$ROUTE add $1 $2
162
			shift; shift
163
                        echo $new_routers > /tmp/${interface}_router
164
		done
165
	fi
166

    
167
	# last ditch effort if no route exists.
168
	DEFAULTROUTE=`$NETSTAT -rn | $GREP default`
169
	if [ -z "${DEFAULTROUTE}" ]; then
170
	    $ROUTE add default -iface $interface
171
	fi
172

    
173
}
174

    
175
add_new_resolv_conf() {
176
	$LOGGER "Creating resolv.conf"
177
	/bin/rm -f /var/etc/nameservers.conf
178
	if [ ! -f "/var/etc/nameserver_$interface" ]; then
179
		# Make sure file exists to avoid errors
180
		touch /var/etc/nameserver_$interface
181
	fi
182
	if [ -n "$new_domain_name_servers" ]; then
183
		# Remove old entries
184
		for nameserver in `cat /var/etc/nameserver_$interface`; do
185
			route delete $nameserver
186
		done
187
		rm -f /var/etc/nameserver_$interface
188
		for nameserver in $new_domain_name_servers; do
189
			echo $nameserver >>/var/etc/nameservers.conf
190
			# Add a route to the nameserver out the correct interface
191
			# so that mulitple wans work correctly with multiple dns
192
			# also backup the nameserver for later route removal
193
			echo $nameserver >>/var/etc/nameserver_$interface
194
			route add $nameserver -if $interface
195
		done
196
		echo $new_domain_name >/var/etc/defaultdomain.conf
197
	fi
198

    
199
	echo $interface > /tmp/rc.newwanip
200

    
201
	return 0
202
}
203

    
204
# Notify rc.newwanip of changes to an interface
205
notify_rc_newwanip() {
206
}
207

    
208
#
209
# Start of active code.
210
#
211

    
212
# Invoke the local dhcp client enter hooks, if they exist.
213
if [ -f /etc/dhclient-enter-hooks ]; then
214
	$LOGGER "dhclient-enter-hooks"
215
	exit_status=0
216
	. /etc/dhclient-enter-hooks
217
	# allow the local script to abort processing of this state
218
	# local script must set exit_status variable to nonzero.
219
	if [ $exit_status -ne 0 ]; then
220
		exit $exit_status
221
	fi
222
fi
223

    
224
if [ -x $NETSTAT ]; then
225
	$LOGGER "netstat"
226
	if_defaultroute=`$NETSTAT -rn | $GREP "^default" | $AWK '{print $6}'`
227
else
228
	$LOGGER "if_defaultroute"
229
	if_defaultroute="x"
230
fi
231

    
232
$LOGGER $reason
233
case $reason in
234
MEDIUM)
235
	$IFCONFIG $interface $medium
236
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
237
	/bin/sleep 1
238
	;;
239

    
240
PREINIT)
241
	delete_old_alias
242
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
243
	/bin/rm -f /tmp/${interface}_router
244
	;;
245

    
246
ARPCHECK|ARPSEND)
247
	;;
248

    
249
BOUND|RENEW|REBIND|REBOOT)
250
	check_hostname
251
	changes=no
252
	if [ -n "$old_ip_address" ]; then
253
		if [ -n "$alias_ip_address" ] && \
254
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
255
			delete_old_alias
256
			changes=yes
257
		fi
258
		if [ "$old_ip_address" != "$new_ip_address" ]; then
259
			delete_old_address
260
			delete_old_routes
261
			changes=yes
262
		fi
263
	fi
264
	if [ "$reason" = BOUND ] || \
265
	   [ "$reason" = REBOOT ] || \
266
	   [ -z "$old_ip_address" ] || \
267
	   [ "$old_ip_address" != "$new_ip_address" ]; then
268
		add_new_address
269
		add_new_routes
270
		changes=yes
271
	fi
272
	if [ -n "$alias_ip_address" ] && \
273
       [ "$new_ip_address" != "$alias_ip_address" ]; then
274
		add_new_alias
275
		changes=yes
276
	fi
277
	add_new_resolv_conf
278
	if [ "$changes" = "yes" ] ; then
279
		notify_rc_newwanip
280
	fi
281
	;;
282

    
283
EXPIRE|FAIL)
284
	delete_old_alias
285
	if [ -n "$old_ip_address" ]; then
286
		delete_old_address
287
		delete_old_routes
288
	fi
289
	;;
290

    
291
TIMEOUT)
292
	delete_old_alias
293
	add_new_address
294
	/bin/sleep 1
295
	if [ -n "$new_routers" ]; then
296
		$LOGGER "New Routers ($interface): $new_routers"
297
		set "$new_routers"
298
		if /sbin/ping -q -c 1 -w 1 "$1"; then
299
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
300
				add_new_alias
301
			fi
302
			add_new_routes
303
			if add_new_resolv_conf; then
304
				notify_rc_newwanip
305
			fi
306
		fi
307
	fi
308
	$IFCONFIG $interface inet -alias $new_ip_address $medium
309
	delete_old_routes
310
	;;
311
esac
312

    
313
# Invoke the local dhcp client exit hooks, if they exist.
314
if [ -f /etc/dhclient-exit-hooks ]; then
315
	$LOGGER "dhclient-exit-hooks"
316
	exit_status=0
317
	. /etc/dhclient-exit-hooks
318
	# allow the local script to abort processing of this state
319
	# local script must set exit_status variable to nonzero.
320
	exit $exit_status
321
fi
(2-2/3)