10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51548Srshoaib * Common Development and Distribution License (the "License"). 61548Srshoaib * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211548Srshoaib 220Sstevel@tonic-gate /* 238941SAnders.Persson@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 320Sstevel@tonic-gate * The Regents of the University of California 330Sstevel@tonic-gate * All Rights Reserved 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 370Sstevel@tonic-gate * contributors. 380Sstevel@tonic-gate */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate #ifndef _SYS_SOCKETVAR_H 410Sstevel@tonic-gate #define _SYS_SOCKETVAR_H 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include <sys/types.h> 440Sstevel@tonic-gate #include <sys/stream.h> 450Sstevel@tonic-gate #include <sys/t_lock.h> 460Sstevel@tonic-gate #include <sys/cred.h> 470Sstevel@tonic-gate #include <sys/vnode.h> 480Sstevel@tonic-gate #include <sys/file.h> 490Sstevel@tonic-gate #include <sys/param.h> 500Sstevel@tonic-gate #include <sys/zone.h> 518348SEric.Yu@Sun.COM #include <sys/sdt.h> 528348SEric.Yu@Sun.COM #include <sys/modctl.h> 538348SEric.Yu@Sun.COM #include <sys/atomic.h> 548348SEric.Yu@Sun.COM #include <sys/socket.h> 558348SEric.Yu@Sun.COM #include <sys/ksocket.h> 568964SAnders.Persson@Sun.COM #include <sys/kstat.h> 570Sstevel@tonic-gate 58*9694SScott.Rotondo@Sun.COM #ifdef _KERNEL 59*9694SScott.Rotondo@Sun.COM #include <sys/vfs_opreg.h> 60*9694SScott.Rotondo@Sun.COM #endif 61*9694SScott.Rotondo@Sun.COM 620Sstevel@tonic-gate #ifdef __cplusplus 630Sstevel@tonic-gate extern "C" { 640Sstevel@tonic-gate #endif 650Sstevel@tonic-gate 660Sstevel@tonic-gate /* 670Sstevel@tonic-gate * Internal representation of the address used to represent addresses 680Sstevel@tonic-gate * in the loopback transport for AF_UNIX. While the sockaddr_un is used 690Sstevel@tonic-gate * as the sockfs layer address for AF_UNIX the pathnames contained in 700Sstevel@tonic-gate * these addresses are not unique (due to relative pathnames) thus can not 710Sstevel@tonic-gate * be used in the transport. 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * The transport level address consists of a magic number (used to separate the 740Sstevel@tonic-gate * name space for specific and implicit binds). For a specific bind 750Sstevel@tonic-gate * this is followed by a "vnode *" which ensures that all specific binds 760Sstevel@tonic-gate * have a unique transport level address. For implicit binds the latter 770Sstevel@tonic-gate * part of the address is a byte string (of the same length as a pointer) 780Sstevel@tonic-gate * that is assigned by the loopback transport. 790Sstevel@tonic-gate * 800Sstevel@tonic-gate * The uniqueness assumes that the loopback transport has a separate namespace 810Sstevel@tonic-gate * for sockets in order to avoid name conflicts with e.g. TLI use of the 820Sstevel@tonic-gate * same transport. 830Sstevel@tonic-gate */ 840Sstevel@tonic-gate struct so_ux_addr { 850Sstevel@tonic-gate void *soua_vp; /* vnode pointer or assigned by tl */ 860Sstevel@tonic-gate uint_t soua_magic; /* See below */ 870Sstevel@tonic-gate }; 880Sstevel@tonic-gate 890Sstevel@tonic-gate #define SOU_MAGIC_EXPLICIT 0x75787670 /* "uxvp" */ 900Sstevel@tonic-gate #define SOU_MAGIC_IMPLICIT 0x616e6f6e /* "anon" */ 910Sstevel@tonic-gate 920Sstevel@tonic-gate struct sockaddr_ux { 930Sstevel@tonic-gate sa_family_t sou_family; /* AF_UNIX */ 940Sstevel@tonic-gate struct so_ux_addr sou_addr; 950Sstevel@tonic-gate }; 960Sstevel@tonic-gate 978348SEric.Yu@Sun.COM #if defined(_KERNEL) || defined(_KMEMUSER) 988348SEric.Yu@Sun.COM 998348SEric.Yu@Sun.COM #include <sys/socket_proto.h> 1008348SEric.Yu@Sun.COM 1010Sstevel@tonic-gate typedef struct sonodeops sonodeops_t; 102741Smasputra typedef struct sonode sonode_t; 1030Sstevel@tonic-gate 1049491SAnders.Persson@Sun.COM struct sodirect_s; 1059491SAnders.Persson@Sun.COM 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * The sonode represents a socket. A sonode never exist in the file system 1080Sstevel@tonic-gate * name space and can not be opened using open() - only the socket, socketpair 1090Sstevel@tonic-gate * and accept calls create sonodes. 1100Sstevel@tonic-gate * 1118348SEric.Yu@Sun.COM * The locking of sockfs uses the so_lock mutex plus the SOLOCKED and 1128348SEric.Yu@Sun.COM * SOREADLOCKED flags in so_flag. The mutex protects all the state in the 1138348SEric.Yu@Sun.COM * sonode. It is expected that the underlying transport protocol serializes 1148348SEric.Yu@Sun.COM * socket operations, so sockfs will not normally not single-thread 1158348SEric.Yu@Sun.COM * operations. However, certain sockets, including TPI based ones, can only 1168348SEric.Yu@Sun.COM * handle one control operation at a time. The SOLOCKED flag is used to 1178348SEric.Yu@Sun.COM * single-thread operations from sockfs users to prevent e.g. multiple bind() 1188348SEric.Yu@Sun.COM * calls to operate on the same sonode concurrently. The SOREADLOCKED flag is 1198348SEric.Yu@Sun.COM * used to ensure that only one thread sleeps in kstrgetmsg for a given 1208348SEric.Yu@Sun.COM * sonode. This is needed to ensure atomic operation for things like 1218348SEric.Yu@Sun.COM * MSG_WAITALL. 1220Sstevel@tonic-gate * 1238348SEric.Yu@Sun.COM * The so_fallback_rwlock is used to ensure that for sockets that can 1248348SEric.Yu@Sun.COM * fall back to TPI, the fallback is not initiated until all pending 1258348SEric.Yu@Sun.COM * operations have completed. 1260Sstevel@tonic-gate * 1270Sstevel@tonic-gate * Note that so_lock is sometimes held across calls that might go to sleep 1280Sstevel@tonic-gate * (kmem_alloc and soallocproto*). This implies that no other lock in 1290Sstevel@tonic-gate * the system should be held when calling into sockfs; from the system call 1308348SEric.Yu@Sun.COM * side or from strrput (in case of TPI based sockets). If locks are held 1318348SEric.Yu@Sun.COM * while calling into sockfs the system might hang when running low on memory. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate struct sonode { 1340Sstevel@tonic-gate struct vnode *so_vnode; /* vnode associated with this sonode */ 1350Sstevel@tonic-gate 1368348SEric.Yu@Sun.COM sonodeops_t *so_ops; /* operations vector for this sonode */ 1378348SEric.Yu@Sun.COM void *so_priv; /* sonode private data */ 1380Sstevel@tonic-gate 1398348SEric.Yu@Sun.COM krwlock_t so_fallback_rwlock; 1400Sstevel@tonic-gate kmutex_t so_lock; /* protects sonode fields */ 1418348SEric.Yu@Sun.COM 1420Sstevel@tonic-gate kcondvar_t so_state_cv; /* synchronize state changes */ 1430Sstevel@tonic-gate kcondvar_t so_want_cv; /* wait due to SOLOCKED */ 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate /* These fields are protected by so_lock */ 1460Sstevel@tonic-gate 1478348SEric.Yu@Sun.COM uint_t so_state; /* internal state flags SS_*, below */ 1488348SEric.Yu@Sun.COM uint_t so_mode; /* characteristics on socket. SM_* */ 1498348SEric.Yu@Sun.COM ushort_t so_flag; /* flags, see below */ 1508348SEric.Yu@Sun.COM int so_count; /* count of opened references */ 1510Sstevel@tonic-gate 1528348SEric.Yu@Sun.COM sock_connid_t so_proto_connid; /* protocol generation number */ 1530Sstevel@tonic-gate 1548348SEric.Yu@Sun.COM ushort_t so_error; /* error affecting connection */ 1558348SEric.Yu@Sun.COM 1568348SEric.Yu@Sun.COM struct sockparams *so_sockparams; /* vnode or socket module */ 1570Sstevel@tonic-gate /* Needed to recreate the same socket for accept */ 1580Sstevel@tonic-gate short so_family; 1590Sstevel@tonic-gate short so_type; 1600Sstevel@tonic-gate short so_protocol; 1610Sstevel@tonic-gate short so_version; /* From so_socket call */ 1628348SEric.Yu@Sun.COM 1638348SEric.Yu@Sun.COM /* Accept queue */ 1648348SEric.Yu@Sun.COM kmutex_t so_acceptq_lock; /* protects accept queue */ 1658348SEric.Yu@Sun.COM struct sonode *so_acceptq_next; /* acceptq list node */ 1668348SEric.Yu@Sun.COM struct sonode *so_acceptq_head; 1678348SEric.Yu@Sun.COM struct sonode **so_acceptq_tail; 1688348SEric.Yu@Sun.COM unsigned int so_acceptq_len; 1698348SEric.Yu@Sun.COM unsigned int so_backlog; /* Listen backlog */ 1708348SEric.Yu@Sun.COM kcondvar_t so_acceptq_cv; /* wait for new conn. */ 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* Options */ 1730Sstevel@tonic-gate short so_options; /* From socket call, see socket.h */ 1740Sstevel@tonic-gate struct linger so_linger; /* SO_LINGER value */ 1758348SEric.Yu@Sun.COM #define so_sndbuf so_proto_props.sopp_txhiwat /* SO_SNDBUF value */ 1768348SEric.Yu@Sun.COM #define so_sndlowat so_proto_props.sopp_txlowat /* tx low water mark */ 1778348SEric.Yu@Sun.COM #define so_rcvbuf so_proto_props.sopp_rxhiwat /* SO_RCVBUF value */ 1788348SEric.Yu@Sun.COM #define so_rcvlowat so_proto_props.sopp_rxlowat /* rx low water mark */ 1798348SEric.Yu@Sun.COM #define so_max_addr_len so_proto_props.sopp_maxaddrlen 1808348SEric.Yu@Sun.COM #define so_minpsz so_proto_props.sopp_minpsz 1818348SEric.Yu@Sun.COM #define so_maxpsz so_proto_props.sopp_maxpsz 1820Sstevel@tonic-gate 1838465SEric.Yu@Sun.COM int so_xpg_rcvbuf; /* SO_RCVBUF value for XPG4 socket */ 1848348SEric.Yu@Sun.COM clock_t so_sndtimeo; /* send timeout */ 1858348SEric.Yu@Sun.COM clock_t so_rcvtimeo; /* recv timeout */ 1868348SEric.Yu@Sun.COM 1870Sstevel@tonic-gate mblk_t *so_oobmsg; /* outofline oob data */ 1888348SEric.Yu@Sun.COM ssize_t so_oobmark; /* offset of the oob data */ 1898348SEric.Yu@Sun.COM 1900Sstevel@tonic-gate pid_t so_pgrp; /* pgrp for signals */ 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate cred_t *so_peercred; /* connected socket peer cred */ 1930Sstevel@tonic-gate pid_t so_cpid; /* connected socket peer cached pid */ 1940Sstevel@tonic-gate zoneid_t so_zoneid; /* opener's zoneid */ 1950Sstevel@tonic-gate 1968348SEric.Yu@Sun.COM struct pollhead so_poll_list; /* common pollhead */ 1978348SEric.Yu@Sun.COM short so_pollev; /* events that should be generated */ 1988348SEric.Yu@Sun.COM 1998348SEric.Yu@Sun.COM /* Receive */ 2008941SAnders.Persson@Sun.COM unsigned int so_rcv_queued; /* # bytes on both rcv lists */ 2018941SAnders.Persson@Sun.COM mblk_t *so_rcv_q_head; /* processing/copyout rcv queue */ 2028348SEric.Yu@Sun.COM mblk_t *so_rcv_q_last_head; 2038941SAnders.Persson@Sun.COM mblk_t *so_rcv_head; /* protocol prequeue */ 2048348SEric.Yu@Sun.COM mblk_t *so_rcv_last_head; /* last mblk in b_next chain */ 2058941SAnders.Persson@Sun.COM kcondvar_t so_rcv_cv; /* wait for data */ 2068348SEric.Yu@Sun.COM uint_t so_rcv_wanted; /* # of bytes wanted by app */ 2078348SEric.Yu@Sun.COM timeout_id_t so_rcv_timer_tid; 2088348SEric.Yu@Sun.COM 2098348SEric.Yu@Sun.COM #define so_rcv_thresh so_proto_props.sopp_rcvthresh 2108348SEric.Yu@Sun.COM #define so_rcv_timer_interval so_proto_props.sopp_rcvtimer 2110Sstevel@tonic-gate 2128941SAnders.Persson@Sun.COM kcondvar_t so_snd_cv; /* wait for snd buffers */ 2138399SRao.Shoaib@Sun.COM uint32_t 2148399SRao.Shoaib@Sun.COM so_snd_qfull: 1, /* Transmit full */ 2158399SRao.Shoaib@Sun.COM so_rcv_wakeup: 1, 2168399SRao.Shoaib@Sun.COM so_snd_wakeup: 1, 2178399SRao.Shoaib@Sun.COM so_not_str: 1, /* B_TRUE if not streams based socket */ 2188399SRao.Shoaib@Sun.COM so_pad_to_bit_31: 28; 219898Skais 2208348SEric.Yu@Sun.COM /* Communication channel with protocol */ 2218348SEric.Yu@Sun.COM sock_lower_handle_t so_proto_handle; 2228348SEric.Yu@Sun.COM sock_downcalls_t *so_downcalls; 2238348SEric.Yu@Sun.COM 2248348SEric.Yu@Sun.COM struct sock_proto_props so_proto_props; /* protocol settings */ 2258348SEric.Yu@Sun.COM boolean_t so_flowctrld; /* Flow controlled */ 2268348SEric.Yu@Sun.COM uint_t so_copyflag; /* Copy related flag */ 2278348SEric.Yu@Sun.COM kcondvar_t so_copy_cv; /* Copy cond variable */ 2288348SEric.Yu@Sun.COM 2298348SEric.Yu@Sun.COM /* kernel sockets */ 2308348SEric.Yu@Sun.COM ksocket_callbacks_t so_ksock_callbacks; 2318348SEric.Yu@Sun.COM void *so_ksock_cb_arg; /* callback argument */ 2328348SEric.Yu@Sun.COM kcondvar_t so_closing_cv; 2336707Sbrutus 2349491SAnders.Persson@Sun.COM /* != NULL for sodirect enabled socket */ 2359491SAnders.Persson@Sun.COM struct sodirect_s *so_direct; 2360Sstevel@tonic-gate }; 2370Sstevel@tonic-gate 2388348SEric.Yu@Sun.COM #define SO_HAVE_DATA(so) \ 2398427SAnders.Persson@Sun.COM /* \ 2408427SAnders.Persson@Sun.COM * For the (tid == 0) case we must check so_rcv_{q_,}head \ 2418427SAnders.Persson@Sun.COM * rather than (so_rcv_queued > 0), since the latter does not \ 2428427SAnders.Persson@Sun.COM * take into account mblks with only control/name information. \ 2438427SAnders.Persson@Sun.COM */ \ 2448427SAnders.Persson@Sun.COM ((so)->so_rcv_timer_tid == 0 && ((so)->so_rcv_head != NULL || \ 2458427SAnders.Persson@Sun.COM (so)->so_rcv_q_head != NULL)) || \ 2468348SEric.Yu@Sun.COM ((so)->so_state & SS_CANTRCVMORE) 2478348SEric.Yu@Sun.COM 2488348SEric.Yu@Sun.COM /* 2498348SEric.Yu@Sun.COM * Events handled by the protocol (in case sd_poll is set) 2508348SEric.Yu@Sun.COM */ 2518348SEric.Yu@Sun.COM #define SO_PROTO_POLLEV (POLLIN|POLLRDNORM|POLLRDBAND) 2528348SEric.Yu@Sun.COM 2538348SEric.Yu@Sun.COM 2548348SEric.Yu@Sun.COM #endif /* _KERNEL || _KMEMUSER */ 2558348SEric.Yu@Sun.COM 2560Sstevel@tonic-gate /* flags */ 2570Sstevel@tonic-gate #define SOMOD 0x0001 /* update socket modification time */ 2580Sstevel@tonic-gate #define SOACC 0x0002 /* update socket access time */ 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate #define SOLOCKED 0x0010 /* use to serialize open/closes */ 2610Sstevel@tonic-gate #define SOREADLOCKED 0x0020 /* serialize kstrgetmsg calls */ 2620Sstevel@tonic-gate #define SOWANT 0x0040 /* some process waiting on lock */ 2630Sstevel@tonic-gate #define SOCLONE 0x0080 /* child of clone driver */ 2640Sstevel@tonic-gate #define SOASYNC_UNBIND 0x0100 /* wait for ACK of async unbind */ 2650Sstevel@tonic-gate 2668399SRao.Shoaib@Sun.COM #define SOCK_IS_NONSTR(so) ((so)->so_not_str) 2678348SEric.Yu@Sun.COM 2680Sstevel@tonic-gate /* 2690Sstevel@tonic-gate * Socket state bits. 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate #define SS_ISCONNECTED 0x00000001 /* socket connected to a peer */ 2720Sstevel@tonic-gate #define SS_ISCONNECTING 0x00000002 /* in process, connecting to peer */ 2730Sstevel@tonic-gate #define SS_ISDISCONNECTING 0x00000004 /* in process of disconnecting */ 2740Sstevel@tonic-gate #define SS_CANTSENDMORE 0x00000008 /* can't send more data to peer */ 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate #define SS_CANTRCVMORE 0x00000010 /* can't receive more data */ 2770Sstevel@tonic-gate #define SS_ISBOUND 0x00000020 /* socket is bound */ 2780Sstevel@tonic-gate #define SS_NDELAY 0x00000040 /* FNDELAY non-blocking */ 2790Sstevel@tonic-gate #define SS_NONBLOCK 0x00000080 /* O_NONBLOCK non-blocking */ 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate #define SS_ASYNC 0x00000100 /* async i/o notify */ 2820Sstevel@tonic-gate #define SS_ACCEPTCONN 0x00000200 /* listen done */ 2838348SEric.Yu@Sun.COM /* unused 0x00000400 */ /* was SS_HASCONNIND */ 2840Sstevel@tonic-gate #define SS_SAVEDEOR 0x00000800 /* Saved MSG_EOR rcv side state */ 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate #define SS_RCVATMARK 0x00001000 /* at mark on input */ 2870Sstevel@tonic-gate #define SS_OOBPEND 0x00002000 /* OOB pending or present - poll */ 2880Sstevel@tonic-gate #define SS_HAVEOOBDATA 0x00004000 /* OOB data present */ 2890Sstevel@tonic-gate #define SS_HADOOBDATA 0x00008000 /* OOB data consumed */ 2908348SEric.Yu@Sun.COM #define SS_CLOSING 0x00010000 /* in process of closing */ 2910Sstevel@tonic-gate 2928348SEric.Yu@Sun.COM /* unused 0x00020000 */ /* was SS_FADDR_NOXLATE */ 2938348SEric.Yu@Sun.COM /* unused 0x00040000 */ /* was SS_HASDATA */ 2948348SEric.Yu@Sun.COM /* unused 0x00080000 */ /* was SS_DONEREAD */ 2958348SEric.Yu@Sun.COM /* unused 0x00100000 */ /* was SS_MOREDATA */ 2968348SEric.Yu@Sun.COM /* unused 0x00200000 */ /* was SS_DIRECT */ 2970Sstevel@tonic-gate 2986707Sbrutus #define SS_SODIRECT 0x00400000 /* transport supports sodirect */ 2990Sstevel@tonic-gate 3008963SAnders.Persson@Sun.COM #define SS_SENTLASTREADSIG 0x01000000 /* last rx signal has been sent */ 3018963SAnders.Persson@Sun.COM #define SS_SENTLASTWRITESIG 0x02000000 /* last tx signal has been sent */ 3028348SEric.Yu@Sun.COM 3038963SAnders.Persson@Sun.COM #define SS_FALLBACK_DRAIN 0x20000000 /* data was/is being drained */ 3048963SAnders.Persson@Sun.COM #define SS_FALLBACK_PENDING 0x40000000 /* fallback is pending */ 3058963SAnders.Persson@Sun.COM #define SS_FALLBACK_COMP 0x80000000 /* fallback has completed */ 3068348SEric.Yu@Sun.COM 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* Set of states when the socket can't be rebound */ 3090Sstevel@tonic-gate #define SS_CANTREBIND (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING|\ 3100Sstevel@tonic-gate SS_CANTSENDMORE|SS_CANTRCVMORE|SS_ACCEPTCONN) 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* 3138348SEric.Yu@Sun.COM * Sockets that can fall back to TPI must ensure that fall back is not 3148348SEric.Yu@Sun.COM * initiated while a thread is using a socket. 3158348SEric.Yu@Sun.COM */ 3168348SEric.Yu@Sun.COM #define SO_BLOCK_FALLBACK(so, fn) { \ 3178348SEric.Yu@Sun.COM ASSERT(MUTEX_NOT_HELD(&(so)->so_lock)); \ 3188348SEric.Yu@Sun.COM rw_enter(&(so)->so_fallback_rwlock, RW_READER); \ 3198348SEric.Yu@Sun.COM if ((so)->so_state & SS_FALLBACK_COMP) { \ 3208348SEric.Yu@Sun.COM rw_exit(&(so)->so_fallback_rwlock); \ 3218348SEric.Yu@Sun.COM return (fn); \ 3228348SEric.Yu@Sun.COM } \ 3238348SEric.Yu@Sun.COM } 3248348SEric.Yu@Sun.COM 3258348SEric.Yu@Sun.COM #define SO_UNBLOCK_FALLBACK(so) { \ 3268348SEric.Yu@Sun.COM rw_exit(&(so)->so_fallback_rwlock); \ 3278348SEric.Yu@Sun.COM } 3288348SEric.Yu@Sun.COM 3298348SEric.Yu@Sun.COM /* Poll events */ 3308348SEric.Yu@Sun.COM #define SO_POLLEV_IN 0x1 /* POLLIN wakeup needed */ 3318348SEric.Yu@Sun.COM #define SO_POLLEV_ALWAYS 0x2 /* wakeups */ 3328348SEric.Yu@Sun.COM 3338348SEric.Yu@Sun.COM /* 3340Sstevel@tonic-gate * Characteristics of sockets. Not changed after the socket is created. 3350Sstevel@tonic-gate */ 3360Sstevel@tonic-gate #define SM_PRIV 0x001 /* privileged for broadcast, raw... */ 3370Sstevel@tonic-gate #define SM_ATOMIC 0x002 /* atomic data transmission */ 3380Sstevel@tonic-gate #define SM_ADDR 0x004 /* addresses given with messages */ 3390Sstevel@tonic-gate #define SM_CONNREQUIRED 0x008 /* connection required by protocol */ 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate #define SM_FDPASSING 0x010 /* passes file descriptors */ 3420Sstevel@tonic-gate #define SM_EXDATA 0x020 /* Can handle T_EXDATA_REQ */ 3430Sstevel@tonic-gate #define SM_OPTDATA 0x040 /* Can handle T_OPTDATA_REQ */ 3440Sstevel@tonic-gate #define SM_BYTESTREAM 0x080 /* Byte stream - can use M_DATA */ 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate #define SM_ACCEPTOR_ID 0x100 /* so_acceptor_id is valid */ 3470Sstevel@tonic-gate 3488348SEric.Yu@Sun.COM #define SM_KERNEL 0x200 /* kernel socket */ 3498348SEric.Yu@Sun.COM 3508401SAnders.Persson@Sun.COM /* The modes below are only for non-streams sockets */ 3518348SEric.Yu@Sun.COM #define SM_ACCEPTSUPP 0x400 /* can handle accept() */ 3528401SAnders.Persson@Sun.COM #define SM_SENDFILESUPP 0x800 /* Private: proto supp sendfile */ 3538348SEric.Yu@Sun.COM 3540Sstevel@tonic-gate /* 3550Sstevel@tonic-gate * Socket versions. Used by the socket library when calling _so_socket(). 3560Sstevel@tonic-gate */ 3570Sstevel@tonic-gate #define SOV_STREAM 0 /* Not a socket - just a stream */ 3580Sstevel@tonic-gate #define SOV_DEFAULT 1 /* Select based on so_default_version */ 3590Sstevel@tonic-gate #define SOV_SOCKSTREAM 2 /* Socket plus streams operations */ 3600Sstevel@tonic-gate #define SOV_SOCKBSD 3 /* Socket with no streams operations */ 3610Sstevel@tonic-gate #define SOV_XPG4_2 4 /* Xnet socket */ 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate #if defined(_KERNEL) || defined(_KMEMUSER) 3648348SEric.Yu@Sun.COM 3650Sstevel@tonic-gate /* 3668348SEric.Yu@Sun.COM * sonode create and destroy functions. 3678348SEric.Yu@Sun.COM */ 3688348SEric.Yu@Sun.COM typedef struct sonode *(*so_create_func_t)(struct sockparams *, 3698348SEric.Yu@Sun.COM int, int, int, int, int, int *, cred_t *); 3708348SEric.Yu@Sun.COM typedef void (*so_destroy_func_t)(struct sonode *); 3718348SEric.Yu@Sun.COM 3728348SEric.Yu@Sun.COM /* STREAM device information */ 3738348SEric.Yu@Sun.COM typedef struct sdev_info { 3748348SEric.Yu@Sun.COM char *sd_devpath; 3758348SEric.Yu@Sun.COM int sd_devpathlen; /* Is 0 if sp_devpath is a static string */ 3768348SEric.Yu@Sun.COM vnode_t *sd_vnode; 3778348SEric.Yu@Sun.COM } sdev_info_t; 3788348SEric.Yu@Sun.COM 3798348SEric.Yu@Sun.COM #define SOCKMOD_VERSION 1 3808348SEric.Yu@Sun.COM /* name of the TPI pseudo socket module */ 3818348SEric.Yu@Sun.COM #define SOTPI_SMOD_NAME "socktpi" 3828348SEric.Yu@Sun.COM 3838348SEric.Yu@Sun.COM typedef struct __smod_priv_s { 3848348SEric.Yu@Sun.COM so_create_func_t smodp_sock_create_func; 3858348SEric.Yu@Sun.COM so_destroy_func_t smodp_sock_destroy_func; 3868348SEric.Yu@Sun.COM so_proto_fallback_func_t smodp_proto_fallback_func; 3878348SEric.Yu@Sun.COM } __smod_priv_t; 3888348SEric.Yu@Sun.COM 3898348SEric.Yu@Sun.COM /* 3908348SEric.Yu@Sun.COM * Socket module register information 3918348SEric.Yu@Sun.COM */ 3928348SEric.Yu@Sun.COM typedef struct smod_reg_s { 3938348SEric.Yu@Sun.COM int smod_version; 3948348SEric.Yu@Sun.COM char *smod_name; 3958348SEric.Yu@Sun.COM size_t smod_uc_version; 3968348SEric.Yu@Sun.COM size_t smod_dc_version; 3978348SEric.Yu@Sun.COM so_proto_create_func_t smod_proto_create_func; 3988348SEric.Yu@Sun.COM 3998348SEric.Yu@Sun.COM /* __smod_priv_data must be NULL */ 4008348SEric.Yu@Sun.COM __smod_priv_t *__smod_priv; 4018348SEric.Yu@Sun.COM } smod_reg_t; 4028348SEric.Yu@Sun.COM 4038348SEric.Yu@Sun.COM /* 4048348SEric.Yu@Sun.COM * Socket module information 4058348SEric.Yu@Sun.COM */ 4068348SEric.Yu@Sun.COM typedef struct smod_info { 4078348SEric.Yu@Sun.COM int smod_version; 4088348SEric.Yu@Sun.COM char *smod_name; 4098348SEric.Yu@Sun.COM uint_t smod_refcnt; /* # of entries */ 4108348SEric.Yu@Sun.COM size_t smod_uc_version; /* upcall version */ 4118348SEric.Yu@Sun.COM size_t smod_dc_version; /* down call version */ 4128348SEric.Yu@Sun.COM so_proto_create_func_t smod_proto_create_func; 4138348SEric.Yu@Sun.COM so_proto_fallback_func_t smod_proto_fallback_func; 4148348SEric.Yu@Sun.COM so_create_func_t smod_sock_create_func; 4158348SEric.Yu@Sun.COM so_destroy_func_t smod_sock_destroy_func; 4168348SEric.Yu@Sun.COM list_node_t smod_node; 4178348SEric.Yu@Sun.COM } smod_info_t; 4188348SEric.Yu@Sun.COM 4198964SAnders.Persson@Sun.COM typedef struct sockparams_stats { 4208964SAnders.Persson@Sun.COM kstat_named_t sps_nfallback; /* # of fallbacks to TPI */ 4218964SAnders.Persson@Sun.COM kstat_named_t sps_nactive; /* # of active sockets */ 4228964SAnders.Persson@Sun.COM kstat_named_t sps_ncreate; /* total # of created sockets */ 4238964SAnders.Persson@Sun.COM } sockparams_stats_t; 4248964SAnders.Persson@Sun.COM 4258348SEric.Yu@Sun.COM /* 4268348SEric.Yu@Sun.COM * sockparams 4278348SEric.Yu@Sun.COM * 4288348SEric.Yu@Sun.COM * Used for mapping family/type/protocol to module 4290Sstevel@tonic-gate */ 4300Sstevel@tonic-gate struct sockparams { 4318348SEric.Yu@Sun.COM /* 4328348SEric.Yu@Sun.COM * The family, type, protocol, sdev_info and smod_info are 4338348SEric.Yu@Sun.COM * set when the entry is created, and they will never change 4348348SEric.Yu@Sun.COM * thereafter. 4358348SEric.Yu@Sun.COM */ 4368348SEric.Yu@Sun.COM int sp_family; 4378348SEric.Yu@Sun.COM int sp_type; 4388348SEric.Yu@Sun.COM int sp_protocol; 4398348SEric.Yu@Sun.COM 4408348SEric.Yu@Sun.COM sdev_info_t sp_sdev_info; /* STREAM device */ 4418348SEric.Yu@Sun.COM char *sp_smod_name; /* socket module name */ 4428348SEric.Yu@Sun.COM smod_info_t *sp_smod_info; /* socket module */ 4438348SEric.Yu@Sun.COM 4448348SEric.Yu@Sun.COM kmutex_t sp_lock; /* lock for refcnt */ 4458348SEric.Yu@Sun.COM uint64_t sp_refcnt; /* entry reference count */ 4468964SAnders.Persson@Sun.COM sockparams_stats_t sp_stats; 4478964SAnders.Persson@Sun.COM kstat_t *sp_kstat; 4488348SEric.Yu@Sun.COM 4498348SEric.Yu@Sun.COM /* 4508348SEric.Yu@Sun.COM * The entries below are only modified while holding 4518348SEric.Yu@Sun.COM * splist_lock as a writer. 4528348SEric.Yu@Sun.COM */ 4538348SEric.Yu@Sun.COM int sp_flags; /* see below */ 4548348SEric.Yu@Sun.COM list_node_t sp_node; 4550Sstevel@tonic-gate }; 4560Sstevel@tonic-gate 4578348SEric.Yu@Sun.COM 4588348SEric.Yu@Sun.COM /* 4598348SEric.Yu@Sun.COM * sockparams flags 4608348SEric.Yu@Sun.COM */ 4618348SEric.Yu@Sun.COM #define SOCKPARAMS_EPHEMERAL 0x1 /* temp. entry, not on global list */ 4628348SEric.Yu@Sun.COM 4638348SEric.Yu@Sun.COM extern void sockparams_init(void); 4648348SEric.Yu@Sun.COM extern struct sockparams *sockparams_hold_ephemeral_bydev(int, int, int, 4658348SEric.Yu@Sun.COM const char *, int, int *); 4668348SEric.Yu@Sun.COM extern struct sockparams *sockparams_hold_ephemeral_bymod(int, int, int, 4678348SEric.Yu@Sun.COM const char *, int, int *); 4688348SEric.Yu@Sun.COM extern void sockparams_ephemeral_drop_last_ref(struct sockparams *); 4698348SEric.Yu@Sun.COM 4708348SEric.Yu@Sun.COM extern void smod_init(void); 4718348SEric.Yu@Sun.COM extern void smod_add(smod_info_t *); 4728348SEric.Yu@Sun.COM extern int smod_register(const smod_reg_t *); 4738348SEric.Yu@Sun.COM extern int smod_unregister(const char *); 4748348SEric.Yu@Sun.COM extern smod_info_t *smod_lookup_byname(const char *); 4758348SEric.Yu@Sun.COM 4768348SEric.Yu@Sun.COM #define SOCKPARAMS_HAS_DEVICE(sp) \ 4778348SEric.Yu@Sun.COM ((sp)->sp_sdev_info.sd_devpath != NULL) 4788348SEric.Yu@Sun.COM 4798348SEric.Yu@Sun.COM /* Increase the smod_info_t reference count */ 4808348SEric.Yu@Sun.COM #define SMOD_INC_REF(smodp) { \ 4818348SEric.Yu@Sun.COM ASSERT((smodp) != NULL); \ 4828348SEric.Yu@Sun.COM DTRACE_PROBE1(smodinfo__inc__ref, struct smod_info *, (smodp)); \ 4838348SEric.Yu@Sun.COM atomic_inc_uint(&(smodp)->smod_refcnt); \ 4848348SEric.Yu@Sun.COM } 4858348SEric.Yu@Sun.COM 4868348SEric.Yu@Sun.COM /* 4878348SEric.Yu@Sun.COM * Decreace the socket module entry reference count. 4888348SEric.Yu@Sun.COM * When no one mapping to the entry, we try to unload the module from the 4898348SEric.Yu@Sun.COM * kernel. If the module can't unload, just leave the module entry with 4908348SEric.Yu@Sun.COM * a zero refcnt. 4918348SEric.Yu@Sun.COM */ 4928348SEric.Yu@Sun.COM #define SMOD_DEC_REF(sp, smodp) { \ 4938348SEric.Yu@Sun.COM ASSERT((smodp) != NULL); \ 4948348SEric.Yu@Sun.COM ASSERT((smodp)->smod_refcnt != 0); \ 4958348SEric.Yu@Sun.COM atomic_dec_uint(&(smodp)->smod_refcnt); \ 4968348SEric.Yu@Sun.COM /* \ 4978348SEric.Yu@Sun.COM * No need to atomically check the return value because the \ 4988348SEric.Yu@Sun.COM * socket module framework will verify that no one is using \ 4998348SEric.Yu@Sun.COM * the module before unloading. Worst thing that can happen \ 5008348SEric.Yu@Sun.COM * here is multiple calls to mod_remove_by_name(), which is OK. \ 5018348SEric.Yu@Sun.COM */ \ 5028348SEric.Yu@Sun.COM if ((smodp)->smod_refcnt == 0) \ 5038348SEric.Yu@Sun.COM (void) mod_remove_by_name((sp)->sp_smod_name); \ 5048348SEric.Yu@Sun.COM } 5058348SEric.Yu@Sun.COM 5068348SEric.Yu@Sun.COM /* Increase the reference count */ 5078348SEric.Yu@Sun.COM #define SOCKPARAMS_INC_REF(sp) { \ 5088348SEric.Yu@Sun.COM ASSERT((sp) != NULL); \ 5098348SEric.Yu@Sun.COM DTRACE_PROBE1(sockparams__inc__ref, struct sockparams *, (sp)); \ 5108348SEric.Yu@Sun.COM mutex_enter(&(sp)->sp_lock); \ 5118348SEric.Yu@Sun.COM (sp)->sp_refcnt++; \ 5128348SEric.Yu@Sun.COM ASSERT((sp)->sp_refcnt != 0); \ 5138348SEric.Yu@Sun.COM mutex_exit(&(sp)->sp_lock); \ 5148348SEric.Yu@Sun.COM } 5158348SEric.Yu@Sun.COM 5168348SEric.Yu@Sun.COM /* 5178348SEric.Yu@Sun.COM * Decrease the reference count. 5188348SEric.Yu@Sun.COM * 5198348SEric.Yu@Sun.COM * If the sockparams is ephemeral, then the thread dropping the last ref 5208348SEric.Yu@Sun.COM * count will destroy the entry. 5218348SEric.Yu@Sun.COM */ 5228348SEric.Yu@Sun.COM #define SOCKPARAMS_DEC_REF(sp) { \ 5238348SEric.Yu@Sun.COM ASSERT((sp) != NULL); \ 5248348SEric.Yu@Sun.COM DTRACE_PROBE1(sockparams__dec__ref, struct sockparams *, (sp)); \ 5258348SEric.Yu@Sun.COM mutex_enter(&(sp)->sp_lock); \ 5268348SEric.Yu@Sun.COM ASSERT((sp)->sp_refcnt > 0); \ 5278348SEric.Yu@Sun.COM if ((sp)->sp_refcnt == 1) { \ 5288348SEric.Yu@Sun.COM if ((sp)->sp_flags & SOCKPARAMS_EPHEMERAL) { \ 5298348SEric.Yu@Sun.COM mutex_exit(&(sp)->sp_lock); \ 5308348SEric.Yu@Sun.COM sockparams_ephemeral_drop_last_ref((sp)); \ 5318348SEric.Yu@Sun.COM } else { \ 5328348SEric.Yu@Sun.COM (sp)->sp_refcnt--; \ 5338348SEric.Yu@Sun.COM if ((sp)->sp_smod_info != NULL) \ 5348348SEric.Yu@Sun.COM SMOD_DEC_REF(sp, (sp)->sp_smod_info); \ 5358348SEric.Yu@Sun.COM (sp)->sp_smod_info = NULL; \ 5368348SEric.Yu@Sun.COM mutex_exit(&(sp)->sp_lock); \ 5378348SEric.Yu@Sun.COM } \ 5388348SEric.Yu@Sun.COM } else { \ 5398348SEric.Yu@Sun.COM (sp)->sp_refcnt--; \ 5408348SEric.Yu@Sun.COM mutex_exit(&(sp)->sp_lock); \ 5418348SEric.Yu@Sun.COM } \ 5428348SEric.Yu@Sun.COM } 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate /* 5450Sstevel@tonic-gate * Used to traverse the list of AF_UNIX sockets to construct the kstat 5460Sstevel@tonic-gate * for netstat(1m). 5470Sstevel@tonic-gate */ 5480Sstevel@tonic-gate struct socklist { 5490Sstevel@tonic-gate kmutex_t sl_lock; 5500Sstevel@tonic-gate struct sonode *sl_list; 5510Sstevel@tonic-gate }; 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate extern struct socklist socklist; 5540Sstevel@tonic-gate /* 5550Sstevel@tonic-gate * ss_full_waits is the number of times the reader thread 5560Sstevel@tonic-gate * waits when the queue is full and ss_empty_waits is the number 5570Sstevel@tonic-gate * of times the consumer thread waits when the queue is empty. 5580Sstevel@tonic-gate * No locks for these as they are just indicators of whether 5590Sstevel@tonic-gate * disk or network or both is slow or fast. 5600Sstevel@tonic-gate */ 5610Sstevel@tonic-gate struct sendfile_stats { 5620Sstevel@tonic-gate uint32_t ss_file_cached; 5630Sstevel@tonic-gate uint32_t ss_file_not_cached; 5640Sstevel@tonic-gate uint32_t ss_full_waits; 5650Sstevel@tonic-gate uint32_t ss_empty_waits; 5660Sstevel@tonic-gate uint32_t ss_file_segmap; 5670Sstevel@tonic-gate }; 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate /* 5700Sstevel@tonic-gate * A single sendfile request is represented by snf_req. 5710Sstevel@tonic-gate */ 5720Sstevel@tonic-gate typedef struct snf_req { 5730Sstevel@tonic-gate struct snf_req *sr_next; 5740Sstevel@tonic-gate mblk_t *sr_mp_head; 5750Sstevel@tonic-gate mblk_t *sr_mp_tail; 5760Sstevel@tonic-gate kmutex_t sr_lock; 5770Sstevel@tonic-gate kcondvar_t sr_cv; 5780Sstevel@tonic-gate uint_t sr_qlen; 5790Sstevel@tonic-gate int sr_hiwat; 5800Sstevel@tonic-gate int sr_lowat; 5810Sstevel@tonic-gate int sr_operation; 5820Sstevel@tonic-gate struct vnode *sr_vp; 5830Sstevel@tonic-gate file_t *sr_fp; 5840Sstevel@tonic-gate ssize_t sr_maxpsz; 5850Sstevel@tonic-gate u_offset_t sr_file_off; 5860Sstevel@tonic-gate u_offset_t sr_file_size; 5870Sstevel@tonic-gate #define SR_READ_DONE 0x80000000 5880Sstevel@tonic-gate int sr_read_error; 5890Sstevel@tonic-gate int sr_write_error; 5900Sstevel@tonic-gate } snf_req_t; 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate /* A queue of sendfile requests */ 5930Sstevel@tonic-gate struct sendfile_queue { 5940Sstevel@tonic-gate snf_req_t *snfq_req_head; 5950Sstevel@tonic-gate snf_req_t *snfq_req_tail; 5960Sstevel@tonic-gate kmutex_t snfq_lock; 5970Sstevel@tonic-gate kcondvar_t snfq_cv; 5980Sstevel@tonic-gate int snfq_svc_threads; /* # of service threads */ 5990Sstevel@tonic-gate int snfq_idle_cnt; /* # of idling threads */ 6000Sstevel@tonic-gate int snfq_max_threads; 6010Sstevel@tonic-gate int snfq_req_cnt; /* Number of requests */ 6020Sstevel@tonic-gate }; 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate #define READ_OP 1 6050Sstevel@tonic-gate #define SNFQ_TIMEOUT (60 * 5 * hz) /* 5 minutes */ 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate /* Socket network operations switch */ 6080Sstevel@tonic-gate struct sonodeops { 6098348SEric.Yu@Sun.COM int (*sop_init)(struct sonode *, struct sonode *, cred_t *, 6100Sstevel@tonic-gate int); 6118348SEric.Yu@Sun.COM int (*sop_accept)(struct sonode *, int, cred_t *, struct sonode **); 6128348SEric.Yu@Sun.COM int (*sop_bind)(struct sonode *, struct sockaddr *, socklen_t, 6138348SEric.Yu@Sun.COM int, cred_t *); 6148348SEric.Yu@Sun.COM int (*sop_listen)(struct sonode *, int, cred_t *); 6150Sstevel@tonic-gate int (*sop_connect)(struct sonode *, const struct sockaddr *, 6168348SEric.Yu@Sun.COM socklen_t, int, int, cred_t *); 6170Sstevel@tonic-gate int (*sop_recvmsg)(struct sonode *, struct msghdr *, 6188348SEric.Yu@Sun.COM struct uio *, cred_t *); 6190Sstevel@tonic-gate int (*sop_sendmsg)(struct sonode *, struct msghdr *, 6208348SEric.Yu@Sun.COM struct uio *, cred_t *); 6218348SEric.Yu@Sun.COM int (*sop_sendmblk)(struct sonode *, struct msghdr *, int, 6228348SEric.Yu@Sun.COM cred_t *, mblk_t **); 6238348SEric.Yu@Sun.COM int (*sop_getpeername)(struct sonode *, struct sockaddr *, 6248348SEric.Yu@Sun.COM socklen_t *, boolean_t, cred_t *); 6258348SEric.Yu@Sun.COM int (*sop_getsockname)(struct sonode *, struct sockaddr *, 6268348SEric.Yu@Sun.COM socklen_t *, cred_t *); 6278348SEric.Yu@Sun.COM int (*sop_shutdown)(struct sonode *, int, cred_t *); 6280Sstevel@tonic-gate int (*sop_getsockopt)(struct sonode *, int, int, void *, 6298348SEric.Yu@Sun.COM socklen_t *, int, cred_t *); 6300Sstevel@tonic-gate int (*sop_setsockopt)(struct sonode *, int, int, const void *, 6318348SEric.Yu@Sun.COM socklen_t, cred_t *); 6328348SEric.Yu@Sun.COM int (*sop_ioctl)(struct sonode *, int, intptr_t, int, 6338348SEric.Yu@Sun.COM cred_t *, int32_t *); 6348348SEric.Yu@Sun.COM int (*sop_poll)(struct sonode *, short, int, short *, 6358348SEric.Yu@Sun.COM struct pollhead **); 6368348SEric.Yu@Sun.COM int (*sop_close)(struct sonode *, int, cred_t *); 6370Sstevel@tonic-gate }; 6380Sstevel@tonic-gate 6398348SEric.Yu@Sun.COM #define SOP_INIT(so, flag, cr, flags) \ 6408348SEric.Yu@Sun.COM ((so)->so_ops->sop_init((so), (flag), (cr), (flags))) 6418348SEric.Yu@Sun.COM #define SOP_ACCEPT(so, fflag, cr, nsop) \ 6428348SEric.Yu@Sun.COM ((so)->so_ops->sop_accept((so), (fflag), (cr), (nsop))) 6438348SEric.Yu@Sun.COM #define SOP_BIND(so, name, namelen, flags, cr) \ 6448348SEric.Yu@Sun.COM ((so)->so_ops->sop_bind((so), (name), (namelen), (flags), (cr))) 6458348SEric.Yu@Sun.COM #define SOP_LISTEN(so, backlog, cr) \ 6468348SEric.Yu@Sun.COM ((so)->so_ops->sop_listen((so), (backlog), (cr))) 6478348SEric.Yu@Sun.COM #define SOP_CONNECT(so, name, namelen, fflag, flags, cr) \ 6488348SEric.Yu@Sun.COM ((so)->so_ops->sop_connect((so), (name), (namelen), (fflag), (flags), \ 6498348SEric.Yu@Sun.COM (cr))) 6508348SEric.Yu@Sun.COM #define SOP_RECVMSG(so, msg, uiop, cr) \ 6518348SEric.Yu@Sun.COM ((so)->so_ops->sop_recvmsg((so), (msg), (uiop), (cr))) 6528348SEric.Yu@Sun.COM #define SOP_SENDMSG(so, msg, uiop, cr) \ 6538348SEric.Yu@Sun.COM ((so)->so_ops->sop_sendmsg((so), (msg), (uiop), (cr))) 6548348SEric.Yu@Sun.COM #define SOP_SENDMBLK(so, msg, size, cr, mpp) \ 6558348SEric.Yu@Sun.COM ((so)->so_ops->sop_sendmblk((so), (msg), (size), (cr), (mpp))) 6568348SEric.Yu@Sun.COM #define SOP_GETPEERNAME(so, addr, addrlen, accept, cr) \ 6578348SEric.Yu@Sun.COM ((so)->so_ops->sop_getpeername((so), (addr), (addrlen), (accept), (cr))) 6588348SEric.Yu@Sun.COM #define SOP_GETSOCKNAME(so, addr, addrlen, cr) \ 6598348SEric.Yu@Sun.COM ((so)->so_ops->sop_getsockname((so), (addr), (addrlen), (cr))) 6608348SEric.Yu@Sun.COM #define SOP_SHUTDOWN(so, how, cr) \ 6618348SEric.Yu@Sun.COM ((so)->so_ops->sop_shutdown((so), (how), (cr))) 6628348SEric.Yu@Sun.COM #define SOP_GETSOCKOPT(so, level, optionname, optval, optlenp, flags, cr) \ 6630Sstevel@tonic-gate ((so)->so_ops->sop_getsockopt((so), (level), (optionname), \ 6648348SEric.Yu@Sun.COM (optval), (optlenp), (flags), (cr))) 6658348SEric.Yu@Sun.COM #define SOP_SETSOCKOPT(so, level, optionname, optval, optlen, cr) \ 6660Sstevel@tonic-gate ((so)->so_ops->sop_setsockopt((so), (level), (optionname), \ 6678348SEric.Yu@Sun.COM (optval), (optlen), (cr))) 6688348SEric.Yu@Sun.COM #define SOP_IOCTL(so, cmd, arg, mode, cr, rvalp) \ 6698348SEric.Yu@Sun.COM ((so)->so_ops->sop_ioctl((so), (cmd), (arg), (mode), (cr), (rvalp))) 6708348SEric.Yu@Sun.COM #define SOP_POLL(so, events, anyyet, reventsp, phpp) \ 6718348SEric.Yu@Sun.COM ((so)->so_ops->sop_poll((so), (events), (anyyet), (reventsp), (phpp))) 6728348SEric.Yu@Sun.COM #define SOP_CLOSE(so, flag, cr) \ 6738348SEric.Yu@Sun.COM ((so)->so_ops->sop_close((so), (flag), (cr))) 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate #endif /* defined(_KERNEL) || defined(_KMEMUSER) */ 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate #ifdef _KERNEL 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate #define ISALIGNED_cmsghdr(addr) \ 6800Sstevel@tonic-gate (((uintptr_t)(addr) & (_CMSG_HDR_ALIGNMENT - 1)) == 0) 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate #define ROUNDUP_cmsglen(len) \ 6830Sstevel@tonic-gate (((len) + _CMSG_HDR_ALIGNMENT - 1) & ~(_CMSG_HDR_ALIGNMENT - 1)) 6840Sstevel@tonic-gate 6858348SEric.Yu@Sun.COM #define IS_NON_STREAM_SOCK(vp) \ 6868348SEric.Yu@Sun.COM ((vp)->v_type == VSOCK && (vp)->v_stream == NULL) 6870Sstevel@tonic-gate /* 6882712Snn35248 * Macros that operate on struct cmsghdr. 6892712Snn35248 * Used in parsing msg_control. 6902712Snn35248 * The CMSG_VALID macro does not assume that the last option buffer is padded. 6910Sstevel@tonic-gate */ 6920Sstevel@tonic-gate #define CMSG_NEXT(cmsg) \ 6930Sstevel@tonic-gate (struct cmsghdr *)((uintptr_t)(cmsg) + \ 6940Sstevel@tonic-gate ROUNDUP_cmsglen((cmsg)->cmsg_len)) 6952712Snn35248 #define CMSG_CONTENT(cmsg) (&((cmsg)[1])) 6962712Snn35248 #define CMSG_CONTENTLEN(cmsg) ((cmsg)->cmsg_len - sizeof (struct cmsghdr)) 6972712Snn35248 #define CMSG_VALID(cmsg, start, end) \ 6982712Snn35248 (ISALIGNED_cmsghdr(cmsg) && \ 6992712Snn35248 ((uintptr_t)(cmsg) >= (uintptr_t)(start)) && \ 7002712Snn35248 ((uintptr_t)(cmsg) < (uintptr_t)(end)) && \ 7012712Snn35248 ((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) && \ 7022712Snn35248 ((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end))) 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate /* 7050Sstevel@tonic-gate * Maximum size of any argument that is copied in (addresses, options, 7060Sstevel@tonic-gate * access rights). MUST be at least MAXPATHLEN + 3. 7070Sstevel@tonic-gate * BSD and SunOS 4.X limited this to MLEN or MCLBYTES. 7080Sstevel@tonic-gate */ 7090Sstevel@tonic-gate #define SO_MAXARGSIZE 8192 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate /* 7120Sstevel@tonic-gate * Convert between vnode and sonode 7130Sstevel@tonic-gate */ 7140Sstevel@tonic-gate #define VTOSO(vp) ((struct sonode *)((vp)->v_data)) 7150Sstevel@tonic-gate #define SOTOV(sp) ((sp)->so_vnode) 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate /* 7180Sstevel@tonic-gate * Internal flags for sobind() 7190Sstevel@tonic-gate */ 7200Sstevel@tonic-gate #define _SOBIND_REBIND 0x01 /* Bind to existing local address */ 7210Sstevel@tonic-gate #define _SOBIND_UNSPEC 0x02 /* Bind to unspecified address */ 7220Sstevel@tonic-gate #define _SOBIND_LOCK_HELD 0x04 /* so_excl_lock held by caller */ 7230Sstevel@tonic-gate #define _SOBIND_NOXLATE 0x08 /* No addr translation for AF_UNIX */ 7240Sstevel@tonic-gate #define _SOBIND_XPG4_2 0x10 /* xpg4.2 semantics */ 7250Sstevel@tonic-gate #define _SOBIND_SOCKBSD 0x20 /* BSD semantics */ 7260Sstevel@tonic-gate #define _SOBIND_LISTEN 0x40 /* Make into SS_ACCEPTCONN */ 7270Sstevel@tonic-gate #define _SOBIND_SOCKETPAIR 0x80 /* Internal flag for so_socketpair() */ 7280Sstevel@tonic-gate /* to enable listen with backlog = 1 */ 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate /* 7310Sstevel@tonic-gate * Internal flags for sounbind() 7320Sstevel@tonic-gate */ 7330Sstevel@tonic-gate #define _SOUNBIND_REBIND 0x01 /* Don't clear fields - will rebind */ 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate /* 7360Sstevel@tonic-gate * Internal flags for soconnect() 7370Sstevel@tonic-gate */ 7380Sstevel@tonic-gate #define _SOCONNECT_NOXLATE 0x01 /* No addr translation for AF_UNIX */ 7390Sstevel@tonic-gate #define _SOCONNECT_DID_BIND 0x02 /* Unbind when connect fails */ 7400Sstevel@tonic-gate #define _SOCONNECT_XPG4_2 0x04 /* xpg4.2 semantics */ 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate /* 7430Sstevel@tonic-gate * Internal flags for sodisconnect() 7440Sstevel@tonic-gate */ 7450Sstevel@tonic-gate #define _SODISCONNECT_LOCK_HELD 0x01 /* so_excl_lock held by caller */ 7460Sstevel@tonic-gate 7470Sstevel@tonic-gate /* 7480Sstevel@tonic-gate * Internal flags for sotpi_getsockopt(). 7490Sstevel@tonic-gate */ 7500Sstevel@tonic-gate #define _SOGETSOCKOPT_XPG4_2 0x01 /* xpg4.2 semantics */ 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate /* 7530Sstevel@tonic-gate * Internal flags for soallocproto*() 7540Sstevel@tonic-gate */ 7550Sstevel@tonic-gate #define _ALLOC_NOSLEEP 0 /* Don't sleep for memory */ 7560Sstevel@tonic-gate #define _ALLOC_INTR 1 /* Sleep until interrupt */ 7570Sstevel@tonic-gate #define _ALLOC_SLEEP 2 /* Sleep forever */ 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate /* 7600Sstevel@tonic-gate * Internal structure for handling AF_UNIX file descriptor passing 7610Sstevel@tonic-gate */ 7620Sstevel@tonic-gate struct fdbuf { 7630Sstevel@tonic-gate int fd_size; /* In bytes, for kmem_free */ 7640Sstevel@tonic-gate int fd_numfd; /* Number of elements below */ 7650Sstevel@tonic-gate char *fd_ebuf; /* Extra buffer to free */ 7660Sstevel@tonic-gate int fd_ebuflen; 7670Sstevel@tonic-gate frtn_t fd_frtn; 7680Sstevel@tonic-gate struct file *fd_fds[1]; /* One or more */ 7690Sstevel@tonic-gate }; 7700Sstevel@tonic-gate #define FDBUF_HDRSIZE (sizeof (struct fdbuf) - sizeof (struct file *)) 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate /* 7730Sstevel@tonic-gate * Variable that can be patched to set what version of socket socket() 7740Sstevel@tonic-gate * will create. 7750Sstevel@tonic-gate */ 7760Sstevel@tonic-gate extern int so_default_version; 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate #ifdef DEBUG 7790Sstevel@tonic-gate /* Turn on extra testing capabilities */ 7800Sstevel@tonic-gate #define SOCK_TEST 7810Sstevel@tonic-gate #endif /* DEBUG */ 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate #ifdef DEBUG 7840Sstevel@tonic-gate char *pr_state(uint_t, uint_t); 7850Sstevel@tonic-gate char *pr_addr(int, struct sockaddr *, t_uscalar_t); 7860Sstevel@tonic-gate int so_verify_oobstate(struct sonode *); 7870Sstevel@tonic-gate #endif /* DEBUG */ 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate /* 7900Sstevel@tonic-gate * DEBUG macros 7910Sstevel@tonic-gate */ 7927632SNick.Todd@Sun.COM #if defined(DEBUG) 7930Sstevel@tonic-gate #define SOCK_DEBUG 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate extern int sockdebug; 7960Sstevel@tonic-gate extern int sockprinterr; 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate #define eprint(args) printf args 7990Sstevel@tonic-gate #define eprintso(so, args) \ 8000Sstevel@tonic-gate { if (sockprinterr && ((so)->so_options & SO_DEBUG)) printf args; } 8010Sstevel@tonic-gate #define eprintline(error) \ 8020Sstevel@tonic-gate { \ 8030Sstevel@tonic-gate if (error != EINTR && (sockprinterr || sockdebug > 0)) \ 8040Sstevel@tonic-gate printf("socket error %d: line %d file %s\n", \ 8050Sstevel@tonic-gate (error), __LINE__, __FILE__); \ 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate #define eprintsoline(so, error) \ 8090Sstevel@tonic-gate { if (sockprinterr && ((so)->so_options & SO_DEBUG)) \ 8100Sstevel@tonic-gate printf("socket(%p) error %d: line %d file %s\n", \ 8117632SNick.Todd@Sun.COM (void *)(so), (error), __LINE__, __FILE__); \ 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate #define dprint(level, args) { if (sockdebug > (level)) printf args; } 8140Sstevel@tonic-gate #define dprintso(so, level, args) \ 8150Sstevel@tonic-gate { if (sockdebug > (level) && ((so)->so_options & SO_DEBUG)) printf args; } 8160Sstevel@tonic-gate 8177632SNick.Todd@Sun.COM #else /* define(DEBUG) */ 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate #define eprint(args) {} 8200Sstevel@tonic-gate #define eprintso(so, args) {} 8210Sstevel@tonic-gate #define eprintline(error) {} 8220Sstevel@tonic-gate #define eprintsoline(so, error) {} 8230Sstevel@tonic-gate #define dprint(level, args) {} 8240Sstevel@tonic-gate #define dprintso(so, level, args) {} 8250Sstevel@tonic-gate 8267632SNick.Todd@Sun.COM #endif /* defined(DEBUG) */ 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate extern struct vfsops sock_vfsops; 8298348SEric.Yu@Sun.COM extern struct vnodeops *socket_vnodeops; 8308348SEric.Yu@Sun.COM extern const struct fs_operation_def socket_vnodeops_template[]; 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate extern dev_t sockdev; 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate /* 8350Sstevel@tonic-gate * sockfs functions 8360Sstevel@tonic-gate */ 8370Sstevel@tonic-gate extern int sock_getmsg(vnode_t *, struct strbuf *, struct strbuf *, 8380Sstevel@tonic-gate uchar_t *, int *, int, rval_t *); 8390Sstevel@tonic-gate extern int sock_putmsg(vnode_t *, struct strbuf *, struct strbuf *, 8400Sstevel@tonic-gate uchar_t, int, int); 8418348SEric.Yu@Sun.COM extern int sogetvp(char *, vnode_t **, int); 8420Sstevel@tonic-gate extern int sockinit(int, char *); 8438348SEric.Yu@Sun.COM extern int soconfig(int, int, int, char *, int, char *); 8448348SEric.Yu@Sun.COM extern int solookup(int, int, int, struct sockparams **); 8450Sstevel@tonic-gate extern void so_lock_single(struct sonode *); 8460Sstevel@tonic-gate extern void so_unlock_single(struct sonode *, int); 8470Sstevel@tonic-gate extern int so_lock_read(struct sonode *, int); 8480Sstevel@tonic-gate extern int so_lock_read_intr(struct sonode *, int); 8490Sstevel@tonic-gate extern void so_unlock_read(struct sonode *); 8500Sstevel@tonic-gate extern void *sogetoff(mblk_t *, t_uscalar_t, t_uscalar_t, uint_t); 8510Sstevel@tonic-gate extern void so_getopt_srcaddr(void *, t_uscalar_t, 8520Sstevel@tonic-gate void **, t_uscalar_t *); 8530Sstevel@tonic-gate extern int so_getopt_unix_close(void *, t_uscalar_t); 8540Sstevel@tonic-gate extern void fdbuf_free(struct fdbuf *); 8550Sstevel@tonic-gate extern mblk_t *fdbuf_allocmsg(int, struct fdbuf *); 8560Sstevel@tonic-gate extern int fdbuf_create(void *, int, struct fdbuf **); 8570Sstevel@tonic-gate extern void so_closefds(void *, t_uscalar_t, int, int); 8580Sstevel@tonic-gate extern int so_getfdopt(void *, t_uscalar_t, int, void **, int *); 8590Sstevel@tonic-gate t_uscalar_t so_optlen(void *, t_uscalar_t, int); 8600Sstevel@tonic-gate extern void so_cmsg2opt(void *, t_uscalar_t, int, mblk_t *); 8610Sstevel@tonic-gate extern t_uscalar_t 8620Sstevel@tonic-gate so_cmsglen(mblk_t *, void *, t_uscalar_t, int); 8630Sstevel@tonic-gate extern int so_opt2cmsg(mblk_t *, void *, t_uscalar_t, int, 8640Sstevel@tonic-gate void *, t_uscalar_t); 8650Sstevel@tonic-gate extern void soisconnecting(struct sonode *); 8660Sstevel@tonic-gate extern void soisconnected(struct sonode *); 8670Sstevel@tonic-gate extern void soisdisconnected(struct sonode *, int); 8680Sstevel@tonic-gate extern void socantsendmore(struct sonode *); 8690Sstevel@tonic-gate extern void socantrcvmore(struct sonode *); 8700Sstevel@tonic-gate extern void soseterror(struct sonode *, int); 8718348SEric.Yu@Sun.COM extern int sogeterr(struct sonode *, boolean_t); 8720Sstevel@tonic-gate extern int sowaitconnected(struct sonode *, int, int); 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate extern ssize_t soreadfile(file_t *, uchar_t *, u_offset_t, int *, size_t); 8750Sstevel@tonic-gate extern void *sock_kstat_init(zoneid_t); 8760Sstevel@tonic-gate extern void sock_kstat_fini(zoneid_t, void *); 8775227Stz204579 extern struct sonode *getsonode(int, int *, file_t **); 8780Sstevel@tonic-gate /* 8795331Samw * Function wrappers (mostly around the sonode switch) for 8800Sstevel@tonic-gate * backward compatibility. 8810Sstevel@tonic-gate */ 8820Sstevel@tonic-gate extern int soaccept(struct sonode *, int, struct sonode **); 8830Sstevel@tonic-gate extern int sobind(struct sonode *, struct sockaddr *, socklen_t, 8840Sstevel@tonic-gate int, int); 8850Sstevel@tonic-gate extern int solisten(struct sonode *, int); 8860Sstevel@tonic-gate extern int soconnect(struct sonode *, const struct sockaddr *, socklen_t, 8870Sstevel@tonic-gate int, int); 8880Sstevel@tonic-gate extern int sorecvmsg(struct sonode *, struct nmsghdr *, struct uio *); 8890Sstevel@tonic-gate extern int sosendmsg(struct sonode *, struct nmsghdr *, struct uio *); 8900Sstevel@tonic-gate extern int soshutdown(struct sonode *, int); 8910Sstevel@tonic-gate extern int sogetsockopt(struct sonode *, int, int, void *, socklen_t *, 8920Sstevel@tonic-gate int); 8930Sstevel@tonic-gate extern int sosetsockopt(struct sonode *, int, int, const void *, 8940Sstevel@tonic-gate t_uscalar_t); 8950Sstevel@tonic-gate 8968348SEric.Yu@Sun.COM extern struct sonode *socreate(struct sockparams *, int, int, int, int, 8978348SEric.Yu@Sun.COM int *); 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate extern int so_copyin(const void *, void *, size_t, int); 9000Sstevel@tonic-gate extern int so_copyout(const void *, void *, size_t, int); 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate #endif 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate /* 9050Sstevel@tonic-gate * Internal structure for obtaining sonode information from the socklist. 9060Sstevel@tonic-gate * These types match those corresponding in the sonode structure. 9070Sstevel@tonic-gate * This is not a published interface, and may change at any time. 9080Sstevel@tonic-gate */ 9090Sstevel@tonic-gate struct sockinfo { 9100Sstevel@tonic-gate uint_t si_size; /* real length of this struct */ 9110Sstevel@tonic-gate short si_family; 9120Sstevel@tonic-gate short si_type; 9130Sstevel@tonic-gate ushort_t si_flag; 9140Sstevel@tonic-gate uint_t si_state; 9150Sstevel@tonic-gate uint_t si_ux_laddr_sou_magic; 9160Sstevel@tonic-gate uint_t si_ux_faddr_sou_magic; 9170Sstevel@tonic-gate t_scalar_t si_serv_type; 9180Sstevel@tonic-gate t_uscalar_t si_laddr_soa_len; 9190Sstevel@tonic-gate t_uscalar_t si_faddr_soa_len; 9200Sstevel@tonic-gate uint16_t si_laddr_family; 9210Sstevel@tonic-gate uint16_t si_faddr_family; 9220Sstevel@tonic-gate char si_laddr_sun_path[MAXPATHLEN + 1]; /* NULL terminated */ 9230Sstevel@tonic-gate char si_faddr_sun_path[MAXPATHLEN + 1]; 9248348SEric.Yu@Sun.COM boolean_t si_faddr_noxlate; 9250Sstevel@tonic-gate zoneid_t si_szoneid; 9260Sstevel@tonic-gate }; 9270Sstevel@tonic-gate 9288348SEric.Yu@Sun.COM #define SOCKMOD_PATH "socketmod" /* dir where sockmods are stored */ 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate #ifdef __cplusplus 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate #endif 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate #endif /* _SYS_SOCKETVAR_H */ 935