summaryrefslogtreecommitdiffstats
path: root/sbin/dhclient6-script
blob: f0f2ffacc5581442633fcd5fb72715bd5a60bdd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/sh
# $Id$
# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
# $FreeBSD: src/sbin/dhclient/dhclient-script,v 1.4 2005/06/10 03:41:18 brooks Exp $
#
# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

NETSTAT=/usr/bin/netstat
GREP=/usr/bin/grep
AWK=/usr/bin/awk
HOSTNAME=/bin/hostname
GREP=/usr/bin/grep
ROUTE=/sbin/route
SED=/usr/bin/sed
ARP=/usr/sbin/arp
IFCONFIG=/sbin/ifconfig

LOCALHOST=::1

if [ -x /usr/bin/logger ]; then
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient6"
else
	LOGGER=echo
fi

#
# Helper functions that implement common actions.
#

check_hostname() {
	current_hostname=`$HOSTNAME`
	if [ -z "$current_hostname" ]; then
		$LOGGER "New Hostname ($interface): $new_host_name"
		$HOSTNAME $new_host_name
	elif [ "$current_hostname" = "$old_host_name" -a \
	       "$new_host_name" != "$old_host_name" ]; then
		$LOGGER "New Hostname ($interface): $new_host_name"
		$HOSTNAME $new_host_name
	fi
}

delete_old_states() {
	$LOGGER "Starting delete_old_states()"
	# If the IP changed, remove states from the old one
	if [ -f /var/db/${interface}_ipv6 ]; then
		OLD_IPv6 = `cat /var/db/${interface}_ipv6`
		$LOGGER "Comparing IPv6s: Old: ${OLD_IPv6} New: ${new_ip6_address}"
		if [ -n "${OLD_IPv6}" ] && [ "${OLD_IPv6}" != "${new_ip6_address}" ]; then
			$LOGGER "Removing states from old IPv6 '${OLD_IPv6}' (new IP '${new_ip6_address}')"
			/sbin/pfctl -k :: -k ${OLD_IPv6}/128
			/sbin/pfctl -k ${OLD_IPv6}/128
			pfctl -K ${OLD_IPv6}/128
		fi
	fi
	# Delete states through old gateway if it's not the same
	# FIXME: dhcp6 doesn't know about routers. Needs fixing elsewhere.
}

delete_old_address() {
	/bin/rm -f /var/db/${interface}_ipv6
	$IFCONFIG $interface inet6 -alias $old_ip6_address
}

add_new_address() {
	if [ -z $new_ip6_address ]; then
		return;
	fi

	$LOGGER "Starting add_new_address()"
	$LOGGER "ifconfig $interface inet6 $new_ip6_address prefixlen $new_ip6_prefixlen"
	$IFCONFIG $interface \
		inet6 $new_ip6_address \
		prefixlen $new_ip6_prefixlen

		$LOGGER "New IPv6 Address ($interface): $new_ip6_address"
		$LOGGER "New Prefix Length ($interface): $new_ip6_prefixlen"

	echo $new_ip6_address > /var/db/${interface}_ipv6
}

add_new_prefix() {
	if [ -z $new_ip6_prefix ]; then
		return
	fi
	$LOGGER "Starting add_new_prefix()"
	echo $new_ip6_prefix > /var/db/${interface}_pd_ipv6
}

delete_old_prefix() {
	if [ -z $old_ip6_prefix ]; then
		return
	fi
	$LOGGER "Starting delete_old_prefix()"
	rm -f /var/db/${interface}_pd_ipv6
}

delete_old_alias() {
	if [ -n "$alias_ip6_address" ]; then
		$IFCONFIG $interface inet6 -alias $alias_ip6_address > /dev/null 2>&1
		$ROUTE delete -inet6 $alias_ip6_address $LOCALHOST > /dev/null 2>&1
	fi
}

add_new_alias() {
	if [ -n "$alias_ip6_address" ]; then
		$IFCONFIG $interface inet6 alias $alias_ip6_address prefixlen $new_ip6_prefixlen
		$ROUTE add -inet6 $alias_ip6_address $LOCALHOST
	fi
}

add_new_resolv_conf() {
	$LOGGER "Creating resolv.conf"
	if [ -f "/var/etc/nameserver_v6$interface" ]; then
		# Remove old entries
		for nameserver in `cat /var/etc/nameserver_v6$interface`; do
			$ROUTE delete -inet6 $nameserver >/dev/null 2>&1
		done
	fi
	if [ -n "$new_dhcp6_name_servers" ]; then 
		/bin/rm -f /var/etc/nameserver_v6$interface
		ALLOWOVERRIDE=`/usr/bin/grep dnsallowoverride /conf/config.xml | /usr/bin/wc -l`
		for nameserver in $new_dhcp6_name_servers; do
			# Add a route to the nameserver out the correct interface
			# so that mulitple wans work correctly with multiple dns
			# also backup the nameserver for later route removal
			if [ $ALLOWOVERRIDE -gt 0 ]; then
				echo $nameserver >>/var/etc/nameserver_v6$interface
				$ROUTE add -inet6 $nameserver -iface $interface
			fi
		done
		echo $new_dhcp6_domain_search >/var/etc/searchdomain_v6$interface
	fi

	return 0
}

# Notify rc.newwanip of changes to an interface
notify_rc_newwanipv6() {
	/etc/rc.newwanipv6 $interface
	# /usr/local/sbin/pfSctl -c "interface newipv6 $interface"
}

#
# Start of active code.
#

# Invoke the local dhcp client enter hooks, if they exist.
if [ -f /etc/dhclient-enter-hooks ]; then
	$LOGGER "dhclient-enter-hooks"
	exit_status=0
	. /etc/dhclient-enter-hooks
	# allow the local script to abort processing of this state
	# local script must set exit_status variable to nonzero.
	if [ $exit_status -ne 0 ]; then
		exit $exit_status
	fi
fi

$LOGGER $reason
case $reason in
PREINIT6)
	delete_old_alias
	delete_old_prefix
	$IFCONFIG $interface inet6 up
	delete_old_states
	;;

BOUND6|RENEW6|REBIND6|REBOOT6)

	set
	check_hostname
	changes="no"
	if [ "$old_ip6_address" != "$new_ip6_address" ]; then
		delete_old_states
	fi
	if [ -n "$old_ip6_address" ]; then
		if [ "$old_ip6_address" != "$alias_ip6_address" ]; then
			delete_old_alias
			changes="yes"
		fi
	fi
	if [ "$old_ip6_prefix" != "$new_ip6_prefix" ]; then
		delete_old_prefix
		changes="yes"
	fi
	if [ "$reason" = BOUND6 ] || \
	   [ "$reason" = REBOOT6 ] || \
	   [ "$reason" = REBIND6 ] || \
	   [ -z "$old_ip6_address" ] || \
	   [ "$old_ip6_address" || "$new_ip6_address" ]; then
		add_new_address
		add_new_prefix
		changes="yes"
	fi
	if [ -n "$alias_ip6_address" ] && \
		[ "$new_ip6_address" != "$alias_ip6_address" ]; then
		add_new_alias
		changes="yes"
	fi
	add_new_resolv_conf
	if [ "$changes" = "yes" ] ; then
		notify_rc_newwanipv6
	fi
	;;

EXPIRE6|FAIL6)
	delete_old_alias
	delete_old_states
	if [ -n "$old_ip6_address" ]; then
		delete_old_address
	fi
	;;

TIMEOUT6)
	delete_old_alias
	add_new_address
	/bin/sleep 1
	$IFCONFIG $interface inet6 -alias $new_ip6_address $medium
	delete_old_states
	;;
esac

# Invoke the local dhcp client exit hooks, if they exist.
if [ -f /etc/dhclient-exit-hooks ]; then
	$LOGGER "dhclient-exit-hooks"
	exit_status=0
	. /etc/dhclient-exit-hooks
	# allow the local script to abort processing of this state
	# local script must set exit_status variable to nonzero.
	exit $exit_status
fi
OpenPOWER on IntegriCloud