Project

General

Profile

Download (8.45 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
	if [ ! -f "/var/etc/nameserver_$interface" ]; then
178
		# Make sure file exists to avoid errors
179
		touch /var/etc/nameserver_$interface
180
	fi
181
	if [ -n "$new_domain_name_servers" ]; then
182
		# Remove old entries
183
		for nameserver in `cat /var/etc/nameserver_$interface`; do
184
			route delete $nameserver
185
		done
186
		rm -f /var/etc/nameserver_$interface
187
		for nameserver in $new_domain_name_servers; do
188
			# Add a route to the nameserver out the correct interface
189
			# so that mulitple wans work correctly with multiple dns
190
			# also backup the nameserver for later route removal
191
			echo $nameserver >>/var/etc/nameserver_$interface
192
			route add $nameserver -iface $interface
193
		done
194
		echo $new_domain_name >/var/etc/defaultdomain.conf
195
	fi
196

    
197
	echo $interface > /tmp/rc.newwanip
198

    
199
	return 0
200
}
201

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

    
206
#
207
# Start of active code.
208
#
209

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

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

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

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

    
244
ARPCHECK|ARPSEND)
245
	;;
246

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

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

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

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