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