-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdhclient-ipv6
executable file
·267 lines (235 loc) · 7.91 KB
/
dhclient-ipv6
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
# vim:tw=80:tabstop=2:shiftwidth=2:expandtab
# Copyright (c) 2012-present, Phil Dibowitz <[email protected]>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in
# binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
# * Neither the name of the author nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# You can find the latest version of this at:
# https://github.com/jaymzh/v6-gw-scripts
#
# Will, given a 'dhclient -6 -P ...' on $EXT_IFACE and assign the prefix
# given to the $INT_IFACE, and twiddle radvd.
#
# For radvd, it takes /etc/radvd.conf.tmpl, replaces "__PREFIX__" with your
# prefix, and "__IFACE__" with your internal interface, and - if it's different
# from /etc/radvd.conf - replaces the config file and restarts the daemon.
#
# Change INT_IFACE or EXT_IFACE by setting them in the config file
CONF='/etc/ipv6_prefix_dhclient.conf'
EXT_IFACE='eth0'
INT_IFACE='eth1'
OTHER_IFACES=''
RADVD_TEMPLATE='/etc/radvd.conf.tmpl'
DNSMASQ_TEMPLATE='/etc/dnsmasq.conf.tmpl'
RA_DAEMON='radvd'
# shellcheck disable=SC1090
[ -r $CONF ] && . $CONF
STATE_DIR=/var/lib/dhcp/dhclient-ipv6-mapping
set_iaid_mapping() {
local iaid="$1"
local iface="$2"
echo "$iface" > "$STATE_DIR/iaid_$iaid"
}
get_interface() {
local iaid=$1
if [ ! -d $STATE_DIR ]; then
mkdir -p $STATE_DIR
set_iaid_mapping "$iaid" "$INT_IFACE"
echo $INT_IFACE
return
fi
ls $STATE_DIR/iaid_* >/dev/null 2>/dev/null
if [ $? -eq 2 ]; then
echo $INT_IFACE
set_iaid_mapping "$iaid" "$INT_IFACE"
fi
# if we've mapped this iaid to an interface, just cat it out
if [ -e "$STATE_DIR/iaid_$iaid" ]; then
iface=$(cat "$STATE_DIR/iaid_$iaid")
# we're not changing the mapping, but we call update anyway mostly
# as a `touch` for the code below that checks the time
echo "$iface"
set_iaid_mapping "$iaid" "$iface"
return
fi
# we haven't - so let's see if we have an interfaces not-yet-mapped
known=""
for file in "$STATE_DIR"/iaid_*; do
known="$known $(cat "$file")"
done
for iface in $INT_IFACE $OTHER_IFACES; do
for kface in $known; do
if [ "$kface" = "$iface" ]; then
continue 2
fi
done
# This interface is not yet mapped, return it
echo $iface
set_iaid_mapping "$iaid" "$iface"
return
done
# OK, all of our interfaces are mapped, but none of them is this one, so we'll
# just return the least-recently touched file. This is a bit odd, because
# we'll end up populating them in the reverse order, but we won't just end up
# re-doing one we just did
# shellcheck disable=SC2012
last=$(ls -t $STATE_DIR/iaid_* | tail -n 1)
iface=$(cat "$last")
rm -f "$last"
echo "$iface"
set_iaid_mapping "$iaid" "$iface"
}
get_prefix() {
local iface="$1"
current_ip=$(
/sbin/ip -6 addr show dev "$iface" scope global |\
/usr/bin/awk '/inet6/ {print $2}'
)
# We'll always configure with a /64 even if we got something bigger
echo "$current_ip" | /bin/sed -E 's@::1/64@::/64@'
}
update_dnsmasq() {
local iface="$1"
local prefix="$2"
tmpfile="/tmp/dnsmasq.conf.$$"
rm -f "$tmpfile"
interfaces=''
prefixes=''
for tface in $INT_IFACE $OTHER_IFACES; do
interfaces="${interfaces}interface=${tface}\n"
int_prefix="$(get_prefix $tface)"
int_dhcp_range="$(echo "$int_prefix" | cut -d '/' -f 1)"
prefixes="${prefixes}dhcp-range=${int_dhcp_range}, ra-stateless, ra-names\n"
done
sed -e "s@__INTERFACES__@$interfaces@g" \
-e "s@__PREFIXES__@$prefixes@g" $DNSMASQ_TEMPLATE \
>> $tmpfile
if ! diff $tmpfile /etc/dnsmasq.conf > /dev/null 2>&1; then
# Update config and restart radvd
mv $tmpfile /etc/dnsmasq.conf
systemctl restart dnsmasq
else
rm $tmpfile
# ensure it's running anyway
systemctl start dnsmasq
fi
}
update_radvd() {
local iface="$1"
local prefix="$2"
# the shell we get from dhclient doesn't have [[ ]]
if ! echo "$prefix" | grep -q '/64$'; then
# if we got something bigger than a /64, we must just use the
# first /64 as autoconfig only works in /64s
net=$(echo "$prefix" | cut -f1 -d/)
prefix="$net/64"
fi
tmpfile="/tmp/radvd.conf.$$"
rm -f "$tmpfile"
for tface in $INT_IFACE $OTHER_IFACES; do
if [ "$iface" = "$tface" ]; then
sed -e "s@__PREFIX__@$prefix@g" \
-e "s@__IFACE__@$iface@g" $RADVD_TEMPLATE \
>> $tmpfile
else
tprefix=$(get_prefix $tface)
if [ -z "$tprefix" ]; then
continue
fi
sed -e "s@__PREFIX__@$tprefix@g" \
-e "s@__IFACE__@$tface@g" $RADVD_TEMPLATE \
>> $tmpfile
fi
done
if ! diff $tmpfile /etc/radvd.conf >/dev/null 2>/dev/null; then
# Update config and restart radvd
mv $tmpfile /etc/radvd.conf
service radvd restart
else
rm $tmpfile
# ensure it's running anyway
service radvd start
fi
}
ipv6_prefix_setup() {
local iface
local current_prefix
# new_iaid comes from DHCP
# shellcheck disable=SC2154
iface=$(get_interface "$new_iaid")
current_prefix=$(get_prefix "$iface")
# shellcheck disable=SC2154
if [ -z "$current_prefix" ] || \
! [ "$current_prefix" = "$new_ip6_prefix" ] ; then
# Setup the new IP
# Note that no matter what size prefix we were given, you have to make
# it a /64 for autoconfig to work, so we cut it down to /64
#
# TODO for the future - if your ISP gives us bigger than a /54, allow
# the user to cut it up instead of requesting multiple
new_ip=$(echo "$new_ip6_prefix" | /bin/sed -E 's@::/([0-9]{2})@::1/64@')
if [ -n "$current_ip" ] ; then
ip -6 addr del "$current_ip" dev "$iface"
fi
ip -6 addr add "$new_ip" dev "$iface"
# Ensure we'll get router advertisements
sysctl -w "net.ipv6.conf.$EXT_IFACE.accept_ra=2"
# Needed for kernels before 2.6.37, don't worry,
# forwarding will still work as long as you have it set
# on your other interface.
sysctl -w "net.ipv6.conf.$EXT_IFACE.forwarding=0"
fi
if [ "$RA_DAEMON" = "dnsmasq" ]; then
update_dnsmasq "$iface" "$new_ip6_prefix"
else
update_radvd "$iface" "$new_ip6_prefix"
fi
}
# interface comes from DHCP
# shellcheck disable=SC2154
if [ "$interface" != "$EXT_IFACE" ] ; then
return
fi
# reason comes from DHCP
# shellcheck disable=SC2154
case "$reason" in
BOUND6|REBIND6)
# We will get called twice here - once for the temp address
# and once for the prefix. We only care about the prefix.
if [ -n "$new_ip6_prefix" ] ; then
ipv6_prefix_setup
fi
;;
RELEASE)
ext_iface_pid="/var/run/dhclient6.${EXT_IFACE}.pid"
if [ -e "$ext_iface_pid" ]; then
echo "Shutting down dhclient6.${EXT_IFACE} via SIGINT"
kill "$(cat "$ext_iface_pid")"
fi
;;
esac