2 * ng_btsocket_hci_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_hci_raw.c,v 1.14 2003/09/14 23:29:06 max Exp $
31 * $FreeBSD: src/sys/netgraph/bluetooth/socket/ng_btsocket_hci_raw.c,v 1.23 2006/11/06 13:42:04 rwatson Exp $
32 * $DragonFly: src/sys/netgraph7/bluetooth/socket/ng_btsocket_hci_raw.c,v 1.2 2008/06/26 23:05:40 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bitstring.h>
38 #include <sys/domain.h>
39 #include <sys/endian.h>
40 #include <sys/errno.h>
41 #include <sys/filedesc.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/mutex.h>
48 #include <sys/protosw.h>
49 #include <sys/queue.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/taskqueue.h>
54 #include "ng_message.h"
56 #include "bluetooth/include/ng_bluetooth.h"
57 #include "bluetooth/include/ng_hci.h"
58 #include "bluetooth/include/ng_l2cap.h"
59 #include "bluetooth/include/ng_btsocket.h"
60 #include "bluetooth/include/ng_btsocket_hci_raw.h"
63 #ifdef NG_SEPARATE_MALLOC
64 MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_HCI_RAW, "netgraph_btsocks_hci_raw",
65 "Netgraph Bluetooth raw HCI sockets");
67 #define M_NETGRAPH_BTSOCKET_HCI_RAW M_NETGRAPH
68 #endif /* NG_SEPARATE_MALLOC */
70 /* Netgraph node methods */
71 static ng_constructor_t ng_btsocket_hci_raw_node_constructor;
72 static ng_rcvmsg_t ng_btsocket_hci_raw_node_rcvmsg;
73 static ng_shutdown_t ng_btsocket_hci_raw_node_shutdown;
74 static ng_newhook_t ng_btsocket_hci_raw_node_newhook;
75 static ng_connect_t ng_btsocket_hci_raw_node_connect;
76 static ng_rcvdata_t ng_btsocket_hci_raw_node_rcvdata;
77 static ng_disconnect_t ng_btsocket_hci_raw_node_disconnect;
79 static void ng_btsocket_hci_raw_input (void *, int);
80 static void ng_btsocket_hci_raw_output(node_p, hook_p, void *, int);
81 static void ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p,
84 static int ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p,
87 #define ng_btsocket_hci_raw_wakeup_input_task() \
88 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_hci_raw_task)
91 struct ng_btsocket_hci_raw_sec_filter {
92 bitstr_t bit_decl(events, 0xff);
93 bitstr_t bit_decl(commands[0x3f], 0x3ff);
96 /* Netgraph type descriptor */
97 static struct ng_type typestruct = {
98 .version = NG_ABI_VERSION,
99 .name = NG_BTSOCKET_HCI_RAW_NODE_TYPE,
100 .constructor = ng_btsocket_hci_raw_node_constructor,
101 .rcvmsg = ng_btsocket_hci_raw_node_rcvmsg,
102 .shutdown = ng_btsocket_hci_raw_node_shutdown,
103 .newhook = ng_btsocket_hci_raw_node_newhook,
104 .connect = ng_btsocket_hci_raw_node_connect,
105 .rcvdata = ng_btsocket_hci_raw_node_rcvdata,
106 .disconnect = ng_btsocket_hci_raw_node_disconnect,
110 extern int ifqmaxlen;
111 static u_int32_t ng_btsocket_hci_raw_debug_level;
112 static u_int32_t ng_btsocket_hci_raw_ioctl_timeout;
113 static node_p ng_btsocket_hci_raw_node;
114 static struct ng_bt_itemq ng_btsocket_hci_raw_queue;
115 static struct mtx ng_btsocket_hci_raw_queue_mtx;
116 static struct task ng_btsocket_hci_raw_task;
117 static LIST_HEAD(, ng_btsocket_hci_raw_pcb) ng_btsocket_hci_raw_sockets;
118 static struct mtx ng_btsocket_hci_raw_sockets_mtx;
119 static u_int32_t ng_btsocket_hci_raw_token;
120 static struct mtx ng_btsocket_hci_raw_token_mtx;
121 static struct ng_btsocket_hci_raw_sec_filter *ng_btsocket_hci_raw_sec_filter;
124 SYSCTL_DECL(_net_bluetooth_hci_sockets);
125 SYSCTL_NODE(_net_bluetooth_hci_sockets, OID_AUTO, raw, CTLFLAG_RW,
126 0, "Bluetooth raw HCI sockets family");
127 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, debug_level, CTLFLAG_RW,
128 &ng_btsocket_hci_raw_debug_level, NG_BTSOCKET_WARN_LEVEL,
129 "Bluetooth raw HCI sockets debug level");
130 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, ioctl_timeout, CTLFLAG_RW,
131 &ng_btsocket_hci_raw_ioctl_timeout, 5,
132 "Bluetooth raw HCI sockets ioctl timeout");
133 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_len, CTLFLAG_RD,
134 &ng_btsocket_hci_raw_queue.len, 0,
135 "Bluetooth raw HCI sockets input queue length");
136 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_maxlen, CTLFLAG_RD,
137 &ng_btsocket_hci_raw_queue.maxlen, 0,
138 "Bluetooth raw HCI sockets input queue max. length");
139 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_drops, CTLFLAG_RD,
140 &ng_btsocket_hci_raw_queue.drops, 0,
141 "Bluetooth raw HCI sockets input queue drops");
144 #define NG_BTSOCKET_HCI_RAW_INFO \
145 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL) \
148 #define NG_BTSOCKET_HCI_RAW_WARN \
149 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL) \
152 #define NG_BTSOCKET_HCI_RAW_ERR \
153 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL) \
156 #define NG_BTSOCKET_HCI_RAW_ALERT \
157 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL) \
160 /****************************************************************************
161 ****************************************************************************
163 ****************************************************************************
164 ****************************************************************************/
167 * Netgraph node constructor. Do not allow to create node of this type.
171 ng_btsocket_hci_raw_node_constructor(node_p node)
174 } /* ng_btsocket_hci_raw_node_constructor */
177 * Netgraph node destructor. Just let old node go and create new fresh one.
181 ng_btsocket_hci_raw_node_shutdown(node_p node)
187 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
189 NG_BTSOCKET_HCI_RAW_ALERT(
190 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
192 ng_btsocket_hci_raw_node = NULL;
197 error = ng_name_node(ng_btsocket_hci_raw_node,
198 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
200 NG_BTSOCKET_HCI_RAW_ALERT(
201 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
203 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
204 ng_btsocket_hci_raw_node = NULL;
210 } /* ng_btsocket_hci_raw_node_shutdown */
213 * Create new hook. Just say "yes"
217 ng_btsocket_hci_raw_node_newhook(node_p node, hook_p hook, char const *name)
220 } /* ng_btsocket_hci_raw_node_newhook */
223 * Connect hook. Just say "yes"
227 ng_btsocket_hci_raw_node_connect(hook_p hook)
230 } /* ng_btsocket_hci_raw_node_connect */
237 ng_btsocket_hci_raw_node_disconnect(hook_p hook)
240 } /* ng_btsocket_hci_raw_node_disconnect */
243 * Receive control message.
244 * Make sure it is a message from HCI node and it is a response.
245 * Enqueue item and schedule input task.
249 ng_btsocket_hci_raw_node_rcvmsg(node_p node, item_p item, hook_p lasthook)
251 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
255 * Check for empty sockets list creates LOR when both sender and
256 * receiver device are connected to the same host, so remove it
261 (msg->header.typecookie == NGM_HCI_COOKIE ||
262 msg->header.typecookie == NGM_GENERIC_COOKIE) &&
263 msg->header.flags & NGF_RESP) {
264 if (msg->header.token == 0) {
269 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
270 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
271 NG_BTSOCKET_HCI_RAW_ERR(
272 "%s: Input queue is full\n", __func__);
274 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
278 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
279 error = ng_btsocket_hci_raw_wakeup_input_task();
281 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
288 } /* ng_btsocket_hci_raw_node_rcvmsg */
291 * Receive packet from the one of our hook.
292 * Prepend every packet with sockaddr_hci and record sender's node name.
293 * Enqueue item and schedule input task.
297 ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item)
299 struct mbuf *nam = NULL;
303 * Check for empty sockets list creates LOR when both sender and
304 * receiver device are connected to the same host, so remove it
308 MGET(nam, MB_DONTWAIT, MT_SONAME);
310 struct sockaddr_hci *sa = mtod(nam, struct sockaddr_hci *);
312 nam->m_len = sizeof(struct sockaddr_hci);
314 sa->hci_len = sizeof(*sa);
315 sa->hci_family = AF_BLUETOOTH;
316 strlcpy(sa->hci_node, NG_PEER_NODE_NAME(hook),
317 sizeof(sa->hci_node));
319 NGI_GET_M(item, nam->m_next);
322 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
323 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
324 NG_BTSOCKET_HCI_RAW_ERR(
325 "%s: Input queue is full\n", __func__);
327 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
331 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
332 error = ng_btsocket_hci_raw_wakeup_input_task();
334 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
336 NG_BTSOCKET_HCI_RAW_ERR(
337 "%s: Failed to allocate address mbuf\n", __func__);
344 } /* ng_btsocket_hci_raw_node_rcvdata */
346 /****************************************************************************
347 ****************************************************************************
349 ****************************************************************************
350 ****************************************************************************/
353 * Get next token. We need token to avoid theoretical race where process
354 * submits ioctl() message then interrupts ioctl() and re-submits another
355 * ioctl() on the same socket *before* first ioctl() complete.
359 ng_btsocket_hci_raw_get_token(u_int32_t *token)
361 mtx_lock(&ng_btsocket_hci_raw_token_mtx);
363 if (++ ng_btsocket_hci_raw_token == 0)
364 ng_btsocket_hci_raw_token = 1;
366 *token = ng_btsocket_hci_raw_token;
368 mtx_unlock(&ng_btsocket_hci_raw_token_mtx);
369 } /* ng_btsocket_hci_raw_get_token */
372 * Send Netgraph message to the node - do not expect reply
376 ng_btsocket_hci_raw_send_ngmsg(char *path, int cmd, void *arg, int arglen)
378 struct ng_mesg *msg = NULL;
381 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, arglen, M_WAITOK | M_NULLOK);
385 if (arg != NULL && arglen > 0)
386 bcopy(arg, msg->data, arglen);
388 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
391 } /* ng_btsocket_hci_raw_send_ngmsg */
394 * Send Netgraph message to the node (no data) and wait for reply
398 ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path,
399 int cmd, void *rsp, int rsplen)
401 struct ng_mesg *msg = NULL;
404 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
406 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
410 ng_btsocket_hci_raw_get_token(&msg->header.token);
411 pcb->token = msg->header.token;
414 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
420 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "hcictl",
421 ng_btsocket_hci_raw_ioctl_timeout * hz);
427 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
428 bcopy(pcb->msg->data, rsp, rsplen);
432 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
435 } /* ng_btsocket_hci_raw_send_sync_ngmsg */
438 * Create control information for the packet
442 ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf **ctl,
448 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
450 if (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION) {
451 dir = (m->m_flags & M_PROTO1)? 1 : 0;
452 *ctl = sbcreatecontrol((caddr_t) &dir, sizeof(dir),
453 SCM_HCI_RAW_DIRECTION, SOL_HCI_RAW);
455 ctl = &((*ctl)->m_next);
458 if (pcb->so->so_options & SO_TIMESTAMP) {
460 *ctl = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
461 SCM_TIMESTAMP, SOL_SOCKET);
463 ctl = &((*ctl)->m_next);
465 } /* ng_btsocket_hci_raw_savctl */
468 * Raw HCI sockets data input routine
472 ng_btsocket_hci_raw_data_input(struct mbuf *nam)
474 ng_btsocket_hci_raw_pcb_p pcb = NULL;
475 struct mbuf *m0 = NULL, *m = NULL;
476 struct sockaddr_hci *sa = NULL;
481 KASSERT((nam->m_type == MT_SONAME),
482 ("%s: m_type=%d\n", __func__, nam->m_type));
483 KASSERT((m0->m_flags & M_PKTHDR),
484 ("%s: m_flags=%#x\n", __func__, m0->m_flags));
486 sa = mtod(nam, struct sockaddr_hci *);
488 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
490 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
492 mtx_lock(&pcb->pcb_mtx);
495 * If socket was bound then check address and
496 * make sure it matches.
499 if (pcb->addr.hci_node[0] != 0 &&
500 strcmp(sa->hci_node, pcb->addr.hci_node) != 0)
504 * Check packet against filters
505 * XXX do we have to call m_pullup() here?
508 if (ng_btsocket_hci_raw_filter(pcb, m0, 1) != 0)
512 * Make a copy of the packet, append to the socket's
513 * receive queue and wakeup socket. sbappendaddr()
514 * will check if socket has enough buffer space.
517 m = m_dup(m0, MB_DONTWAIT);
519 struct mbuf *ctl = NULL;
521 ng_btsocket_hci_raw_savctl(pcb, &ctl, m);
523 if (sbappendaddr(&pcb->so->so_rcv,
524 (struct sockaddr *) sa, m, ctl))
527 NG_BTSOCKET_HCI_RAW_INFO(
528 "%s: sbappendaddr() failed\n", __func__);
535 mtx_unlock(&pcb->pcb_mtx);
538 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
542 } /* ng_btsocket_hci_raw_data_input */
545 * Raw HCI sockets message input routine
549 ng_btsocket_hci_raw_msg_input(struct ng_mesg *msg)
551 ng_btsocket_hci_raw_pcb_p pcb = NULL;
553 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
555 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
556 mtx_lock(&pcb->pcb_mtx);
558 if (msg->header.token == pcb->token) {
562 mtx_unlock(&pcb->pcb_mtx);
563 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
568 mtx_unlock(&pcb->pcb_mtx);
571 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
573 NG_FREE_MSG(msg); /* checks for != NULL */
574 } /* ng_btsocket_hci_raw_msg_input */
577 * Raw HCI sockets input routines
581 ng_btsocket_hci_raw_input(void *context, int pending)
586 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
587 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_hci_raw_queue, item);
588 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
593 switch(item->el_flags & NGQF_TYPE) {
595 struct mbuf *m = NULL;
598 ng_btsocket_hci_raw_data_input(m);
602 struct ng_mesg *msg = NULL;
604 NGI_GET_MSG(item, msg);
605 ng_btsocket_hci_raw_msg_input(msg);
610 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
616 } /* ng_btsocket_hci_raw_input */
619 * Raw HCI sockets output routine
623 ng_btsocket_hci_raw_output(node_p node, hook_p hook, void *arg1, int arg2)
625 struct mbuf *nam = (struct mbuf *) arg1, *m = NULL;
626 struct sockaddr_hci *sa = NULL;
632 KASSERT((nam->m_type == MT_SONAME),
633 ("%s: m_type=%d\n", __func__, nam->m_type));
634 KASSERT((m->m_flags & M_PKTHDR),
635 ("%s: m_flags=%#x\n", __func__, m->m_flags));
637 sa = mtod(nam, struct sockaddr_hci *);
640 * Find downstream hook
641 * XXX For now access node hook list directly. Should be safe because
642 * we used ng_send_fn() and we should have exclusive lock on the node.
645 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
646 if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
647 NG_NODE_NOT_VALID(NG_PEER_NODE(hook)))
650 if (strcmp(sa->hci_node, NG_PEER_NODE_NAME(hook)) == 0) {
651 NG_SEND_DATA_ONLY(error, hook, m); /* sets m to NULL */
656 NG_FREE_M(nam); /* check for != NULL */
658 } /* ng_btsocket_hci_raw_output */
661 * Check frame against security and socket filters.
662 * d (direction bit) == 1 means incoming frame.
666 ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf *m, int d)
668 int type, event, opcode;
670 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
672 switch ((type = *mtod(m, u_int8_t *))) {
674 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)) {
675 opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode);
678 ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF(opcode) - 1],
679 NG_HCI_OCF(opcode) - 1))
683 if (d && !bit_test(pcb->filter.packet_mask, NG_HCI_CMD_PKT - 1))
687 case NG_HCI_ACL_DATA_PKT:
688 case NG_HCI_SCO_DATA_PKT:
689 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) ||
690 !bit_test(pcb->filter.packet_mask, type - 1) ||
695 case NG_HCI_EVENT_PKT:
699 event = mtod(m, ng_hci_event_pkt_t *)->event - 1;
701 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED))
702 if (!bit_test(ng_btsocket_hci_raw_sec_filter->events, event))
705 if (!bit_test(pcb->filter.event_mask, event))
714 } /* ng_btsocket_hci_raw_filter */
717 * Initialize everything
721 ng_btsocket_hci_raw_init(void)
726 ng_btsocket_hci_raw_node = NULL;
727 ng_btsocket_hci_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
728 ng_btsocket_hci_raw_ioctl_timeout = 5;
730 /* Register Netgraph node type */
731 error = ng_newtype(&typestruct);
733 NG_BTSOCKET_HCI_RAW_ALERT(
734 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
739 /* Create Netgrapg node */
740 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
742 NG_BTSOCKET_HCI_RAW_ALERT(
743 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
745 ng_btsocket_hci_raw_node = NULL;
750 error = ng_name_node(ng_btsocket_hci_raw_node,
751 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
753 NG_BTSOCKET_HCI_RAW_ALERT(
754 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
756 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
757 ng_btsocket_hci_raw_node = NULL;
762 /* Create input queue */
763 NG_BT_ITEMQ_INIT(&ng_btsocket_hci_raw_queue, ifqmaxlen);
764 mtx_init(&ng_btsocket_hci_raw_queue_mtx,
765 "btsocks_hci_raw_queue_mtx", NULL, MTX_DEF);
766 TASK_INIT(&ng_btsocket_hci_raw_task, 0,
767 ng_btsocket_hci_raw_input, NULL);
769 /* Create list of sockets */
770 LIST_INIT(&ng_btsocket_hci_raw_sockets);
771 mtx_init(&ng_btsocket_hci_raw_sockets_mtx,
772 "btsocks_hci_raw_sockets_mtx", NULL, MTX_DEF);
775 ng_btsocket_hci_raw_token = 0;
776 mtx_init(&ng_btsocket_hci_raw_token_mtx,
777 "btsocks_hci_raw_token_mtx", NULL, MTX_DEF);
784 ng_btsocket_hci_raw_sec_filter = NULL;
786 ng_btsocket_hci_raw_sec_filter = kmalloc(sizeof(struct ng_btsocket_hci_raw_sec_filter),
787 M_NETGRAPH_BTSOCKET_HCI_RAW,
788 M_WAITOK | M_NULLOK | M_ZERO);
789 if (ng_btsocket_hci_raw_sec_filter == NULL) {
790 printf("%s: Could not allocate security filter!\n", __func__);
795 * XXX How paranoid can we get?
797 * Initialize security filter. If bit is set in the mask then
798 * unprivileged socket is allowed to send (receive) this command
802 /* Enable all events */
803 memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff,
804 sizeof(ng_btsocket_hci_raw_sec_filter->events)/
805 sizeof(ng_btsocket_hci_raw_sec_filter->events[0]));
807 /* Disable some critical events */
808 f = ng_btsocket_hci_raw_sec_filter->events;
809 bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
810 bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
811 bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
813 /* Commands - Link control */
814 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1];
815 bit_set(f, NG_HCI_OCF_INQUIRY - 1);
816 bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
817 bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
818 bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
819 bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
820 bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
821 bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
822 bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
824 /* Commands - Link policy */
825 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1];
826 bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
827 bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1);
829 /* Commands - Host controller and baseband */
830 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1];
831 bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
832 bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
833 bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
834 bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
835 bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
836 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1);
837 bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1);
838 bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
839 bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
840 bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
841 bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
842 bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
843 bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1);
844 bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1);
845 bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
846 bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
847 bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1);
848 bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
849 bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
850 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
851 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
853 /* Commands - Informational */
854 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1];
855 bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
856 bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
857 bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
858 bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
859 bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
861 /* Commands - Status */
862 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1];
863 bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1);
864 bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
865 bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
867 /* Commands - Testing */
868 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1];
869 bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
870 } /* ng_btsocket_hci_raw_init */
873 * Abort connection on socket
877 ng_btsocket_hci_raw_abort(struct socket *so)
879 } /* ng_btsocket_hci_raw_abort */
882 ng_btsocket_hci_raw_close(struct socket *so)
884 } /* ng_btsocket_hci_raw_close */
887 * Create new raw HCI socket
891 ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
893 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
899 if (ng_btsocket_hci_raw_node == NULL)
900 return (EPROTONOSUPPORT);
901 if (proto != BLUETOOTH_PROTO_HCI)
902 return (EPROTONOSUPPORT);
903 if (so->so_type != SOCK_RAW)
904 return (ESOCKTNOSUPPORT);
906 error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
907 NG_BTSOCKET_HCI_RAW_RECVSPACE);
911 pcb = kmalloc(sizeof(*pcb), M_NETGRAPH_BTSOCKET_HCI_RAW,
912 M_WAITOK | M_NULLOK | M_ZERO);
916 so->so_pcb = (caddr_t) pcb;
919 if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
920 pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
923 * Set default socket filter. By default socket only accepts HCI
924 * Command_Complete and Command_Status event packets.
927 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
928 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
930 mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF);
932 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
933 LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
934 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
937 } /* ng_btsocket_hci_raw_attach */
940 * Bind raw HCI socket
944 ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam,
947 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
948 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
952 if (ng_btsocket_hci_raw_node == NULL)
957 if (sa->hci_family != AF_BLUETOOTH)
958 return (EAFNOSUPPORT);
959 if (sa->hci_len != sizeof(*sa))
961 if (sa->hci_node[0] == 0)
964 mtx_lock(&pcb->pcb_mtx);
965 bcopy(sa, &pcb->addr, sizeof(pcb->addr));
966 mtx_unlock(&pcb->pcb_mtx);
969 } /* ng_btsocket_hci_raw_bind */
972 * Connect raw HCI socket
976 ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam,
979 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
980 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
984 if (ng_btsocket_hci_raw_node == NULL)
989 if (sa->hci_family != AF_BLUETOOTH)
990 return (EAFNOSUPPORT);
991 if (sa->hci_len != sizeof(*sa))
993 if (sa->hci_node[0] == 0)
994 return (EDESTADDRREQ);
996 mtx_lock(&pcb->pcb_mtx);
998 if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
999 mtx_unlock(&pcb->pcb_mtx);
1000 return (EADDRNOTAVAIL);
1005 mtx_unlock(&pcb->pcb_mtx);
1008 } /* ng_btsocket_hci_raw_connect */
1011 * Process ioctl on socket
1015 ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
1016 struct ifnet *ifp, struct thread *td)
1018 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1019 char path[NG_NODESIZ + 1];
1020 struct ng_mesg *msg = NULL;
1025 if (ng_btsocket_hci_raw_node == NULL)
1028 mtx_lock(&pcb->pcb_mtx);
1030 /* Check if we have device name */
1031 if (pcb->addr.hci_node[0] == 0) {
1032 mtx_unlock(&pcb->pcb_mtx);
1033 return (EHOSTUNREACH);
1036 /* Check if we have pending ioctl() */
1037 if (pcb->token != 0) {
1038 mtx_unlock(&pcb->pcb_mtx);
1042 snprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1045 case SIOC_HCI_RAW_NODE_GET_STATE: {
1046 struct ng_btsocket_hci_raw_node_state *p =
1047 (struct ng_btsocket_hci_raw_node_state *) data;
1049 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1050 NGM_HCI_NODE_GET_STATE,
1051 &p->state, sizeof(p->state));
1054 case SIOC_HCI_RAW_NODE_INIT:
1055 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1056 error = ng_btsocket_hci_raw_send_ngmsg(path,
1057 NGM_HCI_NODE_INIT, NULL, 0);
1062 case SIOC_HCI_RAW_NODE_GET_DEBUG: {
1063 struct ng_btsocket_hci_raw_node_debug *p =
1064 (struct ng_btsocket_hci_raw_node_debug *) data;
1066 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1067 NGM_HCI_NODE_GET_DEBUG,
1068 &p->debug, sizeof(p->debug));
1071 case SIOC_HCI_RAW_NODE_SET_DEBUG: {
1072 struct ng_btsocket_hci_raw_node_debug *p =
1073 (struct ng_btsocket_hci_raw_node_debug *) data;
1075 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1076 error = ng_btsocket_hci_raw_send_ngmsg(path,
1077 NGM_HCI_NODE_SET_DEBUG, &p->debug,
1083 case SIOC_HCI_RAW_NODE_GET_BUFFER: {
1084 struct ng_btsocket_hci_raw_node_buffer *p =
1085 (struct ng_btsocket_hci_raw_node_buffer *) data;
1087 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1088 NGM_HCI_NODE_GET_BUFFER,
1089 &p->buffer, sizeof(p->buffer));
1092 case SIOC_HCI_RAW_NODE_GET_BDADDR: {
1093 struct ng_btsocket_hci_raw_node_bdaddr *p =
1094 (struct ng_btsocket_hci_raw_node_bdaddr *) data;
1096 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1097 NGM_HCI_NODE_GET_BDADDR,
1098 &p->bdaddr, sizeof(p->bdaddr));
1101 case SIOC_HCI_RAW_NODE_GET_FEATURES: {
1102 struct ng_btsocket_hci_raw_node_features *p =
1103 (struct ng_btsocket_hci_raw_node_features *) data;
1105 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1106 NGM_HCI_NODE_GET_FEATURES,
1107 &p->features, sizeof(p->features));
1110 case SIOC_HCI_RAW_NODE_GET_STAT: {
1111 struct ng_btsocket_hci_raw_node_stat *p =
1112 (struct ng_btsocket_hci_raw_node_stat *) data;
1114 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1115 NGM_HCI_NODE_GET_STAT,
1116 &p->stat, sizeof(p->stat));
1119 case SIOC_HCI_RAW_NODE_RESET_STAT:
1120 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1121 error = ng_btsocket_hci_raw_send_ngmsg(path,
1122 NGM_HCI_NODE_RESET_STAT, NULL, 0);
1127 case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE:
1128 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1129 error = ng_btsocket_hci_raw_send_ngmsg(path,
1130 NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE,
1136 case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE: {
1137 struct ng_btsocket_hci_raw_node_neighbor_cache *p =
1138 (struct ng_btsocket_hci_raw_node_neighbor_cache *) data;
1139 ng_hci_node_get_neighbor_cache_ep *p1 = NULL;
1140 ng_hci_node_neighbor_cache_entry_ep *p2 = NULL;
1142 if (p->num_entries <= 0 ||
1143 p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM ||
1144 p->entries == NULL) {
1149 NG_MKMESSAGE(msg, NGM_HCI_COOKIE,
1150 NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_WAITOK | M_NULLOK);
1155 ng_btsocket_hci_raw_get_token(&msg->header.token);
1156 pcb->token = msg->header.token;
1159 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1165 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1166 PZERO|PCATCH, "hcictl",
1167 ng_btsocket_hci_raw_ioctl_timeout * hz);
1173 if (pcb->msg != NULL &&
1174 pcb->msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) {
1175 /* Return data back to user space */
1176 p1 = (ng_hci_node_get_neighbor_cache_ep *)
1178 p2 = (ng_hci_node_neighbor_cache_entry_ep *)
1181 p->num_entries = min(p->num_entries, p1->num_entries);
1182 if (p->num_entries > 0)
1183 error = copyout((caddr_t) p2,
1184 (caddr_t) p->entries,
1185 p->num_entries * sizeof(*p2));
1189 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1192 case SIOC_HCI_RAW_NODE_GET_CON_LIST: {
1193 struct ng_btsocket_hci_raw_con_list *p =
1194 (struct ng_btsocket_hci_raw_con_list *) data;
1195 ng_hci_node_con_list_ep *p1 = NULL;
1196 ng_hci_node_con_ep *p2 = NULL;
1198 if (p->num_connections == 0 ||
1199 p->num_connections > NG_HCI_MAX_CON_NUM ||
1200 p->connections == NULL) {
1205 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
1206 0, M_WAITOK | M_NULLOK);
1211 ng_btsocket_hci_raw_get_token(&msg->header.token);
1212 pcb->token = msg->header.token;
1215 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1221 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1222 PZERO|PCATCH, "hcictl",
1223 ng_btsocket_hci_raw_ioctl_timeout * hz);
1229 if (pcb->msg != NULL &&
1230 pcb->msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) {
1231 /* Return data back to user space */
1232 p1 = (ng_hci_node_con_list_ep *)(pcb->msg->data);
1233 p2 = (ng_hci_node_con_ep *)(p1 + 1);
1235 p->num_connections = min(p->num_connections,
1236 p1->num_connections);
1237 if (p->num_connections > 0)
1238 error = copyout((caddr_t) p2,
1239 (caddr_t) p->connections,
1240 p->num_connections * sizeof(*p2));
1244 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1247 case SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK: {
1248 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1249 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1252 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1253 NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK,
1254 &p->policy_mask, sizeof(p->policy_mask));
1257 case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: {
1258 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1259 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1262 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1263 error = ng_btsocket_hci_raw_send_ngmsg(path,
1264 NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK,
1266 sizeof(p->policy_mask));
1271 case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: {
1272 struct ng_btsocket_hci_raw_node_packet_mask *p =
1273 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1275 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1276 NGM_HCI_NODE_GET_PACKET_MASK,
1277 &p->packet_mask, sizeof(p->packet_mask));
1280 case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: {
1281 struct ng_btsocket_hci_raw_node_packet_mask *p =
1282 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1284 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1285 error = ng_btsocket_hci_raw_send_ngmsg(path,
1286 NGM_HCI_NODE_SET_PACKET_MASK,
1288 sizeof(p->packet_mask));
1293 case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: {
1294 struct ng_btsocket_hci_raw_node_role_switch *p =
1295 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1297 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1298 NGM_HCI_NODE_GET_ROLE_SWITCH,
1299 &p->role_switch, sizeof(p->role_switch));
1302 case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: {
1303 struct ng_btsocket_hci_raw_node_role_switch *p =
1304 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1306 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1307 error = ng_btsocket_hci_raw_send_ngmsg(path,
1308 NGM_HCI_NODE_SET_ROLE_SWITCH,
1310 sizeof(p->role_switch));
1315 case SIOC_HCI_RAW_NODE_LIST_NAMES: {
1316 struct ng_btsocket_hci_raw_node_list_names *nl =
1317 (struct ng_btsocket_hci_raw_node_list_names *) data;
1318 struct nodeinfo *ni = nl->names;
1320 if (nl->num_names == 0) {
1325 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
1326 0, M_WAITOK | M_NULLOK);
1331 ng_btsocket_hci_raw_get_token(&msg->header.token);
1332 pcb->token = msg->header.token;
1335 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, ".:", 0);
1341 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1342 PZERO|PCATCH, "hcictl",
1343 ng_btsocket_hci_raw_ioctl_timeout * hz);
1349 if (pcb->msg != NULL && pcb->msg->header.cmd == NGM_LISTNAMES) {
1350 /* Return data back to user space */
1351 struct namelist *nl1 = (struct namelist *) pcb->msg->data;
1352 struct nodeinfo *ni1 = &nl1->nodeinfo[0];
1354 while (nl->num_names > 0 && nl1->numnames > 0) {
1355 if (strcmp(ni1->type, NG_HCI_NODE_TYPE) == 0) {
1356 error = copyout((caddr_t) ni1,
1370 nl->num_names = ni - nl->names;
1374 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1382 mtx_unlock(&pcb->pcb_mtx);
1385 } /* ng_btsocket_hci_raw_control */
1388 * Process getsockopt/setsockopt system calls
1392 ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1394 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1395 struct ng_btsocket_hci_raw_filter filter;
1400 if (ng_btsocket_hci_raw_node == NULL)
1403 if (sopt->sopt_level != SOL_HCI_RAW)
1406 mtx_lock(&pcb->pcb_mtx);
1408 switch (sopt->sopt_dir) {
1410 switch (sopt->sopt_name) {
1411 case SO_HCI_RAW_FILTER:
1412 error = sooptcopyout(sopt, &pcb->filter,
1413 sizeof(pcb->filter));
1416 case SO_HCI_RAW_DIRECTION:
1417 dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1418 error = sooptcopyout(sopt, &dir, sizeof(dir));
1428 switch (sopt->sopt_name) {
1429 case SO_HCI_RAW_FILTER:
1430 error = sooptcopyin(sopt, &filter, sizeof(filter),
1433 bcopy(&filter, &pcb->filter,
1434 sizeof(pcb->filter));
1437 case SO_HCI_RAW_DIRECTION:
1438 error = sooptcopyin(sopt, &dir, sizeof(dir),
1444 pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION;
1446 pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1460 mtx_unlock(&pcb->pcb_mtx);
1463 } /* ng_btsocket_hci_raw_ctloutput */
1466 * Detach raw HCI socket
1470 ng_btsocket_hci_raw_detach(struct socket *so)
1472 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1474 KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
1476 if (ng_btsocket_hci_raw_node == NULL)
1479 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
1480 mtx_lock(&pcb->pcb_mtx);
1482 LIST_REMOVE(pcb, next);
1484 mtx_unlock(&pcb->pcb_mtx);
1485 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
1487 mtx_destroy(&pcb->pcb_mtx);
1489 bzero(pcb, sizeof(*pcb));
1490 kfree(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1493 } /* ng_btsocket_hci_raw_detach */
1496 * Disconnect raw HCI socket
1500 ng_btsocket_hci_raw_disconnect(struct socket *so)
1502 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1506 if (ng_btsocket_hci_raw_node == NULL)
1509 mtx_lock(&pcb->pcb_mtx);
1510 soisdisconnected(so);
1511 mtx_unlock(&pcb->pcb_mtx);
1514 } /* ng_btsocket_hci_raw_disconnect */
1517 * Get socket peer's address
1521 ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1523 return (ng_btsocket_hci_raw_sockaddr(so, nam));
1524 } /* ng_btsocket_hci_raw_peeraddr */
1531 ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
1532 struct sockaddr *sa, struct mbuf *control, struct thread *td)
1534 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1535 struct mbuf *nam = NULL;
1538 if (ng_btsocket_hci_raw_node == NULL) {
1546 if (control != NULL) {
1551 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1552 m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1557 if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1558 if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1563 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1568 mtx_lock(&pcb->pcb_mtx);
1570 error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1572 mtx_unlock(&pcb->pcb_mtx);
1577 if (pcb->addr.hci_node[0] == 0) {
1578 mtx_unlock(&pcb->pcb_mtx);
1579 error = EDESTADDRREQ;
1583 sa = (struct sockaddr *) &pcb->addr;
1586 MGET(nam, MB_DONTWAIT, MT_SONAME);
1588 mtx_unlock(&pcb->pcb_mtx);
1593 nam->m_len = sizeof(struct sockaddr_hci);
1594 bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1599 mtx_unlock(&pcb->pcb_mtx);
1601 return (ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1602 ng_btsocket_hci_raw_output, nam, 0));
1604 NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1609 } /* ng_btsocket_hci_raw_send */
1612 * Get socket address
1616 ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1618 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1619 struct sockaddr_hci sa;
1623 if (ng_btsocket_hci_raw_node == NULL)
1626 bzero(&sa, sizeof(sa));
1627 sa.hci_len = sizeof(sa);
1628 sa.hci_family = AF_BLUETOOTH;
1630 mtx_lock(&pcb->pcb_mtx);
1631 strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1632 mtx_unlock(&pcb->pcb_mtx);
1634 *nam = sodupsockaddr((struct sockaddr *) &sa, M_WAITOK | M_NULLOK);
1636 return ((*nam == NULL)? ENOMEM : 0);
1637 } /* ng_btsocket_hci_raw_sockaddr */