xref: /freebsd/sys/netgraph/bluetooth/socket/ng_btsocket.c (revision 6b7b2d80ed4d728d3ffd12c422e57798c1b63a84)
1 /*
2  * ng_btsocket.c
3  */
4 
5 /*-
6  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: ng_btsocket.c,v 1.4 2003/09/14 23:29:06 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bitstring.h>
37 #include <sys/errno.h>
38 #include <sys/domain.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/mbuf.h>
42 #include <sys/mutex.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/taskqueue.h>
48 
49 #include <net/vnet.h>
50 
51 #include <netgraph/ng_message.h>
52 #include <netgraph/netgraph.h>
53 #include <netgraph/bluetooth/include/ng_bluetooth.h>
54 #include <netgraph/bluetooth/include/ng_hci.h>
55 #include <netgraph/bluetooth/include/ng_l2cap.h>
56 #include <netgraph/bluetooth/include/ng_btsocket.h>
57 #include <netgraph/bluetooth/include/ng_btsocket_hci_raw.h>
58 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
59 #include <netgraph/bluetooth/include/ng_btsocket_rfcomm.h>
60 #include <netgraph/bluetooth/include/ng_btsocket_sco.h>
61 
62 static int			ng_btsocket_modevent (module_t, int, void *);
63 static struct domain		ng_btsocket_domain;
64 
65 /*
66  * Bluetooth raw HCI sockets
67  */
68 
69 static struct pr_usrreqs	ng_btsocket_hci_raw_usrreqs = {
70 	.pru_abort =		ng_btsocket_hci_raw_abort,
71 	.pru_attach =		ng_btsocket_hci_raw_attach,
72 	.pru_bind =		ng_btsocket_hci_raw_bind,
73 	.pru_connect =		ng_btsocket_hci_raw_connect,
74 	.pru_control =		ng_btsocket_hci_raw_control,
75 	.pru_detach =		ng_btsocket_hci_raw_detach,
76 	.pru_disconnect =	ng_btsocket_hci_raw_disconnect,
77 	.pru_peeraddr =		ng_btsocket_hci_raw_peeraddr,
78 	.pru_send =		ng_btsocket_hci_raw_send,
79 	.pru_shutdown =		NULL,
80 	.pru_sockaddr =		ng_btsocket_hci_raw_sockaddr,
81 	.pru_close =		ng_btsocket_hci_raw_close,
82 };
83 
84 /*
85  * Bluetooth raw L2CAP sockets
86  */
87 
88 static struct pr_usrreqs	ng_btsocket_l2cap_raw_usrreqs = {
89 	.pru_abort =		ng_btsocket_l2cap_raw_abort,
90 	.pru_attach =		ng_btsocket_l2cap_raw_attach,
91 	.pru_bind =		ng_btsocket_l2cap_raw_bind,
92 	.pru_connect =		ng_btsocket_l2cap_raw_connect,
93 	.pru_control =		ng_btsocket_l2cap_raw_control,
94 	.pru_detach =		ng_btsocket_l2cap_raw_detach,
95 	.pru_disconnect =	ng_btsocket_l2cap_raw_disconnect,
96 	.pru_peeraddr =		ng_btsocket_l2cap_raw_peeraddr,
97 	.pru_send =		ng_btsocket_l2cap_raw_send,
98 	.pru_shutdown =		NULL,
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_shutdown =		NULL,
120 	.pru_sockaddr =		ng_btsocket_l2cap_sockaddr,
121 	.pru_close =		ng_btsocket_l2cap_close,
122 };
123 
124 /*
125  * Bluetooth STREAM RFCOMM sockets
126  */
127 
128 static struct pr_usrreqs	ng_btsocket_rfcomm_usrreqs = {
129 	.pru_abort =		ng_btsocket_rfcomm_abort,
130 	.pru_accept =		ng_btsocket_rfcomm_accept,
131 	.pru_attach =		ng_btsocket_rfcomm_attach,
132 	.pru_bind =		ng_btsocket_rfcomm_bind,
133 	.pru_connect =		ng_btsocket_rfcomm_connect,
134 	.pru_control =		ng_btsocket_rfcomm_control,
135 	.pru_detach =		ng_btsocket_rfcomm_detach,
136 	.pru_disconnect =	ng_btsocket_rfcomm_disconnect,
137         .pru_listen =		ng_btsocket_rfcomm_listen,
138 	.pru_peeraddr =		ng_btsocket_rfcomm_peeraddr,
139 	.pru_send =		ng_btsocket_rfcomm_send,
140 	.pru_shutdown =		NULL,
141 	.pru_sockaddr =		ng_btsocket_rfcomm_sockaddr,
142 	.pru_close =		ng_btsocket_rfcomm_close,
143 };
144 
145 /*
146  * Bluetooth SEQPACKET SCO sockets
147  */
148 
149 static struct pr_usrreqs	ng_btsocket_sco_usrreqs = {
150 	.pru_abort =		ng_btsocket_sco_abort,
151 	.pru_accept =		ng_btsocket_sco_accept,
152 	.pru_attach =		ng_btsocket_sco_attach,
153 	.pru_bind =		ng_btsocket_sco_bind,
154 	.pru_connect =		ng_btsocket_sco_connect,
155 	.pru_control =		ng_btsocket_sco_control,
156 	.pru_detach =		ng_btsocket_sco_detach,
157 	.pru_disconnect =	ng_btsocket_sco_disconnect,
158 	.pru_listen =		ng_btsocket_sco_listen,
159 	.pru_peeraddr =		ng_btsocket_sco_peeraddr,
160 	.pru_send =		ng_btsocket_sco_send,
161 	.pru_shutdown =		NULL,
162 	.pru_sockaddr =		ng_btsocket_sco_sockaddr,
163 	.pru_close =		ng_btsocket_sco_close,
164 };
165 
166 /*
167  * Definitions of protocols supported in the BLUETOOTH domain
168  */
169 
170 static struct protosw		ng_btsocket_protosw[] = {
171 {
172 	.pr_type =		SOCK_RAW,
173 	.pr_domain =		&ng_btsocket_domain,
174 	.pr_protocol =		BLUETOOTH_PROTO_HCI,
175 	.pr_flags =		PR_ATOMIC|PR_ADDR,
176 	.pr_ctloutput =		ng_btsocket_hci_raw_ctloutput,
177 	.pr_init =		ng_btsocket_hci_raw_init,
178 	.pr_usrreqs =		&ng_btsocket_hci_raw_usrreqs,
179 },
180 {
181 	.pr_type =		SOCK_RAW,
182 	.pr_domain =		&ng_btsocket_domain,
183 	.pr_protocol =		BLUETOOTH_PROTO_L2CAP,
184 	.pr_flags =		PR_ATOMIC|PR_ADDR,
185 	.pr_init =		ng_btsocket_l2cap_raw_init,
186 	.pr_usrreqs =		&ng_btsocket_l2cap_raw_usrreqs,
187 },
188 {
189 	.pr_type =		SOCK_SEQPACKET,
190 	.pr_domain =		&ng_btsocket_domain,
191 	.pr_protocol =		BLUETOOTH_PROTO_L2CAP,
192 	.pr_flags =		PR_ATOMIC|PR_CONNREQUIRED,
193 	.pr_ctloutput =		ng_btsocket_l2cap_ctloutput,
194 	.pr_init =		ng_btsocket_l2cap_init,
195 	.pr_usrreqs =		&ng_btsocket_l2cap_usrreqs,
196 },
197 {
198 	.pr_type =		SOCK_STREAM,
199 	.pr_domain =		&ng_btsocket_domain,
200 	.pr_protocol =		BLUETOOTH_PROTO_RFCOMM,
201 	.pr_flags =		PR_CONNREQUIRED,
202 	.pr_ctloutput =		ng_btsocket_rfcomm_ctloutput,
203 	.pr_init =		ng_btsocket_rfcomm_init,
204 	.pr_usrreqs =		&ng_btsocket_rfcomm_usrreqs,
205 },
206 {
207 	.pr_type =		SOCK_SEQPACKET,
208 	.pr_domain =		&ng_btsocket_domain,
209 	.pr_protocol =		BLUETOOTH_PROTO_SCO,
210 	.pr_flags =		PR_ATOMIC|PR_CONNREQUIRED,
211 	.pr_ctloutput =		ng_btsocket_sco_ctloutput,
212 	.pr_init =		ng_btsocket_sco_init,
213 	.pr_usrreqs =		&ng_btsocket_sco_usrreqs,
214 },
215 };
216 #define ng_btsocket_protosw_size \
217 	(sizeof(ng_btsocket_protosw)/sizeof(ng_btsocket_protosw[0]))
218 #define ng_btsocket_protosw_end \
219 	&ng_btsocket_protosw[ng_btsocket_protosw_size]
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, CTLFLAG_RW,
237 	0, "Bluetooth HCI sockets family");
238 SYSCTL_NODE(_net_bluetooth_l2cap, OID_AUTO, sockets, CTLFLAG_RW,
239 	0, "Bluetooth L2CAP sockets family");
240 SYSCTL_NODE(_net_bluetooth_rfcomm, OID_AUTO, sockets, CTLFLAG_RW,
241 	0, "Bluetooth RFCOMM sockets family");
242 SYSCTL_NODE(_net_bluetooth_sco, OID_AUTO, sockets, CTLFLAG_RW,
243 	0, "Bluetooth SCO sockets family");
244 
245 /*
246  * Module
247  */
248 
249 static moduledata_t	ng_btsocket_mod = {
250 	"ng_btsocket",
251 	ng_btsocket_modevent,
252 	NULL
253 };
254 
255 DECLARE_MODULE(ng_btsocket, ng_btsocket_mod, SI_SUB_PROTO_DOMAIN,
256 	SI_ORDER_ANY);
257 MODULE_VERSION(ng_btsocket, NG_BLUETOOTH_VERSION);
258 MODULE_DEPEND(ng_btsocket, ng_bluetooth, NG_BLUETOOTH_VERSION,
259 	NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
260 MODULE_DEPEND(ng_btsocket, netgraph, NG_ABI_VERSION,
261 	NG_ABI_VERSION, NG_ABI_VERSION);
262 
263 /*
264  * Handle loading and unloading for this node type.
265  * This is to handle auxiliary linkages (e.g protocol domain addition).
266  */
267 
268 static int
269 ng_btsocket_modevent(module_t mod, int event, void *data)
270 {
271 	int	error = 0;
272 
273 	switch (event) {
274 	case MOD_LOAD:
275 		break;
276 
277 	case MOD_UNLOAD:
278 		/* XXX can't unload protocol domain yet */
279 		error = EBUSY;
280 		break;
281 
282 	default:
283 		error = EOPNOTSUPP;
284 		break;
285 	}
286 
287 	return (error);
288 } /* ng_btsocket_modevent */
289 
290 VNET_DOMAIN_SET(ng_btsocket_);
291