xref: /titanic_51/usr/src/cmd/cmd-inet/sbin/dhcpagent/init_reboot.c (revision 19449258028e6813f0b7a606b554b2fa37a390ec)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d04ccbb3Scarlsonj  * Common Development and Distribution License (the "License").
6d04ccbb3Scarlsonj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22a1196271SJames Carlson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * INIT_REBOOT state of the DHCP client state machine.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <limits.h>
317c478bd9Sstevel@tonic-gate #include <sys/socket.h>
327c478bd9Sstevel@tonic-gate #include <netinet/in.h>
337c478bd9Sstevel@tonic-gate #include <netinet/dhcp.h>
347c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
357c478bd9Sstevel@tonic-gate #include <netinet/ip_var.h>
367c478bd9Sstevel@tonic-gate #include <netinet/udp_var.h>
377c478bd9Sstevel@tonic-gate #include <dhcpmsg.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "agent.h"
417c478bd9Sstevel@tonic-gate #include "packet.h"
427c478bd9Sstevel@tonic-gate #include "states.h"
437c478bd9Sstevel@tonic-gate #include "util.h"
447c478bd9Sstevel@tonic-gate #include "interface.h"
457c478bd9Sstevel@tonic-gate #include "defaults.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static stop_func_t	stop_init_reboot;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
50d04ccbb3Scarlsonj  * dhcp_init_reboot_v4(): attempts to reuse a cached configuration for a state
51d04ccbb3Scarlsonj  *			  machine.
527c478bd9Sstevel@tonic-gate  *
53d04ccbb3Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
547c478bd9Sstevel@tonic-gate  *  output: void
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
57d04ccbb3Scarlsonj static void
58d04ccbb3Scarlsonj dhcp_init_reboot_v4(dhcp_smach_t *dsmp)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	dhcp_pkt_t		*dpkt;
617c478bd9Sstevel@tonic-gate 	const char		*reqhost;
627c478bd9Sstevel@tonic-gate 	char			hostfile[PATH_MAX + 1];
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * assemble DHCPREQUEST message.  The max dhcp message size
667c478bd9Sstevel@tonic-gate 	 * option is set to the interface max, minus the size of the udp and
677c478bd9Sstevel@tonic-gate 	 * ip headers.
687c478bd9Sstevel@tonic-gate 	 */
697c478bd9Sstevel@tonic-gate 
70d04ccbb3Scarlsonj 	dpkt = init_pkt(dsmp, REQUEST);
71d04ccbb3Scarlsonj 	(void) add_pkt_opt32(dpkt, CD_REQUESTED_IP_ADDR,
72d04ccbb3Scarlsonj 	    dsmp->dsm_ack->pkt->yiaddr.s_addr);
737c478bd9Sstevel@tonic-gate 
74d04ccbb3Scarlsonj 	(void) add_pkt_opt32(dpkt, CD_LEASE_TIME, htonl(DHCP_PERM));
75d04ccbb3Scarlsonj 	(void) add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE,
76d04ccbb3Scarlsonj 	    htons(dsmp->dsm_lif->lif_pif->pif_max - sizeof (struct udpiphdr)));
777c478bd9Sstevel@tonic-gate 
78f4b3ec61Sdh155122 	if (class_id_len != 0)
79d04ccbb3Scarlsonj 		(void) add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len);
80d04ccbb3Scarlsonj 	(void) add_pkt_prl(dpkt, dsmp);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/*
837c478bd9Sstevel@tonic-gate 	 * Set CD_HOSTNAME option if REQUEST_HOSTNAME is set and a hostname
847c478bd9Sstevel@tonic-gate 	 * is found in /etc/hostname.<ifname>
857c478bd9Sstevel@tonic-gate 	 */
86d04ccbb3Scarlsonj 	if (df_get_bool(dsmp->dsm_name, dsmp->dsm_isv6, DF_REQUEST_HOSTNAME)) {
877c478bd9Sstevel@tonic-gate 		(void) snprintf(hostfile, sizeof (hostfile), "/etc/hostname.%s",
88d04ccbb3Scarlsonj 		    dsmp->dsm_name);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		if ((reqhost = iffile_to_hostname(hostfile)) != NULL) {
917c478bd9Sstevel@tonic-gate 			dhcpmsg(MSG_DEBUG, "dhcp_selecting: host %s", reqhost);
92d04ccbb3Scarlsonj 			if ((dsmp->dsm_reqhost = strdup(reqhost)) != NULL)
93d04ccbb3Scarlsonj 				(void) add_pkt_opt(dpkt, CD_HOSTNAME,
94d04ccbb3Scarlsonj 				    dsmp->dsm_reqhost,
95d04ccbb3Scarlsonj 				    strlen(dsmp->dsm_reqhost));
967c478bd9Sstevel@tonic-gate 			else
977c478bd9Sstevel@tonic-gate 				dhcpmsg(MSG_WARNING, "dhcp_selecting: cannot"
987c478bd9Sstevel@tonic-gate 				    " allocate memory for host name option");
99d04ccbb3Scarlsonj 		} else {
100d04ccbb3Scarlsonj 			dhcpmsg(MSG_DEBUG,
101d04ccbb3Scarlsonj 			    "dhcp_selecting: no hostname for %s",
102d04ccbb3Scarlsonj 			    dsmp->dsm_name);
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
106d04ccbb3Scarlsonj 	(void) add_pkt_opt(dpkt, CD_END, NULL, 0);
1077c478bd9Sstevel@tonic-gate 
108d04ccbb3Scarlsonj 	(void) send_pkt(dsmp, dpkt, htonl(INADDR_BROADCAST), stop_init_reboot);
109d04ccbb3Scarlsonj }
110d04ccbb3Scarlsonj 
111d04ccbb3Scarlsonj 
112d04ccbb3Scarlsonj /*
113d04ccbb3Scarlsonj  * dhcp_init_reboot_v6(): attempts to reuse a cached configuration for a state
114d04ccbb3Scarlsonj  *			  machine.  Create a Confirm message and multicast it
115d04ccbb3Scarlsonj  *			  out.
116d04ccbb3Scarlsonj  *
117d04ccbb3Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
118d04ccbb3Scarlsonj  *  output: void
119d04ccbb3Scarlsonj  */
120d04ccbb3Scarlsonj 
121d04ccbb3Scarlsonj static void
122d04ccbb3Scarlsonj dhcp_init_reboot_v6(dhcp_smach_t *dsmp)
123d04ccbb3Scarlsonj {
124d04ccbb3Scarlsonj 	dhcp_pkt_t *dpkt;
125d04ccbb3Scarlsonj 	dhcpv6_option_t *d6o, *d6so, *popt;
126d04ccbb3Scarlsonj 	uint_t olen, solen;
127d04ccbb3Scarlsonj 	dhcpv6_ia_na_t d6in;
128d04ccbb3Scarlsonj 	dhcpv6_iaaddr_t d6ia;
129d04ccbb3Scarlsonj 	char *obase;
130d04ccbb3Scarlsonj 
131d04ccbb3Scarlsonj 	/*
132d04ccbb3Scarlsonj 	 * Assemble a Confirm message based on the current ack.
133d04ccbb3Scarlsonj 	 */
134d04ccbb3Scarlsonj 
135d04ccbb3Scarlsonj 	dpkt = init_pkt(dsmp, DHCPV6_MSG_CONFIRM);
136d04ccbb3Scarlsonj 
137d04ccbb3Scarlsonj 	/*
138d04ccbb3Scarlsonj 	 * Loop over and copy IA_NAs and IAADDRs we have in our last ack.  This
139d04ccbb3Scarlsonj 	 * is what we'll be requesting.
140d04ccbb3Scarlsonj 	 */
141d04ccbb3Scarlsonj 	d6o = NULL;
142d04ccbb3Scarlsonj 	while ((d6o = dhcpv6_pkt_option(dsmp->dsm_ack, d6o, DHCPV6_OPT_IA_NA,
143d04ccbb3Scarlsonj 	    &olen)) != NULL) {
144d04ccbb3Scarlsonj 
145d04ccbb3Scarlsonj 		/*
146d04ccbb3Scarlsonj 		 * Copy in IA_NA option from the ack.  Note that we use zero
147d04ccbb3Scarlsonj 		 * for all timers in accordance with RFC 3315.  (It would make
148d04ccbb3Scarlsonj 		 * some sense to say what we think the current timers are as
149d04ccbb3Scarlsonj 		 * a hint to the server, but the RFC doesn't agree.)
150d04ccbb3Scarlsonj 		 */
151d04ccbb3Scarlsonj 		if (olen < sizeof (dhcpv6_ia_na_t))
152d04ccbb3Scarlsonj 			continue;
153d04ccbb3Scarlsonj 		(void) memcpy(&d6in, d6o, sizeof (d6in));
154d04ccbb3Scarlsonj 		d6in.d6in_t1 = 0;
155d04ccbb3Scarlsonj 		d6in.d6in_t2 = 0;
156d04ccbb3Scarlsonj 		popt = add_pkt_opt(dpkt, DHCPV6_OPT_IA_NA,
157d04ccbb3Scarlsonj 		    (char *)&d6in + sizeof (*d6o),
158d04ccbb3Scarlsonj 		    sizeof (d6in) - sizeof (*d6o));
159d04ccbb3Scarlsonj 		if (popt == NULL)
160d04ccbb3Scarlsonj 			goto failure;
161d04ccbb3Scarlsonj 
162d04ccbb3Scarlsonj 		/*
163d04ccbb3Scarlsonj 		 * Now loop over the IAADDR suboptions and add those.
164d04ccbb3Scarlsonj 		 */
165d04ccbb3Scarlsonj 		obase = (char *)d6o + sizeof (dhcpv6_ia_na_t);
166d04ccbb3Scarlsonj 		olen -= sizeof (dhcpv6_ia_na_t);
167d04ccbb3Scarlsonj 		d6so = NULL;
168d04ccbb3Scarlsonj 		while ((d6so = dhcpv6_find_option(obase, olen, d6so,
169d04ccbb3Scarlsonj 		    DHCPV6_OPT_IAADDR, &solen)) != NULL) {
170d04ccbb3Scarlsonj 			if (solen < sizeof (dhcpv6_iaaddr_t))
171d04ccbb3Scarlsonj 				continue;
172d04ccbb3Scarlsonj 			(void) memcpy(&d6ia, d6so, sizeof (d6ia));
173d04ccbb3Scarlsonj 			d6ia.d6ia_preflife = 0;
174d04ccbb3Scarlsonj 			d6ia.d6ia_vallife = 0;
175d04ccbb3Scarlsonj 			if (add_pkt_subopt(dpkt, popt, DHCPV6_OPT_IAADDR,
176d04ccbb3Scarlsonj 			    (char *)&d6ia + sizeof (*d6so),
177d04ccbb3Scarlsonj 			    sizeof (d6ia) - sizeof (*d6so)) == NULL)
178d04ccbb3Scarlsonj 				goto failure;
179d04ccbb3Scarlsonj 		}
180d04ccbb3Scarlsonj 	}
181d04ccbb3Scarlsonj 
182d04ccbb3Scarlsonj 	/* Add required Option Request option */
183d04ccbb3Scarlsonj 	(void) add_pkt_prl(dpkt, dsmp);
184d04ccbb3Scarlsonj 
185d04ccbb3Scarlsonj 	(void) send_pkt_v6(dsmp, dpkt, ipv6_all_dhcp_relay_and_servers,
186d04ccbb3Scarlsonj 	    stop_init_reboot, DHCPV6_CNF_TIMEOUT, DHCPV6_CNF_MAX_RT);
187d04ccbb3Scarlsonj 
188d04ccbb3Scarlsonj 	return;
189d04ccbb3Scarlsonj 
190d04ccbb3Scarlsonj failure:
191cfb9c9abScarlsonj 	if (!set_start_timer(dsmp))
192d04ccbb3Scarlsonj 		dhcp_selecting(dsmp);
193d04ccbb3Scarlsonj }
194d04ccbb3Scarlsonj 
195d04ccbb3Scarlsonj /*
196d04ccbb3Scarlsonj  * dhcp_init_reboot(): attempts to reuse a cached configuration for a state
197d04ccbb3Scarlsonj  *		       machine.
198d04ccbb3Scarlsonj  *
199d04ccbb3Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
200d04ccbb3Scarlsonj  *  output: void
201d04ccbb3Scarlsonj  */
202d04ccbb3Scarlsonj 
203d04ccbb3Scarlsonj void
204d04ccbb3Scarlsonj dhcp_init_reboot(dhcp_smach_t *dsmp)
205d04ccbb3Scarlsonj {
206d04ccbb3Scarlsonj 	dhcpmsg(MSG_VERBOSE,  "%s has cached configuration - entering "
207d04ccbb3Scarlsonj 	    "INIT_REBOOT", dsmp->dsm_name);
208d04ccbb3Scarlsonj 
209d04ccbb3Scarlsonj 	if (!set_smach_state(dsmp, INIT_REBOOT)) {
210d04ccbb3Scarlsonj 		dhcpmsg(MSG_ERROR, "dhcp_init_reboot: cannot register to "
211d04ccbb3Scarlsonj 		    "collect ACK/NAK packets, reverting to INIT on %s",
212d04ccbb3Scarlsonj 		    dsmp->dsm_name);
213d04ccbb3Scarlsonj 
214d04ccbb3Scarlsonj 		dsmp->dsm_dflags |= DHCP_IF_FAILED;
215d04ccbb3Scarlsonj 		(void) set_smach_state(dsmp, INIT);
216d04ccbb3Scarlsonj 		ipc_action_finish(dsmp, DHCP_IPC_E_MEMORY);
217d04ccbb3Scarlsonj 		return;
218d04ccbb3Scarlsonj 	}
219d04ccbb3Scarlsonj 
220d04ccbb3Scarlsonj 	if (dsmp->dsm_isv6)
221d04ccbb3Scarlsonj 		dhcp_init_reboot_v6(dsmp);
222d04ccbb3Scarlsonj 	else
223d04ccbb3Scarlsonj 		dhcp_init_reboot_v4(dsmp);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * stop_init_reboot(): decides when to stop retransmitting REQUESTs
2287c478bd9Sstevel@tonic-gate  *
229d04ccbb3Scarlsonj  *   input: dhcp_smach_t *: the state machine sending the REQUESTs
2307c478bd9Sstevel@tonic-gate  *	    unsigned int: the number of REQUESTs sent so far
2317c478bd9Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if retransmissions should stop
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static boolean_t
235d04ccbb3Scarlsonj stop_init_reboot(dhcp_smach_t *dsmp, unsigned int n_requests)
2367c478bd9Sstevel@tonic-gate {
237d04ccbb3Scarlsonj 	if (dsmp->dsm_isv6) {
238d04ccbb3Scarlsonj 		uint_t nowabs, maxabs;
2397c478bd9Sstevel@tonic-gate 
240*19449258SJosef 'Jeff' Sipek 		nowabs = NSEC2MSEC(gethrtime());
241*19449258SJosef 'Jeff' Sipek 		maxabs = NSEC2MSEC(dsmp->dsm_neg_hrtime) + DHCPV6_CNF_MAX_RD;
242d04ccbb3Scarlsonj 		if (nowabs < maxabs) {
243d04ccbb3Scarlsonj 			/* Cap the timer based on the maximum */
244d04ccbb3Scarlsonj 			if (nowabs + dsmp->dsm_send_timeout > maxabs)
245d04ccbb3Scarlsonj 				dsmp->dsm_send_timeout = maxabs - nowabs;
246d04ccbb3Scarlsonj 			return (B_FALSE);
247d04ccbb3Scarlsonj 		}
248d04ccbb3Scarlsonj 	} else {
249d04ccbb3Scarlsonj 		if (n_requests < DHCP_MAX_REQUESTS)
250d04ccbb3Scarlsonj 			return (B_FALSE);
251a1196271SJames Carlson 	}
252a1196271SJames Carlson 
253a1196271SJames Carlson 	if (df_get_bool(dsmp->dsm_name, dsmp->dsm_isv6,
254a1196271SJames Carlson 	    DF_VERIFIED_LEASE_ONLY)) {
255a1196271SJames Carlson 		dhcpmsg(MSG_INFO,
256a1196271SJames Carlson 		    "unable to verify existing lease on %s; restarting",
257a1196271SJames Carlson 		    dsmp->dsm_name);
258a1196271SJames Carlson 		dhcp_selecting(dsmp);
259a1196271SJames Carlson 		return (B_TRUE);
260a1196271SJames Carlson 	}
261a1196271SJames Carlson 
262a1196271SJames Carlson 	if (dsmp->dsm_isv6) {
263a1196271SJames Carlson 		dhcpmsg(MSG_INFO, "no Reply to Confirm, using remainder of "
264a1196271SJames Carlson 		    "existing lease on %s", dsmp->dsm_name);
265a1196271SJames Carlson 	} else {
2667c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_INFO, "no ACK/NAK to INIT_REBOOT REQUEST, "
267d04ccbb3Scarlsonj 		    "using remainder of existing lease on %s", dsmp->dsm_name);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
270d04ccbb3Scarlsonj 	/*
271d04ccbb3Scarlsonj 	 * We already stuck our old ack in dsmp->dsm_ack and relativized the
272d04ccbb3Scarlsonj 	 * packet times, so we can just pretend that the server sent it to us
273d04ccbb3Scarlsonj 	 * and move to bound.  If that fails, fall back to selecting.
274d04ccbb3Scarlsonj 	 */
275d04ccbb3Scarlsonj 
276d04ccbb3Scarlsonj 	if (dhcp_bound(dsmp, NULL)) {
277d04ccbb3Scarlsonj 		if (dsmp->dsm_isv6) {
278d04ccbb3Scarlsonj 			if (!save_server_id(dsmp, dsmp->dsm_ack))
279d04ccbb3Scarlsonj 				goto failure;
280d04ccbb3Scarlsonj 			server_unicast_option(dsmp, dsmp->dsm_ack);
281d04ccbb3Scarlsonj 		}
282d04ccbb3Scarlsonj 	} else {
283d04ccbb3Scarlsonj failure:
284d04ccbb3Scarlsonj 		dhcpmsg(MSG_INFO, "unable to use saved lease on %s; restarting",
285d04ccbb3Scarlsonj 		    dsmp->dsm_name);
286d04ccbb3Scarlsonj 		dhcp_selecting(dsmp);
287d04ccbb3Scarlsonj 	}
288d04ccbb3Scarlsonj 
289d04ccbb3Scarlsonj 	return (B_TRUE);
2907c478bd9Sstevel@tonic-gate }
291