1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* ADOPTING state of the client state machine. This is used only during
* diskless boot with IPv4.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/socket.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <sys/systeminfo.h>
#include <netinet/inetutil.h>
#include <netinet/dhcp.h>
#include <dhcpmsg.h>
#include <libdevinfo.h>
#include "agent.h"
#include "async.h"
#include "util.h"
#include "packet.h"
#include "interface.h"
#include "states.h"
typedef struct {
char dk_if_name[IFNAMSIZ];
char dk_ack[1];
} dhcp_kcache_t;
static int get_dhcp_kcache(dhcp_kcache_t **, size_t *);
static boolean_t get_prom_prop(const char *, const char *, uchar_t **,
uint_t *);
/*
* dhcp_adopt(): adopts the interface managed by the kernel for diskless boot
*
* input: void
* output: boolean_t: B_TRUE success, B_FALSE on failure
*/
boolean_t
dhcp_adopt(void)
{
int retval;
dhcp_kcache_t *kcache = NULL;
size_t kcache_size;
PKT_LIST *plp = NULL;
dhcp_lif_t *lif;
dhcp_smach_t *dsmp = NULL;
uint_t client_id_len;
retval = get_dhcp_kcache(&kcache, &kcache_size);
if (retval == 0 || kcache_size < sizeof (dhcp_kcache_t)) {
dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot fetch kernel cache");
goto failure;
}
dhcpmsg(MSG_DEBUG, "dhcp_adopt: fetched %s kcache", kcache->dk_if_name);
/*
* convert the kernel's ACK into binary
*/
plp = alloc_pkt_entry(strlen(kcache->dk_ack) / 2, B_FALSE);
if (plp == NULL)
goto failure;
dhcpmsg(MSG_DEBUG, "dhcp_adopt: allocated ACK of %d bytes", plp->len);
if (hexascii_to_octet(kcache->dk_ack, plp->len * 2, plp->pkt,
&plp->len) != 0) {
dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot convert kernel ACK");
goto failure;
}
if (dhcp_options_scan(plp, B_TRUE) != 0) {
dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot parse kernel ACK");
goto failure;
}
/*
* make an interface to represent the "cached interface" in
* the kernel, hook up the ACK packet we made, and send out
* the extend request (to attempt to renew the lease).
*
* we do a send_extend() instead of doing a dhcp_init_reboot()
* because although dhcp_init_reboot() is more correct from a
* protocol perspective, it introduces a window where a
* diskless client has no IP address but may need to page in
* more of this program. we could mlockall(), but that's
* going to be a mess, especially with handling malloc() and
* stack growth, so it's easier to just renew(). the only
* catch here is that if we are not granted a renewal, we're
* totally hosed and can only bail out.
*/
if ((lif = attach_lif(kcache->dk_if_name, B_FALSE, &retval)) == NULL) {
dhcpmsg(MSG_ERROR, "dhcp_adopt: unable to attach %s: %d",
kcache->dk_if_name, retval);
goto failure;
}
if ((dsmp = insert_smach(lif, &retval)) == NULL) {
dhcpmsg(MSG_ERROR, "dhcp_adopt: unable to create state "
"machine for %s: %d", kcache->dk_if_name, retval);
goto failure;
}
/*
* If the agent is adopting a lease, then OBP is initially
* searched for a client-id.
*/
dhcpmsg(MSG_DEBUG, "dhcp_adopt: getting /chosen:clientid property");
client_id_len = 0;
if (!get_prom_prop("chosen", "client-id", &dsmp->dsm_cid,
&client_id_len)) {
/*
* a failure occurred trying to acquire the client-id
*/
dhcpmsg(MSG_DEBUG,
"dhcp_adopt: cannot allocate client id for %s",
dsmp->dsm_name);
goto failure;
} else if (dsmp->dsm_hwtype == ARPHRD_IB && dsmp->dsm_cid == NULL) {
/*
* when the interface is infiniband and the agent
* is adopting the lease there must be an OBP
* client-id.
*/
dhcpmsg(MSG_DEBUG, "dhcp_adopt: no /chosen:clientid id for %s",
dsmp->dsm_name);
goto failure;
}
dsmp->dsm_cidlen = client_id_len;
if (set_lif_dhcp(lif, B_TRUE) != DHCP_IPC_SUCCESS)
goto failure;
if (!set_smach_state(dsmp, ADOPTING))
goto failure;
dsmp->dsm_dflags = DHCP_IF_PRIMARY;
/*
* move to BOUND and use the information in our ACK packet.
* adoption will continue after DAD via dhcp_adopt_complete.
*/
if (!dhcp_bound(dsmp, plp)) {
dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot use cached packet");
goto failure;
}
free(kcache);
return (B_TRUE);
failure:
/* Note: no need to free lif; dsmp holds reference */
if (dsmp != NULL)
remove_smach(dsmp);
free(kcache);
free_pkt_entry(plp);
return (B_FALSE);
}
/*
* dhcp_adopt_complete(): completes interface adoption process after kernel
* duplicate address detection (DAD) is done.
*
* input: dhcp_smach_t *: the state machine on which a lease is being adopted
* output: none
*/
void
dhcp_adopt_complete(dhcp_smach_t *dsmp)
{
dhcpmsg(MSG_DEBUG, "dhcp_adopt_complete: completing adoption");
if (async_start(dsmp, DHCP_EXTEND, B_FALSE) == 0) {
dhcpmsg(MSG_CRIT, "dhcp_adopt_complete: async_start failed");
return;
}
if (dhcp_extending(dsmp) == 0) {
dhcpmsg(MSG_CRIT,
"dhcp_adopt_complete: cannot send renew request");
return;
}
if (grandparent != (pid_t)0) {
dhcpmsg(MSG_DEBUG, "adoption complete, signalling parent (%ld)"
" to exit.", grandparent);
(void) kill(grandparent, SIGALRM);
}
}
/*
* get_dhcp_kcache(): fetches the DHCP ACK and interface name from the kernel
*
* input: dhcp_kcache_t **: a dynamically-allocated cache packet
* size_t *: the length of that packet (on return)
* output: int: nonzero on success, zero on failure
*/
static int
get_dhcp_kcache(dhcp_kcache_t **kernel_cachep, size_t *kcache_size)
{
char dummy;
long size;
size = sysinfo(SI_DHCP_CACHE, &dummy, sizeof (dummy));
if (size == -1)
return (0);
*kcache_size = size;
*kernel_cachep = malloc(*kcache_size);
if (*kernel_cachep == NULL)
return (0);
(void) sysinfo(SI_DHCP_CACHE, (caddr_t)*kernel_cachep, size);
return (1);
}
/*
* get_prom_prop(): get the value of the named property on the named node in
* devinfo root.
*
* input: const char *: The name of the node containing the property.
* const char *: The name of the property.
* uchar_t **: The property value, modified iff B_TRUE is returned.
* If no value is found the value is set to NULL.
* uint_t *: The length of the property value
* output: boolean_t: Returns B_TRUE if successful (no problems),
* otherwise B_FALSE.
* note: The memory allocated by this function must be freed by
* the caller. This code is derived from
* usr/src/lib/libwanboot/common/bootinfo_aux.c.
*/
static boolean_t
get_prom_prop(const char *nodename, const char *propname, uchar_t **propvaluep,
uint_t *lenp)
{
di_node_t root_node;
di_node_t node;
di_prom_handle_t phdl = DI_PROM_HANDLE_NIL;
di_prom_prop_t pp;
uchar_t *value = NULL;
unsigned int len = 0;
boolean_t success = B_TRUE;
/*
* locate root node
*/
if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL ||
(phdl = di_prom_init()) == DI_PROM_HANDLE_NIL) {
dhcpmsg(MSG_DEBUG, "get_prom_prop: property root node "
"not found");
goto get_prom_prop_cleanup;
}
/*
* locate nodename within '/'
*/
for (node = di_child_node(root_node);
node != DI_NODE_NIL;
node = di_sibling_node(node)) {
if (strcmp(di_node_name(node), nodename) == 0) {
break;
}
}
if (node == DI_NODE_NIL) {
dhcpmsg(MSG_DEBUG, "get_prom_prop: node not found");
goto get_prom_prop_cleanup;
}
/*
* scan all properties of /nodename for the 'propname' property
*/
for (pp = di_prom_prop_next(phdl, node, DI_PROM_PROP_NIL);
pp != DI_PROM_PROP_NIL;
pp = di_prom_prop_next(phdl, node, pp)) {
dhcpmsg(MSG_DEBUG, "get_prom_prop: property = %s",
di_prom_prop_name(pp));
if (strcmp(propname, di_prom_prop_name(pp)) == 0) {
break;
}
}
if (pp == DI_PROM_PROP_NIL) {
dhcpmsg(MSG_DEBUG, "get_prom_prop: property not found");
goto get_prom_prop_cleanup;
}
/*
* get the property; allocate some memory copy it out
*/
len = di_prom_prop_data(pp, (uchar_t **)&value);
if (value == NULL) {
/*
* property data read problems
*/
success = B_FALSE;
dhcpmsg(MSG_ERR, "get_prom_prop: cannot read property data");
goto get_prom_prop_cleanup;
}
if (propvaluep != NULL) {
/*
* allocate somewhere to copy the property value to
*/
*propvaluep = calloc(len, sizeof (uchar_t));
if (*propvaluep == NULL) {
/*
* allocation problems
*/
success = B_FALSE;
dhcpmsg(MSG_ERR, "get_prom_prop: cannot allocate "
"memory for property value");
goto get_prom_prop_cleanup;
}
/*
* copy data out
*/
(void) memcpy(*propvaluep, value, len);
/*
* copy out the length if a suitable pointer has
* been supplied
*/
if (lenp != NULL) {
*lenp = len;
}
dhcpmsg(MSG_DEBUG, "get_prom_prop: property value "
"length = %d", len);
}
get_prom_prop_cleanup:
if (phdl != DI_PROM_HANDLE_NIL) {
di_prom_fini(phdl);
}
if (root_node != DI_NODE_NIL) {
di_fini(root_node);
}
return (success);
}
|