xref: /titanic_52/usr/src/cmd/cmd-inet/sbin/dhcpagent/init_reboot.c (revision fb2f18f820d90b001aea4fb27dd654bc1263c440)
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 	(void) add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len);
82 	(void) add_pkt_prl(dpkt, dsmp);
83 
84 	/*
85 	 * Set CD_HOSTNAME option if REQUEST_HOSTNAME is set and a hostname
86 	 * is found in /etc/hostname.<ifname>
87 	 */
88 	if (df_get_bool(dsmp->dsm_name, dsmp->dsm_isv6, DF_REQUEST_HOSTNAME)) {
89 		(void) snprintf(hostfile, sizeof (hostfile), "/etc/hostname.%s",
90 		    dsmp->dsm_name);
91 
92 		if ((reqhost = iffile_to_hostname(hostfile)) != NULL) {
93 			dhcpmsg(MSG_DEBUG, "dhcp_selecting: host %s", reqhost);
94 			if ((dsmp->dsm_reqhost = strdup(reqhost)) != NULL)
95 				(void) add_pkt_opt(dpkt, CD_HOSTNAME,
96 				    dsmp->dsm_reqhost,
97 				    strlen(dsmp->dsm_reqhost));
98 			else
99 				dhcpmsg(MSG_WARNING, "dhcp_selecting: cannot"
100 				    " allocate memory for host name option");
101 		} else {
102 			dhcpmsg(MSG_DEBUG,
103 			    "dhcp_selecting: no hostname for %s",
104 			    dsmp->dsm_name);
105 		}
106 	}
107 
108 	(void) add_pkt_opt(dpkt, CD_END, NULL, 0);
109 
110 	(void) send_pkt(dsmp, dpkt, htonl(INADDR_BROADCAST), stop_init_reboot);
111 }
112 
113 
114 /*
115  * dhcp_init_reboot_v6(): attempts to reuse a cached configuration for a state
116  *			  machine.  Create a Confirm message and multicast it
117  *			  out.
118  *
119  *   input: dhcp_smach_t *: the state machine to examine for reuse
120  *  output: void
121  */
122 
123 static void
124 dhcp_init_reboot_v6(dhcp_smach_t *dsmp)
125 {
126 	dhcp_pkt_t *dpkt;
127 	dhcpv6_option_t *d6o, *d6so, *popt;
128 	uint_t olen, solen;
129 	dhcpv6_ia_na_t d6in;
130 	dhcpv6_iaaddr_t d6ia;
131 	char *obase;
132 
133 	/*
134 	 * Assemble a Confirm message based on the current ack.
135 	 */
136 
137 	dpkt = init_pkt(dsmp, DHCPV6_MSG_CONFIRM);
138 
139 	/*
140 	 * Loop over and copy IA_NAs and IAADDRs we have in our last ack.  This
141 	 * is what we'll be requesting.
142 	 */
143 	d6o = NULL;
144 	while ((d6o = dhcpv6_pkt_option(dsmp->dsm_ack, d6o, DHCPV6_OPT_IA_NA,
145 	    &olen)) != NULL) {
146 
147 		/*
148 		 * Copy in IA_NA option from the ack.  Note that we use zero
149 		 * for all timers in accordance with RFC 3315.  (It would make
150 		 * some sense to say what we think the current timers are as
151 		 * a hint to the server, but the RFC doesn't agree.)
152 		 */
153 		if (olen < sizeof (dhcpv6_ia_na_t))
154 			continue;
155 		(void) memcpy(&d6in, d6o, sizeof (d6in));
156 		d6in.d6in_t1 = 0;
157 		d6in.d6in_t2 = 0;
158 		popt = add_pkt_opt(dpkt, DHCPV6_OPT_IA_NA,
159 		    (char *)&d6in + sizeof (*d6o),
160 		    sizeof (d6in) - sizeof (*d6o));
161 		if (popt == NULL)
162 			goto failure;
163 
164 		/*
165 		 * Now loop over the IAADDR suboptions and add those.
166 		 */
167 		obase = (char *)d6o + sizeof (dhcpv6_ia_na_t);
168 		olen -= sizeof (dhcpv6_ia_na_t);
169 		d6so = NULL;
170 		while ((d6so = dhcpv6_find_option(obase, olen, d6so,
171 		    DHCPV6_OPT_IAADDR, &solen)) != NULL) {
172 			if (solen < sizeof (dhcpv6_iaaddr_t))
173 				continue;
174 			(void) memcpy(&d6ia, d6so, sizeof (d6ia));
175 			d6ia.d6ia_preflife = 0;
176 			d6ia.d6ia_vallife = 0;
177 			if (add_pkt_subopt(dpkt, popt, DHCPV6_OPT_IAADDR,
178 			    (char *)&d6ia + sizeof (*d6so),
179 			    sizeof (d6ia) - sizeof (*d6so)) == NULL)
180 				goto failure;
181 		}
182 	}
183 
184 	/* Add required Option Request option */
185 	(void) add_pkt_prl(dpkt, dsmp);
186 
187 	(void) send_pkt_v6(dsmp, dpkt, ipv6_all_dhcp_relay_and_servers,
188 	    stop_init_reboot, DHCPV6_CNF_TIMEOUT, DHCPV6_CNF_MAX_RT);
189 
190 	return;
191 
192 failure:
193 	if (iu_schedule_timer_ms(tq, lrand48() % DHCP_SELECT_WAIT, dhcp_start,
194 	    dsmp) != -1)
195 		hold_smach(dsmp);
196 	else
197 		dhcp_selecting(dsmp);
198 }
199 
200 /*
201  * dhcp_init_reboot(): attempts to reuse a cached configuration for a state
202  *		       machine.
203  *
204  *   input: dhcp_smach_t *: the state machine to examine for reuse
205  *  output: void
206  */
207 
208 void
209 dhcp_init_reboot(dhcp_smach_t *dsmp)
210 {
211 	dhcpmsg(MSG_VERBOSE,  "%s has cached configuration - entering "
212 	    "INIT_REBOOT", dsmp->dsm_name);
213 
214 	if (!set_smach_state(dsmp, INIT_REBOOT)) {
215 		dhcpmsg(MSG_ERROR, "dhcp_init_reboot: cannot register to "
216 		    "collect ACK/NAK packets, reverting to INIT on %s",
217 		    dsmp->dsm_name);
218 
219 		dsmp->dsm_dflags |= DHCP_IF_FAILED;
220 		(void) set_smach_state(dsmp, INIT);
221 		ipc_action_finish(dsmp, DHCP_IPC_E_MEMORY);
222 		return;
223 	}
224 
225 	if (dsmp->dsm_isv6)
226 		dhcp_init_reboot_v6(dsmp);
227 	else
228 		dhcp_init_reboot_v4(dsmp);
229 }
230 
231 /*
232  * stop_init_reboot(): decides when to stop retransmitting REQUESTs
233  *
234  *   input: dhcp_smach_t *: the state machine sending the REQUESTs
235  *	    unsigned int: the number of REQUESTs sent so far
236  *  output: boolean_t: B_TRUE if retransmissions should stop
237  */
238 
239 static boolean_t
240 stop_init_reboot(dhcp_smach_t *dsmp, unsigned int n_requests)
241 {
242 	if (dsmp->dsm_isv6) {
243 		uint_t nowabs, maxabs;
244 
245 		nowabs = gethrtime() / (NANOSEC / MILLISEC);
246 		maxabs = dsmp->dsm_neg_hrtime / (NANOSEC / MILLISEC) +
247 		    DHCPV6_CNF_MAX_RD;
248 		if (nowabs < maxabs) {
249 			/* Cap the timer based on the maximum */
250 			if (nowabs + dsmp->dsm_send_timeout > maxabs)
251 				dsmp->dsm_send_timeout = maxabs - nowabs;
252 			return (B_FALSE);
253 		}
254 		dhcpmsg(MSG_INFO, "no Reply to Confirm, using remainder of "
255 		    "existing lease on %s", dsmp->dsm_name);
256 	} else {
257 		if (n_requests < DHCP_MAX_REQUESTS)
258 			return (B_FALSE);
259 		dhcpmsg(MSG_INFO, "no ACK/NAK to INIT_REBOOT REQUEST, "
260 		    "using remainder of existing lease on %s", dsmp->dsm_name);
261 	}
262 
263 	/*
264 	 * We already stuck our old ack in dsmp->dsm_ack and relativized the
265 	 * packet times, so we can just pretend that the server sent it to us
266 	 * and move to bound.  If that fails, fall back to selecting.
267 	 */
268 
269 	if (dhcp_bound(dsmp, NULL)) {
270 		if (dsmp->dsm_isv6) {
271 			if (!save_server_id(dsmp, dsmp->dsm_ack))
272 				goto failure;
273 			server_unicast_option(dsmp, dsmp->dsm_ack);
274 		}
275 	} else {
276 failure:
277 		dhcpmsg(MSG_INFO, "unable to use saved lease on %s; restarting",
278 		    dsmp->dsm_name);
279 		dhcp_selecting(dsmp);
280 	}
281 
282 	return (B_TRUE);
283 }
284