Debian Codegraph @

hooks/dhclient-script

raw
1 #!/bin/bash
2 #
3 # SPDX-License-Identifier: LGPL-2.1+
4 #
5 # dhclient-script for Linux. Dan Halbert, March, 1997.
6 # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
7 # No guarantees about this. I'm a novice at the details of Linux
8 # networking.
9
10 # Notes:
11
12 # 0. This script is based on the netbsd script supplied with dhcp-970306.
13
14 # 1. ifconfig down apparently deletes all relevant routes and flushes
15 # the arp cache, so this doesn't need to be done explicitly.
16
17 # 2. The alias address handling here has not been tested AT ALL.
18 # I'm just going by the doc of modern Linux ip aliasing, which uses
19 # notations like eth0:0, eth0:1, for each alias.
20
21 # 3. I have to calculate the network address, and calculate the broadcast
22 # address if it is not supplied. This might be much more easily done
23 # by the dhclient C code, and passed on.
24
25 # 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
26 # of the $1 in its args.
27
28 # 5. Script refresh in 2017. The aliasing code was too convoluted and needs
29 # to go away. Migrated DHCPv4 script to ip command from iproute2 suite.
30 # This is based on Debian script with some tweaks. ifconfig is no longer
31 # used. Everything is done using ip tool from ip-route2.
32
33 # 6. LXC Fork
34 # Remove hooks and dependency on ifconfig.
35 # Resolve every paths against the $ROOTFS environment variable.
36
37 # 'ip' just looks too weird. Also, we now have unit-tests! Those unit-tests
38 # override this line to use a fake ip-echo tool. It's also convenient
39 # if your system holds ip tool in a non-standard location.
40 ip=/sbin/ip
41
42 # update /etc/resolv.conf based on received values
43 # This updated version mostly follows Debian script by Andrew Pollock et al.
44 make_resolv_conf() {
45 local resolv_conf="${ROOTFS}/etc/resolv.conf"
46 local new_resolv_conf="${ROOTFS}/etc/resolv.conf.dhclient-new"
47
48 # DHCPv4
49 if [ -n "$new_domain_search" ] || [ -n "$new_domain_name" ] ||
50 [ -n "$new_domain_name_servers" ]; then
51 rm -f $new_resolv_conf
52
53 if [ -n "$new_domain_name" ]; then
54 echo domain ${new_domain_name%% *} >>$new_resolv_conf
55 fi
56
57 if [ -n "$new_domain_search" ]; then
58 if [ -n "$new_domain_name" ]; then
59 domain_in_search_list=""
60 for domain in $new_domain_search; do
61 if [ "$domain" = "${new_domain_name}" ] ||
62 [ "$domain" = "${new_domain_name}." ]; then
63 domain_in_search_list="Yes"
64 fi
65 done
66 if [ -z "$domain_in_search_list" ]; then
67 new_domain_search="$new_domain_name $new_domain_search"
68 fi
69 fi
70 echo "search ${new_domain_search}" >> $new_resolv_conf
71 elif [ -n "$new_domain_name" ]; then
72 echo "search ${new_domain_name}" >> $new_resolv_conf
73 fi
74
75 if [ -n "$new_domain_name_servers" ]; then
76 for nameserver in $new_domain_name_servers; do
77 echo nameserver $nameserver >>$new_resolv_conf
78 done
79 else # keep 'old' nameservers
80 sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p $resolv_conf >>$new_resolv_conf
81 fi
82
83 if [ -f "$resolv_conf" ]; then
84 chown --reference=$resolv_conf $new_resolv_conf
85 chmod --reference=$resolv_conf $new_resolv_conf
86 fi
87 mv -f $new_resolv_conf $resolv_conf
88 # DHCPv6
89 elif [ -n "$new_dhcp6_domain_search" ] || [ -n "$new_dhcp6_name_servers" ]; then
90 rm -f $new_resolv_conf
91
92 if [ -n "$new_dhcp6_domain_search" ]; then
93 echo "search ${new_dhcp6_domain_search}" >> $new_resolv_conf
94 fi
95
96 if [ -n "$new_dhcp6_name_servers" ]; then
97 for nameserver in $new_dhcp6_name_servers; do
98 # append %interface to link-local-address nameservers
99 if [ "${nameserver##fe80::}" != "$nameserver" ] ||
100 [ "${nameserver##FE80::}" != "$nameserver" ]; then
101 nameserver="${nameserver}%${interface}"
102 fi
103 echo nameserver $nameserver >>$new_resolv_conf
104 done
105 else # keep 'old' nameservers
106 sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p $resolv_conf >>$new_resolv_conf
107 fi
108
109 if [ -f "$resolv_conf" ]; then
110 chown --reference=$resolv_conf $new_resolv_conf
111 chmod --reference=$resolv_conf $new_resolv_conf
112 fi
113 mv -f $new_resolv_conf $resolv_conf
114 fi
115 }
116
117 # set host name
118 set_hostname() {
119 local current_hostname
120
121 if [ -n "$new_host_name" ]; then
122 current_hostname=$(hostname)
123
124 # current host name is empty, '(none)' or 'localhost' or differs from new one from DHCP
125 if [ -z "$current_hostname" ] ||
126 [ "$current_hostname" = '(none)' ] ||
127 [ "$current_hostname" = 'localhost' ] ||
128 [ "$current_hostname" = "$old_host_name" ]; then
129 if [ "$new_host_name" != "$old_host_name" ]; then
130 hostname "$new_host_name"
131 fi
132 fi
133 fi
134 }
135
136 # Execute the operation
137 case "$reason" in
138
139 ### DHCPv4 Handlers
140
141 MEDIUM|ARPCHECK|ARPSEND)
142 # Do nothing
143 ;;
144 PREINIT)
145 # The DHCP client is requesting that an interface be
146 # configured as required in order to send packets prior to
147 # receiving an actual address. - dhclient-script(8)
148
149 # ensure interface is up
150 ${ip} link set dev ${interface} up
151
152 if [ -n "$alias_ip_address" ]; then
153 # flush alias IP from interface
154 ${ip} -4 addr flush dev ${interface} label ${interface}:0
155 fi
156
157 ;;
158
159 BOUND|RENEW|REBIND|REBOOT)
160 set_hostname
161
162 if [ -n "$old_ip_address" ] && [ -n "$alias_ip_address" ] &&
163 [ "$alias_ip_address" != "$old_ip_address" ]; then
164 # alias IP may have changed => flush it
165 ${ip} -4 addr flush dev ${interface} label ${interface}:0
166 fi
167
168 if [ -n "$old_ip_address" ] &&
169 [ "$old_ip_address" != "$new_ip_address" ]; then
170 # leased IP has changed => flush it
171 ${ip} -4 addr flush dev ${interface} label ${interface}
172 fi
173
174 if [ -z "$old_ip_address" ] ||
175 [ "$old_ip_address" != "$new_ip_address" ] ||
176 [ "$reason" = "BOUND" ] || [ "$reason" = "REBOOT" ]; then
177 # new IP has been leased or leased IP changed => set it
178 ${ip} -4 addr add ${new_ip_address}${new_subnet_mask:+/$new_subnet_mask} \
179 ${new_broadcast_address:+broadcast $new_broadcast_address} \
180 dev ${interface} label ${interface}
181
182 if [ -n "$new_interface_mtu" ]; then
183 # set MTU
184 ${ip} link set dev ${interface} mtu ${new_interface_mtu}
185 fi
186
187 # if we have $new_rfc3442_classless_static_routes then we have to
188 # ignore $new_routers entirely
189 if [ ! "$new_rfc3442_classless_static_routes" ]; then
190 # set if_metric if IF_METRIC is set or there's more than one router
191 if_metric="$IF_METRIC"
192 if [ "${new_routers%% *}" != "${new_routers}" ]; then
193 if_metric=${if_metric:-1}
194 fi
195
196 for router in $new_routers; do
197 if [ "$new_subnet_mask" = "255.255.255.255" ]; then
198 # point-to-point connection => set explicit route
199 ${ip} -4 route add ${router} dev $interface >/dev/null 2>&1
200 fi
201
202 # set default route
203 ${ip} -4 route add default via ${router} dev ${interface} \
204 ${if_metric:+metric $if_metric} >/dev/null 2>&1
205
206 if [ -n "$if_metric" ]; then
207 if_metric=$((if_metric+1))
208 fi
209 done
210 fi
211 fi
212
213 if [ -n "$alias_ip_address" ] &&
214 [ "$new_ip_address" != "$alias_ip_address" ]; then
215 # separate alias IP given, which may have changed
216 # => flush it, set it & add host route to it
217 ${ip} -4 addr flush dev ${interface} label ${interface}:0
218 ${ip} -4 addr add ${alias_ip_address}${alias_subnet_mask:+/$alias_subnet_mask} \
219 dev ${interface} label ${interface}:0
220 ${ip} -4 route add ${alias_ip_address} dev ${interface} >/dev/null 2>&1
221 fi
222
223 # update /etc/resolv.conf
224 make_resolv_conf
225
226 ;;
227
228 EXPIRE|FAIL|RELEASE|STOP)
229 if [ -n "$alias_ip_address" ]; then
230 # flush alias IP
231 ${ip} -4 addr flush dev ${interface} label ${interface}:0
232 fi
233
234 if [ -n "$old_ip_address" ]; then
235 # flush leased IP
236 ${ip} -4 addr flush dev ${interface} label ${interface}
237 fi
238
239 if [ -n "$alias_ip_address" ]; then
240 # alias IP given => set it & add host route to it
241 ${ip} -4 addr add ${alias_ip_address}${alias_subnet_mask:+/$alias_subnet_mask} \
242 dev ${interface} label ${interface}:0
243 ${ip} -4 route add ${alias_ip_address} dev ${interface} >/dev/null 2>&1
244 fi
245
246 ;;
247
248 TIMEOUT)
249 if [ -n "$alias_ip_address" ]; then
250 # flush alias IP
251 ${ip} -4 addr flush dev ${interface} label ${interface}:0
252 fi
253
254 # set IP from recorded lease
255 ${ip} -4 addr add ${new_ip_address}${new_subnet_mask:+/$new_subnet_mask} \
256 ${new_broadcast_address:+broadcast $new_broadcast_address} \
257 dev ${interface} label ${interface}
258
259 if [ -n "$new_interface_mtu" ]; then
260 # set MTU
261 ${ip} link set dev ${interface} mtu ${new_interface_mtu}
262 fi
263
264 # if there is no router recorded in the lease or the 1st router answers pings
265 if [ -z "$new_routers" ] || ping -q -c 1 "${new_routers%% *}"; then
266 # if we have $new_rfc3442_classless_static_routes then we have to
267 # ignore $new_routers entirely
268 if [ ! "$new_rfc3442_classless_static_routes" ]; then
269 if [ -n "$alias_ip_address" ] &&
270 [ "$new_ip_address" != "$alias_ip_address" ]; then
271 # separate alias IP given => set up the alias IP & add host route to it
272 ${ip} -4 addr add \
273 ${alias_ip_address}${alias_subnet_mask:+/$alias_subnet_mask} \
274 dev ${interface} label ${interface}:0
275 ${ip} -4 route add ${alias_ip_address} dev ${interface} >/dev/null 2>&1
276 fi
277
278 # set if_metric if IF_METRIC is set or there's more than one router
279 if_metric="$IF_METRIC"
280 if [ "${new_routers%% *}" != "${new_routers}" ]; then
281 if_metric=${if_metric:-1}
282 fi
283
284 # set default route
285 for router in $new_routers; do
286 ${ip} -4 route add default via ${router} dev ${interface} \
287 ${if_metric:+metric $if_metric} >/dev/null 2>&1
288
289 if [ -n "$if_metric" ]; then
290 if_metric=$((if_metric+1))
291 fi
292 done
293 fi
294
295 # update /etc/resolv.conf
296 make_resolv_conf
297 else
298 # flush all IPs from interface
299 ${ip} -4 addr flush dev ${interface}
300 exit 2
301 fi
302
303 ;;
304
305 ### DHCPv6 Handlers
306 # TODO handle prefix change: ?based on ${old_ip6_prefix} and ${new_ip6_prefix}?
307
308 PREINIT6)
309 # ensure interface is up
310 ${ip} link set ${interface} up
311
312 # We need to give the kernel some time to active interface
313 interface_up_wait_time=5
314 for i in $(seq 0 ${interface_up_wait_time})
315 do
316 ${ip} addr show ${interface} | grep NO-CARRIER >/dev/null 2>&1
317 if [ $? -ne 0 ]; then
318 break;
319 fi
320 sleep 1
321 done
322
323 # flush any stale global permanent IPs from interface
324 ${ip} -6 addr flush dev ${interface} scope global permanent
325
326 # Wait for duplicate address detection for this interface if the
327 # --dad-wait-time parameter has been specified and is greater than
328 # zero.
329 if [ ${dad_wait_time} -gt 0 ]; then
330 # Check if any IPv6 address on this interface is marked as
331 # tentative.
332 ${ip} addr show ${interface} | grep inet6 | grep tentative \
333 &> /dev/null
334 if [ $? -eq 0 ]; then
335 # Wait for duplicate address detection to complete or for
336 # the timeout specified as --dad-wait-time.
337 for i in $(seq 0 $dad_wait_time)
338 do
339 # We're going to poll for the tentative flag every second.
340 sleep 1
341 ${ip} addr show ${interface} | grep inet6 | grep tentative \
342 &> /dev/null
343 if [ $? -ne 0 ]; then
344 break;
345 fi
346 done
347 fi
348 fi
349
350 ;;
351
352 BOUND6|RENEW6|REBIND6)
353 if [ "${new_ip6_address}" ] && [ "${new_ip6_prefixlen}" ]; then
354 # set leased IP
355 ${ip} -6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \
356 dev ${interface} scope global
357 fi
358
359 # update /etc/resolv.conf
360 if [ "${reason}" = BOUND6 ] ||
361 [ "${new_dhcp6_name_servers}" != "${old_dhcp6_name_servers}" ] ||
362 [ "${new_dhcp6_domain_search}" != "${old_dhcp6_domain_search}" ]; then
363 make_resolv_conf
364 fi
365
366 ;;
367
368 DEPREF6)
369 if [ -z "${cur_ip6_prefixlen}" ]; then
370 exit 2
371 fi
372
373 # set preferred lifetime of leased IP to 0
374 ${ip} -6 addr change ${cur_ip6_address}/${cur_ip6_prefixlen} \
375 dev ${interface} scope global preferred_lft 0
376
377 ;;
378
379 EXPIRE6|RELEASE6|STOP6)
380 if [ -z "${old_ip6_address}" ] || [ -z "${old_ip6_prefixlen}" ]; then
381 exit 2
382 fi
383
384 # delete leased IP
385 ${ip} -6 addr del ${old_ip6_address}/${old_ip6_prefixlen} \
386 dev ${interface}
387
388 ;;
389 esac
390
391 exit 0