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