2 * ng_btsocket_l2cap_raw.c
6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: ng_btsocket_l2cap_raw.c,v 1.12 2003/09/14 23:29:06 max Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bitstring.h>
39 #include <sys/domain.h>
40 #include <sys/errno.h>
41 #include <sys/filedesc.h>
42 #include <sys/ioccom.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/mutex.h>
49 #include <sys/protosw.h>
50 #include <sys/queue.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/taskqueue.h>
58 #include <netgraph/ng_message.h>
59 #include <netgraph/netgraph.h>
60 #include <netgraph/bluetooth/include/ng_bluetooth.h>
61 #include <netgraph/bluetooth/include/ng_hci.h>
62 #include <netgraph/bluetooth/include/ng_l2cap.h>
63 #include <netgraph/bluetooth/include/ng_btsocket.h>
64 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
67 #ifdef NG_SEPARATE_MALLOC
68 static MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_L2CAP_RAW,
69 "netgraph_btsocks_l2cap_raw", "Netgraph Bluetooth raw L2CAP sockets");
71 #define M_NETGRAPH_BTSOCKET_L2CAP_RAW M_NETGRAPH
72 #endif /* NG_SEPARATE_MALLOC */
74 /* Netgraph node methods */
75 static ng_constructor_t ng_btsocket_l2cap_raw_node_constructor;
76 static ng_rcvmsg_t ng_btsocket_l2cap_raw_node_rcvmsg;
77 static ng_shutdown_t ng_btsocket_l2cap_raw_node_shutdown;
78 static ng_newhook_t ng_btsocket_l2cap_raw_node_newhook;
79 static ng_connect_t ng_btsocket_l2cap_raw_node_connect;
80 static ng_rcvdata_t ng_btsocket_l2cap_raw_node_rcvdata;
81 static ng_disconnect_t ng_btsocket_l2cap_raw_node_disconnect;
83 static void ng_btsocket_l2cap_raw_input (void *, int);
84 static void ng_btsocket_l2cap_raw_rtclean (void *, int);
85 static void ng_btsocket_l2cap_raw_get_token (u_int32_t *);
87 static int ng_btsocket_l2cap_raw_send_ngmsg
88 (hook_p, int, void *, int);
89 static int ng_btsocket_l2cap_raw_send_sync_ngmsg
90 (ng_btsocket_l2cap_raw_pcb_p, int, void *, int);
92 #define ng_btsocket_l2cap_raw_wakeup_input_task() \
93 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_raw_queue_task)
95 #define ng_btsocket_l2cap_raw_wakeup_route_task() \
96 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_raw_rt_task)
98 /* Netgraph type descriptor */
99 static struct ng_type typestruct = {
100 .version = NG_ABI_VERSION,
101 .name = NG_BTSOCKET_L2CAP_RAW_NODE_TYPE,
102 .constructor = ng_btsocket_l2cap_raw_node_constructor,
103 .rcvmsg = ng_btsocket_l2cap_raw_node_rcvmsg,
104 .shutdown = ng_btsocket_l2cap_raw_node_shutdown,
105 .newhook = ng_btsocket_l2cap_raw_node_newhook,
106 .connect = ng_btsocket_l2cap_raw_node_connect,
107 .rcvdata = ng_btsocket_l2cap_raw_node_rcvdata,
108 .disconnect = ng_btsocket_l2cap_raw_node_disconnect,
112 extern int ifqmaxlen;
113 static u_int32_t ng_btsocket_l2cap_raw_debug_level;
114 static u_int32_t ng_btsocket_l2cap_raw_ioctl_timeout;
115 static node_p ng_btsocket_l2cap_raw_node;
116 static struct ng_bt_itemq ng_btsocket_l2cap_raw_queue;
117 static struct mtx ng_btsocket_l2cap_raw_queue_mtx;
118 static struct task ng_btsocket_l2cap_raw_queue_task;
119 static LIST_HEAD(, ng_btsocket_l2cap_raw_pcb) ng_btsocket_l2cap_raw_sockets;
120 static struct mtx ng_btsocket_l2cap_raw_sockets_mtx;
121 static u_int32_t ng_btsocket_l2cap_raw_token;
122 static struct mtx ng_btsocket_l2cap_raw_token_mtx;
123 static LIST_HEAD(, ng_btsocket_l2cap_rtentry) ng_btsocket_l2cap_raw_rt;
124 static struct mtx ng_btsocket_l2cap_raw_rt_mtx;
125 static struct task ng_btsocket_l2cap_raw_rt_task;
126 static struct timeval ng_btsocket_l2cap_raw_lasttime;
127 static int ng_btsocket_l2cap_raw_curpps;
130 SYSCTL_DECL(_net_bluetooth_l2cap_sockets);
131 static SYSCTL_NODE(_net_bluetooth_l2cap_sockets, OID_AUTO, raw, CTLFLAG_RW,
132 0, "Bluetooth raw L2CAP sockets family");
133 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, debug_level,
135 &ng_btsocket_l2cap_raw_debug_level, NG_BTSOCKET_WARN_LEVEL,
136 "Bluetooth raw L2CAP sockets debug level");
137 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, ioctl_timeout,
139 &ng_btsocket_l2cap_raw_ioctl_timeout, 5,
140 "Bluetooth raw L2CAP sockets ioctl timeout");
141 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_len,
143 &ng_btsocket_l2cap_raw_queue.len, 0,
144 "Bluetooth raw L2CAP sockets input queue length");
145 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_maxlen,
147 &ng_btsocket_l2cap_raw_queue.maxlen, 0,
148 "Bluetooth raw L2CAP sockets input queue max. length");
149 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_drops,
151 &ng_btsocket_l2cap_raw_queue.drops, 0,
152 "Bluetooth raw L2CAP sockets input queue drops");
155 #define NG_BTSOCKET_L2CAP_RAW_INFO \
156 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
157 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
160 #define NG_BTSOCKET_L2CAP_RAW_WARN \
161 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
162 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
165 #define NG_BTSOCKET_L2CAP_RAW_ERR \
166 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
167 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
170 #define NG_BTSOCKET_L2CAP_RAW_ALERT \
171 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
172 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
175 /*****************************************************************************
176 *****************************************************************************
177 ** Netgraph node interface
178 *****************************************************************************
179 *****************************************************************************/
182 * Netgraph node constructor. Do not allow to create node of this type.
186 ng_btsocket_l2cap_raw_node_constructor(node_p node)
189 } /* ng_btsocket_l2cap_raw_node_constructor */
192 * Do local shutdown processing. Let old node go and create new fresh one.
196 ng_btsocket_l2cap_raw_node_shutdown(node_p node)
202 /* Create new node */
203 error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_raw_node);
205 NG_BTSOCKET_L2CAP_RAW_ALERT(
206 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
208 ng_btsocket_l2cap_raw_node = NULL;
213 error = ng_name_node(ng_btsocket_l2cap_raw_node,
214 NG_BTSOCKET_L2CAP_RAW_NODE_TYPE);
216 NG_BTSOCKET_L2CAP_RAW_ALERT(
217 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
219 NG_NODE_UNREF(ng_btsocket_l2cap_raw_node);
220 ng_btsocket_l2cap_raw_node = NULL;
226 } /* ng_btsocket_l2cap_raw_node_shutdown */
229 * We allow any hook to be connected to the node.
233 ng_btsocket_l2cap_raw_node_newhook(node_p node, hook_p hook, char const *name)
236 } /* ng_btsocket_l2cap_raw_node_newhook */
239 * Just say "YEP, that's OK by me!"
243 ng_btsocket_l2cap_raw_node_connect(hook_p hook)
245 NG_HOOK_SET_PRIVATE(hook, NULL);
246 NG_HOOK_REF(hook); /* Keep extra reference to the hook */
249 } /* ng_btsocket_l2cap_raw_node_connect */
252 * Hook disconnection. Schedule route cleanup task
256 ng_btsocket_l2cap_raw_node_disconnect(hook_p hook)
259 * If hook has private information than we must have this hook in
260 * the routing table and must schedule cleaning for the routing table.
261 * Otherwise hook was connected but we never got "hook_info" message,
262 * so we have never added this hook to the routing table and it save
266 if (NG_HOOK_PRIVATE(hook) != NULL)
267 return (ng_btsocket_l2cap_raw_wakeup_route_task());
269 NG_HOOK_UNREF(hook); /* Remove extra reference */
272 } /* ng_btsocket_l2cap_raw_node_disconnect */
275 * Process incoming messages
279 ng_btsocket_l2cap_raw_node_rcvmsg(node_p node, item_p item, hook_p hook)
281 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
284 if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) {
287 * NGM_L2CAP_NODE_HOOK_INFO is special message initiated by
288 * L2CAP layer. Ignore all other messages if they are not
289 * replies or token is zero
292 if (msg->header.cmd != NGM_L2CAP_NODE_HOOK_INFO) {
293 if (msg->header.token == 0 ||
294 !(msg->header.flags & NGF_RESP)) {
300 mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx);
301 if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_raw_queue)) {
302 NG_BTSOCKET_L2CAP_RAW_ERR(
303 "%s: Input queue is full\n", __func__);
305 NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_raw_queue);
311 NGI_SET_HOOK(item, hook);
314 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_raw_queue, item);
315 error = ng_btsocket_l2cap_raw_wakeup_input_task();
317 mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx);
324 } /* ng_btsocket_l2cap_raw_node_rcvmsg */
327 * Receive data on a hook
331 ng_btsocket_l2cap_raw_node_rcvdata(hook_p hook, item_p item)
336 } /* ng_btsocket_l2cap_raw_node_rcvdata */
338 /*****************************************************************************
339 *****************************************************************************
341 *****************************************************************************
342 *****************************************************************************/
345 * L2CAP sockets input routine
349 ng_btsocket_l2cap_raw_input(void *context, int pending)
353 struct ng_mesg *msg = NULL;
356 mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx);
357 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_raw_queue, item);
358 mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx);
363 KASSERT((item->el_flags & NGQF_TYPE) == NGQF_MESG,
364 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
366 NGI_GET_MSG(item, msg);
367 NGI_GET_HOOK(item, hook);
370 switch (msg->header.cmd) {
371 case NGM_L2CAP_NODE_HOOK_INFO: {
372 ng_btsocket_l2cap_rtentry_t *rt = NULL;
374 if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
375 msg->header.arglen != sizeof(bdaddr_t))
378 if (bcmp(msg->data, NG_HCI_BDADDR_ANY,
379 sizeof(bdaddr_t)) == 0)
382 rt = (ng_btsocket_l2cap_rtentry_t *)
383 NG_HOOK_PRIVATE(hook);
385 rt = malloc(sizeof(*rt),
386 M_NETGRAPH_BTSOCKET_L2CAP_RAW,
391 NG_HOOK_SET_PRIVATE(hook, rt);
393 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
395 LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_rt,
398 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
400 bcopy(msg->data, &rt->src, sizeof(rt->src));
403 NG_BTSOCKET_L2CAP_RAW_INFO(
404 "%s: Updating hook \"%s\", src bdaddr=%x:%x:%x:%x:%x:%x\n",
405 __func__, NG_HOOK_NAME(hook),
406 rt->src.b[5], rt->src.b[4], rt->src.b[3],
407 rt->src.b[2], rt->src.b[1], rt->src.b[0]);
409 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
412 case NGM_L2CAP_NODE_GET_FLAGS:
413 case NGM_L2CAP_NODE_GET_DEBUG:
414 case NGM_L2CAP_NODE_GET_CON_LIST:
415 case NGM_L2CAP_NODE_GET_CHAN_LIST:
416 case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO:
417 case NGM_L2CAP_L2CA_PING:
418 case NGM_L2CAP_L2CA_GET_INFO: {
419 ng_btsocket_l2cap_raw_pcb_p pcb = NULL;
421 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
423 LIST_FOREACH(pcb,&ng_btsocket_l2cap_raw_sockets,next) {
424 mtx_lock(&pcb->pcb_mtx);
426 if (pcb->token == msg->header.token) {
430 mtx_unlock(&pcb->pcb_mtx);
434 mtx_unlock(&pcb->pcb_mtx);
437 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
441 NG_BTSOCKET_L2CAP_RAW_WARN(
442 "%s: Unknown message, cmd=%d\n", __func__, msg->header.cmd);
447 NG_HOOK_UNREF(hook); /* remove extra reference */
449 NG_FREE_MSG(msg); /* Checks for msg != NULL */
451 } /* ng_btsocket_l2cap_raw_input */
454 * Route cleanup task. Gets scheduled when hook is disconnected. Here we
455 * will find all sockets that use "invalid" hook and disconnect them.
459 ng_btsocket_l2cap_raw_rtclean(void *context, int pending)
461 ng_btsocket_l2cap_raw_pcb_p pcb = NULL;
462 ng_btsocket_l2cap_rtentry_p rt = NULL;
465 * First disconnect all sockets that use "invalid" hook
468 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
470 LIST_FOREACH(pcb, &ng_btsocket_l2cap_raw_sockets, next) {
471 mtx_lock(&pcb->pcb_mtx);
473 if (pcb->rt != NULL &&
474 pcb->rt->hook != NULL && NG_HOOK_NOT_VALID(pcb->rt->hook)) {
475 if (pcb->so != NULL &&
476 pcb->so->so_state & SS_ISCONNECTED)
477 soisdisconnected(pcb->so);
482 mtx_unlock(&pcb->pcb_mtx);
485 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
488 * Now cleanup routing table
491 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
493 for (rt = LIST_FIRST(&ng_btsocket_l2cap_raw_rt); rt != NULL; ) {
494 ng_btsocket_l2cap_rtentry_p rt_next = LIST_NEXT(rt, next);
496 if (rt->hook != NULL && NG_HOOK_NOT_VALID(rt->hook)) {
497 LIST_REMOVE(rt, next);
499 NG_HOOK_SET_PRIVATE(rt->hook, NULL);
500 NG_HOOK_UNREF(rt->hook); /* Remove extra reference */
502 bzero(rt, sizeof(*rt));
503 free(rt, M_NETGRAPH_BTSOCKET_L2CAP_RAW);
509 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
510 } /* ng_btsocket_l2cap_raw_rtclean */
513 * Initialize everything
517 ng_btsocket_l2cap_raw_init(void)
521 /* Skip initialization of globals for non-default instances. */
522 if (!IS_DEFAULT_VNET(curvnet))
525 ng_btsocket_l2cap_raw_node = NULL;
526 ng_btsocket_l2cap_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
527 ng_btsocket_l2cap_raw_ioctl_timeout = 5;
529 /* Register Netgraph node type */
530 error = ng_newtype(&typestruct);
532 NG_BTSOCKET_L2CAP_RAW_ALERT(
533 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
538 /* Create Netgrapg node */
539 error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_raw_node);
541 NG_BTSOCKET_L2CAP_RAW_ALERT(
542 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
544 ng_btsocket_l2cap_raw_node = NULL;
549 error = ng_name_node(ng_btsocket_l2cap_raw_node,
550 NG_BTSOCKET_L2CAP_RAW_NODE_TYPE);
552 NG_BTSOCKET_L2CAP_RAW_ALERT(
553 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
555 NG_NODE_UNREF(ng_btsocket_l2cap_raw_node);
556 ng_btsocket_l2cap_raw_node = NULL;
561 /* Create input queue */
562 NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_raw_queue, ifqmaxlen);
563 mtx_init(&ng_btsocket_l2cap_raw_queue_mtx,
564 "btsocks_l2cap_raw_queue_mtx", NULL, MTX_DEF);
565 TASK_INIT(&ng_btsocket_l2cap_raw_queue_task, 0,
566 ng_btsocket_l2cap_raw_input, NULL);
568 /* Create list of sockets */
569 LIST_INIT(&ng_btsocket_l2cap_raw_sockets);
570 mtx_init(&ng_btsocket_l2cap_raw_sockets_mtx,
571 "btsocks_l2cap_raw_sockets_mtx", NULL, MTX_DEF);
574 ng_btsocket_l2cap_raw_token = 0;
575 mtx_init(&ng_btsocket_l2cap_raw_token_mtx,
576 "btsocks_l2cap_raw_token_mtx", NULL, MTX_DEF);
579 LIST_INIT(&ng_btsocket_l2cap_raw_rt);
580 mtx_init(&ng_btsocket_l2cap_raw_rt_mtx,
581 "btsocks_l2cap_raw_rt_mtx", NULL, MTX_DEF);
582 TASK_INIT(&ng_btsocket_l2cap_raw_rt_task, 0,
583 ng_btsocket_l2cap_raw_rtclean, NULL);
584 } /* ng_btsocket_l2cap_raw_init */
587 * Abort connection on socket
591 ng_btsocket_l2cap_raw_abort(struct socket *so)
594 (void)ng_btsocket_l2cap_raw_disconnect(so);
595 } /* ng_btsocket_l2cap_raw_abort */
598 ng_btsocket_l2cap_raw_close(struct socket *so)
601 (void)ng_btsocket_l2cap_raw_disconnect(so);
602 } /* ng_btsocket_l2cap_raw_close */
605 * Create and attach new socket
609 ng_btsocket_l2cap_raw_attach(struct socket *so, int proto, struct thread *td)
611 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
617 if (ng_btsocket_l2cap_raw_node == NULL)
618 return (EPROTONOSUPPORT);
619 if (so->so_type != SOCK_RAW)
620 return (ESOCKTNOSUPPORT);
622 /* Reserve send and receive space if it is not reserved yet */
623 error = soreserve(so, NG_BTSOCKET_L2CAP_RAW_SENDSPACE,
624 NG_BTSOCKET_L2CAP_RAW_RECVSPACE);
628 /* Allocate the PCB */
629 pcb = malloc(sizeof(*pcb),
630 M_NETGRAPH_BTSOCKET_L2CAP_RAW, M_NOWAIT|M_ZERO);
634 /* Link the PCB and the socket */
635 so->so_pcb = (caddr_t) pcb;
638 if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
639 pcb->flags |= NG_BTSOCKET_L2CAP_RAW_PRIVILEGED;
641 mtx_init(&pcb->pcb_mtx, "btsocks_l2cap_raw_pcb_mtx", NULL, MTX_DEF);
643 /* Add the PCB to the list */
644 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
645 LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_sockets, pcb, next);
646 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
649 } /* ng_btsocket_l2cap_raw_attach */
656 ng_btsocket_l2cap_raw_bind(struct socket *so, struct sockaddr *nam,
659 ng_btsocket_l2cap_raw_pcb_t *pcb = so2l2cap_raw_pcb(so);
660 struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam;
661 ng_btsocket_l2cap_rtentry_t *rt = NULL;
665 if (ng_btsocket_l2cap_raw_node == NULL)
670 if (sa->l2cap_family != AF_BLUETOOTH)
671 return (EAFNOSUPPORT);
672 if((sa->l2cap_len != sizeof(*sa))&&
673 (sa->l2cap_len != sizeof(struct sockaddr_l2cap_compat)))
676 if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY,
677 sizeof(sa->l2cap_bdaddr)) != 0) {
678 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
680 LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) {
681 if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
684 if (bcmp(&sa->l2cap_bdaddr, &rt->src,
685 sizeof(rt->src)) == 0)
689 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
696 mtx_lock(&pcb->pcb_mtx);
697 bcopy(&sa->l2cap_bdaddr, &pcb->src, sizeof(pcb->src));
699 mtx_unlock(&pcb->pcb_mtx);
702 } /* ng_btsocket_l2cap_raw_bind */
709 ng_btsocket_l2cap_raw_connect(struct socket *so, struct sockaddr *nam,
712 ng_btsocket_l2cap_raw_pcb_t *pcb = so2l2cap_raw_pcb(so);
713 struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam;
714 ng_btsocket_l2cap_rtentry_t *rt = NULL;
719 if (ng_btsocket_l2cap_raw_node == NULL)
724 if (sa->l2cap_family != AF_BLUETOOTH)
725 return (EAFNOSUPPORT);
726 if((sa->l2cap_len != sizeof(*sa))&&
727 (sa->l2cap_len != sizeof(struct sockaddr_l2cap_compat)))
730 if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
733 mtx_lock(&pcb->pcb_mtx);
735 bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst));
737 if (bcmp(&pcb->src, &pcb->dst, sizeof(pcb->src)) == 0) {
738 mtx_unlock(&pcb->pcb_mtx);
740 return (EADDRNOTAVAIL);
744 * If there is route already - use it
747 if (pcb->rt != NULL) {
749 mtx_unlock(&pcb->pcb_mtx);
755 * Find the first hook that does not match specified destination address
758 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
760 LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) {
761 if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
764 if (bcmp(&pcb->dst, &rt->src, sizeof(rt->src)) != 0)
772 bcopy(&rt->src, &pcb->src, sizeof(pcb->src));
778 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
779 mtx_unlock(&pcb->pcb_mtx);
782 } /* ng_btsocket_l2cap_raw_connect */
785 * Process ioctl's calls on socket
789 ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
790 struct ifnet *ifp, struct thread *td)
792 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
793 struct ng_mesg *msg = NULL;
798 if (ng_btsocket_l2cap_raw_node == NULL)
801 mtx_lock(&pcb->pcb_mtx);
803 /* Check if we route info */
804 if (pcb->rt == NULL) {
805 mtx_unlock(&pcb->pcb_mtx);
806 return (EHOSTUNREACH);
809 /* Check if we have pending ioctl() */
810 if (pcb->token != 0) {
811 mtx_unlock(&pcb->pcb_mtx);
816 case SIOC_L2CAP_NODE_GET_FLAGS: {
817 struct ng_btsocket_l2cap_raw_node_flags *p =
818 (struct ng_btsocket_l2cap_raw_node_flags *) data;
820 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
821 NGM_L2CAP_NODE_GET_FLAGS,
822 &p->flags, sizeof(p->flags));
825 case SIOC_L2CAP_NODE_GET_DEBUG: {
826 struct ng_btsocket_l2cap_raw_node_debug *p =
827 (struct ng_btsocket_l2cap_raw_node_debug *) data;
829 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
830 NGM_L2CAP_NODE_GET_DEBUG,
831 &p->debug, sizeof(p->debug));
834 case SIOC_L2CAP_NODE_SET_DEBUG: {
835 struct ng_btsocket_l2cap_raw_node_debug *p =
836 (struct ng_btsocket_l2cap_raw_node_debug *) data;
838 if (pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)
839 error = ng_btsocket_l2cap_raw_send_ngmsg(pcb->rt->hook,
840 NGM_L2CAP_NODE_SET_DEBUG,
841 &p->debug, sizeof(p->debug));
846 case SIOC_L2CAP_NODE_GET_CON_LIST: {
847 struct ng_btsocket_l2cap_raw_con_list *p =
848 (struct ng_btsocket_l2cap_raw_con_list *) data;
849 ng_l2cap_node_con_list_ep *p1 = NULL;
850 ng_l2cap_node_con_ep *p2 = NULL;
852 if (p->num_connections == 0 ||
853 p->num_connections > NG_L2CAP_MAX_CON_NUM ||
854 p->connections == NULL) {
855 mtx_unlock(&pcb->pcb_mtx);
859 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_NODE_GET_CON_LIST,
862 mtx_unlock(&pcb->pcb_mtx);
865 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
866 pcb->token = msg->header.token;
869 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
873 mtx_unlock(&pcb->pcb_mtx);
877 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
878 ng_btsocket_l2cap_raw_ioctl_timeout * hz);
882 mtx_unlock(&pcb->pcb_mtx);
889 mtx_unlock(&pcb->pcb_mtx);
892 msg->header.cmd == NGM_L2CAP_NODE_GET_CON_LIST) {
893 /* Return data back to user space */
894 p1 = (ng_l2cap_node_con_list_ep *)(msg->data);
895 p2 = (ng_l2cap_node_con_ep *)(p1 + 1);
897 p->num_connections = min(p->num_connections,
898 p1->num_connections);
899 if (p->num_connections > 0)
900 error = copyout((caddr_t) p2,
901 (caddr_t) p->connections,
902 p->num_connections * sizeof(*p2));
906 NG_FREE_MSG(msg); /* checks for != NULL */
910 case SIOC_L2CAP_NODE_GET_CHAN_LIST: {
911 struct ng_btsocket_l2cap_raw_chan_list *p =
912 (struct ng_btsocket_l2cap_raw_chan_list *) data;
913 ng_l2cap_node_chan_list_ep *p1 = NULL;
914 ng_l2cap_node_chan_ep *p2 = NULL;
916 if (p->num_channels == 0 ||
917 p->num_channels > NG_L2CAP_MAX_CHAN_NUM ||
918 p->channels == NULL) {
919 mtx_unlock(&pcb->pcb_mtx);
923 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
924 NGM_L2CAP_NODE_GET_CHAN_LIST, 0, M_NOWAIT);
926 mtx_unlock(&pcb->pcb_mtx);
929 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
930 pcb->token = msg->header.token;
933 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
937 mtx_unlock(&pcb->pcb_mtx);
941 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
942 ng_btsocket_l2cap_raw_ioctl_timeout * hz);
946 mtx_unlock(&pcb->pcb_mtx);
953 mtx_unlock(&pcb->pcb_mtx);
956 msg->header.cmd == NGM_L2CAP_NODE_GET_CHAN_LIST) {
957 /* Return data back to user space */
958 p1 = (ng_l2cap_node_chan_list_ep *)(msg->data);
959 p2 = (ng_l2cap_node_chan_ep *)(p1 + 1);
961 p->num_channels = min(p->num_channels,
963 if (p->num_channels > 0)
964 error = copyout((caddr_t) p2,
965 (caddr_t) p->channels,
966 p->num_channels * sizeof(*p2));
970 NG_FREE_MSG(msg); /* checks for != NULL */
974 case SIOC_L2CAP_L2CA_PING: {
975 struct ng_btsocket_l2cap_raw_ping *p =
976 (struct ng_btsocket_l2cap_raw_ping *) data;
977 ng_l2cap_l2ca_ping_ip *ip = NULL;
978 ng_l2cap_l2ca_ping_op *op = NULL;
980 if ((p->echo_size != 0 && p->echo_data == NULL) ||
981 p->echo_size > NG_L2CAP_MAX_ECHO_SIZE) {
982 mtx_unlock(&pcb->pcb_mtx);
986 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
987 NGM_L2CAP_L2CA_PING, sizeof(*ip) + p->echo_size,
990 mtx_unlock(&pcb->pcb_mtx);
993 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
994 pcb->token = msg->header.token;
997 ip = (ng_l2cap_l2ca_ping_ip *)(msg->data);
998 bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
999 ip->echo_size = p->echo_size;
1001 if (ip->echo_size > 0) {
1002 mtx_unlock(&pcb->pcb_mtx);
1003 error = copyin(p->echo_data, ip + 1, p->echo_size);
1004 mtx_lock(&pcb->pcb_mtx);
1009 mtx_unlock(&pcb->pcb_mtx);
1014 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
1018 mtx_unlock(&pcb->pcb_mtx);
1022 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
1023 bluetooth_l2cap_rtx_timeout());
1027 mtx_unlock(&pcb->pcb_mtx);
1034 mtx_unlock(&pcb->pcb_mtx);
1037 msg->header.cmd == NGM_L2CAP_L2CA_PING) {
1038 /* Return data back to the user space */
1039 op = (ng_l2cap_l2ca_ping_op *)(msg->data);
1040 p->result = op->result;
1041 p->echo_size = min(p->echo_size, op->echo_size);
1043 if (p->echo_size > 0)
1044 error = copyout(op + 1, p->echo_data,
1049 NG_FREE_MSG(msg); /* checks for != NULL */
1053 case SIOC_L2CAP_L2CA_GET_INFO: {
1054 struct ng_btsocket_l2cap_raw_get_info *p =
1055 (struct ng_btsocket_l2cap_raw_get_info *) data;
1056 ng_l2cap_l2ca_get_info_ip *ip = NULL;
1057 ng_l2cap_l2ca_get_info_op *op = NULL;
1059 if (!(pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)) {
1060 mtx_unlock(&pcb->pcb_mtx);
1064 if (p->info_size != 0 && p->info_data == NULL) {
1065 mtx_unlock(&pcb->pcb_mtx);
1069 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
1070 NGM_L2CAP_L2CA_GET_INFO, sizeof(*ip) + p->info_size,
1073 mtx_unlock(&pcb->pcb_mtx);
1076 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
1077 pcb->token = msg->header.token;
1080 ip = (ng_l2cap_l2ca_get_info_ip *)(msg->data);
1081 bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
1082 ip->info_type = p->info_type;
1084 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
1088 mtx_unlock(&pcb->pcb_mtx);
1092 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
1093 bluetooth_l2cap_rtx_timeout());
1097 mtx_unlock(&pcb->pcb_mtx);
1104 mtx_unlock(&pcb->pcb_mtx);
1107 msg->header.cmd == NGM_L2CAP_L2CA_GET_INFO) {
1108 /* Return data back to the user space */
1109 op = (ng_l2cap_l2ca_get_info_op *)(msg->data);
1110 p->result = op->result;
1111 p->info_size = min(p->info_size, op->info_size);
1113 if (p->info_size > 0)
1114 error = copyout(op + 1, p->info_data,
1119 NG_FREE_MSG(msg); /* checks for != NULL */
1123 case SIOC_L2CAP_NODE_GET_AUTO_DISCON_TIMO: {
1124 struct ng_btsocket_l2cap_raw_auto_discon_timo *p =
1125 (struct ng_btsocket_l2cap_raw_auto_discon_timo *) data;
1127 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
1128 NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO,
1129 &p->timeout, sizeof(p->timeout));
1132 case SIOC_L2CAP_NODE_SET_AUTO_DISCON_TIMO: {
1133 struct ng_btsocket_l2cap_raw_auto_discon_timo *p =
1134 (struct ng_btsocket_l2cap_raw_auto_discon_timo *) data;
1136 if (pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)
1137 error = ng_btsocket_l2cap_raw_send_ngmsg(pcb->rt->hook,
1138 NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO,
1139 &p->timeout, sizeof(p->timeout));
1149 mtx_unlock(&pcb->pcb_mtx);
1152 } /* ng_btsocket_l2cap_raw_control */
1155 * Detach and destroy socket
1159 ng_btsocket_l2cap_raw_detach(struct socket *so)
1161 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
1163 KASSERT(pcb != NULL, ("nt_btsocket_l2cap_raw_detach: pcb == NULL"));
1164 if (ng_btsocket_l2cap_raw_node == NULL)
1167 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
1168 mtx_lock(&pcb->pcb_mtx);
1170 LIST_REMOVE(pcb, next);
1172 mtx_unlock(&pcb->pcb_mtx);
1173 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
1175 mtx_destroy(&pcb->pcb_mtx);
1177 bzero(pcb, sizeof(*pcb));
1178 free(pcb, M_NETGRAPH_BTSOCKET_L2CAP_RAW);
1181 } /* ng_btsocket_l2cap_raw_detach */
1188 ng_btsocket_l2cap_raw_disconnect(struct socket *so)
1190 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
1194 if (ng_btsocket_l2cap_raw_node == NULL)
1197 mtx_lock(&pcb->pcb_mtx);
1199 soisdisconnected(so);
1200 mtx_unlock(&pcb->pcb_mtx);
1203 } /* ng_btsocket_l2cap_raw_disconnect */
1210 ng_btsocket_l2cap_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1212 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
1213 struct sockaddr_l2cap sa;
1217 if (ng_btsocket_l2cap_raw_node == NULL)
1220 mtx_lock(&pcb->pcb_mtx);
1221 bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
1222 mtx_unlock(&pcb->pcb_mtx);
1225 sa.l2cap_len = sizeof(sa);
1226 sa.l2cap_family = AF_BLUETOOTH;
1228 sa.l2cap_bdaddr_type = BDADDR_BREDR;
1230 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1232 return ((*nam == NULL)? ENOMEM : 0);
1233 } /* ng_btsocket_l2cap_raw_peeraddr */
1236 * Send data to socket
1240 ng_btsocket_l2cap_raw_send(struct socket *so, int flags, struct mbuf *m,
1241 struct sockaddr *nam, struct mbuf *control, struct thread *td)
1243 NG_FREE_M(m); /* Checks for m != NULL */
1246 return (EOPNOTSUPP);
1247 } /* ng_btsocket_l2cap_raw_send */
1250 * Get socket address
1254 ng_btsocket_l2cap_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1256 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so);
1257 struct sockaddr_l2cap sa;
1261 if (ng_btsocket_l2cap_raw_node == NULL)
1264 mtx_lock(&pcb->pcb_mtx);
1265 bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
1266 mtx_unlock(&pcb->pcb_mtx);
1269 sa.l2cap_len = sizeof(sa);
1270 sa.l2cap_family = AF_BLUETOOTH;
1272 sa.l2cap_bdaddr_type = BDADDR_BREDR;
1273 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1275 return ((*nam == NULL)? ENOMEM : 0);
1276 } /* ng_btsocket_l2cap_raw_sockaddr */
1283 ng_btsocket_l2cap_raw_get_token(u_int32_t *token)
1285 mtx_lock(&ng_btsocket_l2cap_raw_token_mtx);
1287 if (++ ng_btsocket_l2cap_raw_token == 0)
1288 ng_btsocket_l2cap_raw_token = 1;
1290 *token = ng_btsocket_l2cap_raw_token;
1292 mtx_unlock(&ng_btsocket_l2cap_raw_token_mtx);
1293 } /* ng_btsocket_l2cap_raw_get_token */
1296 * Send Netgraph message to the node - do not expect reply
1300 ng_btsocket_l2cap_raw_send_ngmsg(hook_p hook, int cmd, void *arg, int arglen)
1302 struct ng_mesg *msg = NULL;
1305 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, arglen, M_NOWAIT);
1309 if (arg != NULL && arglen > 0)
1310 bcopy(arg, msg->data, arglen);
1312 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg, hook, 0);
1315 } /* ng_btsocket_l2cap_raw_send_ngmsg */
1318 * Send Netgraph message to the node (no data) and wait for reply
1322 ng_btsocket_l2cap_raw_send_sync_ngmsg(ng_btsocket_l2cap_raw_pcb_p pcb,
1323 int cmd, void *rsp, int rsplen)
1325 struct ng_mesg *msg = NULL;
1328 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1330 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, 0, M_NOWAIT);
1334 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
1335 pcb->token = msg->header.token;
1338 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
1345 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
1346 ng_btsocket_l2cap_raw_ioctl_timeout * hz);
1352 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
1353 bcopy(pcb->msg->data, rsp, rsplen);
1357 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1360 } /* ng_btsocket_l2cap_raw_send_sync_ngmsg */