xref: /freebsd/sys/netgraph/bluetooth/socket/ng_btsocket.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
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.20 2002/09/13 17:56:58 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/errno.h>
35 #include <sys/domain.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/sysctl.h>
43 #include <bitstring.h>
44 #include <netgraph/ng_message.h>
45 #include <netgraph/netgraph.h>
46 #include "ng_bluetooth.h"
47 #include "ng_hci.h"
48 #include "ng_l2cap.h"
49 #include "ng_btsocket.h"
50 #include "ng_btsocket_hci_raw.h"
51 #include "ng_btsocket_l2cap.h"
52 
53 static int			ng_btsocket_modevent (module_t, int, void *);
54 extern struct domain		ng_btsocket_domain;
55 
56 /*
57  * Bluetooth raw HCI sockets
58  */
59 
60 static struct pr_usrreqs	ng_btsocket_hci_raw_usrreqs = {
61 	ng_btsocket_hci_raw_abort,	/* abort */
62 	pru_accept_notsupp,		/* accept */
63 	ng_btsocket_hci_raw_attach,	/* attach */
64 	ng_btsocket_hci_raw_bind,	/* bind */
65 	ng_btsocket_hci_raw_connect,	/* connect */
66 	pru_connect2_notsupp,		/* connect2 */
67 	ng_btsocket_hci_raw_control,	/* control */
68 	ng_btsocket_hci_raw_detach,	/* detach */
69 	ng_btsocket_hci_raw_disconnect,	/* disconnect */
70 	pru_listen_notsupp,		/* listen */
71 	ng_btsocket_hci_raw_peeraddr,	/* peeraddr */
72 	pru_rcvd_notsupp,		/* rcvd */
73 	pru_rcvoob_notsupp,		/* rcvoob */
74 	ng_btsocket_hci_raw_send,	/* send */
75 	pru_sense_null,			/* send */
76 	NULL,				/* shutdown */
77 	ng_btsocket_hci_raw_sockaddr,	/* sockaddr */
78 	sosend,
79 	soreceive,
80 	sopoll
81 };
82 
83 /*
84  * Bluetooth raw L2CAP sockets
85  */
86 
87 static struct pr_usrreqs	ng_btsocket_l2cap_raw_usrreqs = {
88 	ng_btsocket_l2cap_raw_abort,	/* abort */
89 	pru_accept_notsupp,		/* accept */
90 	ng_btsocket_l2cap_raw_attach,	/* attach */
91 	ng_btsocket_l2cap_raw_bind,	/* bind */
92 	ng_btsocket_l2cap_raw_connect,	/* connect */
93 	pru_connect2_notsupp,		/* connect2 */
94 	ng_btsocket_l2cap_raw_control,	/* control */
95 	ng_btsocket_l2cap_raw_detach,	/* detach */
96 	ng_btsocket_l2cap_raw_disconnect, /* disconnect */
97         pru_listen_notsupp,		/* listen */
98 	ng_btsocket_l2cap_raw_peeraddr,	/* peeraddr */
99 	pru_rcvd_notsupp,		/* rcvd */
100 	pru_rcvoob_notsupp,		/* rcvoob */
101 	ng_btsocket_l2cap_raw_send,	/* send */
102 	pru_sense_null,			/* send */
103 	NULL,				/* shutdown */
104 	ng_btsocket_l2cap_raw_sockaddr,	/* sockaddr */
105 	sosend,
106 	soreceive,
107 	sopoll
108 };
109 
110 /*
111  * Bluetooth SEQPACKET L2CAP sockets
112  */
113 
114 static struct pr_usrreqs	ng_btsocket_l2cap_usrreqs = {
115 	ng_btsocket_l2cap_abort,	/* abort */
116 	ng_btsocket_l2cap_accept,	/* accept */
117 	ng_btsocket_l2cap_attach,	/* attach */
118 	ng_btsocket_l2cap_bind,		/* bind */
119 	ng_btsocket_l2cap_connect,	/* connect */
120 	pru_connect2_notsupp,		/* connect2 */
121 	ng_btsocket_l2cap_control,	/* control */
122 	ng_btsocket_l2cap_detach,	/* detach */
123 	ng_btsocket_l2cap_disconnect,	/* disconnect */
124         ng_btsocket_l2cap_listen,	/* listen */
125 	ng_btsocket_l2cap_peeraddr,	/* peeraddr */
126 	pru_rcvd_notsupp,		/* rcvd */
127 	pru_rcvoob_notsupp,		/* rcvoob */
128 	ng_btsocket_l2cap_send,		/* send */
129 	pru_sense_null,			/* send */
130 	NULL,				/* shutdown */
131 	ng_btsocket_l2cap_sockaddr,	/* sockaddr */
132 	sosend,
133 	soreceive,
134 	sopoll
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 #define ng_btsocket_protosw_size \
183 	(sizeof(ng_btsocket_protosw)/sizeof(ng_btsocket_protosw[0]))
184 #define ng_btsocket_protosw_end \
185 	&ng_btsocket_protosw[ng_btsocket_protosw_size]
186 
187 /*
188  * BLUETOOTH domain
189  */
190 
191 struct domain			ng_btsocket_domain = {
192 	AF_BLUETOOTH,			/* family */
193 	"bluetooth",			/* domain name */
194 	NULL,				/* init() */
195 	NULL,				/* externalize() */
196 	NULL,				/* dispose() */
197 	ng_btsocket_protosw,		/* protosw entry */
198 	ng_btsocket_protosw_end,	/* end of protosw entries */
199 	NULL,				/* next domain in list */
200 	NULL,				/* rtattach() */
201 	0,				/* arg to rtattach in bits */
202 	0				/* maxrtkey */
203 };
204 
205 /*
206  * Socket sysctl tree
207  */
208 
209 SYSCTL_NODE(_net_bluetooth_hci, OID_AUTO, sockets, CTLFLAG_RW,
210 	0, "Bluetooth HCI sockets family");
211 SYSCTL_NODE(_net_bluetooth_l2cap, OID_AUTO, sockets, CTLFLAG_RW,
212 	0, "Bluetooth L2CAP sockets family");
213 
214 /*
215  * Module
216  */
217 
218 static moduledata_t	ng_btsocket_mod = {
219 	"ng_btsocket",
220 	ng_btsocket_modevent,
221 	NULL
222 };
223 
224 DECLARE_MODULE(ng_btsocket, ng_btsocket_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
225 MODULE_VERSION(ng_btsocket, NG_BLUETOOTH_VERSION);
226 MODULE_DEPEND(ng_btsocket, ng_bluetooth, NG_BLUETOOTH_VERSION,
227 	NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);
228 MODULE_DEPEND(ng_btsocket, netgraph, NG_ABI_VERSION,
229 	NG_ABI_VERSION, NG_ABI_VERSION);
230 
231 /*
232  * Handle loading and unloading for this node type.
233  * This is to handle auxiliary linkages (e.g protocol domain addition).
234  */
235 
236 static int
237 ng_btsocket_modevent(module_t mod, int event, void *data)
238 {
239 	int	error = 0;
240 
241 	switch (event) {
242 	case MOD_LOAD:
243 		net_add_domain(&ng_btsocket_domain);
244 		break;
245 
246 	case MOD_UNLOAD:
247 		/* XXX can't unload protocol domain yet */
248 		error = EBUSY;
249 		break;
250 
251 	default:
252 		error = EOPNOTSUPP;
253 		break;
254 	}
255 
256 	return (error);
257 } /* ng_btsocket_modevent */
258 
259