xref: /illumos-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/init_reboot.c (revision cd61ae21816e53b94bc1673f3f1aa651fc3115e8)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  * Copyright (c) 2016-2017, Chris Fraire <cfraire@me.com>.
25  *
26  * INIT_REBOOT state of the DHCP client state machine.
27  */
28 
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netinet/dhcp.h>
34 #include <netinet/udp.h>
35 #include <netinet/ip_var.h>
36 #include <netinet/udp_var.h>
37 #include <dhcpmsg.h>
38 #include <string.h>
39 
40 #include "agent.h"
41 #include "packet.h"
42 #include "states.h"
43 #include "util.h"
44 #include "interface.h"
45 #include "defaults.h"
46 
47 static stop_func_t	stop_init_reboot;
48 
49 /*
50  * dhcp_init_reboot_v4(): attempts to reuse a cached configuration for a state
51  *			  machine.
52  *
53  *   input: dhcp_smach_t *: the state machine to examine for reuse
54  *  output: void
55  */
56 
57 static void
58 dhcp_init_reboot_v4(dhcp_smach_t *dsmp)
59 {
60 	dhcp_pkt_t		*dpkt;
61 
62 	/*
63 	 * assemble DHCPREQUEST message.  The max dhcp message size
64 	 * option is set to the interface max, minus the size of the udp and
65 	 * ip headers.
66 	 */
67 
68 	dpkt = init_pkt(dsmp, REQUEST);
69 	(void) add_pkt_opt32(dpkt, CD_REQUESTED_IP_ADDR,
70 	    dsmp->dsm_ack->pkt->yiaddr.s_addr);
71 
72 	(void) add_pkt_opt32(dpkt, CD_LEASE_TIME, htonl(DHCP_PERM));
73 	(void) add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE,
74 	    htons(dsmp->dsm_lif->lif_pif->pif_max - sizeof (struct udpiphdr)));
75 
76 	if (class_id_len != 0)
77 		(void) add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len);
78 	(void) add_pkt_prl(dpkt, dsmp);
79 
80 	if (!dhcp_add_fqdn_opt(dpkt, dsmp))
81 		(void) dhcp_add_hostname_opt(dpkt, dsmp);
82 
83 	(void) add_pkt_opt(dpkt, CD_END, NULL, 0);
84 
85 	(void) send_pkt(dsmp, dpkt, htonl(INADDR_BROADCAST), stop_init_reboot);
86 }
87 
88 
89 /*
90  * dhcp_init_reboot_v6(): attempts to reuse a cached configuration for a state
91  *			  machine.  Create a Confirm message and multicast it
92  *			  out.
93  *
94  *   input: dhcp_smach_t *: the state machine to examine for reuse
95  *  output: void
96  */
97 
98 static void
99 dhcp_init_reboot_v6(dhcp_smach_t *dsmp)
100 {
101 	dhcp_pkt_t *dpkt;
102 	dhcpv6_option_t *d6o, *d6so, *popt;
103 	uint_t olen, solen;
104 	dhcpv6_ia_na_t d6in;
105 	dhcpv6_iaaddr_t d6ia;
106 	char *obase;
107 
108 	/*
109 	 * Assemble a Confirm message based on the current ack.
110 	 */
111 
112 	dpkt = init_pkt(dsmp, DHCPV6_MSG_CONFIRM);
113 
114 	/*
115 	 * Loop over and copy IA_NAs and IAADDRs we have in our last ack.  This
116 	 * is what we'll be requesting.
117 	 */
118 	d6o = NULL;
119 	while ((d6o = dhcpv6_pkt_option(dsmp->dsm_ack, d6o, DHCPV6_OPT_IA_NA,
120 	    &olen)) != NULL) {
121 
122 		/*
123 		 * Copy in IA_NA option from the ack.  Note that we use zero
124 		 * for all timers in accordance with RFC 3315.  (It would make
125 		 * some sense to say what we think the current timers are as
126 		 * a hint to the server, but the RFC doesn't agree.)
127 		 */
128 		if (olen < sizeof (dhcpv6_ia_na_t))
129 			continue;
130 		(void) memcpy(&d6in, d6o, sizeof (d6in));
131 		d6in.d6in_t1 = 0;
132 		d6in.d6in_t2 = 0;
133 		popt = add_pkt_opt(dpkt, DHCPV6_OPT_IA_NA,
134 		    (char *)&d6in + sizeof (*d6o),
135 		    sizeof (d6in) - sizeof (*d6o));
136 		if (popt == NULL)
137 			goto failure;
138 
139 		/*
140 		 * Now loop over the IAADDR suboptions and add those.
141 		 */
142 		obase = (char *)d6o + sizeof (dhcpv6_ia_na_t);
143 		olen -= sizeof (dhcpv6_ia_na_t);
144 		d6so = NULL;
145 		while ((d6so = dhcpv6_find_option(obase, olen, d6so,
146 		    DHCPV6_OPT_IAADDR, &solen)) != NULL) {
147 			if (solen < sizeof (dhcpv6_iaaddr_t))
148 				continue;
149 			(void) memcpy(&d6ia, d6so, sizeof (d6ia));
150 			d6ia.d6ia_preflife = 0;
151 			d6ia.d6ia_vallife = 0;
152 			if (add_pkt_subopt(dpkt, popt, DHCPV6_OPT_IAADDR,
153 			    (char *)&d6ia + sizeof (*d6so),
154 			    sizeof (d6ia) - sizeof (*d6so)) == NULL)
155 				goto failure;
156 		}
157 	}
158 
159 	/* Add required Option Request option */
160 	(void) add_pkt_prl(dpkt, dsmp);
161 
162 	(void) send_pkt_v6(dsmp, dpkt, ipv6_all_dhcp_relay_and_servers,
163 	    stop_init_reboot, DHCPV6_CNF_TIMEOUT, DHCPV6_CNF_MAX_RT);
164 
165 	return;
166 
167 failure:
168 	if (!set_start_timer(dsmp))
169 		dhcp_selecting(dsmp);
170 }
171 
172 /*
173  * dhcp_init_reboot(): attempts to reuse a cached configuration for a state
174  *		       machine.
175  *
176  *   input: dhcp_smach_t *: the state machine to examine for reuse
177  *  output: void
178  */
179 
180 void
181 dhcp_init_reboot(dhcp_smach_t *dsmp)
182 {
183 	dhcpmsg(MSG_VERBOSE,  "%s has cached configuration - entering "
184 	    "INIT_REBOOT", dsmp->dsm_name);
185 
186 	if (!set_smach_state(dsmp, INIT_REBOOT)) {
187 		dhcpmsg(MSG_ERROR, "dhcp_init_reboot: cannot register to "
188 		    "collect ACK/NAK packets, reverting to INIT on %s",
189 		    dsmp->dsm_name);
190 
191 		dsmp->dsm_dflags |= DHCP_IF_FAILED;
192 		(void) set_smach_state(dsmp, INIT);
193 		ipc_action_finish(dsmp, DHCP_IPC_E_MEMORY);
194 		return;
195 	}
196 
197 	if (dsmp->dsm_isv6)
198 		dhcp_init_reboot_v6(dsmp);
199 	else
200 		dhcp_init_reboot_v4(dsmp);
201 }
202 
203 /*
204  * stop_init_reboot(): decides when to stop retransmitting REQUESTs
205  *
206  *   input: dhcp_smach_t *: the state machine sending the REQUESTs
207  *	    unsigned int: the number of REQUESTs sent so far
208  *  output: boolean_t: B_TRUE if retransmissions should stop
209  */
210 
211 static boolean_t
212 stop_init_reboot(dhcp_smach_t *dsmp, unsigned int n_requests)
213 {
214 	if (dsmp->dsm_isv6) {
215 		uint_t nowabs, maxabs;
216 
217 		nowabs = NSEC2MSEC(gethrtime());
218 		maxabs = NSEC2MSEC(dsmp->dsm_neg_hrtime) + DHCPV6_CNF_MAX_RD;
219 		if (nowabs < maxabs) {
220 			/* Cap the timer based on the maximum */
221 			if (nowabs + dsmp->dsm_send_timeout > maxabs)
222 				dsmp->dsm_send_timeout = maxabs - nowabs;
223 			return (B_FALSE);
224 		}
225 	} else {
226 		if (n_requests < DHCP_MAX_REQUESTS)
227 			return (B_FALSE);
228 	}
229 
230 	if (df_get_bool(dsmp->dsm_name, dsmp->dsm_isv6,
231 	    DF_VERIFIED_LEASE_ONLY)) {
232 		dhcpmsg(MSG_INFO,
233 		    "unable to verify existing lease on %s; restarting",
234 		    dsmp->dsm_name);
235 		dhcp_selecting(dsmp);
236 		return (B_TRUE);
237 	}
238 
239 	if (dsmp->dsm_isv6) {
240 		dhcpmsg(MSG_INFO, "no Reply to Confirm, using remainder of "
241 		    "existing lease on %s", dsmp->dsm_name);
242 	} else {
243 		dhcpmsg(MSG_INFO, "no ACK/NAK to INIT_REBOOT REQUEST, "
244 		    "using remainder of existing lease on %s", dsmp->dsm_name);
245 	}
246 
247 	/*
248 	 * We already stuck our old ack in dsmp->dsm_ack and relativized the
249 	 * packet times, so we can just pretend that the server sent it to us
250 	 * and move to bound.  If that fails, fall back to selecting.
251 	 */
252 
253 	if (dhcp_bound(dsmp, NULL)) {
254 		if (dsmp->dsm_isv6) {
255 			if (!save_server_id(dsmp, dsmp->dsm_ack))
256 				goto failure;
257 			server_unicast_option(dsmp, dsmp->dsm_ack);
258 		}
259 	} else {
260 failure:
261 		dhcpmsg(MSG_INFO, "unable to use saved lease on %s; restarting",
262 		    dsmp->dsm_name);
263 		dhcp_selecting(dsmp);
264 	}
265 
266 	return (B_TRUE);
267 }
268