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: src/sys/netgraph/bluetooth/socket/ng_btsocket.c,v 1.13 2006/07/21 17:11:13 rwatson Exp $
32  * $DragonFly: src/sys/netgraph7/bluetooth/socket/ng_btsocket.c,v 1.2 2008/06/26 23:05:40 dillon Exp $
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bitstring.h>
38 #include <sys/errno.h>
39 #include <sys/domain.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/mbuf.h>
43 #include <sys/mutex.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/taskqueue.h>
49 #include "ng_message.h"
50 #include "netgraph.h"
51 #include "bluetooth/include/ng_bluetooth.h"
52 #include "bluetooth/include/ng_hci.h"
53 #include "bluetooth/include/ng_l2cap.h"
54 #include "bluetooth/include/ng_btsocket.h"
55 #include "bluetooth/include/ng_btsocket_hci_raw.h"
56 #include "bluetooth/include/ng_btsocket_l2cap.h"
57 #include "bluetooth/include/ng_btsocket_rfcomm.h"
58 
59 static int			ng_btsocket_modevent (module_t, int, void *);
60 extern struct domain		ng_btsocket_domain;
61 
62 /*
63  * Bluetooth raw HCI sockets
64  */
65 
66 static struct pr_usrreqs	ng_btsocket_hci_raw_usrreqs = {
67 	.pru_abort =		ng_btsocket_hci_raw_abort,
68 	.pru_attach =		ng_btsocket_hci_raw_attach,
69 	.pru_bind =		ng_btsocket_hci_raw_bind,
70 	.pru_connect =		ng_btsocket_hci_raw_connect,
71 	.pru_control =		ng_btsocket_hci_raw_control,
72 	.pru_detach =		ng_btsocket_hci_raw_detach,
73 	.pru_disconnect =	ng_btsocket_hci_raw_disconnect,
74 	.pru_peeraddr =		ng_btsocket_hci_raw_peeraddr,
75 	.pru_send =		ng_btsocket_hci_raw_send,
76 	.pru_shutdown =		NULL,
77 	.pru_sockaddr =		ng_btsocket_hci_raw_sockaddr,
78 	.pru_close =		ng_btsocket_hci_raw_close,
79 };
80 
81 /*
82  * Bluetooth raw L2CAP sockets
83  */
84 
85 static struct pr_usrreqs	ng_btsocket_l2cap_raw_usrreqs = {
86 	.pru_abort =		ng_btsocket_l2cap_raw_abort,
87 	.pru_attach =		ng_btsocket_l2cap_raw_attach,
88 	.pru_bind =		ng_btsocket_l2cap_raw_bind,
89 	.pru_connect =		ng_btsocket_l2cap_raw_connect,
90 	.pru_control =		ng_btsocket_l2cap_raw_control,
91 	.pru_detach =		ng_btsocket_l2cap_raw_detach,
92 	.pru_disconnect =	ng_btsocket_l2cap_raw_disconnect,
93 	.pru_peeraddr =		ng_btsocket_l2cap_raw_peeraddr,
94 	.pru_send =		ng_btsocket_l2cap_raw_send,
95 	.pru_shutdown =		NULL,
96 	.pru_sockaddr =		ng_btsocket_l2cap_raw_sockaddr,
97 	.pru_close =		ng_btsocket_l2cap_raw_close,
98 };
99 
100 /*
101  * Bluetooth SEQPACKET L2CAP sockets
102  */
103 
104 static struct pr_usrreqs	ng_btsocket_l2cap_usrreqs = {
105 	.pru_abort =		ng_btsocket_l2cap_abort,
106 	.pru_accept =		ng_btsocket_l2cap_accept,
107 	.pru_attach =		ng_btsocket_l2cap_attach,
108 	.pru_bind =		ng_btsocket_l2cap_bind,
109 	.pru_connect =		ng_btsocket_l2cap_connect,
110 	.pru_control =		ng_btsocket_l2cap_control,
111 	.pru_detach =		ng_btsocket_l2cap_detach,
112 	.pru_disconnect =	ng_btsocket_l2cap_disconnect,
113         .pru_listen =		ng_btsocket_l2cap_listen,
114 	.pru_peeraddr =		ng_btsocket_l2cap_peeraddr,
115 	.pru_send =		ng_btsocket_l2cap_send,
116 	.pru_shutdown =		NULL,
117 	.pru_sockaddr =		ng_btsocket_l2cap_sockaddr,
118 	.pru_close =		ng_btsocket_l2cap_close,
119 };
120 
121 /*
122  * Bluetooth STREAM RFCOMM sockets
123  */
124 
125 static struct pr_usrreqs	ng_btsocket_rfcomm_usrreqs = {
126 	.pru_abort =		ng_btsocket_rfcomm_abort,
127 	.pru_accept =		ng_btsocket_rfcomm_accept,
128 	.pru_attach =		ng_btsocket_rfcomm_attach,
129 	.pru_bind =		ng_btsocket_rfcomm_bind,
130 	.pru_connect =		ng_btsocket_rfcomm_connect,
131 	.pru_control =		ng_btsocket_rfcomm_control,
132 	.pru_detach =		ng_btsocket_rfcomm_detach,
133 	.pru_disconnect =	ng_btsocket_rfcomm_disconnect,
134         .pru_listen =		ng_btsocket_rfcomm_listen,
135 	.pru_peeraddr =		ng_btsocket_rfcomm_peeraddr,
136 	.pru_send =		ng_btsocket_rfcomm_send,
137 	.pru_shutdown =		NULL,
138 	.pru_sockaddr =		ng_btsocket_rfcomm_sockaddr,
139 	.pru_close =		ng_btsocket_rfcomm_close,
140 };
141 
142 /*
143  * Definitions of protocols supported in the BLUETOOTH domain
144  */
145 
146 static struct protosw		ng_btsocket_protosw[] = {
147 {
148 	.pr_type =		SOCK_RAW,
149 	.pr_domain =		&ng_btsocket_domain,
150 	.pr_protocol =		BLUETOOTH_PROTO_HCI,
151 	.pr_flags =		PR_ATOMIC|PR_ADDR,
152 	.pr_ctloutput =		ng_btsocket_hci_raw_ctloutput,
153 	.pr_init =		ng_btsocket_hci_raw_init,
154 	.pr_usrreqs =		&ng_btsocket_hci_raw_usrreqs,
155 },
156 {
157 	.pr_type =		SOCK_RAW,
158 	.pr_domain =		&ng_btsocket_domain,
159 	.pr_protocol =		BLUETOOTH_PROTO_L2CAP,
160 	.pr_flags =		PR_ATOMIC|PR_ADDR,
161 	.pr_init =		ng_btsocket_l2cap_raw_init,
162 	.pr_usrreqs =		&ng_btsocket_l2cap_raw_usrreqs,
163 },
164 {
165 	.pr_type =		SOCK_SEQPACKET,
166 	.pr_domain =		&ng_btsocket_domain,
167 	.pr_protocol =		BLUETOOTH_PROTO_L2CAP,
168 	.pr_flags =		PR_ATOMIC|PR_CONNREQUIRED,
169 	.pr_ctloutput =		ng_btsocket_l2cap_ctloutput,
170 	.pr_init =		ng_btsocket_l2cap_init,
171 	.pr_usrreqs =		&ng_btsocket_l2cap_usrreqs,
172 },
173 {
174 	.pr_type =		SOCK_STREAM,
175 	.pr_domain =		&ng_btsocket_domain,
176 	.pr_protocol =		BLUETOOTH_PROTO_RFCOMM,
177 	.pr_flags =		PR_CONNREQUIRED,
178 	.pr_ctloutput =		ng_btsocket_rfcomm_ctloutput,
179 	.pr_init =		ng_btsocket_rfcomm_init,
180 	.pr_usrreqs =		&ng_btsocket_rfcomm_usrreqs,
181 }
182 };
183 #define ng_btsocket_protosw_size \
184 	(sizeof(ng_btsocket_protosw)/sizeof(ng_btsocket_protosw[0]))
185 #define ng_btsocket_protosw_end \
186 	&ng_btsocket_protosw[ng_btsocket_protosw_size]
187 
188 /*
189  * BLUETOOTH domain
190  */
191 
192 struct domain			ng_btsocket_domain = {
193 	.dom_family =		AF_BLUETOOTH,
194 	.dom_name =		"bluetooth",
195 	.dom_protosw =		ng_btsocket_protosw,
196 	.dom_protoswNPROTOSW =	ng_btsocket_protosw_end
197 };
198 
199 /*
200  * Socket sysctl tree
201  */
202 
203 SYSCTL_NODE(_net_bluetooth_hci, OID_AUTO, sockets, CTLFLAG_RW,
204 	0, "Bluetooth HCI sockets family");
205 SYSCTL_NODE(_net_bluetooth_l2cap, OID_AUTO, sockets, CTLFLAG_RW,
206 	0, "Bluetooth L2CAP sockets family");
207 SYSCTL_NODE(_net_bluetooth_rfcomm, OID_AUTO, sockets, CTLFLAG_RW,
208 	0, "Bluetooth RFCOMM sockets family");
209 
210 /*
211  * Module
212  */
213 
214 static moduledata_t	ng_btsocket_mod = {
215 	"ng_btsocket",
216 	ng_btsocket_modevent,
217 	NULL
218 };
219 
220 DECLARE_MODULE(ng_btsocket, ng_btsocket_mod, SI_SUB_PROTO_DOMAIN,
221 	SI_ORDER_ANY);
222 MODULE_VERSION(ng_btsocket, NG_BLUETOOTH_VERSION);
223 MODULE_DEPEND(ng_btsocket, ng_bluetooth, NG_BLUETOOTH_VERSION,
224 	NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
225 MODULE_DEPEND(ng_btsocket, netgraph, NG_ABI_VERSION,
226 	NG_ABI_VERSION, NG_ABI_VERSION);
227 
228 /*
229  * Handle loading and unloading for this node type.
230  * This is to handle auxiliary linkages (e.g protocol domain addition).
231  */
232 
233 static int
234 ng_btsocket_modevent(module_t mod, int event, void *data)
235 {
236 	int	error = 0;
237 
238 	switch (event) {
239 	case MOD_LOAD:
240 		net_add_domain(&ng_btsocket_domain);
241 		break;
242 
243 	case MOD_UNLOAD:
244 		/* XXX can't unload protocol domain yet */
245 		error = EBUSY;
246 		break;
247 
248 	default:
249 		error = EOPNOTSUPP;
250 		break;
251 	}
252 
253 	return (error);
254 } /* ng_btsocket_modevent */
255 
256