1 /*
2  * ng_btsocket.c
3  */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: ng_btsocket.c,v 1.4 2003/09/14 23:29:06 max 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 
50 #include <net/vnet.h>
51 
52 #include <netgraph/ng_message.h>
53 #include <netgraph/netgraph.h>
54 #include <netgraph/bluetooth/include/ng_bluetooth.h>
55 #include <netgraph/bluetooth/include/ng_hci.h>
56 #include <netgraph/bluetooth/include/ng_l2cap.h>
57 #include <netgraph/bluetooth/include/ng_btsocket.h>
58 #include <netgraph/bluetooth/include/ng_btsocket_hci_raw.h>
59 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
60 #include <netgraph/bluetooth/include/ng_btsocket_rfcomm.h>
61 #include <netgraph/bluetooth/include/ng_btsocket_sco.h>
62 
63 static int			ng_btsocket_modevent (module_t, int, void *);
64 
65 /*
66  * Definitions of protocols supported in the BLUETOOTH domain
67  */
68 
69 /* Bluetooth raw HCI sockets */
70 static struct protosw ng_btsocket_hci_raw_protosw = {
71 	.pr_type =		SOCK_RAW,
72 	.pr_protocol =		BLUETOOTH_PROTO_HCI,
73 	.pr_flags =		PR_ATOMIC|PR_ADDR,
74 	.pr_ctloutput =		ng_btsocket_hci_raw_ctloutput,
75 	.pr_abort =		ng_btsocket_hci_raw_abort,
76 	.pr_attach =		ng_btsocket_hci_raw_attach,
77 	.pr_bind =		ng_btsocket_hci_raw_bind,
78 	.pr_connect =		ng_btsocket_hci_raw_connect,
79 	.pr_control =		ng_btsocket_hci_raw_control,
80 	.pr_detach =		ng_btsocket_hci_raw_detach,
81 	.pr_disconnect =	ng_btsocket_hci_raw_disconnect,
82 	.pr_peeraddr =		ng_btsocket_hci_raw_sockaddr,
83 	.pr_send =		ng_btsocket_hci_raw_send,
84 	.pr_sockaddr =		ng_btsocket_hci_raw_sockaddr,
85 	.pr_close =		ng_btsocket_hci_raw_close,
86 };
87 
88 /* Bluetooth raw L2CAP sockets */
89 static struct protosw ng_btsocket_l2cap_raw_protosw = {
90 	.pr_type =		SOCK_RAW,
91 	.pr_protocol =		BLUETOOTH_PROTO_L2CAP,
92 	.pr_flags =		PR_ATOMIC|PR_ADDR,
93 	.pr_abort =		ng_btsocket_l2cap_raw_abort,
94 	.pr_attach =		ng_btsocket_l2cap_raw_attach,
95 	.pr_bind =		ng_btsocket_l2cap_raw_bind,
96 	.pr_connect =		ng_btsocket_l2cap_raw_connect,
97 	.pr_control =		ng_btsocket_l2cap_raw_control,
98 	.pr_detach =		ng_btsocket_l2cap_raw_detach,
99 	.pr_disconnect =	ng_btsocket_l2cap_raw_disconnect,
100 	.pr_peeraddr =		ng_btsocket_l2cap_raw_peeraddr,
101 	.pr_send =		ng_btsocket_l2cap_raw_send,
102 	.pr_sockaddr =		ng_btsocket_l2cap_raw_sockaddr,
103 	.pr_close =		ng_btsocket_l2cap_raw_close,
104 };
105 
106 /* Bluetooth SEQPACKET L2CAP sockets */
107 static struct protosw ng_btsocket_l2cap_protosw = {
108 	.pr_type =		SOCK_SEQPACKET,
109 	.pr_protocol =		BLUETOOTH_PROTO_L2CAP,
110 	.pr_flags =		PR_ATOMIC|PR_CONNREQUIRED,
111 	.pr_ctloutput =		ng_btsocket_l2cap_ctloutput,
112 	.pr_abort =		ng_btsocket_l2cap_abort,
113 	.pr_accept =		ng_btsocket_l2cap_peeraddr,
114 	.pr_attach =		ng_btsocket_l2cap_attach,
115 	.pr_bind =		ng_btsocket_l2cap_bind,
116 	.pr_connect =		ng_btsocket_l2cap_connect,
117 	.pr_control =		ng_btsocket_l2cap_control,
118 	.pr_detach =		ng_btsocket_l2cap_detach,
119 	.pr_disconnect =	ng_btsocket_l2cap_disconnect,
120         .pr_listen =		ng_btsocket_l2cap_listen,
121 	.pr_peeraddr =		ng_btsocket_l2cap_peeraddr,
122 	.pr_send =		ng_btsocket_l2cap_send,
123 	.pr_sockaddr =		ng_btsocket_l2cap_sockaddr,
124 	.pr_close =		ng_btsocket_l2cap_close,
125 };
126 
127 /* Bluetooth STREAM RFCOMM sockets */
128 static struct protosw ng_btsocket_rfcomm_protosw = {
129 	.pr_type =		SOCK_STREAM,
130 	.pr_protocol =		BLUETOOTH_PROTO_RFCOMM,
131 	.pr_flags =		PR_CONNREQUIRED,
132 	.pr_ctloutput =		ng_btsocket_rfcomm_ctloutput,
133 	.pr_abort =		ng_btsocket_rfcomm_abort,
134 	.pr_accept =		ng_btsocket_rfcomm_peeraddr,
135 	.pr_attach =		ng_btsocket_rfcomm_attach,
136 	.pr_bind =		ng_btsocket_rfcomm_bind,
137 	.pr_connect =		ng_btsocket_rfcomm_connect,
138 	.pr_control =		ng_btsocket_rfcomm_control,
139 	.pr_detach =		ng_btsocket_rfcomm_detach,
140 	.pr_disconnect =	ng_btsocket_rfcomm_disconnect,
141         .pr_listen =		ng_btsocket_rfcomm_listen,
142 	.pr_peeraddr =		ng_btsocket_rfcomm_peeraddr,
143 	.pr_send =		ng_btsocket_rfcomm_send,
144 	.pr_sockaddr =		ng_btsocket_rfcomm_sockaddr,
145 	.pr_close =		ng_btsocket_rfcomm_close,
146 };
147 
148 /* Bluetooth SEQPACKET SCO sockets */
149 static struct protosw ng_btsocket_sco_protosw = {
150 	.pr_type =		SOCK_SEQPACKET,
151 	.pr_protocol =		BLUETOOTH_PROTO_SCO,
152 	.pr_flags =		PR_ATOMIC|PR_CONNREQUIRED,
153 	.pr_ctloutput =		ng_btsocket_sco_ctloutput,
154 	.pr_abort =		ng_btsocket_sco_abort,
155 	.pr_accept =		ng_btsocket_sco_peeraddr,
156 	.pr_attach =		ng_btsocket_sco_attach,
157 	.pr_bind =		ng_btsocket_sco_bind,
158 	.pr_connect =		ng_btsocket_sco_connect,
159 	.pr_control =		ng_btsocket_sco_control,
160 	.pr_detach =		ng_btsocket_sco_detach,
161 	.pr_disconnect =	ng_btsocket_sco_disconnect,
162 	.pr_listen =		ng_btsocket_sco_listen,
163 	.pr_peeraddr =		ng_btsocket_sco_peeraddr,
164 	.pr_send =		ng_btsocket_sco_send,
165 	.pr_sockaddr =		ng_btsocket_sco_sockaddr,
166 	.pr_close =		ng_btsocket_sco_close,
167 };
168 
169 /*
170  * BLUETOOTH domain
171  */
172 
173 static struct domain ng_btsocket_domain = {
174 	.dom_family =		AF_BLUETOOTH,
175 	.dom_name =		"bluetooth",
176 	.dom_nprotosw =		5,
177 	.dom_protosw = {
178 		&ng_btsocket_hci_raw_protosw,
179 		&ng_btsocket_l2cap_raw_protosw,
180 		&ng_btsocket_l2cap_protosw,
181 		&ng_btsocket_rfcomm_protosw,
182 		&ng_btsocket_sco_protosw,
183 	},
184 };
185 
186 /*
187  * Socket sysctl tree
188  */
189 
190 SYSCTL_NODE(_net_bluetooth_hci, OID_AUTO, sockets,
191     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
192     "Bluetooth HCI sockets family");
193 SYSCTL_NODE(_net_bluetooth_l2cap, OID_AUTO, sockets,
194     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
195     "Bluetooth L2CAP sockets family");
196 SYSCTL_NODE(_net_bluetooth_rfcomm, OID_AUTO, sockets,
197     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
198     "Bluetooth RFCOMM sockets family");
199 SYSCTL_NODE(_net_bluetooth_sco, OID_AUTO, sockets,
200     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
201     "Bluetooth SCO sockets family");
202 
203 /*
204  * Module
205  */
206 
207 static moduledata_t	ng_btsocket_mod = {
208 	"ng_btsocket",
209 	ng_btsocket_modevent,
210 	NULL
211 };
212 
213 DECLARE_MODULE(ng_btsocket, ng_btsocket_mod, SI_SUB_PROTO_DOMAIN,
214 	SI_ORDER_ANY);
215 MODULE_VERSION(ng_btsocket, NG_BLUETOOTH_VERSION);
216 MODULE_DEPEND(ng_btsocket, ng_bluetooth, NG_BLUETOOTH_VERSION,
217 	NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
218 MODULE_DEPEND(ng_btsocket, netgraph, NG_ABI_VERSION,
219 	NG_ABI_VERSION, NG_ABI_VERSION);
220 
221 /*
222  * Handle loading and unloading for this node type.
223  * This is to handle auxiliary linkages (e.g protocol domain addition).
224  */
225 
226 static int
227 ng_btsocket_modevent(module_t mod, int event, void *data)
228 {
229 	int	error = 0;
230 
231 	switch (event) {
232 	case MOD_LOAD:
233 		break;
234 
235 	case MOD_UNLOAD:
236 		/* XXX can't unload protocol domain yet */
237 		error = EBUSY;
238 		break;
239 
240 	default:
241 		error = EOPNOTSUPP;
242 		break;
243 	}
244 
245 	return (error);
246 } /* ng_btsocket_modevent */
247 
248 DOMAIN_SET(ng_btsocket_);
249