summaryrefslogtreecommitdiff
path: root/usr/src/lib/libwanboot/common/bootinfo_aux.c
blob: a72561da57086afc2f7d75ee2d955f4dfe1be378 (plain)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <stdlib.h>
#include <dhcp_impl.h>
#include <netinet/inetutil.h>
#include <sys/systeminfo.h>
#include <netinet/in.h>
#include <strings.h>
#include <net/if.h>
#include <libdevinfo.h>
#include <sys/isa_defs.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <alloca.h>
#include <stdio.h>
#include <sys/sockio.h>
#include <sys/statvfs.h>
#include <sys/utsname.h>
#include <bootinfo.h>
#include <bootinfo_aux.h>

#define	MAXIFS	256	/* default max number of interfaces */

/*
 * Callback structure used when walking the device tree.
 */
typedef struct {
	char		*cb_path;	/* device path we want to match */
	di_node_t	cb_node;	/* found leaf node of device path */
} cb_t;

/*
 * Handles on devinfo stuff.
 */
static di_node_t	root_node = DI_NODE_NIL;
static di_prom_handle_t	phdl = DI_PROM_HANDLE_NIL;

/*
 * Root filesystem type string.
 */
static char *rootfs_type = NULL;

/*
 * Handles on DHCP's packet list and interface-name.
 */
static PKT_LIST	*dhcp_pl = NULL;
static char	dhcp_ifn[IFNAMSIZ + 1];

/*
 * Deallocate dhcp_pl.
 */
static void
dhcp_info_end(void)
{
	if (dhcp_pl != NULL) {
		free(dhcp_pl->pkt);
		free(dhcp_pl);
	}
	dhcp_pl = NULL;
	dhcp_ifn[0] = '\0';
}

/*
 * Determine whether the kernel has a cached DHCP ACK, and if so
 * initialize dhcp_pl and dhcp_ifn.
 */
static boolean_t
dhcp_info_init(void)
{
	boolean_t	ret = B_FALSE;
	char		dummy;
	char		*dhcack = NULL;
	long		dhcacksz;
	char		*ackp;

	/*
	 * See whether the kernel has a cached DHCP ACK, and if so get it.
	 * If there is no DHCP ACK, then the returned length is equal to
	 * the size of an empty string.
	 */
	if ((dhcacksz = sysinfo(SI_DHCP_CACHE, &dummy,
	    sizeof (dummy))) == sizeof ("")) {
		return (B_TRUE);
	}
	if ((dhcack = malloc(dhcacksz)) == NULL) {
		goto cleanup;
	}
	if ((dhcp_pl = calloc(1, sizeof (PKT_LIST))) == NULL) {
		goto cleanup;
	}
	(void) sysinfo(SI_DHCP_CACHE, (caddr_t)dhcack, dhcacksz);

	/*
	 * The first IFNAMSIZ bytes are reserved for the interface name;
	 * the ACK follows.
	 */
	ackp = &dhcack[IFNAMSIZ];

	/*
	 * Convert and scan the options.
	 */
	dhcp_pl->len = strlen(ackp) / 2;
	if ((dhcp_pl->pkt = malloc(dhcp_pl->len)) == NULL) {
		goto cleanup;
	}
	if (hexascii_to_octet(ackp, dhcp_pl->len * 2,
	    dhcp_pl->pkt, &dhcp_pl->len) != 0) {
		goto cleanup;
	}
	if (dhcp_options_scan(dhcp_pl, B_TRUE) != 0) {
		goto cleanup;
	}

	/*
	 * Set the interface-name.
	 */
	(void) strlcpy(dhcp_ifn, dhcack, sizeof (dhcp_ifn));

	ret = B_TRUE;
cleanup:
	if (!ret) {
		dhcp_info_end();
	}
	if (dhcack != NULL) {
		free(dhcack);
	}

	return (ret);
}

/*
 * Deallocate devinfo stuff.
 */
static void
destroy_snapshot(void)
{
	if (phdl != DI_PROM_HANDLE_NIL) {
		di_prom_fini(phdl);
	}
	phdl = DI_PROM_HANDLE_NIL;

	if (root_node != DI_NODE_NIL) {
		di_fini(root_node);
	}
	root_node = DI_NODE_NIL;
}

/*
 * Take a snapshot of the device tree, i.e. get a devinfo handle and
 * a PROM handle.
 */
static boolean_t
snapshot_devtree(void)
{
	/*
	 * Deallocate any existing devinfo stuff first.
	 */
	destroy_snapshot();

	if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL ||
	    (phdl = di_prom_init()) == DI_PROM_HANDLE_NIL) {
		destroy_snapshot();
		return (B_FALSE);
	}

	return (B_TRUE);
}

/*
 * Get the value of the named property on the named node in root.
 */
static char *
get_prop(const char *nodename, const char *propname, size_t *lenp)
{
	di_node_t		node;
	di_prom_prop_t		pp;
	char			*val = NULL;
	int			len;

	/*
	 * 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) {
		return (NULL);
	}

	/*
	 * 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)) {
		if (strcmp(propname, di_prom_prop_name(pp)) == 0) {
			break;
		}
	}
	if (pp == DI_PROM_PROP_NIL) {
		return (NULL);
	}

	/*
	 * Found the property; copy out its length and return its value.
	 */
	len = di_prom_prop_data(pp, (uchar_t **)&val);
	if (lenp != NULL) {
		*lenp = len;
	}
	return (val);
}

/*
 * Strip any trailing arguments from a device path.
 * Returned memory must be freed by caller.
 */
static char *
strip_args(char *path, size_t len)
{
	char	*stripped_path = NULL;

	if (path != NULL && len != 0 &&
	    (stripped_path = calloc(len + 1, sizeof (char))) != NULL) {
		char	*p;

		(void) memcpy(stripped_path, path, len);
		if ((p = strchr(stripped_path, ':')) != NULL) {
			*p = '\0';
		}
	}
	return (stripped_path);
}

/*
 * Return the "bootpath" property (sans arguments) from /chosen.
 * Returned memory must be freed by caller.
 */
static char *
get_bootpath(void)
{
	char	*path;
	size_t	len;

	path = get_prop("chosen", "bootpath", &len);
	return (strip_args(path, len));
}

/*
 * Return the "net" property (sans arguments) from /aliases.
 * Returned memory must be freed by caller.
 */
static char *
get_netalias(void)
{
	char	*path;
	size_t	len;

	path = get_prop("aliases", "net", &len);
	return (strip_args(path, len));
}

/*
 * Callback used by path2node().
 */
static int
p2n_cb(di_node_t node, void *arg)
{
	int	ret = DI_WALK_CONTINUE;
	cb_t	*cbp = arg;
	char	*phys_path = di_devfs_path(node);

	if (strcmp(cbp->cb_path, phys_path) == 0) {
		cbp->cb_node = node;
		ret = DI_WALK_TERMINATE;
	}
	di_devfs_path_free(phys_path);

	return (ret);
}

/*
 * Map a device path to its matching di_node_t.
 */
static di_node_t
path2node(char *path)
{
	cb_t	cb;

	cb.cb_path = path;
	cb.cb_node = DI_NODE_NIL;

	(void) di_walk_node(root_node, DI_WALK_CLDFIRST, &cb, p2n_cb);

	return (cb.cb_node);
}

/*
 * Check whether node corresponds to a network device.
 */
static boolean_t
is_network_device(di_node_t node)
{
	char		*type;

	return (di_prom_prop_lookup_strings(phdl, node,
	    "device_type", &type) > 0 && strcmp(type, "network") == 0);
}

/*
 * Initialise bootmisc with the rootfs-type.
 */
static boolean_t
rootfs_type_init(void)
{
	static struct statvfs	vfs;

	if (statvfs("/", &vfs) >= 0) {
		if (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) == 0) {
			vfs.f_basetype[sizeof ("nfs") - 1] = '\0';
		}
		rootfs_type = vfs.f_basetype;
	}

	return (rootfs_type != NULL && bi_put_bootmisc(BI_ROOTFS_TYPE,
	    rootfs_type, strlen(rootfs_type) + 1));
}

/*
 * Initialise bootmisc with the interface-name of the primary network device,
 * and the net-config-strategy employed in configuring that device.
 */
static boolean_t
netif_init(char *ifn, char *ncs)
{
	return (bi_put_bootmisc(BI_INTERFACE_NAME, ifn, strlen(ifn) + 1) &&
	    bi_put_bootmisc(BI_NET_CONFIG_STRATEGY, ncs, strlen(ncs) + 1));
}

/*
 * Determine whether the interface was configured manually.
 */
static boolean_t
manual_if_init(void)
{
	boolean_t	ret = B_FALSE;
	char		*ncs;
	char		*devpath;
	di_node_t	node;
	int		instance;
	char		*drvname;
	char		ifname[IFNAMSIZ + 1];

	/*
	 * If net-config-strategy isn't "manual", don't go any further.
	 */
	if ((ncs = get_prop("chosen", BI_NET_CONFIG_STRATEGY, NULL)) == NULL ||
	    strcmp(ncs, "manual") != 0) {
		return (B_FALSE);
	}

	/*
	 * First check the 'bootpath' property of /chosen to see whether
	 * it specifies the path of a network device; if so, use this.
	 */
	if ((devpath = get_bootpath()) == NULL ||
	    (node = path2node(devpath)) == DI_NODE_NIL ||
	    !is_network_device(node)) {
		/*
		 * Must have been booted from CD-ROM or disk; attempt to
		 * use the path defined by the 'net' property of /aliases.
		 */
		free(devpath);
		if ((devpath = get_netalias()) == NULL ||
		    (node = path2node(devpath)) == DI_NODE_NIL ||
		    !is_network_device(node)) {
			goto cleanup;
		}
	}

	/*
	 * Get the driver name and instance number of this node.
	 * We may have to load the driver.
	 */
	if ((drvname = di_driver_name(node)) == NULL) {
		goto cleanup;
	}
	if ((instance = di_instance(node)) == -1) {
		di_node_t	tmp;

		/*
		 * Attempt to load the driver, create a new snapshot of the
		 * (possibly changed) device tree and re-compute our node.
		 */
		if ((tmp = di_init_driver(drvname, 0)) != DI_NODE_NIL) {
			di_fini(tmp);

			if (!snapshot_devtree() ||
			    (node = path2node(devpath)) == DI_NODE_NIL) {
				goto cleanup;
			}
		}
		instance = di_instance(node);
	}

	/*
	 * Construct the interface name.
	 */
	if (instance == -1) {
		(void) snprintf(ifname, sizeof (ifname),
		    "%s", di_driver_name(node));
	} else {
		(void) snprintf(ifname, sizeof (ifname),
		    "%s%d", di_driver_name(node), instance);
	}

	ret = netif_init(ifname, "manual");
cleanup:
	free(devpath);
	return (ret);
}

/*
 * Determine whether the interface was configured via DHCP.
 */
static boolean_t
dhcp_if_init(void)
{
	return (strlen(dhcp_ifn) != 0 && netif_init(dhcp_ifn, "dhcp"));
}

static boolean_t
bootmisc_init(void)
{
	return (rootfs_type_init() &&
	    (manual_if_init() || dhcp_if_init()));
}


/*
 * Functions dealing with bootinfo initialization/cleanup.
 */
boolean_t
bi_init_bootinfo(void)
{
	if (snapshot_devtree() && dhcp_info_init() && bootmisc_init()) {
		return (B_TRUE);
	}
	bi_end_bootinfo();
	return (B_FALSE);
}

void
bi_end_bootinfo(void)
{
	destroy_snapshot();
	dhcp_info_end();
}

/*
 * Function dealing with /chosen data.
 */
boolean_t
bi_get_chosen_prop(const char *name, void *valbuf, size_t *vallenp)
{
	char		*val;
	size_t		buflen = *vallenp;

	if ((val = get_prop("chosen", name, vallenp)) == NULL) {
		return (B_FALSE);
	}
	if (*vallenp <= buflen) {
		(void) memcpy(valbuf, val, *vallenp);
	}

	return (B_TRUE);
}

/*
 * Function dealing with DHCP data.
 */
boolean_t
bi_get_dhcp_info(uchar_t optcat, uint16_t optcode, uint16_t optsize,
    void *valbuf, size_t *vallenp)
{
	return (dhcp_getinfo_pl(dhcp_pl,
	    optcat, optcode, optsize, valbuf, vallenp));
}