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