xref: /onnv-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/init_reboot.c (revision 3431:9f2d277dcffa)
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
5*3431Scarlsonj  * Common Development and Distribution License (the "License").
6*3431Scarlsonj  * 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*3431Scarlsonj  * 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>
32*3431Scarlsonj #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 /*
53*3431Scarlsonj  * dhcp_init_reboot_v4(): attempts to reuse a cached configuration for a state
54*3431Scarlsonj  *			  machine.
550Sstevel@tonic-gate  *
56*3431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
570Sstevel@tonic-gate  *  output: void
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate 
60*3431Scarlsonj static void
61*3431Scarlsonj 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 
73*3431Scarlsonj 	dpkt = init_pkt(dsmp, REQUEST);
74*3431Scarlsonj 	(void) add_pkt_opt32(dpkt, CD_REQUESTED_IP_ADDR,
75*3431Scarlsonj 	    dsmp->dsm_ack->pkt->yiaddr.s_addr);
760Sstevel@tonic-gate 
77*3431Scarlsonj 	(void) add_pkt_opt32(dpkt, CD_LEASE_TIME, htonl(DHCP_PERM));
78*3431Scarlsonj 	(void) add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE,
79*3431Scarlsonj 	    htons(dsmp->dsm_lif->lif_pif->pif_max - sizeof (struct udpiphdr)));
800Sstevel@tonic-gate 
81*3431Scarlsonj 	(void) add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len);
82*3431Scarlsonj 	(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 	 */
88*3431Scarlsonj 	if (df_get_bool(dsmp->dsm_name, dsmp->dsm_isv6, DF_REQUEST_HOSTNAME)) {
890Sstevel@tonic-gate 		(void) snprintf(hostfile, sizeof (hostfile), "/etc/hostname.%s",
90*3431Scarlsonj 		    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);
94*3431Scarlsonj 			if ((dsmp->dsm_reqhost = strdup(reqhost)) != NULL)
95*3431Scarlsonj 				(void) add_pkt_opt(dpkt, CD_HOSTNAME,
96*3431Scarlsonj 				    dsmp->dsm_reqhost,
97*3431Scarlsonj 				    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");
101*3431Scarlsonj 		} else {
102*3431Scarlsonj 			dhcpmsg(MSG_DEBUG,
103*3431Scarlsonj 			    "dhcp_selecting: no hostname for %s",
104*3431Scarlsonj 			    dsmp->dsm_name);
1050Sstevel@tonic-gate 		}
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
108*3431Scarlsonj 	(void) add_pkt_opt(dpkt, CD_END, NULL, 0);
109*3431Scarlsonj 
110*3431Scarlsonj 	(void) send_pkt(dsmp, dpkt, htonl(INADDR_BROADCAST), stop_init_reboot);
111*3431Scarlsonj }
112*3431Scarlsonj 
113*3431Scarlsonj 
114*3431Scarlsonj /*
115*3431Scarlsonj  * dhcp_init_reboot_v6(): attempts to reuse a cached configuration for a state
116*3431Scarlsonj  *			  machine.  Create a Confirm message and multicast it
117*3431Scarlsonj  *			  out.
118*3431Scarlsonj  *
119*3431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
120*3431Scarlsonj  *  output: void
121*3431Scarlsonj  */
122*3431Scarlsonj 
123*3431Scarlsonj static void
124*3431Scarlsonj dhcp_init_reboot_v6(dhcp_smach_t *dsmp)
125*3431Scarlsonj {
126*3431Scarlsonj 	dhcp_pkt_t *dpkt;
127*3431Scarlsonj 	dhcpv6_option_t *d6o, *d6so, *popt;
128*3431Scarlsonj 	uint_t olen, solen;
129*3431Scarlsonj 	dhcpv6_ia_na_t d6in;
130*3431Scarlsonj 	dhcpv6_iaaddr_t d6ia;
131*3431Scarlsonj 	char *obase;
132*3431Scarlsonj 
133*3431Scarlsonj 	/*
134*3431Scarlsonj 	 * Assemble a Confirm message based on the current ack.
135*3431Scarlsonj 	 */
136*3431Scarlsonj 
137*3431Scarlsonj 	dpkt = init_pkt(dsmp, DHCPV6_MSG_CONFIRM);
138*3431Scarlsonj 
139*3431Scarlsonj 	/*
140*3431Scarlsonj 	 * Loop over and copy IA_NAs and IAADDRs we have in our last ack.  This
141*3431Scarlsonj 	 * is what we'll be requesting.
142*3431Scarlsonj 	 */
143*3431Scarlsonj 	d6o = NULL;
144*3431Scarlsonj 	while ((d6o = dhcpv6_pkt_option(dsmp->dsm_ack, d6o, DHCPV6_OPT_IA_NA,
145*3431Scarlsonj 	    &olen)) != NULL) {
146*3431Scarlsonj 
147*3431Scarlsonj 		/*
148*3431Scarlsonj 		 * Copy in IA_NA option from the ack.  Note that we use zero
149*3431Scarlsonj 		 * for all timers in accordance with RFC 3315.  (It would make
150*3431Scarlsonj 		 * some sense to say what we think the current timers are as
151*3431Scarlsonj 		 * a hint to the server, but the RFC doesn't agree.)
152*3431Scarlsonj 		 */
153*3431Scarlsonj 		if (olen < sizeof (dhcpv6_ia_na_t))
154*3431Scarlsonj 			continue;
155*3431Scarlsonj 		(void) memcpy(&d6in, d6o, sizeof (d6in));
156*3431Scarlsonj 		d6in.d6in_t1 = 0;
157*3431Scarlsonj 		d6in.d6in_t2 = 0;
158*3431Scarlsonj 		popt = add_pkt_opt(dpkt, DHCPV6_OPT_IA_NA,
159*3431Scarlsonj 		    (char *)&d6in + sizeof (*d6o),
160*3431Scarlsonj 		    sizeof (d6in) - sizeof (*d6o));
161*3431Scarlsonj 		if (popt == NULL)
162*3431Scarlsonj 			goto failure;
1630Sstevel@tonic-gate 
164*3431Scarlsonj 		/*
165*3431Scarlsonj 		 * Now loop over the IAADDR suboptions and add those.
166*3431Scarlsonj 		 */
167*3431Scarlsonj 		obase = (char *)d6o + sizeof (dhcpv6_ia_na_t);
168*3431Scarlsonj 		olen -= sizeof (dhcpv6_ia_na_t);
169*3431Scarlsonj 		d6so = NULL;
170*3431Scarlsonj 		while ((d6so = dhcpv6_find_option(obase, olen, d6so,
171*3431Scarlsonj 		    DHCPV6_OPT_IAADDR, &solen)) != NULL) {
172*3431Scarlsonj 			if (solen < sizeof (dhcpv6_iaaddr_t))
173*3431Scarlsonj 				continue;
174*3431Scarlsonj 			(void) memcpy(&d6ia, d6so, sizeof (d6ia));
175*3431Scarlsonj 			d6ia.d6ia_preflife = 0;
176*3431Scarlsonj 			d6ia.d6ia_vallife = 0;
177*3431Scarlsonj 			if (add_pkt_subopt(dpkt, popt, DHCPV6_OPT_IAADDR,
178*3431Scarlsonj 			    (char *)&d6ia + sizeof (*d6so),
179*3431Scarlsonj 			    sizeof (d6ia) - sizeof (*d6so)) == NULL)
180*3431Scarlsonj 				goto failure;
181*3431Scarlsonj 		}
182*3431Scarlsonj 	}
183*3431Scarlsonj 
184*3431Scarlsonj 	/* Add required Option Request option */
185*3431Scarlsonj 	(void) add_pkt_prl(dpkt, dsmp);
186*3431Scarlsonj 
187*3431Scarlsonj 	(void) send_pkt_v6(dsmp, dpkt, ipv6_all_dhcp_relay_and_servers,
188*3431Scarlsonj 	    stop_init_reboot, DHCPV6_CNF_TIMEOUT, DHCPV6_CNF_MAX_RT);
189*3431Scarlsonj 
190*3431Scarlsonj 	return;
191*3431Scarlsonj 
192*3431Scarlsonj failure:
193*3431Scarlsonj 	if (iu_schedule_timer_ms(tq, lrand48() % DHCP_SELECT_WAIT, dhcp_start,
194*3431Scarlsonj 	    dsmp) != -1)
195*3431Scarlsonj 		hold_smach(dsmp);
196*3431Scarlsonj 	else
197*3431Scarlsonj 		dhcp_selecting(dsmp);
198*3431Scarlsonj }
199*3431Scarlsonj 
200*3431Scarlsonj /*
201*3431Scarlsonj  * dhcp_init_reboot(): attempts to reuse a cached configuration for a state
202*3431Scarlsonj  *		       machine.
203*3431Scarlsonj  *
204*3431Scarlsonj  *   input: dhcp_smach_t *: the state machine to examine for reuse
205*3431Scarlsonj  *  output: void
206*3431Scarlsonj  */
207*3431Scarlsonj 
208*3431Scarlsonj void
209*3431Scarlsonj dhcp_init_reboot(dhcp_smach_t *dsmp)
210*3431Scarlsonj {
211*3431Scarlsonj 	dhcpmsg(MSG_VERBOSE,  "%s has cached configuration - entering "
212*3431Scarlsonj 	    "INIT_REBOOT", dsmp->dsm_name);
213*3431Scarlsonj 
214*3431Scarlsonj 	if (!set_smach_state(dsmp, INIT_REBOOT)) {
215*3431Scarlsonj 		dhcpmsg(MSG_ERROR, "dhcp_init_reboot: cannot register to "
216*3431Scarlsonj 		    "collect ACK/NAK packets, reverting to INIT on %s",
217*3431Scarlsonj 		    dsmp->dsm_name);
218*3431Scarlsonj 
219*3431Scarlsonj 		dsmp->dsm_dflags |= DHCP_IF_FAILED;
220*3431Scarlsonj 		(void) set_smach_state(dsmp, INIT);
221*3431Scarlsonj 		ipc_action_finish(dsmp, DHCP_IPC_E_MEMORY);
222*3431Scarlsonj 		return;
223*3431Scarlsonj 	}
224*3431Scarlsonj 
225*3431Scarlsonj 	if (dsmp->dsm_isv6)
226*3431Scarlsonj 		dhcp_init_reboot_v6(dsmp);
227*3431Scarlsonj 	else
228*3431Scarlsonj 		dhcp_init_reboot_v4(dsmp);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate  * stop_init_reboot(): decides when to stop retransmitting REQUESTs
2330Sstevel@tonic-gate  *
234*3431Scarlsonj  *   input: dhcp_smach_t *: the state machine sending the REQUESTs
2350Sstevel@tonic-gate  *	    unsigned int: the number of REQUESTs sent so far
2360Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if retransmissions should stop
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate static boolean_t
240*3431Scarlsonj stop_init_reboot(dhcp_smach_t *dsmp, unsigned int n_requests)
2410Sstevel@tonic-gate {
242*3431Scarlsonj 	if (dsmp->dsm_isv6) {
243*3431Scarlsonj 		uint_t nowabs, maxabs;
2440Sstevel@tonic-gate 
245*3431Scarlsonj 		nowabs = gethrtime() / (NANOSEC / MILLISEC);
246*3431Scarlsonj 		maxabs = dsmp->dsm_neg_hrtime / (NANOSEC / MILLISEC) +
247*3431Scarlsonj 		    DHCPV6_CNF_MAX_RD;
248*3431Scarlsonj 		if (nowabs < maxabs) {
249*3431Scarlsonj 			/* Cap the timer based on the maximum */
250*3431Scarlsonj 			if (nowabs + dsmp->dsm_send_timeout > maxabs)
251*3431Scarlsonj 				dsmp->dsm_send_timeout = maxabs - nowabs;
252*3431Scarlsonj 			return (B_FALSE);
253*3431Scarlsonj 		}
254*3431Scarlsonj 		dhcpmsg(MSG_INFO, "no Reply to Confirm, using remainder of "
255*3431Scarlsonj 		    "existing lease on %s", dsmp->dsm_name);
256*3431Scarlsonj 	} else {
257*3431Scarlsonj 		if (n_requests < DHCP_MAX_REQUESTS)
258*3431Scarlsonj 			return (B_FALSE);
259*3431Scarlsonj 		dhcpmsg(MSG_INFO, "no ACK/NAK to INIT_REBOOT REQUEST, "
260*3431Scarlsonj 		    "using remainder of existing lease on %s", dsmp->dsm_name);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
263*3431Scarlsonj 	/*
264*3431Scarlsonj 	 * We already stuck our old ack in dsmp->dsm_ack and relativized the
265*3431Scarlsonj 	 * packet times, so we can just pretend that the server sent it to us
266*3431Scarlsonj 	 * and move to bound.  If that fails, fall back to selecting.
267*3431Scarlsonj 	 */
268*3431Scarlsonj 
269*3431Scarlsonj 	if (dhcp_bound(dsmp, NULL)) {
270*3431Scarlsonj 		if (dsmp->dsm_isv6) {
271*3431Scarlsonj 			if (!save_server_id(dsmp, dsmp->dsm_ack))
272*3431Scarlsonj 				goto failure;
273*3431Scarlsonj 			server_unicast_option(dsmp, dsmp->dsm_ack);
274*3431Scarlsonj 		}
275*3431Scarlsonj 	} else {
276*3431Scarlsonj failure:
277*3431Scarlsonj 		dhcpmsg(MSG_INFO, "unable to use saved lease on %s; restarting",
278*3431Scarlsonj 		    dsmp->dsm_name);
279*3431Scarlsonj 		dhcp_selecting(dsmp);
280*3431Scarlsonj 	}
281*3431Scarlsonj 
282*3431Scarlsonj 	return (B_TRUE);
2830Sstevel@tonic-gate }
284