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*5577Ssangeeta * Common Development and Distribution License (the "License").
6*5577Ssangeeta * 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*5577Ssangeeta * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * 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 /* EXPORT DELETE START */
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/salib.h>
320Sstevel@tonic-gate #include <sys/promif.h>
330Sstevel@tonic-gate #include <sys/wanboot_impl.h>
340Sstevel@tonic-gate #include <netinet/in.h>
350Sstevel@tonic-gate #include <parseURL.h>
360Sstevel@tonic-gate #include <bootlog.h>
370Sstevel@tonic-gate #include <sys/socket.h>
380Sstevel@tonic-gate #include <netinet/inetutil.h>
390Sstevel@tonic-gate #include <netinet/dhcp.h>
400Sstevel@tonic-gate #include <dhcp_impl.h>
410Sstevel@tonic-gate #include <lib/inet/mac.h>
420Sstevel@tonic-gate #include <lib/inet/ipv4.h>
430Sstevel@tonic-gate #include <lib/inet/dhcpv4.h>
440Sstevel@tonic-gate #include <lib/sock/sock_test.h>
450Sstevel@tonic-gate #include <sys/sunos_dhcp_class.h>
460Sstevel@tonic-gate #include <aes.h>
470Sstevel@tonic-gate #include <des3.h>
480Sstevel@tonic-gate #include <hmac_sha1.h>
490Sstevel@tonic-gate #include <netdb.h>
500Sstevel@tonic-gate #include <wanboot_conf.h>
510Sstevel@tonic-gate #include <bootinfo.h>
520Sstevel@tonic-gate /* EXPORT DELETE END */
530Sstevel@tonic-gate
540Sstevel@tonic-gate #include "wbcli.h"
550Sstevel@tonic-gate
560Sstevel@tonic-gate /* EXPORT DELETE START */
570Sstevel@tonic-gate
580Sstevel@tonic-gate #define skipspace(p) while (isspace(*(p))) ++p
590Sstevel@tonic-gate
600Sstevel@tonic-gate #define skiptext(p) while (*(p) != '\0' && !isspace(*(p)) && \
610Sstevel@tonic-gate *(p) != '=' && *(p) != ',') ++p
620Sstevel@tonic-gate
630Sstevel@tonic-gate #define PROMPT "boot> "
640Sstevel@tonic-gate #define TEST_PROMPT "boot-test> "
650Sstevel@tonic-gate
660Sstevel@tonic-gate #define CLI_SET 0
670Sstevel@tonic-gate #define CLI_FAIL (-1)
680Sstevel@tonic-gate #define CLI_EXIT (-2)
690Sstevel@tonic-gate #define CLI_CONT (-3)
700Sstevel@tonic-gate
710Sstevel@tonic-gate #define CLF_CMD 0x00000001 /* builtin command */
720Sstevel@tonic-gate #define CLF_ARG 0x00000002 /* boot argument directive */
730Sstevel@tonic-gate
740Sstevel@tonic-gate #define CLF_IF 0x00000100 /* interface parameter */
750Sstevel@tonic-gate #define CLF_BM 0x00000200 /* bootmisc parameter */
760Sstevel@tonic-gate
770Sstevel@tonic-gate #define CLF_VALSET 0x00010000 /* value set, may be null */
780Sstevel@tonic-gate #define CLF_HIDDEN 0x00020000 /* don't show its value (key) */
790Sstevel@tonic-gate #define CLF_VALMOD 0x00040000 /* value modified by the user */
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * Macros for use in managing the flags in the cli_list[].
830Sstevel@tonic-gate * The conventions we follow are:
840Sstevel@tonic-gate *
850Sstevel@tonic-gate * CLF_VALSET is cleared if a value is removed from varptr
860Sstevel@tonic-gate * CLF_VALSET is set if a value has been placed in varptr
870Sstevel@tonic-gate * (that value need not be vetted)
880Sstevel@tonic-gate * CLF_HIDDEN is set if a value must not be exposed to the user
890Sstevel@tonic-gate * CLF_HIDDEN is cleared if a value can be exposed to the user
900Sstevel@tonic-gate * CLF_VALMOD is cleared if a value in varptr has not been modified
910Sstevel@tonic-gate * CLF_VALMOD is set if a value in varptr has been modified by
920Sstevel@tonic-gate * the user
930Sstevel@tonic-gate */
940Sstevel@tonic-gate #ifdef DEBUG
950Sstevel@tonic-gate #define CLF_SETVAL(var) { \
960Sstevel@tonic-gate (((var)->flags) |= CLF_VALSET); \
970Sstevel@tonic-gate printf("set %s\n", var->varname);\
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate #define CLF_ISSET(var) (printf("%s\n", \
1010Sstevel@tonic-gate (((var)->flags) & CLF_VALSET) != 0 \
1020Sstevel@tonic-gate ? "is set" : "not set"), \
1030Sstevel@tonic-gate ((((var)->flags) & CLF_VALSET) != 0))
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate #define CLF_CLRHIDDEN(var) { \
1060Sstevel@tonic-gate (((var)->flags) &= ~CLF_HIDDEN); \
1070Sstevel@tonic-gate printf("unhide %s\n", var->varname); \
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate #define CLF_ISHIDDEN(var) (printf("%s\n", \
1110Sstevel@tonic-gate (((var)->flags) & CLF_HIDDEN) != 0 \
1120Sstevel@tonic-gate ? "is hidden" : "not hidden"), \
1130Sstevel@tonic-gate ((((var)->flags) & CLF_HIDDEN) != 0))
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate #define CLF_MODVAL(var) { \
1160Sstevel@tonic-gate (((var)->flags) |= \
1170Sstevel@tonic-gate (CLF_VALMOD | CLF_VALSET)); \
1180Sstevel@tonic-gate printf("modified %s\n", var->varname);\
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate #define CLF_ISMOD(var) (printf("%s\n", \
1220Sstevel@tonic-gate (((var)->flags) & CLF_VALMOD) != 0 \
1230Sstevel@tonic-gate ? "is set" : "not set"), \
1240Sstevel@tonic-gate ((((var)->flags) & CLF_VALMOD) != 0))
1250Sstevel@tonic-gate #else /* DEBUG */
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate #define CLF_SETVAL(var) (((var)->flags) |= CLF_VALSET)
1280Sstevel@tonic-gate #define CLF_ISSET(var) ((((var)->flags) & CLF_VALSET) != 0)
1290Sstevel@tonic-gate #define CLF_CLRHIDDEN(var) (((var)->flags) &= ~CLF_HIDDEN)
1300Sstevel@tonic-gate #define CLF_ISHIDDEN(var) ((((var)->flags) & CLF_HIDDEN) != 0)
1310Sstevel@tonic-gate #define CLF_MODVAL(var) (((var)->flags) |= (CLF_VALMOD | CLF_VALSET))
1320Sstevel@tonic-gate #define CLF_ISMOD(var) ((((var)->flags) & CLF_VALMOD) != 0)
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate #endif /* DEBUG */
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * The width of the widest varname below - currently "subnet_mask".
1380Sstevel@tonic-gate */
1390Sstevel@tonic-gate #define VAR_MAXWIDTH strlen(BI_SUBNET_MASK)
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate struct cli_ent;
1420Sstevel@tonic-gate typedef int claction_t(struct cli_ent *, char *, boolean_t);
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate typedef struct cli_ent {
1450Sstevel@tonic-gate char *varname;
1460Sstevel@tonic-gate claction_t *action;
1470Sstevel@tonic-gate int flags;
1480Sstevel@tonic-gate void *varptr;
1490Sstevel@tonic-gate uint_t varlen;
1500Sstevel@tonic-gate uint_t varmax;
1510Sstevel@tonic-gate } cli_ent_t;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate static cli_ent_t *find_cli_ent(char *varstr);
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate static char cmdbuf[2048]; /* interpreter buffer */
1560Sstevel@tonic-gate static char hostip[INET_ADDRSTRLEN];
1570Sstevel@tonic-gate static char subnet[INET_ADDRSTRLEN];
1580Sstevel@tonic-gate static char router[INET_ADDRSTRLEN];
1590Sstevel@tonic-gate static char hostname[MAXHOSTNAMELEN];
1600Sstevel@tonic-gate static char httpproxy[INET_ADDRSTRLEN + 5]; /* a.b.c.d:p */
1610Sstevel@tonic-gate static char bootserverURL[URL_MAX_STRLEN + 1];
1620Sstevel@tonic-gate static unsigned char clientid[WB_MAX_CID_LEN];
1630Sstevel@tonic-gate static unsigned char aeskey[AES_128_KEY_SIZE];
1640Sstevel@tonic-gate static unsigned char des3key[DES3_KEY_SIZE];
1650Sstevel@tonic-gate static unsigned char sha1key[WANBOOT_HMAC_KEY_SIZE];
1660Sstevel@tonic-gate static boolean_t args_specified_prompt = B_FALSE;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate extern bc_handle_t bc_handle;
1690Sstevel@tonic-gate extern int getchar(void);
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate static claction_t clcid, clkey, clip, clstr, clurl, clhp;
1720Sstevel@tonic-gate static claction_t clhelp, cllist, clprompt, cldhcp, cltest, clgo, clexit;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate static cli_ent_t cli_list[] = {
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate * Commands/bootargs:
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate { "test", cltest, CLF_ARG,
1790Sstevel@tonic-gate NULL, 0, 0 },
1800Sstevel@tonic-gate { "dhcp", cldhcp, CLF_ARG,
1810Sstevel@tonic-gate NULL, 0, 0 },
1820Sstevel@tonic-gate { "prompt", clprompt, CLF_CMD | CLF_ARG,
1830Sstevel@tonic-gate NULL, 0, 0 },
1840Sstevel@tonic-gate { "list", cllist, CLF_CMD,
1850Sstevel@tonic-gate NULL, 0, 0 },
1860Sstevel@tonic-gate { "help", clhelp, CLF_CMD,
1870Sstevel@tonic-gate NULL, 0, 0 },
1880Sstevel@tonic-gate { "go", clgo, CLF_CMD,
1890Sstevel@tonic-gate NULL, 0, 0 },
1900Sstevel@tonic-gate { "exit", clexit, CLF_CMD,
1910Sstevel@tonic-gate NULL, 0, 0 },
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate * Interface:
1950Sstevel@tonic-gate */
1960Sstevel@tonic-gate { BI_HOST_IP, clip, CLF_IF,
1970Sstevel@tonic-gate hostip, 0, sizeof (hostip) },
1980Sstevel@tonic-gate { BI_SUBNET_MASK, clip, CLF_IF,
1990Sstevel@tonic-gate subnet, 0, sizeof (subnet) },
2000Sstevel@tonic-gate { BI_ROUTER_IP, clip, CLF_IF,
2010Sstevel@tonic-gate router, 0, sizeof (router) },
2020Sstevel@tonic-gate { BI_HOSTNAME, clstr, CLF_IF,
2030Sstevel@tonic-gate hostname, 0, sizeof (hostname) },
2040Sstevel@tonic-gate { BI_HTTP_PROXY, clhp, CLF_IF,
2050Sstevel@tonic-gate httpproxy, 0, sizeof (httpproxy) },
2060Sstevel@tonic-gate { BI_CLIENT_ID, clcid, CLF_IF,
2070Sstevel@tonic-gate clientid, 0, sizeof (clientid) },
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate * Bootmisc:
2110Sstevel@tonic-gate */
2120Sstevel@tonic-gate { BI_AES_KEY, clkey, CLF_BM | CLF_HIDDEN,
2130Sstevel@tonic-gate aeskey, 0, sizeof (aeskey) },
2140Sstevel@tonic-gate { BI_3DES_KEY, clkey, CLF_BM | CLF_HIDDEN,
2150Sstevel@tonic-gate des3key, 0, sizeof (des3key) },
2160Sstevel@tonic-gate { BI_SHA1_KEY, clkey, CLF_BM | CLF_HIDDEN,
2170Sstevel@tonic-gate sha1key, 0, sizeof (sha1key) },
2180Sstevel@tonic-gate { BI_BOOTSERVER, clurl, CLF_BM,
2190Sstevel@tonic-gate bootserverURL, 0, sizeof (bootserverURL) },
2200Sstevel@tonic-gate };
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate static int num_cli_ent = (sizeof (cli_list) / sizeof (cli_ent_t));
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate * Fetch a line from the user, handling backspace appropriately.
2260Sstevel@tonic-gate */
2270Sstevel@tonic-gate static int
editline(char * buf,int count)2280Sstevel@tonic-gate editline(char *buf, int count)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate int i = 0;
2310Sstevel@tonic-gate char c;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate while (i < count - 1) {
2340Sstevel@tonic-gate c = getchar();
2350Sstevel@tonic-gate if (c == '\n') {
2360Sstevel@tonic-gate break;
2370Sstevel@tonic-gate } else if (c == '\b') {
2380Sstevel@tonic-gate /* Clear for backspace. */
2390Sstevel@tonic-gate if (i > 0)
2400Sstevel@tonic-gate i--;
2410Sstevel@tonic-gate continue;
2420Sstevel@tonic-gate } else {
2430Sstevel@tonic-gate buf[i++] = c;
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate buf[i] = '\0';
2470Sstevel@tonic-gate return (i);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate * Assign a client-id to cliptr, or output cliptr's value as a client-id.
2520Sstevel@tonic-gate * On assignment the value is specified in valstr, either in hexascii or
2530Sstevel@tonic-gate * as a quoted string; on output its value is printed in hexascii.
2540Sstevel@tonic-gate */
2550Sstevel@tonic-gate static int
clcid(cli_ent_t * cliptr,char * valstr,boolean_t out)2560Sstevel@tonic-gate clcid(cli_ent_t *cliptr, char *valstr, boolean_t out)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate uint_t len, vmax;
2590Sstevel@tonic-gate boolean_t hexascii = B_TRUE;
2600Sstevel@tonic-gate char buffer[2 * WB_MAX_CID_LEN + 1];
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate if (out) {
2630Sstevel@tonic-gate len = cliptr->varlen * 2 + 1;
2640Sstevel@tonic-gate (void) octet_to_hexascii(cliptr->varptr, cliptr->varlen,
2650Sstevel@tonic-gate buffer, &len);
2660Sstevel@tonic-gate printf("%s", buffer);
2670Sstevel@tonic-gate return (CLI_CONT);
2680Sstevel@tonic-gate } else {
2690Sstevel@tonic-gate len = strlen(valstr);
2700Sstevel@tonic-gate vmax = cliptr->varmax - 1; /* space for the prefix */
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate * Check whether the value is a quoted string; if so, strip
2740Sstevel@tonic-gate * the quotes and note that it's not in hexascii.
2750Sstevel@tonic-gate */
2760Sstevel@tonic-gate if ((valstr[0] == '"' || valstr[0] == '\'') &&
2770Sstevel@tonic-gate valstr[len-1] == valstr[0]) {
2780Sstevel@tonic-gate hexascii = B_FALSE;
2790Sstevel@tonic-gate ++valstr;
2800Sstevel@tonic-gate len -= 2;
2810Sstevel@tonic-gate valstr[len] = '\0';
2820Sstevel@tonic-gate } else {
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate * If the value contains any non-hex digits assume
2850Sstevel@tonic-gate * that it's not in hexascii.
2860Sstevel@tonic-gate */
2870Sstevel@tonic-gate char *p;
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate for (p = valstr; *p != '\0'; ++p) {
2900Sstevel@tonic-gate if (!isxdigit(*p)) {
2910Sstevel@tonic-gate hexascii = B_FALSE;
2920Sstevel@tonic-gate break;
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate if (hexascii) {
2980Sstevel@tonic-gate if (len > vmax * 2 ||
2990Sstevel@tonic-gate hexascii_to_octet(valstr, len,
3000Sstevel@tonic-gate (char *)(cliptr->varptr), &vmax) != 0) {
3010Sstevel@tonic-gate return (CLI_FAIL);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate cliptr->varlen = vmax;
3040Sstevel@tonic-gate } else {
3050Sstevel@tonic-gate if (len > vmax) {
3060Sstevel@tonic-gate return (CLI_FAIL);
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate bcopy(valstr, cliptr->varptr, len);
3090Sstevel@tonic-gate cliptr->varlen = len;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate return (CLI_SET);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate * Assign a key to cliptr, or output cliptr's value as a key.
3180Sstevel@tonic-gate * On assignment the value is specified in valstr in hexascii;
3190Sstevel@tonic-gate * on output its value is printed in hexascii, provided the key
3200Sstevel@tonic-gate * was entered at the interpreter (not obtained from OBP and
3210Sstevel@tonic-gate * thus hidden).
3220Sstevel@tonic-gate */
3230Sstevel@tonic-gate static int
clkey(cli_ent_t * cliptr,char * valstr,boolean_t out)3240Sstevel@tonic-gate clkey(cli_ent_t *cliptr, char *valstr, boolean_t out)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate uint_t len, vmax;
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate if (out) {
3290Sstevel@tonic-gate char buffer[2 * WANBOOT_MAXKEYLEN + 1];
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate if (!CLF_ISHIDDEN(cliptr)) {
3320Sstevel@tonic-gate len = cliptr->varlen * 2 + 1;
3330Sstevel@tonic-gate (void) octet_to_hexascii(cliptr->varptr,
3340Sstevel@tonic-gate cliptr->varlen, buffer, &len);
3350Sstevel@tonic-gate printf("%s", buffer);
3360Sstevel@tonic-gate } else {
3370Sstevel@tonic-gate printf("*HIDDEN*");
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate return (CLI_CONT);
3400Sstevel@tonic-gate } else {
3410Sstevel@tonic-gate len = strlen(valstr);
3420Sstevel@tonic-gate vmax = cliptr->varmax;
3430Sstevel@tonic-gate if (len != vmax * 2 || hexascii_to_octet(valstr, len,
3440Sstevel@tonic-gate cliptr->varptr, &vmax) != 0) {
3450Sstevel@tonic-gate return (CLI_FAIL);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate cliptr->varlen = vmax;
3480Sstevel@tonic-gate CLF_CLRHIDDEN(cliptr);
3490Sstevel@tonic-gate return (CLI_SET);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate * Assign an IP address to cliptr, or output cliptr's value as an
3550Sstevel@tonic-gate * IP address. On assignment the value is specified in valstr in
3560Sstevel@tonic-gate * dotted-decimal format; on output its value is printed in dotted-
3570Sstevel@tonic-gate * decimal format.
3580Sstevel@tonic-gate */
3590Sstevel@tonic-gate static int
clip(cli_ent_t * cliptr,char * valstr,boolean_t out)3600Sstevel@tonic-gate clip(cli_ent_t *cliptr, char *valstr, boolean_t out)
3610Sstevel@tonic-gate {
3620Sstevel@tonic-gate uint_t len;
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate if (out) {
3650Sstevel@tonic-gate printf("%s", (char *)cliptr->varptr);
3660Sstevel@tonic-gate return (CLI_CONT);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate if (inet_addr(valstr) == (in_addr_t)-1 ||
3700Sstevel@tonic-gate (len = strlen(valstr)) >= cliptr->varmax) {
3710Sstevel@tonic-gate return (CLI_FAIL);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate (void) strcpy(cliptr->varptr, valstr);
3750Sstevel@tonic-gate cliptr->varlen = len + 1;
3760Sstevel@tonic-gate return (CLI_SET);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate * Assign an arbitrary string to cliptr, or output cliptr's value as a string.
3810Sstevel@tonic-gate */
3820Sstevel@tonic-gate static int
clstr(cli_ent_t * cliptr,char * valstr,boolean_t out)3830Sstevel@tonic-gate clstr(cli_ent_t *cliptr, char *valstr, boolean_t out)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate uint_t len;
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate if (out) {
3880Sstevel@tonic-gate printf("%s", (char *)cliptr->varptr);
3890Sstevel@tonic-gate return (CLI_CONT);
3900Sstevel@tonic-gate } else {
3910Sstevel@tonic-gate if ((len = strlen(valstr)) >= cliptr->varmax) {
3920Sstevel@tonic-gate return (CLI_FAIL);
3930Sstevel@tonic-gate } else {
3940Sstevel@tonic-gate (void) strcpy(cliptr->varptr, valstr);
3950Sstevel@tonic-gate cliptr->varlen = len + 1;
3960Sstevel@tonic-gate return (CLI_SET);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate * Assign a URL to cliptr (having verified the format), or output cliptr's
4030Sstevel@tonic-gate * value as a URL. The host must be specified in dotted-decimal, and the
4040Sstevel@tonic-gate * scheme must not be https.
4050Sstevel@tonic-gate */
4060Sstevel@tonic-gate static int
clurl(cli_ent_t * cliptr,char * valstr,boolean_t out)4070Sstevel@tonic-gate clurl(cli_ent_t *cliptr, char *valstr, boolean_t out)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate url_t u;
4100Sstevel@tonic-gate uint_t len;
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate if (out) {
4130Sstevel@tonic-gate printf("%s", (char *)cliptr->varptr);
4140Sstevel@tonic-gate return (CLI_CONT);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate if (url_parse(valstr, &u) != URL_PARSE_SUCCESS ||
4180Sstevel@tonic-gate u.https || inet_addr(u.hport.hostname) == (in_addr_t)-1 ||
4190Sstevel@tonic-gate (len = strlen(valstr)) >= cliptr->varmax) {
4200Sstevel@tonic-gate return (CLI_FAIL);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate (void) strcpy(cliptr->varptr, valstr);
4240Sstevel@tonic-gate cliptr->varlen = len + 1;
4250Sstevel@tonic-gate return (CLI_SET);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate * Assign a hostport to cliptr (having verified the format), or output cliptr's
4300Sstevel@tonic-gate * value as a hostport. The host must be specified in dotted-decimal.
4310Sstevel@tonic-gate */
4320Sstevel@tonic-gate static int
clhp(cli_ent_t * cliptr,char * valstr,boolean_t out)4330Sstevel@tonic-gate clhp(cli_ent_t *cliptr, char *valstr, boolean_t out)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate url_hport_t u;
4360Sstevel@tonic-gate uint_t len;
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate if (out) {
4390Sstevel@tonic-gate printf("%s", (char *)cliptr->varptr);
4400Sstevel@tonic-gate return (CLI_CONT);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate if (url_parse_hostport(valstr, &u, URL_DFLT_PROXY_PORT) !=
4440Sstevel@tonic-gate URL_PARSE_SUCCESS ||
4450Sstevel@tonic-gate inet_addr(u.hostname) == (in_addr_t)-1 ||
4460Sstevel@tonic-gate (len = strlen(valstr)) >= cliptr->varmax) {
4470Sstevel@tonic-gate return (CLI_FAIL);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate (void) strcpy(cliptr->varptr, valstr);
4510Sstevel@tonic-gate cliptr->varlen = len + 1;
4520Sstevel@tonic-gate return (CLI_SET);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate * Exit the interpreter and return to the booter.
4570Sstevel@tonic-gate */
4580Sstevel@tonic-gate /*ARGSUSED*/
4590Sstevel@tonic-gate static int
clgo(cli_ent_t * cliptr,char * valstr,boolean_t out)4600Sstevel@tonic-gate clgo(cli_ent_t *cliptr, char *valstr, boolean_t out)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate return (CLI_EXIT);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate * Exit the interpreter and return to OBP.
4670Sstevel@tonic-gate */
4680Sstevel@tonic-gate /*ARGSUSED*/
4690Sstevel@tonic-gate static int
clexit(cli_ent_t * cliptr,char * valstr,boolean_t out)4700Sstevel@tonic-gate clexit(cli_ent_t *cliptr, char *valstr, boolean_t out)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate prom_exit_to_mon();
4730Sstevel@tonic-gate /*NOTREACHED*/
4740Sstevel@tonic-gate return (CLI_EXIT);
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate * Provide simple help information.
4790Sstevel@tonic-gate */
4800Sstevel@tonic-gate /*ARGSUSED*/
4810Sstevel@tonic-gate static int
clhelp(cli_ent_t * cliptr,char * valstr,boolean_t out)4820Sstevel@tonic-gate clhelp(cli_ent_t *cliptr, char *valstr, boolean_t out)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate printf("var=val - set variable\n");
4850Sstevel@tonic-gate printf("var= - unset variable\n");
4860Sstevel@tonic-gate printf("var - print variable\n");
4870Sstevel@tonic-gate printf("list - list variables and their values\n");
4880Sstevel@tonic-gate printf("prompt - prompt for unset variables\n");
4890Sstevel@tonic-gate printf("go - continue booting\n");
4900Sstevel@tonic-gate printf("exit - quit boot interpreter and return to OBP\n");
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate return (CLI_CONT);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate * List variables and their current values.
4970Sstevel@tonic-gate */
4980Sstevel@tonic-gate /*ARGSUSED*/
4990Sstevel@tonic-gate static int
cllist(cli_ent_t * cliptr,char * valstr,boolean_t out)5000Sstevel@tonic-gate cllist(cli_ent_t *cliptr, char *valstr, boolean_t out)
5010Sstevel@tonic-gate {
502785Seota int wanted = (int)(uintptr_t)valstr; /* use uintptr_t for gcc */
5030Sstevel@tonic-gate int i;
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate wanted &= ~(CLF_CMD | CLF_ARG);
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate for (cliptr = cli_list; cliptr < &cli_list[num_cli_ent]; cliptr++) {
5080Sstevel@tonic-gate if ((cliptr->flags & (CLF_CMD | CLF_ARG)) != 0 ||
5090Sstevel@tonic-gate (cliptr->flags & wanted) == 0) {
5100Sstevel@tonic-gate continue;
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate printf("%s: ", cliptr->varname);
5130Sstevel@tonic-gate /*
5140Sstevel@tonic-gate * Line the values up - space to the width of the widest
5150Sstevel@tonic-gate * varname + 1 for the ':'.
5160Sstevel@tonic-gate */
5170Sstevel@tonic-gate for (i = VAR_MAXWIDTH + 1 - strlen(cliptr->varname);
5180Sstevel@tonic-gate i > 0; --i) {
5190Sstevel@tonic-gate printf(" ");
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate if (CLF_ISSET(cliptr) || CLF_ISHIDDEN(cliptr)) {
5230Sstevel@tonic-gate (void) cliptr->action(cliptr, NULL, B_TRUE);
5240Sstevel@tonic-gate printf("\n");
5250Sstevel@tonic-gate } else {
5260Sstevel@tonic-gate printf("UNSET\n");
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate return (CLI_CONT);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate * Prompt for wanted values.
5350Sstevel@tonic-gate */
5360Sstevel@tonic-gate /*ARGSUSED*/
5370Sstevel@tonic-gate static int
clprompt(cli_ent_t * cliptr,char * valstr,boolean_t out)5380Sstevel@tonic-gate clprompt(cli_ent_t *cliptr, char *valstr, boolean_t out)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate char *p;
541785Seota int wanted = (int)(uintptr_t)valstr; /* use uintrptr_t for gcc */
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate /*
5440Sstevel@tonic-gate * If processing boot arguments, simply note the fact that clprompt()
5450Sstevel@tonic-gate * should be invoked later when other parameters may be supplied.
5460Sstevel@tonic-gate */
5470Sstevel@tonic-gate if ((wanted & CLF_ARG) != 0) {
5480Sstevel@tonic-gate args_specified_prompt = B_TRUE;
5490Sstevel@tonic-gate return (CLI_CONT);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate wanted &= ~(CLF_CMD | CLF_ARG);
5520Sstevel@tonic-gate
5530Sstevel@tonic-gate for (cliptr = cli_list; cliptr < &cli_list[num_cli_ent]; ++cliptr) {
5540Sstevel@tonic-gate if ((cliptr->flags & wanted) == 0) {
5550Sstevel@tonic-gate continue;
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate printf("%s", cliptr->varname);
5590Sstevel@tonic-gate if (CLF_ISSET(cliptr)) {
5600Sstevel@tonic-gate printf(" [");
5610Sstevel@tonic-gate (void) cliptr->action(cliptr, NULL, B_TRUE);
5620Sstevel@tonic-gate printf("]");
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate printf("? ");
5650Sstevel@tonic-gate (void) editline(cmdbuf, sizeof (cmdbuf));
5660Sstevel@tonic-gate printf("\n");
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate p = cmdbuf;
5690Sstevel@tonic-gate skipspace(p);
5700Sstevel@tonic-gate if (*p == '\0') { /* nothing there */
5710Sstevel@tonic-gate continue;
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate /* Get valstr and nul terminate */
5750Sstevel@tonic-gate valstr = p;
5760Sstevel@tonic-gate ++p;
5770Sstevel@tonic-gate skiptext(p);
5780Sstevel@tonic-gate *p = '\0';
5790Sstevel@tonic-gate
5800Sstevel@tonic-gate /* If empty value, do nothing */
5810Sstevel@tonic-gate if (strlen(valstr) == 0) {
5820Sstevel@tonic-gate continue;
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate switch (cliptr->action(cliptr, valstr, B_FALSE)) {
5860Sstevel@tonic-gate case CLI_SET:
5870Sstevel@tonic-gate CLF_MODVAL(cliptr);
5880Sstevel@tonic-gate break;
5890Sstevel@tonic-gate case CLI_FAIL:
5900Sstevel@tonic-gate printf("Incorrect format, parameter unchanged!\n");
5910Sstevel@tonic-gate break;
5920Sstevel@tonic-gate case CLI_EXIT:
5930Sstevel@tonic-gate return (CLI_EXIT);
5940Sstevel@tonic-gate case CLI_CONT:
5950Sstevel@tonic-gate break;
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate return (CLI_CONT);
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate /*
6030Sstevel@tonic-gate * If the PROM has done DHCP, bind the interface; otherwise do the full
6040Sstevel@tonic-gate * DHCP packet exchange.
6050Sstevel@tonic-gate */
6060Sstevel@tonic-gate /*ARGSUSED*/
6070Sstevel@tonic-gate static int
cldhcp(cli_ent_t * cliptr,char * valstr,boolean_t out)6080Sstevel@tonic-gate cldhcp(cli_ent_t *cliptr, char *valstr, boolean_t out)
6090Sstevel@tonic-gate {
6100Sstevel@tonic-gate static boolean_t first_time = B_TRUE;
6110Sstevel@tonic-gate static int ret = CLI_CONT;
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate if (first_time) {
6140Sstevel@tonic-gate /*
6150Sstevel@tonic-gate * Set DHCP's idea of the client_id from our cached value.
6160Sstevel@tonic-gate */
6170Sstevel@tonic-gate cliptr = find_cli_ent(BI_CLIENT_ID);
6180Sstevel@tonic-gate if (CLF_ISMOD(cliptr)) {
6190Sstevel@tonic-gate dhcp_set_client_id(cliptr->varptr, cliptr->varlen);
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_INFO, "Starting DHCP configuration");
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate (void) ipv4_setpromiscuous(B_TRUE);
6250Sstevel@tonic-gate if (dhcp() == 0) {
6260Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_INFO,
6270Sstevel@tonic-gate "DHCP configuration succeeded");
6280Sstevel@tonic-gate } else {
6290Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
6300Sstevel@tonic-gate "DHCP configuration failed");
6310Sstevel@tonic-gate ret = CLI_FAIL;
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate (void) ipv4_setpromiscuous(B_FALSE);
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate first_time = B_FALSE;
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate return (ret);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate * Invoke the socket test interpreter (for testing purposes only).
6430Sstevel@tonic-gate */
6440Sstevel@tonic-gate /*ARGSUSED*/
6450Sstevel@tonic-gate static int
cltest(cli_ent_t * cliptr,char * valstr,boolean_t out)6460Sstevel@tonic-gate cltest(cli_ent_t *cliptr, char *valstr, boolean_t out)
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate (void) ipv4_setpromiscuous(B_FALSE);
6490Sstevel@tonic-gate printf("\n");
6500Sstevel@tonic-gate for (;;) {
6510Sstevel@tonic-gate printf(TEST_PROMPT);
6520Sstevel@tonic-gate if (editline(cmdbuf, sizeof (cmdbuf)) > 0) {
6530Sstevel@tonic-gate printf("\n");
6540Sstevel@tonic-gate (void) st_interpret(cmdbuf);
6550Sstevel@tonic-gate } else {
6560Sstevel@tonic-gate prom_exit_to_mon();
6570Sstevel@tonic-gate /* NOTREACHED */
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate /* NOTREACHED */
6620Sstevel@tonic-gate return (CLI_CONT);
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate
6650Sstevel@tonic-gate /*
6660Sstevel@tonic-gate * Return the cliptr corresponding to the named variable.
6670Sstevel@tonic-gate */
6680Sstevel@tonic-gate static cli_ent_t *
find_cli_ent(char * varstr)6690Sstevel@tonic-gate find_cli_ent(char *varstr)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate cli_ent_t *cliptr;
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate for (cliptr = cli_list; cliptr < &cli_list[num_cli_ent]; ++cliptr) {
6740Sstevel@tonic-gate if (strcmp(varstr, cliptr->varname) == 0) {
6750Sstevel@tonic-gate return (cliptr);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate return (NULL);
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate /*
6830Sstevel@tonic-gate * Evaluate the commands provided by the user (either as "-o" boot arguments
6840Sstevel@tonic-gate * or interactively at the boot interpreter).
6850Sstevel@tonic-gate */
6860Sstevel@tonic-gate static int
cli_eval_buf(char * inbuf,int wanted)6870Sstevel@tonic-gate cli_eval_buf(char *inbuf, int wanted)
6880Sstevel@tonic-gate {
6890Sstevel@tonic-gate char *p, *varstr, *end_varstr, *valstr, *end_valstr;
6900Sstevel@tonic-gate boolean_t assign;
6910Sstevel@tonic-gate cli_ent_t *cliptr;
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate for (p = inbuf; *p != '\0'; ) {
6940Sstevel@tonic-gate skipspace(p);
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate /* If nothing more on line, go get the next one */
6970Sstevel@tonic-gate if (*p == '\0') {
6980Sstevel@tonic-gate break;
6990Sstevel@tonic-gate } else if (*p == ',') { /* orphan ',' ? */
7000Sstevel@tonic-gate ++p;
7010Sstevel@tonic-gate continue;
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate
7040Sstevel@tonic-gate /* Get ptrs to start & end of variable */
7050Sstevel@tonic-gate varstr = p;
7060Sstevel@tonic-gate ++p;
7070Sstevel@tonic-gate skiptext(p);
7080Sstevel@tonic-gate end_varstr = p;
7090Sstevel@tonic-gate skipspace(p);
7100Sstevel@tonic-gate
7110Sstevel@tonic-gate /* See if we're doing an assignment */
7120Sstevel@tonic-gate valstr = NULL;
7130Sstevel@tonic-gate if (*p != '=') { /* nope, just printing */
7140Sstevel@tonic-gate assign = B_FALSE;
7150Sstevel@tonic-gate } else {
7160Sstevel@tonic-gate assign = B_TRUE;
7170Sstevel@tonic-gate ++p; /* past '=' */
7180Sstevel@tonic-gate skipspace(p);
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate /* Assigning something? (else clear variable) */
7210Sstevel@tonic-gate if (*p != '\0' && *p != ',') {
7220Sstevel@tonic-gate /* Get ptrs to start & end of valstr */
7230Sstevel@tonic-gate valstr = p;
7240Sstevel@tonic-gate ++p;
7250Sstevel@tonic-gate skiptext(p);
7260Sstevel@tonic-gate end_valstr = p;
7270Sstevel@tonic-gate skipspace(p);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate /* Skip ',' delimiter if present */
7320Sstevel@tonic-gate if (*p == ',') {
7330Sstevel@tonic-gate ++p;
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate /* Nul-terminate varstr and valstr (if appropriate) */
7370Sstevel@tonic-gate *end_varstr = '\0';
7380Sstevel@tonic-gate if (valstr != NULL) {
7390Sstevel@tonic-gate *end_valstr = '\0';
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate
7420Sstevel@tonic-gate if ((cliptr = find_cli_ent(varstr)) == NULL) {
7430Sstevel@tonic-gate printf("Unknown variable '%s'; ignored\n", varstr);
7440Sstevel@tonic-gate continue;
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate * It's an error to specify a parameter which can only be a
7490Sstevel@tonic-gate * boot argument (and not a command) when not processing the
7500Sstevel@tonic-gate * boot arguments.
7510Sstevel@tonic-gate */
7520Sstevel@tonic-gate if ((cliptr->flags & (CLF_CMD | CLF_ARG)) == CLF_ARG &&
7530Sstevel@tonic-gate (wanted & CLF_ARG) == 0) {
7540Sstevel@tonic-gate printf("'%s' may only be specified as a "
7550Sstevel@tonic-gate "boot argument; ignored\n", varstr);
7560Sstevel@tonic-gate continue;
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate * When doing an assignment, verify that it's not a command
7610Sstevel@tonic-gate * or argument name, and that it is permissible in the current
7620Sstevel@tonic-gate * context. An 'empty' assignment (var=) is treated the same
7630Sstevel@tonic-gate * as a null assignment (var="").
7640Sstevel@tonic-gate *
7650Sstevel@tonic-gate * If processing the boot arguments, it is an error to not
7660Sstevel@tonic-gate * assign a value to a non-argument parameter.
7670Sstevel@tonic-gate */
7680Sstevel@tonic-gate if (assign) {
7690Sstevel@tonic-gate if ((cliptr->flags & (CLF_CMD | CLF_ARG)) != 0) {
7700Sstevel@tonic-gate printf("'%s' is a command and cannot "
7710Sstevel@tonic-gate "be assigned\n", varstr);
7720Sstevel@tonic-gate return (CLI_FAIL);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate if ((cliptr->flags & wanted) == 0) {
7750Sstevel@tonic-gate printf("'%s' cannot be assigned\n", varstr);
7760Sstevel@tonic-gate return (CLI_FAIL);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate if (valstr == NULL) {
7800Sstevel@tonic-gate cliptr->varlen = 0;
7810Sstevel@tonic-gate CLF_MODVAL(cliptr);
7820Sstevel@tonic-gate continue;
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate } else if ((wanted & CLF_ARG) != 0 &&
7850Sstevel@tonic-gate (cliptr->flags & (CLF_CMD | CLF_ARG)) == 0) {
7860Sstevel@tonic-gate printf("'%s' must be assigned when specified in "
7870Sstevel@tonic-gate " the boot arguments\n", varstr);
7880Sstevel@tonic-gate return (CLI_FAIL);
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate * Pass 'wanted' to command-handling functions, in particular
7930Sstevel@tonic-gate * clprompt() and cllist().
7940Sstevel@tonic-gate */
7950Sstevel@tonic-gate if ((cliptr->flags & CLF_CMD) != 0) {
796785Seota /* use uintptr_t to suppress the gcc warning */
797785Seota valstr = (char *)(uintptr_t)wanted;
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate
8000Sstevel@tonic-gate /*
8010Sstevel@tonic-gate * Call the parameter's action function.
8020Sstevel@tonic-gate */
8030Sstevel@tonic-gate switch (cliptr->action(cliptr, valstr, !assign)) {
8040Sstevel@tonic-gate case CLI_SET:
8050Sstevel@tonic-gate CLF_MODVAL(cliptr);
8060Sstevel@tonic-gate break;
8070Sstevel@tonic-gate case CLI_FAIL:
8080Sstevel@tonic-gate printf("Incorrect format: variable '%s' not set\n",
8090Sstevel@tonic-gate cliptr->varname);
8100Sstevel@tonic-gate break;
8110Sstevel@tonic-gate case CLI_EXIT:
8120Sstevel@tonic-gate return (CLI_EXIT);
8130Sstevel@tonic-gate case CLI_CONT:
8140Sstevel@tonic-gate if (!assign) {
8150Sstevel@tonic-gate printf("\n");
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate break;
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate return (CLI_CONT);
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate
8240Sstevel@tonic-gate static void
cli_interpret(int wanted)8250Sstevel@tonic-gate cli_interpret(int wanted)
8260Sstevel@tonic-gate {
8270Sstevel@tonic-gate printf("\n");
8280Sstevel@tonic-gate do {
8290Sstevel@tonic-gate printf(PROMPT);
8300Sstevel@tonic-gate (void) editline(cmdbuf, sizeof (cmdbuf));
8310Sstevel@tonic-gate printf("\n");
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate } while (cli_eval_buf(cmdbuf, wanted) != CLI_EXIT);
8340Sstevel@tonic-gate }
8350Sstevel@tonic-gate
8360Sstevel@tonic-gate #if defined(__sparcv9)
8370Sstevel@tonic-gate /*
8380Sstevel@tonic-gate * This routine queries the PROM to see what encryption keys exist.
8390Sstevel@tonic-gate */
8400Sstevel@tonic-gate static void
get_prom_encr_keys()8410Sstevel@tonic-gate get_prom_encr_keys()
8420Sstevel@tonic-gate {
8430Sstevel@tonic-gate cli_ent_t *cliptr;
8440Sstevel@tonic-gate char encr_key[WANBOOT_MAXKEYLEN];
8450Sstevel@tonic-gate int keylen;
8460Sstevel@tonic-gate int status;
8470Sstevel@tonic-gate int ret;
8480Sstevel@tonic-gate
8490Sstevel@tonic-gate /*
8500Sstevel@tonic-gate * At the top of the priority list, we have AES.
8510Sstevel@tonic-gate */
8520Sstevel@tonic-gate ret = prom_get_security_key(WANBOOT_AES_128_KEY_NAME, encr_key,
8530Sstevel@tonic-gate WANBOOT_MAXKEYLEN, &keylen, &status);
8540Sstevel@tonic-gate if ((ret == 0) && (status == 0) && (keylen == AES_128_KEY_SIZE)) {
8550Sstevel@tonic-gate cliptr = find_cli_ent(BI_AES_KEY);
8560Sstevel@tonic-gate bcopy(encr_key, cliptr->varptr, AES_128_KEY_SIZE);
8570Sstevel@tonic-gate cliptr->varlen = AES_128_KEY_SIZE;
8580Sstevel@tonic-gate CLF_MODVAL(cliptr);
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate /*
8620Sstevel@tonic-gate * Next, 3DES.
8630Sstevel@tonic-gate */
8640Sstevel@tonic-gate ret = prom_get_security_key(WANBOOT_DES3_KEY_NAME, encr_key,
8650Sstevel@tonic-gate WANBOOT_MAXKEYLEN, &keylen, &status);
8660Sstevel@tonic-gate if ((ret == 0) && (status == 0) && (keylen == DES3_KEY_SIZE)) {
8670Sstevel@tonic-gate cliptr = find_cli_ent(BI_3DES_KEY);
8680Sstevel@tonic-gate bcopy(encr_key, cliptr->varptr, DES3_KEY_SIZE);
8690Sstevel@tonic-gate cliptr->varlen = DES3_KEY_SIZE;
8700Sstevel@tonic-gate CLF_MODVAL(cliptr);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate }
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate /*
8750Sstevel@tonic-gate * This routine queries the PROM to see what hashing keys exist.
8760Sstevel@tonic-gate */
8770Sstevel@tonic-gate static void
get_prom_hash_keys()8780Sstevel@tonic-gate get_prom_hash_keys()
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate cli_ent_t *cliptr;
8810Sstevel@tonic-gate char hash_key[WANBOOT_HMAC_KEY_SIZE];
8820Sstevel@tonic-gate int keylen;
8830Sstevel@tonic-gate int status;
8840Sstevel@tonic-gate int ret;
8850Sstevel@tonic-gate
8860Sstevel@tonic-gate /*
8870Sstevel@tonic-gate * The only supported key thus far is SHA1.
8880Sstevel@tonic-gate */
8890Sstevel@tonic-gate ret = prom_get_security_key(WANBOOT_HMAC_SHA1_KEY_NAME, hash_key,
8900Sstevel@tonic-gate WANBOOT_HMAC_KEY_SIZE, &keylen, &status);
8910Sstevel@tonic-gate if ((ret == 0) && (status == 0) && (keylen == WANBOOT_HMAC_KEY_SIZE)) {
8920Sstevel@tonic-gate cliptr = find_cli_ent(BI_SHA1_KEY);
8930Sstevel@tonic-gate bcopy(hash_key, cliptr->varptr, WANBOOT_HMAC_KEY_SIZE);
8940Sstevel@tonic-gate cliptr->varlen = WANBOOT_HMAC_KEY_SIZE;
8950Sstevel@tonic-gate CLF_MODVAL(cliptr);
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate }
8980Sstevel@tonic-gate #endif /* defined(__sparcv9) */
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate /*
9010Sstevel@tonic-gate * For the given parameter type(s), get values from bootinfo and cache in
9020Sstevel@tonic-gate * the local variables used by the "boot>" interpreter.
9030Sstevel@tonic-gate */
9040Sstevel@tonic-gate static void
bootinfo_defaults(int which)9050Sstevel@tonic-gate bootinfo_defaults(int which)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate cli_ent_t *cliptr;
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate for (cliptr = cli_list; cliptr < &cli_list[num_cli_ent]; ++cliptr) {
9100Sstevel@tonic-gate if ((cliptr->flags & which) != 0 && !CLF_ISSET(cliptr)) {
9110Sstevel@tonic-gate size_t len = cliptr->varmax;
9120Sstevel@tonic-gate
9130Sstevel@tonic-gate if (bootinfo_get(cliptr->varname, cliptr->varptr,
9140Sstevel@tonic-gate &len, NULL) == BI_E_SUCCESS) {
9150Sstevel@tonic-gate cliptr->varlen = len;
9160Sstevel@tonic-gate CLF_SETVAL(cliptr);
9170Sstevel@tonic-gate }
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate /*
9230Sstevel@tonic-gate * For the given parameter type(s), store values entered at the "boot>"
9240Sstevel@tonic-gate * interpreter back into bootinfo.
9250Sstevel@tonic-gate */
9260Sstevel@tonic-gate static void
update_bootinfo(int which)9270Sstevel@tonic-gate update_bootinfo(int which)
9280Sstevel@tonic-gate {
9290Sstevel@tonic-gate cli_ent_t *cliptr;
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate for (cliptr = cli_list; cliptr < &cli_list[num_cli_ent]; ++cliptr) {
9320Sstevel@tonic-gate if ((cliptr->flags & which) != 0 && CLF_ISMOD(cliptr)) {
9330Sstevel@tonic-gate (void) bootinfo_put(cliptr->varname,
9340Sstevel@tonic-gate cliptr->varptr, cliptr->varlen, 0);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate
9390Sstevel@tonic-gate /*
9400Sstevel@tonic-gate * Return the net-config-strategy: "dhcp", "manual" or "rarp"
9410Sstevel@tonic-gate */
9420Sstevel@tonic-gate static char *
net_config_strategy(void)9430Sstevel@tonic-gate net_config_strategy(void)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate static char ncs[8]; /* "dhcp" or "manual" */
9460Sstevel@tonic-gate size_t len = sizeof (ncs);
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate if (ncs[0] == '\0' &&
9490Sstevel@tonic-gate bootinfo_get(BI_NET_CONFIG_STRATEGY, ncs, &len, NULL) !=
9500Sstevel@tonic-gate BI_E_SUCCESS) {
9510Sstevel@tonic-gate /*
9520Sstevel@tonic-gate * Support for old PROMs: create the net-config-strategy
9530Sstevel@tonic-gate * property under /chosen with an appropriate value. If we
9540Sstevel@tonic-gate * have a bootp-response (not interested in its value, just
9550Sstevel@tonic-gate * its presence) then we did DHCP; otherwise configuration
9560Sstevel@tonic-gate * is manual.
9570Sstevel@tonic-gate */
9580Sstevel@tonic-gate if (bootinfo_get(BI_BOOTP_RESPONSE, NULL, NULL,
9590Sstevel@tonic-gate NULL) == BI_E_BUF2SMALL) {
9600Sstevel@tonic-gate (void) strcpy(ncs, "dhcp");
9610Sstevel@tonic-gate } else {
9620Sstevel@tonic-gate (void) strcpy(ncs, "manual");
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate (void) bootinfo_put(BI_NET_CONFIG_STRATEGY, ncs, strlen(ncs),
9650Sstevel@tonic-gate BI_R_CHOSEN);
9660Sstevel@tonic-gate
9670Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_INFO,
9680Sstevel@tonic-gate "Default net-config-strategy: %s", ncs);
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate
9710Sstevel@tonic-gate return (ncs);
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate
9740Sstevel@tonic-gate /*
9750Sstevel@tonic-gate * If there is no client-id property published in /chosen (by the PROM or the
9760Sstevel@tonic-gate * boot interpreter) provide a default client-id based on the MAC address of
9770Sstevel@tonic-gate * the client.
9780Sstevel@tonic-gate * As specified in RFC2132 (section 9.14), this is prefixed with a byte
9790Sstevel@tonic-gate * which specifies the ARP hardware type defined in RFC1700 (for Ethernet,
9800Sstevel@tonic-gate * this should be 1).
9810Sstevel@tonic-gate */
9820Sstevel@tonic-gate static void
generate_default_clientid(void)9830Sstevel@tonic-gate generate_default_clientid(void)
9840Sstevel@tonic-gate {
9850Sstevel@tonic-gate char clid[WB_MAX_CID_LEN];
9860Sstevel@tonic-gate size_t len = sizeof (clid);
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate if (bootinfo_get(BI_CLIENT_ID, clid, &len, NULL) != BI_E_SUCCESS) {
9890Sstevel@tonic-gate len = mac_get_addr_len() + 1; /* include hwtype */
9900Sstevel@tonic-gate
9910Sstevel@tonic-gate if (len > sizeof (clid)) {
9920Sstevel@tonic-gate return;
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate
9950Sstevel@tonic-gate clid[0] = mac_arp_type(mac_get_type());
9960Sstevel@tonic-gate bcopy(mac_get_addr_buf(), &clid[1], len - 1);
9970Sstevel@tonic-gate
9980Sstevel@tonic-gate (void) bootinfo_put(BI_CLIENT_ID, clid, len, 0);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate
10020Sstevel@tonic-gate /*
10030Sstevel@tonic-gate * Determine the URL of the boot server from the 'file' parameter to OBP,
10040Sstevel@tonic-gate * the SbootURI or BootFile DHCP options, or the 'bootserver' value entered
10050Sstevel@tonic-gate * either as a "-o" argument or at the interpreter.
10060Sstevel@tonic-gate */
10070Sstevel@tonic-gate static void
determine_bootserver_url(void)10080Sstevel@tonic-gate determine_bootserver_url(void)
10090Sstevel@tonic-gate {
10100Sstevel@tonic-gate char bs[URL_MAX_STRLEN + 1];
10110Sstevel@tonic-gate size_t len;
10120Sstevel@tonic-gate url_t url;
10130Sstevel@tonic-gate
10140Sstevel@tonic-gate if (bootinfo_get(BI_BOOTSERVER, bs, &len, NULL) != BI_E_SUCCESS) {
10150Sstevel@tonic-gate /*
10160Sstevel@tonic-gate * If OBP has published a network-boot-file property in
10170Sstevel@tonic-gate * /chosen (or there is a DHCP BootFile or SbootURI vendor
10180Sstevel@tonic-gate * option) and it's a URL, construct the bootserver URL
10190Sstevel@tonic-gate * from it.
10200Sstevel@tonic-gate */
10210Sstevel@tonic-gate len = URL_MAX_STRLEN;
10220Sstevel@tonic-gate if (bootinfo_get(BI_NETWORK_BOOT_FILE, bs, &len, NULL) !=
10230Sstevel@tonic-gate BI_E_SUCCESS) {
10240Sstevel@tonic-gate len = URL_MAX_STRLEN;
10250Sstevel@tonic-gate if (bootinfo_get(BI_BOOTFILE, bs, &len, NULL) !=
10260Sstevel@tonic-gate BI_E_SUCCESS) {
10270Sstevel@tonic-gate return;
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate }
10300Sstevel@tonic-gate if (url_parse(bs, &url) == URL_PARSE_SUCCESS) {
10310Sstevel@tonic-gate (void) bootinfo_put(BI_BOOTSERVER, bs, len, 0);
10320Sstevel@tonic-gate }
10330Sstevel@tonic-gate }
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate /*
10370Sstevel@tonic-gate * Provide a classful subnet mask based on the client's IP address.
10380Sstevel@tonic-gate */
10390Sstevel@tonic-gate static in_addr_t
generate_classful_subnet(in_addr_t client_ipaddr)10400Sstevel@tonic-gate generate_classful_subnet(in_addr_t client_ipaddr)
10410Sstevel@tonic-gate {
10420Sstevel@tonic-gate struct in_addr subnetmask;
10430Sstevel@tonic-gate char *netstr;
10440Sstevel@tonic-gate
10450Sstevel@tonic-gate if (IN_CLASSA(client_ipaddr)) {
10460Sstevel@tonic-gate subnetmask.s_addr = IN_CLASSA_NET;
10470Sstevel@tonic-gate } else if (IN_CLASSB(client_ipaddr)) {
10480Sstevel@tonic-gate subnetmask.s_addr = IN_CLASSB_NET;
1049*5577Ssangeeta } else if (IN_CLASSC(client_ipaddr)) {
1050*5577Ssangeeta subnetmask.s_addr = IN_CLASSC_NET;
10510Sstevel@tonic-gate } else {
1052*5577Ssangeeta subnetmask.s_addr = IN_CLASSE_NET;
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate
10550Sstevel@tonic-gate netstr = inet_ntoa(subnetmask);
10560Sstevel@tonic-gate (void) bootinfo_put(BI_SUBNET_MASK, netstr, strlen(netstr) + 1, 0);
10570Sstevel@tonic-gate
10580Sstevel@tonic-gate return (subnetmask.s_addr);
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate
10610Sstevel@tonic-gate /*
10620Sstevel@tonic-gate * Informational output to the user (if interactive) or the bootlogger.
10630Sstevel@tonic-gate */
10640Sstevel@tonic-gate static void
info(const char * msg,boolean_t interactive)10650Sstevel@tonic-gate info(const char *msg, boolean_t interactive)
10660Sstevel@tonic-gate {
10670Sstevel@tonic-gate if (interactive) {
10680Sstevel@tonic-gate printf("%s\n", msg);
10690Sstevel@tonic-gate } else {
10700Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_INFO, "%s", msg);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate /*
10750Sstevel@tonic-gate * Determine whether we have sufficient information to proceed with booting,
10760Sstevel@tonic-gate * either for configuring the interface and downloading the bootconf file,
10770Sstevel@tonic-gate * or for downloading the miniroot.
10780Sstevel@tonic-gate */
10790Sstevel@tonic-gate static int
config_incomplete(int why,boolean_t interactive)10800Sstevel@tonic-gate config_incomplete(int why, boolean_t interactive)
10810Sstevel@tonic-gate {
10820Sstevel@tonic-gate boolean_t error = B_FALSE;
10830Sstevel@tonic-gate char buf[URL_MAX_STRLEN + 1];
10840Sstevel@tonic-gate size_t len;
10850Sstevel@tonic-gate char *urlstr;
10860Sstevel@tonic-gate url_t u;
10870Sstevel@tonic-gate struct hostent *hp;
10880Sstevel@tonic-gate in_addr_t client_ipaddr, ipaddr, bsnet, pxnet;
10890Sstevel@tonic-gate static in_addr_t subnetmask, clnet;
10900Sstevel@tonic-gate static boolean_t have_router = B_FALSE;
10910Sstevel@tonic-gate static boolean_t have_proxy = B_FALSE;
10920Sstevel@tonic-gate boolean_t have_root_server = B_FALSE;
10930Sstevel@tonic-gate boolean_t have_boot_logger = B_FALSE;
10940Sstevel@tonic-gate in_addr_t rsnet, blnet;
10950Sstevel@tonic-gate
10960Sstevel@tonic-gate /*
10970Sstevel@tonic-gate * Note that 'have_router', 'have_proxy', 'subnetmask', and 'clnet'
10980Sstevel@tonic-gate * are static, so that their values (gathered when checking the
10990Sstevel@tonic-gate * interface configuration) may be used again when checking the boot
11000Sstevel@tonic-gate * configuration.
11010Sstevel@tonic-gate */
11020Sstevel@tonic-gate if (why == CLF_IF) {
11030Sstevel@tonic-gate /*
11040Sstevel@tonic-gate * A valid host IP address is an absolute requirement.
11050Sstevel@tonic-gate */
11060Sstevel@tonic-gate len = sizeof (buf);
11070Sstevel@tonic-gate if (bootinfo_get(BI_HOST_IP, buf, &len, NULL) == BI_E_SUCCESS) {
11080Sstevel@tonic-gate if ((client_ipaddr = inet_addr(buf)) == (in_addr_t)-1) {
11090Sstevel@tonic-gate info("host-ip invalid!", interactive);
11100Sstevel@tonic-gate error = B_TRUE;
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate } else {
11130Sstevel@tonic-gate info("host-ip not set!", interactive);
11140Sstevel@tonic-gate error = B_TRUE;
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate * If a subnet mask was provided, use it; otherwise infer it.
11190Sstevel@tonic-gate */
11200Sstevel@tonic-gate len = sizeof (buf);
11210Sstevel@tonic-gate if (bootinfo_get(BI_SUBNET_MASK, buf, &len, NULL) ==
11220Sstevel@tonic-gate BI_E_SUCCESS) {
11230Sstevel@tonic-gate if ((subnetmask = inet_addr(buf)) == (in_addr_t)-1) {
11240Sstevel@tonic-gate info("subnet-mask invalid!", interactive);
11250Sstevel@tonic-gate error = B_TRUE;
11260Sstevel@tonic-gate }
11270Sstevel@tonic-gate } else {
11280Sstevel@tonic-gate info("Defaulting to classful subnetting", interactive);
11290Sstevel@tonic-gate
11300Sstevel@tonic-gate subnetmask = generate_classful_subnet(client_ipaddr);
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate clnet = client_ipaddr & subnetmask;
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate /*
11350Sstevel@tonic-gate * A legal bootserver URL is also an absolute requirement.
11360Sstevel@tonic-gate */
11370Sstevel@tonic-gate len = sizeof (buf);
11380Sstevel@tonic-gate if (bootinfo_get(BI_BOOTSERVER, buf, &len, NULL) ==
11390Sstevel@tonic-gate BI_E_SUCCESS) {
11400Sstevel@tonic-gate if (url_parse(buf, &u) != URL_PARSE_SUCCESS ||
11410Sstevel@tonic-gate u.https ||
11420Sstevel@tonic-gate (ipaddr = inet_addr(u.hport.hostname)) ==
11430Sstevel@tonic-gate (in_addr_t)-1) {
11440Sstevel@tonic-gate info("bootserver not legal URL!", interactive);
11450Sstevel@tonic-gate error = B_TRUE;
11460Sstevel@tonic-gate } else {
11470Sstevel@tonic-gate bsnet = ipaddr & subnetmask;
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate } else {
11500Sstevel@tonic-gate info("bootserver not specified!", interactive);
11510Sstevel@tonic-gate error = B_TRUE;
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate
11540Sstevel@tonic-gate /*
11550Sstevel@tonic-gate * Is there a correctly-defined router?
11560Sstevel@tonic-gate */
11570Sstevel@tonic-gate len = sizeof (buf);
11580Sstevel@tonic-gate if (bootinfo_get(BI_ROUTER_IP, buf, &len, NULL) ==
11590Sstevel@tonic-gate BI_E_SUCCESS) {
11600Sstevel@tonic-gate if ((ipaddr = inet_addr(buf)) == (in_addr_t)-1) {
11610Sstevel@tonic-gate info("router-ip invalid!", interactive);
11620Sstevel@tonic-gate error = B_TRUE;
11630Sstevel@tonic-gate } else if (clnet != (ipaddr & subnetmask)) {
11640Sstevel@tonic-gate info("router not on local subnet!",
11650Sstevel@tonic-gate interactive);
11660Sstevel@tonic-gate error = B_TRUE;
11670Sstevel@tonic-gate } else {
11680Sstevel@tonic-gate have_router = B_TRUE;
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate
11720Sstevel@tonic-gate /*
11730Sstevel@tonic-gate * Is there a correctly-defined proxy?
11740Sstevel@tonic-gate */
11750Sstevel@tonic-gate len = sizeof (buf);
11760Sstevel@tonic-gate if (bootinfo_get(BI_HTTP_PROXY, buf, &len, NULL) ==
11770Sstevel@tonic-gate BI_E_SUCCESS) {
11780Sstevel@tonic-gate url_hport_t u;
11790Sstevel@tonic-gate
11800Sstevel@tonic-gate if (url_parse_hostport(buf, &u, URL_DFLT_PROXY_PORT) !=
11810Sstevel@tonic-gate URL_PARSE_SUCCESS ||
11820Sstevel@tonic-gate (ipaddr = inet_addr(u.hostname)) == (in_addr_t)-1) {
11830Sstevel@tonic-gate info("http-proxy port invalid!", interactive);
11840Sstevel@tonic-gate error = B_TRUE;
11850Sstevel@tonic-gate } else {
11860Sstevel@tonic-gate /*
11870Sstevel@tonic-gate * The proxy is only of use to us if it's on
11880Sstevel@tonic-gate * our local subnet, or if a router has been
11890Sstevel@tonic-gate * specified (which should hopefully allow us
11900Sstevel@tonic-gate * to access the proxy).
11910Sstevel@tonic-gate */
11920Sstevel@tonic-gate pxnet = ipaddr & subnetmask;
11930Sstevel@tonic-gate have_proxy = (have_router || pxnet == clnet);
11940Sstevel@tonic-gate }
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate /*
11980Sstevel@tonic-gate * If there is no router and no proxy (either on the local
11990Sstevel@tonic-gate * subnet or reachable via a router), then the bootserver
12000Sstevel@tonic-gate * URL must be on the local net.
12010Sstevel@tonic-gate */
12020Sstevel@tonic-gate if (!error && !have_router && !have_proxy && bsnet != clnet) {
12030Sstevel@tonic-gate info("bootserver URL not on local subnet",
12040Sstevel@tonic-gate interactive);
12050Sstevel@tonic-gate error = B_TRUE;
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate } else {
12080Sstevel@tonic-gate /*
12090Sstevel@tonic-gate * There must be a correctly-defined root_server URL.
12100Sstevel@tonic-gate */
12110Sstevel@tonic-gate if ((urlstr = bootconf_get(&bc_handle,
12120Sstevel@tonic-gate BC_ROOT_SERVER)) == NULL) {
12130Sstevel@tonic-gate info("no root_server URL!", interactive);
12140Sstevel@tonic-gate error = B_TRUE;
12150Sstevel@tonic-gate } else if (url_parse(urlstr, &u) != URL_PARSE_SUCCESS) {
12160Sstevel@tonic-gate info("root_server not legal URL!", interactive);
12170Sstevel@tonic-gate error = B_TRUE;
12180Sstevel@tonic-gate } else if ((hp = gethostbyname(u.hport.hostname)) == NULL) {
12190Sstevel@tonic-gate info("cannot resolve root_server hostname!",
12200Sstevel@tonic-gate interactive);
12210Sstevel@tonic-gate error = B_TRUE;
12220Sstevel@tonic-gate } else {
12230Sstevel@tonic-gate rsnet = *(in_addr_t *)hp->h_addr & subnetmask;
12240Sstevel@tonic-gate have_root_server = B_TRUE;
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate * Is there a correctly-defined (non-empty) boot_logger URL?
12290Sstevel@tonic-gate */
12300Sstevel@tonic-gate if ((urlstr = bootconf_get(&bc_handle,
12310Sstevel@tonic-gate BC_BOOT_LOGGER)) != NULL) {
12320Sstevel@tonic-gate if (url_parse(urlstr, &u) != URL_PARSE_SUCCESS) {
12330Sstevel@tonic-gate info("boot_logger not legal URL!", interactive);
12340Sstevel@tonic-gate error = B_TRUE;
12350Sstevel@tonic-gate } else if ((hp = gethostbyname(u.hport.hostname)) ==
12360Sstevel@tonic-gate NULL) {
12370Sstevel@tonic-gate info("cannot resolve boot_logger hostname!",
12380Sstevel@tonic-gate interactive);
12390Sstevel@tonic-gate error = B_TRUE;
12400Sstevel@tonic-gate } else {
12410Sstevel@tonic-gate blnet = *(in_addr_t *)hp->h_addr & subnetmask;
12420Sstevel@tonic-gate have_boot_logger = B_TRUE;
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate }
12450Sstevel@tonic-gate
12460Sstevel@tonic-gate /*
12470Sstevel@tonic-gate * If there is no router and no proxy (either on the local
12480Sstevel@tonic-gate * subnet or reachable via a router), then the root_server
12490Sstevel@tonic-gate * URL (and the boot_logger URL if specified) must be on the
12500Sstevel@tonic-gate * local net.
12510Sstevel@tonic-gate */
12520Sstevel@tonic-gate if (!error && !have_router && !have_proxy) {
12530Sstevel@tonic-gate if (have_root_server && rsnet != clnet) {
12540Sstevel@tonic-gate info("root_server URL not on local subnet",
12550Sstevel@tonic-gate interactive);
12560Sstevel@tonic-gate error = B_TRUE;
12570Sstevel@tonic-gate }
12580Sstevel@tonic-gate if (have_boot_logger && blnet != clnet) {
12590Sstevel@tonic-gate info("boot_logger URL not on local subnet",
12600Sstevel@tonic-gate interactive);
12610Sstevel@tonic-gate error = B_TRUE;
12620Sstevel@tonic-gate }
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate
12660Sstevel@tonic-gate return (error);
12670Sstevel@tonic-gate }
12680Sstevel@tonic-gate
12690Sstevel@tonic-gate /*
12700Sstevel@tonic-gate * Actually setup our network interface with the values derived from the
12710Sstevel@tonic-gate * PROM, DHCP or interactively from the user.
12720Sstevel@tonic-gate */
12730Sstevel@tonic-gate static void
setup_interface()12740Sstevel@tonic-gate setup_interface()
12750Sstevel@tonic-gate {
12760Sstevel@tonic-gate char str[MAXHOSTNAMELEN]; /* will accomodate an IP too */
12770Sstevel@tonic-gate size_t len;
12780Sstevel@tonic-gate struct in_addr in_addr;
12790Sstevel@tonic-gate
12800Sstevel@tonic-gate len = sizeof (str);
12810Sstevel@tonic-gate if (bootinfo_get(BI_HOST_IP, str, &len, NULL) == BI_E_SUCCESS &&
12820Sstevel@tonic-gate (in_addr.s_addr = inet_addr(str)) != (in_addr_t)-1) {
12830Sstevel@tonic-gate in_addr.s_addr = htonl(in_addr.s_addr);
12840Sstevel@tonic-gate ipv4_setipaddr(&in_addr);
12850Sstevel@tonic-gate }
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate len = sizeof (str);
12880Sstevel@tonic-gate if (bootinfo_get(BI_SUBNET_MASK, str, &len, NULL) == BI_E_SUCCESS &&
12890Sstevel@tonic-gate (in_addr.s_addr = inet_addr(str)) != (in_addr_t)-1) {
12900Sstevel@tonic-gate in_addr.s_addr = htonl(in_addr.s_addr);
12910Sstevel@tonic-gate ipv4_setnetmask(&in_addr);
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate
12940Sstevel@tonic-gate len = sizeof (str);
12950Sstevel@tonic-gate if (bootinfo_get(BI_ROUTER_IP, str, &len, NULL) == BI_E_SUCCESS &&
12960Sstevel@tonic-gate (in_addr.s_addr = inet_addr(str)) != (in_addr_t)-1) {
12970Sstevel@tonic-gate in_addr.s_addr = htonl(in_addr.s_addr);
12980Sstevel@tonic-gate ipv4_setdefaultrouter(&in_addr);
12990Sstevel@tonic-gate (void) ipv4_route(IPV4_ADD_ROUTE, RT_DEFAULT, NULL, &in_addr);
13000Sstevel@tonic-gate }
13010Sstevel@tonic-gate
13020Sstevel@tonic-gate len = sizeof (str);
13030Sstevel@tonic-gate if (bootinfo_get(BI_HOSTNAME, str, &len, NULL) == BI_E_SUCCESS) {
13040Sstevel@tonic-gate (void) sethostname(str, len);
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate }
13070Sstevel@tonic-gate
13080Sstevel@tonic-gate /* EXPORT DELETE END */
13090Sstevel@tonic-gate boolean_t
wanboot_init_interface(char * boot_arguments)13100Sstevel@tonic-gate wanboot_init_interface(char *boot_arguments)
13110Sstevel@tonic-gate {
13120Sstevel@tonic-gate /* EXPORT DELETE START */
13130Sstevel@tonic-gate boolean_t interactive;
13140Sstevel@tonic-gate int which;
13150Sstevel@tonic-gate
13160Sstevel@tonic-gate #if defined(__sparcv9)
13170Sstevel@tonic-gate /*
13180Sstevel@tonic-gate * Get the keys from PROM before we allow the user
13190Sstevel@tonic-gate * to override them from the CLI.
13200Sstevel@tonic-gate */
13210Sstevel@tonic-gate get_prom_encr_keys();
13220Sstevel@tonic-gate get_prom_hash_keys();
13230Sstevel@tonic-gate #endif /* defined(__sparcv9) */
13240Sstevel@tonic-gate
13250Sstevel@tonic-gate /*
13260Sstevel@tonic-gate * If there is already a bootp-response property under
13270Sstevel@tonic-gate * /chosen then the PROM must have done DHCP for us;
13280Sstevel@tonic-gate * invoke dhcp() to 'bind' the interface.
13290Sstevel@tonic-gate */
13300Sstevel@tonic-gate if (bootinfo_get(BI_BOOTP_RESPONSE, NULL, NULL, NULL) ==
13310Sstevel@tonic-gate BI_E_BUF2SMALL) {
13320Sstevel@tonic-gate (void) cldhcp(NULL, NULL, 0);
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate
13350Sstevel@tonic-gate /*
13360Sstevel@tonic-gate * Obtain default interface values from bootinfo.
13370Sstevel@tonic-gate */
13380Sstevel@tonic-gate bootinfo_defaults(CLF_IF);
13390Sstevel@tonic-gate
13400Sstevel@tonic-gate /*
13410Sstevel@tonic-gate * Process the boot arguments (following the "-o" option).
13420Sstevel@tonic-gate */
13430Sstevel@tonic-gate if (boot_arguments != NULL) {
13440Sstevel@tonic-gate (void) cli_eval_buf(boot_arguments,
13450Sstevel@tonic-gate (CLF_ARG | CLF_IF | CLF_BM));
13460Sstevel@tonic-gate }
13470Sstevel@tonic-gate
13480Sstevel@tonic-gate /*
13490Sstevel@tonic-gate * Stash away any interface/bootmisc parameter values we got
13500Sstevel@tonic-gate * from either the PROM or the boot arguments.
13510Sstevel@tonic-gate */
13520Sstevel@tonic-gate update_bootinfo(CLF_IF | CLF_BM);
13530Sstevel@tonic-gate
13540Sstevel@tonic-gate /*
13550Sstevel@tonic-gate * If we don't already have a value for bootserver, try to
13560Sstevel@tonic-gate * deduce one. Refresh wbcli's idea of these values.
13570Sstevel@tonic-gate */
13580Sstevel@tonic-gate determine_bootserver_url();
13590Sstevel@tonic-gate bootinfo_defaults(CLF_BM);
13600Sstevel@tonic-gate
13610Sstevel@tonic-gate /*
13620Sstevel@tonic-gate * Check that the information we have collected thus far is sufficient.
13630Sstevel@tonic-gate */
13640Sstevel@tonic-gate interactive = args_specified_prompt;
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate if (interactive) {
13670Sstevel@tonic-gate /*
13680Sstevel@tonic-gate * Drop into the boot interpreter to allow the input
13690Sstevel@tonic-gate * of keys, bootserver and bootmisc, and in the case
13700Sstevel@tonic-gate * that net-config-strategy == "manual" the interface
13710Sstevel@tonic-gate * parameters.
13720Sstevel@tonic-gate */
13730Sstevel@tonic-gate which = CLF_BM | CLF_CMD;
13740Sstevel@tonic-gate if (strcmp(net_config_strategy(), "manual") == 0)
13750Sstevel@tonic-gate which |= CLF_IF;
13760Sstevel@tonic-gate
13770Sstevel@tonic-gate do {
13780Sstevel@tonic-gate cli_interpret(which);
13790Sstevel@tonic-gate update_bootinfo(CLF_IF | CLF_BM);
13800Sstevel@tonic-gate } while (config_incomplete(CLF_IF, interactive));
13810Sstevel@tonic-gate } else {
13820Sstevel@tonic-gate /*
13830Sstevel@tonic-gate * The user is not to be given the opportunity to
13840Sstevel@tonic-gate * enter further values; fail.
13850Sstevel@tonic-gate */
13860Sstevel@tonic-gate if (config_incomplete(CLF_IF, interactive)) {
13870Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
13880Sstevel@tonic-gate "interface incorrectly configured");
13890Sstevel@tonic-gate return (B_FALSE);
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate }
13920Sstevel@tonic-gate
13930Sstevel@tonic-gate /*
13940Sstevel@tonic-gate * If a wanboot-enabled PROM hasn't processed client-id in
13950Sstevel@tonic-gate * network-boot-arguments, or no value for client-id has been
13960Sstevel@tonic-gate * specified to the boot interpreter, then provide a default
13970Sstevel@tonic-gate * client-id based on our MAC address.
13980Sstevel@tonic-gate */
13990Sstevel@tonic-gate generate_default_clientid();
14000Sstevel@tonic-gate
14010Sstevel@tonic-gate /*
14020Sstevel@tonic-gate * If net-config-strategy == "manual" then we must setup
14030Sstevel@tonic-gate * the interface now; if "dhcp" then it will already have
14040Sstevel@tonic-gate * been setup.
14050Sstevel@tonic-gate */
14060Sstevel@tonic-gate if (strcmp(net_config_strategy(), "manual") == 0)
14070Sstevel@tonic-gate setup_interface();
14080Sstevel@tonic-gate /* EXPORT DELETE END */
14090Sstevel@tonic-gate return (B_TRUE);
14100Sstevel@tonic-gate }
14110Sstevel@tonic-gate
14120Sstevel@tonic-gate boolean_t
wanboot_verify_config(void)14130Sstevel@tonic-gate wanboot_verify_config(void)
14140Sstevel@tonic-gate {
14150Sstevel@tonic-gate /* EXPORT DELETE START */
14160Sstevel@tonic-gate /*
14170Sstevel@tonic-gate * Check that the wanboot.conf file defines a valid root_server
14180Sstevel@tonic-gate * URL, and check that, if given, the boot_logger URL is valid.
14190Sstevel@tonic-gate */
14200Sstevel@tonic-gate if (config_incomplete(0, B_FALSE)) {
14210Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
14220Sstevel@tonic-gate "incomplete boot configuration");
14230Sstevel@tonic-gate return (B_FALSE);
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate /* EXPORT DELETE END */
14260Sstevel@tonic-gate return (B_TRUE);
14270Sstevel@tonic-gate }
1428