1139825Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes *	@(#)unpcb.h	8.1 (Berkeley) 6/2/93
3050477Speter * $FreeBSD$
311541Srgrimes */
321541Srgrimes
332165Spaul#ifndef _SYS_UNPCB_H_
342165Spaul#define _SYS_UNPCB_H_
352165Spaul
3636079Swollman#include <sys/queue.h>
3781857Sdd#include <sys/ucred.h>
3836079Swollman
391541Srgrimes/*
401541Srgrimes * Protocol control block for an active
411541Srgrimes * instance of a UNIX internal protocol.
421541Srgrimes *
43108470Sschweikh * A socket may be associated with a vnode in the
4496755Strhodes * filesystem.  If so, the unp_vnode pointer holds
451541Srgrimes * a reference count to this vnode, which should be irele'd
461541Srgrimes * when the socket goes away.
471541Srgrimes *
481541Srgrimes * A socket may be connected to another socket, in which
491541Srgrimes * case the control block of the socket to which it is connected
501541Srgrimes * is given by unp_conn.
511541Srgrimes *
521541Srgrimes * A socket may be referenced by a number of sockets (e.g. several
531541Srgrimes * sockets may be connected to a datagram socket.)  These sockets
541541Srgrimes * are in a linked list starting with unp_refs, linked through
551541Srgrimes * unp_nextref and null-terminated.  Note that a socket may be referenced
561541Srgrimes * by a number of other sockets and may also reference a socket (not
571541Srgrimes * necessarily one which is referencing it).  This generates
581541Srgrimes * the need for unp_refs and unp_nextref to be separate fields.
591541Srgrimes *
601541Srgrimes * Stream sockets keep copies of receive sockbuf sb_cc and sb_mbcnt
611541Srgrimes * so that changes in the sockbuf may be computed to modify
621541Srgrimes * back pressure on the sender accordingly.
631541Srgrimes */
6436079Swollmantypedef	u_quad_t	unp_gen_t;
6560938SjakeLIST_HEAD(unp_head, unpcb);
6636079Swollman
6783045Sobrienstruct unpcb {
6860938Sjake	LIST_ENTRY(unpcb) unp_link; 	/* glue on list of all PCBs */
691541Srgrimes	struct	socket *unp_socket;	/* pointer back to socket */
70174988Sjeff	struct	file *unp_file;		/* back-pointer to file for gc. */
711541Srgrimes	struct	vnode *unp_vnode;	/* if associated with file */
721541Srgrimes	ino_t	unp_ino;		/* fake inode number */
731541Srgrimes	struct	unpcb *unp_conn;	/* control block of connected socket */
7436079Swollman	struct	unp_head unp_refs;	/* referencing socket linked list */
7560938Sjake	LIST_ENTRY(unpcb) unp_reflink;	/* link in unp_refs list */
7628270Swollman	struct	sockaddr_un *unp_addr;	/* bound address of socket */
771541Srgrimes	int	unp_cc;			/* copy of rcv.sb_cc */
781541Srgrimes	int	unp_mbcnt;		/* copy of rcv.sb_mbcnt */
7936079Swollman	unp_gen_t unp_gencnt;		/* generation count of this instance */
80174988Sjeff	short	unp_flags;		/* flags */
81174988Sjeff	short	unp_gcflag;		/* Garbage collector flags. */
8281857Sdd	struct	xucred unp_peercred;	/* peer credentials, if applicable */
83165810Sjhb	u_int	unp_refcount;
84174988Sjeff	u_int	unp_msgcount;		/* references from message queue */
85167030Srwatson	struct	mtx unp_mtx;		/* mutex */
861541Srgrimes};
871541Srgrimes
8881857Sdd/*
8981857Sdd * Flags in unp_flags.
9081857Sdd *
9181857Sdd * UNP_HAVEPC - indicates that the unp_peercred member is filled in
9281857Sdd * and is really the credentials of the connected peer.  This is used
9381857Sdd * to determine whether the contents should be sent to the user or
9481857Sdd * not.
9581857Sdd *
9681857Sdd * UNP_HAVEPCCACHED - indicates that the unp_peercred member is filled
9781857Sdd * in, but does *not* contain the credentials of the connected peer
9881857Sdd * (there may not even be a peer).  This is set in unp_listen() when
9981857Sdd * it fills in unp_peercred for later consumption by unp_connect().
10081857Sdd */
10181857Sdd#define UNP_HAVEPC			0x001
10281857Sdd#define UNP_HAVEPCCACHED		0x002
103144978Smdodd#define	UNP_WANTCRED			0x004	/* credentials wanted */
104144978Smdodd#define	UNP_CONNWAIT			0x008	/* connect blocks until accepted */
10581857Sdd
106174988Sjeff#define	UNPGC_REF			0x1	/* unpcb has external ref. */
107174988Sjeff#define	UNPGC_DEAD			0x2	/* unpcb might be dead. */
108174988Sjeff#define	UNPGC_SCANNED			0x4	/* Has been scanned. */
109174988Sjeff
110160590Srwatson/*
111160590Srwatson * These flags are used to handle non-atomicity in connect() and bind()
112160590Srwatson * operations on a socket: in particular, to avoid races between multiple
113160590Srwatson * threads or processes operating simultaneously on the same socket.
114160590Srwatson */
115160590Srwatson#define	UNP_CONNECTING			0x010	/* Currently connecting. */
116160590Srwatson#define	UNP_BINDING			0x020	/* Currently binding. */
117160590Srwatson
1181541Srgrimes#define	sotounpcb(so)	((struct unpcb *)((so)->so_pcb))
1192165Spaul
12036079Swollman/* Hack alert -- this structure depends on <sys/socketvar.h>. */
12136079Swollman#ifdef	_SYS_SOCKETVAR_H_
12283045Sobrienstruct xunpcb {
12336079Swollman	size_t	xu_len;			/* length of this structure */
12436079Swollman	struct	unpcb *xu_unpp;		/* to help netstat, fstat */
12536079Swollman	struct	unpcb xu_unp;		/* our information */
12636079Swollman	union {
12736079Swollman		struct	sockaddr_un xuu_addr;	/* our bound address */
12836079Swollman		char	xu_dummy1[256];
12936079Swollman	} xu_au;
13036079Swollman#define	xu_addr	xu_au.xuu_addr
13136079Swollman	union {
13236079Swollman		struct	sockaddr_un xuu_caddr; /* their bound address */
13336079Swollman		char	xu_dummy2[256];
13436079Swollman	} xu_cau;
13536079Swollman#define	xu_caddr xu_cau.xuu_caddr
13636079Swollman	struct	xsocket	xu_socket;
13736079Swollman	u_quad_t	xu_alignment_hack;
13836079Swollman};
13936079Swollman
14083045Sobrienstruct xunpgen {
14136079Swollman	size_t	xug_len;
14236079Swollman	u_int	xug_count;
14336079Swollman	unp_gen_t xug_gen;
14436079Swollman	so_gen_t xug_sogen;
14536079Swollman};
14636079Swollman#endif /* _SYS_SOCKETVAR_H_ */
14736079Swollman
14836079Swollman#endif /* _SYS_UNPCB_H_ */
149