debianutils: bump to version 4.8.1
[buildroot-gz.git] / package / dhcp / dhclient-script
blobcb292b6fbdf9fc21f57b6844b409b6479b7702d7
1 #!/bin/sh
3 # dhclient-script from OpenWRT project
4 # http://git.openwrt.org/?p=packages.git;a=blob;f=net/isc-dhcp/files/dhclient-script;h=4afebc0ad20ebac51c5baae5ed01c6713e3a0fd0;hb=HEAD
6 make_resolv_conf() {
7 if [ x"$new_domain_name_servers" != x ]; then
8 cat /dev/null > /etc/resolv.conf.dhclient
9 chmod 644 /etc/resolv.conf.dhclient
10 if [ x"$new_domain_search" != x ]; then
11 echo search $new_domain_search >> /etc/resolv.conf.dhclient
12 elif [ x"$new_domain_name" != x ]; then
13 # Note that the DHCP 'Domain Name Option' is really just a domain
14 # name, and that this practice of using the domain name option as
15 # a search path is both nonstandard and deprecated.
16 echo search $new_domain_name >> /etc/resolv.conf.dhclient
18 for nameserver in $new_domain_name_servers; do
19 echo nameserver $nameserver >>/etc/resolv.conf.dhclient
20 done
22 elif [ "x${new_dhcp6_name_servers}" != x ] ; then
23 cat /dev/null > /etc/resolv.conf.dhclient6
24 chmod 644 /etc/resolv.conf.dhclient6
26 if [ "x${new_dhcp6_domain_search}" != x ] ; then
27 echo search ${new_dhcp6_domain_search} >> /etc/resolv.conf.dhclient6
29 for nameserver in ${new_dhcp6_name_servers} ; do
30 echo nameserver ${nameserver} >> /etc/resolv.conf.dhclient6
31 done
34 # if both v4 and v6 clients are running, concatenate results
35 cat /etc/resolv.conf.* > /etc/resolv.conf
38 # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
39 exit_with_hooks() {
40 exit_status=$1
41 if [ -f /etc/dhclient-exit-hooks ]; then
42 . /etc/dhclient-exit-hooks
44 # probably should do something with exit status of the local script
45 exit $exit_status
48 # Invoke the local dhcp client enter hooks, if they exist.
49 if [ -f /etc/dhclient-enter-hooks ]; then
50 exit_status=0
51 . /etc/dhclient-enter-hooks
52 # allow the local script to abort processing of this state
53 # local script must set exit_status variable to nonzero.
54 if [ $exit_status -ne 0 ]; then
55 exit $exit_status
59 ###
60 ### DHCPv4 Handlers
61 ###
63 if [ x$new_broadcast_address != x ]; then
64 new_broadcast_arg="broadcast $new_broadcast_address"
66 if [ x$new_subnet_mask != x ]; then
67 new_subnet_arg="netmask $new_subnet_mask"
69 if [ x$alias_subnet_mask != x ]; then
70 alias_subnet_arg="netmask $alias_subnet_mask"
73 if [ x$reason = xMEDIUM ]; then
74 # Linux doesn't do mediums (ok, ok, media).
75 exit_with_hooks 0
78 if [ x$reason = xPREINIT ]; then
79 if [ x$alias_ip_address != x ]; then
80 # Bring down alias interface. Its routes will disappear too.
81 ifconfig $interface:0- 0.0.0.0
83 ifconfig $interface 0.0.0.0 up
85 # We need to give the kernel some time to get the interface up.
86 sleep 1
88 exit_with_hooks 0
91 if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
92 exit_with_hooks 0
95 if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
96 [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
97 current_hostname=`hostname`
98 if [ x$current_hostname = x ] || \
99 [ x$current_hostname = x$old_host_name ]; then
100 if [ x$current_hostname = x ] || \
101 [ x$new_host_name != x$old_host_name ]; then
102 hostname $new_host_name
106 if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
107 [ x$alias_ip_address != x$old_ip_address ]; then
108 # Possible new alias. Remove old alias.
109 ifconfig $interface:0- 0.0.0.0
111 if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
112 # IP address changed. Bringing down the interface will delete all routes,
113 # and clear the ARP cache.
114 ifconfig $interface 0.0.0.0 down
117 if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
118 [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
120 ifconfig $interface $new_ip_address $new_subnet_arg \
121 $new_broadcast_arg
122 for router in $new_routers; do
123 if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
124 route add -host $router dev $interface
126 route add default gw $router
127 done
129 if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
130 then
131 ifconfig $interface:0- 0.0.0.0
132 ifconfig $interface:0 $alias_ip_address $alias_subnet_arg
133 route add -host $alias_ip_address $interface:0
135 make_resolv_conf
136 exit_with_hooks 0
139 if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
140 || [ x$reason = xSTOP ]; then
141 if [ x$alias_ip_address != x ]; then
142 # Turn off alias interface.
143 ifconfig $interface:0- 0.0.0.0
145 if [ x$old_ip_address != x ]; then
146 # Shut down interface, which will delete routes and clear arp cache.
147 ifconfig $interface 0.0.0.0 down
149 if [ x$alias_ip_address != x ]; then
150 ifconfig $interface:0 $alias_ip_address $alias_subnet_arg
151 route add -host $alias_ip_address $interface:0
154 # remove v4 dns configuration for this interface
155 rm /etc/resolv.conf.dhclient
156 cat /etc/resolv.conf.* > /etc/resolv.conf
158 exit_with_hooks 0
161 if [ x$reason = xTIMEOUT ]; then
162 if [ x$alias_ip_address != x ]; then
163 ifconfig $interface:0- 0.0.0.0
165 ifconfig $interface $new_ip_address $new_subnet_arg \
166 $new_broadcast_arg
167 set $new_routers
168 if ping -q -c 1 $1; then
169 if [ x$new_ip_address != x$alias_ip_address ] && \
170 [ x$alias_ip_address != x ]; then
171 ifconfig $interface:0 $alias_ip_address $alias_subnet_arg
172 route add -host $alias_ip_address dev $interface:0
174 for router in $new_routers; do
175 if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
176 route add -host $router dev $interface
178 route add default gw $router
179 done
180 make_resolv_conf
181 exit_with_hooks 0
183 ifconfig $interface 0.0.0.0 down
184 exit_with_hooks 1
188 ### DHCPv6 Handlers
191 if [ x$reason = xPREINIT6 ]; then
192 # Ensure interface is up.
193 ifconfig ${interface} up
195 # Remove any stale addresses from aborted clients.
196 ip -f inet6 addr flush dev ${interface} scope global
198 exit_with_hooks 0
201 if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ] ; then
202 echo Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix}
204 exit_with_hooks 0
207 if [ x$reason = xBOUND6 ]; then
208 if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then
209 exit_with_hooks 2;
212 ifconfig ${interface} add ${new_ip6_address}/${new_ip6_prefixlen}
214 # Check for nameserver options.
215 make_resolv_conf
217 ### <<
218 # Set up softwire tunnel
219 if [ x${new_dhcp6_softwire} != x ] ; then
220 /etc/init.d/dhclient stop
221 ifconfig ${interface} 0.0.0.0
222 ip -6 tunnel add tun0 mode ipip6 \
223 remote ${new_dhcp6_softwire} \
224 local ${new_ip6_address} \
225 dev ${interface} encaplimit none
226 ip link set tun0 up
227 ip route add default dev tun0
229 ### >>
231 exit_with_hooks 0
234 if [ x$reason = xRENEW6 ] || [ x$reason = xREBIND6 ]; then
235 if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then
236 exit_with_hooks 2;
239 ifconfig ${interface} add ${new_ip6_address}/${new_ip6_prefixlen}
241 # Make sure nothing has moved around on us.
243 # Nameservers/domains/etc.
244 if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
245 [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then
246 make_resolv_conf
249 exit_with_hooks 0
252 if [ x$reason = xDEPREF6 ]; then
253 if [ x${new_ip6_address} = x ] ; then
254 exit_with_hooks 2;
257 # Busybox ifconfig has no way to communicate this to the kernel, so ignore it
259 exit_with_hooks 0
262 if [ x$reason = xEXPIRE6 -o x$reason = xRELEASE6 -o x$reason = xSTOP6 ]; then
263 if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then
264 exit_with_hooks 2;
267 ifconfig ${interface} del ${old_ip6_address}/${old_ip6_prefixlen}
269 # remove v6 dns configuration for this interface
270 rm /etc/resolv.conf.dhclient6
271 cat /etc/resolv.conf.* > /etc/resolv.conf
273 ### <<
274 # Tear down softwire tunnel
275 if [ x${old_dhcp6_softwire} != x ] ; then
276 ip link set tun0 down
277 ip tunnel del tun0
279 ### >>
281 exit_with_hooks 0
284 exit_with_hooks 0