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