xref: /minix/external/bsd/dhcp/dist/client/scripts/openbsd (revision fb9c64b2)
1#!/bin/sh
2
3make_resolv_conf() {
4  if [ x"$new_domain_name_servers" != x ]; then
5    cat /dev/null > /etc/resolv.conf.dhclient
6    if [ x"$new_domain_search" != x ]; then
7      echo search $new_domain_search >> /etc/resolv.conf.dhclient
8    elif [ x"$new_domain_name" != x ]; then
9      # Note that the DHCP 'Domain Name Option' is really just a domain
10      # name, and that this practice of using the domain name option as
11      # a search path is both nonstandard and deprecated.
12      echo search $new_domain_name >> /etc/resolv.conf.dhclient
13    fi
14    for nameserver in $new_domain_name_servers; do
15      echo nameserver $nameserver >>/etc/resolv.conf.dhclient
16    done
17
18    mv /etc/resolv.conf.dhclient /etc/resolv.conf
19  elif [ "x${new_dhcp6_name_servers}" != x ] ; then
20    cat /dev/null > /etc/resolv.conf.dhclient6
21    chmod 644 /etc/resolv.conf.dhclient6
22
23    if [ "x${new_dhcp6_domain_search}" != x ] ; then
24      echo search ${new_dhcp6_domain_search} >> /etc/resolv.conf.dhclient6
25    fi
26    for nameserver in ${new_dhcp6_name_servers} ; do
27      # If the nameserver has a link-local address
28      # add a <zone_id> (interface name) to it.
29      case $nameserver in
30	fe80:*) zone_id="%$interface";;
31	FE80:*) zone_id="%$interface";;
32	*)      zone_id="";;
33      esac
34      echo nameserver ${nameserver}$zone_id >> /etc/resolv.conf.dhclient6
35    done
36
37    mv /etc/resolv.conf.dhclient6 /etc/resolv.conf
38  fi
39}
40
41# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
42exit_with_hooks() {
43  exit_status=$1
44  if [ -f /etc/dhclient-exit-hooks ]; then
45    . /etc/dhclient-exit-hooks
46  fi
47# probably should do something with exit status of the local script
48  exit $exit_status
49}
50
51# Invoke the local dhcp client enter hooks, if they exist.
52if [ -f /etc/dhclient-enter-hooks ]; then
53  exit_status=0
54  . /etc/dhclient-enter-hooks
55  # allow the local script to abort processing of this state
56  # local script must set exit_status variable to nonzero.
57  if [ $exit_status -ne 0 ]; then
58    exit $exit_status
59  fi
60fi
61
62if [ x$new_network_number != x ]; then
63   echo New Network Number: $new_network_number
64fi
65
66if [ x$new_broadcast_address != x ]; then
67 echo New Broadcast Address: $new_broadcast_address
68  new_broadcast_arg="broadcast $new_broadcast_address"
69fi
70if [ x$old_broadcast_address != x ]; then
71  old_broadcast_arg="broadcast $old_broadcast_address"
72fi
73if [ x$new_subnet_mask != x ]; then
74  new_netmask_arg="netmask $new_subnet_mask"
75fi
76if [ x$old_subnet_mask != x ]; then
77  old_netmask_arg="netmask $old_subnet_mask"
78fi
79if [ x$alias_subnet_mask != x ]; then
80  alias_subnet_arg="netmask $alias_subnet_mask"
81fi
82
83if [ x$reason = xMEDIUM ]; then
84  eval "ifconfig $interface $medium"
85  eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
86  sleep 1
87  exit_with_hooks 0
88fi
89
90###
91### DHCPv4 Handlers
92###
93
94if [ x$reason = xPREINIT ]; then
95  if [ x$alias_ip_address != x ]; then
96    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
97    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
98  fi
99  ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
100		broadcast 255.255.255.255 up
101  exit_with_hooks 0
102fi
103
104if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
105  exit_with_hooks 0;
106fi
107
108if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
109   [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
110  current_hostname=`hostname`
111  if [ x$current_hostname = x ] || \
112     [ x$current_hostname = x$old_host_name ]; then
113    if [ x$current_hostname = x ] || \
114       [ x$new_host_name != x$old_host_name ]; then
115      hostname $new_host_name
116    fi
117  fi
118
119  if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
120		[ x$alias_ip_address != x$old_ip_address ]; then
121    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
122    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
123  fi
124  if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]
125   then
126    eval "ifconfig $interface inet -alias $old_ip_address $medium"
127    route delete $old_ip_address 127.1 >/dev/null 2>&1
128    for router in $old_routers; do
129      route delete default $router >/dev/null 2>&1
130    done
131    if [ "$old_static_routes" != "" ]; then
132      set $old_static_routes
133      while [ $# -gt 1 ]; do
134	route delete $1 $2
135	shift; shift
136      done
137    fi
138    arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -n -d \1/p' |sh
139  fi
140  if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
141     [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
142    eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \
143					$new_broadcast_arg $medium"
144    route add $new_ip_address 127.1 >/dev/null 2>&1
145    for router in $new_routers; do
146      route add default $router >/dev/null 2>&1
147    done
148    if [ "$new_static_routes" != "" ]; then
149      set $new_static_routes
150      while [ $# -gt 1 ]; do
151	route add $1 $2
152	shift; shift
153      done
154    fi
155  else
156    # we haven't changed the address, have we changed other options
157    # that we wish to update?
158    if [ x$new_routers != x ] && [ x$new_routers != x$old_routers ] ; then
159      # if we've changed routers delete the old and add the new.
160      $LOGGER "New Routers: $new_routers"
161      for router in $old_routers; do
162        route delete default $router >/dev/null 2>&1
163      done
164      for router in $new_routers; do
165        route add default $router >/dev/null 2>&1
166      done
167    fi
168  fi
169  if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
170   then
171    ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
172    route add $alias_ip_address 127.0.0.1
173  fi
174  make_resolv_conf
175  exit_with_hooks 0
176fi
177
178if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
179   || [ x$reason = xSTOP ]; then
180  if [ x$alias_ip_address != x ]; then
181    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
182    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
183  fi
184  if [ x$old_ip_address != x ]; then
185    eval "ifconfig $interface inet -alias $old_ip_address $medium"
186    route delete $old_ip_address 127.1 >/dev/null 2>&1
187    for router in $old_routers; do
188      route delete default $router >/dev/null 2>&1
189    done
190    if [ "$old_static_routes" != "" ]; then
191      set $old_static_routes
192      while [ $# -gt 1 ]; do
193	route delete $1 $2
194	shift; shift
195      done
196    fi
197    arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -n -d \1/p' \
198						|sh >/dev/null 2>&1
199  fi
200  if [ x$alias_ip_address != x ]; then
201    ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
202    route add $alias_ip_address 127.0.0.1
203  fi
204  exit_with_hooks 0
205fi
206
207if [ x$reason = xTIMEOUT ]; then
208  if [ x$alias_ip_address != x ]; then
209    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
210    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
211  fi
212  eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \
213					$new_broadcast_arg $medium"
214  sleep 1
215  if [ "$new_routers" != "" ]; then
216    set $new_routers
217    if ping -q -c 1 -w 1 $1; then
218      if [ x$new_ip_address != x$alias_ip_address ] && \
219			[ x$alias_ip_address != x ]; then
220	ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
221	route add $alias_ip_address 127.0.0.1
222      fi
223      route add $new_ip_address 127.1 >/dev/null 2>&1
224      for router in $new_routers; do
225	route add default $router >/dev/null 2>&1
226      done
227      set $new_static_routes
228      while [ $# -gt 1 ]; do
229	route add $0 $1
230	shift; shift
231      done
232      make_resolv_conf
233      exit_with_hooks 0
234    fi
235  fi
236  eval "ifconfig $interface inet -alias $new_ip_address $medium"
237  for router in $old_routers; do
238    route delete default $router >/dev/null 2>&1
239  done
240  if [ "$old_static_routes" != "" ]; then
241    set $old_static_routes
242    while [ $# -gt 1 ]; do
243      route delete $1 $2
244      shift; shift
245    done
246  fi
247  arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -n -d \1/p' \
248							|sh >/dev/null 2>&1
249  exit_with_hooks 1
250fi
251
252###
253### DHCPv6 Handlers
254###
255
256if [ ${reason} = PREINIT6 ] ; then
257  # Ensure interface is up.
258  ifconfig ${interface} up
259
260  # XXX: Remove any stale addresses from aborted clients.
261
262  exit_with_hooks 0
263fi
264
265if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ] ; then
266    echo Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix}
267
268    exit_with_hooks 0
269fi
270
271if [ ${reason} = BOUND6 ] ; then
272  if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then
273    exit_with_hooks 2;
274  fi
275
276  ifconfig ${interface} inet6 add ${new_ip6_address}/${new_ip6_prefixlen}
277
278  # Check for nameserver options.
279  make_resolv_conf
280
281  exit_with_hooks 0
282fi
283
284if [ ${reason} = RENEW6 ] || [ ${reason} = REBIND6 ] ; then
285  # Make sure nothing has moved around on us.
286
287  # Nameservers/domains/etc.
288  if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
289     [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then
290    make_resolv_conf
291  fi
292
293  exit_with_hooks 0
294fi
295
296if [ ${reason} = DEPREF6 ] ; then
297  if [ x${new_ip6_prefixlen} = x ] ; then
298    exit_with_hooks 2;
299  fi
300
301  # XXX:
302  # There doesn't appear to be a way to update an addr to indicate
303  # preference.
304
305  exit_with_hooks 0
306fi
307
308if [ ${reason} = EXPIRE6 -o ${reason} = RELEASE6 -o ${reason} = STOP6 ] ; then
309  if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then
310    exit_with_hooks 2;
311  fi
312
313  ifconfig ${interface} inet6 delete ${old_ip6_address}/${old_ip6_prefixlen}
314
315  exit_with_hooks 0
316fi
317
318exit_with_hooks 0
319