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 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 40 * Copyright 2020 OmniOS Community Edition (OmniOSce) Association. 41 */ 42 43 #ifndef _SYS_SOCKETVAR_H 44 #define _SYS_SOCKETVAR_H 45 46 #include <sys/types.h> 47 #include <sys/stream.h> 48 #include <sys/t_lock.h> 49 #include <sys/cred.h> 50 #include <sys/vnode.h> 51 #include <sys/file.h> 52 #include <sys/param.h> 53 #include <sys/zone.h> 54 #include <sys/sdt.h> 55 #include <sys/modctl.h> 56 #include <sys/atomic.h> 57 #include <sys/socket.h> 58 #include <sys/ksocket.h> 59 #include <sys/kstat.h> 60 61 #ifdef _KERNEL 62 #include <sys/vfs_opreg.h> 63 #endif 64 65 #ifdef __cplusplus 66 extern "C" { 67 #endif 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 #if defined(_KERNEL) || defined(_KMEMUSER) 101 102 #include <sys/socket_proto.h> 103 104 typedef struct sonodeops sonodeops_t; 105 typedef struct sonode sonode_t; 106 107 struct sodirect_s; 108 109 /* 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. 113 * 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. 125 * 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. 129 * 130 * Note that so_lock is sometimes held across calls that might go to sleep 131 * (kmem_alloc and soallocproto*). This implies that no other lock in 132 * the system should be held when calling into sockfs; from the system call 133 * side or from strrput (in case of TPI based sockets). If locks are held 134 * while calling into sockfs the system might hang when running low on memory. 135 */ 136 struct sonode { 137 struct vnode *so_vnode; /* vnode associated with this sonode */ 138 139 sonodeops_t *so_ops; /* operations vector for this sonode */ 140 void *so_priv; /* sonode private data */ 141 142 krwlock_t so_fallback_rwlock; 143 kmutex_t so_lock; /* protects sonode fields */ 144 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 */ 148 149 /* These fields are protected by so_lock */ 150 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 */ 155 156 sock_connid_t so_proto_connid; /* protocol generation number */ 157 158 ushort_t so_error; /* error affecting connection */ 159 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; 165 short so_version; /* From so_socket call */ 166 167 /* Accept queue */ 168 kmutex_t so_acceptq_lock; /* protects accept queue */ 169 list_t so_acceptq_list; /* pending conns */ 170 list_t so_acceptq_defer; /* deferred conns */ 171 list_node_t so_acceptq_node; /* acceptq list node */ 172 unsigned int so_acceptq_len; /* # of conns (both lists) */ 173 unsigned int so_backlog; /* Listen backlog */ 174 kcondvar_t so_acceptq_cv; /* wait for new conn. */ 175 struct sonode *so_listener; /* parent socket */ 176 177 /* Options */ 178 short so_options; /* From socket call, see socket.h */ 179 struct linger so_linger; /* SO_LINGER value */ 180 #define so_sndbuf so_proto_props.sopp_txhiwat /* SO_SNDBUF value */ 181 #define so_sndlowat so_proto_props.sopp_txlowat /* tx low water mark */ 182 #define so_rcvbuf so_proto_props.sopp_rxhiwat /* SO_RCVBUF value */ 183 #define so_rcvlowat so_proto_props.sopp_rxlowat /* rx low water mark */ 184 #define so_max_addr_len so_proto_props.sopp_maxaddrlen 185 #define so_minpsz so_proto_props.sopp_minpsz 186 #define so_maxpsz so_proto_props.sopp_maxpsz 187 188 int so_xpg_rcvbuf; /* SO_RCVBUF value for XPG4 socket */ 189 clock_t so_sndtimeo; /* send timeout */ 190 clock_t so_rcvtimeo; /* recv timeout */ 191 192 mblk_t *so_oobmsg; /* outofline oob data */ 193 ssize_t so_oobmark; /* offset of the oob data */ 194 195 pid_t so_pgrp; /* pgrp for signals */ 196 197 cred_t *so_peercred; /* connected socket peer cred */ 198 pid_t so_cpid; /* connected socket peer cached pid */ 199 zoneid_t so_zoneid; /* opener's zoneid */ 200 201 struct pollhead so_poll_list; /* common pollhead */ 202 short so_pollev; /* events that should be generated */ 203 204 /* Receive */ 205 unsigned int so_rcv_queued; /* # bytes on both rcv lists */ 206 mblk_t *so_rcv_q_head; /* processing/copyout rcv queue */ 207 mblk_t *so_rcv_q_last_head; 208 mblk_t *so_rcv_head; /* protocol prequeue */ 209 mblk_t *so_rcv_last_head; /* last mblk in b_next chain */ 210 kcondvar_t so_rcv_cv; /* wait for data */ 211 uint_t so_rcv_wanted; /* # of bytes wanted by app */ 212 timeout_id_t so_rcv_timer_tid; 213 214 #define so_rcv_thresh so_proto_props.sopp_rcvthresh 215 #define so_rcv_timer_interval so_proto_props.sopp_rcvtimer 216 217 kcondvar_t so_snd_cv; /* wait for snd buffers */ 218 uint32_t 219 so_snd_qfull: 1, /* Transmit full */ 220 so_rcv_wakeup: 1, 221 so_snd_wakeup: 1, 222 so_not_str: 1, /* B_TRUE if not streams based socket */ 223 so_pad_to_bit_31: 28; 224 225 /* Communication channel with protocol */ 226 sock_lower_handle_t so_proto_handle; 227 sock_downcalls_t *so_downcalls; 228 229 struct sock_proto_props so_proto_props; /* protocol settings */ 230 boolean_t so_flowctrld; /* Flow controlled */ 231 uint_t so_copyflag; /* Copy related flag */ 232 kcondvar_t so_copy_cv; /* Copy cond variable */ 233 234 /* kernel sockets */ 235 ksocket_callbacks_t so_ksock_callbacks; 236 void *so_ksock_cb_arg; /* callback argument */ 237 kcondvar_t so_closing_cv; 238 239 /* != NULL for sodirect enabled socket */ 240 struct sodirect_s *so_direct; 241 242 /* socket filters */ 243 uint_t so_filter_active; /* # of active fil */ 244 uint_t so_filter_tx; /* pending tx ops */ 245 struct sof_instance *so_filter_top; /* top of stack */ 246 struct sof_instance *so_filter_bottom; /* bottom of stack */ 247 clock_t so_filter_defertime; /* time when deferred */ 248 }; 249 250 #define SO_HAVE_DATA(so) \ 251 /* \ 252 * For the (tid == 0) case we must check so_rcv_{q_,}head \ 253 * rather than (so_rcv_queued > 0), since the latter does not \ 254 * take into account mblks with only control/name information. \ 255 */ \ 256 ((so)->so_rcv_timer_tid == 0 && ((so)->so_rcv_head != NULL || \ 257 (so)->so_rcv_q_head != NULL)) || \ 258 ((so)->so_state & SS_CANTRCVMORE) 259 260 /* 261 * Events handled by the protocol (in case sd_poll is set) 262 */ 263 #define SO_PROTO_POLLEV (POLLIN|POLLRDNORM|POLLRDBAND) 264 265 266 #endif /* _KERNEL || _KMEMUSER */ 267 268 /* flags */ 269 #define SOMOD 0x0001 /* update socket modification time */ 270 #define SOACC 0x0002 /* update socket access time */ 271 272 #define SOLOCKED 0x0010 /* use to serialize open/closes */ 273 #define SOREADLOCKED 0x0020 /* serialize kstrgetmsg calls */ 274 #define SOCLONE 0x0040 /* child of clone driver */ 275 #define SOASYNC_UNBIND 0x0080 /* wait for ACK of async unbind */ 276 277 #define SOCK_IS_NONSTR(so) ((so)->so_not_str) 278 279 /* 280 * Socket state bits. 281 */ 282 #define SS_ISCONNECTED 0x00000001 /* socket connected to a peer */ 283 #define SS_ISCONNECTING 0x00000002 /* in process, connecting to peer */ 284 #define SS_ISDISCONNECTING 0x00000004 /* in process of disconnecting */ 285 #define SS_CANTSENDMORE 0x00000008 /* can't send more data to peer */ 286 287 #define SS_CANTRCVMORE 0x00000010 /* can't receive more data */ 288 #define SS_ISBOUND 0x00000020 /* socket is bound */ 289 #define SS_NDELAY 0x00000040 /* FNDELAY non-blocking */ 290 #define SS_NONBLOCK 0x00000080 /* O_NONBLOCK non-blocking */ 291 292 #define SS_ASYNC 0x00000100 /* async i/o notify */ 293 #define SS_ACCEPTCONN 0x00000200 /* listen done */ 294 /* unused 0x00000400 */ /* was SS_HASCONNIND */ 295 #define SS_SAVEDEOR 0x00000800 /* Saved MSG_EOR rcv side state */ 296 297 #define SS_RCVATMARK 0x00001000 /* at mark on input */ 298 #define SS_OOBPEND 0x00002000 /* OOB pending or present - poll */ 299 #define SS_HAVEOOBDATA 0x00004000 /* OOB data present */ 300 #define SS_HADOOBDATA 0x00008000 /* OOB data consumed */ 301 #define SS_CLOSING 0x00010000 /* in process of closing */ 302 303 #define SS_FIL_DEFER 0x00020000 /* filter deferred notification */ 304 #define SS_FILOP_OK 0x00040000 /* socket can attach filters */ 305 #define SS_FIL_RCV_FLOWCTRL 0x00080000 /* filter asserted rcv flow ctrl */ 306 #define SS_FIL_SND_FLOWCTRL 0x00100000 /* filter asserted snd flow ctrl */ 307 #define SS_FIL_STOP 0x00200000 /* no more filter actions */ 308 309 #define SS_SODIRECT 0x00400000 /* transport supports sodirect */ 310 311 #define SS_SENTLASTREADSIG 0x01000000 /* last rx signal has been sent */ 312 #define SS_SENTLASTWRITESIG 0x02000000 /* last tx signal has been sent */ 313 314 #define SS_FALLBACK_DRAIN 0x20000000 /* data was/is being drained */ 315 #define SS_FALLBACK_PENDING 0x40000000 /* fallback is pending */ 316 #define SS_FALLBACK_COMP 0x80000000 /* fallback has completed */ 317 318 319 /* Set of states when the socket can't be rebound */ 320 #define SS_CANTREBIND (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING|\ 321 SS_CANTSENDMORE|SS_CANTRCVMORE|SS_ACCEPTCONN) 322 323 /* 324 * Sockets that can fall back to TPI must ensure that fall back is not 325 * initiated while a thread is using a socket. 326 */ 327 #define SO_BLOCK_FALLBACK(so, fn) \ 328 ASSERT(MUTEX_NOT_HELD(&(so)->so_lock)); \ 329 rw_enter(&(so)->so_fallback_rwlock, RW_READER); \ 330 if ((so)->so_state & (SS_FALLBACK_COMP|SS_FILOP_OK)) { \ 331 if ((so)->so_state & SS_FALLBACK_COMP) { \ 332 rw_exit(&(so)->so_fallback_rwlock); \ 333 return (fn); \ 334 } else { \ 335 mutex_enter(&(so)->so_lock); \ 336 (so)->so_state &= ~SS_FILOP_OK; \ 337 mutex_exit(&(so)->so_lock); \ 338 } \ 339 } 340 341 #define SO_UNBLOCK_FALLBACK(so) { \ 342 rw_exit(&(so)->so_fallback_rwlock); \ 343 } 344 345 #define SO_SND_FLOWCTRLD(so) \ 346 ((so)->so_snd_qfull || (so)->so_state & SS_FIL_SND_FLOWCTRL) 347 348 /* Poll events */ 349 #define SO_POLLEV_IN 0x1 /* POLLIN wakeup needed */ 350 #define SO_POLLEV_ALWAYS 0x2 /* wakeups */ 351 352 /* 353 * Characteristics of sockets. Not changed after the socket is created. 354 */ 355 #define SM_PRIV 0x001 /* privileged for broadcast, raw... */ 356 #define SM_ATOMIC 0x002 /* atomic data transmission */ 357 #define SM_ADDR 0x004 /* addresses given with messages */ 358 #define SM_CONNREQUIRED 0x008 /* connection required by protocol */ 359 360 #define SM_FDPASSING 0x010 /* passes file descriptors */ 361 #define SM_EXDATA 0x020 /* Can handle T_EXDATA_REQ */ 362 #define SM_OPTDATA 0x040 /* Can handle T_OPTDATA_REQ */ 363 #define SM_BYTESTREAM 0x080 /* Byte stream - can use M_DATA */ 364 365 #define SM_ACCEPTOR_ID 0x100 /* so_acceptor_id is valid */ 366 367 #define SM_KERNEL 0x200 /* kernel socket */ 368 369 /* The modes below are only for non-streams sockets */ 370 #define SM_ACCEPTSUPP 0x400 /* can handle accept() */ 371 #define SM_SENDFILESUPP 0x800 /* Private: proto supp sendfile */ 372 373 /* 374 * Socket versions. Used by the socket library when calling _so_socket(). 375 */ 376 #define SOV_STREAM 0 /* Not a socket - just a stream */ 377 #define SOV_DEFAULT 1 /* Select based on so_default_version */ 378 #define SOV_SOCKSTREAM 2 /* Socket plus streams operations */ 379 #define SOV_SOCKBSD 3 /* Socket with no streams operations */ 380 #define SOV_XPG4_2 4 /* Xnet socket */ 381 382 #if defined(_KERNEL) || defined(_KMEMUSER) 383 384 /* 385 * sonode create and destroy functions. 386 */ 387 typedef struct sonode *(*so_create_func_t)(struct sockparams *, 388 int, int, int, int, int, int *, cred_t *); 389 typedef void (*so_destroy_func_t)(struct sonode *); 390 391 /* STREAM device information */ 392 typedef struct sdev_info { 393 char *sd_devpath; 394 int sd_devpathlen; /* Is 0 if sp_devpath is a static string */ 395 vnode_t *sd_vnode; 396 } sdev_info_t; 397 398 #define SOCKMOD_VERSION_1 1 399 #define SOCKMOD_VERSION 2 400 401 /* name of the TPI pseudo socket module */ 402 #define SOTPI_SMOD_NAME "socktpi" 403 404 typedef struct __smod_priv_s { 405 so_create_func_t smodp_sock_create_func; 406 so_destroy_func_t smodp_sock_destroy_func; 407 so_proto_fallback_func_t smodp_proto_fallback_func; 408 const char *smodp_fallback_devpath_v4; 409 const char *smodp_fallback_devpath_v6; 410 } __smod_priv_t; 411 412 /* 413 * Socket module register information 414 */ 415 typedef struct smod_reg_s { 416 int smod_version; 417 char *smod_name; 418 size_t smod_uc_version; 419 size_t smod_dc_version; 420 so_proto_create_func_t smod_proto_create_func; 421 422 /* __smod_priv_data must be NULL */ 423 __smod_priv_t *__smod_priv; 424 } smod_reg_t; 425 426 /* 427 * Socket module information 428 */ 429 typedef struct smod_info { 430 int smod_version; 431 char *smod_name; 432 uint_t smod_refcnt; /* # of entries */ 433 size_t smod_uc_version; /* upcall version */ 434 size_t smod_dc_version; /* down call version */ 435 so_proto_create_func_t smod_proto_create_func; 436 so_proto_fallback_func_t smod_proto_fallback_func; 437 const char *smod_fallback_devpath_v4; 438 const char *smod_fallback_devpath_v6; 439 so_create_func_t smod_sock_create_func; 440 so_destroy_func_t smod_sock_destroy_func; 441 list_node_t smod_node; 442 } smod_info_t; 443 444 typedef struct sockparams_stats { 445 kstat_named_t sps_nfallback; /* # of fallbacks to TPI */ 446 kstat_named_t sps_nactive; /* # of active sockets */ 447 kstat_named_t sps_ncreate; /* total # of created sockets */ 448 } sockparams_stats_t; 449 450 /* 451 * sockparams 452 * 453 * Used for mapping family/type/protocol to a socket module or STREAMS device 454 */ 455 struct sockparams { 456 /* 457 * The family, type, protocol, sdev_info and smod_name are 458 * set when the entry is created, and they will never change 459 * thereafter. 460 */ 461 int sp_family; 462 int sp_type; 463 int sp_protocol; 464 465 sdev_info_t sp_sdev_info; /* STREAM device */ 466 char *sp_smod_name; /* socket module name */ 467 468 kmutex_t sp_lock; /* lock for refcnt and smod_info */ 469 uint64_t sp_refcnt; /* entry reference count */ 470 smod_info_t *sp_smod_info; /* socket module */ 471 472 sockparams_stats_t sp_stats; 473 kstat_t *sp_kstat; 474 475 /* 476 * The entries below are only modified while holding 477 * sockconf_lock as a writer. 478 */ 479 int sp_flags; /* see below */ 480 list_node_t sp_node; 481 482 list_t sp_auto_filters; /* list of automatic filters */ 483 list_t sp_prog_filters; /* list of programmatic filters */ 484 }; 485 486 struct sof_entry; 487 488 typedef struct sp_filter { 489 struct sof_entry *spf_filter; 490 list_node_t spf_node; 491 } sp_filter_t; 492 493 494 /* 495 * sockparams flags 496 */ 497 #define SOCKPARAMS_EPHEMERAL 0x1 /* temp. entry, not on global list */ 498 499 extern void sockparams_init(void); 500 extern struct sockparams *sockparams_hold_ephemeral_bydev(int, int, int, 501 const char *, int, int *); 502 extern struct sockparams *sockparams_hold_ephemeral_bymod(int, int, int, 503 const char *, int, int *); 504 extern void sockparams_ephemeral_drop_last_ref(struct sockparams *); 505 506 extern struct sockparams *sockparams_create(int, int, int, char *, char *, int, 507 int, int, int *); 508 extern void sockparams_destroy(struct sockparams *); 509 extern int sockparams_add(struct sockparams *); 510 extern int sockparams_delete(int, int, int); 511 extern int sockparams_new_filter(struct sof_entry *); 512 extern void sockparams_filter_cleanup(struct sof_entry *); 513 extern int sockparams_copyout_socktable(uintptr_t); 514 515 extern void smod_init(void); 516 extern void smod_add(smod_info_t *); 517 extern int smod_register(const smod_reg_t *); 518 extern int smod_unregister(const char *); 519 extern smod_info_t *smod_lookup_byname(const char *); 520 521 #define SOCKPARAMS_HAS_DEVICE(sp) \ 522 ((sp)->sp_sdev_info.sd_devpath != NULL) 523 524 /* Increase the smod_info_t reference count */ 525 #define SMOD_INC_REF(smodp) { \ 526 ASSERT((smodp) != NULL); \ 527 DTRACE_PROBE1(smodinfo__inc__ref, struct smod_info *, (smodp)); \ 528 atomic_inc_uint(&(smodp)->smod_refcnt); \ 529 } 530 531 /* 532 * Decreace the socket module entry reference count. 533 * When no one mapping to the entry, we try to unload the module from the 534 * kernel. If the module can't unload, just leave the module entry with 535 * a zero refcnt. 536 */ 537 #define SMOD_DEC_REF(smodp, modname) { \ 538 ASSERT((smodp) != NULL); \ 539 ASSERT((smodp)->smod_refcnt != 0); \ 540 atomic_dec_uint(&(smodp)->smod_refcnt); \ 541 /* \ 542 * No need to atomically check the return value because the \ 543 * socket module framework will verify that no one is using \ 544 * the module before unloading. Worst thing that can happen \ 545 * here is multiple calls to mod_remove_by_name(), which is OK. \ 546 */ \ 547 if ((smodp)->smod_refcnt == 0) \ 548 (void) mod_remove_by_name(modname); \ 549 } 550 551 /* Increase the reference count */ 552 #define SOCKPARAMS_INC_REF(sp) { \ 553 ASSERT((sp) != NULL); \ 554 DTRACE_PROBE1(sockparams__inc__ref, struct sockparams *, (sp)); \ 555 mutex_enter(&(sp)->sp_lock); \ 556 (sp)->sp_refcnt++; \ 557 ASSERT((sp)->sp_refcnt != 0); \ 558 mutex_exit(&(sp)->sp_lock); \ 559 } 560 561 /* 562 * Decrease the reference count. 563 * 564 * If the sockparams is ephemeral, then the thread dropping the last ref 565 * count will destroy the entry. 566 */ 567 #define SOCKPARAMS_DEC_REF(sp) { \ 568 ASSERT((sp) != NULL); \ 569 DTRACE_PROBE1(sockparams__dec__ref, struct sockparams *, (sp)); \ 570 mutex_enter(&(sp)->sp_lock); \ 571 ASSERT((sp)->sp_refcnt > 0); \ 572 if ((sp)->sp_refcnt == 1) { \ 573 if ((sp)->sp_flags & SOCKPARAMS_EPHEMERAL) { \ 574 mutex_exit(&(sp)->sp_lock); \ 575 sockparams_ephemeral_drop_last_ref((sp)); \ 576 } else { \ 577 (sp)->sp_refcnt--; \ 578 if ((sp)->sp_smod_info != NULL) { \ 579 SMOD_DEC_REF((sp)->sp_smod_info, \ 580 (sp)->sp_smod_name); \ 581 } \ 582 (sp)->sp_smod_info = NULL; \ 583 mutex_exit(&(sp)->sp_lock); \ 584 } \ 585 } else { \ 586 (sp)->sp_refcnt--; \ 587 mutex_exit(&(sp)->sp_lock); \ 588 } \ 589 } 590 591 /* 592 * Used to traverse the list of AF_UNIX sockets to construct the kstat 593 * for netstat(1m). 594 */ 595 struct socklist { 596 kmutex_t sl_lock; 597 struct sonode *sl_list; 598 }; 599 600 extern struct socklist socklist; 601 /* 602 * ss_full_waits is the number of times the reader thread 603 * waits when the queue is full and ss_empty_waits is the number 604 * of times the consumer thread waits when the queue is empty. 605 * No locks for these as they are just indicators of whether 606 * disk or network or both is slow or fast. 607 */ 608 struct sendfile_stats { 609 uint32_t ss_file_cached; 610 uint32_t ss_file_not_cached; 611 uint32_t ss_full_waits; 612 uint32_t ss_empty_waits; 613 uint32_t ss_file_segmap; 614 }; 615 616 /* 617 * A single sendfile request is represented by snf_req. 618 */ 619 typedef struct snf_req { 620 struct snf_req *sr_next; 621 mblk_t *sr_mp_head; 622 mblk_t *sr_mp_tail; 623 kmutex_t sr_lock; 624 kcondvar_t sr_cv; 625 uint_t sr_qlen; 626 int sr_hiwat; 627 int sr_lowat; 628 int sr_operation; 629 struct vnode *sr_vp; 630 file_t *sr_fp; 631 ssize_t sr_maxpsz; 632 u_offset_t sr_file_off; 633 u_offset_t sr_file_size; 634 #define SR_READ_DONE 0x80000000 635 int sr_read_error; 636 int sr_write_error; 637 } snf_req_t; 638 639 /* A queue of sendfile requests */ 640 struct sendfile_queue { 641 snf_req_t *snfq_req_head; 642 snf_req_t *snfq_req_tail; 643 kmutex_t snfq_lock; 644 kcondvar_t snfq_cv; 645 int snfq_svc_threads; /* # of service threads */ 646 int snfq_idle_cnt; /* # of idling threads */ 647 int snfq_max_threads; 648 int snfq_req_cnt; /* Number of requests */ 649 }; 650 651 #define READ_OP 1 652 #define SNFQ_TIMEOUT (60 * 5 * hz) /* 5 minutes */ 653 654 /* Socket network operations switch */ 655 struct sonodeops { 656 int (*sop_init)(struct sonode *, struct sonode *, cred_t *, 657 int); 658 int (*sop_accept)(struct sonode *, int, cred_t *, struct sonode **); 659 int (*sop_bind)(struct sonode *, struct sockaddr *, socklen_t, 660 int, cred_t *); 661 int (*sop_listen)(struct sonode *, int, cred_t *); 662 int (*sop_connect)(struct sonode *, struct sockaddr *, 663 socklen_t, int, int, cred_t *); 664 int (*sop_recvmsg)(struct sonode *, struct msghdr *, 665 struct uio *, cred_t *); 666 int (*sop_sendmsg)(struct sonode *, struct msghdr *, 667 struct uio *, cred_t *); 668 int (*sop_sendmblk)(struct sonode *, struct msghdr *, int, 669 cred_t *, mblk_t **); 670 int (*sop_getpeername)(struct sonode *, struct sockaddr *, 671 socklen_t *, boolean_t, cred_t *); 672 int (*sop_getsockname)(struct sonode *, struct sockaddr *, 673 socklen_t *, cred_t *); 674 int (*sop_shutdown)(struct sonode *, int, cred_t *); 675 int (*sop_getsockopt)(struct sonode *, int, int, void *, 676 socklen_t *, int, cred_t *); 677 int (*sop_setsockopt)(struct sonode *, int, int, const void *, 678 socklen_t, cred_t *); 679 int (*sop_ioctl)(struct sonode *, int, intptr_t, int, 680 cred_t *, int32_t *); 681 int (*sop_poll)(struct sonode *, short, int, short *, 682 struct pollhead **); 683 int (*sop_close)(struct sonode *, int, cred_t *); 684 }; 685 686 #define SOP_INIT(so, flag, cr, flags) \ 687 ((so)->so_ops->sop_init((so), (flag), (cr), (flags))) 688 #define SOP_ACCEPT(so, fflag, cr, nsop) \ 689 ((so)->so_ops->sop_accept((so), (fflag), (cr), (nsop))) 690 #define SOP_BIND(so, name, namelen, flags, cr) \ 691 ((so)->so_ops->sop_bind((so), (name), (namelen), (flags), (cr))) 692 #define SOP_LISTEN(so, backlog, cr) \ 693 ((so)->so_ops->sop_listen((so), (backlog), (cr))) 694 #define SOP_CONNECT(so, name, namelen, fflag, flags, cr) \ 695 ((so)->so_ops->sop_connect((so), (name), (namelen), (fflag), (flags), \ 696 (cr))) 697 #define SOP_RECVMSG(so, msg, uiop, cr) \ 698 ((so)->so_ops->sop_recvmsg((so), (msg), (uiop), (cr))) 699 #define SOP_SENDMSG(so, msg, uiop, cr) \ 700 ((so)->so_ops->sop_sendmsg((so), (msg), (uiop), (cr))) 701 #define SOP_SENDMBLK(so, msg, size, cr, mpp) \ 702 ((so)->so_ops->sop_sendmblk((so), (msg), (size), (cr), (mpp))) 703 #define SOP_GETPEERNAME(so, addr, addrlen, accept, cr) \ 704 ((so)->so_ops->sop_getpeername((so), (addr), (addrlen), (accept), (cr))) 705 #define SOP_GETSOCKNAME(so, addr, addrlen, cr) \ 706 ((so)->so_ops->sop_getsockname((so), (addr), (addrlen), (cr))) 707 #define SOP_SHUTDOWN(so, how, cr) \ 708 ((so)->so_ops->sop_shutdown((so), (how), (cr))) 709 #define SOP_GETSOCKOPT(so, level, optionname, optval, optlenp, flags, cr) \ 710 ((so)->so_ops->sop_getsockopt((so), (level), (optionname), \ 711 (optval), (optlenp), (flags), (cr))) 712 #define SOP_SETSOCKOPT(so, level, optionname, optval, optlen, cr) \ 713 ((so)->so_ops->sop_setsockopt((so), (level), (optionname), \ 714 (optval), (optlen), (cr))) 715 #define SOP_IOCTL(so, cmd, arg, mode, cr, rvalp) \ 716 ((so)->so_ops->sop_ioctl((so), (cmd), (arg), (mode), (cr), (rvalp))) 717 #define SOP_POLL(so, events, anyyet, reventsp, phpp) \ 718 ((so)->so_ops->sop_poll((so), (events), (anyyet), (reventsp), (phpp))) 719 #define SOP_CLOSE(so, flag, cr) \ 720 ((so)->so_ops->sop_close((so), (flag), (cr))) 721 722 #endif /* defined(_KERNEL) || defined(_KMEMUSER) */ 723 724 #ifdef _KERNEL 725 726 #define ISALIGNED_cmsghdr(addr) \ 727 (((uintptr_t)(addr) & (_CMSG_HDR_ALIGNMENT - 1)) == 0) 728 729 #define ROUNDUP_cmsglen(len) \ 730 (((len) + _CMSG_HDR_ALIGNMENT - 1) & ~(_CMSG_HDR_ALIGNMENT - 1)) 731 732 #define IS_NON_STREAM_SOCK(vp) \ 733 ((vp)->v_type == VSOCK && (vp)->v_stream == NULL) 734 /* 735 * Macros that operate on struct cmsghdr. 736 * Used in parsing msg_control. 737 * The CMSG_VALID macro does not assume that the last option buffer is padded. 738 */ 739 #define CMSG_NEXT(cmsg) \ 740 (struct cmsghdr *)((uintptr_t)(cmsg) + \ 741 ROUNDUP_cmsglen((cmsg)->cmsg_len)) 742 #define CMSG_CONTENT(cmsg) (&((cmsg)[1])) 743 #define CMSG_CONTENTLEN(cmsg) ((cmsg)->cmsg_len - sizeof (struct cmsghdr)) 744 #define CMSG_VALID(cmsg, start, end) \ 745 (ISALIGNED_cmsghdr(cmsg) && \ 746 ((uintptr_t)(cmsg) >= (uintptr_t)(start)) && \ 747 ((uintptr_t)(cmsg) < (uintptr_t)(end)) && \ 748 ((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) && \ 749 ((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end))) 750 751 /* 752 * Maximum size of any argument that is copied in (addresses, options, 753 * access rights). MUST be at least MAXPATHLEN + 3. 754 * BSD and SunOS 4.X limited this to MLEN or MCLBYTES. 755 */ 756 #define SO_MAXARGSIZE 8192 757 758 /* 759 * Convert between vnode and sonode 760 */ 761 #define VTOSO(vp) ((struct sonode *)((vp)->v_data)) 762 #define SOTOV(sp) ((sp)->so_vnode) 763 764 /* 765 * Internal flags for sobind() 766 */ 767 #define _SOBIND_REBIND 0x01 /* Bind to existing local address */ 768 #define _SOBIND_UNSPEC 0x02 /* Bind to unspecified address */ 769 #define _SOBIND_LOCK_HELD 0x04 /* so_excl_lock held by caller */ 770 #define _SOBIND_NOXLATE 0x08 /* No addr translation for AF_UNIX */ 771 #define _SOBIND_XPG4_2 0x10 /* xpg4.2 semantics */ 772 #define _SOBIND_SOCKBSD 0x20 /* BSD semantics */ 773 #define _SOBIND_LISTEN 0x40 /* Make into SS_ACCEPTCONN */ 774 #define _SOBIND_SOCKETPAIR 0x80 /* Internal flag for so_socketpair() */ 775 /* to enable listen with backlog = 1 */ 776 777 /* 778 * Internal flags for sounbind() 779 */ 780 #define _SOUNBIND_REBIND 0x01 /* Don't clear fields - will rebind */ 781 782 /* 783 * Internal flags for soconnect() 784 */ 785 #define _SOCONNECT_NOXLATE 0x01 /* No addr translation for AF_UNIX */ 786 #define _SOCONNECT_DID_BIND 0x02 /* Unbind when connect fails */ 787 #define _SOCONNECT_XPG4_2 0x04 /* xpg4.2 semantics */ 788 789 /* 790 * Internal flags for sodisconnect() 791 */ 792 #define _SODISCONNECT_LOCK_HELD 0x01 /* so_excl_lock held by caller */ 793 794 /* 795 * Internal flags for sotpi_getsockopt(). 796 */ 797 #define _SOGETSOCKOPT_XPG4_2 0x01 /* xpg4.2 semantics */ 798 799 /* 800 * Internal flags for soallocproto*() 801 */ 802 #define _ALLOC_NOSLEEP 0 /* Don't sleep for memory */ 803 #define _ALLOC_INTR 1 /* Sleep until interrupt */ 804 #define _ALLOC_SLEEP 2 /* Sleep forever */ 805 806 /* 807 * Internal structure for handling AF_UNIX file descriptor passing 808 */ 809 struct fdbuf { 810 int fd_size; /* In bytes, for kmem_free */ 811 int fd_numfd; /* Number of elements below */ 812 char *fd_ebuf; /* Extra buffer to free */ 813 int fd_ebuflen; 814 frtn_t fd_frtn; 815 struct file *fd_fds[1]; /* One or more */ 816 }; 817 #define FDBUF_HDRSIZE (sizeof (struct fdbuf) - sizeof (struct file *)) 818 819 /* 820 * Variable that can be patched to set what version of socket socket() 821 * will create. 822 */ 823 extern int so_default_version; 824 825 #ifdef DEBUG 826 /* Turn on extra testing capabilities */ 827 #define SOCK_TEST 828 #endif /* DEBUG */ 829 830 #ifdef DEBUG 831 char *pr_state(uint_t, uint_t); 832 char *pr_addr(int, struct sockaddr *, t_uscalar_t); 833 int so_verify_oobstate(struct sonode *); 834 #endif /* DEBUG */ 835 836 /* 837 * DEBUG macros 838 */ 839 #if defined(DEBUG) 840 #define SOCK_DEBUG 841 842 extern int sockdebug; 843 extern int sockprinterr; 844 845 #define eprint(args) printf args 846 #define eprintso(so, args) \ 847 { if (sockprinterr && ((so)->so_options & SO_DEBUG)) printf args; } 848 #define eprintline(error) \ 849 { \ 850 if (error != EINTR && (sockprinterr || sockdebug > 0)) \ 851 printf("socket error %d: line %d file %s\n", \ 852 (error), __LINE__, __FILE__); \ 853 } 854 855 #define eprintsoline(so, error) \ 856 { if (sockprinterr && ((so)->so_options & SO_DEBUG)) \ 857 printf("socket(%p) error %d: line %d file %s\n", \ 858 (void *)(so), (error), __LINE__, __FILE__); \ 859 } 860 #define dprint(level, args) { if (sockdebug > (level)) printf args; } 861 #define dprintso(so, level, args) \ 862 { if (sockdebug > (level) && ((so)->so_options & SO_DEBUG)) printf args; } 863 864 #else /* define(DEBUG) */ 865 866 #define eprint(args) {} 867 #define eprintso(so, args) {} 868 #define eprintline(error) {} 869 #define eprintsoline(so, error) {} 870 #define dprint(level, args) {} 871 #define dprintso(so, level, args) {} 872 873 #endif /* defined(DEBUG) */ 874 875 extern struct vfsops sock_vfsops; 876 extern struct vnodeops *socket_vnodeops; 877 extern const struct fs_operation_def socket_vnodeops_template[]; 878 879 extern dev_t sockdev; 880 881 extern krwlock_t sockconf_lock; 882 883 /* 884 * sockfs functions 885 */ 886 extern int sock_getmsg(vnode_t *, struct strbuf *, struct strbuf *, 887 uchar_t *, int *, int, rval_t *); 888 extern int sock_putmsg(vnode_t *, struct strbuf *, struct strbuf *, 889 uchar_t, int, int); 890 extern int sogetvp(char *, vnode_t **, int); 891 extern int sockinit(int, char *); 892 extern int solookup(int, int, int, struct sockparams **); 893 extern void so_lock_single(struct sonode *); 894 extern void so_unlock_single(struct sonode *, int); 895 extern int so_lock_read(struct sonode *, int); 896 extern int so_lock_read_intr(struct sonode *, int); 897 extern void so_unlock_read(struct sonode *); 898 extern void *sogetoff(mblk_t *, t_uscalar_t, t_uscalar_t, uint_t); 899 extern void so_getopt_srcaddr(void *, t_uscalar_t, 900 void **, t_uscalar_t *); 901 extern int so_getopt_unix_close(void *, t_uscalar_t); 902 extern void fdbuf_free(struct fdbuf *); 903 extern mblk_t *fdbuf_allocmsg(int, struct fdbuf *); 904 extern int fdbuf_create(void *, int, struct fdbuf **); 905 extern void so_closefds(void *, t_uscalar_t, int, int); 906 extern void so_truncatecmsg(void *, t_uscalar_t, uint_t); 907 908 extern int so_getfdopt(void *, t_uscalar_t, int, void **, int *); 909 t_uscalar_t so_optlen(void *, t_uscalar_t, int); 910 extern void so_cmsg2opt(void *, t_uscalar_t, int, mblk_t *); 911 extern t_uscalar_t 912 so_cmsglen(mblk_t *, void *, t_uscalar_t, int); 913 extern int so_opt2cmsg(mblk_t *, void *, t_uscalar_t, int, 914 void *, t_uscalar_t); 915 extern void soisconnecting(struct sonode *); 916 extern void soisconnected(struct sonode *); 917 extern void soisdisconnected(struct sonode *, int); 918 extern void socantsendmore(struct sonode *); 919 extern void socantrcvmore(struct sonode *); 920 extern void soseterror(struct sonode *, int); 921 extern int sogeterr(struct sonode *, boolean_t); 922 extern int sowaitconnected(struct sonode *, int, int); 923 924 extern ssize_t soreadfile(file_t *, uchar_t *, u_offset_t, int *, size_t); 925 extern void *sock_kstat_init(zoneid_t); 926 extern void sock_kstat_fini(zoneid_t, void *); 927 extern struct sonode *getsonode(int, int *, file_t **); 928 /* 929 * Function wrappers (mostly around the sonode switch) for 930 * backward compatibility. 931 */ 932 extern int soaccept(struct sonode *, int, struct sonode **); 933 extern int sobind(struct sonode *, struct sockaddr *, socklen_t, 934 int, int); 935 extern int solisten(struct sonode *, int); 936 extern int soconnect(struct sonode *, struct sockaddr *, socklen_t, 937 int, int); 938 extern int sorecvmsg(struct sonode *, struct nmsghdr *, struct uio *); 939 extern int sosendmsg(struct sonode *, struct nmsghdr *, struct uio *); 940 extern int soshutdown(struct sonode *, int); 941 extern int sogetsockopt(struct sonode *, int, int, void *, socklen_t *, 942 int); 943 extern int sosetsockopt(struct sonode *, int, int, const void *, 944 t_uscalar_t); 945 946 extern struct sonode *socreate(struct sockparams *, int, int, int, int, 947 int *); 948 949 extern int so_copyin(const void *, void *, size_t, int); 950 extern int so_copyout(const void *, void *, size_t, int); 951 952 #endif 953 954 /* 955 * Internal structure for obtaining sonode information from the socklist. 956 * These types match those corresponding in the sonode structure. 957 * This is not a published interface, and may change at any time. It is 958 * used for passing information back up to the kstat consumers. By converting 959 * kernel addresses to strings, we should be able to pass information from 960 * the kernel to userland regardless of n-bit kernel we are using. 961 */ 962 963 #define ADRSTRLEN (2 * sizeof (uint64_t) + 1) 964 965 struct sockinfo { 966 uint_t si_size; /* real length of this struct */ 967 short si_family; 968 short si_type; 969 ushort_t si_flag; 970 uint_t si_state; 971 uint_t si_ux_laddr_sou_magic; 972 uint_t si_ux_faddr_sou_magic; 973 t_scalar_t si_serv_type; 974 t_uscalar_t si_laddr_soa_len; 975 t_uscalar_t si_faddr_soa_len; 976 uint16_t si_laddr_family; 977 uint16_t si_faddr_family; 978 char si_laddr_sun_path[MAXPATHLEN + 1]; /* NULL terminated */ 979 char si_faddr_sun_path[MAXPATHLEN + 1]; 980 boolean_t si_faddr_noxlate; 981 zoneid_t si_szoneid; 982 char si_son_straddr[ADRSTRLEN]; 983 char si_lvn_straddr[ADRSTRLEN]; 984 char si_fvn_straddr[ADRSTRLEN]; 985 uint64_t si_inode; 986 }; 987 988 /* 989 * Subcodes for sockconf() system call 990 */ 991 #define SOCKCONFIG_ADD_SOCK 0 992 #define SOCKCONFIG_REMOVE_SOCK 1 993 #define SOCKCONFIG_ADD_FILTER 2 994 #define SOCKCONFIG_REMOVE_FILTER 3 995 #define SOCKCONFIG_GET_SOCKTABLE 4 996 997 /* 998 * Data structures for configuring socket filters. 999 */ 1000 1001 /* 1002 * Placement hint for automatic filters 1003 */ 1004 typedef enum { 1005 SOF_HINT_NONE, 1006 SOF_HINT_TOP, 1007 SOF_HINT_BOTTOM, 1008 SOF_HINT_BEFORE, 1009 SOF_HINT_AFTER 1010 } sof_hint_t; 1011 1012 /* 1013 * Socket tuple. Used by sockconfig_filter_props to list socket 1014 * types of interest. 1015 */ 1016 typedef struct sof_socktuple { 1017 int sofst_family; 1018 int sofst_type; 1019 int sofst_protocol; 1020 } sof_socktuple_t; 1021 1022 /* 1023 * Socket filter properties used by sockconfig() system call. 1024 */ 1025 struct sockconfig_filter_props { 1026 char *sfp_modname; 1027 boolean_t sfp_autoattach; 1028 sof_hint_t sfp_hint; 1029 char *sfp_hintarg; 1030 uint_t sfp_socktuple_cnt; 1031 sof_socktuple_t *sfp_socktuple; 1032 }; 1033 1034 /* 1035 * Data structures for the in-kernel socket configuration table. 1036 */ 1037 typedef struct sockconfig_socktable_entry { 1038 int se_family; 1039 int se_type; 1040 int se_protocol; 1041 int se_refcnt; 1042 int se_flags; 1043 char se_modname[MODMAXNAMELEN]; 1044 char se_strdev[MAXPATHLEN]; 1045 } sockconfig_socktable_entry_t; 1046 1047 typedef struct sockconfig_socktable { 1048 uint_t num_of_entries; 1049 sockconfig_socktable_entry_t *st_entries; 1050 } sockconfig_socktable_t; 1051 1052 #ifdef _SYSCALL32 1053 1054 typedef struct sof_socktuple32 { 1055 int32_t sofst_family; 1056 int32_t sofst_type; 1057 int32_t sofst_protocol; 1058 } sof_socktuple32_t; 1059 1060 struct sockconfig_filter_props32 { 1061 caddr32_t sfp_modname; 1062 boolean_t sfp_autoattach; 1063 sof_hint_t sfp_hint; 1064 caddr32_t sfp_hintarg; 1065 uint32_t sfp_socktuple_cnt; 1066 caddr32_t sfp_socktuple; 1067 }; 1068 1069 typedef struct sockconfig_socktable32 { 1070 uint_t num_of_entries; 1071 caddr32_t st_entries; 1072 } sockconfig_socktable32_t; 1073 1074 #endif /* _SYSCALL32 */ 1075 1076 #define SOCKMOD_PATH "socketmod" /* dir where sockmods are stored */ 1077 1078 #ifdef __cplusplus 1079 } 1080 #endif 1081 1082 #endif /* _SYS_SOCKETVAR_H */ 1083