forked from amazonlinux/amazon-ec2-net-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2net-functions
350 lines (320 loc) · 10.6 KB
/
ec2net-functions
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# -*-Shell-script-*-
# Copyright 2012 Amazon.com, Inc. and its affiliates. All Rights Reserved.
#
# Licensed under the MIT License. See the LICENSE accompanying this file
# for the specific language governing permissions and limitations under
# the License.
# This file is not a stand-alone shell script; it provides functions
# to ec2 network scripts that source it.
# Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH
# metadata query requires an interface and hardware address
if [ -z "${INTERFACE}" ]; then
exit
fi
HWADDR=$(cat /sys/class/net/${INTERFACE}/address 2>/dev/null)
while test "${HWADDR}" = "00:00:00:00:00:00"; do
sleep 0.1
HWADDR=$(cat /sys/class/net/${INTERFACE}/address 2>/dev/null)
done
if [ -z "${HWADDR}" ] && [ "${ACTION}" != "remove" ]; then
exit
fi
export HWADDR
# generate a routing table number
RTABLE=${INTERFACE#eth}
let RTABLE+=10000
metadata_base="http://169.254.169.254/latest/meta-data/network/interfaces/macs"
config_file="/etc/sysconfig/network-scripts/ifcfg-${INTERFACE}"
route_file="/etc/sysconfig/network-scripts/route-${INTERFACE}"
route6_file="/etc/sysconfig/network-scripts/route6-${INTERFACE}"
dhclient_file="/etc/dhcp/dhclient-${INTERFACE}.conf"
# make no changes to unmanaged interfaces
if [ -s ${config_file} ]; then
unmanaged=$(LANG=C grep -l "^[[:space:]]*EC2SYNC=no\([[:space:]#]\|$\)" $config_file)
if [ "${config_file}" == "${unmanaged}" ]; then
logger --tag ec2net "Not managing ${INTERFACE}"
exit
fi
fi
get_meta() {
attempts=60
false
while [ "${?}" -gt 0 ]; do
if [ "${attempts}" -eq 0 ]; then
logger --tag ec2net "[get_meta] Failed to get ${metadata_base}/${HWADDR}/${1}"
return
fi
logger --tag ec2net "[get_meta] Trying to get ${metadata_base}/${HWADDR}/${1}"
meta=$(curl -s -f ${metadata_base}/${HWADDR}/${1})
if [ "${?}" -gt 0 ]; then
let attempts--
sleep 0.5
false
fi
done
echo "${meta}"
}
get_cidr() {
cidr=$(get_meta 'subnet-ipv4-cidr-block')
echo "${cidr}"
}
get_ipv4s() {
ipv4s=$(get_meta 'local-ipv4s')
echo "${ipv4s}"
}
get_primary_ipv4() {
ipv4s=($(get_ipv4s))
echo "${ipv4s[0]}"
}
get_secondary_ipv4s() {
ipv4s=($(get_ipv4s))
echo "${ipv4s[@]:1}"
}
get_ipv6s() {
/sbin/ip -6 addr list dev ${INTERFACE} scope global \
| grep "inet6" \
| awk '{print $2}' | cut -d/ -f1
}
get_ipv6_gateway() {
# Because we start dhclient -6 immediately on interface
# hotplug, it's possible we get a DHCP response before we
# receive a router advertisement. The only immediate clue we
# have about the gateway is the MAC address embedded in the
# DHCP6 server ID. If that env var is passed to dhclient-script
# we determine the router address from that; otherwise we wait
# up to 10 seconds for an RA route to be added by the kernel.
if echo "$new_dhcp6_server_id" | grep -q "^0:3:0:1:"; then
logger --tag ec2net "[get_ipv6_gateway] Using DHCP6 environment variable"
octets=($(echo "$new_dhcp6_server_id" | rev | cut -d : -f -6 | rev | tr : ' '))
# The gateway's link local address is derived from the
# hardware address by converting the MAC-48 to an EUI-64:
# 00:00:5e : 00:53:35
# ^^ ^^^^^ ff:fe is inserted in the middle
# first octet is xored with 0x2 (second LSB is flipped)
# thus 02:00:5e:ff:fe:00:53:35.
#
# The EUI-64 is used as the last 64 bits in an fe80::/64
# address, so fe80::200:5eff:fe00:5335.
declare -A quibbles # quad nibbles
quibbles[0]=$(( ((0x${octets[0]} ^ 2) << 8) + 0x${octets[1]} ))
quibbles[1]=$(( 0x${octets[2]}ff ))
quibbles[2]=$(( 0xfe00 + 0x${octets[3]} ))
quibbles[3]=$(( (0x${octets[4]} << 8) + 0x${octets[5]} ))
printf "fe80::%04x:%04x:%04x:%04x\n" ${quibbles[@]}
else
logger --tag ec2net "[get_ipv6_gateway] Waiting for IPv6 router advertisement"
attempts=20
while true; do
if [ "${attempts}" -eq 0 ]; then
logger --tag ec2net "[get_ipv6_gateway] Failed to receive router advertisement"
return
fi
gateway6=$(/sbin/ip -6 route show dev "${INTERFACE}" | grep ^default | awk '{print $3}')
if [ -n "${gateway6}" ]; then
break
else
let attempts--
sleep 0.5
fi
done
echo "${gateway6}"
fi
}
remove_primary() {
if [ "${INTERFACE}" == "eth0" ]; then
return
fi
logger --tag ec2net "[remove_primary] Removing configs for ${INTERFACE}"
rm -f ${config_file}
rm -f ${route_file}
rm -f ${route6_file}
rm -f ${dhclient_file}
}
rewrite_primary() {
if [ "${INTERFACE}" == "eth0" ]; then
return
fi
logger --tag ec2net "[rewrite_primary] Rewriting configs for ${INTERFACE}"
cidr=$(get_cidr)
if [ -z ${cidr} ]; then
return
fi
network=$(echo ${cidr}|cut -d/ -f1)
router=$(( $(echo ${network}|cut -d. -f4) + 1))
gateway="$(echo ${network}|cut -d. -f1-3).${router}"
primary_ipv4="$(get_primary_ipv4)"
cat <<- EOF > ${config_file}
DEVICE=${INTERFACE}
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
USERCTL=yes
PEERDNS=no
IPV6INIT=yes
DHCPV6C=yes
DHCPV6C_OPTIONS=-nw
PERSISTENT_DHCLIENT=yes
HWADDR=${HWADDR}
DEFROUTE=no
EC2SYNC=yes
EOF
cat <<- EOF > ${route_file}
default via ${gateway} dev ${INTERFACE} table ${RTABLE}
default via ${gateway} dev ${INTERFACE} metric ${RTABLE}
${cidr} dev ${INTERFACE} proto kernel scope link src ${primary_ipv4} table ${RTABLE}
EOF
# We would normally write to ${route6_file} here but the
# gateway is an fe80:: link local address that we get from the
# RA. We only get an RA if the interface has an IPv6 address.
# So we wait until dhclient -6 runs rewrite_rules() and add the
# ${RTABLE} table there.
rm -f ${route6_file}
# Use broadcast address instead of unicast dhcp server address.
# Works around an issue with two interfaces on the same subnet.
# Unicast lease requests go out the first available interface,
# and dhclient ignores the response. Broadcast requests go out
# the expected interface, and dhclient accepts the lease offer.
cat <<- EOF > ${dhclient_file}
supersede dhcp-server-identifier 255.255.255.255;
EOF
}
remove_aliases() {
logger --tag ec2net "[remove_aliases] Removing aliases of ${INTERFACE}"
/sbin/ip -4 addr flush dev ${INTERFACE} secondary
}
rewrite_aliases() {
aliases=$(get_secondary_ipv4s)
if [ ${#aliases[*]} -eq 0 ]; then
remove_aliases
return
fi
logger --tag ec2net "[rewrite_aliases] Rewriting aliases of ${INTERFACE}"
# The network prefix can be provided in the environment by
# e.g. DHCP, but if it's not available then we need it to
# correctly configure secondary addresses.
if [ -z "${PREFIX}" ]; then
cidr=$(get_cidr)
PREFIX=$(echo ${cidr}|cut -d/ -f2)
fi
[ -n "${PREFIX##*[!0-9]*}" ] || return
# Retrieve a list of secondary IP addresses on the interface.
# Treat this as the stale list. For each IP address obtained
# from metadata, cross it off the stale list if present, or
# add it to the interface otherwise. Then, remove any address
# remaining in the stale list.
declare -A secondaries
for secondary in $(/sbin/ip -4 addr list dev ${INTERFACE} secondary \
|grep "inet .* secondary ${INTERFACE}" \
|awk '{print $2}'|cut -d/ -f1); do
secondaries[${secondary}]=1
done
for alias in ${aliases}; do
if [[ ${secondaries[${alias}]} ]]; then
unset secondaries[${alias}]
else
/sbin/ip -4 addr add ${alias}/${PREFIX} brd + dev ${INTERFACE}
fi
done
for secondary in "${!secondaries[@]}"; do
/sbin/ip -4 addr del ${secondary}/${PREFIX} dev ${INTERFACE}
done
}
remove_rules() {
if [ "${INTERFACE}" == "eth0" ]; then
return
fi
logger --tag ec2net "[remove_rules] Removing rules for ${INTERFACE}"
for rule in $(/sbin/ip -4 rule list \
|grep "from .* lookup ${RTABLE}" \
|awk -F: '{print $1}'); do
/sbin/ip -4 rule delete pref "${rule}"
done
for rule in $(/sbin/ip -6 rule list \
|grep "from .* lookup ${RTABLE}" \
|awk -F: '{print $1}'); do
/sbin/ip -6 rule delete pref "${rule}"
done
}
rewrite_rules() {
if [ "${INTERFACE}" == "eth0" ]; then
return
fi
ips=($(get_ipv4s))
ip6s=($(get_ipv6s))
if [ ${#ips[*]} -eq 0 ]; then
remove_rules
return
fi
# This is the part we would do in rewrite_primary() if we knew
# the gateway address.
if [ ${#ip6s[*]} -gt 0 -a -z "$(/sbin/ip -6 route show table ${RTABLE})" ]; then
gateway6=$(get_ipv6_gateway)
# Manually add the route, then add it to ${route6_file} so it
# gets brought down with the rest of the interface.
/sbin/ip -6 route add default via ${gateway6} dev ${INTERFACE} table ${RTABLE}
cat <<- EOF > ${route6_file}
default via ${gateway6} dev ${INTERFACE} table ${RTABLE}
EOF
fi
logger --tag ec2net "[rewrite_rules] Rewriting rules for ${INTERFACE}"
# Retrieve a list of IP rules for the route table that belongs
# to this interface. Treat this as the stale list. For each IP
# address obtained from metadata, cross the corresponding rule
# off the stale list if present. Otherwise, add a rule sending
# outbound traffic from that IP to the interface route table.
# Then, remove all other rules found in the stale list.
declare -A rules
for rule in $(/sbin/ip -4 rule list \
|grep "from .* lookup ${RTABLE}" \
|awk '{print $1$3}'); do
split=(${rule//:/ })
rules[${split[1]}]=${split[0]}
done
for ip in ${ips[@]}; do
if [[ ${rules[${ip}]} ]]; then
unset rules[${ip}]
else
/sbin/ip -4 rule add from ${ip} lookup ${RTABLE}
fi
done
for rule in "${!rules[@]}"; do
/sbin/ip -4 rule delete pref "${rules[${rule}]}"
done
# Now do the same, but for IPv6
declare -A rules6
for rule in $(/sbin/ip -6 rule list \
|grep "from .* lookup ${RTABLE}" \
|awk '{print $1$3}'); do
split=(${rule/:/ }) # take care to only replace the first :
rules6[${split[1]}]=${split[0]}
done
for ip in ${ip6s[@]}; do
if [[ ${rules6[${ip}]} ]]; then
unset rules6[${ip}]
else
/sbin/ip -6 rule add from ${ip} lookup ${RTABLE}
fi
done
for rule in "${!rules6[@]}"; do
/sbin/ip -6 rule delete pref "${rules6[${rule}]}"
done
}
plug_interface() {
logger --tag ec2net "[plug_interface] ${INTERFACE} plugged"
rewrite_primary
}
unplug_interface() {
logger --tag ec2net "[unplug_interface] ${INTERFACE} unplugged"
remove_rules
remove_aliases
}
activate_primary() {
logger --tag ec2net "[activate_primary] Activating ${INTERFACE}"
/sbin/ifup ${INTERFACE}
}
deactivate_primary() {
logger --tag ec2net "[deactivate_primary] Deactivating ${INTERFACE}"
/sbin/ifdown ${INTERFACE}
}