head	1.24;
access;
symbols
	RELENG_8_4:1.24.0.2
	RELENG_9_1_0_RELEASE:1.21.2.3.2.2
	RELENG_9_1:1.21.2.3.0.2
	RELENG_9_1_BP:1.21.2.3
	RELENG_8_3_0_RELEASE:1.20.2.3.2.1
	RELENG_8_3:1.20.2.3.0.2
	RELENG_8_3_BP:1.20.2.3
	RELENG_9_0_0_RELEASE:1.21.2.1.2.1
	RELENG_9_0:1.21.2.1.0.2
	RELENG_9_0_BP:1.21.2.1
	RELENG_9:1.21.0.2
	RELENG_9_BP:1.21
	RELENG_7_4_0_RELEASE:1.17.2.2.8.1
	RELENG_8_2_0_RELEASE:1.20.2.1.6.1
	RELENG_7_4:1.17.2.2.0.8
	RELENG_7_4_BP:1.17.2.2
	RELENG_8_2:1.20.2.1.0.6
	RELENG_8_2_BP:1.20.2.1
	RELENG_8_1_0_RELEASE:1.20.2.1.4.1
	RELENG_8_1:1.20.2.1.0.4
	RELENG_8_1_BP:1.20.2.1
	RELENG_7_3_0_RELEASE:1.17.2.2.6.1
	RELENG_7_3:1.17.2.2.0.6
	RELENG_7_3_BP:1.17.2.2
	RELENG_8_0_0_RELEASE:1.20.2.1.2.1
	RELENG_8_0:1.20.2.1.0.2
	RELENG_8_0_BP:1.20.2.1
	RELENG_8:1.20.0.2
	RELENG_8_BP:1.20
	RELENG_7_2_0_RELEASE:1.17.2.2.4.1
	RELENG_7_2:1.17.2.2.0.4
	RELENG_7_2_BP:1.17.2.2
	RELENG_7_1_0_RELEASE:1.17.2.2.2.1
	RELENG_6_4_0_RELEASE:1.4.2.8.2.1
	RELENG_7_1:1.17.2.2.0.2
	RELENG_7_1_BP:1.17.2.2
	RELENG_6_4:1.4.2.8.0.2
	RELENG_6_4_BP:1.4.2.8
	RELENG_7_0_0_RELEASE:1.17
	RELENG_6_3_0_RELEASE:1.4.2.6
	RELENG_7_0:1.17.0.4
	RELENG_7_0_BP:1.17
	RELENG_6_3:1.4.2.6.0.2
	RELENG_6_3_BP:1.4.2.6
	RELENG_7:1.17.0.2
	RELENG_7_BP:1.17
	RELENG_6_2_0_RELEASE:1.4.2.4
	RELENG_6_2:1.4.2.4.0.2
	RELENG_6_2_BP:1.4.2.4
	RELENG_6_1_0_RELEASE:1.4.2.3
	RELENG_6_1:1.4.2.3.0.2
	RELENG_6_1_BP:1.4.2.3
	RELENG_6_0_0_RELEASE:1.4.2.2
	RELENG_6_0:1.4.2.2.0.2
	RELENG_6_0_BP:1.4.2.2
	RELENG_6:1.4.0.2
	RELENG_6_BP:1.4
	dhclient_openbsd_3_7:1.1.1.1
	OPENBSD:1.1.1;
locks; strict;
comment	@# @;


1.24
date	2012.11.17.01.50.19;	author svnexp;	state Exp;
branches
	1.24.2.1;
next	1.23;

1.23
date	2011.12.13.11.54.51;	author glebius;	state Exp;
branches;
next	1.22;

1.22
date	2011.12.04.14.44.31;	author dumbbell;	state Exp;
branches;
next	1.21;

1.21
date	2011.03.18.12.23.20;	author ume;	state Exp;
branches
	1.21.2.1;
next	1.20;

1.20
date	2008.06.09.20.03.35;	author brooks;	state Exp;
branches
	1.20.2.1;
next	1.19;

1.19
date	2008.03.30.02.42.39;	author brooks;	state Exp;
branches;
next	1.18;

1.18
date	2008.01.21.23.54.57;	author brooks;	state Exp;
branches;
next	1.17;

1.17
date	2007.07.03.17.49.32;	author thompsa;	state Exp;
branches
	1.17.2.1;
next	1.16;

1.16
date	2007.04.13.15.07.10;	author emaste;	state Exp;
branches;
next	1.15;

1.15
date	2007.02.09.17.50.26;	author emaste;	state Exp;
branches;
next	1.14;

1.14
date	2006.01.26.21.05.39;	author brooks;	state Exp;
branches;
next	1.13;

1.13
date	2006.01.24.06.11.55;	author brooks;	state Exp;
branches;
next	1.12;

1.12
date	2006.01.23.05.42.59;	author wes;	state Exp;
branches;
next	1.11;

1.11
date	2006.01.10.05.33.33;	author brooks;	state Exp;
branches;
next	1.10;

1.10
date	2005.09.08.22.49.17;	author brooks;	state Exp;
branches;
next	1.9;

1.9
date	2005.09.08.22.15.19;	author brooks;	state Exp;
branches;
next	1.8;

1.8
date	2005.08.26.20.31.04;	author brooks;	state Exp;
branches;
next	1.7;

1.7
date	2005.08.26.01.07.51;	author brooks;	state Exp;
branches;
next	1.6;

1.6
date	2005.08.26.01.02.38;	author brooks;	state Exp;
branches;
next	1.5;

1.5
date	2005.07.26.18.27.37;	author brooks;	state Exp;
branches;
next	1.4;

1.4
date	2005.06.10.03.41.18;	author brooks;	state Exp;
branches
	1.4.2.1;
next	1.3;

1.3
date	2005.06.08.18.21.56;	author brooks;	state Exp;
branches;
next	1.2;

1.2
date	2005.06.07.04.32.29;	author brooks;	state Exp;
branches;
next	1.1;

1.1
date	2005.06.07.04.05.05;	author brooks;	state Exp;
branches
	1.1.1.1;
next	;

1.24.2.1
date	2012.11.17.01.50.19;	author svnexp;	state dead;
branches;
next	1.24.2.2;

1.24.2.2
date	2013.03.28.13.03.31;	author svnexp;	state Exp;
branches;
next	;

1.21.2.1
date	2011.09.23.00.51.37;	author kensmith;	state Exp;
branches
	1.21.2.1.2.1;
next	1.21.2.2;

1.21.2.2
date	2012.01.05.11.14.28;	author glebius;	state Exp;
branches;
next	1.21.2.3;

1.21.2.3
date	2012.01.26.19.46.13;	author dumbbell;	state Exp;
branches
	1.21.2.3.2.1;
next	1.21.2.4;

1.21.2.4
date	2012.11.17.11.36.31;	author svnexp;	state Exp;
branches;
next	;

1.21.2.1.2.1
date	2011.11.11.04.20.22;	author kensmith;	state Exp;
branches;
next	1.21.2.1.2.2;

1.21.2.1.2.2
date	2012.11.17.08.36.30;	author svnexp;	state Exp;
branches;
next	;

1.21.2.3.2.1
date	2012.08.05.23.54.33;	author kensmith;	state Exp;
branches;
next	1.21.2.3.2.2;

1.21.2.3.2.2
date	2012.11.17.08.47.20;	author svnexp;	state Exp;
branches;
next	;

1.20.2.1
date	2009.08.03.08.13.06;	author kensmith;	state Exp;
branches
	1.20.2.1.2.1
	1.20.2.1.4.1
	1.20.2.1.6.1;
next	1.20.2.2;

1.20.2.2
date	2012.01.13.23.25.58;	author glebius;	state Exp;
branches;
next	1.20.2.3;

1.20.2.3
date	2012.01.26.22.01.05;	author dumbbell;	state Exp;
branches
	1.20.2.3.2.1;
next	1.20.2.4;

1.20.2.4
date	2012.11.17.10.36.15;	author svnexp;	state Exp;
branches;
next	;

1.20.2.1.2.1
date	2009.10.25.01.10.29;	author kensmith;	state Exp;
branches;
next	;

1.20.2.1.4.1
date	2010.06.14.02.09.06;	author kensmith;	state Exp;
branches;
next	;

1.20.2.1.6.1
date	2010.12.21.17.09.25;	author kensmith;	state Exp;
branches;
next	;

1.20.2.3.2.1
date	2012.03.03.06.15.13;	author kensmith;	state Exp;
branches;
next	1.20.2.3.2.2;

1.20.2.3.2.2
date	2012.11.17.08.24.55;	author svnexp;	state Exp;
branches;
next	;

1.17.2.1
date	2008.03.12.17.28.47;	author brooks;	state Exp;
branches;
next	1.17.2.2;

1.17.2.2
date	2008.04.11.00.17.59;	author brooks;	state Exp;
branches
	1.17.2.2.2.1
	1.17.2.2.4.1
	1.17.2.2.6.1
	1.17.2.2.8.1;
next	1.17.2.3;

1.17.2.3
date	2012.11.17.08.03.32;	author svnexp;	state Exp;
branches;
next	;

1.17.2.2.2.1
date	2008.11.25.02.59.29;	author kensmith;	state Exp;
branches;
next	;

1.17.2.2.4.1
date	2009.04.15.03.14.26;	author kensmith;	state Exp;
branches;
next	;

1.17.2.2.6.1
date	2010.02.10.00.26.20;	author kensmith;	state Exp;
branches;
next	;

1.17.2.2.8.1
date	2010.12.21.17.10.29;	author kensmith;	state Exp;
branches;
next	1.17.2.2.8.2;

1.17.2.2.8.2
date	2012.11.17.08.16.53;	author svnexp;	state Exp;
branches;
next	;

1.4.2.1
date	2005.07.29.23.36.28;	author brooks;	state Exp;
branches;
next	1.4.2.2;

1.4.2.2
date	2005.09.10.17.01.16;	author brooks;	state Exp;
branches;
next	1.4.2.3;

1.4.2.3
date	2006.01.24.06.01.59;	author brooks;	state Exp;
branches;
next	1.4.2.4;

1.4.2.4
date	2006.05.13.03.24.00;	author brooks;	state Exp;
branches;
next	1.4.2.5;

1.4.2.5
date	2007.10.11.20.23.33;	author thompsa;	state Exp;
branches;
next	1.4.2.6;

1.4.2.6
date	2007.10.31.14.30.20;	author emaste;	state Exp;
branches;
next	1.4.2.7;

1.4.2.7
date	2008.03.12.17.29.42;	author brooks;	state Exp;
branches;
next	1.4.2.8;

1.4.2.8
date	2008.04.11.00.18.37;	author brooks;	state Exp;
branches
	1.4.2.8.2.1;
next	1.4.2.9;

1.4.2.9
date	2012.11.17.07.41.14;	author svnexp;	state Exp;
branches;
next	;

1.4.2.8.2.1
date	2008.10.02.02.57.24;	author kensmith;	state Exp;
branches;
next	;

1.1.1.1
date	2005.06.07.04.05.05;	author brooks;	state Exp;
branches;
next	;


desc
@@


1.24
log
@Switching exporter and resync
@
text
@#!/bin/sh
#
# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
# $FreeBSD: head/sbin/dhclient/dhclient-script 229002 2011-12-30 14:46:53Z dumbbell $
#
# Copyright (c) 2003 Kenneth R Westerback <krw@@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#

ARP=/usr/sbin/arp
HOSTNAME=/bin/hostname
IFCONFIG='/sbin/ifconfig -n'

LOCALHOST=127.0.0.1

if [ -x /usr/bin/logger ]; then
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
else
	LOGGER=echo
fi

#
# Helper functions that implement common actions.
#

check_hostname() {
	current_hostname=`$HOSTNAME`
	if [ -z "$current_hostname" ]; then
		$LOGGER "New Hostname ($interface): $new_host_name"
		$HOSTNAME $new_host_name
	elif [ "$current_hostname" = "$old_host_name" -a \
	       "$new_host_name" != "$old_host_name" ]; then
		$LOGGER "New Hostname ($interface): $new_host_name"
		$HOSTNAME $new_host_name
	fi
}

arp_flush() {
	arp -an -i $interface | \
		sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
		sh >/dev/null 2>&1
}

delete_old_address() {
	eval "$IFCONFIG $interface inet -alias $old_ip_address $medium"
}

add_new_address() {
	eval "$IFCONFIG $interface \
		inet $new_ip_address \
		netmask $new_subnet_mask \
		broadcast $new_broadcast_address \
		$medium"

	$LOGGER "New IP Address ($interface): $new_ip_address"
	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
	$LOGGER "New Routers ($interface): $new_routers"
}

delete_old_alias() {
	if [ -n "$alias_ip_address" ]; then
		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
		#route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
	fi
}

add_new_alias() {
	if [ -n "$alias_ip_address" ]; then
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
		    $alias_subnet_mask
		#route add $alias_ip_address $LOCALHOST
	fi
}

fill_classless_routes() {
	set $1
	while [ $# -ge 5 ]; do
		if [ $1 -eq 0 ]; then
			route="default"
		elif [ $1 -le 8 ]; then
			route="$2.0.0.0/$1"
			shift
		elif [ $1 -le 16 ]; then
			route="$2.$3.0.0/$1"
			shift; shift
		elif [ $1 -le 24 ]; then
			route="$2.$3.$4.0/$1"
			shift; shift; shift
		else
			route="$2.$3.$4.$5/$1"
			shift; shift; shift; shift
		fi
		shift
		router="$1.$2.$3.$4"
		classless_routes="$classless_routes $route $router"
		shift; shift; shift; shift
	done
}

delete_old_routes() {
	#route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
	if [ -n "$old_classless_routes" ]; then
		fill_classless_routes "$old_classless_routes"
		set $classless_routes
		while [ $# -gt 1 ]; do
			route delete "$1" "$2"
			shift; shift
		done
		return 0;
	fi

	# If we supported multiple default routes, we'd be removing each
	# one here.  We don't so just delete the default route if it's
	# through our interface.
	if is_default_interface; then
		route delete default >/dev/null 2>&1
	fi

	if [ -n "$old_static_routes" ]; then
		set $old_static_routes
		while [ $# -gt 1 ]; do
			route delete "$1" "$2"
			shift; shift
		done
	fi

	arp_flush
}

add_new_routes() {
	#route add $new_ip_address $LOCALHOST >/dev/null 2>&1

	# RFC 3442: If the DHCP server returns both a Classless Static
	# Routes option and a Router option, the DHCP client MUST ignore
	# the Router option.
	#
	# DHCP clients that support this option (Classless Static Routes)
	# MUST NOT install the routes specified in the Static Routes
	# option (option code 33) if both a Static Routes option and the
	# Classless Static Routes option are provided.

	if [ -n "$new_classless_routes" ]; then
		fill_classless_routes "$new_classless_routes"
		$LOGGER "New Classless Static Routes ($interface): $classless_routes"
		set $classless_routes
		while [ $# -gt 1 ]; do
			if [ "0.0.0.0" = "$2" ]; then
				route add "$1" -iface "$interface"
			else
				route add "$1" "$2"
			fi
			shift; shift
		done
		return
	fi

	for router in $new_routers; do
		if is_default_interface; then

			if [ "$new_ip_address" = "$router" ]; then
				route add default -iface $router >/dev/null 2>&1
			else
				route add default $router >/dev/null 2>&1
			fi
		fi
		# 2nd and subsequent default routers error out, so explicitly
		# stop processing the list after the first one.
		break
	done

	if [ -n "$new_static_routes" ]; then
		$LOGGER "New Static Routes ($interface): $new_static_routes"
		set $new_static_routes
		while [ $# -gt 1 ]; do
			route add $1 $2
			shift; shift
		done
	fi
}

add_new_resolv_conf() {
	# XXX Old code did not create/update resolv.conf unless both
	# $new_domain_name and $new_domain_name_servers were provided.  PR
	# #3135 reported some ISP's only provide $new_domain_name_servers and
	# thus broke the script. This code creates the resolv.conf if either
	# are provided.

	local tmpres=/var/run/resolv.conf.${interface}
	rm -f $tmpres

	if [ -n "$new_domain_search" ]; then
		echo "search $new_domain_search" >>$tmpres
	elif [ -n "$new_domain_name" ]; then
		echo "search $new_domain_name" >>$tmpres
	fi

	if [ -n "$new_domain_name_servers" ]; then
		for nameserver in $new_domain_name_servers; do
			echo "nameserver $nameserver" >>$tmpres
		done
	fi

	if [ -f $tmpres ]; then
		if [ -f /etc/resolv.conf.tail ]; then
			cat /etc/resolv.conf.tail >>$tmpres
		fi

		case $resolvconf_enable in
		# "no", "false", "off", or "0"
		[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
			# When resolv.conf is not changed actually, we don't
			# need to update it.
			# If /usr is not mounted yet, we cannot use cmp, then
			# the following test fails.  In such case, we simply
			# ignore an error and do update resolv.conf.
			if cmp -s $tmpres /etc/resolv.conf; then
				rm -f $tmpres
				return 0
			fi 2>/dev/null

			# In case (e.g. during OpenBSD installs)
			# /etc/resolv.conf is a symbolic link, take
			# care to preserve the link and write the new
			# data in the correct location.

			if [ -f /etc/resolv.conf ]; then
				cat /etc/resolv.conf > /etc/resolv.conf.save
			fi
			cat $tmpres > /etc/resolv.conf

			# Try to ensure correct ownership and permissions.
			chown -RL root:wheel /etc/resolv.conf
			chmod -RL 644 /etc/resolv.conf
			;;

		*)
			/sbin/resolvconf -a ${interface} < $tmpres
			;;
		esac

		rm -f $tmpres

		return 0
	fi

	return 1
}

# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
exit_with_hooks() {
	exit_status=$1
	if [ -f /etc/dhclient-exit-hooks ]; then
		. /etc/dhclient-exit-hooks
	fi
	# probably should do something with exit status of the local script
	exit $exit_status
}

# Get the interface with the current ipv4 default route on it using only
# commands that are available prior to /usr being mounted.
is_default_interface()
{
	routeget="`route -n get -inet default`"
	oldifs="$IFS"
	IFS="
"
	defif=
	for line in $routeget ; do
		case $line in
		*interface:*)
			defif=${line##*: }
			;;
		esac
	done
	IFS=${oldifs}

	if [ -z "$defif" -o "$defif" = "$interface" ]; then
		return 0
	else
		return 1
	fi
}

#
# Start of active code.
#

# Invoke the local dhcp client enter hooks, if they exist.
if [ -f /etc/dhclient-enter-hooks ]; then
	exit_status=0
	. /etc/dhclient-enter-hooks
	# allow the local script to abort processing of this state
	# local script must set exit_status variable to nonzero.
	if [ $exit_status -ne 0 ]; then
		exit $exit_status
	fi
fi

: ${resolvconf_enable="YES"}

case $reason in
MEDIUM)
	eval "$IFCONFIG $interface $medium"
	eval "$IFCONFIG $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
	sleep 1
	;;

PREINIT)
	delete_old_alias
	$IFCONFIG $interface inet alias 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
	;;

ARPCHECK|ARPSEND)
	;;

BOUND|RENEW|REBIND|REBOOT)
	check_hostname
	if [ -n "$old_ip_address" ]; then
		if [ "$old_ip_address" != "$alias_ip_address" ]; then
			delete_old_alias
		fi
		if [ "$old_ip_address" != "$new_ip_address" ]; then
			delete_old_address
			delete_old_routes
		fi
	fi
	if [ "$reason" = BOUND ] || \
	   [ "$reason" = REBOOT ] || \
	   [ -z "$old_ip_address" ] || \
	   [ "$old_ip_address" != "$new_ip_address" ]; then
		add_new_address
		add_new_routes
	fi
	if [ "$new_ip_address" != "$alias_ip_address" ]; then
		add_new_alias
	fi
	if is_default_interface; then
		add_new_resolv_conf
	fi
	;;

EXPIRE|FAIL)
	delete_old_alias
	if [ -n "$old_ip_address" ]; then
		delete_old_address
		delete_old_routes
	fi
	if [ -x $ARP ]; then
		$ARP -d -a -i $interface
	fi
	# XXX Why add alias we just deleted above?
	add_new_alias
	if is_default_interface; then
		case $resolvconf_enable in
		# "no", "false", "off", or "0"
		[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
			if [ -f /etc/resolv.conf.save ]; then
				cat /etc/resolv.conf.save > /etc/resolv.conf
			fi
			;;
		*)
			/sbin/resolvconf -d ${interface}
			;;
		esac
	fi
	;;

TIMEOUT)
	delete_old_alias
	add_new_address
	sleep 1
	if [ -n "$new_routers" ]; then
		$LOGGER "New Routers ($interface): $new_routers"
		set "$new_routers"
		if ping -q -c 1 -t 1 "$1"; then
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
				add_new_alias
			fi
			add_new_routes
			if ! is_default_interface; then
				exit_with_hooks 0
			fi
			if add_new_resolv_conf; then
				exit_with_hooks 0
			fi
		fi
	fi
	eval "$IFCONFIG $interface inet -alias $new_ip_address $medium"
	delete_old_routes
	exit_with_hooks 1
	;;
esac

exit_with_hooks 0
@


1.24.2.1
log
@file dhclient-script was added on branch RELENG_8_4 on 2013-03-28 13:03:31 +0000
@
text
@d1 407
@


1.24.2.2
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/248810
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@a0 386
#!/bin/sh
#
# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
# $FreeBSD: releng/8.4/sbin/dhclient/dhclient-script 230729 2012-01-29 11:00:00Z dumbbell $
#
# Copyright (c) 2003 Kenneth R Westerback <krw@@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#

ARP=/usr/sbin/arp
HOSTNAME=/bin/hostname
IFCONFIG='/sbin/ifconfig -n'

LOCALHOST=127.0.0.1

if [ -x /usr/bin/logger ]; then
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
else
	LOGGER=echo
fi

#
# Helper functions that implement common actions.
#

check_hostname() {
	current_hostname=`$HOSTNAME`
	if [ -z "$current_hostname" ]; then
		$LOGGER "New Hostname ($interface): $new_host_name"
		$HOSTNAME $new_host_name
	elif [ "$current_hostname" = "$old_host_name" -a \
	       "$new_host_name" != "$old_host_name" ]; then
		$LOGGER "New Hostname ($interface): $new_host_name"
		$HOSTNAME $new_host_name
	fi
}

arp_flush() {
	arp -an -i $interface | \
		sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
		sh >/dev/null 2>&1
}

delete_old_address() {
	eval "$IFCONFIG $interface inet -alias $old_ip_address $medium"
}

add_new_address() {
	eval "$IFCONFIG $interface \
		inet $new_ip_address \
		netmask $new_subnet_mask \
		broadcast $new_broadcast_address \
		$medium"

	$LOGGER "New IP Address ($interface): $new_ip_address"
	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
	$LOGGER "New Routers ($interface): $new_routers"
}

delete_old_alias() {
	if [ -n "$alias_ip_address" ]; then
		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
		#route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
	fi
}

add_new_alias() {
	if [ -n "$alias_ip_address" ]; then
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
		    $alias_subnet_mask
		#route add $alias_ip_address $LOCALHOST
	fi
}

fill_classless_routes() {
	set $1
	while [ $# -ge 5 ]; do
		if [ $1 -eq 0 ]; then
			route="default"
		elif [ $1 -le 8 ]; then
			route="$2.0.0.0/$1"
			shift
		elif [ $1 -le 16 ]; then
			route="$2.$3.0.0/$1"
			shift; shift
		elif [ $1 -le 24 ]; then
			route="$2.$3.$4.0/$1"
			shift; shift; shift
		else
			route="$2.$3.$4.$5/$1"
			shift; shift; shift; shift
		fi
		shift
		router="$1.$2.$3.$4"
		classless_routes="$classless_routes $route $router"
		shift; shift; shift; shift
	done
}

delete_old_routes() {
	#route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
	if [ -n "$old_classless_routes" ]; then
		fill_classless_routes "$old_classless_routes"
		set $classless_routes
		while [ $# -gt 1 ]; do
			route delete "$1" "$2"
			shift; shift
		done
		return 0;
	fi

	# If we supported multiple default routes, we'd be removing each
	# one here.  We don't so just delete the default route if it's
	# through our interface.
	if is_default_interface; then
		route delete default >/dev/null 2>&1
	fi

	if [ -n "$old_static_routes" ]; then
		set $old_static_routes
		while [ $# -gt 1 ]; do
			route delete "$1" "$2"
			shift; shift
		done
	fi

	arp_flush
}

add_new_routes() {
	#route add $new_ip_address $LOCALHOST >/dev/null 2>&1

	# RFC 3442: If the DHCP server returns both a Classless Static
	# Routes option and a Router option, the DHCP client MUST ignore
	# the Router option.
	#
	# DHCP clients that support this option (Classless Static Routes)
	# MUST NOT install the routes specified in the Static Routes
	# option (option code 33) if both a Static Routes option and the
	# Classless Static Routes option are provided.

	if [ -n "$new_classless_routes" ]; then
		fill_classless_routes "$new_classless_routes"
		$LOGGER "New Classless Static Routes ($interface): $classless_routes"
		set $classless_routes
		while [ $# -gt 1 ]; do
			if [ "0.0.0.0" = "$2" ]; then
				route add "$1" -iface "$interface"
			else
				route add "$1" "$2"
			fi
			shift; shift
		done
		return
	fi

	for router in $new_routers; do
		if is_default_interface; then

			if [ "$new_ip_address" = "$router" ]; then
				route add default -iface $router >/dev/null 2>&1
			else
				route add default $router >/dev/null 2>&1
			fi
		fi
		# 2nd and subsequent default routers error out, so explicitly
		# stop processing the list after the first one.
		break
	done

	if [ -n "$new_static_routes" ]; then
		$LOGGER "New Static Routes ($interface): $new_static_routes"
		set $new_static_routes
		while [ $# -gt 1 ]; do
			route add $1 $2
			shift; shift
		done
	fi
}

add_new_resolv_conf() {
	# XXX Old code did not create/update resolv.conf unless both
	# $new_domain_name and $new_domain_name_servers were provided.  PR
	# #3135 reported some ISP's only provide $new_domain_name_servers and
	# thus broke the script. This code creates the resolv.conf if either
	# are provided.

	local tmpres=/var/run/resolv.conf.${interface}
	rm -f $tmpres

	if [ -n "$new_domain_search" ]; then
		echo "search $new_domain_search" >>$tmpres
	elif [ -n "$new_domain_name" ]; then
		echo "search $new_domain_name" >>$tmpres
	fi

	if [ -n "$new_domain_name_servers" ]; then
		for nameserver in $new_domain_name_servers; do
			echo "nameserver $nameserver" >>$tmpres
		done
	fi

	if [ -f $tmpres ]; then
		if [ -f /etc/resolv.conf.tail ]; then
			cat /etc/resolv.conf.tail >>$tmpres
		fi

		# When resolv.conf is not changed actually, we don't
		# need to update it.
		# If /usr is not mounted yet, we cannot use cmp, then
		# the following test fails.  In such case, we simply
		# ignore an error and do update resolv.conf.
		if cmp -s $tmpres /etc/resolv.conf; then
			rm -f $tmpres
			return 0
		fi 2>/dev/null

		# In case (e.g. during OpenBSD installs) /etc/resolv.conf
		# is a symbolic link, take care to preserve the link and write
		# the new data in the correct location.

		if [ -f /etc/resolv.conf ]; then
			cat /etc/resolv.conf > /etc/resolv.conf.save
		fi
		cat $tmpres > /etc/resolv.conf
		rm -f $tmpres

		# Try to ensure correct ownership and permissions.
		chown -RL root:wheel /etc/resolv.conf
		chmod -RL 644 /etc/resolv.conf

		return 0
	fi

	return 1
}

# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
exit_with_hooks() {
	exit_status=$1
	if [ -f /etc/dhclient-exit-hooks ]; then
		. /etc/dhclient-exit-hooks
	fi
	# probably should do something with exit status of the local script
	exit $exit_status
}

# Get the interface with the current ipv4 default route on it using only
# commands that are available prior to /usr being mounted.
is_default_interface()
{
	routeget="`route -n get -inet default`"
	oldifs="$IFS"
	IFS="
"
	defif=
	for line in $routeget ; do
		case $line in
		*interface:*)
			defif=${line##*: }
			;;
		esac
	done
	IFS=${oldifs}

	if [ -z "$defif" -o "$defif" = "$interface" ]; then
		return 0
	else
		return 1
	fi
}

#
# Start of active code.
#

# Invoke the local dhcp client enter hooks, if they exist.
if [ -f /etc/dhclient-enter-hooks ]; then
	exit_status=0
	. /etc/dhclient-enter-hooks
	# allow the local script to abort processing of this state
	# local script must set exit_status variable to nonzero.
	if [ $exit_status -ne 0 ]; then
		exit $exit_status
	fi
fi

case $reason in
MEDIUM)
	eval "$IFCONFIG $interface $medium"
	eval "$IFCONFIG $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
	sleep 1
	;;

PREINIT)
	delete_old_alias
	$IFCONFIG $interface inet alias 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
	;;

ARPCHECK|ARPSEND)
	;;

BOUND|RENEW|REBIND|REBOOT)
	check_hostname
	if [ -n "$old_ip_address" ]; then
		if [ "$old_ip_address" != "$alias_ip_address" ]; then
			delete_old_alias
		fi
		if [ "$old_ip_address" != "$new_ip_address" ]; then
			delete_old_address
			delete_old_routes
		fi
	fi
	if [ "$reason" = BOUND ] || \
	   [ "$reason" = REBOOT ] || \
	   [ -z "$old_ip_address" ] || \
	   [ "$old_ip_address" != "$new_ip_address" ]; then
		add_new_address
		add_new_routes
	fi
	if [ "$new_ip_address" != "$alias_ip_address" ]; then
		add_new_alias
	fi
	if is_default_interface; then
		add_new_resolv_conf
	fi
	;;

EXPIRE|FAIL)
	delete_old_alias
	if [ -n "$old_ip_address" ]; then
		delete_old_address
		delete_old_routes
	fi
	if [ -x $ARP ]; then
		$ARP -d -a -i $interface
	fi
	# XXX Why add alias we just deleted above?
	add_new_alias
	if is_default_interface; then
		if [ -f /etc/resolv.conf.save ]; then
			cat /etc/resolv.conf.save > /etc/resolv.conf
		fi
	fi
	;;

TIMEOUT)
	delete_old_alias
	add_new_address
	sleep 1
	if [ -n "$new_routers" ]; then
		$LOGGER "New Routers ($interface): $new_routers"
		set "$new_routers"
		if ping -q -c 1 -t 1 "$1"; then
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
				add_new_alias
			fi
			add_new_routes
			if ! is_default_interface; then
				exit_with_hooks 0
			fi
			if add_new_resolv_conf; then
				exit_with_hooks 0
			fi
		fi
	fi
	eval "$IFCONFIG $interface inet -alias $new_ip_address $medium"
	delete_old_routes
	exit_with_hooks 1
	;;
esac

exit_with_hooks 0
@


1.23
log
@SVN rev 228463 on 2011-12-13 11:54:51Z by glebius

dhclient-script relied on incorrect behavior of SIOCAIFADDR ioctl,
that changed 0.0.0.0/0.0.0.0 prefix to 0.0.0.0/255.0.0.0.

In the r228313 this behavior was fixed, and since dhclient-script
got broken.

I'm not sure this fix is a perfect one, it just changes
dhclient-script to set 0.0.0.0/255.0.0.0 explicitly.

PR:		kern/163206
@
text
@d4 1
a4 1
# $FreeBSD$
@


1.22
log
@SVN rev 228259 on 2011-12-04 14:44:31Z by dumbbell

Support domain-search in dhclient(8)

The "domain-search" option (option 119) allows a DHCP server to publish
a list of implicit domain suffixes used during name lookup. This option
is described in RFC 3397.

For instance, if the domain-search option says:
    ".example.org .example.com"
and one wants to resolve "foobar", the resolver will try:
    1. "foobar.example.org"
    2. "foobar.example.com"

The file /etc/resolv.conf is updated with a "search" directive if the
DHCP server provides "domain-search".

A regression test suite is included in this patch under
tools/regression/sbin/dhclient.

PR:		bin/151940
Sponsored by	Yakaz (http://www.yakaz.com)
@
text
@d323 1
a323 1
	$IFCONFIG $interface inet alias 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
@


1.21
log
@SVN rev 219739 on 2011-03-18 12:23:20Z by ume

Use resolvconf(8) to update /etc/resolv.conf.
If you don't want to use resolvconf(8) to update /etc/resolv.conf,
you can put resolvconf_enable="NO" into /etc/dhclient-enter-hooks.
@
text
@d204 3
a206 1
	if [ -n "$new_domain_name" ]; then
@


1.21.2.1
log
@SVN rev 225736 on 2011-09-23 00:51:37Z by kensmith

Copy head to stable/9 as part of 9.0-RELEASE release cycle.

Approved by:	re (implicit)
@
text
@@


1.21.2.2
log
@SVN rev 229583 on 2012-01-05 11:14:28Z by glebius

Merge r228463, that explicily uses 255.0.0.0 mask for the temporary prefix.
This change isn't actually needed in the stable/9, but let it be here, in
case if anyone tries to run stable/9 world on a head/ kernel.
@
text
@d321 1
a321 1
	$IFCONFIG $interface inet alias 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
@


1.21.2.3
log
@SVN rev 230597 on 2012-01-26 19:46:13Z by dumbbell

MFC r228259:
Support domain-search in dhclient(8)

The "domain-search" option (option 119) allows a DHCP server to publish
a list of implicit domain suffixes used during name lookup. This option
is described in RFC 3397.

For instance, if the domain-search option says:
    ".example.org .example.com"
and one wants to resolve "foobar", the resolver will try:
    1. "foobar.example.org"
    2. "foobar.example.com"

The file /etc/resolv.conf is updated with a "search" directive if the
DHCP server provides "domain-search".

A regression test suite is included in this patch under
tools/regression/sbin/dhclient.

PR:		bin/151940
Sponsored by:	Yakaz (http://www.yakaz.com)

MFC r229000:
Invalid Domain Search option isn't considered as a fatal error

In the original Domain Search option patch, an invalid option value
would cause the whole lease to be rejected. However, DHCP servers who
emit such an invalid value are more common than I thought. With this new
patch, just the option is rejected, not the entire lease.

PR:		bin/163431
Submitted by:	Fabian Keil <fk@@fabiankeil.de> (earlier version)
Reviewed by:	Fabian Keil <fk@@fabiankeil.de>
Sponsored by:	Yakaz (http://www.yakaz.com)

MFC r229001:
Adapt testsuite following change in Domain Search error handling

In this testsuite, warning() and error() have the same behaviour.

PR:		bin/163431
Sponsored by:	Yakaz (http://www.yakaz.com)
@
text
@d204 1
a204 3
	if [ -n "$new_domain_search" ]; then
		echo "search $new_domain_search" >>$tmpres
	elif [ -n "$new_domain_name" ]; then
@


1.21.2.4
log
@## SVN ##
## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/ 242902
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ## r242902 | dteske | 2012-11-11 23:29:45 +0000 (Sun, 11 Nov 2012) | 10 lines
## SVN ##
## SVN ## Fix a regression introduced by SVN r211417 that saw the breakage of a feature
## SVN ## documented in usr.sbin/sysinstall/help/shortcuts.hlp (reproduced below):
## SVN ##
## SVN ## If /usr/sbin/sysinstall is linked to another filename, say
## SVN ## `/usr/local/bin/configPackages', then the basename will be used
## SVN ## as an implicit command name.
## SVN ##
## SVN ## Reviewed by:	adrian (co-mentor)
## SVN ## Approved by:	adrian (co-mentor)
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ##
@
text
@d4 1
a4 1
# $FreeBSD: stable/9/sbin/dhclient/dhclient-script 230728 2012-01-29 10:55:19Z dumbbell $
@


1.21.2.3.2.1
log
@SVN rev 239080 on 2012-08-05 23:54:33Z by kensmith

Copy stable/9 to releng/9.1 as part of the 9.1-RELEASE release process.

Approved by:	re (implicit)
@
text
@@


1.21.2.3.2.2
log
@Switch importer
@
text
@d4 1
a4 1
# $FreeBSD: releng/9.1/sbin/dhclient/dhclient-script 230728 2012-01-29 10:55:19Z dumbbell $
@


1.21.2.1.2.1
log
@SVN rev 227445 on 2011-11-11 04:20:22Z by kensmith

Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
cycle.

Approved by:	re (implicit)
@
text
@@


1.21.2.1.2.2
log
@Switch importer
@
text
@d4 1
a4 1
# $FreeBSD: releng/9.0/sbin/dhclient/dhclient-script 219739 2011-03-18 12:23:20Z ume $
@


1.20
log
@SVN rev 179689 on 2008-06-09 20:03:35Z by brooks

Use the -n flag to route(8) when calling "route get".  Otherwise we hang
for a long time if we get a lease, but DNS isn't working.

MFC after:	1 week
@
text
@d219 32
a250 13
		# When resolv.conf is not changed actually, we don't
		# need to update it.
		# If /usr is not mounted yet, we cannot use cmp, then
		# the following test fails.  In such case, we simply
		# ignore an error and do update resolv.conf.
		if cmp -s $tmpres /etc/resolv.conf; then
			rm -f $tmpres
			return 0
		fi 2>/dev/null

		# In case (e.g. during OpenBSD installs) /etc/resolv.conf
		# is a symbolic link, take care to preserve the link and write
		# the new data in the correct location.
a251 4
		if [ -f /etc/resolv.conf ]; then
			cat /etc/resolv.conf > /etc/resolv.conf.save
		fi
		cat $tmpres > /etc/resolv.conf
a253 4
		# Try to ensure correct ownership and permissions.
		chown -RL root:wheel /etc/resolv.conf
		chmod -RL 644 /etc/resolv.conf

d310 2
d365 11
a375 3
		if [ -f /etc/resolv.conf.save ]; then
			cat /etc/resolv.conf.save > /etc/resolv.conf
		fi
@


1.20.2.1
log
@SVN rev 196045 on 2009-08-03 08:13:06Z by kensmith

Copy head to stable/8 as part of 8.0 Release cycle.

Approved by:	re (Implicit)
@
text
@@


1.20.2.2
log
@SVN rev 230093 on 2012-01-13 23:25:58Z by glebius

Merge r228463, that explicily uses 255.0.0.0 mask for the temporary prefix.
This change isn't actually needed in the stable/8, but let it be here, in
case if anyone tries to run stable/8 world on a head/ kernel.
@
text
@d308 1
a308 1
	$IFCONFIG $interface inet alias 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
@


1.20.2.3
log
@SVN rev 230603 on 2012-01-26 22:01:05Z by dumbbell

MFC r228259:
Support domain-search in dhclient(8)

The "domain-search" option (option 119) allows a DHCP server to publish
a list of implicit domain suffixes used during name lookup. This option
is described in RFC 3397.

For instance, if the domain-search option says:
    ".example.org .example.com"
and one wants to resolve "foobar", the resolver will try:
    1. "foobar.example.org"
    2. "foobar.example.com"

The file /etc/resolv.conf is updated with a "search" directive if the
DHCP server provides "domain-search".

A regression test suite is included in this patch under
tools/regression/sbin/dhclient.

PR:		bin/151940
Sponsored by:	Yakaz (http://www.yakaz.com)

MFC r229000:
Invalid Domain Search option isn't considered as a fatal error

In the original Domain Search option patch, an invalid option value
would cause the whole lease to be rejected. However, DHCP servers who
emit such an invalid value are more common than I thought. With this new
patch, just the option is rejected, not the entire lease.

PR:		bin/163431
Submitted by:	Fabian Keil <fk@@fabiankeil.de> (earlier version)
Reviewed by:	Fabian Keil <fk@@fabiankeil.de>
Sponsored by:	Yakaz (http://www.yakaz.com)

MFC r229001:
Adapt testsuite following change in Domain Search error handling

In this testsuite, warning() and error() have the same behaviour.

PR:		bin/163431
Sponsored by:	Yakaz (http://www.yakaz.com)
@
text
@d204 1
a204 3
	if [ -n "$new_domain_search" ]; then
		echo "search $new_domain_search" >>$tmpres
	elif [ -n "$new_domain_name" ]; then
@


1.20.2.4
log
@## SVN ##
## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/ 242909
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ## r242909 | dim | 2012-11-12 07:47:19 +0000 (Mon, 12 Nov 2012) | 20 lines
## SVN ##
## SVN ## MFC r242625:
## SVN ##
## SVN ## Remove duplicate const specifiers in many drivers (I hope I got all of
## SVN ## them, please let me know if not).  Most of these are of the form:
## SVN ##
## SVN ## static const struct bzzt_type {
## SVN ##       [...list of members...]
## SVN ## } const bzzt_devs[] = {
## SVN ##       [...list of initializers...]
## SVN ## };
## SVN ##
## SVN ## The second const is unnecessary, as arrays cannot be modified anyway,
## SVN ## and if the elements are const, the whole thing is const automatically
## SVN ## (e.g. it is placed in .rodata).
## SVN ##
## SVN ## I have verified this does not change the binary output of a full kernel
## SVN ## build (except for build timestamps embedded in the object files).
## SVN ##
## SVN ## Reviewed by:	yongari, marius
## SVN ##
## SVN ## ------------------------------------------------------------------------
## SVN ##
@
text
@d4 1
a4 1
# $FreeBSD: stable/8/sbin/dhclient/dhclient-script 230729 2012-01-29 11:00:00Z dumbbell $
@


1.20.2.3.2.1
log
@SVN rev 232438 on 2012-03-03 06:15:13Z by kensmith

Copy stable/8 to releng/8.3 as part of 8.3-RELEASE release cycle.

Approved by:	re (implicit)
@
text
@@


1.20.2.3.2.2
log
@Switch importer
@
text
@d4 1
a4 1
# $FreeBSD: releng/8.3/sbin/dhclient/dhclient-script 230729 2012-01-29 11:00:00Z dumbbell $
@


1.20.2.1.6.1
log
@SVN rev 216617 on 2010-12-21 17:09:25Z by kensmith

Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.

Approved by:	re (implicit)
@
text
@@


1.20.2.1.4.1
log
@SVN rev 209145 on 2010-06-14 02:09:06Z by kensmith

Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.

Approved by:	re (implicit)
@
text
@@


1.20.2.1.2.1
log
@SVN rev 198460 on 2009-10-25 01:10:29Z by kensmith

Copy stable/8 to releng/8.0 as part of 8.0-RELEASE release procedure.

Approved by:	re (implicit)
@
text
@@


1.19
log
@Add a new function is_default_interface() which determines if this
interface is one with the default route (or there isn't one).  Use it to
decide if we should adjust the default route and /etc/resolv.conf.

Fix the delete of the default route.  The if statement was totally bogus
and the delete only worked due to a typo. [1]

Reported by:	Jordan Coleman <jordan at JordanColeman dot com> [1]
MFC after:	1 week
@
text
@d263 1
a263 1
	routeget="`route get -inet default`"
@


1.18
log
@During PREINIT, when giving the interface the address 0.0.0.0, do it as an
alias to avoid distrubing other addresses.

PR:		bin/119255
Submitted by:	Jaakko Heinonen <jh at saunalahti dot fi>
@
text
@a22 1
AWK=/usr/bin/awk
a24 1
NETSTAT=/usr/bin/netstat
d125 6
a130 5
	for router in $old_routers; do
		if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then
			route delete default $route >/dev/null 2>&1
		fi
	done
d171 7
a177 4
		if [ "$new_ip_address" = "$router" ]; then
			route add default -iface $router >/dev/null 2>&1
		else
			route add default $router >/dev/null 2>&1
d259 25
a298 6
if [ -x $NETSTAT ]; then
	if_defaultroute=`$NETSTAT -rnf inet | $AWK '{if ($1=="default") printf $6}'`
else
	if_defaultroute="x"
fi

d335 3
a337 1
	add_new_resolv_conf
d351 4
a354 2
	if [ -f /etc/resolv.conf.save ]; then
		cat /etc/resolv.conf.save > /etc/resolv.conf
d370 3
@


1.17
log
@Use the -n flag on ifconfig so that dhclient does not cause the kernel module
to be reloaded when the interface is torn down.

Reviewed by:	brooks
Approved by:	re (kensmith)
@
text
@d287 1
a287 1
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
@


1.17.2.1
log
@MFC rev 1.18:

During PREINIT, when giving the interface the address 0.0.0.0, do it as an
alias to avoid distrubing other addresses.

PR:		bin/119255
Submitted by:	Jaakko Heinonen <jh at saunalahti dot fi>
@
text
@d287 1
a287 1
	$IFCONFIG $interface inet alias 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
@


1.17.2.2
log
@MFC rev 1.19

Add a new function is_default_interface() which determines if this
interface is one with the default route (or there isn't one).  Use it to
decide if we should adjust the default route and /etc/resolv.conf.

Fix the delete of the default route.  The if statement was totally bogus
and the delete only worked due to a typo. [1]

Reported by:    Jordan Coleman <jordan at JordanColeman dot com> [1]
@
text
@d23 1
d26 1
d127 5
a131 6
	# If we supported multiple default routes, we'd be removing each
	# one here.  We don't so just delete the default route if it's
	# through our interface.
	if is_default_interface; then
		route delete default >/dev/null 2>&1
	fi
d172 4
a175 7
		if is_default_interface; then

			if [ "$new_ip_address" = "$router" ]; then
				route add default -iface $router >/dev/null 2>&1
			else
				route add default $router >/dev/null 2>&1
			fi
a256 25
# Get the interface with the current ipv4 default route on it using only
# commands that are available prior to /usr being mounted.
is_default_interface()
{
	routeget="`route get -inet default`"
	oldifs="$IFS"
	IFS="
"
	defif=
	for line in $routeget ; do
		case $line in
		*interface:*)
			defif=${line##*: }
			;;
		esac
	done
	IFS=${oldifs}

	if [ -z "$defif" -o "$defif" = "$interface" ]; then
		return 0
	else
		return 1
	fi
}

d272 6
d314 1
a314 3
	if is_default_interface; then
		add_new_resolv_conf
	fi
d328 2
a329 4
	if is_default_interface; then
		if [ -f /etc/resolv.conf.save ]; then
			cat /etc/resolv.conf.save > /etc/resolv.conf
		fi
a344 3
			if ! is_default_interface; then
				exit_with_hooks 0
			fi
@


1.17.2.3
log
@Switch importer
@
text
@d4 1
a4 1
# $FreeBSD: stable/7/sbin/dhclient/dhclient-script 178089 2008-04-11 00:18:37Z brooks $
@


1.17.2.2.8.1
log
@SVN rev 216618 on 2010-12-21 17:10:29Z by kensmith

Copy stable/7 to releng/7.4 in preparation for FreeBSD-7.4 release.

Approved by:	re (implicit)
@
text
@@


1.17.2.2.8.2
log
@Switch importer
@
text
@d4 1
a4 1
# $FreeBSD: releng/7.4/sbin/dhclient/dhclient-script 178089 2008-04-11 00:18:37Z brooks $
@


1.17.2.2.6.1
log
@SVN rev 203736 on 2010-02-10 00:26:20Z by kensmith

Copy stable/7 to releng/7.3 as part of the 7.3-RELEASE process.

Approved by:	re (implicit)
@
text
@@


1.17.2.2.4.1
log
@SVN rev 191087 on 2009-04-15 03:14:26Z by kensmith

Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.

Approved by:	re (implicit)
@
text
@@


1.17.2.2.2.1
log
@SVN rev 185281 on 2008-11-25 02:59:29Z by kensmith

Create releng/7.1 in preparation for moving into RC phase of 7.1 release
cycle.

Approved by:	re (implicit)
@
text
@@


1.16
log
@The minimum size of an RFC3442 destination descriptor is five bytes, so
correct test to -ge 5.  Without this change an RFC3442 encoded default
route would be ignored.

Reported by:	Cedric Jonas <cedric at decemplex dot net>
@
text
@d25 1
d59 1
a59 1
	eval "ifconfig $interface inet -alias $old_ip_address $medium"
d63 1
a63 1
	eval "ifconfig $interface \
d77 1
a77 1
		ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
d84 1
a84 1
		ifconfig $interface inet alias $alias_ip_address netmask \
d280 2
a281 2
	eval "ifconfig $interface $medium"
	eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
d287 1
a287 1
	ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
d350 1
a350 1
	eval "ifconfig $interface inet -alias $new_ip_address $medium"
@


1.15
log
@Implement RFC3442, the Classless Static Route option.

The original DHCP specification includes a route option but it supports
only class-based routes.  RFC3442 adds support for specifying the netmask
width for each static route.  A variable length encoding is used to minimize
the size of this option.

PR:             bin/99534
Submitted by:   Andrey V. Elsukov <bu7cher@@yandex.ru>
Reviewed by:    brooks
@
text
@d91 1
a91 1
	while [ $# -gt 5 ]; do
@


1.14
log
@Fix rev 1.12.

/tmp may not be writeable yet when dhclient is first run via
/etc/rc.d/netif so using it may not work.  Also, writing to a
predictable file in /tmp as root is a really bad idea since a malicious
user may be able to win a race and insert a symlink which will allow
them to cause any file to be overwritten.  To solve these problems,
create the tempory file in /var/run which will exist this early and is
writable only by root.

Security:	Local risk if users can cause dhclient to run on demand
		(such as by unplugging and replugging the network cable).
@
text
@d89 25
d116 10
d145 25
@


1.13
log
@Give the TIMEOUT case a chance to work by using -t # instead of the
OpenBSD -w # when invoking ping.

PR:		bin/92187
Submitted by:	"Shin'ya Kumabuchi" <kumabu at t3 dot rim dot or dot jp>
MFC After:	6 days
@
text
@d138 1
a138 1
	local tmpres=${TMPDIR:-/tmp}/resolv.conf.std
@


1.12
log
@Make dhclient-script more agreeable with read-only /etc.

PR:		90518
Submitted by:	John E. Hein <jhein@@timing.com>
MFC after:	3 days
@
text
@d279 1
a279 1
		if ping -q -c 1 -w 1 "$1"; then
@


1.11
log
@When we give up on an interface, use the arp(8) command to remove all
entries from the interface rather than using ifconfig's delete command.
This preserves non-dhclient configured addresses (though they are wiped
out when dhclient is restarted).

MFC after:	1 week
@
text
@d138 2
a139 1
	rm -f /etc/resolv.conf.std
d142 1
a142 1
		echo "search $new_domain_name" >>/etc/resolv.conf.std
d147 1
a147 1
			echo "nameserver $nameserver" >>/etc/resolv.conf.std
d151 1
a151 1
	if [ -f /etc/resolv.conf.std ]; then
d153 1
a153 1
			cat /etc/resolv.conf.tail >>/etc/resolv.conf.std
d161 2
a162 2
		if cmp -s /etc/resolv.conf.std /etc/resolv.conf; then
			rm -f /etc/resolv.conf.std
d173 2
a174 2
		cat /etc/resolv.conf.std > /etc/resolv.conf
		rm -f /etc/resolv.conf.std
@


1.10
log
@Avoid updating resolv.conf when no changes have actually occured.

Submitted by:	ume
@
text
@d22 1
a22 1
NETSTAT=/usr/bin/netstat
d25 1
d261 3
a263 1
	ifconfig $interface delete
@


1.9
log
@When we fail to aquire a lease, our lease expires without a sucessful
renewal, or we lose link, be more forceful about clearing interface
state so another interface that connects to the same network has a
chance of working.  This doesn't address attemping to connect to both at
once, but appears to allow unplugging from a wired interface and then
inserting a wireless card that associates with an AP bridged to the same
LAN.
@
text
@d154 10
@


1.8
log
@The $medium string often contains quoted values with spaces in them (ssids,
for example).  Follow the example of the ISC script and wrap ifconfig
calls using $medium in eval "..." so this works.

Reported by:	iedowse
@
text
@d250 1
@


1.7
log
@Use a more robust, grep-free command to get the interface of the current
default route.

Submitted by:	Rostislav Krasny <rosti dot bsd at gmail dot com>
@
text
@d57 1
a57 1
	ifconfig $interface inet -alias $old_ip_address $medium
d61 1
a61 1
	ifconfig $interface \
d65 1
a65 1
		$medium
d207 2
a208 2
	ifconfig $interface $medium
	ifconfig $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
d274 1
a274 1
	ifconfig $interface inet -alias $new_ip_address $medium
@


1.6
log
@Don't and/remove a route to our assigned IP through 127.0.0.1.  It
serves no apparent purpose (we commented this out ages ago in the ISC
scripts) and cases problems with some ADSL setups.

Reported by:	Rostislav Krasny <rosti dot bsd at gmail dot com>
@
text
@a22 1
GREP=/usr/bin/grep
d200 1
a200 1
	if_defaultroute=`$NETSTAT -rn | $GREP "^default" | $AWK '{print $6}'`
@


1.5
log
@s/if_defaulroute/if_defaultroute/

We may now handle route updates better.
@
text
@d77 1
a77 1
		route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
d85 1
a85 1
		route add $alias_ip_address $LOCALHOST
d90 1
a90 1
	route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
d109 1
a109 1
	route add $new_ip_address $LOCALHOST >/dev/null 2>&1
@


1.4
log
@Add support for /etc/dhclient-enter-hooks to match /etc/dhclient-exit-hooks.
@
text
@d201 1
a201 1
	if_defaulroute=`$NETSTAT -rn | $GREP "^default" | $AWK '{print $6}'`
@


1.4.2.1
log
@Sync dhclient with HEAD:

 - Don't complain when debouncing dhclient startup.
 - Fix buffer handling in reveive_packet().  This fixes infinite cpu
   eating loops and probably some crashes.
 - Spell if_defaultroute route correctly in dhclient-script so we
   are allowed to change the default route.
 - Document dhclient -b.
 - Treat reassociation like association.
 - Do not force server-name to be a valid domain name.
 - Handle servers that send NUL-terminated host-name options.

Approved by:	re (scottl)
@
text
@d201 1
a201 1
	if_defaultroute=`$NETSTAT -rn | $GREP "^default" | $AWK '{print $6}'`
@


1.4.2.2
log
@Sync dhclient with HEAD with exception of bpf.c:1.5.

sbin/dhclient/*.c:
 - add __FBSDID
sbin/dhclient/conflex.c: 1.3-1.4
 - fix a minor buffer overflow in config parsing
 - actually support backslash escaping in config files
bin/dhclient/dhclient-script: 1.6-1.10
 - don't create or remove routes to our address through 127.0.0.1
 - improved detection of the interface of the default route
 - support quotes in the medium string
 - clear interface state more effectively on failure
 - don't update resolv.conf when unchanged
bin/dhclient/dhclient.c: 1.10-1.11
 - validate domain-name (used as resolv.conf search string)
 - handle superseded subnet-mask correctly

Approved by:	re (scottl)
@
text
@d23 1
d58 1
a58 1
	eval "ifconfig $interface inet -alias $old_ip_address $medium"
d62 1
a62 1
	eval "ifconfig $interface \
d66 1
a66 1
		$medium"
d77 1
a77 1
		#route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
d85 1
a85 1
		#route add $alias_ip_address $LOCALHOST
d90 1
a90 1
	#route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
d109 1
a109 1
	#route add $new_ip_address $LOCALHOST >/dev/null 2>&1
a154 10
		# When resolv.conf is not changed actually, we don't
		# need to update it.
		# If /usr is not mounted yet, we cannot use cmp, then
		# the following test fails.  In such case, we simply
		# ignore an error and do update resolv.conf.
		if cmp -s /etc/resolv.conf.std /etc/resolv.conf; then
			rm -f /etc/resolv.conf.std
			return 0
		fi 2>/dev/null

d201 1
a201 1
	if_defaultroute=`$NETSTAT -rnf inet | $AWK '{if ($1=="default") printf $6}'`
d208 2
a209 2
	eval "ifconfig $interface $medium"
	eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
a250 1
	ifconfig $interface delete
d275 1
a275 1
	eval "ifconfig $interface inet -alias $new_ip_address $medium"
@


1.4.2.3
log
@MFC rev 1.11:
  When we give up on an interface, use the arp(8) command to remove all
  entries from the interface rather than using ifconfig's delete command.
  This preserves non-dhclient configured addresses (though they are wiped
  out when dhclient is restarted).
@
text
@d22 1
a22 1
ARP=/usr/sbin/arp
a24 1
NETSTAT=/usr/bin/netstat
d260 1
a260 3
	if [ -x $ARP ]; then
		$ARP -d -a -i $interface
	fi
@


1.4.2.4
log
@MFC revs 1.12-14:

 1.12,14 - Use /var/run instead of /etc for temporary files
 1.13 - Fix the TIMEOUT case

PR:		bin/90518, bin/92187
@
text
@d138 1
a138 2
	local tmpres=/var/run/resolv.conf.${interface}
	rm -f $tmpres
d141 1
a141 1
		echo "search $new_domain_name" >>$tmpres
d146 1
a146 1
			echo "nameserver $nameserver" >>$tmpres
d150 1
a150 1
	if [ -f $tmpres ]; then
d152 1
a152 1
			cat /etc/resolv.conf.tail >>$tmpres
d160 2
a161 2
		if cmp -s $tmpres /etc/resolv.conf; then
			rm -f $tmpres
d172 2
a173 2
		cat $tmpres > /etc/resolv.conf
		rm -f $tmpres
d278 1
a278 1
		if ping -q -c 1 -t 1 "$1"; then
@


1.4.2.5
log
@MFC r1.17
  Use the -n flag on ifconfig so that dhclient does not cause the kernel module
  to be reloaded when the interface is torn down.
@
text
@a24 1
IFCONFIG='/sbin/ifconfig -n'
d58 1
a58 1
	eval "$IFCONFIG $interface inet -alias $old_ip_address $medium"
d62 1
a62 1
	eval "$IFCONFIG $interface \
d76 1
a76 1
		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
d83 1
a83 1
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
d219 2
a220 2
	eval "$IFCONFIG $interface $medium"
	eval "$IFCONFIG $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
d226 1
a226 1
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
d289 1
a289 1
	eval "$IFCONFIG $interface inet -alias $new_ip_address $medium"
@


1.4.2.6
log
@MFC to RELENG_6 dhclient RFC3442 support (the classless static route
option)

dhclient.conf 1.2

  Add a $FreeBSD$ tag missing after the original import.  Note that this
  file isn't actually installed; the one in src/etc is.

clparse.c 1.3
dhclient-script 1.15
dhclient.c 1.21
dhclient.conf 1.3
dhcp.h 1.3
tables.c 1.4

  Implement RFC3442, the Classless Static Route option.

  The original DHCP specification includes a route option but it
  supports only class-based routes.  RFC3442 adds support for
  specifying the netmask width for each static route.  A variable
  length encoding is used to minimize the size of this option.

  PR:             bin/99534
  Submitted by:   Andrey V. Elsukov <bu7cher@@yandex.ru>
  Reviewed by:    brooks

dhclient-script 1.16

  The minimum size of an RFC3442 destination descriptor is five bytes,
  so correct test to -ge 5.  Without this change an RFC3442 encoded
  default route would be ignored.
@
text
@a89 25
fill_classless_routes() {
	set $1
	while [ $# -ge 5 ]; do
		if [ $1 -eq 0 ]; then
			route="default"
		elif [ $1 -le 8 ]; then
			route="$2.0.0.0/$1"
			shift
		elif [ $1 -le 16 ]; then
			route="$2.$3.0.0/$1"
			shift; shift
		elif [ $1 -le 24 ]; then
			route="$2.$3.$4.0/$1"
			shift; shift; shift
		else
			route="$2.$3.$4.$5/$1"
			shift; shift; shift; shift
		fi
		shift
		router="$1.$2.$3.$4"
		classless_routes="$classless_routes $route $router"
		shift; shift; shift; shift
	done
}

a91 10
	if [ -n "$old_classless_routes" ]; then
		fill_classless_routes "$old_classless_routes"
		set $classless_routes
		while [ $# -gt 1 ]; do
			route delete "$1" "$2"
			shift; shift
		done
		return 0;
	fi

a110 25

	# RFC 3442: If the DHCP server returns both a Classless Static
	# Routes option and a Router option, the DHCP client MUST ignore
	# the Router option.
	#
	# DHCP clients that support this option (Classless Static Routes)
	# MUST NOT install the routes specified in the Static Routes
	# option (option code 33) if both a Static Routes option and the
	# Classless Static Routes option are provided.

	if [ -n "$new_classless_routes" ]; then
		fill_classless_routes "$new_classless_routes"
		$LOGGER "New Classless Static Routes ($interface): $classless_routes"
		set $classless_routes
		while [ $# -gt 1 ]; do
			if [ "0.0.0.0" = "$2" ]; then
				route add "$1" -iface "$interface"
			else
				route add "$1" "$2"
			fi
			shift; shift
		done
		return
	fi

@


1.4.2.7
log
@MFC rev 1.18:

During PREINIT, when giving the interface the address 0.0.0.0, do it as an
alias to avoid distrubing other addresses.

PR:		bin/119255
Submitted by:	Jaakko Heinonen <jh at saunalahti dot fi>
@
text
@d287 1
a287 1
	$IFCONFIG $interface inet alias 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
@


1.4.2.8
log
@MFC rev 1.19

Add a new function is_default_interface() which determines if this
interface is one with the default route (or there isn't one).  Use it to
decide if we should adjust the default route and /etc/resolv.conf.

Fix the delete of the default route.  The if statement was totally bogus
and the delete only worked due to a typo. [1]

Reported by:    Jordan Coleman <jordan at JordanColeman dot com> [1]
@
text
@d23 1
d26 1
d127 5
a131 6
	# If we supported multiple default routes, we'd be removing each
	# one here.  We don't so just delete the default route if it's
	# through our interface.
	if is_default_interface; then
		route delete default >/dev/null 2>&1
	fi
d172 4
a175 7
		if is_default_interface; then

			if [ "$new_ip_address" = "$router" ]; then
				route add default -iface $router >/dev/null 2>&1
			else
				route add default $router >/dev/null 2>&1
			fi
a256 25
# Get the interface with the current ipv4 default route on it using only
# commands that are available prior to /usr being mounted.
is_default_interface()
{
	routeget="`route get -inet default`"
	oldifs="$IFS"
	IFS="
"
	defif=
	for line in $routeget ; do
		case $line in
		*interface:*)
			defif=${line##*: }
			;;
		esac
	done
	IFS=${oldifs}

	if [ -z "$defif" -o "$defif" = "$interface" ]; then
		return 0
	else
		return 1
	fi
}

d272 6
d314 1
a314 3
	if is_default_interface; then
		add_new_resolv_conf
	fi
d328 2
a329 4
	if is_default_interface; then
		if [ -f /etc/resolv.conf.save ]; then
			cat /etc/resolv.conf.save > /etc/resolv.conf
		fi
a344 3
			if ! is_default_interface; then
				exit_with_hooks 0
			fi
@


1.4.2.9
log
@Switch importer
@
text
@d4 1
a4 1
# $FreeBSD: stable/6/sbin/dhclient/dhclient-script 178089 2008-04-11 00:18:37Z brooks $
@


1.4.2.8.2.1
log
@SVN rev 183531 on 2008-10-02 02:57:24Z by kensmith

Create releng/6.4 from stable/6 in preparation for 6.4-RC1.

Approved by:	re (implicit)
@
text
@@


1.3
log
@Add support for /etc/dhclient-exit-hooks file.

Tested by:	Max Boyarov <max_b at tut dot by>
@
text
@d189 11
@


1.2
log
@FreeBSDize the dhclient-script.

Submitted by:	sam
@
text
@d175 10
d260 1
a260 1
				exit 0
d266 1
a266 1
	exit 1
d270 1
a270 1
exit 0
@


1.1
log
@Initial revision
@
text
@d4 1
d22 13
d39 18
d58 1
a58 4
	if [ -n "$old_ip_address" ]; then
		ifconfig $interface inet -alias $old_ip_address $medium
		route delete "$old_ip_address" 127.0.0.1 >/dev/null 2>&1
	fi
d68 4
a71 2
	# XXX Original TIMEOUT code did not do this unless $new_routers was set?
	route add $new_ip_address 127.0.0.1 >/dev/null 2>&1
d77 1
a77 1
		route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
d85 1
a85 1
		route add $alias_ip_address 127.0.0.1
d90 6
a95 3
	# Delete existing default route. We only allow one, so no need to
	# process $old_routers list.
	route delete default >/dev/null 2>&1
d105 1
a105 1
	arp -dan
d109 1
a109 1
	route delete default >/dev/null 2>&1
d122 1
d179 4
a182 6
if [ -n "$new_network_number" ]; then
	echo "New Network Number: $new_network_number"
fi

if [ -n "$new_broadcast_address" ]; then
	echo "New Broadcast Address: $new_broadcast_address"
d201 1
d242 1
a254 2
	# XXX Why not a delete_old_address as before all other invocations of
	#     delete_old_routes?
@


1.1.1.1
log
@Import the OpenBSD dhclient as shipped with OpenBSD-3.7 (the tag
OPENBSD_3_7).
@
text
@@
