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
55648Ssetje * Common Development and Distribution License (the "License").
65648Ssetje * 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*9034SJerry.Gilliam@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate /* EXPORT DELETE START */
280Sstevel@tonic-gate #include <sys/promif.h>
290Sstevel@tonic-gate #include <sys/obpdefs.h>
300Sstevel@tonic-gate #include <sys/bootvfs.h>
310Sstevel@tonic-gate #include <sys/bootconf.h>
320Sstevel@tonic-gate #include <netinet/in.h>
330Sstevel@tonic-gate #include <sys/wanboot_impl.h>
340Sstevel@tonic-gate #include <boot_http.h>
350Sstevel@tonic-gate #include <aes.h>
360Sstevel@tonic-gate #include <des3.h>
370Sstevel@tonic-gate #include <cbc.h>
380Sstevel@tonic-gate #include <hmac_sha1.h>
390Sstevel@tonic-gate #include <sys/sha1.h>
400Sstevel@tonic-gate #include <sys/sha1_consts.h>
410Sstevel@tonic-gate #include <bootlog.h>
420Sstevel@tonic-gate #include <parseURL.h>
430Sstevel@tonic-gate #include <netboot_paths.h>
440Sstevel@tonic-gate #include <netinet/inetutil.h>
450Sstevel@tonic-gate #include <sys/salib.h>
460Sstevel@tonic-gate #include <inet/mac.h>
470Sstevel@tonic-gate #include <inet/ipv4.h>
480Sstevel@tonic-gate #include <dhcp_impl.h>
490Sstevel@tonic-gate #include <inet/dhcpv4.h>
500Sstevel@tonic-gate #include <bootinfo.h>
510Sstevel@tonic-gate #include <wanboot_conf.h>
520Sstevel@tonic-gate #include "boot_plat.h"
530Sstevel@tonic-gate #include "ramdisk.h"
540Sstevel@tonic-gate #include "wbcli.h"
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * Types of downloads
580Sstevel@tonic-gate */
590Sstevel@tonic-gate #define MINIINFO "miniinfo"
600Sstevel@tonic-gate #define MINIROOT "miniroot"
610Sstevel@tonic-gate #define WANBOOTFS "wanbootfs"
620Sstevel@tonic-gate
630Sstevel@tonic-gate #define WANBOOT_RETRY_NOMAX -1
640Sstevel@tonic-gate #define WANBOOT_RETRY_ROOT_MAX 50
650Sstevel@tonic-gate #define WANBOOT_RETRY_MAX 5
660Sstevel@tonic-gate #define WANBOOT_RETRY_SECS 5
670Sstevel@tonic-gate #define WANBOOT_RETRY_MAX_SECS 30
680Sstevel@tonic-gate
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate * Our read requests should timeout after 25 seconds
710Sstevel@tonic-gate */
720Sstevel@tonic-gate #define SOCKET_READ_TIMEOUT 25
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * Experimentation has shown that an 8K download buffer is optimal
760Sstevel@tonic-gate */
775648Ssetje #define HTTP_XFER_SIZE 8192
785648Ssetje static char buffer[HTTP_XFER_SIZE];
790Sstevel@tonic-gate
800Sstevel@tonic-gate bc_handle_t bc_handle;
810Sstevel@tonic-gate
820Sstevel@tonic-gate extern int determine_fstype_and_mountroot(char *);
830Sstevel@tonic-gate extern uint64_t get_ticks(void);
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * The following is used to determine whether the certs and private key
870Sstevel@tonic-gate * files will be in PEM format or PKCS12 format. 'use_p12' is zero
880Sstevel@tonic-gate * to use PEM format, and 1 when PKCS12 format is to be used. It is
890Sstevel@tonic-gate * done this way, as a global, so that it can be patched if needs be
900Sstevel@tonic-gate * using the OBP debugger.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate uint32_t use_p12 = 1;
930Sstevel@tonic-gate
940Sstevel@tonic-gate #define CONTENT_LENGTH "Content-Length"
950Sstevel@tonic-gate
960Sstevel@tonic-gate #define NONCELEN (2 * HMAC_DIGEST_LEN) /* two hex nibbles/byte */
970Sstevel@tonic-gate #define WANBOOTFS_NONCE_FILE "/nonce"
980Sstevel@tonic-gate
990Sstevel@tonic-gate static char nonce[NONCELEN + 1];
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate enum URLtype {
1020Sstevel@tonic-gate URLtype_wanbootfs = 0,
1030Sstevel@tonic-gate URLtype_miniroot = 1
1040Sstevel@tonic-gate };
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate static char *URLtoCGIcontent[] = {
1070Sstevel@tonic-gate "bootfs",
1080Sstevel@tonic-gate "rootfs"
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate #define CGIcontent(urltype) URLtoCGIcontent[urltype]
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /* Encryption algorithms */
1130Sstevel@tonic-gate typedef enum {
1140Sstevel@tonic-gate ENCR_NONE,
1150Sstevel@tonic-gate ENCR_3DES,
1160Sstevel@tonic-gate ENCR_AES
1170Sstevel@tonic-gate } encr_type_t;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /* Hash algorithms */
1200Sstevel@tonic-gate typedef enum {
1210Sstevel@tonic-gate HASH_NONE,
1220Sstevel@tonic-gate HASH_HMAC_SHA1
1230Sstevel@tonic-gate } hash_type_t;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate * Keys ...
1270Sstevel@tonic-gate */
1280Sstevel@tonic-gate static encr_type_t encr_type = ENCR_NONE;
1290Sstevel@tonic-gate static unsigned char *g_encr_key = NULL;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate static hash_type_t hash_type = HASH_NONE;
1320Sstevel@tonic-gate static unsigned char *g_hash_key = NULL;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate void
print_errors(const char * func,http_handle_t handle)1350Sstevel@tonic-gate print_errors(const char *func, http_handle_t handle)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate char const *msg;
1380Sstevel@tonic-gate ulong_t err;
1390Sstevel@tonic-gate uint_t src;
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate while ((err = http_get_lasterr(handle, &src)) != 0) {
1420Sstevel@tonic-gate msg = http_errorstr(src, err);
1430Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT,
1440Sstevel@tonic-gate "%s: errsrc %u, err %lu (0x%lx)", func, src, err, err);
1450Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT, "%s", msg);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * This routine is called by a consumer to determine whether or not a
1510Sstevel@tonic-gate * retry should be attempted. If a retry is in order (depends upon the
1520Sstevel@tonic-gate * 'retry_cnt' and 'retry_max' arguments), then this routine will print a
1530Sstevel@tonic-gate * message indicating this is the case and will determine an appropriate
1540Sstevel@tonic-gate * "sleep" time before retrying. The "sleep" time will depend upon the
1550Sstevel@tonic-gate * 'retry_cnt' and will max out at WANBOOT_RETRY_MAX_SECS.
1560Sstevel@tonic-gate *
1570Sstevel@tonic-gate * Returns:
1580Sstevel@tonic-gate * B_TRUE = retry is in order
1590Sstevel@tonic-gate * B_FALSE = retry limit exceeded
1600Sstevel@tonic-gate */
1610Sstevel@tonic-gate boolean_t
wanboot_retry(int retry_cnt,int retry_max)1620Sstevel@tonic-gate wanboot_retry(int retry_cnt, int retry_max)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate unsigned int seconds;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate if (retry_max == WANBOOT_RETRY_NOMAX || retry_cnt <= retry_max) {
1670Sstevel@tonic-gate seconds = WANBOOT_RETRY_SECS * retry_cnt;
1680Sstevel@tonic-gate if (seconds > WANBOOT_RETRY_MAX_SECS) {
1690Sstevel@tonic-gate seconds = WANBOOT_RETRY_MAX_SECS;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_INFO,
1720Sstevel@tonic-gate "Will retry in %d seconds ...", seconds);
1730Sstevel@tonic-gate (void) sleep(seconds);
1740Sstevel@tonic-gate return (B_TRUE);
1750Sstevel@tonic-gate } else {
1760Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_INFO,
1770Sstevel@tonic-gate "Maximum retries exceeded.");
1780Sstevel@tonic-gate return (B_FALSE);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate * Determine which encryption algorithm the client is configured to use.
1840Sstevel@tonic-gate * WAN boot determines which key to use by order of priority. That is
1850Sstevel@tonic-gate * multiple encryption keys may exist in the PROM, but the first one found
1860Sstevel@tonic-gate * (while searching in a preferred order) is the one that will be used.
1870Sstevel@tonic-gate */
1880Sstevel@tonic-gate static void
init_encryption(void)1890Sstevel@tonic-gate init_encryption(void)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate static unsigned char key[WANBOOT_MAXKEYLEN];
1920Sstevel@tonic-gate size_t len = sizeof (key);
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate if (bootinfo_get(BI_AES_KEY, (char *)&key, &len, NULL) ==
1950Sstevel@tonic-gate BI_E_SUCCESS) {
1960Sstevel@tonic-gate encr_type = ENCR_AES;
1970Sstevel@tonic-gate g_encr_key = key;
1980Sstevel@tonic-gate } else if (bootinfo_get(BI_3DES_KEY, (char *)&key, &len, NULL) ==
1990Sstevel@tonic-gate BI_E_SUCCESS) {
2000Sstevel@tonic-gate encr_type = ENCR_3DES;
2010Sstevel@tonic-gate g_encr_key = key;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate * Determine whether the client is configured to use hashing.
2070Sstevel@tonic-gate */
2080Sstevel@tonic-gate static void
init_hashing(void)2090Sstevel@tonic-gate init_hashing(void)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate static unsigned char key[WANBOOT_HMAC_KEY_SIZE];
2120Sstevel@tonic-gate size_t len = sizeof (key);
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate if (bootinfo_get(BI_SHA1_KEY, (char *)&key, &len, NULL) ==
2150Sstevel@tonic-gate BI_E_SUCCESS) {
2160Sstevel@tonic-gate hash_type = HASH_HMAC_SHA1;
2170Sstevel@tonic-gate g_hash_key = key;
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate * Read some CPU-specific rapidly-varying data (assumed to be of length
2230Sstevel@tonic-gate * sizeof (hrtime_t) in the non-SPARC case), and digestify it to further
2240Sstevel@tonic-gate * randomize the output.
2250Sstevel@tonic-gate */
2260Sstevel@tonic-gate char *
generate_nonce(void)2270Sstevel@tonic-gate generate_nonce(void)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate uint64_t t;
2300Sstevel@tonic-gate SHA1_CTX c;
2310Sstevel@tonic-gate unsigned char digest[HMAC_DIGEST_LEN];
2320Sstevel@tonic-gate uint_t nlen = sizeof (nonce);
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate int err;
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate * Read SPARC %tick register or x86 TSC
2380Sstevel@tonic-gate */
2390Sstevel@tonic-gate t = get_ticks();
2400Sstevel@tonic-gate SHA1Init(&c);
2410Sstevel@tonic-gate SHA1Update(&c, (const uint8_t *)&t, sizeof (t));
2420Sstevel@tonic-gate SHA1Final(digest, &c);
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate err = octet_to_hexascii(digest, sizeof (digest), nonce, &nlen);
2450Sstevel@tonic-gate if (err != 0) {
2460Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
2470Sstevel@tonic-gate "cannot convert nonce to ASCII: error %d", err);
2480Sstevel@tonic-gate return (NULL);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate nonce[NONCELEN] = '\0';
2510Sstevel@tonic-gate return (nonce);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate * Given a server URL, builds a URL to request one of the wanboot
2560Sstevel@tonic-gate * datastreams.
2570Sstevel@tonic-gate *
2580Sstevel@tonic-gate * Returns:
2590Sstevel@tonic-gate * -1 = Non-recoverable error
2600Sstevel@tonic-gate * 0 = Success
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate static int
build_request_url(url_t * req_url,enum URLtype ut,const url_t * server_url)2630Sstevel@tonic-gate build_request_url(url_t *req_url, enum URLtype ut, const url_t *server_url)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate char clid[WB_MAX_CID_LEN];
2660Sstevel@tonic-gate size_t clen;
2670Sstevel@tonic-gate char wid[WB_MAX_CID_LEN * 2 + 1];
2680Sstevel@tonic-gate uint_t wlen;
2690Sstevel@tonic-gate struct in_addr ip;
2700Sstevel@tonic-gate struct in_addr mask;
2710Sstevel@tonic-gate char *netstr;
2720Sstevel@tonic-gate char *ppath;
2730Sstevel@tonic-gate size_t plen;
2740Sstevel@tonic-gate const char reqstr[] = "/?CONTENT=%s&IP=%s&CID=%s";
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate /*
2770Sstevel@tonic-gate * Initialize the request
2780Sstevel@tonic-gate */
2790Sstevel@tonic-gate *req_url = *server_url;
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate * Build the network number string
2830Sstevel@tonic-gate */
2840Sstevel@tonic-gate ipv4_getipaddr(&ip);
2850Sstevel@tonic-gate ipv4_getnetmask(&mask);
2860Sstevel@tonic-gate ip.s_addr = ip.s_addr & mask.s_addr;
2870Sstevel@tonic-gate netstr = inet_ntoa(ip);
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate * Get the wan id
2910Sstevel@tonic-gate */
2920Sstevel@tonic-gate clen = sizeof (clid);
2930Sstevel@tonic-gate if (bootinfo_get(BI_CLIENT_ID, clid, &clen, NULL) != BI_E_SUCCESS) {
2940Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
2950Sstevel@tonic-gate "Cannot retrieve the client ID");
2960Sstevel@tonic-gate return (-1);
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate wlen = sizeof (wid);
2990Sstevel@tonic-gate (void) octet_to_hexascii(clid, clen, wid, &wlen);
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate * Build the request, making sure that the length of the
3030Sstevel@tonic-gate * constructed URL falls within the supported maximum.
3040Sstevel@tonic-gate */
3050Sstevel@tonic-gate plen = strlen(req_url->abspath);
3060Sstevel@tonic-gate ppath = req_url->abspath + plen;
3070Sstevel@tonic-gate if (snprintf(ppath, URL_MAX_PATHLEN - plen, reqstr,
3080Sstevel@tonic-gate CGIcontent(ut), netstr, wid) >= URL_MAX_PATHLEN - plen) {
3090Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
3100Sstevel@tonic-gate "The URL path length of the %s request is greater than "
3110Sstevel@tonic-gate "the maximum of %d", CGIcontent(ut), URL_MAX_PATHLEN);
3120Sstevel@tonic-gate return (-1);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate * If the URL type requires a nonce, then supply it.
3170Sstevel@tonic-gate * It will be returned in the reply to detect attempted
3180Sstevel@tonic-gate * replays.
3190Sstevel@tonic-gate */
3200Sstevel@tonic-gate if (ut == URLtype_wanbootfs) {
3210Sstevel@tonic-gate char *n = generate_nonce();
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate if (n != NULL) {
3240Sstevel@tonic-gate plen += strlen("&NONCE=") + NONCELEN;
3250Sstevel@tonic-gate if (plen > URL_MAX_PATHLEN)
3260Sstevel@tonic-gate return (-1);
3270Sstevel@tonic-gate (void) strcat(req_url->abspath, "&NONCE=");
3280Sstevel@tonic-gate (void) strcat(req_url->abspath, n);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate return (0);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate /*
3360Sstevel@tonic-gate * This routine reads data from an HTTP connection into a buffer.
3370Sstevel@tonic-gate *
3380Sstevel@tonic-gate * Returns:
3390Sstevel@tonic-gate * 0 = Success
3400Sstevel@tonic-gate * 1 = HTTP download error
3410Sstevel@tonic-gate */
3420Sstevel@tonic-gate static int
read_bytes(http_handle_t handle,char * buffer,size_t cnt)3430Sstevel@tonic-gate read_bytes(http_handle_t handle, char *buffer, size_t cnt)
3440Sstevel@tonic-gate {
3450Sstevel@tonic-gate int len;
3460Sstevel@tonic-gate size_t i;
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate for (i = 0; i < cnt; i += len) {
3490Sstevel@tonic-gate len = http_read_body(handle, &buffer[i], cnt - i);
3500Sstevel@tonic-gate if (len <= 0) {
3510Sstevel@tonic-gate print_errors("http_read_body", handle);
3520Sstevel@tonic-gate return (1);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate return (0);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate * This routine compares two hash digests, one computed by the server and
3600Sstevel@tonic-gate * the other computed by the client to verify that a transmitted message
3610Sstevel@tonic-gate * was received without corruption.
3620Sstevel@tonic-gate *
3630Sstevel@tonic-gate * Notes:
3640Sstevel@tonic-gate * The client only computes a digest if it is configured with a
3650Sstevel@tonic-gate * hash key. If it is not, then the server should not have a hash
3660Sstevel@tonic-gate * key for the client either and therefore should have sent a
3670Sstevel@tonic-gate * zero filled digest.
3680Sstevel@tonic-gate *
3690Sstevel@tonic-gate * Returns:
3700Sstevel@tonic-gate * B_TRUE = digest was verified
3710Sstevel@tonic-gate * B_FALSE = digest did not verify
3720Sstevel@tonic-gate */
3730Sstevel@tonic-gate static boolean_t
verify_digests(const char * what,unsigned char * cdigest,unsigned char * sdigest)3740Sstevel@tonic-gate verify_digests(const char *what, unsigned char *cdigest, unsigned char *sdigest)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate static char null_digest[HMAC_DIGEST_LEN];
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate if (bcmp(sdigest, cdigest, HMAC_DIGEST_LEN) != 0) {
3790Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
3800Sstevel@tonic-gate "%s: invalid hash digest", what);
3810Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
3820Sstevel@tonic-gate "This may signify a client/server key mismatch");
3830Sstevel@tonic-gate if (bcmp(sdigest, null_digest, HMAC_DIGEST_LEN) == 0) {
3840Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
3850Sstevel@tonic-gate "(client has key but wrong signature_type?)");
3860Sstevel@tonic-gate } else if (bcmp(cdigest, null_digest, HMAC_DIGEST_LEN) == 0) {
3870Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
3880Sstevel@tonic-gate "(signature_type specified but no client key?)");
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
3910Sstevel@tonic-gate "or possible corruption of the image in transit");
3920Sstevel@tonic-gate return (B_FALSE);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate return (B_TRUE);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate /*
3990Sstevel@tonic-gate * This routine reads the part of a multipart message that contains a
4000Sstevel@tonic-gate * hash digest. Errors in reading the digest are differentiated from
4010Sstevel@tonic-gate * other kinds of errors so that the caller can decide whether or
4020Sstevel@tonic-gate * not a retry is worthwhile.
4030Sstevel@tonic-gate *
4040Sstevel@tonic-gate * Note:
4050Sstevel@tonic-gate * The hash digest can either be an HMAC digest or it can be
4060Sstevel@tonic-gate * a zero length message (representing no hash digest).
4070Sstevel@tonic-gate *
4080Sstevel@tonic-gate * Returns:
4090Sstevel@tonic-gate * -1 = Non-recoverable error
4100Sstevel@tonic-gate * 0 = Success
4110Sstevel@tonic-gate * 1 = HTTP download error
4120Sstevel@tonic-gate */
4130Sstevel@tonic-gate static int
read_digest(const char * what,http_handle_t handle,unsigned char * sdigest)4140Sstevel@tonic-gate read_digest(const char *what, http_handle_t handle, unsigned char *sdigest)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate char *lenstr;
4170Sstevel@tonic-gate size_t digest_size;
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * Process the HMAC digest header.
4210Sstevel@tonic-gate */
4220Sstevel@tonic-gate if (http_process_part_headers(handle, NULL) != 0) {
4230Sstevel@tonic-gate print_errors("http_process_part_headers", handle);
4240Sstevel@tonic-gate return (1);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate lenstr = http_get_header_value(handle, CONTENT_LENGTH);
4270Sstevel@tonic-gate if (lenstr == NULL) {
4280Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT,
4295648Ssetje "%s: error getting digest length", what);
4300Sstevel@tonic-gate return (1);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate digest_size = (size_t)strtol(lenstr, NULL, 10);
4330Sstevel@tonic-gate free(lenstr);
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate * Validate the HMAC digest length.
4370Sstevel@tonic-gate */
4380Sstevel@tonic-gate if (digest_size != HMAC_DIGEST_LEN) {
4390Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
4405648Ssetje "%s: error validating response - invalid digest size",
4415648Ssetje what);
4420Sstevel@tonic-gate return (-1);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate /*
4460Sstevel@tonic-gate * Read the HMAC digest.
4470Sstevel@tonic-gate */
4480Sstevel@tonic-gate if (read_bytes(handle, (char *)sdigest, digest_size) != 0) {
4490Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT,
4505648Ssetje "%s: error reading digest", what);
4510Sstevel@tonic-gate return (1);
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate return (0);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate /*
4580Sstevel@tonic-gate * This routine reads data from an HTTP connection and writes the data
4590Sstevel@tonic-gate * to a ramdisk. It also, optionally computes a hash digest of the processed
4600Sstevel@tonic-gate * data. This routine may be called to continue writing a previously aborted
4610Sstevel@tonic-gate * write. If this is the case, then the offset will be non-zero and the write
4620Sstevel@tonic-gate * pointer into the ramdisk will be positioned correctly by the caller.
4630Sstevel@tonic-gate *
4640Sstevel@tonic-gate * Returns:
4650Sstevel@tonic-gate * -1 = Non-recoverable error
4660Sstevel@tonic-gate * 0 = Success
4670Sstevel@tonic-gate * 1 = HTTP download error
4680Sstevel@tonic-gate */
4690Sstevel@tonic-gate static int
write_msg_to_ramdisk(const char * what,caddr_t addr,http_handle_t handle,size_t ramdisk_size,off_t * offset,SHA1_CTX * sha)4705648Ssetje write_msg_to_ramdisk(const char *what, caddr_t addr, http_handle_t handle,
4710Sstevel@tonic-gate size_t ramdisk_size, off_t *offset, SHA1_CTX *sha)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate int len;
4740Sstevel@tonic-gate long nleft;
4750Sstevel@tonic-gate static int bootlog_message_interval;
4760Sstevel@tonic-gate static int bootlog_progress;
4770Sstevel@tonic-gate int ret;
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate * Read the data and write it to the ramdisk.
4810Sstevel@tonic-gate */
4820Sstevel@tonic-gate if (*offset == 0) {
4830Sstevel@tonic-gate bootlog_progress = 0;
4840Sstevel@tonic-gate bootlog_message_interval = ramdisk_size / sizeof (buffer);
4850Sstevel@tonic-gate if (bootlog_message_interval < 500)
4860Sstevel@tonic-gate bootlog_message_interval /= 5;
4870Sstevel@tonic-gate else
4880Sstevel@tonic-gate bootlog_message_interval /= 50;
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE,
4910Sstevel@tonic-gate "Reading %s file system (%ld kB)",
4920Sstevel@tonic-gate what, ramdisk_size / 1024);
4930Sstevel@tonic-gate } else {
4940Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE,
4950Sstevel@tonic-gate "Continuing read of %s file system (%ld kB)",
4960Sstevel@tonic-gate what, ramdisk_size / 1024);
4970Sstevel@tonic-gate }
4985648Ssetje for (ret = 0; ret == 0 && *offset < ramdisk_size;
4995648Ssetje *offset += len, addr += len) {
5000Sstevel@tonic-gate nleft = ramdisk_size - *offset;
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate if (nleft > sizeof (buffer))
5030Sstevel@tonic-gate nleft = sizeof (buffer);
5040Sstevel@tonic-gate
5055648Ssetje len = http_read_body(handle, addr, nleft);
5060Sstevel@tonic-gate if (len <= 0) {
5070Sstevel@tonic-gate print_errors("http_read_body", handle);
5080Sstevel@tonic-gate /*
5090Sstevel@tonic-gate * In the case of a partial failure, http_read_body()
5100Sstevel@tonic-gate * returns into 'len', 1 - the number of bytes read.
5110Sstevel@tonic-gate * So, a -65 means 64 bytes read and an error occurred.
5120Sstevel@tonic-gate */
5130Sstevel@tonic-gate if (len != 0) {
5140Sstevel@tonic-gate len = -(len + 1);
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate ret = 1;
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate if (sha != NULL) {
5195648Ssetje HMACUpdate(sha, (uchar_t *)addr, (size_t)len);
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate if (bootlog_progress == bootlog_message_interval) {
5220Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_PROGRESS,
5230Sstevel@tonic-gate "%s: Read %ld of %ld kB (%ld%%)", what,
5240Sstevel@tonic-gate *offset / 1024, ramdisk_size / 1024,
5250Sstevel@tonic-gate *offset * 100 / ramdisk_size);
5260Sstevel@tonic-gate bootlog_progress = 0;
5270Sstevel@tonic-gate } else {
5280Sstevel@tonic-gate bootlog_progress++;
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate if (ret == 0) {
5320Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_PROGRESS,
5330Sstevel@tonic-gate "%s: Read %ld of %ld kB (%ld%%)", what,
5340Sstevel@tonic-gate *offset / 1024, ramdisk_size / 1024,
5350Sstevel@tonic-gate *offset * 100 / ramdisk_size);
5360Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_INFO, "%s: Download complete", what);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate return (ret);
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate /*
5420Sstevel@tonic-gate * This routine is called with a bootinfo parameter name. If the parameter
5430Sstevel@tonic-gate * has a value it should be a URL, and this will be used to initialize the
5440Sstevel@tonic-gate * http_url structure.
5450Sstevel@tonic-gate *
5460Sstevel@tonic-gate * Returns:
5470Sstevel@tonic-gate * -1 = Non-recoverable error
5480Sstevel@tonic-gate * 0 = Success
5490Sstevel@tonic-gate * 1 = DHCP option not set
5500Sstevel@tonic-gate */
5510Sstevel@tonic-gate static int
get_url(char * name,url_t * url)5520Sstevel@tonic-gate get_url(char *name, url_t *url)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate char buf[URL_MAX_STRLEN];
5550Sstevel@tonic-gate size_t len;
5560Sstevel@tonic-gate int ret;
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate bzero(buf, sizeof (buf));
5590Sstevel@tonic-gate len = sizeof (buf) - 1;
5600Sstevel@tonic-gate if (bootinfo_get(name, buf, &len, NULL) != BI_E_SUCCESS || len == 0) {
5610Sstevel@tonic-gate return (1);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate * Parse the URL.
5660Sstevel@tonic-gate */
5670Sstevel@tonic-gate ret = url_parse(buf, url);
5680Sstevel@tonic-gate if (ret != URL_PARSE_SUCCESS) {
5690Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
5700Sstevel@tonic-gate "Unable to parse URL %s", buf);
5710Sstevel@tonic-gate return (-1);
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate return (0);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate * This routine initiates an HTTP request and returns a handle so that
5790Sstevel@tonic-gate * the caller can process the response.
5800Sstevel@tonic-gate *
5810Sstevel@tonic-gate * Notes:
5820Sstevel@tonic-gate * Requests may be either secure or not. If the request is secure, then
5830Sstevel@tonic-gate * this routine assumes that a wanboot file system exists and
5840Sstevel@tonic-gate * uses its contents to provide the HTTP library with the information
5850Sstevel@tonic-gate * that will be required by SSL.
5860Sstevel@tonic-gate *
5870Sstevel@tonic-gate * In order to facilitate transmission retries, this routine supports
5880Sstevel@tonic-gate * range requests. A caller may request a range by providing a non-zero
5890Sstevel@tonic-gate * offset. In which case, a range request is made that ranges from the
5900Sstevel@tonic-gate * offet to the end of the file.
5910Sstevel@tonic-gate *
5920Sstevel@tonic-gate * If the client is configured to use an HTTP proxy, then this routine
5930Sstevel@tonic-gate * will make the HTTP library aware of the proxy.
5940Sstevel@tonic-gate *
5950Sstevel@tonic-gate * Any HTTP errors encountered in downloading or processing the message
5960Sstevel@tonic-gate * are not deemed unrecoverable errors. The caller can simply try the
5970Sstevel@tonic-gate * request once again.
5980Sstevel@tonic-gate *
5990Sstevel@tonic-gate * Returns:
6000Sstevel@tonic-gate * -1 = Non-recoverable error
6010Sstevel@tonic-gate * 0 = Success
6020Sstevel@tonic-gate * 1 = HTTP download error
6030Sstevel@tonic-gate */
6040Sstevel@tonic-gate static int
establish_http_connection(const char * what,http_handle_t * handlep,url_t * url,offset_t offset)6050Sstevel@tonic-gate establish_http_connection(const char *what, http_handle_t *handlep,
6061279Svh115876 url_t *url, offset_t offset)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate static boolean_t is_auth_file_init = B_FALSE;
6090Sstevel@tonic-gate static boolean_t is_proxy_init = B_FALSE;
6100Sstevel@tonic-gate static boolean_t proxy_exists = B_FALSE;
6110Sstevel@tonic-gate static url_hport_t proxy_hp;
6120Sstevel@tonic-gate http_respinfo_t *resp;
6130Sstevel@tonic-gate char buf[URL_MAX_STRLEN];
6140Sstevel@tonic-gate size_t len = sizeof (buf) - 1;
6150Sstevel@tonic-gate int ret;
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate /* Check for HTTP proxy */
6180Sstevel@tonic-gate if (!is_proxy_init &&
6190Sstevel@tonic-gate bootinfo_get(BI_HTTP_PROXY, buf, &len, NULL) == BI_E_SUCCESS &&
6200Sstevel@tonic-gate strlen(buf) > 0) {
6210Sstevel@tonic-gate /*
6220Sstevel@tonic-gate * Parse the hostport.
6230Sstevel@tonic-gate */
6240Sstevel@tonic-gate ret = url_parse_hostport(buf, &proxy_hp, URL_DFLT_PROXY_PORT);
6250Sstevel@tonic-gate if (ret == URL_PARSE_SUCCESS) {
6260Sstevel@tonic-gate proxy_exists = B_TRUE;
6270Sstevel@tonic-gate } else {
6280Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
6290Sstevel@tonic-gate "%s is not set to a valid hostport value",
6300Sstevel@tonic-gate BI_HTTP_PROXY);
6310Sstevel@tonic-gate return (-1);
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate is_proxy_init = B_TRUE;
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate
6360Sstevel@tonic-gate http_set_p12_format(use_p12);
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate /*
6390Sstevel@tonic-gate * Initialize the handle that will be used for the request.
6400Sstevel@tonic-gate */
6410Sstevel@tonic-gate *handlep = http_srv_init(url);
6420Sstevel@tonic-gate if (*handlep == NULL) {
6430Sstevel@tonic-gate print_errors("http_srv_init", NULL);
6440Sstevel@tonic-gate return (-1);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate /*
6480Sstevel@tonic-gate * Is the request a secure one? If it is, then we need to do further
6490Sstevel@tonic-gate * setup. Search the wanboot file system for files that will be
6500Sstevel@tonic-gate * needed by SSL.
6510Sstevel@tonic-gate */
6520Sstevel@tonic-gate if (url->https) {
6530Sstevel@tonic-gate char *cas;
6540Sstevel@tonic-gate boolean_t client_authentication = B_FALSE;
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate if (http_set_random_file(*handlep, "/dev/urandom") < 0) {
6570Sstevel@tonic-gate print_errors("http_set_random_file", *handlep);
6580Sstevel@tonic-gate (void) http_srv_close(*handlep);
6590Sstevel@tonic-gate return (-1);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate /*
6630Sstevel@tonic-gate * We only need to initialize the CA once as it is not handle
6640Sstevel@tonic-gate * specific.
6650Sstevel@tonic-gate */
6660Sstevel@tonic-gate if (!is_auth_file_init) {
6670Sstevel@tonic-gate if (http_set_certificate_authority_file(NB_CA_CERT_PATH)
6680Sstevel@tonic-gate < 0) {
6690Sstevel@tonic-gate print_errors(
6700Sstevel@tonic-gate "http_set_certificate_authority_file",
6710Sstevel@tonic-gate *handlep);
6720Sstevel@tonic-gate (void) http_srv_close(*handlep);
6730Sstevel@tonic-gate return (-1);
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate is_auth_file_init = B_TRUE;
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate /*
6800Sstevel@tonic-gate * The client certificate and key will not exist unless
6810Sstevel@tonic-gate * client authentication has been configured. If it is
6820Sstevel@tonic-gate * configured then the webserver will have added these
6830Sstevel@tonic-gate * files to the wanboot file system and the HTTP library
6840Sstevel@tonic-gate * needs to be made aware of their existence.
6850Sstevel@tonic-gate */
6860Sstevel@tonic-gate if ((cas = bootconf_get(&bc_handle,
6870Sstevel@tonic-gate BC_CLIENT_AUTHENTICATION)) != NULL &&
6880Sstevel@tonic-gate strcmp(cas, "yes") == 0) {
6890Sstevel@tonic-gate client_authentication = B_TRUE;
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate if (http_set_client_certificate_file(*handlep,
6920Sstevel@tonic-gate NB_CLIENT_CERT_PATH) < 0) {
6930Sstevel@tonic-gate print_errors("http_set_client_certificate_file",
6940Sstevel@tonic-gate *handlep);
6950Sstevel@tonic-gate (void) http_srv_close(*handlep);
6960Sstevel@tonic-gate return (-1);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate if (http_set_private_key_file(*handlep,
7000Sstevel@tonic-gate NB_CLIENT_KEY_PATH) < 0) {
7010Sstevel@tonic-gate print_errors("http_set_private_key_file",
7020Sstevel@tonic-gate *handlep);
7030Sstevel@tonic-gate (void) http_srv_close(*handlep);
7040Sstevel@tonic-gate return (-1);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate /*
7090Sstevel@tonic-gate * We do not really need to set this unless client
7100Sstevel@tonic-gate * authentication is configured or unless pkcs12 files
7110Sstevel@tonic-gate * are used.
7120Sstevel@tonic-gate */
7130Sstevel@tonic-gate if ((client_authentication || use_p12) &&
7140Sstevel@tonic-gate http_set_password(*handlep, WANBOOT_PASSPHRASE) < 0) {
7150Sstevel@tonic-gate print_errors("http_set_password", *handlep);
7160Sstevel@tonic-gate (void) http_srv_close(*handlep);
7170Sstevel@tonic-gate return (-1);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate /*
7220Sstevel@tonic-gate * If the client is using a proxy, tell the library.
7230Sstevel@tonic-gate */
7240Sstevel@tonic-gate if (proxy_exists) {
7250Sstevel@tonic-gate if (http_set_proxy(*handlep, &proxy_hp) != 0) {
7260Sstevel@tonic-gate print_errors("http_set_proxy", *handlep);
7270Sstevel@tonic-gate (void) http_srv_close(*handlep);
7280Sstevel@tonic-gate return (-1);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate (void) http_set_socket_read_timeout(*handlep, SOCKET_READ_TIMEOUT);
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate /*
7350Sstevel@tonic-gate * Ok, connect to the webserver.
7360Sstevel@tonic-gate */
7370Sstevel@tonic-gate if (http_srv_connect(*handlep) == -1) {
7380Sstevel@tonic-gate print_errors("http_srv_connect", *handlep);
7390Sstevel@tonic-gate (void) http_srv_close(*handlep);
7400Sstevel@tonic-gate return (1);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate /*
7440Sstevel@tonic-gate * If the offset is 0, then we assume that we want the entire
7450Sstevel@tonic-gate * message. If the offset is not 0, then we assume that we are
7460Sstevel@tonic-gate * retrying a previously interrupted transfer and thus we make
7470Sstevel@tonic-gate * a range request.
7480Sstevel@tonic-gate */
7490Sstevel@tonic-gate if (offset == 0) {
7500Sstevel@tonic-gate if ((ret = http_get_request(*handlep, url->abspath)) == 0) {
7510Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE,
7520Sstevel@tonic-gate "%s: http_get_request: sent", what);
7530Sstevel@tonic-gate } else {
7540Sstevel@tonic-gate print_errors("http_get_request", *handlep);
7550Sstevel@tonic-gate (void) http_srv_close(*handlep);
7560Sstevel@tonic-gate return (1);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate } else {
7590Sstevel@tonic-gate if ((ret = http_get_range_request(*handlep, url->abspath,
7600Sstevel@tonic-gate offset, 0)) == 0) {
7610Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE,
7620Sstevel@tonic-gate "%s: http_get_range_request: sent", what);
7630Sstevel@tonic-gate } else {
7640Sstevel@tonic-gate print_errors("http_get_range_request", *handlep);
7650Sstevel@tonic-gate (void) http_srv_close(*handlep);
7660Sstevel@tonic-gate return (1);
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate
7700Sstevel@tonic-gate /*
7710Sstevel@tonic-gate * Tell the library to read in the response headers.
7720Sstevel@tonic-gate */
7730Sstevel@tonic-gate ret = http_process_headers(*handlep, &resp);
7740Sstevel@tonic-gate if (ret == -1) {
7750Sstevel@tonic-gate print_errors("http_process_headers", *handlep);
7760Sstevel@tonic-gate (void) http_srv_close(*handlep);
7770Sstevel@tonic-gate return (1);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate /*
7810Sstevel@tonic-gate * Check for a valid response code.
7820Sstevel@tonic-gate */
7830Sstevel@tonic-gate if ((offset == 0 && resp->code != 200) ||
7840Sstevel@tonic-gate (offset != 0 && resp->code != 206)) {
7850Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT,
7865648Ssetje "%s: Request returned code %d", what, resp->code);
7870Sstevel@tonic-gate if (resp->statusmsg != NULL && resp->statusmsg[0] != '\0')
7880Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT,
7895648Ssetje "%s", resp->statusmsg);
7900Sstevel@tonic-gate http_free_respinfo(resp);
7910Sstevel@tonic-gate (void) http_srv_close(*handlep);
7920Sstevel@tonic-gate return (1);
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate http_free_respinfo(resp);
7950Sstevel@tonic-gate
7960Sstevel@tonic-gate /*
7970Sstevel@tonic-gate * Success.
7980Sstevel@tonic-gate */
7990Sstevel@tonic-gate return (0);
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate /*
8030Sstevel@tonic-gate * This routine is called by get_miniinfo() to receive the reply
8040Sstevel@tonic-gate * to the request for the miniroot metadata. The reply is a two
8050Sstevel@tonic-gate * part multipart message. The first part of the message contains
8060Sstevel@tonic-gate * the miniroot file size. The second part of the message contains
8070Sstevel@tonic-gate * a hash digest of the miniroot as computed by the server. This
8080Sstevel@tonic-gate * routine receives both message parts and returns them to the caller.
8090Sstevel@tonic-gate *
8100Sstevel@tonic-gate * Notes:
8110Sstevel@tonic-gate * If the miniroot is going to be downloaded securely or if the
8120Sstevel@tonic-gate * the server has no hash key for the client, then the hash digest
8130Sstevel@tonic-gate * downloaded contains all zeros.
8140Sstevel@tonic-gate *
8150Sstevel@tonic-gate * Any HTTP errors encountered in downloading or processing the message
8160Sstevel@tonic-gate * are not deemed unrecoverable errors. That is, get_miniinfo()
8170Sstevel@tonic-gate * tries re-requesting the message and tries processing it again.
8180Sstevel@tonic-gate *
8190Sstevel@tonic-gate * Returns:
8200Sstevel@tonic-gate * -1 = Non-recoverable error
8210Sstevel@tonic-gate * 0 = Success
8220Sstevel@tonic-gate * 1 = HTTP download error
8230Sstevel@tonic-gate */
8240Sstevel@tonic-gate static int
process_miniinfo(http_handle_t handle,size_t * mini_size,unsigned char * sdigest)8250Sstevel@tonic-gate process_miniinfo(http_handle_t handle, size_t *mini_size,
8260Sstevel@tonic-gate unsigned char *sdigest)
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate char *lenstr;
8290Sstevel@tonic-gate size_t cnt;
8300Sstevel@tonic-gate
8310Sstevel@tonic-gate /*
8320Sstevel@tonic-gate * Process the file size header.
8330Sstevel@tonic-gate */
8340Sstevel@tonic-gate if (http_process_part_headers(handle, NULL) != 0) {
8350Sstevel@tonic-gate print_errors("http_process_part_headers", handle);
8360Sstevel@tonic-gate return (1);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate lenstr = http_get_header_value(handle, CONTENT_LENGTH);
8390Sstevel@tonic-gate if (lenstr == NULL) {
8400Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT, "%s: error getting length "
8410Sstevel@tonic-gate "of first part of multipart message", MINIINFO);
8420Sstevel@tonic-gate return (1);
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate cnt = (size_t)strtol(lenstr, NULL, 10);
8450Sstevel@tonic-gate free(lenstr);
8460Sstevel@tonic-gate if (cnt == 0 || cnt >= sizeof (buffer)) {
8470Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT, "%s: length of first part "
8480Sstevel@tonic-gate "of multipart message not a legal size", MINIINFO);
8490Sstevel@tonic-gate return (1);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate
8520Sstevel@tonic-gate if (read_bytes(handle, buffer, cnt) != 0) {
8530Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT,
8540Sstevel@tonic-gate "%s: error reading miniroot size", MINIINFO);
8550Sstevel@tonic-gate return (1);
8560Sstevel@tonic-gate }
8570Sstevel@tonic-gate buffer[cnt] = '\0';
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate *mini_size = (size_t)strtol(buffer, NULL, 10);
8600Sstevel@tonic-gate if (*mini_size == 0) {
8610Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT, "%s: body of first part "
8620Sstevel@tonic-gate "of multipart message not a legal size", MINIINFO);
8630Sstevel@tonic-gate return (1);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate return (read_digest(MINIINFO, handle, sdigest));
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate /*
8700Sstevel@tonic-gate * This routine is called by get_miniroot() to retrieve the miniroot
8710Sstevel@tonic-gate * metadata (miniroot size and a hash digest). This routine sends an
8720Sstevel@tonic-gate * HTTP GET request to the webserver to request the download of the
8730Sstevel@tonic-gate * miniroot metadata and relies on process_miniinfo() to receive the
8740Sstevel@tonic-gate * reply, process it and ultimately return to it the miniroot size and
8750Sstevel@tonic-gate * the hash digest.
8760Sstevel@tonic-gate *
8770Sstevel@tonic-gate * Note:
8780Sstevel@tonic-gate * Any HTTP errors encountered in downloading or processing the message
8790Sstevel@tonic-gate * are not deemed unrecoverable errors. That is, get_miniinfo() should
8800Sstevel@tonic-gate * try re-requesting the message and try processing again.
8810Sstevel@tonic-gate *
8820Sstevel@tonic-gate * Returns:
8830Sstevel@tonic-gate * -1 = Non-recoverable error
8840Sstevel@tonic-gate * 0 = Success
8850Sstevel@tonic-gate */
8860Sstevel@tonic-gate int
get_miniinfo(const url_t * server_url,size_t * mini_size,unsigned char * sdigest)8870Sstevel@tonic-gate get_miniinfo(const url_t *server_url, size_t *mini_size,
8880Sstevel@tonic-gate unsigned char *sdigest)
8890Sstevel@tonic-gate {
8900Sstevel@tonic-gate http_handle_t handle;
8910Sstevel@tonic-gate url_t req_url;
8920Sstevel@tonic-gate int retry_cnt = 0;
8930Sstevel@tonic-gate int retry_max = WANBOOT_RETRY_MAX;
8940Sstevel@tonic-gate int ret;
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate /*
8970Sstevel@tonic-gate * Build the URL to request the miniroot info.
8980Sstevel@tonic-gate */
8990Sstevel@tonic-gate if (build_request_url(&req_url, URLtype_miniroot, server_url) == -1) {
9000Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
9010Sstevel@tonic-gate "Can't build the URL to make the %s request",
9020Sstevel@tonic-gate CGIcontent(URLtype_miniroot));
9030Sstevel@tonic-gate return (-1);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate
9060Sstevel@tonic-gate /*
9070Sstevel@tonic-gate * Go get the miniroot info. If we fail reading the
9080Sstevel@tonic-gate * response we re-request the info in its entirety.
9090Sstevel@tonic-gate */
9100Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE, "Downloading miniroot info");
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate do {
9130Sstevel@tonic-gate if ((ret = establish_http_connection(MINIINFO, &handle,
9140Sstevel@tonic-gate &req_url, 0)) < 0) {
9150Sstevel@tonic-gate break;
9160Sstevel@tonic-gate } else if (ret > 0) {
9170Sstevel@tonic-gate if (wanboot_retry(++retry_cnt, retry_max)) {
9180Sstevel@tonic-gate continue;
9190Sstevel@tonic-gate } else {
9200Sstevel@tonic-gate break;
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate
9240Sstevel@tonic-gate if ((ret = process_miniinfo(handle, mini_size,
9255648Ssetje sdigest)) > 0) {
9260Sstevel@tonic-gate if (!wanboot_retry(++retry_cnt, retry_max)) {
9270Sstevel@tonic-gate (void) http_srv_close(handle);
9280Sstevel@tonic-gate break;
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate
9320Sstevel@tonic-gate (void) http_srv_close(handle);
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate } while (ret > 0);
9350Sstevel@tonic-gate
9360Sstevel@tonic-gate /*
9370Sstevel@tonic-gate * Success.
9380Sstevel@tonic-gate */
9390Sstevel@tonic-gate if (ret == 0) {
9400Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE,
9410Sstevel@tonic-gate "Miniroot info download successful");
9420Sstevel@tonic-gate return (0);
9430Sstevel@tonic-gate } else {
9440Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
9450Sstevel@tonic-gate "Miniroot info download aborted");
9460Sstevel@tonic-gate return (-1);
9470Sstevel@tonic-gate }
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate
9500Sstevel@tonic-gate /*
9510Sstevel@tonic-gate * This routine is called by get_miniroot() to receive the reply to
9520Sstevel@tonic-gate * the request for the miniroot download. The miniroot is written
9530Sstevel@tonic-gate * to ramdisk as it is received and a hash digest is optionally computed
9540Sstevel@tonic-gate * as it does so. The miniroot is downloaded as one large message.
9550Sstevel@tonic-gate * Because the message is so large, this routine is prepared to deal
9560Sstevel@tonic-gate * with errors in the middle of download. If an error occurs during
9570Sstevel@tonic-gate * download, then this message processes all received data up to the
9580Sstevel@tonic-gate * point of the error and returns to get_miniroot() an error signifying
9590Sstevel@tonic-gate * that a download error has occurred. Presumably, get_miniroot()
9600Sstevel@tonic-gate * re-requests the remaining part of the miniroot not yet processed and
9610Sstevel@tonic-gate * calls this routine back to process the reply. When this routine
9620Sstevel@tonic-gate * returns succesfully, it returns a devpath to the ramdisk and the
9630Sstevel@tonic-gate * computed hash (if computed).
9640Sstevel@tonic-gate *
9650Sstevel@tonic-gate * Note:
9660Sstevel@tonic-gate * In order to facilitate reentry, the ramdisk is left open
9670Sstevel@tonic-gate * and the original miniroot_size and HMAC handle are kept
9680Sstevel@tonic-gate * static.
9690Sstevel@tonic-gate *
9700Sstevel@tonic-gate * Returns:
9710Sstevel@tonic-gate * -1 = Non-recoverable error
9720Sstevel@tonic-gate * 0 = Success
9730Sstevel@tonic-gate * 1 = HTTP download error
9740Sstevel@tonic-gate */
9750Sstevel@tonic-gate static int
process_miniroot(http_handle_t handle,hash_type_t htype,size_t length,char ** devpath,off_t * offset,unsigned char * cdigest)9760Sstevel@tonic-gate process_miniroot(http_handle_t handle, hash_type_t htype,
9770Sstevel@tonic-gate size_t length, char **devpath, off_t *offset, unsigned char *cdigest)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate static SHA1_CTX sha;
9800Sstevel@tonic-gate static size_t miniroot_size;
9815648Ssetje static caddr_t miniroot_vaddr = NULL;
9820Sstevel@tonic-gate int ret;
9830Sstevel@tonic-gate
9845648Ssetje if (miniroot_vaddr == NULL) {
9850Sstevel@tonic-gate if (htype == HASH_HMAC_SHA1) {
9860Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_INFO,
9870Sstevel@tonic-gate "%s: Authentication will use HMAC-SHA1", MINIROOT);
9880Sstevel@tonic-gate HMACInit(&sha, g_hash_key, WANBOOT_HMAC_KEY_SIZE);
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate
9910Sstevel@tonic-gate miniroot_size = length;
9920Sstevel@tonic-gate
9935648Ssetje miniroot_vaddr = create_ramdisk(RD_ROOTFS, miniroot_size,
9945648Ssetje devpath);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate
9975648Ssetje miniroot_vaddr += *offset;
9985648Ssetje
9995648Ssetje if ((ret = write_msg_to_ramdisk(MINIROOT, miniroot_vaddr, handle,
10005648Ssetje miniroot_size, offset, (htype == HASH_NONE) ? NULL : &sha)) != 0) {
10010Sstevel@tonic-gate return (ret);
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate
10040Sstevel@tonic-gate if (htype != HASH_NONE) {
10050Sstevel@tonic-gate HMACFinal(&sha, g_hash_key, WANBOOT_HMAC_KEY_SIZE, cdigest);
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate
10080Sstevel@tonic-gate return (0);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate
10110Sstevel@tonic-gate /*
10120Sstevel@tonic-gate * This routine retrieves the miniroot from the webserver. The miniroot
10130Sstevel@tonic-gate * is retrieved in two steps. First a request is made to the server
10140Sstevel@tonic-gate * to retrieve miniroot metadata (miniroot size and a hash digest).
10150Sstevel@tonic-gate * The second request actually results in the download of the miniroot.
10160Sstevel@tonic-gate *
10170Sstevel@tonic-gate * This routine relies on get_miniinfo() to make and process
10180Sstevel@tonic-gate * the request for the miniroot metadata and returns the
10190Sstevel@tonic-gate * miniroot size and the hash digest of the miniroot as computed by
10200Sstevel@tonic-gate * the server.
10210Sstevel@tonic-gate *
10220Sstevel@tonic-gate * If get_miniinfo() returns successfully, then this routine sends
10230Sstevel@tonic-gate * an HTTP GET request to the webserver to request download of the
10240Sstevel@tonic-gate * miniroot. This routine relies on process_miniroot() to receive
10250Sstevel@tonic-gate * the reply, process it and ultimately return to it a device path to
10260Sstevel@tonic-gate * a ramdisk containing the miniroot and a client computed hash digest.
10270Sstevel@tonic-gate * This routine verifies that the client computed hash digest matches
10280Sstevel@tonic-gate * the one retrieved by get_miniinfo().
10290Sstevel@tonic-gate *
10300Sstevel@tonic-gate * If an error occurs in the transfer of the miniroot from the server
10310Sstevel@tonic-gate * to the client, then the client re-requests the download of the
10320Sstevel@tonic-gate * miniroot using a range request and only requests the part of the
10330Sstevel@tonic-gate * miniroot not previously downloaded and written to ramdisk. The
10340Sstevel@tonic-gate * process_miniroot() routine has the intelligence to recognize that
10350Sstevel@tonic-gate * it is processing a range request. Errors not related to the actual
10360Sstevel@tonic-gate * message download are deemed unrecoverable.
10370Sstevel@tonic-gate *
10380Sstevel@tonic-gate * Note:
10390Sstevel@tonic-gate * If the client request for the miniroot is a secure request or
10400Sstevel@tonic-gate * if the server is not configured with a hash key for the client,
10410Sstevel@tonic-gate * then the hash digest downloaded from the server will contain
10420Sstevel@tonic-gate * all zeros. This routine verifies that the server and client are
10430Sstevel@tonic-gate * in-sync with respect to the need for hash verification.
10440Sstevel@tonic-gate *
10450Sstevel@tonic-gate * Returns:
10460Sstevel@tonic-gate * -1 = Non-recoverable error
10470Sstevel@tonic-gate * 0 = Success
10480Sstevel@tonic-gate */
10490Sstevel@tonic-gate int
get_miniroot(char ** devpath)10500Sstevel@tonic-gate get_miniroot(char **devpath)
10510Sstevel@tonic-gate {
10520Sstevel@tonic-gate http_handle_t handle;
10530Sstevel@tonic-gate unsigned char cdigest[HMAC_DIGEST_LEN];
10540Sstevel@tonic-gate unsigned char sdigest[HMAC_DIGEST_LEN];
10550Sstevel@tonic-gate char *urlstr;
10560Sstevel@tonic-gate url_t server_url;
10570Sstevel@tonic-gate size_t mini_size;
10580Sstevel@tonic-gate off_t offset;
10590Sstevel@tonic-gate int plen;
10600Sstevel@tonic-gate int retry_cnt = 0;
10610Sstevel@tonic-gate int retry_max = WANBOOT_RETRY_ROOT_MAX;
10620Sstevel@tonic-gate int ret;
10630Sstevel@tonic-gate
10640Sstevel@tonic-gate /*
10650Sstevel@tonic-gate * Get the miniroot URL.
10660Sstevel@tonic-gate */
10670Sstevel@tonic-gate if ((urlstr = bootconf_get(&bc_handle, BC_ROOT_SERVER)) == NULL) {
10680Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
10690Sstevel@tonic-gate "Missing root_server URL");
10700Sstevel@tonic-gate return (-1);
10710Sstevel@tonic-gate } else if (url_parse(urlstr, &server_url) != URL_PARSE_SUCCESS) {
10720Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
10730Sstevel@tonic-gate "Unable to parse URL %s", urlstr);
10740Sstevel@tonic-gate return (-1);
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate
10770Sstevel@tonic-gate /*
10780Sstevel@tonic-gate * We must get the miniroot info before we can request
10790Sstevel@tonic-gate * the miniroot itself.
10800Sstevel@tonic-gate */
10810Sstevel@tonic-gate if (get_miniinfo(&server_url, &mini_size, sdigest) != 0) {
10820Sstevel@tonic-gate return (-1);
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate plen = sizeof (server_url.abspath);
10860Sstevel@tonic-gate if ((urlstr = bootconf_get(&bc_handle, BC_ROOT_FILE)) == NULL ||
10870Sstevel@tonic-gate strlcpy(server_url.abspath, urlstr, plen) >= plen) {
10880Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
10890Sstevel@tonic-gate "Cannot retrieve the miniroot path");
10900Sstevel@tonic-gate return (-1);
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate
10930Sstevel@tonic-gate /*
10940Sstevel@tonic-gate * Go get the miniroot. If we fail reading the response
10950Sstevel@tonic-gate * then we re-request only the range we have yet to read,
10960Sstevel@tonic-gate * unless the error was "unrecoverable" in which case we
10970Sstevel@tonic-gate * re-request the entire file system.
10980Sstevel@tonic-gate */
10990Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE, "Downloading miniroot");
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate bzero(cdigest, sizeof (cdigest));
11020Sstevel@tonic-gate offset = 0;
11030Sstevel@tonic-gate do {
11040Sstevel@tonic-gate if ((ret = establish_http_connection(MINIROOT, &handle,
11050Sstevel@tonic-gate &server_url, offset)) < 0) {
11060Sstevel@tonic-gate break;
11070Sstevel@tonic-gate } else if (ret > 0) {
11080Sstevel@tonic-gate if (wanboot_retry(++retry_cnt, retry_max)) {
11090Sstevel@tonic-gate continue;
11100Sstevel@tonic-gate } else {
11110Sstevel@tonic-gate break;
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate }
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate if ((ret = process_miniroot(handle,
11160Sstevel@tonic-gate server_url.https ? HASH_NONE : hash_type,
11170Sstevel@tonic-gate mini_size, devpath, &offset, cdigest)) > 0) {
11180Sstevel@tonic-gate if (!wanboot_retry(++retry_cnt, retry_max)) {
11190Sstevel@tonic-gate (void) http_srv_close(handle);
11200Sstevel@tonic-gate break;
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate (void) http_srv_close(handle);
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate } while (ret > 0);
11270Sstevel@tonic-gate
11280Sstevel@tonic-gate /*
11290Sstevel@tonic-gate * Validate the computed digest against the one received.
11300Sstevel@tonic-gate */
11310Sstevel@tonic-gate if (ret != 0 || !verify_digests(MINIROOT, cdigest, sdigest)) {
11320Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
11330Sstevel@tonic-gate "Miniroot download aborted");
11340Sstevel@tonic-gate return (-1);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate
11370Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE, "Miniroot download successful");
11380Sstevel@tonic-gate return (0);
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate /*
11420Sstevel@tonic-gate * This routine is called to finish the decryption process.
11430Sstevel@tonic-gate * Its purpose is to free the resources allocated by the
11440Sstevel@tonic-gate * encryption init routines.
11450Sstevel@tonic-gate */
11460Sstevel@tonic-gate static void
encr_fini(encr_type_t etype,void * eh)11470Sstevel@tonic-gate encr_fini(encr_type_t etype, void *eh)
11480Sstevel@tonic-gate {
11490Sstevel@tonic-gate switch (etype) {
11500Sstevel@tonic-gate case ENCR_3DES:
11510Sstevel@tonic-gate des3_fini(eh);
11520Sstevel@tonic-gate break;
11530Sstevel@tonic-gate case ENCR_AES:
11540Sstevel@tonic-gate aes_fini(eh);
11550Sstevel@tonic-gate break;
11560Sstevel@tonic-gate default:
11570Sstevel@tonic-gate break;
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate
11610Sstevel@tonic-gate /*
11625648Ssetje * This routine is called by process_wanbootfs() to decrypt the encrypted
11635648Ssetje * file system from ramdisk in place. The method of decryption
11640Sstevel@tonic-gate * (algorithm) will have already been determined by process_wanbootfs()
11650Sstevel@tonic-gate * and the cbc_handle passed to this routine will already have been
11660Sstevel@tonic-gate * initialized appropriately.
11670Sstevel@tonic-gate *
11680Sstevel@tonic-gate * Returns:
11690Sstevel@tonic-gate * -1 = Non-recoverable error
11700Sstevel@tonic-gate * 0 = Success
11710Sstevel@tonic-gate */
11720Sstevel@tonic-gate static int
decrypt_wanbootfs(caddr_t addr,cbc_handle_t * ch,uint8_t * iv,size_t wanbootfs_size)11735648Ssetje decrypt_wanbootfs(caddr_t addr, cbc_handle_t *ch, uint8_t *iv,
11745648Ssetje size_t wanbootfs_size)
11750Sstevel@tonic-gate {
11765648Ssetje if (!cbc_decrypt(ch, (uint8_t *)addr, wanbootfs_size, iv)) {
11775648Ssetje bootlog("wanboot", BOOTLOG_CRIT,
11785648Ssetje "%s: cbc decrypt error", WANBOOTFS);
11795648Ssetje return (-1);
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate return (0);
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate /*
11850Sstevel@tonic-gate * This routine is called by get_wanbootfs() to receive the reply to
11860Sstevel@tonic-gate * the request for the wanboot file system. The reply is a multipart message.
11870Sstevel@tonic-gate * The first part of the message is the file system (which may or may
11880Sstevel@tonic-gate * not be encrypted). If encrypted, then the first block of the message
11890Sstevel@tonic-gate * part is the CBC IV value used by the server to encrypt the remaining
11900Sstevel@tonic-gate * part of the message part and is used by the client to decrypt it. The
11910Sstevel@tonic-gate * second message part is a hash digest of the first part (the file
11920Sstevel@tonic-gate * system) as computed by the server. If no hash key is configured
11930Sstevel@tonic-gate * for the client, then the hash digest simply contains all zeros. This
11940Sstevel@tonic-gate * routine receives both message parts. The file system is written to ramdisk
11950Sstevel@tonic-gate * as it is received and simultaneously computes a hash digest (if a hash
11960Sstevel@tonic-gate * key exists). Once the entire part is received, if the file system is
11970Sstevel@tonic-gate * encrypted, it is read from ramdisk, decrypted and rewritten back to
11980Sstevel@tonic-gate * ramdisk. The server computed hash digest is then read and along with the
11990Sstevel@tonic-gate * ramdisk device path and the client computed hash digest is returned to the
12000Sstevel@tonic-gate * caller.
12010Sstevel@tonic-gate *
12020Sstevel@tonic-gate * Notes:
12030Sstevel@tonic-gate * In order to decrypt the file system and to compute the client
12040Sstevel@tonic-gate * hash digest, an encryption key and a hash key is retrieved from
12050Sstevel@tonic-gate * the PROM (or the wanboot interpreter). The non-existence of these
12060Sstevel@tonic-gate * keys has implications on how the message response is processed and
12070Sstevel@tonic-gate * it is assumed that the server is configured identically.
12080Sstevel@tonic-gate *
12090Sstevel@tonic-gate * Any HTTP errors encountered in downloading or processing the message
12100Sstevel@tonic-gate * are not deemed unrecoverable errors. That is, get_wanbootfs() will
12110Sstevel@tonic-gate * try re-requesting the message and will try processing it again.
12120Sstevel@tonic-gate *
12130Sstevel@tonic-gate * Returns:
12140Sstevel@tonic-gate * -1 = Non-recoverable error
12150Sstevel@tonic-gate * 0 = Success
12160Sstevel@tonic-gate * 1 = HTTP download error
12170Sstevel@tonic-gate */
12180Sstevel@tonic-gate static int
process_wanbootfs(http_handle_t handle,char ** devpath,unsigned char * cdigest,unsigned char * sdigest)12190Sstevel@tonic-gate process_wanbootfs(http_handle_t handle, char **devpath,
12200Sstevel@tonic-gate unsigned char *cdigest, unsigned char *sdigest)
12210Sstevel@tonic-gate {
12220Sstevel@tonic-gate /* iv[] must be sized to store the largest possible encryption block */
12230Sstevel@tonic-gate uint8_t iv[WANBOOT_MAXBLOCKLEN];
12240Sstevel@tonic-gate cbc_handle_t ch;
12250Sstevel@tonic-gate void *eh;
12260Sstevel@tonic-gate SHA1_CTX sha;
12270Sstevel@tonic-gate char *lenstr;
12280Sstevel@tonic-gate size_t wanbootfs_size;
12290Sstevel@tonic-gate size_t block_size;
12300Sstevel@tonic-gate off_t offset;
12315648Ssetje static caddr_t bootfs_vaddr = NULL;
12320Sstevel@tonic-gate int ret;
12330Sstevel@tonic-gate
12340Sstevel@tonic-gate switch (hash_type) {
12350Sstevel@tonic-gate case HASH_HMAC_SHA1:
12360Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_INFO,
12370Sstevel@tonic-gate "%s: Authentication will use HMAC-SHA1", WANBOOTFS);
12380Sstevel@tonic-gate HMACInit(&sha, g_hash_key, WANBOOT_HMAC_KEY_SIZE);
12390Sstevel@tonic-gate break;
12400Sstevel@tonic-gate case HASH_NONE:
12410Sstevel@tonic-gate break;
12420Sstevel@tonic-gate default:
12430Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
12440Sstevel@tonic-gate "%s: unrecognized hash type", WANBOOTFS);
12450Sstevel@tonic-gate return (-1);
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate
12480Sstevel@tonic-gate switch (encr_type) {
12490Sstevel@tonic-gate case ENCR_3DES:
12500Sstevel@tonic-gate bootlog("wanboot",
12510Sstevel@tonic-gate BOOTLOG_INFO, "%s: Decryption will use 3DES", WANBOOTFS);
12520Sstevel@tonic-gate if (des3_init(&eh) != 0) {
12530Sstevel@tonic-gate return (-1);
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate block_size = DES3_BLOCK_SIZE;
12560Sstevel@tonic-gate des3_key(eh, g_encr_key);
12570Sstevel@tonic-gate cbc_makehandle(&ch, eh, DES3_KEY_SIZE, block_size,
12580Sstevel@tonic-gate DES3_IV_SIZE, des3_encrypt, des3_decrypt);
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate break;
12610Sstevel@tonic-gate case ENCR_AES:
12620Sstevel@tonic-gate bootlog("wanboot",
12630Sstevel@tonic-gate BOOTLOG_INFO, "%s: Decryption will use AES", WANBOOTFS);
12640Sstevel@tonic-gate if (aes_init(&eh) != 0) {
12650Sstevel@tonic-gate return (-1);
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate block_size = AES_BLOCK_SIZE;
12680Sstevel@tonic-gate aes_key(eh, g_encr_key, AES_128_KEY_SIZE);
12690Sstevel@tonic-gate cbc_makehandle(&ch, eh, AES_128_KEY_SIZE, block_size,
12700Sstevel@tonic-gate AES_IV_SIZE, aes_encrypt, aes_decrypt);
12710Sstevel@tonic-gate break;
12720Sstevel@tonic-gate case ENCR_NONE:
12730Sstevel@tonic-gate break;
12740Sstevel@tonic-gate default:
12750Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
12760Sstevel@tonic-gate "%s: unrecognized encryption type", WANBOOTFS);
12770Sstevel@tonic-gate return (-1);
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate
12800Sstevel@tonic-gate /*
12810Sstevel@tonic-gate * Process the header.
12820Sstevel@tonic-gate */
12830Sstevel@tonic-gate if (http_process_part_headers(handle, NULL) != 0) {
12840Sstevel@tonic-gate print_errors("http_process_part_headers", handle);
12850Sstevel@tonic-gate return (1);
12860Sstevel@tonic-gate }
12870Sstevel@tonic-gate lenstr = http_get_header_value(handle, CONTENT_LENGTH);
12880Sstevel@tonic-gate if (lenstr == NULL) {
12890Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT, "%s: error getting length "
12900Sstevel@tonic-gate "of first part of multipart message", WANBOOTFS);
12910Sstevel@tonic-gate return (1);
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate wanbootfs_size = (size_t)strtol(lenstr, NULL, 10);
12940Sstevel@tonic-gate free(lenstr);
12950Sstevel@tonic-gate if (wanbootfs_size == 0) {
12960Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT, "%s: length of first part "
12970Sstevel@tonic-gate "of multipart message not a legal size", WANBOOTFS);
12980Sstevel@tonic-gate return (1);
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate
13010Sstevel@tonic-gate /*
13020Sstevel@tonic-gate * If encrypted, then read the iv.
13030Sstevel@tonic-gate */
13040Sstevel@tonic-gate if (encr_type != ENCR_NONE) {
13050Sstevel@tonic-gate if (read_bytes(handle, (char *)iv, block_size) != 0) {
13060Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_ALERT,
13070Sstevel@tonic-gate "%s: error reading hash iv", WANBOOTFS);
13080Sstevel@tonic-gate return (1);
13090Sstevel@tonic-gate }
13100Sstevel@tonic-gate wanbootfs_size -= block_size;
13110Sstevel@tonic-gate if (hash_type != HASH_NONE) {
13120Sstevel@tonic-gate HMACUpdate(&sha, (uchar_t *)iv, block_size);
13130Sstevel@tonic-gate }
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate
13160Sstevel@tonic-gate /*
13170Sstevel@tonic-gate * We can only create the ramdisk once. So, if we've
13180Sstevel@tonic-gate * already created it, then it means we've re-entered
13190Sstevel@tonic-gate * this routine from an earlier partial failure. Use
13200Sstevel@tonic-gate * the already existing ramdisk and seek back to the
13210Sstevel@tonic-gate * beginning of the file.
13220Sstevel@tonic-gate */
13235648Ssetje if (bootfs_vaddr == NULL) {
13245648Ssetje bootfs_vaddr = create_ramdisk(RD_BOOTFS, wanbootfs_size,
13255648Ssetje devpath);
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate
13280Sstevel@tonic-gate offset = 0;
13290Sstevel@tonic-gate
13305648Ssetje if ((ret = write_msg_to_ramdisk(WANBOOTFS, bootfs_vaddr, handle,
13315648Ssetje wanbootfs_size, &offset, (hash_type == HASH_NONE) ? NULL : &sha))
13325648Ssetje != 0) {
13330Sstevel@tonic-gate return (ret);
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate
13360Sstevel@tonic-gate if (hash_type != HASH_NONE) {
13370Sstevel@tonic-gate HMACFinal(&sha, g_hash_key, WANBOOT_HMAC_KEY_SIZE, cdigest);
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate
13400Sstevel@tonic-gate /*
13410Sstevel@tonic-gate * If encrypted, then decrypt it.
13420Sstevel@tonic-gate */
13430Sstevel@tonic-gate if (encr_type != ENCR_NONE) {
13445648Ssetje ret = decrypt_wanbootfs(bootfs_vaddr, &ch, iv, wanbootfs_size);
13450Sstevel@tonic-gate if (ret != 0) {
13460Sstevel@tonic-gate encr_fini(encr_type, eh);
13470Sstevel@tonic-gate return (-1);
13480Sstevel@tonic-gate }
13490Sstevel@tonic-gate encr_fini(encr_type, eh);
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate return (read_digest(WANBOOTFS, handle, sdigest));
13530Sstevel@tonic-gate }
13540Sstevel@tonic-gate
13550Sstevel@tonic-gate /*
13560Sstevel@tonic-gate * This routine sends an HTTP GET request to the webserver to
13570Sstevel@tonic-gate * request the wanboot file system for the client. The server
13580Sstevel@tonic-gate * will reply by sending a multipart message. This routine will rely
13590Sstevel@tonic-gate * on process_wanbootfs() to receive the multipart message, process it
13600Sstevel@tonic-gate * and ultimately return to it a device path to a ramdisk containing
13610Sstevel@tonic-gate * the wanboot file system, a client computed hash digest and a
13620Sstevel@tonic-gate * server computed hash digest. This routine will verify that the
13630Sstevel@tonic-gate * client computed hash digest matches the one sent by the server. This
13640Sstevel@tonic-gate * routine will also verify that the nonce received in the reply matches
13650Sstevel@tonic-gate * the one sent in the request.
13660Sstevel@tonic-gate *
13670Sstevel@tonic-gate * If an error occurs in the transfer of the message from the server
13680Sstevel@tonic-gate * to the client, then the client re-requests the download in its
13690Sstevel@tonic-gate * entirety. Errors not related to the actual message download are
13700Sstevel@tonic-gate * deemed unrecoverable.
13710Sstevel@tonic-gate *
13720Sstevel@tonic-gate * Returns:
13730Sstevel@tonic-gate * -1 = Non-recoverable error
13740Sstevel@tonic-gate * 0 = Success
13750Sstevel@tonic-gate */
13760Sstevel@tonic-gate int
get_wanbootfs(const url_t * server_url)13770Sstevel@tonic-gate get_wanbootfs(const url_t *server_url)
13780Sstevel@tonic-gate {
13790Sstevel@tonic-gate http_handle_t handle;
13800Sstevel@tonic-gate unsigned char cdigest[HMAC_DIGEST_LEN];
13810Sstevel@tonic-gate unsigned char sdigest[HMAC_DIGEST_LEN];
13820Sstevel@tonic-gate url_t req_url;
13830Sstevel@tonic-gate char *devpath;
13840Sstevel@tonic-gate int ret;
13850Sstevel@tonic-gate int fd;
13860Sstevel@tonic-gate char buf[NONCELEN + 1];
13870Sstevel@tonic-gate int retry_cnt = 0;
13880Sstevel@tonic-gate int retry_max = WANBOOT_RETRY_MAX;
13890Sstevel@tonic-gate
13900Sstevel@tonic-gate /*
13910Sstevel@tonic-gate * Build the URL to request the wanboot file system. This URL
13920Sstevel@tonic-gate * will include the CGI script name and the IP, CID, and
13930Sstevel@tonic-gate * NONCE parameters.
13940Sstevel@tonic-gate */
13950Sstevel@tonic-gate if (build_request_url(&req_url, URLtype_wanbootfs, server_url) == -1) {
13960Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
13970Sstevel@tonic-gate "Can't build the URL to make the %s request",
13980Sstevel@tonic-gate CGIcontent(URLtype_wanbootfs));
13990Sstevel@tonic-gate return (-1);
14000Sstevel@tonic-gate }
14010Sstevel@tonic-gate
14020Sstevel@tonic-gate /*
14030Sstevel@tonic-gate * Go get the wanboot file system. If we fail reading the
14040Sstevel@tonic-gate * response we re-request the entire file system.
14050Sstevel@tonic-gate */
14060Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE, "Downloading wanboot file system");
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate bzero(cdigest, sizeof (cdigest));
14090Sstevel@tonic-gate do {
14100Sstevel@tonic-gate if ((ret = establish_http_connection(WANBOOTFS, &handle,
14115648Ssetje &req_url, 0)) < 0) {
14120Sstevel@tonic-gate break;
14130Sstevel@tonic-gate } else if (ret > 0) {
14140Sstevel@tonic-gate if (wanboot_retry(++retry_cnt, retry_max)) {
14150Sstevel@tonic-gate continue;
14160Sstevel@tonic-gate } else {
14170Sstevel@tonic-gate break;
14180Sstevel@tonic-gate }
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate
14210Sstevel@tonic-gate if ((ret = process_wanbootfs(handle, &devpath,
14225648Ssetje cdigest, sdigest)) > 0) {
14230Sstevel@tonic-gate if (!wanboot_retry(++retry_cnt, retry_max)) {
14240Sstevel@tonic-gate (void) http_srv_close(handle);
14250Sstevel@tonic-gate break;
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate }
14280Sstevel@tonic-gate
14290Sstevel@tonic-gate (void) http_srv_close(handle);
14300Sstevel@tonic-gate
14310Sstevel@tonic-gate } while (ret > 0);
14320Sstevel@tonic-gate
14330Sstevel@tonic-gate /*
14340Sstevel@tonic-gate * Validate the computed digest against the one received.
14350Sstevel@tonic-gate */
14360Sstevel@tonic-gate if (ret != 0 ||
14375648Ssetje !verify_digests(WANBOOTFS, cdigest, sdigest)) {
14380Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
14390Sstevel@tonic-gate "The wanboot file system download aborted");
14400Sstevel@tonic-gate return (-1);
14410Sstevel@tonic-gate }
14420Sstevel@tonic-gate
14430Sstevel@tonic-gate /*
14440Sstevel@tonic-gate * Mount the wanboot file system.
14450Sstevel@tonic-gate */
14460Sstevel@tonic-gate if (determine_fstype_and_mountroot(devpath) != VFS_SUCCESS) {
14470Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
14480Sstevel@tonic-gate "Could not mount the wanboot filesystem.");
14490Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
14500Sstevel@tonic-gate "This may signify a client/server key mismatch");
14510Sstevel@tonic-gate if (encr_type != ENCR_NONE) {
14520Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
14530Sstevel@tonic-gate "(client has key but wrong encryption_type?)");
14540Sstevel@tonic-gate } else {
14550Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
14560Sstevel@tonic-gate "(encryption_type specified but no client key?)");
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate return (-1);
14590Sstevel@tonic-gate }
14600Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE,
14610Sstevel@tonic-gate "The wanboot file system has been mounted");
14620Sstevel@tonic-gate
14630Sstevel@tonic-gate /*
14640Sstevel@tonic-gate * The wanboot file system should contain a nonce. Read it
14650Sstevel@tonic-gate * and compare it against the nonce sent in the request.
14660Sstevel@tonic-gate */
14670Sstevel@tonic-gate if ((fd = open(WANBOOTFS_NONCE_FILE, O_RDONLY)) == -1) {
14680Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
14695648Ssetje "No nonce found in the wanboot file system");
14700Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
14710Sstevel@tonic-gate "The wanboot file system download aborted");
14720Sstevel@tonic-gate return (-1);
14730Sstevel@tonic-gate }
14740Sstevel@tonic-gate
14750Sstevel@tonic-gate if (read(fd, buf, NONCELEN) != NONCELEN ||
14760Sstevel@tonic-gate bcmp(nonce, buf, NONCELEN) != 0) {
14770Sstevel@tonic-gate (void) close(fd);
14780Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
14795648Ssetje "Invalid nonce found in the wanboot file system");
14800Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
14810Sstevel@tonic-gate "The wanboot file system download aborted");
14820Sstevel@tonic-gate return (-1);
14830Sstevel@tonic-gate }
14840Sstevel@tonic-gate
14850Sstevel@tonic-gate (void) close(fd);
14860Sstevel@tonic-gate
14870Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_VERBOSE,
14880Sstevel@tonic-gate "The wanboot file system download was successful");
14890Sstevel@tonic-gate return (0);
14900Sstevel@tonic-gate }
14910Sstevel@tonic-gate
14920Sstevel@tonic-gate static boolean_t
init_netdev(char * bpath)14930Sstevel@tonic-gate init_netdev(char *bpath)
14940Sstevel@tonic-gate {
1495789Sahrens pnode_t anode;
14960Sstevel@tonic-gate int proplen;
1497*9034SJerry.Gilliam@Sun.COM char netalias[OBP_MAXPATHLEN];
1498*9034SJerry.Gilliam@Sun.COM static char devpath[OBP_MAXPATHLEN];
1499*9034SJerry.Gilliam@Sun.COM char *p;
1500*9034SJerry.Gilliam@Sun.COM
1501*9034SJerry.Gilliam@Sun.COM bzero(netalias, sizeof (netalias));
1502*9034SJerry.Gilliam@Sun.COM bzero(devpath, sizeof (devpath));
15030Sstevel@tonic-gate
15040Sstevel@tonic-gate /*
15050Sstevel@tonic-gate * Wanboot will either have loaded over the network (in which case
15060Sstevel@tonic-gate * bpath will name a network device), or from CD-ROM or disk. In
1507*9034SJerry.Gilliam@Sun.COM * either case ensure that the 'net' alias corresponds to a network
15080Sstevel@tonic-gate * device, and that if a network boot was performed that it is
15090Sstevel@tonic-gate * identical to bpath. This is so that the interface name can always
15100Sstevel@tonic-gate * be determined for CD-ROM or disk boots, and for manually-configured
15110Sstevel@tonic-gate * network boots. The latter restriction may be relaxed in the future.
15120Sstevel@tonic-gate */
15130Sstevel@tonic-gate anode = prom_alias_node();
1514*9034SJerry.Gilliam@Sun.COM if ((proplen = prom_getproplen(anode, "net")) <= 0 ||
1515*9034SJerry.Gilliam@Sun.COM proplen > sizeof (netalias)) {
1516*9034SJerry.Gilliam@Sun.COM goto error;
1517*9034SJerry.Gilliam@Sun.COM }
1518*9034SJerry.Gilliam@Sun.COM (void) prom_getprop(anode, "net", (caddr_t)netalias);
15190Sstevel@tonic-gate
1520*9034SJerry.Gilliam@Sun.COM /*
1521*9034SJerry.Gilliam@Sun.COM * Strip boot arguments from the net device to form
1522*9034SJerry.Gilliam@Sun.COM * the boot device path, returned as netdev_path.
1523*9034SJerry.Gilliam@Sun.COM */
1524*9034SJerry.Gilliam@Sun.COM if (strlcpy(devpath, netalias, sizeof (devpath)) >= sizeof (devpath))
1525*9034SJerry.Gilliam@Sun.COM goto error;
1526*9034SJerry.Gilliam@Sun.COM if ((p = strchr(devpath, ':')) != NULL) {
1527*9034SJerry.Gilliam@Sun.COM *p = '\0';
1528*9034SJerry.Gilliam@Sun.COM }
15290Sstevel@tonic-gate
1530*9034SJerry.Gilliam@Sun.COM if (!is_netdev(netalias)) {
1531*9034SJerry.Gilliam@Sun.COM bootlog("wanboot", BOOTLOG_CRIT, "'net'=%s\n", netalias);
1532*9034SJerry.Gilliam@Sun.COM goto error;
1533*9034SJerry.Gilliam@Sun.COM }
1534*9034SJerry.Gilliam@Sun.COM
1535*9034SJerry.Gilliam@Sun.COM if (is_netdev(bpath)) {
1536*9034SJerry.Gilliam@Sun.COM /*
1537*9034SJerry.Gilliam@Sun.COM * If bpath is a network device path, then v2path
1538*9034SJerry.Gilliam@Sun.COM * will be a copy of this sans device arguments.
1539*9034SJerry.Gilliam@Sun.COM */
1540*9034SJerry.Gilliam@Sun.COM if (strcmp(v2path, devpath) != 0) {
1541*9034SJerry.Gilliam@Sun.COM bootlog("wanboot", BOOTLOG_CRIT,
1542*9034SJerry.Gilliam@Sun.COM "'net'=%s\n", netalias);
15430Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
15440Sstevel@tonic-gate "wanboot requires that the 'net' alias refers to ");
15450Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
15460Sstevel@tonic-gate "the network device path from which it loaded");
15470Sstevel@tonic-gate return (B_FALSE);
15480Sstevel@tonic-gate }
1549*9034SJerry.Gilliam@Sun.COM } else {
1550*9034SJerry.Gilliam@Sun.COM bpath = netalias;
15510Sstevel@tonic-gate }
15520Sstevel@tonic-gate
15530Sstevel@tonic-gate /*
1554*9034SJerry.Gilliam@Sun.COM * Configure the network and return the network device.
1555*9034SJerry.Gilliam@Sun.COM */
1556*9034SJerry.Gilliam@Sun.COM bootlog("wanboot", BOOTLOG_INFO, "configuring %s\n", bpath);
1557*9034SJerry.Gilliam@Sun.COM netdev_path = devpath;
1558*9034SJerry.Gilliam@Sun.COM mac_init(bpath);
1559*9034SJerry.Gilliam@Sun.COM return (B_TRUE);
1560*9034SJerry.Gilliam@Sun.COM
1561*9034SJerry.Gilliam@Sun.COM error:
1562*9034SJerry.Gilliam@Sun.COM /*
15630Sstevel@tonic-gate * If we haven't established a device path for a network interface,
15640Sstevel@tonic-gate * then we're doomed.
15650Sstevel@tonic-gate */
15660Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
15670Sstevel@tonic-gate "No network device available for wanboot!");
15680Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
15690Sstevel@tonic-gate "(Ensure that the 'net' alias is set correctly)");
15700Sstevel@tonic-gate return (B_FALSE);
15710Sstevel@tonic-gate }
15720Sstevel@tonic-gate
15730Sstevel@tonic-gate /*
15740Sstevel@tonic-gate * This implementation of bootprog() is used solely by wanboot.
15750Sstevel@tonic-gate *
15760Sstevel@tonic-gate * The basic algorithm is as follows:
15770Sstevel@tonic-gate *
15780Sstevel@tonic-gate * - The wanboot options (those specified using the "-o" flag) are processed,
15790Sstevel@tonic-gate * and if necessary the wanboot interpreter is invoked to collect other
15800Sstevel@tonic-gate * options.
15810Sstevel@tonic-gate *
15820Sstevel@tonic-gate * - The wanboot filesystem (containing certificates, wanboot.conf file, etc.)
15830Sstevel@tonic-gate * is then downloaded into the bootfs ramdisk, which is mounted for use
15840Sstevel@tonic-gate * by OpenSSL, access to wanboot.conf, etc.
15850Sstevel@tonic-gate *
15860Sstevel@tonic-gate * - The wanboot miniroot is downloaded over http/https into the rootfs
15870Sstevel@tonic-gate * ramdisk. The bootfs filesystem is unmounted, and the rootfs filesystem
15885648Ssetje * is booted.
15890Sstevel@tonic-gate */
15900Sstevel@tonic-gate /* EXPORT DELETE END */
15910Sstevel@tonic-gate /*ARGSUSED*/
15920Sstevel@tonic-gate int
bootprog(char * bpath,char * bargs,boolean_t user_specified_filename)15930Sstevel@tonic-gate bootprog(char *bpath, char *bargs, boolean_t user_specified_filename)
15940Sstevel@tonic-gate {
15950Sstevel@tonic-gate /* EXPORT DELETE START */
15960Sstevel@tonic-gate char *miniroot_path;
15970Sstevel@tonic-gate url_t server_url;
15980Sstevel@tonic-gate int ret;
15990Sstevel@tonic-gate
16000Sstevel@tonic-gate if (!init_netdev(bpath)) {
16010Sstevel@tonic-gate return (-1);
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate
16040Sstevel@tonic-gate if (!bootinfo_init()) {
16050Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT, "Cannot initialize bootinfo");
16060Sstevel@tonic-gate return (-1);
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate
16090Sstevel@tonic-gate /*
16100Sstevel@tonic-gate * Get default values from PROM, etc., process any boot arguments
16110Sstevel@tonic-gate * (specified with the "-o" option), and initialize the interface.
16120Sstevel@tonic-gate */
16130Sstevel@tonic-gate if (!wanboot_init_interface(wanboot_arguments)) {
16140Sstevel@tonic-gate return (-1);
16150Sstevel@tonic-gate }
16160Sstevel@tonic-gate
16170Sstevel@tonic-gate /*
16180Sstevel@tonic-gate * Determine which encryption and hashing algorithms the client
16190Sstevel@tonic-gate * is configured to use.
16200Sstevel@tonic-gate */
16210Sstevel@tonic-gate init_encryption();
16220Sstevel@tonic-gate init_hashing();
16230Sstevel@tonic-gate
16240Sstevel@tonic-gate /*
16250Sstevel@tonic-gate * Get the bootserver value. Should be of the form:
16260Sstevel@tonic-gate * http://host[:port]/abspath.
16270Sstevel@tonic-gate */
16280Sstevel@tonic-gate ret = get_url(BI_BOOTSERVER, &server_url);
16290Sstevel@tonic-gate if (ret != 0) {
16300Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
16310Sstevel@tonic-gate "Unable to retrieve the bootserver URL");
16320Sstevel@tonic-gate return (-1);
16330Sstevel@tonic-gate }
16340Sstevel@tonic-gate
16350Sstevel@tonic-gate /*
16360Sstevel@tonic-gate * Get the wanboot file system and mount it. Contains metdata
16370Sstevel@tonic-gate * needed by wanboot.
16380Sstevel@tonic-gate */
16390Sstevel@tonic-gate if (get_wanbootfs(&server_url) != 0) {
16400Sstevel@tonic-gate return (-1);
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate
16430Sstevel@tonic-gate /*
16440Sstevel@tonic-gate * Check that there is a valid wanboot.conf file in the wanboot
16450Sstevel@tonic-gate * file system.
16460Sstevel@tonic-gate */
16470Sstevel@tonic-gate if (bootconf_init(&bc_handle, NULL) != BC_E_NOERROR) {
16480Sstevel@tonic-gate bootlog("wanboot", BOOTLOG_CRIT,
16490Sstevel@tonic-gate "wanboot.conf error (code=%d)", bc_handle.bc_error_code);
16500Sstevel@tonic-gate return (-1);
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate
16530Sstevel@tonic-gate /*
16540Sstevel@tonic-gate * Set the time
16550Sstevel@tonic-gate */
16560Sstevel@tonic-gate init_boot_time();
16570Sstevel@tonic-gate
16580Sstevel@tonic-gate /*
16590Sstevel@tonic-gate * Verify that URLs in wanboot.conf can be reached, etc.
16600Sstevel@tonic-gate */
16610Sstevel@tonic-gate if (!wanboot_verify_config()) {
16620Sstevel@tonic-gate return (-1);
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate
16650Sstevel@tonic-gate /*
16660Sstevel@tonic-gate * Retrieve the miniroot.
16670Sstevel@tonic-gate */
16680Sstevel@tonic-gate if (get_miniroot(&miniroot_path) != 0) {
16690Sstevel@tonic-gate return (-1);
16700Sstevel@tonic-gate }
16710Sstevel@tonic-gate
16720Sstevel@tonic-gate /*
16730Sstevel@tonic-gate * We don't need the wanboot file system mounted anymore and
16740Sstevel@tonic-gate * should unmount it so that we can mount the miniroot.
16750Sstevel@tonic-gate */
16760Sstevel@tonic-gate (void) unmountroot();
16770Sstevel@tonic-gate
16785648Ssetje boot_ramdisk(RD_ROOTFS);
16790Sstevel@tonic-gate
16800Sstevel@tonic-gate /* EXPORT DELETE END */
16810Sstevel@tonic-gate return (0);
16820Sstevel@tonic-gate }
1683