xref: /onnv-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/init_reboot.c (revision 3448:aaf16568054b)
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>
323431Scarlsonj #include <stdlib.h>
330Sstevel@tonic-gate #include <limits.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <netinet/in.h>
360Sstevel@tonic-gate #include <netinet/dhcp.h>
370Sstevel@tonic-gate #include <netinet/udp.h>
380Sstevel@tonic-gate #include <netinet/ip_var.h>
390Sstevel@tonic-gate #include <netinet/udp_var.h>
400Sstevel@tonic-gate #include <dhcpmsg.h>
410Sstevel@tonic-gate #include <string.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include "agent.h"
440Sstevel@tonic-gate #include "packet.h"
450Sstevel@tonic-gate #include "states.h"
460Sstevel@tonic-gate #include "util.h"
470Sstevel@tonic-gate #include "interface.h"
480Sstevel@tonic-gate #include "defaults.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static stop_func_t	stop_init_reboot;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
533431Scarlsonj  * dhcp_init_reboot_v4(): attempts to reuse a cached configuration for a state
543431Scarlsonj  *			  machine.
550Sstevel@tonic-gate  *
563431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
570Sstevel@tonic-gate  *  output: void
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate 
603431Scarlsonj static void
613431Scarlsonj dhcp_init_reboot_v4(dhcp_smach_t *dsmp)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	dhcp_pkt_t		*dpkt;
640Sstevel@tonic-gate 	const char		*reqhost;
650Sstevel@tonic-gate 	char			hostfile[PATH_MAX + 1];
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	/*
680Sstevel@tonic-gate 	 * assemble DHCPREQUEST message.  The max dhcp message size
690Sstevel@tonic-gate 	 * option is set to the interface max, minus the size of the udp and
700Sstevel@tonic-gate 	 * ip headers.
710Sstevel@tonic-gate 	 */
720Sstevel@tonic-gate 
733431Scarlsonj 	dpkt = init_pkt(dsmp, REQUEST);
743431Scarlsonj 	(void) add_pkt_opt32(dpkt, CD_REQUESTED_IP_ADDR,
753431Scarlsonj 	    dsmp->dsm_ack->pkt->yiaddr.s_addr);
760Sstevel@tonic-gate 
773431Scarlsonj 	(void) add_pkt_opt32(dpkt, CD_LEASE_TIME, htonl(DHCP_PERM));
783431Scarlsonj 	(void) add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE,
793431Scarlsonj 	    htons(dsmp->dsm_lif->lif_pif->pif_max - sizeof (struct udpiphdr)));
800Sstevel@tonic-gate 
81*3448Sdh155122 	if (class_id_len != 0)
82*3448Sdh155122 		(void) add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len);
833431Scarlsonj 	(void) add_pkt_prl(dpkt, dsmp);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	/*
860Sstevel@tonic-gate 	 * Set CD_HOSTNAME option if REQUEST_HOSTNAME is set and a hostname
870Sstevel@tonic-gate 	 * is found in /etc/hostname.<ifname>
880Sstevel@tonic-gate 	 */
893431Scarlsonj 	if (df_get_bool(dsmp->dsm_name, dsmp->dsm_isv6, DF_REQUEST_HOSTNAME)) {
900Sstevel@tonic-gate 		(void) snprintf(hostfile, sizeof (hostfile), "/etc/hostname.%s",
913431Scarlsonj 		    dsmp->dsm_name);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 		if ((reqhost = iffile_to_hostname(hostfile)) != NULL) {
940Sstevel@tonic-gate 			dhcpmsg(MSG_DEBUG, "dhcp_selecting: host %s", reqhost);
953431Scarlsonj 			if ((dsmp->dsm_reqhost = strdup(reqhost)) != NULL)
963431Scarlsonj 				(void) add_pkt_opt(dpkt, CD_HOSTNAME,
973431Scarlsonj 				    dsmp->dsm_reqhost,
983431Scarlsonj 				    strlen(dsmp->dsm_reqhost));
990Sstevel@tonic-gate 			else
1000Sstevel@tonic-gate 				dhcpmsg(MSG_WARNING, "dhcp_selecting: cannot"
1010Sstevel@tonic-gate 				    " allocate memory for host name option");
1023431Scarlsonj 		} else {
1033431Scarlsonj 			dhcpmsg(MSG_DEBUG,
1043431Scarlsonj 			    "dhcp_selecting: no hostname for %s",
1053431Scarlsonj 			    dsmp->dsm_name);
1060Sstevel@tonic-gate 		}
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1093431Scarlsonj 	(void) add_pkt_opt(dpkt, CD_END, NULL, 0);
1103431Scarlsonj 
1113431Scarlsonj 	(void) send_pkt(dsmp, dpkt, htonl(INADDR_BROADCAST), stop_init_reboot);
1123431Scarlsonj }
1133431Scarlsonj 
1143431Scarlsonj 
1153431Scarlsonj /*
1163431Scarlsonj  * dhcp_init_reboot_v6(): attempts to reuse a cached configuration for a state
1173431Scarlsonj  *			  machine.  Create a Confirm message and multicast it
1183431Scarlsonj  *			  out.
1193431Scarlsonj  *
1203431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
1213431Scarlsonj  *  output: void
1223431Scarlsonj  */
1233431Scarlsonj 
1243431Scarlsonj static void
1253431Scarlsonj dhcp_init_reboot_v6(dhcp_smach_t *dsmp)
1263431Scarlsonj {
1273431Scarlsonj 	dhcp_pkt_t *dpkt;
1283431Scarlsonj 	dhcpv6_option_t *d6o, *d6so, *popt;
1293431Scarlsonj 	uint_t olen, solen;
1303431Scarlsonj 	dhcpv6_ia_na_t d6in;
1313431Scarlsonj 	dhcpv6_iaaddr_t d6ia;
1323431Scarlsonj 	char *obase;
1333431Scarlsonj 
1343431Scarlsonj 	/*
1353431Scarlsonj 	 * Assemble a Confirm message based on the current ack.
1363431Scarlsonj 	 */
1373431Scarlsonj 
1383431Scarlsonj 	dpkt = init_pkt(dsmp, DHCPV6_MSG_CONFIRM);
1393431Scarlsonj 
1403431Scarlsonj 	/*
1413431Scarlsonj 	 * Loop over and copy IA_NAs and IAADDRs we have in our last ack.  This
1423431Scarlsonj 	 * is what we'll be requesting.
1433431Scarlsonj 	 */
1443431Scarlsonj 	d6o = NULL;
1453431Scarlsonj 	while ((d6o = dhcpv6_pkt_option(dsmp->dsm_ack, d6o, DHCPV6_OPT_IA_NA,
1463431Scarlsonj 	    &olen)) != NULL) {
1473431Scarlsonj 
1483431Scarlsonj 		/*
1493431Scarlsonj 		 * Copy in IA_NA option from the ack.  Note that we use zero
1503431Scarlsonj 		 * for all timers in accordance with RFC 3315.  (It would make
1513431Scarlsonj 		 * some sense to say what we think the current timers are as
1523431Scarlsonj 		 * a hint to the server, but the RFC doesn't agree.)
1533431Scarlsonj 		 */
1543431Scarlsonj 		if (olen < sizeof (dhcpv6_ia_na_t))
1553431Scarlsonj 			continue;
1563431Scarlsonj 		(void) memcpy(&d6in, d6o, sizeof (d6in));
1573431Scarlsonj 		d6in.d6in_t1 = 0;
1583431Scarlsonj 		d6in.d6in_t2 = 0;
1593431Scarlsonj 		popt = add_pkt_opt(dpkt, DHCPV6_OPT_IA_NA,
1603431Scarlsonj 		    (char *)&d6in + sizeof (*d6o),
1613431Scarlsonj 		    sizeof (d6in) - sizeof (*d6o));
1623431Scarlsonj 		if (popt == NULL)
1633431Scarlsonj 			goto failure;
1640Sstevel@tonic-gate 
1653431Scarlsonj 		/*
1663431Scarlsonj 		 * Now loop over the IAADDR suboptions and add those.
1673431Scarlsonj 		 */
1683431Scarlsonj 		obase = (char *)d6o + sizeof (dhcpv6_ia_na_t);
1693431Scarlsonj 		olen -= sizeof (dhcpv6_ia_na_t);
1703431Scarlsonj 		d6so = NULL;
1713431Scarlsonj 		while ((d6so = dhcpv6_find_option(obase, olen, d6so,
1723431Scarlsonj 		    DHCPV6_OPT_IAADDR, &solen)) != NULL) {
1733431Scarlsonj 			if (solen < sizeof (dhcpv6_iaaddr_t))
1743431Scarlsonj 				continue;
1753431Scarlsonj 			(void) memcpy(&d6ia, d6so, sizeof (d6ia));
1763431Scarlsonj 			d6ia.d6ia_preflife = 0;
1773431Scarlsonj 			d6ia.d6ia_vallife = 0;
1783431Scarlsonj 			if (add_pkt_subopt(dpkt, popt, DHCPV6_OPT_IAADDR,
1793431Scarlsonj 			    (char *)&d6ia + sizeof (*d6so),
1803431Scarlsonj 			    sizeof (d6ia) - sizeof (*d6so)) == NULL)
1813431Scarlsonj 				goto failure;
1823431Scarlsonj 		}
1833431Scarlsonj 	}
1843431Scarlsonj 
1853431Scarlsonj 	/* Add required Option Request option */
1863431Scarlsonj 	(void) add_pkt_prl(dpkt, dsmp);
1873431Scarlsonj 
1883431Scarlsonj 	(void) send_pkt_v6(dsmp, dpkt, ipv6_all_dhcp_relay_and_servers,
1893431Scarlsonj 	    stop_init_reboot, DHCPV6_CNF_TIMEOUT, DHCPV6_CNF_MAX_RT);
1903431Scarlsonj 
1913431Scarlsonj 	return;
1923431Scarlsonj 
1933431Scarlsonj failure:
1943431Scarlsonj 	if (iu_schedule_timer_ms(tq, lrand48() % DHCP_SELECT_WAIT, dhcp_start,
1953431Scarlsonj 	    dsmp) != -1)
1963431Scarlsonj 		hold_smach(dsmp);
1973431Scarlsonj 	else
1983431Scarlsonj 		dhcp_selecting(dsmp);
1993431Scarlsonj }
2003431Scarlsonj 
2013431Scarlsonj /*
2023431Scarlsonj  * dhcp_init_reboot(): attempts to reuse a cached configuration for a state
2033431Scarlsonj  *		       machine.
2043431Scarlsonj  *
2053431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
2063431Scarlsonj  *  output: void
2073431Scarlsonj  */
2083431Scarlsonj 
2093431Scarlsonj void
2103431Scarlsonj dhcp_init_reboot(dhcp_smach_t *dsmp)
2113431Scarlsonj {
2123431Scarlsonj 	dhcpmsg(MSG_VERBOSE,  "%s has cached configuration - entering "
2133431Scarlsonj 	    "INIT_REBOOT", dsmp->dsm_name);
2143431Scarlsonj 
2153431Scarlsonj 	if (!set_smach_state(dsmp, INIT_REBOOT)) {
2163431Scarlsonj 		dhcpmsg(MSG_ERROR, "dhcp_init_reboot: cannot register to "
2173431Scarlsonj 		    "collect ACK/NAK packets, reverting to INIT on %s",
2183431Scarlsonj 		    dsmp->dsm_name);
2193431Scarlsonj 
2203431Scarlsonj 		dsmp->dsm_dflags |= DHCP_IF_FAILED;
2213431Scarlsonj 		(void) set_smach_state(dsmp, INIT);
2223431Scarlsonj 		ipc_action_finish(dsmp, DHCP_IPC_E_MEMORY);
2233431Scarlsonj 		return;
2243431Scarlsonj 	}
2253431Scarlsonj 
2263431Scarlsonj 	if (dsmp->dsm_isv6)
2273431Scarlsonj 		dhcp_init_reboot_v6(dsmp);
2283431Scarlsonj 	else
2293431Scarlsonj 		dhcp_init_reboot_v4(dsmp);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate  * stop_init_reboot(): decides when to stop retransmitting REQUESTs
2340Sstevel@tonic-gate  *
2353431Scarlsonj  *   input: dhcp_smach_t *: the state machine sending the REQUESTs
2360Sstevel@tonic-gate  *	    unsigned int: the number of REQUESTs sent so far
2370Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if retransmissions should stop
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate static boolean_t
2413431Scarlsonj stop_init_reboot(dhcp_smach_t *dsmp, unsigned int n_requests)
2420Sstevel@tonic-gate {
2433431Scarlsonj 	if (dsmp->dsm_isv6) {
2443431Scarlsonj 		uint_t nowabs, maxabs;
2450Sstevel@tonic-gate 
2463431Scarlsonj 		nowabs = gethrtime() / (NANOSEC / MILLISEC);
2473431Scarlsonj 		maxabs = dsmp->dsm_neg_hrtime / (NANOSEC / MILLISEC) +
2483431Scarlsonj 		    DHCPV6_CNF_MAX_RD;
2493431Scarlsonj 		if (nowabs < maxabs) {
2503431Scarlsonj 			/* Cap the timer based on the maximum */
2513431Scarlsonj 			if (nowabs + dsmp->dsm_send_timeout > maxabs)
2523431Scarlsonj 				dsmp->dsm_send_timeout = maxabs - nowabs;
2533431Scarlsonj 			return (B_FALSE);
2543431Scarlsonj 		}
2553431Scarlsonj 		dhcpmsg(MSG_INFO, "no Reply to Confirm, using remainder of "
2563431Scarlsonj 		    "existing lease on %s", dsmp->dsm_name);
2573431Scarlsonj 	} else {
2583431Scarlsonj 		if (n_requests < DHCP_MAX_REQUESTS)
2593431Scarlsonj 			return (B_FALSE);
2603431Scarlsonj 		dhcpmsg(MSG_INFO, "no ACK/NAK to INIT_REBOOT REQUEST, "
2613431Scarlsonj 		    "using remainder of existing lease on %s", dsmp->dsm_name);
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2643431Scarlsonj 	/*
2653431Scarlsonj 	 * We already stuck our old ack in dsmp->dsm_ack and relativized the
2663431Scarlsonj 	 * packet times, so we can just pretend that the server sent it to us
2673431Scarlsonj 	 * and move to bound.  If that fails, fall back to selecting.
2683431Scarlsonj 	 */
2693431Scarlsonj 
2703431Scarlsonj 	if (dhcp_bound(dsmp, NULL)) {
2713431Scarlsonj 		if (dsmp->dsm_isv6) {
2723431Scarlsonj 			if (!save_server_id(dsmp, dsmp->dsm_ack))
2733431Scarlsonj 				goto failure;
2743431Scarlsonj 			server_unicast_option(dsmp, dsmp->dsm_ack);
2753431Scarlsonj 		}
2763431Scarlsonj 	} else {
2773431Scarlsonj failure:
2783431Scarlsonj 		dhcpmsg(MSG_INFO, "unable to use saved lease on %s; restarting",
2793431Scarlsonj 		    dsmp->dsm_name);
2803431Scarlsonj 		dhcp_selecting(dsmp);
2813431Scarlsonj 	}
2823431Scarlsonj 
2833431Scarlsonj 	return (B_TRUE);
2840Sstevel@tonic-gate }
285