xref: /titanic_41/usr/src/cmd/cmd-inet/sbin/dhcpagent/init_reboot.c (revision 46736d35df047bb400483364f76bfcb08cdcbb25)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * INIT_REBOOT state of the DHCP client state machine.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/types.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <limits.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/dhcp.h>
37 #include <netinet/udp.h>
38 #include <netinet/ip_var.h>
39 #include <netinet/udp_var.h>
40 #include <dhcpmsg.h>
41 #include <string.h>
42 
43 #include "agent.h"
44 #include "packet.h"
45 #include "states.h"
46 #include "util.h"
47 #include "interface.h"
48 #include "defaults.h"
49 
50 static stop_func_t	stop_init_reboot;
51 
52 /*
53  * dhcp_init_reboot_v4(): attempts to reuse a cached configuration for a state
54  *			  machine.
55  *
56  *   input: dhcp_smach_t *: the state machine to examine for reuse
57  *  output: void
58  */
59 
60 static void
61 dhcp_init_reboot_v4(dhcp_smach_t *dsmp)
62 {
63 	dhcp_pkt_t		*dpkt;
64 	const char		*reqhost;
65 	char			hostfile[PATH_MAX + 1];
66 
67 	/*
68 	 * assemble DHCPREQUEST message.  The max dhcp message size
69 	 * option is set to the interface max, minus the size of the udp and
70 	 * ip headers.
71 	 */
72 
73 	dpkt = init_pkt(dsmp, REQUEST);
74 	(void) add_pkt_opt32(dpkt, CD_REQUESTED_IP_ADDR,
75 	    dsmp->dsm_ack->pkt->yiaddr.s_addr);
76 
77 	(void) add_pkt_opt32(dpkt, CD_LEASE_TIME, htonl(DHCP_PERM));
78 	(void) add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE,
79 	    htons(dsmp->dsm_lif->lif_pif->pif_max - sizeof (struct udpiphdr)));
80 
81 	if (class_id_len != 0)
82 		(void) add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len);
83 	(void) add_pkt_prl(dpkt, dsmp);
84 
85 	/*
86 	 * Set CD_HOSTNAME option if REQUEST_HOSTNAME is set and a hostname
87 	 * is found in /etc/hostname.<ifname>
88 	 */
89 	if (df_get_bool(dsmp->dsm_name, dsmp->dsm_isv6, DF_REQUEST_HOSTNAME)) {
90 		(void) snprintf(hostfile, sizeof (hostfile), "/etc/hostname.%s",
91 		    dsmp->dsm_name);
92 
93 		if ((reqhost = iffile_to_hostname(hostfile)) != NULL) {
94 			dhcpmsg(MSG_DEBUG, "dhcp_selecting: host %s", reqhost);
95 			if ((dsmp->dsm_reqhost = strdup(reqhost)) != NULL)
96 				(void) add_pkt_opt(dpkt, CD_HOSTNAME,
97 				    dsmp->dsm_reqhost,
98 				    strlen(dsmp->dsm_reqhost));
99 			else
100 				dhcpmsg(MSG_WARNING, "dhcp_selecting: cannot"
101 				    " allocate memory for host name option");
102 		} else {
103 			dhcpmsg(MSG_DEBUG,
104 			    "dhcp_selecting: no hostname for %s",
105 			    dsmp->dsm_name);
106 		}
107 	}
108 
109 	(void) add_pkt_opt(dpkt, CD_END, NULL, 0);
110 
111 	(void) send_pkt(dsmp, dpkt, htonl(INADDR_BROADCAST), stop_init_reboot);
112 }
113 
114 
115 /*
116  * dhcp_init_reboot_v6(): attempts to reuse a cached configuration for a state
117  *			  machine.  Create a Confirm message and multicast it
118  *			  out.
119  *
120  *   input: dhcp_smach_t *: the state machine to examine for reuse
121  *  output: void
122  */
123 
124 static void
125 dhcp_init_reboot_v6(dhcp_smach_t *dsmp)
126 {
127 	dhcp_pkt_t *dpkt;
128 	dhcpv6_option_t *d6o, *d6so, *popt;
129 	uint_t olen, solen;
130 	dhcpv6_ia_na_t d6in;
131 	dhcpv6_iaaddr_t d6ia;
132 	char *obase;
133 
134 	/*
135 	 * Assemble a Confirm message based on the current ack.
136 	 */
137 
138 	dpkt = init_pkt(dsmp, DHCPV6_MSG_CONFIRM);
139 
140 	/*
141 	 * Loop over and copy IA_NAs and IAADDRs we have in our last ack.  This
142 	 * is what we'll be requesting.
143 	 */
144 	d6o = NULL;
145 	while ((d6o = dhcpv6_pkt_option(dsmp->dsm_ack, d6o, DHCPV6_OPT_IA_NA,
146 	    &olen)) != NULL) {
147 
148 		/*
149 		 * Copy in IA_NA option from the ack.  Note that we use zero
150 		 * for all timers in accordance with RFC 3315.  (It would make
151 		 * some sense to say what we think the current timers are as
152 		 * a hint to the server, but the RFC doesn't agree.)
153 		 */
154 		if (olen < sizeof (dhcpv6_ia_na_t))
155 			continue;
156 		(void) memcpy(&d6in, d6o, sizeof (d6in));
157 		d6in.d6in_t1 = 0;
158 		d6in.d6in_t2 = 0;
159 		popt = add_pkt_opt(dpkt, DHCPV6_OPT_IA_NA,
160 		    (char *)&d6in + sizeof (*d6o),
161 		    sizeof (d6in) - sizeof (*d6o));
162 		if (popt == NULL)
163 			goto failure;
164 
165 		/*
166 		 * Now loop over the IAADDR suboptions and add those.
167 		 */
168 		obase = (char *)d6o + sizeof (dhcpv6_ia_na_t);
169 		olen -= sizeof (dhcpv6_ia_na_t);
170 		d6so = NULL;
171 		while ((d6so = dhcpv6_find_option(obase, olen, d6so,
172 		    DHCPV6_OPT_IAADDR, &solen)) != NULL) {
173 			if (solen < sizeof (dhcpv6_iaaddr_t))
174 				continue;
175 			(void) memcpy(&d6ia, d6so, sizeof (d6ia));
176 			d6ia.d6ia_preflife = 0;
177 			d6ia.d6ia_vallife = 0;
178 			if (add_pkt_subopt(dpkt, popt, DHCPV6_OPT_IAADDR,
179 			    (char *)&d6ia + sizeof (*d6so),
180 			    sizeof (d6ia) - sizeof (*d6so)) == NULL)
181 				goto failure;
182 		}
183 	}
184 
185 	/* Add required Option Request option */
186 	(void) add_pkt_prl(dpkt, dsmp);
187 
188 	(void) send_pkt_v6(dsmp, dpkt, ipv6_all_dhcp_relay_and_servers,
189 	    stop_init_reboot, DHCPV6_CNF_TIMEOUT, DHCPV6_CNF_MAX_RT);
190 
191 	return;
192 
193 failure:
194 	if (iu_schedule_timer_ms(tq, lrand48() % DHCP_SELECT_WAIT, dhcp_start,
195 	    dsmp) != -1)
196 		hold_smach(dsmp);
197 	else
198 		dhcp_selecting(dsmp);
199 }
200 
201 /*
202  * dhcp_init_reboot(): attempts to reuse a cached configuration for a state
203  *		       machine.
204  *
205  *   input: dhcp_smach_t *: the state machine to examine for reuse
206  *  output: void
207  */
208 
209 void
210 dhcp_init_reboot(dhcp_smach_t *dsmp)
211 {
212 	dhcpmsg(MSG_VERBOSE,  "%s has cached configuration - entering "
213 	    "INIT_REBOOT", dsmp->dsm_name);
214 
215 	if (!set_smach_state(dsmp, INIT_REBOOT)) {
216 		dhcpmsg(MSG_ERROR, "dhcp_init_reboot: cannot register to "
217 		    "collect ACK/NAK packets, reverting to INIT on %s",
218 		    dsmp->dsm_name);
219 
220 		dsmp->dsm_dflags |= DHCP_IF_FAILED;
221 		(void) set_smach_state(dsmp, INIT);
222 		ipc_action_finish(dsmp, DHCP_IPC_E_MEMORY);
223 		return;
224 	}
225 
226 	if (dsmp->dsm_isv6)
227 		dhcp_init_reboot_v6(dsmp);
228 	else
229 		dhcp_init_reboot_v4(dsmp);
230 }
231 
232 /*
233  * stop_init_reboot(): decides when to stop retransmitting REQUESTs
234  *
235  *   input: dhcp_smach_t *: the state machine sending the REQUESTs
236  *	    unsigned int: the number of REQUESTs sent so far
237  *  output: boolean_t: B_TRUE if retransmissions should stop
238  */
239 
240 static boolean_t
241 stop_init_reboot(dhcp_smach_t *dsmp, unsigned int n_requests)
242 {
243 	if (dsmp->dsm_isv6) {
244 		uint_t nowabs, maxabs;
245 
246 		nowabs = gethrtime() / (NANOSEC / MILLISEC);
247 		maxabs = dsmp->dsm_neg_hrtime / (NANOSEC / MILLISEC) +
248 		    DHCPV6_CNF_MAX_RD;
249 		if (nowabs < maxabs) {
250 			/* Cap the timer based on the maximum */
251 			if (nowabs + dsmp->dsm_send_timeout > maxabs)
252 				dsmp->dsm_send_timeout = maxabs - nowabs;
253 			return (B_FALSE);
254 		}
255 		dhcpmsg(MSG_INFO, "no Reply to Confirm, using remainder of "
256 		    "existing lease on %s", dsmp->dsm_name);
257 	} else {
258 		if (n_requests < DHCP_MAX_REQUESTS)
259 			return (B_FALSE);
260 		dhcpmsg(MSG_INFO, "no ACK/NAK to INIT_REBOOT REQUEST, "
261 		    "using remainder of existing lease on %s", dsmp->dsm_name);
262 	}
263 
264 	/*
265 	 * We already stuck our old ack in dsmp->dsm_ack and relativized the
266 	 * packet times, so we can just pretend that the server sent it to us
267 	 * and move to bound.  If that fails, fall back to selecting.
268 	 */
269 
270 	if (dhcp_bound(dsmp, NULL)) {
271 		if (dsmp->dsm_isv6) {
272 			if (!save_server_id(dsmp, dsmp->dsm_ack))
273 				goto failure;
274 			server_unicast_option(dsmp, dsmp->dsm_ack);
275 		}
276 	} else {
277 failure:
278 		dhcpmsg(MSG_INFO, "unable to use saved lease on %s; restarting",
279 		    dsmp->dsm_name);
280 		dhcp_selecting(dsmp);
281 	}
282 
283 	return (B_TRUE);
284 }
285