11eaf0ac3Slogwang /*- 2*d4a07e70Sfengbojiang * SPDX-License-Identifier: BSD-3-Clause 3*d4a07e70Sfengbojiang * 41eaf0ac3Slogwang * Copyright (c) 1982, 1986, 1989, 1993 51eaf0ac3Slogwang * The Regents of the University of California. All rights reserved. 61eaf0ac3Slogwang * 71eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without 81eaf0ac3Slogwang * modification, are permitted provided that the following conditions 91eaf0ac3Slogwang * are met: 101eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright 111eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer. 121eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright 131eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the 141eaf0ac3Slogwang * documentation and/or other materials provided with the distribution. 15*d4a07e70Sfengbojiang * 3. Neither the name of the University nor the names of its contributors 161eaf0ac3Slogwang * may be used to endorse or promote products derived from this software 171eaf0ac3Slogwang * without specific prior written permission. 181eaf0ac3Slogwang * 191eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 201eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 211eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 231eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 241eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 251eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 261eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 271eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 281eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 291eaf0ac3Slogwang * SUCH DAMAGE. 301eaf0ac3Slogwang * 311eaf0ac3Slogwang * @(#)unpcb.h 8.1 (Berkeley) 6/2/93 321eaf0ac3Slogwang * $FreeBSD$ 331eaf0ac3Slogwang */ 341eaf0ac3Slogwang 351eaf0ac3Slogwang #ifndef _SYS_UNPCB_H_ 361eaf0ac3Slogwang #define _SYS_UNPCB_H_ 371eaf0ac3Slogwang 38*d4a07e70Sfengbojiang typedef uint64_t unp_gen_t; 39*d4a07e70Sfengbojiang 40*d4a07e70Sfengbojiang #if defined(_KERNEL) || defined(_WANT_UNPCB) 411eaf0ac3Slogwang #include <sys/queue.h> 421eaf0ac3Slogwang #include <sys/ucred.h> 431eaf0ac3Slogwang 441eaf0ac3Slogwang /* 451eaf0ac3Slogwang * Protocol control block for an active 461eaf0ac3Slogwang * instance of a UNIX internal protocol. 471eaf0ac3Slogwang * 481eaf0ac3Slogwang * A socket may be associated with a vnode in the 491eaf0ac3Slogwang * filesystem. If so, the unp_vnode pointer holds 501eaf0ac3Slogwang * a reference count to this vnode, which should be irele'd 511eaf0ac3Slogwang * when the socket goes away. 521eaf0ac3Slogwang * 531eaf0ac3Slogwang * A socket may be connected to another socket, in which 541eaf0ac3Slogwang * case the control block of the socket to which it is connected 551eaf0ac3Slogwang * is given by unp_conn. 561eaf0ac3Slogwang * 571eaf0ac3Slogwang * A socket may be referenced by a number of sockets (e.g. several 581eaf0ac3Slogwang * sockets may be connected to a datagram socket.) These sockets 591eaf0ac3Slogwang * are in a linked list starting with unp_refs, linked through 601eaf0ac3Slogwang * unp_nextref and null-terminated. Note that a socket may be referenced 611eaf0ac3Slogwang * by a number of other sockets and may also reference a socket (not 621eaf0ac3Slogwang * necessarily one which is referencing it). This generates 631eaf0ac3Slogwang * the need for unp_refs and unp_nextref to be separate fields. 641eaf0ac3Slogwang * 651eaf0ac3Slogwang * Stream sockets keep copies of receive sockbuf sb_cc and sb_mbcnt 661eaf0ac3Slogwang * so that changes in the sockbuf may be computed to modify 671eaf0ac3Slogwang * back pressure on the sender accordingly. 68*d4a07e70Sfengbojiang * 69*d4a07e70Sfengbojiang * Locking key: 70*d4a07e70Sfengbojiang * (a) Atomic 71*d4a07e70Sfengbojiang * (c) Constant 72*d4a07e70Sfengbojiang * (g) Locked using linkage lock 73*d4a07e70Sfengbojiang * (l) Locked using list lock 74*d4a07e70Sfengbojiang * (p) Locked using pcb lock 751eaf0ac3Slogwang */ 761eaf0ac3Slogwang LIST_HEAD(unp_head, unpcb); 771eaf0ac3Slogwang 781eaf0ac3Slogwang struct unpcb { 79*d4a07e70Sfengbojiang /* Cache line 1 */ 80*d4a07e70Sfengbojiang struct mtx unp_mtx; /* PCB mutex */ 81*d4a07e70Sfengbojiang struct unpcb *unp_conn; /* (p) connected socket */ 82*d4a07e70Sfengbojiang volatile u_int unp_refcount; /* (a, p) atomic refcount */ 83*d4a07e70Sfengbojiang short unp_flags; /* (p) PCB flags */ 84*d4a07e70Sfengbojiang short unp_gcflag; /* (g) Garbage collector flags */ 85*d4a07e70Sfengbojiang struct sockaddr_un *unp_addr; /* (p) bound address of socket */ 86*d4a07e70Sfengbojiang struct socket *unp_socket; /* (c) pointer back to socket */ 87*d4a07e70Sfengbojiang /* Cache line 2 */ 88*d4a07e70Sfengbojiang u_int unp_pairbusy; /* (p) threads acquiring peer locks */ 89*d4a07e70Sfengbojiang struct vnode *unp_vnode; /* (p) associated file if applicable */ 90*d4a07e70Sfengbojiang struct xucred unp_peercred; /* (p) peer credentials if applicable */ 91*d4a07e70Sfengbojiang LIST_ENTRY(unpcb) unp_reflink; /* (l) link in unp_refs list */ 92*d4a07e70Sfengbojiang LIST_ENTRY(unpcb) unp_link; /* (g) glue on list of all PCBs */ 93*d4a07e70Sfengbojiang struct unp_head unp_refs; /* (l) referencing socket linked list */ 94*d4a07e70Sfengbojiang unp_gen_t unp_gencnt; /* (g) generation count of this item */ 95*d4a07e70Sfengbojiang struct file *unp_file; /* (g) back-pointer to file for gc */ 96*d4a07e70Sfengbojiang u_int unp_msgcount; /* (g) references from message queue */ 97*d4a07e70Sfengbojiang u_int unp_gcrefs; /* (g) garbage collector refcount */ 98*d4a07e70Sfengbojiang ino_t unp_ino; /* (g) fake inode number */ 99*d4a07e70Sfengbojiang LIST_ENTRY(unpcb) unp_dead; /* (g) link in dead list */ 100*d4a07e70Sfengbojiang } __attribute__((__aligned__(64))); 1011eaf0ac3Slogwang 1021eaf0ac3Slogwang /* 1031eaf0ac3Slogwang * Flags in unp_flags. 1041eaf0ac3Slogwang * 1051eaf0ac3Slogwang * UNP_HAVEPC - indicates that the unp_peercred member is filled in 1061eaf0ac3Slogwang * and is really the credentials of the connected peer. This is used 1071eaf0ac3Slogwang * to determine whether the contents should be sent to the user or 1081eaf0ac3Slogwang * not. 1091eaf0ac3Slogwang */ 1101eaf0ac3Slogwang #define UNP_HAVEPC 0x001 111*d4a07e70Sfengbojiang #define UNP_WANTCRED_ALWAYS 0x002 /* credentials wanted always */ 112*d4a07e70Sfengbojiang #define UNP_WANTCRED_ONESHOT 0x004 /* credentials wanted once */ 1131eaf0ac3Slogwang #define UNP_CONNWAIT 0x008 /* connect blocks until accepted */ 1141eaf0ac3Slogwang 115*d4a07e70Sfengbojiang #define UNP_WANTCRED_MASK (UNP_WANTCRED_ONESHOT | UNP_WANTCRED_ALWAYS) 1161eaf0ac3Slogwang 1171eaf0ac3Slogwang /* 1181eaf0ac3Slogwang * These flags are used to handle non-atomicity in connect() and bind() 1191eaf0ac3Slogwang * operations on a socket: in particular, to avoid races between multiple 1201eaf0ac3Slogwang * threads or processes operating simultaneously on the same socket. 1211eaf0ac3Slogwang */ 1221eaf0ac3Slogwang #define UNP_CONNECTING 0x010 /* Currently connecting. */ 1231eaf0ac3Slogwang #define UNP_BINDING 0x020 /* Currently binding. */ 124*d4a07e70Sfengbojiang #define UNP_WAITING 0x040 /* Peer state is changing. */ 125*d4a07e70Sfengbojiang 126*d4a07e70Sfengbojiang /* 127*d4a07e70Sfengbojiang * Flags in unp_gcflag. 128*d4a07e70Sfengbojiang */ 129*d4a07e70Sfengbojiang #define UNPGC_DEAD 0x1 /* unpcb might be dead. */ 130*d4a07e70Sfengbojiang #define UNPGC_IGNORE_RIGHTS 0x2 /* Attached rights are freed */ 1311eaf0ac3Slogwang 1321eaf0ac3Slogwang #define sotounpcb(so) ((struct unpcb *)((so)->so_pcb)) 1331eaf0ac3Slogwang 134*d4a07e70Sfengbojiang #endif /* _KERNEL || _WANT_UNPCB */ 135*d4a07e70Sfengbojiang 136*d4a07e70Sfengbojiang /* 137*d4a07e70Sfengbojiang * UNPCB structure exported to user-land via sysctl(3). 138*d4a07e70Sfengbojiang * 139*d4a07e70Sfengbojiang * Fields prefixed with "xu_" are unique to the export structure, and fields 140*d4a07e70Sfengbojiang * with "unp_" or other prefixes match corresponding fields of 'struct unpcb'. 141*d4a07e70Sfengbojiang * 142*d4a07e70Sfengbojiang * Legend: 143*d4a07e70Sfengbojiang * (s) - used by userland utilities in src 144*d4a07e70Sfengbojiang * (p) - used by utilities in ports 145*d4a07e70Sfengbojiang * (3) - is known to be used by third party software not in ports 146*d4a07e70Sfengbojiang * (n) - no known usage 147*d4a07e70Sfengbojiang * 148*d4a07e70Sfengbojiang * Evil hack: declare only if sys/socketvar.h have been included. 149*d4a07e70Sfengbojiang */ 1501eaf0ac3Slogwang #ifdef _SYS_SOCKETVAR_H_ 1511eaf0ac3Slogwang struct xunpcb { 152*d4a07e70Sfengbojiang ksize_t xu_len; /* length of this structure */ 153*d4a07e70Sfengbojiang kvaddr_t xu_unpp; /* to help netstat, fstat */ 154*d4a07e70Sfengbojiang kvaddr_t unp_vnode; /* (s) */ 155*d4a07e70Sfengbojiang kvaddr_t unp_conn; /* (s) */ 156*d4a07e70Sfengbojiang kvaddr_t xu_firstref; /* (s) */ 157*d4a07e70Sfengbojiang kvaddr_t xu_nextref; /* (s) */ 158*d4a07e70Sfengbojiang unp_gen_t unp_gencnt; /* (s) */ 159*d4a07e70Sfengbojiang int64_t xu_spare64[8]; 160*d4a07e70Sfengbojiang int32_t xu_spare32[8]; 1611eaf0ac3Slogwang union { 162*d4a07e70Sfengbojiang struct sockaddr_un xu_addr; /* our bound address */ 1631eaf0ac3Slogwang char xu_dummy1[256]; 1641eaf0ac3Slogwang }; 165*d4a07e70Sfengbojiang union { 166*d4a07e70Sfengbojiang struct sockaddr_un xu_caddr; /* their bound address */ 167*d4a07e70Sfengbojiang char xu_dummy2[256]; 168*d4a07e70Sfengbojiang }; 169*d4a07e70Sfengbojiang struct xsocket xu_socket; 170*d4a07e70Sfengbojiang } __attribute__((__aligned__(MAX(8, sizeof(void *))))); 1711eaf0ac3Slogwang 1721eaf0ac3Slogwang struct xunpgen { 173*d4a07e70Sfengbojiang ksize_t xug_len; 1741eaf0ac3Slogwang u_int xug_count; 1751eaf0ac3Slogwang unp_gen_t xug_gen; 1761eaf0ac3Slogwang so_gen_t xug_sogen; 177*d4a07e70Sfengbojiang } __attribute__((__aligned__(8))); 1781eaf0ac3Slogwang #endif /* _SYS_SOCKETVAR_H_ */ 1791eaf0ac3Slogwang 180*d4a07e70Sfengbojiang #if defined(_KERNEL) 181*d4a07e70Sfengbojiang struct thread; 182*d4a07e70Sfengbojiang 183*d4a07e70Sfengbojiang /* In uipc_userreq.c */ 184*d4a07e70Sfengbojiang void 185*d4a07e70Sfengbojiang unp_copy_peercred(struct thread *td, struct unpcb *client_unp, 186*d4a07e70Sfengbojiang struct unpcb *server_unp, struct unpcb *listen_unp); 187*d4a07e70Sfengbojiang #endif 188*d4a07e70Sfengbojiang 1891eaf0ac3Slogwang #endif /* _SYS_UNPCB_H_ */ 190