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