2 # dhclient-script for Linux. Dan Halbert, March, 1997.
3 # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
4 # No guarantees about this. I'm a novice at the details of Linux
9 # 0. This script is based on the netbsd script supplied with dhcp-970306.
11 # 1. ifconfig down apparently deletes all relevant routes and flushes
12 # the arp cache, so this doesn't need to be done explicitly.
14 # 2. The alias address handling here has not been tested AT ALL.
15 # I'm just going by the doc of modern Linux ip aliasing, which uses
16 # notations like eth0:0, eth0:1, for each alias.
18 # 3. I have to calculate the network address, and calculate the broadcast
19 # address if it is not supplied. This might be much more easily done
20 # by the dhclient C code, and passed on.
22 # 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
23 # of the $1 in its args.
25 # 'ip' just looks too weird. /sbin/ip looks less weird.
29 if [ x
"$new_domain_name_servers" != x
]; then
30 cat /dev
/null
> /etc
/resolv.conf.dhclient
31 chmod 644 /etc
/resolv.conf.dhclient
32 if [ x
"$new_domain_search" != x
]; then
33 echo search
$new_domain_search >> /etc
/resolv.conf.dhclient
34 elif [ x
"$new_domain_name" != x
]; then
35 # Note that the DHCP 'Domain Name Option' is really just a domain
36 # name, and that this practice of using the domain name option as
37 # a search path is both nonstandard and deprecated.
38 echo search
$new_domain_name >> /etc
/resolv.conf.dhclient
40 for nameserver
in $new_domain_name_servers; do
41 echo nameserver
$nameserver >>/etc
/resolv.conf.dhclient
44 mv /etc
/resolv.conf.dhclient
/etc
/resolv.conf
45 elif [ "x${new_dhcp6_name_servers}" != x
] ; then
46 cat /dev
/null
> /etc
/resolv.conf.dhclient6
47 chmod 644 /etc
/resolv.conf.dhclient6
49 if [ "x${new_dhcp6_domain_search}" != x
] ; then
50 echo search
${new_dhcp6_domain_search} >> /etc
/resolv.conf.dhclient6
53 for nameserver
in ${new_dhcp6_name_servers} ; do
54 # If the nameserver has a link-local address
55 # add a <zone_id> (interface name) to it.
56 if [[ "$nameserver" =~ ^fe80
:: ]]
62 echo nameserver
${nameserver}$zone_id >> /etc
/resolv.conf.dhclient6
66 mv /etc
/resolv.conf.dhclient6
/etc
/resolv.conf
70 # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
73 if [ -f /etc
/dhclient-exit-hooks
]; then
74 .
/etc
/dhclient-exit-hooks
76 # probably should do something with exit status of the local script
80 # Invoke the local dhcp client enter hooks, if they exist.
81 if [ -f /etc
/dhclient-enter-hooks
]; then
83 .
/etc
/dhclient-enter-hooks
84 # allow the local script to abort processing of this state
85 # local script must set exit_status variable to nonzero.
86 if [ $exit_status -ne 0 ]; then
95 if [ x
$new_broadcast_address != x
]; then
96 new_broadcast_arg
="broadcast $new_broadcast_address"
98 if [ x
$old_broadcast_address != x
]; then
99 old_broadcast_arg
="broadcast $old_broadcast_address"
101 if [ x
$new_subnet_mask != x
]; then
102 new_subnet_arg
="netmask $new_subnet_mask"
104 if [ x
$old_subnet_mask != x
]; then
105 old_subnet_arg
="netmask $old_subnet_mask"
107 if [ x
$alias_subnet_mask != x
]; then
108 alias_subnet_arg
="netmask $alias_subnet_mask"
110 if [ x
$new_interface_mtu != x
]; then
111 mtu_arg
="mtu $new_interface_mtu"
113 if [ x
$IF_METRIC != x
]; then
114 metric_arg
="metric $IF_METRIC"
117 if [ x
$reason = xMEDIUM
]; then
118 # Linux doesn't do mediums (ok, ok, media).
122 if [ x
$reason = xPREINIT
]; then
123 if [ x
$alias_ip_address != x
]; then
124 # Bring down alias interface. Its routes will disappear too.
125 ifconfig
$interface:0- inet
0
127 ifconfig
$interface 0 up
129 # We need to give the kernel some time to get the interface up.
135 if [ x
$reason = xARPCHECK
] ||
[ x
$reason = xARPSEND
]; then
139 if [ x
$reason = xBOUND
] ||
[ x
$reason = xRENEW
] || \
140 [ x
$reason = xREBIND
] ||
[ x
$reason = xREBOOT
]; then
141 current_hostname
=`hostname`
142 if [ x
$current_hostname = x
] || \
143 [ x
$current_hostname = "x(none)" ] || \
144 [ x
$current_hostname = xlocalhost
] || \
145 [ x
$current_hostname = x
$old_host_name ]; then
146 if [ x
$new_host_name != x
$old_host_name ]; then
147 hostname
"$new_host_name"
151 if [ x
$old_ip_address != x
] && [ x
$alias_ip_address != x
] && \
152 [ x
$alias_ip_address != x
$old_ip_address ]; then
153 # Possible new alias. Remove old alias.
154 ifconfig
$interface:0- inet
0
156 if [ x
$old_ip_address != x
] && [ x
$old_ip_address != x
$new_ip_address ]; then
157 # IP address changed. Bringing down the interface will delete all routes,
158 # and clear the ARP cache.
159 ifconfig
$interface inet
0 down
162 if [ x
$old_ip_address = x
] ||
[ x
$old_ip_address != x
$new_ip_address ] || \
163 [ x
$reason = xBOUND
] ||
[ x
$reason = xREBOOT
]; then
165 ifconfig
$interface inet
$new_ip_address $new_subnet_arg \
166 $new_broadcast_arg $mtu_arg
167 # Add a network route to the computed network address.
168 for router
in $new_routers; do
169 if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
170 route add
-host $router dev
$interface
172 route add default gw
$router $metric_arg dev
$interface
175 # we haven't changed the address, have we changed other options
176 # that we wish to update?
177 if [ x
$new_routers != x
] && [ x
$new_routers != x
$old_routers ] ; then
178 # if we've changed routers delete the old and add the new.
179 for router
in $old_routers; do
180 route del default gw
$router
182 for router
in $new_routers; do
183 if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
184 route add
-host $router dev
$interface
186 route add default gw
$router $metric_arg dev
$interface
190 if [ x
$new_ip_address != x
$alias_ip_address ] && [ x
$alias_ip_address != x
];
192 ifconfig
$interface:0- inet
0
193 ifconfig
$interface:0 inet
$alias_ip_address $alias_subnet_arg
194 route add
-host $alias_ip_address $interface:0
200 if [ x
$reason = xEXPIRE
] ||
[ x
$reason = xFAIL
] ||
[ x
$reason = xRELEASE
] \
201 ||
[ x
$reason = xSTOP
]; then
202 if [ x
$alias_ip_address != x
]; then
203 # Turn off alias interface.
204 ifconfig
$interface:0- inet
0
206 if [ x
$old_ip_address != x
]; then
207 # Shut down interface, which will delete routes and clear arp cache.
208 ifconfig
$interface inet
0 down
210 if [ x
$alias_ip_address != x
]; then
211 ifconfig
$interface:0 inet
$alias_ip_address $alias_subnet_arg
212 route add
-host $alias_ip_address $interface:0
217 if [ x
$reason = xTIMEOUT
]; then
218 if [ x
$alias_ip_address != x
]; then
219 ifconfig
$interface:0- inet
0
221 ifconfig
$interface inet
$new_ip_address $new_subnet_arg \
222 $new_broadcast_arg $mtu_arg
224 if ping -q -c 1 $1; then
225 if [ x
$new_ip_address != x
$alias_ip_address ] && \
226 [ x
$alias_ip_address != x
]; then
227 ifconfig
$interface:0 inet
$alias_ip_address $alias_subnet_arg
228 route add
-host $alias_ip_address dev
$interface:0
230 for router
in $new_routers; do
231 if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
232 route add
-host $router dev
$interface
234 route add default gw
$router $metric_arg dev
$interface
239 ifconfig
$interface inet
0 down
247 if [ x
$reason = xPREINIT6
] ; then
248 # Ensure interface is up.
249 ${ip} link
set ${interface} up
251 # We need to give the kernel some time to active interface
252 interface_up_wait_time
=5
253 for i
in $
(seq 0 ${interface_up_wait_time})
255 ifconfig
${interface} |
grep RUNNING
>/dev
/null
2>&1
256 if [ $?
-eq 0 ]; then
262 # Remove any stale addresses from aborted clients.
263 ${ip} -f inet6 addr flush dev
${interface} scope global permanent
265 # Wait for duplicate address detection for this interface if the
266 # --dad-wait-time parameter has been specified and is greater than
268 if [ ${dad_wait_time} -gt 0 ]; then
269 # Check if any IPv6 address on this interface is marked as
271 ${ip} addr show
${interface} |
grep inet6 |
grep tentative \
273 if [ $?
-eq 0 ]; then
274 # Wait for duplicate address detection to complete or for
275 # the timeout specified as --dad-wait-time.
276 for i
in $
(seq 0 $dad_wait_time)
278 # We're going to poll for the tentative flag every second.
280 ${ip} addr show
${interface} |
grep inet6 |
grep tentative \
282 if [ $?
-ne 0 ]; then
292 if [ x
${old_ip6_prefix} != x
] ||
[ x
${new_ip6_prefix} != x
] ; then
293 echo Prefix
${reason} old=${old_ip6_prefix} new=${new_ip6_prefix}
298 if [ x
$reason = xBOUND6
] ; then
299 if [ x
${new_ip6_address} = x
] ||
[ x
${new_ip6_prefixlen} = x
] ; then
303 ${ip} -f inet6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \
304 dev
${interface} scope global
306 # Check for nameserver options.
312 if [ x
$reason = xRENEW6
] ||
[ x
$reason = xREBIND6
] ; then
313 if [ x
${new_ip6_address} != x
] && [ x
${new_ip6_prefixlen} != x
] ; then
314 ${ip} -f inet6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \
315 dev
${interface} scope global
318 # Make sure nothing has moved around on us.
320 # Nameservers/domains/etc.
321 if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
322 [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then
329 if [ x
$reason = xDEPREF6
] ; then
330 if [ x
${new_ip6_prefixlen} = x
] ; then
334 ${ip} -f inet6 addr change ${new_ip6_address}/${new_ip6_prefixlen} \
335 dev
${interface} scope global preferred_lft
0
340 if [ x
$reason = xEXPIRE6
-o x
$reason = xRELEASE6
-o x
$reason = xSTOP6
] ; then
341 if [ x
${old_ip6_address} = x
] ||
[ x
${old_ip6_prefixlen} = x
] ; then
345 ${ip} -f inet6 addr del ${old_ip6_address}/${old_ip6_prefixlen} \