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 $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bitstring.h>
37 #include <sys/domain.h>
38 #include <sys/endian.h>
39 #include <sys/errno.h>
40 #include <sys/filedesc.h>
41 #include <sys/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 "ng_message.h"
55 #include "bluetooth/include/ng_bluetooth.h"
56 #include "bluetooth/include/ng_hci.h"
57 #include "bluetooth/include/ng_l2cap.h"
58 #include "bluetooth/include/ng_btsocket.h"
59 #include "bluetooth/include/ng_btsocket_hci_raw.h"
62 #ifdef NG_SEPARATE_MALLOC
63 MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_HCI_RAW, "netgraph_btsocks_hci_raw",
64 "Netgraph Bluetooth raw HCI sockets");
66 #define M_NETGRAPH_BTSOCKET_HCI_RAW M_NETGRAPH
67 #endif /* NG_SEPARATE_MALLOC */
69 /* Netgraph node methods */
70 static ng_constructor_t ng_btsocket_hci_raw_node_constructor;
71 static ng_rcvmsg_t ng_btsocket_hci_raw_node_rcvmsg;
72 static ng_shutdown_t ng_btsocket_hci_raw_node_shutdown;
73 static ng_newhook_t ng_btsocket_hci_raw_node_newhook;
74 static ng_connect_t ng_btsocket_hci_raw_node_connect;
75 static ng_rcvdata_t ng_btsocket_hci_raw_node_rcvdata;
76 static ng_disconnect_t ng_btsocket_hci_raw_node_disconnect;
78 static void ng_btsocket_hci_raw_input (void *, int);
79 static void ng_btsocket_hci_raw_output(node_p, hook_p, void *, int);
80 static void ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p,
83 static int ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p,
86 #define ng_btsocket_hci_raw_wakeup_input_task() \
87 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_hci_raw_task)
90 struct ng_btsocket_hci_raw_sec_filter {
91 bitstr_t bit_decl(events, 0xff);
92 bitstr_t bit_decl(commands[0x3f], 0x3ff);
95 /* Netgraph type descriptor */
96 static struct ng_type typestruct = {
97 .version = NG_ABI_VERSION,
98 .name = NG_BTSOCKET_HCI_RAW_NODE_TYPE,
99 .constructor = ng_btsocket_hci_raw_node_constructor,
100 .rcvmsg = ng_btsocket_hci_raw_node_rcvmsg,
101 .shutdown = ng_btsocket_hci_raw_node_shutdown,
102 .newhook = ng_btsocket_hci_raw_node_newhook,
103 .connect = ng_btsocket_hci_raw_node_connect,
104 .rcvdata = ng_btsocket_hci_raw_node_rcvdata,
105 .disconnect = ng_btsocket_hci_raw_node_disconnect,
109 extern int ifqmaxlen;
110 static u_int32_t ng_btsocket_hci_raw_debug_level;
111 static u_int32_t ng_btsocket_hci_raw_ioctl_timeout;
112 static node_p ng_btsocket_hci_raw_node;
113 static struct ng_bt_itemq ng_btsocket_hci_raw_queue;
114 static struct mtx ng_btsocket_hci_raw_queue_mtx;
115 static struct task ng_btsocket_hci_raw_task;
116 static LIST_HEAD(, ng_btsocket_hci_raw_pcb) ng_btsocket_hci_raw_sockets;
117 static struct mtx ng_btsocket_hci_raw_sockets_mtx;
118 static u_int32_t ng_btsocket_hci_raw_token;
119 static struct mtx ng_btsocket_hci_raw_token_mtx;
120 static struct ng_btsocket_hci_raw_sec_filter *ng_btsocket_hci_raw_sec_filter;
123 SYSCTL_DECL(_net_bluetooth_hci_sockets);
124 SYSCTL_NODE(_net_bluetooth_hci_sockets, OID_AUTO, raw, CTLFLAG_RW,
125 0, "Bluetooth raw HCI sockets family");
126 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, debug_level, CTLFLAG_RW,
127 &ng_btsocket_hci_raw_debug_level, NG_BTSOCKET_WARN_LEVEL,
128 "Bluetooth raw HCI sockets debug level");
129 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, ioctl_timeout, CTLFLAG_RW,
130 &ng_btsocket_hci_raw_ioctl_timeout, 5,
131 "Bluetooth raw HCI sockets ioctl timeout");
132 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_len, CTLFLAG_RD,
133 &ng_btsocket_hci_raw_queue.len, 0,
134 "Bluetooth raw HCI sockets input queue length");
135 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_maxlen, CTLFLAG_RD,
136 &ng_btsocket_hci_raw_queue.maxlen, 0,
137 "Bluetooth raw HCI sockets input queue max. length");
138 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_drops, CTLFLAG_RD,
139 &ng_btsocket_hci_raw_queue.drops, 0,
140 "Bluetooth raw HCI sockets input queue drops");
143 #define NG_BTSOCKET_HCI_RAW_INFO \
144 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL) \
147 #define NG_BTSOCKET_HCI_RAW_WARN \
148 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL) \
151 #define NG_BTSOCKET_HCI_RAW_ERR \
152 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL) \
155 #define NG_BTSOCKET_HCI_RAW_ALERT \
156 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL) \
159 /****************************************************************************
160 ****************************************************************************
162 ****************************************************************************
163 ****************************************************************************/
166 * Netgraph node constructor. Do not allow to create node of this type.
170 ng_btsocket_hci_raw_node_constructor(node_p node)
173 } /* ng_btsocket_hci_raw_node_constructor */
176 * Netgraph node destructor. Just let old node go and create new fresh one.
180 ng_btsocket_hci_raw_node_shutdown(node_p node)
186 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
188 NG_BTSOCKET_HCI_RAW_ALERT(
189 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
191 ng_btsocket_hci_raw_node = NULL;
196 error = ng_name_node(ng_btsocket_hci_raw_node,
197 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
199 NG_BTSOCKET_HCI_RAW_ALERT(
200 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
202 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
203 ng_btsocket_hci_raw_node = NULL;
209 } /* ng_btsocket_hci_raw_node_shutdown */
212 * Create new hook. Just say "yes"
216 ng_btsocket_hci_raw_node_newhook(node_p node, hook_p hook, char const *name)
219 } /* ng_btsocket_hci_raw_node_newhook */
222 * Connect hook. Just say "yes"
226 ng_btsocket_hci_raw_node_connect(hook_p hook)
229 } /* ng_btsocket_hci_raw_node_connect */
236 ng_btsocket_hci_raw_node_disconnect(hook_p hook)
239 } /* ng_btsocket_hci_raw_node_disconnect */
242 * Receive control message.
243 * Make sure it is a message from HCI node and it is a response.
244 * Enqueue item and schedule input task.
248 ng_btsocket_hci_raw_node_rcvmsg(node_p node, item_p item, hook_p lasthook)
250 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
254 * Check for empty sockets list creates LOR when both sender and
255 * receiver device are connected to the same host, so remove it
260 (msg->header.typecookie == NGM_HCI_COOKIE ||
261 msg->header.typecookie == NGM_GENERIC_COOKIE) &&
262 msg->header.flags & NGF_RESP) {
263 if (msg->header.token == 0) {
268 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
269 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
270 NG_BTSOCKET_HCI_RAW_ERR(
271 "%s: Input queue is full\n", __func__);
273 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
277 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
278 error = ng_btsocket_hci_raw_wakeup_input_task();
280 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
287 } /* ng_btsocket_hci_raw_node_rcvmsg */
290 * Receive packet from the one of our hook.
291 * Prepend every packet with sockaddr_hci and record sender's node name.
292 * Enqueue item and schedule input task.
296 ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item)
298 struct mbuf *nam = NULL;
302 * Check for empty sockets list creates LOR when both sender and
303 * receiver device are connected to the same host, so remove it
307 MGET(nam, MB_DONTWAIT, MT_SONAME);
309 struct sockaddr_hci *sa = mtod(nam, struct sockaddr_hci *);
311 nam->m_len = sizeof(struct sockaddr_hci);
313 sa->hci_len = sizeof(*sa);
314 sa->hci_family = AF_BLUETOOTH;
315 strlcpy(sa->hci_node, NG_PEER_NODE_NAME(hook),
316 sizeof(sa->hci_node));
318 NGI_GET_M(item, nam->m_next);
321 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
322 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
323 NG_BTSOCKET_HCI_RAW_ERR(
324 "%s: Input queue is full\n", __func__);
326 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
330 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
331 error = ng_btsocket_hci_raw_wakeup_input_task();
333 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
335 NG_BTSOCKET_HCI_RAW_ERR(
336 "%s: Failed to allocate address mbuf\n", __func__);
343 } /* ng_btsocket_hci_raw_node_rcvdata */
345 /****************************************************************************
346 ****************************************************************************
348 ****************************************************************************
349 ****************************************************************************/
352 * Get next token. We need token to avoid theoretical race where process
353 * submits ioctl() message then interrupts ioctl() and re-submits another
354 * ioctl() on the same socket *before* first ioctl() complete.
358 ng_btsocket_hci_raw_get_token(u_int32_t *token)
360 mtx_lock(&ng_btsocket_hci_raw_token_mtx);
362 if (++ ng_btsocket_hci_raw_token == 0)
363 ng_btsocket_hci_raw_token = 1;
365 *token = ng_btsocket_hci_raw_token;
367 mtx_unlock(&ng_btsocket_hci_raw_token_mtx);
368 } /* ng_btsocket_hci_raw_get_token */
371 * Send Netgraph message to the node - do not expect reply
375 ng_btsocket_hci_raw_send_ngmsg(char *path, int cmd, void *arg, int arglen)
377 struct ng_mesg *msg = NULL;
380 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, arglen, M_WAITOK | M_NULLOK);
384 if (arg != NULL && arglen > 0)
385 bcopy(arg, msg->data, arglen);
387 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
390 } /* ng_btsocket_hci_raw_send_ngmsg */
393 * Send Netgraph message to the node (no data) and wait for reply
397 ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path,
398 int cmd, void *rsp, int rsplen)
400 struct ng_mesg *msg = NULL;
403 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
405 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
409 ng_btsocket_hci_raw_get_token(&msg->header.token);
410 pcb->token = msg->header.token;
413 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
419 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "hcictl",
420 ng_btsocket_hci_raw_ioctl_timeout * hz);
426 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
427 bcopy(pcb->msg->data, rsp, rsplen);
431 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
434 } /* ng_btsocket_hci_raw_send_sync_ngmsg */
437 * Create control information for the packet
441 ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf **ctl,
447 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
449 if (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION) {
450 dir = (m->m_flags & M_PROTO1)? 1 : 0;
451 *ctl = sbcreatecontrol((caddr_t) &dir, sizeof(dir),
452 SCM_HCI_RAW_DIRECTION, SOL_HCI_RAW);
454 ctl = &((*ctl)->m_next);
457 if (pcb->so->so_options & SO_TIMESTAMP) {
459 *ctl = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
460 SCM_TIMESTAMP, SOL_SOCKET);
462 ctl = &((*ctl)->m_next);
464 } /* ng_btsocket_hci_raw_savctl */
467 * Raw HCI sockets data input routine
471 ng_btsocket_hci_raw_data_input(struct mbuf *nam)
473 ng_btsocket_hci_raw_pcb_p pcb = NULL;
474 struct mbuf *m0 = NULL, *m = NULL;
475 struct sockaddr_hci *sa = NULL;
480 KASSERT((nam->m_type == MT_SONAME),
481 ("%s: m_type=%d\n", __func__, nam->m_type));
482 KASSERT((m0->m_flags & M_PKTHDR),
483 ("%s: m_flags=%#x\n", __func__, m0->m_flags));
485 sa = mtod(nam, struct sockaddr_hci *);
487 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
489 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
491 mtx_lock(&pcb->pcb_mtx);
494 * If socket was bound then check address and
495 * make sure it matches.
498 if (pcb->addr.hci_node[0] != 0 &&
499 strcmp(sa->hci_node, pcb->addr.hci_node) != 0)
503 * Check packet against filters
504 * XXX do we have to call m_pullup() here?
507 if (ng_btsocket_hci_raw_filter(pcb, m0, 1) != 0)
511 * Make a copy of the packet, append to the socket's
512 * receive queue and wakeup socket. sbappendaddr()
513 * will check if socket has enough buffer space.
516 m = m_dup(m0, MB_DONTWAIT);
518 struct mbuf *ctl = NULL;
520 ng_btsocket_hci_raw_savctl(pcb, &ctl, m);
522 if (sbappendaddr(&pcb->so->so_rcv,
523 (struct sockaddr *) sa, m, ctl))
526 NG_BTSOCKET_HCI_RAW_INFO(
527 "%s: sbappendaddr() failed\n", __func__);
534 mtx_unlock(&pcb->pcb_mtx);
537 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
541 } /* ng_btsocket_hci_raw_data_input */
544 * Raw HCI sockets message input routine
548 ng_btsocket_hci_raw_msg_input(struct ng_mesg *msg)
550 ng_btsocket_hci_raw_pcb_p pcb = NULL;
552 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
554 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
555 mtx_lock(&pcb->pcb_mtx);
557 if (msg->header.token == pcb->token) {
561 mtx_unlock(&pcb->pcb_mtx);
562 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
567 mtx_unlock(&pcb->pcb_mtx);
570 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
572 NG_FREE_MSG(msg); /* checks for != NULL */
573 } /* ng_btsocket_hci_raw_msg_input */
576 * Raw HCI sockets input routines
580 ng_btsocket_hci_raw_input(void *context, int pending)
585 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
586 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_hci_raw_queue, item);
587 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
592 switch(item->el_flags & NGQF_TYPE) {
594 struct mbuf *m = NULL;
597 ng_btsocket_hci_raw_data_input(m);
601 struct ng_mesg *msg = NULL;
603 NGI_GET_MSG(item, msg);
604 ng_btsocket_hci_raw_msg_input(msg);
609 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
615 } /* ng_btsocket_hci_raw_input */
618 * Raw HCI sockets output routine
622 ng_btsocket_hci_raw_output(node_p node, hook_p hook, void *arg1, int arg2)
624 struct mbuf *nam = (struct mbuf *) arg1, *m = NULL;
625 struct sockaddr_hci *sa = NULL;
631 KASSERT((nam->m_type == MT_SONAME),
632 ("%s: m_type=%d\n", __func__, nam->m_type));
633 KASSERT((m->m_flags & M_PKTHDR),
634 ("%s: m_flags=%#x\n", __func__, m->m_flags));
636 sa = mtod(nam, struct sockaddr_hci *);
639 * Find downstream hook
640 * XXX For now access node hook list directly. Should be safe because
641 * we used ng_send_fn() and we should have exclusive lock on the node.
644 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
645 if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
646 NG_NODE_NOT_VALID(NG_PEER_NODE(hook)))
649 if (strcmp(sa->hci_node, NG_PEER_NODE_NAME(hook)) == 0) {
650 NG_SEND_DATA_ONLY(error, hook, m); /* sets m to NULL */
655 NG_FREE_M(nam); /* check for != NULL */
657 } /* ng_btsocket_hci_raw_output */
660 * Check frame against security and socket filters.
661 * d (direction bit) == 1 means incoming frame.
665 ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf *m, int d)
667 int type, event, opcode;
669 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
671 switch ((type = *mtod(m, u_int8_t *))) {
673 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)) {
674 opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode);
677 ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF(opcode) - 1],
678 NG_HCI_OCF(opcode) - 1))
682 if (d && !bit_test(pcb->filter.packet_mask, NG_HCI_CMD_PKT - 1))
686 case NG_HCI_ACL_DATA_PKT:
687 case NG_HCI_SCO_DATA_PKT:
688 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) ||
689 !bit_test(pcb->filter.packet_mask, type - 1) ||
694 case NG_HCI_EVENT_PKT:
698 event = mtod(m, ng_hci_event_pkt_t *)->event - 1;
700 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED))
701 if (!bit_test(ng_btsocket_hci_raw_sec_filter->events, event))
704 if (!bit_test(pcb->filter.event_mask, event))
713 } /* ng_btsocket_hci_raw_filter */
716 * Initialize everything
720 ng_btsocket_hci_raw_init(void)
725 ng_btsocket_hci_raw_node = NULL;
726 ng_btsocket_hci_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
727 ng_btsocket_hci_raw_ioctl_timeout = 5;
729 /* Register Netgraph node type */
730 error = ng_newtype(&typestruct);
732 NG_BTSOCKET_HCI_RAW_ALERT(
733 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
738 /* Create Netgrapg node */
739 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
741 NG_BTSOCKET_HCI_RAW_ALERT(
742 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
744 ng_btsocket_hci_raw_node = NULL;
749 error = ng_name_node(ng_btsocket_hci_raw_node,
750 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
752 NG_BTSOCKET_HCI_RAW_ALERT(
753 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
755 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
756 ng_btsocket_hci_raw_node = NULL;
761 /* Create input queue */
762 NG_BT_ITEMQ_INIT(&ng_btsocket_hci_raw_queue, ifqmaxlen);
763 mtx_init(&ng_btsocket_hci_raw_queue_mtx,
764 "btsocks_hci_raw_queue_mtx", NULL, MTX_DEF);
765 TASK_INIT(&ng_btsocket_hci_raw_task, 0,
766 ng_btsocket_hci_raw_input, NULL);
768 /* Create list of sockets */
769 LIST_INIT(&ng_btsocket_hci_raw_sockets);
770 mtx_init(&ng_btsocket_hci_raw_sockets_mtx,
771 "btsocks_hci_raw_sockets_mtx", NULL, MTX_DEF);
774 ng_btsocket_hci_raw_token = 0;
775 mtx_init(&ng_btsocket_hci_raw_token_mtx,
776 "btsocks_hci_raw_token_mtx", NULL, MTX_DEF);
783 ng_btsocket_hci_raw_sec_filter = NULL;
785 ng_btsocket_hci_raw_sec_filter = kmalloc(sizeof(struct ng_btsocket_hci_raw_sec_filter),
786 M_NETGRAPH_BTSOCKET_HCI_RAW,
787 M_WAITOK | M_NULLOK | M_ZERO);
788 if (ng_btsocket_hci_raw_sec_filter == NULL) {
789 kprintf("%s: Could not allocate security filter!\n", __func__);
794 * XXX How paranoid can we get?
796 * Initialize security filter. If bit is set in the mask then
797 * unprivileged socket is allowed to send (receive) this command
801 /* Enable all events */
802 memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff,
803 sizeof(ng_btsocket_hci_raw_sec_filter->events)/
804 sizeof(ng_btsocket_hci_raw_sec_filter->events[0]));
806 /* Disable some critical events */
807 f = ng_btsocket_hci_raw_sec_filter->events;
808 bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
809 bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
810 bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
812 /* Commands - Link control */
813 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1];
814 bit_set(f, NG_HCI_OCF_INQUIRY - 1);
815 bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
816 bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
817 bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
818 bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
819 bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
820 bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
821 bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
823 /* Commands - Link policy */
824 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1];
825 bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
826 bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1);
828 /* Commands - Host controller and baseband */
829 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1];
830 bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
831 bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
832 bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
833 bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
834 bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
835 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1);
836 bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1);
837 bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
838 bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
839 bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
840 bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
841 bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
842 bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1);
843 bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1);
844 bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
845 bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
846 bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1);
847 bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
848 bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
849 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
850 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
852 /* Commands - Informational */
853 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1];
854 bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
855 bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
856 bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
857 bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
858 bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
860 /* Commands - Status */
861 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1];
862 bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1);
863 bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
864 bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
866 /* Commands - Testing */
867 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1];
868 bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
869 } /* ng_btsocket_hci_raw_init */
872 * Abort connection on socket
876 ng_btsocket_hci_raw_abort(struct socket *so)
878 } /* ng_btsocket_hci_raw_abort */
881 ng_btsocket_hci_raw_close(struct socket *so)
883 } /* ng_btsocket_hci_raw_close */
886 * Create new raw HCI socket
890 ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
892 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
898 if (ng_btsocket_hci_raw_node == NULL)
899 return (EPROTONOSUPPORT);
900 if (proto != BLUETOOTH_PROTO_HCI)
901 return (EPROTONOSUPPORT);
902 if (so->so_type != SOCK_RAW)
903 return (ESOCKTNOSUPPORT);
905 error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
906 NG_BTSOCKET_HCI_RAW_RECVSPACE);
910 pcb = kmalloc(sizeof(*pcb), M_NETGRAPH_BTSOCKET_HCI_RAW,
911 M_WAITOK | M_NULLOK | M_ZERO);
915 so->so_pcb = (caddr_t) pcb;
918 if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
919 pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
922 * Set default socket filter. By default socket only accepts HCI
923 * Command_Complete and Command_Status event packets.
926 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
927 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
929 mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF);
931 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
932 LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
933 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
936 } /* ng_btsocket_hci_raw_attach */
939 * Bind raw HCI socket
943 ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam,
946 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
947 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
951 if (ng_btsocket_hci_raw_node == NULL)
956 if (sa->hci_family != AF_BLUETOOTH)
957 return (EAFNOSUPPORT);
958 if (sa->hci_len != sizeof(*sa))
960 if (sa->hci_node[0] == 0)
963 mtx_lock(&pcb->pcb_mtx);
964 bcopy(sa, &pcb->addr, sizeof(pcb->addr));
965 mtx_unlock(&pcb->pcb_mtx);
968 } /* ng_btsocket_hci_raw_bind */
971 * Connect raw HCI socket
975 ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam,
978 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
979 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
983 if (ng_btsocket_hci_raw_node == NULL)
988 if (sa->hci_family != AF_BLUETOOTH)
989 return (EAFNOSUPPORT);
990 if (sa->hci_len != sizeof(*sa))
992 if (sa->hci_node[0] == 0)
993 return (EDESTADDRREQ);
995 mtx_lock(&pcb->pcb_mtx);
997 if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
998 mtx_unlock(&pcb->pcb_mtx);
999 return (EADDRNOTAVAIL);
1004 mtx_unlock(&pcb->pcb_mtx);
1007 } /* ng_btsocket_hci_raw_connect */
1010 * Process ioctl on socket
1014 ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
1015 struct ifnet *ifp, struct thread *td)
1017 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1018 char path[NG_NODESIZ + 1];
1019 struct ng_mesg *msg = NULL;
1024 if (ng_btsocket_hci_raw_node == NULL)
1027 mtx_lock(&pcb->pcb_mtx);
1029 /* Check if we have device name */
1030 if (pcb->addr.hci_node[0] == 0) {
1031 mtx_unlock(&pcb->pcb_mtx);
1032 return (EHOSTUNREACH);
1035 /* Check if we have pending ioctl() */
1036 if (pcb->token != 0) {
1037 mtx_unlock(&pcb->pcb_mtx);
1041 ksnprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1044 case SIOC_HCI_RAW_NODE_GET_STATE: {
1045 struct ng_btsocket_hci_raw_node_state *p =
1046 (struct ng_btsocket_hci_raw_node_state *) data;
1048 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1049 NGM_HCI_NODE_GET_STATE,
1050 &p->state, sizeof(p->state));
1053 case SIOC_HCI_RAW_NODE_INIT:
1054 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1055 error = ng_btsocket_hci_raw_send_ngmsg(path,
1056 NGM_HCI_NODE_INIT, NULL, 0);
1061 case SIOC_HCI_RAW_NODE_GET_DEBUG: {
1062 struct ng_btsocket_hci_raw_node_debug *p =
1063 (struct ng_btsocket_hci_raw_node_debug *) data;
1065 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1066 NGM_HCI_NODE_GET_DEBUG,
1067 &p->debug, sizeof(p->debug));
1070 case SIOC_HCI_RAW_NODE_SET_DEBUG: {
1071 struct ng_btsocket_hci_raw_node_debug *p =
1072 (struct ng_btsocket_hci_raw_node_debug *) data;
1074 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1075 error = ng_btsocket_hci_raw_send_ngmsg(path,
1076 NGM_HCI_NODE_SET_DEBUG, &p->debug,
1082 case SIOC_HCI_RAW_NODE_GET_BUFFER: {
1083 struct ng_btsocket_hci_raw_node_buffer *p =
1084 (struct ng_btsocket_hci_raw_node_buffer *) data;
1086 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1087 NGM_HCI_NODE_GET_BUFFER,
1088 &p->buffer, sizeof(p->buffer));
1091 case SIOC_HCI_RAW_NODE_GET_BDADDR: {
1092 struct ng_btsocket_hci_raw_node_bdaddr *p =
1093 (struct ng_btsocket_hci_raw_node_bdaddr *) data;
1095 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1096 NGM_HCI_NODE_GET_BDADDR,
1097 &p->bdaddr, sizeof(p->bdaddr));
1100 case SIOC_HCI_RAW_NODE_GET_FEATURES: {
1101 struct ng_btsocket_hci_raw_node_features *p =
1102 (struct ng_btsocket_hci_raw_node_features *) data;
1104 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1105 NGM_HCI_NODE_GET_FEATURES,
1106 &p->features, sizeof(p->features));
1109 case SIOC_HCI_RAW_NODE_GET_STAT: {
1110 struct ng_btsocket_hci_raw_node_stat *p =
1111 (struct ng_btsocket_hci_raw_node_stat *) data;
1113 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1114 NGM_HCI_NODE_GET_STAT,
1115 &p->stat, sizeof(p->stat));
1118 case SIOC_HCI_RAW_NODE_RESET_STAT:
1119 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1120 error = ng_btsocket_hci_raw_send_ngmsg(path,
1121 NGM_HCI_NODE_RESET_STAT, NULL, 0);
1126 case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE:
1127 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1128 error = ng_btsocket_hci_raw_send_ngmsg(path,
1129 NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE,
1135 case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE: {
1136 struct ng_btsocket_hci_raw_node_neighbor_cache *p =
1137 (struct ng_btsocket_hci_raw_node_neighbor_cache *) data;
1138 ng_hci_node_get_neighbor_cache_ep *p1 = NULL;
1139 ng_hci_node_neighbor_cache_entry_ep *p2 = NULL;
1141 if (p->num_entries <= 0 ||
1142 p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM ||
1143 p->entries == NULL) {
1148 NG_MKMESSAGE(msg, NGM_HCI_COOKIE,
1149 NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_WAITOK | M_NULLOK);
1154 ng_btsocket_hci_raw_get_token(&msg->header.token);
1155 pcb->token = msg->header.token;
1158 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1164 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1165 PZERO|PCATCH, "hcictl",
1166 ng_btsocket_hci_raw_ioctl_timeout * hz);
1172 if (pcb->msg != NULL &&
1173 pcb->msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) {
1174 /* Return data back to user space */
1175 p1 = (ng_hci_node_get_neighbor_cache_ep *)
1177 p2 = (ng_hci_node_neighbor_cache_entry_ep *)
1180 p->num_entries = min(p->num_entries, p1->num_entries);
1181 if (p->num_entries > 0)
1182 error = copyout((caddr_t) p2,
1183 (caddr_t) p->entries,
1184 p->num_entries * sizeof(*p2));
1188 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1191 case SIOC_HCI_RAW_NODE_GET_CON_LIST: {
1192 struct ng_btsocket_hci_raw_con_list *p =
1193 (struct ng_btsocket_hci_raw_con_list *) data;
1194 ng_hci_node_con_list_ep *p1 = NULL;
1195 ng_hci_node_con_ep *p2 = NULL;
1197 if (p->num_connections == 0 ||
1198 p->num_connections > NG_HCI_MAX_CON_NUM ||
1199 p->connections == NULL) {
1204 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
1205 0, M_WAITOK | M_NULLOK);
1210 ng_btsocket_hci_raw_get_token(&msg->header.token);
1211 pcb->token = msg->header.token;
1214 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1220 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1221 PZERO|PCATCH, "hcictl",
1222 ng_btsocket_hci_raw_ioctl_timeout * hz);
1228 if (pcb->msg != NULL &&
1229 pcb->msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) {
1230 /* Return data back to user space */
1231 p1 = (ng_hci_node_con_list_ep *)(pcb->msg->data);
1232 p2 = (ng_hci_node_con_ep *)(p1 + 1);
1234 p->num_connections = min(p->num_connections,
1235 p1->num_connections);
1236 if (p->num_connections > 0)
1237 error = copyout((caddr_t) p2,
1238 (caddr_t) p->connections,
1239 p->num_connections * sizeof(*p2));
1243 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1246 case SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK: {
1247 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1248 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1251 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1252 NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK,
1253 &p->policy_mask, sizeof(p->policy_mask));
1256 case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: {
1257 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1258 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1261 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1262 error = ng_btsocket_hci_raw_send_ngmsg(path,
1263 NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK,
1265 sizeof(p->policy_mask));
1270 case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: {
1271 struct ng_btsocket_hci_raw_node_packet_mask *p =
1272 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1274 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1275 NGM_HCI_NODE_GET_PACKET_MASK,
1276 &p->packet_mask, sizeof(p->packet_mask));
1279 case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: {
1280 struct ng_btsocket_hci_raw_node_packet_mask *p =
1281 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1283 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1284 error = ng_btsocket_hci_raw_send_ngmsg(path,
1285 NGM_HCI_NODE_SET_PACKET_MASK,
1287 sizeof(p->packet_mask));
1292 case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: {
1293 struct ng_btsocket_hci_raw_node_role_switch *p =
1294 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1296 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1297 NGM_HCI_NODE_GET_ROLE_SWITCH,
1298 &p->role_switch, sizeof(p->role_switch));
1301 case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: {
1302 struct ng_btsocket_hci_raw_node_role_switch *p =
1303 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1305 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1306 error = ng_btsocket_hci_raw_send_ngmsg(path,
1307 NGM_HCI_NODE_SET_ROLE_SWITCH,
1309 sizeof(p->role_switch));
1314 case SIOC_HCI_RAW_NODE_LIST_NAMES: {
1315 struct ng_btsocket_hci_raw_node_list_names *nl =
1316 (struct ng_btsocket_hci_raw_node_list_names *) data;
1317 struct nodeinfo *ni = nl->names;
1319 if (nl->num_names == 0) {
1324 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
1325 0, M_WAITOK | M_NULLOK);
1330 ng_btsocket_hci_raw_get_token(&msg->header.token);
1331 pcb->token = msg->header.token;
1334 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, ".:", 0);
1340 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1341 PZERO|PCATCH, "hcictl",
1342 ng_btsocket_hci_raw_ioctl_timeout * hz);
1348 if (pcb->msg != NULL && pcb->msg->header.cmd == NGM_LISTNAMES) {
1349 /* Return data back to user space */
1350 struct namelist *nl1 = (struct namelist *) pcb->msg->data;
1351 struct nodeinfo *ni1 = &nl1->nodeinfo[0];
1353 while (nl->num_names > 0 && nl1->numnames > 0) {
1354 if (strcmp(ni1->type, NG_HCI_NODE_TYPE) == 0) {
1355 error = copyout((caddr_t) ni1,
1369 nl->num_names = ni - nl->names;
1373 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1381 mtx_unlock(&pcb->pcb_mtx);
1384 } /* ng_btsocket_hci_raw_control */
1387 * Process getsockopt/setsockopt system calls
1391 ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1393 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1394 struct ng_btsocket_hci_raw_filter filter;
1399 if (ng_btsocket_hci_raw_node == NULL)
1402 if (sopt->sopt_level != SOL_HCI_RAW)
1405 mtx_lock(&pcb->pcb_mtx);
1407 switch (sopt->sopt_dir) {
1409 switch (sopt->sopt_name) {
1410 case SO_HCI_RAW_FILTER:
1411 error = sooptcopyout(sopt, &pcb->filter,
1412 sizeof(pcb->filter));
1415 case SO_HCI_RAW_DIRECTION:
1416 dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1417 error = sooptcopyout(sopt, &dir, sizeof(dir));
1427 switch (sopt->sopt_name) {
1428 case SO_HCI_RAW_FILTER:
1429 error = sooptcopyin(sopt, &filter, sizeof(filter),
1432 bcopy(&filter, &pcb->filter,
1433 sizeof(pcb->filter));
1436 case SO_HCI_RAW_DIRECTION:
1437 error = sooptcopyin(sopt, &dir, sizeof(dir),
1443 pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION;
1445 pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1459 mtx_unlock(&pcb->pcb_mtx);
1462 } /* ng_btsocket_hci_raw_ctloutput */
1465 * Detach raw HCI socket
1469 ng_btsocket_hci_raw_detach(struct socket *so)
1471 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1473 KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
1475 if (ng_btsocket_hci_raw_node == NULL)
1478 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
1479 mtx_lock(&pcb->pcb_mtx);
1481 LIST_REMOVE(pcb, next);
1483 mtx_unlock(&pcb->pcb_mtx);
1484 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
1486 mtx_destroy(&pcb->pcb_mtx);
1488 bzero(pcb, sizeof(*pcb));
1489 kfree(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1492 } /* ng_btsocket_hci_raw_detach */
1495 * Disconnect raw HCI socket
1499 ng_btsocket_hci_raw_disconnect(struct socket *so)
1501 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1505 if (ng_btsocket_hci_raw_node == NULL)
1508 mtx_lock(&pcb->pcb_mtx);
1509 soisdisconnected(so);
1510 mtx_unlock(&pcb->pcb_mtx);
1513 } /* ng_btsocket_hci_raw_disconnect */
1516 * Get socket peer's address
1520 ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1522 return (ng_btsocket_hci_raw_sockaddr(so, nam));
1523 } /* ng_btsocket_hci_raw_peeraddr */
1530 ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
1531 struct sockaddr *sa, struct mbuf *control, struct thread *td)
1533 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1534 struct mbuf *nam = NULL;
1537 if (ng_btsocket_hci_raw_node == NULL) {
1545 if (control != NULL) {
1550 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1551 m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1556 if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1557 if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1562 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1567 mtx_lock(&pcb->pcb_mtx);
1569 error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1571 mtx_unlock(&pcb->pcb_mtx);
1576 if (pcb->addr.hci_node[0] == 0) {
1577 mtx_unlock(&pcb->pcb_mtx);
1578 error = EDESTADDRREQ;
1582 sa = (struct sockaddr *) &pcb->addr;
1585 MGET(nam, MB_DONTWAIT, MT_SONAME);
1587 mtx_unlock(&pcb->pcb_mtx);
1592 nam->m_len = sizeof(struct sockaddr_hci);
1593 bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1598 mtx_unlock(&pcb->pcb_mtx);
1600 return (ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1601 ng_btsocket_hci_raw_output, nam, 0));
1603 NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1608 } /* ng_btsocket_hci_raw_send */
1611 * Get socket address
1615 ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1617 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1618 struct sockaddr_hci sa;
1622 if (ng_btsocket_hci_raw_node == NULL)
1625 bzero(&sa, sizeof(sa));
1626 sa.hci_len = sizeof(sa);
1627 sa.hci_family = AF_BLUETOOTH;
1629 mtx_lock(&pcb->pcb_mtx);
1630 strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1631 mtx_unlock(&pcb->pcb_mtx);
1633 *nam = sodupsockaddr((struct sockaddr *) &sa, M_WAITOK | M_NULLOK);
1635 return ((*nam == NULL)? ENOMEM : 0);
1636 } /* ng_btsocket_hci_raw_sockaddr */