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