10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3431Scarlsonj * Common Development and Distribution License (the "License").
6*3431Scarlsonj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*3431Scarlsonj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23*3431Scarlsonj * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <ctype.h>
32*3431Scarlsonj #include <string.h>
330Sstevel@tonic-gate #include <dhcpagent_ipc.h>
340Sstevel@tonic-gate #include <dhcp_inittab.h>
350Sstevel@tonic-gate #include <dhcp_symbol.h>
360Sstevel@tonic-gate
37*3431Scarlsonj #define DHCP_INFO_VENDOR_START_V4 256
38*3431Scarlsonj #define DHCP_INFO_VENDOR_START_V6 65536
390Sstevel@tonic-gate
400Sstevel@tonic-gate static void
usage(const char * program)410Sstevel@tonic-gate usage(const char *program)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate (void) fprintf(stderr,
44*3431Scarlsonj "usage: %s [-c] [-i interface] [-n limit] [-v {4|6}] code\n"
45*3431Scarlsonj " %s [-c] [-i interface] [-n limit] [-v {4|6}] identifier\n",
460Sstevel@tonic-gate program, program);
470Sstevel@tonic-gate
480Sstevel@tonic-gate exit(DHCP_EXIT_BADARGS);
490Sstevel@tonic-gate }
500Sstevel@tonic-gate
510Sstevel@tonic-gate int
main(int argc,char ** argv)520Sstevel@tonic-gate main(int argc, char **argv)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate ssize_t max_lines = -1;
550Sstevel@tonic-gate size_t gran, n_spaces = 0;
560Sstevel@tonic-gate dhcp_optnum_t optnum;
570Sstevel@tonic-gate dhcp_ipc_request_t *request;
580Sstevel@tonic-gate dhcp_ipc_reply_t *reply;
590Sstevel@tonic-gate int c, error, i;
600Sstevel@tonic-gate char *ifname = "";
610Sstevel@tonic-gate char *value, *valuep;
620Sstevel@tonic-gate dhcp_symbol_t *entry;
630Sstevel@tonic-gate DHCP_OPT *opt;
640Sstevel@tonic-gate size_t opt_len;
650Sstevel@tonic-gate boolean_t is_canonical = B_FALSE;
66*3431Scarlsonj long version = 4;
67*3431Scarlsonj boolean_t isv6;
68*3431Scarlsonj uint8_t *valptr;
690Sstevel@tonic-gate
70*3431Scarlsonj while ((c = getopt(argc, argv, "ci:n:v:")) != EOF) {
710Sstevel@tonic-gate
720Sstevel@tonic-gate switch (c) {
730Sstevel@tonic-gate
740Sstevel@tonic-gate case 'c':
750Sstevel@tonic-gate is_canonical = B_TRUE;
760Sstevel@tonic-gate break;
770Sstevel@tonic-gate
780Sstevel@tonic-gate case 'i':
790Sstevel@tonic-gate ifname = optarg;
800Sstevel@tonic-gate break;
810Sstevel@tonic-gate
820Sstevel@tonic-gate case 'n':
830Sstevel@tonic-gate max_lines = strtoul(optarg, NULL, 0);
840Sstevel@tonic-gate break;
850Sstevel@tonic-gate
86*3431Scarlsonj case 'v':
87*3431Scarlsonj version = strtol(optarg, NULL, 0);
88*3431Scarlsonj if (version != 4 && version != 6)
89*3431Scarlsonj usage(argv[0]);
90*3431Scarlsonj break;
91*3431Scarlsonj
920Sstevel@tonic-gate case '?':
930Sstevel@tonic-gate usage(argv[0]);
940Sstevel@tonic-gate
950Sstevel@tonic-gate default:
960Sstevel@tonic-gate break;
970Sstevel@tonic-gate }
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate if (argc - optind != 1)
1010Sstevel@tonic-gate usage(argv[0]);
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * we either have a code or an identifer. if we have a code,
1050Sstevel@tonic-gate * then values over 256 indicate a vendor option. if we have
1060Sstevel@tonic-gate * an identifier, then use inittab_getbyname() to turn the
1070Sstevel@tonic-gate * identifier into a code, then send the request over the wire.
1080Sstevel@tonic-gate */
1090Sstevel@tonic-gate
110*3431Scarlsonj isv6 = (version == 6);
111*3431Scarlsonj
1120Sstevel@tonic-gate if (isalpha(*argv[optind])) {
1130Sstevel@tonic-gate
114*3431Scarlsonj entry = inittab_getbyname(ITAB_CAT_SITE | ITAB_CAT_STANDARD |
115*3431Scarlsonj ITAB_CAT_VENDOR | ITAB_CAT_FIELD |
116*3431Scarlsonj (isv6 ? ITAB_CAT_V6 : 0), ITAB_CONS_INFO,
1170Sstevel@tonic-gate argv[optind]);
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if (entry == NULL) {
1200Sstevel@tonic-gate (void) fprintf(stderr, "%s: unknown identifier `%s'\n",
1210Sstevel@tonic-gate argv[0], argv[optind]);
1220Sstevel@tonic-gate return (DHCP_EXIT_BADARGS);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate optnum.code = entry->ds_code;
1260Sstevel@tonic-gate optnum.category = entry->ds_category;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate } else {
129*3431Scarlsonj ulong_t start;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate optnum.code = strtoul(argv[optind], 0, 0);
132*3431Scarlsonj optnum.category = ITAB_CAT_STANDARD | ITAB_CAT_SITE;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate * sigh. this is a hack, but it's needed for backward
1360Sstevel@tonic-gate * compatibility with the CA dhcpinfo program.
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate
139*3431Scarlsonj start = isv6 ? DHCP_INFO_VENDOR_START_V6 :
140*3431Scarlsonj DHCP_INFO_VENDOR_START_V4;
141*3431Scarlsonj if (optnum.code > start) {
142*3431Scarlsonj optnum.code -= start;
1430Sstevel@tonic-gate optnum.category = ITAB_CAT_VENDOR;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
146*3431Scarlsonj if (isv6)
147*3431Scarlsonj optnum.category |= ITAB_CAT_V6;
148*3431Scarlsonj
1490Sstevel@tonic-gate entry = inittab_getbycode(optnum.category, ITAB_CONS_INFO,
1500Sstevel@tonic-gate optnum.code);
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if (entry == NULL) {
1530Sstevel@tonic-gate (void) fprintf(stderr, "%s: unknown code `%s'\n",
1540Sstevel@tonic-gate argv[0], argv[optind]);
1550Sstevel@tonic-gate return (DHCP_EXIT_BADARGS);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate optnum.category = entry->ds_category;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate optnum.size = entry->ds_max * inittab_type_to_size(entry);
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate * send the request to the agent and reap the reply
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate
166*3431Scarlsonj request = dhcp_ipc_alloc_request(DHCP_GET_TAG | (isv6 ? DHCP_V6 : 0),
167*3431Scarlsonj ifname, &optnum, sizeof (dhcp_optnum_t), DHCP_TYPE_OPTNUM);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate if (request == NULL)
1700Sstevel@tonic-gate return (DHCP_EXIT_SYSTEM);
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate error = dhcp_ipc_make_request(request, &reply, DHCP_IPC_WAIT_DEFAULT);
1730Sstevel@tonic-gate if (error != 0 || reply->return_code != 0) {
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate if (error == 0)
1760Sstevel@tonic-gate error = reply->return_code;
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", argv[0],
1790Sstevel@tonic-gate dhcp_ipc_strerror(error));
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate if (error == DHCP_IPC_E_TIMEOUT)
1820Sstevel@tonic-gate return (DHCP_EXIT_TIMEOUT);
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate return (DHCP_EXIT_FAILURE);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate opt = dhcp_ipc_get_data(reply, &opt_len, NULL);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate * no data means that the client has an ACK but has no information
1910Sstevel@tonic-gate * about the specified option; return success
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate if (opt_len == 0)
1950Sstevel@tonic-gate return (DHCP_EXIT_SUCCESS);
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate /*
1980Sstevel@tonic-gate * check for protocol error
1990Sstevel@tonic-gate */
2000Sstevel@tonic-gate
201*3431Scarlsonj if (isv6) {
202*3431Scarlsonj dhcpv6_option_t d6o;
203*3431Scarlsonj
204*3431Scarlsonj if (opt_len < sizeof (d6o))
205*3431Scarlsonj return (DHCP_EXIT_FAILURE);
206*3431Scarlsonj (void) memcpy(&d6o, opt, sizeof (d6o));
207*3431Scarlsonj if (opt_len != ntohs(d6o.d6o_len) + sizeof (d6o))
208*3431Scarlsonj return (DHCP_EXIT_FAILURE);
209*3431Scarlsonj valptr = (uint8_t *)opt + sizeof (d6o);
210*3431Scarlsonj opt_len -= sizeof (d6o);
211*3431Scarlsonj } else {
212*3431Scarlsonj if (opt_len < 2 || (opt_len - 2 != opt->len))
213*3431Scarlsonj return (DHCP_EXIT_FAILURE);
214*3431Scarlsonj opt_len -= 2;
215*3431Scarlsonj valptr = opt->value;
216*3431Scarlsonj }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if (is_canonical) {
2190Sstevel@tonic-gate
220*3431Scarlsonj value = malloc(opt_len * (sizeof ("0xNN") + 1));
2210Sstevel@tonic-gate if (value == NULL) {
2220Sstevel@tonic-gate (void) fprintf(stderr, "%s: out of memory\n", argv[0]);
2230Sstevel@tonic-gate return (DHCP_EXIT_FAILURE);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
226*3431Scarlsonj for (i = 0, valuep = value; i < opt_len; i++)
227*3431Scarlsonj valuep += sprintf(valuep, "0x%02X ", valptr[i]);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate valuep[-1] = '\0';
2300Sstevel@tonic-gate gran = 1;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate } else {
2330Sstevel@tonic-gate
234*3431Scarlsonj value = inittab_decode(entry, valptr, opt_len, B_TRUE);
2350Sstevel@tonic-gate if (value == NULL) {
2360Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot decode agent's "
2370Sstevel@tonic-gate "reply\n", argv[0]);
2380Sstevel@tonic-gate return (DHCP_EXIT_FAILURE);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate gran = entry->ds_gran;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate * now display `gran' items per line, printing at most `max_lines'.
2460Sstevel@tonic-gate */
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate for (i = 0; value[i] != '\0'; i++) {
2490Sstevel@tonic-gate if (value[i] == ' ') {
2500Sstevel@tonic-gate if ((++n_spaces % gran) == 0) {
2510Sstevel@tonic-gate value[i] = '\n';
2520Sstevel@tonic-gate if (max_lines != -1 && --max_lines == 0) {
2530Sstevel@tonic-gate value[i] = '\0';
2540Sstevel@tonic-gate break;
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate (void) printf("%s\n", value);
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate return (DHCP_EXIT_SUCCESS);
2630Sstevel@tonic-gate }
264