Merge commit '7e3488dc6cdcb0c04e1ce167a1a3bfef83b5f2e0'
[unleashed.git] / include / sys / socketvar.h
blob6d122ff87532ae23187da7028095bb5c7dca1c13
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
32 * All Rights Reserved
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
36 * contributors.
39 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
42 #ifndef _SYS_SOCKETVAR_H
43 #define _SYS_SOCKETVAR_H
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 <sys/sdt.h>
54 #include <sys/modctl.h>
55 #include <sys/atomic.h>
56 #include <sys/socket.h>
57 #include <sys/ksocket.h>
58 #include <sys/kstat.h>
59 #include <sys/stdbool.h>
61 #ifdef _KERNEL
62 #include <sys/vfs.h>
63 #endif
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
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.
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.
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.
87 struct so_ux_addr {
88 void *soua_vp; /* vnode pointer or assigned by tl */
89 uint_t soua_magic; /* See below */
92 #define SOU_MAGIC_EXPLICIT 0x75787670 /* "uxvp" */
93 #define SOU_MAGIC_IMPLICIT 0x616e6f6e /* "anon" */
95 struct sockaddr_ux {
96 sa_family_t sou_family; /* AF_UNIX */
97 struct so_ux_addr sou_addr;
100 #if defined(_KERNEL) || defined(_KMEMUSER)
102 #include <sys/socket_proto.h>
104 typedef struct sonodeops sonodeops_t;
105 typedef struct sonode sonode_t;
107 struct sodirect_s;
110 * The sonode represents a socket. A sonode never exist in the file system
111 * name space and can not be opened using open() - only the socket, socketpair
112 * and accept calls create sonodes.
114 * The locking of sockfs uses the so_lock mutex plus the SOLOCKED and
115 * SOREADLOCKED flags in so_flag. The mutex protects all the state in the
116 * sonode. It is expected that the underlying transport protocol serializes
117 * socket operations, so sockfs will not normally not single-thread
118 * operations. However, certain sockets, including TPI based ones, can only
119 * handle one control operation at a time. The SOLOCKED flag is used to
120 * single-thread operations from sockfs users to prevent e.g. multiple bind()
121 * calls to operate on the same sonode concurrently. The SOREADLOCKED flag is
122 * used to ensure that only one thread sleeps in kstrgetmsg for a given
123 * sonode. This is needed to ensure atomic operation for things like
124 * MSG_WAITALL.
126 * The so_fallback_rwlock is used to ensure that for sockets that can
127 * fall back to TPI, the fallback is not initiated until all pending
128 * operations have completed.
130 * Note that so_lock is sometimes held across calls that might go to sleep
131 * (kmem_alloc and soallocproto*). This implies that no other lock in
132 * the system should be held when calling into sockfs; from the system call
133 * side or from strrput (in case of TPI based sockets). If locks are held
134 * while calling into sockfs the system might hang when running low on memory.
136 struct sonode {
137 struct vnode *so_vnode; /* vnode associated with this sonode */
139 sonodeops_t *so_ops; /* operations vector for this sonode */
140 void *so_priv; /* sonode private data */
142 krwlock_t so_fallback_rwlock;
143 kmutex_t so_lock; /* protects sonode fields */
145 kcondvar_t so_state_cv; /* synchronize state changes */
146 kcondvar_t so_single_cv; /* wait due to SOLOCKED */
147 kcondvar_t so_read_cv; /* wait due to SOREADLOCKED */
149 /* These fields are protected by so_lock */
151 uint_t so_state; /* internal state flags SS_*, below */
152 uint_t so_mode; /* characteristics on socket. SM_* */
153 ushort_t so_flag; /* flags, see below */
154 int so_count; /* count of opened references */
156 sock_connid_t so_proto_connid; /* protocol generation number */
158 ushort_t so_error; /* error affecting connection */
160 struct sockparams *so_sockparams; /* vnode or socket module */
161 /* Needed to recreate the same socket for accept */
162 short so_family;
163 short so_type;
164 short so_protocol;
166 * XXX: We removed socket versions, but SOV_STREAM, which was
167 * documented as "Not a socket - just a stream", is special. We use
168 * this flag as a direct replacement for now, but drop the SOV_* and
169 * "version" terminology for clarity.
171 bool so_is_stream;
173 /* Accept queue */
174 kmutex_t so_acceptq_lock; /* protects accept queue */
175 list_t so_acceptq_list; /* pending conns */
176 list_t so_acceptq_defer; /* deferred conns */
177 list_node_t so_acceptq_node; /* acceptq list node */
178 unsigned int so_acceptq_len; /* # of conns (both lists) */
179 unsigned int so_backlog; /* Listen backlog */
180 kcondvar_t so_acceptq_cv; /* wait for new conn. */
181 struct sonode *so_listener; /* parent socket */
183 /* Options */
184 short so_options; /* From socket call, see socket.h */
185 struct linger so_linger; /* SO_LINGER value */
186 #define so_sndbuf so_proto_props.sopp_txhiwat /* SO_SNDBUF value */
187 #define so_sndlowat so_proto_props.sopp_txlowat /* tx low water mark */
188 #define so_rcvbuf so_proto_props.sopp_rxhiwat /* SO_RCVBUF value */
189 #define so_rcvlowat so_proto_props.sopp_rxlowat /* rx low water mark */
190 #define so_max_addr_len so_proto_props.sopp_maxaddrlen
191 #define so_minpsz so_proto_props.sopp_minpsz
192 #define so_maxpsz so_proto_props.sopp_maxpsz
194 clock_t so_sndtimeo; /* send timeout */
195 clock_t so_rcvtimeo; /* recv timeout */
197 mblk_t *so_oobmsg; /* outofline oob data */
198 ssize_t so_oobmark; /* offset of the oob data */
200 pid_t so_pgrp; /* pgrp for signals */
202 cred_t *so_peercred; /* connected socket peer cred */
203 pid_t so_cpid; /* connected socket peer cached pid */
204 zoneid_t so_zoneid; /* opener's zoneid */
206 struct pollhead so_poll_list; /* common pollhead */
207 short so_pollev; /* events that should be generated */
209 /* Receive */
210 unsigned int so_rcv_queued; /* # bytes on both rcv lists */
211 mblk_t *so_rcv_q_head; /* processing/copyout rcv queue */
212 mblk_t *so_rcv_q_last_head;
213 mblk_t *so_rcv_head; /* protocol prequeue */
214 mblk_t *so_rcv_last_head; /* last mblk in b_next chain */
215 kcondvar_t so_rcv_cv; /* wait for data */
216 uint_t so_rcv_wanted; /* # of bytes wanted by app */
217 timeout_id_t so_rcv_timer_tid;
219 #define so_rcv_thresh so_proto_props.sopp_rcvthresh
220 #define so_rcv_timer_interval so_proto_props.sopp_rcvtimer
222 kcondvar_t so_snd_cv; /* wait for snd buffers */
223 uint32_t
224 so_snd_qfull: 1, /* Transmit full */
225 so_rcv_wakeup: 1,
226 so_snd_wakeup: 1,
227 so_not_str: 1, /* B_TRUE if not streams based socket */
228 so_pad_to_bit_31: 28;
230 /* Communication channel with protocol */
231 sock_lower_handle_t so_proto_handle;
232 sock_downcalls_t *so_downcalls;
234 struct sock_proto_props so_proto_props; /* protocol settings */
235 boolean_t so_flowctrld; /* Flow controlled */
236 uint_t so_copyflag; /* Copy related flag */
237 kcondvar_t so_copy_cv; /* Copy cond variable */
239 /* kernel sockets */
240 ksocket_callbacks_t so_ksock_callbacks;
241 void *so_ksock_cb_arg; /* callback argument */
242 kcondvar_t so_closing_cv;
244 /* != NULL for sodirect enabled socket */
245 struct sodirect_s *so_direct;
247 /* socket filters */
248 uint_t so_filter_active; /* # of active fil */
249 uint_t so_filter_tx; /* pending tx ops */
250 struct sof_instance *so_filter_top; /* top of stack */
251 struct sof_instance *so_filter_bottom; /* bottom of stack */
252 clock_t so_filter_defertime; /* time when deferred */
255 #define SO_HAVE_DATA(so) \
256 /* \
257 * For the (tid == 0) case we must check so_rcv_{q_,}head \
258 * rather than (so_rcv_queued > 0), since the latter does not \
259 * take into account mblks with only control/name information. \
260 */ \
261 ((so)->so_rcv_timer_tid == 0 && ((so)->so_rcv_head != NULL || \
262 (so)->so_rcv_q_head != NULL)) || \
263 ((so)->so_state & SS_CANTRCVMORE)
266 * Events handled by the protocol (in case sd_poll is set)
268 #define SO_PROTO_POLLEV (POLLIN|POLLRDNORM|POLLRDBAND)
271 #endif /* _KERNEL || _KMEMUSER */
273 /* flags */
274 #define SOMOD 0x0001 /* update socket modification time */
275 #define SOACC 0x0002 /* update socket access time */
277 #define SOLOCKED 0x0010 /* use to serialize open/closes */
278 #define SOREADLOCKED 0x0020 /* serialize kstrgetmsg calls */
279 #define SOCLONE 0x0040 /* child of clone driver */
280 #define SOASYNC_UNBIND 0x0080 /* wait for ACK of async unbind */
282 #define SOCK_IS_NONSTR(so) ((so)->so_not_str)
285 * Socket state bits.
287 #define SS_ISCONNECTED 0x00000001 /* socket connected to a peer */
288 #define SS_ISCONNECTING 0x00000002 /* in process, connecting to peer */
289 #define SS_ISDISCONNECTING 0x00000004 /* in process of disconnecting */
290 #define SS_CANTSENDMORE 0x00000008 /* can't send more data to peer */
292 #define SS_CANTRCVMORE 0x00000010 /* can't receive more data */
293 #define SS_ISBOUND 0x00000020 /* socket is bound */
294 #define SS_NDELAY 0x00000040 /* FNDELAY non-blocking */
295 #define SS_NONBLOCK 0x00000080 /* O_NONBLOCK non-blocking */
297 #define SS_ASYNC 0x00000100 /* async i/o notify */
298 #define SS_ACCEPTCONN 0x00000200 /* listen done */
299 /* unused 0x00000400 */ /* was SS_HASCONNIND */
300 #define SS_SAVEDEOR 0x00000800 /* Saved MSG_EOR rcv side state */
302 #define SS_RCVATMARK 0x00001000 /* at mark on input */
303 #define SS_OOBPEND 0x00002000 /* OOB pending or present - poll */
304 #define SS_HAVEOOBDATA 0x00004000 /* OOB data present */
305 #define SS_HADOOBDATA 0x00008000 /* OOB data consumed */
306 #define SS_CLOSING 0x00010000 /* in process of closing */
308 #define SS_FIL_DEFER 0x00020000 /* filter deferred notification */
309 #define SS_FILOP_OK 0x00040000 /* socket can attach filters */
310 #define SS_FIL_RCV_FLOWCTRL 0x00080000 /* filter asserted rcv flow ctrl */
311 #define SS_FIL_SND_FLOWCTRL 0x00100000 /* filter asserted snd flow ctrl */
312 #define SS_FIL_STOP 0x00200000 /* no more filter actions */
314 #define SS_SODIRECT 0x00400000 /* transport supports sodirect */
316 #define SS_SENTLASTREADSIG 0x01000000 /* last rx signal has been sent */
317 #define SS_SENTLASTWRITESIG 0x02000000 /* last tx signal has been sent */
319 #define SS_FALLBACK_DRAIN 0x20000000 /* data was/is being drained */
320 #define SS_FALLBACK_PENDING 0x40000000 /* fallback is pending */
321 #define SS_FALLBACK_COMP 0x80000000 /* fallback has completed */
324 /* Set of states when the socket can't be rebound */
325 #define SS_CANTREBIND (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING|\
326 SS_CANTSENDMORE|SS_CANTRCVMORE|SS_ACCEPTCONN)
329 * Sockets that can fall back to TPI must ensure that fall back is not
330 * initiated while a thread is using a socket.
332 #define SO_BLOCK_FALLBACK(so, fn) \
333 ASSERT(MUTEX_NOT_HELD(&(so)->so_lock)); \
334 rw_enter(&(so)->so_fallback_rwlock, RW_READER); \
335 if ((so)->so_state & (SS_FALLBACK_COMP|SS_FILOP_OK)) { \
336 if ((so)->so_state & SS_FALLBACK_COMP) { \
337 rw_exit(&(so)->so_fallback_rwlock); \
338 return (fn); \
339 } else { \
340 mutex_enter(&(so)->so_lock); \
341 (so)->so_state &= ~SS_FILOP_OK; \
342 mutex_exit(&(so)->so_lock); \
346 #define SO_UNBLOCK_FALLBACK(so) { \
347 rw_exit(&(so)->so_fallback_rwlock); \
350 #define SO_SND_FLOWCTRLD(so) \
351 ((so)->so_snd_qfull || (so)->so_state & SS_FIL_SND_FLOWCTRL)
353 /* Poll events */
354 #define SO_POLLEV_IN 0x1 /* POLLIN wakeup needed */
355 #define SO_POLLEV_ALWAYS 0x2 /* wakeups */
358 * Characteristics of sockets. Not changed after the socket is created.
360 #define SM_PRIV 0x001 /* privileged for broadcast, raw... */
361 #define SM_ATOMIC 0x002 /* atomic data transmission */
362 #define SM_ADDR 0x004 /* addresses given with messages */
363 #define SM_CONNREQUIRED 0x008 /* connection required by protocol */
365 #define SM_FDPASSING 0x010 /* passes file descriptors */
366 #define SM_EXDATA 0x020 /* Can handle T_EXDATA_REQ */
367 #define SM_OPTDATA 0x040 /* Can handle T_OPTDATA_REQ */
368 #define SM_BYTESTREAM 0x080 /* Byte stream - can use M_DATA */
370 #define SM_ACCEPTOR_ID 0x100 /* so_acceptor_id is valid */
372 #define SM_KERNEL 0x200 /* kernel socket */
374 /* The modes below are only for non-streams sockets */
375 #define SM_ACCEPTSUPP 0x400 /* can handle accept() */
376 #define SM_SENDFILESUPP 0x800 /* Private: proto supp sendfile */
378 #if defined(_KERNEL) || defined(_KMEMUSER)
381 * sonode create and destroy functions.
383 typedef struct sonode *(*so_create_func_t)(struct sockparams *,
384 int, int, int, int, int *, cred_t *);
385 typedef void (*so_destroy_func_t)(struct sonode *);
387 /* STREAM device information */
388 typedef struct sdev_info {
389 char *sd_devpath;
390 int sd_devpathlen; /* Is 0 if sp_devpath is a static string */
391 vnode_t *sd_vnode;
392 } sdev_info_t;
394 #define SOCKMOD_VERSION_1 1
395 #define SOCKMOD_VERSION 2
397 /* name of the TPI pseudo socket module */
398 #define SOTPI_SMOD_NAME "socktpi"
400 typedef struct __smod_priv_s {
401 so_create_func_t smodp_sock_create_func;
402 so_destroy_func_t smodp_sock_destroy_func;
403 so_proto_fallback_func_t smodp_proto_fallback_func;
404 const char *smodp_fallback_devpath_v4;
405 const char *smodp_fallback_devpath_v6;
406 } __smod_priv_t;
409 * Socket module register information
411 typedef struct smod_reg_s {
412 int smod_version;
413 char *smod_name;
414 size_t smod_uc_version;
415 size_t smod_dc_version;
416 so_proto_create_func_t smod_proto_create_func;
418 /* __smod_priv_data must be NULL */
419 __smod_priv_t *__smod_priv;
420 } smod_reg_t;
423 * Socket module information
425 typedef struct smod_info {
426 int smod_version;
427 char *smod_name;
428 uint_t smod_refcnt; /* # of entries */
429 size_t smod_uc_version; /* upcall version */
430 size_t smod_dc_version; /* down call version */
431 so_proto_create_func_t smod_proto_create_func;
432 so_proto_fallback_func_t smod_proto_fallback_func;
433 const char *smod_fallback_devpath_v4;
434 const char *smod_fallback_devpath_v6;
435 so_create_func_t smod_sock_create_func;
436 so_destroy_func_t smod_sock_destroy_func;
437 list_node_t smod_node;
438 } smod_info_t;
440 typedef struct sockparams_stats {
441 kstat_named_t sps_nfallback; /* # of fallbacks to TPI */
442 kstat_named_t sps_nactive; /* # of active sockets */
443 kstat_named_t sps_ncreate; /* total # of created sockets */
444 } sockparams_stats_t;
447 * sockparams
449 * Used for mapping family/type/protocol to a socket module or STREAMS device
451 struct sockparams {
453 * The family, type, protocol, sdev_info and smod_name are
454 * set when the entry is created, and they will never change
455 * thereafter.
457 int sp_family;
458 int sp_type;
459 int sp_protocol;
461 sdev_info_t sp_sdev_info; /* STREAM device */
462 char *sp_smod_name; /* socket module name */
464 kmutex_t sp_lock; /* lock for refcnt and smod_info */
465 uint64_t sp_refcnt; /* entry reference count */
466 smod_info_t *sp_smod_info; /* socket module */
468 sockparams_stats_t sp_stats;
469 kstat_t *sp_kstat;
472 * The entries below are only modified while holding
473 * sockconf_lock as a writer.
475 int sp_flags; /* see below */
476 list_node_t sp_node;
478 list_t sp_auto_filters; /* list of automatic filters */
479 list_t sp_prog_filters; /* list of programmatic filters */
482 struct sof_entry;
484 typedef struct sp_filter {
485 struct sof_entry *spf_filter;
486 list_node_t spf_node;
487 } sp_filter_t;
491 * sockparams flags
493 #define SOCKPARAMS_EPHEMERAL 0x1 /* temp. entry, not on global list */
495 extern void sockparams_init(void);
496 extern struct sockparams *sockparams_hold_ephemeral_bydev(int, int, int,
497 const char *, int, int *);
498 extern struct sockparams *sockparams_hold_ephemeral_bymod(int, int, int,
499 const char *, int, int *);
500 extern void sockparams_ephemeral_drop_last_ref(struct sockparams *);
502 extern struct sockparams *sockparams_create(int, int, int, char *, char *, int,
503 int, int, int *);
504 extern void sockparams_destroy(struct sockparams *);
505 extern int sockparams_add(struct sockparams *);
506 extern int sockparams_delete(int, int, int);
507 extern int sockparams_new_filter(struct sof_entry *);
508 extern void sockparams_filter_cleanup(struct sof_entry *);
509 extern int sockparams_copyout_socktable(uintptr_t);
511 extern void smod_init(void);
512 extern void smod_add(smod_info_t *);
513 extern int smod_register(const smod_reg_t *);
514 extern int smod_unregister(const char *);
515 extern smod_info_t *smod_lookup_byname(const char *);
517 #define SOCKPARAMS_HAS_DEVICE(sp) \
518 ((sp)->sp_sdev_info.sd_devpath != NULL)
520 /* Increase the smod_info_t reference count */
521 #define SMOD_INC_REF(smodp) { \
522 ASSERT((smodp) != NULL); \
523 DTRACE_PROBE1(smodinfo__inc__ref, struct smod_info *, (smodp)); \
524 atomic_inc_uint(&(smodp)->smod_refcnt); \
528 * Decreace the socket module entry reference count.
529 * When no one mapping to the entry, we try to unload the module from the
530 * kernel. If the module can't unload, just leave the module entry with
531 * a zero refcnt.
533 #define SMOD_DEC_REF(smodp, modname) { \
534 ASSERT((smodp) != NULL); \
535 ASSERT((smodp)->smod_refcnt != 0); \
536 atomic_dec_uint(&(smodp)->smod_refcnt); \
537 /* \
538 * No need to atomically check the return value because the \
539 * socket module framework will verify that no one is using \
540 * the module before unloading. Worst thing that can happen \
541 * here is multiple calls to mod_remove_by_name(), which is OK. \
542 */ \
543 if ((smodp)->smod_refcnt == 0) \
544 (void) mod_remove_by_name(modname); \
547 /* Increase the reference count */
548 #define SOCKPARAMS_INC_REF(sp) { \
549 ASSERT((sp) != NULL); \
550 DTRACE_PROBE1(sockparams__inc__ref, struct sockparams *, (sp)); \
551 mutex_enter(&(sp)->sp_lock); \
552 (sp)->sp_refcnt++; \
553 ASSERT((sp)->sp_refcnt != 0); \
554 mutex_exit(&(sp)->sp_lock); \
558 * Decrease the reference count.
560 * If the sockparams is ephemeral, then the thread dropping the last ref
561 * count will destroy the entry.
563 #define SOCKPARAMS_DEC_REF(sp) { \
564 ASSERT((sp) != NULL); \
565 DTRACE_PROBE1(sockparams__dec__ref, struct sockparams *, (sp)); \
566 mutex_enter(&(sp)->sp_lock); \
567 ASSERT((sp)->sp_refcnt > 0); \
568 if ((sp)->sp_refcnt == 1) { \
569 if ((sp)->sp_flags & SOCKPARAMS_EPHEMERAL) { \
570 mutex_exit(&(sp)->sp_lock); \
571 sockparams_ephemeral_drop_last_ref((sp)); \
572 } else { \
573 (sp)->sp_refcnt--; \
574 if ((sp)->sp_smod_info != NULL) { \
575 SMOD_DEC_REF((sp)->sp_smod_info, \
576 (sp)->sp_smod_name); \
578 (sp)->sp_smod_info = NULL; \
579 mutex_exit(&(sp)->sp_lock); \
581 } else { \
582 (sp)->sp_refcnt--; \
583 mutex_exit(&(sp)->sp_lock); \
588 * Used to traverse the list of AF_UNIX sockets to construct the kstat
589 * for netstat(1m).
591 struct socklist {
592 kmutex_t sl_lock;
593 struct sonode *sl_list;
596 extern struct socklist socklist;
598 * ss_full_waits is the number of times the reader thread
599 * waits when the queue is full and ss_empty_waits is the number
600 * of times the consumer thread waits when the queue is empty.
601 * No locks for these as they are just indicators of whether
602 * disk or network or both is slow or fast.
604 struct sendfile_stats {
605 uint32_t ss_file_cached;
606 uint32_t ss_file_not_cached;
607 uint32_t ss_full_waits;
608 uint32_t ss_empty_waits;
609 uint32_t ss_file_segmap;
613 * A single sendfile request is represented by snf_req.
615 typedef struct snf_req {
616 struct snf_req *sr_next;
617 mblk_t *sr_mp_head;
618 mblk_t *sr_mp_tail;
619 kmutex_t sr_lock;
620 kcondvar_t sr_cv;
621 uint_t sr_qlen;
622 int sr_hiwat;
623 int sr_lowat;
624 int sr_operation;
625 struct vnode *sr_vp;
626 file_t *sr_fp;
627 ssize_t sr_maxpsz;
628 uoff_t sr_file_off;
629 uoff_t sr_file_size;
630 #define SR_READ_DONE 0x80000000
631 int sr_read_error;
632 int sr_write_error;
633 } snf_req_t;
635 /* A queue of sendfile requests */
636 struct sendfile_queue {
637 snf_req_t *snfq_req_head;
638 snf_req_t *snfq_req_tail;
639 kmutex_t snfq_lock;
640 kcondvar_t snfq_cv;
641 int snfq_svc_threads; /* # of service threads */
642 int snfq_idle_cnt; /* # of idling threads */
643 int snfq_max_threads;
644 int snfq_req_cnt; /* Number of requests */
647 #define READ_OP 1
648 #define SNFQ_TIMEOUT (60 * 5 * hz) /* 5 minutes */
650 /* Socket network operations switch */
651 struct sonodeops {
652 int (*sop_init)(struct sonode *, struct sonode *, cred_t *,
653 int);
654 int (*sop_accept)(struct sonode *, int, cred_t *, struct sonode **);
655 int (*sop_bind)(struct sonode *, struct sockaddr *, socklen_t,
656 int, cred_t *);
657 int (*sop_listen)(struct sonode *, int, cred_t *);
658 int (*sop_connect)(struct sonode *, struct sockaddr *,
659 socklen_t, int, int, cred_t *);
660 int (*sop_recvmsg)(struct sonode *, struct msghdr *,
661 struct uio *, cred_t *);
662 int (*sop_sendmsg)(struct sonode *, struct msghdr *,
663 struct uio *, cred_t *);
664 int (*sop_sendmblk)(struct sonode *, struct msghdr *, int,
665 cred_t *, mblk_t **);
666 int (*sop_getpeername)(struct sonode *, struct sockaddr *,
667 socklen_t *, boolean_t, cred_t *);
668 int (*sop_getsockname)(struct sonode *, struct sockaddr *,
669 socklen_t *, cred_t *);
670 int (*sop_shutdown)(struct sonode *, int, cred_t *);
671 int (*sop_getsockopt)(struct sonode *, int, int, void *,
672 socklen_t *, int, cred_t *);
673 int (*sop_setsockopt)(struct sonode *, int, int, const void *,
674 socklen_t, cred_t *);
675 int (*sop_ioctl)(struct sonode *, int, intptr_t, int,
676 cred_t *, int32_t *);
677 int (*sop_poll)(struct sonode *, short, int, short *,
678 struct pollhead **);
679 int (*sop_close)(struct sonode *, int, cred_t *);
682 #define SOP_INIT(so, flag, cr, flags) \
683 ((so)->so_ops->sop_init((so), (flag), (cr), (flags)))
684 #define SOP_ACCEPT(so, fflag, cr, nsop) \
685 ((so)->so_ops->sop_accept((so), (fflag), (cr), (nsop)))
686 #define SOP_BIND(so, name, namelen, flags, cr) \
687 ((so)->so_ops->sop_bind((so), (name), (namelen), (flags), (cr)))
688 #define SOP_LISTEN(so, backlog, cr) \
689 ((so)->so_ops->sop_listen((so), (backlog), (cr)))
690 #define SOP_CONNECT(so, name, namelen, fflag, flags, cr) \
691 ((so)->so_ops->sop_connect((so), (name), (namelen), (fflag), (flags), \
692 (cr)))
693 #define SOP_RECVMSG(so, msg, uiop, cr) \
694 ((so)->so_ops->sop_recvmsg((so), (msg), (uiop), (cr)))
695 #define SOP_SENDMSG(so, msg, uiop, cr) \
696 ((so)->so_ops->sop_sendmsg((so), (msg), (uiop), (cr)))
697 #define SOP_SENDMBLK(so, msg, size, cr, mpp) \
698 ((so)->so_ops->sop_sendmblk((so), (msg), (size), (cr), (mpp)))
699 #define SOP_GETPEERNAME(so, addr, addrlen, accept, cr) \
700 ((so)->so_ops->sop_getpeername((so), (addr), (addrlen), (accept), (cr)))
701 #define SOP_GETSOCKNAME(so, addr, addrlen, cr) \
702 ((so)->so_ops->sop_getsockname((so), (addr), (addrlen), (cr)))
703 #define SOP_SHUTDOWN(so, how, cr) \
704 ((so)->so_ops->sop_shutdown((so), (how), (cr)))
705 #define SOP_GETSOCKOPT(so, level, optionname, optval, optlenp, flags, cr) \
706 ((so)->so_ops->sop_getsockopt((so), (level), (optionname), \
707 (optval), (optlenp), (flags), (cr)))
708 #define SOP_SETSOCKOPT(so, level, optionname, optval, optlen, cr) \
709 ((so)->so_ops->sop_setsockopt((so), (level), (optionname), \
710 (optval), (optlen), (cr)))
711 #define SOP_IOCTL(so, cmd, arg, mode, cr, rvalp) \
712 ((so)->so_ops->sop_ioctl((so), (cmd), (arg), (mode), (cr), (rvalp)))
713 #define SOP_POLL(so, events, anyyet, reventsp, phpp) \
714 ((so)->so_ops->sop_poll((so), (events), (anyyet), (reventsp), (phpp)))
715 #define SOP_CLOSE(so, flag, cr) \
716 ((so)->so_ops->sop_close((so), (flag), (cr)))
718 #endif /* defined(_KERNEL) || defined(_KMEMUSER) */
720 #ifdef _KERNEL
722 #define ISALIGNED_cmsghdr(addr) \
723 (((uintptr_t)(addr) & (_CMSG_HDR_ALIGNMENT - 1)) == 0)
725 #define ROUNDUP_cmsglen(len) \
726 (((len) + _CMSG_HDR_ALIGNMENT - 1) & ~(_CMSG_HDR_ALIGNMENT - 1))
728 #define IS_NON_STREAM_SOCK(vp) \
729 ((vp)->v_type == VSOCK && (vp)->v_stream == NULL)
731 * Macros that operate on struct cmsghdr.
732 * Used in parsing msg_control.
733 * The CMSG_VALID macro does not assume that the last option buffer is padded.
735 #define CMSG_NEXT(cmsg) \
736 (struct cmsghdr *)((uintptr_t)(cmsg) + \
737 ROUNDUP_cmsglen((cmsg)->cmsg_len))
738 #define CMSG_CONTENT(cmsg) (&((cmsg)[1]))
739 #define CMSG_CONTENTLEN(cmsg) ((cmsg)->cmsg_len - sizeof (struct cmsghdr))
740 #define CMSG_VALID(cmsg, start, end) \
741 (ISALIGNED_cmsghdr(cmsg) && \
742 ((uintptr_t)(cmsg) >= (uintptr_t)(start)) && \
743 ((uintptr_t)(cmsg) < (uintptr_t)(end)) && \
744 ((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) && \
745 ((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end)))
748 * Maximum size of any argument that is copied in (addresses, options,
749 * access rights). MUST be at least MAXPATHLEN + 3.
750 * BSD and SunOS 4.X limited this to MLEN or MCLBYTES.
752 #define SO_MAXARGSIZE 8192
755 * Convert between vnode and sonode
757 #define VTOSO(vp) ((struct sonode *)((vp)->v_data))
758 #define SOTOV(sp) ((sp)->so_vnode)
761 * Internal flags for sobind()
763 #define _SOBIND_REBIND 0x01 /* Bind to existing local address */
764 #define _SOBIND_UNSPEC 0x02 /* Bind to unspecified address */
765 #define _SOBIND_LOCK_HELD 0x04 /* so_excl_lock held by caller */
766 #define _SOBIND_NOXLATE 0x08 /* No addr translation for AF_UNIX */
767 #define _SOBIND_LISTEN 0x40 /* Make into SS_ACCEPTCONN */
768 #define _SOBIND_SOCKETPAIR 0x80 /* Internal flag for so_socketpair() */
769 /* to enable listen with backlog = 1 */
772 * Internal flags for sounbind()
774 #define _SOUNBIND_REBIND 0x01 /* Don't clear fields - will rebind */
777 * Internal flags for soconnect()
779 #define _SOCONNECT_NOXLATE 0x01 /* No addr translation for AF_UNIX */
780 #define _SOCONNECT_DID_BIND 0x02 /* Unbind when connect fails */
783 * Internal flags for sodisconnect()
785 #define _SODISCONNECT_LOCK_HELD 0x01 /* so_excl_lock held by caller */
788 * Internal flags for soallocproto*()
790 #define _ALLOC_NOSLEEP 0 /* Don't sleep for memory */
791 #define _ALLOC_INTR 1 /* Sleep until interrupt */
792 #define _ALLOC_SLEEP 2 /* Sleep forever */
795 * Internal structure for handling AF_UNIX file descriptor passing
797 struct fdbuf {
798 int fd_size; /* In bytes, for kmem_free */
799 int fd_numfd; /* Number of elements below */
800 char *fd_ebuf; /* Extra buffer to free */
801 int fd_ebuflen;
802 frtn_t fd_frtn;
803 struct file *fd_fds[1]; /* One or more */
805 #define FDBUF_HDRSIZE (sizeof (struct fdbuf) - sizeof (struct file *))
808 * Variable that can be patched to set what version of socket socket()
809 * will create.
811 extern int so_default_version;
813 #ifdef DEBUG
814 /* Turn on extra testing capabilities */
815 #define SOCK_TEST
816 #endif /* DEBUG */
818 #ifdef DEBUG
819 char *pr_state(uint_t, uint_t);
820 char *pr_addr(int, struct sockaddr *, t_uscalar_t);
821 int so_verify_oobstate(struct sonode *);
822 #endif /* DEBUG */
825 * DEBUG macros
827 #if defined(DEBUG)
828 #define SOCK_DEBUG
830 extern int sockdebug;
831 extern int sockprinterr;
833 #define eprint(args) printf args
834 #define eprintso(so, args) \
835 { if (sockprinterr && ((so)->so_options & SO_DEBUG)) printf args; }
836 #define eprintline(error) \
838 if (error != EINTR && (sockprinterr || sockdebug > 0)) \
839 printf("socket error %d: line %d file %s\n", \
840 (error), __LINE__, __FILE__); \
843 #define eprintsoline(so, error) \
844 { if (sockprinterr && ((so)->so_options & SO_DEBUG)) \
845 printf("socket(%p) error %d: line %d file %s\n", \
846 (void *)(so), (error), __LINE__, __FILE__); \
848 #define dprint(level, args) { if (sockdebug > (level)) printf args; }
849 #define dprintso(so, level, args) \
850 { if (sockdebug > (level) && ((so)->so_options & SO_DEBUG)) printf args; }
852 #else /* define(DEBUG) */
854 #define eprint(args) {}
855 #define eprintso(so, args) {}
856 #define eprintline(error) {}
857 #define eprintsoline(so, error) {}
858 #define dprint(level, args) {}
859 #define dprintso(so, level, args) {}
861 #endif /* defined(DEBUG) */
863 extern const struct vnodeops socket_vnodeops;
865 extern dev_t sockdev;
867 extern krwlock_t sockconf_lock;
870 * sockfs functions
872 extern int sock_getmsg(vnode_t *, struct strbuf *, struct strbuf *,
873 uchar_t *, int *, int, rval_t *);
874 extern int sock_putmsg(vnode_t *, struct strbuf *, struct strbuf *,
875 uchar_t, int, int);
876 extern int sogetvp(char *, vnode_t **, int);
877 extern int sockinit(int, char *);
878 extern int solookup(int, int, int, struct sockparams **);
879 extern void so_lock_single(struct sonode *);
880 extern void so_unlock_single(struct sonode *, int);
881 extern int so_lock_read(struct sonode *, int);
882 extern int so_lock_read_intr(struct sonode *, int);
883 extern void so_unlock_read(struct sonode *);
884 extern void *sogetoff(mblk_t *, t_uscalar_t, t_uscalar_t, uint_t);
885 extern void so_getopt_srcaddr(void *, t_uscalar_t,
886 void **, t_uscalar_t *);
887 extern int so_getopt_unix_close(void *, t_uscalar_t);
888 extern void fdbuf_free(struct fdbuf *);
889 extern mblk_t *fdbuf_allocmsg(int, struct fdbuf *);
890 extern int fdbuf_create(void *, int, struct fdbuf **);
891 extern void so_closefds(void *, t_uscalar_t, int);
892 extern int so_getfdopt(void *, t_uscalar_t, void **, int *);
893 t_uscalar_t so_optlen(void *, t_uscalar_t);
894 extern void so_cmsg2opt(void *, t_uscalar_t, mblk_t *);
895 extern t_uscalar_t
896 so_cmsglen(mblk_t *, void *, t_uscalar_t);
897 extern int so_opt2cmsg(mblk_t *, void *, t_uscalar_t,
898 void *, t_uscalar_t);
899 extern void soisconnecting(struct sonode *);
900 extern void soisconnected(struct sonode *);
901 extern void soisdisconnected(struct sonode *, int);
902 extern void socantsendmore(struct sonode *);
903 extern void socantrcvmore(struct sonode *);
904 extern void soseterror(struct sonode *, int);
905 extern int sogeterr(struct sonode *, boolean_t);
906 extern int sowaitconnected(struct sonode *, int, int);
908 extern ssize_t soreadfile(file_t *, uchar_t *, uoff_t, int *, size_t);
909 extern void *sock_kstat_init(zoneid_t);
910 extern void sock_kstat_fini(zoneid_t, void *);
911 extern struct sonode *getsonode(int, int *, file_t **);
913 * Function wrappers (mostly around the sonode switch) for
914 * backward compatibility.
916 extern int soaccept(struct sonode *, int, struct sonode **);
917 extern int sobind(struct sonode *, struct sockaddr *, socklen_t,
918 int, int);
919 extern int solisten(struct sonode *, int);
920 extern int soconnect(struct sonode *, struct sockaddr *, socklen_t,
921 int, int);
922 extern int sorecvmsg(struct sonode *, struct msghdr *, struct uio *);
923 extern int sosendmsg(struct sonode *, struct msghdr *, struct uio *);
924 extern int soshutdown(struct sonode *, int);
925 extern int sogetsockopt(struct sonode *, int, int, void *, socklen_t *,
926 int);
927 extern int sosetsockopt(struct sonode *, int, int, const void *,
928 t_uscalar_t);
930 extern struct sonode *socreate(struct sockparams *, int, int, int, int *);
932 extern int so_copyin(const void *, void *, size_t, int);
933 extern int so_copyout(const void *, void *, size_t, int);
935 #endif
938 * Internal structure for obtaining sonode information from the socklist.
939 * These types match those corresponding in the sonode structure.
940 * This is not a published interface, and may change at any time.
942 struct sockinfo {
943 uint_t si_size; /* real length of this struct */
944 short si_family;
945 short si_type;
946 ushort_t si_flag;
947 uint_t si_state;
948 uint_t si_ux_laddr_sou_magic;
949 uint_t si_ux_faddr_sou_magic;
950 t_scalar_t si_serv_type;
951 t_uscalar_t si_laddr_soa_len;
952 t_uscalar_t si_faddr_soa_len;
953 uint16_t si_laddr_family;
954 uint16_t si_faddr_family;
955 char si_laddr_sun_path[MAXPATHLEN + 1]; /* NULL terminated */
956 char si_faddr_sun_path[MAXPATHLEN + 1];
957 boolean_t si_faddr_noxlate;
958 zoneid_t si_szoneid;
962 * Subcodes for sockconf() system call
964 #define SOCKCONFIG_ADD_SOCK 0
965 #define SOCKCONFIG_REMOVE_SOCK 1
966 #define SOCKCONFIG_ADD_FILTER 2
967 #define SOCKCONFIG_REMOVE_FILTER 3
968 #define SOCKCONFIG_GET_SOCKTABLE 4
971 * Data structures for configuring socket filters.
975 * Placement hint for automatic filters
977 typedef enum {
978 SOF_HINT_NONE,
979 SOF_HINT_TOP,
980 SOF_HINT_BOTTOM,
981 SOF_HINT_BEFORE,
982 SOF_HINT_AFTER
983 } sof_hint_t;
986 * Socket tuple. Used by sockconfig_filter_props to list socket
987 * types of interest.
989 typedef struct sof_socktuple {
990 int sofst_family;
991 int sofst_type;
992 int sofst_protocol;
993 } sof_socktuple_t;
996 * Socket filter properties used by sockconfig() system call.
998 struct sockconfig_filter_props {
999 char *sfp_modname;
1000 boolean_t sfp_autoattach;
1001 sof_hint_t sfp_hint;
1002 char *sfp_hintarg;
1003 uint_t sfp_socktuple_cnt;
1004 sof_socktuple_t *sfp_socktuple;
1008 * Data structures for the in-kernel socket configuration table.
1010 typedef struct sockconfig_socktable_entry {
1011 int se_family;
1012 int se_type;
1013 int se_protocol;
1014 int se_refcnt;
1015 int se_flags;
1016 char se_modname[MODMAXNAMELEN];
1017 char se_strdev[MAXPATHLEN];
1018 } sockconfig_socktable_entry_t;
1020 typedef struct sockconfig_socktable {
1021 uint_t num_of_entries;
1022 sockconfig_socktable_entry_t *st_entries;
1023 } sockconfig_socktable_t;
1025 #ifdef _SYSCALL32
1027 typedef struct sof_socktuple32 {
1028 int32_t sofst_family;
1029 int32_t sofst_type;
1030 int32_t sofst_protocol;
1031 } sof_socktuple32_t;
1033 struct sockconfig_filter_props32 {
1034 caddr32_t sfp_modname;
1035 boolean_t sfp_autoattach;
1036 sof_hint_t sfp_hint;
1037 caddr32_t sfp_hintarg;
1038 uint32_t sfp_socktuple_cnt;
1039 caddr32_t sfp_socktuple;
1042 typedef struct sockconfig_socktable32 {
1043 uint_t num_of_entries;
1044 caddr32_t st_entries;
1045 } sockconfig_socktable32_t;
1047 #endif /* _SYSCALL32 */
1049 #define SOCKMOD_PATH "socketmod" /* dir where sockmods are stored */
1051 #ifdef __cplusplus
1053 #endif
1055 #endif /* _SYS_SOCKETVAR_H */