xref: /freebsd-13-stable/sys/netgraph/bluetooth/socket/ng_btsocket.c (revision 4b40a16f0d188422227478889b38cc341d50f88f)
1 /*
2  * ng_btsocket.c
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause
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.c,v 1.4 2003/09/14 23:29:06 max Exp $
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bitstring.h>
38 #include <sys/errno.h>
39 #include <sys/domain.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/mbuf.h>
43 #include <sys/mutex.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/taskqueue.h>
49 
50 #include <net/vnet.h>
51 
52 #include <netgraph/ng_message.h>
53 #include <netgraph/netgraph.h>
54 #include <netgraph/bluetooth/include/ng_bluetooth.h>
55 #include <netgraph/bluetooth/include/ng_hci.h>
56 #include <netgraph/bluetooth/include/ng_l2cap.h>
57 #include <netgraph/bluetooth/include/ng_btsocket.h>
58 #include <netgraph/bluetooth/include/ng_btsocket_hci_raw.h>
59 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
60 #include <netgraph/bluetooth/include/ng_btsocket_rfcomm.h>
61 #include <netgraph/bluetooth/include/ng_btsocket_sco.h>
62 
63 static int			ng_btsocket_modevent (module_t, int, void *);
64 static struct domain		ng_btsocket_domain;
65 
66 /*
67  * Bluetooth raw HCI sockets
68  */
69 
70 static struct pr_usrreqs	ng_btsocket_hci_raw_usrreqs = {
71 	.pru_abort =		ng_btsocket_hci_raw_abort,
72 	.pru_attach =		ng_btsocket_hci_raw_attach,
73 	.pru_bind =		ng_btsocket_hci_raw_bind,
74 	.pru_connect =		ng_btsocket_hci_raw_connect,
75 	.pru_control =		ng_btsocket_hci_raw_control,
76 	.pru_detach =		ng_btsocket_hci_raw_detach,
77 	.pru_disconnect =	ng_btsocket_hci_raw_disconnect,
78 	.pru_peeraddr =		ng_btsocket_hci_raw_peeraddr,
79 	.pru_send =		ng_btsocket_hci_raw_send,
80 	.pru_shutdown =		NULL,
81 	.pru_sockaddr =		ng_btsocket_hci_raw_sockaddr,
82 	.pru_close =		ng_btsocket_hci_raw_close,
83 };
84 
85 /*
86  * Bluetooth raw L2CAP sockets
87  */
88 
89 static struct pr_usrreqs	ng_btsocket_l2cap_raw_usrreqs = {
90 	.pru_abort =		ng_btsocket_l2cap_raw_abort,
91 	.pru_attach =		ng_btsocket_l2cap_raw_attach,
92 	.pru_bind =		ng_btsocket_l2cap_raw_bind,
93 	.pru_connect =		ng_btsocket_l2cap_raw_connect,
94 	.pru_control =		ng_btsocket_l2cap_raw_control,
95 	.pru_detach =		ng_btsocket_l2cap_raw_detach,
96 	.pru_disconnect =	ng_btsocket_l2cap_raw_disconnect,
97 	.pru_peeraddr =		ng_btsocket_l2cap_raw_peeraddr,
98 	.pru_send =		ng_btsocket_l2cap_raw_send,
99 	.pru_shutdown =		NULL,
100 	.pru_sockaddr =		ng_btsocket_l2cap_raw_sockaddr,
101 	.pru_close =		ng_btsocket_l2cap_raw_close,
102 };
103 
104 /*
105  * Bluetooth SEQPACKET L2CAP sockets
106  */
107 
108 static struct pr_usrreqs	ng_btsocket_l2cap_usrreqs = {
109 	.pru_abort =		ng_btsocket_l2cap_abort,
110 	.pru_accept =		ng_btsocket_l2cap_accept,
111 	.pru_attach =		ng_btsocket_l2cap_attach,
112 	.pru_bind =		ng_btsocket_l2cap_bind,
113 	.pru_connect =		ng_btsocket_l2cap_connect,
114 	.pru_control =		ng_btsocket_l2cap_control,
115 	.pru_detach =		ng_btsocket_l2cap_detach,
116 	.pru_disconnect =	ng_btsocket_l2cap_disconnect,
117         .pru_listen =		ng_btsocket_l2cap_listen,
118 	.pru_peeraddr =		ng_btsocket_l2cap_peeraddr,
119 	.pru_send =		ng_btsocket_l2cap_send,
120 	.pru_shutdown =		NULL,
121 	.pru_sockaddr =		ng_btsocket_l2cap_sockaddr,
122 	.pru_close =		ng_btsocket_l2cap_close,
123 };
124 
125 /*
126  * Bluetooth STREAM RFCOMM sockets
127  */
128 
129 static struct pr_usrreqs	ng_btsocket_rfcomm_usrreqs = {
130 	.pru_abort =		ng_btsocket_rfcomm_abort,
131 	.pru_accept =		ng_btsocket_rfcomm_accept,
132 	.pru_attach =		ng_btsocket_rfcomm_attach,
133 	.pru_bind =		ng_btsocket_rfcomm_bind,
134 	.pru_connect =		ng_btsocket_rfcomm_connect,
135 	.pru_control =		ng_btsocket_rfcomm_control,
136 	.pru_detach =		ng_btsocket_rfcomm_detach,
137 	.pru_disconnect =	ng_btsocket_rfcomm_disconnect,
138         .pru_listen =		ng_btsocket_rfcomm_listen,
139 	.pru_peeraddr =		ng_btsocket_rfcomm_peeraddr,
140 	.pru_send =		ng_btsocket_rfcomm_send,
141 	.pru_shutdown =		NULL,
142 	.pru_sockaddr =		ng_btsocket_rfcomm_sockaddr,
143 	.pru_close =		ng_btsocket_rfcomm_close,
144 };
145 
146 /*
147  * Bluetooth SEQPACKET SCO sockets
148  */
149 
150 static struct pr_usrreqs	ng_btsocket_sco_usrreqs = {
151 	.pru_abort =		ng_btsocket_sco_abort,
152 	.pru_accept =		ng_btsocket_sco_accept,
153 	.pru_attach =		ng_btsocket_sco_attach,
154 	.pru_bind =		ng_btsocket_sco_bind,
155 	.pru_connect =		ng_btsocket_sco_connect,
156 	.pru_control =		ng_btsocket_sco_control,
157 	.pru_detach =		ng_btsocket_sco_detach,
158 	.pru_disconnect =	ng_btsocket_sco_disconnect,
159 	.pru_listen =		ng_btsocket_sco_listen,
160 	.pru_peeraddr =		ng_btsocket_sco_peeraddr,
161 	.pru_send =		ng_btsocket_sco_send,
162 	.pru_shutdown =		NULL,
163 	.pru_sockaddr =		ng_btsocket_sco_sockaddr,
164 	.pru_close =		ng_btsocket_sco_close,
165 };
166 
167 /*
168  * Definitions of protocols supported in the BLUETOOTH domain
169  */
170 
171 static struct protosw		ng_btsocket_protosw[] = {
172 {
173 	.pr_type =		SOCK_RAW,
174 	.pr_domain =		&ng_btsocket_domain,
175 	.pr_protocol =		BLUETOOTH_PROTO_HCI,
176 	.pr_flags =		PR_ATOMIC|PR_ADDR,
177 	.pr_ctloutput =		ng_btsocket_hci_raw_ctloutput,
178 	.pr_init =		ng_btsocket_hci_raw_init,
179 	.pr_usrreqs =		&ng_btsocket_hci_raw_usrreqs,
180 },
181 {
182 	.pr_type =		SOCK_RAW,
183 	.pr_domain =		&ng_btsocket_domain,
184 	.pr_protocol =		BLUETOOTH_PROTO_L2CAP,
185 	.pr_flags =		PR_ATOMIC|PR_ADDR,
186 	.pr_init =		ng_btsocket_l2cap_raw_init,
187 	.pr_usrreqs =		&ng_btsocket_l2cap_raw_usrreqs,
188 },
189 {
190 	.pr_type =		SOCK_SEQPACKET,
191 	.pr_domain =		&ng_btsocket_domain,
192 	.pr_protocol =		BLUETOOTH_PROTO_L2CAP,
193 	.pr_flags =		PR_ATOMIC|PR_CONNREQUIRED,
194 	.pr_ctloutput =		ng_btsocket_l2cap_ctloutput,
195 	.pr_init =		ng_btsocket_l2cap_init,
196 	.pr_usrreqs =		&ng_btsocket_l2cap_usrreqs,
197 },
198 {
199 	.pr_type =		SOCK_STREAM,
200 	.pr_domain =		&ng_btsocket_domain,
201 	.pr_protocol =		BLUETOOTH_PROTO_RFCOMM,
202 	.pr_flags =		PR_CONNREQUIRED,
203 	.pr_ctloutput =		ng_btsocket_rfcomm_ctloutput,
204 	.pr_init =		ng_btsocket_rfcomm_init,
205 	.pr_usrreqs =		&ng_btsocket_rfcomm_usrreqs,
206 },
207 {
208 	.pr_type =		SOCK_SEQPACKET,
209 	.pr_domain =		&ng_btsocket_domain,
210 	.pr_protocol =		BLUETOOTH_PROTO_SCO,
211 	.pr_flags =		PR_ATOMIC|PR_CONNREQUIRED,
212 	.pr_ctloutput =		ng_btsocket_sco_ctloutput,
213 	.pr_init =		ng_btsocket_sco_init,
214 	.pr_usrreqs =		&ng_btsocket_sco_usrreqs,
215 },
216 };
217 
218 #define ng_btsocket_protosw_end \
219 	&ng_btsocket_protosw[nitems(ng_btsocket_protosw)]
220 
221 /*
222  * BLUETOOTH domain
223  */
224 
225 static struct domain ng_btsocket_domain = {
226 	.dom_family =		AF_BLUETOOTH,
227 	.dom_name =		"bluetooth",
228 	.dom_protosw =		ng_btsocket_protosw,
229 	.dom_protoswNPROTOSW =	ng_btsocket_protosw_end
230 };
231 
232 /*
233  * Socket sysctl tree
234  */
235 
236 SYSCTL_NODE(_net_bluetooth_hci, OID_AUTO, sockets,
237     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
238     "Bluetooth HCI sockets family");
239 SYSCTL_NODE(_net_bluetooth_l2cap, OID_AUTO, sockets,
240     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
241     "Bluetooth L2CAP sockets family");
242 SYSCTL_NODE(_net_bluetooth_rfcomm, OID_AUTO, sockets,
243     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
244     "Bluetooth RFCOMM sockets family");
245 SYSCTL_NODE(_net_bluetooth_sco, OID_AUTO, sockets,
246     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
247     "Bluetooth SCO sockets family");
248 
249 /*
250  * Module
251  */
252 
253 static moduledata_t	ng_btsocket_mod = {
254 	"ng_btsocket",
255 	ng_btsocket_modevent,
256 	NULL
257 };
258 
259 DECLARE_MODULE(ng_btsocket, ng_btsocket_mod, SI_SUB_PROTO_DOMAIN,
260 	SI_ORDER_ANY);
261 MODULE_VERSION(ng_btsocket, NG_BLUETOOTH_VERSION);
262 MODULE_DEPEND(ng_btsocket, ng_bluetooth, NG_BLUETOOTH_VERSION,
263 	NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
264 MODULE_DEPEND(ng_btsocket, netgraph, NG_ABI_VERSION,
265 	NG_ABI_VERSION, NG_ABI_VERSION);
266 
267 /*
268  * Handle loading and unloading for this node type.
269  * This is to handle auxiliary linkages (e.g protocol domain addition).
270  */
271 
272 static int
ng_btsocket_modevent(module_t mod,int event,void * data)273 ng_btsocket_modevent(module_t mod, int event, void *data)
274 {
275 	int	error = 0;
276 
277 	switch (event) {
278 	case MOD_LOAD:
279 		break;
280 
281 	case MOD_UNLOAD:
282 		/* XXX can't unload protocol domain yet */
283 		error = EBUSY;
284 		break;
285 
286 	default:
287 		error = EOPNOTSUPP;
288 		break;
289 	}
290 
291 	return (error);
292 } /* ng_btsocket_modevent */
293 
294 VNET_DOMAIN_SET(ng_btsocket_);
295