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