]>
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 MB |
84 | exit_status=$1 |
85 | if [ -f /etc/dhclient-exit-hooks ]; then | |
86 | . /etc/dhclient-exit-hooks | |
87 | fi | |
88 | # probably should do something with exit status of the local script | |
89 | exit $exit_status | |
1397e9b3 ER |
90 | } |
91 | ||
b606a60a | 92 | |
1397e9b3 ER |
93 | # Invoke the local dhcp client enter hooks, if they exist. |
94 | if [ -f /etc/dhclient-enter-hooks ]; then | |
b606a60a MB |
95 | exit_status=0 |
96 | . /etc/dhclient-enter-hooks | |
97 | # allow the local script to abort processing of this state | |
98 | # local script must set exit_status variable to nonzero. | |
99 | if [ $exit_status -ne 0 ]; then | |
100 | exit $exit_status | |
101 | fi | |
1397e9b3 ER |
102 | fi |
103 | ||
b606a60a MB |
104 | ### |
105 | ### DHCPv4 Handlers | |
106 | ### | |
1397e9b3 | 107 | |
b606a60a MB |
108 | if [ -n "$new_broadcast_address" ]; then |
109 | new_broadcast_arg="broadcast $new_broadcast_address" | |
1397e9b3 | 110 | fi |
b606a60a MB |
111 | if [ -n "$old_broadcast_address" ]; then |
112 | old_broadcast_arg="broadcast $old_broadcast_address" | |
1397e9b3 | 113 | fi |
b606a60a MB |
114 | if [ -n "$new_subnet_mask" ]; then |
115 | new_subnet_arg="$(calc_prefix $new_subnet_mask)" | |
1397e9b3 | 116 | fi |
b606a60a MB |
117 | if [ -n "$old_subnet_mask" ]; then |
118 | old_subnet_arg="$(calc_prefix $old_subnet_mask)" | |
1397e9b3 | 119 | fi |
b606a60a MB |
120 | if [ -n "$alias_subnet_mask" ]; then |
121 | alias_subnet_arg="$(calc_prefix $alias_subnet_mask)" | |
1397e9b3 | 122 | fi |
b606a60a MB |
123 | [ -z "new_subnet_arg" ] && $new_subnet_arg="32" |
124 | [ -z "old_subnet_arg" ] && $old_subnet_arg="32" | |
125 | [ -z "alias_subnet_arg" ] && $alias_subnet_arg="32" | |
1397e9b3 | 126 | |
b606a60a MB |
127 | case "$reason" in |
128 | MEDIUM) | |
129 | # Linux doesn't do mediums (ok, ok, media). | |
130 | ;; | |
1397e9b3 | 131 | |
b606a60a MB |
132 | PREINIT) |
133 | if [ -n "$alias_ip_address" ]; then | |
134 | /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
135 | fi | |
136 | /sbin/ip link set $interface up | |
1397e9b3 | 137 | |
b606a60a MB |
138 | # We need to give the kernel some time to get the interface up. |
139 | sleep 1 | |
140 | ;; | |
1397e9b3 | 141 | |
b606a60a MB |
142 | BOUND|RENEW|REBIND|REBOOT) |
143 | current_hostname=`hostname` | |
144 | if [ -z "$current_hostname" ]; then | |
145 | hostname $new_host_name | |
146 | elif [ "$current_hostname" = "$old_host_name" -a \ | |
147 | "$new_host_name" != "$old_host_name" ]; then | |
148 | hostname $new_host_name | |
149 | fi | |
1397e9b3 | 150 | |
b606a60a MB |
151 | if [ -n "$old_ip_address" -a -n "$alias_ip_address" -a \ |
152 | "$alias_ip_address" != "$old_ip_address" ]; then | |
153 | # Possible new alias. Remove old alias. | |
154 | /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
155 | fi | |
156 | if [ -n "$old_ip_address" -a "$old_ip_address" != "$new_ip_address" ]; then | |
157 | # IP address changed. | |
158 | /sbin/ip addr del $old_ip_address/$old_subnet_arg dev $interface | |
159 | for router in $old_routers; do | |
160 | /sbin/ip route del default via $router | |
161 | done | |
162 | if [ -n "$old_static_routes" ]; then | |
163 | set -- $old_static_routes | |
164 | while [ $# -gt 1 ]; do | |
165 | /sbin/ip route del $1 via $2 | |
166 | shift; shift | |
167 | done | |
168 | fi | |
169 | fi | |
170 | if [ -z "$old_ip_address" -o "$old_ip_address" != "$new_ip_address" -o \ | |
171 | "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then | |
172 | /sbin/ip addr add $new_ip_address/$new_subnet_arg $new_broadcast_arg dev $interface | |
173 | # Add a network route to the computed network address. | |
174 | for router in $new_routers; do | |
175 | /sbin/ip route add default via $router | |
176 | done | |
177 | if [ -n "$new_static_routes" ]; then | |
178 | set -- $new_static_routes | |
179 | while [ $# -gt 1 ]; do | |
180 | /sbin/ip route add $1 via $2 | |
181 | shift; shift | |
182 | done | |
183 | fi | |
184 | fi | |
185 | if [ -n "$alias_ip_address" -a "$new_ip_address" != "$alias_ip_address" ]; then | |
186 | /sbin/ip addr add $alias_ip_address/$alias_subnet_arg dev $interface | |
187 | fi | |
188 | make_resolv_conf | |
189 | ;; | |
190 | ||
191 | EXPIRE|FAIL|RELEASE|STOP) | |
192 | if [ -n "$alias_ip_address" ]; then | |
193 | # Turn off alias interface. | |
194 | /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
195 | fi | |
196 | if [ -n "$old_ip_address" ]; then | |
197 | /sbin/ip addr del $old_ip_address/$old_subnet_arg dev $interface | |
198 | for router in $old_routers; do | |
199 | /sbin/ip route del default via $router | |
200 | done | |
201 | if [ -n "$old_static_routes" ]; then | |
202 | set -- $old_static_routes | |
203 | while [ $# -gt 1 ]; do | |
204 | /sbin/ip route del $1 via $2 | |
205 | shift; shift | |
206 | done | |
207 | fi | |
208 | fi | |
209 | if [ -n "$alias_ip_address" ]; then | |
210 | /sbin/ip addr add $alias_ip_address/$alias_subnet_arg dev $interface | |
211 | fi | |
212 | ;; | |
213 | ||
214 | TIMEOUT) | |
215 | if [ -n "$alias_ip_address" ]; then | |
216 | /sbin/ip addr del $alias_ip_address/$alias_subnet_arg dev $interface | |
217 | fi | |
218 | /sbin/ip addr add $new_ip_address/$new_subnet_arg $new_broadcast_arg dev $interface | |
219 | ||
220 | set $new_routers | |
221 | if ping -q -c 1 $1; then | |
222 | if [ "$new_ip_address" != "$alias_ip_address" ] && \ | |
223 | [ -n "$alias_ip_address" ]; then | |
224 | /sbin/ip addr add $alias_ip_address/$alias_subnet_arg dev $interface | |
225 | fi | |
226 | for router in $new_routers; do | |
227 | /sbin/ip route add default via $router | |
228 | done | |
229 | if [ -n "$new_static_routes" ]; then | |
230 | set -- $new_static_routes | |
231 | while [ $# -gt 1 ]; do | |
232 | /sbin/ip route add $1 via $2 | |
233 | shift; shift | |
234 | done | |
235 | fi | |
236 | make_resolv_conf | |
237 | exit_with_hooks 0 | |
238 | fi | |
239 | ||
240 | /sbin/ip addr del $new_ip_address/$new_subnet_arg dev $interface | |
241 | for router in $old_routers; do | |
242 | /sbin/ip route del default via $router | |
243 | done | |
244 | if [ -n "$old_static_routes" ]; then | |
245 | set -- $old_static_routes | |
246 | while [ $# -gt 1 ]; do | |
247 | /sbin/ip route del $1 via $2 | |
248 | shift; shift | |
249 | done | |
250 | fi | |
251 | exit_with_hooks 1 | |
252 | ;; | |
253 | ||
254 | PREINIT6) | |
255 | # Ensure interface is up. | |
256 | /sbin/ip link set ${interface} up | |
257 | ||
258 | # Remove any stale addresses from aborted clients. | |
259 | /sbin/ip -f inet6 addr flush dev ${interface} scope global permanent | |
260 | ||
261 | exit_with_hooks 0 | |
262 | ;; | |
263 | BOUND6) | |
264 | if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then | |
265 | exit_with_hooks 2; | |
266 | fi | |
267 | ||
268 | /sbin/ip -f inet6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \ | |
269 | dev ${interface} scope global | |
270 | ||
271 | # Check for nameserver options. | |
272 | make_resolv_conf | |
273 | ||
274 | exit_with_hooks 0 | |
275 | ;; | |
276 | ||
277 | RENEW6|REBIND6) | |
278 | # Make sure nothing has moved around on us. | |
279 | ||
280 | # Nameservers/domains/etc. | |
281 | if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] || | |
282 | [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then | |
283 | make_resolv_conf | |
284 | fi | |
285 | ||
286 | exit_with_hooks 0 | |
287 | ;; | |
288 | ||
289 | DEPREF6) | |
290 | if [ x${new_ip6_prefixlen} = x ] ; then | |
291 | exit_with_hooks 2; | |
292 | fi | |
293 | ||
294 | # There doesn't appear to be a way to update an addr to indicate | |
295 | # preference. | |
296 | # /sbin/ip -f inet6 addr ??? ${new_ip6_address}/${new_ip6_prefixlen} \ | |
297 | # dev ${interface} scope global deprecated? | |
298 | ||
299 | exit_with_hooks 0 | |
300 | ;; | |
301 | ||
302 | EXPIRE6|RELEASE6|STOP6) | |
303 | if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then | |
304 | exit_with_hooks 2; | |
305 | fi | |
306 | ||
307 | /sbin/ip -f inet6 addr del ${old_ip6_address}/${old_ip6_prefixlen} \ | |
308 | dev ${interface} | |
309 | ||
310 | exit_with_hooks 0 | |
311 | ;; | |
312 | ||
313 | *) | |
314 | ;; | |
315 | esac | |
1397e9b3 ER |
316 | |
317 | exit_with_hooks 0 |