1 /*
2  * ng_btsocket_l2cap.h
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7  *
8  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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  * $Id: ng_btsocket_l2cap.h,v 1.4 2003/03/25 23:53:33 max Exp $
33  * $FreeBSD$
34  */
35 
36 #ifndef _NETGRAPH_BTSOCKET_L2CAP_H_
37 #define _NETGRAPH_BTSOCKET_L2CAP_H_
38 
39 /*
40  * L2CAP routing entry
41  */
42 
43 struct ng_hook;
44 struct ng_message;
45 
46 struct ng_btsocket_l2cap_rtentry {
47 	bdaddr_t				 src;  /* source BD_ADDR */
48 	struct ng_hook				*hook; /* downstream hook */
49 	LIST_ENTRY(ng_btsocket_l2cap_rtentry)	 next; /* link to next */
50 };
51 typedef struct ng_btsocket_l2cap_rtentry	ng_btsocket_l2cap_rtentry_t;
52 typedef struct ng_btsocket_l2cap_rtentry *	ng_btsocket_l2cap_rtentry_p;
53 
54 /*****************************************************************************
55  *****************************************************************************
56  **                          SOCK_RAW L2CAP sockets                         **
57  *****************************************************************************
58  *****************************************************************************/
59 
60 #define NG_BTSOCKET_L2CAP_RAW_SENDSPACE	NG_L2CAP_MTU_DEFAULT
61 #define NG_BTSOCKET_L2CAP_RAW_RECVSPACE	NG_L2CAP_MTU_DEFAULT
62 
63 /*
64  * Bluetooth raw L2CAP socket PCB
65  */
66 
67 struct ng_btsocket_l2cap_raw_pcb {
68 	struct socket				*so;	/* socket */
69 
70 	u_int32_t				 flags; /* flags */
71 #define NG_BTSOCKET_L2CAP_RAW_PRIVILEGED	(1 << 0)
72 
73 	bdaddr_t				 src;	/* source address */
74 	bdaddr_t				 dst;	/* dest address */
75 	uint8_t			 	 	 srctype;/*source addr type*/
76 	uint8_t			 	 	 dsttype;/*source addr type*/
77 	ng_btsocket_l2cap_rtentry_p		 rt;    /* routing info */
78 
79 	u_int32_t				 token;	/* message token */
80 	struct ng_mesg				*msg;   /* message */
81 
82 	struct mtx				 pcb_mtx; /* pcb mutex */
83 
84 	LIST_ENTRY(ng_btsocket_l2cap_raw_pcb)	 next;  /* link to next PCB */
85 };
86 typedef struct ng_btsocket_l2cap_raw_pcb	ng_btsocket_l2cap_raw_pcb_t;
87 typedef struct ng_btsocket_l2cap_raw_pcb *	ng_btsocket_l2cap_raw_pcb_p;
88 
89 #define	so2l2cap_raw_pcb(so) \
90 	((struct ng_btsocket_l2cap_raw_pcb *)((so)->so_pcb))
91 
92 /*
93  * Bluetooth raw L2CAP socket methods
94  */
95 
96 #ifdef _KERNEL
97 
98 void ng_btsocket_l2cap_raw_init       (void);
99 void ng_btsocket_l2cap_raw_abort      (struct socket *);
100 void ng_btsocket_l2cap_raw_close      (struct socket *);
101 int  ng_btsocket_l2cap_raw_attach     (struct socket *, int, struct thread *);
102 int  ng_btsocket_l2cap_raw_bind       (struct socket *, struct sockaddr *,
103                                        struct thread *);
104 int  ng_btsocket_l2cap_raw_connect    (struct socket *, struct sockaddr *,
105                                        struct thread *);
106 int  ng_btsocket_l2cap_raw_control    (struct socket *, u_long, caddr_t,
107                                        struct ifnet *, struct thread *);
108 void ng_btsocket_l2cap_raw_detach     (struct socket *);
109 int  ng_btsocket_l2cap_raw_disconnect (struct socket *);
110 int  ng_btsocket_l2cap_raw_peeraddr   (struct socket *, struct sockaddr **);
111 int  ng_btsocket_l2cap_raw_send       (struct socket *, int, struct mbuf *,
112                                        struct sockaddr *, struct mbuf *,
113                                        struct thread *);
114 int  ng_btsocket_l2cap_raw_sockaddr   (struct socket *, struct sockaddr **);
115 
116 #endif /* _KERNEL */
117 
118 /*****************************************************************************
119  *****************************************************************************
120  **                    SOCK_SEQPACKET L2CAP sockets                         **
121  *****************************************************************************
122  *****************************************************************************/
123 
124 #define NG_BTSOCKET_L2CAP_SENDSPACE	NG_L2CAP_MTU_DEFAULT /* (64 * 1024) */
125 #define NG_BTSOCKET_L2CAP_RECVSPACE	(64 * 1024)
126 
127 /*
128  * Bluetooth L2CAP socket PCB
129  */
130 
131 struct ng_btsocket_l2cap_pcb {
132 	struct socket			*so;	     /* Pointer to socket */
133 
134 	bdaddr_t			 src;	     /* Source address */
135 	bdaddr_t			 dst;	     /* Destination address */
136 	uint8_t			 	 srctype;	/*source addr type*/
137 	uint8_t			 	 dsttype;	/*source addr type*/
138 
139 	u_int16_t			 psm;	     /* PSM */
140 	u_int16_t			 cid;	     /* Local channel ID */
141 	uint8_t				 idtype;
142 	u_int16_t			 flags;      /* socket flags */
143 #define NG_BTSOCKET_L2CAP_CLIENT	(1 << 0)     /* socket is client */
144 #define NG_BTSOCKET_L2CAP_TIMO		(1 << 1)     /* timeout pending */
145 
146 	u_int8_t			 state;      /* socket state */
147 #define NG_BTSOCKET_L2CAP_CLOSED	0            /* socket closed */
148 #define NG_BTSOCKET_L2CAP_CONNECTING	1            /* wait for connect */
149 #define NG_BTSOCKET_L2CAP_CONFIGURING	2            /* wait for config */
150 #define NG_BTSOCKET_L2CAP_OPEN		3            /* socket open */
151 #define NG_BTSOCKET_L2CAP_DISCONNECTING	4            /* wait for disconnect */
152 #define NG_BTSOCKET_L2CAP_W4_ENC_CHANGE 5
153 
154 	u_int8_t			 cfg_state;  /* config state */
155 #define	NG_BTSOCKET_L2CAP_CFG_IN	(1 << 0)     /* incoming path done */
156 #define	NG_BTSOCKET_L2CAP_CFG_OUT	(1 << 1)     /* outgoing path done */
157 #define	NG_BTSOCKET_L2CAP_CFG_BOTH \
158 	(NG_BTSOCKET_L2CAP_CFG_IN | NG_BTSOCKET_L2CAP_CFG_OUT)
159 
160 #define	NG_BTSOCKET_L2CAP_CFG_IN_SENT	(1 << 2)     /* L2CAP ConfigReq sent */
161 #define	NG_BTSOCKET_L2CAP_CFG_OUT_SENT	(1 << 3)     /* ---/--- */
162 	uint8_t 			 encryption;
163 	u_int16_t			 imtu;       /* Incoming MTU */
164 	ng_l2cap_flow_t			 iflow;      /* Input flow spec */
165 
166 	u_int16_t			 omtu;       /* Outgoing MTU */
167 	ng_l2cap_flow_t			 oflow;      /* Outgoing flow spec */
168 
169 	u_int16_t			 flush_timo; /* flush timeout */
170 	u_int16_t			 link_timo;  /* link timeout */
171 
172 	struct callout			 timo;       /* timeout */
173 
174 	u_int32_t			 token;	     /* message token */
175 	ng_btsocket_l2cap_rtentry_p	 rt;         /* routing info */
176 
177 	struct mtx			 pcb_mtx;    /* pcb mutex */
178 	uint16_t			 need_encrypt; /*encryption needed*/
179 
180 	LIST_ENTRY(ng_btsocket_l2cap_pcb) next;      /* link to next PCB */
181 };
182 typedef struct ng_btsocket_l2cap_pcb	ng_btsocket_l2cap_pcb_t;
183 typedef struct ng_btsocket_l2cap_pcb *	ng_btsocket_l2cap_pcb_p;
184 
185 #define	so2l2cap_pcb(so) \
186 	((struct ng_btsocket_l2cap_pcb *)((so)->so_pcb))
187 
188 /*
189  * Bluetooth L2CAP socket methods
190  */
191 
192 #ifdef _KERNEL
193 
194 void ng_btsocket_l2cap_init       (void);
195 void ng_btsocket_l2cap_abort      (struct socket *);
196 void ng_btsocket_l2cap_close      (struct socket *);
197 int  ng_btsocket_l2cap_accept     (struct socket *, struct sockaddr **);
198 int  ng_btsocket_l2cap_attach     (struct socket *, int, struct thread *);
199 int  ng_btsocket_l2cap_bind       (struct socket *, struct sockaddr *,
200                                    struct thread *);
201 int  ng_btsocket_l2cap_connect    (struct socket *, struct sockaddr *,
202                                    struct thread *);
203 int  ng_btsocket_l2cap_control    (struct socket *, u_long, caddr_t,
204                                    struct ifnet *, struct thread *);
205 int  ng_btsocket_l2cap_ctloutput  (struct socket *, struct sockopt *);
206 void ng_btsocket_l2cap_detach     (struct socket *);
207 int  ng_btsocket_l2cap_disconnect (struct socket *);
208 int  ng_btsocket_l2cap_listen     (struct socket *, int, struct thread *);
209 int  ng_btsocket_l2cap_peeraddr   (struct socket *, struct sockaddr **);
210 int  ng_btsocket_l2cap_send       (struct socket *, int, struct mbuf *,
211                                    struct sockaddr *, struct mbuf *,
212                                    struct thread *);
213 int  ng_btsocket_l2cap_sockaddr   (struct socket *, struct sockaddr **);
214 
215 #endif /* _KERNEL */
216 
217 #endif /* _NETGRAPH_BTSOCKET_L2CAP_H_ */
218