xref: /onnv-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/init_reboot.c (revision 4106:dd61d0159830)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53431Scarlsonj  * Common Development and Distribution License (the "License").
63431Scarlsonj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223431Scarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * INIT_REBOOT state of the DHCP client state machine.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <limits.h>
330Sstevel@tonic-gate #include <sys/socket.h>
340Sstevel@tonic-gate #include <netinet/in.h>
350Sstevel@tonic-gate #include <netinet/dhcp.h>
360Sstevel@tonic-gate #include <netinet/udp.h>
370Sstevel@tonic-gate #include <netinet/ip_var.h>
380Sstevel@tonic-gate #include <netinet/udp_var.h>
390Sstevel@tonic-gate #include <dhcpmsg.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include "agent.h"
430Sstevel@tonic-gate #include "packet.h"
440Sstevel@tonic-gate #include "states.h"
450Sstevel@tonic-gate #include "util.h"
460Sstevel@tonic-gate #include "interface.h"
470Sstevel@tonic-gate #include "defaults.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static stop_func_t	stop_init_reboot;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
523431Scarlsonj  * dhcp_init_reboot_v4(): attempts to reuse a cached configuration for a state
533431Scarlsonj  *			  machine.
540Sstevel@tonic-gate  *
553431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
560Sstevel@tonic-gate  *  output: void
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
593431Scarlsonj static void
603431Scarlsonj dhcp_init_reboot_v4(dhcp_smach_t *dsmp)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	dhcp_pkt_t		*dpkt;
630Sstevel@tonic-gate 	const char		*reqhost;
640Sstevel@tonic-gate 	char			hostfile[PATH_MAX + 1];
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * assemble DHCPREQUEST message.  The max dhcp message size
680Sstevel@tonic-gate 	 * option is set to the interface max, minus the size of the udp and
690Sstevel@tonic-gate 	 * ip headers.
700Sstevel@tonic-gate 	 */
710Sstevel@tonic-gate 
723431Scarlsonj 	dpkt = init_pkt(dsmp, REQUEST);
733431Scarlsonj 	(void) add_pkt_opt32(dpkt, CD_REQUESTED_IP_ADDR,
743431Scarlsonj 	    dsmp->dsm_ack->pkt->yiaddr.s_addr);
750Sstevel@tonic-gate 
763431Scarlsonj 	(void) add_pkt_opt32(dpkt, CD_LEASE_TIME, htonl(DHCP_PERM));
773431Scarlsonj 	(void) add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE,
783431Scarlsonj 	    htons(dsmp->dsm_lif->lif_pif->pif_max - sizeof (struct udpiphdr)));
790Sstevel@tonic-gate 
803448Sdh155122 	if (class_id_len != 0)
813448Sdh155122 		(void) add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len);
823431Scarlsonj 	(void) add_pkt_prl(dpkt, dsmp);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	/*
850Sstevel@tonic-gate 	 * Set CD_HOSTNAME option if REQUEST_HOSTNAME is set and a hostname
860Sstevel@tonic-gate 	 * is found in /etc/hostname.<ifname>
870Sstevel@tonic-gate 	 */
883431Scarlsonj 	if (df_get_bool(dsmp->dsm_name, dsmp->dsm_isv6, DF_REQUEST_HOSTNAME)) {
890Sstevel@tonic-gate 		(void) snprintf(hostfile, sizeof (hostfile), "/etc/hostname.%s",
903431Scarlsonj 		    dsmp->dsm_name);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 		if ((reqhost = iffile_to_hostname(hostfile)) != NULL) {
930Sstevel@tonic-gate 			dhcpmsg(MSG_DEBUG, "dhcp_selecting: host %s", reqhost);
943431Scarlsonj 			if ((dsmp->dsm_reqhost = strdup(reqhost)) != NULL)
953431Scarlsonj 				(void) add_pkt_opt(dpkt, CD_HOSTNAME,
963431Scarlsonj 				    dsmp->dsm_reqhost,
973431Scarlsonj 				    strlen(dsmp->dsm_reqhost));
980Sstevel@tonic-gate 			else
990Sstevel@tonic-gate 				dhcpmsg(MSG_WARNING, "dhcp_selecting: cannot"
1000Sstevel@tonic-gate 				    " allocate memory for host name option");
1013431Scarlsonj 		} else {
1023431Scarlsonj 			dhcpmsg(MSG_DEBUG,
1033431Scarlsonj 			    "dhcp_selecting: no hostname for %s",
1043431Scarlsonj 			    dsmp->dsm_name);
1050Sstevel@tonic-gate 		}
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1083431Scarlsonj 	(void) add_pkt_opt(dpkt, CD_END, NULL, 0);
1093431Scarlsonj 
1103431Scarlsonj 	(void) send_pkt(dsmp, dpkt, htonl(INADDR_BROADCAST), stop_init_reboot);
1113431Scarlsonj }
1123431Scarlsonj 
1133431Scarlsonj 
1143431Scarlsonj /*
1153431Scarlsonj  * dhcp_init_reboot_v6(): attempts to reuse a cached configuration for a state
1163431Scarlsonj  *			  machine.  Create a Confirm message and multicast it
1173431Scarlsonj  *			  out.
1183431Scarlsonj  *
1193431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
1203431Scarlsonj  *  output: void
1213431Scarlsonj  */
1223431Scarlsonj 
1233431Scarlsonj static void
1243431Scarlsonj dhcp_init_reboot_v6(dhcp_smach_t *dsmp)
1253431Scarlsonj {
1263431Scarlsonj 	dhcp_pkt_t *dpkt;
1273431Scarlsonj 	dhcpv6_option_t *d6o, *d6so, *popt;
1283431Scarlsonj 	uint_t olen, solen;
1293431Scarlsonj 	dhcpv6_ia_na_t d6in;
1303431Scarlsonj 	dhcpv6_iaaddr_t d6ia;
1313431Scarlsonj 	char *obase;
1323431Scarlsonj 
1333431Scarlsonj 	/*
1343431Scarlsonj 	 * Assemble a Confirm message based on the current ack.
1353431Scarlsonj 	 */
1363431Scarlsonj 
1373431Scarlsonj 	dpkt = init_pkt(dsmp, DHCPV6_MSG_CONFIRM);
1383431Scarlsonj 
1393431Scarlsonj 	/*
1403431Scarlsonj 	 * Loop over and copy IA_NAs and IAADDRs we have in our last ack.  This
1413431Scarlsonj 	 * is what we'll be requesting.
1423431Scarlsonj 	 */
1433431Scarlsonj 	d6o = NULL;
1443431Scarlsonj 	while ((d6o = dhcpv6_pkt_option(dsmp->dsm_ack, d6o, DHCPV6_OPT_IA_NA,
1453431Scarlsonj 	    &olen)) != NULL) {
1463431Scarlsonj 
1473431Scarlsonj 		/*
1483431Scarlsonj 		 * Copy in IA_NA option from the ack.  Note that we use zero
1493431Scarlsonj 		 * for all timers in accordance with RFC 3315.  (It would make
1503431Scarlsonj 		 * some sense to say what we think the current timers are as
1513431Scarlsonj 		 * a hint to the server, but the RFC doesn't agree.)
1523431Scarlsonj 		 */
1533431Scarlsonj 		if (olen < sizeof (dhcpv6_ia_na_t))
1543431Scarlsonj 			continue;
1553431Scarlsonj 		(void) memcpy(&d6in, d6o, sizeof (d6in));
1563431Scarlsonj 		d6in.d6in_t1 = 0;
1573431Scarlsonj 		d6in.d6in_t2 = 0;
1583431Scarlsonj 		popt = add_pkt_opt(dpkt, DHCPV6_OPT_IA_NA,
1593431Scarlsonj 		    (char *)&d6in + sizeof (*d6o),
1603431Scarlsonj 		    sizeof (d6in) - sizeof (*d6o));
1613431Scarlsonj 		if (popt == NULL)
1623431Scarlsonj 			goto failure;
1630Sstevel@tonic-gate 
1643431Scarlsonj 		/*
1653431Scarlsonj 		 * Now loop over the IAADDR suboptions and add those.
1663431Scarlsonj 		 */
1673431Scarlsonj 		obase = (char *)d6o + sizeof (dhcpv6_ia_na_t);
1683431Scarlsonj 		olen -= sizeof (dhcpv6_ia_na_t);
1693431Scarlsonj 		d6so = NULL;
1703431Scarlsonj 		while ((d6so = dhcpv6_find_option(obase, olen, d6so,
1713431Scarlsonj 		    DHCPV6_OPT_IAADDR, &solen)) != NULL) {
1723431Scarlsonj 			if (solen < sizeof (dhcpv6_iaaddr_t))
1733431Scarlsonj 				continue;
1743431Scarlsonj 			(void) memcpy(&d6ia, d6so, sizeof (d6ia));
1753431Scarlsonj 			d6ia.d6ia_preflife = 0;
1763431Scarlsonj 			d6ia.d6ia_vallife = 0;
1773431Scarlsonj 			if (add_pkt_subopt(dpkt, popt, DHCPV6_OPT_IAADDR,
1783431Scarlsonj 			    (char *)&d6ia + sizeof (*d6so),
1793431Scarlsonj 			    sizeof (d6ia) - sizeof (*d6so)) == NULL)
1803431Scarlsonj 				goto failure;
1813431Scarlsonj 		}
1823431Scarlsonj 	}
1833431Scarlsonj 
1843431Scarlsonj 	/* Add required Option Request option */
1853431Scarlsonj 	(void) add_pkt_prl(dpkt, dsmp);
1863431Scarlsonj 
1873431Scarlsonj 	(void) send_pkt_v6(dsmp, dpkt, ipv6_all_dhcp_relay_and_servers,
1883431Scarlsonj 	    stop_init_reboot, DHCPV6_CNF_TIMEOUT, DHCPV6_CNF_MAX_RT);
1893431Scarlsonj 
1903431Scarlsonj 	return;
1913431Scarlsonj 
1923431Scarlsonj failure:
193*4106Scarlsonj 	if (!set_start_timer(dsmp))
1943431Scarlsonj 		dhcp_selecting(dsmp);
1953431Scarlsonj }
1963431Scarlsonj 
1973431Scarlsonj /*
1983431Scarlsonj  * dhcp_init_reboot(): attempts to reuse a cached configuration for a state
1993431Scarlsonj  *		       machine.
2003431Scarlsonj  *
2013431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
2023431Scarlsonj  *  output: void
2033431Scarlsonj  */
2043431Scarlsonj 
2053431Scarlsonj void
2063431Scarlsonj dhcp_init_reboot(dhcp_smach_t *dsmp)
2073431Scarlsonj {
2083431Scarlsonj 	dhcpmsg(MSG_VERBOSE,  "%s has cached configuration - entering "
2093431Scarlsonj 	    "INIT_REBOOT", dsmp->dsm_name);
2103431Scarlsonj 
2113431Scarlsonj 	if (!set_smach_state(dsmp, INIT_REBOOT)) {
2123431Scarlsonj 		dhcpmsg(MSG_ERROR, "dhcp_init_reboot: cannot register to "
2133431Scarlsonj 		    "collect ACK/NAK packets, reverting to INIT on %s",
2143431Scarlsonj 		    dsmp->dsm_name);
2153431Scarlsonj 
2163431Scarlsonj 		dsmp->dsm_dflags |= DHCP_IF_FAILED;
2173431Scarlsonj 		(void) set_smach_state(dsmp, INIT);
2183431Scarlsonj 		ipc_action_finish(dsmp, DHCP_IPC_E_MEMORY);
2193431Scarlsonj 		return;
2203431Scarlsonj 	}
2213431Scarlsonj 
2223431Scarlsonj 	if (dsmp->dsm_isv6)
2233431Scarlsonj 		dhcp_init_reboot_v6(dsmp);
2243431Scarlsonj 	else
2253431Scarlsonj 		dhcp_init_reboot_v4(dsmp);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * stop_init_reboot(): decides when to stop retransmitting REQUESTs
2300Sstevel@tonic-gate  *
2313431Scarlsonj  *   input: dhcp_smach_t *: the state machine sending the REQUESTs
2320Sstevel@tonic-gate  *	    unsigned int: the number of REQUESTs sent so far
2330Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if retransmissions should stop
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static boolean_t
2373431Scarlsonj stop_init_reboot(dhcp_smach_t *dsmp, unsigned int n_requests)
2380Sstevel@tonic-gate {
2393431Scarlsonj 	if (dsmp->dsm_isv6) {
2403431Scarlsonj 		uint_t nowabs, maxabs;
2410Sstevel@tonic-gate 
2423431Scarlsonj 		nowabs = gethrtime() / (NANOSEC / MILLISEC);
2433431Scarlsonj 		maxabs = dsmp->dsm_neg_hrtime / (NANOSEC / MILLISEC) +
2443431Scarlsonj 		    DHCPV6_CNF_MAX_RD;
2453431Scarlsonj 		if (nowabs < maxabs) {
2463431Scarlsonj 			/* Cap the timer based on the maximum */
2473431Scarlsonj 			if (nowabs + dsmp->dsm_send_timeout > maxabs)
2483431Scarlsonj 				dsmp->dsm_send_timeout = maxabs - nowabs;
2493431Scarlsonj 			return (B_FALSE);
2503431Scarlsonj 		}
2513431Scarlsonj 		dhcpmsg(MSG_INFO, "no Reply to Confirm, using remainder of "
2523431Scarlsonj 		    "existing lease on %s", dsmp->dsm_name);
2533431Scarlsonj 	} else {
2543431Scarlsonj 		if (n_requests < DHCP_MAX_REQUESTS)
2553431Scarlsonj 			return (B_FALSE);
2563431Scarlsonj 		dhcpmsg(MSG_INFO, "no ACK/NAK to INIT_REBOOT REQUEST, "
2573431Scarlsonj 		    "using remainder of existing lease on %s", dsmp->dsm_name);
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2603431Scarlsonj 	/*
2613431Scarlsonj 	 * We already stuck our old ack in dsmp->dsm_ack and relativized the
2623431Scarlsonj 	 * packet times, so we can just pretend that the server sent it to us
2633431Scarlsonj 	 * and move to bound.  If that fails, fall back to selecting.
2643431Scarlsonj 	 */
2653431Scarlsonj 
2663431Scarlsonj 	if (dhcp_bound(dsmp, NULL)) {
2673431Scarlsonj 		if (dsmp->dsm_isv6) {
2683431Scarlsonj 			if (!save_server_id(dsmp, dsmp->dsm_ack))
2693431Scarlsonj 				goto failure;
2703431Scarlsonj 			server_unicast_option(dsmp, dsmp->dsm_ack);
2713431Scarlsonj 		}
2723431Scarlsonj 	} else {
2733431Scarlsonj failure:
2743431Scarlsonj 		dhcpmsg(MSG_INFO, "unable to use saved lease on %s; restarting",
2753431Scarlsonj 		    dsmp->dsm_name);
2763431Scarlsonj 		dhcp_selecting(dsmp);
2773431Scarlsonj 	}
2783431Scarlsonj 
2793431Scarlsonj 	return (B_TRUE);
2800Sstevel@tonic-gate }
281