Project

General

Profile

Download (8.21 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=`/sbin/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 to interface: $interface"
134

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

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

    
167
add_new_resolv_conf() {
168
	$LOGGER "Creating resolv.conf"
169
	if [ ! -f "/var/etc/nameserver_$interface" ]; then
170
		# Make sure file exists to avoid errors
171
		touch /var/etc/nameserver_$interface
172
	fi
173
	if [ -n "$new_domain_name_servers" ]; then
174
		# Remove old entries
175
		for nameserver in `cat /var/etc/nameserver_$interface`; do
176
			$ROUTE delete $nameserver
177
		done
178
		rm -f /var/etc/nameserver_$interface
179
		for nameserver in $new_domain_name_servers; do
180
			# Add a route to the nameserver out the correct interface
181
			# so that mulitple wans work correctly with multiple dns
182
			# also backup the nameserver for later route removal
183
			echo $nameserver >>/var/etc/nameserver_$interface
184
			$ROUTE add $nameserver -iface $interface
185
		done
186
		echo $new_domain_name >/var/etc/defaultdomain.conf
187
	fi
188

    
189
	echo $interface > /tmp/rc.newwanip
190

    
191
	return 0
192
}
193

    
194
# Notify rc.newwanip of changes to an interface
195
notify_rc_newwanip() {
196
}
197

    
198
#
199
# Start of active code.
200
#
201

    
202
# Invoke the local dhcp client enter hooks, if they exist.
203
if [ -f /etc/dhclient-enter-hooks ]; then
204
	$LOGGER "dhclient-enter-hooks"
205
	exit_status=0
206
	. /etc/dhclient-enter-hooks
207
	# allow the local script to abort processing of this state
208
	# local script must set exit_status variable to nonzero.
209
	if [ $exit_status -ne 0 ]; then
210
		exit $exit_status
211
	fi
212
fi
213

    
214
if [ -x $ROUTE ]; then
215
	if_defaultroute=`$ROUTE get default | $GREP interface | $AWK '{print $2}'`
216
else
217
	$LOGGER "if_defaultroute"
218
	if_defaultroute="x"
219
fi
220

    
221
$LOGGER $reason
222
case $reason in
223
MEDIUM)
224
	$IFCONFIG $interface $medium
225
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
226
	/bin/sleep 1
227
	;;
228

    
229
PREINIT)
230
	delete_old_alias
231
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
232
	/bin/rm -f /tmp/${interface}_router
233
	;;
234

    
235
ARPCHECK|ARPSEND)
236
	;;
237

    
238
BOUND|RENEW|REBIND|REBOOT)
239
	check_hostname
240
	changes=no
241
	if [ -n "$old_ip_address" ]; then
242
		if [ -n "$alias_ip_address" ] && \
243
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
244
			delete_old_alias
245
			changes=yes
246
		fi
247
		if [ "$old_ip_address" != "$new_ip_address" ]; then
248
			delete_old_address
249
			delete_old_routes
250
			changes=yes
251
		fi
252
	fi
253
	if [ "$reason" = BOUND ] || \
254
	   [ "$reason" = REBOOT ] || \
255
	   [ -z "$old_ip_address" ] || \
256
	   [ "$old_ip_address" != "$new_ip_address" ]; then
257
		add_new_address
258
		add_new_routes
259
		changes=yes
260
	fi
261
	if [ -n "$alias_ip_address" ] && \
262
       [ "$new_ip_address" != "$alias_ip_address" ]; then
263
		add_new_alias
264
		changes=yes
265
	fi
266
	add_new_resolv_conf
267
	if [ "$changes" = "yes" ] ; then
268
		notify_rc_newwanip
269
	fi
270
	;;
271

    
272
EXPIRE|FAIL)
273
	delete_old_alias
274
	if [ -n "$old_ip_address" ]; then
275
		delete_old_address
276
		delete_old_routes
277
	fi
278
	;;
279

    
280
TIMEOUT)
281
	delete_old_alias
282
	add_new_address
283
	/bin/sleep 1
284
	if [ -n "$new_routers" ]; then
285
		$LOGGER "New Routers ($interface): $new_routers"
286
		set "$new_routers"
287
		if /sbin/ping -q -c 1 -w 1 "$1"; then
288
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
289
				add_new_alias
290
			fi
291
			add_new_routes
292
			if add_new_resolv_conf; then
293
				notify_rc_newwanip
294
			fi
295
		fi
296
	fi
297
	$IFCONFIG $interface inet -alias $new_ip_address $medium
298
	delete_old_routes
299
	;;
300
esac
301

    
302
# Invoke the local dhcp client exit hooks, if they exist.
303
if [ -f /etc/dhclient-exit-hooks ]; then
304
	$LOGGER "dhclient-exit-hooks"
305
	exit_status=0
306
	. /etc/dhclient-exit-hooks
307
	# allow the local script to abort processing of this state
308
	# local script must set exit_status variable to nonzero.
309
	exit $exit_status
310
fi
(2-2/3)