xref: /freebsd/sys/netgraph/bluetooth/socket/ng_btsocket.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*
2  * ng_btsocket.c
3  *
4  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: ng_btsocket.c,v 1.4 2003/09/14 23:29:06 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bitstring.h>
35 #include <sys/errno.h>
36 #include <sys/domain.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/mbuf.h>
40 #include <sys/mutex.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/taskqueue.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/netgraph.h>
48 #include <netgraph/bluetooth/include/ng_bluetooth.h>
49 #include <netgraph/bluetooth/include/ng_hci.h>
50 #include <netgraph/bluetooth/include/ng_l2cap.h>
51 #include <netgraph/bluetooth/include/ng_btsocket.h>
52 #include <netgraph/bluetooth/include/ng_btsocket_hci_raw.h>
53 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
54 #include <netgraph/bluetooth/include/ng_btsocket_rfcomm.h>
55 
56 static int			ng_btsocket_modevent (module_t, int, void *);
57 extern struct domain		ng_btsocket_domain;
58 
59 /*
60  * Bluetooth raw HCI sockets
61  */
62 
63 static struct pr_usrreqs	ng_btsocket_hci_raw_usrreqs = {
64 	ng_btsocket_hci_raw_abort,	/* abort */
65 	pru_accept_notsupp,		/* accept */
66 	ng_btsocket_hci_raw_attach,	/* attach */
67 	ng_btsocket_hci_raw_bind,	/* bind */
68 	ng_btsocket_hci_raw_connect,	/* connect */
69 	pru_connect2_notsupp,		/* connect2 */
70 	ng_btsocket_hci_raw_control,	/* control */
71 	ng_btsocket_hci_raw_detach,	/* detach */
72 	ng_btsocket_hci_raw_disconnect,	/* disconnect */
73 	pru_listen_notsupp,		/* listen */
74 	ng_btsocket_hci_raw_peeraddr,	/* peeraddr */
75 	pru_rcvd_notsupp,		/* rcvd */
76 	pru_rcvoob_notsupp,		/* rcvoob */
77 	ng_btsocket_hci_raw_send,	/* send */
78 	pru_sense_null,			/* send */
79 	NULL,				/* shutdown */
80 	ng_btsocket_hci_raw_sockaddr,	/* sockaddr */
81 	sosend,
82 	soreceive,
83 	sopoll,
84 	pru_sosetlabel_null
85 };
86 
87 /*
88  * Bluetooth raw L2CAP sockets
89  */
90 
91 static struct pr_usrreqs	ng_btsocket_l2cap_raw_usrreqs = {
92 	ng_btsocket_l2cap_raw_abort,	/* abort */
93 	pru_accept_notsupp,		/* accept */
94 	ng_btsocket_l2cap_raw_attach,	/* attach */
95 	ng_btsocket_l2cap_raw_bind,	/* bind */
96 	ng_btsocket_l2cap_raw_connect,	/* connect */
97 	pru_connect2_notsupp,		/* connect2 */
98 	ng_btsocket_l2cap_raw_control,	/* control */
99 	ng_btsocket_l2cap_raw_detach,	/* detach */
100 	ng_btsocket_l2cap_raw_disconnect, /* disconnect */
101         pru_listen_notsupp,		/* listen */
102 	ng_btsocket_l2cap_raw_peeraddr,	/* peeraddr */
103 	pru_rcvd_notsupp,		/* rcvd */
104 	pru_rcvoob_notsupp,		/* rcvoob */
105 	ng_btsocket_l2cap_raw_send,	/* send */
106 	pru_sense_null,			/* send */
107 	NULL,				/* shutdown */
108 	ng_btsocket_l2cap_raw_sockaddr,	/* sockaddr */
109 	sosend,
110 	soreceive,
111 	sopoll,
112 	pru_sosetlabel_null
113 };
114 
115 /*
116  * Bluetooth SEQPACKET L2CAP sockets
117  */
118 
119 static struct pr_usrreqs	ng_btsocket_l2cap_usrreqs = {
120 	ng_btsocket_l2cap_abort,	/* abort */
121 	ng_btsocket_l2cap_accept,	/* accept */
122 	ng_btsocket_l2cap_attach,	/* attach */
123 	ng_btsocket_l2cap_bind,		/* bind */
124 	ng_btsocket_l2cap_connect,	/* connect */
125 	pru_connect2_notsupp,		/* connect2 */
126 	ng_btsocket_l2cap_control,	/* control */
127 	ng_btsocket_l2cap_detach,	/* detach */
128 	ng_btsocket_l2cap_disconnect,	/* disconnect */
129         ng_btsocket_l2cap_listen,	/* listen */
130 	ng_btsocket_l2cap_peeraddr,	/* peeraddr */
131 	pru_rcvd_notsupp,		/* rcvd */
132 	pru_rcvoob_notsupp,		/* rcvoob */
133 	ng_btsocket_l2cap_send,		/* send */
134 	pru_sense_null,			/* send */
135 	NULL,				/* shutdown */
136 	ng_btsocket_l2cap_sockaddr,	/* sockaddr */
137 	sosend,
138 	soreceive,
139 	sopoll,
140 	pru_sosetlabel_null
141 };
142 
143 /*
144  * Bluetooth STREAM RFCOMM sockets
145  */
146 
147 static struct pr_usrreqs	ng_btsocket_rfcomm_usrreqs = {
148 	ng_btsocket_rfcomm_abort,	/* abort */
149 	ng_btsocket_rfcomm_accept,	/* accept */
150 	ng_btsocket_rfcomm_attach,	/* attach */
151 	ng_btsocket_rfcomm_bind,	/* bind */
152 	ng_btsocket_rfcomm_connect,	/* connect */
153 	pru_connect2_notsupp,		/* connect2 */
154 	ng_btsocket_rfcomm_control,	/* control */
155 	ng_btsocket_rfcomm_detach,	/* detach */
156 	ng_btsocket_rfcomm_disconnect,	/* disconnect */
157         ng_btsocket_rfcomm_listen,	/* listen */
158 	ng_btsocket_rfcomm_peeraddr,	/* peeraddr */
159 	pru_rcvd_notsupp,		/* rcvd */
160 	pru_rcvoob_notsupp,		/* rcvoob */
161 	ng_btsocket_rfcomm_send,	/* send */
162 	pru_sense_null,			/* send */
163 	NULL,				/* shutdown */
164 	ng_btsocket_rfcomm_sockaddr,	/* sockaddr */
165 	sosend,
166 	soreceive,
167 	sopoll,
168 	pru_sosetlabel_null
169 };
170 
171 /*
172  * Definitions of protocols supported in the BLUETOOTH domain
173  */
174 
175 static struct protosw		ng_btsocket_protosw[] = {
176 {
177 	SOCK_RAW,			/* protocol type */
178 	&ng_btsocket_domain,		/* backpointer to domain */
179 	BLUETOOTH_PROTO_HCI,		/* protocol */
180 	PR_ATOMIC | PR_ADDR,		/* flags */
181 	NULL, NULL, NULL,		/* input, output, ctlinput */
182 	ng_btsocket_hci_raw_ctloutput,	/* ctloutput */
183 	NULL,				/* ousrreq() */
184 	ng_btsocket_hci_raw_init,	/* init */
185 	NULL, NULL, NULL,		/* fasttimeo, slowtimo, drain */
186 	&ng_btsocket_hci_raw_usrreqs,	/* usrreq table (above) */
187 	/* { NULL } */			/* pfh (protocol filter head?) */
188 },
189 {
190 	SOCK_RAW,			/* protocol type */
191 	&ng_btsocket_domain,		/* backpointer to domain */
192 	BLUETOOTH_PROTO_L2CAP,		/* protocol */
193 	PR_ATOMIC | PR_ADDR,		/* flags */
194 	NULL, NULL, NULL,		/* input, output, ctlinput */
195 	NULL,				/* ctloutput */
196 	NULL,				/* ousrreq() */
197 	ng_btsocket_l2cap_raw_init,	/* init */
198 	NULL, NULL, NULL,		/* fasttimeo, slowtimo, drain */
199 	&ng_btsocket_l2cap_raw_usrreqs,	/* usrreq table (above) */
200 	/* { NULL } */			/* pfh (protocol filter head?) */
201 },
202 {
203 	SOCK_SEQPACKET,			/* protocol type */
204 	&ng_btsocket_domain,		/* backpointer to domain */
205 	BLUETOOTH_PROTO_L2CAP,		/* protocol */
206 	PR_ATOMIC | PR_CONNREQUIRED,	/* flags */
207 	NULL, NULL, NULL,		/* input, output, ctlinput */
208 	ng_btsocket_l2cap_ctloutput,	/* ctloutput */
209 	NULL,				/* ousrreq() */
210 	ng_btsocket_l2cap_init,		/* init */
211 	NULL, NULL, NULL,		/* fasttimeo, slowtimo, drain */
212 	&ng_btsocket_l2cap_usrreqs,	/* usrreq table (above) */
213 	/* { NULL } */			/* pfh (protocol filter head?) */
214 },
215 {
216 	SOCK_STREAM,			/* protocol type */
217 	&ng_btsocket_domain,		/* backpointer to domain */
218 	BLUETOOTH_PROTO_RFCOMM,		/* protocol */
219 	PR_ATOMIC | PR_CONNREQUIRED,	/* flags */
220 	NULL, NULL, NULL,		/* input, output, ctlinput */
221 	ng_btsocket_rfcomm_ctloutput,	/* ctloutput */
222 	NULL,				/* ousrreq() */
223 	ng_btsocket_rfcomm_init,	/* init */
224 	NULL, NULL, NULL,		/* fasttimeo, slowtimo, drain */
225 	&ng_btsocket_rfcomm_usrreqs,	/* usrreq table (above) */
226 	/* { NULL } */			/* pfh (protocol filter head?) */
227 }
228 };
229 #define ng_btsocket_protosw_size \
230 	(sizeof(ng_btsocket_protosw)/sizeof(ng_btsocket_protosw[0]))
231 #define ng_btsocket_protosw_end \
232 	&ng_btsocket_protosw[ng_btsocket_protosw_size]
233 
234 /*
235  * BLUETOOTH domain
236  */
237 
238 struct domain			ng_btsocket_domain = {
239 	AF_BLUETOOTH,			/* family */
240 	"bluetooth",			/* domain name */
241 	NULL,				/* init() */
242 	NULL,				/* externalize() */
243 	NULL,				/* dispose() */
244 	ng_btsocket_protosw,		/* protosw entry */
245 	ng_btsocket_protosw_end,	/* end of protosw entries */
246 	NULL,				/* next domain in list */
247 	NULL,				/* rtattach() */
248 	0,				/* arg to rtattach in bits */
249 	0				/* maxrtkey */
250 };
251 
252 /*
253  * Socket sysctl tree
254  */
255 
256 SYSCTL_NODE(_net_bluetooth_hci, OID_AUTO, sockets, CTLFLAG_RW,
257 	0, "Bluetooth HCI sockets family");
258 SYSCTL_NODE(_net_bluetooth_l2cap, OID_AUTO, sockets, CTLFLAG_RW,
259 	0, "Bluetooth L2CAP sockets family");
260 SYSCTL_NODE(_net_bluetooth_rfcomm, OID_AUTO, sockets, CTLFLAG_RW,
261 	0, "Bluetooth RFCOMM sockets family");
262 
263 /*
264  * Module
265  */
266 
267 static moduledata_t	ng_btsocket_mod = {
268 	"ng_btsocket",
269 	ng_btsocket_modevent,
270 	NULL
271 };
272 
273 DECLARE_MODULE(ng_btsocket, ng_btsocket_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
274 MODULE_VERSION(ng_btsocket, NG_BLUETOOTH_VERSION);
275 MODULE_DEPEND(ng_btsocket, ng_bluetooth, NG_BLUETOOTH_VERSION,
276 	NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
277 MODULE_DEPEND(ng_btsocket, netgraph, NG_ABI_VERSION,
278 	NG_ABI_VERSION, NG_ABI_VERSION);
279 
280 /*
281  * Handle loading and unloading for this node type.
282  * This is to handle auxiliary linkages (e.g protocol domain addition).
283  */
284 
285 static int
286 ng_btsocket_modevent(module_t mod, int event, void *data)
287 {
288 	int	error = 0;
289 
290 	switch (event) {
291 	case MOD_LOAD:
292 		net_add_domain(&ng_btsocket_domain);
293 		break;
294 
295 	case MOD_UNLOAD:
296 		/* XXX can't unload protocol domain yet */
297 		error = EBUSY;
298 		break;
299 
300 	default:
301 		error = EOPNOTSUPP;
302 		break;
303 	}
304 
305 	return (error);
306 } /* ng_btsocket_modevent */
307 
308