xref: /titanic_50/usr/src/stand/lib/sock/socket.c (revision 66ac517ef5ae556eb413e14e9546003581b9045f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*66ac517eSjk115741  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * socket.c, Code implementing a simple socket interface.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include "socket_impl.h"
337c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
367c478bd9Sstevel@tonic-gate #include <sys/socket.h>
377c478bd9Sstevel@tonic-gate #include <netinet/in.h>
387c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
397c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
407c478bd9Sstevel@tonic-gate #include <sys/uio.h>
417c478bd9Sstevel@tonic-gate #include <sys/salib.h>
427c478bd9Sstevel@tonic-gate #include "socket_inet.h"
437c478bd9Sstevel@tonic-gate #include "ipv4.h"
447c478bd9Sstevel@tonic-gate #include "ipv4_impl.h"
457c478bd9Sstevel@tonic-gate #include "udp_inet.h"
467c478bd9Sstevel@tonic-gate #include "tcp_inet.h"
477c478bd9Sstevel@tonic-gate #include "mac.h"
487c478bd9Sstevel@tonic-gate #include "mac_impl.h"
497c478bd9Sstevel@tonic-gate #include <sys/promif.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate struct inetboot_socket	sockets[MAXSOCKET] = { 0 };
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* Default send and receive socket buffer size */
547c478bd9Sstevel@tonic-gate #define	SO_DEF_SNDBUF	48*1024
557c478bd9Sstevel@tonic-gate #define	SO_DEF_RCVBUF	48*1024
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* Default max socket buffer size */
587c478bd9Sstevel@tonic-gate #define	SO_MAX_BUF	4*1024*1024
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static ssize_t dgram_sendto(int, const void *, size_t, int,
617c478bd9Sstevel@tonic-gate     const struct sockaddr *, int);
627c478bd9Sstevel@tonic-gate static ssize_t stream_sendto(int, const void *, size_t, int);
637c478bd9Sstevel@tonic-gate static int bind_check(int, const struct sockaddr *);
647c478bd9Sstevel@tonic-gate static int quickbind(int);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* Check the validity of a fd and return the socket index of that fd. */
677c478bd9Sstevel@tonic-gate int
687c478bd9Sstevel@tonic-gate so_check_fd(int fd, int *errno)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	int i;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	i = FD_TO_SOCKET(fd);
737c478bd9Sstevel@tonic-gate 	if (i < 0 || i >= MAXSOCKET) {
747c478bd9Sstevel@tonic-gate 		*errno = ENOTSOCK;
757c478bd9Sstevel@tonic-gate 		return (-1);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	if (sockets[i].type == INETBOOT_UNUSED) {
787c478bd9Sstevel@tonic-gate 		*errno = ENOTSOCK;
797c478bd9Sstevel@tonic-gate 		return (-1);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	return (i);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * Create an endpoint for network communication. Returns a descriptor.
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  * Notes:
887c478bd9Sstevel@tonic-gate  *	Only PF_INET communication domains are supported. Within
897c478bd9Sstevel@tonic-gate  * 	this domain, only SOCK_RAW, SOCK_DGRAM and SOCK_STREAM types are
907c478bd9Sstevel@tonic-gate  *	supported.
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate int
937c478bd9Sstevel@tonic-gate socket(int domain, int type, int protocol)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	static int sock_initialized;
967c478bd9Sstevel@tonic-gate 	int i;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	errno = 0;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (!sock_initialized) {
1017c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAXSOCKET; i++)
1027c478bd9Sstevel@tonic-gate 			sockets[i].type = INETBOOT_UNUSED;
1037c478bd9Sstevel@tonic-gate 		sock_initialized = B_TRUE;
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	if (domain != AF_INET) {
1067c478bd9Sstevel@tonic-gate 		errno = EPROTONOSUPPORT;
1077c478bd9Sstevel@tonic-gate 		return (-1);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* Find available socket */
1117c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAXSOCKET; i++) {
1127c478bd9Sstevel@tonic-gate 		if (sockets[i].type == INETBOOT_UNUSED)
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	if (i >= MAXSOCKET) {
1167c478bd9Sstevel@tonic-gate 		errno = EMFILE;	/* No slots left. */
1177c478bd9Sstevel@tonic-gate 		return (-1);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/* Some socket initialization... */
1217c478bd9Sstevel@tonic-gate 	sockets[i].so_rcvbuf = SO_DEF_RCVBUF;
1227c478bd9Sstevel@tonic-gate 	sockets[i].so_sndbuf = SO_DEF_SNDBUF;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/*
1257c478bd9Sstevel@tonic-gate 	 * Note that we ignore the protocol field for SOCK_DGRAM and
1267c478bd9Sstevel@tonic-gate 	 * SOCK_STREAM.  When we support different protocols in future,
1277c478bd9Sstevel@tonic-gate 	 * this needs to be changed.
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	switch (type) {
1307c478bd9Sstevel@tonic-gate 	case SOCK_RAW:
1317c478bd9Sstevel@tonic-gate 		ipv4_raw_socket(&sockets[i], (uint8_t)protocol);
1327c478bd9Sstevel@tonic-gate 		break;
1337c478bd9Sstevel@tonic-gate 	case SOCK_DGRAM:
1347c478bd9Sstevel@tonic-gate 		udp_socket_init(&sockets[i]);
1357c478bd9Sstevel@tonic-gate 		break;
1367c478bd9Sstevel@tonic-gate 	case SOCK_STREAM:
1377c478bd9Sstevel@tonic-gate 		tcp_socket_init(&sockets[i]);
1387c478bd9Sstevel@tonic-gate 		break;
1397c478bd9Sstevel@tonic-gate 	default:
1407c478bd9Sstevel@tonic-gate 		errno = EPROTOTYPE;
1417c478bd9Sstevel@tonic-gate 		break;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (errno != 0)
1457c478bd9Sstevel@tonic-gate 		return (-1);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* IPv4 generic initialization. */
1487c478bd9Sstevel@tonic-gate 	ipv4_socket_init(&sockets[i]);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/* MAC generic initialization. */
1517c478bd9Sstevel@tonic-gate 	mac_socket_init(&sockets[i]);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	return (i + SOCKETTYPE);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate int
1577c478bd9Sstevel@tonic-gate getsockname(int s, struct sockaddr *name,  socklen_t *namelen)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	int i;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	errno = 0;
1627c478bd9Sstevel@tonic-gate 	if ((i = so_check_fd(s, &errno)) == -1)
1637c478bd9Sstevel@tonic-gate 		return (-1);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (*namelen < sizeof (struct sockaddr_in)) {
1667c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1677c478bd9Sstevel@tonic-gate 		return (-1);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/* Structure assignment... */
1717c478bd9Sstevel@tonic-gate 	*((struct sockaddr_in *)name) = sockets[i].bind;
1727c478bd9Sstevel@tonic-gate 	*namelen = sizeof (struct sockaddr_in);
1737c478bd9Sstevel@tonic-gate 	return (0);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * The socket options we support are:
1787c478bd9Sstevel@tonic-gate  * SO_RCVTIMEO	-	Value is in msecs, and is of uint32_t.
1797c478bd9Sstevel@tonic-gate  * SO_DONTROUTE	-	Value is an int, and is a boolean (nonzero if set).
1807c478bd9Sstevel@tonic-gate  * SO_REUSEADDR -	Value is an int boolean.
1817c478bd9Sstevel@tonic-gate  * SO_RCVBUF -		Value is an int.
1827c478bd9Sstevel@tonic-gate  * SO_SNDBUF -		Value is an int.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate int
1857c478bd9Sstevel@tonic-gate getsockopt(int s, int level, int option, void *optval, socklen_t *optlen)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	int i;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	errno = 0;
1907c478bd9Sstevel@tonic-gate 	if ((i = so_check_fd(s, &errno)) == -1)
1917c478bd9Sstevel@tonic-gate 		return (-1);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	switch (level) {
1947c478bd9Sstevel@tonic-gate 	case SOL_SOCKET: {
1957c478bd9Sstevel@tonic-gate 		switch (option) {
1967c478bd9Sstevel@tonic-gate 		case SO_RCVTIMEO:
1977c478bd9Sstevel@tonic-gate 			if (*optlen == sizeof (uint32_t)) {
1987c478bd9Sstevel@tonic-gate 				*(uint32_t *)optval = sockets[i].in_timeout;
1997c478bd9Sstevel@tonic-gate 			} else {
2007c478bd9Sstevel@tonic-gate 				*optlen = 0;
2017c478bd9Sstevel@tonic-gate 				errno = EINVAL;
2027c478bd9Sstevel@tonic-gate 			}
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 		case SO_DONTROUTE:
2057c478bd9Sstevel@tonic-gate 			if (*optlen == sizeof (int)) {
2067c478bd9Sstevel@tonic-gate 				*(int *)optval =
2077c478bd9Sstevel@tonic-gate 				    (sockets[i].out_flags & SO_DONTROUTE);
2087c478bd9Sstevel@tonic-gate 			} else {
2097c478bd9Sstevel@tonic-gate 				*optlen = 0;
2107c478bd9Sstevel@tonic-gate 				errno = EINVAL;
2117c478bd9Sstevel@tonic-gate 			}
2127c478bd9Sstevel@tonic-gate 			break;
2137c478bd9Sstevel@tonic-gate 		case SO_REUSEADDR:
2147c478bd9Sstevel@tonic-gate 			if (*optlen == sizeof (int)) {
2157c478bd9Sstevel@tonic-gate 				*(int *)optval =
2167c478bd9Sstevel@tonic-gate 				    (sockets[i].so_opt & SO_REUSEADDR);
2177c478bd9Sstevel@tonic-gate 			} else {
2187c478bd9Sstevel@tonic-gate 				*optlen = 0;
2197c478bd9Sstevel@tonic-gate 				errno = EINVAL;
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 		case SO_RCVBUF:
2237c478bd9Sstevel@tonic-gate 			if (*optlen == sizeof (int)) {
2247c478bd9Sstevel@tonic-gate 				*(int *)optval = sockets[i].so_rcvbuf;
2257c478bd9Sstevel@tonic-gate 			} else {
2267c478bd9Sstevel@tonic-gate 				*optlen = 0;
2277c478bd9Sstevel@tonic-gate 				errno = EINVAL;
2287c478bd9Sstevel@tonic-gate 			}
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 		case SO_SNDBUF:
2317c478bd9Sstevel@tonic-gate 			if (*optlen == sizeof (int)) {
2327c478bd9Sstevel@tonic-gate 				*(int *)optval = sockets[i].so_sndbuf;
2337c478bd9Sstevel@tonic-gate 			} else {
2347c478bd9Sstevel@tonic-gate 				*optlen = 0;
2357c478bd9Sstevel@tonic-gate 				errno = EINVAL;
2367c478bd9Sstevel@tonic-gate 			}
2377c478bd9Sstevel@tonic-gate 			break;
2387c478bd9Sstevel@tonic-gate 		case SO_LINGER:
2397c478bd9Sstevel@tonic-gate 			if (*optlen == sizeof (struct linger)) {
2407c478bd9Sstevel@tonic-gate 				/* struct copy */
2417c478bd9Sstevel@tonic-gate 				*(struct linger *)optval = sockets[i].so_linger;
2427c478bd9Sstevel@tonic-gate 			} else {
2437c478bd9Sstevel@tonic-gate 				*optlen = 0;
2447c478bd9Sstevel@tonic-gate 				errno = EINVAL;
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 		default:
2477c478bd9Sstevel@tonic-gate 			errno = ENOPROTOOPT;
2487c478bd9Sstevel@tonic-gate 			break;
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 		break;
2517c478bd9Sstevel@tonic-gate 	} /* case SOL_SOCKET */
2527c478bd9Sstevel@tonic-gate 	case IPPROTO_TCP:
2537c478bd9Sstevel@tonic-gate 	case IPPROTO_IP: {
2547c478bd9Sstevel@tonic-gate 		switch (option) {
2557c478bd9Sstevel@tonic-gate 		default:
2567c478bd9Sstevel@tonic-gate 			*optlen = 0;
2577c478bd9Sstevel@tonic-gate 			errno = ENOPROTOOPT;
2587c478bd9Sstevel@tonic-gate 			break;
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 		break;
2617c478bd9Sstevel@tonic-gate 	} /* case IPPROTO_IP or IPPROTO_TCP */
2627c478bd9Sstevel@tonic-gate 	default:
2637c478bd9Sstevel@tonic-gate 		errno = ENOPROTOOPT;
2647c478bd9Sstevel@tonic-gate 		break;
2657c478bd9Sstevel@tonic-gate 	} /* switch (level) */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (errno != 0)
2687c478bd9Sstevel@tonic-gate 		return (-1);
2697c478bd9Sstevel@tonic-gate 	else
2707c478bd9Sstevel@tonic-gate 		return (0);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * Generate a network-order source port from the privileged range if
2757c478bd9Sstevel@tonic-gate  * "reserved" is true, dynamic/private range otherwise. We consider the
2767c478bd9Sstevel@tonic-gate  * range of 512-1023 privileged ports as ports we can use. This mirrors
2777c478bd9Sstevel@tonic-gate  * historical rpc client practice for privileged port selection.
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate in_port_t
2807c478bd9Sstevel@tonic-gate get_source_port(boolean_t reserved)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	static in_port_t	dynamic = IPPORT_DYNAMIC_START - 1,
2837c478bd9Sstevel@tonic-gate 				    rsvdport = (IPPORT_RESERVED / 2) - 1;
2847c478bd9Sstevel@tonic-gate 	in_port_t		p;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (reserved) {
2877c478bd9Sstevel@tonic-gate 		if (++rsvdport >= IPPORT_RESERVED)
2887c478bd9Sstevel@tonic-gate 			p = rsvdport = IPPORT_RESERVED / 2;
2897c478bd9Sstevel@tonic-gate 		else
2907c478bd9Sstevel@tonic-gate 			p = rsvdport;
2917c478bd9Sstevel@tonic-gate 	} else
2927c478bd9Sstevel@tonic-gate 		p = ++dynamic;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	return (htons(p));
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * The socket options we support are:
2997c478bd9Sstevel@tonic-gate  * SO_RECVTIMEO	-	Value is uint32_t msecs.
3007c478bd9Sstevel@tonic-gate  * SO_DONTROUTE	-	Value is int boolean (nonzero == TRUE, zero == FALSE).
3017c478bd9Sstevel@tonic-gate  * SO_REUSEADDR -	value is int boolean.
3027c478bd9Sstevel@tonic-gate  * SO_RCVBUF -		Value is int.
3037c478bd9Sstevel@tonic-gate  * SO_SNDBUF -		Value is int.
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate int
3067c478bd9Sstevel@tonic-gate setsockopt(int s, int level, int option, const void *optval, socklen_t optlen)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	int i;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	errno = 0;
3117c478bd9Sstevel@tonic-gate 	if ((i = so_check_fd(s, &errno)) == -1)
3127c478bd9Sstevel@tonic-gate 		return (-1);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	switch (level) {
3157c478bd9Sstevel@tonic-gate 	case SOL_SOCKET: {
3167c478bd9Sstevel@tonic-gate 		switch (option) {
3177c478bd9Sstevel@tonic-gate 		case SO_RCVTIMEO:
3187c478bd9Sstevel@tonic-gate 			if (optlen == sizeof (uint32_t))
3197c478bd9Sstevel@tonic-gate 				sockets[i].in_timeout = *(uint32_t *)optval;
3207c478bd9Sstevel@tonic-gate 			else {
3217c478bd9Sstevel@tonic-gate 				errno = EINVAL;
3227c478bd9Sstevel@tonic-gate 			}
3237c478bd9Sstevel@tonic-gate 			break;
3247c478bd9Sstevel@tonic-gate 		case SO_DONTROUTE:
3257c478bd9Sstevel@tonic-gate 			if (optlen == sizeof (int)) {
3267c478bd9Sstevel@tonic-gate 				if (*(int *)optval)
3277c478bd9Sstevel@tonic-gate 					sockets[i].out_flags |= SO_DONTROUTE;
3287c478bd9Sstevel@tonic-gate 				else
3297c478bd9Sstevel@tonic-gate 					sockets[i].out_flags &= ~SO_DONTROUTE;
3307c478bd9Sstevel@tonic-gate 			} else {
3317c478bd9Sstevel@tonic-gate 				errno = EINVAL;
3327c478bd9Sstevel@tonic-gate 			}
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 		case SO_REUSEADDR:
3357c478bd9Sstevel@tonic-gate 			if (optlen == sizeof (int)) {
3367c478bd9Sstevel@tonic-gate 				if (*(int *)optval)
3377c478bd9Sstevel@tonic-gate 					sockets[i].so_opt |= SO_REUSEADDR;
3387c478bd9Sstevel@tonic-gate 				else
3397c478bd9Sstevel@tonic-gate 					sockets[i].so_opt &= ~SO_REUSEADDR;
3407c478bd9Sstevel@tonic-gate 			} else {
3417c478bd9Sstevel@tonic-gate 				errno = EINVAL;
3427c478bd9Sstevel@tonic-gate 			}
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 		case SO_RCVBUF:
3457c478bd9Sstevel@tonic-gate 			if (optlen == sizeof (int)) {
3467c478bd9Sstevel@tonic-gate 				sockets[i].so_rcvbuf = *(int *)optval;
3477c478bd9Sstevel@tonic-gate 				if (sockets[i].so_rcvbuf > SO_MAX_BUF)
3487c478bd9Sstevel@tonic-gate 					sockets[i].so_rcvbuf = SO_MAX_BUF;
3497c478bd9Sstevel@tonic-gate 				(void) tcp_opt_set(sockets[i].pcb,
3507c478bd9Sstevel@tonic-gate 				    level, option, optval, optlen);
3517c478bd9Sstevel@tonic-gate 			} else {
3527c478bd9Sstevel@tonic-gate 				errno = EINVAL;
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 			break;
3557c478bd9Sstevel@tonic-gate 		case SO_SNDBUF:
3567c478bd9Sstevel@tonic-gate 			if (optlen == sizeof (int)) {
3577c478bd9Sstevel@tonic-gate 				sockets[i].so_sndbuf = *(int *)optval;
3587c478bd9Sstevel@tonic-gate 				if (sockets[i].so_sndbuf > SO_MAX_BUF)
3597c478bd9Sstevel@tonic-gate 					sockets[i].so_sndbuf = SO_MAX_BUF;
3607c478bd9Sstevel@tonic-gate 				(void) tcp_opt_set(sockets[i].pcb,
3617c478bd9Sstevel@tonic-gate 				    level, option, optval, optlen);
3627c478bd9Sstevel@tonic-gate 			} else {
3637c478bd9Sstevel@tonic-gate 				errno = EINVAL;
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 			break;
3667c478bd9Sstevel@tonic-gate 		case SO_LINGER:
3677c478bd9Sstevel@tonic-gate 			if (optlen == sizeof (struct linger)) {
3687c478bd9Sstevel@tonic-gate 				/* struct copy */
3697c478bd9Sstevel@tonic-gate 				sockets[i].so_linger = *(struct linger *)optval;
3707c478bd9Sstevel@tonic-gate 				(void) tcp_opt_set(sockets[i].pcb,
3717c478bd9Sstevel@tonic-gate 				    level, option, optval, optlen);
3727c478bd9Sstevel@tonic-gate 			} else {
3737c478bd9Sstevel@tonic-gate 				errno = EINVAL;
3747c478bd9Sstevel@tonic-gate 			}
3757c478bd9Sstevel@tonic-gate 			break;
3767c478bd9Sstevel@tonic-gate 		default:
3777c478bd9Sstevel@tonic-gate 			errno = ENOPROTOOPT;
3787c478bd9Sstevel@tonic-gate 			break;
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 		break;
3817c478bd9Sstevel@tonic-gate 	} /* case SOL_SOCKET */
3827c478bd9Sstevel@tonic-gate 	case IPPROTO_TCP:
3837c478bd9Sstevel@tonic-gate 	case IPPROTO_IP: {
3847c478bd9Sstevel@tonic-gate 		switch (option) {
3857c478bd9Sstevel@tonic-gate 		default:
3867c478bd9Sstevel@tonic-gate 			errno = ENOPROTOOPT;
3877c478bd9Sstevel@tonic-gate 			break;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 		break;
3907c478bd9Sstevel@tonic-gate 	} /* case IPPROTO_IP  or IPPROTO_TCP */
3917c478bd9Sstevel@tonic-gate 	default:
3927c478bd9Sstevel@tonic-gate 		errno = ENOPROTOOPT;
3937c478bd9Sstevel@tonic-gate 		break;
3947c478bd9Sstevel@tonic-gate 	} /* switch (level) */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (errno != 0)
3977c478bd9Sstevel@tonic-gate 		return (-1);
3987c478bd9Sstevel@tonic-gate 	else
3997c478bd9Sstevel@tonic-gate 		return (0);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Shut down part of a full-duplex connection.
4047c478bd9Sstevel@tonic-gate  *
4057c478bd9Sstevel@tonic-gate  * Only supported for TCP sockets
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate int
4087c478bd9Sstevel@tonic-gate shutdown(int s, int how)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	int sock_id;
4117c478bd9Sstevel@tonic-gate 	int i;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	errno = 0;
4147c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(s, &errno)) == -1)
4157c478bd9Sstevel@tonic-gate 		return (-1);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/* shutdown only supported for TCP sockets */
4187c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].type != INETBOOT_STREAM) {
4197c478bd9Sstevel@tonic-gate 		errno = EOPNOTSUPP;
4207c478bd9Sstevel@tonic-gate 		return (-1);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (!(sockets[sock_id].so_state & SS_ISCONNECTED)) {
4247c478bd9Sstevel@tonic-gate 		errno = ENOTCONN;
4257c478bd9Sstevel@tonic-gate 		return (-1);
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	switch (how) {
4297c478bd9Sstevel@tonic-gate 	case 0:
4307c478bd9Sstevel@tonic-gate 		sockets[sock_id].so_state |= SS_CANTRCVMORE;
4317c478bd9Sstevel@tonic-gate 		break;
4327c478bd9Sstevel@tonic-gate 	case 1:
4337c478bd9Sstevel@tonic-gate 		sockets[sock_id].so_state |= SS_CANTSENDMORE;
4347c478bd9Sstevel@tonic-gate 		break;
4357c478bd9Sstevel@tonic-gate 	case 2:
4367c478bd9Sstevel@tonic-gate 		sockets[sock_id].so_state |= (SS_CANTRCVMORE | SS_CANTSENDMORE);
4377c478bd9Sstevel@tonic-gate 		break;
4387c478bd9Sstevel@tonic-gate 	default:
4397c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4407c478bd9Sstevel@tonic-gate 		return (-1);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	switch (sockets[sock_id].so_state &
4447c478bd9Sstevel@tonic-gate 				(SS_CANTRCVMORE | SS_CANTSENDMORE)) {
4457c478bd9Sstevel@tonic-gate 	case (SS_CANTRCVMORE | SS_CANTSENDMORE):
4467c478bd9Sstevel@tonic-gate 		/* Call lower level protocol close routine. */
4477c478bd9Sstevel@tonic-gate 		for (i = TRANSPORT_LVL; i >= MEDIA_LVL; i--) {
4487c478bd9Sstevel@tonic-gate 			if (sockets[sock_id].close[i] != NULL) {
4497c478bd9Sstevel@tonic-gate 				(void) sockets[sock_id].close[i](sock_id);
4507c478bd9Sstevel@tonic-gate 			}
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 		nuke_grams(&sockets[sock_id].inq);
4537c478bd9Sstevel@tonic-gate 		break;
4547c478bd9Sstevel@tonic-gate 	case SS_CANTRCVMORE:
4557c478bd9Sstevel@tonic-gate 		nuke_grams(&sockets[sock_id].inq);
4567c478bd9Sstevel@tonic-gate 		break;
4577c478bd9Sstevel@tonic-gate 	case SS_CANTSENDMORE:
4587c478bd9Sstevel@tonic-gate 		/* Call lower level protocol close routine. */
4597c478bd9Sstevel@tonic-gate 		if (tcp_shutdown(sock_id) < 0)
4607c478bd9Sstevel@tonic-gate 			return (-1);
4617c478bd9Sstevel@tonic-gate 		break;
4627c478bd9Sstevel@tonic-gate 	default:
4637c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4647c478bd9Sstevel@tonic-gate 		return (-1);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	return (0);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate /*
4717c478bd9Sstevel@tonic-gate  * "close" a socket.
4727c478bd9Sstevel@tonic-gate  */
4737c478bd9Sstevel@tonic-gate int
4747c478bd9Sstevel@tonic-gate socket_close(int s)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	int sock_id, i;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	errno = 0;
4797c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(s, &errno)) == -1)
4807c478bd9Sstevel@tonic-gate 		return (-1);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	/* Call lower level protocol close routine. */
4837c478bd9Sstevel@tonic-gate 	for (i = TRANSPORT_LVL; i >= MEDIA_LVL; i--) {
4847c478bd9Sstevel@tonic-gate 		if (sockets[sock_id].close[i] != NULL) {
4857c478bd9Sstevel@tonic-gate 			/*
4867c478bd9Sstevel@tonic-gate 			 * Note that the close() routine of other
4877c478bd9Sstevel@tonic-gate 			 * layers can return an error.  But right
4887c478bd9Sstevel@tonic-gate 			 * now, the only mechanism to report that
4897c478bd9Sstevel@tonic-gate 			 * back is for the close() routine to set
4907c478bd9Sstevel@tonic-gate 			 * the errno and socket_close() will return
4917c478bd9Sstevel@tonic-gate 			 * an error.  But the close operation will
4927c478bd9Sstevel@tonic-gate 			 * not be stopped.
4937c478bd9Sstevel@tonic-gate 			 */
4947c478bd9Sstevel@tonic-gate 			(void) sockets[sock_id].close[i](sock_id);
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/*
4997c478bd9Sstevel@tonic-gate 	 * Clear the input queue.  This has to be done
5007c478bd9Sstevel@tonic-gate 	 * after the lower level protocol close routines have been
5017c478bd9Sstevel@tonic-gate 	 * called as they may want to do something about the queue.
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	nuke_grams(&sockets[sock_id].inq);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&sockets[sock_id], sizeof (struct inetboot_socket));
5067c478bd9Sstevel@tonic-gate 	sockets[sock_id].type = INETBOOT_UNUSED;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	return (0);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate  * Read up to `nbyte' of data from socket `s' into `buf'; if non-zero,
5137c478bd9Sstevel@tonic-gate  * then give up after `read_timeout' seconds.  Returns the number of
5147c478bd9Sstevel@tonic-gate  * bytes read, or -1 on failure.
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate int
5177c478bd9Sstevel@tonic-gate socket_read(int s, void *buf, size_t nbyte, int read_timeout)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	ssize_t	n;
5207c478bd9Sstevel@tonic-gate 	uint_t	start, diff;
5217c478bd9Sstevel@tonic-gate 	struct sockaddr from;
5227c478bd9Sstevel@tonic-gate 	uint_t fromlen = sizeof (from);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * keep calling non-blocking recvfrom until something received
5267c478bd9Sstevel@tonic-gate 	 * or an error occurs
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	start = prom_gettime();
529*66ac517eSjk115741 	for (;;) {
530*66ac517eSjk115741 		n = recvfrom(s, buf, nbyte, MSG_DONTWAIT, NULL, NULL);
531*66ac517eSjk115741 		if (n == -1 && errno == EWOULDBLOCK) {
5327c478bd9Sstevel@tonic-gate 			diff = (uint_t)((prom_gettime() - start) + 500) / 1000;
5337c478bd9Sstevel@tonic-gate 			if (read_timeout != 0 && diff > read_timeout) {
5347c478bd9Sstevel@tonic-gate 				errno = EINTR;
5357c478bd9Sstevel@tonic-gate 				return (-1);
5367c478bd9Sstevel@tonic-gate 			}
537*66ac517eSjk115741 		} else {
5387c478bd9Sstevel@tonic-gate 			return (n);
5397c478bd9Sstevel@tonic-gate 		}
540*66ac517eSjk115741 	}
541*66ac517eSjk115741 }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * Write up to `nbyte' bytes of data from `buf' to the address pointed to
5457c478bd9Sstevel@tonic-gate  * `addr' using socket `s'.  Returns the number of bytes writte on success,
5467c478bd9Sstevel@tonic-gate  * or -1 on failure.
5477c478bd9Sstevel@tonic-gate  */
5487c478bd9Sstevel@tonic-gate int
5497c478bd9Sstevel@tonic-gate socket_write(int s, const void *buf, size_t nbyte, struct sockaddr_in *addr)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	return (sendto(s, buf, nbyte, 0, (struct sockaddr *)addr,
5527c478bd9Sstevel@tonic-gate 	    sizeof (*addr)));
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate static int
5567c478bd9Sstevel@tonic-gate bind_check(int sock_id, const struct sockaddr *addr)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	int k;
5597c478bd9Sstevel@tonic-gate 	struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	/* Do not check for duplicate bind() if SO_REUSEADDR option is set. */
5627c478bd9Sstevel@tonic-gate 	if (! (sockets[sock_id].so_opt & SO_REUSEADDR)) {
5637c478bd9Sstevel@tonic-gate 		for (k = 0; k < MAXSOCKET; k++) {
5647c478bd9Sstevel@tonic-gate 			if (sockets[k].type != INETBOOT_UNUSED &&
5657c478bd9Sstevel@tonic-gate 			    sockets[k].proto == sockets[sock_id].proto &&
5667c478bd9Sstevel@tonic-gate 			    sockets[k].bound) {
5677c478bd9Sstevel@tonic-gate 				if ((sockets[k].bind.sin_addr.s_addr ==
5687c478bd9Sstevel@tonic-gate 				    in_addr->sin_addr.s_addr) &&
5697c478bd9Sstevel@tonic-gate 				    (sockets[k].bind.sin_port ==
5707c478bd9Sstevel@tonic-gate 				    in_addr->sin_port)) {
5717c478bd9Sstevel@tonic-gate 					errno = EADDRINUSE;
5727c478bd9Sstevel@tonic-gate 					return (-1);
5737c478bd9Sstevel@tonic-gate 				}
5747c478bd9Sstevel@tonic-gate 			}
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 	return (0);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /* Assign a name to an unnamed socket. */
5817c478bd9Sstevel@tonic-gate int
5827c478bd9Sstevel@tonic-gate bind(int s, const struct sockaddr *name, socklen_t namelen)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	int i;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	errno = 0;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if ((i = so_check_fd(s, &errno)) == -1)
5897c478bd9Sstevel@tonic-gate 		return (-1);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	if (name == NULL) {
5927c478bd9Sstevel@tonic-gate 		/* unbind */
5937c478bd9Sstevel@tonic-gate 		if (sockets[i].bound) {
5947c478bd9Sstevel@tonic-gate 			bzero((caddr_t)&sockets[i].bind,
5957c478bd9Sstevel@tonic-gate 			    sizeof (struct sockaddr_in));
5967c478bd9Sstevel@tonic-gate 			sockets[i].bound = B_FALSE;
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 		return (0);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 	if (namelen != sizeof (struct sockaddr_in) || name == NULL) {
6017c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6027c478bd9Sstevel@tonic-gate 		return (-1);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	if (name->sa_family != AF_INET) {
6057c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
6067c478bd9Sstevel@tonic-gate 		return (-1);
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 	if (sockets[i].bound) {
6097c478bd9Sstevel@tonic-gate 		if (bcmp((caddr_t)&sockets[i].bind, (caddr_t)name,
6107c478bd9Sstevel@tonic-gate 		    namelen) == 0) {
6117c478bd9Sstevel@tonic-gate 			/* attempt to bind to same address ok... */
6127c478bd9Sstevel@tonic-gate 			return (0);
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 		errno = EINVAL;	/* already bound */
6157c478bd9Sstevel@tonic-gate 		return (-1);
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (errno != 0) {
6197c478bd9Sstevel@tonic-gate 		return (-1);
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	/* Check for duplicate bind(). */
6237c478bd9Sstevel@tonic-gate 	if (bind_check(i, name) < 0)
6247c478bd9Sstevel@tonic-gate 		return (-1);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	bcopy((caddr_t)name, (caddr_t)&sockets[i].bind, namelen);
6277c478bd9Sstevel@tonic-gate 	if (sockets[i].type == INETBOOT_STREAM) {
6287c478bd9Sstevel@tonic-gate 		if (tcp_bind(i) < 0) {
6297c478bd9Sstevel@tonic-gate 			return (-1);
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 	sockets[i].bound = B_TRUE;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	return (0);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate static int
6387c478bd9Sstevel@tonic-gate quickbind(int sock_id)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	int i;
6417c478bd9Sstevel@tonic-gate 	struct sockaddr_in addr;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/*
6447c478bd9Sstevel@tonic-gate 	 * XXX This needs more work.  Right now, if ipv4_setipaddr()
6457c478bd9Sstevel@tonic-gate 	 * have not been called, this will be wrong.  But we need
6467c478bd9Sstevel@tonic-gate 	 * something better.  Need to be revisited.
6477c478bd9Sstevel@tonic-gate 	 */
6487c478bd9Sstevel@tonic-gate 	ipv4_getipaddr(&addr.sin_addr);
6497c478bd9Sstevel@tonic-gate 	addr.sin_family = AF_INET;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	for (i = SMALLEST_ANON_PORT; i <= LARGEST_ANON_PORT; i++) {
6527c478bd9Sstevel@tonic-gate 		addr.sin_port = htons(i);
6537c478bd9Sstevel@tonic-gate 		if (bind_check(sock_id, (struct sockaddr *)&addr) == 0)
6547c478bd9Sstevel@tonic-gate 			break;
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 	/* Need to clear errno as it is probably set by bind_check(). */
6577c478bd9Sstevel@tonic-gate 	errno = 0;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if (i <= LARGEST_ANON_PORT) {
6607c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)&addr, (caddr_t)&sockets[sock_id].bind,
6617c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in));
6627c478bd9Sstevel@tonic-gate 		sockets[sock_id].bound = B_TRUE;
6637c478bd9Sstevel@tonic-gate #ifdef DEBUG
6647c478bd9Sstevel@tonic-gate 		printf("quick bind done addr %s port %d\n",
6657c478bd9Sstevel@tonic-gate 		    inet_ntoa(sockets[sock_id].bind.sin_addr),
6667c478bd9Sstevel@tonic-gate 			ntohs(sockets[sock_id].bind.sin_port));
6677c478bd9Sstevel@tonic-gate #endif
6687c478bd9Sstevel@tonic-gate 		return (0);
6697c478bd9Sstevel@tonic-gate 	} else {
6707c478bd9Sstevel@tonic-gate 		return (-1);
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate int
6757c478bd9Sstevel@tonic-gate listen(int fd, int backlog)
6767c478bd9Sstevel@tonic-gate {
6777c478bd9Sstevel@tonic-gate 	int sock_id;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	errno = 0;
6807c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(fd, &errno)) == -1)
6817c478bd9Sstevel@tonic-gate 		return (-1);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].type != INETBOOT_STREAM) {
6847c478bd9Sstevel@tonic-gate 		errno = EOPNOTSUPP;
6857c478bd9Sstevel@tonic-gate 		return (-1);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].so_error != 0) {
6887c478bd9Sstevel@tonic-gate 		errno = sockets[sock_id].so_error;
6897c478bd9Sstevel@tonic-gate 		return (-1);
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 	return (tcp_listen(sock_id, backlog));
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate int
6957c478bd9Sstevel@tonic-gate accept(int fd, struct sockaddr *addr,  socklen_t *addr_len)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate 	int sock_id;
6987c478bd9Sstevel@tonic-gate 	int new_sd;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	errno = 0;
7017c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(fd, &errno)) == -1)
7027c478bd9Sstevel@tonic-gate 		return (-1);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].type != INETBOOT_STREAM) {
7057c478bd9Sstevel@tonic-gate 		errno = EOPNOTSUPP;
7067c478bd9Sstevel@tonic-gate 		return (-1);
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].so_error != 0) {
7097c478bd9Sstevel@tonic-gate 		errno = sockets[sock_id].so_error;
7107c478bd9Sstevel@tonic-gate 		return (-1);
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 	if ((new_sd = tcp_accept(sock_id, addr, addr_len)) == -1)
7137c478bd9Sstevel@tonic-gate 		return (-1);
7147c478bd9Sstevel@tonic-gate 	sock_id = so_check_fd(new_sd, &errno);
7157c478bd9Sstevel@tonic-gate 	sockets[sock_id].so_state |= SS_ISCONNECTED;
7167c478bd9Sstevel@tonic-gate 	return (new_sd);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate int
7207c478bd9Sstevel@tonic-gate connect(int fd, const  struct sockaddr *addr, socklen_t addr_len)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	int sock_id;
7237c478bd9Sstevel@tonic-gate 	int so_type;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	errno = 0;
7267c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(fd, &errno)) == -1)
7277c478bd9Sstevel@tonic-gate 		return (-1);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	so_type = sockets[sock_id].type;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (addr == NULL || addr_len == 0) {
7327c478bd9Sstevel@tonic-gate 		errno = EINVAL;
7337c478bd9Sstevel@tonic-gate 		return (-1);
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 	/* Don't allow connect for raw socket. */
7367c478bd9Sstevel@tonic-gate 	if (so_type == INETBOOT_RAW) {
7377c478bd9Sstevel@tonic-gate 		errno = EPROTONOSUPPORT;
7387c478bd9Sstevel@tonic-gate 		return (-1);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].so_state & SS_ISCONNECTED) {
7427c478bd9Sstevel@tonic-gate 		errno = EINVAL;
7437c478bd9Sstevel@tonic-gate 		return (-1);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].so_error != 0) {
7477c478bd9Sstevel@tonic-gate 		errno = sockets[sock_id].so_error;
7487c478bd9Sstevel@tonic-gate 		return (-1);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/* If the socket is not bound, we need to do a quick bind. */
7527c478bd9Sstevel@tonic-gate 	if (!sockets[sock_id].bound) {
7537c478bd9Sstevel@tonic-gate 		/* For TCP socket, just call tcp_bind(). */
7547c478bd9Sstevel@tonic-gate 		if (so_type == INETBOOT_STREAM) {
7557c478bd9Sstevel@tonic-gate 			if (tcp_bind(sock_id) < 0)
7567c478bd9Sstevel@tonic-gate 				return (-1);
7577c478bd9Sstevel@tonic-gate 		} else {
7587c478bd9Sstevel@tonic-gate 			if (quickbind(sock_id) < 0) {
7597c478bd9Sstevel@tonic-gate 				errno = EADDRNOTAVAIL;
7607c478bd9Sstevel@tonic-gate 				return (-1);
7617c478bd9Sstevel@tonic-gate 			}
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	/* Should do some sanity check for addr .... */
7657c478bd9Sstevel@tonic-gate 	bcopy((caddr_t)addr, &sockets[sock_id].remote,
7667c478bd9Sstevel@tonic-gate 	    sizeof (struct sockaddr_in));
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].type == INETBOOT_STREAM) {
7697c478bd9Sstevel@tonic-gate 		/* Call TCP connect routine. */
7707c478bd9Sstevel@tonic-gate 		if (tcp_connect(sock_id) == 0)
7717c478bd9Sstevel@tonic-gate 			sockets[sock_id].so_state |= SS_ISCONNECTED;
7727c478bd9Sstevel@tonic-gate 		else {
7737c478bd9Sstevel@tonic-gate 			if (sockets[sock_id].so_error != 0)
7747c478bd9Sstevel@tonic-gate 				errno = sockets[sock_id].so_error;
7757c478bd9Sstevel@tonic-gate 			return (-1);
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 	} else {
7787c478bd9Sstevel@tonic-gate 		sockets[sock_id].so_state |= SS_ISCONNECTED;
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 	return (0);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate /* Just a wrapper around recvfrom(). */
7847c478bd9Sstevel@tonic-gate ssize_t
7857c478bd9Sstevel@tonic-gate recv(int s, void *buf, size_t len, int flags)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	return (recvfrom(s, buf, len, flags, NULL, NULL));
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * Receive messages from a connectionless socket. Legal flags are 0 and
7927c478bd9Sstevel@tonic-gate  * MSG_DONTWAIT. MSG_WAITALL is not currently supported.
7937c478bd9Sstevel@tonic-gate  *
7947c478bd9Sstevel@tonic-gate  * Returns length of message for success, -1 if error occurred.
7957c478bd9Sstevel@tonic-gate  */
7967c478bd9Sstevel@tonic-gate ssize_t
7977c478bd9Sstevel@tonic-gate recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from,
7987c478bd9Sstevel@tonic-gate     socklen_t *fromlen)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	int			sock_id, i;
8017c478bd9Sstevel@tonic-gate 	ssize_t			datalen, bytes = 0;
8027c478bd9Sstevel@tonic-gate 	struct inetgram		*icp;
8037c478bd9Sstevel@tonic-gate 	enum SockType		so_type;
8047c478bd9Sstevel@tonic-gate 	char			*tmp_buf;
8057c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	errno = 0;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(s, &errno)) == -1) {
8107c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8117c478bd9Sstevel@tonic-gate 		return (-1);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].type == INETBOOT_STREAM &&
8157c478bd9Sstevel@tonic-gate 	    !(sockets[sock_id].so_state & SS_ISCONNECTED)) {
8167c478bd9Sstevel@tonic-gate 		errno = ENOTCONN;
8177c478bd9Sstevel@tonic-gate 		return (-1);
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (buf == NULL || len == 0) {
8217c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8227c478bd9Sstevel@tonic-gate 		return (-1);
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 	/* Yup - MSG_WAITALL not implemented */
8257c478bd9Sstevel@tonic-gate 	if ((flags & ~MSG_DONTWAIT) != 0) {
8267c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8277c478bd9Sstevel@tonic-gate 		return (-1);
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate retry:
8317c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].inq == NULL) {
8327c478bd9Sstevel@tonic-gate 		/* Go out and check the wire */
8337c478bd9Sstevel@tonic-gate 		for (i = MEDIA_LVL; i < APP_LVL; i++) {
8347c478bd9Sstevel@tonic-gate 			if (sockets[sock_id].input[i] != NULL) {
8357c478bd9Sstevel@tonic-gate 				if (sockets[sock_id].input[i](sock_id) < 0) {
8367c478bd9Sstevel@tonic-gate 					if (sockets[sock_id].so_error != 0) {
8377c478bd9Sstevel@tonic-gate 						errno =
8387c478bd9Sstevel@tonic-gate 						    sockets[sock_id].so_error;
8397c478bd9Sstevel@tonic-gate 					}
8407c478bd9Sstevel@tonic-gate 					return (-1);
8417c478bd9Sstevel@tonic-gate 				}
8427c478bd9Sstevel@tonic-gate 			}
8437c478bd9Sstevel@tonic-gate 		}
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	so_type = sockets[sock_id].type;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	/* Remove unknown inetgrams from the head of inq.  Can this happen? */
8497c478bd9Sstevel@tonic-gate 	while ((icp = sockets[sock_id].inq) != NULL) {
8507c478bd9Sstevel@tonic-gate 		if ((so_type == INETBOOT_DGRAM ||
8517c478bd9Sstevel@tonic-gate 		    so_type == INETBOOT_STREAM) &&
8527c478bd9Sstevel@tonic-gate 		    icp->igm_level != APP_LVL) {
8537c478bd9Sstevel@tonic-gate #ifdef	DEBUG
8547c478bd9Sstevel@tonic-gate 			printf("recvfrom: unexpected level %d frame found\n",
8557c478bd9Sstevel@tonic-gate 			    icp->igm_level);
8567c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
8577c478bd9Sstevel@tonic-gate 			del_gram(&sockets[sock_id].inq, icp, B_TRUE);
8587c478bd9Sstevel@tonic-gate 			continue;
8597c478bd9Sstevel@tonic-gate 		} else {
8607c478bd9Sstevel@tonic-gate 			break;
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	if (icp == NULL) {
8667c478bd9Sstevel@tonic-gate 		/*
8677c478bd9Sstevel@tonic-gate 		 * Checking for error should be done everytime a lower layer
8687c478bd9Sstevel@tonic-gate 		 * input routing is called.  For example, if TCP gets a RST,
8697c478bd9Sstevel@tonic-gate 		 * this should be reported asap.
8707c478bd9Sstevel@tonic-gate 		 */
8717c478bd9Sstevel@tonic-gate 		if (sockets[sock_id].so_state & SS_CANTRCVMORE) {
8727c478bd9Sstevel@tonic-gate 			if (sockets[sock_id].so_error != 0) {
8737c478bd9Sstevel@tonic-gate 				errno = sockets[sock_id].so_error;
8747c478bd9Sstevel@tonic-gate 				return (-1);
8757c478bd9Sstevel@tonic-gate 			} else {
8767c478bd9Sstevel@tonic-gate 				return (0);
8777c478bd9Sstevel@tonic-gate 			}
8787c478bd9Sstevel@tonic-gate 		}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 		if ((flags & MSG_DONTWAIT) == 0)
8817c478bd9Sstevel@tonic-gate 			goto retry;	/* wait forever */
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 		/* no data */
8847c478bd9Sstevel@tonic-gate 		errno = EWOULDBLOCK;
8857c478bd9Sstevel@tonic-gate 		return (-1);
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	if (from != NULL && fromlen != NULL) {
8897c478bd9Sstevel@tonic-gate 		switch (so_type) {
8907c478bd9Sstevel@tonic-gate 		case INETBOOT_STREAM:
8917c478bd9Sstevel@tonic-gate 			/* Need to copy from the socket's remote address. */
8927c478bd9Sstevel@tonic-gate 			bcopy(&(sockets[sock_id].remote), from, MIN(*fromlen,
8937c478bd9Sstevel@tonic-gate 			    sizeof (struct sockaddr_in)));
8947c478bd9Sstevel@tonic-gate 			break;
8957c478bd9Sstevel@tonic-gate 		case INETBOOT_RAW:
8967c478bd9Sstevel@tonic-gate 		case INETBOOT_DGRAM:
8977c478bd9Sstevel@tonic-gate 		default:
8987c478bd9Sstevel@tonic-gate 			if (*fromlen > sizeof (icp->igm_saddr))
8997c478bd9Sstevel@tonic-gate 				*fromlen = sizeof (icp->igm_saddr);
9007c478bd9Sstevel@tonic-gate 			bcopy((caddr_t)&(icp->igm_saddr), (caddr_t)from,
9017c478bd9Sstevel@tonic-gate 			    MIN(*fromlen, sizeof (struct sockaddr_in)));
9027c478bd9Sstevel@tonic-gate 			break;
9037c478bd9Sstevel@tonic-gate 		}
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	mp = icp->igm_mp;
9077c478bd9Sstevel@tonic-gate 	switch (so_type) {
9087c478bd9Sstevel@tonic-gate 	case INETBOOT_STREAM:
9097c478bd9Sstevel@tonic-gate 		/*
9107c478bd9Sstevel@tonic-gate 		 * If the message has igm_id == TCP_CALLB_MAGIC_ID, we need
9117c478bd9Sstevel@tonic-gate 		 * to drain the data held by tcp and try again.
9127c478bd9Sstevel@tonic-gate 		 */
9137c478bd9Sstevel@tonic-gate 		if (icp->igm_id == TCP_CALLB_MAGIC_ID) {
9147c478bd9Sstevel@tonic-gate 			del_gram(&sockets[sock_id].inq, icp, B_TRUE);
9157c478bd9Sstevel@tonic-gate 			tcp_rcv_drain_sock(sock_id);
9167c478bd9Sstevel@tonic-gate 			goto retry;
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 		/* TCP should put only user data in the inetgram. */
9207c478bd9Sstevel@tonic-gate 		tmp_buf = (char *)buf;
9217c478bd9Sstevel@tonic-gate 		while (len > 0 && icp != NULL) {
9227c478bd9Sstevel@tonic-gate 			datalen = mp->b_wptr - mp->b_rptr;
9237c478bd9Sstevel@tonic-gate 			if (len < datalen) {
9247c478bd9Sstevel@tonic-gate 				bcopy(mp->b_rptr, tmp_buf, len);
9257c478bd9Sstevel@tonic-gate 				bytes += len;
9267c478bd9Sstevel@tonic-gate 				mp->b_rptr += len;
9277c478bd9Sstevel@tonic-gate 				break;
9287c478bd9Sstevel@tonic-gate 			} else {
9297c478bd9Sstevel@tonic-gate 				bcopy(mp->b_rptr, tmp_buf, datalen);
9307c478bd9Sstevel@tonic-gate 				len -= datalen;
9317c478bd9Sstevel@tonic-gate 				bytes += datalen;
9327c478bd9Sstevel@tonic-gate 				tmp_buf += datalen;
9337c478bd9Sstevel@tonic-gate 				del_gram(&sockets[sock_id].inq, icp, B_TRUE);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 				/*
9367c478bd9Sstevel@tonic-gate 				 * If we have any embedded magic messages just
9377c478bd9Sstevel@tonic-gate 				 * drop them.
9387c478bd9Sstevel@tonic-gate 				 */
9397c478bd9Sstevel@tonic-gate 				while ((icp = sockets[sock_id].inq) != NULL) {
9407c478bd9Sstevel@tonic-gate 					if (icp->igm_id != TCP_CALLB_MAGIC_ID)
9417c478bd9Sstevel@tonic-gate 						break;
9427c478bd9Sstevel@tonic-gate 					del_gram(&sockets[sock_id].inq, icp,
9437c478bd9Sstevel@tonic-gate 						B_TRUE);
9447c478bd9Sstevel@tonic-gate 				}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 				if (icp == NULL)
9477c478bd9Sstevel@tonic-gate 					break;
9487c478bd9Sstevel@tonic-gate 				mp = icp->igm_mp;
9497c478bd9Sstevel@tonic-gate 			}
9507c478bd9Sstevel@tonic-gate 		}
9517c478bd9Sstevel@tonic-gate 		sockets[sock_id].so_rcvbuf += (int32_t)bytes;
9527c478bd9Sstevel@tonic-gate 		break;
9537c478bd9Sstevel@tonic-gate 	case INETBOOT_DGRAM:
9547c478bd9Sstevel@tonic-gate 		datalen = mp->b_wptr - mp->b_rptr;
9557c478bd9Sstevel@tonic-gate 		if (len < datalen)
9567c478bd9Sstevel@tonic-gate 			bytes = len;
9577c478bd9Sstevel@tonic-gate 		else
9587c478bd9Sstevel@tonic-gate 			bytes = datalen;
9597c478bd9Sstevel@tonic-gate 		bcopy(mp->b_rptr, buf, bytes);
9607c478bd9Sstevel@tonic-gate 		del_gram(&sockets[sock_id].inq, icp, B_TRUE);
9617c478bd9Sstevel@tonic-gate 		break;
9627c478bd9Sstevel@tonic-gate 	case INETBOOT_RAW:
9637c478bd9Sstevel@tonic-gate 	default:
9647c478bd9Sstevel@tonic-gate 		datalen = mp->b_wptr - mp->b_rptr;
9657c478bd9Sstevel@tonic-gate 		if (len < datalen)
9667c478bd9Sstevel@tonic-gate 			bytes = len;
9677c478bd9Sstevel@tonic-gate 		else
9687c478bd9Sstevel@tonic-gate 			bytes = datalen;
9697c478bd9Sstevel@tonic-gate 		bcopy(mp->b_rptr, buf, bytes);
9707c478bd9Sstevel@tonic-gate 		del_gram(&sockets[sock_id].inq, icp, B_TRUE);
9717c478bd9Sstevel@tonic-gate 		break;
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate #ifdef	DEBUG
9757c478bd9Sstevel@tonic-gate 	printf("recvfrom(%d): data: (0x%x,%d)\n", sock_id,
9767c478bd9Sstevel@tonic-gate 	    (icp != NULL) ? icp->igm_mp : 0, bytes);
9777c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
9787c478bd9Sstevel@tonic-gate 	return (bytes);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate /* Just a wrapper around sendto(). */
9837c478bd9Sstevel@tonic-gate ssize_t
9847c478bd9Sstevel@tonic-gate send(int s, const void *msg, size_t len, int flags)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	return (sendto(s, msg, len, flags, NULL, 0));
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate /*
9907c478bd9Sstevel@tonic-gate  * Transmit a message through a socket.
9917c478bd9Sstevel@tonic-gate  *
9927c478bd9Sstevel@tonic-gate  * Supported flags: MSG_DONTROUTE or 0.
9937c478bd9Sstevel@tonic-gate  */
9947c478bd9Sstevel@tonic-gate ssize_t
9957c478bd9Sstevel@tonic-gate sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to,
9967c478bd9Sstevel@tonic-gate     socklen_t tolen)
9977c478bd9Sstevel@tonic-gate {
9987c478bd9Sstevel@tonic-gate 	enum SockType so_type;
9997c478bd9Sstevel@tonic-gate 	int sock_id;
10007c478bd9Sstevel@tonic-gate 	ssize_t bytes;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	errno = 0;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(s, &errno)) == -1) {
10057c478bd9Sstevel@tonic-gate 		return (-1);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 	if (msg == NULL) {
10087c478bd9Sstevel@tonic-gate 		errno = EINVAL;
10097c478bd9Sstevel@tonic-gate 		return (-1);
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 	so_type = sockets[sock_id].type;
10127c478bd9Sstevel@tonic-gate 	if ((flags & ~MSG_DONTROUTE) != 0) {
10137c478bd9Sstevel@tonic-gate 		errno = EINVAL;
10147c478bd9Sstevel@tonic-gate 		return (-1);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].so_error != 0) {
10177c478bd9Sstevel@tonic-gate 		errno = sockets[sock_id].so_error;
10187c478bd9Sstevel@tonic-gate 		return (-1);
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 	if (to != NULL && to->sa_family != AF_INET) {
10217c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
10227c478bd9Sstevel@tonic-gate 		return (-1);
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	switch (so_type) {
10267c478bd9Sstevel@tonic-gate 	case INETBOOT_RAW:
10277c478bd9Sstevel@tonic-gate 	case INETBOOT_DGRAM:
10287c478bd9Sstevel@tonic-gate 		if (!(sockets[sock_id].so_state & SS_ISCONNECTED) &&
10297c478bd9Sstevel@tonic-gate 		    (to == NULL || tolen != sizeof (struct sockaddr_in))) {
10307c478bd9Sstevel@tonic-gate 			errno = EINVAL;
10317c478bd9Sstevel@tonic-gate 			return (-1);
10327c478bd9Sstevel@tonic-gate 		}
10337c478bd9Sstevel@tonic-gate 		bytes = dgram_sendto(sock_id, msg, len, flags, to, tolen);
10347c478bd9Sstevel@tonic-gate 		break;
10357c478bd9Sstevel@tonic-gate 	case INETBOOT_STREAM:
10367c478bd9Sstevel@tonic-gate 		if (!((sockets[sock_id].so_state & SS_ISCONNECTED) ||
10377c478bd9Sstevel@tonic-gate 		    (sockets[sock_id].so_state & SS_ISCONNECTING))) {
10387c478bd9Sstevel@tonic-gate 			errno = EINVAL;
10397c478bd9Sstevel@tonic-gate 			return (-1);
10407c478bd9Sstevel@tonic-gate 		}
10417c478bd9Sstevel@tonic-gate 		if (sockets[sock_id].so_state & SS_CANTSENDMORE) {
10427c478bd9Sstevel@tonic-gate 			errno = EPIPE;
10437c478bd9Sstevel@tonic-gate 			return (-1);
10447c478bd9Sstevel@tonic-gate 		}
10457c478bd9Sstevel@tonic-gate 		bytes = stream_sendto(sock_id, msg, len, flags);
10467c478bd9Sstevel@tonic-gate 		break;
10477c478bd9Sstevel@tonic-gate 	default:
10487c478bd9Sstevel@tonic-gate 		/* Should not happen... */
10497c478bd9Sstevel@tonic-gate 		errno = EPROTOTYPE;
10507c478bd9Sstevel@tonic-gate 		return (-1);
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 	return (bytes);
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate static ssize_t
10567c478bd9Sstevel@tonic-gate dgram_sendto(int i, const void *msg, size_t len, int flags,
10577c478bd9Sstevel@tonic-gate     const struct sockaddr *to, int tolen)
10587c478bd9Sstevel@tonic-gate {
10597c478bd9Sstevel@tonic-gate 	struct inetgram		oc;
10607c478bd9Sstevel@tonic-gate 	int			l, offset;
10617c478bd9Sstevel@tonic-gate 	size_t			tlen;
10627c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate #ifdef	DEBUG
10657c478bd9Sstevel@tonic-gate 	{
10667c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
10677c478bd9Sstevel@tonic-gate 	printf("sendto(%d): msg of length: %d sent to port %d and host: %s\n",
10687c478bd9Sstevel@tonic-gate 	    i, len, ntohs(sin->sin_port), inet_ntoa(sin->sin_addr));
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	nuke_grams(&sockets[i].inq); /* flush the input queue */
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	/* calculate offset for data */
10757c478bd9Sstevel@tonic-gate 	offset = sockets[i].headerlen[MEDIA_LVL](NULL) +
10767c478bd9Sstevel@tonic-gate 	    (sockets[i].headerlen[NETWORK_LVL])(NULL);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&oc, sizeof (oc));
10797c478bd9Sstevel@tonic-gate 	if (sockets[i].type != INETBOOT_RAW) {
10807c478bd9Sstevel@tonic-gate 		offset += (sockets[i].headerlen[TRANSPORT_LVL])(NULL);
10817c478bd9Sstevel@tonic-gate 		oc.igm_level = TRANSPORT_LVL;
10827c478bd9Sstevel@tonic-gate 	} else
10837c478bd9Sstevel@tonic-gate 		oc.igm_level = NETWORK_LVL;
10847c478bd9Sstevel@tonic-gate 	oc.igm_oflags = flags;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if (to != NULL) {
10877c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)to, (caddr_t)&oc.igm_saddr, tolen);
10887c478bd9Sstevel@tonic-gate 	} else {
10897c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)&sockets[i].remote, (caddr_t)&oc.igm_saddr,
10907c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in));
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/* Get a legal source port if the socket isn't bound. */
10947c478bd9Sstevel@tonic-gate 	if (sockets[i].bound == B_FALSE &&
10957c478bd9Sstevel@tonic-gate 	    ntohs(oc.igm_saddr.sin_port == 0)) {
10967c478bd9Sstevel@tonic-gate 		((struct sockaddr_in *)&oc.igm_saddr)->sin_port =
10977c478bd9Sstevel@tonic-gate 		    get_source_port(B_FALSE);
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	/* Round up to 16bit value for checksum purposes */
11017c478bd9Sstevel@tonic-gate 	if (sockets[i].type == INETBOOT_DGRAM) {
11027c478bd9Sstevel@tonic-gate 		tlen = ((len + sizeof (uint16_t) - 1) &
11037c478bd9Sstevel@tonic-gate 		    ~(sizeof (uint16_t) - 1));
11047c478bd9Sstevel@tonic-gate 	} else
11057c478bd9Sstevel@tonic-gate 		tlen = len;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	if ((oc.igm_mp = allocb(tlen + offset, 0)) == NULL) {
11087c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
11097c478bd9Sstevel@tonic-gate 		return (-1);
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 	mp = oc.igm_mp;
11127c478bd9Sstevel@tonic-gate 	mp->b_rptr = mp->b_wptr += offset;
11137c478bd9Sstevel@tonic-gate 	bcopy((caddr_t)msg, mp->b_wptr, len);
11147c478bd9Sstevel@tonic-gate 	mp->b_wptr += len;
11157c478bd9Sstevel@tonic-gate 	for (l = TRANSPORT_LVL; l >= MEDIA_LVL; l--) {
11167c478bd9Sstevel@tonic-gate 		if (sockets[i].output[l] != NULL) {
11177c478bd9Sstevel@tonic-gate 			if (sockets[i].output[l](i, &oc) < 0) {
11187c478bd9Sstevel@tonic-gate 				freeb(mp);
11197c478bd9Sstevel@tonic-gate 				if (errno == 0)
11207c478bd9Sstevel@tonic-gate 					errno = EIO;
11217c478bd9Sstevel@tonic-gate 				return (-1);
11227c478bd9Sstevel@tonic-gate 			}
11237c478bd9Sstevel@tonic-gate 		}
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 	freeb(mp);
11267c478bd9Sstevel@tonic-gate 	return (len);
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate /* ARGSUSED */
11307c478bd9Sstevel@tonic-gate static ssize_t
11317c478bd9Sstevel@tonic-gate stream_sendto(int i, const void *msg, size_t len, int flags)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate 	int cnt;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	assert(sockets[i].pcb != NULL);
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	/*
11387c478bd9Sstevel@tonic-gate 	 * Call directly TCP's send routine.  We do this because TCP
11397c478bd9Sstevel@tonic-gate 	 * needs to decide whether to send out the data.
11407c478bd9Sstevel@tonic-gate 	 *
11417c478bd9Sstevel@tonic-gate 	 * Note also that currently, TCP ignores all flags passed in for
11427c478bd9Sstevel@tonic-gate 	 * TCP socket.
11437c478bd9Sstevel@tonic-gate 	 */
11447c478bd9Sstevel@tonic-gate 	if ((cnt = tcp_send(i, sockets[i].pcb, msg, len)) < 0) {
11457c478bd9Sstevel@tonic-gate 		if (sockets[i].so_error != 0)
11467c478bd9Sstevel@tonic-gate 			errno = sockets[i].so_error;
11477c478bd9Sstevel@tonic-gate 		return (-1);
11487c478bd9Sstevel@tonic-gate 	} else {
11497c478bd9Sstevel@tonic-gate 		return (cnt);
11507c478bd9Sstevel@tonic-gate 	}
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate /*
11547c478bd9Sstevel@tonic-gate  * Returns ptr to the last inetgram in the list, or null if list is null
11557c478bd9Sstevel@tonic-gate  */
11567c478bd9Sstevel@tonic-gate struct inetgram *
11577c478bd9Sstevel@tonic-gate last_gram(struct inetgram *igp)
11587c478bd9Sstevel@tonic-gate {
11597c478bd9Sstevel@tonic-gate 	struct inetgram	*wp;
11607c478bd9Sstevel@tonic-gate 	for (wp = igp; wp != NULL; wp = wp->igm_next) {
11617c478bd9Sstevel@tonic-gate 		if (wp->igm_next == NULL)
11627c478bd9Sstevel@tonic-gate 			return (wp);
11637c478bd9Sstevel@tonic-gate 	}
11647c478bd9Sstevel@tonic-gate 	return (NULL);
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate /*
11687c478bd9Sstevel@tonic-gate  * Adds an inetgram or list of inetgrams to the end of the list.
11697c478bd9Sstevel@tonic-gate  */
11707c478bd9Sstevel@tonic-gate void
11717c478bd9Sstevel@tonic-gate add_grams(struct inetgram **igpp, struct inetgram *newgp)
11727c478bd9Sstevel@tonic-gate {
11737c478bd9Sstevel@tonic-gate 	struct inetgram	 *wp;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	if (newgp == NULL)
11767c478bd9Sstevel@tonic-gate 		return;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	if (*igpp == NULL)
11797c478bd9Sstevel@tonic-gate 		*igpp = newgp;
11807c478bd9Sstevel@tonic-gate 	else {
11817c478bd9Sstevel@tonic-gate 		wp = last_gram(*igpp);
11827c478bd9Sstevel@tonic-gate 		wp->igm_next = newgp;
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate }
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate /*
11877c478bd9Sstevel@tonic-gate  * Nuke a whole list of grams.
11887c478bd9Sstevel@tonic-gate  */
11897c478bd9Sstevel@tonic-gate void
11907c478bd9Sstevel@tonic-gate nuke_grams(struct inetgram **lgpp)
11917c478bd9Sstevel@tonic-gate {
11927c478bd9Sstevel@tonic-gate 	while (*lgpp != NULL)
11937c478bd9Sstevel@tonic-gate 		del_gram(lgpp, *lgpp, B_TRUE);
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate /*
11977c478bd9Sstevel@tonic-gate  * Remove the referenced inetgram. List is altered accordingly. Destroy the
11987c478bd9Sstevel@tonic-gate  * referenced inetgram if freeit is B_TRUE.
11997c478bd9Sstevel@tonic-gate  */
12007c478bd9Sstevel@tonic-gate void
12017c478bd9Sstevel@tonic-gate del_gram(struct inetgram **lgpp, struct inetgram *igp, int freeit)
12027c478bd9Sstevel@tonic-gate {
12037c478bd9Sstevel@tonic-gate 	struct inetgram	*wp, *pp = NULL;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	if (lgpp == NULL || igp == NULL)
12067c478bd9Sstevel@tonic-gate 		return;
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	wp = *lgpp;
12097c478bd9Sstevel@tonic-gate 	while (wp != NULL) {
12107c478bd9Sstevel@tonic-gate 		if (wp == igp) {
12117c478bd9Sstevel@tonic-gate 			/* detach wp from the list */
12127c478bd9Sstevel@tonic-gate 			if (*lgpp == wp)
12137c478bd9Sstevel@tonic-gate 				*lgpp = (*lgpp)->igm_next;
12147c478bd9Sstevel@tonic-gate 			else
12157c478bd9Sstevel@tonic-gate 				pp->igm_next = wp->igm_next;
12167c478bd9Sstevel@tonic-gate 			igp->igm_next = NULL;
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 			if (freeit) {
12197c478bd9Sstevel@tonic-gate 				if (igp->igm_mp != NULL)
12207c478bd9Sstevel@tonic-gate 					freeb(igp->igm_mp);
12217c478bd9Sstevel@tonic-gate 				bkmem_free((caddr_t)igp,
12227c478bd9Sstevel@tonic-gate 				    sizeof (struct inetgram));
12237c478bd9Sstevel@tonic-gate 			}
12247c478bd9Sstevel@tonic-gate 			break;
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 		pp = wp;
12277c478bd9Sstevel@tonic-gate 		wp = wp->igm_next;
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate struct nct_t nct[] = {
12327c478bd9Sstevel@tonic-gate 	"bootp",	NCT_BOOTP_DHCP,
12337c478bd9Sstevel@tonic-gate 	"dhcp",		NCT_BOOTP_DHCP,
12347c478bd9Sstevel@tonic-gate 	"rarp",		NCT_RARP_BOOTPARAMS,
12357c478bd9Sstevel@tonic-gate 	"manual",	NCT_MANUAL
12367c478bd9Sstevel@tonic-gate };
12377c478bd9Sstevel@tonic-gate int	nct_entries = sizeof (nct) / sizeof (nct[0]);
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate /*
12407c478bd9Sstevel@tonic-gate  * Figure out from the bootpath what kind of network configuration strategy
12417c478bd9Sstevel@tonic-gate  * we should use. Returns the network config strategy.
12427c478bd9Sstevel@tonic-gate  */
12437c478bd9Sstevel@tonic-gate int
12447c478bd9Sstevel@tonic-gate get_netconfig_strategy(void)
12457c478bd9Sstevel@tonic-gate {
12467c478bd9Sstevel@tonic-gate 	int	i;
12477c478bd9Sstevel@tonic-gate #if !defined(__i386)
12487c478bd9Sstevel@tonic-gate 	/* sparc */
12497c478bd9Sstevel@tonic-gate #define	ISSPACE(c) (c == ' ' || c == '\t' || c == '\n' || c == '\0')
12507c478bd9Sstevel@tonic-gate 	char	lbootpath[OBP_MAXPATHLEN];
12517c478bd9Sstevel@tonic-gate 	char	net_options[NCT_BUFSIZE];
12527c478bd9Sstevel@tonic-gate 	char	*op, *nop, *sp;
12537c478bd9Sstevel@tonic-gate 	dnode_t	cn;
12547c478bd9Sstevel@tonic-gate 	int	proplen;
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	/* If the PROM DHCP cache exists, we're done */
12577c478bd9Sstevel@tonic-gate 	if (prom_cached_reply(B_TRUE))
12587c478bd9Sstevel@tonic-gate 		return (NCT_BOOTP_DHCP);
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	/*
12617c478bd9Sstevel@tonic-gate 	 *	Newer (version 4) PROMs will put the name in the
12627c478bd9Sstevel@tonic-gate 	 *	"net-config-strategy" property.
12637c478bd9Sstevel@tonic-gate 	 */
12647c478bd9Sstevel@tonic-gate 	cn = prom_finddevice("/chosen");
12657c478bd9Sstevel@tonic-gate 	if ((proplen = prom_getproplen(cn, "net-config-strategy")) <
12667c478bd9Sstevel@tonic-gate 	    sizeof (net_options)) {
12677c478bd9Sstevel@tonic-gate 		(void) prom_getprop(cn, "net-config-strategy", net_options);
12687c478bd9Sstevel@tonic-gate 		net_options[proplen] = '\0';
12697c478bd9Sstevel@tonic-gate 	} else {
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 		/*
12727c478bd9Sstevel@tonic-gate 		 * We're reduced to sacanning bootpath for the prototol to use.
12737c478bd9Sstevel@tonic-gate 		 * Since there was no "net-config-strategy" property, this is
12747c478bd9Sstevel@tonic-gate 		 * an old PROM, so we need to excise any extraneous key/value
12757c478bd9Sstevel@tonic-gate 		 * initializations from bootpath[].
12767c478bd9Sstevel@tonic-gate 		 */
12777c478bd9Sstevel@tonic-gate 		for (op = prom_bootpath(), sp = lbootpath; op != NULL &&
12787c478bd9Sstevel@tonic-gate 		    !ISSPACE(*op); sp++, op++)
12797c478bd9Sstevel@tonic-gate 			*sp = *op;
12807c478bd9Sstevel@tonic-gate 		*sp = '\0';
12817c478bd9Sstevel@tonic-gate 		/* find the last '/' (in the device path) */
12827c478bd9Sstevel@tonic-gate 		if ((op = strrchr(lbootpath, '/')) == NULL)	/* last '/' */
12837c478bd9Sstevel@tonic-gate 			op = lbootpath;
12847c478bd9Sstevel@tonic-gate 		else
12857c478bd9Sstevel@tonic-gate 			op++;
12867c478bd9Sstevel@tonic-gate 		/* then look for the ':' separating it from the protocol */
12877c478bd9Sstevel@tonic-gate 		while (*op != ':' && *op != '\0')
12887c478bd9Sstevel@tonic-gate 			op++;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		if (*op == ':') {
12917c478bd9Sstevel@tonic-gate 			for (nop = net_options, op++;
12927c478bd9Sstevel@tonic-gate 			    *op != '\0' && *op != '/' && !ISSPACE(*op) &&
12937c478bd9Sstevel@tonic-gate 			    nop < &net_options[NCT_BUFSIZE]; nop++, op++)
12947c478bd9Sstevel@tonic-gate 				*nop = *op;
12957c478bd9Sstevel@tonic-gate 			*nop = '\0';
12967c478bd9Sstevel@tonic-gate 		} else
12977c478bd9Sstevel@tonic-gate 			net_options[0] = '\0';
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate #undef	ISSPACE
13017c478bd9Sstevel@tonic-gate #else
13027c478bd9Sstevel@tonic-gate 	/* i86 */
13037c478bd9Sstevel@tonic-gate 	extern struct bootops bootops;
13047c478bd9Sstevel@tonic-gate 	extern int bgetprop(struct bootops *, char *, caddr_t, int, phandle_t);
13057c478bd9Sstevel@tonic-gate 	char	net_options[MAXNAMELEN];
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	/*
13087c478bd9Sstevel@tonic-gate 	 * Look at net-config-strategy boot property to determine what protocol
13097c478bd9Sstevel@tonic-gate 	 * will be used.
13107c478bd9Sstevel@tonic-gate 	 */
13117c478bd9Sstevel@tonic-gate 	(void) bgetprop(&bootops, "net-config-strategy", net_options,
13127c478bd9Sstevel@tonic-gate 	    sizeof (net_options), 0);
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate #endif	/* __i386 */
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	for (i = 0; i < nct_entries; i++)
13177c478bd9Sstevel@tonic-gate 		if (strcmp(net_options, nct[i].p_name) == 0)
13187c478bd9Sstevel@tonic-gate 			return (nct[i].p_id);
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	return (NCT_DEFAULT);
13217c478bd9Sstevel@tonic-gate }
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate /* Modified STREAM routines for ease of porting core TCP code. */
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13267c478bd9Sstevel@tonic-gate mblk_t *
13277c478bd9Sstevel@tonic-gate allocb(size_t size, uint_t pri)
13287c478bd9Sstevel@tonic-gate {
13297c478bd9Sstevel@tonic-gate 	unsigned char *base;
13307c478bd9Sstevel@tonic-gate 	mblk_t *mp;
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	if ((mp = (mblk_t *)bkmem_zalloc(sizeof (mblk_t))) == NULL)
13337c478bd9Sstevel@tonic-gate 		return (NULL);
13347c478bd9Sstevel@tonic-gate 	if ((base = (unsigned char *)bkmem_zalloc(size)) == NULL)
13357c478bd9Sstevel@tonic-gate 		return (NULL);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	mp->b_next = mp->b_prev = mp->b_cont = NULL;
13387c478bd9Sstevel@tonic-gate 	mp->b_rptr = mp->b_wptr = mp->b_datap = (unsigned char *)base;
13397c478bd9Sstevel@tonic-gate 	mp->b_size = size;
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	return (mp);
13427c478bd9Sstevel@tonic-gate }
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate void
13457c478bd9Sstevel@tonic-gate freeb(mblk_t *mp)
13467c478bd9Sstevel@tonic-gate {
13477c478bd9Sstevel@tonic-gate #ifdef DEBUG
13487c478bd9Sstevel@tonic-gate 	printf("freeb datap %x\n", mp->b_datap);
13497c478bd9Sstevel@tonic-gate #endif
13507c478bd9Sstevel@tonic-gate 	bkmem_free((caddr_t)(mp->b_datap), mp->b_size);
13517c478bd9Sstevel@tonic-gate #ifdef DEBUG
13527c478bd9Sstevel@tonic-gate 	printf("freeb mp %x\n", mp);
13537c478bd9Sstevel@tonic-gate #endif
13547c478bd9Sstevel@tonic-gate 	bkmem_free((caddr_t)mp, sizeof (mblk_t));
13557c478bd9Sstevel@tonic-gate }
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate void
13587c478bd9Sstevel@tonic-gate freemsg(mblk_t *mp)
13597c478bd9Sstevel@tonic-gate {
13607c478bd9Sstevel@tonic-gate 	while (mp) {
13617c478bd9Sstevel@tonic-gate 		mblk_t *mp_cont = mp->b_cont;
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 		freeb(mp);
13647c478bd9Sstevel@tonic-gate 		mp = mp_cont;
13657c478bd9Sstevel@tonic-gate 	}
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate mblk_t *
13697c478bd9Sstevel@tonic-gate copyb(mblk_t *bp)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	mblk_t *nbp;
13727c478bd9Sstevel@tonic-gate 	unsigned char *ndp;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	assert((uintptr_t)(bp->b_wptr - bp->b_rptr) >= 0);
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	if (!(nbp = allocb(bp->b_size, 0)))
13777c478bd9Sstevel@tonic-gate 		return (NULL);
13787c478bd9Sstevel@tonic-gate 	nbp->b_cont = NULL;
13797c478bd9Sstevel@tonic-gate 	ndp = nbp->b_datap;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	nbp->b_rptr = ndp + (bp->b_rptr - bp->b_datap);
13827c478bd9Sstevel@tonic-gate 	nbp->b_wptr = nbp->b_rptr + (bp->b_wptr - bp->b_rptr);
13837c478bd9Sstevel@tonic-gate 	bcopy(bp->b_datap, nbp->b_datap, bp->b_size);
13847c478bd9Sstevel@tonic-gate 	return (nbp);
13857c478bd9Sstevel@tonic-gate }
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate /* To simplify things, dupb() is implemented as copyb(). */
13887c478bd9Sstevel@tonic-gate mblk_t *
13897c478bd9Sstevel@tonic-gate dupb(mblk_t *mp)
13907c478bd9Sstevel@tonic-gate {
13917c478bd9Sstevel@tonic-gate 	return (copyb(mp));
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate /*
13957c478bd9Sstevel@tonic-gate  * get number of data bytes in message
13967c478bd9Sstevel@tonic-gate  */
13977c478bd9Sstevel@tonic-gate size_t
13987c478bd9Sstevel@tonic-gate msgdsize(mblk_t *bp)
13997c478bd9Sstevel@tonic-gate {
14007c478bd9Sstevel@tonic-gate 	size_t count = 0;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	for (; bp != NULL; bp = bp->b_cont) {
14037c478bd9Sstevel@tonic-gate 		assert(bp->b_wptr >= bp->b_rptr);
14047c478bd9Sstevel@tonic-gate 		count += bp->b_wptr - bp->b_rptr;
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 	return (count);
14077c478bd9Sstevel@tonic-gate }
1408