xref: /openbsd/sys/sys/unpcb.h (revision 097a140d)
1 /*	$OpenBSD: unpcb.h,v 1.18 2021/02/10 08:20:09 mvs Exp $	*/
2 /*	$NetBSD: unpcb.h,v 1.6 1994/06/29 06:46:08 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)unpcb.h	8.1 (Berkeley) 6/2/93
33  */
34 
35 /*
36  * Protocol control block for an active
37  * instance of a UNIX internal protocol.
38  *
39  * A socket may be associated with an vnode in the
40  * file system.  If so, the unp_vnode pointer holds
41  * a reference count to this vnode, which should be vrele'd
42  * when the socket goes away.
43  *
44  * A socket may be connected to another socket, in which
45  * case the control block of the socket to which it is connected
46  * is given by unp_conn.
47  *
48  * A socket may be referenced by a number of sockets (e.g. several
49  * sockets may be connected to a datagram socket.)  These sockets
50  * are in a linked list starting with unp_refs, linked through
51  * unp_nextref and null-terminated.  Note that a socket may be referenced
52  * by a number of other sockets and may also reference a socket (not
53  * necessarily one which is referencing it).  This generates
54  * the need for unp_refs and unp_nextref to be separate fields.
55  *
56  * Stream sockets keep copies of receive sockbuf sb_cc and sb_mbcnt
57  * so that changes in the sockbuf may be computed to modify
58  * back pressure on the sender accordingly.
59  *
60  * Locks used to protect struct members:
61  *      I       immutable after creation
62  *      U       unp_lock
63  */
64 
65 
66 struct	unpcb {
67 	struct	socket *unp_socket;	/* [I] pointer back to socket */
68 	struct	vnode *unp_vnode;	/* [U] if associated with file */
69 	struct	file *unp_file;		/* [U] backpointer for unp_gc() */
70 	struct	unpcb *unp_conn;	/* [U] control block of connected socket */
71 	ino_t	unp_ino;		/* [U] fake inode number */
72 	SLIST_HEAD(,unpcb) unp_refs;	/* [U] referencing socket linked list */
73 	SLIST_ENTRY(unpcb) unp_nextref;	/* [U] link in unp_refs list */
74 	struct	mbuf *unp_addr;		/* [U] bound address of socket */
75 	long	unp_msgcount;		/* [U] references from socket rcv buf */
76 	int	unp_flags;		/* [U] this unpcb contains peer eids */
77 	struct	sockpeercred unp_connid;/* [U] id of peer process */
78 	struct	timespec unp_ctime;	/* [I] holds creation time */
79 	LIST_ENTRY(unpcb) unp_link;	/* [U] link in per-AF list of sockets */
80 };
81 
82 /*
83  * flag bits in unp_flags
84  */
85 #define UNP_FEIDS	0x01		/* unp_connid contains information */
86 #define UNP_FEIDSBIND	0x02		/* unp_connid was set by a bind */
87 #define UNP_GCMARK	0x04		/* mark during unp_gc() */
88 #define UNP_GCDEFER	0x08		/* ref'd, but not marked in this pass */
89 #define UNP_GCDEAD	0x10		/* unref'd in this pass */
90 #define UNP_BINDING	0x20		/* unp is binding now */
91 #define UNP_CONNECTING	0x40		/* unp is connecting now */
92 
93 #define	sotounpcb(so)	((struct unpcb *)((so)->so_pcb))
94 
95 #ifdef _KERNEL
96 struct fdpass {
97 	struct file	*fp;
98 	int		 flags;
99 };
100 
101 int	uipc_usrreq(struct socket *, int , struct mbuf *,
102 			 struct mbuf *, struct mbuf *, struct proc *);
103 int	uipc_attach(struct socket *, int);
104 int	uipc_detach(struct socket *);
105 
106 void	unp_init(void);
107 int	unp_bind(struct unpcb *, struct mbuf *, struct proc *);
108 int	unp_connect(struct socket *, struct mbuf *, struct proc *);
109 int	unp_connect2(struct socket *, struct socket *);
110 void	unp_detach(struct unpcb *);
111 void	unp_disconnect(struct unpcb *);
112 void	unp_drop(struct unpcb *, int);
113 void	unp_gc(void *);
114 void	unp_shutdown(struct unpcb *);
115 int 	unp_externalize(struct mbuf *, socklen_t, int);
116 int	unp_internalize(struct mbuf *, struct proc *);
117 void 	unp_dispose(struct mbuf *);
118 #endif /* _KERNEL */
119