cb292b6fbdf9fc21f57b6844b409b6479b7702d7
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / package / dhcp / dhclient-script
1 #!/bin/sh
2
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
5
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
17     fi
18     for nameserver in $new_domain_name_servers; do
19       echo nameserver $nameserver >>/etc/resolv.conf.dhclient
20     done
21
22   elif [ "x${new_dhcp6_name_servers}" != x ] ; then
23     cat /dev/null > /etc/resolv.conf.dhclient6
24     chmod 644 /etc/resolv.conf.dhclient6
25
26     if [ "x${new_dhcp6_domain_search}" != x ] ; then
27       echo search ${new_dhcp6_domain_search} >> /etc/resolv.conf.dhclient6
28     fi
29     for nameserver in ${new_dhcp6_name_servers} ; do
30       echo nameserver ${nameserver} >> /etc/resolv.conf.dhclient6
31     done
32   fi
33
34   # if both v4 and v6 clients are running, concatenate results
35   cat /etc/resolv.conf.* > /etc/resolv.conf
36 }
37
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
43   fi
44 # probably should do something with exit status of the local script
45   exit $exit_status
46 }
47
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
56   fi
57 fi
58
59 ###
60 ### DHCPv4 Handlers
61 ###
62
63 if [ x$new_broadcast_address != x ]; then
64   new_broadcast_arg="broadcast $new_broadcast_address"
65 fi
66 if [ x$new_subnet_mask != x ]; then
67   new_subnet_arg="netmask $new_subnet_mask"
68 fi
69 if [ x$alias_subnet_mask != x ]; then
70   alias_subnet_arg="netmask $alias_subnet_mask"
71 fi
72
73 if [ x$reason = xMEDIUM ]; then
74   # Linux doesn't do mediums (ok, ok, media).
75   exit_with_hooks 0
76 fi
77
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
82   fi
83   ifconfig $interface 0.0.0.0 up
84
85   # We need to give the kernel some time to get the interface up.
86   sleep 1
87
88   exit_with_hooks 0
89 fi
90
91 if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
92   exit_with_hooks 0
93 fi
94   
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
103     fi
104   fi
105     
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
110   fi
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
115
116   fi
117   if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
118      [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
119
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
125       fi
126       route add default gw $router
127     done
128   fi
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
134   fi
135   make_resolv_conf
136   exit_with_hooks 0
137 fi
138
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
144   fi
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
148   fi
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
152   fi
153
154   # remove v4 dns configuration for this interface
155   rm /etc/resolv.conf.dhclient
156   cat /etc/resolv.conf.* > /etc/resolv.conf
157
158   exit_with_hooks 0
159 fi
160
161 if [ x$reason = xTIMEOUT ]; then
162   if [ x$alias_ip_address != x ]; then
163     ifconfig $interface:0- 0.0.0.0
164   fi
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
173     fi
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
177       fi
178       route add default gw $router
179     done
180     make_resolv_conf
181     exit_with_hooks 0
182   fi
183   ifconfig $interface 0.0.0.0 down
184   exit_with_hooks 1
185 fi
186
187 ###
188 ### DHCPv6 Handlers
189 ###
190
191 if [ x$reason = xPREINIT6 ]; then
192   # Ensure interface is up.
193   ifconfig ${interface} up
194
195   # Remove any stale addresses from aborted clients.
196   ip -f inet6 addr flush dev ${interface} scope global
197
198   exit_with_hooks 0
199 fi
200
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}
203
204     exit_with_hooks 0
205 fi
206
207 if [ x$reason = xBOUND6 ]; then
208   if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then
209     exit_with_hooks 2;
210   fi
211
212   ifconfig ${interface} add ${new_ip6_address}/${new_ip6_prefixlen}
213
214   # Check for nameserver options.
215   make_resolv_conf
216
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
228   fi
229 ### >>
230
231   exit_with_hooks 0
232 fi
233
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;
237   fi
238
239   ifconfig ${interface} add ${new_ip6_address}/${new_ip6_prefixlen}
240
241   # Make sure nothing has moved around on us.
242
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
247   fi
248
249   exit_with_hooks 0
250 fi
251
252 if [ x$reason = xDEPREF6 ]; then
253   if [ x${new_ip6_address} = x ] ; then
254     exit_with_hooks 2;
255   fi
256
257   # Busybox ifconfig has no way to communicate this to the kernel, so ignore it
258
259   exit_with_hooks 0
260 fi
261
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;
265   fi
266
267   ifconfig ${interface} del ${old_ip6_address}/${old_ip6_prefixlen}
268
269   # remove v6 dns configuration for this interface
270   rm /etc/resolv.conf.dhclient6
271   cat /etc/resolv.conf.* > /etc/resolv.conf
272
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
278   fi
279 ### >>
280
281   exit_with_hooks 0
282 fi
283
284 exit_with_hooks 0