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