xref: /titanic_44/usr/src/stand/lib/sock/socket.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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 /*
2366ac517eSjk115741  * 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 
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * keep calling non-blocking recvfrom until something received
5247c478bd9Sstevel@tonic-gate 	 * or an error occurs
5257c478bd9Sstevel@tonic-gate 	 */
5267c478bd9Sstevel@tonic-gate 	start = prom_gettime();
52766ac517eSjk115741 	for (;;) {
52866ac517eSjk115741 		n = recvfrom(s, buf, nbyte, MSG_DONTWAIT, NULL, NULL);
52966ac517eSjk115741 		if (n == -1 && errno == EWOULDBLOCK) {
5307c478bd9Sstevel@tonic-gate 			diff = (uint_t)((prom_gettime() - start) + 500) / 1000;
5317c478bd9Sstevel@tonic-gate 			if (read_timeout != 0 && diff > read_timeout) {
5327c478bd9Sstevel@tonic-gate 				errno = EINTR;
5337c478bd9Sstevel@tonic-gate 				return (-1);
5347c478bd9Sstevel@tonic-gate 			}
53566ac517eSjk115741 		} else {
5367c478bd9Sstevel@tonic-gate 			return (n);
5377c478bd9Sstevel@tonic-gate 		}
53866ac517eSjk115741 	}
53966ac517eSjk115741 }
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate /*
5427c478bd9Sstevel@tonic-gate  * Write up to `nbyte' bytes of data from `buf' to the address pointed to
5437c478bd9Sstevel@tonic-gate  * `addr' using socket `s'.  Returns the number of bytes writte on success,
5447c478bd9Sstevel@tonic-gate  * or -1 on failure.
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate int
5477c478bd9Sstevel@tonic-gate socket_write(int s, const void *buf, size_t nbyte, struct sockaddr_in *addr)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	return (sendto(s, buf, nbyte, 0, (struct sockaddr *)addr,
5507c478bd9Sstevel@tonic-gate 	    sizeof (*addr)));
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate static int
5547c478bd9Sstevel@tonic-gate bind_check(int sock_id, const struct sockaddr *addr)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	int k;
5577c478bd9Sstevel@tonic-gate 	struct sockaddr_in *in_addr = (struct sockaddr_in *)addr;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	/* Do not check for duplicate bind() if SO_REUSEADDR option is set. */
5607c478bd9Sstevel@tonic-gate 	if (! (sockets[sock_id].so_opt & SO_REUSEADDR)) {
5617c478bd9Sstevel@tonic-gate 		for (k = 0; k < MAXSOCKET; k++) {
5627c478bd9Sstevel@tonic-gate 			if (sockets[k].type != INETBOOT_UNUSED &&
5637c478bd9Sstevel@tonic-gate 			    sockets[k].proto == sockets[sock_id].proto &&
5647c478bd9Sstevel@tonic-gate 			    sockets[k].bound) {
5657c478bd9Sstevel@tonic-gate 				if ((sockets[k].bind.sin_addr.s_addr ==
5667c478bd9Sstevel@tonic-gate 				    in_addr->sin_addr.s_addr) &&
5677c478bd9Sstevel@tonic-gate 				    (sockets[k].bind.sin_port ==
5687c478bd9Sstevel@tonic-gate 				    in_addr->sin_port)) {
5697c478bd9Sstevel@tonic-gate 					errno = EADDRINUSE;
5707c478bd9Sstevel@tonic-gate 					return (-1);
5717c478bd9Sstevel@tonic-gate 				}
5727c478bd9Sstevel@tonic-gate 			}
5737c478bd9Sstevel@tonic-gate 		}
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 	return (0);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate /* Assign a name to an unnamed socket. */
5797c478bd9Sstevel@tonic-gate int
5807c478bd9Sstevel@tonic-gate bind(int s, const struct sockaddr *name, socklen_t namelen)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	int i;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	errno = 0;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if ((i = so_check_fd(s, &errno)) == -1)
5877c478bd9Sstevel@tonic-gate 		return (-1);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (name == NULL) {
5907c478bd9Sstevel@tonic-gate 		/* unbind */
5917c478bd9Sstevel@tonic-gate 		if (sockets[i].bound) {
5927c478bd9Sstevel@tonic-gate 			bzero((caddr_t)&sockets[i].bind,
5937c478bd9Sstevel@tonic-gate 			    sizeof (struct sockaddr_in));
5947c478bd9Sstevel@tonic-gate 			sockets[i].bound = B_FALSE;
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 		return (0);
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 	if (namelen != sizeof (struct sockaddr_in) || name == NULL) {
5997c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6007c478bd9Sstevel@tonic-gate 		return (-1);
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 	if (name->sa_family != AF_INET) {
6037c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
6047c478bd9Sstevel@tonic-gate 		return (-1);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 	if (sockets[i].bound) {
6077c478bd9Sstevel@tonic-gate 		if (bcmp((caddr_t)&sockets[i].bind, (caddr_t)name,
6087c478bd9Sstevel@tonic-gate 		    namelen) == 0) {
6097c478bd9Sstevel@tonic-gate 			/* attempt to bind to same address ok... */
6107c478bd9Sstevel@tonic-gate 			return (0);
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 		errno = EINVAL;	/* already bound */
6137c478bd9Sstevel@tonic-gate 		return (-1);
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (errno != 0) {
6177c478bd9Sstevel@tonic-gate 		return (-1);
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/* Check for duplicate bind(). */
6217c478bd9Sstevel@tonic-gate 	if (bind_check(i, name) < 0)
6227c478bd9Sstevel@tonic-gate 		return (-1);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	bcopy((caddr_t)name, (caddr_t)&sockets[i].bind, namelen);
6257c478bd9Sstevel@tonic-gate 	if (sockets[i].type == INETBOOT_STREAM) {
6267c478bd9Sstevel@tonic-gate 		if (tcp_bind(i) < 0) {
6277c478bd9Sstevel@tonic-gate 			return (-1);
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 	sockets[i].bound = B_TRUE;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	return (0);
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate static int
6367c478bd9Sstevel@tonic-gate quickbind(int sock_id)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	int i;
6397c478bd9Sstevel@tonic-gate 	struct sockaddr_in addr;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	 * XXX This needs more work.  Right now, if ipv4_setipaddr()
6437c478bd9Sstevel@tonic-gate 	 * have not been called, this will be wrong.  But we need
6447c478bd9Sstevel@tonic-gate 	 * something better.  Need to be revisited.
6457c478bd9Sstevel@tonic-gate 	 */
6467c478bd9Sstevel@tonic-gate 	ipv4_getipaddr(&addr.sin_addr);
6477c478bd9Sstevel@tonic-gate 	addr.sin_family = AF_INET;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	for (i = SMALLEST_ANON_PORT; i <= LARGEST_ANON_PORT; i++) {
6507c478bd9Sstevel@tonic-gate 		addr.sin_port = htons(i);
6517c478bd9Sstevel@tonic-gate 		if (bind_check(sock_id, (struct sockaddr *)&addr) == 0)
6527c478bd9Sstevel@tonic-gate 			break;
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	/* Need to clear errno as it is probably set by bind_check(). */
6557c478bd9Sstevel@tonic-gate 	errno = 0;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (i <= LARGEST_ANON_PORT) {
6587c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)&addr, (caddr_t)&sockets[sock_id].bind,
6597c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in));
6607c478bd9Sstevel@tonic-gate 		sockets[sock_id].bound = B_TRUE;
6617c478bd9Sstevel@tonic-gate #ifdef DEBUG
6627c478bd9Sstevel@tonic-gate 		printf("quick bind done addr %s port %d\n",
6637c478bd9Sstevel@tonic-gate 		    inet_ntoa(sockets[sock_id].bind.sin_addr),
6647c478bd9Sstevel@tonic-gate 			ntohs(sockets[sock_id].bind.sin_port));
6657c478bd9Sstevel@tonic-gate #endif
6667c478bd9Sstevel@tonic-gate 		return (0);
6677c478bd9Sstevel@tonic-gate 	} else {
6687c478bd9Sstevel@tonic-gate 		return (-1);
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate int
6737c478bd9Sstevel@tonic-gate listen(int fd, int backlog)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	int sock_id;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	errno = 0;
6787c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(fd, &errno)) == -1)
6797c478bd9Sstevel@tonic-gate 		return (-1);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].type != INETBOOT_STREAM) {
6827c478bd9Sstevel@tonic-gate 		errno = EOPNOTSUPP;
6837c478bd9Sstevel@tonic-gate 		return (-1);
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].so_error != 0) {
6867c478bd9Sstevel@tonic-gate 		errno = sockets[sock_id].so_error;
6877c478bd9Sstevel@tonic-gate 		return (-1);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 	return (tcp_listen(sock_id, backlog));
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate int
6937c478bd9Sstevel@tonic-gate accept(int fd, struct sockaddr *addr,  socklen_t *addr_len)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	int sock_id;
6967c478bd9Sstevel@tonic-gate 	int new_sd;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	errno = 0;
6997c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(fd, &errno)) == -1)
7007c478bd9Sstevel@tonic-gate 		return (-1);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].type != INETBOOT_STREAM) {
7037c478bd9Sstevel@tonic-gate 		errno = EOPNOTSUPP;
7047c478bd9Sstevel@tonic-gate 		return (-1);
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].so_error != 0) {
7077c478bd9Sstevel@tonic-gate 		errno = sockets[sock_id].so_error;
7087c478bd9Sstevel@tonic-gate 		return (-1);
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 	if ((new_sd = tcp_accept(sock_id, addr, addr_len)) == -1)
7117c478bd9Sstevel@tonic-gate 		return (-1);
7127c478bd9Sstevel@tonic-gate 	sock_id = so_check_fd(new_sd, &errno);
7137c478bd9Sstevel@tonic-gate 	sockets[sock_id].so_state |= SS_ISCONNECTED;
7147c478bd9Sstevel@tonic-gate 	return (new_sd);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate int
7187c478bd9Sstevel@tonic-gate connect(int fd, const  struct sockaddr *addr, socklen_t addr_len)
7197c478bd9Sstevel@tonic-gate {
7207c478bd9Sstevel@tonic-gate 	int sock_id;
7217c478bd9Sstevel@tonic-gate 	int so_type;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	errno = 0;
7247c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(fd, &errno)) == -1)
7257c478bd9Sstevel@tonic-gate 		return (-1);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	so_type = sockets[sock_id].type;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if (addr == NULL || addr_len == 0) {
7307c478bd9Sstevel@tonic-gate 		errno = EINVAL;
7317c478bd9Sstevel@tonic-gate 		return (-1);
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	/* Don't allow connect for raw socket. */
7347c478bd9Sstevel@tonic-gate 	if (so_type == INETBOOT_RAW) {
7357c478bd9Sstevel@tonic-gate 		errno = EPROTONOSUPPORT;
7367c478bd9Sstevel@tonic-gate 		return (-1);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].so_state & SS_ISCONNECTED) {
7407c478bd9Sstevel@tonic-gate 		errno = EINVAL;
7417c478bd9Sstevel@tonic-gate 		return (-1);
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].so_error != 0) {
7457c478bd9Sstevel@tonic-gate 		errno = sockets[sock_id].so_error;
7467c478bd9Sstevel@tonic-gate 		return (-1);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/* If the socket is not bound, we need to do a quick bind. */
7507c478bd9Sstevel@tonic-gate 	if (!sockets[sock_id].bound) {
7517c478bd9Sstevel@tonic-gate 		/* For TCP socket, just call tcp_bind(). */
7527c478bd9Sstevel@tonic-gate 		if (so_type == INETBOOT_STREAM) {
7537c478bd9Sstevel@tonic-gate 			if (tcp_bind(sock_id) < 0)
7547c478bd9Sstevel@tonic-gate 				return (-1);
7557c478bd9Sstevel@tonic-gate 		} else {
7567c478bd9Sstevel@tonic-gate 			if (quickbind(sock_id) < 0) {
7577c478bd9Sstevel@tonic-gate 				errno = EADDRNOTAVAIL;
7587c478bd9Sstevel@tonic-gate 				return (-1);
7597c478bd9Sstevel@tonic-gate 			}
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 	/* Should do some sanity check for addr .... */
7637c478bd9Sstevel@tonic-gate 	bcopy((caddr_t)addr, &sockets[sock_id].remote,
7647c478bd9Sstevel@tonic-gate 	    sizeof (struct sockaddr_in));
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].type == INETBOOT_STREAM) {
7677c478bd9Sstevel@tonic-gate 		/* Call TCP connect routine. */
7687c478bd9Sstevel@tonic-gate 		if (tcp_connect(sock_id) == 0)
7697c478bd9Sstevel@tonic-gate 			sockets[sock_id].so_state |= SS_ISCONNECTED;
7707c478bd9Sstevel@tonic-gate 		else {
7717c478bd9Sstevel@tonic-gate 			if (sockets[sock_id].so_error != 0)
7727c478bd9Sstevel@tonic-gate 				errno = sockets[sock_id].so_error;
7737c478bd9Sstevel@tonic-gate 			return (-1);
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 	} else {
7767c478bd9Sstevel@tonic-gate 		sockets[sock_id].so_state |= SS_ISCONNECTED;
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 	return (0);
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate /* Just a wrapper around recvfrom(). */
7827c478bd9Sstevel@tonic-gate ssize_t
7837c478bd9Sstevel@tonic-gate recv(int s, void *buf, size_t len, int flags)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	return (recvfrom(s, buf, len, flags, NULL, NULL));
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate /*
7897c478bd9Sstevel@tonic-gate  * Receive messages from a connectionless socket. Legal flags are 0 and
7907c478bd9Sstevel@tonic-gate  * MSG_DONTWAIT. MSG_WAITALL is not currently supported.
7917c478bd9Sstevel@tonic-gate  *
7927c478bd9Sstevel@tonic-gate  * Returns length of message for success, -1 if error occurred.
7937c478bd9Sstevel@tonic-gate  */
7947c478bd9Sstevel@tonic-gate ssize_t
7957c478bd9Sstevel@tonic-gate recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from,
7967c478bd9Sstevel@tonic-gate     socklen_t *fromlen)
7977c478bd9Sstevel@tonic-gate {
7987c478bd9Sstevel@tonic-gate 	int			sock_id, i;
7997c478bd9Sstevel@tonic-gate 	ssize_t			datalen, bytes = 0;
8007c478bd9Sstevel@tonic-gate 	struct inetgram		*icp;
8017c478bd9Sstevel@tonic-gate 	enum SockType		so_type;
8027c478bd9Sstevel@tonic-gate 	char			*tmp_buf;
8037c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	errno = 0;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(s, &errno)) == -1) {
8087c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8097c478bd9Sstevel@tonic-gate 		return (-1);
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].type == INETBOOT_STREAM &&
8137c478bd9Sstevel@tonic-gate 	    !(sockets[sock_id].so_state & SS_ISCONNECTED)) {
8147c478bd9Sstevel@tonic-gate 		errno = ENOTCONN;
8157c478bd9Sstevel@tonic-gate 		return (-1);
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if (buf == NULL || len == 0) {
8197c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8207c478bd9Sstevel@tonic-gate 		return (-1);
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	/* Yup - MSG_WAITALL not implemented */
8237c478bd9Sstevel@tonic-gate 	if ((flags & ~MSG_DONTWAIT) != 0) {
8247c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8257c478bd9Sstevel@tonic-gate 		return (-1);
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate retry:
8297c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].inq == NULL) {
8307c478bd9Sstevel@tonic-gate 		/* Go out and check the wire */
8317c478bd9Sstevel@tonic-gate 		for (i = MEDIA_LVL; i < APP_LVL; i++) {
8327c478bd9Sstevel@tonic-gate 			if (sockets[sock_id].input[i] != NULL) {
8337c478bd9Sstevel@tonic-gate 				if (sockets[sock_id].input[i](sock_id) < 0) {
8347c478bd9Sstevel@tonic-gate 					if (sockets[sock_id].so_error != 0) {
8357c478bd9Sstevel@tonic-gate 						errno =
8367c478bd9Sstevel@tonic-gate 						    sockets[sock_id].so_error;
8377c478bd9Sstevel@tonic-gate 					}
8387c478bd9Sstevel@tonic-gate 					return (-1);
8397c478bd9Sstevel@tonic-gate 				}
8407c478bd9Sstevel@tonic-gate 			}
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	so_type = sockets[sock_id].type;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	/* Remove unknown inetgrams from the head of inq.  Can this happen? */
8477c478bd9Sstevel@tonic-gate 	while ((icp = sockets[sock_id].inq) != NULL) {
8487c478bd9Sstevel@tonic-gate 		if ((so_type == INETBOOT_DGRAM ||
8497c478bd9Sstevel@tonic-gate 		    so_type == INETBOOT_STREAM) &&
8507c478bd9Sstevel@tonic-gate 		    icp->igm_level != APP_LVL) {
8517c478bd9Sstevel@tonic-gate #ifdef	DEBUG
8527c478bd9Sstevel@tonic-gate 			printf("recvfrom: unexpected level %d frame found\n",
8537c478bd9Sstevel@tonic-gate 			    icp->igm_level);
8547c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
8557c478bd9Sstevel@tonic-gate 			del_gram(&sockets[sock_id].inq, icp, B_TRUE);
8567c478bd9Sstevel@tonic-gate 			continue;
8577c478bd9Sstevel@tonic-gate 		} else {
8587c478bd9Sstevel@tonic-gate 			break;
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	if (icp == NULL) {
8647c478bd9Sstevel@tonic-gate 		/*
8657c478bd9Sstevel@tonic-gate 		 * Checking for error should be done everytime a lower layer
8667c478bd9Sstevel@tonic-gate 		 * input routing is called.  For example, if TCP gets a RST,
8677c478bd9Sstevel@tonic-gate 		 * this should be reported asap.
8687c478bd9Sstevel@tonic-gate 		 */
8697c478bd9Sstevel@tonic-gate 		if (sockets[sock_id].so_state & SS_CANTRCVMORE) {
8707c478bd9Sstevel@tonic-gate 			if (sockets[sock_id].so_error != 0) {
8717c478bd9Sstevel@tonic-gate 				errno = sockets[sock_id].so_error;
8727c478bd9Sstevel@tonic-gate 				return (-1);
8737c478bd9Sstevel@tonic-gate 			} else {
8747c478bd9Sstevel@tonic-gate 				return (0);
8757c478bd9Sstevel@tonic-gate 			}
8767c478bd9Sstevel@tonic-gate 		}
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		if ((flags & MSG_DONTWAIT) == 0)
8797c478bd9Sstevel@tonic-gate 			goto retry;	/* wait forever */
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 		/* no data */
8827c478bd9Sstevel@tonic-gate 		errno = EWOULDBLOCK;
8837c478bd9Sstevel@tonic-gate 		return (-1);
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	if (from != NULL && fromlen != NULL) {
8877c478bd9Sstevel@tonic-gate 		switch (so_type) {
8887c478bd9Sstevel@tonic-gate 		case INETBOOT_STREAM:
8897c478bd9Sstevel@tonic-gate 			/* Need to copy from the socket's remote address. */
8907c478bd9Sstevel@tonic-gate 			bcopy(&(sockets[sock_id].remote), from, MIN(*fromlen,
8917c478bd9Sstevel@tonic-gate 			    sizeof (struct sockaddr_in)));
8927c478bd9Sstevel@tonic-gate 			break;
8937c478bd9Sstevel@tonic-gate 		case INETBOOT_RAW:
8947c478bd9Sstevel@tonic-gate 		case INETBOOT_DGRAM:
8957c478bd9Sstevel@tonic-gate 		default:
8967c478bd9Sstevel@tonic-gate 			if (*fromlen > sizeof (icp->igm_saddr))
8977c478bd9Sstevel@tonic-gate 				*fromlen = sizeof (icp->igm_saddr);
8987c478bd9Sstevel@tonic-gate 			bcopy((caddr_t)&(icp->igm_saddr), (caddr_t)from,
8997c478bd9Sstevel@tonic-gate 			    MIN(*fromlen, sizeof (struct sockaddr_in)));
9007c478bd9Sstevel@tonic-gate 			break;
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	mp = icp->igm_mp;
9057c478bd9Sstevel@tonic-gate 	switch (so_type) {
9067c478bd9Sstevel@tonic-gate 	case INETBOOT_STREAM:
9077c478bd9Sstevel@tonic-gate 		/*
9087c478bd9Sstevel@tonic-gate 		 * If the message has igm_id == TCP_CALLB_MAGIC_ID, we need
9097c478bd9Sstevel@tonic-gate 		 * to drain the data held by tcp and try again.
9107c478bd9Sstevel@tonic-gate 		 */
9117c478bd9Sstevel@tonic-gate 		if (icp->igm_id == TCP_CALLB_MAGIC_ID) {
9127c478bd9Sstevel@tonic-gate 			del_gram(&sockets[sock_id].inq, icp, B_TRUE);
9137c478bd9Sstevel@tonic-gate 			tcp_rcv_drain_sock(sock_id);
9147c478bd9Sstevel@tonic-gate 			goto retry;
9157c478bd9Sstevel@tonic-gate 		}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		/* TCP should put only user data in the inetgram. */
9187c478bd9Sstevel@tonic-gate 		tmp_buf = (char *)buf;
9197c478bd9Sstevel@tonic-gate 		while (len > 0 && icp != NULL) {
9207c478bd9Sstevel@tonic-gate 			datalen = mp->b_wptr - mp->b_rptr;
9217c478bd9Sstevel@tonic-gate 			if (len < datalen) {
9227c478bd9Sstevel@tonic-gate 				bcopy(mp->b_rptr, tmp_buf, len);
9237c478bd9Sstevel@tonic-gate 				bytes += len;
9247c478bd9Sstevel@tonic-gate 				mp->b_rptr += len;
9257c478bd9Sstevel@tonic-gate 				break;
9267c478bd9Sstevel@tonic-gate 			} else {
9277c478bd9Sstevel@tonic-gate 				bcopy(mp->b_rptr, tmp_buf, datalen);
9287c478bd9Sstevel@tonic-gate 				len -= datalen;
9297c478bd9Sstevel@tonic-gate 				bytes += datalen;
9307c478bd9Sstevel@tonic-gate 				tmp_buf += datalen;
9317c478bd9Sstevel@tonic-gate 				del_gram(&sockets[sock_id].inq, icp, B_TRUE);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 				/*
9347c478bd9Sstevel@tonic-gate 				 * If we have any embedded magic messages just
9357c478bd9Sstevel@tonic-gate 				 * drop them.
9367c478bd9Sstevel@tonic-gate 				 */
9377c478bd9Sstevel@tonic-gate 				while ((icp = sockets[sock_id].inq) != NULL) {
9387c478bd9Sstevel@tonic-gate 					if (icp->igm_id != TCP_CALLB_MAGIC_ID)
9397c478bd9Sstevel@tonic-gate 						break;
9407c478bd9Sstevel@tonic-gate 					del_gram(&sockets[sock_id].inq, icp,
9417c478bd9Sstevel@tonic-gate 						B_TRUE);
9427c478bd9Sstevel@tonic-gate 				}
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 				if (icp == NULL)
9457c478bd9Sstevel@tonic-gate 					break;
9467c478bd9Sstevel@tonic-gate 				mp = icp->igm_mp;
9477c478bd9Sstevel@tonic-gate 			}
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 		sockets[sock_id].so_rcvbuf += (int32_t)bytes;
9507c478bd9Sstevel@tonic-gate 		break;
9517c478bd9Sstevel@tonic-gate 	case INETBOOT_DGRAM:
9527c478bd9Sstevel@tonic-gate 		datalen = mp->b_wptr - mp->b_rptr;
9537c478bd9Sstevel@tonic-gate 		if (len < datalen)
9547c478bd9Sstevel@tonic-gate 			bytes = len;
9557c478bd9Sstevel@tonic-gate 		else
9567c478bd9Sstevel@tonic-gate 			bytes = datalen;
9577c478bd9Sstevel@tonic-gate 		bcopy(mp->b_rptr, buf, bytes);
9587c478bd9Sstevel@tonic-gate 		del_gram(&sockets[sock_id].inq, icp, B_TRUE);
9597c478bd9Sstevel@tonic-gate 		break;
9607c478bd9Sstevel@tonic-gate 	case INETBOOT_RAW:
9617c478bd9Sstevel@tonic-gate 	default:
9627c478bd9Sstevel@tonic-gate 		datalen = mp->b_wptr - mp->b_rptr;
9637c478bd9Sstevel@tonic-gate 		if (len < datalen)
9647c478bd9Sstevel@tonic-gate 			bytes = len;
9657c478bd9Sstevel@tonic-gate 		else
9667c478bd9Sstevel@tonic-gate 			bytes = datalen;
9677c478bd9Sstevel@tonic-gate 		bcopy(mp->b_rptr, buf, bytes);
9687c478bd9Sstevel@tonic-gate 		del_gram(&sockets[sock_id].inq, icp, B_TRUE);
9697c478bd9Sstevel@tonic-gate 		break;
9707c478bd9Sstevel@tonic-gate 	}
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate #ifdef	DEBUG
9737c478bd9Sstevel@tonic-gate 	printf("recvfrom(%d): data: (0x%x,%d)\n", sock_id,
9747c478bd9Sstevel@tonic-gate 	    (icp != NULL) ? icp->igm_mp : 0, bytes);
9757c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
9767c478bd9Sstevel@tonic-gate 	return (bytes);
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /* Just a wrapper around sendto(). */
9817c478bd9Sstevel@tonic-gate ssize_t
9827c478bd9Sstevel@tonic-gate send(int s, const void *msg, size_t len, int flags)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	return (sendto(s, msg, len, flags, NULL, 0));
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate /*
9887c478bd9Sstevel@tonic-gate  * Transmit a message through a socket.
9897c478bd9Sstevel@tonic-gate  *
9907c478bd9Sstevel@tonic-gate  * Supported flags: MSG_DONTROUTE or 0.
9917c478bd9Sstevel@tonic-gate  */
9927c478bd9Sstevel@tonic-gate ssize_t
9937c478bd9Sstevel@tonic-gate sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to,
9947c478bd9Sstevel@tonic-gate     socklen_t tolen)
9957c478bd9Sstevel@tonic-gate {
9967c478bd9Sstevel@tonic-gate 	enum SockType so_type;
9977c478bd9Sstevel@tonic-gate 	int sock_id;
9987c478bd9Sstevel@tonic-gate 	ssize_t bytes;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	errno = 0;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	if ((sock_id = so_check_fd(s, &errno)) == -1) {
10037c478bd9Sstevel@tonic-gate 		return (-1);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 	if (msg == NULL) {
10067c478bd9Sstevel@tonic-gate 		errno = EINVAL;
10077c478bd9Sstevel@tonic-gate 		return (-1);
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 	so_type = sockets[sock_id].type;
10107c478bd9Sstevel@tonic-gate 	if ((flags & ~MSG_DONTROUTE) != 0) {
10117c478bd9Sstevel@tonic-gate 		errno = EINVAL;
10127c478bd9Sstevel@tonic-gate 		return (-1);
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 	if (sockets[sock_id].so_error != 0) {
10157c478bd9Sstevel@tonic-gate 		errno = sockets[sock_id].so_error;
10167c478bd9Sstevel@tonic-gate 		return (-1);
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 	if (to != NULL && to->sa_family != AF_INET) {
10197c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
10207c478bd9Sstevel@tonic-gate 		return (-1);
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	switch (so_type) {
10247c478bd9Sstevel@tonic-gate 	case INETBOOT_RAW:
10257c478bd9Sstevel@tonic-gate 	case INETBOOT_DGRAM:
10267c478bd9Sstevel@tonic-gate 		if (!(sockets[sock_id].so_state & SS_ISCONNECTED) &&
10277c478bd9Sstevel@tonic-gate 		    (to == NULL || tolen != sizeof (struct sockaddr_in))) {
10287c478bd9Sstevel@tonic-gate 			errno = EINVAL;
10297c478bd9Sstevel@tonic-gate 			return (-1);
10307c478bd9Sstevel@tonic-gate 		}
10317c478bd9Sstevel@tonic-gate 		bytes = dgram_sendto(sock_id, msg, len, flags, to, tolen);
10327c478bd9Sstevel@tonic-gate 		break;
10337c478bd9Sstevel@tonic-gate 	case INETBOOT_STREAM:
10347c478bd9Sstevel@tonic-gate 		if (!((sockets[sock_id].so_state & SS_ISCONNECTED) ||
10357c478bd9Sstevel@tonic-gate 		    (sockets[sock_id].so_state & SS_ISCONNECTING))) {
10367c478bd9Sstevel@tonic-gate 			errno = EINVAL;
10377c478bd9Sstevel@tonic-gate 			return (-1);
10387c478bd9Sstevel@tonic-gate 		}
10397c478bd9Sstevel@tonic-gate 		if (sockets[sock_id].so_state & SS_CANTSENDMORE) {
10407c478bd9Sstevel@tonic-gate 			errno = EPIPE;
10417c478bd9Sstevel@tonic-gate 			return (-1);
10427c478bd9Sstevel@tonic-gate 		}
10437c478bd9Sstevel@tonic-gate 		bytes = stream_sendto(sock_id, msg, len, flags);
10447c478bd9Sstevel@tonic-gate 		break;
10457c478bd9Sstevel@tonic-gate 	default:
10467c478bd9Sstevel@tonic-gate 		/* Should not happen... */
10477c478bd9Sstevel@tonic-gate 		errno = EPROTOTYPE;
10487c478bd9Sstevel@tonic-gate 		return (-1);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 	return (bytes);
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate static ssize_t
10547c478bd9Sstevel@tonic-gate dgram_sendto(int i, const void *msg, size_t len, int flags,
10557c478bd9Sstevel@tonic-gate     const struct sockaddr *to, int tolen)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	struct inetgram		oc;
10587c478bd9Sstevel@tonic-gate 	int			l, offset;
10597c478bd9Sstevel@tonic-gate 	size_t			tlen;
10607c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate #ifdef	DEBUG
10637c478bd9Sstevel@tonic-gate 	{
10647c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
10657c478bd9Sstevel@tonic-gate 	printf("sendto(%d): msg of length: %d sent to port %d and host: %s\n",
10667c478bd9Sstevel@tonic-gate 	    i, len, ntohs(sin->sin_port), inet_ntoa(sin->sin_addr));
10677c478bd9Sstevel@tonic-gate 	}
10687c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	nuke_grams(&sockets[i].inq); /* flush the input queue */
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	/* calculate offset for data */
10737c478bd9Sstevel@tonic-gate 	offset = sockets[i].headerlen[MEDIA_LVL](NULL) +
10747c478bd9Sstevel@tonic-gate 	    (sockets[i].headerlen[NETWORK_LVL])(NULL);
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&oc, sizeof (oc));
10777c478bd9Sstevel@tonic-gate 	if (sockets[i].type != INETBOOT_RAW) {
10787c478bd9Sstevel@tonic-gate 		offset += (sockets[i].headerlen[TRANSPORT_LVL])(NULL);
10797c478bd9Sstevel@tonic-gate 		oc.igm_level = TRANSPORT_LVL;
10807c478bd9Sstevel@tonic-gate 	} else
10817c478bd9Sstevel@tonic-gate 		oc.igm_level = NETWORK_LVL;
10827c478bd9Sstevel@tonic-gate 	oc.igm_oflags = flags;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	if (to != NULL) {
10857c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)to, (caddr_t)&oc.igm_saddr, tolen);
10867c478bd9Sstevel@tonic-gate 	} else {
10877c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)&sockets[i].remote, (caddr_t)&oc.igm_saddr,
10887c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in));
10897c478bd9Sstevel@tonic-gate 	}
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	/* Get a legal source port if the socket isn't bound. */
10927c478bd9Sstevel@tonic-gate 	if (sockets[i].bound == B_FALSE &&
10937c478bd9Sstevel@tonic-gate 	    ntohs(oc.igm_saddr.sin_port == 0)) {
10947c478bd9Sstevel@tonic-gate 		((struct sockaddr_in *)&oc.igm_saddr)->sin_port =
10957c478bd9Sstevel@tonic-gate 		    get_source_port(B_FALSE);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	/* Round up to 16bit value for checksum purposes */
10997c478bd9Sstevel@tonic-gate 	if (sockets[i].type == INETBOOT_DGRAM) {
11007c478bd9Sstevel@tonic-gate 		tlen = ((len + sizeof (uint16_t) - 1) &
11017c478bd9Sstevel@tonic-gate 		    ~(sizeof (uint16_t) - 1));
11027c478bd9Sstevel@tonic-gate 	} else
11037c478bd9Sstevel@tonic-gate 		tlen = len;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	if ((oc.igm_mp = allocb(tlen + offset, 0)) == NULL) {
11067c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
11077c478bd9Sstevel@tonic-gate 		return (-1);
11087c478bd9Sstevel@tonic-gate 	}
11097c478bd9Sstevel@tonic-gate 	mp = oc.igm_mp;
11107c478bd9Sstevel@tonic-gate 	mp->b_rptr = mp->b_wptr += offset;
11117c478bd9Sstevel@tonic-gate 	bcopy((caddr_t)msg, mp->b_wptr, len);
11127c478bd9Sstevel@tonic-gate 	mp->b_wptr += len;
11137c478bd9Sstevel@tonic-gate 	for (l = TRANSPORT_LVL; l >= MEDIA_LVL; l--) {
11147c478bd9Sstevel@tonic-gate 		if (sockets[i].output[l] != NULL) {
11157c478bd9Sstevel@tonic-gate 			if (sockets[i].output[l](i, &oc) < 0) {
11167c478bd9Sstevel@tonic-gate 				freeb(mp);
11177c478bd9Sstevel@tonic-gate 				if (errno == 0)
11187c478bd9Sstevel@tonic-gate 					errno = EIO;
11197c478bd9Sstevel@tonic-gate 				return (-1);
11207c478bd9Sstevel@tonic-gate 			}
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 	freeb(mp);
11247c478bd9Sstevel@tonic-gate 	return (len);
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate /* ARGSUSED */
11287c478bd9Sstevel@tonic-gate static ssize_t
11297c478bd9Sstevel@tonic-gate stream_sendto(int i, const void *msg, size_t len, int flags)
11307c478bd9Sstevel@tonic-gate {
11317c478bd9Sstevel@tonic-gate 	int cnt;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	assert(sockets[i].pcb != NULL);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	/*
11367c478bd9Sstevel@tonic-gate 	 * Call directly TCP's send routine.  We do this because TCP
11377c478bd9Sstevel@tonic-gate 	 * needs to decide whether to send out the data.
11387c478bd9Sstevel@tonic-gate 	 *
11397c478bd9Sstevel@tonic-gate 	 * Note also that currently, TCP ignores all flags passed in for
11407c478bd9Sstevel@tonic-gate 	 * TCP socket.
11417c478bd9Sstevel@tonic-gate 	 */
11427c478bd9Sstevel@tonic-gate 	if ((cnt = tcp_send(i, sockets[i].pcb, msg, len)) < 0) {
11437c478bd9Sstevel@tonic-gate 		if (sockets[i].so_error != 0)
11447c478bd9Sstevel@tonic-gate 			errno = sockets[i].so_error;
11457c478bd9Sstevel@tonic-gate 		return (-1);
11467c478bd9Sstevel@tonic-gate 	} else {
11477c478bd9Sstevel@tonic-gate 		return (cnt);
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate /*
11527c478bd9Sstevel@tonic-gate  * Returns ptr to the last inetgram in the list, or null if list is null
11537c478bd9Sstevel@tonic-gate  */
11547c478bd9Sstevel@tonic-gate struct inetgram *
11557c478bd9Sstevel@tonic-gate last_gram(struct inetgram *igp)
11567c478bd9Sstevel@tonic-gate {
11577c478bd9Sstevel@tonic-gate 	struct inetgram	*wp;
11587c478bd9Sstevel@tonic-gate 	for (wp = igp; wp != NULL; wp = wp->igm_next) {
11597c478bd9Sstevel@tonic-gate 		if (wp->igm_next == NULL)
11607c478bd9Sstevel@tonic-gate 			return (wp);
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 	return (NULL);
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate /*
11667c478bd9Sstevel@tonic-gate  * Adds an inetgram or list of inetgrams to the end of the list.
11677c478bd9Sstevel@tonic-gate  */
11687c478bd9Sstevel@tonic-gate void
11697c478bd9Sstevel@tonic-gate add_grams(struct inetgram **igpp, struct inetgram *newgp)
11707c478bd9Sstevel@tonic-gate {
11717c478bd9Sstevel@tonic-gate 	struct inetgram	 *wp;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	if (newgp == NULL)
11747c478bd9Sstevel@tonic-gate 		return;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	if (*igpp == NULL)
11777c478bd9Sstevel@tonic-gate 		*igpp = newgp;
11787c478bd9Sstevel@tonic-gate 	else {
11797c478bd9Sstevel@tonic-gate 		wp = last_gram(*igpp);
11807c478bd9Sstevel@tonic-gate 		wp->igm_next = newgp;
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate /*
11857c478bd9Sstevel@tonic-gate  * Nuke a whole list of grams.
11867c478bd9Sstevel@tonic-gate  */
11877c478bd9Sstevel@tonic-gate void
11887c478bd9Sstevel@tonic-gate nuke_grams(struct inetgram **lgpp)
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate 	while (*lgpp != NULL)
11917c478bd9Sstevel@tonic-gate 		del_gram(lgpp, *lgpp, B_TRUE);
11927c478bd9Sstevel@tonic-gate }
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate /*
11957c478bd9Sstevel@tonic-gate  * Remove the referenced inetgram. List is altered accordingly. Destroy the
11967c478bd9Sstevel@tonic-gate  * referenced inetgram if freeit is B_TRUE.
11977c478bd9Sstevel@tonic-gate  */
11987c478bd9Sstevel@tonic-gate void
11997c478bd9Sstevel@tonic-gate del_gram(struct inetgram **lgpp, struct inetgram *igp, int freeit)
12007c478bd9Sstevel@tonic-gate {
12017c478bd9Sstevel@tonic-gate 	struct inetgram	*wp, *pp = NULL;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	if (lgpp == NULL || igp == NULL)
12047c478bd9Sstevel@tonic-gate 		return;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	wp = *lgpp;
12077c478bd9Sstevel@tonic-gate 	while (wp != NULL) {
12087c478bd9Sstevel@tonic-gate 		if (wp == igp) {
12097c478bd9Sstevel@tonic-gate 			/* detach wp from the list */
12107c478bd9Sstevel@tonic-gate 			if (*lgpp == wp)
12117c478bd9Sstevel@tonic-gate 				*lgpp = (*lgpp)->igm_next;
12127c478bd9Sstevel@tonic-gate 			else
12137c478bd9Sstevel@tonic-gate 				pp->igm_next = wp->igm_next;
12147c478bd9Sstevel@tonic-gate 			igp->igm_next = NULL;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 			if (freeit) {
12177c478bd9Sstevel@tonic-gate 				if (igp->igm_mp != NULL)
12187c478bd9Sstevel@tonic-gate 					freeb(igp->igm_mp);
12197c478bd9Sstevel@tonic-gate 				bkmem_free((caddr_t)igp,
12207c478bd9Sstevel@tonic-gate 				    sizeof (struct inetgram));
12217c478bd9Sstevel@tonic-gate 			}
12227c478bd9Sstevel@tonic-gate 			break;
12237c478bd9Sstevel@tonic-gate 		}
12247c478bd9Sstevel@tonic-gate 		pp = wp;
12257c478bd9Sstevel@tonic-gate 		wp = wp->igm_next;
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate struct nct_t nct[] = {
12307c478bd9Sstevel@tonic-gate 	"bootp",	NCT_BOOTP_DHCP,
12317c478bd9Sstevel@tonic-gate 	"dhcp",		NCT_BOOTP_DHCP,
12327c478bd9Sstevel@tonic-gate 	"rarp",		NCT_RARP_BOOTPARAMS,
12337c478bd9Sstevel@tonic-gate 	"manual",	NCT_MANUAL
12347c478bd9Sstevel@tonic-gate };
12357c478bd9Sstevel@tonic-gate int	nct_entries = sizeof (nct) / sizeof (nct[0]);
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate /*
12387c478bd9Sstevel@tonic-gate  * Figure out from the bootpath what kind of network configuration strategy
12397c478bd9Sstevel@tonic-gate  * we should use. Returns the network config strategy.
12407c478bd9Sstevel@tonic-gate  */
12417c478bd9Sstevel@tonic-gate int
12427c478bd9Sstevel@tonic-gate get_netconfig_strategy(void)
12437c478bd9Sstevel@tonic-gate {
12447c478bd9Sstevel@tonic-gate 	int	i;
12457c478bd9Sstevel@tonic-gate #if !defined(__i386)
12467c478bd9Sstevel@tonic-gate 	/* sparc */
12477c478bd9Sstevel@tonic-gate #define	ISSPACE(c) (c == ' ' || c == '\t' || c == '\n' || c == '\0')
12487c478bd9Sstevel@tonic-gate 	char	lbootpath[OBP_MAXPATHLEN];
12497c478bd9Sstevel@tonic-gate 	char	net_options[NCT_BUFSIZE];
12507c478bd9Sstevel@tonic-gate 	char	*op, *nop, *sp;
1251*fa9e4066Sahrens 	pnode_t	cn;
12527c478bd9Sstevel@tonic-gate 	int	proplen;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	/* If the PROM DHCP cache exists, we're done */
12557c478bd9Sstevel@tonic-gate 	if (prom_cached_reply(B_TRUE))
12567c478bd9Sstevel@tonic-gate 		return (NCT_BOOTP_DHCP);
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	/*
12597c478bd9Sstevel@tonic-gate 	 *	Newer (version 4) PROMs will put the name in the
12607c478bd9Sstevel@tonic-gate 	 *	"net-config-strategy" property.
12617c478bd9Sstevel@tonic-gate 	 */
12627c478bd9Sstevel@tonic-gate 	cn = prom_finddevice("/chosen");
12637c478bd9Sstevel@tonic-gate 	if ((proplen = prom_getproplen(cn, "net-config-strategy")) <
12647c478bd9Sstevel@tonic-gate 	    sizeof (net_options)) {
12657c478bd9Sstevel@tonic-gate 		(void) prom_getprop(cn, "net-config-strategy", net_options);
12667c478bd9Sstevel@tonic-gate 		net_options[proplen] = '\0';
12677c478bd9Sstevel@tonic-gate 	} else {
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 		/*
12707c478bd9Sstevel@tonic-gate 		 * We're reduced to sacanning bootpath for the prototol to use.
12717c478bd9Sstevel@tonic-gate 		 * Since there was no "net-config-strategy" property, this is
12727c478bd9Sstevel@tonic-gate 		 * an old PROM, so we need to excise any extraneous key/value
12737c478bd9Sstevel@tonic-gate 		 * initializations from bootpath[].
12747c478bd9Sstevel@tonic-gate 		 */
12757c478bd9Sstevel@tonic-gate 		for (op = prom_bootpath(), sp = lbootpath; op != NULL &&
12767c478bd9Sstevel@tonic-gate 		    !ISSPACE(*op); sp++, op++)
12777c478bd9Sstevel@tonic-gate 			*sp = *op;
12787c478bd9Sstevel@tonic-gate 		*sp = '\0';
12797c478bd9Sstevel@tonic-gate 		/* find the last '/' (in the device path) */
12807c478bd9Sstevel@tonic-gate 		if ((op = strrchr(lbootpath, '/')) == NULL)	/* last '/' */
12817c478bd9Sstevel@tonic-gate 			op = lbootpath;
12827c478bd9Sstevel@tonic-gate 		else
12837c478bd9Sstevel@tonic-gate 			op++;
12847c478bd9Sstevel@tonic-gate 		/* then look for the ':' separating it from the protocol */
12857c478bd9Sstevel@tonic-gate 		while (*op != ':' && *op != '\0')
12867c478bd9Sstevel@tonic-gate 			op++;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 		if (*op == ':') {
12897c478bd9Sstevel@tonic-gate 			for (nop = net_options, op++;
12907c478bd9Sstevel@tonic-gate 			    *op != '\0' && *op != '/' && !ISSPACE(*op) &&
12917c478bd9Sstevel@tonic-gate 			    nop < &net_options[NCT_BUFSIZE]; nop++, op++)
12927c478bd9Sstevel@tonic-gate 				*nop = *op;
12937c478bd9Sstevel@tonic-gate 			*nop = '\0';
12947c478bd9Sstevel@tonic-gate 		} else
12957c478bd9Sstevel@tonic-gate 			net_options[0] = '\0';
12967c478bd9Sstevel@tonic-gate 	}
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate #undef	ISSPACE
12997c478bd9Sstevel@tonic-gate #else
13007c478bd9Sstevel@tonic-gate 	/* i86 */
13017c478bd9Sstevel@tonic-gate 	extern struct bootops bootops;
13027c478bd9Sstevel@tonic-gate 	extern int bgetprop(struct bootops *, char *, caddr_t, int, phandle_t);
13037c478bd9Sstevel@tonic-gate 	char	net_options[MAXNAMELEN];
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	/*
13067c478bd9Sstevel@tonic-gate 	 * Look at net-config-strategy boot property to determine what protocol
13077c478bd9Sstevel@tonic-gate 	 * will be used.
13087c478bd9Sstevel@tonic-gate 	 */
13097c478bd9Sstevel@tonic-gate 	(void) bgetprop(&bootops, "net-config-strategy", net_options,
13107c478bd9Sstevel@tonic-gate 	    sizeof (net_options), 0);
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate #endif	/* __i386 */
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	for (i = 0; i < nct_entries; i++)
13157c478bd9Sstevel@tonic-gate 		if (strcmp(net_options, nct[i].p_name) == 0)
13167c478bd9Sstevel@tonic-gate 			return (nct[i].p_id);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	return (NCT_DEFAULT);
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate /* Modified STREAM routines for ease of porting core TCP code. */
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13247c478bd9Sstevel@tonic-gate mblk_t *
13257c478bd9Sstevel@tonic-gate allocb(size_t size, uint_t pri)
13267c478bd9Sstevel@tonic-gate {
13277c478bd9Sstevel@tonic-gate 	unsigned char *base;
13287c478bd9Sstevel@tonic-gate 	mblk_t *mp;
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	if ((mp = (mblk_t *)bkmem_zalloc(sizeof (mblk_t))) == NULL)
13317c478bd9Sstevel@tonic-gate 		return (NULL);
13327c478bd9Sstevel@tonic-gate 	if ((base = (unsigned char *)bkmem_zalloc(size)) == NULL)
13337c478bd9Sstevel@tonic-gate 		return (NULL);
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	mp->b_next = mp->b_prev = mp->b_cont = NULL;
13367c478bd9Sstevel@tonic-gate 	mp->b_rptr = mp->b_wptr = mp->b_datap = (unsigned char *)base;
13377c478bd9Sstevel@tonic-gate 	mp->b_size = size;
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	return (mp);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate void
13437c478bd9Sstevel@tonic-gate freeb(mblk_t *mp)
13447c478bd9Sstevel@tonic-gate {
13457c478bd9Sstevel@tonic-gate #ifdef DEBUG
13467c478bd9Sstevel@tonic-gate 	printf("freeb datap %x\n", mp->b_datap);
13477c478bd9Sstevel@tonic-gate #endif
13487c478bd9Sstevel@tonic-gate 	bkmem_free((caddr_t)(mp->b_datap), mp->b_size);
13497c478bd9Sstevel@tonic-gate #ifdef DEBUG
13507c478bd9Sstevel@tonic-gate 	printf("freeb mp %x\n", mp);
13517c478bd9Sstevel@tonic-gate #endif
13527c478bd9Sstevel@tonic-gate 	bkmem_free((caddr_t)mp, sizeof (mblk_t));
13537c478bd9Sstevel@tonic-gate }
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate void
13567c478bd9Sstevel@tonic-gate freemsg(mblk_t *mp)
13577c478bd9Sstevel@tonic-gate {
13587c478bd9Sstevel@tonic-gate 	while (mp) {
13597c478bd9Sstevel@tonic-gate 		mblk_t *mp_cont = mp->b_cont;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 		freeb(mp);
13627c478bd9Sstevel@tonic-gate 		mp = mp_cont;
13637c478bd9Sstevel@tonic-gate 	}
13647c478bd9Sstevel@tonic-gate }
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate mblk_t *
13677c478bd9Sstevel@tonic-gate copyb(mblk_t *bp)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	mblk_t *nbp;
13707c478bd9Sstevel@tonic-gate 	unsigned char *ndp;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	assert((uintptr_t)(bp->b_wptr - bp->b_rptr) >= 0);
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	if (!(nbp = allocb(bp->b_size, 0)))
13757c478bd9Sstevel@tonic-gate 		return (NULL);
13767c478bd9Sstevel@tonic-gate 	nbp->b_cont = NULL;
13777c478bd9Sstevel@tonic-gate 	ndp = nbp->b_datap;
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	nbp->b_rptr = ndp + (bp->b_rptr - bp->b_datap);
13807c478bd9Sstevel@tonic-gate 	nbp->b_wptr = nbp->b_rptr + (bp->b_wptr - bp->b_rptr);
13817c478bd9Sstevel@tonic-gate 	bcopy(bp->b_datap, nbp->b_datap, bp->b_size);
13827c478bd9Sstevel@tonic-gate 	return (nbp);
13837c478bd9Sstevel@tonic-gate }
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate /* To simplify things, dupb() is implemented as copyb(). */
13867c478bd9Sstevel@tonic-gate mblk_t *
13877c478bd9Sstevel@tonic-gate dupb(mblk_t *mp)
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate 	return (copyb(mp));
13907c478bd9Sstevel@tonic-gate }
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate /*
13937c478bd9Sstevel@tonic-gate  * get number of data bytes in message
13947c478bd9Sstevel@tonic-gate  */
13957c478bd9Sstevel@tonic-gate size_t
13967c478bd9Sstevel@tonic-gate msgdsize(mblk_t *bp)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate 	size_t count = 0;
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	for (; bp != NULL; bp = bp->b_cont) {
14017c478bd9Sstevel@tonic-gate 		assert(bp->b_wptr >= bp->b_rptr);
14027c478bd9Sstevel@tonic-gate 		count += bp->b_wptr - bp->b_rptr;
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 	return (count);
14057c478bd9Sstevel@tonic-gate }
1406