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