]> git.ipfire.org Git - people/stevee/network.git/blame - src/dhclient-script
dhclient: Add support for DHCPv6 and PD
[people/stevee/network.git] / src / dhclient-script
CommitLineData
e9ea243e
MT
1#!/bin/bash
2
3. /usr/lib/network/functions
4
5# Configure logging
6LOG_FACILITY="dhclient-script"
7
9038a844
MT
8network_settings_read
9
e9ea243e
MT
10assert isset interface
11assert isset reason
12
13assert device_exists ${interface}
14
e9ea243e
MT
15basename="$(basename $0)"
16log DEBUG "${basename} called for interface=${interface} reason=${reason}"
17
83d72e63
MT
18# Log all information from dhclient
19if enabled DEBUG; then
20 while read line; do
21 [[ ${line} =~ ^(cur|old|new)_ ]] || continue
22 log DEBUG " ${line}"
23 done <<< "$(printenv | sort)"
24fi
25
e9ea243e
MT
26# Main pitchfork.
27case "${reason}" in
28 MEDIUM)
29 # Linux does not handle MEDIUM.
30 exit 0
31 ;;
32
83d72e63
MT
33 # IPv6
34
35 PREINIT6)
36 if ! device_is_up "${interface}"; then
37 log WARNING "Device '${interface}' was not brought up before starting the DHCP client"
38 device_set_up "${interface}"
39 fi
40
41 # Flush all other aborted IP addresses
42 ipv6_address_flush "${interface}"
43
44 # Wait until DAD for the link-local address has finished
45 for address in $(ipv6_device_get_addresses "${interface}" --scope="link"); do
46 ipv6_wait_for_dad "${address}" "${interface}" && exit ${EXIT_OK}
47 done
48
49 log ERROR "There is no active link-local address on ${interface}"
50
51 exit ${EXIT_ERROR}
52 ;;
53
54 BOUND6)
55 # We will be called twice. Once for the assigned address and once for an assigned prefix.
56
57 # Handle temporarily-assigned address
58 if isset new_ip6_address && isset new_ip6_prefixlen; then
59 ipv6_address_add "${new_ip6_address}/${new_ip6_prefixlen}" "${interface}" \
60 --valid-lifetime="${new_max_life}" --preferred-lifetime="${new_preferred_life}"
61
62 # Save configuration
63 routing_db_set "${interface}" "ipv6" "local-ip-address" "${new_ip6_address}/${new_ip6_prefixlen}"
64 routing_db_set "${interface}" "ipv6" "active" 1
65 #routing_db_set "${interface}" "ipv6" "domain-name" "${new_
66
67 exit 0
68
69 # Handle Prefix Delegation
70 elif isset new_ip6_prefix; then
71 routing_db_set "${interface}" "ipv6" "delegated-prefix" "${new_ip6_prefix}"
72
73 exit 0
74 fi
75 ;;
76
77 RENEW6|REBIND6)
78 # Will be called twice like BOUND6.
79
80 if isset new_ip6_address && isset new_ip6_prefixlen; then
81 # Update nameservers if those have changed
82 if [[ "${old_dhcp6_name_servers}" != "${new_dhcp6_name_servers}" ]]; then
83 routing_db_set "${interface}" "ipv6" "domain-name-servers" "${new_dhcp6_name_servers}"
84 dns_generate_resolvconf
85 fi
86
87 # Update the lifetime if the address has not changed
88 if [[ "${old_ip6_address}" = "${new_ip6_address}" ]]; then
89 ipv6_address_change_lifetime "${new_ip6_address}/${new_ip6_prefixlen}" "${interface}" \
90 --valid-lifetime="${new_max_life}" --preferred-lifetime="${new_preferred_life}"
91
92 exit ${EXIT_OK}
93 fi
94
95 # Remove any old IP addresses
96 if [ -n "${old_ip6_address}" ]; then
97 ipv6_address_del "${old_ip6_address}/${old_ip6_prefixlen}" "${interface}"
98 fi
99
100 # Add the new one
101 ipv6_address_add "${new_ip6_address}/${new_ip6_prefixlen}" "${interface}" \
102 --valid-lifetime="${new_max_life}" --preferred-lifetime="${new_preferred_life}"
103
104 exit ${EXIT_OK}
105
106 # Handle Prefix Delegation
107 elif isset new_ip6_prefix || isset old_ip6_prefix; then
108 if [[ "${old_ip6_prefix}" = "${new_ip6_prefix}" ]]; then
109 # TODO What do we need to do if the prefix hasn't changed?
110 exit ${EXIT_OK}
111 fi
112
113 log DEBUG "The delegated prefix has changed from ${old_ip6_prefix} to ${new_ip6_prefix}"
114 routing_db_set "${interface}" "ipv6" "delegated-prefix" "${new_ip6_prefix}"
115
116 exit ${EXIT_OK}
117 fi
118 ;;
119
120 DEPREF6)
121 # Check if all necessary information is there
122 if ! isset cur_ip6_address || ! isset cur_ip6_prefixlen; then
123 exit ${EXIT_ERROR}
124 fi
125
126 # Set lifetime to zero
127 ipv6_address_change_lifetime "${cur_ip6_address}/${cur_ip6_prefixlen}" "${interface}" \
128 --preferred-lifetime=0 || exit ${EXIT_ERROR}
129 ;;
130
131 # IPv4
132
e9ea243e
MT
133 PREINIT)
134 # Bring up the device if it hasn't been done before.
135 if ! device_is_up ${interface}; then
136 log DEBUG "The interface '${interface}' does not appear to be up."
137
138 zone_up ${interface}
139 fi
140
141 # If the use configured a delay, we will honour that.
142 if [ -n "${DELAY}" ]; then
143 assert isinteger DELAY
144 sleep ${DELAY}
145
146 # If he didn't, we will try to detect is STP has brought the
147 # bridge up.
148 elif device_is_bridge ${interface}; then
149 counter=60
150
151 while [ ${counter} -gt 0 ]; do
152 # We may end this, when the bridge is in forwarding mode.
153 if bridge_is_forwarding ${interface}; then
154 log DEBUG "Bridge '${interface}' is in forwarding mode."
155 break
156 fi
157
158 counter=$(( ${counter} - 1 ))
159 sleep 1
160 done
161
162 # Tell the daemon, that we are not ready to go on.
163 if [ ${counter} -eq 0 ]; then
164 log ERROR "Bridge '${interface}' is not in forwarding mode."
165 log ERROR "Could not go on with getting a DHCP lease. Exiting."
166
167 exit 1
168 fi
169 fi
170
171 exit 0
172 ;;
173
e9ea243e
MT
174 BOUND|RENEW|REBIND|REBOOT)
175 # Check if the IP address has changed. If so, delete all routes and stuff.
176 if [ -n "${old_ip_address}" -a "${old_ip_address}" != "${new_ip_address}" ]; then
177 ipv4_flush_device ${interface}
178 fi
179
180 case "${reason}" in
181 BOUND|REBOOT)
182 if [ ! "${old_ip_address}" = "${new_ip_address}" ] || \
183 [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] || \
184 [ ! "${old_network_number}" = "${new_network_number}" ] || \
185 [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] || \
186 [ ! "${old_routers}" = "${new_routers}" ] || \
187 [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ]; then
188
189
190 # Calc a prefix out of address and subnet mask.
191 new_prefix="$(ipv4_get_prefix ${new_ip_address} ${new_subnet_mask})"
192
193 # Set the new ip address.
194 ip_address_add ${interface} ${new_ip_address}/${new_prefix}
195 device_set_up ${interface}
196
197
198 # A MTU of 576 is used for X.25 and dialup connections. Some broken DHCP
199 # servers send out an MTU of 576 bytes, which will be ignored.
200 if [ -n "${new_interface_mtu}" ] && [ ${new_interface_mtu} -gt 576 ]; then
201 device_set_mtu ${interface} ${new_interface_mtu}
202 fi
203
204 # Save configuration
205 routing_db_set ${interface} ipv4 type "ipv4-dhcp"
206 routing_db_set ${interface} ipv4 local-ip-address "${new_ip_address}/${new_prefix}"
2af98498 207 routing_db_set ${interface} ipv4 remote-ip-address "${new_routers}"
e9ea243e 208 routing_db_set ${interface} ipv4 active 1
a9ebc53b
KB
209 routing_db_set ${interface} ipv4 domain-name "${new_domain_name}"
210 routing_db_set ${interface} ipv4 domain-name-servers "${new_domain_name_servers}"
e5651e17 211 routing_db_set ${interface} ipv4 domain-name-servers-priority "${DNS_SERVER_DYNAMIC_PRIORITY}"
e9ea243e
MT
212
213 # Update the routing tables.
214 routing_update ${interface} ipv4
2af98498 215 routing_default_update
a9ebc53b
KB
216
217 # Update resolv.conf
218 dns_generate_resolvconf
e9ea243e
MT
219 fi
220 ;;
221 esac
222
223 exit 0
224 ;;
225
226 EXPIRE|FAIL|RELEASE|STOP)
227 # Remove the currently configured addresses from the device.
228 if [ -n "${old_ip_address}" ]; then
229 ipv4_flush_device ${interface}
230 fi
231
2af98498
MT
232 routing_db_remove ${interface} ipv4
233 routing_default_update
234
e9ea243e
MT
235 exit 0
236 ;;
83d72e63
MT
237
238 *)
239 log ERROR "Unhandled reason: ${reason}"
240 ;;
e9ea243e
MT
241esac
242
243exit 1