xref: /netbsd-src/sys/netcan/can_pcb.h (revision 6e4cb2b9ab4107b5ab560c348a691ae05ac595a5)
1*6e4cb2b9Sbouyer /*	$NetBSD: can_pcb.h,v 1.2 2017/05/27 21:02:56 bouyer Exp $	*/
2*6e4cb2b9Sbouyer 
3*6e4cb2b9Sbouyer /*-
4*6e4cb2b9Sbouyer  * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc.
5*6e4cb2b9Sbouyer  * All rights reserved.
6*6e4cb2b9Sbouyer  *
7*6e4cb2b9Sbouyer  * This code is derived from software contributed to The NetBSD Foundation
8*6e4cb2b9Sbouyer  * by Robert Swindells and Manuel Bouyer
9*6e4cb2b9Sbouyer  *
10*6e4cb2b9Sbouyer  * Redistribution and use in source and binary forms, with or without
11*6e4cb2b9Sbouyer  * modification, are permitted provided that the following conditions
12*6e4cb2b9Sbouyer  * are met:
13*6e4cb2b9Sbouyer  * 1. Redistributions of source code must retain the above copyright
14*6e4cb2b9Sbouyer  *    notice, this list of conditions and the following disclaimer.
15*6e4cb2b9Sbouyer  * 2. Redistributions in binary form must reproduce the above copyright
16*6e4cb2b9Sbouyer  *    notice, this list of conditions and the following disclaimer in the
17*6e4cb2b9Sbouyer  *    documentation and/or other materials provided with the distribution.
18*6e4cb2b9Sbouyer  *
19*6e4cb2b9Sbouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*6e4cb2b9Sbouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*6e4cb2b9Sbouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*6e4cb2b9Sbouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*6e4cb2b9Sbouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*6e4cb2b9Sbouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*6e4cb2b9Sbouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*6e4cb2b9Sbouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*6e4cb2b9Sbouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*6e4cb2b9Sbouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*6e4cb2b9Sbouyer  * POSSIBILITY OF SUCH DAMAGE.
30*6e4cb2b9Sbouyer  */
31*6e4cb2b9Sbouyer 
32*6e4cb2b9Sbouyer #ifndef _NETCAN_CAN_PCB_H_
33*6e4cb2b9Sbouyer #define _NETCAN_CAN_PCB_H_
34*6e4cb2b9Sbouyer 
35*6e4cb2b9Sbouyer #include <sys/queue.h>
36*6e4cb2b9Sbouyer 
37*6e4cb2b9Sbouyer /*
38*6e4cb2b9Sbouyer  * Common structure pcb for can protocol implementation.
39*6e4cb2b9Sbouyer  * Here are stored pointers to local and foreign host table
40*6e4cb2b9Sbouyer  * entries, local and foreign socket numbers, and pointers
41*6e4cb2b9Sbouyer  * up (to a socket structure) and down (to a protocol-specific)
42*6e4cb2b9Sbouyer  * control block.
43*6e4cb2b9Sbouyer  */
44*6e4cb2b9Sbouyer struct canpcbpolicy;
45*6e4cb2b9Sbouyer 
46*6e4cb2b9Sbouyer 
47*6e4cb2b9Sbouyer struct canpcb {
48*6e4cb2b9Sbouyer 	LIST_ENTRY(canpcb) canp_hash;
49*6e4cb2b9Sbouyer 	LIST_ENTRY(canpcb) canp_lhash;
50*6e4cb2b9Sbouyer 	TAILQ_ENTRY(canpcb) canp_queue;
51*6e4cb2b9Sbouyer 	kmutex_t 	canp_mtx;	/* protect states and refcount */
52*6e4cb2b9Sbouyer 	int		canp_state;
53*6e4cb2b9Sbouyer 	int		canp_flags;
54*6e4cb2b9Sbouyer 	struct		socket *canp_socket;	/* back pointer to socket */
55*6e4cb2b9Sbouyer 	struct		ifnet *canp_ifp; /* interface this socket is bound to */
56*6e4cb2b9Sbouyer 
57*6e4cb2b9Sbouyer 	struct		canpcbtable *canp_table;
58*6e4cb2b9Sbouyer 	struct		can_filter *canp_filters; /* filter array */
59*6e4cb2b9Sbouyer 	int		canp_nfilters; /* size of canp_filters */
60*6e4cb2b9Sbouyer 
61*6e4cb2b9Sbouyer 	int		canp_refcount;
62*6e4cb2b9Sbouyer };
63*6e4cb2b9Sbouyer 
64*6e4cb2b9Sbouyer LIST_HEAD(canpcbhead, canpcb);
65*6e4cb2b9Sbouyer 
66*6e4cb2b9Sbouyer TAILQ_HEAD(canpcbqueue, canpcb);
67*6e4cb2b9Sbouyer 
68*6e4cb2b9Sbouyer struct canpcbtable {
69*6e4cb2b9Sbouyer 	struct	canpcbqueue canpt_queue;
70*6e4cb2b9Sbouyer 	struct	canpcbhead *canpt_bindhashtbl;
71*6e4cb2b9Sbouyer 	struct	canpcbhead *canpt_connecthashtbl;
72*6e4cb2b9Sbouyer 	u_long	canpt_bindhash;
73*6e4cb2b9Sbouyer 	u_long	canpt_connecthash;
74*6e4cb2b9Sbouyer };
75*6e4cb2b9Sbouyer 
76*6e4cb2b9Sbouyer /* states in canp_state: */
77*6e4cb2b9Sbouyer #define	CANP_DETACHED		0
78*6e4cb2b9Sbouyer #define	CANP_ATTACHED		1
79*6e4cb2b9Sbouyer #define	CANP_BOUND		2
80*6e4cb2b9Sbouyer #define	CANP_CONNECTED		3
81*6e4cb2b9Sbouyer 
82*6e4cb2b9Sbouyer /* flags in canp_flags: */
83*6e4cb2b9Sbouyer #define CANP_NO_LOOPBACK	0x0001 /* local loopback disabled */
84*6e4cb2b9Sbouyer #define CANP_RECEIVE_OWN	0x0002 /* receive own message */
85*6e4cb2b9Sbouyer 
86*6e4cb2b9Sbouyer 
87*6e4cb2b9Sbouyer #define	sotocanpcb(so)		((struct canpcb *)(so)->so_pcb)
88*6e4cb2b9Sbouyer 
89*6e4cb2b9Sbouyer #ifdef _KERNEL
90*6e4cb2b9Sbouyer void	can_losing(struct canpcb *);
91*6e4cb2b9Sbouyer int	can_pcballoc (struct socket *, void *);
92*6e4cb2b9Sbouyer int	can_pcbbind(void *, struct sockaddr_can *, struct lwp *);
93*6e4cb2b9Sbouyer int	can_pcbconnect(void *, struct sockaddr_can *);
94*6e4cb2b9Sbouyer void	can_pcbdetach(void *);
95*6e4cb2b9Sbouyer void	can_pcbdisconnect(void *);
96*6e4cb2b9Sbouyer void	can_pcbinit(struct canpcbtable *, int, int);
97*6e4cb2b9Sbouyer int	can_pcbnotify(struct canpcbtable *, u_int32_t,
98*6e4cb2b9Sbouyer 	    u_int32_t, int, void (*)(struct canpcb *, int));
99*6e4cb2b9Sbouyer void	can_pcbnotifyall(struct canpcbtable *, u_int32_t, int,
100*6e4cb2b9Sbouyer 	    void (*)(struct canpcb *, int));
101*6e4cb2b9Sbouyer void	can_pcbpurgeif0(struct canpcbtable *, struct ifnet *);
102*6e4cb2b9Sbouyer void	can_pcbpurgeif(struct canpcbtable *, struct ifnet *);
103*6e4cb2b9Sbouyer void	can_pcbstate(struct canpcb *, int);
104*6e4cb2b9Sbouyer void	can_setsockaddr(struct canpcb *, struct sockaddr_can *);
105*6e4cb2b9Sbouyer int	can_pcbsetfilter(struct canpcb *, struct can_filter *, int);
106*6e4cb2b9Sbouyer bool	can_pcbfilter(struct canpcb *, struct mbuf *);
107*6e4cb2b9Sbouyer 
108*6e4cb2b9Sbouyer /* refcount management */
109*6e4cb2b9Sbouyer void	canp_ref(struct canpcb *);
110*6e4cb2b9Sbouyer void	canp_unref(struct canpcb *);
111*6e4cb2b9Sbouyer #endif
112*6e4cb2b9Sbouyer 
113*6e4cb2b9Sbouyer #endif /* _NETCAN_CAN_PCB_H_ */
114