xref: /onnv-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/init_reboot.c (revision 9633:309290318ad0)
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 /*
22*9633Sjames.d.carlson@sun.com  * Copyright 2009 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 #include <sys/types.h>
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <limits.h>
310Sstevel@tonic-gate #include <sys/socket.h>
320Sstevel@tonic-gate #include <netinet/in.h>
330Sstevel@tonic-gate #include <netinet/dhcp.h>
340Sstevel@tonic-gate #include <netinet/udp.h>
350Sstevel@tonic-gate #include <netinet/ip_var.h>
360Sstevel@tonic-gate #include <netinet/udp_var.h>
370Sstevel@tonic-gate #include <dhcpmsg.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "agent.h"
410Sstevel@tonic-gate #include "packet.h"
420Sstevel@tonic-gate #include "states.h"
430Sstevel@tonic-gate #include "util.h"
440Sstevel@tonic-gate #include "interface.h"
450Sstevel@tonic-gate #include "defaults.h"
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static stop_func_t	stop_init_reboot;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
503431Scarlsonj  * dhcp_init_reboot_v4(): attempts to reuse a cached configuration for a state
513431Scarlsonj  *			  machine.
520Sstevel@tonic-gate  *
533431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
540Sstevel@tonic-gate  *  output: void
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
573431Scarlsonj static void
dhcp_init_reboot_v4(dhcp_smach_t * dsmp)583431Scarlsonj dhcp_init_reboot_v4(dhcp_smach_t *dsmp)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	dhcp_pkt_t		*dpkt;
610Sstevel@tonic-gate 	const char		*reqhost;
620Sstevel@tonic-gate 	char			hostfile[PATH_MAX + 1];
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	/*
650Sstevel@tonic-gate 	 * assemble DHCPREQUEST message.  The max dhcp message size
660Sstevel@tonic-gate 	 * option is set to the interface max, minus the size of the udp and
670Sstevel@tonic-gate 	 * ip headers.
680Sstevel@tonic-gate 	 */
690Sstevel@tonic-gate 
703431Scarlsonj 	dpkt = init_pkt(dsmp, REQUEST);
713431Scarlsonj 	(void) add_pkt_opt32(dpkt, CD_REQUESTED_IP_ADDR,
723431Scarlsonj 	    dsmp->dsm_ack->pkt->yiaddr.s_addr);
730Sstevel@tonic-gate 
743431Scarlsonj 	(void) add_pkt_opt32(dpkt, CD_LEASE_TIME, htonl(DHCP_PERM));
753431Scarlsonj 	(void) add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE,
763431Scarlsonj 	    htons(dsmp->dsm_lif->lif_pif->pif_max - sizeof (struct udpiphdr)));
770Sstevel@tonic-gate 
783448Sdh155122 	if (class_id_len != 0)
793448Sdh155122 		(void) add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len);
803431Scarlsonj 	(void) add_pkt_prl(dpkt, dsmp);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	/*
830Sstevel@tonic-gate 	 * Set CD_HOSTNAME option if REQUEST_HOSTNAME is set and a hostname
840Sstevel@tonic-gate 	 * is found in /etc/hostname.<ifname>
850Sstevel@tonic-gate 	 */
863431Scarlsonj 	if (df_get_bool(dsmp->dsm_name, dsmp->dsm_isv6, DF_REQUEST_HOSTNAME)) {
870Sstevel@tonic-gate 		(void) snprintf(hostfile, sizeof (hostfile), "/etc/hostname.%s",
883431Scarlsonj 		    dsmp->dsm_name);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 		if ((reqhost = iffile_to_hostname(hostfile)) != NULL) {
910Sstevel@tonic-gate 			dhcpmsg(MSG_DEBUG, "dhcp_selecting: host %s", reqhost);
923431Scarlsonj 			if ((dsmp->dsm_reqhost = strdup(reqhost)) != NULL)
933431Scarlsonj 				(void) add_pkt_opt(dpkt, CD_HOSTNAME,
943431Scarlsonj 				    dsmp->dsm_reqhost,
953431Scarlsonj 				    strlen(dsmp->dsm_reqhost));
960Sstevel@tonic-gate 			else
970Sstevel@tonic-gate 				dhcpmsg(MSG_WARNING, "dhcp_selecting: cannot"
980Sstevel@tonic-gate 				    " allocate memory for host name option");
993431Scarlsonj 		} else {
1003431Scarlsonj 			dhcpmsg(MSG_DEBUG,
1013431Scarlsonj 			    "dhcp_selecting: no hostname for %s",
1023431Scarlsonj 			    dsmp->dsm_name);
1030Sstevel@tonic-gate 		}
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1063431Scarlsonj 	(void) add_pkt_opt(dpkt, CD_END, NULL, 0);
1073431Scarlsonj 
1083431Scarlsonj 	(void) send_pkt(dsmp, dpkt, htonl(INADDR_BROADCAST), stop_init_reboot);
1093431Scarlsonj }
1103431Scarlsonj 
1113431Scarlsonj 
1123431Scarlsonj /*
1133431Scarlsonj  * dhcp_init_reboot_v6(): attempts to reuse a cached configuration for a state
1143431Scarlsonj  *			  machine.  Create a Confirm message and multicast it
1153431Scarlsonj  *			  out.
1163431Scarlsonj  *
1173431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
1183431Scarlsonj  *  output: void
1193431Scarlsonj  */
1203431Scarlsonj 
1213431Scarlsonj static void
dhcp_init_reboot_v6(dhcp_smach_t * dsmp)1223431Scarlsonj dhcp_init_reboot_v6(dhcp_smach_t *dsmp)
1233431Scarlsonj {
1243431Scarlsonj 	dhcp_pkt_t *dpkt;
1253431Scarlsonj 	dhcpv6_option_t *d6o, *d6so, *popt;
1263431Scarlsonj 	uint_t olen, solen;
1273431Scarlsonj 	dhcpv6_ia_na_t d6in;
1283431Scarlsonj 	dhcpv6_iaaddr_t d6ia;
1293431Scarlsonj 	char *obase;
1303431Scarlsonj 
1313431Scarlsonj 	/*
1323431Scarlsonj 	 * Assemble a Confirm message based on the current ack.
1333431Scarlsonj 	 */
1343431Scarlsonj 
1353431Scarlsonj 	dpkt = init_pkt(dsmp, DHCPV6_MSG_CONFIRM);
1363431Scarlsonj 
1373431Scarlsonj 	/*
1383431Scarlsonj 	 * Loop over and copy IA_NAs and IAADDRs we have in our last ack.  This
1393431Scarlsonj 	 * is what we'll be requesting.
1403431Scarlsonj 	 */
1413431Scarlsonj 	d6o = NULL;
1423431Scarlsonj 	while ((d6o = dhcpv6_pkt_option(dsmp->dsm_ack, d6o, DHCPV6_OPT_IA_NA,
1433431Scarlsonj 	    &olen)) != NULL) {
1443431Scarlsonj 
1453431Scarlsonj 		/*
1463431Scarlsonj 		 * Copy in IA_NA option from the ack.  Note that we use zero
1473431Scarlsonj 		 * for all timers in accordance with RFC 3315.  (It would make
1483431Scarlsonj 		 * some sense to say what we think the current timers are as
1493431Scarlsonj 		 * a hint to the server, but the RFC doesn't agree.)
1503431Scarlsonj 		 */
1513431Scarlsonj 		if (olen < sizeof (dhcpv6_ia_na_t))
1523431Scarlsonj 			continue;
1533431Scarlsonj 		(void) memcpy(&d6in, d6o, sizeof (d6in));
1543431Scarlsonj 		d6in.d6in_t1 = 0;
1553431Scarlsonj 		d6in.d6in_t2 = 0;
1563431Scarlsonj 		popt = add_pkt_opt(dpkt, DHCPV6_OPT_IA_NA,
1573431Scarlsonj 		    (char *)&d6in + sizeof (*d6o),
1583431Scarlsonj 		    sizeof (d6in) - sizeof (*d6o));
1593431Scarlsonj 		if (popt == NULL)
1603431Scarlsonj 			goto failure;
1610Sstevel@tonic-gate 
1623431Scarlsonj 		/*
1633431Scarlsonj 		 * Now loop over the IAADDR suboptions and add those.
1643431Scarlsonj 		 */
1653431Scarlsonj 		obase = (char *)d6o + sizeof (dhcpv6_ia_na_t);
1663431Scarlsonj 		olen -= sizeof (dhcpv6_ia_na_t);
1673431Scarlsonj 		d6so = NULL;
1683431Scarlsonj 		while ((d6so = dhcpv6_find_option(obase, olen, d6so,
1693431Scarlsonj 		    DHCPV6_OPT_IAADDR, &solen)) != NULL) {
1703431Scarlsonj 			if (solen < sizeof (dhcpv6_iaaddr_t))
1713431Scarlsonj 				continue;
1723431Scarlsonj 			(void) memcpy(&d6ia, d6so, sizeof (d6ia));
1733431Scarlsonj 			d6ia.d6ia_preflife = 0;
1743431Scarlsonj 			d6ia.d6ia_vallife = 0;
1753431Scarlsonj 			if (add_pkt_subopt(dpkt, popt, DHCPV6_OPT_IAADDR,
1763431Scarlsonj 			    (char *)&d6ia + sizeof (*d6so),
1773431Scarlsonj 			    sizeof (d6ia) - sizeof (*d6so)) == NULL)
1783431Scarlsonj 				goto failure;
1793431Scarlsonj 		}
1803431Scarlsonj 	}
1813431Scarlsonj 
1823431Scarlsonj 	/* Add required Option Request option */
1833431Scarlsonj 	(void) add_pkt_prl(dpkt, dsmp);
1843431Scarlsonj 
1853431Scarlsonj 	(void) send_pkt_v6(dsmp, dpkt, ipv6_all_dhcp_relay_and_servers,
1863431Scarlsonj 	    stop_init_reboot, DHCPV6_CNF_TIMEOUT, DHCPV6_CNF_MAX_RT);
1873431Scarlsonj 
1883431Scarlsonj 	return;
1893431Scarlsonj 
1903431Scarlsonj failure:
1914106Scarlsonj 	if (!set_start_timer(dsmp))
1923431Scarlsonj 		dhcp_selecting(dsmp);
1933431Scarlsonj }
1943431Scarlsonj 
1953431Scarlsonj /*
1963431Scarlsonj  * dhcp_init_reboot(): attempts to reuse a cached configuration for a state
1973431Scarlsonj  *		       machine.
1983431Scarlsonj  *
1993431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
2003431Scarlsonj  *  output: void
2013431Scarlsonj  */
2023431Scarlsonj 
2033431Scarlsonj void
dhcp_init_reboot(dhcp_smach_t * dsmp)2043431Scarlsonj dhcp_init_reboot(dhcp_smach_t *dsmp)
2053431Scarlsonj {
2063431Scarlsonj 	dhcpmsg(MSG_VERBOSE,  "%s has cached configuration - entering "
2073431Scarlsonj 	    "INIT_REBOOT", dsmp->dsm_name);
2083431Scarlsonj 
2093431Scarlsonj 	if (!set_smach_state(dsmp, INIT_REBOOT)) {
2103431Scarlsonj 		dhcpmsg(MSG_ERROR, "dhcp_init_reboot: cannot register to "
2113431Scarlsonj 		    "collect ACK/NAK packets, reverting to INIT on %s",
2123431Scarlsonj 		    dsmp->dsm_name);
2133431Scarlsonj 
2143431Scarlsonj 		dsmp->dsm_dflags |= DHCP_IF_FAILED;
2153431Scarlsonj 		(void) set_smach_state(dsmp, INIT);
2163431Scarlsonj 		ipc_action_finish(dsmp, DHCP_IPC_E_MEMORY);
2173431Scarlsonj 		return;
2183431Scarlsonj 	}
2193431Scarlsonj 
2203431Scarlsonj 	if (dsmp->dsm_isv6)
2213431Scarlsonj 		dhcp_init_reboot_v6(dsmp);
2223431Scarlsonj 	else
2233431Scarlsonj 		dhcp_init_reboot_v4(dsmp);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * stop_init_reboot(): decides when to stop retransmitting REQUESTs
2280Sstevel@tonic-gate  *
2293431Scarlsonj  *   input: dhcp_smach_t *: the state machine sending the REQUESTs
2300Sstevel@tonic-gate  *	    unsigned int: the number of REQUESTs sent so far
2310Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if retransmissions should stop
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate static boolean_t
stop_init_reboot(dhcp_smach_t * dsmp,unsigned int n_requests)2353431Scarlsonj stop_init_reboot(dhcp_smach_t *dsmp, unsigned int n_requests)
2360Sstevel@tonic-gate {
2373431Scarlsonj 	if (dsmp->dsm_isv6) {
2383431Scarlsonj 		uint_t nowabs, maxabs;
2390Sstevel@tonic-gate 
2403431Scarlsonj 		nowabs = gethrtime() / (NANOSEC / MILLISEC);
2413431Scarlsonj 		maxabs = dsmp->dsm_neg_hrtime / (NANOSEC / MILLISEC) +
2423431Scarlsonj 		    DHCPV6_CNF_MAX_RD;
2433431Scarlsonj 		if (nowabs < maxabs) {
2443431Scarlsonj 			/* Cap the timer based on the maximum */
2453431Scarlsonj 			if (nowabs + dsmp->dsm_send_timeout > maxabs)
2463431Scarlsonj 				dsmp->dsm_send_timeout = maxabs - nowabs;
2473431Scarlsonj 			return (B_FALSE);
2483431Scarlsonj 		}
249*9633Sjames.d.carlson@sun.com 	} else {
250*9633Sjames.d.carlson@sun.com 		if (n_requests < DHCP_MAX_REQUESTS)
251*9633Sjames.d.carlson@sun.com 			return (B_FALSE);
252*9633Sjames.d.carlson@sun.com 	}
253*9633Sjames.d.carlson@sun.com 
254*9633Sjames.d.carlson@sun.com 	if (df_get_bool(dsmp->dsm_name, dsmp->dsm_isv6,
255*9633Sjames.d.carlson@sun.com 	    DF_VERIFIED_LEASE_ONLY)) {
256*9633Sjames.d.carlson@sun.com 		dhcpmsg(MSG_INFO,
257*9633Sjames.d.carlson@sun.com 		    "unable to verify existing lease on %s; restarting",
258*9633Sjames.d.carlson@sun.com 		    dsmp->dsm_name);
259*9633Sjames.d.carlson@sun.com 		dhcp_selecting(dsmp);
260*9633Sjames.d.carlson@sun.com 		return (B_TRUE);
261*9633Sjames.d.carlson@sun.com 	}
262*9633Sjames.d.carlson@sun.com 
263*9633Sjames.d.carlson@sun.com 	if (dsmp->dsm_isv6) {
2643431Scarlsonj 		dhcpmsg(MSG_INFO, "no Reply to Confirm, using remainder of "
2653431Scarlsonj 		    "existing lease on %s", dsmp->dsm_name);
2663431Scarlsonj 	} else {
2673431Scarlsonj 		dhcpmsg(MSG_INFO, "no ACK/NAK to INIT_REBOOT REQUEST, "
2683431Scarlsonj 		    "using remainder of existing lease on %s", dsmp->dsm_name);
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 
2713431Scarlsonj 	/*
2723431Scarlsonj 	 * We already stuck our old ack in dsmp->dsm_ack and relativized the
2733431Scarlsonj 	 * packet times, so we can just pretend that the server sent it to us
2743431Scarlsonj 	 * and move to bound.  If that fails, fall back to selecting.
2753431Scarlsonj 	 */
2763431Scarlsonj 
2773431Scarlsonj 	if (dhcp_bound(dsmp, NULL)) {
2783431Scarlsonj 		if (dsmp->dsm_isv6) {
2793431Scarlsonj 			if (!save_server_id(dsmp, dsmp->dsm_ack))
2803431Scarlsonj 				goto failure;
2813431Scarlsonj 			server_unicast_option(dsmp, dsmp->dsm_ack);
2823431Scarlsonj 		}
2833431Scarlsonj 	} else {
2843431Scarlsonj failure:
2853431Scarlsonj 		dhcpmsg(MSG_INFO, "unable to use saved lease on %s; restarting",
2863431Scarlsonj 		    dsmp->dsm_name);
2873431Scarlsonj 		dhcp_selecting(dsmp);
2883431Scarlsonj 	}
2893431Scarlsonj 
2903431Scarlsonj 	return (B_TRUE);
2910Sstevel@tonic-gate }
292