]> git.ipfire.org Git - people/ms/network.git/blob - src/dhclient-script
hostapd: Dump config file in debug mode
[people/ms/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_server_update
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 # We will try to detect is STP has brought the bridge up
172 if device_is_bridge ${interface}; then
173 counter=60
174
175 while [ ${counter} -gt 0 ]; do
176 # We may end this, when the bridge is in forwarding mode.
177 if bridge_is_forwarding ${interface}; then
178 log DEBUG "Bridge '${interface}' is in forwarding mode."
179 break
180 fi
181
182 counter=$(( ${counter} - 1 ))
183 sleep 1
184 done
185
186 # Tell the daemon, that we are not ready to go on.
187 if [ ${counter} -eq 0 ]; then
188 log ERROR "Bridge '${interface}' is not in forwarding mode."
189 log ERROR "Could not go on with getting a DHCP lease. Exiting."
190
191 exit 1
192 fi
193 fi
194
195 exit 0
196 ;;
197
198 BOUND|RENEW|REBIND|REBOOT)
199 # Check if the IP address has changed. If so, delete all routes and stuff.
200 if [ -n "${old_ip_address}" -a "${old_ip_address}" != "${new_ip_address}" ] || \
201 [ "${reason}" = "BOUND" ]; then
202 ipv4_flush_device ${interface}
203 fi
204
205 case "${reason}" in
206 BOUND|REBIND|REBOOT)
207 if [ ! "${old_ip_address}" = "${new_ip_address}" ] || \
208 [ ! "${old_subnet_mask}" = "${new_subnet_mask}" ] || \
209 [ ! "${old_network_number}" = "${new_network_number}" ] || \
210 [ ! "${old_broadcast_address}" = "${new_broadcast_address}" ] || \
211 [ ! "${old_routers}" = "${new_routers}" ] || \
212 [ ! "${old_interface_mtu}" = "${new_interface_mtu}" ] || \
213 [ "${reason}" = "BOUND" ]; then
214
215
216 # Calc the prefix from the subnet mask
217 new_prefix="$(ipv4_netmask2prefix "${new_subnet_mask}")"
218
219 # Set the new ip address.
220 ip_address_add "${interface}" "${new_ip_address}/${new_prefix}"
221
222 # A MTU of 576 is used for X.25 and dialup connections. Some broken DHCP
223 # servers send out an MTU of 576 bytes, which will be ignored.
224 if [ -n "${new_interface_mtu}" ] && [ ${new_interface_mtu} -gt 576 ]; then
225 device_set_mtu ${interface} ${new_interface_mtu}
226 fi
227
228 # Save configuration
229 db_set "${interface}/ipv4/type" "ipv4-dhcp"
230 db_set "${interface}/ipv4/local-ip-address" \
231 "${new_ip_address}/${new_prefix}"
232 db_set "${interface}/ipv4/remote-ip-address" "${new_routers}"
233 db_set "${interface}/ipv4/active" 1
234 db_set "${interface}/ipv4/domain-name" "${new_domain_name}"
235 db_set "${interface}/ipv4/domain-name-servers" \
236 "${new_domain_name_servers}"
237 db_set "${interface}/ipv4/domain-name-servers-priority" \
238 "${DNS_SERVER_DYNAMIC_PRIORITY}"
239
240 # Update the routing tables.
241 routing_update ${interface} ipv4
242 routing_default_update
243
244 # Update DNS configuration
245 dns_server_update
246 fi
247 ;;
248 esac
249
250 exit 0
251 ;;
252
253 EXPIRE|FAIL|RELEASE|STOP)
254 # Remove the currently configured addresses from the device.
255 if [ -n "${old_ip_address}" ]; then
256 ipv4_flush_device ${interface}
257 fi
258
259 db_delete "${interface}/ipv4"
260 routing_default_update
261
262 exit 0
263 ;;
264
265 *)
266 log ERROR "Unhandled reason: ${reason}"
267 ;;
268 esac
269
270 exit 1