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