xref: /illumos-gate/usr/src/uts/common/sys/socketvar.h (revision 2e0fe3efe5f9d579d4e44b3532d8e342c68b40ca)
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 (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #ifndef _SYS_SOCKETVAR_H
40 #define	_SYS_SOCKETVAR_H
41 
42 #include <sys/types.h>
43 #include <sys/stream.h>
44 #include <sys/t_lock.h>
45 #include <sys/cred.h>
46 #include <sys/vnode.h>
47 #include <sys/file.h>
48 #include <sys/param.h>
49 #include <sys/zone.h>
50 #include <sys/sdt.h>
51 #include <sys/modctl.h>
52 #include <sys/atomic.h>
53 #include <sys/socket.h>
54 #include <sys/ksocket.h>
55 #include <sys/kstat.h>
56 
57 #ifdef _KERNEL
58 #include <sys/vfs_opreg.h>
59 #endif
60 
61 #ifdef	__cplusplus
62 extern "C" {
63 #endif
64 
65 /*
66  * Internal representation of the address used to represent addresses
67  * in the loopback transport for AF_UNIX. While the sockaddr_un is used
68  * as the sockfs layer address for AF_UNIX the pathnames contained in
69  * these addresses are not unique (due to relative pathnames) thus can not
70  * be used in the transport.
71  *
72  * The transport level address consists of a magic number (used to separate the
73  * name space for specific and implicit binds). For a specific bind
74  * this is followed by a "vnode *" which ensures that all specific binds
75  * have a unique transport level address. For implicit binds the latter
76  * part of the address is a byte string (of the same length as a pointer)
77  * that is assigned by the loopback transport.
78  *
79  * The uniqueness assumes that the loopback transport has a separate namespace
80  * for sockets in order to avoid name conflicts with e.g. TLI use of the
81  * same transport.
82  */
83 struct so_ux_addr {
84 	void	*soua_vp;	/* vnode pointer or assigned by tl */
85 	uint_t	soua_magic;	/* See below */
86 };
87 
88 #define	SOU_MAGIC_EXPLICIT	0x75787670	/* "uxvp" */
89 #define	SOU_MAGIC_IMPLICIT	0x616e6f6e	/* "anon" */
90 
91 struct sockaddr_ux {
92 	sa_family_t		sou_family;	/* AF_UNIX */
93 	struct so_ux_addr	sou_addr;
94 };
95 
96 #if defined(_KERNEL) || defined(_KMEMUSER)
97 
98 #include <sys/socket_proto.h>
99 
100 typedef struct sonodeops sonodeops_t;
101 typedef struct sonode sonode_t;
102 
103 struct sodirect_s;
104 
105 /*
106  * The sonode represents a socket. A sonode never exist in the file system
107  * name space and can not be opened using open() - only the socket, socketpair
108  * and accept calls create sonodes.
109  *
110  * The locking of sockfs uses the so_lock mutex plus the SOLOCKED and
111  * SOREADLOCKED flags in so_flag. The mutex protects all the state in the
112  * sonode. It is expected that the underlying transport protocol serializes
113  * socket operations, so sockfs will not normally not single-thread
114  * operations. However, certain sockets, including TPI based ones, can only
115  * handle one control operation at a time. The SOLOCKED flag is used to
116  * single-thread operations from sockfs users to prevent e.g. multiple bind()
117  * calls to operate on the same sonode concurrently. The SOREADLOCKED flag is
118  * used to ensure that only one thread sleeps in kstrgetmsg for a given
119  * sonode. This is needed to ensure atomic operation for things like
120  * MSG_WAITALL.
121  *
122  * The so_fallback_rwlock is used to ensure that for sockets that can
123  * fall back to TPI, the fallback is not initiated until all pending
124  * operations have completed.
125  *
126  * Note that so_lock is sometimes held across calls that might go to sleep
127  * (kmem_alloc and soallocproto*). This implies that no other lock in
128  * the system should be held when calling into sockfs; from the system call
129  * side or from strrput (in case of TPI based sockets). If locks are held
130  * while calling into sockfs the system might hang when running low on memory.
131  */
132 struct sonode {
133 	struct	vnode	*so_vnode;	/* vnode associated with this sonode */
134 
135 	sonodeops_t 	*so_ops;	/* operations vector for this sonode */
136 	void		*so_priv;	/* sonode private data */
137 
138 	krwlock_t	so_fallback_rwlock;
139 	kmutex_t	so_lock;	/* protects sonode fields */
140 
141 	kcondvar_t	so_state_cv;	/* synchronize state changes */
142 	kcondvar_t	so_single_cv;	/* wait due to SOLOCKED */
143 	kcondvar_t	so_read_cv;	/* wait due to SOREADLOCKED */
144 
145 	/* These fields are protected by so_lock */
146 
147 	uint_t		so_state;	/* internal state flags SS_*, below */
148 	uint_t		so_mode;	/* characteristics on socket. SM_* */
149 	ushort_t 	so_flag;	/* flags, see below */
150 	int		so_count;	/* count of opened references */
151 
152 	sock_connid_t	so_proto_connid; /* protocol generation number */
153 
154 	ushort_t 	so_error;	/* error affecting connection */
155 
156 	struct sockparams *so_sockparams;	/* vnode or socket module */
157 	/* Needed to recreate the same socket for accept */
158 	short	so_family;
159 	short	so_type;
160 	short	so_protocol;
161 	short	so_version;		/* From so_socket call */
162 
163 	/* Accept queue */
164 	kmutex_t	so_acceptq_lock;	/* protects accept queue */
165 	struct sonode	*so_acceptq_next;	/* acceptq list node */
166 	struct sonode 	*so_acceptq_head;
167 	struct sonode	**so_acceptq_tail;
168 	unsigned int	so_acceptq_len;
169 	unsigned int	so_backlog;		/* Listen backlog */
170 	kcondvar_t	so_acceptq_cv;		/* wait for new conn. */
171 
172 	/* Options */
173 	short	so_options;		/* From socket call, see socket.h */
174 	struct linger	so_linger;	/* SO_LINGER value */
175 #define	so_sndbuf	so_proto_props.sopp_txhiwat	/* SO_SNDBUF value */
176 #define	so_sndlowat	so_proto_props.sopp_txlowat	/* tx low water mark */
177 #define	so_rcvbuf	so_proto_props.sopp_rxhiwat	/* SO_RCVBUF value */
178 #define	so_rcvlowat	so_proto_props.sopp_rxlowat	/* rx low water mark */
179 #define	so_max_addr_len	so_proto_props.sopp_maxaddrlen
180 #define	so_minpsz	so_proto_props.sopp_minpsz
181 #define	so_maxpsz	so_proto_props.sopp_maxpsz
182 
183 	int	so_xpg_rcvbuf;		/* SO_RCVBUF value for XPG4 socket */
184 	clock_t	so_sndtimeo;		/* send timeout */
185 	clock_t	so_rcvtimeo;		/* recv timeout */
186 
187 	mblk_t	*so_oobmsg;		/* outofline oob data */
188 	ssize_t	so_oobmark;		/* offset of the oob data */
189 
190 	pid_t	so_pgrp;		/* pgrp for signals */
191 
192 	cred_t		*so_peercred;	/* connected socket peer cred */
193 	pid_t		so_cpid;	/* connected socket peer cached pid */
194 	zoneid_t	so_zoneid;	/* opener's zoneid */
195 
196 	struct pollhead	so_poll_list;	/* common pollhead */
197 	short		so_pollev;	/* events that should be generated */
198 
199 	/* Receive */
200 	unsigned int	so_rcv_queued;	/* # bytes on both rcv lists */
201 	mblk_t		*so_rcv_q_head;	/* processing/copyout rcv queue */
202 	mblk_t		*so_rcv_q_last_head;
203 	mblk_t		*so_rcv_head;	/* protocol prequeue */
204 	mblk_t		*so_rcv_last_head;	/* last mblk in b_next chain */
205 	kcondvar_t	so_rcv_cv;	/* wait for data */
206 	uint_t		so_rcv_wanted;	/* # of bytes wanted by app */
207 	timeout_id_t	so_rcv_timer_tid;
208 
209 #define	so_rcv_thresh	so_proto_props.sopp_rcvthresh
210 #define	so_rcv_timer_interval so_proto_props.sopp_rcvtimer
211 
212 	kcondvar_t	so_snd_cv;	/* wait for snd buffers */
213 	uint32_t
214 		so_snd_qfull: 1,	/* Transmit full */
215 		so_rcv_wakeup: 1,
216 		so_snd_wakeup: 1,
217 		so_not_str: 1,	/* B_TRUE if not streams based socket */
218 		so_pad_to_bit_31: 28;
219 
220 	/* Communication channel with protocol */
221 	sock_lower_handle_t	so_proto_handle;
222 	sock_downcalls_t 	*so_downcalls;
223 
224 	struct sock_proto_props	so_proto_props; /* protocol settings */
225 	boolean_t		so_flowctrld;	/* Flow controlled */
226 	uint_t			so_copyflag;	/* Copy related flag */
227 	kcondvar_t		so_copy_cv;	/* Copy cond variable */
228 
229 	/* kernel sockets */
230 	ksocket_callbacks_t 	so_ksock_callbacks;
231 	void			*so_ksock_cb_arg;	/* callback argument */
232 	kcondvar_t		so_closing_cv;
233 
234 	/* != NULL for sodirect enabled socket */
235 	struct sodirect_s	*so_direct;
236 };
237 
238 #define	SO_HAVE_DATA(so)						\
239 	/*								\
240 	 * For the (tid == 0) case we must check so_rcv_{q_,}head	\
241 	 * rather than (so_rcv_queued > 0), since the latter does not	\
242 	 * take into account mblks with only control/name information.	\
243 	 */								\
244 	((so)->so_rcv_timer_tid == 0 && ((so)->so_rcv_head != NULL ||	\
245 	(so)->so_rcv_q_head != NULL)) ||				\
246 	((so)->so_state & SS_CANTRCVMORE)
247 
248 /*
249  * Events handled by the protocol (in case sd_poll is set)
250  */
251 #define	SO_PROTO_POLLEV		(POLLIN|POLLRDNORM|POLLRDBAND)
252 
253 
254 #endif /* _KERNEL || _KMEMUSER */
255 
256 /* flags */
257 #define	SOMOD		0x0001		/* update socket modification time */
258 #define	SOACC		0x0002		/* update socket access time */
259 
260 #define	SOLOCKED	0x0010		/* use to serialize open/closes */
261 #define	SOREADLOCKED	0x0020		/* serialize kstrgetmsg calls */
262 #define	SOCLONE		0x0040		/* child of clone driver */
263 #define	SOASYNC_UNBIND	0x0080		/* wait for ACK of async unbind */
264 
265 #define	SOCK_IS_NONSTR(so)	((so)->so_not_str)
266 
267 /*
268  * Socket state bits.
269  */
270 #define	SS_ISCONNECTED		0x00000001 /* socket connected to a peer */
271 #define	SS_ISCONNECTING		0x00000002 /* in process, connecting to peer */
272 #define	SS_ISDISCONNECTING	0x00000004 /* in process of disconnecting */
273 #define	SS_CANTSENDMORE		0x00000008 /* can't send more data to peer */
274 
275 #define	SS_CANTRCVMORE		0x00000010 /* can't receive more data */
276 #define	SS_ISBOUND		0x00000020 /* socket is bound */
277 #define	SS_NDELAY		0x00000040 /* FNDELAY non-blocking */
278 #define	SS_NONBLOCK		0x00000080 /* O_NONBLOCK non-blocking */
279 
280 #define	SS_ASYNC		0x00000100 /* async i/o notify */
281 #define	SS_ACCEPTCONN		0x00000200 /* listen done */
282 /*	unused			0x00000400 */	/* was SS_HASCONNIND */
283 #define	SS_SAVEDEOR		0x00000800 /* Saved MSG_EOR rcv side state */
284 
285 #define	SS_RCVATMARK		0x00001000 /* at mark on input */
286 #define	SS_OOBPEND		0x00002000 /* OOB pending or present - poll */
287 #define	SS_HAVEOOBDATA		0x00004000 /* OOB data present */
288 #define	SS_HADOOBDATA		0x00008000 /* OOB data consumed */
289 #define	SS_CLOSING		0x00010000 /* in process of closing */
290 
291 /*	unused			0x00020000 */	/* was SS_FADDR_NOXLATE */
292 /*	unused			0x00040000 */	/* was SS_HASDATA */
293 /*	unused 			0x00080000 */	/* was SS_DONEREAD */
294 /*	unused 			0x00100000 */	/* was SS_MOREDATA */
295 /*	unused 			0x00200000 */	/* was SS_DIRECT */
296 
297 #define	SS_SODIRECT		0x00400000 /* transport supports sodirect */
298 
299 #define	SS_SENTLASTREADSIG	0x01000000 /* last rx signal has been sent */
300 #define	SS_SENTLASTWRITESIG	0x02000000 /* last tx signal has been sent */
301 
302 #define	SS_FALLBACK_DRAIN	0x20000000 /* data was/is being drained */
303 #define	SS_FALLBACK_PENDING	0x40000000 /* fallback is pending */
304 #define	SS_FALLBACK_COMP	0x80000000 /* fallback has completed */
305 
306 
307 /* Set of states when the socket can't be rebound */
308 #define	SS_CANTREBIND	(SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING|\
309 			    SS_CANTSENDMORE|SS_CANTRCVMORE|SS_ACCEPTCONN)
310 
311 /*
312  * Sockets that can fall back to TPI must ensure that fall back is not
313  * initiated while a thread is using a socket.
314  */
315 #define	SO_BLOCK_FALLBACK(so, fn) {			\
316 	ASSERT(MUTEX_NOT_HELD(&(so)->so_lock));		\
317 	rw_enter(&(so)->so_fallback_rwlock, RW_READER);	\
318 	if ((so)->so_state & SS_FALLBACK_COMP) {	\
319 		rw_exit(&(so)->so_fallback_rwlock);	\
320 		return (fn);				\
321 	}						\
322 }
323 
324 #define	SO_UNBLOCK_FALLBACK(so)	{			\
325 	rw_exit(&(so)->so_fallback_rwlock);		\
326 }
327 
328 /* Poll events */
329 #define	SO_POLLEV_IN		0x1	/* POLLIN wakeup needed */
330 #define	SO_POLLEV_ALWAYS	0x2	/* wakeups */
331 
332 /*
333  * Characteristics of sockets. Not changed after the socket is created.
334  */
335 #define	SM_PRIV			0x001	/* privileged for broadcast, raw... */
336 #define	SM_ATOMIC		0x002	/* atomic data transmission */
337 #define	SM_ADDR			0x004	/* addresses given with messages */
338 #define	SM_CONNREQUIRED		0x008	/* connection required by protocol */
339 
340 #define	SM_FDPASSING		0x010	/* passes file descriptors */
341 #define	SM_EXDATA		0x020	/* Can handle T_EXDATA_REQ */
342 #define	SM_OPTDATA		0x040	/* Can handle T_OPTDATA_REQ */
343 #define	SM_BYTESTREAM		0x080	/* Byte stream - can use M_DATA */
344 
345 #define	SM_ACCEPTOR_ID		0x100	/* so_acceptor_id is valid */
346 
347 #define	SM_KERNEL		0x200	/* kernel socket */
348 
349 /* The modes below are only for non-streams sockets */
350 #define	SM_ACCEPTSUPP		0x400	/* can handle accept() */
351 #define	SM_SENDFILESUPP		0x800	/* Private: proto supp sendfile  */
352 
353 /*
354  * Socket versions. Used by the socket library when calling _so_socket().
355  */
356 #define	SOV_STREAM	0	/* Not a socket - just a stream */
357 #define	SOV_DEFAULT	1	/* Select based on so_default_version */
358 #define	SOV_SOCKSTREAM	2	/* Socket plus streams operations */
359 #define	SOV_SOCKBSD	3	/* Socket with no streams operations */
360 #define	SOV_XPG4_2	4	/* Xnet socket */
361 
362 #if defined(_KERNEL) || defined(_KMEMUSER)
363 
364 /*
365  * sonode create and destroy functions.
366  */
367 typedef struct sonode *(*so_create_func_t)(struct sockparams *,
368     int, int, int, int, int, int *, cred_t *);
369 typedef void (*so_destroy_func_t)(struct sonode *);
370 
371 /* STREAM device information */
372 typedef struct sdev_info {
373 	char	*sd_devpath;
374 	int	sd_devpathlen; /* Is 0 if sp_devpath is a static string */
375 	vnode_t	*sd_vnode;
376 } sdev_info_t;
377 
378 #define	SOCKMOD_VERSION		1
379 /* name of the TPI pseudo socket module */
380 #define	SOTPI_SMOD_NAME		"socktpi"
381 
382 typedef struct __smod_priv_s {
383 	so_create_func_t	smodp_sock_create_func;
384 	so_destroy_func_t	smodp_sock_destroy_func;
385 	so_proto_fallback_func_t smodp_proto_fallback_func;
386 } __smod_priv_t;
387 
388 /*
389  * Socket module register information
390  */
391 typedef struct smod_reg_s {
392 	int		smod_version;
393 	char		*smod_name;
394 	size_t		smod_uc_version;
395 	size_t		smod_dc_version;
396 	so_proto_create_func_t	smod_proto_create_func;
397 
398 	/* __smod_priv_data must be NULL */
399 	__smod_priv_t	*__smod_priv;
400 } smod_reg_t;
401 
402 /*
403  * Socket module information
404  */
405 typedef struct smod_info {
406 	int		smod_version;
407 	char		*smod_name;
408 	uint_t		smod_refcnt;		/* # of entries */
409 	size_t		smod_uc_version; 	/* upcall version */
410 	size_t		smod_dc_version;	/* down call version */
411 	so_proto_create_func_t	smod_proto_create_func;
412 	so_proto_fallback_func_t smod_proto_fallback_func;
413 	so_create_func_t	smod_sock_create_func;
414 	so_destroy_func_t	smod_sock_destroy_func;
415 	list_node_t	smod_node;
416 } smod_info_t;
417 
418 typedef struct sockparams_stats {
419 	kstat_named_t	sps_nfallback;	/* # of fallbacks to TPI */
420 	kstat_named_t	sps_nactive;	/* # of active sockets */
421 	kstat_named_t	sps_ncreate;	/* total # of created sockets */
422 } sockparams_stats_t;
423 
424 /*
425  * sockparams
426  *
427  * Used for mapping family/type/protocol to a socket module or STREAMS device
428  */
429 struct sockparams {
430 	/*
431 	 * The family, type, protocol, sdev_info and smod_name are
432 	 * set when the entry is created, and they will never change
433 	 * thereafter.
434 	 */
435 	int		sp_family;
436 	int		sp_type;
437 	int		sp_protocol;
438 
439 	sdev_info_t	sp_sdev_info;	/* STREAM device */
440 	char		*sp_smod_name;	/* socket module name */
441 
442 	kmutex_t	sp_lock;	/* lock for refcnt and smod_info */
443 	uint64_t	sp_refcnt;	/* entry reference count */
444 	smod_info_t	*sp_smod_info;	/* socket module */
445 
446 	sockparams_stats_t sp_stats;
447 	kstat_t		*sp_kstat;
448 
449 	/*
450 	 * The entries below are only modified while holding
451 	 * splist_lock as a writer.
452 	 */
453 	int		sp_flags;	/* see below */
454 	list_node_t	sp_node;
455 };
456 
457 
458 /*
459  * sockparams flags
460  */
461 #define	SOCKPARAMS_EPHEMERAL	0x1	/* temp. entry, not on global list */
462 
463 extern void sockparams_init(void);
464 extern struct sockparams *sockparams_hold_ephemeral_bydev(int, int, int,
465     const char *, int, int *);
466 extern struct sockparams *sockparams_hold_ephemeral_bymod(int, int, int,
467     const char *, int, int *);
468 extern void sockparams_ephemeral_drop_last_ref(struct sockparams *);
469 
470 extern void smod_init(void);
471 extern void smod_add(smod_info_t *);
472 extern int smod_register(const smod_reg_t *);
473 extern int smod_unregister(const char *);
474 extern smod_info_t *smod_lookup_byname(const char *);
475 
476 #define	SOCKPARAMS_HAS_DEVICE(sp)					\
477 	((sp)->sp_sdev_info.sd_devpath != NULL)
478 
479 /* Increase the smod_info_t reference count */
480 #define	SMOD_INC_REF(smodp) {						\
481 	ASSERT((smodp) != NULL);					\
482 	DTRACE_PROBE1(smodinfo__inc__ref, struct smod_info *, (smodp));	\
483 	atomic_inc_uint(&(smodp)->smod_refcnt);				\
484 }
485 
486 /*
487  * Decreace the socket module entry reference count.
488  * When no one mapping to the entry, we try to unload the module from the
489  * kernel. If the module can't unload, just leave the module entry with
490  * a zero refcnt.
491  */
492 #define	SMOD_DEC_REF(smodp, modname) {					\
493 	ASSERT((smodp) != NULL);					\
494 	ASSERT((smodp)->smod_refcnt != 0);				\
495 	atomic_dec_uint(&(smodp)->smod_refcnt);				\
496 	/*								\
497 	 * No need to atomically check the return value because the	\
498 	 * socket module framework will verify that no one is using	\
499 	 * the module before unloading. Worst thing that can happen	\
500 	 * here is multiple calls to mod_remove_by_name(), which is OK.	\
501 	 */								\
502 	if ((smodp)->smod_refcnt == 0)					\
503 		(void) mod_remove_by_name(modname);			\
504 }
505 
506 /* Increase the reference count */
507 #define	SOCKPARAMS_INC_REF(sp) {					\
508 	ASSERT((sp) != NULL);						\
509 	DTRACE_PROBE1(sockparams__inc__ref, struct sockparams *, (sp));	\
510 	mutex_enter(&(sp)->sp_lock);					\
511 	(sp)->sp_refcnt++;						\
512 	ASSERT((sp)->sp_refcnt != 0);					\
513 	mutex_exit(&(sp)->sp_lock);					\
514 }
515 
516 /*
517  * Decrease the reference count.
518  *
519  * If the sockparams is ephemeral, then the thread dropping the last ref
520  * count will destroy the entry.
521  */
522 #define	SOCKPARAMS_DEC_REF(sp) {					\
523 	ASSERT((sp) != NULL);						\
524 	DTRACE_PROBE1(sockparams__dec__ref, struct sockparams *, (sp));	\
525 	mutex_enter(&(sp)->sp_lock);					\
526 	ASSERT((sp)->sp_refcnt > 0);					\
527 	if ((sp)->sp_refcnt == 1) {					\
528 		if ((sp)->sp_flags & SOCKPARAMS_EPHEMERAL) {		\
529 			mutex_exit(&(sp)->sp_lock);			\
530 			sockparams_ephemeral_drop_last_ref((sp));	\
531 		} else {						\
532 			(sp)->sp_refcnt--;				\
533 			if ((sp)->sp_smod_info != NULL) {		\
534 				SMOD_DEC_REF((sp)->sp_smod_info,	\
535 				    (sp)->sp_smod_name);		\
536 			}						\
537 			(sp)->sp_smod_info = NULL;			\
538 			mutex_exit(&(sp)->sp_lock);			\
539 		}							\
540 	} else {							\
541 		(sp)->sp_refcnt--;					\
542 		mutex_exit(&(sp)->sp_lock);				\
543 	}								\
544 }
545 
546 /*
547  * Used to traverse the list of AF_UNIX sockets to construct the kstat
548  * for netstat(1m).
549  */
550 struct socklist {
551 	kmutex_t	sl_lock;
552 	struct sonode	*sl_list;
553 };
554 
555 extern struct socklist socklist;
556 /*
557  * ss_full_waits is the number of times the reader thread
558  * waits when the queue is full and ss_empty_waits is the number
559  * of times the consumer thread waits when the queue is empty.
560  * No locks for these as they are just indicators of whether
561  * disk or network or both is slow or fast.
562  */
563 struct sendfile_stats {
564 	uint32_t ss_file_cached;
565 	uint32_t ss_file_not_cached;
566 	uint32_t ss_full_waits;
567 	uint32_t ss_empty_waits;
568 	uint32_t ss_file_segmap;
569 };
570 
571 /*
572  * A single sendfile request is represented by snf_req.
573  */
574 typedef struct snf_req {
575 	struct snf_req	*sr_next;
576 	mblk_t		*sr_mp_head;
577 	mblk_t		*sr_mp_tail;
578 	kmutex_t	sr_lock;
579 	kcondvar_t	sr_cv;
580 	uint_t		sr_qlen;
581 	int		sr_hiwat;
582 	int		sr_lowat;
583 	int		sr_operation;
584 	struct vnode	*sr_vp;
585 	file_t 		*sr_fp;
586 	ssize_t		sr_maxpsz;
587 	u_offset_t	sr_file_off;
588 	u_offset_t	sr_file_size;
589 #define	SR_READ_DONE	0x80000000
590 	int		sr_read_error;
591 	int		sr_write_error;
592 } snf_req_t;
593 
594 /* A queue of sendfile requests */
595 struct sendfile_queue {
596 	snf_req_t	*snfq_req_head;
597 	snf_req_t	*snfq_req_tail;
598 	kmutex_t	snfq_lock;
599 	kcondvar_t	snfq_cv;
600 	int		snfq_svc_threads;	/* # of service threads */
601 	int		snfq_idle_cnt;		/* # of idling threads */
602 	int		snfq_max_threads;
603 	int		snfq_req_cnt;		/* Number of requests */
604 };
605 
606 #define	READ_OP			1
607 #define	SNFQ_TIMEOUT		(60 * 5 * hz)	/* 5 minutes */
608 
609 /* Socket network operations switch */
610 struct sonodeops {
611 	int 	(*sop_init)(struct sonode *, struct sonode *, cred_t *,
612 		    int);
613 	int	(*sop_accept)(struct sonode *, int, cred_t *, struct sonode **);
614 	int	(*sop_bind)(struct sonode *, struct sockaddr *, socklen_t,
615 		    int, cred_t *);
616 	int	(*sop_listen)(struct sonode *, int, cred_t *);
617 	int	(*sop_connect)(struct sonode *, const struct sockaddr *,
618 		    socklen_t, int, int, cred_t *);
619 	int	(*sop_recvmsg)(struct sonode *, struct msghdr *,
620 		    struct uio *, cred_t *);
621 	int	(*sop_sendmsg)(struct sonode *, struct msghdr *,
622 		    struct uio *, cred_t *);
623 	int	(*sop_sendmblk)(struct sonode *, struct msghdr *, int,
624 		    cred_t *, mblk_t **);
625 	int	(*sop_getpeername)(struct sonode *, struct sockaddr *,
626 		    socklen_t *, boolean_t, cred_t *);
627 	int	(*sop_getsockname)(struct sonode *, struct sockaddr *,
628 		    socklen_t *, cred_t *);
629 	int	(*sop_shutdown)(struct sonode *, int, cred_t *);
630 	int	(*sop_getsockopt)(struct sonode *, int, int, void *,
631 		    socklen_t *, int, cred_t *);
632 	int 	(*sop_setsockopt)(struct sonode *, int, int, const void *,
633 		    socklen_t, cred_t *);
634 	int 	(*sop_ioctl)(struct sonode *, int, intptr_t, int,
635 		    cred_t *, int32_t *);
636 	int 	(*sop_poll)(struct sonode *, short, int, short *,
637 		    struct pollhead **);
638 	int 	(*sop_close)(struct sonode *, int, cred_t *);
639 };
640 
641 #define	SOP_INIT(so, flag, cr, flags)	\
642 	((so)->so_ops->sop_init((so), (flag), (cr), (flags)))
643 #define	SOP_ACCEPT(so, fflag, cr, nsop)	\
644 	((so)->so_ops->sop_accept((so), (fflag), (cr), (nsop)))
645 #define	SOP_BIND(so, name, namelen, flags, cr)	\
646 	((so)->so_ops->sop_bind((so), (name), (namelen), (flags), (cr)))
647 #define	SOP_LISTEN(so, backlog, cr)	\
648 	((so)->so_ops->sop_listen((so), (backlog), (cr)))
649 #define	SOP_CONNECT(so, name, namelen, fflag, flags, cr)	\
650 	((so)->so_ops->sop_connect((so), (name), (namelen), (fflag), (flags), \
651 	(cr)))
652 #define	SOP_RECVMSG(so, msg, uiop, cr)	\
653 	((so)->so_ops->sop_recvmsg((so), (msg), (uiop), (cr)))
654 #define	SOP_SENDMSG(so, msg, uiop, cr)	\
655 	((so)->so_ops->sop_sendmsg((so), (msg), (uiop), (cr)))
656 #define	SOP_SENDMBLK(so, msg, size, cr, mpp)	\
657 	((so)->so_ops->sop_sendmblk((so), (msg), (size), (cr), (mpp)))
658 #define	SOP_GETPEERNAME(so, addr, addrlen, accept, cr)	\
659 	((so)->so_ops->sop_getpeername((so), (addr), (addrlen), (accept), (cr)))
660 #define	SOP_GETSOCKNAME(so, addr, addrlen, cr)	\
661 	((so)->so_ops->sop_getsockname((so), (addr), (addrlen), (cr)))
662 #define	SOP_SHUTDOWN(so, how, cr)	\
663 	((so)->so_ops->sop_shutdown((so), (how), (cr)))
664 #define	SOP_GETSOCKOPT(so, level, optionname, optval, optlenp, flags, cr) \
665 	((so)->so_ops->sop_getsockopt((so), (level), (optionname),	\
666 	    (optval), (optlenp), (flags), (cr)))
667 #define	SOP_SETSOCKOPT(so, level, optionname, optval, optlen, cr)	\
668 	((so)->so_ops->sop_setsockopt((so), (level), (optionname),	\
669 	    (optval), (optlen), (cr)))
670 #define	SOP_IOCTL(so, cmd, arg, mode, cr, rvalp)	\
671 	((so)->so_ops->sop_ioctl((so), (cmd), (arg), (mode), (cr), (rvalp)))
672 #define	SOP_POLL(so, events, anyyet, reventsp, phpp) \
673 	((so)->so_ops->sop_poll((so), (events), (anyyet), (reventsp), (phpp)))
674 #define	SOP_CLOSE(so, flag, cr)	\
675 	((so)->so_ops->sop_close((so), (flag), (cr)))
676 
677 #endif /* defined(_KERNEL) || defined(_KMEMUSER) */
678 
679 #ifdef _KERNEL
680 
681 #define	ISALIGNED_cmsghdr(addr) \
682 		(((uintptr_t)(addr) & (_CMSG_HDR_ALIGNMENT - 1)) == 0)
683 
684 #define	ROUNDUP_cmsglen(len) \
685 	(((len) + _CMSG_HDR_ALIGNMENT - 1) & ~(_CMSG_HDR_ALIGNMENT - 1))
686 
687 #define	IS_NON_STREAM_SOCK(vp) \
688 	((vp)->v_type == VSOCK && (vp)->v_stream == NULL)
689 /*
690  * Macros that operate on struct cmsghdr.
691  * Used in parsing msg_control.
692  * The CMSG_VALID macro does not assume that the last option buffer is padded.
693  */
694 #define	CMSG_NEXT(cmsg)						\
695 	(struct cmsghdr *)((uintptr_t)(cmsg) +			\
696 	    ROUNDUP_cmsglen((cmsg)->cmsg_len))
697 #define	CMSG_CONTENT(cmsg)	(&((cmsg)[1]))
698 #define	CMSG_CONTENTLEN(cmsg)	((cmsg)->cmsg_len - sizeof (struct cmsghdr))
699 #define	CMSG_VALID(cmsg, start, end)					\
700 	(ISALIGNED_cmsghdr(cmsg) &&					\
701 	((uintptr_t)(cmsg) >= (uintptr_t)(start)) &&			\
702 	((uintptr_t)(cmsg) < (uintptr_t)(end)) &&			\
703 	((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) &&	\
704 	((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end)))
705 
706 /*
707  * Maximum size of any argument that is copied in (addresses, options,
708  * access rights). MUST be at least MAXPATHLEN + 3.
709  * BSD and SunOS 4.X limited this to MLEN or MCLBYTES.
710  */
711 #define	SO_MAXARGSIZE	8192
712 
713 /*
714  * Convert between vnode and sonode
715  */
716 #define	VTOSO(vp)	((struct sonode *)((vp)->v_data))
717 #define	SOTOV(sp)	((sp)->so_vnode)
718 
719 /*
720  * Internal flags for sobind()
721  */
722 #define	_SOBIND_REBIND		0x01	/* Bind to existing local address */
723 #define	_SOBIND_UNSPEC		0x02	/* Bind to unspecified address */
724 #define	_SOBIND_LOCK_HELD	0x04	/* so_excl_lock held by caller */
725 #define	_SOBIND_NOXLATE		0x08	/* No addr translation for AF_UNIX */
726 #define	_SOBIND_XPG4_2		0x10	/* xpg4.2 semantics */
727 #define	_SOBIND_SOCKBSD		0x20	/* BSD semantics */
728 #define	_SOBIND_LISTEN		0x40	/* Make into SS_ACCEPTCONN */
729 #define	_SOBIND_SOCKETPAIR	0x80	/* Internal flag for so_socketpair() */
730 					/* to enable listen with backlog = 1 */
731 
732 /*
733  * Internal flags for sounbind()
734  */
735 #define	_SOUNBIND_REBIND	0x01	/* Don't clear fields - will rebind */
736 
737 /*
738  * Internal flags for soconnect()
739  */
740 #define	_SOCONNECT_NOXLATE	0x01	/* No addr translation for AF_UNIX */
741 #define	_SOCONNECT_DID_BIND	0x02	/* Unbind when connect fails */
742 #define	_SOCONNECT_XPG4_2	0x04	/* xpg4.2 semantics */
743 
744 /*
745  * Internal flags for sodisconnect()
746  */
747 #define	_SODISCONNECT_LOCK_HELD	0x01	/* so_excl_lock held by caller */
748 
749 /*
750  * Internal flags for sotpi_getsockopt().
751  */
752 #define	_SOGETSOCKOPT_XPG4_2	0x01	/* xpg4.2 semantics */
753 
754 /*
755  * Internal flags for soallocproto*()
756  */
757 #define	_ALLOC_NOSLEEP		0	/* Don't sleep for memory */
758 #define	_ALLOC_INTR		1	/* Sleep until interrupt */
759 #define	_ALLOC_SLEEP		2	/* Sleep forever */
760 
761 /*
762  * Internal structure for handling AF_UNIX file descriptor passing
763  */
764 struct fdbuf {
765 	int		fd_size;	/* In bytes, for kmem_free */
766 	int		fd_numfd;	/* Number of elements below */
767 	char		*fd_ebuf;	/* Extra buffer to free  */
768 	int		fd_ebuflen;
769 	frtn_t		fd_frtn;
770 	struct file	*fd_fds[1];	/* One or more */
771 };
772 #define	FDBUF_HDRSIZE	(sizeof (struct fdbuf) - sizeof (struct file *))
773 
774 /*
775  * Variable that can be patched to set what version of socket socket()
776  * will create.
777  */
778 extern int so_default_version;
779 
780 #ifdef DEBUG
781 /* Turn on extra testing capabilities */
782 #define	SOCK_TEST
783 #endif /* DEBUG */
784 
785 #ifdef DEBUG
786 char	*pr_state(uint_t, uint_t);
787 char	*pr_addr(int, struct sockaddr *, t_uscalar_t);
788 int	so_verify_oobstate(struct sonode *);
789 #endif /* DEBUG */
790 
791 /*
792  * DEBUG macros
793  */
794 #if defined(DEBUG)
795 #define	SOCK_DEBUG
796 
797 extern int sockdebug;
798 extern int sockprinterr;
799 
800 #define	eprint(args)	printf args
801 #define	eprintso(so, args) \
802 { if (sockprinterr && ((so)->so_options & SO_DEBUG)) printf args; }
803 #define	eprintline(error)					\
804 {								\
805 	if (error != EINTR && (sockprinterr || sockdebug > 0))	\
806 		printf("socket error %d: line %d file %s\n",	\
807 			(error), __LINE__, __FILE__);		\
808 }
809 
810 #define	eprintsoline(so, error)					\
811 { if (sockprinterr && ((so)->so_options & SO_DEBUG))		\
812 	printf("socket(%p) error %d: line %d file %s\n",	\
813 		(void *)(so), (error), __LINE__, __FILE__);	\
814 }
815 #define	dprint(level, args)	{ if (sockdebug > (level)) printf args; }
816 #define	dprintso(so, level, args) \
817 { if (sockdebug > (level) && ((so)->so_options & SO_DEBUG)) printf args; }
818 
819 #else /* define(DEBUG) */
820 
821 #define	eprint(args)		{}
822 #define	eprintso(so, args)	{}
823 #define	eprintline(error)	{}
824 #define	eprintsoline(so, error)	{}
825 #define	dprint(level, args)	{}
826 #define	dprintso(so, level, args) {}
827 
828 #endif /* defined(DEBUG) */
829 
830 extern struct vfsops			sock_vfsops;
831 extern struct vnodeops			*socket_vnodeops;
832 extern const struct fs_operation_def	socket_vnodeops_template[];
833 
834 extern dev_t				sockdev;
835 
836 /*
837  * sockfs functions
838  */
839 extern int	sock_getmsg(vnode_t *, struct strbuf *, struct strbuf *,
840 			uchar_t *, int *, int, rval_t *);
841 extern int	sock_putmsg(vnode_t *, struct strbuf *, struct strbuf *,
842 			uchar_t, int, int);
843 extern int	sogetvp(char *, vnode_t **, int);
844 extern int	sockinit(int, char *);
845 extern int	soconfig(int, int, int,	char *, int, char *);
846 extern int	solookup(int, int, int, struct sockparams **);
847 extern void	so_lock_single(struct sonode *);
848 extern void	so_unlock_single(struct sonode *, int);
849 extern int	so_lock_read(struct sonode *, int);
850 extern int	so_lock_read_intr(struct sonode *, int);
851 extern void	so_unlock_read(struct sonode *);
852 extern void	*sogetoff(mblk_t *, t_uscalar_t, t_uscalar_t, uint_t);
853 extern void	so_getopt_srcaddr(void *, t_uscalar_t,
854 			void **, t_uscalar_t *);
855 extern int	so_getopt_unix_close(void *, t_uscalar_t);
856 extern void	fdbuf_free(struct fdbuf *);
857 extern mblk_t	*fdbuf_allocmsg(int, struct fdbuf *);
858 extern int	fdbuf_create(void *, int, struct fdbuf **);
859 extern void	so_closefds(void *, t_uscalar_t, int, int);
860 extern int	so_getfdopt(void *, t_uscalar_t, int, void **, int *);
861 t_uscalar_t	so_optlen(void *, t_uscalar_t, int);
862 extern void	so_cmsg2opt(void *, t_uscalar_t, int, mblk_t *);
863 extern t_uscalar_t
864 		so_cmsglen(mblk_t *, void *, t_uscalar_t, int);
865 extern int	so_opt2cmsg(mblk_t *, void *, t_uscalar_t, int,
866 			void *, t_uscalar_t);
867 extern void	soisconnecting(struct sonode *);
868 extern void	soisconnected(struct sonode *);
869 extern void	soisdisconnected(struct sonode *, int);
870 extern void	socantsendmore(struct sonode *);
871 extern void	socantrcvmore(struct sonode *);
872 extern void	soseterror(struct sonode *, int);
873 extern int	sogeterr(struct sonode *, boolean_t);
874 extern int	sowaitconnected(struct sonode *, int, int);
875 
876 extern ssize_t	soreadfile(file_t *, uchar_t *, u_offset_t, int *, size_t);
877 extern void	*sock_kstat_init(zoneid_t);
878 extern void	sock_kstat_fini(zoneid_t, void *);
879 extern struct sonode *getsonode(int, int *, file_t **);
880 /*
881  * Function wrappers (mostly around the sonode switch) for
882  * backward compatibility.
883  */
884 extern int	soaccept(struct sonode *, int, struct sonode **);
885 extern int	sobind(struct sonode *, struct sockaddr *, socklen_t,
886 		    int, int);
887 extern int	solisten(struct sonode *, int);
888 extern int	soconnect(struct sonode *, const struct sockaddr *, socklen_t,
889 		    int, int);
890 extern int	sorecvmsg(struct sonode *, struct nmsghdr *, struct uio *);
891 extern int	sosendmsg(struct sonode *, struct nmsghdr *, struct uio *);
892 extern int	soshutdown(struct sonode *, int);
893 extern int	sogetsockopt(struct sonode *, int, int, void *, socklen_t *,
894 		    int);
895 extern int	sosetsockopt(struct sonode *, int, int, const void *,
896 		    t_uscalar_t);
897 
898 extern struct sonode	*socreate(struct sockparams *, int, int, int, int,
899 			    int *);
900 
901 extern int	so_copyin(const void *, void *, size_t, int);
902 extern int	so_copyout(const void *, void *, size_t, int);
903 
904 #endif
905 
906 /*
907  * Internal structure for obtaining sonode information from the socklist.
908  * These types match those corresponding in the sonode structure.
909  * This is not a published interface, and may change at any time.
910  */
911 struct sockinfo {
912 	uint_t		si_size;		/* real length of this struct */
913 	short		si_family;
914 	short		si_type;
915 	ushort_t	si_flag;
916 	uint_t		si_state;
917 	uint_t		si_ux_laddr_sou_magic;
918 	uint_t		si_ux_faddr_sou_magic;
919 	t_scalar_t	si_serv_type;
920 	t_uscalar_t	si_laddr_soa_len;
921 	t_uscalar_t	si_faddr_soa_len;
922 	uint16_t	si_laddr_family;
923 	uint16_t	si_faddr_family;
924 	char		si_laddr_sun_path[MAXPATHLEN + 1]; /* NULL terminated */
925 	char		si_faddr_sun_path[MAXPATHLEN + 1];
926 	boolean_t	si_faddr_noxlate;
927 	zoneid_t	si_szoneid;
928 };
929 
930 #define	SOCKMOD_PATH	"socketmod"	/* dir where sockmods are stored */
931 
932 #ifdef	__cplusplus
933 }
934 #endif
935 
936 #endif	/* _SYS_SOCKETVAR_H */
937