1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #ifndef _SYS_SOCKETVAR_H
41*0Sstevel@tonic-gate #define	_SYS_SOCKETVAR_H
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include <sys/types.h>
46*0Sstevel@tonic-gate #include <sys/stream.h>
47*0Sstevel@tonic-gate #include <sys/t_lock.h>
48*0Sstevel@tonic-gate #include <sys/cred.h>
49*0Sstevel@tonic-gate #include <sys/vnode.h>
50*0Sstevel@tonic-gate #include <sys/file.h>
51*0Sstevel@tonic-gate #include <sys/param.h>
52*0Sstevel@tonic-gate #include <sys/zone.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #ifdef	__cplusplus
55*0Sstevel@tonic-gate extern "C" {
56*0Sstevel@tonic-gate #endif
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * Internal representation used for addresses.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate struct soaddr {
64*0Sstevel@tonic-gate 	struct sockaddr	*soa_sa;	/* Actual address */
65*0Sstevel@tonic-gate 	t_uscalar_t	soa_len;	/* Length in bytes for kmem_free */
66*0Sstevel@tonic-gate 	t_uscalar_t	soa_maxlen;	/* Allocated length */
67*0Sstevel@tonic-gate };
68*0Sstevel@tonic-gate /* Maximum size address for transports that have ADDR_size == 1 */
69*0Sstevel@tonic-gate #define	SOA_DEFSIZE	128
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * Internal representation of the address used to represent addresses
73*0Sstevel@tonic-gate  * in the loopback transport for AF_UNIX. While the sockaddr_un is used
74*0Sstevel@tonic-gate  * as the sockfs layer address for AF_UNIX the pathnames contained in
75*0Sstevel@tonic-gate  * these addresses are not unique (due to relative pathnames) thus can not
76*0Sstevel@tonic-gate  * be used in the transport.
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  * The transport level address consists of a magic number (used to separate the
79*0Sstevel@tonic-gate  * name space for specific and implicit binds). For a specific bind
80*0Sstevel@tonic-gate  * this is followed by a "vnode *" which ensures that all specific binds
81*0Sstevel@tonic-gate  * have a unique transport level address. For implicit binds the latter
82*0Sstevel@tonic-gate  * part of the address is a byte string (of the same length as a pointer)
83*0Sstevel@tonic-gate  * that is assigned by the loopback transport.
84*0Sstevel@tonic-gate  *
85*0Sstevel@tonic-gate  * The uniqueness assumes that the loopback transport has a separate namespace
86*0Sstevel@tonic-gate  * for sockets in order to avoid name conflicts with e.g. TLI use of the
87*0Sstevel@tonic-gate  * same transport.
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate struct so_ux_addr {
90*0Sstevel@tonic-gate 	void	*soua_vp;	/* vnode pointer or assigned by tl */
91*0Sstevel@tonic-gate 	uint_t	soua_magic;	/* See below */
92*0Sstevel@tonic-gate };
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate #define	SOU_MAGIC_EXPLICIT	0x75787670	/* "uxvp" */
95*0Sstevel@tonic-gate #define	SOU_MAGIC_IMPLICIT	0x616e6f6e	/* "anon" */
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate struct sockaddr_ux {
98*0Sstevel@tonic-gate 	sa_family_t		sou_family;	/* AF_UNIX */
99*0Sstevel@tonic-gate 	struct so_ux_addr	sou_addr;
100*0Sstevel@tonic-gate };
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate typedef struct sonodeops sonodeops_t;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate /*
105*0Sstevel@tonic-gate  * The sonode represents a socket. A sonode never exist in the file system
106*0Sstevel@tonic-gate  * name space and can not be opened using open() - only the socket, socketpair
107*0Sstevel@tonic-gate  * and accept calls create sonodes.
108*0Sstevel@tonic-gate  *
109*0Sstevel@tonic-gate  * When an AF_UNIX socket is bound to a pathname the sockfs
110*0Sstevel@tonic-gate  * creates a VSOCK vnode in the underlying file system. However, the vnodeops
111*0Sstevel@tonic-gate  * etc in this VNODE remain those of the underlying file system.
112*0Sstevel@tonic-gate  * Sockfs uses the v_stream pointer in the underlying file system VSOCK node
113*0Sstevel@tonic-gate  * to find the sonode bound to the pathname. The bound pathname vnode
114*0Sstevel@tonic-gate  * is accessed through so_ux_vp.
115*0Sstevel@tonic-gate  *
116*0Sstevel@tonic-gate  * A socket always corresponds to a VCHR stream representing the transport
117*0Sstevel@tonic-gate  * provider (e.g. /dev/tcp). This information is retrieved from the kernel
118*0Sstevel@tonic-gate  * socket configuration table and entered into so_accessvp. sockfs uses
119*0Sstevel@tonic-gate  * this to perform VOP_ACCESS checks before allowing an open of the transport
120*0Sstevel@tonic-gate  * provider.
121*0Sstevel@tonic-gate  *
122*0Sstevel@tonic-gate  * The locking of sockfs uses the so_lock mutex plus the SOLOCKED
123*0Sstevel@tonic-gate  * and SOREADLOCKED flags in so_flag. The mutex protects all the state
124*0Sstevel@tonic-gate  * in the sonode. The SOLOCKED flag is used to single-thread operations from
125*0Sstevel@tonic-gate  * sockfs users to prevent e.g. multiple bind() calls to operate on the
126*0Sstevel@tonic-gate  * same sonode concurrently. The SOREADLOCKED flag is used to ensure that
127*0Sstevel@tonic-gate  * only one thread sleeps in kstrgetmsg for a given sonode. This is needed
128*0Sstevel@tonic-gate  * to ensure atomic operation for things like MSG_WAITALL.
129*0Sstevel@tonic-gate  *
130*0Sstevel@tonic-gate  * Note that so_lock is sometimes held across calls that might go to sleep
131*0Sstevel@tonic-gate  * (kmem_alloc and soallocproto*). This implies that no other lock in
132*0Sstevel@tonic-gate  * the system should be held when calling into sockfs; from the system call
133*0Sstevel@tonic-gate  * side or from strrput. If locks are held while calling into sockfs
134*0Sstevel@tonic-gate  * the system might hang when running low on memory.
135*0Sstevel@tonic-gate  */
136*0Sstevel@tonic-gate struct sonode {
137*0Sstevel@tonic-gate 	struct	vnode	*so_vnode;	/* vnode associated with this sonode */
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	sonodeops_t	*so_ops;	/* operations vector for this sonode */
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/*
142*0Sstevel@tonic-gate 	 * These fields are initialized once.
143*0Sstevel@tonic-gate 	 */
144*0Sstevel@tonic-gate 	dev_t		so_dev;		/* device the sonode represents */
145*0Sstevel@tonic-gate 	struct	vnode	*so_accessvp;	/* vnode for the /dev entry */
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	/* The locks themselves */
148*0Sstevel@tonic-gate 	kmutex_t	so_lock;	/* protects sonode fields */
149*0Sstevel@tonic-gate 	kmutex_t	so_plumb_lock;	/* serializes plumbs, and the related */
150*0Sstevel@tonic-gate 					/* fields so_version and so_pushcnt */
151*0Sstevel@tonic-gate 	kcondvar_t	so_state_cv;	/* synchronize state changes */
152*0Sstevel@tonic-gate 	kcondvar_t	so_ack_cv;	/* wait for TPI acks */
153*0Sstevel@tonic-gate 	kcondvar_t	so_connind_cv;	/* wait for T_CONN_IND */
154*0Sstevel@tonic-gate 	kcondvar_t	so_want_cv;	/* wait due to SOLOCKED */
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	/* These fields are protected by so_lock */
157*0Sstevel@tonic-gate 	uint_t	so_state;		/* internal state flags SS_*, below */
158*0Sstevel@tonic-gate 	uint_t	so_mode;		/* characteristics on socket. SM_* */
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	mblk_t	*so_ack_mp;		/* TPI ack received from below */
161*0Sstevel@tonic-gate 	mblk_t	*so_conn_ind_head;	/* b_next list of T_CONN_IND */
162*0Sstevel@tonic-gate 	mblk_t	*so_conn_ind_tail;
163*0Sstevel@tonic-gate 	mblk_t	*so_unbind_mp;		/* Preallocated T_UNBIND_REQ message */
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	ushort_t so_flag;		/* flags, see below */
166*0Sstevel@tonic-gate 	dev_t	so_fsid;		/* file system identifier */
167*0Sstevel@tonic-gate 	time_t  so_atime;		/* time of last access */
168*0Sstevel@tonic-gate 	time_t  so_mtime;		/* time of last modification */
169*0Sstevel@tonic-gate 	time_t  so_ctime;		/* time of last attributes change */
170*0Sstevel@tonic-gate 	int	so_count;		/* count of opened references */
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	/* Needed to recreate the same socket for accept */
173*0Sstevel@tonic-gate 	short	so_family;
174*0Sstevel@tonic-gate 	short	so_type;
175*0Sstevel@tonic-gate 	short	so_protocol;
176*0Sstevel@tonic-gate 	short	so_version;		/* From so_socket call */
177*0Sstevel@tonic-gate 	short	so_pushcnt;		/* Number of modules above "sockmod" */
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	/* Options */
180*0Sstevel@tonic-gate 	short	so_options;		/* From socket call, see socket.h */
181*0Sstevel@tonic-gate 	struct linger	so_linger;	/* SO_LINGER value */
182*0Sstevel@tonic-gate 	int	so_sndbuf;		/* SO_SNDBUF value */
183*0Sstevel@tonic-gate 	int	so_rcvbuf;		/* SO_RCVBUF value */
184*0Sstevel@tonic-gate 	int	so_sndlowat;		/* send low water mark */
185*0Sstevel@tonic-gate 	int	so_rcvlowat;		/* receive low water mark */
186*0Sstevel@tonic-gate #ifdef notyet
187*0Sstevel@tonic-gate 	int	so_sndtimeo;		/* Not yet implemented */
188*0Sstevel@tonic-gate 	int	so_rcvtimeo;		/* Not yet implemented */
189*0Sstevel@tonic-gate #endif /* notyet */
190*0Sstevel@tonic-gate 	ushort_t so_error;		/* error affecting connection */
191*0Sstevel@tonic-gate 	ushort_t so_delayed_error;	/* From T_uderror_ind */
192*0Sstevel@tonic-gate 	int	so_backlog;		/* Listen backlog */
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	/*
195*0Sstevel@tonic-gate 	 * The counts (so_oobcnt and so_oobsigcnt) track the number of
196*0Sstevel@tonic-gate 	 * urgent indicates that are (logically) queued on the stream head
197*0Sstevel@tonic-gate 	 * read queue. The urgent data is queued on the stream head
198*0Sstevel@tonic-gate 	 * as follows.
199*0Sstevel@tonic-gate 	 *
200*0Sstevel@tonic-gate 	 * In the normal case the SIGURG is not generated until
201*0Sstevel@tonic-gate 	 * the T_EXDATA_IND arrives at the stream head. However, transports
202*0Sstevel@tonic-gate 	 * that have an early indication that urgent data is pending
203*0Sstevel@tonic-gate 	 * (e.g. TCP receiving a "new" urgent pointer value) can send up
204*0Sstevel@tonic-gate 	 * an M_PCPROTO/SIGURG message to generate the signal early.
205*0Sstevel@tonic-gate 	 *
206*0Sstevel@tonic-gate 	 * The mark is indicated by either:
207*0Sstevel@tonic-gate 	 *  - a T_EXDATA_IND (with no M_DATA b_cont) with MSGMARK set.
208*0Sstevel@tonic-gate 	 *    When this message is consumed by sorecvmsg the socket layer
209*0Sstevel@tonic-gate 	 *    sets SS_RCVATMARK until data has been consumed past the mark.
210*0Sstevel@tonic-gate 	 *  - a message with MSGMARKNEXT set (indicating that the
211*0Sstevel@tonic-gate 	 *    first byte of the next message constitutes the mark). When
212*0Sstevel@tonic-gate 	 *    the last byte of the MSGMARKNEXT message is consumed in
213*0Sstevel@tonic-gate 	 *    the stream head the stream head sets STRATMARK. This flag
214*0Sstevel@tonic-gate 	 *    is cleared when at least one byte is read. (Note that
215*0Sstevel@tonic-gate 	 *    the MSGMARKNEXT messages can be of zero length when there
216*0Sstevel@tonic-gate 	 *    is no previous data to which the marknext can be attached.)
217*0Sstevel@tonic-gate 	 *
218*0Sstevel@tonic-gate 	 * While the T_EXDATA_IND method is the common case which is used
219*0Sstevel@tonic-gate 	 * with all TPI transports, the MSGMARKNEXT method is needed to
220*0Sstevel@tonic-gate 	 * indicate the mark when e.g. the TCP urgent byte has not been
221*0Sstevel@tonic-gate 	 * received yet but the TCP urgent pointer has made TCP generate
222*0Sstevel@tonic-gate 	 * the M_PCSIG/SIGURG.
223*0Sstevel@tonic-gate 	 *
224*0Sstevel@tonic-gate 	 * The signal (the M_PCSIG carrying the SIGURG) and the mark
225*0Sstevel@tonic-gate 	 * indication can not be delivered as a single message, since
226*0Sstevel@tonic-gate 	 * the signal should be delivered as high priority and any mark
227*0Sstevel@tonic-gate 	 * indication must flow with the data. This implies that immediately
228*0Sstevel@tonic-gate 	 * when the SIGURG has been delivered if the stream head queue is
229*0Sstevel@tonic-gate 	 * empty it is impossible to determine if this will be the position
230*0Sstevel@tonic-gate 	 * of the mark. This race condition is resolved by using MSGNOTMARKNEXT
231*0Sstevel@tonic-gate 	 * messages and the STRNOTATMARK flag in the stream head. The
232*0Sstevel@tonic-gate 	 * SIOCATMARK code calls the stream head to wait for either a
233*0Sstevel@tonic-gate 	 * non-empty queue or one of the STR*ATMARK flags being set.
234*0Sstevel@tonic-gate 	 * This implies that any transport that is sending M_PCSIG(SIGURG)
235*0Sstevel@tonic-gate 	 * should send the appropriate MSGNOTMARKNEXT message (which can be
236*0Sstevel@tonic-gate 	 * zero length) after sending an M_PCSIG to prevent SIOCATMARK
237*0Sstevel@tonic-gate 	 * from sleeping unnecessarily.
238*0Sstevel@tonic-gate 	 */
239*0Sstevel@tonic-gate 	mblk_t	*so_oobmsg;		/* outofline oob data */
240*0Sstevel@tonic-gate 	uint_t	so_oobsigcnt;		/* Number of SIGURG generated */
241*0Sstevel@tonic-gate 	uint_t	so_oobcnt;		/* Number of T_EXDATA_IND queued */
242*0Sstevel@tonic-gate 	pid_t	so_pgrp;		/* pgrp for signals */
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	/* From T_info_ack */
245*0Sstevel@tonic-gate 	t_uscalar_t	so_tsdu_size;
246*0Sstevel@tonic-gate 	t_uscalar_t	so_etsdu_size;
247*0Sstevel@tonic-gate 	t_scalar_t	so_addr_size;
248*0Sstevel@tonic-gate 	t_uscalar_t	so_opt_size;
249*0Sstevel@tonic-gate 	t_uscalar_t	so_tidu_size;
250*0Sstevel@tonic-gate 	t_scalar_t	so_serv_type;
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	/* From T_capability_ack */
253*0Sstevel@tonic-gate 	t_uscalar_t	so_acceptor_id;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	/* Internal provider information */
256*0Sstevel@tonic-gate 	struct tpi_provinfo	*so_provinfo;
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	/*
259*0Sstevel@tonic-gate 	 * The local and remote addresses have multiple purposes
260*0Sstevel@tonic-gate 	 * but one of the key reasons for their existence and careful
261*0Sstevel@tonic-gate 	 * tracking in sockfs is to support getsockname and getpeername
262*0Sstevel@tonic-gate 	 * when the transport does not handle the TI_GET*NAME ioctls
263*0Sstevel@tonic-gate 	 * and caching when it does (signalled by valid bits in so_state).
264*0Sstevel@tonic-gate 	 * When all transports support the new TPI (with T_ADDR_REQ)
265*0Sstevel@tonic-gate 	 * we can revisit this code.
266*0Sstevel@tonic-gate 	 * The other usage of so_faddr is to keep the "connected to"
267*0Sstevel@tonic-gate 	 * address for datagram sockets.
268*0Sstevel@tonic-gate 	 * Finally, for AF_UNIX both local and remote addresses are used
269*0Sstevel@tonic-gate 	 * to record the sockaddr_un since we use a separate namespace
270*0Sstevel@tonic-gate 	 * in the loopback transport.
271*0Sstevel@tonic-gate 	 */
272*0Sstevel@tonic-gate 	struct soaddr so_laddr;		/* Local address */
273*0Sstevel@tonic-gate 	struct soaddr so_faddr;		/* Peer address */
274*0Sstevel@tonic-gate #define	so_laddr_sa	so_laddr.soa_sa
275*0Sstevel@tonic-gate #define	so_faddr_sa	so_faddr.soa_sa
276*0Sstevel@tonic-gate #define	so_laddr_len	so_laddr.soa_len
277*0Sstevel@tonic-gate #define	so_faddr_len	so_faddr.soa_len
278*0Sstevel@tonic-gate #define	so_laddr_maxlen	so_laddr.soa_maxlen
279*0Sstevel@tonic-gate #define	so_faddr_maxlen	so_faddr.soa_maxlen
280*0Sstevel@tonic-gate 	mblk_t		*so_eaddr_mp;	/* for so_delayed_error */
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	/*
283*0Sstevel@tonic-gate 	 * For AF_UNIX sockets:
284*0Sstevel@tonic-gate 	 * so_ux_laddr/faddr records the internal addresses used with the
285*0Sstevel@tonic-gate 	 * transport.
286*0Sstevel@tonic-gate 	 * so_ux_vp and v_stream->sd_vnode form the cross-
287*0Sstevel@tonic-gate 	 * linkage between the underlying fs vnode corresponding to
288*0Sstevel@tonic-gate 	 * the bound sockaddr_un and the socket node.
289*0Sstevel@tonic-gate 	 */
290*0Sstevel@tonic-gate 	struct so_ux_addr so_ux_laddr;	/* laddr bound with the transport */
291*0Sstevel@tonic-gate 	struct so_ux_addr so_ux_faddr;	/* temporary peer address */
292*0Sstevel@tonic-gate 	struct vnode	*so_ux_bound_vp; /* bound AF_UNIX file system vnode */
293*0Sstevel@tonic-gate 	struct sonode	*so_next;	/* next sonode on socklist	*/
294*0Sstevel@tonic-gate 	struct sonode	*so_prev;	/* previous sonode on socklist	*/
295*0Sstevel@tonic-gate 	mblk_t	*so_discon_ind_mp;	/* T_DISCON_IND received from below */
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 					/* put here for delayed processing  */
298*0Sstevel@tonic-gate 	void		*so_priv;	/* sonode private data */
299*0Sstevel@tonic-gate 	cred_t		*so_peercred;	/* connected socket peer cred */
300*0Sstevel@tonic-gate 	pid_t		so_cpid;	/* connected socket peer cached pid */
301*0Sstevel@tonic-gate 	zoneid_t	so_zoneid;	/* opener's zoneid */
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	kmem_cache_t	*so_cache;	/* object cache of this "sonode". */
304*0Sstevel@tonic-gate 	void		*so_obj;	/* object to free */
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	/*
307*0Sstevel@tonic-gate 	 * For NL7C sockets:
308*0Sstevel@tonic-gate 	 *
309*0Sstevel@tonic-gate 	 * so_nl7c_flags	the NL7C state of URL processing.
310*0Sstevel@tonic-gate 	 *
311*0Sstevel@tonic-gate 	 * so_nl7c_rcv_mp	mblk_t chain of already received data to be
312*0Sstevel@tonic-gate 	 *			passed up to the app after NL7C gives up on
313*0Sstevel@tonic-gate 	 *			a socket.
314*0Sstevel@tonic-gate 	 *
315*0Sstevel@tonic-gate 	 * so_nl7c_rcv_rval	returned rval for last mblk_t from above.
316*0Sstevel@tonic-gate 	 *
317*0Sstevel@tonic-gate 	 * so_nl7c_uri		the URI currently being processed.
318*0Sstevel@tonic-gate 	 *
319*0Sstevel@tonic-gate 	 * so_nl7c_rtime	URI request gethrestime_sec().
320*0Sstevel@tonic-gate 	 */
321*0Sstevel@tonic-gate 	uint64_t	so_nl7c_flags;
322*0Sstevel@tonic-gate 	mblk_t		*so_nl7c_rcv_mp;
323*0Sstevel@tonic-gate 	int64_t		so_nl7c_rcv_rval;
324*0Sstevel@tonic-gate 	void		*so_nl7c_uri;
325*0Sstevel@tonic-gate 	time_t		so_nl7c_rtime;
326*0Sstevel@tonic-gate };
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate /* flags */
329*0Sstevel@tonic-gate #define	SOMOD		0x0001		/* update socket modification time */
330*0Sstevel@tonic-gate #define	SOACC		0x0002		/* update socket access time */
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate #define	SOLOCKED	0x0010		/* use to serialize open/closes */
333*0Sstevel@tonic-gate #define	SOREADLOCKED	0x0020		/* serialize kstrgetmsg calls */
334*0Sstevel@tonic-gate #define	SOWANT		0x0040		/* some process waiting on lock */
335*0Sstevel@tonic-gate #define	SOCLONE		0x0080		/* child of clone driver */
336*0Sstevel@tonic-gate #define	SOASYNC_UNBIND	0x0100		/* wait for ACK of async unbind */
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate /*
339*0Sstevel@tonic-gate  * Socket state bits.
340*0Sstevel@tonic-gate  */
341*0Sstevel@tonic-gate #define	SS_ISCONNECTED		0x00000001 /* socket connected to a peer */
342*0Sstevel@tonic-gate #define	SS_ISCONNECTING		0x00000002 /* in process, connecting to peer */
343*0Sstevel@tonic-gate #define	SS_ISDISCONNECTING	0x00000004 /* in process of disconnecting */
344*0Sstevel@tonic-gate #define	SS_CANTSENDMORE		0x00000008 /* can't send more data to peer */
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate #define	SS_CANTRCVMORE		0x00000010 /* can't receive more data */
347*0Sstevel@tonic-gate #define	SS_ISBOUND		0x00000020 /* socket is bound */
348*0Sstevel@tonic-gate #define	SS_NDELAY		0x00000040 /* FNDELAY non-blocking */
349*0Sstevel@tonic-gate #define	SS_NONBLOCK		0x00000080 /* O_NONBLOCK non-blocking */
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate #define	SS_ASYNC		0x00000100 /* async i/o notify */
352*0Sstevel@tonic-gate #define	SS_ACCEPTCONN		0x00000200 /* listen done */
353*0Sstevel@tonic-gate #define	SS_HASCONNIND		0x00000400 /* T_CONN_IND for poll */
354*0Sstevel@tonic-gate #define	SS_SAVEDEOR		0x00000800 /* Saved MSG_EOR rcv side state */
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate #define	SS_RCVATMARK		0x00001000 /* at mark on input */
357*0Sstevel@tonic-gate #define	SS_OOBPEND		0x00002000 /* OOB pending or present - poll */
358*0Sstevel@tonic-gate #define	SS_HAVEOOBDATA		0x00004000 /* OOB data present */
359*0Sstevel@tonic-gate #define	SS_HADOOBDATA		0x00008000 /* OOB data consumed */
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate #define	SS_FADDR_NOXLATE	0x00020000 /* No xlation of faddr for AF_UNIX */
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate #define	SS_HASDATA		0x00040000 /* NCAfs: data available */
364*0Sstevel@tonic-gate #define	SS_DONEREAD		0x00080000 /* NCAfs: all data read */
365*0Sstevel@tonic-gate #define	SS_MOREDATA		0x00100000 /* NCAfs: NCA has more data */
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate #define	SS_TCP_FAST_ACCEPT	0x00200000 /* Use TCP's accept fast-path */
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate #define	SS_LADDR_VALID		0x01000000	/* so_laddr valid for user */
370*0Sstevel@tonic-gate #define	SS_FADDR_VALID		0x02000000	/* so_faddr valid for user */
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate /* Set of states when the socket can't be rebound */
373*0Sstevel@tonic-gate #define	SS_CANTREBIND	(SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING|\
374*0Sstevel@tonic-gate 			    SS_CANTSENDMORE|SS_CANTRCVMORE|SS_ACCEPTCONN)
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate /*
377*0Sstevel@tonic-gate  * Characteristics of sockets. Not changed after the socket is created.
378*0Sstevel@tonic-gate  */
379*0Sstevel@tonic-gate #define	SM_PRIV			0x001	/* privileged for broadcast, raw... */
380*0Sstevel@tonic-gate #define	SM_ATOMIC		0x002	/* atomic data transmission */
381*0Sstevel@tonic-gate #define	SM_ADDR			0x004	/* addresses given with messages */
382*0Sstevel@tonic-gate #define	SM_CONNREQUIRED		0x008	/* connection required by protocol */
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate #define	SM_FDPASSING		0x010	/* passes file descriptors */
385*0Sstevel@tonic-gate #define	SM_EXDATA		0x020	/* Can handle T_EXDATA_REQ */
386*0Sstevel@tonic-gate #define	SM_OPTDATA		0x040	/* Can handle T_OPTDATA_REQ */
387*0Sstevel@tonic-gate #define	SM_BYTESTREAM		0x080	/* Byte stream - can use M_DATA */
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate #define	SM_ACCEPTOR_ID		0x100	/* so_acceptor_id is valid */
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate /*
392*0Sstevel@tonic-gate  * Socket versions. Used by the socket library when calling _so_socket().
393*0Sstevel@tonic-gate  */
394*0Sstevel@tonic-gate #define	SOV_STREAM	0	/* Not a socket - just a stream */
395*0Sstevel@tonic-gate #define	SOV_DEFAULT	1	/* Select based on so_default_version */
396*0Sstevel@tonic-gate #define	SOV_SOCKSTREAM	2	/* Socket plus streams operations */
397*0Sstevel@tonic-gate #define	SOV_SOCKBSD	3	/* Socket with no streams operations */
398*0Sstevel@tonic-gate #define	SOV_XPG4_2	4	/* Xnet socket */
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate #if defined(_KERNEL) || defined(_KMEMUSER)
401*0Sstevel@tonic-gate /*
402*0Sstevel@tonic-gate  * Used for mapping family/type/protocol to vnode.
403*0Sstevel@tonic-gate  * Defined here so that crash can use it.
404*0Sstevel@tonic-gate  */
405*0Sstevel@tonic-gate struct sockparams {
406*0Sstevel@tonic-gate 	int	sp_domain;
407*0Sstevel@tonic-gate 	int	sp_type;
408*0Sstevel@tonic-gate 	int	sp_protocol;
409*0Sstevel@tonic-gate 	char	*sp_devpath;
410*0Sstevel@tonic-gate 	int	sp_devpathlen;	/* Is 0 if sp_devpath is a static string */
411*0Sstevel@tonic-gate 	vnode_t	*sp_vnode;
412*0Sstevel@tonic-gate 	struct sockparams *sp_next;
413*0Sstevel@tonic-gate };
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate extern struct sockparams *sphead;
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate /*
418*0Sstevel@tonic-gate  * Used to traverse the list of AF_UNIX sockets to construct the kstat
419*0Sstevel@tonic-gate  * for netstat(1m).
420*0Sstevel@tonic-gate  */
421*0Sstevel@tonic-gate struct socklist {
422*0Sstevel@tonic-gate 	kmutex_t	sl_lock;
423*0Sstevel@tonic-gate 	struct sonode	*sl_list;
424*0Sstevel@tonic-gate };
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate extern struct socklist socklist;
427*0Sstevel@tonic-gate /*
428*0Sstevel@tonic-gate  * ss_full_waits is the number of times the reader thread
429*0Sstevel@tonic-gate  * waits when the queue is full and ss_empty_waits is the number
430*0Sstevel@tonic-gate  * of times the consumer thread waits when the queue is empty.
431*0Sstevel@tonic-gate  * No locks for these as they are just indicators of whether
432*0Sstevel@tonic-gate  * disk or network or both is slow or fast.
433*0Sstevel@tonic-gate  */
434*0Sstevel@tonic-gate struct sendfile_stats {
435*0Sstevel@tonic-gate 	uint32_t ss_file_cached;
436*0Sstevel@tonic-gate 	uint32_t ss_file_not_cached;
437*0Sstevel@tonic-gate 	uint32_t ss_full_waits;
438*0Sstevel@tonic-gate 	uint32_t ss_empty_waits;
439*0Sstevel@tonic-gate 	uint32_t ss_file_segmap;
440*0Sstevel@tonic-gate };
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate /*
443*0Sstevel@tonic-gate  * A single sendfile request is represented by snf_req.
444*0Sstevel@tonic-gate  */
445*0Sstevel@tonic-gate typedef struct snf_req {
446*0Sstevel@tonic-gate 	struct snf_req	*sr_next;
447*0Sstevel@tonic-gate 	mblk_t		*sr_mp_head;
448*0Sstevel@tonic-gate 	mblk_t		*sr_mp_tail;
449*0Sstevel@tonic-gate 	kmutex_t	sr_lock;
450*0Sstevel@tonic-gate 	kcondvar_t	sr_cv;
451*0Sstevel@tonic-gate 	uint_t		sr_qlen;
452*0Sstevel@tonic-gate 	int		sr_hiwat;
453*0Sstevel@tonic-gate 	int		sr_lowat;
454*0Sstevel@tonic-gate 	int		sr_operation;
455*0Sstevel@tonic-gate 	struct vnode	*sr_vp;
456*0Sstevel@tonic-gate 	file_t 		*sr_fp;
457*0Sstevel@tonic-gate 	ssize_t		sr_maxpsz;
458*0Sstevel@tonic-gate 	u_offset_t	sr_file_off;
459*0Sstevel@tonic-gate 	u_offset_t	sr_file_size;
460*0Sstevel@tonic-gate #define	SR_READ_DONE	0x80000000
461*0Sstevel@tonic-gate 	int		sr_read_error;
462*0Sstevel@tonic-gate 	int		sr_write_error;
463*0Sstevel@tonic-gate } snf_req_t;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate /* A queue of sendfile requests */
466*0Sstevel@tonic-gate struct sendfile_queue {
467*0Sstevel@tonic-gate 	snf_req_t	*snfq_req_head;
468*0Sstevel@tonic-gate 	snf_req_t	*snfq_req_tail;
469*0Sstevel@tonic-gate 	kmutex_t	snfq_lock;
470*0Sstevel@tonic-gate 	kcondvar_t	snfq_cv;
471*0Sstevel@tonic-gate 	int		snfq_svc_threads;	/* # of service threads */
472*0Sstevel@tonic-gate 	int		snfq_idle_cnt;		/* # of idling threads */
473*0Sstevel@tonic-gate 	int		snfq_max_threads;
474*0Sstevel@tonic-gate 	int		snfq_req_cnt;		/* Number of requests */
475*0Sstevel@tonic-gate };
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate #define	READ_OP			1
478*0Sstevel@tonic-gate #define	SNFQ_TIMEOUT		(60 * 5 * hz)	/* 5 minutes */
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate /* Socket network operations switch */
481*0Sstevel@tonic-gate struct sonodeops {
482*0Sstevel@tonic-gate 	int	(*sop_accept)(struct sonode *, int, struct sonode **);
483*0Sstevel@tonic-gate 	int	(*sop_bind)(struct sonode *, struct sockaddr *, socklen_t,
484*0Sstevel@tonic-gate 		    int);
485*0Sstevel@tonic-gate 	int	(*sop_listen)(struct sonode *, int);
486*0Sstevel@tonic-gate 	int	(*sop_connect)(struct sonode *, const struct sockaddr *,
487*0Sstevel@tonic-gate 		    socklen_t, int, int);
488*0Sstevel@tonic-gate 	int	(*sop_recvmsg)(struct sonode *, struct msghdr *,
489*0Sstevel@tonic-gate 		    struct uio *);
490*0Sstevel@tonic-gate 	int	(*sop_sendmsg)(struct sonode *, struct msghdr *,
491*0Sstevel@tonic-gate 		    struct uio *);
492*0Sstevel@tonic-gate 	int	(*sop_getpeername)(struct sonode *);
493*0Sstevel@tonic-gate 	int	(*sop_getsockname)(struct sonode *);
494*0Sstevel@tonic-gate 	int	(*sop_shutdown)(struct sonode *, int);
495*0Sstevel@tonic-gate 	int	(*sop_getsockopt)(struct sonode *, int, int, void *,
496*0Sstevel@tonic-gate 		    socklen_t *, int);
497*0Sstevel@tonic-gate 	int 	(*sop_setsockopt)(struct sonode *, int, int, const void *,
498*0Sstevel@tonic-gate 		    socklen_t);
499*0Sstevel@tonic-gate };
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate #define	SOP_ACCEPT(so, fflag, nsop)	\
502*0Sstevel@tonic-gate 	((so)->so_ops->sop_accept((so), (fflag), (nsop)))
503*0Sstevel@tonic-gate #define	SOP_BIND(so, name, namelen, flags)	\
504*0Sstevel@tonic-gate 	((so)->so_ops->sop_bind((so), (name), (namelen), (flags)))
505*0Sstevel@tonic-gate #define	SOP_LISTEN(so, backlog)	\
506*0Sstevel@tonic-gate 	((so)->so_ops->sop_listen((so), (backlog)))
507*0Sstevel@tonic-gate #define	SOP_CONNECT(so, name, namelen, fflag, flags)	\
508*0Sstevel@tonic-gate 	((so)->so_ops->sop_connect((so), (name), (namelen), (fflag), (flags)))
509*0Sstevel@tonic-gate #define	SOP_RECVMSG(so, msg, uiop)	\
510*0Sstevel@tonic-gate 	((so)->so_ops->sop_recvmsg((so), (msg), (uiop)))
511*0Sstevel@tonic-gate #define	SOP_SENDMSG(so, msg, uiop)	\
512*0Sstevel@tonic-gate 	((so)->so_ops->sop_sendmsg((so), (msg), (uiop)))
513*0Sstevel@tonic-gate #define	SOP_GETPEERNAME(so)	\
514*0Sstevel@tonic-gate 	((so)->so_ops->sop_getpeername((so)))
515*0Sstevel@tonic-gate #define	SOP_GETSOCKNAME(so)	\
516*0Sstevel@tonic-gate 	((so)->so_ops->sop_getsockname((so)))
517*0Sstevel@tonic-gate #define	SOP_SHUTDOWN(so, how)	\
518*0Sstevel@tonic-gate 	((so)->so_ops->sop_shutdown((so), (how)))
519*0Sstevel@tonic-gate #define	SOP_GETSOCKOPT(so, level, optionname, optval, optlenp, flags)	\
520*0Sstevel@tonic-gate 	((so)->so_ops->sop_getsockopt((so), (level), (optionname),	\
521*0Sstevel@tonic-gate 	    (optval), (optlenp), (flags)))
522*0Sstevel@tonic-gate #define	SOP_SETSOCKOPT(so, level, optionname, optval, optlen)		\
523*0Sstevel@tonic-gate 	((so)->so_ops->sop_setsockopt((so), (level), (optionname),	\
524*0Sstevel@tonic-gate 	    (optval), (optlen)))
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate #endif /* defined(_KERNEL) || defined(_KMEMUSER) */
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate #ifdef _KERNEL
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate #define	ISALIGNED_cmsghdr(addr) \
531*0Sstevel@tonic-gate 		(((uintptr_t)(addr) & (_CMSG_HDR_ALIGNMENT - 1)) == 0)
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate #define	ROUNDUP_cmsglen(len) \
534*0Sstevel@tonic-gate 	(((len) + _CMSG_HDR_ALIGNMENT - 1) & ~(_CMSG_HDR_ALIGNMENT - 1))
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate /*
537*0Sstevel@tonic-gate  * Used in parsing msg_control
538*0Sstevel@tonic-gate  */
539*0Sstevel@tonic-gate #define	CMSG_NEXT(cmsg)						\
540*0Sstevel@tonic-gate 	(struct cmsghdr *)((uintptr_t)(cmsg) +			\
541*0Sstevel@tonic-gate 	    ROUNDUP_cmsglen((cmsg)->cmsg_len))
542*0Sstevel@tonic-gate 
543*0Sstevel@tonic-gate /*
544*0Sstevel@tonic-gate  * Maximum size of any argument that is copied in (addresses, options,
545*0Sstevel@tonic-gate  * access rights). MUST be at least MAXPATHLEN + 3.
546*0Sstevel@tonic-gate  * BSD and SunOS 4.X limited this to MLEN or MCLBYTES.
547*0Sstevel@tonic-gate  */
548*0Sstevel@tonic-gate #define	SO_MAXARGSIZE	8192
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate /*
551*0Sstevel@tonic-gate  * Convert between vnode and sonode
552*0Sstevel@tonic-gate  */
553*0Sstevel@tonic-gate #define	VTOSO(vp)	((struct sonode *)((vp)->v_data))
554*0Sstevel@tonic-gate #define	SOTOV(sp)	((sp)->so_vnode)
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate /*
557*0Sstevel@tonic-gate  * Internal flags for sobind()
558*0Sstevel@tonic-gate  */
559*0Sstevel@tonic-gate #define	_SOBIND_REBIND		0x01	/* Bind to existing local address */
560*0Sstevel@tonic-gate #define	_SOBIND_UNSPEC		0x02	/* Bind to unspecified address */
561*0Sstevel@tonic-gate #define	_SOBIND_LOCK_HELD	0x04	/* so_excl_lock held by caller */
562*0Sstevel@tonic-gate #define	_SOBIND_NOXLATE		0x08	/* No addr translation for AF_UNIX */
563*0Sstevel@tonic-gate #define	_SOBIND_XPG4_2		0x10	/* xpg4.2 semantics */
564*0Sstevel@tonic-gate #define	_SOBIND_SOCKBSD		0x20	/* BSD semantics */
565*0Sstevel@tonic-gate #define	_SOBIND_LISTEN		0x40	/* Make into SS_ACCEPTCONN */
566*0Sstevel@tonic-gate #define	_SOBIND_SOCKETPAIR	0x80	/* Internal flag for so_socketpair() */
567*0Sstevel@tonic-gate 					/* to enable listen with backlog = 1 */
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate /*
570*0Sstevel@tonic-gate  * Internal flags for sounbind()
571*0Sstevel@tonic-gate  */
572*0Sstevel@tonic-gate #define	_SOUNBIND_REBIND	0x01	/* Don't clear fields - will rebind */
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate /*
575*0Sstevel@tonic-gate  * Internal flags for soconnect()
576*0Sstevel@tonic-gate  */
577*0Sstevel@tonic-gate #define	_SOCONNECT_NOXLATE	0x01	/* No addr translation for AF_UNIX */
578*0Sstevel@tonic-gate #define	_SOCONNECT_DID_BIND	0x02	/* Unbind when connect fails */
579*0Sstevel@tonic-gate #define	_SOCONNECT_XPG4_2	0x04	/* xpg4.2 semantics */
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate /*
582*0Sstevel@tonic-gate  * Internal flags for sodisconnect()
583*0Sstevel@tonic-gate  */
584*0Sstevel@tonic-gate #define	_SODISCONNECT_LOCK_HELD	0x01	/* so_excl_lock held by caller */
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate /*
587*0Sstevel@tonic-gate  * Internal flags for sotpi_getsockopt().
588*0Sstevel@tonic-gate  */
589*0Sstevel@tonic-gate #define	_SOGETSOCKOPT_XPG4_2	0x01	/* xpg4.2 semantics */
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate /*
592*0Sstevel@tonic-gate  * Internal flags for soallocproto*()
593*0Sstevel@tonic-gate  */
594*0Sstevel@tonic-gate #define	_ALLOC_NOSLEEP		0	/* Don't sleep for memory */
595*0Sstevel@tonic-gate #define	_ALLOC_INTR		1	/* Sleep until interrupt */
596*0Sstevel@tonic-gate #define	_ALLOC_SLEEP		2	/* Sleep forever */
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate /*
599*0Sstevel@tonic-gate  * Internal structure for handling AF_UNIX file descriptor passing
600*0Sstevel@tonic-gate  */
601*0Sstevel@tonic-gate struct fdbuf {
602*0Sstevel@tonic-gate 	int		fd_size;	/* In bytes, for kmem_free */
603*0Sstevel@tonic-gate 	int		fd_numfd;	/* Number of elements below */
604*0Sstevel@tonic-gate 	char		*fd_ebuf;	/* Extra buffer to free  */
605*0Sstevel@tonic-gate 	int		fd_ebuflen;
606*0Sstevel@tonic-gate 	frtn_t		fd_frtn;
607*0Sstevel@tonic-gate 	struct file	*fd_fds[1];	/* One or more */
608*0Sstevel@tonic-gate };
609*0Sstevel@tonic-gate #define	FDBUF_HDRSIZE	(sizeof (struct fdbuf) - sizeof (struct file *))
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate /*
612*0Sstevel@tonic-gate  * Variable that can be patched to set what version of socket socket()
613*0Sstevel@tonic-gate  * will create.
614*0Sstevel@tonic-gate  */
615*0Sstevel@tonic-gate extern int so_default_version;
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate #ifdef DEBUG
618*0Sstevel@tonic-gate /* Turn on extra testing capabilities */
619*0Sstevel@tonic-gate #define	SOCK_TEST
620*0Sstevel@tonic-gate #endif /* DEBUG */
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate #ifdef DEBUG
623*0Sstevel@tonic-gate char	*pr_state(uint_t, uint_t);
624*0Sstevel@tonic-gate char	*pr_addr(int, struct sockaddr *, t_uscalar_t);
625*0Sstevel@tonic-gate int	so_verify_oobstate(struct sonode *);
626*0Sstevel@tonic-gate #endif /* DEBUG */
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate /*
629*0Sstevel@tonic-gate  * DEBUG macros
630*0Sstevel@tonic-gate  */
631*0Sstevel@tonic-gate #if defined(DEBUG) && !defined(__lint)
632*0Sstevel@tonic-gate #define	SOCK_DEBUG
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate extern int sockdebug;
635*0Sstevel@tonic-gate extern int sockprinterr;
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate #define	eprint(args)	printf args
638*0Sstevel@tonic-gate #define	eprintso(so, args) \
639*0Sstevel@tonic-gate { if (sockprinterr && ((so)->so_options & SO_DEBUG)) printf args; }
640*0Sstevel@tonic-gate #define	eprintline(error)					\
641*0Sstevel@tonic-gate {								\
642*0Sstevel@tonic-gate 	if (error != EINTR && (sockprinterr || sockdebug > 0))	\
643*0Sstevel@tonic-gate 		printf("socket error %d: line %d file %s\n",	\
644*0Sstevel@tonic-gate 			(error), __LINE__, __FILE__);		\
645*0Sstevel@tonic-gate }
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate #define	eprintsoline(so, error)					\
648*0Sstevel@tonic-gate { if (sockprinterr && ((so)->so_options & SO_DEBUG))		\
649*0Sstevel@tonic-gate 	printf("socket(%p) error %d: line %d file %s\n",	\
650*0Sstevel@tonic-gate 		(so), (error), __LINE__, __FILE__);		\
651*0Sstevel@tonic-gate }
652*0Sstevel@tonic-gate #define	dprint(level, args)	{ if (sockdebug > (level)) printf args; }
653*0Sstevel@tonic-gate #define	dprintso(so, level, args) \
654*0Sstevel@tonic-gate { if (sockdebug > (level) && ((so)->so_options & SO_DEBUG)) printf args; }
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate #else /* define(DEBUG) && !defined(__lint) */
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate #define	eprint(args)		{}
659*0Sstevel@tonic-gate #define	eprintso(so, args)	{}
660*0Sstevel@tonic-gate #define	eprintline(error)	{}
661*0Sstevel@tonic-gate #define	eprintsoline(so, error)	{}
662*0Sstevel@tonic-gate #define	dprint(level, args)	{}
663*0Sstevel@tonic-gate #define	dprintso(so, level, args) {}
664*0Sstevel@tonic-gate #ifdef DEBUG
665*0Sstevel@tonic-gate #undef DEBUG
666*0Sstevel@tonic-gate #endif
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate #endif /* defined(DEBUG) && !defined(__lint) */
669*0Sstevel@tonic-gate 
670*0Sstevel@tonic-gate extern struct vfsops			sock_vfsops;
671*0Sstevel@tonic-gate extern struct vnodeops			*socktpi_vnodeops;
672*0Sstevel@tonic-gate extern const struct fs_operation_def	socktpi_vnodeops_template[];
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate extern sonodeops_t			sotpi_sonodeops;
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate extern dev_t				sockdev;
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate /* NCAfs symbols */
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate extern int	socknca_read(struct vnode *, struct uio *, int, struct cred *,
681*0Sstevel@tonic-gate 		    struct caller_context *);
682*0Sstevel@tonic-gate extern int	socknca_write(struct vnode *, struct uio *, int, struct cred *,
683*0Sstevel@tonic-gate 		    struct caller_context *);
684*0Sstevel@tonic-gate extern int	socknca_ioctl(struct vnode *, int, intptr_t, int,
685*0Sstevel@tonic-gate 		    struct cred *, int *);
686*0Sstevel@tonic-gate extern int	nca_poll(struct vnode *, short, int, short *,
687*0Sstevel@tonic-gate 		    struct pollhead **);
688*0Sstevel@tonic-gate extern int	socknca_close(struct vnode *, int, int, offset_t,
689*0Sstevel@tonic-gate 		    struct cred *);
690*0Sstevel@tonic-gate extern void	socknca_inactive(struct vnode *, struct cred *);
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate extern const struct fs_operation_def	socknca_vnodeops_template[];
693*0Sstevel@tonic-gate extern struct vnodeops			*socknca_vnodeops;
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate extern void		sonca_init(void);
696*0Sstevel@tonic-gate extern struct sonode	*sonca_create(vnode_t *, int, int, int, int,
697*0Sstevel@tonic-gate 			    struct sonode *, int *);
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate /*
700*0Sstevel@tonic-gate  * sockfs functions
701*0Sstevel@tonic-gate  */
702*0Sstevel@tonic-gate extern int	sock_getmsg(vnode_t *, struct strbuf *, struct strbuf *,
703*0Sstevel@tonic-gate 			uchar_t *, int *, int, rval_t *);
704*0Sstevel@tonic-gate extern int	sock_putmsg(vnode_t *, struct strbuf *, struct strbuf *,
705*0Sstevel@tonic-gate 			uchar_t, int, int);
706*0Sstevel@tonic-gate struct sonode	*sotpi_create(vnode_t *, int, int, int, int, struct sonode *,
707*0Sstevel@tonic-gate 			int *);
708*0Sstevel@tonic-gate extern int	socktpi_open(struct vnode **, int, struct cred *);
709*0Sstevel@tonic-gate extern int	so_sock2stream(struct sonode *);
710*0Sstevel@tonic-gate extern void	so_stream2sock(struct sonode *);
711*0Sstevel@tonic-gate extern int	sockinit(int, char *);
712*0Sstevel@tonic-gate extern struct vnode
713*0Sstevel@tonic-gate 		*makesockvp(struct vnode *, int, int, int);
714*0Sstevel@tonic-gate extern void	sockfree(struct sonode *);
715*0Sstevel@tonic-gate extern void	so_update_attrs(struct sonode *, int);
716*0Sstevel@tonic-gate extern int	soconfig(int, int, int,	char *, int);
717*0Sstevel@tonic-gate extern struct vnode
718*0Sstevel@tonic-gate 		*solookup(int, int, int, char *, int *);
719*0Sstevel@tonic-gate extern void	so_lock_single(struct sonode *);
720*0Sstevel@tonic-gate extern void	so_unlock_single(struct sonode *, int);
721*0Sstevel@tonic-gate extern int	so_lock_read(struct sonode *, int);
722*0Sstevel@tonic-gate extern int	so_lock_read_intr(struct sonode *, int);
723*0Sstevel@tonic-gate extern void	so_unlock_read(struct sonode *);
724*0Sstevel@tonic-gate extern void	*sogetoff(mblk_t *, t_uscalar_t, t_uscalar_t, uint_t);
725*0Sstevel@tonic-gate extern void	so_getopt_srcaddr(void *, t_uscalar_t,
726*0Sstevel@tonic-gate 			void **, t_uscalar_t *);
727*0Sstevel@tonic-gate extern int	so_getopt_unix_close(void *, t_uscalar_t);
728*0Sstevel@tonic-gate extern int	so_addr_verify(struct sonode *, const struct sockaddr *,
729*0Sstevel@tonic-gate 			socklen_t);
730*0Sstevel@tonic-gate extern int	so_ux_addr_xlate(struct sonode *, struct sockaddr *,
731*0Sstevel@tonic-gate 			socklen_t, int, void **, socklen_t *);
732*0Sstevel@tonic-gate extern void	fdbuf_free(struct fdbuf *);
733*0Sstevel@tonic-gate extern mblk_t	*fdbuf_allocmsg(int, struct fdbuf *);
734*0Sstevel@tonic-gate extern int	fdbuf_create(void *, int, struct fdbuf **);
735*0Sstevel@tonic-gate extern void	so_closefds(void *, t_uscalar_t, int, int);
736*0Sstevel@tonic-gate extern int	so_getfdopt(void *, t_uscalar_t, int, void **, int *);
737*0Sstevel@tonic-gate t_uscalar_t	so_optlen(void *, t_uscalar_t, int);
738*0Sstevel@tonic-gate extern void	so_cmsg2opt(void *, t_uscalar_t, int, mblk_t *);
739*0Sstevel@tonic-gate extern t_uscalar_t
740*0Sstevel@tonic-gate 		so_cmsglen(mblk_t *, void *, t_uscalar_t, int);
741*0Sstevel@tonic-gate extern int	so_opt2cmsg(mblk_t *, void *, t_uscalar_t, int,
742*0Sstevel@tonic-gate 			void *, t_uscalar_t);
743*0Sstevel@tonic-gate extern void	soisconnecting(struct sonode *);
744*0Sstevel@tonic-gate extern void	soisconnected(struct sonode *);
745*0Sstevel@tonic-gate extern void	soisdisconnected(struct sonode *, int);
746*0Sstevel@tonic-gate extern void	socantsendmore(struct sonode *);
747*0Sstevel@tonic-gate extern void	socantrcvmore(struct sonode *);
748*0Sstevel@tonic-gate extern void	soseterror(struct sonode *, int);
749*0Sstevel@tonic-gate extern int	sogeterr(struct sonode *);
750*0Sstevel@tonic-gate extern int	sogetrderr(vnode_t *, int, int *);
751*0Sstevel@tonic-gate extern int	sogetwrerr(vnode_t *, int, int *);
752*0Sstevel@tonic-gate extern void	so_unix_close(struct sonode *);
753*0Sstevel@tonic-gate extern mblk_t	*soallocproto(size_t, int);
754*0Sstevel@tonic-gate extern mblk_t	*soallocproto1(const void *, ssize_t, ssize_t, int);
755*0Sstevel@tonic-gate extern void	soappendmsg(mblk_t *, const void *, ssize_t);
756*0Sstevel@tonic-gate extern mblk_t	*soallocproto2(const void *, ssize_t, const void *, ssize_t,
757*0Sstevel@tonic-gate 			ssize_t, int);
758*0Sstevel@tonic-gate extern mblk_t	*soallocproto3(const void *, ssize_t, const void *, ssize_t,
759*0Sstevel@tonic-gate 			const void *, ssize_t, ssize_t, int);
760*0Sstevel@tonic-gate extern int	sowaitprim(struct sonode *, t_scalar_t, t_scalar_t,
761*0Sstevel@tonic-gate 			t_uscalar_t, mblk_t **, clock_t);
762*0Sstevel@tonic-gate extern int	sowaitokack(struct sonode *, t_scalar_t);
763*0Sstevel@tonic-gate extern int	sowaitack(struct sonode *, mblk_t **, clock_t);
764*0Sstevel@tonic-gate extern void	soqueueack(struct sonode *, mblk_t *);
765*0Sstevel@tonic-gate extern int	sowaitconnind(struct sonode *, int, mblk_t **);
766*0Sstevel@tonic-gate extern void	soqueueconnind(struct sonode *, mblk_t *);
767*0Sstevel@tonic-gate extern int	soflushconnind(struct sonode *, t_scalar_t);
768*0Sstevel@tonic-gate extern void	so_drain_discon_ind(struct sonode *);
769*0Sstevel@tonic-gate extern void	so_flush_discon_ind(struct sonode *);
770*0Sstevel@tonic-gate extern int	sowaitconnected(struct sonode *, int, int);
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate extern int	sosend_dgram(struct sonode *, struct sockaddr *,
773*0Sstevel@tonic-gate 			socklen_t, struct uio *, int);
774*0Sstevel@tonic-gate extern int	sosend_svc(struct sonode *, struct uio *, t_scalar_t, int, int);
775*0Sstevel@tonic-gate extern void	so_installhooks(struct sonode *);
776*0Sstevel@tonic-gate extern int	so_strinit(struct sonode *, struct sonode *);
777*0Sstevel@tonic-gate extern int	sotpi_recvmsg(struct sonode *, struct nmsghdr *,
778*0Sstevel@tonic-gate 		    struct uio *);
779*0Sstevel@tonic-gate extern int	sotpi_getpeername(struct sonode *);
780*0Sstevel@tonic-gate extern int	sotpi_getsockopt(struct sonode *, int, int, void *,
781*0Sstevel@tonic-gate 		    socklen_t *, int);
782*0Sstevel@tonic-gate extern int	sotpi_setsockopt(struct sonode *, int, int, const void *,
783*0Sstevel@tonic-gate 		    socklen_t);
784*0Sstevel@tonic-gate extern int	socktpi_ioctl(struct vnode *, int, intptr_t, int,
785*0Sstevel@tonic-gate 		    struct cred *, int *);
786*0Sstevel@tonic-gate extern int	sodisconnect(struct sonode *, t_scalar_t, int);
787*0Sstevel@tonic-gate extern ssize_t	soreadfile(file_t *, uchar_t *, u_offset_t, int *, size_t);
788*0Sstevel@tonic-gate extern int	so_set_asyncsigs(vnode_t *, pid_t, int, int, cred_t *);
789*0Sstevel@tonic-gate extern int	so_set_events(struct sonode *, vnode_t *, cred_t *);
790*0Sstevel@tonic-gate extern int	so_flip_async(struct sonode *, vnode_t *, int, cred_t *);
791*0Sstevel@tonic-gate extern int	so_set_siggrp(struct sonode *, vnode_t *, pid_t, int, cred_t *);
792*0Sstevel@tonic-gate extern void	*sock_kstat_init(zoneid_t);
793*0Sstevel@tonic-gate extern void	sock_kstat_fini(zoneid_t, void *);
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate /*
796*0Sstevel@tonic-gate  * Function wrappers (mostly arround the sonode switch) for
797*0Sstevel@tonic-gate  * backward compatibility.
798*0Sstevel@tonic-gate  */
799*0Sstevel@tonic-gate extern int	soaccept(struct sonode *, int, struct sonode **);
800*0Sstevel@tonic-gate extern int	sobind(struct sonode *, struct sockaddr *, socklen_t,
801*0Sstevel@tonic-gate 		    int, int);
802*0Sstevel@tonic-gate extern int	solisten(struct sonode *, int);
803*0Sstevel@tonic-gate extern int	soconnect(struct sonode *, const struct sockaddr *, socklen_t,
804*0Sstevel@tonic-gate 		    int, int);
805*0Sstevel@tonic-gate extern int	sorecvmsg(struct sonode *, struct nmsghdr *, struct uio *);
806*0Sstevel@tonic-gate extern int	sosendmsg(struct sonode *, struct nmsghdr *, struct uio *);
807*0Sstevel@tonic-gate extern int	sogetpeername(struct sonode *);
808*0Sstevel@tonic-gate extern int	sogetsockname(struct sonode *);
809*0Sstevel@tonic-gate extern int	soshutdown(struct sonode *, int);
810*0Sstevel@tonic-gate extern int	sogetsockopt(struct sonode *, int, int, void *, socklen_t *,
811*0Sstevel@tonic-gate 		    int);
812*0Sstevel@tonic-gate extern int	sosetsockopt(struct sonode *, int, int, const void *,
813*0Sstevel@tonic-gate 		    t_uscalar_t);
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate extern struct sonode	*socreate(vnode_t *, int, int, int, int,
816*0Sstevel@tonic-gate 			    struct sonode *, int *);
817*0Sstevel@tonic-gate 
818*0Sstevel@tonic-gate extern int	so_copyin(const void *, void *, size_t, int);
819*0Sstevel@tonic-gate extern int	so_copyout(const void *, void *, size_t, int);
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate extern int	socktpi_access(struct vnode *, int, int, struct cred *);
822*0Sstevel@tonic-gate extern int	socktpi_fid(struct vnode *, struct fid *);
823*0Sstevel@tonic-gate extern int	socktpi_fsync(struct vnode *, int, struct cred *);
824*0Sstevel@tonic-gate extern int	socktpi_getattr(struct vnode *, struct vattr *, int,
825*0Sstevel@tonic-gate 		    struct cred *);
826*0Sstevel@tonic-gate extern int	socktpi_seek(struct vnode *, offset_t, offset_t *);
827*0Sstevel@tonic-gate extern int	socktpi_setattr(struct vnode *, struct vattr *, int,
828*0Sstevel@tonic-gate 		    struct cred *, caller_context_t *);
829*0Sstevel@tonic-gate extern int	socktpi_setfl(vnode_t *, int, int, cred_t *);
830*0Sstevel@tonic-gate 
831*0Sstevel@tonic-gate /* SCTP sockfs */
832*0Sstevel@tonic-gate extern struct sonode	*sosctp_create(vnode_t *, int, int, int, int,
833*0Sstevel@tonic-gate 			    struct sonode *, int *);
834*0Sstevel@tonic-gate extern int sosctp_init(void);
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate #endif
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate /*
839*0Sstevel@tonic-gate  * Internal structure for obtaining sonode information from the socklist.
840*0Sstevel@tonic-gate  * These types match those corresponding in the sonode structure.
841*0Sstevel@tonic-gate  * This is not a published interface, and may change at any time.
842*0Sstevel@tonic-gate  */
843*0Sstevel@tonic-gate struct sockinfo {
844*0Sstevel@tonic-gate 	uint_t		si_size;		/* real length of this struct */
845*0Sstevel@tonic-gate 	short		si_family;
846*0Sstevel@tonic-gate 	short		si_type;
847*0Sstevel@tonic-gate 	ushort_t	si_flag;
848*0Sstevel@tonic-gate 	uint_t		si_state;
849*0Sstevel@tonic-gate 	uint_t		si_ux_laddr_sou_magic;
850*0Sstevel@tonic-gate 	uint_t		si_ux_faddr_sou_magic;
851*0Sstevel@tonic-gate 	t_scalar_t	si_serv_type;
852*0Sstevel@tonic-gate 	t_uscalar_t	si_laddr_soa_len;
853*0Sstevel@tonic-gate 	t_uscalar_t	si_faddr_soa_len;
854*0Sstevel@tonic-gate 	uint16_t	si_laddr_family;
855*0Sstevel@tonic-gate 	uint16_t	si_faddr_family;
856*0Sstevel@tonic-gate 	char		si_laddr_sun_path[MAXPATHLEN + 1]; /* NULL terminated */
857*0Sstevel@tonic-gate 	char		si_faddr_sun_path[MAXPATHLEN + 1];
858*0Sstevel@tonic-gate 	zoneid_t	si_szoneid;
859*0Sstevel@tonic-gate };
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate #ifdef	__cplusplus
863*0Sstevel@tonic-gate }
864*0Sstevel@tonic-gate #endif
865*0Sstevel@tonic-gate 
866*0Sstevel@tonic-gate #endif	/* _SYS_SOCKETVAR_H */
867