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 MALLOC(ng_btsocket_hci_raw_sec_filter,
787 struct ng_btsocket_hci_raw_sec_filter *,
788 sizeof(struct ng_btsocket_hci_raw_sec_filter),
789 M_NETGRAPH_BTSOCKET_HCI_RAW, M_WAITOK | M_NULLOK | M_ZERO);
790 if (ng_btsocket_hci_raw_sec_filter == NULL) {
791 printf("%s: Could not allocate security filter!\n", __func__);
796 * XXX How paranoid can we get?
798 * Initialize security filter. If bit is set in the mask then
799 * unprivileged socket is allowed to send (receive) this command
803 /* Enable all events */
804 memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff,
805 sizeof(ng_btsocket_hci_raw_sec_filter->events)/
806 sizeof(ng_btsocket_hci_raw_sec_filter->events[0]));
808 /* Disable some critical events */
809 f = ng_btsocket_hci_raw_sec_filter->events;
810 bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
811 bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
812 bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
814 /* Commands - Link control */
815 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1];
816 bit_set(f, NG_HCI_OCF_INQUIRY - 1);
817 bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
818 bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
819 bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
820 bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
821 bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
822 bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
823 bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
825 /* Commands - Link policy */
826 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1];
827 bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
828 bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1);
830 /* Commands - Host controller and baseband */
831 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1];
832 bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
833 bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
834 bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
835 bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
836 bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
837 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1);
838 bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1);
839 bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
840 bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
841 bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
842 bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
843 bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
844 bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1);
845 bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1);
846 bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
847 bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
848 bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1);
849 bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
850 bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
851 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
852 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
854 /* Commands - Informational */
855 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1];
856 bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
857 bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
858 bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
859 bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
860 bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
862 /* Commands - Status */
863 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1];
864 bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1);
865 bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
866 bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
868 /* Commands - Testing */
869 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1];
870 bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
871 } /* ng_btsocket_hci_raw_init */
874 * Abort connection on socket
878 ng_btsocket_hci_raw_abort(struct socket *so)
880 } /* ng_btsocket_hci_raw_abort */
883 ng_btsocket_hci_raw_close(struct socket *so)
885 } /* ng_btsocket_hci_raw_close */
888 * Create new raw HCI socket
892 ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
894 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
900 if (ng_btsocket_hci_raw_node == NULL)
901 return (EPROTONOSUPPORT);
902 if (proto != BLUETOOTH_PROTO_HCI)
903 return (EPROTONOSUPPORT);
904 if (so->so_type != SOCK_RAW)
905 return (ESOCKTNOSUPPORT);
907 error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
908 NG_BTSOCKET_HCI_RAW_RECVSPACE);
912 MALLOC(pcb, ng_btsocket_hci_raw_pcb_p, sizeof(*pcb),
913 M_NETGRAPH_BTSOCKET_HCI_RAW, M_WAITOK | M_NULLOK | M_ZERO);
917 so->so_pcb = (caddr_t) pcb;
920 if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
921 pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
924 * Set default socket filter. By default socket only accepts HCI
925 * Command_Complete and Command_Status event packets.
928 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
929 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
931 mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF);
933 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
934 LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
935 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
938 } /* ng_btsocket_hci_raw_attach */
941 * Bind raw HCI socket
945 ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam,
948 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
949 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
953 if (ng_btsocket_hci_raw_node == NULL)
958 if (sa->hci_family != AF_BLUETOOTH)
959 return (EAFNOSUPPORT);
960 if (sa->hci_len != sizeof(*sa))
962 if (sa->hci_node[0] == 0)
965 mtx_lock(&pcb->pcb_mtx);
966 bcopy(sa, &pcb->addr, sizeof(pcb->addr));
967 mtx_unlock(&pcb->pcb_mtx);
970 } /* ng_btsocket_hci_raw_bind */
973 * Connect raw HCI socket
977 ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam,
980 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
981 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
985 if (ng_btsocket_hci_raw_node == NULL)
990 if (sa->hci_family != AF_BLUETOOTH)
991 return (EAFNOSUPPORT);
992 if (sa->hci_len != sizeof(*sa))
994 if (sa->hci_node[0] == 0)
995 return (EDESTADDRREQ);
997 mtx_lock(&pcb->pcb_mtx);
999 if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
1000 mtx_unlock(&pcb->pcb_mtx);
1001 return (EADDRNOTAVAIL);
1006 mtx_unlock(&pcb->pcb_mtx);
1009 } /* ng_btsocket_hci_raw_connect */
1012 * Process ioctl on socket
1016 ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
1017 struct ifnet *ifp, struct thread *td)
1019 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1020 char path[NG_NODESIZ + 1];
1021 struct ng_mesg *msg = NULL;
1026 if (ng_btsocket_hci_raw_node == NULL)
1029 mtx_lock(&pcb->pcb_mtx);
1031 /* Check if we have device name */
1032 if (pcb->addr.hci_node[0] == 0) {
1033 mtx_unlock(&pcb->pcb_mtx);
1034 return (EHOSTUNREACH);
1037 /* Check if we have pending ioctl() */
1038 if (pcb->token != 0) {
1039 mtx_unlock(&pcb->pcb_mtx);
1043 snprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1046 case SIOC_HCI_RAW_NODE_GET_STATE: {
1047 struct ng_btsocket_hci_raw_node_state *p =
1048 (struct ng_btsocket_hci_raw_node_state *) data;
1050 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1051 NGM_HCI_NODE_GET_STATE,
1052 &p->state, sizeof(p->state));
1055 case SIOC_HCI_RAW_NODE_INIT:
1056 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1057 error = ng_btsocket_hci_raw_send_ngmsg(path,
1058 NGM_HCI_NODE_INIT, NULL, 0);
1063 case SIOC_HCI_RAW_NODE_GET_DEBUG: {
1064 struct ng_btsocket_hci_raw_node_debug *p =
1065 (struct ng_btsocket_hci_raw_node_debug *) data;
1067 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1068 NGM_HCI_NODE_GET_DEBUG,
1069 &p->debug, sizeof(p->debug));
1072 case SIOC_HCI_RAW_NODE_SET_DEBUG: {
1073 struct ng_btsocket_hci_raw_node_debug *p =
1074 (struct ng_btsocket_hci_raw_node_debug *) data;
1076 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1077 error = ng_btsocket_hci_raw_send_ngmsg(path,
1078 NGM_HCI_NODE_SET_DEBUG, &p->debug,
1084 case SIOC_HCI_RAW_NODE_GET_BUFFER: {
1085 struct ng_btsocket_hci_raw_node_buffer *p =
1086 (struct ng_btsocket_hci_raw_node_buffer *) data;
1088 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1089 NGM_HCI_NODE_GET_BUFFER,
1090 &p->buffer, sizeof(p->buffer));
1093 case SIOC_HCI_RAW_NODE_GET_BDADDR: {
1094 struct ng_btsocket_hci_raw_node_bdaddr *p =
1095 (struct ng_btsocket_hci_raw_node_bdaddr *) data;
1097 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1098 NGM_HCI_NODE_GET_BDADDR,
1099 &p->bdaddr, sizeof(p->bdaddr));
1102 case SIOC_HCI_RAW_NODE_GET_FEATURES: {
1103 struct ng_btsocket_hci_raw_node_features *p =
1104 (struct ng_btsocket_hci_raw_node_features *) data;
1106 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1107 NGM_HCI_NODE_GET_FEATURES,
1108 &p->features, sizeof(p->features));
1111 case SIOC_HCI_RAW_NODE_GET_STAT: {
1112 struct ng_btsocket_hci_raw_node_stat *p =
1113 (struct ng_btsocket_hci_raw_node_stat *) data;
1115 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1116 NGM_HCI_NODE_GET_STAT,
1117 &p->stat, sizeof(p->stat));
1120 case SIOC_HCI_RAW_NODE_RESET_STAT:
1121 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1122 error = ng_btsocket_hci_raw_send_ngmsg(path,
1123 NGM_HCI_NODE_RESET_STAT, NULL, 0);
1128 case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE:
1129 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1130 error = ng_btsocket_hci_raw_send_ngmsg(path,
1131 NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE,
1137 case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE: {
1138 struct ng_btsocket_hci_raw_node_neighbor_cache *p =
1139 (struct ng_btsocket_hci_raw_node_neighbor_cache *) data;
1140 ng_hci_node_get_neighbor_cache_ep *p1 = NULL;
1141 ng_hci_node_neighbor_cache_entry_ep *p2 = NULL;
1143 if (p->num_entries <= 0 ||
1144 p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM ||
1145 p->entries == NULL) {
1150 NG_MKMESSAGE(msg, NGM_HCI_COOKIE,
1151 NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_WAITOK | M_NULLOK);
1156 ng_btsocket_hci_raw_get_token(&msg->header.token);
1157 pcb->token = msg->header.token;
1160 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1166 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1167 PZERO|PCATCH, "hcictl",
1168 ng_btsocket_hci_raw_ioctl_timeout * hz);
1174 if (pcb->msg != NULL &&
1175 pcb->msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) {
1176 /* Return data back to user space */
1177 p1 = (ng_hci_node_get_neighbor_cache_ep *)
1179 p2 = (ng_hci_node_neighbor_cache_entry_ep *)
1182 p->num_entries = min(p->num_entries, p1->num_entries);
1183 if (p->num_entries > 0)
1184 error = copyout((caddr_t) p2,
1185 (caddr_t) p->entries,
1186 p->num_entries * sizeof(*p2));
1190 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1193 case SIOC_HCI_RAW_NODE_GET_CON_LIST: {
1194 struct ng_btsocket_hci_raw_con_list *p =
1195 (struct ng_btsocket_hci_raw_con_list *) data;
1196 ng_hci_node_con_list_ep *p1 = NULL;
1197 ng_hci_node_con_ep *p2 = NULL;
1199 if (p->num_connections == 0 ||
1200 p->num_connections > NG_HCI_MAX_CON_NUM ||
1201 p->connections == NULL) {
1206 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
1207 0, M_WAITOK | M_NULLOK);
1212 ng_btsocket_hci_raw_get_token(&msg->header.token);
1213 pcb->token = msg->header.token;
1216 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1222 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1223 PZERO|PCATCH, "hcictl",
1224 ng_btsocket_hci_raw_ioctl_timeout * hz);
1230 if (pcb->msg != NULL &&
1231 pcb->msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) {
1232 /* Return data back to user space */
1233 p1 = (ng_hci_node_con_list_ep *)(pcb->msg->data);
1234 p2 = (ng_hci_node_con_ep *)(p1 + 1);
1236 p->num_connections = min(p->num_connections,
1237 p1->num_connections);
1238 if (p->num_connections > 0)
1239 error = copyout((caddr_t) p2,
1240 (caddr_t) p->connections,
1241 p->num_connections * sizeof(*p2));
1245 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1248 case SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK: {
1249 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1250 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1253 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1254 NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK,
1255 &p->policy_mask, sizeof(p->policy_mask));
1258 case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: {
1259 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1260 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1263 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1264 error = ng_btsocket_hci_raw_send_ngmsg(path,
1265 NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK,
1267 sizeof(p->policy_mask));
1272 case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: {
1273 struct ng_btsocket_hci_raw_node_packet_mask *p =
1274 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1276 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1277 NGM_HCI_NODE_GET_PACKET_MASK,
1278 &p->packet_mask, sizeof(p->packet_mask));
1281 case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: {
1282 struct ng_btsocket_hci_raw_node_packet_mask *p =
1283 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1285 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1286 error = ng_btsocket_hci_raw_send_ngmsg(path,
1287 NGM_HCI_NODE_SET_PACKET_MASK,
1289 sizeof(p->packet_mask));
1294 case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: {
1295 struct ng_btsocket_hci_raw_node_role_switch *p =
1296 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1298 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1299 NGM_HCI_NODE_GET_ROLE_SWITCH,
1300 &p->role_switch, sizeof(p->role_switch));
1303 case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: {
1304 struct ng_btsocket_hci_raw_node_role_switch *p =
1305 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1307 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1308 error = ng_btsocket_hci_raw_send_ngmsg(path,
1309 NGM_HCI_NODE_SET_ROLE_SWITCH,
1311 sizeof(p->role_switch));
1316 case SIOC_HCI_RAW_NODE_LIST_NAMES: {
1317 struct ng_btsocket_hci_raw_node_list_names *nl =
1318 (struct ng_btsocket_hci_raw_node_list_names *) data;
1319 struct nodeinfo *ni = nl->names;
1321 if (nl->num_names == 0) {
1326 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
1327 0, M_WAITOK | M_NULLOK);
1332 ng_btsocket_hci_raw_get_token(&msg->header.token);
1333 pcb->token = msg->header.token;
1336 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, ".:", 0);
1342 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1343 PZERO|PCATCH, "hcictl",
1344 ng_btsocket_hci_raw_ioctl_timeout * hz);
1350 if (pcb->msg != NULL && pcb->msg->header.cmd == NGM_LISTNAMES) {
1351 /* Return data back to user space */
1352 struct namelist *nl1 = (struct namelist *) pcb->msg->data;
1353 struct nodeinfo *ni1 = &nl1->nodeinfo[0];
1355 while (nl->num_names > 0 && nl1->numnames > 0) {
1356 if (strcmp(ni1->type, NG_HCI_NODE_TYPE) == 0) {
1357 error = copyout((caddr_t) ni1,
1371 nl->num_names = ni - nl->names;
1375 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1383 mtx_unlock(&pcb->pcb_mtx);
1386 } /* ng_btsocket_hci_raw_control */
1389 * Process getsockopt/setsockopt system calls
1393 ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1395 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1396 struct ng_btsocket_hci_raw_filter filter;
1401 if (ng_btsocket_hci_raw_node == NULL)
1404 if (sopt->sopt_level != SOL_HCI_RAW)
1407 mtx_lock(&pcb->pcb_mtx);
1409 switch (sopt->sopt_dir) {
1411 switch (sopt->sopt_name) {
1412 case SO_HCI_RAW_FILTER:
1413 error = sooptcopyout(sopt, &pcb->filter,
1414 sizeof(pcb->filter));
1417 case SO_HCI_RAW_DIRECTION:
1418 dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1419 error = sooptcopyout(sopt, &dir, sizeof(dir));
1429 switch (sopt->sopt_name) {
1430 case SO_HCI_RAW_FILTER:
1431 error = sooptcopyin(sopt, &filter, sizeof(filter),
1434 bcopy(&filter, &pcb->filter,
1435 sizeof(pcb->filter));
1438 case SO_HCI_RAW_DIRECTION:
1439 error = sooptcopyin(sopt, &dir, sizeof(dir),
1445 pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION;
1447 pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1461 mtx_unlock(&pcb->pcb_mtx);
1464 } /* ng_btsocket_hci_raw_ctloutput */
1467 * Detach raw HCI socket
1471 ng_btsocket_hci_raw_detach(struct socket *so)
1473 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1475 KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
1477 if (ng_btsocket_hci_raw_node == NULL)
1480 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
1481 mtx_lock(&pcb->pcb_mtx);
1483 LIST_REMOVE(pcb, next);
1485 mtx_unlock(&pcb->pcb_mtx);
1486 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
1488 mtx_destroy(&pcb->pcb_mtx);
1490 bzero(pcb, sizeof(*pcb));
1491 FREE(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1494 } /* ng_btsocket_hci_raw_detach */
1497 * Disconnect raw HCI socket
1501 ng_btsocket_hci_raw_disconnect(struct socket *so)
1503 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1507 if (ng_btsocket_hci_raw_node == NULL)
1510 mtx_lock(&pcb->pcb_mtx);
1511 soisdisconnected(so);
1512 mtx_unlock(&pcb->pcb_mtx);
1515 } /* ng_btsocket_hci_raw_disconnect */
1518 * Get socket peer's address
1522 ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1524 return (ng_btsocket_hci_raw_sockaddr(so, nam));
1525 } /* ng_btsocket_hci_raw_peeraddr */
1532 ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
1533 struct sockaddr *sa, struct mbuf *control, struct thread *td)
1535 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1536 struct mbuf *nam = NULL;
1539 if (ng_btsocket_hci_raw_node == NULL) {
1547 if (control != NULL) {
1552 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1553 m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1558 if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1559 if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1564 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1569 mtx_lock(&pcb->pcb_mtx);
1571 error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1573 mtx_unlock(&pcb->pcb_mtx);
1578 if (pcb->addr.hci_node[0] == 0) {
1579 mtx_unlock(&pcb->pcb_mtx);
1580 error = EDESTADDRREQ;
1584 sa = (struct sockaddr *) &pcb->addr;
1587 MGET(nam, MB_DONTWAIT, MT_SONAME);
1589 mtx_unlock(&pcb->pcb_mtx);
1594 nam->m_len = sizeof(struct sockaddr_hci);
1595 bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1600 mtx_unlock(&pcb->pcb_mtx);
1602 return (ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1603 ng_btsocket_hci_raw_output, nam, 0));
1605 NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1610 } /* ng_btsocket_hci_raw_send */
1613 * Get socket address
1617 ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1619 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1620 struct sockaddr_hci sa;
1624 if (ng_btsocket_hci_raw_node == NULL)
1627 bzero(&sa, sizeof(sa));
1628 sa.hci_len = sizeof(sa);
1629 sa.hci_family = AF_BLUETOOTH;
1631 mtx_lock(&pcb->pcb_mtx);
1632 strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1633 mtx_unlock(&pcb->pcb_mtx);
1635 *nam = sodupsockaddr((struct sockaddr *) &sa, M_WAITOK | M_NULLOK);
1637 return ((*nam == NULL)? ENOMEM : 0);
1638 } /* ng_btsocket_hci_raw_sockaddr */