]>
Commit | Line | Data |
---|---|---|
b606a60a | 1 | #!/bin/sh |
1397e9b3 ER |
2 | # dhclient-script for Linux. Dan Halbert, March, 1997. |
3 | # Updated for Linux 2.[12] by Brian J. Murrell, January 1999. | |
1397e9b3 ER |
4 | |
5 | # Notes: | |
6 | ||
7 | # 0. This script is based on the netbsd script supplied with dhcp-970306. | |
8 | ||
b606a60a | 9 | # 1. This script was modified to work with iproute2 |
1397e9b3 | 10 | |
b606a60a MB |
11 | if [ -n "$dhc_dbus" -a -x /usr/bin/dbus-send ]; then |
12 | /usr/bin/dbus-send \ | |
13 | --system \ | |
14 | --dest=com.redhat.dhcp \ | |
15 | --type=method_call \ | |
16 | /com/redhat/dhcp/$interface \ | |
17 | com.redhat.dhcp.set \ | |
18 | 'string:'"$(unset PATH SHLVL _ PWD dhc_dbus ; env)" | |
1397e9b3 | 19 | |
b606a60a MB |
20 | if [ $(($dhc_dbus & 31)) -eq 31 ]; then |
21 | exit 0 | |
22 | fi | |
23 | fi | |
1397e9b3 | 24 | |
b606a60a MB |
25 | calc_prefix() |
26 | { | |
27 | local old_IFS=$IFS | |
28 | IFS='.' | |
29 | local netmask=$(echo $1) | |
30 | IFS=$old_IFS | |
1397e9b3 | 31 | |
b606a60a MB |
32 | local ret=0 |
33 | local endp=0 | |
34 | for n in $netmask ; do | |
35 | for i in 128 64 32 16 8 4 2 1 ; do | |
36 | if [ $(($n & $i)) -ne 0 ]; then | |
37 | if [ $endp -eq 0 ]; then | |
38 | ret=$(($ret + 1)) | |
39 | else | |
40 | echo "32" | |
41 | return | |
42 | fi | |
43 | else | |
44 | endp=1 | |
45 | fi | |
46 | done | |
47 | done | |
48 | echo $ret | |
49 | } | |
1397e9b3 ER |
50 | |
51 | make_resolv_conf() { | |
b606a60a MB |
52 | if [ -n "$new_domain_name_servers" ]; then |
53 | :> /etc/resolv.conf.dhclient | |
54 | chmod 644 /etc/resolv.conf.dhclient | |
55 | if [ -n "$new_domain_search" ]; then | |
56 | echo search $new_domain_search >> /etc/resolv.conf.dhclient | |
57 | elif [ -n "$new_domain_name" ]; then | |
58 | # Note that the DHCP 'Domain Name Option' is really just a domain | |
59 | # name, and that this practice of using the domain name option as | |
60 | # a search path is both nonstandard and deprecated. | |
61 | echo search $new_domain_name >> /etc/resolv.conf.dhclient | |
62 | fi | |
63 | for nameserver in $new_domain_name_servers; do | |
64 | echo nameserver $nameserver >>/etc/resolv.conf.dhclient | |
65 | done | |
66 | mv /etc/resolv.conf.dhclient /etc/resolv.conf | |
67 | elif [ -n "${new_dhcp6_name_servers}" ] ; then | |
68 | :> /etc/resolv.conf.dhclient6 | |
69 | chmod 644 /etc/resolv.conf.dhclient6 | |
70 | ||
71 | if [ "x${new_dhcp6_domain_search}" != x ] ; then | |
72 | echo search ${new_dhcp6_domain_search} >> /etc/resolv.conf.dhclient6 | |
73 | fi | |
74 | for nameserver in ${new_dhcp6_name_servers} ; do | |
75 | echo nameserver ${nameserver} >> /etc/resolv.conf.dhclient6 | |
76 | done | |
77 | mv /etc/resolv.conf.dhclient6 /etc/resolv.conf | |
78 | fi | |
1397e9b3 ER |
79 | } |
80 | ||
b606a60a MB |
81 | # Must be used on exit. |
82 | # Invokes the local dhcp client exit hooks, if any. | |
1397e9b3 | 83 | exit_with_hooks() { |
b606a60a | 84 | exit_status=$1 |
51ba29e1 JK |
85 | |
86 | for i in /etc/dhclient-exit-hooks.d/*[!~,] ; do | |
87 | [ -f "$i" ] || continue | |
88 | # Don't run *.{rpmsave,rpmorig,rpmnew,swp} scripts | |
89 | [ "${i%.rpmsave}" != "${i}" ] && continue | |
90 | [ "${i%.rpmorig}" != "${i}" ] && continue | |
91 | [ "${i%.rpmnew}" != "${i}" ] && continue | |
92 | [ "${i%.swp}" != "${i}" ] && continue | |
93 | [ "${i%,v}" != "${i}" ] && continue | |
94 | ||
95 | . ${i} | |
96 | done | |
b606a60a MB |
97 | # probably should do something with exit status of the local script |
98 | exit $exit_status | |
1397e9b3 ER |
99 | } |
100 | ||
51ba29e1 JK |
101 | exit_status=0 |
102 | for i in /etc/dhclient-enter-hooks.d/*[!~,] ; do | |
103 | [ -f "$i" ] || continue | |
104 | # Don't run *.{rpmsave,rpmorig,rpmnew,swp} scripts | |
105 | [ "${i%.rpmsave}" != "${i}" ] && continue | |
106 | [ "${i%.rpmorig}" != "${i}" ] && continue | |
107 | [ "${i%.rpmnew}" != "${i}" ] && continue | |
108 | [ "${i%.swp}" != "${i}" ] && continue | |
109 | [ "${i%,v}" != "${i}" ] && continue | |
b606a60a | 110 | |
51ba29e1 JK |
111 | . ${i} |
112 | ||
b606a60a MB |
113 | if [ $exit_status -ne 0 ]; then |
114 | exit $exit_status | |
115 | fi | |
51ba29e1 | 116 | done |
1397e9b3 | 117 | |
b606a60a MB |
118 | ### |
119 | ### DHCPv4 Handlers | |
120 | ### | |
1397e9b3 | 121 | |
b606a60a MB |
122 | if [ -n "$new_broadcast_address" ]; then |
123 | new_broadcast_arg="broadcast $new_broadcast_address" | |
1397e9b3 | 124 | fi |
b606a60a MB |
125 | if [ -n "$old_broadcast_address" ]; then |
126 | old_broadcast_arg="broadcast $old_broadcast_address" | |
1397e9b3 | 127 | fi |
b606a60a MB |
128 | if [ -n "$new_subnet_mask" ]; then |
129 | new_subnet_arg="$(calc_prefix $new_subnet_mask)" | |
1397e9b3 | 130 | fi |
b606a60a MB |
131 | if [ -n "$old_subnet_mask" ]; then |
132 | old_subnet_arg="$(calc_prefix $old_subnet_mask)" | |
1397e9b3 | 133 | fi |
b606a60a MB |
134 | if [ -n "$alias_subnet_mask" ]; then |
135 | alias_subnet_arg="$(calc_prefix $alias_subnet_mask)" | |
1397e9b3 | 136 | fi |
b606a60a MB |
137 | [ -z "new_subnet_arg" ] && $new_subnet_arg="32" |
138 | [ -z "old_subnet_arg" ] && $old_subnet_arg="32" | |
139 | [ -z "alias_subnet_arg" ] && $alias_subnet_arg="32" | |
1397e9b3 | 140 | |
b606a60a MB |
141 | case "$reason" in |
142 | MEDIUM) | |
143 | # Linux doesn't do mediums (ok, ok, media). | |
144 | ;; | |
1397e9b3 | 145 | |
b606a60a MB |
146 | PREINIT) |
147 | if [ -n "$alias_ip_address" ]; then | |
148 | /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
149 | fi | |
150 | /sbin/ip link set $interface up | |
1397e9b3 | 151 | |
b606a60a MB |
152 | # We need to give the kernel some time to get the interface up. |
153 | sleep 1 | |
154 | ;; | |
1397e9b3 | 155 | |
b606a60a MB |
156 | BOUND|RENEW|REBIND|REBOOT) |
157 | current_hostname=`hostname` | |
158 | if [ -z "$current_hostname" ]; then | |
159 | hostname $new_host_name | |
160 | elif [ "$current_hostname" = "$old_host_name" -a \ | |
161 | "$new_host_name" != "$old_host_name" ]; then | |
162 | hostname $new_host_name | |
163 | fi | |
1397e9b3 | 164 | |
b606a60a MB |
165 | if [ -n "$old_ip_address" -a -n "$alias_ip_address" -a \ |
166 | "$alias_ip_address" != "$old_ip_address" ]; then | |
167 | # Possible new alias. Remove old alias. | |
168 | /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
169 | fi | |
170 | if [ -n "$old_ip_address" -a "$old_ip_address" != "$new_ip_address" ]; then | |
171 | # IP address changed. | |
172 | /sbin/ip addr del $old_ip_address/$old_subnet_arg dev $interface | |
173 | for router in $old_routers; do | |
174 | /sbin/ip route del default via $router | |
175 | done | |
176 | if [ -n "$old_static_routes" ]; then | |
177 | set -- $old_static_routes | |
178 | while [ $# -gt 1 ]; do | |
179 | /sbin/ip route del $1 via $2 | |
180 | shift; shift | |
181 | done | |
182 | fi | |
183 | fi | |
184 | if [ -z "$old_ip_address" -o "$old_ip_address" != "$new_ip_address" -o \ | |
185 | "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then | |
186 | /sbin/ip addr add $new_ip_address/$new_subnet_arg $new_broadcast_arg dev $interface | |
187 | # Add a network route to the computed network address. | |
188 | for router in $new_routers; do | |
189 | /sbin/ip route add default via $router | |
190 | done | |
191 | if [ -n "$new_static_routes" ]; then | |
192 | set -- $new_static_routes | |
193 | while [ $# -gt 1 ]; do | |
194 | /sbin/ip route add $1 via $2 | |
195 | shift; shift | |
196 | done | |
197 | fi | |
198 | fi | |
199 | if [ -n "$alias_ip_address" -a "$new_ip_address" != "$alias_ip_address" ]; then | |
200 | /sbin/ip addr add $alias_ip_address/$alias_subnet_arg dev $interface | |
201 | fi | |
202 | make_resolv_conf | |
203 | ;; | |
204 | ||
205 | EXPIRE|FAIL|RELEASE|STOP) | |
206 | if [ -n "$alias_ip_address" ]; then | |
207 | # Turn off alias interface. | |
208 | /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
209 | fi | |
210 | if [ -n "$old_ip_address" ]; then | |
211 | /sbin/ip addr del $old_ip_address/$old_subnet_arg dev $interface | |
212 | for router in $old_routers; do | |
213 | /sbin/ip route del default via $router | |
214 | done | |
215 | if [ -n "$old_static_routes" ]; then | |
216 | set -- $old_static_routes | |
217 | while [ $# -gt 1 ]; do | |
218 | /sbin/ip route del $1 via $2 | |
219 | shift; shift | |
220 | done | |
221 | fi | |
222 | fi | |
223 | if [ -n "$alias_ip_address" ]; then | |
224 | /sbin/ip addr add $alias_ip_address/$alias_subnet_arg dev $interface | |
225 | fi | |
226 | ;; | |
227 | ||
228 | TIMEOUT) | |
229 | if [ -n "$alias_ip_address" ]; then | |
230 | /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
231 | fi | |
232 | /sbin/ip addr add $new_ip_address/$new_subnet_arg $new_broadcast_arg dev $interface | |
233 | ||
234 | set $new_routers | |
235 | if ping -q -c 1 $1; then | |
236 | if [ "$new_ip_address" != "$alias_ip_address" ] && \ | |
237 | [ -n "$alias_ip_address" ]; then | |
238 | /sbin/ip addr add $alias_ip_address/$alias_subnet_arg dev $interface | |
239 | fi | |
240 | for router in $new_routers; do | |
241 | /sbin/ip route add default via $router | |
242 | done | |
243 | if [ -n "$new_static_routes" ]; then | |
244 | set -- $new_static_routes | |
245 | while [ $# -gt 1 ]; do | |
246 | /sbin/ip route add $1 via $2 | |
247 | shift; shift | |
248 | done | |
249 | fi | |
250 | make_resolv_conf | |
251 | exit_with_hooks 0 | |
252 | fi | |
253 | ||
254 | /sbin/ip addr del $new_ip_address/$new_subnet_arg dev $interface | |
255 | for router in $old_routers; do | |
256 | /sbin/ip route del default via $router | |
257 | done | |
258 | if [ -n "$old_static_routes" ]; then | |
259 | set -- $old_static_routes | |
260 | while [ $# -gt 1 ]; do | |
261 | /sbin/ip route del $1 via $2 | |
262 | shift; shift | |
263 | done | |
264 | fi | |
265 | exit_with_hooks 1 | |
266 | ;; | |
267 | ||
268 | PREINIT6) | |
269 | # Ensure interface is up. | |
270 | /sbin/ip link set ${interface} up | |
271 | ||
272 | # Remove any stale addresses from aborted clients. | |
273 | /sbin/ip -f inet6 addr flush dev ${interface} scope global permanent | |
274 | ||
275 | exit_with_hooks 0 | |
276 | ;; | |
277 | BOUND6) | |
278 | if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then | |
279 | exit_with_hooks 2; | |
280 | fi | |
281 | ||
282 | /sbin/ip -f inet6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \ | |
283 | dev ${interface} scope global | |
284 | ||
285 | # Check for nameserver options. | |
286 | make_resolv_conf | |
287 | ||
288 | exit_with_hooks 0 | |
289 | ;; | |
290 | ||
291 | RENEW6|REBIND6) | |
292 | # Make sure nothing has moved around on us. | |
293 | ||
294 | # Nameservers/domains/etc. | |
295 | if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] || | |
296 | [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then | |
297 | make_resolv_conf | |
298 | fi | |
299 | ||
300 | exit_with_hooks 0 | |
301 | ;; | |
302 | ||
303 | DEPREF6) | |
304 | if [ x${new_ip6_prefixlen} = x ] ; then | |
305 | exit_with_hooks 2; | |
306 | fi | |
307 | ||
308 | # There doesn't appear to be a way to update an addr to indicate | |
309 | # preference. | |
310 | # /sbin/ip -f inet6 addr ??? ${new_ip6_address}/${new_ip6_prefixlen} \ | |
311 | # dev ${interface} scope global deprecated? | |
312 | ||
313 | exit_with_hooks 0 | |
314 | ;; | |
315 | ||
316 | EXPIRE6|RELEASE6|STOP6) | |
317 | if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then | |
318 | exit_with_hooks 2; | |
319 | fi | |
320 | ||
321 | /sbin/ip -f inet6 addr del ${old_ip6_address}/${old_ip6_prefixlen} \ | |
322 | dev ${interface} | |
323 | ||
324 | exit_with_hooks 0 | |
325 | ;; | |
326 | ||
327 | *) | |
328 | ;; | |
329 | esac | |
1397e9b3 ER |
330 | |
331 | exit_with_hooks 0 |