1b06ebda0SMatthew Dillon /*
2b06ebda0SMatthew Dillon * ng_btsocket_l2cap.c
3b06ebda0SMatthew Dillon */
4b06ebda0SMatthew Dillon
5b06ebda0SMatthew Dillon /*-
6b06ebda0SMatthew Dillon * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7b06ebda0SMatthew Dillon * All rights reserved.
8b06ebda0SMatthew Dillon *
9b06ebda0SMatthew Dillon * Redistribution and use in source and binary forms, with or without
10b06ebda0SMatthew Dillon * modification, are permitted provided that the following conditions
11b06ebda0SMatthew Dillon * are met:
12b06ebda0SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
13b06ebda0SMatthew Dillon * notice, this list of conditions and the following disclaimer.
14b06ebda0SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
15b06ebda0SMatthew Dillon * notice, this list of conditions and the following disclaimer in the
16b06ebda0SMatthew Dillon * documentation and/or other materials provided with the distribution.
17b06ebda0SMatthew Dillon *
18b06ebda0SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19b06ebda0SMatthew Dillon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b06ebda0SMatthew Dillon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b06ebda0SMatthew Dillon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22b06ebda0SMatthew Dillon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b06ebda0SMatthew Dillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24b06ebda0SMatthew Dillon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25b06ebda0SMatthew Dillon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26b06ebda0SMatthew Dillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27b06ebda0SMatthew Dillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28b06ebda0SMatthew Dillon * SUCH DAMAGE.
29b06ebda0SMatthew Dillon *
30b06ebda0SMatthew Dillon * $Id: ng_btsocket_l2cap.c,v 1.16 2003/09/14 23:29:06 max Exp $
31b06ebda0SMatthew Dillon * $FreeBSD: src/sys/netgraph/bluetooth/socket/ng_btsocket_l2cap.c,v 1.25 2007/10/31 16:17:20 emax Exp $
32b06ebda0SMatthew Dillon */
33b06ebda0SMatthew Dillon
34b06ebda0SMatthew Dillon #include <sys/param.h>
35b06ebda0SMatthew Dillon #include <sys/systm.h>
36*84bfc1a1SSascha Wildner #include <sys/bitstring.h>
37b06ebda0SMatthew Dillon #include <sys/domain.h>
38b06ebda0SMatthew Dillon #include <sys/endian.h>
39b06ebda0SMatthew Dillon #include <sys/errno.h>
40b06ebda0SMatthew Dillon #include <sys/filedesc.h>
41b06ebda0SMatthew Dillon #include <sys/kernel.h>
42b06ebda0SMatthew Dillon #include <sys/lock.h>
43b06ebda0SMatthew Dillon #include <sys/malloc.h>
44b06ebda0SMatthew Dillon #include <sys/mbuf.h>
45b06ebda0SMatthew Dillon #include <sys/protosw.h>
46b06ebda0SMatthew Dillon #include <sys/queue.h>
47b06ebda0SMatthew Dillon #include <sys/socket.h>
48b06ebda0SMatthew Dillon #include <sys/socketvar.h>
49b06ebda0SMatthew Dillon #include <sys/sysctl.h>
50b06ebda0SMatthew Dillon #include <sys/taskqueue.h>
51e85b99abSSascha Wildner #include <sys/msgport2.h>
52e85b99abSSascha Wildner #include <sys/refcount.h>
53e85b99abSSascha Wildner #include <netgraph7/ng_message.h>
54e85b99abSSascha Wildner #include <netgraph7/netgraph.h>
55e85b99abSSascha Wildner #include <netgraph7/netgraph2.h>
56e85b99abSSascha Wildner #include <netgraph7/bluetooth/include/ng_bluetooth.h>
57e85b99abSSascha Wildner #include <netgraph7/bluetooth/include/ng_hci.h>
58e85b99abSSascha Wildner #include <netgraph7/bluetooth/include/ng_l2cap.h>
59e85b99abSSascha Wildner #include <netgraph7/bluetooth/include/ng_btsocket.h>
60e85b99abSSascha Wildner #include <netgraph7/bluetooth/include/ng_btsocket_l2cap.h>
61b06ebda0SMatthew Dillon
62b06ebda0SMatthew Dillon /* MALLOC define */
63b06ebda0SMatthew Dillon #ifdef NG_SEPARATE_MALLOC
64b06ebda0SMatthew Dillon MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_L2CAP, "netgraph_btsocks_l2cap",
65b06ebda0SMatthew Dillon "Netgraph Bluetooth L2CAP sockets");
66b06ebda0SMatthew Dillon #else
67b06ebda0SMatthew Dillon #define M_NETGRAPH_BTSOCKET_L2CAP M_NETGRAPH
68b06ebda0SMatthew Dillon #endif /* NG_SEPARATE_MALLOC */
69b06ebda0SMatthew Dillon
70b06ebda0SMatthew Dillon /* Netgraph node methods */
71b06ebda0SMatthew Dillon static ng_constructor_t ng_btsocket_l2cap_node_constructor;
72b06ebda0SMatthew Dillon static ng_rcvmsg_t ng_btsocket_l2cap_node_rcvmsg;
73b06ebda0SMatthew Dillon static ng_shutdown_t ng_btsocket_l2cap_node_shutdown;
74b06ebda0SMatthew Dillon static ng_newhook_t ng_btsocket_l2cap_node_newhook;
75b06ebda0SMatthew Dillon static ng_connect_t ng_btsocket_l2cap_node_connect;
76b06ebda0SMatthew Dillon static ng_rcvdata_t ng_btsocket_l2cap_node_rcvdata;
77b06ebda0SMatthew Dillon static ng_disconnect_t ng_btsocket_l2cap_node_disconnect;
78b06ebda0SMatthew Dillon
79b06ebda0SMatthew Dillon static void ng_btsocket_l2cap_input (void *, int);
80b06ebda0SMatthew Dillon static void ng_btsocket_l2cap_rtclean (void *, int);
81b06ebda0SMatthew Dillon
82b06ebda0SMatthew Dillon /* Netgraph type descriptor */
83b06ebda0SMatthew Dillon static struct ng_type typestruct = {
84b06ebda0SMatthew Dillon .version = NG_ABI_VERSION,
85b06ebda0SMatthew Dillon .name = NG_BTSOCKET_L2CAP_NODE_TYPE,
86b06ebda0SMatthew Dillon .constructor = ng_btsocket_l2cap_node_constructor,
87b06ebda0SMatthew Dillon .rcvmsg = ng_btsocket_l2cap_node_rcvmsg,
88b06ebda0SMatthew Dillon .shutdown = ng_btsocket_l2cap_node_shutdown,
89b06ebda0SMatthew Dillon .newhook = ng_btsocket_l2cap_node_newhook,
90b06ebda0SMatthew Dillon .connect = ng_btsocket_l2cap_node_connect,
91b06ebda0SMatthew Dillon .rcvdata = ng_btsocket_l2cap_node_rcvdata,
92b06ebda0SMatthew Dillon .disconnect = ng_btsocket_l2cap_node_disconnect,
93b06ebda0SMatthew Dillon };
94b06ebda0SMatthew Dillon
95b06ebda0SMatthew Dillon /* Globals */
96b06ebda0SMatthew Dillon extern int ifqmaxlen;
97b06ebda0SMatthew Dillon static u_int32_t ng_btsocket_l2cap_debug_level;
98b06ebda0SMatthew Dillon static node_p ng_btsocket_l2cap_node;
99b06ebda0SMatthew Dillon static struct ng_bt_itemq ng_btsocket_l2cap_queue;
100e85b99abSSascha Wildner static struct lock ng_btsocket_l2cap_queue_lock;
101b06ebda0SMatthew Dillon static struct task ng_btsocket_l2cap_queue_task;
102b06ebda0SMatthew Dillon static LIST_HEAD(, ng_btsocket_l2cap_pcb) ng_btsocket_l2cap_sockets;
103e85b99abSSascha Wildner static struct lock ng_btsocket_l2cap_sockets_lock;
104b06ebda0SMatthew Dillon static LIST_HEAD(, ng_btsocket_l2cap_rtentry) ng_btsocket_l2cap_rt;
105e85b99abSSascha Wildner static struct lock ng_btsocket_l2cap_rt_lock;
106b06ebda0SMatthew Dillon static struct task ng_btsocket_l2cap_rt_task;
107b06ebda0SMatthew Dillon
108b06ebda0SMatthew Dillon /* Sysctl tree */
109b06ebda0SMatthew Dillon SYSCTL_DECL(_net_bluetooth_l2cap_sockets);
110b06ebda0SMatthew Dillon SYSCTL_NODE(_net_bluetooth_l2cap_sockets, OID_AUTO, seq, CTLFLAG_RW,
111b06ebda0SMatthew Dillon 0, "Bluetooth SEQPACKET L2CAP sockets family");
112b06ebda0SMatthew Dillon SYSCTL_INT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, debug_level,
113b06ebda0SMatthew Dillon CTLFLAG_RW,
114b06ebda0SMatthew Dillon &ng_btsocket_l2cap_debug_level, NG_BTSOCKET_WARN_LEVEL,
115b06ebda0SMatthew Dillon "Bluetooth SEQPACKET L2CAP sockets debug level");
116b06ebda0SMatthew Dillon SYSCTL_INT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_len,
117b06ebda0SMatthew Dillon CTLFLAG_RD,
118b06ebda0SMatthew Dillon &ng_btsocket_l2cap_queue.len, 0,
119b06ebda0SMatthew Dillon "Bluetooth SEQPACKET L2CAP sockets input queue length");
120b06ebda0SMatthew Dillon SYSCTL_INT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_maxlen,
121b06ebda0SMatthew Dillon CTLFLAG_RD,
122b06ebda0SMatthew Dillon &ng_btsocket_l2cap_queue.maxlen, 0,
123b06ebda0SMatthew Dillon "Bluetooth SEQPACKET L2CAP sockets input queue max. length");
124b06ebda0SMatthew Dillon SYSCTL_INT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_drops,
125b06ebda0SMatthew Dillon CTLFLAG_RD,
126b06ebda0SMatthew Dillon &ng_btsocket_l2cap_queue.drops, 0,
127b06ebda0SMatthew Dillon "Bluetooth SEQPACKET L2CAP sockets input queue drops");
128b06ebda0SMatthew Dillon
129b06ebda0SMatthew Dillon /* Debug */
130b06ebda0SMatthew Dillon #define NG_BTSOCKET_L2CAP_INFO \
131b06ebda0SMatthew Dillon if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_INFO_LEVEL) \
132a62226e4SSascha Wildner kprintf
133b06ebda0SMatthew Dillon
134b06ebda0SMatthew Dillon #define NG_BTSOCKET_L2CAP_WARN \
135b06ebda0SMatthew Dillon if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_WARN_LEVEL) \
136a62226e4SSascha Wildner kprintf
137b06ebda0SMatthew Dillon
138b06ebda0SMatthew Dillon #define NG_BTSOCKET_L2CAP_ERR \
139b06ebda0SMatthew Dillon if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_ERR_LEVEL) \
140a62226e4SSascha Wildner kprintf
141b06ebda0SMatthew Dillon
142b06ebda0SMatthew Dillon #define NG_BTSOCKET_L2CAP_ALERT \
143b06ebda0SMatthew Dillon if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_ALERT_LEVEL) \
144a62226e4SSascha Wildner kprintf
145b06ebda0SMatthew Dillon
146b06ebda0SMatthew Dillon /*
147b06ebda0SMatthew Dillon * Netgraph message processing routines
148b06ebda0SMatthew Dillon */
149b06ebda0SMatthew Dillon
150b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_process_l2ca_con_req_rsp
151b06ebda0SMatthew Dillon (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
152b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_process_l2ca_con_rsp_rsp
153b06ebda0SMatthew Dillon (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
154b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_process_l2ca_con_ind
155b06ebda0SMatthew Dillon (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
156b06ebda0SMatthew Dillon
157b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_process_l2ca_cfg_req_rsp
158b06ebda0SMatthew Dillon (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
159b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp
160b06ebda0SMatthew Dillon (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
161b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_process_l2ca_cfg_ind
162b06ebda0SMatthew Dillon (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
163b06ebda0SMatthew Dillon
164b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_process_l2ca_discon_rsp
165b06ebda0SMatthew Dillon (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
166b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_process_l2ca_discon_ind
167b06ebda0SMatthew Dillon (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
168b06ebda0SMatthew Dillon
169b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_process_l2ca_write_rsp
170b06ebda0SMatthew Dillon (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
171b06ebda0SMatthew Dillon
172b06ebda0SMatthew Dillon /*
173b06ebda0SMatthew Dillon * Send L2CA_xxx messages to the lower layer
174b06ebda0SMatthew Dillon */
175b06ebda0SMatthew Dillon
176b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_send_l2ca_con_req
177b06ebda0SMatthew Dillon (ng_btsocket_l2cap_pcb_p);
178b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_send_l2ca_con_rsp_req
179b06ebda0SMatthew Dillon (u_int32_t, ng_btsocket_l2cap_rtentry_p, bdaddr_p, int, int, int);
180b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_send_l2ca_cfg_req
181b06ebda0SMatthew Dillon (ng_btsocket_l2cap_pcb_p);
182b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_send_l2ca_cfg_rsp
183b06ebda0SMatthew Dillon (ng_btsocket_l2cap_pcb_p);
184b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_send_l2ca_discon_req
185b06ebda0SMatthew Dillon (u_int32_t, ng_btsocket_l2cap_pcb_p);
186b06ebda0SMatthew Dillon
187b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_send2
188b06ebda0SMatthew Dillon (ng_btsocket_l2cap_pcb_p);
189b06ebda0SMatthew Dillon
190b06ebda0SMatthew Dillon /*
191b06ebda0SMatthew Dillon * Timeout processing routines
192b06ebda0SMatthew Dillon */
193b06ebda0SMatthew Dillon
194b06ebda0SMatthew Dillon static void ng_btsocket_l2cap_timeout (ng_btsocket_l2cap_pcb_p);
195b06ebda0SMatthew Dillon static void ng_btsocket_l2cap_untimeout (ng_btsocket_l2cap_pcb_p);
196b06ebda0SMatthew Dillon static void ng_btsocket_l2cap_process_timeout (void *);
197b06ebda0SMatthew Dillon
198b06ebda0SMatthew Dillon /*
199b06ebda0SMatthew Dillon * Other stuff
200b06ebda0SMatthew Dillon */
201b06ebda0SMatthew Dillon
202b06ebda0SMatthew Dillon static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_addr(bdaddr_p, int);
203b06ebda0SMatthew Dillon static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_token(u_int32_t);
204b06ebda0SMatthew Dillon static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_cid (bdaddr_p, int);
205b06ebda0SMatthew Dillon static int ng_btsocket_l2cap_result2errno(int);
206b06ebda0SMatthew Dillon
207b06ebda0SMatthew Dillon #define ng_btsocket_l2cap_wakeup_input_task() \
208e85b99abSSascha Wildner taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_queue_task)
209b06ebda0SMatthew Dillon
210b06ebda0SMatthew Dillon #define ng_btsocket_l2cap_wakeup_route_task() \
211e85b99abSSascha Wildner taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_rt_task)
212b06ebda0SMatthew Dillon
213b06ebda0SMatthew Dillon /*****************************************************************************
214b06ebda0SMatthew Dillon *****************************************************************************
215b06ebda0SMatthew Dillon ** Netgraph node interface
216b06ebda0SMatthew Dillon *****************************************************************************
217b06ebda0SMatthew Dillon *****************************************************************************/
218b06ebda0SMatthew Dillon
219b06ebda0SMatthew Dillon /*
220b06ebda0SMatthew Dillon * Netgraph node constructor. Do not allow to create node of this type.
221b06ebda0SMatthew Dillon */
222b06ebda0SMatthew Dillon
223b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_node_constructor(node_p node)224b06ebda0SMatthew Dillon ng_btsocket_l2cap_node_constructor(node_p node)
225b06ebda0SMatthew Dillon {
226b06ebda0SMatthew Dillon return (EINVAL);
227b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_node_constructor */
228b06ebda0SMatthew Dillon
229b06ebda0SMatthew Dillon /*
230b06ebda0SMatthew Dillon * Do local shutdown processing. Let old node go and create new fresh one.
231b06ebda0SMatthew Dillon */
232b06ebda0SMatthew Dillon
233b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_node_shutdown(node_p node)234b06ebda0SMatthew Dillon ng_btsocket_l2cap_node_shutdown(node_p node)
235b06ebda0SMatthew Dillon {
236b06ebda0SMatthew Dillon int error = 0;
237b06ebda0SMatthew Dillon
238b06ebda0SMatthew Dillon NG_NODE_UNREF(node);
239b06ebda0SMatthew Dillon
240b06ebda0SMatthew Dillon /* Create new node */
241b06ebda0SMatthew Dillon error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_node);
242b06ebda0SMatthew Dillon if (error != 0) {
243b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ALERT(
244b06ebda0SMatthew Dillon "%s: Could not create Netgraph node, error=%d\n", __func__, error);
245b06ebda0SMatthew Dillon
246b06ebda0SMatthew Dillon ng_btsocket_l2cap_node = NULL;
247b06ebda0SMatthew Dillon
248b06ebda0SMatthew Dillon return (error);
249b06ebda0SMatthew Dillon }
250b06ebda0SMatthew Dillon
251b06ebda0SMatthew Dillon error = ng_name_node(ng_btsocket_l2cap_node,
252b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_NODE_TYPE);
253b06ebda0SMatthew Dillon if (error != 0) {
254b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ALERT(
255b06ebda0SMatthew Dillon "%s: Could not name Netgraph node, error=%d\n", __func__, error);
256b06ebda0SMatthew Dillon
257b06ebda0SMatthew Dillon NG_NODE_UNREF(ng_btsocket_l2cap_node);
258b06ebda0SMatthew Dillon ng_btsocket_l2cap_node = NULL;
259b06ebda0SMatthew Dillon
260b06ebda0SMatthew Dillon return (error);
261b06ebda0SMatthew Dillon }
262b06ebda0SMatthew Dillon
263b06ebda0SMatthew Dillon return (0);
264b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_node_shutdown */
265b06ebda0SMatthew Dillon
266b06ebda0SMatthew Dillon /*
267b06ebda0SMatthew Dillon * We allow any hook to be connected to the node.
268b06ebda0SMatthew Dillon */
269b06ebda0SMatthew Dillon
270b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_node_newhook(node_p node,hook_p hook,char const * name)271b06ebda0SMatthew Dillon ng_btsocket_l2cap_node_newhook(node_p node, hook_p hook, char const *name)
272b06ebda0SMatthew Dillon {
273b06ebda0SMatthew Dillon return (0);
274b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_node_newhook */
275b06ebda0SMatthew Dillon
276b06ebda0SMatthew Dillon /*
277b06ebda0SMatthew Dillon * Just say "YEP, that's OK by me!"
278b06ebda0SMatthew Dillon */
279b06ebda0SMatthew Dillon
280b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_node_connect(hook_p hook)281b06ebda0SMatthew Dillon ng_btsocket_l2cap_node_connect(hook_p hook)
282b06ebda0SMatthew Dillon {
283b06ebda0SMatthew Dillon NG_HOOK_SET_PRIVATE(hook, NULL);
284b06ebda0SMatthew Dillon NG_HOOK_REF(hook); /* Keep extra reference to the hook */
285b06ebda0SMatthew Dillon
286b06ebda0SMatthew Dillon #if 0
287b06ebda0SMatthew Dillon NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
288b06ebda0SMatthew Dillon NG_HOOK_FORCE_QUEUE(hook);
289b06ebda0SMatthew Dillon #endif
290b06ebda0SMatthew Dillon
291b06ebda0SMatthew Dillon return (0);
292b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_node_connect */
293b06ebda0SMatthew Dillon
294b06ebda0SMatthew Dillon /*
295b06ebda0SMatthew Dillon * Hook disconnection. Schedule route cleanup task
296b06ebda0SMatthew Dillon */
297b06ebda0SMatthew Dillon
298b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_node_disconnect(hook_p hook)299b06ebda0SMatthew Dillon ng_btsocket_l2cap_node_disconnect(hook_p hook)
300b06ebda0SMatthew Dillon {
301b06ebda0SMatthew Dillon /*
302b06ebda0SMatthew Dillon * If hook has private information than we must have this hook in
303b06ebda0SMatthew Dillon * the routing table and must schedule cleaning for the routing table.
304b06ebda0SMatthew Dillon * Otherwise hook was connected but we never got "hook_info" message,
305b06ebda0SMatthew Dillon * so we have never added this hook to the routing table and it save
306b06ebda0SMatthew Dillon * to just delete it.
307b06ebda0SMatthew Dillon */
308b06ebda0SMatthew Dillon
309b06ebda0SMatthew Dillon if (NG_HOOK_PRIVATE(hook) != NULL)
310b06ebda0SMatthew Dillon return (ng_btsocket_l2cap_wakeup_route_task());
311b06ebda0SMatthew Dillon
312b06ebda0SMatthew Dillon NG_HOOK_UNREF(hook); /* Remove extra reference */
313b06ebda0SMatthew Dillon
314b06ebda0SMatthew Dillon return (0);
315b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_node_disconnect */
316b06ebda0SMatthew Dillon
317b06ebda0SMatthew Dillon /*
318b06ebda0SMatthew Dillon * Process incoming messages
319b06ebda0SMatthew Dillon */
320b06ebda0SMatthew Dillon
321b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_node_rcvmsg(node_p node,item_p item,hook_p hook)322b06ebda0SMatthew Dillon ng_btsocket_l2cap_node_rcvmsg(node_p node, item_p item, hook_p hook)
323b06ebda0SMatthew Dillon {
324b06ebda0SMatthew Dillon struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
325b06ebda0SMatthew Dillon int error = 0;
326b06ebda0SMatthew Dillon
327b06ebda0SMatthew Dillon if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) {
328e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_queue_lock, LK_EXCLUSIVE);
329b06ebda0SMatthew Dillon if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) {
330b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
331b06ebda0SMatthew Dillon "%s: Input queue is full (msg)\n", __func__);
332b06ebda0SMatthew Dillon
333b06ebda0SMatthew Dillon NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_queue);
334b06ebda0SMatthew Dillon NG_FREE_ITEM(item);
335b06ebda0SMatthew Dillon error = ENOBUFS;
336b06ebda0SMatthew Dillon } else {
337b06ebda0SMatthew Dillon if (hook != NULL) {
338b06ebda0SMatthew Dillon NG_HOOK_REF(hook);
339b06ebda0SMatthew Dillon NGI_SET_HOOK(item, hook);
340b06ebda0SMatthew Dillon }
341b06ebda0SMatthew Dillon
342e85b99abSSascha Wildner ng_ref_item(item);
343b06ebda0SMatthew Dillon NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item);
344b06ebda0SMatthew Dillon error = ng_btsocket_l2cap_wakeup_input_task();
345b06ebda0SMatthew Dillon }
346e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_queue_lock, LK_RELEASE);
347b06ebda0SMatthew Dillon } else {
348b06ebda0SMatthew Dillon NG_FREE_ITEM(item);
349b06ebda0SMatthew Dillon error = EINVAL;
350b06ebda0SMatthew Dillon }
351b06ebda0SMatthew Dillon
352b06ebda0SMatthew Dillon return (error);
353b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_node_rcvmsg */
354b06ebda0SMatthew Dillon
355b06ebda0SMatthew Dillon /*
356b06ebda0SMatthew Dillon * Receive data on a hook
357b06ebda0SMatthew Dillon */
358b06ebda0SMatthew Dillon
359b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_node_rcvdata(hook_p hook,item_p item)360b06ebda0SMatthew Dillon ng_btsocket_l2cap_node_rcvdata(hook_p hook, item_p item)
361b06ebda0SMatthew Dillon {
362b06ebda0SMatthew Dillon int error = 0;
363b06ebda0SMatthew Dillon
364e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_queue_lock, LK_EXCLUSIVE);
365b06ebda0SMatthew Dillon if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) {
366b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
367b06ebda0SMatthew Dillon "%s: Input queue is full (data)\n", __func__);
368b06ebda0SMatthew Dillon
369b06ebda0SMatthew Dillon NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_queue);
370b06ebda0SMatthew Dillon NG_FREE_ITEM(item);
371b06ebda0SMatthew Dillon error = ENOBUFS;
372b06ebda0SMatthew Dillon } else {
373b06ebda0SMatthew Dillon NG_HOOK_REF(hook);
374b06ebda0SMatthew Dillon NGI_SET_HOOK(item, hook);
375b06ebda0SMatthew Dillon
376e85b99abSSascha Wildner ng_ref_item(item);
377b06ebda0SMatthew Dillon NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item);
378b06ebda0SMatthew Dillon error = ng_btsocket_l2cap_wakeup_input_task();
379b06ebda0SMatthew Dillon }
380e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_queue_lock, LK_RELEASE);
381b06ebda0SMatthew Dillon
382b06ebda0SMatthew Dillon return (error);
383b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_node_rcvdata */
384b06ebda0SMatthew Dillon
385b06ebda0SMatthew Dillon /*
386b06ebda0SMatthew Dillon * Process L2CA_Connect respose. Socket layer must have initiated connection,
387b06ebda0SMatthew Dillon * so we have to have a socket associated with message token.
388b06ebda0SMatthew Dillon */
389b06ebda0SMatthew Dillon
390b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_process_l2ca_con_req_rsp(struct ng_mesg * msg,ng_btsocket_l2cap_rtentry_p rt)391b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_con_req_rsp(struct ng_mesg *msg,
392b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt)
393b06ebda0SMatthew Dillon {
394b06ebda0SMatthew Dillon ng_l2cap_l2ca_con_op *op = NULL;
395b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = NULL;
396b06ebda0SMatthew Dillon int error = 0;
397b06ebda0SMatthew Dillon
398b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(*op))
399b06ebda0SMatthew Dillon return (EMSGSIZE);
400b06ebda0SMatthew Dillon
401b06ebda0SMatthew Dillon op = (ng_l2cap_l2ca_con_op *)(msg->data);
402b06ebda0SMatthew Dillon
403e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
404b06ebda0SMatthew Dillon
405b06ebda0SMatthew Dillon /* Look for the socket with the token */
406b06ebda0SMatthew Dillon pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
407b06ebda0SMatthew Dillon if (pcb == NULL) {
408e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
409b06ebda0SMatthew Dillon return (ENOENT);
410b06ebda0SMatthew Dillon }
411b06ebda0SMatthew Dillon
412e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
413b06ebda0SMatthew Dillon
414b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
415b06ebda0SMatthew Dillon "%s: Got L2CA_Connect response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
416b06ebda0SMatthew Dillon "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, status=%d, " \
417b06ebda0SMatthew Dillon "state=%d\n", __func__, msg->header.token,
418b06ebda0SMatthew Dillon pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
419b06ebda0SMatthew Dillon pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
420b06ebda0SMatthew Dillon pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
421b06ebda0SMatthew Dillon pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
422b06ebda0SMatthew Dillon pcb->psm, op->lcid, op->result, op->status,
423b06ebda0SMatthew Dillon pcb->state);
424b06ebda0SMatthew Dillon
425b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) {
426e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
427e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
428b06ebda0SMatthew Dillon
429b06ebda0SMatthew Dillon return (ENOENT);
430b06ebda0SMatthew Dillon }
431b06ebda0SMatthew Dillon
432b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
433b06ebda0SMatthew Dillon
434b06ebda0SMatthew Dillon if (op->result == NG_L2CAP_PENDING) {
435b06ebda0SMatthew Dillon ng_btsocket_l2cap_timeout(pcb);
436e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
437e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
438b06ebda0SMatthew Dillon
439b06ebda0SMatthew Dillon return (0);
440b06ebda0SMatthew Dillon }
441b06ebda0SMatthew Dillon
442b06ebda0SMatthew Dillon if (op->result == NG_L2CAP_SUCCESS) {
443b06ebda0SMatthew Dillon /*
444b06ebda0SMatthew Dillon * Channel is now open, so update local channel ID and
445b06ebda0SMatthew Dillon * start configuration process. Source and destination
446b06ebda0SMatthew Dillon * addresses as well as route must be already set.
447b06ebda0SMatthew Dillon */
448b06ebda0SMatthew Dillon
449b06ebda0SMatthew Dillon pcb->cid = op->lcid;
450b06ebda0SMatthew Dillon
451b06ebda0SMatthew Dillon error = ng_btsocket_l2cap_send_l2ca_cfg_req(pcb);
452b06ebda0SMatthew Dillon if (error != 0) {
453b06ebda0SMatthew Dillon /* Send disconnect request with "zero" token */
454b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
455b06ebda0SMatthew Dillon
456b06ebda0SMatthew Dillon /* ... and close the socket */
457b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
458b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
459b06ebda0SMatthew Dillon } else {
460b06ebda0SMatthew Dillon pcb->cfg_state = NG_BTSOCKET_L2CAP_CFG_IN_SENT;
461b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CONFIGURING;
462b06ebda0SMatthew Dillon
463b06ebda0SMatthew Dillon ng_btsocket_l2cap_timeout(pcb);
464b06ebda0SMatthew Dillon }
465b06ebda0SMatthew Dillon } else {
466b06ebda0SMatthew Dillon /*
467b06ebda0SMatthew Dillon * We have failed to open connection, so convert result
468b06ebda0SMatthew Dillon * code to "errno" code and disconnect the socket. Channel
469b06ebda0SMatthew Dillon * already has been closed.
470b06ebda0SMatthew Dillon */
471b06ebda0SMatthew Dillon
472b06ebda0SMatthew Dillon pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
473b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
474b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
475b06ebda0SMatthew Dillon }
476b06ebda0SMatthew Dillon
477e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
478e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
479b06ebda0SMatthew Dillon
480b06ebda0SMatthew Dillon return (error);
481b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_process_l2ca_con_req_rsp */
482b06ebda0SMatthew Dillon
483b06ebda0SMatthew Dillon /*
484b06ebda0SMatthew Dillon * Process L2CA_ConnectRsp response
485b06ebda0SMatthew Dillon */
486b06ebda0SMatthew Dillon
487b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(struct ng_mesg * msg,ng_btsocket_l2cap_rtentry_p rt)488b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(struct ng_mesg *msg,
489b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt)
490b06ebda0SMatthew Dillon {
491b06ebda0SMatthew Dillon ng_l2cap_l2ca_con_rsp_op *op = NULL;
492b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = NULL;
493b06ebda0SMatthew Dillon
494b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(*op))
495b06ebda0SMatthew Dillon return (EMSGSIZE);
496b06ebda0SMatthew Dillon
497b06ebda0SMatthew Dillon op = (ng_l2cap_l2ca_con_rsp_op *)(msg->data);
498b06ebda0SMatthew Dillon
499e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
500b06ebda0SMatthew Dillon
501b06ebda0SMatthew Dillon /* Look for the socket with the token */
502b06ebda0SMatthew Dillon pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
503b06ebda0SMatthew Dillon if (pcb == NULL) {
504e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
505b06ebda0SMatthew Dillon return (ENOENT);
506b06ebda0SMatthew Dillon }
507b06ebda0SMatthew Dillon
508e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
509b06ebda0SMatthew Dillon
510b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
511b06ebda0SMatthew Dillon "%s: Got L2CA_ConnectRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
512b06ebda0SMatthew Dillon "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d\n",
513b06ebda0SMatthew Dillon __func__, msg->header.token,
514b06ebda0SMatthew Dillon pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
515b06ebda0SMatthew Dillon pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
516b06ebda0SMatthew Dillon pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
517b06ebda0SMatthew Dillon pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
518b06ebda0SMatthew Dillon pcb->psm, pcb->cid, op->result, pcb->state);
519b06ebda0SMatthew Dillon
520b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) {
521e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
522e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
523b06ebda0SMatthew Dillon
524b06ebda0SMatthew Dillon return (ENOENT);
525b06ebda0SMatthew Dillon }
526b06ebda0SMatthew Dillon
527b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
528b06ebda0SMatthew Dillon
529b06ebda0SMatthew Dillon /* Check the result and disconnect the socket on failure */
530b06ebda0SMatthew Dillon if (op->result != NG_L2CAP_SUCCESS) {
531b06ebda0SMatthew Dillon /* Close the socket - channel already closed */
532b06ebda0SMatthew Dillon pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
533b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
534b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
535b06ebda0SMatthew Dillon } else {
536b06ebda0SMatthew Dillon /* Move to CONFIGURING state and wait for CONFIG_IND */
537b06ebda0SMatthew Dillon pcb->cfg_state = 0;
538b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CONFIGURING;
539b06ebda0SMatthew Dillon ng_btsocket_l2cap_timeout(pcb);
540b06ebda0SMatthew Dillon }
541b06ebda0SMatthew Dillon
542e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
543e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
544b06ebda0SMatthew Dillon
545b06ebda0SMatthew Dillon return (0);
546b06ebda0SMatthew Dillon } /* ng_btsocket_process_l2ca_con_rsp_rsp */
547b06ebda0SMatthew Dillon
548b06ebda0SMatthew Dillon /*
549b06ebda0SMatthew Dillon * Process L2CA_Connect indicator. Find socket that listens on address
550b06ebda0SMatthew Dillon * and PSM. Find exact or closest match. Create new socket and initiate
551b06ebda0SMatthew Dillon * connection.
552b06ebda0SMatthew Dillon */
553b06ebda0SMatthew Dillon
554b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_process_l2ca_con_ind(struct ng_mesg * msg,ng_btsocket_l2cap_rtentry_p rt)555b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_con_ind(struct ng_mesg *msg,
556b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt)
557b06ebda0SMatthew Dillon {
558b06ebda0SMatthew Dillon ng_l2cap_l2ca_con_ind_ip *ip = NULL;
559b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = NULL, *pcb1 = NULL;
560b06ebda0SMatthew Dillon int error = 0;
561b06ebda0SMatthew Dillon u_int32_t token = 0;
562b06ebda0SMatthew Dillon u_int16_t result = 0;
563b06ebda0SMatthew Dillon
564b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(*ip))
565b06ebda0SMatthew Dillon return (EMSGSIZE);
566b06ebda0SMatthew Dillon
567b06ebda0SMatthew Dillon ip = (ng_l2cap_l2ca_con_ind_ip *)(msg->data);
568b06ebda0SMatthew Dillon
569b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
570b06ebda0SMatthew Dillon "%s: Got L2CA_Connect indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
571b06ebda0SMatthew Dillon "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, ident=%d\n",
572b06ebda0SMatthew Dillon __func__,
573b06ebda0SMatthew Dillon rt->src.b[5], rt->src.b[4], rt->src.b[3],
574b06ebda0SMatthew Dillon rt->src.b[2], rt->src.b[1], rt->src.b[0],
575b06ebda0SMatthew Dillon ip->bdaddr.b[5], ip->bdaddr.b[4], ip->bdaddr.b[3],
576b06ebda0SMatthew Dillon ip->bdaddr.b[2], ip->bdaddr.b[1], ip->bdaddr.b[0],
577b06ebda0SMatthew Dillon ip->psm, ip->lcid, ip->ident);
578b06ebda0SMatthew Dillon
579e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
580b06ebda0SMatthew Dillon
581b06ebda0SMatthew Dillon pcb = ng_btsocket_l2cap_pcb_by_addr(&rt->src, ip->psm);
582b06ebda0SMatthew Dillon if (pcb != NULL) {
583b06ebda0SMatthew Dillon struct socket *so1 = NULL;
584b06ebda0SMatthew Dillon
585e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
586b06ebda0SMatthew Dillon
587b06ebda0SMatthew Dillon /*
588b06ebda0SMatthew Dillon * First check the pending connections queue and if we have
589b06ebda0SMatthew Dillon * space then create new socket and set proper source address.
590b06ebda0SMatthew Dillon */
591b06ebda0SMatthew Dillon
592b06ebda0SMatthew Dillon if (pcb->so->so_qlen <= pcb->so->so_qlimit)
593b06ebda0SMatthew Dillon so1 = sonewconn(pcb->so, 0);
594b06ebda0SMatthew Dillon
595b06ebda0SMatthew Dillon if (so1 == NULL) {
596b06ebda0SMatthew Dillon result = NG_L2CAP_NO_RESOURCES;
597b06ebda0SMatthew Dillon goto respond;
598b06ebda0SMatthew Dillon }
599b06ebda0SMatthew Dillon
600b06ebda0SMatthew Dillon /*
601b06ebda0SMatthew Dillon * If we got here than we have created new socket. So complete
602b06ebda0SMatthew Dillon * connection. If we we listening on specific address then copy
603b06ebda0SMatthew Dillon * source address from listening socket, otherwise copy source
604b06ebda0SMatthew Dillon * address from hook's routing information.
605b06ebda0SMatthew Dillon */
606b06ebda0SMatthew Dillon
607b06ebda0SMatthew Dillon pcb1 = so2l2cap_pcb(so1);
608b06ebda0SMatthew Dillon KASSERT((pcb1 != NULL),
609b06ebda0SMatthew Dillon ("%s: pcb1 == NULL\n", __func__));
610b06ebda0SMatthew Dillon
611e85b99abSSascha Wildner lockmgr(&pcb1->pcb_lock, LK_EXCLUSIVE);
612b06ebda0SMatthew Dillon
613b06ebda0SMatthew Dillon if (bcmp(&pcb->src, NG_HCI_BDADDR_ANY, sizeof(pcb->src)) != 0)
614b06ebda0SMatthew Dillon bcopy(&pcb->src, &pcb1->src, sizeof(pcb1->src));
615b06ebda0SMatthew Dillon else
616b06ebda0SMatthew Dillon bcopy(&rt->src, &pcb1->src, sizeof(pcb1->src));
617b06ebda0SMatthew Dillon
618b06ebda0SMatthew Dillon pcb1->flags &= ~NG_BTSOCKET_L2CAP_CLIENT;
619b06ebda0SMatthew Dillon
620b06ebda0SMatthew Dillon bcopy(&ip->bdaddr, &pcb1->dst, sizeof(pcb1->dst));
621b06ebda0SMatthew Dillon pcb1->psm = ip->psm;
622b06ebda0SMatthew Dillon pcb1->cid = ip->lcid;
623b06ebda0SMatthew Dillon pcb1->rt = rt;
624b06ebda0SMatthew Dillon
625b06ebda0SMatthew Dillon /* Copy socket settings */
626b06ebda0SMatthew Dillon pcb1->imtu = pcb->imtu;
627b06ebda0SMatthew Dillon bcopy(&pcb->oflow, &pcb1->oflow, sizeof(pcb1->oflow));
628b06ebda0SMatthew Dillon pcb1->flush_timo = pcb->flush_timo;
629b06ebda0SMatthew Dillon
630b06ebda0SMatthew Dillon token = pcb1->token;
631b06ebda0SMatthew Dillon } else
632b06ebda0SMatthew Dillon /* Nobody listens on requested BDADDR/PSM */
633b06ebda0SMatthew Dillon result = NG_L2CAP_PSM_NOT_SUPPORTED;
634b06ebda0SMatthew Dillon
635b06ebda0SMatthew Dillon respond:
636b06ebda0SMatthew Dillon error = ng_btsocket_l2cap_send_l2ca_con_rsp_req(token, rt,
637b06ebda0SMatthew Dillon &ip->bdaddr, ip->ident, ip->lcid, result);
638b06ebda0SMatthew Dillon if (pcb1 != NULL) {
639b06ebda0SMatthew Dillon if (error != 0) {
640b06ebda0SMatthew Dillon pcb1->so->so_error = error;
641b06ebda0SMatthew Dillon pcb1->state = NG_BTSOCKET_L2CAP_CLOSED;
642b06ebda0SMatthew Dillon soisdisconnected(pcb1->so);
643b06ebda0SMatthew Dillon } else {
644b06ebda0SMatthew Dillon pcb1->state = NG_BTSOCKET_L2CAP_CONNECTING;
645b06ebda0SMatthew Dillon soisconnecting(pcb1->so);
646b06ebda0SMatthew Dillon
647b06ebda0SMatthew Dillon ng_btsocket_l2cap_timeout(pcb1);
648b06ebda0SMatthew Dillon }
649b06ebda0SMatthew Dillon
650e85b99abSSascha Wildner lockmgr(&pcb1->pcb_lock, LK_RELEASE);
651b06ebda0SMatthew Dillon }
652b06ebda0SMatthew Dillon
653b06ebda0SMatthew Dillon if (pcb != NULL)
654e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
655b06ebda0SMatthew Dillon
656e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
657b06ebda0SMatthew Dillon
658b06ebda0SMatthew Dillon return (error);
659b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_process_l2ca_con_ind */
660b06ebda0SMatthew Dillon
661b06ebda0SMatthew Dillon /*
662b06ebda0SMatthew Dillon * Process L2CA_Config response
663b06ebda0SMatthew Dillon */
664b06ebda0SMatthew Dillon
665b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(struct ng_mesg * msg,ng_btsocket_l2cap_rtentry_p rt)666b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(struct ng_mesg *msg,
667b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt)
668b06ebda0SMatthew Dillon {
669b06ebda0SMatthew Dillon ng_l2cap_l2ca_cfg_op *op = NULL;
670b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb = NULL;
671b06ebda0SMatthew Dillon
672b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(*op))
673b06ebda0SMatthew Dillon return (EMSGSIZE);
674b06ebda0SMatthew Dillon
675b06ebda0SMatthew Dillon op = (ng_l2cap_l2ca_cfg_op *)(msg->data);
676b06ebda0SMatthew Dillon
677e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
678b06ebda0SMatthew Dillon
679b06ebda0SMatthew Dillon /*
680b06ebda0SMatthew Dillon * Socket must have issued a Configure request, so we must have a
681b06ebda0SMatthew Dillon * socket that wants to be configured. Use Netgraph message token
682b06ebda0SMatthew Dillon * to find it
683b06ebda0SMatthew Dillon */
684b06ebda0SMatthew Dillon
685b06ebda0SMatthew Dillon pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
686b06ebda0SMatthew Dillon if (pcb == NULL) {
687b06ebda0SMatthew Dillon /*
688b06ebda0SMatthew Dillon * XXX FIXME what to do here? We could not find a
689b06ebda0SMatthew Dillon * socket with requested token. We even can not send
690b06ebda0SMatthew Dillon * Disconnect, because we do not know channel ID
691b06ebda0SMatthew Dillon */
692b06ebda0SMatthew Dillon
693e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
694b06ebda0SMatthew Dillon return (ENOENT);
695b06ebda0SMatthew Dillon }
696b06ebda0SMatthew Dillon
697e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
698b06ebda0SMatthew Dillon
699b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
700b06ebda0SMatthew Dillon "%s: Got L2CA_Config response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
701b06ebda0SMatthew Dillon "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d, " \
702b06ebda0SMatthew Dillon "cfg_state=%x\n",
703b06ebda0SMatthew Dillon __func__, msg->header.token,
704b06ebda0SMatthew Dillon pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
705b06ebda0SMatthew Dillon pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
706b06ebda0SMatthew Dillon pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
707b06ebda0SMatthew Dillon pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
708b06ebda0SMatthew Dillon pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state);
709b06ebda0SMatthew Dillon
710b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
711e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
712e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
713b06ebda0SMatthew Dillon
714b06ebda0SMatthew Dillon return (ENOENT);
715b06ebda0SMatthew Dillon }
716b06ebda0SMatthew Dillon
717b06ebda0SMatthew Dillon if (op->result == NG_L2CAP_SUCCESS) {
718b06ebda0SMatthew Dillon /*
719b06ebda0SMatthew Dillon * XXX FIXME Actually set flush and link timeout.
720b06ebda0SMatthew Dillon * Set QoS here if required. Resolve conficts (flush_timo).
721b06ebda0SMatthew Dillon * Save incoming MTU (peer's outgoing MTU) and outgoing flow
722b06ebda0SMatthew Dillon * spec.
723b06ebda0SMatthew Dillon */
724b06ebda0SMatthew Dillon
725b06ebda0SMatthew Dillon pcb->imtu = op->imtu;
726b06ebda0SMatthew Dillon bcopy(&op->oflow, &pcb->oflow, sizeof(pcb->oflow));
727b06ebda0SMatthew Dillon pcb->flush_timo = op->flush_timo;
728b06ebda0SMatthew Dillon
729b06ebda0SMatthew Dillon /*
730b06ebda0SMatthew Dillon * We have configured incoming side, so record it and check
731b06ebda0SMatthew Dillon * if configuration is complete. If complete then mark socket
732b06ebda0SMatthew Dillon * as connected, otherwise wait for the peer.
733b06ebda0SMatthew Dillon */
734b06ebda0SMatthew Dillon
735b06ebda0SMatthew Dillon pcb->cfg_state &= ~NG_BTSOCKET_L2CAP_CFG_IN_SENT;
736b06ebda0SMatthew Dillon pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_IN;
737b06ebda0SMatthew Dillon
738b06ebda0SMatthew Dillon if (pcb->cfg_state == NG_BTSOCKET_L2CAP_CFG_BOTH) {
739b06ebda0SMatthew Dillon /* Configuration complete - mark socket as open */
740b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
741b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_OPEN;
742b06ebda0SMatthew Dillon soisconnected(pcb->so);
743b06ebda0SMatthew Dillon }
744b06ebda0SMatthew Dillon } else {
745b06ebda0SMatthew Dillon /*
746b06ebda0SMatthew Dillon * Something went wrong. Could be unacceptable parameters,
747b06ebda0SMatthew Dillon * reject or unknown option. That's too bad, but we will
748b06ebda0SMatthew Dillon * not negotiate. Send Disconnect and close the channel.
749b06ebda0SMatthew Dillon */
750b06ebda0SMatthew Dillon
751b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
752b06ebda0SMatthew Dillon
753b06ebda0SMatthew Dillon switch (op->result) {
754b06ebda0SMatthew Dillon case NG_L2CAP_UNACCEPTABLE_PARAMS:
755b06ebda0SMatthew Dillon case NG_L2CAP_UNKNOWN_OPTION:
756b06ebda0SMatthew Dillon pcb->so->so_error = EINVAL;
757b06ebda0SMatthew Dillon break;
758b06ebda0SMatthew Dillon
759b06ebda0SMatthew Dillon default:
760b06ebda0SMatthew Dillon pcb->so->so_error = ECONNRESET;
761b06ebda0SMatthew Dillon break;
762b06ebda0SMatthew Dillon }
763b06ebda0SMatthew Dillon
764b06ebda0SMatthew Dillon /* Send disconnect with "zero" token */
765b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
766b06ebda0SMatthew Dillon
767b06ebda0SMatthew Dillon /* ... and close the socket */
768b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
769b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
770b06ebda0SMatthew Dillon }
771b06ebda0SMatthew Dillon
772e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
773e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
774b06ebda0SMatthew Dillon
775b06ebda0SMatthew Dillon return (0);
776b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_process_l2ca_cfg_req_rsp */
777b06ebda0SMatthew Dillon
778b06ebda0SMatthew Dillon /*
779b06ebda0SMatthew Dillon * Process L2CA_ConfigRsp response
780b06ebda0SMatthew Dillon */
781b06ebda0SMatthew Dillon
782b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(struct ng_mesg * msg,ng_btsocket_l2cap_rtentry_p rt)783b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(struct ng_mesg *msg,
784b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt)
785b06ebda0SMatthew Dillon {
786b06ebda0SMatthew Dillon ng_l2cap_l2ca_cfg_rsp_op *op = NULL;
787b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = NULL;
788b06ebda0SMatthew Dillon int error = 0;
789b06ebda0SMatthew Dillon
790b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(*op))
791b06ebda0SMatthew Dillon return (EMSGSIZE);
792b06ebda0SMatthew Dillon
793b06ebda0SMatthew Dillon op = (ng_l2cap_l2ca_cfg_rsp_op *)(msg->data);
794b06ebda0SMatthew Dillon
795e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
796b06ebda0SMatthew Dillon
797b06ebda0SMatthew Dillon /* Look for the socket with the token */
798b06ebda0SMatthew Dillon pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
799b06ebda0SMatthew Dillon if (pcb == NULL) {
800e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
801b06ebda0SMatthew Dillon return (ENOENT);
802b06ebda0SMatthew Dillon }
803b06ebda0SMatthew Dillon
804e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
805b06ebda0SMatthew Dillon
806b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
807b06ebda0SMatthew Dillon "%s: Got L2CA_ConfigRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
808b06ebda0SMatthew Dillon "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d, " \
809b06ebda0SMatthew Dillon "cfg_state=%x\n",
810b06ebda0SMatthew Dillon __func__, msg->header.token,
811b06ebda0SMatthew Dillon pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
812b06ebda0SMatthew Dillon pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
813b06ebda0SMatthew Dillon pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
814b06ebda0SMatthew Dillon pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
815b06ebda0SMatthew Dillon pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state);
816b06ebda0SMatthew Dillon
817b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
818e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
819e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
820b06ebda0SMatthew Dillon
821b06ebda0SMatthew Dillon return (ENOENT);
822b06ebda0SMatthew Dillon }
823b06ebda0SMatthew Dillon
824b06ebda0SMatthew Dillon /* Check the result and disconnect socket of failure */
825b06ebda0SMatthew Dillon if (op->result != NG_L2CAP_SUCCESS)
826b06ebda0SMatthew Dillon goto disconnect;
827b06ebda0SMatthew Dillon
828b06ebda0SMatthew Dillon /*
829b06ebda0SMatthew Dillon * Now we done with remote side configuration. Configure local
830b06ebda0SMatthew Dillon * side if we have not done it yet.
831b06ebda0SMatthew Dillon */
832b06ebda0SMatthew Dillon
833b06ebda0SMatthew Dillon pcb->cfg_state &= ~NG_BTSOCKET_L2CAP_CFG_OUT_SENT;
834b06ebda0SMatthew Dillon pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_OUT;
835b06ebda0SMatthew Dillon
836b06ebda0SMatthew Dillon if (pcb->cfg_state == NG_BTSOCKET_L2CAP_CFG_BOTH) {
837b06ebda0SMatthew Dillon /* Configuration complete - mask socket as open */
838b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
839b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_OPEN;
840b06ebda0SMatthew Dillon soisconnected(pcb->so);
841b06ebda0SMatthew Dillon } else {
842b06ebda0SMatthew Dillon if (!(pcb->cfg_state & NG_BTSOCKET_L2CAP_CFG_IN_SENT)) {
843b06ebda0SMatthew Dillon /* Send L2CA_Config request - incoming path */
844b06ebda0SMatthew Dillon error = ng_btsocket_l2cap_send_l2ca_cfg_req(pcb);
845b06ebda0SMatthew Dillon if (error != 0)
846b06ebda0SMatthew Dillon goto disconnect;
847b06ebda0SMatthew Dillon
848b06ebda0SMatthew Dillon pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_IN_SENT;
849b06ebda0SMatthew Dillon }
850b06ebda0SMatthew Dillon }
851b06ebda0SMatthew Dillon
852e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
853e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
854b06ebda0SMatthew Dillon
855b06ebda0SMatthew Dillon return (error);
856b06ebda0SMatthew Dillon
857b06ebda0SMatthew Dillon disconnect:
858b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
859b06ebda0SMatthew Dillon
860b06ebda0SMatthew Dillon /* Send disconnect with "zero" token */
861b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
862b06ebda0SMatthew Dillon
863b06ebda0SMatthew Dillon /* ... and close the socket */
864b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
865b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
866b06ebda0SMatthew Dillon
867e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
868e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
869b06ebda0SMatthew Dillon
870b06ebda0SMatthew Dillon return (error);
871b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp */
872b06ebda0SMatthew Dillon
873b06ebda0SMatthew Dillon /*
874b06ebda0SMatthew Dillon * Process L2CA_Config indicator
875b06ebda0SMatthew Dillon */
876b06ebda0SMatthew Dillon
877b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_process_l2ca_cfg_ind(struct ng_mesg * msg,ng_btsocket_l2cap_rtentry_p rt)878b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_cfg_ind(struct ng_mesg *msg,
879b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt)
880b06ebda0SMatthew Dillon {
881b06ebda0SMatthew Dillon ng_l2cap_l2ca_cfg_ind_ip *ip = NULL;
882b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = NULL;
883b06ebda0SMatthew Dillon int error = 0;
884b06ebda0SMatthew Dillon
885b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(*ip))
886b06ebda0SMatthew Dillon return (EMSGSIZE);
887b06ebda0SMatthew Dillon
888b06ebda0SMatthew Dillon ip = (ng_l2cap_l2ca_cfg_ind_ip *)(msg->data);
889b06ebda0SMatthew Dillon
890e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
891b06ebda0SMatthew Dillon
892b06ebda0SMatthew Dillon /* Check for the open socket that has given channel ID */
893b06ebda0SMatthew Dillon pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid);
894b06ebda0SMatthew Dillon if (pcb == NULL) {
895e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
896b06ebda0SMatthew Dillon return (ENOENT);
897b06ebda0SMatthew Dillon }
898b06ebda0SMatthew Dillon
899e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
900b06ebda0SMatthew Dillon
901b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
902b06ebda0SMatthew Dillon "%s: Got L2CA_Config indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
903b06ebda0SMatthew Dillon "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, state=%d, cfg_state=%x\n",
904b06ebda0SMatthew Dillon __func__,
905b06ebda0SMatthew Dillon pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
906b06ebda0SMatthew Dillon pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
907b06ebda0SMatthew Dillon pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
908b06ebda0SMatthew Dillon pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
909b06ebda0SMatthew Dillon pcb->psm, pcb->cid, pcb->state, pcb->cfg_state);
910b06ebda0SMatthew Dillon
911b06ebda0SMatthew Dillon /* XXX FIXME re-configuration on open socket */
912b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
913e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
914e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
915b06ebda0SMatthew Dillon
916b06ebda0SMatthew Dillon return (ENOENT);
917b06ebda0SMatthew Dillon }
918b06ebda0SMatthew Dillon
919b06ebda0SMatthew Dillon /*
920b06ebda0SMatthew Dillon * XXX FIXME Actually set flush and link timeout. Set QoS here if
921b06ebda0SMatthew Dillon * required. Resolve conficts (flush_timo). Note outgoing MTU (peer's
922b06ebda0SMatthew Dillon * incoming MTU) and incoming flow spec.
923b06ebda0SMatthew Dillon */
924b06ebda0SMatthew Dillon
925b06ebda0SMatthew Dillon pcb->omtu = ip->omtu;
926b06ebda0SMatthew Dillon bcopy(&ip->iflow, &pcb->iflow, sizeof(pcb->iflow));
927b06ebda0SMatthew Dillon pcb->flush_timo = ip->flush_timo;
928b06ebda0SMatthew Dillon
929b06ebda0SMatthew Dillon /*
930b06ebda0SMatthew Dillon * Send L2CA_Config response to our peer and check for the errors,
931b06ebda0SMatthew Dillon * if any send disconnect to close the channel.
932b06ebda0SMatthew Dillon */
933b06ebda0SMatthew Dillon
934b06ebda0SMatthew Dillon if (!(pcb->cfg_state & NG_BTSOCKET_L2CAP_CFG_OUT_SENT)) {
935b06ebda0SMatthew Dillon error = ng_btsocket_l2cap_send_l2ca_cfg_rsp(pcb);
936b06ebda0SMatthew Dillon if (error != 0) {
937b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
938b06ebda0SMatthew Dillon
939b06ebda0SMatthew Dillon pcb->so->so_error = error;
940b06ebda0SMatthew Dillon
941b06ebda0SMatthew Dillon /* Send disconnect with "zero" token */
942b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
943b06ebda0SMatthew Dillon
944b06ebda0SMatthew Dillon /* ... and close the socket */
945b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
946b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
947b06ebda0SMatthew Dillon } else
948b06ebda0SMatthew Dillon pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_OUT_SENT;
949b06ebda0SMatthew Dillon }
950b06ebda0SMatthew Dillon
951e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
952e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
953b06ebda0SMatthew Dillon
954b06ebda0SMatthew Dillon return (error);
955b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_process_l2cap_cfg_ind */
956b06ebda0SMatthew Dillon
957b06ebda0SMatthew Dillon /*
958b06ebda0SMatthew Dillon * Process L2CA_Disconnect response
959b06ebda0SMatthew Dillon */
960b06ebda0SMatthew Dillon
961b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_process_l2ca_discon_rsp(struct ng_mesg * msg,ng_btsocket_l2cap_rtentry_p rt)962b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_discon_rsp(struct ng_mesg *msg,
963b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt)
964b06ebda0SMatthew Dillon {
965b06ebda0SMatthew Dillon ng_l2cap_l2ca_discon_op *op = NULL;
966b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = NULL;
967b06ebda0SMatthew Dillon
968b06ebda0SMatthew Dillon /* Check message */
969b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(*op))
970b06ebda0SMatthew Dillon return (EMSGSIZE);
971b06ebda0SMatthew Dillon
972b06ebda0SMatthew Dillon op = (ng_l2cap_l2ca_discon_op *)(msg->data);
973b06ebda0SMatthew Dillon
974e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
975b06ebda0SMatthew Dillon
976b06ebda0SMatthew Dillon /*
977b06ebda0SMatthew Dillon * Socket layer must have issued L2CA_Disconnect request, so there
978b06ebda0SMatthew Dillon * must be a socket that wants to be disconnected. Use Netgraph
979b06ebda0SMatthew Dillon * message token to find it.
980b06ebda0SMatthew Dillon */
981b06ebda0SMatthew Dillon
982b06ebda0SMatthew Dillon pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
983b06ebda0SMatthew Dillon if (pcb == NULL) {
984e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
985b06ebda0SMatthew Dillon return (0);
986b06ebda0SMatthew Dillon }
987b06ebda0SMatthew Dillon
988e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
989b06ebda0SMatthew Dillon
990b06ebda0SMatthew Dillon /* XXX Close socket no matter what op->result says */
991b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
992b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
993b06ebda0SMatthew Dillon "%s: Got L2CA_Disconnect response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
994b06ebda0SMatthew Dillon "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d\n",
995b06ebda0SMatthew Dillon __func__, msg->header.token,
996b06ebda0SMatthew Dillon pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
997b06ebda0SMatthew Dillon pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
998b06ebda0SMatthew Dillon pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
999b06ebda0SMatthew Dillon pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
1000b06ebda0SMatthew Dillon pcb->psm, pcb->cid, op->result, pcb->state);
1001b06ebda0SMatthew Dillon
1002b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
1003b06ebda0SMatthew Dillon
1004b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1005b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
1006b06ebda0SMatthew Dillon }
1007b06ebda0SMatthew Dillon
1008e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
1009e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1010b06ebda0SMatthew Dillon
1011b06ebda0SMatthew Dillon return (0);
1012b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_process_l2ca_discon_rsp */
1013b06ebda0SMatthew Dillon
1014b06ebda0SMatthew Dillon /*
1015b06ebda0SMatthew Dillon * Process L2CA_Disconnect indicator
1016b06ebda0SMatthew Dillon */
1017b06ebda0SMatthew Dillon
1018b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_process_l2ca_discon_ind(struct ng_mesg * msg,ng_btsocket_l2cap_rtentry_p rt)1019b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_discon_ind(struct ng_mesg *msg,
1020b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt)
1021b06ebda0SMatthew Dillon {
1022b06ebda0SMatthew Dillon ng_l2cap_l2ca_discon_ind_ip *ip = NULL;
1023b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = NULL;
1024b06ebda0SMatthew Dillon
1025b06ebda0SMatthew Dillon /* Check message */
1026b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(*ip))
1027b06ebda0SMatthew Dillon return (EMSGSIZE);
1028b06ebda0SMatthew Dillon
1029b06ebda0SMatthew Dillon ip = (ng_l2cap_l2ca_discon_ind_ip *)(msg->data);
1030b06ebda0SMatthew Dillon
1031e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
1032b06ebda0SMatthew Dillon
1033b06ebda0SMatthew Dillon /* Look for the socket with given channel ID */
1034b06ebda0SMatthew Dillon pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid);
1035b06ebda0SMatthew Dillon if (pcb == NULL) {
1036e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1037b06ebda0SMatthew Dillon return (0);
1038b06ebda0SMatthew Dillon }
1039b06ebda0SMatthew Dillon
1040b06ebda0SMatthew Dillon /*
1041b06ebda0SMatthew Dillon * Channel has already been destroyed, so disconnect the socket
1042b06ebda0SMatthew Dillon * and be done with it. If there was any pending request we can
1043b06ebda0SMatthew Dillon * not do anything here anyway.
1044b06ebda0SMatthew Dillon */
1045b06ebda0SMatthew Dillon
1046e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1047b06ebda0SMatthew Dillon
1048b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
1049b06ebda0SMatthew Dillon "%s: Got L2CA_Disconnect indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1050b06ebda0SMatthew Dillon "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, state=%d\n",
1051b06ebda0SMatthew Dillon __func__,
1052b06ebda0SMatthew Dillon pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
1053b06ebda0SMatthew Dillon pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
1054b06ebda0SMatthew Dillon pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
1055b06ebda0SMatthew Dillon pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
1056b06ebda0SMatthew Dillon pcb->psm, pcb->cid, pcb->state);
1057b06ebda0SMatthew Dillon
1058b06ebda0SMatthew Dillon if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
1059b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
1060b06ebda0SMatthew Dillon
1061b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1062b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
1063b06ebda0SMatthew Dillon
1064e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
1065e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1066b06ebda0SMatthew Dillon
1067b06ebda0SMatthew Dillon return (0);
1068b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_process_l2ca_discon_ind */
1069b06ebda0SMatthew Dillon
1070b06ebda0SMatthew Dillon /*
1071b06ebda0SMatthew Dillon * Process L2CA_Write response
1072b06ebda0SMatthew Dillon */
1073b06ebda0SMatthew Dillon
1074b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_process_l2ca_write_rsp(struct ng_mesg * msg,ng_btsocket_l2cap_rtentry_p rt)1075b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_write_rsp(struct ng_mesg *msg,
1076b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt)
1077b06ebda0SMatthew Dillon {
1078b06ebda0SMatthew Dillon ng_l2cap_l2ca_write_op *op = NULL;
1079b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = NULL;
1080b06ebda0SMatthew Dillon
1081b06ebda0SMatthew Dillon /* Check message */
1082b06ebda0SMatthew Dillon if (msg->header.arglen != sizeof(*op))
1083b06ebda0SMatthew Dillon return (EMSGSIZE);
1084b06ebda0SMatthew Dillon
1085b06ebda0SMatthew Dillon op = (ng_l2cap_l2ca_write_op *)(msg->data);
1086b06ebda0SMatthew Dillon
1087e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
1088b06ebda0SMatthew Dillon
1089b06ebda0SMatthew Dillon /* Look for the socket with given token */
1090b06ebda0SMatthew Dillon pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
1091b06ebda0SMatthew Dillon if (pcb == NULL) {
1092e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1093b06ebda0SMatthew Dillon return (ENOENT);
1094b06ebda0SMatthew Dillon }
1095b06ebda0SMatthew Dillon
1096e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1097b06ebda0SMatthew Dillon
1098b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
1099b06ebda0SMatthew Dillon "%s: Got L2CA_Write response, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1100b06ebda0SMatthew Dillon "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, length=%d, " \
1101b06ebda0SMatthew Dillon "state=%d\n", __func__,
1102b06ebda0SMatthew Dillon pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
1103b06ebda0SMatthew Dillon pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
1104b06ebda0SMatthew Dillon pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
1105b06ebda0SMatthew Dillon pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
1106b06ebda0SMatthew Dillon pcb->psm, pcb->cid, op->result, op->length,
1107b06ebda0SMatthew Dillon pcb->state);
1108b06ebda0SMatthew Dillon
1109b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
1110e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
1111e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1112b06ebda0SMatthew Dillon
1113b06ebda0SMatthew Dillon return (ENOENT);
1114b06ebda0SMatthew Dillon }
1115b06ebda0SMatthew Dillon
1116b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
1117b06ebda0SMatthew Dillon
1118b06ebda0SMatthew Dillon /*
1119b06ebda0SMatthew Dillon * Check if we have more data to send
1120b06ebda0SMatthew Dillon */
1121b06ebda0SMatthew Dillon
1122e85b99abSSascha Wildner sbdroprecord(&pcb->so->so_snd.sb);
1123e85b99abSSascha Wildner if (pcb->so->so_snd.sb.sb_cc > 0) {
1124b06ebda0SMatthew Dillon if (ng_btsocket_l2cap_send2(pcb) == 0)
1125b06ebda0SMatthew Dillon ng_btsocket_l2cap_timeout(pcb);
1126b06ebda0SMatthew Dillon else
1127e85b99abSSascha Wildner sbdroprecord(&pcb->so->so_snd.sb); /* XXX */
1128b06ebda0SMatthew Dillon }
1129b06ebda0SMatthew Dillon
1130b06ebda0SMatthew Dillon /*
1131b06ebda0SMatthew Dillon * Now set the result, drop packet from the socket send queue and
1132b06ebda0SMatthew Dillon * ask for more (wakeup sender)
1133b06ebda0SMatthew Dillon */
1134b06ebda0SMatthew Dillon
1135b06ebda0SMatthew Dillon pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
1136b06ebda0SMatthew Dillon sowwakeup(pcb->so);
1137b06ebda0SMatthew Dillon
1138e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
1139e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1140b06ebda0SMatthew Dillon
1141b06ebda0SMatthew Dillon return (0);
1142b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_process_l2ca_write_rsp */
1143b06ebda0SMatthew Dillon
1144b06ebda0SMatthew Dillon /*
1145b06ebda0SMatthew Dillon * Send L2CA_Connect request
1146b06ebda0SMatthew Dillon */
1147b06ebda0SMatthew Dillon
1148b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_send_l2ca_con_req(ng_btsocket_l2cap_pcb_p pcb)1149b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_con_req(ng_btsocket_l2cap_pcb_p pcb)
1150b06ebda0SMatthew Dillon {
1151b06ebda0SMatthew Dillon struct ng_mesg *msg = NULL;
1152b06ebda0SMatthew Dillon ng_l2cap_l2ca_con_ip *ip = NULL;
1153b06ebda0SMatthew Dillon int error = 0;
1154b06ebda0SMatthew Dillon
1155e85b99abSSascha Wildner KKASSERT(lockowned(&pcb->pcb_lock) != 0);
1156b06ebda0SMatthew Dillon
1157b06ebda0SMatthew Dillon if (pcb->rt == NULL ||
1158b06ebda0SMatthew Dillon pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1159b06ebda0SMatthew Dillon return (ENETDOWN);
1160b06ebda0SMatthew Dillon
1161b06ebda0SMatthew Dillon NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CON,
11625a975a3dSMatthew Dillon sizeof(*ip), M_WAITOK | M_NULLOK);
1163b06ebda0SMatthew Dillon if (msg == NULL)
1164b06ebda0SMatthew Dillon return (ENOMEM);
1165b06ebda0SMatthew Dillon
1166b06ebda0SMatthew Dillon msg->header.token = pcb->token;
1167b06ebda0SMatthew Dillon
1168b06ebda0SMatthew Dillon ip = (ng_l2cap_l2ca_con_ip *)(msg->data);
1169b06ebda0SMatthew Dillon bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
1170b06ebda0SMatthew Dillon ip->psm = pcb->psm;
1171b06ebda0SMatthew Dillon
1172b06ebda0SMatthew Dillon NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0);
1173b06ebda0SMatthew Dillon
1174b06ebda0SMatthew Dillon return (error);
1175b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_send_l2ca_con_req */
1176b06ebda0SMatthew Dillon
1177b06ebda0SMatthew Dillon /*
1178b06ebda0SMatthew Dillon * Send L2CA_Connect response
1179b06ebda0SMatthew Dillon */
1180b06ebda0SMatthew Dillon
1181b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_send_l2ca_con_rsp_req(u_int32_t token,ng_btsocket_l2cap_rtentry_p rt,bdaddr_p dst,int ident,int lcid,int result)1182b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_con_rsp_req(u_int32_t token,
1183b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt, bdaddr_p dst, int ident,
1184b06ebda0SMatthew Dillon int lcid, int result)
1185b06ebda0SMatthew Dillon {
1186b06ebda0SMatthew Dillon struct ng_mesg *msg = NULL;
1187b06ebda0SMatthew Dillon ng_l2cap_l2ca_con_rsp_ip *ip = NULL;
1188b06ebda0SMatthew Dillon int error = 0;
1189b06ebda0SMatthew Dillon
1190b06ebda0SMatthew Dillon if (rt == NULL || rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
1191b06ebda0SMatthew Dillon return (ENETDOWN);
1192b06ebda0SMatthew Dillon
1193b06ebda0SMatthew Dillon NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CON_RSP,
11945a975a3dSMatthew Dillon sizeof(*ip), M_WAITOK | M_NULLOK);
1195b06ebda0SMatthew Dillon if (msg == NULL)
1196b06ebda0SMatthew Dillon return (ENOMEM);
1197b06ebda0SMatthew Dillon
1198b06ebda0SMatthew Dillon msg->header.token = token;
1199b06ebda0SMatthew Dillon
1200b06ebda0SMatthew Dillon ip = (ng_l2cap_l2ca_con_rsp_ip *)(msg->data);
1201b06ebda0SMatthew Dillon bcopy(dst, &ip->bdaddr, sizeof(ip->bdaddr));
1202b06ebda0SMatthew Dillon ip->ident = ident;
1203b06ebda0SMatthew Dillon ip->lcid = lcid;
1204b06ebda0SMatthew Dillon ip->result = result;
1205b06ebda0SMatthew Dillon ip->status = 0;
1206b06ebda0SMatthew Dillon
1207b06ebda0SMatthew Dillon NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg, rt->hook, 0);
1208b06ebda0SMatthew Dillon
1209b06ebda0SMatthew Dillon return (error);
1210b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_send_l2ca_con_rsp_req */
1211b06ebda0SMatthew Dillon
1212b06ebda0SMatthew Dillon /*
1213b06ebda0SMatthew Dillon * Send L2CA_Config request
1214b06ebda0SMatthew Dillon */
1215b06ebda0SMatthew Dillon
1216b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_send_l2ca_cfg_req(ng_btsocket_l2cap_pcb_p pcb)1217b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_cfg_req(ng_btsocket_l2cap_pcb_p pcb)
1218b06ebda0SMatthew Dillon {
1219b06ebda0SMatthew Dillon struct ng_mesg *msg = NULL;
1220b06ebda0SMatthew Dillon ng_l2cap_l2ca_cfg_ip *ip = NULL;
1221b06ebda0SMatthew Dillon int error = 0;
1222b06ebda0SMatthew Dillon
1223e85b99abSSascha Wildner KKASSERT(lockowned(&pcb->pcb_lock) != 0);
1224b06ebda0SMatthew Dillon
1225b06ebda0SMatthew Dillon if (pcb->rt == NULL ||
1226b06ebda0SMatthew Dillon pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1227b06ebda0SMatthew Dillon return (ENETDOWN);
1228b06ebda0SMatthew Dillon
1229b06ebda0SMatthew Dillon NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CFG,
12305a975a3dSMatthew Dillon sizeof(*ip), M_WAITOK | M_NULLOK);
1231b06ebda0SMatthew Dillon if (msg == NULL)
1232b06ebda0SMatthew Dillon return (ENOMEM);
1233b06ebda0SMatthew Dillon
1234b06ebda0SMatthew Dillon msg->header.token = pcb->token;
1235b06ebda0SMatthew Dillon
1236b06ebda0SMatthew Dillon ip = (ng_l2cap_l2ca_cfg_ip *)(msg->data);
1237b06ebda0SMatthew Dillon ip->lcid = pcb->cid;
1238b06ebda0SMatthew Dillon ip->imtu = pcb->imtu;
1239b06ebda0SMatthew Dillon bcopy(&pcb->oflow, &ip->oflow, sizeof(ip->oflow));
1240b06ebda0SMatthew Dillon ip->flush_timo = pcb->flush_timo;
1241b06ebda0SMatthew Dillon ip->link_timo = pcb->link_timo;
1242b06ebda0SMatthew Dillon
1243b06ebda0SMatthew Dillon NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0);
1244b06ebda0SMatthew Dillon
1245b06ebda0SMatthew Dillon return (error);
1246b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_send_l2ca_cfg_req */
1247b06ebda0SMatthew Dillon
1248b06ebda0SMatthew Dillon /*
1249b06ebda0SMatthew Dillon * Send L2CA_Config response
1250b06ebda0SMatthew Dillon */
1251b06ebda0SMatthew Dillon
1252b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_send_l2ca_cfg_rsp(ng_btsocket_l2cap_pcb_p pcb)1253b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_cfg_rsp(ng_btsocket_l2cap_pcb_p pcb)
1254b06ebda0SMatthew Dillon {
1255b06ebda0SMatthew Dillon struct ng_mesg *msg = NULL;
1256b06ebda0SMatthew Dillon ng_l2cap_l2ca_cfg_rsp_ip *ip = NULL;
1257b06ebda0SMatthew Dillon int error = 0;
1258b06ebda0SMatthew Dillon
1259e85b99abSSascha Wildner KKASSERT(lockowned(&pcb->pcb_lock) != 0);
1260b06ebda0SMatthew Dillon
1261b06ebda0SMatthew Dillon if (pcb->rt == NULL ||
1262b06ebda0SMatthew Dillon pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1263b06ebda0SMatthew Dillon return (ENETDOWN);
1264b06ebda0SMatthew Dillon
1265b06ebda0SMatthew Dillon NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CFG_RSP,
12665a975a3dSMatthew Dillon sizeof(*ip), M_WAITOK | M_NULLOK);
1267b06ebda0SMatthew Dillon if (msg == NULL)
1268b06ebda0SMatthew Dillon return (ENOMEM);
1269b06ebda0SMatthew Dillon
1270b06ebda0SMatthew Dillon msg->header.token = pcb->token;
1271b06ebda0SMatthew Dillon
1272b06ebda0SMatthew Dillon ip = (ng_l2cap_l2ca_cfg_rsp_ip *)(msg->data);
1273b06ebda0SMatthew Dillon ip->lcid = pcb->cid;
1274b06ebda0SMatthew Dillon ip->omtu = pcb->omtu;
1275b06ebda0SMatthew Dillon bcopy(&pcb->iflow, &ip->iflow, sizeof(ip->iflow));
1276b06ebda0SMatthew Dillon
1277b06ebda0SMatthew Dillon NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg, pcb->rt->hook, 0);
1278b06ebda0SMatthew Dillon
1279b06ebda0SMatthew Dillon return (error);
1280b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_send_l2ca_cfg_rsp */
1281b06ebda0SMatthew Dillon
1282b06ebda0SMatthew Dillon /*
1283b06ebda0SMatthew Dillon * Send L2CA_Disconnect request
1284b06ebda0SMatthew Dillon */
1285b06ebda0SMatthew Dillon
1286b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_send_l2ca_discon_req(u_int32_t token,ng_btsocket_l2cap_pcb_p pcb)1287b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_discon_req(u_int32_t token,
1288b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb)
1289b06ebda0SMatthew Dillon {
1290b06ebda0SMatthew Dillon struct ng_mesg *msg = NULL;
1291b06ebda0SMatthew Dillon ng_l2cap_l2ca_discon_ip *ip = NULL;
1292b06ebda0SMatthew Dillon int error = 0;
1293b06ebda0SMatthew Dillon
1294e85b99abSSascha Wildner KKASSERT(lockowned(&pcb->pcb_lock) != 0);
1295b06ebda0SMatthew Dillon
1296b06ebda0SMatthew Dillon if (pcb->rt == NULL ||
1297b06ebda0SMatthew Dillon pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1298b06ebda0SMatthew Dillon return (ENETDOWN);
1299b06ebda0SMatthew Dillon
1300b06ebda0SMatthew Dillon NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_DISCON,
13015a975a3dSMatthew Dillon sizeof(*ip), M_WAITOK | M_NULLOK);
1302b06ebda0SMatthew Dillon if (msg == NULL)
1303b06ebda0SMatthew Dillon return (ENOMEM);
1304b06ebda0SMatthew Dillon
1305b06ebda0SMatthew Dillon msg->header.token = token;
1306b06ebda0SMatthew Dillon
1307b06ebda0SMatthew Dillon ip = (ng_l2cap_l2ca_discon_ip *)(msg->data);
1308b06ebda0SMatthew Dillon ip->lcid = pcb->cid;
1309b06ebda0SMatthew Dillon
1310b06ebda0SMatthew Dillon NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0);
1311b06ebda0SMatthew Dillon
1312b06ebda0SMatthew Dillon return (error);
1313b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_send_l2ca_discon_req */
1314b06ebda0SMatthew Dillon
1315b06ebda0SMatthew Dillon /*****************************************************************************
1316b06ebda0SMatthew Dillon *****************************************************************************
1317b06ebda0SMatthew Dillon ** Socket interface
1318b06ebda0SMatthew Dillon *****************************************************************************
1319b06ebda0SMatthew Dillon *****************************************************************************/
1320b06ebda0SMatthew Dillon
1321b06ebda0SMatthew Dillon /*
1322b06ebda0SMatthew Dillon * L2CAP sockets data input routine
1323b06ebda0SMatthew Dillon */
1324b06ebda0SMatthew Dillon
1325b06ebda0SMatthew Dillon static void
ng_btsocket_l2cap_data_input(struct mbuf * m,hook_p hook)1326b06ebda0SMatthew Dillon ng_btsocket_l2cap_data_input(struct mbuf *m, hook_p hook)
1327b06ebda0SMatthew Dillon {
1328b06ebda0SMatthew Dillon ng_l2cap_hdr_t *hdr = NULL;
1329b06ebda0SMatthew Dillon ng_l2cap_clt_hdr_t *clt_hdr = NULL;
1330b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = NULL;
1331b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_t *rt = NULL;
1332b06ebda0SMatthew Dillon
1333b06ebda0SMatthew Dillon if (hook == NULL) {
1334b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ALERT(
1335b06ebda0SMatthew Dillon "%s: Invalid source hook for L2CAP data packet\n", __func__);
1336b06ebda0SMatthew Dillon goto drop;
1337b06ebda0SMatthew Dillon }
1338b06ebda0SMatthew Dillon
1339b06ebda0SMatthew Dillon rt = (ng_btsocket_l2cap_rtentry_t *) NG_HOOK_PRIVATE(hook);
1340b06ebda0SMatthew Dillon if (rt == NULL) {
1341b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ALERT(
1342b06ebda0SMatthew Dillon "%s: Could not find out source bdaddr for L2CAP data packet\n", __func__);
1343b06ebda0SMatthew Dillon goto drop;
1344b06ebda0SMatthew Dillon }
1345b06ebda0SMatthew Dillon
1346b06ebda0SMatthew Dillon /* Make sure we can access header */
1347b06ebda0SMatthew Dillon if (m->m_pkthdr.len < sizeof(*hdr)) {
1348b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
1349b06ebda0SMatthew Dillon "%s: L2CAP data packet too small, len=%d\n", __func__, m->m_pkthdr.len);
1350b06ebda0SMatthew Dillon goto drop;
1351b06ebda0SMatthew Dillon }
1352b06ebda0SMatthew Dillon
1353b06ebda0SMatthew Dillon if (m->m_len < sizeof(*hdr)) {
1354b06ebda0SMatthew Dillon m = m_pullup(m, sizeof(*hdr));
1355b06ebda0SMatthew Dillon if (m == NULL)
1356b06ebda0SMatthew Dillon goto drop;
1357b06ebda0SMatthew Dillon }
1358b06ebda0SMatthew Dillon
1359b06ebda0SMatthew Dillon /* Strip L2CAP packet header and verify packet length */
1360b06ebda0SMatthew Dillon hdr = mtod(m, ng_l2cap_hdr_t *);
1361b06ebda0SMatthew Dillon m_adj(m, sizeof(*hdr));
1362b06ebda0SMatthew Dillon
1363b06ebda0SMatthew Dillon if (hdr->length != m->m_pkthdr.len) {
1364b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
1365b06ebda0SMatthew Dillon "%s: Bad L2CAP data packet length, len=%d, length=%d\n",
1366b06ebda0SMatthew Dillon __func__, m->m_pkthdr.len, hdr->length);
1367b06ebda0SMatthew Dillon goto drop;
1368b06ebda0SMatthew Dillon }
1369b06ebda0SMatthew Dillon
1370b06ebda0SMatthew Dillon /*
1371b06ebda0SMatthew Dillon * Now process packet. Two cases:
1372b06ebda0SMatthew Dillon *
1373b06ebda0SMatthew Dillon * 1) Normal packet (cid != 2) then find connected socket and append
1374b06ebda0SMatthew Dillon * mbuf to the socket queue. Wakeup socket.
1375b06ebda0SMatthew Dillon *
1376b06ebda0SMatthew Dillon * 2) Broadcast packet (cid == 2) then find all sockets that connected
1377b06ebda0SMatthew Dillon * to the given PSM and have SO_BROADCAST bit set and append mbuf
1378b06ebda0SMatthew Dillon * to the socket queue. Wakeup socket.
1379b06ebda0SMatthew Dillon */
1380b06ebda0SMatthew Dillon
1381b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
1382b06ebda0SMatthew Dillon "%s: Received L2CAP data packet: src bdaddr=%x:%x:%x:%x:%x:%x, " \
1383b06ebda0SMatthew Dillon "dcid=%d, length=%d\n",
1384b06ebda0SMatthew Dillon __func__,
1385b06ebda0SMatthew Dillon rt->src.b[5], rt->src.b[4], rt->src.b[3],
1386b06ebda0SMatthew Dillon rt->src.b[2], rt->src.b[1], rt->src.b[0],
1387b06ebda0SMatthew Dillon hdr->dcid, hdr->length);
1388b06ebda0SMatthew Dillon
1389b06ebda0SMatthew Dillon if (hdr->dcid >= NG_L2CAP_FIRST_CID) {
1390b06ebda0SMatthew Dillon
1391e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
1392b06ebda0SMatthew Dillon
1393b06ebda0SMatthew Dillon /* Normal packet: find connected socket */
1394b06ebda0SMatthew Dillon pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, hdr->dcid);
1395b06ebda0SMatthew Dillon if (pcb == NULL) {
1396e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1397b06ebda0SMatthew Dillon goto drop;
1398b06ebda0SMatthew Dillon }
1399b06ebda0SMatthew Dillon
1400e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1401b06ebda0SMatthew Dillon
1402b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
1403b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
1404b06ebda0SMatthew Dillon "%s: No connected socket found, src bdaddr=%x:%x:%x:%x:%x:%x, dcid=%d, " \
1405b06ebda0SMatthew Dillon "state=%d\n", __func__,
1406b06ebda0SMatthew Dillon rt->src.b[5], rt->src.b[4], rt->src.b[3],
1407b06ebda0SMatthew Dillon rt->src.b[2], rt->src.b[1], rt->src.b[0],
1408b06ebda0SMatthew Dillon hdr->dcid, pcb->state);
1409b06ebda0SMatthew Dillon
1410e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
1411e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1412b06ebda0SMatthew Dillon goto drop;
1413b06ebda0SMatthew Dillon }
1414b06ebda0SMatthew Dillon
1415b06ebda0SMatthew Dillon /* Check packet size against socket's incoming MTU */
1416b06ebda0SMatthew Dillon if (hdr->length > pcb->imtu) {
1417b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
1418b06ebda0SMatthew Dillon "%s: L2CAP data packet too big, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1419b06ebda0SMatthew Dillon "dcid=%d, length=%d, imtu=%d\n",
1420b06ebda0SMatthew Dillon __func__,
1421b06ebda0SMatthew Dillon rt->src.b[5], rt->src.b[4], rt->src.b[3],
1422b06ebda0SMatthew Dillon rt->src.b[2], rt->src.b[1], rt->src.b[0],
1423b06ebda0SMatthew Dillon hdr->dcid, hdr->length, pcb->imtu);
1424b06ebda0SMatthew Dillon
1425e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
1426e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1427b06ebda0SMatthew Dillon goto drop;
1428b06ebda0SMatthew Dillon }
1429b06ebda0SMatthew Dillon
1430b06ebda0SMatthew Dillon /* Check if we have enough space in socket receive queue */
1431e85b99abSSascha Wildner if (m->m_pkthdr.len > ssb_space(&pcb->so->so_rcv)) {
1432b06ebda0SMatthew Dillon
1433b06ebda0SMatthew Dillon /*
1434b06ebda0SMatthew Dillon * This is really bad. Receive queue on socket does
1435b06ebda0SMatthew Dillon * not have enough space for the packet. We do not
1436b06ebda0SMatthew Dillon * have any other choice but drop the packet. L2CAP
1437b06ebda0SMatthew Dillon * does not provide any flow control.
1438b06ebda0SMatthew Dillon */
1439b06ebda0SMatthew Dillon
1440b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
1441b06ebda0SMatthew Dillon "%s: Not enough space in socket receive queue. Dropping L2CAP data packet, " \
1442b06ebda0SMatthew Dillon "src bdaddr=%x:%x:%x:%x:%x:%x, dcid=%d, len=%d, space=%ld\n",
1443b06ebda0SMatthew Dillon __func__,
1444b06ebda0SMatthew Dillon rt->src.b[5], rt->src.b[4], rt->src.b[3],
1445b06ebda0SMatthew Dillon rt->src.b[2], rt->src.b[1], rt->src.b[0],
1446b06ebda0SMatthew Dillon hdr->dcid, m->m_pkthdr.len,
1447e85b99abSSascha Wildner ssb_space(&pcb->so->so_rcv));
1448b06ebda0SMatthew Dillon
1449e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
1450e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1451b06ebda0SMatthew Dillon goto drop;
1452b06ebda0SMatthew Dillon }
1453b06ebda0SMatthew Dillon
1454b06ebda0SMatthew Dillon /* Append packet to the socket receive queue and wakeup */
1455e85b99abSSascha Wildner sbappendrecord(&pcb->so->so_rcv.sb, m);
1456b06ebda0SMatthew Dillon m = NULL;
1457b06ebda0SMatthew Dillon
1458b06ebda0SMatthew Dillon sorwakeup(pcb->so);
1459b06ebda0SMatthew Dillon
1460e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
1461e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1462b06ebda0SMatthew Dillon } else if (hdr->dcid == NG_L2CAP_CLT_CID) {
1463b06ebda0SMatthew Dillon /* Broadcast packet: give packet to all sockets */
1464b06ebda0SMatthew Dillon
1465b06ebda0SMatthew Dillon /* Check packet size against connectionless MTU */
1466b06ebda0SMatthew Dillon if (hdr->length > NG_L2CAP_MTU_DEFAULT) {
1467b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
1468b06ebda0SMatthew Dillon "%s: Connectionless L2CAP data packet too big, " \
1469b06ebda0SMatthew Dillon "src bdaddr=%x:%x:%x:%x:%x:%x, length=%d\n",
1470b06ebda0SMatthew Dillon __func__,
1471b06ebda0SMatthew Dillon rt->src.b[5], rt->src.b[4], rt->src.b[3],
1472b06ebda0SMatthew Dillon rt->src.b[2], rt->src.b[1], rt->src.b[0],
1473b06ebda0SMatthew Dillon hdr->length);
1474b06ebda0SMatthew Dillon goto drop;
1475b06ebda0SMatthew Dillon }
1476b06ebda0SMatthew Dillon
1477b06ebda0SMatthew Dillon /* Make sure we can access connectionless header */
1478b06ebda0SMatthew Dillon if (m->m_pkthdr.len < sizeof(*clt_hdr)) {
1479b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
1480b06ebda0SMatthew Dillon "%s: Can not get L2CAP connectionless packet header, " \
1481b06ebda0SMatthew Dillon "src bdaddr=%x:%x:%x:%x:%x:%x, length=%d\n",
1482b06ebda0SMatthew Dillon __func__,
1483b06ebda0SMatthew Dillon rt->src.b[5], rt->src.b[4], rt->src.b[3],
1484b06ebda0SMatthew Dillon rt->src.b[2], rt->src.b[1], rt->src.b[0],
1485b06ebda0SMatthew Dillon hdr->length);
1486b06ebda0SMatthew Dillon goto drop;
1487b06ebda0SMatthew Dillon }
1488b06ebda0SMatthew Dillon
1489b06ebda0SMatthew Dillon if (m->m_len < sizeof(*clt_hdr)) {
1490b06ebda0SMatthew Dillon m = m_pullup(m, sizeof(*clt_hdr));
1491b06ebda0SMatthew Dillon if (m == NULL)
1492b06ebda0SMatthew Dillon goto drop;
1493b06ebda0SMatthew Dillon }
1494b06ebda0SMatthew Dillon
1495b06ebda0SMatthew Dillon /* Strip connectionless header and deliver packet */
1496b06ebda0SMatthew Dillon clt_hdr = mtod(m, ng_l2cap_clt_hdr_t *);
1497b06ebda0SMatthew Dillon m_adj(m, sizeof(*clt_hdr));
1498b06ebda0SMatthew Dillon
1499b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
1500b06ebda0SMatthew Dillon "%s: Got L2CAP connectionless data packet, " \
1501b06ebda0SMatthew Dillon "src bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, length=%d\n",
1502b06ebda0SMatthew Dillon __func__,
1503b06ebda0SMatthew Dillon rt->src.b[5], rt->src.b[4], rt->src.b[3],
1504b06ebda0SMatthew Dillon rt->src.b[2], rt->src.b[1], rt->src.b[0],
1505b06ebda0SMatthew Dillon clt_hdr->psm, hdr->length);
1506b06ebda0SMatthew Dillon
1507e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
1508b06ebda0SMatthew Dillon
1509b06ebda0SMatthew Dillon LIST_FOREACH(pcb, &ng_btsocket_l2cap_sockets, next) {
1510b06ebda0SMatthew Dillon struct mbuf *copy = NULL;
1511b06ebda0SMatthew Dillon
1512e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1513b06ebda0SMatthew Dillon
1514b06ebda0SMatthew Dillon if (bcmp(&rt->src, &pcb->src, sizeof(pcb->src)) != 0 ||
1515b06ebda0SMatthew Dillon pcb->psm != clt_hdr->psm ||
1516b06ebda0SMatthew Dillon pcb->state != NG_BTSOCKET_L2CAP_OPEN ||
1517b06ebda0SMatthew Dillon (pcb->so->so_options & SO_BROADCAST) == 0 ||
1518e85b99abSSascha Wildner m->m_pkthdr.len > ssb_space(&pcb->so->so_rcv))
1519b06ebda0SMatthew Dillon goto next;
1520b06ebda0SMatthew Dillon
1521b06ebda0SMatthew Dillon /*
1522b06ebda0SMatthew Dillon * Create a copy of the packet and append it to the
1523b06ebda0SMatthew Dillon * socket's queue. If m_dup() failed - no big deal
1524b06ebda0SMatthew Dillon * it is a broadcast traffic after all
1525b06ebda0SMatthew Dillon */
1526b06ebda0SMatthew Dillon
1527b5523eacSSascha Wildner copy = m_dup(m, M_NOWAIT);
1528b06ebda0SMatthew Dillon if (copy != NULL) {
1529e85b99abSSascha Wildner sbappendrecord(&pcb->so->so_rcv.sb, copy);
1530b06ebda0SMatthew Dillon sorwakeup(pcb->so);
1531b06ebda0SMatthew Dillon }
1532b06ebda0SMatthew Dillon next:
1533e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
1534b06ebda0SMatthew Dillon }
1535b06ebda0SMatthew Dillon
1536e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1537b06ebda0SMatthew Dillon }
1538b06ebda0SMatthew Dillon drop:
1539b06ebda0SMatthew Dillon NG_FREE_M(m); /* checks for m != NULL */
1540b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_data_input */
1541b06ebda0SMatthew Dillon
1542b06ebda0SMatthew Dillon /*
1543b06ebda0SMatthew Dillon * L2CAP sockets default message input routine
1544b06ebda0SMatthew Dillon */
1545b06ebda0SMatthew Dillon
1546b06ebda0SMatthew Dillon static void
ng_btsocket_l2cap_default_msg_input(struct ng_mesg * msg,hook_p hook)1547b06ebda0SMatthew Dillon ng_btsocket_l2cap_default_msg_input(struct ng_mesg *msg, hook_p hook)
1548b06ebda0SMatthew Dillon {
1549b06ebda0SMatthew Dillon switch (msg->header.cmd) {
1550b06ebda0SMatthew Dillon case NGM_L2CAP_NODE_HOOK_INFO: {
1551b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_t *rt = NULL;
1552b06ebda0SMatthew Dillon
1553b06ebda0SMatthew Dillon if (hook == NULL || msg->header.arglen != sizeof(bdaddr_t))
1554b06ebda0SMatthew Dillon break;
1555b06ebda0SMatthew Dillon
1556b06ebda0SMatthew Dillon if (bcmp(msg->data, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
1557b06ebda0SMatthew Dillon break;
1558b06ebda0SMatthew Dillon
1559e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_rt_lock, LK_EXCLUSIVE);
1560b06ebda0SMatthew Dillon
1561b06ebda0SMatthew Dillon rt = (ng_btsocket_l2cap_rtentry_t *) NG_HOOK_PRIVATE(hook);
1562b06ebda0SMatthew Dillon if (rt == NULL) {
1563fc025606SSascha Wildner rt = kmalloc(sizeof(*rt), M_NETGRAPH_BTSOCKET_L2CAP,
1564fc025606SSascha Wildner M_WAITOK | M_NULLOK | M_ZERO);
1565b06ebda0SMatthew Dillon if (rt == NULL) {
1566e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_rt_lock, LK_RELEASE);
1567b06ebda0SMatthew Dillon break;
1568b06ebda0SMatthew Dillon }
1569b06ebda0SMatthew Dillon
1570b06ebda0SMatthew Dillon LIST_INSERT_HEAD(&ng_btsocket_l2cap_rt, rt, next);
1571b06ebda0SMatthew Dillon
1572b06ebda0SMatthew Dillon NG_HOOK_SET_PRIVATE(hook, rt);
1573b06ebda0SMatthew Dillon }
1574b06ebda0SMatthew Dillon
1575b06ebda0SMatthew Dillon bcopy(msg->data, &rt->src, sizeof(rt->src));
1576b06ebda0SMatthew Dillon rt->hook = hook;
1577b06ebda0SMatthew Dillon
1578e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_rt_lock, LK_RELEASE);
1579b06ebda0SMatthew Dillon
1580b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
1581b06ebda0SMatthew Dillon "%s: Updating hook \"%s\", src bdaddr=%x:%x:%x:%x:%x:%x\n",
1582b06ebda0SMatthew Dillon __func__, NG_HOOK_NAME(hook),
1583b06ebda0SMatthew Dillon rt->src.b[5], rt->src.b[4], rt->src.b[3],
1584b06ebda0SMatthew Dillon rt->src.b[2], rt->src.b[1], rt->src.b[0]);
1585b06ebda0SMatthew Dillon } break;
1586b06ebda0SMatthew Dillon
1587b06ebda0SMatthew Dillon default:
1588b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_WARN(
1589b06ebda0SMatthew Dillon "%s: Unknown message, cmd=%d\n", __func__, msg->header.cmd);
1590b06ebda0SMatthew Dillon break;
1591b06ebda0SMatthew Dillon }
1592b06ebda0SMatthew Dillon
1593b06ebda0SMatthew Dillon NG_FREE_MSG(msg); /* Checks for msg != NULL */
1594b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_default_msg_input */
1595b06ebda0SMatthew Dillon
1596b06ebda0SMatthew Dillon /*
1597b06ebda0SMatthew Dillon * L2CAP sockets L2CA message input routine
1598b06ebda0SMatthew Dillon */
1599b06ebda0SMatthew Dillon
1600b06ebda0SMatthew Dillon static void
ng_btsocket_l2cap_l2ca_msg_input(struct ng_mesg * msg,hook_p hook)1601b06ebda0SMatthew Dillon ng_btsocket_l2cap_l2ca_msg_input(struct ng_mesg *msg, hook_p hook)
1602b06ebda0SMatthew Dillon {
1603b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt = NULL;
1604b06ebda0SMatthew Dillon
1605b06ebda0SMatthew Dillon if (hook == NULL) {
1606b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ALERT(
1607b06ebda0SMatthew Dillon "%s: Invalid source hook for L2CA message\n", __func__);
1608b06ebda0SMatthew Dillon goto drop;
1609b06ebda0SMatthew Dillon }
1610b06ebda0SMatthew Dillon
1611b06ebda0SMatthew Dillon rt = (ng_btsocket_l2cap_rtentry_p) NG_HOOK_PRIVATE(hook);
1612b06ebda0SMatthew Dillon if (rt == NULL) {
1613b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ALERT(
1614b06ebda0SMatthew Dillon "%s: Could not find out source bdaddr for L2CA message\n", __func__);
1615b06ebda0SMatthew Dillon goto drop;
1616b06ebda0SMatthew Dillon }
1617b06ebda0SMatthew Dillon
1618b06ebda0SMatthew Dillon switch (msg->header.cmd) {
1619b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CON: /* L2CA_Connect response */
1620b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_con_req_rsp(msg, rt);
1621b06ebda0SMatthew Dillon break;
1622b06ebda0SMatthew Dillon
1623b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CON_RSP: /* L2CA_ConnectRsp response */
1624b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(msg, rt);
1625b06ebda0SMatthew Dillon break;
1626b06ebda0SMatthew Dillon
1627b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CON_IND: /* L2CA_Connect indicator */
1628b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_con_ind(msg, rt);
1629b06ebda0SMatthew Dillon break;
1630b06ebda0SMatthew Dillon
1631b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CFG: /* L2CA_Config response */
1632b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(msg, rt);
1633b06ebda0SMatthew Dillon break;
1634b06ebda0SMatthew Dillon
1635b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CFG_RSP: /* L2CA_ConfigRsp response */
1636b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(msg, rt);
1637b06ebda0SMatthew Dillon break;
1638b06ebda0SMatthew Dillon
1639b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CFG_IND: /* L2CA_Config indicator */
1640b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_cfg_ind(msg, rt);
1641b06ebda0SMatthew Dillon break;
1642b06ebda0SMatthew Dillon
1643b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_DISCON: /* L2CA_Disconnect response */
1644b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_discon_rsp(msg, rt);
1645b06ebda0SMatthew Dillon break;
1646b06ebda0SMatthew Dillon
1647b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_DISCON_IND: /* L2CA_Disconnect indicator */
1648b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_discon_ind(msg, rt);
1649b06ebda0SMatthew Dillon break;
1650b06ebda0SMatthew Dillon
1651b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_WRITE: /* L2CA_Write response */
1652b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_l2ca_write_rsp(msg, rt);
1653b06ebda0SMatthew Dillon break;
1654b06ebda0SMatthew Dillon
1655b06ebda0SMatthew Dillon /* XXX FIXME add other L2CA messages */
1656b06ebda0SMatthew Dillon
1657b06ebda0SMatthew Dillon default:
1658b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_WARN(
1659b06ebda0SMatthew Dillon "%s: Unknown L2CA message, cmd=%d\n", __func__, msg->header.cmd);
1660b06ebda0SMatthew Dillon break;
1661b06ebda0SMatthew Dillon }
1662b06ebda0SMatthew Dillon drop:
1663b06ebda0SMatthew Dillon NG_FREE_MSG(msg);
1664b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_l2ca_msg_input */
1665b06ebda0SMatthew Dillon
1666b06ebda0SMatthew Dillon /*
1667b06ebda0SMatthew Dillon * L2CAP sockets input routine
1668b06ebda0SMatthew Dillon */
1669b06ebda0SMatthew Dillon
1670b06ebda0SMatthew Dillon static void
ng_btsocket_l2cap_input(void * context,int pending)1671b06ebda0SMatthew Dillon ng_btsocket_l2cap_input(void *context, int pending)
1672b06ebda0SMatthew Dillon {
1673b06ebda0SMatthew Dillon item_p item = NULL;
1674b06ebda0SMatthew Dillon hook_p hook = NULL;
1675b06ebda0SMatthew Dillon
1676b06ebda0SMatthew Dillon for (;;) {
1677e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_queue_lock, LK_EXCLUSIVE);
1678b06ebda0SMatthew Dillon NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_queue, item);
1679e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_queue_lock, LK_RELEASE);
1680b06ebda0SMatthew Dillon
1681b06ebda0SMatthew Dillon if (item == NULL)
1682b06ebda0SMatthew Dillon break;
1683b06ebda0SMatthew Dillon
1684b06ebda0SMatthew Dillon NGI_GET_HOOK(item, hook);
1685b06ebda0SMatthew Dillon if (hook != NULL && NG_HOOK_NOT_VALID(hook))
1686b06ebda0SMatthew Dillon goto drop;
1687b06ebda0SMatthew Dillon
1688b06ebda0SMatthew Dillon switch(item->el_flags & NGQF_TYPE) {
1689b06ebda0SMatthew Dillon case NGQF_DATA: {
1690b06ebda0SMatthew Dillon struct mbuf *m = NULL;
1691b06ebda0SMatthew Dillon
1692b06ebda0SMatthew Dillon NGI_GET_M(item, m);
1693b06ebda0SMatthew Dillon ng_btsocket_l2cap_data_input(m, hook);
1694b06ebda0SMatthew Dillon } break;
1695b06ebda0SMatthew Dillon
1696b06ebda0SMatthew Dillon case NGQF_MESG: {
1697b06ebda0SMatthew Dillon struct ng_mesg *msg = NULL;
1698b06ebda0SMatthew Dillon
1699b06ebda0SMatthew Dillon NGI_GET_MSG(item, msg);
1700b06ebda0SMatthew Dillon
1701b06ebda0SMatthew Dillon switch (msg->header.cmd) {
1702b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CON:
1703b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CON_RSP:
1704b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CON_IND:
1705b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CFG:
1706b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CFG_RSP:
1707b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_CFG_IND:
1708b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_DISCON:
1709b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_DISCON_IND:
1710b06ebda0SMatthew Dillon case NGM_L2CAP_L2CA_WRITE:
1711b06ebda0SMatthew Dillon /* XXX FIXME add other L2CA messages */
1712b06ebda0SMatthew Dillon ng_btsocket_l2cap_l2ca_msg_input(msg, hook);
1713b06ebda0SMatthew Dillon break;
1714b06ebda0SMatthew Dillon
1715b06ebda0SMatthew Dillon default:
1716b06ebda0SMatthew Dillon ng_btsocket_l2cap_default_msg_input(msg, hook);
1717b06ebda0SMatthew Dillon break;
1718b06ebda0SMatthew Dillon }
1719b06ebda0SMatthew Dillon } break;
1720b06ebda0SMatthew Dillon
1721b06ebda0SMatthew Dillon default:
1722b06ebda0SMatthew Dillon KASSERT(0,
1723b06ebda0SMatthew Dillon ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
1724b06ebda0SMatthew Dillon break;
1725b06ebda0SMatthew Dillon }
1726b06ebda0SMatthew Dillon drop:
1727b06ebda0SMatthew Dillon if (hook != NULL)
1728b06ebda0SMatthew Dillon NG_HOOK_UNREF(hook);
1729b06ebda0SMatthew Dillon
1730b06ebda0SMatthew Dillon NG_FREE_ITEM(item);
1731e85b99abSSascha Wildner ng_unref_item(item, 0);
1732b06ebda0SMatthew Dillon }
1733b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_input */
1734b06ebda0SMatthew Dillon
1735b06ebda0SMatthew Dillon /*
1736b06ebda0SMatthew Dillon * Route cleanup task. Gets scheduled when hook is disconnected. Here we
1737b06ebda0SMatthew Dillon * will find all sockets that use "invalid" hook and disconnect them.
1738b06ebda0SMatthew Dillon */
1739b06ebda0SMatthew Dillon
1740b06ebda0SMatthew Dillon static void
ng_btsocket_l2cap_rtclean(void * context,int pending)1741b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtclean(void *context, int pending)
1742b06ebda0SMatthew Dillon {
1743b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb = NULL, pcb_next = NULL;
1744b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt = NULL;
1745b06ebda0SMatthew Dillon
1746e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_rt_lock, LK_EXCLUSIVE);
1747e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
1748b06ebda0SMatthew Dillon
1749b06ebda0SMatthew Dillon /*
1750b06ebda0SMatthew Dillon * First disconnect all sockets that use "invalid" hook
1751b06ebda0SMatthew Dillon */
1752b06ebda0SMatthew Dillon
1753b06ebda0SMatthew Dillon for (pcb = LIST_FIRST(&ng_btsocket_l2cap_sockets); pcb != NULL; ) {
1754e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1755b06ebda0SMatthew Dillon pcb_next = LIST_NEXT(pcb, next);
1756b06ebda0SMatthew Dillon
1757b06ebda0SMatthew Dillon if (pcb->rt != NULL &&
1758b06ebda0SMatthew Dillon pcb->rt->hook != NULL && NG_HOOK_NOT_VALID(pcb->rt->hook)) {
1759b06ebda0SMatthew Dillon if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
1760b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
1761b06ebda0SMatthew Dillon
1762b06ebda0SMatthew Dillon pcb->so->so_error = ENETDOWN;
1763b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1764b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
1765b06ebda0SMatthew Dillon
1766b06ebda0SMatthew Dillon pcb->token = 0;
1767b06ebda0SMatthew Dillon pcb->cid = 0;
1768b06ebda0SMatthew Dillon pcb->rt = NULL;
1769b06ebda0SMatthew Dillon }
1770b06ebda0SMatthew Dillon
1771e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
1772b06ebda0SMatthew Dillon pcb = pcb_next;
1773b06ebda0SMatthew Dillon }
1774b06ebda0SMatthew Dillon
1775b06ebda0SMatthew Dillon /*
1776b06ebda0SMatthew Dillon * Now cleanup routing table
1777b06ebda0SMatthew Dillon */
1778b06ebda0SMatthew Dillon
1779b06ebda0SMatthew Dillon for (rt = LIST_FIRST(&ng_btsocket_l2cap_rt); rt != NULL; ) {
1780b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_p rt_next = LIST_NEXT(rt, next);
1781b06ebda0SMatthew Dillon
1782b06ebda0SMatthew Dillon if (rt->hook != NULL && NG_HOOK_NOT_VALID(rt->hook)) {
1783b06ebda0SMatthew Dillon LIST_REMOVE(rt, next);
1784b06ebda0SMatthew Dillon
1785b06ebda0SMatthew Dillon NG_HOOK_SET_PRIVATE(rt->hook, NULL);
1786b06ebda0SMatthew Dillon NG_HOOK_UNREF(rt->hook); /* Remove extra reference */
1787b06ebda0SMatthew Dillon
1788b06ebda0SMatthew Dillon bzero(rt, sizeof(*rt));
1789fc025606SSascha Wildner kfree(rt, M_NETGRAPH_BTSOCKET_L2CAP);
1790b06ebda0SMatthew Dillon }
1791b06ebda0SMatthew Dillon
1792b06ebda0SMatthew Dillon rt = rt_next;
1793b06ebda0SMatthew Dillon }
1794b06ebda0SMatthew Dillon
1795e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
1796e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_rt_lock, LK_RELEASE);
1797b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_rtclean */
1798b06ebda0SMatthew Dillon
1799b06ebda0SMatthew Dillon /*
1800b06ebda0SMatthew Dillon * Initialize everything
1801b06ebda0SMatthew Dillon */
1802b06ebda0SMatthew Dillon
1803b06ebda0SMatthew Dillon void
ng_btsocket_l2cap_init(void)1804b06ebda0SMatthew Dillon ng_btsocket_l2cap_init(void)
1805b06ebda0SMatthew Dillon {
1806b06ebda0SMatthew Dillon int error = 0;
1807b06ebda0SMatthew Dillon
1808b06ebda0SMatthew Dillon ng_btsocket_l2cap_node = NULL;
1809b06ebda0SMatthew Dillon ng_btsocket_l2cap_debug_level = NG_BTSOCKET_WARN_LEVEL;
1810b06ebda0SMatthew Dillon
1811b06ebda0SMatthew Dillon /* Register Netgraph node type */
1812b06ebda0SMatthew Dillon error = ng_newtype(&typestruct);
1813b06ebda0SMatthew Dillon if (error != 0) {
1814b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ALERT(
1815b06ebda0SMatthew Dillon "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
1816b06ebda0SMatthew Dillon
1817b06ebda0SMatthew Dillon return;
1818b06ebda0SMatthew Dillon }
1819b06ebda0SMatthew Dillon
1820b06ebda0SMatthew Dillon /* Create Netgrapg node */
1821b06ebda0SMatthew Dillon error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_node);
1822b06ebda0SMatthew Dillon if (error != 0) {
1823b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ALERT(
1824b06ebda0SMatthew Dillon "%s: Could not create Netgraph node, error=%d\n", __func__, error);
1825b06ebda0SMatthew Dillon
1826b06ebda0SMatthew Dillon ng_btsocket_l2cap_node = NULL;
1827b06ebda0SMatthew Dillon
1828b06ebda0SMatthew Dillon return;
1829b06ebda0SMatthew Dillon }
1830b06ebda0SMatthew Dillon
1831b06ebda0SMatthew Dillon error = ng_name_node(ng_btsocket_l2cap_node,
1832b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_NODE_TYPE);
1833b06ebda0SMatthew Dillon if (error != 0) {
1834b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ALERT(
1835b06ebda0SMatthew Dillon "%s: Could not name Netgraph node, error=%d\n", __func__, error);
1836b06ebda0SMatthew Dillon
1837b06ebda0SMatthew Dillon NG_NODE_UNREF(ng_btsocket_l2cap_node);
1838b06ebda0SMatthew Dillon ng_btsocket_l2cap_node = NULL;
1839b06ebda0SMatthew Dillon
1840b06ebda0SMatthew Dillon return;
1841b06ebda0SMatthew Dillon }
1842b06ebda0SMatthew Dillon
1843b06ebda0SMatthew Dillon /* Create input queue */
1844b06ebda0SMatthew Dillon NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_queue, ifqmaxlen);
1845e85b99abSSascha Wildner lockinit(&ng_btsocket_l2cap_queue_lock,
1846e85b99abSSascha Wildner "btsocks_l2cap_queue_lock", 0, 0);
1847b06ebda0SMatthew Dillon TASK_INIT(&ng_btsocket_l2cap_queue_task, 0,
1848b06ebda0SMatthew Dillon ng_btsocket_l2cap_input, NULL);
1849b06ebda0SMatthew Dillon
1850b06ebda0SMatthew Dillon /* Create list of sockets */
1851b06ebda0SMatthew Dillon LIST_INIT(&ng_btsocket_l2cap_sockets);
1852e85b99abSSascha Wildner lockinit(&ng_btsocket_l2cap_sockets_lock,
1853e85b99abSSascha Wildner "btsocks_l2cap_sockets_lock", 0, 0);
1854b06ebda0SMatthew Dillon
1855b06ebda0SMatthew Dillon /* Routing table */
1856b06ebda0SMatthew Dillon LIST_INIT(&ng_btsocket_l2cap_rt);
1857e85b99abSSascha Wildner lockinit(&ng_btsocket_l2cap_rt_lock,
1858e85b99abSSascha Wildner "btsocks_l2cap_rt_lock", 0, 0);
1859b06ebda0SMatthew Dillon TASK_INIT(&ng_btsocket_l2cap_rt_task, 0,
1860b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtclean, NULL);
1861b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_init */
1862b06ebda0SMatthew Dillon
1863b06ebda0SMatthew Dillon /*
1864b06ebda0SMatthew Dillon * Abort connection on socket
1865b06ebda0SMatthew Dillon */
1866b06ebda0SMatthew Dillon
1867b06ebda0SMatthew Dillon void
ng_btsocket_l2cap_abort(netmsg_t msg)1868e85b99abSSascha Wildner ng_btsocket_l2cap_abort(netmsg_t msg)
1869b06ebda0SMatthew Dillon {
1870e85b99abSSascha Wildner struct socket *so = msg->abort.base.nm_so;
1871e85b99abSSascha Wildner
1872b06ebda0SMatthew Dillon so->so_error = ECONNABORTED;
1873b06ebda0SMatthew Dillon
1874e85b99abSSascha Wildner ng_btsocket_l2cap_disconnect(msg);
1875b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_abort */
1876b06ebda0SMatthew Dillon
1877e85b99abSSascha Wildner #if 0 /* XXX */
1878b06ebda0SMatthew Dillon void
1879b06ebda0SMatthew Dillon ng_btsocket_l2cap_close(struct socket *so)
1880b06ebda0SMatthew Dillon {
1881b06ebda0SMatthew Dillon
1882b06ebda0SMatthew Dillon (void)ng_btsocket_l2cap_disconnect(so);
1883b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_close */
1884e85b99abSSascha Wildner #endif
1885b06ebda0SMatthew Dillon
1886b06ebda0SMatthew Dillon /*
1887b06ebda0SMatthew Dillon * Accept connection on socket. Nothing to do here, socket must be connected
1888b06ebda0SMatthew Dillon * and ready, so just return peer address and be done with it.
1889b06ebda0SMatthew Dillon */
1890b06ebda0SMatthew Dillon
1891e85b99abSSascha Wildner void
ng_btsocket_l2cap_accept(netmsg_t msg)1892e85b99abSSascha Wildner ng_btsocket_l2cap_accept(netmsg_t msg)
1893b06ebda0SMatthew Dillon {
1894e85b99abSSascha Wildner int error = 0;
1895b06ebda0SMatthew Dillon
1896e85b99abSSascha Wildner if (ng_btsocket_l2cap_node == NULL) {
1897e85b99abSSascha Wildner error = EINVAL;
1898e85b99abSSascha Wildner goto out;
1899e85b99abSSascha Wildner }
1900e85b99abSSascha Wildner
1901e85b99abSSascha Wildner ng_btsocket_l2cap_peeraddr(msg);
1902e85b99abSSascha Wildner return;
1903e85b99abSSascha Wildner
1904e85b99abSSascha Wildner out:
1905e85b99abSSascha Wildner lwkt_replymsg(&msg->accept.base.lmsg, error);
1906b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_accept */
1907b06ebda0SMatthew Dillon
1908b06ebda0SMatthew Dillon /*
1909b06ebda0SMatthew Dillon * Create and attach new socket
1910b06ebda0SMatthew Dillon */
1911b06ebda0SMatthew Dillon
1912e85b99abSSascha Wildner void
ng_btsocket_l2cap_attach(netmsg_t msg)1913e85b99abSSascha Wildner ng_btsocket_l2cap_attach(netmsg_t msg)
1914b06ebda0SMatthew Dillon {
1915e85b99abSSascha Wildner struct socket *so = msg->attach.base.nm_so;
1916e85b99abSSascha Wildner int proto = msg->attach.nm_proto;
1917b06ebda0SMatthew Dillon static u_int32_t token = 0;
1918b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
1919e85b99abSSascha Wildner int error = 0;
1920b06ebda0SMatthew Dillon
1921b06ebda0SMatthew Dillon /* Check socket and protocol */
1922e85b99abSSascha Wildner if (ng_btsocket_l2cap_node == NULL) {
1923e85b99abSSascha Wildner error = EPROTONOSUPPORT;
1924e85b99abSSascha Wildner goto out;
1925e85b99abSSascha Wildner }
1926e85b99abSSascha Wildner if (so->so_type != SOCK_SEQPACKET) {
1927e85b99abSSascha Wildner error = ESOCKTNOSUPPORT;
1928e85b99abSSascha Wildner goto out;
1929e85b99abSSascha Wildner }
1930b06ebda0SMatthew Dillon
1931b06ebda0SMatthew Dillon #if 0 /* XXX sonewconn() calls "pru_attach" with proto == 0 */
1932b06ebda0SMatthew Dillon if (proto != 0)
1933e85b99abSSascha Wildner if (proto != BLUETOOTH_PROTO_L2CAP) {
1934e85b99abSSascha Wildner error = EPROTONOSUPPORT;
1935e85b99abSSascha Wildner goto out;
1936e85b99abSSascha Wildner }
1937b06ebda0SMatthew Dillon #endif /* XXX */
1938b06ebda0SMatthew Dillon
1939e85b99abSSascha Wildner if (pcb != NULL) {
1940e85b99abSSascha Wildner error = EISCONN;
1941e85b99abSSascha Wildner goto out;
1942e85b99abSSascha Wildner }
1943b06ebda0SMatthew Dillon
1944b06ebda0SMatthew Dillon /* Reserve send and receive space if it is not reserved yet */
1945e85b99abSSascha Wildner if ((so->so_snd.ssb_hiwat == 0) || (so->so_rcv.ssb_hiwat == 0)) {
1946b06ebda0SMatthew Dillon error = soreserve(so, NG_BTSOCKET_L2CAP_SENDSPACE,
1947e85b99abSSascha Wildner NG_BTSOCKET_L2CAP_RECVSPACE, NULL);
1948b06ebda0SMatthew Dillon if (error != 0)
1949e85b99abSSascha Wildner goto out;
1950b06ebda0SMatthew Dillon }
1951b06ebda0SMatthew Dillon
1952b06ebda0SMatthew Dillon /* Allocate the PCB */
1953fc025606SSascha Wildner pcb = kmalloc(sizeof(*pcb), M_NETGRAPH_BTSOCKET_L2CAP,
1954fc025606SSascha Wildner M_WAITOK | M_NULLOK | M_ZERO);
1955e85b99abSSascha Wildner if (pcb == NULL) {
1956e85b99abSSascha Wildner error = ENOMEM;
1957e85b99abSSascha Wildner goto out;
1958e85b99abSSascha Wildner }
1959b06ebda0SMatthew Dillon
1960b06ebda0SMatthew Dillon /* Link the PCB and the socket */
1961b06ebda0SMatthew Dillon so->so_pcb = (caddr_t) pcb;
1962b06ebda0SMatthew Dillon pcb->so = so;
1963b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1964b06ebda0SMatthew Dillon
1965b06ebda0SMatthew Dillon /* Initialize PCB */
1966b06ebda0SMatthew Dillon pcb->imtu = pcb->omtu = NG_L2CAP_MTU_DEFAULT;
1967b06ebda0SMatthew Dillon
1968b06ebda0SMatthew Dillon /* Default flow */
1969b06ebda0SMatthew Dillon pcb->iflow.flags = 0x0;
1970b06ebda0SMatthew Dillon pcb->iflow.service_type = NG_HCI_SERVICE_TYPE_BEST_EFFORT;
1971b06ebda0SMatthew Dillon pcb->iflow.token_rate = 0xffffffff; /* maximum */
1972b06ebda0SMatthew Dillon pcb->iflow.token_bucket_size = 0xffffffff; /* maximum */
1973b06ebda0SMatthew Dillon pcb->iflow.peak_bandwidth = 0x00000000; /* maximum */
1974b06ebda0SMatthew Dillon pcb->iflow.latency = 0xffffffff; /* don't care */
1975b06ebda0SMatthew Dillon pcb->iflow.delay_variation = 0xffffffff; /* don't care */
1976b06ebda0SMatthew Dillon
1977b06ebda0SMatthew Dillon bcopy(&pcb->iflow, &pcb->oflow, sizeof(pcb->oflow));
1978b06ebda0SMatthew Dillon
1979b06ebda0SMatthew Dillon pcb->flush_timo = NG_L2CAP_FLUSH_TIMO_DEFAULT;
1980b06ebda0SMatthew Dillon pcb->link_timo = NG_L2CAP_LINK_TIMO_DEFAULT;
1981b06ebda0SMatthew Dillon
1982e85b99abSSascha Wildner callout_init_mp(&pcb->timo);
1983b06ebda0SMatthew Dillon
1984b06ebda0SMatthew Dillon /*
1985b06ebda0SMatthew Dillon * XXX Mark PCB mutex as DUPOK to prevent "duplicated lock of
1986b06ebda0SMatthew Dillon * the same type" message. When accepting new L2CAP connection
1987b06ebda0SMatthew Dillon * ng_btsocket_l2cap_process_l2ca_con_ind() holds both PCB mutexes
1988b06ebda0SMatthew Dillon * for "old" (accepting) PCB and "new" (created) PCB.
1989b06ebda0SMatthew Dillon */
1990b06ebda0SMatthew Dillon
1991e85b99abSSascha Wildner lockinit(&pcb->pcb_lock, "btsocks_l2cap_pcb_lock", 0, LK_CANRECURSE);
1992b06ebda0SMatthew Dillon
1993b06ebda0SMatthew Dillon /*
1994b06ebda0SMatthew Dillon * Add the PCB to the list
1995b06ebda0SMatthew Dillon *
1996b06ebda0SMatthew Dillon * XXX FIXME VERY IMPORTANT!
1997b06ebda0SMatthew Dillon *
1998b06ebda0SMatthew Dillon * This is totally FUBAR. We could get here in two cases:
1999b06ebda0SMatthew Dillon *
2000b06ebda0SMatthew Dillon * 1) When user calls socket()
2001b06ebda0SMatthew Dillon * 2) When we need to accept new incomming connection and call
2002b06ebda0SMatthew Dillon * sonewconn()
2003b06ebda0SMatthew Dillon *
2004b06ebda0SMatthew Dillon * In the first case we must acquire ng_btsocket_l2cap_sockets_mtx.
2005b06ebda0SMatthew Dillon * In the second case we hold ng_btsocket_l2cap_sockets_mtx already.
2006b06ebda0SMatthew Dillon * So we now need to distinguish between these cases. From reading
2007b06ebda0SMatthew Dillon * /sys/kern/uipc_socket.c we can find out that sonewconn() calls
2008b06ebda0SMatthew Dillon * pru_attach with proto == 0 and td == NULL. For now use this fact
2009b06ebda0SMatthew Dillon * to figure out if we were called from socket() or from sonewconn().
2010b06ebda0SMatthew Dillon */
2011b06ebda0SMatthew Dillon
2012e85b99abSSascha Wildner if (proto != 0)
2013e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
2014b06ebda0SMatthew Dillon else
2015e85b99abSSascha Wildner KKASSERT(lockowned(&ng_btsocket_l2cap_sockets_lock) != 0);
2016b06ebda0SMatthew Dillon
2017b06ebda0SMatthew Dillon /* Set PCB token. Use ng_btsocket_l2cap_sockets_mtx for protection */
2018b06ebda0SMatthew Dillon if (++ token == 0)
2019b06ebda0SMatthew Dillon token ++;
2020b06ebda0SMatthew Dillon
2021b06ebda0SMatthew Dillon pcb->token = token;
2022b06ebda0SMatthew Dillon
2023b06ebda0SMatthew Dillon LIST_INSERT_HEAD(&ng_btsocket_l2cap_sockets, pcb, next);
2024b06ebda0SMatthew Dillon
2025e85b99abSSascha Wildner if (proto != 0)
2026e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
2027b06ebda0SMatthew Dillon
2028e85b99abSSascha Wildner out:
2029e85b99abSSascha Wildner lwkt_replymsg(&msg->attach.base.lmsg, error);
2030b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_attach */
2031b06ebda0SMatthew Dillon
2032b06ebda0SMatthew Dillon /*
2033b06ebda0SMatthew Dillon * Bind socket
2034b06ebda0SMatthew Dillon */
2035b06ebda0SMatthew Dillon
2036e85b99abSSascha Wildner void
ng_btsocket_l2cap_bind(netmsg_t msg)2037e85b99abSSascha Wildner ng_btsocket_l2cap_bind(netmsg_t msg)
2038b06ebda0SMatthew Dillon {
2039e85b99abSSascha Wildner struct socket *so = msg->bind.base.nm_so;
2040e85b99abSSascha Wildner struct sockaddr *nam = msg->bind.nm_nam;
2041b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = NULL;
2042b06ebda0SMatthew Dillon struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam;
2043b06ebda0SMatthew Dillon int psm, error = 0;
2044b06ebda0SMatthew Dillon
2045e85b99abSSascha Wildner if (ng_btsocket_l2cap_node == NULL) {
2046e85b99abSSascha Wildner error = EINVAL;
2047e85b99abSSascha Wildner goto out;
2048e85b99abSSascha Wildner }
2049b06ebda0SMatthew Dillon
2050b06ebda0SMatthew Dillon /* Verify address */
2051e85b99abSSascha Wildner if (sa == NULL) {
2052e85b99abSSascha Wildner error = EINVAL;
2053e85b99abSSascha Wildner goto out;
2054e85b99abSSascha Wildner }
2055e85b99abSSascha Wildner if (sa->l2cap_family != AF_BLUETOOTH) {
2056e85b99abSSascha Wildner error = EAFNOSUPPORT;
2057e85b99abSSascha Wildner goto out;
2058e85b99abSSascha Wildner }
2059e85b99abSSascha Wildner if (sa->l2cap_len != sizeof(*sa)) {
2060e85b99abSSascha Wildner error = EINVAL;
2061e85b99abSSascha Wildner goto out;
2062e85b99abSSascha Wildner }
2063b06ebda0SMatthew Dillon
2064b06ebda0SMatthew Dillon psm = le16toh(sa->l2cap_psm);
2065b06ebda0SMatthew Dillon
2066b06ebda0SMatthew Dillon /*
2067b06ebda0SMatthew Dillon * Check if other socket has this address already (look for exact
2068b06ebda0SMatthew Dillon * match PSM and bdaddr) and assign socket address if it's available.
2069b06ebda0SMatthew Dillon *
2070b06ebda0SMatthew Dillon * Note: socket can be bound to ANY PSM (zero) thus allowing several
2071b06ebda0SMatthew Dillon * channels with the same PSM between the same pair of BD_ADDR'es.
2072b06ebda0SMatthew Dillon */
2073b06ebda0SMatthew Dillon
2074e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
2075b06ebda0SMatthew Dillon
2076b06ebda0SMatthew Dillon LIST_FOREACH(pcb, &ng_btsocket_l2cap_sockets, next)
2077b06ebda0SMatthew Dillon if (psm != 0 && psm == pcb->psm &&
2078b06ebda0SMatthew Dillon bcmp(&pcb->src, &sa->l2cap_bdaddr, sizeof(bdaddr_t)) == 0)
2079b06ebda0SMatthew Dillon break;
2080b06ebda0SMatthew Dillon
2081b06ebda0SMatthew Dillon if (pcb == NULL) {
2082b06ebda0SMatthew Dillon /* Set socket address */
2083b06ebda0SMatthew Dillon pcb = so2l2cap_pcb(so);
2084b06ebda0SMatthew Dillon if (pcb != NULL) {
2085b06ebda0SMatthew Dillon bcopy(&sa->l2cap_bdaddr, &pcb->src, sizeof(pcb->src));
2086b06ebda0SMatthew Dillon pcb->psm = psm;
2087b06ebda0SMatthew Dillon } else
2088b06ebda0SMatthew Dillon error = EINVAL;
2089b06ebda0SMatthew Dillon } else
2090b06ebda0SMatthew Dillon error = EADDRINUSE;
2091b06ebda0SMatthew Dillon
2092e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
2093b06ebda0SMatthew Dillon
2094e85b99abSSascha Wildner out:
2095e85b99abSSascha Wildner lwkt_replymsg(&msg->bind.base.lmsg, error);
2096b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_bind */
2097b06ebda0SMatthew Dillon
2098b06ebda0SMatthew Dillon /*
2099b06ebda0SMatthew Dillon * Connect socket
2100b06ebda0SMatthew Dillon */
2101b06ebda0SMatthew Dillon
2102e85b99abSSascha Wildner void
ng_btsocket_l2cap_connect(netmsg_t msg)2103e85b99abSSascha Wildner ng_btsocket_l2cap_connect(netmsg_t msg)
2104b06ebda0SMatthew Dillon {
2105e85b99abSSascha Wildner struct socket *so = msg->connect.base.nm_so;
2106e85b99abSSascha Wildner struct sockaddr *nam = msg->connect.nm_nam;
2107b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = so2l2cap_pcb(so);
2108b06ebda0SMatthew Dillon struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam;
2109b06ebda0SMatthew Dillon ng_btsocket_l2cap_rtentry_t *rt = NULL;
2110b06ebda0SMatthew Dillon int have_src, error = 0;
2111b06ebda0SMatthew Dillon
2112b06ebda0SMatthew Dillon /* Check socket */
2113e85b99abSSascha Wildner if (pcb == NULL) {
2114e85b99abSSascha Wildner error = EINVAL;
2115e85b99abSSascha Wildner goto out;
2116e85b99abSSascha Wildner }
2117e85b99abSSascha Wildner if (ng_btsocket_l2cap_node == NULL) {
2118e85b99abSSascha Wildner error = EINVAL;
2119e85b99abSSascha Wildner goto out;
2120e85b99abSSascha Wildner }
2121e85b99abSSascha Wildner if (pcb->state == NG_BTSOCKET_L2CAP_CONNECTING) {
2122e85b99abSSascha Wildner error = EINPROGRESS;
2123e85b99abSSascha Wildner goto out;
2124e85b99abSSascha Wildner }
2125b06ebda0SMatthew Dillon
2126b06ebda0SMatthew Dillon /* Verify address */
2127e85b99abSSascha Wildner if (sa == NULL) {
2128e85b99abSSascha Wildner error = EINVAL;
2129e85b99abSSascha Wildner goto out;
2130e85b99abSSascha Wildner }
2131e85b99abSSascha Wildner if (sa->l2cap_family != AF_BLUETOOTH) {
2132e85b99abSSascha Wildner error = EAFNOSUPPORT;
2133e85b99abSSascha Wildner goto out;
2134e85b99abSSascha Wildner }
2135e85b99abSSascha Wildner if (sa->l2cap_len != sizeof(*sa)) {
2136e85b99abSSascha Wildner error = EINVAL;
2137e85b99abSSascha Wildner goto out;
2138e85b99abSSascha Wildner }
2139b06ebda0SMatthew Dillon if (sa->l2cap_psm == 0 ||
2140e85b99abSSascha Wildner bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0) {
2141e85b99abSSascha Wildner error = EDESTADDRREQ;
2142e85b99abSSascha Wildner goto out;
2143e85b99abSSascha Wildner }
2144e85b99abSSascha Wildner if (pcb->psm != 0 && pcb->psm != le16toh(sa->l2cap_psm)) {
2145e85b99abSSascha Wildner error = EINVAL;
2146e85b99abSSascha Wildner goto out;
2147e85b99abSSascha Wildner }
2148b06ebda0SMatthew Dillon
2149b06ebda0SMatthew Dillon /*
2150b06ebda0SMatthew Dillon * Routing. Socket should be bound to some source address. The source
2151b06ebda0SMatthew Dillon * address can be ANY. Destination address must be set and it must not
2152b06ebda0SMatthew Dillon * be ANY. If source address is ANY then find first rtentry that has
2153b06ebda0SMatthew Dillon * src != dst.
2154b06ebda0SMatthew Dillon */
2155b06ebda0SMatthew Dillon
2156e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_rt_lock, LK_EXCLUSIVE);
2157e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
2158e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
2159b06ebda0SMatthew Dillon
2160b06ebda0SMatthew Dillon /* Send destination address and PSM */
2161b06ebda0SMatthew Dillon bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst));
2162b06ebda0SMatthew Dillon pcb->psm = le16toh(sa->l2cap_psm);
2163b06ebda0SMatthew Dillon
2164b06ebda0SMatthew Dillon pcb->rt = NULL;
2165b06ebda0SMatthew Dillon have_src = bcmp(&pcb->src, NG_HCI_BDADDR_ANY, sizeof(pcb->src));
2166b06ebda0SMatthew Dillon
2167b06ebda0SMatthew Dillon LIST_FOREACH(rt, &ng_btsocket_l2cap_rt, next) {
2168b06ebda0SMatthew Dillon if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
2169b06ebda0SMatthew Dillon continue;
2170b06ebda0SMatthew Dillon
2171b06ebda0SMatthew Dillon /* Match src and dst */
2172b06ebda0SMatthew Dillon if (have_src) {
2173b06ebda0SMatthew Dillon if (bcmp(&pcb->src, &rt->src, sizeof(rt->src)) == 0)
2174b06ebda0SMatthew Dillon break;
2175b06ebda0SMatthew Dillon } else {
2176b06ebda0SMatthew Dillon if (bcmp(&pcb->dst, &rt->src, sizeof(rt->src)) != 0)
2177b06ebda0SMatthew Dillon break;
2178b06ebda0SMatthew Dillon }
2179b06ebda0SMatthew Dillon }
2180b06ebda0SMatthew Dillon
2181b06ebda0SMatthew Dillon if (rt != NULL) {
2182b06ebda0SMatthew Dillon pcb->rt = rt;
2183b06ebda0SMatthew Dillon
2184b06ebda0SMatthew Dillon if (!have_src)
2185b06ebda0SMatthew Dillon bcopy(&rt->src, &pcb->src, sizeof(pcb->src));
2186b06ebda0SMatthew Dillon } else
2187b06ebda0SMatthew Dillon error = EHOSTUNREACH;
2188b06ebda0SMatthew Dillon
2189b06ebda0SMatthew Dillon /*
2190b06ebda0SMatthew Dillon * Send L2CA_Connect request
2191b06ebda0SMatthew Dillon */
2192b06ebda0SMatthew Dillon
2193b06ebda0SMatthew Dillon if (error == 0) {
2194b06ebda0SMatthew Dillon error = ng_btsocket_l2cap_send_l2ca_con_req(pcb);
2195b06ebda0SMatthew Dillon if (error == 0) {
2196b06ebda0SMatthew Dillon pcb->flags |= NG_BTSOCKET_L2CAP_CLIENT;
2197b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CONNECTING;
2198b06ebda0SMatthew Dillon soisconnecting(pcb->so);
2199b06ebda0SMatthew Dillon
2200b06ebda0SMatthew Dillon ng_btsocket_l2cap_timeout(pcb);
2201b06ebda0SMatthew Dillon }
2202b06ebda0SMatthew Dillon }
2203b06ebda0SMatthew Dillon
2204e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
2205e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
2206e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_rt_lock, LK_RELEASE);
2207b06ebda0SMatthew Dillon
2208e85b99abSSascha Wildner out:
2209e85b99abSSascha Wildner lwkt_replymsg(&msg->connect.base.lmsg, error);
2210b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_connect */
2211b06ebda0SMatthew Dillon
2212b06ebda0SMatthew Dillon /*
2213b06ebda0SMatthew Dillon * Process ioctl's calls on socket
2214b06ebda0SMatthew Dillon */
2215b06ebda0SMatthew Dillon
2216e85b99abSSascha Wildner void
ng_btsocket_l2cap_control(netmsg_t msg)2217e85b99abSSascha Wildner ng_btsocket_l2cap_control(netmsg_t msg)
2218b06ebda0SMatthew Dillon {
2219e85b99abSSascha Wildner lwkt_replymsg(&msg->control.base.lmsg, EINVAL);
2220b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_control */
2221b06ebda0SMatthew Dillon
2222b06ebda0SMatthew Dillon /*
2223b06ebda0SMatthew Dillon * Process getsockopt/setsockopt system calls
2224b06ebda0SMatthew Dillon */
2225b06ebda0SMatthew Dillon
2226e85b99abSSascha Wildner void
ng_btsocket_l2cap_ctloutput(netmsg_t msg)2227e85b99abSSascha Wildner ng_btsocket_l2cap_ctloutput(netmsg_t msg)
2228b06ebda0SMatthew Dillon {
2229e85b99abSSascha Wildner struct socket *so = msg->ctloutput.base.nm_so;
2230e85b99abSSascha Wildner struct sockopt *sopt = msg->ctloutput.nm_sopt;
2231b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2232b06ebda0SMatthew Dillon int error = 0;
2233b06ebda0SMatthew Dillon ng_l2cap_cfg_opt_val_t v;
2234b06ebda0SMatthew Dillon
2235e85b99abSSascha Wildner if (pcb == NULL) {
2236e85b99abSSascha Wildner error = EINVAL;
2237e85b99abSSascha Wildner goto out;
2238e85b99abSSascha Wildner }
2239e85b99abSSascha Wildner if (ng_btsocket_l2cap_node == NULL) {
2240e85b99abSSascha Wildner error = EINVAL;
2241e85b99abSSascha Wildner goto out;
2242e85b99abSSascha Wildner }
2243b06ebda0SMatthew Dillon
2244b06ebda0SMatthew Dillon if (sopt->sopt_level != SOL_L2CAP)
2245e85b99abSSascha Wildner goto out;
2246b06ebda0SMatthew Dillon
2247e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
2248b06ebda0SMatthew Dillon
2249b06ebda0SMatthew Dillon switch (sopt->sopt_dir) {
2250b06ebda0SMatthew Dillon case SOPT_GET:
2251b06ebda0SMatthew Dillon switch (sopt->sopt_name) {
2252b06ebda0SMatthew Dillon case SO_L2CAP_IMTU: /* get incoming MTU */
2253b06ebda0SMatthew Dillon error = sooptcopyout(sopt, &pcb->imtu,
2254b06ebda0SMatthew Dillon sizeof(pcb->imtu));
2255b06ebda0SMatthew Dillon break;
2256b06ebda0SMatthew Dillon
2257b06ebda0SMatthew Dillon case SO_L2CAP_OMTU: /* get outgoing (peer incoming) MTU */
2258b06ebda0SMatthew Dillon error = sooptcopyout(sopt, &pcb->omtu,
2259b06ebda0SMatthew Dillon sizeof(pcb->omtu));
2260b06ebda0SMatthew Dillon break;
2261b06ebda0SMatthew Dillon
2262b06ebda0SMatthew Dillon case SO_L2CAP_IFLOW: /* get incoming flow spec. */
2263b06ebda0SMatthew Dillon error = sooptcopyout(sopt, &pcb->iflow,
2264b06ebda0SMatthew Dillon sizeof(pcb->iflow));
2265b06ebda0SMatthew Dillon break;
2266b06ebda0SMatthew Dillon
2267b06ebda0SMatthew Dillon case SO_L2CAP_OFLOW: /* get outgoing flow spec. */
2268b06ebda0SMatthew Dillon error = sooptcopyout(sopt, &pcb->oflow,
2269b06ebda0SMatthew Dillon sizeof(pcb->oflow));
2270b06ebda0SMatthew Dillon break;
2271b06ebda0SMatthew Dillon
2272b06ebda0SMatthew Dillon case SO_L2CAP_FLUSH: /* get flush timeout */
2273b06ebda0SMatthew Dillon error = sooptcopyout(sopt, &pcb->flush_timo,
2274b06ebda0SMatthew Dillon sizeof(pcb->flush_timo));
2275b06ebda0SMatthew Dillon break;
2276b06ebda0SMatthew Dillon
2277b06ebda0SMatthew Dillon default:
2278b06ebda0SMatthew Dillon error = ENOPROTOOPT;
2279b06ebda0SMatthew Dillon break;
2280b06ebda0SMatthew Dillon }
2281b06ebda0SMatthew Dillon break;
2282b06ebda0SMatthew Dillon
2283b06ebda0SMatthew Dillon case SOPT_SET:
2284b06ebda0SMatthew Dillon /*
2285b06ebda0SMatthew Dillon * XXX
2286b06ebda0SMatthew Dillon * We do not allow to change these parameters while socket is
2287b06ebda0SMatthew Dillon * connected or we are in the process of creating a connection.
2288b06ebda0SMatthew Dillon * May be this should indicate re-configuration of the open
2289b06ebda0SMatthew Dillon * channel?
2290b06ebda0SMatthew Dillon */
2291b06ebda0SMatthew Dillon
2292b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
2293b06ebda0SMatthew Dillon error = EACCES;
2294b06ebda0SMatthew Dillon break;
2295b06ebda0SMatthew Dillon }
2296b06ebda0SMatthew Dillon
2297b06ebda0SMatthew Dillon switch (sopt->sopt_name) {
2298b06ebda0SMatthew Dillon case SO_L2CAP_IMTU: /* set incoming MTU */
2299b06ebda0SMatthew Dillon error = sooptcopyin(sopt, &v, sizeof(v), sizeof(v.mtu));
2300b06ebda0SMatthew Dillon if (error == 0)
2301b06ebda0SMatthew Dillon pcb->imtu = v.mtu;
2302b06ebda0SMatthew Dillon break;
2303b06ebda0SMatthew Dillon
2304b06ebda0SMatthew Dillon case SO_L2CAP_OFLOW: /* set outgoing flow spec. */
2305b06ebda0SMatthew Dillon error = sooptcopyin(sopt, &v, sizeof(v),sizeof(v.flow));
2306b06ebda0SMatthew Dillon if (error == 0)
2307b06ebda0SMatthew Dillon bcopy(&v.flow, &pcb->oflow, sizeof(pcb->oflow));
2308b06ebda0SMatthew Dillon break;
2309b06ebda0SMatthew Dillon
2310b06ebda0SMatthew Dillon case SO_L2CAP_FLUSH: /* set flush timeout */
2311b06ebda0SMatthew Dillon error = sooptcopyin(sopt, &v, sizeof(v),
2312b06ebda0SMatthew Dillon sizeof(v.flush_timo));
2313b06ebda0SMatthew Dillon if (error == 0)
2314b06ebda0SMatthew Dillon pcb->flush_timo = v.flush_timo;
2315b06ebda0SMatthew Dillon break;
2316b06ebda0SMatthew Dillon
2317b06ebda0SMatthew Dillon default:
2318b06ebda0SMatthew Dillon error = ENOPROTOOPT;
2319b06ebda0SMatthew Dillon break;
2320b06ebda0SMatthew Dillon }
2321b06ebda0SMatthew Dillon break;
2322b06ebda0SMatthew Dillon
2323b06ebda0SMatthew Dillon default:
2324b06ebda0SMatthew Dillon error = EINVAL;
2325b06ebda0SMatthew Dillon break;
2326b06ebda0SMatthew Dillon }
2327b06ebda0SMatthew Dillon
2328e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
2329b06ebda0SMatthew Dillon
2330e85b99abSSascha Wildner out:
2331e85b99abSSascha Wildner lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
2332b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_ctloutput */
2333b06ebda0SMatthew Dillon
2334b06ebda0SMatthew Dillon /*
2335b06ebda0SMatthew Dillon * Detach and destroy socket
2336b06ebda0SMatthew Dillon */
2337b06ebda0SMatthew Dillon
2338b06ebda0SMatthew Dillon void
ng_btsocket_l2cap_detach(netmsg_t msg)2339e85b99abSSascha Wildner ng_btsocket_l2cap_detach(netmsg_t msg)
2340b06ebda0SMatthew Dillon {
2341e85b99abSSascha Wildner struct socket *so = msg->detach.base.nm_so;
2342b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2343e85b99abSSascha Wildner int error = 0;
2344b06ebda0SMatthew Dillon
2345b06ebda0SMatthew Dillon KASSERT(pcb != NULL, ("ng_btsocket_l2cap_detach: pcb == NULL"));
2346b06ebda0SMatthew Dillon
2347b06ebda0SMatthew Dillon if (ng_btsocket_l2cap_node == NULL)
2348e85b99abSSascha Wildner goto out;
2349b06ebda0SMatthew Dillon
2350e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_EXCLUSIVE);
2351e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
2352b06ebda0SMatthew Dillon
2353b06ebda0SMatthew Dillon /* XXX what to do with pending request? */
2354b06ebda0SMatthew Dillon if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
2355b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
2356b06ebda0SMatthew Dillon
2357b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED &&
2358b06ebda0SMatthew Dillon pcb->state != NG_BTSOCKET_L2CAP_DISCONNECTING)
2359b06ebda0SMatthew Dillon /* Send disconnect request with "zero" token */
2360b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
2361b06ebda0SMatthew Dillon
2362b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
2363b06ebda0SMatthew Dillon
2364b06ebda0SMatthew Dillon LIST_REMOVE(pcb, next);
2365b06ebda0SMatthew Dillon
2366e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
2367e85b99abSSascha Wildner lockmgr(&ng_btsocket_l2cap_sockets_lock, LK_RELEASE);
2368b06ebda0SMatthew Dillon
2369e85b99abSSascha Wildner lockuninit(&pcb->pcb_lock);
2370b06ebda0SMatthew Dillon bzero(pcb, sizeof(*pcb));
2371fc025606SSascha Wildner kfree(pcb, M_NETGRAPH_BTSOCKET_L2CAP);
2372b06ebda0SMatthew Dillon
2373b06ebda0SMatthew Dillon soisdisconnected(so);
2374b06ebda0SMatthew Dillon so->so_pcb = NULL;
2375e85b99abSSascha Wildner
2376e85b99abSSascha Wildner out:
2377e85b99abSSascha Wildner lwkt_replymsg(&msg->detach.base.lmsg, error);
2378b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_detach */
2379b06ebda0SMatthew Dillon
2380b06ebda0SMatthew Dillon /*
2381b06ebda0SMatthew Dillon * Disconnect socket
2382b06ebda0SMatthew Dillon */
2383b06ebda0SMatthew Dillon
2384e85b99abSSascha Wildner void
ng_btsocket_l2cap_disconnect(netmsg_t msg)2385e85b99abSSascha Wildner ng_btsocket_l2cap_disconnect(netmsg_t msg)
2386b06ebda0SMatthew Dillon {
2387e85b99abSSascha Wildner struct socket *so = msg->disconnect.base.nm_so;
2388b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2389b06ebda0SMatthew Dillon int error = 0;
2390b06ebda0SMatthew Dillon
2391e85b99abSSascha Wildner if (pcb == NULL) {
2392e85b99abSSascha Wildner error = EINVAL;
2393e85b99abSSascha Wildner goto out;
2394e85b99abSSascha Wildner }
2395e85b99abSSascha Wildner if (ng_btsocket_l2cap_node == NULL) {
2396e85b99abSSascha Wildner error = EINVAL;
2397e85b99abSSascha Wildner goto out;
2398e85b99abSSascha Wildner }
2399b06ebda0SMatthew Dillon
2400e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
2401b06ebda0SMatthew Dillon
2402b06ebda0SMatthew Dillon if (pcb->state == NG_BTSOCKET_L2CAP_DISCONNECTING) {
2403e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
2404e85b99abSSascha Wildner error = EINPROGRESS;
2405e85b99abSSascha Wildner goto out;
2406b06ebda0SMatthew Dillon }
2407b06ebda0SMatthew Dillon
2408b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
2409b06ebda0SMatthew Dillon /* XXX FIXME what to do with pending request? */
2410b06ebda0SMatthew Dillon if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
2411b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(pcb);
2412b06ebda0SMatthew Dillon
2413b06ebda0SMatthew Dillon error = ng_btsocket_l2cap_send_l2ca_discon_req(pcb->token, pcb);
2414b06ebda0SMatthew Dillon if (error == 0) {
2415b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_DISCONNECTING;
2416b06ebda0SMatthew Dillon soisdisconnecting(so);
2417b06ebda0SMatthew Dillon
2418b06ebda0SMatthew Dillon ng_btsocket_l2cap_timeout(pcb);
2419b06ebda0SMatthew Dillon }
2420b06ebda0SMatthew Dillon
2421b06ebda0SMatthew Dillon /* XXX FIXME what to do if error != 0 */
2422b06ebda0SMatthew Dillon }
2423b06ebda0SMatthew Dillon
2424e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
2425b06ebda0SMatthew Dillon
2426e85b99abSSascha Wildner out:
2427e85b99abSSascha Wildner lwkt_replymsg(&msg->disconnect.base.lmsg, error);
2428b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_disconnect */
2429b06ebda0SMatthew Dillon
2430b06ebda0SMatthew Dillon /*
2431b06ebda0SMatthew Dillon * Listen on socket
2432b06ebda0SMatthew Dillon */
2433b06ebda0SMatthew Dillon
2434e85b99abSSascha Wildner void
ng_btsocket_l2cap_listen(netmsg_t msg)2435e85b99abSSascha Wildner ng_btsocket_l2cap_listen(netmsg_t msg)
2436b06ebda0SMatthew Dillon {
2437e85b99abSSascha Wildner struct socket *so = msg->listen.base.nm_so;
2438e85b99abSSascha Wildner struct thread *td = msg->listen.nm_td;
2439e85b99abSSascha Wildner int backlog = msg->listen.nm_flags; /* XXX */
2440b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2441e85b99abSSascha Wildner int error = 0;
2442b06ebda0SMatthew Dillon
2443e85b99abSSascha Wildner if (so->so_state &
2444e85b99abSSascha Wildner (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) {
2445e85b99abSSascha Wildner error = EINVAL;
2446b06ebda0SMatthew Dillon goto out;
2447e85b99abSSascha Wildner }
2448b06ebda0SMatthew Dillon if (pcb == NULL) {
2449b06ebda0SMatthew Dillon error = EINVAL;
2450b06ebda0SMatthew Dillon goto out;
2451b06ebda0SMatthew Dillon }
2452b06ebda0SMatthew Dillon if (ng_btsocket_l2cap_node == NULL) {
2453b06ebda0SMatthew Dillon error = EINVAL;
2454b06ebda0SMatthew Dillon goto out;
2455b06ebda0SMatthew Dillon }
2456b06ebda0SMatthew Dillon if (pcb->psm == 0) {
2457b06ebda0SMatthew Dillon error = EADDRNOTAVAIL;
2458b06ebda0SMatthew Dillon goto out;
2459b06ebda0SMatthew Dillon }
2460e85b99abSSascha Wildner solisten(so, backlog, td);
2461e85b99abSSascha Wildner
2462b06ebda0SMatthew Dillon out:
2463e85b99abSSascha Wildner lwkt_replymsg(&msg->listen.base.lmsg, error);
2464b06ebda0SMatthew Dillon } /* ng_btsocket_listen */
2465b06ebda0SMatthew Dillon
2466b06ebda0SMatthew Dillon /*
2467b06ebda0SMatthew Dillon * Get peer address
2468b06ebda0SMatthew Dillon */
2469b06ebda0SMatthew Dillon
2470e85b99abSSascha Wildner void
ng_btsocket_l2cap_peeraddr(netmsg_t msg)2471e85b99abSSascha Wildner ng_btsocket_l2cap_peeraddr(netmsg_t msg)
2472b06ebda0SMatthew Dillon {
2473e85b99abSSascha Wildner struct socket *so = msg->peeraddr.base.nm_so;
2474e85b99abSSascha Wildner struct sockaddr **nam = msg->peeraddr.nm_nam;
2475b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2476b06ebda0SMatthew Dillon struct sockaddr_l2cap sa;
2477e85b99abSSascha Wildner int error = 0;
2478b06ebda0SMatthew Dillon
2479e85b99abSSascha Wildner if (pcb == NULL) {
2480e85b99abSSascha Wildner error = EINVAL;
2481e85b99abSSascha Wildner goto out;
2482e85b99abSSascha Wildner }
2483e85b99abSSascha Wildner if (ng_btsocket_l2cap_node == NULL) {
2484e85b99abSSascha Wildner error = EINVAL;
2485e85b99abSSascha Wildner goto out;
2486e85b99abSSascha Wildner }
2487b06ebda0SMatthew Dillon
2488b06ebda0SMatthew Dillon bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
2489b06ebda0SMatthew Dillon sa.l2cap_psm = htole16(pcb->psm);
2490b06ebda0SMatthew Dillon sa.l2cap_len = sizeof(sa);
2491b06ebda0SMatthew Dillon sa.l2cap_family = AF_BLUETOOTH;
2492b06ebda0SMatthew Dillon
2493e85b99abSSascha Wildner *nam = dup_sockaddr((struct sockaddr *) &sa);
2494b06ebda0SMatthew Dillon
2495e85b99abSSascha Wildner if (*nam == NULL)
2496e85b99abSSascha Wildner error = ENOMEM;
2497e85b99abSSascha Wildner
2498e85b99abSSascha Wildner out:
2499e85b99abSSascha Wildner lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
2500b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_peeraddr */
2501b06ebda0SMatthew Dillon
2502b06ebda0SMatthew Dillon /*
2503b06ebda0SMatthew Dillon * Send data to socket
2504b06ebda0SMatthew Dillon */
2505b06ebda0SMatthew Dillon
2506e85b99abSSascha Wildner void
ng_btsocket_l2cap_send(netmsg_t msg)2507e85b99abSSascha Wildner ng_btsocket_l2cap_send(netmsg_t msg)
2508b06ebda0SMatthew Dillon {
2509e85b99abSSascha Wildner struct socket *so = msg->send.base.nm_so;
2510e85b99abSSascha Wildner struct mbuf *m = msg->send.nm_m;
2511e85b99abSSascha Wildner struct mbuf *control = msg->send.nm_control;
2512b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_t *pcb = so2l2cap_pcb(so);
2513b06ebda0SMatthew Dillon int error = 0;
2514b06ebda0SMatthew Dillon
2515b06ebda0SMatthew Dillon if (ng_btsocket_l2cap_node == NULL) {
2516b06ebda0SMatthew Dillon error = ENETDOWN;
2517b06ebda0SMatthew Dillon goto drop;
2518b06ebda0SMatthew Dillon }
2519b06ebda0SMatthew Dillon
2520b06ebda0SMatthew Dillon /* Check socket and input */
2521b06ebda0SMatthew Dillon if (pcb == NULL || m == NULL || control != NULL) {
2522b06ebda0SMatthew Dillon error = EINVAL;
2523b06ebda0SMatthew Dillon goto drop;
2524b06ebda0SMatthew Dillon }
2525b06ebda0SMatthew Dillon
2526e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
2527b06ebda0SMatthew Dillon
2528b06ebda0SMatthew Dillon /* Make sure socket is connected */
2529b06ebda0SMatthew Dillon if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
2530e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
2531b06ebda0SMatthew Dillon error = ENOTCONN;
2532b06ebda0SMatthew Dillon goto drop;
2533b06ebda0SMatthew Dillon }
2534b06ebda0SMatthew Dillon
2535b06ebda0SMatthew Dillon /* Check route */
2536b06ebda0SMatthew Dillon if (pcb->rt == NULL ||
2537b06ebda0SMatthew Dillon pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook)) {
2538e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
2539b06ebda0SMatthew Dillon error = ENETDOWN;
2540b06ebda0SMatthew Dillon goto drop;
2541b06ebda0SMatthew Dillon }
2542b06ebda0SMatthew Dillon
2543b06ebda0SMatthew Dillon /* Check packet size agains outgoing (peer's incoming) MTU) */
2544b06ebda0SMatthew Dillon if (m->m_pkthdr.len > pcb->omtu) {
2545b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
2546b06ebda0SMatthew Dillon "%s: Packet too big, len=%d, omtu=%d\n", __func__, m->m_pkthdr.len, pcb->omtu);
2547b06ebda0SMatthew Dillon
2548e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
2549b06ebda0SMatthew Dillon error = EMSGSIZE;
2550b06ebda0SMatthew Dillon goto drop;
2551b06ebda0SMatthew Dillon }
2552b06ebda0SMatthew Dillon
2553b06ebda0SMatthew Dillon /*
2554b06ebda0SMatthew Dillon * First put packet on socket send queue. Then check if we have
2555b06ebda0SMatthew Dillon * pending timeout. If we do not have timeout then we must send
2556b06ebda0SMatthew Dillon * packet and schedule timeout. Otherwise do nothing and wait for
2557b06ebda0SMatthew Dillon * L2CA_WRITE_RSP.
2558b06ebda0SMatthew Dillon */
2559b06ebda0SMatthew Dillon
2560e85b99abSSascha Wildner sbappendrecord(&pcb->so->so_snd.sb, m);
2561b06ebda0SMatthew Dillon m = NULL;
2562b06ebda0SMatthew Dillon
2563b06ebda0SMatthew Dillon if (!(pcb->flags & NG_BTSOCKET_L2CAP_TIMO)) {
2564b06ebda0SMatthew Dillon error = ng_btsocket_l2cap_send2(pcb);
2565b06ebda0SMatthew Dillon if (error == 0)
2566b06ebda0SMatthew Dillon ng_btsocket_l2cap_timeout(pcb);
2567b06ebda0SMatthew Dillon else
2568e85b99abSSascha Wildner sbdroprecord(&pcb->so->so_snd.sb); /* XXX */
2569b06ebda0SMatthew Dillon }
2570b06ebda0SMatthew Dillon
2571e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
2572b06ebda0SMatthew Dillon drop:
2573b06ebda0SMatthew Dillon NG_FREE_M(m); /* checks for != NULL */
2574b06ebda0SMatthew Dillon NG_FREE_M(control);
2575b06ebda0SMatthew Dillon
2576e85b99abSSascha Wildner lwkt_replymsg(&msg->send.base.lmsg, error);
2577b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_send */
2578b06ebda0SMatthew Dillon
2579b06ebda0SMatthew Dillon /*
2580b06ebda0SMatthew Dillon * Send first packet in the socket queue to the L2CAP layer
2581b06ebda0SMatthew Dillon */
2582b06ebda0SMatthew Dillon
2583b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_send2(ng_btsocket_l2cap_pcb_p pcb)2584b06ebda0SMatthew Dillon ng_btsocket_l2cap_send2(ng_btsocket_l2cap_pcb_p pcb)
2585b06ebda0SMatthew Dillon {
2586b06ebda0SMatthew Dillon struct mbuf *m = NULL;
2587b06ebda0SMatthew Dillon ng_l2cap_l2ca_hdr_t *hdr = NULL;
2588b06ebda0SMatthew Dillon int error = 0;
2589b06ebda0SMatthew Dillon
2590e85b99abSSascha Wildner KKASSERT(lockowned(&pcb->pcb_lock) != 0);
2591b06ebda0SMatthew Dillon
2592e85b99abSSascha Wildner if (pcb->so->so_snd.sb.sb_cc == 0)
2593b06ebda0SMatthew Dillon return (EINVAL); /* XXX */
2594b06ebda0SMatthew Dillon
2595b5523eacSSascha Wildner m = m_dup(pcb->so->so_snd.sb.sb_mb, M_NOWAIT);
2596b06ebda0SMatthew Dillon if (m == NULL)
2597b06ebda0SMatthew Dillon return (ENOBUFS);
2598b06ebda0SMatthew Dillon
2599b06ebda0SMatthew Dillon /* Create L2CA packet header */
2600b5523eacSSascha Wildner M_PREPEND(m, sizeof(*hdr), M_NOWAIT);
2601b06ebda0SMatthew Dillon if (m != NULL)
2602b06ebda0SMatthew Dillon if (m->m_len < sizeof(*hdr))
2603b06ebda0SMatthew Dillon m = m_pullup(m, sizeof(*hdr));
2604b06ebda0SMatthew Dillon
2605b06ebda0SMatthew Dillon if (m == NULL) {
2606b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
2607b06ebda0SMatthew Dillon "%s: Failed to create L2CA packet header\n", __func__);
2608b06ebda0SMatthew Dillon
2609b06ebda0SMatthew Dillon return (ENOBUFS);
2610b06ebda0SMatthew Dillon }
2611b06ebda0SMatthew Dillon
2612b06ebda0SMatthew Dillon hdr = mtod(m, ng_l2cap_l2ca_hdr_t *);
2613b06ebda0SMatthew Dillon hdr->token = pcb->token;
2614b06ebda0SMatthew Dillon hdr->length = m->m_pkthdr.len - sizeof(*hdr);
2615b06ebda0SMatthew Dillon hdr->lcid = pcb->cid;
2616b06ebda0SMatthew Dillon
2617b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_INFO(
2618b06ebda0SMatthew Dillon "%s: Sending packet: len=%d, length=%d, lcid=%d, token=%d, state=%d\n",
2619b06ebda0SMatthew Dillon __func__, m->m_pkthdr.len, hdr->length, hdr->lcid,
2620b06ebda0SMatthew Dillon hdr->token, pcb->state);
2621b06ebda0SMatthew Dillon
2622b06ebda0SMatthew Dillon /*
2623b06ebda0SMatthew Dillon * If we got here than we have successfuly creates new L2CAP
2624b06ebda0SMatthew Dillon * data packet and now we can send it to the L2CAP layer
2625b06ebda0SMatthew Dillon */
2626b06ebda0SMatthew Dillon
2627b06ebda0SMatthew Dillon NG_SEND_DATA_ONLY(error, pcb->rt->hook, m);
2628b06ebda0SMatthew Dillon
2629b06ebda0SMatthew Dillon return (error);
2630b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_send2 */
2631b06ebda0SMatthew Dillon
2632b06ebda0SMatthew Dillon /*
2633b06ebda0SMatthew Dillon * Get socket address
2634b06ebda0SMatthew Dillon */
2635b06ebda0SMatthew Dillon
2636e85b99abSSascha Wildner void
ng_btsocket_l2cap_sockaddr(netmsg_t msg)2637e85b99abSSascha Wildner ng_btsocket_l2cap_sockaddr(netmsg_t msg)
2638b06ebda0SMatthew Dillon {
2639e85b99abSSascha Wildner struct socket *so = msg->sockaddr.base.nm_so;
2640e85b99abSSascha Wildner struct sockaddr **nam = msg->sockaddr.nm_nam;
2641b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2642b06ebda0SMatthew Dillon struct sockaddr_l2cap sa;
2643e85b99abSSascha Wildner int error = 0;
2644b06ebda0SMatthew Dillon
2645e85b99abSSascha Wildner if (pcb == NULL) {
2646e85b99abSSascha Wildner error = EINVAL;
2647e85b99abSSascha Wildner goto out;
2648e85b99abSSascha Wildner }
2649e85b99abSSascha Wildner if (ng_btsocket_l2cap_node == NULL) {
2650e85b99abSSascha Wildner error = EINVAL;
2651e85b99abSSascha Wildner goto out;
2652e85b99abSSascha Wildner }
2653b06ebda0SMatthew Dillon
2654b06ebda0SMatthew Dillon bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
2655b06ebda0SMatthew Dillon sa.l2cap_psm = htole16(pcb->psm);
2656b06ebda0SMatthew Dillon sa.l2cap_len = sizeof(sa);
2657b06ebda0SMatthew Dillon sa.l2cap_family = AF_BLUETOOTH;
2658b06ebda0SMatthew Dillon
2659e85b99abSSascha Wildner *nam = dup_sockaddr((struct sockaddr *) &sa);
2660b06ebda0SMatthew Dillon
2661e85b99abSSascha Wildner if (*nam == NULL)
2662e85b99abSSascha Wildner error = ENOMEM;
2663e85b99abSSascha Wildner
2664e85b99abSSascha Wildner out:
2665e85b99abSSascha Wildner lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
2666b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_sockaddr */
2667b06ebda0SMatthew Dillon
2668b06ebda0SMatthew Dillon /*****************************************************************************
2669b06ebda0SMatthew Dillon *****************************************************************************
2670b06ebda0SMatthew Dillon ** Misc. functions
2671b06ebda0SMatthew Dillon *****************************************************************************
2672b06ebda0SMatthew Dillon *****************************************************************************/
2673b06ebda0SMatthew Dillon
2674b06ebda0SMatthew Dillon /*
2675b06ebda0SMatthew Dillon * Look for the socket that listens on given PSM and bdaddr. Returns exact or
2676b06ebda0SMatthew Dillon * close match (if any). Caller must hold ng_btsocket_l2cap_sockets_mtx.
2677b06ebda0SMatthew Dillon */
2678b06ebda0SMatthew Dillon
2679b06ebda0SMatthew Dillon static ng_btsocket_l2cap_pcb_p
ng_btsocket_l2cap_pcb_by_addr(bdaddr_p bdaddr,int psm)2680b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_by_addr(bdaddr_p bdaddr, int psm)
2681b06ebda0SMatthew Dillon {
2682b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p p = NULL, p1 = NULL;
2683b06ebda0SMatthew Dillon
2684e85b99abSSascha Wildner KKASSERT(lockowned(&ng_btsocket_l2cap_sockets_lock) != 0);
2685b06ebda0SMatthew Dillon
2686b06ebda0SMatthew Dillon LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next) {
2687b06ebda0SMatthew Dillon if (p->so == NULL || !(p->so->so_options & SO_ACCEPTCONN) ||
2688b06ebda0SMatthew Dillon p->psm != psm)
2689b06ebda0SMatthew Dillon continue;
2690b06ebda0SMatthew Dillon
2691b06ebda0SMatthew Dillon if (bcmp(&p->src, bdaddr, sizeof(p->src)) == 0)
2692b06ebda0SMatthew Dillon break;
2693b06ebda0SMatthew Dillon
2694b06ebda0SMatthew Dillon if (bcmp(&p->src, NG_HCI_BDADDR_ANY, sizeof(p->src)) == 0)
2695b06ebda0SMatthew Dillon p1 = p;
2696b06ebda0SMatthew Dillon }
2697b06ebda0SMatthew Dillon
2698b06ebda0SMatthew Dillon return ((p != NULL)? p : p1);
2699b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_pcb_by_addr */
2700b06ebda0SMatthew Dillon
2701b06ebda0SMatthew Dillon /*
2702b06ebda0SMatthew Dillon * Look for the socket that has given token.
2703b06ebda0SMatthew Dillon * Caller must hold ng_btsocket_l2cap_sockets_mtx.
2704b06ebda0SMatthew Dillon */
2705b06ebda0SMatthew Dillon
2706b06ebda0SMatthew Dillon static ng_btsocket_l2cap_pcb_p
ng_btsocket_l2cap_pcb_by_token(u_int32_t token)2707b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_by_token(u_int32_t token)
2708b06ebda0SMatthew Dillon {
2709b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p p = NULL;
2710b06ebda0SMatthew Dillon
2711b06ebda0SMatthew Dillon if (token == 0)
2712b06ebda0SMatthew Dillon return (NULL);
2713b06ebda0SMatthew Dillon
2714e85b99abSSascha Wildner KKASSERT(lockowned(&ng_btsocket_l2cap_sockets_lock) != 0);
2715b06ebda0SMatthew Dillon
2716b06ebda0SMatthew Dillon LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next)
2717b06ebda0SMatthew Dillon if (p->token == token)
2718b06ebda0SMatthew Dillon break;
2719b06ebda0SMatthew Dillon
2720b06ebda0SMatthew Dillon return (p);
2721b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_pcb_by_token */
2722b06ebda0SMatthew Dillon
2723b06ebda0SMatthew Dillon /*
2724b06ebda0SMatthew Dillon * Look for the socket that assigned to given source address and channel ID.
2725b06ebda0SMatthew Dillon * Caller must hold ng_btsocket_l2cap_sockets_mtx
2726b06ebda0SMatthew Dillon */
2727b06ebda0SMatthew Dillon
2728b06ebda0SMatthew Dillon static ng_btsocket_l2cap_pcb_p
ng_btsocket_l2cap_pcb_by_cid(bdaddr_p src,int cid)2729b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_by_cid(bdaddr_p src, int cid)
2730b06ebda0SMatthew Dillon {
2731b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p p = NULL;
2732b06ebda0SMatthew Dillon
2733e85b99abSSascha Wildner KKASSERT(lockowned(&ng_btsocket_l2cap_sockets_lock) != 0);
2734b06ebda0SMatthew Dillon
2735b06ebda0SMatthew Dillon LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next)
2736b06ebda0SMatthew Dillon if (p->cid == cid && bcmp(src, &p->src, sizeof(p->src)) == 0)
2737b06ebda0SMatthew Dillon break;
2738b06ebda0SMatthew Dillon
2739b06ebda0SMatthew Dillon return (p);
2740b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_pcb_by_cid */
2741b06ebda0SMatthew Dillon
2742b06ebda0SMatthew Dillon /*
2743b06ebda0SMatthew Dillon * Set timeout on socket
2744b06ebda0SMatthew Dillon */
2745b06ebda0SMatthew Dillon
2746b06ebda0SMatthew Dillon static void
ng_btsocket_l2cap_timeout(ng_btsocket_l2cap_pcb_p pcb)2747b06ebda0SMatthew Dillon ng_btsocket_l2cap_timeout(ng_btsocket_l2cap_pcb_p pcb)
2748b06ebda0SMatthew Dillon {
2749e85b99abSSascha Wildner KKASSERT(lockowned(&pcb->pcb_lock) != 0);
2750b06ebda0SMatthew Dillon
2751b06ebda0SMatthew Dillon if (!(pcb->flags & NG_BTSOCKET_L2CAP_TIMO)) {
2752b06ebda0SMatthew Dillon pcb->flags |= NG_BTSOCKET_L2CAP_TIMO;
2753e85b99abSSascha Wildner callout_reset(&pcb->timo, bluetooth_l2cap_ertx_timeout(),
2754e85b99abSSascha Wildner ng_btsocket_l2cap_process_timeout, pcb);
2755b06ebda0SMatthew Dillon } else
2756b06ebda0SMatthew Dillon KASSERT(0,
2757b06ebda0SMatthew Dillon ("%s: Duplicated socket timeout?!\n", __func__));
2758b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_timeout */
2759b06ebda0SMatthew Dillon
2760b06ebda0SMatthew Dillon /*
2761b06ebda0SMatthew Dillon * Unset timeout on socket
2762b06ebda0SMatthew Dillon */
2763b06ebda0SMatthew Dillon
2764b06ebda0SMatthew Dillon static void
ng_btsocket_l2cap_untimeout(ng_btsocket_l2cap_pcb_p pcb)2765b06ebda0SMatthew Dillon ng_btsocket_l2cap_untimeout(ng_btsocket_l2cap_pcb_p pcb)
2766b06ebda0SMatthew Dillon {
2767e85b99abSSascha Wildner KKASSERT(lockowned(&pcb->pcb_lock) != 0);
2768b06ebda0SMatthew Dillon
2769b06ebda0SMatthew Dillon if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO) {
2770e85b99abSSascha Wildner callout_stop(&pcb->timo);
2771b06ebda0SMatthew Dillon pcb->flags &= ~NG_BTSOCKET_L2CAP_TIMO;
2772b06ebda0SMatthew Dillon } else
2773b06ebda0SMatthew Dillon KASSERT(0,
2774b06ebda0SMatthew Dillon ("%s: No socket timeout?!\n", __func__));
2775b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_untimeout */
2776b06ebda0SMatthew Dillon
2777b06ebda0SMatthew Dillon /*
2778b06ebda0SMatthew Dillon * Process timeout on socket
2779b06ebda0SMatthew Dillon */
2780b06ebda0SMatthew Dillon
2781b06ebda0SMatthew Dillon static void
ng_btsocket_l2cap_process_timeout(void * xpcb)2782b06ebda0SMatthew Dillon ng_btsocket_l2cap_process_timeout(void *xpcb)
2783b06ebda0SMatthew Dillon {
2784b06ebda0SMatthew Dillon ng_btsocket_l2cap_pcb_p pcb = (ng_btsocket_l2cap_pcb_p) xpcb;
2785b06ebda0SMatthew Dillon
2786e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
2787b06ebda0SMatthew Dillon
2788b06ebda0SMatthew Dillon pcb->flags &= ~NG_BTSOCKET_L2CAP_TIMO;
2789b06ebda0SMatthew Dillon pcb->so->so_error = ETIMEDOUT;
2790b06ebda0SMatthew Dillon
2791b06ebda0SMatthew Dillon switch (pcb->state) {
2792b06ebda0SMatthew Dillon case NG_BTSOCKET_L2CAP_CONNECTING:
2793b06ebda0SMatthew Dillon case NG_BTSOCKET_L2CAP_CONFIGURING:
2794b06ebda0SMatthew Dillon /* Send disconnect request with "zero" token */
2795b06ebda0SMatthew Dillon if (pcb->cid != 0)
2796b06ebda0SMatthew Dillon ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
2797b06ebda0SMatthew Dillon
2798b06ebda0SMatthew Dillon /* ... and close the socket */
2799b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
2800b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
2801b06ebda0SMatthew Dillon break;
2802b06ebda0SMatthew Dillon
2803b06ebda0SMatthew Dillon case NG_BTSOCKET_L2CAP_OPEN:
2804b06ebda0SMatthew Dillon /* Send timeout - drop packet and wakeup sender */
2805e85b99abSSascha Wildner sbdroprecord(&pcb->so->so_snd.sb);
2806b06ebda0SMatthew Dillon sowwakeup(pcb->so);
2807b06ebda0SMatthew Dillon break;
2808b06ebda0SMatthew Dillon
2809b06ebda0SMatthew Dillon case NG_BTSOCKET_L2CAP_DISCONNECTING:
2810b06ebda0SMatthew Dillon /* Disconnect timeout - disconnect the socket anyway */
2811b06ebda0SMatthew Dillon pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
2812b06ebda0SMatthew Dillon soisdisconnected(pcb->so);
2813b06ebda0SMatthew Dillon break;
2814b06ebda0SMatthew Dillon
2815b06ebda0SMatthew Dillon default:
2816b06ebda0SMatthew Dillon NG_BTSOCKET_L2CAP_ERR(
2817b06ebda0SMatthew Dillon "%s: Invalid socket state=%d\n", __func__, pcb->state);
2818b06ebda0SMatthew Dillon break;
2819b06ebda0SMatthew Dillon }
2820b06ebda0SMatthew Dillon
2821e85b99abSSascha Wildner lockmgr(&pcb->pcb_lock, LK_RELEASE);
2822b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_process_timeout */
2823b06ebda0SMatthew Dillon
2824b06ebda0SMatthew Dillon /*
2825b06ebda0SMatthew Dillon * Translate HCI/L2CAP error code into "errno" code
2826b06ebda0SMatthew Dillon * XXX Note: Some L2CAP and HCI error codes have the same value, but
2827b06ebda0SMatthew Dillon * different meaning
2828b06ebda0SMatthew Dillon */
2829b06ebda0SMatthew Dillon
2830b06ebda0SMatthew Dillon static int
ng_btsocket_l2cap_result2errno(int result)2831b06ebda0SMatthew Dillon ng_btsocket_l2cap_result2errno(int result)
2832b06ebda0SMatthew Dillon {
2833b06ebda0SMatthew Dillon switch (result) {
2834b06ebda0SMatthew Dillon case 0x00: /* No error */
2835b06ebda0SMatthew Dillon return (0);
2836b06ebda0SMatthew Dillon
2837b06ebda0SMatthew Dillon case 0x01: /* Unknown HCI command */
2838b06ebda0SMatthew Dillon return (ENODEV);
2839b06ebda0SMatthew Dillon
2840b06ebda0SMatthew Dillon case 0x02: /* No connection */
2841b06ebda0SMatthew Dillon return (ENOTCONN);
2842b06ebda0SMatthew Dillon
2843b06ebda0SMatthew Dillon case 0x03: /* Hardware failure */
2844b06ebda0SMatthew Dillon return (EIO);
2845b06ebda0SMatthew Dillon
2846b06ebda0SMatthew Dillon case 0x04: /* Page timeout */
2847b06ebda0SMatthew Dillon return (EHOSTDOWN);
2848b06ebda0SMatthew Dillon
2849b06ebda0SMatthew Dillon case 0x05: /* Authentication failure */
2850b06ebda0SMatthew Dillon case 0x06: /* Key missing */
2851b06ebda0SMatthew Dillon case 0x18: /* Pairing not allowed */
2852b06ebda0SMatthew Dillon case 0x21: /* Role change not allowed */
2853b06ebda0SMatthew Dillon case 0x24: /* LMP PSU not allowed */
2854b06ebda0SMatthew Dillon case 0x25: /* Encryption mode not acceptable */
2855b06ebda0SMatthew Dillon case 0x26: /* Unit key used */
2856b06ebda0SMatthew Dillon return (EACCES);
2857b06ebda0SMatthew Dillon
2858b06ebda0SMatthew Dillon case 0x07: /* Memory full */
2859b06ebda0SMatthew Dillon return (ENOMEM);
2860b06ebda0SMatthew Dillon
2861b06ebda0SMatthew Dillon case 0x08: /* Connection timeout */
2862b06ebda0SMatthew Dillon case 0x10: /* Host timeout */
2863b06ebda0SMatthew Dillon case 0x22: /* LMP response timeout */
2864b06ebda0SMatthew Dillon case 0xee: /* HCI timeout */
2865b06ebda0SMatthew Dillon case 0xeeee: /* L2CAP timeout */
2866b06ebda0SMatthew Dillon return (ETIMEDOUT);
2867b06ebda0SMatthew Dillon
2868b06ebda0SMatthew Dillon case 0x09: /* Max number of connections */
2869b06ebda0SMatthew Dillon case 0x0a: /* Max number of SCO connections to a unit */
2870b06ebda0SMatthew Dillon return (EMLINK);
2871b06ebda0SMatthew Dillon
2872b06ebda0SMatthew Dillon case 0x0b: /* ACL connection already exists */
2873b06ebda0SMatthew Dillon return (EEXIST);
2874b06ebda0SMatthew Dillon
2875b06ebda0SMatthew Dillon case 0x0c: /* Command disallowed */
2876b06ebda0SMatthew Dillon return (EBUSY);
2877b06ebda0SMatthew Dillon
2878b06ebda0SMatthew Dillon case 0x0d: /* Host rejected due to limited resources */
2879b06ebda0SMatthew Dillon case 0x0e: /* Host rejected due to securiity reasons */
2880b06ebda0SMatthew Dillon case 0x0f: /* Host rejected due to remote unit is a personal unit */
2881b06ebda0SMatthew Dillon case 0x1b: /* SCO offset rejected */
2882b06ebda0SMatthew Dillon case 0x1c: /* SCO interval rejected */
2883b06ebda0SMatthew Dillon case 0x1d: /* SCO air mode rejected */
2884b06ebda0SMatthew Dillon return (ECONNREFUSED);
2885b06ebda0SMatthew Dillon
2886b06ebda0SMatthew Dillon case 0x11: /* Unsupported feature or parameter value */
2887b06ebda0SMatthew Dillon case 0x19: /* Unknown LMP PDU */
2888b06ebda0SMatthew Dillon case 0x1a: /* Unsupported remote feature */
2889b06ebda0SMatthew Dillon case 0x20: /* Unsupported LMP parameter value */
2890b06ebda0SMatthew Dillon case 0x27: /* QoS is not supported */
2891b06ebda0SMatthew Dillon case 0x29: /* Paring with unit key not supported */
2892b06ebda0SMatthew Dillon return (EOPNOTSUPP);
2893b06ebda0SMatthew Dillon
2894b06ebda0SMatthew Dillon case 0x12: /* Invalid HCI command parameter */
2895b06ebda0SMatthew Dillon case 0x1e: /* Invalid LMP parameters */
2896b06ebda0SMatthew Dillon return (EINVAL);
2897b06ebda0SMatthew Dillon
2898b06ebda0SMatthew Dillon case 0x13: /* Other end terminated connection: User ended connection */
2899b06ebda0SMatthew Dillon case 0x14: /* Other end terminated connection: Low resources */
2900b06ebda0SMatthew Dillon case 0x15: /* Other end terminated connection: About to power off */
2901b06ebda0SMatthew Dillon return (ECONNRESET);
2902b06ebda0SMatthew Dillon
2903b06ebda0SMatthew Dillon case 0x16: /* Connection terminated by local host */
2904b06ebda0SMatthew Dillon return (ECONNABORTED);
2905b06ebda0SMatthew Dillon
2906b06ebda0SMatthew Dillon #if 0 /* XXX not yet */
2907b06ebda0SMatthew Dillon case 0x17: /* Repeated attempts */
2908b06ebda0SMatthew Dillon case 0x1f: /* Unspecified error */
2909b06ebda0SMatthew Dillon case 0x23: /* LMP error transaction collision */
2910b06ebda0SMatthew Dillon case 0x28: /* Instant passed */
2911b06ebda0SMatthew Dillon #endif
2912b06ebda0SMatthew Dillon }
2913b06ebda0SMatthew Dillon
2914b06ebda0SMatthew Dillon return (ENOSYS);
2915b06ebda0SMatthew Dillon } /* ng_btsocket_l2cap_result2errno */
2916b06ebda0SMatthew Dillon
2917