xref: /minix/external/bsd/dhcp/dist/client/scripts/netbsd (revision 83ee113e)
1#!/bin/sh
2
3make_resolv_conf() {
4  if [ "x$new_domain_name" != x ] && [ 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 if [ x$new_interface_mtu != x ]; then
83   mtu_arg="mtu $new_interface_mtu"
84 fi
85if [ x$IF_METRIC != x ]; then
86  metric_arg="metric $IF_METRIC"
87fi
88
89if [ x$reason = xMEDIUM ]; then
90  eval "ifconfig $interface $medium"
91  eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
92  sleep 1
93  exit_with_hooks 0
94fi
95
96###
97### DHCPv4 Handlers
98###
99
100if [ x$reason = xPREINIT ]; then
101  if [ x$alias_ip_address != x ]; then
102    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
103    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
104  fi
105  ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
106		broadcast 255.255.255.255 up
107  exit_with_hooks 0
108fi
109
110if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
111  exit_with_hooks 0
112fi
113
114if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
115   [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
116  current_hostname=`hostname`
117  if [ x$current_hostname = x ] || \
118     [ x$current_hostname = x$old_host_name ]; then
119    if [ x$current_hostname = x ] || \
120       [ x$new_host_name != x$old_host_name ]; then
121      hostname $new_host_name
122    fi
123  fi
124
125  if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
126		[ x$alias_ip_address != x$old_ip_address ]; then
127    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
128    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
129  fi
130  if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]
131   then
132    eval "ifconfig $interface inet -alias $old_ip_address $medium"
133    route delete $old_ip_address 127.1 >/dev/null 2>&1
134    for router in $old_routers; do
135      route delete default $router >/dev/null 2>&1
136    done
137    if [ "$old_static_routes" != "" ]; then
138      set $old_static_routes
139      while [ $# -gt 1 ]; do
140	route delete $1 $2
141	shift; shift
142      done
143    fi
144    arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -n -d \1/p' |sh
145  fi
146  if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
147     [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
148    eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \
149			$new_broadcast_arg $mtu_arg $metric_arg $medium"
150    route add $new_ip_address 127.1 >/dev/null 2>&1
151    for router in $new_routers; do
152      route add default $router >/dev/null 2>&1
153    done
154    if [ "$new_static_routes" != "" ]; then
155      set $new_static_routes
156      while [ $# -gt 1 ]; do
157	route add $1 $2
158	shift; shift
159      done
160    fi
161  else
162    # we haven't changed the address, have we changed other options
163    # that we wish to update?
164    if [ x$new_routers != x ] && [ x$new_routers != x$old_routers ] ; then
165      # if we've changed routers delete the old and add the new.
166      $LOGGER "New Routers: $new_routers"
167      for router in $old_routers; do
168        route delete default $router >/dev/null 2>&1
169      done
170      for router in $new_routers; do
171        route add default $router >/dev/null 2>&1
172      done
173    fi
174  fi
175  if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
176   then
177    ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
178    route add $alias_ip_address 127.0.0.1
179  fi
180  make_resolv_conf
181  exit_with_hooks 0
182fi
183
184if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
185   || [ x$reason = xSTOP ]; then
186  if [ x$alias_ip_address != x ]; then
187    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
188    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
189  fi
190  if [ x$old_ip_address != x ]; then
191    eval "ifconfig $interface inet -alias $old_ip_address $medium"
192    route delete $old_ip_address 127.1 >/dev/null 2>&1
193    for router in $old_routers; do
194      route delete default $router >/dev/null 2>&1
195    done
196    if [ "$old_static_routes" != "" ]; then
197      set $old_static_routes
198      while [ $# -gt 1 ]; do
199	route delete $1 $2
200	shift; shift
201      done
202    fi
203    arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -n -d \1/p' \
204						|sh >/dev/null 2>&1
205  fi
206  if [ x$alias_ip_address != x ]; then
207    ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
208    route add $alias_ip_address 127.0.0.1
209  fi
210  exit_with_hooks 0
211fi
212
213if [ x$reason = xTIMEOUT ]; then
214  if [ x$alias_ip_address != x ]; then
215    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
216    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
217  fi
218  eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \
219			$new_broadcast_arg $mtu_arg $metric_arg $medium"
220  sleep 1
221  if [ "$new_routers" != "" ]; then
222    set $new_routers
223    if ping -q -c 1 -w 1 $1; then
224      if [ x$new_ip_address != x$alias_ip_address ] && \
225			[ x$alias_ip_address != x ]; then
226	ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
227	route add $alias_ip_address 127.0.0.1
228      fi
229      route add $new_ip_address 127.1 >/dev/null 2>&1
230      for router in $new_routers; do
231	route add default $router >/dev/null 2>&1
232      done
233      set $new_static_routes
234      while [ $# -gt 1 ]; do
235	route add $0 $1
236	shift; shift
237      done
238      make_resolv_conf
239      exit_with_hooks 0
240    fi
241  fi
242  eval "ifconfig $interface inet -alias $new_ip_address $medium"
243  for router in $old_routers; do
244    route delete default $router >/dev/null 2>&1
245  done
246  if [ "$old_static_routes" != "" ]; then
247    set $old_static_routes
248    while [ $# -gt 1 ]; do
249      route delete $1 $2
250      shift; shift
251    done
252  fi
253  arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -n -d \1/p' \
254							|sh >/dev/null 2>&1
255  exit_with_hooks 1
256fi
257
258###
259### DHCPv6 Handlers
260###
261
262if [ ${reason} = PREINIT6 ] ; then
263  # Ensure interface is up.
264  ifconfig ${interface} up
265
266  # XXX: Remove any stale addresses from aborted clients.
267
268  exit_with_hooks 0
269fi
270
271if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ] ; then
272    echo Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix}
273
274    exit_with_hooks 0
275fi
276
277if [ ${reason} = BOUND6 ] ; then
278  if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then
279    exit_with_hooks 2;
280  fi
281
282  ifconfig ${interface} inet6 add ${new_ip6_address}/${new_ip6_prefixlen}
283
284  # Check for nameserver options.
285  make_resolv_conf
286
287  exit_with_hooks 0
288fi
289
290if [ ${reason} = RENEW6 ] || [ ${reason} = REBIND6 ] ; then
291  # Make sure nothing has moved around on us.
292
293  # Nameservers/domains/etc.
294  if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
295     [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then
296    make_resolv_conf
297  fi
298
299  exit_with_hooks 0
300fi
301
302if [ ${reason} = DEPREF6 ] ; then
303  if [ x${new_ip6_prefixlen} = x ] ; then
304    exit_with_hooks 2;
305  fi
306
307  # XXX:
308  # There doesn't appear to be a way to update an addr to indicate
309  # preference.
310
311  exit_with_hooks 0
312fi
313
314if [ ${reason} = EXPIRE6 -o ${reason} = RELEASE6 -o ${reason} = STOP6 ] ; then
315  if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then
316    exit_with_hooks 2;
317  fi
318
319  ifconfig ${interface} inet6 delete ${old_ip6_address}/${old_ip6_prefixlen}
320
321  exit_with_hooks 0
322fi
323
324exit_with_hooks 0
325