xref: /dragonfly/sbin/dhclient/dhclient-script (revision 65c3a693)
1846204b6SHasso Tepper#!/bin/sh
2846204b6SHasso Tepper#
38e373f92SAntonio Huete Jimenez# $OpenBSD: src/sbin/dhclient/Attic/dhclient-script,v 1.23 2012/09/18 18:27:55 krw Exp $
4846204b6SHasso Tepper#
5846204b6SHasso Tepper# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
6846204b6SHasso Tepper#
7846204b6SHasso Tepper# Permission to use, copy, modify, and distribute this software for any
8846204b6SHasso Tepper# purpose with or without fee is hereby granted, provided that the above
9846204b6SHasso Tepper# copyright notice and this permission notice appear in all copies.
10846204b6SHasso Tepper#
11846204b6SHasso Tepper# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12846204b6SHasso Tepper# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13846204b6SHasso Tepper# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14846204b6SHasso Tepper# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15846204b6SHasso Tepper# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16846204b6SHasso Tepper# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17846204b6SHasso Tepper# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18846204b6SHasso Tepper#
19846204b6SHasso Tepper#
20846204b6SHasso Tepper
21846204b6SHasso Tepper#
22846204b6SHasso Tepper# Helper functions that implement common actions.
23846204b6SHasso Tepper#
24846204b6SHasso Tepper
25846204b6SHasso Tepperdelete_old_address() {
26846204b6SHasso Tepper	if [ -n "$old_ip_address" ]; then
277cbe3601SAntonio Huete Jimenez		ifconfig $interface inet $old_ip_address delete
2834910103SAntonio Huete Jimenez		#route delete "$old_ip_address" 127.0.0.1 >/dev/null 2>&1
29da3467a7SMatthew Dillon	fi
306bdc8b43SMatthew Dillon	if [ -n "$old_classless_routes" ]; then
316bdc8b43SMatthew Dillon		fill_classless_routes "$old_classless_routes"
326bdc8b43SMatthew Dillon		set $classless_routes
336bdc8b43SMatthew Dillon		while [ $# -gt 1 ]; do
346bdc8b43SMatthew Dillon			route delete "$1" "$2"
356bdc8b43SMatthew Dillon			shift; shift
366bdc8b43SMatthew Dillon		done
376bdc8b43SMatthew Dillon		return 0;
38846204b6SHasso Tepper	fi
39846204b6SHasso Tepper}
40846204b6SHasso Tepper
41846204b6SHasso Tepperadd_new_address() {
42846204b6SHasso Tepper	ifconfig $interface \
43846204b6SHasso Tepper		inet $new_ip_address \
44846204b6SHasso Tepper		netmask $new_subnet_mask \
457cbe3601SAntonio Huete Jimenez		broadcast $new_broadcast_address
46846204b6SHasso Tepper
47846204b6SHasso Tepper	# XXX Original TIMEOUT code did not do this unless $new_routers was set?
4834910103SAntonio Huete Jimenez	#route add $new_ip_address 127.0.0.1 >/dev/null 2>&1
49846204b6SHasso Tepper}
50846204b6SHasso Tepper
516bdc8b43SMatthew Dillonfill_classless_routes() {
526bdc8b43SMatthew Dillon	set $1
536bdc8b43SMatthew Dillon	while [ $# -ge 5 ]; do
546bdc8b43SMatthew Dillon		if [ $1 -eq 0 ]; then
556bdc8b43SMatthew Dillon			route="default"
566bdc8b43SMatthew Dillon		elif [ $1 -le 8 ]; then
576bdc8b43SMatthew Dillon			route="$2.0.0.0/$1"
586bdc8b43SMatthew Dillon			shift
596bdc8b43SMatthew Dillon		elif [ $1 -le 16 ]; then
606bdc8b43SMatthew Dillon			route="$2.$3.0.0/$1"
616bdc8b43SMatthew Dillon			shift; shift
626bdc8b43SMatthew Dillon		elif [ $1 -le 24 ]; then
636bdc8b43SMatthew Dillon			route="$2.$3.$4.0/$1"
646bdc8b43SMatthew Dillon			shift; shift; shift
656bdc8b43SMatthew Dillon		else
666bdc8b43SMatthew Dillon			route="$2.$3.$4.$5/$1"
676bdc8b43SMatthew Dillon			shift; shift; shift; shift
686bdc8b43SMatthew Dillon		fi
696bdc8b43SMatthew Dillon		shift
706bdc8b43SMatthew Dillon		router="$1.$2.$3.$4"
716bdc8b43SMatthew Dillon		classless_routes="$classless_routes $route $router"
726bdc8b43SMatthew Dillon		shift; shift; shift; shift
736bdc8b43SMatthew Dillon	done
746bdc8b43SMatthew Dillon}
756bdc8b43SMatthew Dillon
76846204b6SHasso Tepperdelete_old_routes() {
77*65c3a693SMatthew Dillon	arp -d -i $interface -an
78846204b6SHasso Tepper}
79846204b6SHasso Tepper
80846204b6SHasso Tepperadd_new_routes() {
816bdc8b43SMatthew Dillon	# RFC 3442: If the DHCP server returns both a Classless Static
826bdc8b43SMatthew Dillon	# Routes option and a Router option, the DHCP client MUST ignore
836bdc8b43SMatthew Dillon	# the Router option.
846bdc8b43SMatthew Dillon	#
856bdc8b43SMatthew Dillon	# DHCP clients that support this option (Classless Static Routes)
866bdc8b43SMatthew Dillon	# MUST NOT install the routes specified in the Static Routes
876bdc8b43SMatthew Dillon	# option (option code 33) if both a Static Routes option and the
886bdc8b43SMatthew Dillon	# Classless Static Routes option are provided.
896bdc8b43SMatthew Dillon
906bdc8b43SMatthew Dillon	if [ -n "$new_classless_routes" ]; then
916bdc8b43SMatthew Dillon		fill_classless_routes "$new_classless_routes"
926bdc8b43SMatthew Dillon		$LOGGER "New Classless Static Routes ($interface): $classless_routes"
936bdc8b43SMatthew Dillon		set $classless_routes
946bdc8b43SMatthew Dillon		while [ $# -gt 1 ]; do
956bdc8b43SMatthew Dillon			if [ "0.0.0.0" = "$2" ]; then
966bdc8b43SMatthew Dillon				route add "$1" -iface "$interface"
976bdc8b43SMatthew Dillon			else
986bdc8b43SMatthew Dillon				route add "$1" "$2"
996bdc8b43SMatthew Dillon			fi
1006bdc8b43SMatthew Dillon			shift; shift
1016bdc8b43SMatthew Dillon		done
1026bdc8b43SMatthew Dillon		return
1036bdc8b43SMatthew Dillon	fi
1046bdc8b43SMatthew Dillon
105846204b6SHasso Tepper	for router in $new_routers; do
1063a961bf3SAntonio Huete Jimenez		route -q delete default
107846204b6SHasso Tepper		if [ "$new_ip_address" = "$router" ]; then
1083a961bf3SAntonio Huete Jimenez			route -q add default -iface $router
109846204b6SHasso Tepper		else
1103a961bf3SAntonio Huete Jimenez			route -q add default $router
111846204b6SHasso Tepper		fi
112846204b6SHasso Tepper		# 2nd and subsequent default routers error out, so explicitly
113846204b6SHasso Tepper		# stop processing the list after the first one.
114846204b6SHasso Tepper		break
115846204b6SHasso Tepper	done
116846204b6SHasso Tepper}
117846204b6SHasso Tepper
118846204b6SHasso Tepperadd_new_resolv_conf() {
119b976f566SAntonio Huete Jimenez	# Create resolv.conf when either $new_domain_name_servers or
120b976f566SAntonio Huete Jimenez	# $new_domain_name are provided. As reported in PR#3135, some ISPs
121b976f566SAntonio Huete Jimenez	# provide only $new_domain_name_servers.
122846204b6SHasso Tepper
123426fda14SRoy Marples	local tmpres="/var/run/dhclient-resolv.conf.$interface"
124426fda14SRoy Marples
125426fda14SRoy Marples	rm -f "$tmpres"
126846204b6SHasso Tepper
127846204b6SHasso Tepper	if [ -n "$new_domain_name" ]; then
128426fda14SRoy Marples		echo "search $new_domain_name" >>"$tmpres"
129846204b6SHasso Tepper	fi
130846204b6SHasso Tepper
131846204b6SHasso Tepper	if [ -n "$new_domain_name_servers" ]; then
132846204b6SHasso Tepper		for nameserver in $new_domain_name_servers; do
133426fda14SRoy Marples			echo "nameserver $nameserver" >>"$tmpres"
134846204b6SHasso Tepper		done
135846204b6SHasso Tepper	fi
136846204b6SHasso Tepper
137426fda14SRoy Marples	if [ -f "$tmpres" ]; then
138426fda14SRoy Marples		/sbin/resolvconf -a "$interface.dhcp" <"$tmpres"
139426fda14SRoy Marples		rm -f "$tmpres"
140426fda14SRoy Marples	else
141426fda14SRoy Marples		/sbin/resolvconf -d "$interface.dhcp" -f
142846204b6SHasso Tepper	fi
143846204b6SHasso Tepper
144846204b6SHasso Tepper	return 0
145846204b6SHasso Tepper}
146846204b6SHasso Tepper
147846204b6SHasso Tepper#
148846204b6SHasso Tepper# Start of active code.
149846204b6SHasso Tepper#
150846204b6SHasso Tepper
151846204b6SHasso Teppercase $reason in
152846204b6SHasso TepperMEDIUM)
1537cbe3601SAntonio Huete Jimenez	# Not called by OpenBSD dhclient(8).
154846204b6SHasso Tepper	;;
155846204b6SHasso Tepper
156846204b6SHasso TepperPREINIT)
1577cbe3601SAntonio Huete Jimenez	# Not called by OpenBSD dhclient(8).
158601f8f90SAntonio Huete Jimenez	;;
159601f8f90SAntonio Huete Jimenez
160601f8f90SAntonio Huete JimenezARPSEND)
1617cbe3601SAntonio Huete Jimenez	# Not called by OpenBSD dhclient(8).
162601f8f90SAntonio Huete Jimenez	exit 1
163846204b6SHasso Tepper	;;
164846204b6SHasso Tepper
1657cbe3601SAntonio Huete JimenezARPCHECK)
1667cbe3601SAntonio Huete Jimenez	# Not called by OpenBSD dhclient(8).
1677cbe3601SAntonio Huete Jimenez	# Always succeed. i.e. accept lease.
1687cbe3601SAntonio Huete Jimenez	;;
1697cbe3601SAntonio Huete Jimenez
170846204b6SHasso TepperBOUND|RENEW|REBIND|REBOOT)
171846204b6SHasso Tepper	if [ -n "$old_ip_address" ]; then
172846204b6SHasso Tepper		if [ "$old_ip_address" != "$new_ip_address" ]; then
173846204b6SHasso Tepper			delete_old_address
174846204b6SHasso Tepper			delete_old_routes
175846204b6SHasso Tepper		fi
176846204b6SHasso Tepper	fi
177846204b6SHasso Tepper	if [ "$reason" = BOUND ] ||
178846204b6SHasso Tepper	   [ "$reason" = REBOOT ] ||
179846204b6SHasso Tepper	   [ -z "$old_ip_address" ] ||
180846204b6SHasso Tepper	   [ "$old_ip_address" != "$new_ip_address" ]; then
181846204b6SHasso Tepper		add_new_address
182846204b6SHasso Tepper		add_new_routes
183846204b6SHasso Tepper	fi
184846204b6SHasso Tepper	add_new_resolv_conf
185846204b6SHasso Tepper	;;
186846204b6SHasso Tepper
187846204b6SHasso TepperEXPIRE|FAIL)
188846204b6SHasso Tepper	if [ -n "$old_ip_address" ]; then
189846204b6SHasso Tepper		delete_old_address
190846204b6SHasso Tepper		delete_old_routes
191846204b6SHasso Tepper	fi
192426fda14SRoy Marples	/sbin/resolvconf -d "$interface.dhcp" -f
193846204b6SHasso Tepper	;;
194846204b6SHasso Tepper
195846204b6SHasso TepperTIMEOUT)
196846204b6SHasso Tepper	add_new_address
197846204b6SHasso Tepper	sleep 1
198846204b6SHasso Tepper	if [ -n "$new_routers" ]; then
199846204b6SHasso Tepper		set "$new_routers"
200846204b6SHasso Tepper		if ping -q -c 1 -w 1 "$1"; then
201846204b6SHasso Tepper			add_new_routes
202846204b6SHasso Tepper			if add_new_resolv_conf; then
203846204b6SHasso Tepper				exit 0
204846204b6SHasso Tepper			fi
205846204b6SHasso Tepper		fi
206846204b6SHasso Tepper	fi
207057c89ffSAntonio Huete Jimenez	ifconfig $interface inet $new_ip_address delete
208846204b6SHasso Tepper	# XXX Why not a delete_old_address as before all other invocations of
209846204b6SHasso Tepper	#     delete_old_routes?
210846204b6SHasso Tepper	delete_old_routes
211846204b6SHasso Tepper	exit 1
212846204b6SHasso Tepper	;;
213846204b6SHasso Tepperesac
214846204b6SHasso Tepper
215846204b6SHasso Tepperexit 0
216