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