2 * ng_btsocket_l2cap_raw.c
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_raw.c,v 1.12 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/errno.h>
39 #include <sys/filedesc.h>
40 #include <sys/ioccom.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #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>
53 #include <netgraph/ng_message.h>
54 #include <netgraph/netgraph.h>
55 #include <netgraph/bluetooth/include/ng_bluetooth.h>
56 #include <netgraph/bluetooth/include/ng_hci.h>
57 #include <netgraph/bluetooth/include/ng_l2cap.h>
58 #include <netgraph/bluetooth/include/ng_btsocket.h>
59 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
62 #ifdef NG_SEPARATE_MALLOC
63 MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_L2CAP_RAW, "netgraph_btsocks_l2cap_raw",
64 "Netgraph Bluetooth raw L2CAP sockets");
66 #define M_NETGRAPH_BTSOCKET_L2CAP_RAW M_NETGRAPH
67 #endif /* NG_SEPARATE_MALLOC */
69 /* Netgraph node methods */
70 static ng_constructor_t ng_btsocket_l2cap_raw_node_constructor;
71 static ng_rcvmsg_t ng_btsocket_l2cap_raw_node_rcvmsg;
72 static ng_shutdown_t ng_btsocket_l2cap_raw_node_shutdown;
73 static ng_newhook_t ng_btsocket_l2cap_raw_node_newhook;
74 static ng_connect_t ng_btsocket_l2cap_raw_node_connect;
75 static ng_rcvdata_t ng_btsocket_l2cap_raw_node_rcvdata;
76 static ng_disconnect_t ng_btsocket_l2cap_raw_node_disconnect;
78 static void ng_btsocket_l2cap_raw_input (void *, int);
79 static void ng_btsocket_l2cap_raw_rtclean (void *, int);
80 static void ng_btsocket_l2cap_raw_get_token (u_int32_t *);
82 static int ng_btsocket_l2cap_raw_send_ngmsg
83 (hook_p, int, void *, int);
84 static int ng_btsocket_l2cap_raw_send_sync_ngmsg
85 (ng_btsocket_l2cap_raw_pcb_p, int, void *, int);
87 #define ng_btsocket_l2cap_raw_wakeup_input_task() \
88 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_raw_queue_task)
90 #define ng_btsocket_l2cap_raw_wakeup_route_task() \
91 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_raw_rt_task)
93 /* Netgraph type descriptor */
94 static struct ng_type typestruct = {
95 .version = NG_ABI_VERSION,
96 .name = NG_BTSOCKET_L2CAP_RAW_NODE_TYPE,
97 .constructor = ng_btsocket_l2cap_raw_node_constructor,
98 .rcvmsg = ng_btsocket_l2cap_raw_node_rcvmsg,
99 .shutdown = ng_btsocket_l2cap_raw_node_shutdown,
100 .newhook = ng_btsocket_l2cap_raw_node_newhook,
101 .connect = ng_btsocket_l2cap_raw_node_connect,
102 .rcvdata = ng_btsocket_l2cap_raw_node_rcvdata,
103 .disconnect = ng_btsocket_l2cap_raw_node_disconnect,
107 extern int ifqmaxlen;
108 static u_int32_t ng_btsocket_l2cap_raw_debug_level;
109 static u_int32_t ng_btsocket_l2cap_raw_ioctl_timeout;
110 static node_p ng_btsocket_l2cap_raw_node;
111 static struct ng_bt_itemq ng_btsocket_l2cap_raw_queue;
112 static struct mtx ng_btsocket_l2cap_raw_queue_mtx;
113 static struct task ng_btsocket_l2cap_raw_queue_task;
114 static LIST_HEAD(, ng_btsocket_l2cap_raw_pcb) ng_btsocket_l2cap_raw_sockets;
115 static struct mtx ng_btsocket_l2cap_raw_sockets_mtx;
116 static u_int32_t ng_btsocket_l2cap_raw_token;
117 static struct mtx ng_btsocket_l2cap_raw_token_mtx;
118 static LIST_HEAD(, ng_btsocket_l2cap_rtentry) ng_btsocket_l2cap_raw_rt;
119 static struct mtx ng_btsocket_l2cap_raw_rt_mtx;
120 static struct task ng_btsocket_l2cap_raw_rt_task;
121 static struct timeval ng_btsocket_l2cap_raw_lasttime;
122 static int ng_btsocket_l2cap_raw_curpps;
125 SYSCTL_DECL(_net_bluetooth_l2cap_sockets);
126 SYSCTL_NODE(_net_bluetooth_l2cap_sockets, OID_AUTO, raw, CTLFLAG_RW,
127 0, "Bluetooth raw L2CAP sockets family");
128 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, debug_level,
130 &ng_btsocket_l2cap_raw_debug_level, NG_BTSOCKET_WARN_LEVEL,
131 "Bluetooth raw L2CAP sockets debug level");
132 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, ioctl_timeout,
134 &ng_btsocket_l2cap_raw_ioctl_timeout, 5,
135 "Bluetooth raw L2CAP sockets ioctl timeout");
136 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_len,
138 &ng_btsocket_l2cap_raw_queue.len, 0,
139 "Bluetooth raw L2CAP sockets input queue length");
140 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_maxlen,
142 &ng_btsocket_l2cap_raw_queue.maxlen, 0,
143 "Bluetooth raw L2CAP sockets input queue max. length");
144 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_drops,
146 &ng_btsocket_l2cap_raw_queue.drops, 0,
147 "Bluetooth raw L2CAP sockets input queue drops");
150 #define NG_BTSOCKET_L2CAP_RAW_INFO \
151 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
152 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
155 #define NG_BTSOCKET_L2CAP_RAW_WARN \
156 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
157 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
160 #define NG_BTSOCKET_L2CAP_RAW_ERR \
161 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
162 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
165 #define NG_BTSOCKET_L2CAP_RAW_ALERT \
166 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
167 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
170 /*****************************************************************************
171 *****************************************************************************
172 ** Netgraph node interface
173 *****************************************************************************
174 *****************************************************************************/
177 * Netgraph node constructor. Do not allow to create node of this type.
181 ng_btsocket_l2cap_raw_node_constructor(node_p node)
184 } /* ng_btsocket_l2cap_raw_node_constructor */
187 * Do local shutdown processing. Let old node go and create new fresh one.
191 ng_btsocket_l2cap_raw_node_shutdown(node_p node)
197 /* Create new node */
198 error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_raw_node);
200 NG_BTSOCKET_L2CAP_RAW_ALERT(
201 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
203 ng_btsocket_l2cap_raw_node = NULL;
208 error = ng_name_node(ng_btsocket_l2cap_raw_node,
209 NG_BTSOCKET_L2CAP_RAW_NODE_TYPE);
211 NG_BTSOCKET_L2CAP_RAW_ALERT(
212 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
214 NG_NODE_UNREF(ng_btsocket_l2cap_raw_node);
215 ng_btsocket_l2cap_raw_node = NULL;
221 } /* ng_btsocket_l2cap_raw_node_shutdown */
224 * We allow any hook to be connected to the node.
228 ng_btsocket_l2cap_raw_node_newhook(node_p node, hook_p hook, char const *name)
231 } /* ng_btsocket_l2cap_raw_node_newhook */
234 * Just say "YEP, that's OK by me!"
238 ng_btsocket_l2cap_raw_node_connect(hook_p hook)
240 NG_HOOK_SET_PRIVATE(hook, NULL);
241 NG_HOOK_REF(hook); /* Keep extra reference to the hook */
244 } /* ng_btsocket_l2cap_raw_node_connect */
247 * Hook disconnection. Schedule route cleanup task
251 ng_btsocket_l2cap_raw_node_disconnect(hook_p hook)
254 * If hook has private information than we must have this hook in
255 * the routing table and must schedule cleaning for the routing table.
256 * Otherwise hook was connected but we never got "hook_info" message,
257 * so we have never added this hook to the routing table and it save
261 if (NG_HOOK_PRIVATE(hook) != NULL)
262 return (ng_btsocket_l2cap_raw_wakeup_route_task());
264 NG_HOOK_UNREF(hook); /* Remove extra reference */
267 } /* ng_btsocket_l2cap_raw_node_disconnect */
270 * Process incoming messages
274 ng_btsocket_l2cap_raw_node_rcvmsg(node_p node, item_p item, hook_p hook)
276 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
279 if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) {
282 * NGM_L2CAP_NODE_HOOK_INFO is special message initiated by
283 * L2CAP layer. Ignore all other messages if they are not
284 * replies or token is zero
287 if (msg->header.cmd != NGM_L2CAP_NODE_HOOK_INFO) {
288 if (msg->header.token == 0 ||
289 !(msg->header.flags & NGF_RESP)) {
295 mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx);
296 if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_raw_queue)) {
297 NG_BTSOCKET_L2CAP_RAW_ERR(
298 "%s: Input queue is full\n", __func__);
300 NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_raw_queue);
306 NGI_SET_HOOK(item, hook);
309 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_raw_queue, item);
310 error = ng_btsocket_l2cap_raw_wakeup_input_task();
312 mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx);
319 } /* ng_btsocket_l2cap_raw_node_rcvmsg */
322 * Receive data on a hook
326 ng_btsocket_l2cap_raw_node_rcvdata(hook_p hook, item_p item)
331 } /* ng_btsocket_l2cap_raw_node_rcvdata */
333 /*****************************************************************************
334 *****************************************************************************
336 *****************************************************************************
337 *****************************************************************************/
340 * L2CAP sockets input routine
344 ng_btsocket_l2cap_raw_input(void *context, int pending)
348 struct ng_mesg *msg = NULL;
351 mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx);
352 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_raw_queue, item);
353 mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx);
358 KASSERT((item->el_flags & NGQF_TYPE) == NGQF_MESG,
359 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
361 NGI_GET_MSG(item, msg);
362 NGI_GET_HOOK(item, hook);
365 switch (msg->header.cmd) {
366 case NGM_L2CAP_NODE_HOOK_INFO: {
367 ng_btsocket_l2cap_rtentry_t *rt = NULL;
369 if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
370 msg->header.arglen != sizeof(bdaddr_t))
373 if (bcmp(msg->data, NG_HCI_BDADDR_ANY,
374 sizeof(bdaddr_t)) == 0)
377 rt = (ng_btsocket_l2cap_rtentry_t *)
378 NG_HOOK_PRIVATE(hook);
380 rt = malloc(sizeof(*rt),
381 M_NETGRAPH_BTSOCKET_L2CAP_RAW,
386 NG_HOOK_SET_PRIVATE(hook, rt);
388 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
390 LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_rt,
393 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
395 bcopy(msg->data, &rt->src, sizeof(rt->src));
398 NG_BTSOCKET_L2CAP_RAW_INFO(
399 "%s: Updating hook \"%s\", src bdaddr=%x:%x:%x:%x:%x:%x\n",
400 __func__, NG_HOOK_NAME(hook),
401 rt->src.b[5], rt->src.b[4], rt->src.b[3],
402 rt->src.b[2], rt->src.b[1], rt->src.b[0]);
404 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
407 case NGM_L2CAP_NODE_GET_FLAGS:
408 case NGM_L2CAP_NODE_GET_DEBUG:
409 case NGM_L2CAP_NODE_GET_CON_LIST:
410 case NGM_L2CAP_NODE_GET_CHAN_LIST:
411 case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO:
412 case NGM_L2CAP_L2CA_PING:
413 case NGM_L2CAP_L2CA_GET_INFO: {
414 ng_btsocket_l2cap_raw_pcb_p pcb = NULL;
416 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
418 LIST_FOREACH(pcb,&ng_btsocket_l2cap_raw_sockets,next) {
419 mtx_lock(&pcb->pcb_mtx);
421 if (pcb->token == msg->header.token) {
425 mtx_unlock(&pcb->pcb_mtx);
429 mtx_unlock(&pcb->pcb_mtx);
432 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
436 NG_BTSOCKET_L2CAP_RAW_WARN(
437 "%s: Unknown message, cmd=%d\n", __func__, msg->header.cmd);
442 NG_HOOK_UNREF(hook); /* remove extra reference */
444 NG_FREE_MSG(msg); /* Checks for msg != NULL */
446 } /* ng_btsocket_l2cap_raw_input */
449 * Route cleanup task. Gets scheduled when hook is disconnected. Here we
450 * will find all sockets that use "invalid" hook and disconnect them.
454 ng_btsocket_l2cap_raw_rtclean(void *context, int pending)
456 ng_btsocket_l2cap_raw_pcb_p pcb = NULL;
457 ng_btsocket_l2cap_rtentry_p rt = NULL;
460 * First disconnect all sockets that use "invalid" hook
463 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
465 LIST_FOREACH(pcb, &ng_btsocket_l2cap_raw_sockets, next) {
466 mtx_lock(&pcb->pcb_mtx);
468 if (pcb->rt != NULL &&
469 pcb->rt->hook != NULL && NG_HOOK_NOT_VALID(pcb->rt->hook)) {
470 if (pcb->so != NULL &&
471 pcb->so->so_state & SS_ISCONNECTED)
472 soisdisconnected(pcb->so);
477 mtx_unlock(&pcb->pcb_mtx);
480 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
483 * Now cleanup routing table
486 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
488 for (rt = LIST_FIRST(&ng_btsocket_l2cap_raw_rt); rt != NULL; ) {
489 ng_btsocket_l2cap_rtentry_p rt_next = LIST_NEXT(rt, next);
491 if (rt->hook != NULL && NG_HOOK_NOT_VALID(rt->hook)) {
492 LIST_REMOVE(rt, next);
494 NG_HOOK_SET_PRIVATE(rt->hook, NULL);
495 NG_HOOK_UNREF(rt->hook); /* Remove extra reference */
497 bzero(rt, sizeof(*rt));
498 free(rt, M_NETGRAPH_BTSOCKET_L2CAP_RAW);
504 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
505 } /* ng_btsocket_l2cap_raw_rtclean */
508 * Initialize everything
512 ng_btsocket_l2cap_raw_init(void)
516 ng_btsocket_l2cap_raw_node = NULL;
517 ng_btsocket_l2cap_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
518 ng_btsocket_l2cap_raw_ioctl_timeout = 5;
520 /* Register Netgraph node type */
521 error = ng_newtype(&typestruct);
523 NG_BTSOCKET_L2CAP_RAW_ALERT(
524 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
529 /* Create Netgrapg node */
530 error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_raw_node);
532 NG_BTSOCKET_L2CAP_RAW_ALERT(
533 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
535 ng_btsocket_l2cap_raw_node = NULL;
540 error = ng_name_node(ng_btsocket_l2cap_raw_node,
541 NG_BTSOCKET_L2CAP_RAW_NODE_TYPE);
543 NG_BTSOCKET_L2CAP_RAW_ALERT(
544 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
546 NG_NODE_UNREF(ng_btsocket_l2cap_raw_node);
547 ng_btsocket_l2cap_raw_node = NULL;
552 /* Create input queue */
553 NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_raw_queue, ifqmaxlen);
554 mtx_init(&ng_btsocket_l2cap_raw_queue_mtx,
555 "btsocks_l2cap_raw_queue_mtx", NULL, MTX_DEF);
556 TASK_INIT(&ng_btsocket_l2cap_raw_queue_task, 0,
557 ng_btsocket_l2cap_raw_input, NULL);
559 /* Create list of sockets */
560 LIST_INIT(&ng_btsocket_l2cap_raw_sockets);
561 mtx_init(&ng_btsocket_l2cap_raw_sockets_mtx,
562 "btsocks_l2cap_raw_sockets_mtx", NULL, MTX_DEF);
565 ng_btsocket_l2cap_raw_token = 0;
566 mtx_init(&ng_btsocket_l2cap_raw_token_mtx,
567 "btsocks_l2cap_raw_token_mtx", NULL, MTX_DEF);
570 LIST_INIT(&ng_btsocket_l2cap_raw_rt);
571 mtx_init(&ng_btsocket_l2cap_raw_rt_mtx,
572 "btsocks_l2cap_raw_rt_mtx", NULL, MTX_DEF);
573 TASK_INIT(&ng_btsocket_l2cap_raw_rt_task, 0,
574 ng_btsocket_l2cap_raw_rtclean, NULL);
575 } /* ng_btsocket_l2cap_raw_init */
578 * Abort connection on socket
582 ng_btsocket_l2cap_raw_abort(struct socket *so)
585 (void)ng_btsocket_l2cap_raw_disconnect(so);
586 } /* ng_btsocket_l2cap_raw_abort */
589 ng_btsocket_l2cap_raw_close(struct socket *so)
592 (void)ng_btsocket_l2cap_raw_disconnect(so);
593 } /* ng_btsocket_l2cap_raw_close */
596 * Create and attach new socket
600 ng_btsocket_l2cap_raw_attach(struct socket *so, int proto, struct thread *td)
602 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
608 if (ng_btsocket_l2cap_raw_node == NULL)
609 return (EPROTONOSUPPORT);
610 if (so->so_type != SOCK_RAW)
611 return (ESOCKTNOSUPPORT);
613 /* Reserve send and receive space if it is not reserved yet */
614 error = soreserve(so, NG_BTSOCKET_L2CAP_RAW_SENDSPACE,
615 NG_BTSOCKET_L2CAP_RAW_RECVSPACE);
619 /* Allocate the PCB */
620 pcb = malloc(sizeof(*pcb),
621 M_NETGRAPH_BTSOCKET_L2CAP_RAW, M_NOWAIT|M_ZERO);
625 /* Link the PCB and the socket */
626 so->so_pcb = (caddr_t) pcb;
629 if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
630 pcb->flags |= NG_BTSOCKET_L2CAP_RAW_PRIVILEGED;
632 mtx_init(&pcb->pcb_mtx, "btsocks_l2cap_raw_pcb_mtx", NULL, MTX_DEF);
634 /* Add the PCB to the list */
635 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
636 LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_sockets, pcb, next);
637 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
640 } /* ng_btsocket_l2cap_raw_attach */
647 ng_btsocket_l2cap_raw_bind(struct socket *so, struct sockaddr *nam,
650 ng_btsocket_l2cap_raw_pcb_t *pcb = so2l2cap_raw_pcb(so);
651 struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam;
652 ng_btsocket_l2cap_rtentry_t *rt = NULL;
656 if (ng_btsocket_l2cap_raw_node == NULL)
661 if (sa->l2cap_family != AF_BLUETOOTH)
662 return (EAFNOSUPPORT);
663 if (sa->l2cap_len != sizeof(*sa))
666 if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY,
667 sizeof(sa->l2cap_bdaddr)) != 0) {
668 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
670 LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) {
671 if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
674 if (bcmp(&sa->l2cap_bdaddr, &rt->src,
675 sizeof(rt->src)) == 0)
679 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
686 mtx_lock(&pcb->pcb_mtx);
687 bcopy(&sa->l2cap_bdaddr, &pcb->src, sizeof(pcb->src));
689 mtx_unlock(&pcb->pcb_mtx);
692 } /* ng_btsocket_l2cap_raw_bind */
699 ng_btsocket_l2cap_raw_connect(struct socket *so, struct sockaddr *nam,
702 ng_btsocket_l2cap_raw_pcb_t *pcb = so2l2cap_raw_pcb(so);
703 struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam;
704 ng_btsocket_l2cap_rtentry_t *rt = NULL;
709 if (ng_btsocket_l2cap_raw_node == NULL)
714 if (sa->l2cap_family != AF_BLUETOOTH)
715 return (EAFNOSUPPORT);
716 if (sa->l2cap_len != sizeof(*sa))
718 if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
721 mtx_lock(&pcb->pcb_mtx);
723 bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst));
725 if (bcmp(&pcb->src, &pcb->dst, sizeof(pcb->src)) == 0) {
726 mtx_unlock(&pcb->pcb_mtx);
728 return (EADDRNOTAVAIL);
732 * If there is route already - use it
735 if (pcb->rt != NULL) {
737 mtx_unlock(&pcb->pcb_mtx);
743 * Find the first hook that does not match specified destination address
746 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
748 LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) {
749 if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
752 if (bcmp(&pcb->dst, &rt->src, sizeof(rt->src)) != 0)
760 bcopy(&rt->src, &pcb->src, sizeof(pcb->src));
766 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
767 mtx_unlock(&pcb->pcb_mtx);
770 } /* ng_btsocket_l2cap_raw_connect */
773 * Process ioctl's calls on socket
777 ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
778 struct ifnet *ifp, struct thread *td)
780 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
781 struct ng_mesg *msg = NULL;
786 if (ng_btsocket_l2cap_raw_node == NULL)
789 mtx_lock(&pcb->pcb_mtx);
791 /* Check if we route info */
792 if (pcb->rt == NULL) {
793 mtx_unlock(&pcb->pcb_mtx);
794 return (EHOSTUNREACH);
797 /* Check if we have pending ioctl() */
798 if (pcb->token != 0) {
799 mtx_unlock(&pcb->pcb_mtx);
804 case SIOC_L2CAP_NODE_GET_FLAGS: {
805 struct ng_btsocket_l2cap_raw_node_flags *p =
806 (struct ng_btsocket_l2cap_raw_node_flags *) data;
808 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
809 NGM_L2CAP_NODE_GET_FLAGS,
810 &p->flags, sizeof(p->flags));
813 case SIOC_L2CAP_NODE_GET_DEBUG: {
814 struct ng_btsocket_l2cap_raw_node_debug *p =
815 (struct ng_btsocket_l2cap_raw_node_debug *) data;
817 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
818 NGM_L2CAP_NODE_GET_DEBUG,
819 &p->debug, sizeof(p->debug));
822 case SIOC_L2CAP_NODE_SET_DEBUG: {
823 struct ng_btsocket_l2cap_raw_node_debug *p =
824 (struct ng_btsocket_l2cap_raw_node_debug *) data;
826 if (pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)
827 error = ng_btsocket_l2cap_raw_send_ngmsg(pcb->rt->hook,
828 NGM_L2CAP_NODE_SET_DEBUG,
829 &p->debug, sizeof(p->debug));
834 case SIOC_L2CAP_NODE_GET_CON_LIST: {
835 struct ng_btsocket_l2cap_raw_con_list *p =
836 (struct ng_btsocket_l2cap_raw_con_list *) data;
837 ng_l2cap_node_con_list_ep *p1 = NULL;
838 ng_l2cap_node_con_ep *p2 = NULL;
840 if (p->num_connections == 0 ||
841 p->num_connections > NG_L2CAP_MAX_CON_NUM ||
842 p->connections == NULL) {
847 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_NODE_GET_CON_LIST,
853 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
854 pcb->token = msg->header.token;
857 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
864 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
865 ng_btsocket_l2cap_raw_ioctl_timeout * hz);
871 if (pcb->msg != NULL &&
872 pcb->msg->header.cmd == NGM_L2CAP_NODE_GET_CON_LIST) {
873 /* Return data back to user space */
874 p1 = (ng_l2cap_node_con_list_ep *)(pcb->msg->data);
875 p2 = (ng_l2cap_node_con_ep *)(p1 + 1);
877 p->num_connections = min(p->num_connections,
878 p1->num_connections);
879 if (p->num_connections > 0)
880 error = copyout((caddr_t) p2,
881 (caddr_t) p->connections,
882 p->num_connections * sizeof(*p2));
886 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
889 case SIOC_L2CAP_NODE_GET_CHAN_LIST: {
890 struct ng_btsocket_l2cap_raw_chan_list *p =
891 (struct ng_btsocket_l2cap_raw_chan_list *) data;
892 ng_l2cap_node_chan_list_ep *p1 = NULL;
893 ng_l2cap_node_chan_ep *p2 = NULL;
895 if (p->num_channels == 0 ||
896 p->num_channels > NG_L2CAP_MAX_CHAN_NUM ||
897 p->channels == NULL) {
902 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
903 NGM_L2CAP_NODE_GET_CHAN_LIST, 0, M_NOWAIT);
908 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
909 pcb->token = msg->header.token;
912 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
919 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
920 ng_btsocket_l2cap_raw_ioctl_timeout * hz);
926 if (pcb->msg != NULL &&
927 pcb->msg->header.cmd == NGM_L2CAP_NODE_GET_CHAN_LIST) {
928 /* Return data back to user space */
929 p1 = (ng_l2cap_node_chan_list_ep *)(pcb->msg->data);
930 p2 = (ng_l2cap_node_chan_ep *)(p1 + 1);
932 p->num_channels = min(p->num_channels,
934 if (p->num_channels > 0)
935 error = copyout((caddr_t) p2,
936 (caddr_t) p->channels,
937 p->num_channels * sizeof(*p2));
941 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
944 case SIOC_L2CAP_L2CA_PING: {
945 struct ng_btsocket_l2cap_raw_ping *p =
946 (struct ng_btsocket_l2cap_raw_ping *) data;
947 ng_l2cap_l2ca_ping_ip *ip = NULL;
948 ng_l2cap_l2ca_ping_op *op = NULL;
950 if ((p->echo_size != 0 && p->echo_data == NULL) ||
951 p->echo_size > NG_L2CAP_MAX_ECHO_SIZE) {
956 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
957 NGM_L2CAP_L2CA_PING, sizeof(*ip) + p->echo_size,
963 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
964 pcb->token = msg->header.token;
967 ip = (ng_l2cap_l2ca_ping_ip *)(msg->data);
968 bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
969 ip->echo_size = p->echo_size;
971 if (ip->echo_size > 0) {
972 error = copyin(p->echo_data, ip + 1, p->echo_size);
980 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
987 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
988 bluetooth_l2cap_rtx_timeout());
994 if (pcb->msg != NULL &&
995 pcb->msg->header.cmd == NGM_L2CAP_L2CA_PING) {
996 /* Return data back to the user space */
997 op = (ng_l2cap_l2ca_ping_op *)(pcb->msg->data);
998 p->result = op->result;
999 p->echo_size = min(p->echo_size, op->echo_size);
1001 if (p->echo_size > 0)
1002 error = copyout(op + 1, p->echo_data,
1007 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1010 case SIOC_L2CAP_L2CA_GET_INFO: {
1011 struct ng_btsocket_l2cap_raw_get_info *p =
1012 (struct ng_btsocket_l2cap_raw_get_info *) data;
1013 ng_l2cap_l2ca_get_info_ip *ip = NULL;
1014 ng_l2cap_l2ca_get_info_op *op = NULL;
1016 if (!(pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)) {
1021 if (p->info_size != 0 && p->info_data == NULL) {
1026 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
1027 NGM_L2CAP_L2CA_GET_INFO, sizeof(*ip) + p->info_size,
1033 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
1034 pcb->token = msg->header.token;
1037 ip = (ng_l2cap_l2ca_get_info_ip *)(msg->data);
1038 bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
1039 ip->info_type = p->info_type;
1041 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
1048 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
1049 bluetooth_l2cap_rtx_timeout());
1055 if (pcb->msg != NULL &&
1056 pcb->msg->header.cmd == NGM_L2CAP_L2CA_GET_INFO) {
1057 /* Return data back to the user space */
1058 op = (ng_l2cap_l2ca_get_info_op *)(pcb->msg->data);
1059 p->result = op->result;
1060 p->info_size = min(p->info_size, op->info_size);
1062 if (p->info_size > 0)
1063 error = copyout(op + 1, p->info_data,
1068 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1071 case SIOC_L2CAP_NODE_GET_AUTO_DISCON_TIMO: {
1072 struct ng_btsocket_l2cap_raw_auto_discon_timo *p =
1073 (struct ng_btsocket_l2cap_raw_auto_discon_timo *) data;
1075 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
1076 NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO,
1077 &p->timeout, sizeof(p->timeout));
1080 case SIOC_L2CAP_NODE_SET_AUTO_DISCON_TIMO: {
1081 struct ng_btsocket_l2cap_raw_auto_discon_timo *p =
1082 (struct ng_btsocket_l2cap_raw_auto_discon_timo *) data;
1084 if (pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)
1085 error = ng_btsocket_l2cap_raw_send_ngmsg(pcb->rt->hook,
1086 NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO,
1087 &p->timeout, sizeof(p->timeout));
1097 mtx_unlock(&pcb->pcb_mtx);
1100 } /* ng_btsocket_l2cap_raw_control */
1103 * Detach and destroy socket
1107 ng_btsocket_l2cap_raw_detach(struct socket *so)
1109 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
1111 KASSERT(pcb != NULL, ("nt_btsocket_l2cap_raw_detach: pcb == NULL"));
1112 if (ng_btsocket_l2cap_raw_node == NULL)
1115 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
1116 mtx_lock(&pcb->pcb_mtx);
1118 LIST_REMOVE(pcb, next);
1120 mtx_unlock(&pcb->pcb_mtx);
1121 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
1123 mtx_destroy(&pcb->pcb_mtx);
1125 bzero(pcb, sizeof(*pcb));
1126 free(pcb, M_NETGRAPH_BTSOCKET_L2CAP_RAW);
1129 } /* ng_btsocket_l2cap_raw_detach */
1136 ng_btsocket_l2cap_raw_disconnect(struct socket *so)
1138 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
1142 if (ng_btsocket_l2cap_raw_node == NULL)
1145 mtx_lock(&pcb->pcb_mtx);
1147 soisdisconnected(so);
1148 mtx_unlock(&pcb->pcb_mtx);
1151 } /* ng_btsocket_l2cap_raw_disconnect */
1158 ng_btsocket_l2cap_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1160 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
1161 struct sockaddr_l2cap sa;
1165 if (ng_btsocket_l2cap_raw_node == NULL)
1168 mtx_lock(&pcb->pcb_mtx);
1169 bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
1170 mtx_unlock(&pcb->pcb_mtx);
1173 sa.l2cap_len = sizeof(sa);
1174 sa.l2cap_family = AF_BLUETOOTH;
1176 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1178 return ((*nam == NULL)? ENOMEM : 0);
1179 } /* ng_btsocket_l2cap_raw_peeraddr */
1182 * Send data to socket
1186 ng_btsocket_l2cap_raw_send(struct socket *so, int flags, struct mbuf *m,
1187 struct sockaddr *nam, struct mbuf *control, struct thread *td)
1189 NG_FREE_M(m); /* Checks for m != NULL */
1192 return (EOPNOTSUPP);
1193 } /* ng_btsocket_l2cap_raw_send */
1196 * Get socket address
1200 ng_btsocket_l2cap_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1202 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
1203 struct sockaddr_l2cap sa;
1207 if (ng_btsocket_l2cap_raw_node == NULL)
1210 mtx_lock(&pcb->pcb_mtx);
1211 bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
1212 mtx_unlock(&pcb->pcb_mtx);
1215 sa.l2cap_len = sizeof(sa);
1216 sa.l2cap_family = AF_BLUETOOTH;
1218 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1220 return ((*nam == NULL)? ENOMEM : 0);
1221 } /* ng_btsocket_l2cap_raw_sockaddr */
1228 ng_btsocket_l2cap_raw_get_token(u_int32_t *token)
1230 mtx_lock(&ng_btsocket_l2cap_raw_token_mtx);
1232 if (++ ng_btsocket_l2cap_raw_token == 0)
1233 ng_btsocket_l2cap_raw_token = 1;
1235 *token = ng_btsocket_l2cap_raw_token;
1237 mtx_unlock(&ng_btsocket_l2cap_raw_token_mtx);
1238 } /* ng_btsocket_l2cap_raw_get_token */
1241 * Send Netgraph message to the node - do not expect reply
1245 ng_btsocket_l2cap_raw_send_ngmsg(hook_p hook, int cmd, void *arg, int arglen)
1247 struct ng_mesg *msg = NULL;
1250 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, arglen, M_NOWAIT);
1254 if (arg != NULL && arglen > 0)
1255 bcopy(arg, msg->data, arglen);
1257 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg, hook, 0);
1260 } /* ng_btsocket_l2cap_raw_send_ngmsg */
1263 * Send Netgraph message to the node (no data) and wait for reply
1267 ng_btsocket_l2cap_raw_send_sync_ngmsg(ng_btsocket_l2cap_raw_pcb_p pcb,
1268 int cmd, void *rsp, int rsplen)
1270 struct ng_mesg *msg = NULL;
1273 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1275 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, 0, M_NOWAIT);
1279 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
1280 pcb->token = msg->header.token;
1283 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
1290 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
1291 ng_btsocket_l2cap_raw_ioctl_timeout * hz);
1297 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
1298 bcopy(pcb->msg->data, rsp, rsplen);
1302 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1305 } /* ng_btsocket_l2cap_raw_send_sync_ngmsg */