xref: /freebsd/sys/netgraph/bluetooth/socket/ng_btsocket.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
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 	.pr_type =		SOCK_RAW,
144 	.pr_domain =		&ng_btsocket_domain,
145 	.pr_protocol =		BLUETOOTH_PROTO_HCI,
146 	.pr_flags =		PR_ATOMIC|PR_ADDR,
147 	.pr_ctloutput =		ng_btsocket_hci_raw_ctloutput,
148 	.pr_init =		ng_btsocket_hci_raw_init,
149 	.pr_usrreqs =		&ng_btsocket_hci_raw_usrreqs,
150 },
151 {
152 	.pr_type =		SOCK_RAW,
153 	.pr_domain =		&ng_btsocket_domain,
154 	.pr_protocol =		BLUETOOTH_PROTO_L2CAP,
155 	.pr_flags =		PR_ATOMIC|PR_ADDR,
156 	.pr_init =		ng_btsocket_l2cap_raw_init,
157 	.pr_usrreqs =		&ng_btsocket_l2cap_raw_usrreqs,
158 },
159 {
160 	.pr_type =		SOCK_SEQPACKET,
161 	.pr_domain =		&ng_btsocket_domain,
162 	.pr_protocol =		BLUETOOTH_PROTO_L2CAP,
163 	.pr_flags =		PR_ATOMIC|PR_CONNREQUIRED,
164 	.pr_ctloutput =		ng_btsocket_l2cap_ctloutput,
165 	.pr_init =		ng_btsocket_l2cap_init,
166 	.pr_usrreqs =		&ng_btsocket_l2cap_usrreqs,
167 },
168 {
169 	.pr_type =		SOCK_STREAM,
170 	.pr_domain =		&ng_btsocket_domain,
171 	.pr_protocol =		BLUETOOTH_PROTO_RFCOMM,
172 	.pr_flags =		PR_CONNREQUIRED,
173 	.pr_ctloutput =		ng_btsocket_rfcomm_ctloutput,
174 	.pr_init =		ng_btsocket_rfcomm_init,
175 	.pr_usrreqs =		&ng_btsocket_rfcomm_usrreqs,
176 }
177 };
178 #define ng_btsocket_protosw_size \
179 	(sizeof(ng_btsocket_protosw)/sizeof(ng_btsocket_protosw[0]))
180 #define ng_btsocket_protosw_end \
181 	&ng_btsocket_protosw[ng_btsocket_protosw_size]
182 
183 /*
184  * BLUETOOTH domain
185  */
186 
187 struct domain			ng_btsocket_domain = {
188 	.dom_family =		AF_BLUETOOTH,
189 	.dom_name =		"bluetooth",
190 	.dom_protosw =		ng_btsocket_protosw,
191 	.dom_protoswNPROTOSW =	ng_btsocket_protosw_end
192 };
193 
194 /*
195  * Socket sysctl tree
196  */
197 
198 SYSCTL_NODE(_net_bluetooth_hci, OID_AUTO, sockets, CTLFLAG_RW,
199 	0, "Bluetooth HCI sockets family");
200 SYSCTL_NODE(_net_bluetooth_l2cap, OID_AUTO, sockets, CTLFLAG_RW,
201 	0, "Bluetooth L2CAP sockets family");
202 SYSCTL_NODE(_net_bluetooth_rfcomm, OID_AUTO, sockets, CTLFLAG_RW,
203 	0, "Bluetooth RFCOMM sockets family");
204 
205 /*
206  * Module
207  */
208 
209 static moduledata_t	ng_btsocket_mod = {
210 	"ng_btsocket",
211 	ng_btsocket_modevent,
212 	NULL
213 };
214 
215 DECLARE_MODULE(ng_btsocket, ng_btsocket_mod, SI_SUB_PROTO_DOMAIN,
216 	SI_ORDER_ANY);
217 MODULE_VERSION(ng_btsocket, NG_BLUETOOTH_VERSION);
218 MODULE_DEPEND(ng_btsocket, ng_bluetooth, NG_BLUETOOTH_VERSION,
219 	NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
220 MODULE_DEPEND(ng_btsocket, netgraph, NG_ABI_VERSION,
221 	NG_ABI_VERSION, NG_ABI_VERSION);
222 
223 /*
224  * Handle loading and unloading for this node type.
225  * This is to handle auxiliary linkages (e.g protocol domain addition).
226  */
227 
228 static int
229 ng_btsocket_modevent(module_t mod, int event, void *data)
230 {
231 	int	error = 0;
232 
233 	switch (event) {
234 	case MOD_LOAD:
235 		net_add_domain(&ng_btsocket_domain);
236 		break;
237 
238 	case MOD_UNLOAD:
239 		/* XXX can't unload protocol domain yet */
240 		error = EBUSY;
241 		break;
242 
243 	default:
244 		error = EOPNOTSUPP;
245 		break;
246 	}
247 
248 	return (error);
249 } /* ng_btsocket_modevent */
250 
251