6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: ng_btsocket_l2cap.c,v 1.16 2003/09/14 23:29:06 max Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bitstring.h>
37 #include <sys/domain.h>
38 #include <sys/endian.h>
39 #include <sys/errno.h>
40 #include <sys/filedesc.h>
41 #include <sys/ioccom.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/protosw.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/taskqueue.h>
56 #include <netgraph/ng_message.h>
57 #include <netgraph/netgraph.h>
58 #include <netgraph/bluetooth/include/ng_bluetooth.h>
59 #include <netgraph/bluetooth/include/ng_hci.h>
60 #include <netgraph/bluetooth/include/ng_l2cap.h>
61 #include <netgraph/bluetooth/include/ng_btsocket.h>
62 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
65 #ifdef NG_SEPARATE_MALLOC
66 static MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_L2CAP, "netgraph_btsocks_l2cap",
67 "Netgraph Bluetooth L2CAP sockets");
69 #define M_NETGRAPH_BTSOCKET_L2CAP M_NETGRAPH
70 #endif /* NG_SEPARATE_MALLOC */
72 /* Netgraph node methods */
73 static ng_constructor_t ng_btsocket_l2cap_node_constructor;
74 static ng_rcvmsg_t ng_btsocket_l2cap_node_rcvmsg;
75 static ng_shutdown_t ng_btsocket_l2cap_node_shutdown;
76 static ng_newhook_t ng_btsocket_l2cap_node_newhook;
77 static ng_connect_t ng_btsocket_l2cap_node_connect;
78 static ng_rcvdata_t ng_btsocket_l2cap_node_rcvdata;
79 static ng_disconnect_t ng_btsocket_l2cap_node_disconnect;
81 static void ng_btsocket_l2cap_input (void *, int);
82 static void ng_btsocket_l2cap_rtclean (void *, int);
84 /* Netgraph type descriptor */
85 static struct ng_type typestruct = {
86 .version = NG_ABI_VERSION,
87 .name = NG_BTSOCKET_L2CAP_NODE_TYPE,
88 .constructor = ng_btsocket_l2cap_node_constructor,
89 .rcvmsg = ng_btsocket_l2cap_node_rcvmsg,
90 .shutdown = ng_btsocket_l2cap_node_shutdown,
91 .newhook = ng_btsocket_l2cap_node_newhook,
92 .connect = ng_btsocket_l2cap_node_connect,
93 .rcvdata = ng_btsocket_l2cap_node_rcvdata,
94 .disconnect = ng_btsocket_l2cap_node_disconnect,
99 static u_int32_t ng_btsocket_l2cap_debug_level;
100 static node_p ng_btsocket_l2cap_node;
101 static struct ng_bt_itemq ng_btsocket_l2cap_queue;
102 static struct mtx ng_btsocket_l2cap_queue_mtx;
103 static struct task ng_btsocket_l2cap_queue_task;
104 static LIST_HEAD(, ng_btsocket_l2cap_pcb) ng_btsocket_l2cap_sockets;
105 static struct mtx ng_btsocket_l2cap_sockets_mtx;
106 static LIST_HEAD(, ng_btsocket_l2cap_rtentry) ng_btsocket_l2cap_rt;
107 static struct mtx ng_btsocket_l2cap_rt_mtx;
108 static struct task ng_btsocket_l2cap_rt_task;
109 static struct timeval ng_btsocket_l2cap_lasttime;
110 static int ng_btsocket_l2cap_curpps;
113 SYSCTL_DECL(_net_bluetooth_l2cap_sockets);
114 static SYSCTL_NODE(_net_bluetooth_l2cap_sockets, OID_AUTO, seq, CTLFLAG_RW,
115 0, "Bluetooth SEQPACKET L2CAP sockets family");
116 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, debug_level,
118 &ng_btsocket_l2cap_debug_level, NG_BTSOCKET_WARN_LEVEL,
119 "Bluetooth SEQPACKET L2CAP sockets debug level");
120 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_len,
122 &ng_btsocket_l2cap_queue.len, 0,
123 "Bluetooth SEQPACKET L2CAP sockets input queue length");
124 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_maxlen,
126 &ng_btsocket_l2cap_queue.maxlen, 0,
127 "Bluetooth SEQPACKET L2CAP sockets input queue max. length");
128 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_drops,
130 &ng_btsocket_l2cap_queue.drops, 0,
131 "Bluetooth SEQPACKET L2CAP sockets input queue drops");
134 #define NG_BTSOCKET_L2CAP_INFO \
135 if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
136 ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
139 #define NG_BTSOCKET_L2CAP_WARN \
140 if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
141 ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
144 #define NG_BTSOCKET_L2CAP_ERR \
145 if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
146 ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
149 #define NG_BTSOCKET_L2CAP_ALERT \
150 if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
151 ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
155 * Netgraph message processing routines
158 static int ng_btsocket_l2cap_process_l2ca_con_req_rsp
159 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
160 static int ng_btsocket_l2cap_process_l2ca_con_rsp_rsp
161 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
162 static int ng_btsocket_l2cap_process_l2ca_con_ind
163 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
165 static int ng_btsocket_l2cap_process_l2ca_cfg_req_rsp
166 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
167 static int ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp
168 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
169 static int ng_btsocket_l2cap_process_l2ca_cfg_ind
170 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
172 static int ng_btsocket_l2cap_process_l2ca_discon_rsp
173 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
174 static int ng_btsocket_l2cap_process_l2ca_discon_ind
175 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
177 static int ng_btsocket_l2cap_process_l2ca_write_rsp
178 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
181 * Send L2CA_xxx messages to the lower layer
184 static int ng_btsocket_l2cap_send_l2ca_con_req
185 (ng_btsocket_l2cap_pcb_p);
186 static int ng_btsocket_l2cap_send_l2ca_con_rsp_req
187 (u_int32_t, ng_btsocket_l2cap_rtentry_p, bdaddr_p, int, int, int);
188 static int ng_btsocket_l2cap_send_l2ca_cfg_req
189 (ng_btsocket_l2cap_pcb_p);
190 static int ng_btsocket_l2cap_send_l2ca_cfg_rsp
191 (ng_btsocket_l2cap_pcb_p);
192 static int ng_btsocket_l2cap_send_l2ca_discon_req
193 (u_int32_t, ng_btsocket_l2cap_pcb_p);
195 static int ng_btsocket_l2cap_send2
196 (ng_btsocket_l2cap_pcb_p);
199 * Timeout processing routines
202 static void ng_btsocket_l2cap_timeout (ng_btsocket_l2cap_pcb_p);
203 static void ng_btsocket_l2cap_untimeout (ng_btsocket_l2cap_pcb_p);
204 static void ng_btsocket_l2cap_process_timeout (void *);
210 static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_addr(bdaddr_p, int);
211 static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_token(u_int32_t);
212 static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_cid (bdaddr_p, int);
213 static int ng_btsocket_l2cap_result2errno(int);
215 #define ng_btsocket_l2cap_wakeup_input_task() \
216 taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_l2cap_queue_task)
218 #define ng_btsocket_l2cap_wakeup_route_task() \
219 taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_l2cap_rt_task)
221 /*****************************************************************************
222 *****************************************************************************
223 ** Netgraph node interface
224 *****************************************************************************
225 *****************************************************************************/
228 * Netgraph node constructor. Do not allow to create node of this type.
232 ng_btsocket_l2cap_node_constructor(node_p node)
235 } /* ng_btsocket_l2cap_node_constructor */
238 * Do local shutdown processing. Let old node go and create new fresh one.
242 ng_btsocket_l2cap_node_shutdown(node_p node)
248 /* Create new node */
249 error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_node);
251 NG_BTSOCKET_L2CAP_ALERT(
252 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
254 ng_btsocket_l2cap_node = NULL;
259 error = ng_name_node(ng_btsocket_l2cap_node,
260 NG_BTSOCKET_L2CAP_NODE_TYPE);
262 NG_BTSOCKET_L2CAP_ALERT(
263 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
265 NG_NODE_UNREF(ng_btsocket_l2cap_node);
266 ng_btsocket_l2cap_node = NULL;
272 } /* ng_btsocket_l2cap_node_shutdown */
275 * We allow any hook to be connected to the node.
279 ng_btsocket_l2cap_node_newhook(node_p node, hook_p hook, char const *name)
282 } /* ng_btsocket_l2cap_node_newhook */
285 * Just say "YEP, that's OK by me!"
289 ng_btsocket_l2cap_node_connect(hook_p hook)
291 NG_HOOK_SET_PRIVATE(hook, NULL);
292 NG_HOOK_REF(hook); /* Keep extra reference to the hook */
295 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
296 NG_HOOK_FORCE_QUEUE(hook);
300 } /* ng_btsocket_l2cap_node_connect */
303 * Hook disconnection. Schedule route cleanup task
307 ng_btsocket_l2cap_node_disconnect(hook_p hook)
310 * If hook has private information than we must have this hook in
311 * the routing table and must schedule cleaning for the routing table.
312 * Otherwise hook was connected but we never got "hook_info" message,
313 * so we have never added this hook to the routing table and it save
317 if (NG_HOOK_PRIVATE(hook) != NULL)
318 return (ng_btsocket_l2cap_wakeup_route_task());
320 NG_HOOK_UNREF(hook); /* Remove extra reference */
323 } /* ng_btsocket_l2cap_node_disconnect */
326 * Process incoming messages
330 ng_btsocket_l2cap_node_rcvmsg(node_p node, item_p item, hook_p hook)
332 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
335 if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) {
336 mtx_lock(&ng_btsocket_l2cap_queue_mtx);
337 if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) {
338 NG_BTSOCKET_L2CAP_ERR(
339 "%s: Input queue is full (msg)\n", __func__);
341 NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_queue);
347 NGI_SET_HOOK(item, hook);
350 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item);
351 error = ng_btsocket_l2cap_wakeup_input_task();
353 mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
360 } /* ng_btsocket_l2cap_node_rcvmsg */
363 * Receive data on a hook
367 ng_btsocket_l2cap_node_rcvdata(hook_p hook, item_p item)
371 mtx_lock(&ng_btsocket_l2cap_queue_mtx);
372 if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) {
373 NG_BTSOCKET_L2CAP_ERR(
374 "%s: Input queue is full (data)\n", __func__);
376 NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_queue);
381 NGI_SET_HOOK(item, hook);
383 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item);
384 error = ng_btsocket_l2cap_wakeup_input_task();
386 mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
389 } /* ng_btsocket_l2cap_node_rcvdata */
392 * Process L2CA_Connect respose. Socket layer must have initiated connection,
393 * so we have to have a socket associated with message token.
397 ng_btsocket_l2cap_process_l2ca_con_req_rsp(struct ng_mesg *msg,
398 ng_btsocket_l2cap_rtentry_p rt)
400 ng_l2cap_l2ca_con_op *op = NULL;
401 ng_btsocket_l2cap_pcb_t *pcb = NULL;
404 if (msg->header.arglen != sizeof(*op))
407 op = (ng_l2cap_l2ca_con_op *)(msg->data);
409 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
411 /* Look for the socket with the token */
412 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
414 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
418 mtx_lock(&pcb->pcb_mtx);
420 NG_BTSOCKET_L2CAP_INFO(
421 "%s: Got L2CA_Connect response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
422 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, status=%d, " \
423 "state=%d\n", __func__, msg->header.token,
424 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
425 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
426 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
427 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
428 pcb->psm, op->lcid, op->result, op->status,
431 if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) {
432 mtx_unlock(&pcb->pcb_mtx);
433 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
438 ng_btsocket_l2cap_untimeout(pcb);
440 if (op->result == NG_L2CAP_PENDING) {
441 ng_btsocket_l2cap_timeout(pcb);
442 mtx_unlock(&pcb->pcb_mtx);
443 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
448 if (op->result == NG_L2CAP_SUCCESS) {
450 * Channel is now open, so update local channel ID and
451 * start configuration process. Source and destination
452 * addresses as well as route must be already set.
457 error = ng_btsocket_l2cap_send_l2ca_cfg_req(pcb);
459 /* Send disconnect request with "zero" token */
460 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
462 /* ... and close the socket */
463 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
464 soisdisconnected(pcb->so);
466 pcb->cfg_state = NG_BTSOCKET_L2CAP_CFG_IN_SENT;
467 pcb->state = NG_BTSOCKET_L2CAP_CONFIGURING;
469 ng_btsocket_l2cap_timeout(pcb);
473 * We have failed to open connection, so convert result
474 * code to "errno" code and disconnect the socket. Channel
475 * already has been closed.
478 pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
479 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
480 soisdisconnected(pcb->so);
483 mtx_unlock(&pcb->pcb_mtx);
484 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
487 } /* ng_btsocket_l2cap_process_l2ca_con_req_rsp */
490 * Process L2CA_ConnectRsp response
494 ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(struct ng_mesg *msg,
495 ng_btsocket_l2cap_rtentry_p rt)
497 ng_l2cap_l2ca_con_rsp_op *op = NULL;
498 ng_btsocket_l2cap_pcb_t *pcb = NULL;
500 if (msg->header.arglen != sizeof(*op))
503 op = (ng_l2cap_l2ca_con_rsp_op *)(msg->data);
505 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
507 /* Look for the socket with the token */
508 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
510 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
514 mtx_lock(&pcb->pcb_mtx);
516 NG_BTSOCKET_L2CAP_INFO(
517 "%s: Got L2CA_ConnectRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
518 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d\n",
519 __func__, msg->header.token,
520 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
521 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
522 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
523 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
524 pcb->psm, pcb->cid, op->result, pcb->state);
526 if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) {
527 mtx_unlock(&pcb->pcb_mtx);
528 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
533 ng_btsocket_l2cap_untimeout(pcb);
535 /* Check the result and disconnect the socket on failure */
536 if (op->result != NG_L2CAP_SUCCESS) {
537 /* Close the socket - channel already closed */
538 pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
539 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
540 soisdisconnected(pcb->so);
542 /* Move to CONFIGURING state and wait for CONFIG_IND */
544 pcb->state = NG_BTSOCKET_L2CAP_CONFIGURING;
545 ng_btsocket_l2cap_timeout(pcb);
548 mtx_unlock(&pcb->pcb_mtx);
549 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
552 } /* ng_btsocket_process_l2ca_con_rsp_rsp */
555 * Process L2CA_Connect indicator. Find socket that listens on address
556 * and PSM. Find exact or closest match. Create new socket and initiate
561 ng_btsocket_l2cap_process_l2ca_con_ind(struct ng_mesg *msg,
562 ng_btsocket_l2cap_rtentry_p rt)
564 ng_l2cap_l2ca_con_ind_ip *ip = NULL;
565 ng_btsocket_l2cap_pcb_t *pcb = NULL, *pcb1 = NULL;
568 u_int16_t result = 0;
570 if (msg->header.arglen != sizeof(*ip))
573 ip = (ng_l2cap_l2ca_con_ind_ip *)(msg->data);
575 NG_BTSOCKET_L2CAP_INFO(
576 "%s: Got L2CA_Connect indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
577 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, ident=%d\n",
579 rt->src.b[5], rt->src.b[4], rt->src.b[3],
580 rt->src.b[2], rt->src.b[1], rt->src.b[0],
581 ip->bdaddr.b[5], ip->bdaddr.b[4], ip->bdaddr.b[3],
582 ip->bdaddr.b[2], ip->bdaddr.b[1], ip->bdaddr.b[0],
583 ip->psm, ip->lcid, ip->ident);
585 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
587 pcb = ng_btsocket_l2cap_pcb_by_addr(&rt->src, ip->psm);
589 struct socket *so1 = NULL;
591 mtx_lock(&pcb->pcb_mtx);
594 * First check the pending connections queue and if we have
595 * space then create new socket and set proper source address.
598 if (pcb->so->so_qlen <= pcb->so->so_qlimit) {
599 CURVNET_SET(pcb->so->so_vnet);
600 so1 = sonewconn(pcb->so, 0);
605 result = NG_L2CAP_NO_RESOURCES;
610 * If we got here than we have created new socket. So complete
611 * connection. If we we listening on specific address then copy
612 * source address from listening socket, otherwise copy source
613 * address from hook's routing information.
616 pcb1 = so2l2cap_pcb(so1);
617 KASSERT((pcb1 != NULL),
618 ("%s: pcb1 == NULL\n", __func__));
620 mtx_lock(&pcb1->pcb_mtx);
622 if (bcmp(&pcb->src, NG_HCI_BDADDR_ANY, sizeof(pcb->src)) != 0)
623 bcopy(&pcb->src, &pcb1->src, sizeof(pcb1->src));
625 bcopy(&rt->src, &pcb1->src, sizeof(pcb1->src));
627 pcb1->flags &= ~NG_BTSOCKET_L2CAP_CLIENT;
629 bcopy(&ip->bdaddr, &pcb1->dst, sizeof(pcb1->dst));
631 pcb1->cid = ip->lcid;
634 /* Copy socket settings */
635 pcb1->imtu = pcb->imtu;
636 bcopy(&pcb->oflow, &pcb1->oflow, sizeof(pcb1->oflow));
637 pcb1->flush_timo = pcb->flush_timo;
641 /* Nobody listens on requested BDADDR/PSM */
642 result = NG_L2CAP_PSM_NOT_SUPPORTED;
645 error = ng_btsocket_l2cap_send_l2ca_con_rsp_req(token, rt,
646 &ip->bdaddr, ip->ident, ip->lcid, result);
649 pcb1->so->so_error = error;
650 pcb1->state = NG_BTSOCKET_L2CAP_CLOSED;
651 soisdisconnected(pcb1->so);
653 pcb1->state = NG_BTSOCKET_L2CAP_CONNECTING;
654 soisconnecting(pcb1->so);
656 ng_btsocket_l2cap_timeout(pcb1);
659 mtx_unlock(&pcb1->pcb_mtx);
663 mtx_unlock(&pcb->pcb_mtx);
665 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
668 } /* ng_btsocket_l2cap_process_l2ca_con_ind */
671 * Process L2CA_Config response
675 ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(struct ng_mesg *msg,
676 ng_btsocket_l2cap_rtentry_p rt)
678 ng_l2cap_l2ca_cfg_op *op = NULL;
679 ng_btsocket_l2cap_pcb_p pcb = NULL;
681 if (msg->header.arglen != sizeof(*op))
684 op = (ng_l2cap_l2ca_cfg_op *)(msg->data);
686 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
689 * Socket must have issued a Configure request, so we must have a
690 * socket that wants to be configured. Use Netgraph message token
694 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
697 * XXX FIXME what to do here? We could not find a
698 * socket with requested token. We even can not send
699 * Disconnect, because we do not know channel ID
702 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
706 mtx_lock(&pcb->pcb_mtx);
708 NG_BTSOCKET_L2CAP_INFO(
709 "%s: Got L2CA_Config response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
710 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d, " \
712 __func__, msg->header.token,
713 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
714 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
715 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
716 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
717 pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state);
719 if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
720 mtx_unlock(&pcb->pcb_mtx);
721 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
726 if (op->result == NG_L2CAP_SUCCESS) {
728 * XXX FIXME Actually set flush and link timeout.
729 * Set QoS here if required. Resolve conficts (flush_timo).
730 * Save incoming MTU (peer's outgoing MTU) and outgoing flow
734 pcb->imtu = op->imtu;
735 bcopy(&op->oflow, &pcb->oflow, sizeof(pcb->oflow));
736 pcb->flush_timo = op->flush_timo;
739 * We have configured incoming side, so record it and check
740 * if configuration is complete. If complete then mark socket
741 * as connected, otherwise wait for the peer.
744 pcb->cfg_state &= ~NG_BTSOCKET_L2CAP_CFG_IN_SENT;
745 pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_IN;
747 if (pcb->cfg_state == NG_BTSOCKET_L2CAP_CFG_BOTH) {
748 /* Configuration complete - mark socket as open */
749 ng_btsocket_l2cap_untimeout(pcb);
750 pcb->state = NG_BTSOCKET_L2CAP_OPEN;
751 soisconnected(pcb->so);
755 * Something went wrong. Could be unacceptable parameters,
756 * reject or unknown option. That's too bad, but we will
757 * not negotiate. Send Disconnect and close the channel.
760 ng_btsocket_l2cap_untimeout(pcb);
762 switch (op->result) {
763 case NG_L2CAP_UNACCEPTABLE_PARAMS:
764 case NG_L2CAP_UNKNOWN_OPTION:
765 pcb->so->so_error = EINVAL;
769 pcb->so->so_error = ECONNRESET;
773 /* Send disconnect with "zero" token */
774 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
776 /* ... and close the socket */
777 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
778 soisdisconnected(pcb->so);
781 mtx_unlock(&pcb->pcb_mtx);
782 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
785 } /* ng_btsocket_l2cap_process_l2ca_cfg_req_rsp */
788 * Process L2CA_ConfigRsp response
792 ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(struct ng_mesg *msg,
793 ng_btsocket_l2cap_rtentry_p rt)
795 ng_l2cap_l2ca_cfg_rsp_op *op = NULL;
796 ng_btsocket_l2cap_pcb_t *pcb = NULL;
799 if (msg->header.arglen != sizeof(*op))
802 op = (ng_l2cap_l2ca_cfg_rsp_op *)(msg->data);
804 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
806 /* Look for the socket with the token */
807 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
809 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
813 mtx_lock(&pcb->pcb_mtx);
815 NG_BTSOCKET_L2CAP_INFO(
816 "%s: Got L2CA_ConfigRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
817 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d, " \
819 __func__, msg->header.token,
820 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
821 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
822 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
823 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
824 pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state);
826 if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
827 mtx_unlock(&pcb->pcb_mtx);
828 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
833 /* Check the result and disconnect socket of failure */
834 if (op->result != NG_L2CAP_SUCCESS)
838 * Now we done with remote side configuration. Configure local
839 * side if we have not done it yet.
842 pcb->cfg_state &= ~NG_BTSOCKET_L2CAP_CFG_OUT_SENT;
843 pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_OUT;
845 if (pcb->cfg_state == NG_BTSOCKET_L2CAP_CFG_BOTH) {
846 /* Configuration complete - mask socket as open */
847 ng_btsocket_l2cap_untimeout(pcb);
848 pcb->state = NG_BTSOCKET_L2CAP_OPEN;
849 soisconnected(pcb->so);
851 if (!(pcb->cfg_state & NG_BTSOCKET_L2CAP_CFG_IN_SENT)) {
852 /* Send L2CA_Config request - incoming path */
853 error = ng_btsocket_l2cap_send_l2ca_cfg_req(pcb);
857 pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_IN_SENT;
861 mtx_unlock(&pcb->pcb_mtx);
862 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
867 ng_btsocket_l2cap_untimeout(pcb);
869 /* Send disconnect with "zero" token */
870 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
872 /* ... and close the socket */
873 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
874 soisdisconnected(pcb->so);
876 mtx_unlock(&pcb->pcb_mtx);
877 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
880 } /* ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp */
883 * Process L2CA_Config indicator
887 ng_btsocket_l2cap_process_l2ca_cfg_ind(struct ng_mesg *msg,
888 ng_btsocket_l2cap_rtentry_p rt)
890 ng_l2cap_l2ca_cfg_ind_ip *ip = NULL;
891 ng_btsocket_l2cap_pcb_t *pcb = NULL;
894 if (msg->header.arglen != sizeof(*ip))
897 ip = (ng_l2cap_l2ca_cfg_ind_ip *)(msg->data);
899 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
901 /* Check for the open socket that has given channel ID */
902 pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid);
904 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
908 mtx_lock(&pcb->pcb_mtx);
910 NG_BTSOCKET_L2CAP_INFO(
911 "%s: Got L2CA_Config indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
912 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, state=%d, cfg_state=%x\n",
914 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
915 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
916 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
917 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
918 pcb->psm, pcb->cid, pcb->state, pcb->cfg_state);
920 /* XXX FIXME re-configuration on open socket */
921 if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
922 mtx_unlock(&pcb->pcb_mtx);
923 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
929 * XXX FIXME Actually set flush and link timeout. Set QoS here if
930 * required. Resolve conficts (flush_timo). Note outgoing MTU (peer's
931 * incoming MTU) and incoming flow spec.
934 pcb->omtu = ip->omtu;
935 bcopy(&ip->iflow, &pcb->iflow, sizeof(pcb->iflow));
936 pcb->flush_timo = ip->flush_timo;
939 * Send L2CA_Config response to our peer and check for the errors,
940 * if any send disconnect to close the channel.
943 if (!(pcb->cfg_state & NG_BTSOCKET_L2CAP_CFG_OUT_SENT)) {
944 error = ng_btsocket_l2cap_send_l2ca_cfg_rsp(pcb);
946 ng_btsocket_l2cap_untimeout(pcb);
948 pcb->so->so_error = error;
950 /* Send disconnect with "zero" token */
951 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
953 /* ... and close the socket */
954 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
955 soisdisconnected(pcb->so);
957 pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_OUT_SENT;
960 mtx_unlock(&pcb->pcb_mtx);
961 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
964 } /* ng_btsocket_l2cap_process_l2cap_cfg_ind */
967 * Process L2CA_Disconnect response
971 ng_btsocket_l2cap_process_l2ca_discon_rsp(struct ng_mesg *msg,
972 ng_btsocket_l2cap_rtentry_p rt)
974 ng_l2cap_l2ca_discon_op *op = NULL;
975 ng_btsocket_l2cap_pcb_t *pcb = NULL;
978 if (msg->header.arglen != sizeof(*op))
981 op = (ng_l2cap_l2ca_discon_op *)(msg->data);
983 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
986 * Socket layer must have issued L2CA_Disconnect request, so there
987 * must be a socket that wants to be disconnected. Use Netgraph
988 * message token to find it.
991 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
993 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
997 mtx_lock(&pcb->pcb_mtx);
999 /* XXX Close socket no matter what op->result says */
1000 if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
1001 NG_BTSOCKET_L2CAP_INFO(
1002 "%s: Got L2CA_Disconnect response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1003 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d\n",
1004 __func__, msg->header.token,
1005 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
1006 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
1007 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
1008 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
1009 pcb->psm, pcb->cid, op->result, pcb->state);
1011 ng_btsocket_l2cap_untimeout(pcb);
1013 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1014 soisdisconnected(pcb->so);
1017 mtx_unlock(&pcb->pcb_mtx);
1018 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1021 } /* ng_btsocket_l2cap_process_l2ca_discon_rsp */
1024 * Process L2CA_Disconnect indicator
1028 ng_btsocket_l2cap_process_l2ca_discon_ind(struct ng_mesg *msg,
1029 ng_btsocket_l2cap_rtentry_p rt)
1031 ng_l2cap_l2ca_discon_ind_ip *ip = NULL;
1032 ng_btsocket_l2cap_pcb_t *pcb = NULL;
1035 if (msg->header.arglen != sizeof(*ip))
1038 ip = (ng_l2cap_l2ca_discon_ind_ip *)(msg->data);
1040 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
1042 /* Look for the socket with given channel ID */
1043 pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid);
1045 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1050 * Channel has already been destroyed, so disconnect the socket
1051 * and be done with it. If there was any pending request we can
1052 * not do anything here anyway.
1055 mtx_lock(&pcb->pcb_mtx);
1057 NG_BTSOCKET_L2CAP_INFO(
1058 "%s: Got L2CA_Disconnect indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1059 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, state=%d\n",
1061 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
1062 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
1063 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
1064 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
1065 pcb->psm, pcb->cid, pcb->state);
1067 if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
1068 ng_btsocket_l2cap_untimeout(pcb);
1070 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1071 soisdisconnected(pcb->so);
1073 mtx_unlock(&pcb->pcb_mtx);
1074 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1077 } /* ng_btsocket_l2cap_process_l2ca_discon_ind */
1080 * Process L2CA_Write response
1084 ng_btsocket_l2cap_process_l2ca_write_rsp(struct ng_mesg *msg,
1085 ng_btsocket_l2cap_rtentry_p rt)
1087 ng_l2cap_l2ca_write_op *op = NULL;
1088 ng_btsocket_l2cap_pcb_t *pcb = NULL;
1091 if (msg->header.arglen != sizeof(*op))
1094 op = (ng_l2cap_l2ca_write_op *)(msg->data);
1096 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
1098 /* Look for the socket with given token */
1099 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
1101 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1105 mtx_lock(&pcb->pcb_mtx);
1107 NG_BTSOCKET_L2CAP_INFO(
1108 "%s: Got L2CA_Write response, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1109 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, length=%d, " \
1110 "state=%d\n", __func__,
1111 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
1112 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
1113 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
1114 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
1115 pcb->psm, pcb->cid, op->result, op->length,
1118 if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
1119 mtx_unlock(&pcb->pcb_mtx);
1120 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1125 ng_btsocket_l2cap_untimeout(pcb);
1128 * Check if we have more data to send
1131 sbdroprecord(&pcb->so->so_snd);
1132 if (pcb->so->so_snd.sb_cc > 0) {
1133 if (ng_btsocket_l2cap_send2(pcb) == 0)
1134 ng_btsocket_l2cap_timeout(pcb);
1136 sbdroprecord(&pcb->so->so_snd); /* XXX */
1140 * Now set the result, drop packet from the socket send queue and
1141 * ask for more (wakeup sender)
1144 pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
1147 mtx_unlock(&pcb->pcb_mtx);
1148 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1151 } /* ng_btsocket_l2cap_process_l2ca_write_rsp */
1154 * Send L2CA_Connect request
1158 ng_btsocket_l2cap_send_l2ca_con_req(ng_btsocket_l2cap_pcb_p pcb)
1160 struct ng_mesg *msg = NULL;
1161 ng_l2cap_l2ca_con_ip *ip = NULL;
1164 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1166 if (pcb->rt == NULL ||
1167 pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1170 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CON,
1171 sizeof(*ip), M_NOWAIT);
1175 msg->header.token = pcb->token;
1177 ip = (ng_l2cap_l2ca_con_ip *)(msg->data);
1178 bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
1181 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0);
1184 } /* ng_btsocket_l2cap_send_l2ca_con_req */
1187 * Send L2CA_Connect response
1191 ng_btsocket_l2cap_send_l2ca_con_rsp_req(u_int32_t token,
1192 ng_btsocket_l2cap_rtentry_p rt, bdaddr_p dst, int ident,
1193 int lcid, int result)
1195 struct ng_mesg *msg = NULL;
1196 ng_l2cap_l2ca_con_rsp_ip *ip = NULL;
1199 if (rt == NULL || rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
1202 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CON_RSP,
1203 sizeof(*ip), M_NOWAIT);
1207 msg->header.token = token;
1209 ip = (ng_l2cap_l2ca_con_rsp_ip *)(msg->data);
1210 bcopy(dst, &ip->bdaddr, sizeof(ip->bdaddr));
1213 ip->result = result;
1216 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg, rt->hook, 0);
1219 } /* ng_btsocket_l2cap_send_l2ca_con_rsp_req */
1222 * Send L2CA_Config request
1226 ng_btsocket_l2cap_send_l2ca_cfg_req(ng_btsocket_l2cap_pcb_p pcb)
1228 struct ng_mesg *msg = NULL;
1229 ng_l2cap_l2ca_cfg_ip *ip = NULL;
1232 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1234 if (pcb->rt == NULL ||
1235 pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1238 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CFG,
1239 sizeof(*ip), M_NOWAIT);
1243 msg->header.token = pcb->token;
1245 ip = (ng_l2cap_l2ca_cfg_ip *)(msg->data);
1246 ip->lcid = pcb->cid;
1247 ip->imtu = pcb->imtu;
1248 bcopy(&pcb->oflow, &ip->oflow, sizeof(ip->oflow));
1249 ip->flush_timo = pcb->flush_timo;
1250 ip->link_timo = pcb->link_timo;
1252 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0);
1255 } /* ng_btsocket_l2cap_send_l2ca_cfg_req */
1258 * Send L2CA_Config response
1262 ng_btsocket_l2cap_send_l2ca_cfg_rsp(ng_btsocket_l2cap_pcb_p pcb)
1264 struct ng_mesg *msg = NULL;
1265 ng_l2cap_l2ca_cfg_rsp_ip *ip = NULL;
1268 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1270 if (pcb->rt == NULL ||
1271 pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1274 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CFG_RSP,
1275 sizeof(*ip), M_NOWAIT);
1279 msg->header.token = pcb->token;
1281 ip = (ng_l2cap_l2ca_cfg_rsp_ip *)(msg->data);
1282 ip->lcid = pcb->cid;
1283 ip->omtu = pcb->omtu;
1284 bcopy(&pcb->iflow, &ip->iflow, sizeof(ip->iflow));
1286 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg, pcb->rt->hook, 0);
1289 } /* ng_btsocket_l2cap_send_l2ca_cfg_rsp */
1292 * Send L2CA_Disconnect request
1296 ng_btsocket_l2cap_send_l2ca_discon_req(u_int32_t token,
1297 ng_btsocket_l2cap_pcb_p pcb)
1299 struct ng_mesg *msg = NULL;
1300 ng_l2cap_l2ca_discon_ip *ip = NULL;
1303 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1305 if (pcb->rt == NULL ||
1306 pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook))
1309 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_DISCON,
1310 sizeof(*ip), M_NOWAIT);
1314 msg->header.token = token;
1316 ip = (ng_l2cap_l2ca_discon_ip *)(msg->data);
1317 ip->lcid = pcb->cid;
1319 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0);
1322 } /* ng_btsocket_l2cap_send_l2ca_discon_req */
1324 /*****************************************************************************
1325 *****************************************************************************
1327 *****************************************************************************
1328 *****************************************************************************/
1331 * L2CAP sockets data input routine
1335 ng_btsocket_l2cap_data_input(struct mbuf *m, hook_p hook)
1337 ng_l2cap_hdr_t *hdr = NULL;
1338 ng_l2cap_clt_hdr_t *clt_hdr = NULL;
1339 ng_btsocket_l2cap_pcb_t *pcb = NULL;
1340 ng_btsocket_l2cap_rtentry_t *rt = NULL;
1343 NG_BTSOCKET_L2CAP_ALERT(
1344 "%s: Invalid source hook for L2CAP data packet\n", __func__);
1348 rt = (ng_btsocket_l2cap_rtentry_t *) NG_HOOK_PRIVATE(hook);
1350 NG_BTSOCKET_L2CAP_ALERT(
1351 "%s: Could not find out source bdaddr for L2CAP data packet\n", __func__);
1355 /* Make sure we can access header */
1356 if (m->m_pkthdr.len < sizeof(*hdr)) {
1357 NG_BTSOCKET_L2CAP_ERR(
1358 "%s: L2CAP data packet too small, len=%d\n", __func__, m->m_pkthdr.len);
1362 if (m->m_len < sizeof(*hdr)) {
1363 m = m_pullup(m, sizeof(*hdr));
1368 /* Strip L2CAP packet header and verify packet length */
1369 hdr = mtod(m, ng_l2cap_hdr_t *);
1370 m_adj(m, sizeof(*hdr));
1372 if (hdr->length != m->m_pkthdr.len) {
1373 NG_BTSOCKET_L2CAP_ERR(
1374 "%s: Bad L2CAP data packet length, len=%d, length=%d\n",
1375 __func__, m->m_pkthdr.len, hdr->length);
1380 * Now process packet. Two cases:
1382 * 1) Normal packet (cid != 2) then find connected socket and append
1383 * mbuf to the socket queue. Wakeup socket.
1385 * 2) Broadcast packet (cid == 2) then find all sockets that connected
1386 * to the given PSM and have SO_BROADCAST bit set and append mbuf
1387 * to the socket queue. Wakeup socket.
1390 NG_BTSOCKET_L2CAP_INFO(
1391 "%s: Received L2CAP data packet: src bdaddr=%x:%x:%x:%x:%x:%x, " \
1392 "dcid=%d, length=%d\n",
1394 rt->src.b[5], rt->src.b[4], rt->src.b[3],
1395 rt->src.b[2], rt->src.b[1], rt->src.b[0],
1396 hdr->dcid, hdr->length);
1398 if (hdr->dcid >= NG_L2CAP_FIRST_CID) {
1400 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
1402 /* Normal packet: find connected socket */
1403 pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, hdr->dcid);
1405 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1409 mtx_lock(&pcb->pcb_mtx);
1411 if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
1412 NG_BTSOCKET_L2CAP_ERR(
1413 "%s: No connected socket found, src bdaddr=%x:%x:%x:%x:%x:%x, dcid=%d, " \
1414 "state=%d\n", __func__,
1415 rt->src.b[5], rt->src.b[4], rt->src.b[3],
1416 rt->src.b[2], rt->src.b[1], rt->src.b[0],
1417 hdr->dcid, pcb->state);
1419 mtx_unlock(&pcb->pcb_mtx);
1420 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1424 /* Check packet size against socket's incoming MTU */
1425 if (hdr->length > pcb->imtu) {
1426 NG_BTSOCKET_L2CAP_ERR(
1427 "%s: L2CAP data packet too big, src bdaddr=%x:%x:%x:%x:%x:%x, " \
1428 "dcid=%d, length=%d, imtu=%d\n",
1430 rt->src.b[5], rt->src.b[4], rt->src.b[3],
1431 rt->src.b[2], rt->src.b[1], rt->src.b[0],
1432 hdr->dcid, hdr->length, pcb->imtu);
1434 mtx_unlock(&pcb->pcb_mtx);
1435 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1439 /* Check if we have enough space in socket receive queue */
1440 if (m->m_pkthdr.len > sbspace(&pcb->so->so_rcv)) {
1443 * This is really bad. Receive queue on socket does
1444 * not have enough space for the packet. We do not
1445 * have any other choice but drop the packet. L2CAP
1446 * does not provide any flow control.
1449 NG_BTSOCKET_L2CAP_ERR(
1450 "%s: Not enough space in socket receive queue. Dropping L2CAP data packet, " \
1451 "src bdaddr=%x:%x:%x:%x:%x:%x, dcid=%d, len=%d, space=%ld\n",
1453 rt->src.b[5], rt->src.b[4], rt->src.b[3],
1454 rt->src.b[2], rt->src.b[1], rt->src.b[0],
1455 hdr->dcid, m->m_pkthdr.len,
1456 sbspace(&pcb->so->so_rcv));
1458 mtx_unlock(&pcb->pcb_mtx);
1459 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1463 /* Append packet to the socket receive queue and wakeup */
1464 sbappendrecord(&pcb->so->so_rcv, m);
1469 mtx_unlock(&pcb->pcb_mtx);
1470 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1471 } else if (hdr->dcid == NG_L2CAP_CLT_CID) {
1472 /* Broadcast packet: give packet to all sockets */
1474 /* Check packet size against connectionless MTU */
1475 if (hdr->length > NG_L2CAP_MTU_DEFAULT) {
1476 NG_BTSOCKET_L2CAP_ERR(
1477 "%s: Connectionless L2CAP data packet too big, " \
1478 "src bdaddr=%x:%x:%x:%x:%x:%x, length=%d\n",
1480 rt->src.b[5], rt->src.b[4], rt->src.b[3],
1481 rt->src.b[2], rt->src.b[1], rt->src.b[0],
1486 /* Make sure we can access connectionless header */
1487 if (m->m_pkthdr.len < sizeof(*clt_hdr)) {
1488 NG_BTSOCKET_L2CAP_ERR(
1489 "%s: Can not get L2CAP connectionless packet header, " \
1490 "src bdaddr=%x:%x:%x:%x:%x:%x, length=%d\n",
1492 rt->src.b[5], rt->src.b[4], rt->src.b[3],
1493 rt->src.b[2], rt->src.b[1], rt->src.b[0],
1498 if (m->m_len < sizeof(*clt_hdr)) {
1499 m = m_pullup(m, sizeof(*clt_hdr));
1504 /* Strip connectionless header and deliver packet */
1505 clt_hdr = mtod(m, ng_l2cap_clt_hdr_t *);
1506 m_adj(m, sizeof(*clt_hdr));
1508 NG_BTSOCKET_L2CAP_INFO(
1509 "%s: Got L2CAP connectionless data packet, " \
1510 "src bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, length=%d\n",
1512 rt->src.b[5], rt->src.b[4], rt->src.b[3],
1513 rt->src.b[2], rt->src.b[1], rt->src.b[0],
1514 clt_hdr->psm, hdr->length);
1516 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
1518 LIST_FOREACH(pcb, &ng_btsocket_l2cap_sockets, next) {
1519 struct mbuf *copy = NULL;
1521 mtx_lock(&pcb->pcb_mtx);
1523 if (bcmp(&rt->src, &pcb->src, sizeof(pcb->src)) != 0 ||
1524 pcb->psm != clt_hdr->psm ||
1525 pcb->state != NG_BTSOCKET_L2CAP_OPEN ||
1526 (pcb->so->so_options & SO_BROADCAST) == 0 ||
1527 m->m_pkthdr.len > sbspace(&pcb->so->so_rcv))
1531 * Create a copy of the packet and append it to the
1532 * socket's queue. If m_dup() failed - no big deal
1533 * it is a broadcast traffic after all
1536 copy = m_dup(m, M_NOWAIT);
1538 sbappendrecord(&pcb->so->so_rcv, copy);
1542 mtx_unlock(&pcb->pcb_mtx);
1545 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1548 NG_FREE_M(m); /* checks for m != NULL */
1549 } /* ng_btsocket_l2cap_data_input */
1552 * L2CAP sockets default message input routine
1556 ng_btsocket_l2cap_default_msg_input(struct ng_mesg *msg, hook_p hook)
1558 switch (msg->header.cmd) {
1559 case NGM_L2CAP_NODE_HOOK_INFO: {
1560 ng_btsocket_l2cap_rtentry_t *rt = NULL;
1562 if (hook == NULL || msg->header.arglen != sizeof(bdaddr_t))
1565 if (bcmp(msg->data, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
1568 mtx_lock(&ng_btsocket_l2cap_rt_mtx);
1570 rt = (ng_btsocket_l2cap_rtentry_t *) NG_HOOK_PRIVATE(hook);
1572 rt = malloc(sizeof(*rt),
1573 M_NETGRAPH_BTSOCKET_L2CAP, M_NOWAIT|M_ZERO);
1575 mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
1579 LIST_INSERT_HEAD(&ng_btsocket_l2cap_rt, rt, next);
1581 NG_HOOK_SET_PRIVATE(hook, rt);
1584 bcopy(msg->data, &rt->src, sizeof(rt->src));
1587 mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
1589 NG_BTSOCKET_L2CAP_INFO(
1590 "%s: Updating hook \"%s\", src bdaddr=%x:%x:%x:%x:%x:%x\n",
1591 __func__, NG_HOOK_NAME(hook),
1592 rt->src.b[5], rt->src.b[4], rt->src.b[3],
1593 rt->src.b[2], rt->src.b[1], rt->src.b[0]);
1597 NG_BTSOCKET_L2CAP_WARN(
1598 "%s: Unknown message, cmd=%d\n", __func__, msg->header.cmd);
1602 NG_FREE_MSG(msg); /* Checks for msg != NULL */
1603 } /* ng_btsocket_l2cap_default_msg_input */
1606 * L2CAP sockets L2CA message input routine
1610 ng_btsocket_l2cap_l2ca_msg_input(struct ng_mesg *msg, hook_p hook)
1612 ng_btsocket_l2cap_rtentry_p rt = NULL;
1615 NG_BTSOCKET_L2CAP_ALERT(
1616 "%s: Invalid source hook for L2CA message\n", __func__);
1620 rt = (ng_btsocket_l2cap_rtentry_p) NG_HOOK_PRIVATE(hook);
1622 NG_BTSOCKET_L2CAP_ALERT(
1623 "%s: Could not find out source bdaddr for L2CA message\n", __func__);
1627 switch (msg->header.cmd) {
1628 case NGM_L2CAP_L2CA_CON: /* L2CA_Connect response */
1629 ng_btsocket_l2cap_process_l2ca_con_req_rsp(msg, rt);
1632 case NGM_L2CAP_L2CA_CON_RSP: /* L2CA_ConnectRsp response */
1633 ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(msg, rt);
1636 case NGM_L2CAP_L2CA_CON_IND: /* L2CA_Connect indicator */
1637 ng_btsocket_l2cap_process_l2ca_con_ind(msg, rt);
1640 case NGM_L2CAP_L2CA_CFG: /* L2CA_Config response */
1641 ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(msg, rt);
1644 case NGM_L2CAP_L2CA_CFG_RSP: /* L2CA_ConfigRsp response */
1645 ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(msg, rt);
1648 case NGM_L2CAP_L2CA_CFG_IND: /* L2CA_Config indicator */
1649 ng_btsocket_l2cap_process_l2ca_cfg_ind(msg, rt);
1652 case NGM_L2CAP_L2CA_DISCON: /* L2CA_Disconnect response */
1653 ng_btsocket_l2cap_process_l2ca_discon_rsp(msg, rt);
1656 case NGM_L2CAP_L2CA_DISCON_IND: /* L2CA_Disconnect indicator */
1657 ng_btsocket_l2cap_process_l2ca_discon_ind(msg, rt);
1660 case NGM_L2CAP_L2CA_WRITE: /* L2CA_Write response */
1661 ng_btsocket_l2cap_process_l2ca_write_rsp(msg, rt);
1664 /* XXX FIXME add other L2CA messages */
1667 NG_BTSOCKET_L2CAP_WARN(
1668 "%s: Unknown L2CA message, cmd=%d\n", __func__, msg->header.cmd);
1673 } /* ng_btsocket_l2cap_l2ca_msg_input */
1676 * L2CAP sockets input routine
1680 ng_btsocket_l2cap_input(void *context, int pending)
1686 mtx_lock(&ng_btsocket_l2cap_queue_mtx);
1687 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_queue, item);
1688 mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
1693 NGI_GET_HOOK(item, hook);
1694 if (hook != NULL && NG_HOOK_NOT_VALID(hook))
1697 switch(item->el_flags & NGQF_TYPE) {
1699 struct mbuf *m = NULL;
1702 ng_btsocket_l2cap_data_input(m, hook);
1706 struct ng_mesg *msg = NULL;
1708 NGI_GET_MSG(item, msg);
1710 switch (msg->header.cmd) {
1711 case NGM_L2CAP_L2CA_CON:
1712 case NGM_L2CAP_L2CA_CON_RSP:
1713 case NGM_L2CAP_L2CA_CON_IND:
1714 case NGM_L2CAP_L2CA_CFG:
1715 case NGM_L2CAP_L2CA_CFG_RSP:
1716 case NGM_L2CAP_L2CA_CFG_IND:
1717 case NGM_L2CAP_L2CA_DISCON:
1718 case NGM_L2CAP_L2CA_DISCON_IND:
1719 case NGM_L2CAP_L2CA_WRITE:
1720 /* XXX FIXME add other L2CA messages */
1721 ng_btsocket_l2cap_l2ca_msg_input(msg, hook);
1725 ng_btsocket_l2cap_default_msg_input(msg, hook);
1732 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
1737 NG_HOOK_UNREF(hook);
1741 } /* ng_btsocket_l2cap_input */
1744 * Route cleanup task. Gets scheduled when hook is disconnected. Here we
1745 * will find all sockets that use "invalid" hook and disconnect them.
1749 ng_btsocket_l2cap_rtclean(void *context, int pending)
1751 ng_btsocket_l2cap_pcb_p pcb = NULL, pcb_next = NULL;
1752 ng_btsocket_l2cap_rtentry_p rt = NULL;
1754 mtx_lock(&ng_btsocket_l2cap_rt_mtx);
1755 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
1758 * First disconnect all sockets that use "invalid" hook
1761 for (pcb = LIST_FIRST(&ng_btsocket_l2cap_sockets); pcb != NULL; ) {
1762 mtx_lock(&pcb->pcb_mtx);
1763 pcb_next = LIST_NEXT(pcb, next);
1765 if (pcb->rt != NULL &&
1766 pcb->rt->hook != NULL && NG_HOOK_NOT_VALID(pcb->rt->hook)) {
1767 if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
1768 ng_btsocket_l2cap_untimeout(pcb);
1770 pcb->so->so_error = ENETDOWN;
1771 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1772 soisdisconnected(pcb->so);
1779 mtx_unlock(&pcb->pcb_mtx);
1784 * Now cleanup routing table
1787 for (rt = LIST_FIRST(&ng_btsocket_l2cap_rt); rt != NULL; ) {
1788 ng_btsocket_l2cap_rtentry_p rt_next = LIST_NEXT(rt, next);
1790 if (rt->hook != NULL && NG_HOOK_NOT_VALID(rt->hook)) {
1791 LIST_REMOVE(rt, next);
1793 NG_HOOK_SET_PRIVATE(rt->hook, NULL);
1794 NG_HOOK_UNREF(rt->hook); /* Remove extra reference */
1796 bzero(rt, sizeof(*rt));
1797 free(rt, M_NETGRAPH_BTSOCKET_L2CAP);
1803 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
1804 mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
1805 } /* ng_btsocket_l2cap_rtclean */
1808 * Initialize everything
1812 ng_btsocket_l2cap_init(void)
1816 ng_btsocket_l2cap_node = NULL;
1817 ng_btsocket_l2cap_debug_level = NG_BTSOCKET_WARN_LEVEL;
1819 /* Register Netgraph node type */
1820 error = ng_newtype(&typestruct);
1822 NG_BTSOCKET_L2CAP_ALERT(
1823 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
1828 /* Create Netgrapg node */
1829 error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_node);
1831 NG_BTSOCKET_L2CAP_ALERT(
1832 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
1834 ng_btsocket_l2cap_node = NULL;
1839 error = ng_name_node(ng_btsocket_l2cap_node,
1840 NG_BTSOCKET_L2CAP_NODE_TYPE);
1842 NG_BTSOCKET_L2CAP_ALERT(
1843 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
1845 NG_NODE_UNREF(ng_btsocket_l2cap_node);
1846 ng_btsocket_l2cap_node = NULL;
1851 /* Create input queue */
1852 NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_queue, ifqmaxlen);
1853 mtx_init(&ng_btsocket_l2cap_queue_mtx,
1854 "btsocks_l2cap_queue_mtx", NULL, MTX_DEF);
1855 TASK_INIT(&ng_btsocket_l2cap_queue_task, 0,
1856 ng_btsocket_l2cap_input, NULL);
1858 /* Create list of sockets */
1859 LIST_INIT(&ng_btsocket_l2cap_sockets);
1860 mtx_init(&ng_btsocket_l2cap_sockets_mtx,
1861 "btsocks_l2cap_sockets_mtx", NULL, MTX_DEF);
1864 LIST_INIT(&ng_btsocket_l2cap_rt);
1865 mtx_init(&ng_btsocket_l2cap_rt_mtx,
1866 "btsocks_l2cap_rt_mtx", NULL, MTX_DEF);
1867 TASK_INIT(&ng_btsocket_l2cap_rt_task, 0,
1868 ng_btsocket_l2cap_rtclean, NULL);
1869 } /* ng_btsocket_l2cap_init */
1872 * Abort connection on socket
1876 ng_btsocket_l2cap_abort(struct socket *so)
1878 so->so_error = ECONNABORTED;
1880 (void)ng_btsocket_l2cap_disconnect(so);
1881 } /* ng_btsocket_l2cap_abort */
1884 ng_btsocket_l2cap_close(struct socket *so)
1887 (void)ng_btsocket_l2cap_disconnect(so);
1888 } /* ng_btsocket_l2cap_close */
1891 * Accept connection on socket. Nothing to do here, socket must be connected
1892 * and ready, so just return peer address and be done with it.
1896 ng_btsocket_l2cap_accept(struct socket *so, struct sockaddr **nam)
1898 if (ng_btsocket_l2cap_node == NULL)
1901 return (ng_btsocket_l2cap_peeraddr(so, nam));
1902 } /* ng_btsocket_l2cap_accept */
1905 * Create and attach new socket
1909 ng_btsocket_l2cap_attach(struct socket *so, int proto, struct thread *td)
1911 static u_int32_t token = 0;
1912 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
1915 /* Check socket and protocol */
1916 if (ng_btsocket_l2cap_node == NULL)
1917 return (EPROTONOSUPPORT);
1918 if (so->so_type != SOCK_SEQPACKET)
1919 return (ESOCKTNOSUPPORT);
1921 #if 0 /* XXX sonewconn() calls "pru_attach" with proto == 0 */
1923 if (proto != BLUETOOTH_PROTO_L2CAP)
1924 return (EPROTONOSUPPORT);
1930 /* Reserve send and receive space if it is not reserved yet */
1931 if ((so->so_snd.sb_hiwat == 0) || (so->so_rcv.sb_hiwat == 0)) {
1932 error = soreserve(so, NG_BTSOCKET_L2CAP_SENDSPACE,
1933 NG_BTSOCKET_L2CAP_RECVSPACE);
1938 /* Allocate the PCB */
1939 pcb = malloc(sizeof(*pcb),
1940 M_NETGRAPH_BTSOCKET_L2CAP, M_NOWAIT | M_ZERO);
1944 /* Link the PCB and the socket */
1945 so->so_pcb = (caddr_t) pcb;
1947 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
1949 /* Initialize PCB */
1950 pcb->imtu = pcb->omtu = NG_L2CAP_MTU_DEFAULT;
1953 pcb->iflow.flags = 0x0;
1954 pcb->iflow.service_type = NG_HCI_SERVICE_TYPE_BEST_EFFORT;
1955 pcb->iflow.token_rate = 0xffffffff; /* maximum */
1956 pcb->iflow.token_bucket_size = 0xffffffff; /* maximum */
1957 pcb->iflow.peak_bandwidth = 0x00000000; /* maximum */
1958 pcb->iflow.latency = 0xffffffff; /* don't care */
1959 pcb->iflow.delay_variation = 0xffffffff; /* don't care */
1961 bcopy(&pcb->iflow, &pcb->oflow, sizeof(pcb->oflow));
1963 pcb->flush_timo = NG_L2CAP_FLUSH_TIMO_DEFAULT;
1964 pcb->link_timo = NG_L2CAP_LINK_TIMO_DEFAULT;
1966 callout_handle_init(&pcb->timo);
1969 * XXX Mark PCB mutex as DUPOK to prevent "duplicated lock of
1970 * the same type" message. When accepting new L2CAP connection
1971 * ng_btsocket_l2cap_process_l2ca_con_ind() holds both PCB mutexes
1972 * for "old" (accepting) PCB and "new" (created) PCB.
1975 mtx_init(&pcb->pcb_mtx, "btsocks_l2cap_pcb_mtx", NULL,
1979 * Add the PCB to the list
1981 * XXX FIXME VERY IMPORTANT!
1983 * This is totally FUBAR. We could get here in two cases:
1985 * 1) When user calls socket()
1986 * 2) When we need to accept new incomming connection and call
1989 * In the first case we must acquire ng_btsocket_l2cap_sockets_mtx.
1990 * In the second case we hold ng_btsocket_l2cap_sockets_mtx already.
1991 * So we now need to distinguish between these cases. From reading
1992 * /sys/kern/uipc_socket.c we can find out that sonewconn() calls
1993 * pru_attach with proto == 0 and td == NULL. For now use this fact
1994 * to figure out if we were called from socket() or from sonewconn().
1998 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
2000 mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
2002 /* Set PCB token. Use ng_btsocket_l2cap_sockets_mtx for protection */
2008 LIST_INSERT_HEAD(&ng_btsocket_l2cap_sockets, pcb, next);
2011 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
2014 } /* ng_btsocket_l2cap_attach */
2021 ng_btsocket_l2cap_bind(struct socket *so, struct sockaddr *nam,
2024 ng_btsocket_l2cap_pcb_t *pcb = NULL;
2025 struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam;
2028 if (ng_btsocket_l2cap_node == NULL)
2031 /* Verify address */
2034 if (sa->l2cap_family != AF_BLUETOOTH)
2035 return (EAFNOSUPPORT);
2036 if (sa->l2cap_len != sizeof(*sa))
2039 psm = le16toh(sa->l2cap_psm);
2042 * Check if other socket has this address already (look for exact
2043 * match PSM and bdaddr) and assign socket address if it's available.
2045 * Note: socket can be bound to ANY PSM (zero) thus allowing several
2046 * channels with the same PSM between the same pair of BD_ADDR'es.
2049 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
2051 LIST_FOREACH(pcb, &ng_btsocket_l2cap_sockets, next)
2052 if (psm != 0 && psm == pcb->psm &&
2053 bcmp(&pcb->src, &sa->l2cap_bdaddr, sizeof(bdaddr_t)) == 0)
2057 /* Set socket address */
2058 pcb = so2l2cap_pcb(so);
2060 bcopy(&sa->l2cap_bdaddr, &pcb->src, sizeof(pcb->src));
2067 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
2070 } /* ng_btsocket_l2cap_bind */
2077 ng_btsocket_l2cap_connect(struct socket *so, struct sockaddr *nam,
2080 ng_btsocket_l2cap_pcb_t *pcb = so2l2cap_pcb(so);
2081 struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam;
2082 ng_btsocket_l2cap_rtentry_t *rt = NULL;
2083 int have_src, error = 0;
2088 if (ng_btsocket_l2cap_node == NULL)
2090 if (pcb->state == NG_BTSOCKET_L2CAP_CONNECTING)
2091 return (EINPROGRESS);
2093 /* Verify address */
2096 if (sa->l2cap_family != AF_BLUETOOTH)
2097 return (EAFNOSUPPORT);
2098 if (sa->l2cap_len != sizeof(*sa))
2100 if (sa->l2cap_psm == 0 ||
2101 bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
2102 return (EDESTADDRREQ);
2103 if (pcb->psm != 0 && pcb->psm != le16toh(sa->l2cap_psm))
2107 * Routing. Socket should be bound to some source address. The source
2108 * address can be ANY. Destination address must be set and it must not
2109 * be ANY. If source address is ANY then find first rtentry that has
2113 mtx_lock(&ng_btsocket_l2cap_rt_mtx);
2114 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
2115 mtx_lock(&pcb->pcb_mtx);
2117 /* Send destination address and PSM */
2118 bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst));
2119 pcb->psm = le16toh(sa->l2cap_psm);
2122 have_src = bcmp(&pcb->src, NG_HCI_BDADDR_ANY, sizeof(pcb->src));
2124 LIST_FOREACH(rt, &ng_btsocket_l2cap_rt, next) {
2125 if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
2128 /* Match src and dst */
2130 if (bcmp(&pcb->src, &rt->src, sizeof(rt->src)) == 0)
2133 if (bcmp(&pcb->dst, &rt->src, sizeof(rt->src)) != 0)
2142 bcopy(&rt->src, &pcb->src, sizeof(pcb->src));
2144 error = EHOSTUNREACH;
2147 * Send L2CA_Connect request
2151 error = ng_btsocket_l2cap_send_l2ca_con_req(pcb);
2153 pcb->flags |= NG_BTSOCKET_L2CAP_CLIENT;
2154 pcb->state = NG_BTSOCKET_L2CAP_CONNECTING;
2155 soisconnecting(pcb->so);
2157 ng_btsocket_l2cap_timeout(pcb);
2161 mtx_unlock(&pcb->pcb_mtx);
2162 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
2163 mtx_unlock(&ng_btsocket_l2cap_rt_mtx);
2166 } /* ng_btsocket_l2cap_connect */
2169 * Process ioctl's calls on socket
2173 ng_btsocket_l2cap_control(struct socket *so, u_long cmd, caddr_t data,
2174 struct ifnet *ifp, struct thread *td)
2177 } /* ng_btsocket_l2cap_control */
2180 * Process getsockopt/setsockopt system calls
2184 ng_btsocket_l2cap_ctloutput(struct socket *so, struct sockopt *sopt)
2186 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2188 ng_l2cap_cfg_opt_val_t v;
2192 if (ng_btsocket_l2cap_node == NULL)
2195 if (sopt->sopt_level != SOL_L2CAP)
2198 mtx_lock(&pcb->pcb_mtx);
2200 switch (sopt->sopt_dir) {
2202 switch (sopt->sopt_name) {
2203 case SO_L2CAP_IMTU: /* get incoming MTU */
2204 error = sooptcopyout(sopt, &pcb->imtu,
2208 case SO_L2CAP_OMTU: /* get outgoing (peer incoming) MTU */
2209 error = sooptcopyout(sopt, &pcb->omtu,
2213 case SO_L2CAP_IFLOW: /* get incoming flow spec. */
2214 error = sooptcopyout(sopt, &pcb->iflow,
2215 sizeof(pcb->iflow));
2218 case SO_L2CAP_OFLOW: /* get outgoing flow spec. */
2219 error = sooptcopyout(sopt, &pcb->oflow,
2220 sizeof(pcb->oflow));
2223 case SO_L2CAP_FLUSH: /* get flush timeout */
2224 error = sooptcopyout(sopt, &pcb->flush_timo,
2225 sizeof(pcb->flush_timo));
2229 error = ENOPROTOOPT;
2237 * We do not allow to change these parameters while socket is
2238 * connected or we are in the process of creating a connection.
2239 * May be this should indicate re-configuration of the open
2243 if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
2248 switch (sopt->sopt_name) {
2249 case SO_L2CAP_IMTU: /* set incoming MTU */
2250 error = sooptcopyin(sopt, &v, sizeof(v), sizeof(v.mtu));
2255 case SO_L2CAP_OFLOW: /* set outgoing flow spec. */
2256 error = sooptcopyin(sopt, &v, sizeof(v),sizeof(v.flow));
2258 bcopy(&v.flow, &pcb->oflow, sizeof(pcb->oflow));
2261 case SO_L2CAP_FLUSH: /* set flush timeout */
2262 error = sooptcopyin(sopt, &v, sizeof(v),
2263 sizeof(v.flush_timo));
2265 pcb->flush_timo = v.flush_timo;
2269 error = ENOPROTOOPT;
2279 mtx_unlock(&pcb->pcb_mtx);
2282 } /* ng_btsocket_l2cap_ctloutput */
2285 * Detach and destroy socket
2289 ng_btsocket_l2cap_detach(struct socket *so)
2291 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2293 KASSERT(pcb != NULL, ("ng_btsocket_l2cap_detach: pcb == NULL"));
2295 if (ng_btsocket_l2cap_node == NULL)
2298 mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
2299 mtx_lock(&pcb->pcb_mtx);
2301 /* XXX what to do with pending request? */
2302 if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
2303 ng_btsocket_l2cap_untimeout(pcb);
2305 if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED &&
2306 pcb->state != NG_BTSOCKET_L2CAP_DISCONNECTING)
2307 /* Send disconnect request with "zero" token */
2308 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
2310 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
2312 LIST_REMOVE(pcb, next);
2314 mtx_unlock(&pcb->pcb_mtx);
2315 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
2317 mtx_destroy(&pcb->pcb_mtx);
2318 bzero(pcb, sizeof(*pcb));
2319 free(pcb, M_NETGRAPH_BTSOCKET_L2CAP);
2321 soisdisconnected(so);
2323 } /* ng_btsocket_l2cap_detach */
2330 ng_btsocket_l2cap_disconnect(struct socket *so)
2332 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2337 if (ng_btsocket_l2cap_node == NULL)
2340 mtx_lock(&pcb->pcb_mtx);
2342 if (pcb->state == NG_BTSOCKET_L2CAP_DISCONNECTING) {
2343 mtx_unlock(&pcb->pcb_mtx);
2344 return (EINPROGRESS);
2347 if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) {
2348 /* XXX FIXME what to do with pending request? */
2349 if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO)
2350 ng_btsocket_l2cap_untimeout(pcb);
2352 error = ng_btsocket_l2cap_send_l2ca_discon_req(pcb->token, pcb);
2354 pcb->state = NG_BTSOCKET_L2CAP_DISCONNECTING;
2355 soisdisconnecting(so);
2357 ng_btsocket_l2cap_timeout(pcb);
2360 /* XXX FIXME what to do if error != 0 */
2363 mtx_unlock(&pcb->pcb_mtx);
2366 } /* ng_btsocket_l2cap_disconnect */
2373 ng_btsocket_l2cap_listen(struct socket *so, int backlog, struct thread *td)
2375 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2379 error = solisten_proto_check(so);
2386 if (ng_btsocket_l2cap_node == NULL) {
2390 if (pcb->psm == 0) {
2391 error = EADDRNOTAVAIL;
2394 solisten_proto(so, backlog);
2398 } /* ng_btsocket_listen */
2405 ng_btsocket_l2cap_peeraddr(struct socket *so, struct sockaddr **nam)
2407 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2408 struct sockaddr_l2cap sa;
2412 if (ng_btsocket_l2cap_node == NULL)
2415 bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
2416 sa.l2cap_psm = htole16(pcb->psm);
2417 sa.l2cap_len = sizeof(sa);
2418 sa.l2cap_family = AF_BLUETOOTH;
2420 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
2422 return ((*nam == NULL)? ENOMEM : 0);
2423 } /* ng_btsocket_l2cap_peeraddr */
2426 * Send data to socket
2430 ng_btsocket_l2cap_send(struct socket *so, int flags, struct mbuf *m,
2431 struct sockaddr *nam, struct mbuf *control, struct thread *td)
2433 ng_btsocket_l2cap_pcb_t *pcb = so2l2cap_pcb(so);
2436 if (ng_btsocket_l2cap_node == NULL) {
2441 /* Check socket and input */
2442 if (pcb == NULL || m == NULL || control != NULL) {
2447 mtx_lock(&pcb->pcb_mtx);
2449 /* Make sure socket is connected */
2450 if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) {
2451 mtx_unlock(&pcb->pcb_mtx);
2457 if (pcb->rt == NULL ||
2458 pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook)) {
2459 mtx_unlock(&pcb->pcb_mtx);
2464 /* Check packet size agains outgoing (peer's incoming) MTU) */
2465 if (m->m_pkthdr.len > pcb->omtu) {
2466 NG_BTSOCKET_L2CAP_ERR(
2467 "%s: Packet too big, len=%d, omtu=%d\n", __func__, m->m_pkthdr.len, pcb->omtu);
2469 mtx_unlock(&pcb->pcb_mtx);
2475 * First put packet on socket send queue. Then check if we have
2476 * pending timeout. If we do not have timeout then we must send
2477 * packet and schedule timeout. Otherwise do nothing and wait for
2481 sbappendrecord(&pcb->so->so_snd, m);
2484 if (!(pcb->flags & NG_BTSOCKET_L2CAP_TIMO)) {
2485 error = ng_btsocket_l2cap_send2(pcb);
2487 ng_btsocket_l2cap_timeout(pcb);
2489 sbdroprecord(&pcb->so->so_snd); /* XXX */
2492 mtx_unlock(&pcb->pcb_mtx);
2494 NG_FREE_M(m); /* checks for != NULL */
2498 } /* ng_btsocket_l2cap_send */
2501 * Send first packet in the socket queue to the L2CAP layer
2505 ng_btsocket_l2cap_send2(ng_btsocket_l2cap_pcb_p pcb)
2507 struct mbuf *m = NULL;
2508 ng_l2cap_l2ca_hdr_t *hdr = NULL;
2511 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
2513 if (pcb->so->so_snd.sb_cc == 0)
2514 return (EINVAL); /* XXX */
2516 m = m_dup(pcb->so->so_snd.sb_mb, M_NOWAIT);
2520 /* Create L2CA packet header */
2521 M_PREPEND(m, sizeof(*hdr), M_NOWAIT);
2523 if (m->m_len < sizeof(*hdr))
2524 m = m_pullup(m, sizeof(*hdr));
2527 NG_BTSOCKET_L2CAP_ERR(
2528 "%s: Failed to create L2CA packet header\n", __func__);
2533 hdr = mtod(m, ng_l2cap_l2ca_hdr_t *);
2534 hdr->token = pcb->token;
2535 hdr->length = m->m_pkthdr.len - sizeof(*hdr);
2536 hdr->lcid = pcb->cid;
2538 NG_BTSOCKET_L2CAP_INFO(
2539 "%s: Sending packet: len=%d, length=%d, lcid=%d, token=%d, state=%d\n",
2540 __func__, m->m_pkthdr.len, hdr->length, hdr->lcid,
2541 hdr->token, pcb->state);
2544 * If we got here than we have successfuly creates new L2CAP
2545 * data packet and now we can send it to the L2CAP layer
2548 NG_SEND_DATA_ONLY(error, pcb->rt->hook, m);
2551 } /* ng_btsocket_l2cap_send2 */
2554 * Get socket address
2558 ng_btsocket_l2cap_sockaddr(struct socket *so, struct sockaddr **nam)
2560 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so);
2561 struct sockaddr_l2cap sa;
2565 if (ng_btsocket_l2cap_node == NULL)
2568 bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
2569 sa.l2cap_psm = htole16(pcb->psm);
2570 sa.l2cap_len = sizeof(sa);
2571 sa.l2cap_family = AF_BLUETOOTH;
2573 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
2575 return ((*nam == NULL)? ENOMEM : 0);
2576 } /* ng_btsocket_l2cap_sockaddr */
2578 /*****************************************************************************
2579 *****************************************************************************
2581 *****************************************************************************
2582 *****************************************************************************/
2585 * Look for the socket that listens on given PSM and bdaddr. Returns exact or
2586 * close match (if any). Caller must hold ng_btsocket_l2cap_sockets_mtx.
2589 static ng_btsocket_l2cap_pcb_p
2590 ng_btsocket_l2cap_pcb_by_addr(bdaddr_p bdaddr, int psm)
2592 ng_btsocket_l2cap_pcb_p p = NULL, p1 = NULL;
2594 mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
2596 LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next) {
2597 if (p->so == NULL || !(p->so->so_options & SO_ACCEPTCONN) ||
2601 if (bcmp(&p->src, bdaddr, sizeof(p->src)) == 0)
2604 if (bcmp(&p->src, NG_HCI_BDADDR_ANY, sizeof(p->src)) == 0)
2608 return ((p != NULL)? p : p1);
2609 } /* ng_btsocket_l2cap_pcb_by_addr */
2612 * Look for the socket that has given token.
2613 * Caller must hold ng_btsocket_l2cap_sockets_mtx.
2616 static ng_btsocket_l2cap_pcb_p
2617 ng_btsocket_l2cap_pcb_by_token(u_int32_t token)
2619 ng_btsocket_l2cap_pcb_p p = NULL;
2624 mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
2626 LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next)
2627 if (p->token == token)
2631 } /* ng_btsocket_l2cap_pcb_by_token */
2634 * Look for the socket that assigned to given source address and channel ID.
2635 * Caller must hold ng_btsocket_l2cap_sockets_mtx
2638 static ng_btsocket_l2cap_pcb_p
2639 ng_btsocket_l2cap_pcb_by_cid(bdaddr_p src, int cid)
2641 ng_btsocket_l2cap_pcb_p p = NULL;
2643 mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED);
2645 LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next)
2646 if (p->cid == cid && bcmp(src, &p->src, sizeof(p->src)) == 0)
2650 } /* ng_btsocket_l2cap_pcb_by_cid */
2653 * Set timeout on socket
2657 ng_btsocket_l2cap_timeout(ng_btsocket_l2cap_pcb_p pcb)
2659 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
2661 if (!(pcb->flags & NG_BTSOCKET_L2CAP_TIMO)) {
2662 pcb->flags |= NG_BTSOCKET_L2CAP_TIMO;
2663 pcb->timo = timeout(ng_btsocket_l2cap_process_timeout, pcb,
2664 bluetooth_l2cap_ertx_timeout());
2667 ("%s: Duplicated socket timeout?!\n", __func__));
2668 } /* ng_btsocket_l2cap_timeout */
2671 * Unset timeout on socket
2675 ng_btsocket_l2cap_untimeout(ng_btsocket_l2cap_pcb_p pcb)
2677 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
2679 if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO) {
2680 untimeout(ng_btsocket_l2cap_process_timeout, pcb, pcb->timo);
2681 pcb->flags &= ~NG_BTSOCKET_L2CAP_TIMO;
2684 ("%s: No socket timeout?!\n", __func__));
2685 } /* ng_btsocket_l2cap_untimeout */
2688 * Process timeout on socket
2692 ng_btsocket_l2cap_process_timeout(void *xpcb)
2694 ng_btsocket_l2cap_pcb_p pcb = (ng_btsocket_l2cap_pcb_p) xpcb;
2696 mtx_lock(&pcb->pcb_mtx);
2698 pcb->flags &= ~NG_BTSOCKET_L2CAP_TIMO;
2699 pcb->so->so_error = ETIMEDOUT;
2701 switch (pcb->state) {
2702 case NG_BTSOCKET_L2CAP_CONNECTING:
2703 case NG_BTSOCKET_L2CAP_CONFIGURING:
2704 /* Send disconnect request with "zero" token */
2706 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
2708 /* ... and close the socket */
2709 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
2710 soisdisconnected(pcb->so);
2713 case NG_BTSOCKET_L2CAP_OPEN:
2714 /* Send timeout - drop packet and wakeup sender */
2715 sbdroprecord(&pcb->so->so_snd);
2719 case NG_BTSOCKET_L2CAP_DISCONNECTING:
2720 /* Disconnect timeout - disconnect the socket anyway */
2721 pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
2722 soisdisconnected(pcb->so);
2726 NG_BTSOCKET_L2CAP_ERR(
2727 "%s: Invalid socket state=%d\n", __func__, pcb->state);
2731 mtx_unlock(&pcb->pcb_mtx);
2732 } /* ng_btsocket_l2cap_process_timeout */
2735 * Translate HCI/L2CAP error code into "errno" code
2736 * XXX Note: Some L2CAP and HCI error codes have the same value, but
2741 ng_btsocket_l2cap_result2errno(int result)
2744 case 0x00: /* No error */
2747 case 0x01: /* Unknown HCI command */
2750 case 0x02: /* No connection */
2753 case 0x03: /* Hardware failure */
2756 case 0x04: /* Page timeout */
2759 case 0x05: /* Authentication failure */
2760 case 0x06: /* Key missing */
2761 case 0x18: /* Pairing not allowed */
2762 case 0x21: /* Role change not allowed */
2763 case 0x24: /* LMP PSU not allowed */
2764 case 0x25: /* Encryption mode not acceptable */
2765 case 0x26: /* Unit key used */
2768 case 0x07: /* Memory full */
2771 case 0x08: /* Connection timeout */
2772 case 0x10: /* Host timeout */
2773 case 0x22: /* LMP response timeout */
2774 case 0xee: /* HCI timeout */
2775 case 0xeeee: /* L2CAP timeout */
2778 case 0x09: /* Max number of connections */
2779 case 0x0a: /* Max number of SCO connections to a unit */
2782 case 0x0b: /* ACL connection already exists */
2785 case 0x0c: /* Command disallowed */
2788 case 0x0d: /* Host rejected due to limited resources */
2789 case 0x0e: /* Host rejected due to securiity reasons */
2790 case 0x0f: /* Host rejected due to remote unit is a personal unit */
2791 case 0x1b: /* SCO offset rejected */
2792 case 0x1c: /* SCO interval rejected */
2793 case 0x1d: /* SCO air mode rejected */
2794 return (ECONNREFUSED);
2796 case 0x11: /* Unsupported feature or parameter value */
2797 case 0x19: /* Unknown LMP PDU */
2798 case 0x1a: /* Unsupported remote feature */
2799 case 0x20: /* Unsupported LMP parameter value */
2800 case 0x27: /* QoS is not supported */
2801 case 0x29: /* Paring with unit key not supported */
2802 return (EOPNOTSUPP);
2804 case 0x12: /* Invalid HCI command parameter */
2805 case 0x1e: /* Invalid LMP parameters */
2808 case 0x13: /* Other end terminated connection: User ended connection */
2809 case 0x14: /* Other end terminated connection: Low resources */
2810 case 0x15: /* Other end terminated connection: About to power off */
2811 return (ECONNRESET);
2813 case 0x16: /* Connection terminated by local host */
2814 return (ECONNABORTED);
2816 #if 0 /* XXX not yet */
2817 case 0x17: /* Repeated attempts */
2818 case 0x1f: /* Unspecified error */
2819 case 0x23: /* LMP error transaction collision */
2820 case 0x28: /* Instant passed */
2825 } /* ng_btsocket_l2cap_result2errno */