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