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 $
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/ioccom.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>
57 #include <netgraph/ng_message.h>
58 #include <netgraph/netgraph.h>
59 #include <netgraph/bluetooth/include/ng_bluetooth.h>
60 #include <netgraph/bluetooth/include/ng_hci.h>
61 #include <netgraph/bluetooth/include/ng_l2cap.h>
62 #include <netgraph/bluetooth/include/ng_btsocket.h>
63 #include <netgraph/bluetooth/include/ng_btsocket_hci_raw.h>
66 #ifdef NG_SEPARATE_MALLOC
67 static MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_HCI_RAW, "netgraph_btsocks_hci_raw",
68 "Netgraph Bluetooth raw HCI sockets");
70 #define M_NETGRAPH_BTSOCKET_HCI_RAW M_NETGRAPH
71 #endif /* NG_SEPARATE_MALLOC */
73 /* Netgraph node methods */
74 static ng_constructor_t ng_btsocket_hci_raw_node_constructor;
75 static ng_rcvmsg_t ng_btsocket_hci_raw_node_rcvmsg;
76 static ng_shutdown_t ng_btsocket_hci_raw_node_shutdown;
77 static ng_newhook_t ng_btsocket_hci_raw_node_newhook;
78 static ng_connect_t ng_btsocket_hci_raw_node_connect;
79 static ng_rcvdata_t ng_btsocket_hci_raw_node_rcvdata;
80 static ng_disconnect_t ng_btsocket_hci_raw_node_disconnect;
82 static void ng_btsocket_hci_raw_input (void *, int);
83 static void ng_btsocket_hci_raw_output(node_p, hook_p, void *, int);
84 static void ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p,
87 static int ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p,
90 #define ng_btsocket_hci_raw_wakeup_input_task() \
91 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_hci_raw_task)
94 struct ng_btsocket_hci_raw_sec_filter {
95 bitstr_t bit_decl(events, 0xff);
96 bitstr_t bit_decl(commands[0x3f], 0x3ff);
99 /* Netgraph type descriptor */
100 static struct ng_type typestruct = {
101 .version = NG_ABI_VERSION,
102 .name = NG_BTSOCKET_HCI_RAW_NODE_TYPE,
103 .constructor = ng_btsocket_hci_raw_node_constructor,
104 .rcvmsg = ng_btsocket_hci_raw_node_rcvmsg,
105 .shutdown = ng_btsocket_hci_raw_node_shutdown,
106 .newhook = ng_btsocket_hci_raw_node_newhook,
107 .connect = ng_btsocket_hci_raw_node_connect,
108 .rcvdata = ng_btsocket_hci_raw_node_rcvdata,
109 .disconnect = ng_btsocket_hci_raw_node_disconnect,
113 static u_int32_t ng_btsocket_hci_raw_debug_level;
114 static u_int32_t ng_btsocket_hci_raw_ioctl_timeout;
115 static node_p ng_btsocket_hci_raw_node;
116 static struct ng_bt_itemq ng_btsocket_hci_raw_queue;
117 static struct mtx ng_btsocket_hci_raw_queue_mtx;
118 static struct task ng_btsocket_hci_raw_task;
119 static LIST_HEAD(, ng_btsocket_hci_raw_pcb) ng_btsocket_hci_raw_sockets;
120 static struct mtx ng_btsocket_hci_raw_sockets_mtx;
121 static u_int32_t ng_btsocket_hci_raw_token;
122 static struct mtx ng_btsocket_hci_raw_token_mtx;
123 static struct ng_btsocket_hci_raw_sec_filter *ng_btsocket_hci_raw_sec_filter;
124 static struct timeval ng_btsocket_hci_raw_lasttime;
125 static int ng_btsocket_hci_raw_curpps;
128 SYSCTL_DECL(_net_bluetooth_hci_sockets);
129 static SYSCTL_NODE(_net_bluetooth_hci_sockets, OID_AUTO, raw, CTLFLAG_RW,
130 0, "Bluetooth raw HCI sockets family");
131 SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, debug_level, CTLFLAG_RW,
132 &ng_btsocket_hci_raw_debug_level, NG_BTSOCKET_WARN_LEVEL,
133 "Bluetooth raw HCI sockets debug level");
134 SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, ioctl_timeout, CTLFLAG_RW,
135 &ng_btsocket_hci_raw_ioctl_timeout, 5,
136 "Bluetooth raw HCI sockets ioctl timeout");
137 SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_len, CTLFLAG_RD,
138 &ng_btsocket_hci_raw_queue.len, 0,
139 "Bluetooth raw HCI sockets input queue length");
140 SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_maxlen, CTLFLAG_RD,
141 &ng_btsocket_hci_raw_queue.maxlen, 0,
142 "Bluetooth raw HCI sockets input queue max. length");
143 SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_drops, CTLFLAG_RD,
144 &ng_btsocket_hci_raw_queue.drops, 0,
145 "Bluetooth raw HCI sockets input queue drops");
148 #define NG_BTSOCKET_HCI_RAW_INFO \
149 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
150 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
153 #define NG_BTSOCKET_HCI_RAW_WARN \
154 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
155 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
158 #define NG_BTSOCKET_HCI_RAW_ERR \
159 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
160 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
163 #define NG_BTSOCKET_HCI_RAW_ALERT \
164 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
165 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
168 /****************************************************************************
169 ****************************************************************************
171 ****************************************************************************
172 ****************************************************************************/
175 * Netgraph node constructor. Do not allow to create node of this type.
179 ng_btsocket_hci_raw_node_constructor(node_p node)
182 } /* ng_btsocket_hci_raw_node_constructor */
185 * Netgraph node destructor. Just let old node go and create new fresh one.
189 ng_btsocket_hci_raw_node_shutdown(node_p node)
195 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
197 NG_BTSOCKET_HCI_RAW_ALERT(
198 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
200 ng_btsocket_hci_raw_node = NULL;
205 error = ng_name_node(ng_btsocket_hci_raw_node,
206 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
208 NG_BTSOCKET_HCI_RAW_ALERT(
209 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
211 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
212 ng_btsocket_hci_raw_node = NULL;
218 } /* ng_btsocket_hci_raw_node_shutdown */
221 * Create new hook. Just say "yes"
225 ng_btsocket_hci_raw_node_newhook(node_p node, hook_p hook, char const *name)
228 } /* ng_btsocket_hci_raw_node_newhook */
231 * Connect hook. Just say "yes"
235 ng_btsocket_hci_raw_node_connect(hook_p hook)
238 } /* ng_btsocket_hci_raw_node_connect */
245 ng_btsocket_hci_raw_node_disconnect(hook_p hook)
248 } /* ng_btsocket_hci_raw_node_disconnect */
251 * Receive control message.
252 * Make sure it is a message from HCI node and it is a response.
253 * Enqueue item and schedule input task.
257 ng_btsocket_hci_raw_node_rcvmsg(node_p node, item_p item, hook_p lasthook)
259 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
263 * Check for empty sockets list creates LOR when both sender and
264 * receiver device are connected to the same host, so remove it
269 (msg->header.typecookie == NGM_HCI_COOKIE ||
270 msg->header.typecookie == NGM_GENERIC_COOKIE) &&
271 msg->header.flags & NGF_RESP) {
272 if (msg->header.token == 0) {
277 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
278 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
279 NG_BTSOCKET_HCI_RAW_ERR(
280 "%s: Input queue is full\n", __func__);
282 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
286 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
287 error = ng_btsocket_hci_raw_wakeup_input_task();
289 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
296 } /* ng_btsocket_hci_raw_node_rcvmsg */
299 * Receive packet from the one of our hook.
300 * Prepend every packet with sockaddr_hci and record sender's node name.
301 * Enqueue item and schedule input task.
305 ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item)
307 struct mbuf *nam = NULL;
311 * Check for empty sockets list creates LOR when both sender and
312 * receiver device are connected to the same host, so remove it
316 MGET(nam, M_NOWAIT, MT_SONAME);
318 struct sockaddr_hci *sa = mtod(nam, struct sockaddr_hci *);
320 nam->m_len = sizeof(struct sockaddr_hci);
322 sa->hci_len = sizeof(*sa);
323 sa->hci_family = AF_BLUETOOTH;
324 strlcpy(sa->hci_node, NG_PEER_NODE_NAME(hook),
325 sizeof(sa->hci_node));
327 NGI_GET_M(item, nam->m_next);
330 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
331 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
332 NG_BTSOCKET_HCI_RAW_ERR(
333 "%s: Input queue is full\n", __func__);
335 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
339 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
340 error = ng_btsocket_hci_raw_wakeup_input_task();
342 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
344 NG_BTSOCKET_HCI_RAW_ERR(
345 "%s: Failed to allocate address mbuf\n", __func__);
352 } /* ng_btsocket_hci_raw_node_rcvdata */
354 /****************************************************************************
355 ****************************************************************************
357 ****************************************************************************
358 ****************************************************************************/
361 * Get next token. We need token to avoid theoretical race where process
362 * submits ioctl() message then interrupts ioctl() and re-submits another
363 * ioctl() on the same socket *before* first ioctl() complete.
367 ng_btsocket_hci_raw_get_token(u_int32_t *token)
369 mtx_lock(&ng_btsocket_hci_raw_token_mtx);
371 if (++ ng_btsocket_hci_raw_token == 0)
372 ng_btsocket_hci_raw_token = 1;
374 *token = ng_btsocket_hci_raw_token;
376 mtx_unlock(&ng_btsocket_hci_raw_token_mtx);
377 } /* ng_btsocket_hci_raw_get_token */
380 * Send Netgraph message to the node - do not expect reply
384 ng_btsocket_hci_raw_send_ngmsg(char *path, int cmd, void *arg, int arglen)
386 struct ng_mesg *msg = NULL;
389 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, arglen, M_NOWAIT);
393 if (arg != NULL && arglen > 0)
394 bcopy(arg, msg->data, arglen);
396 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
399 } /* ng_btsocket_hci_raw_send_ngmsg */
402 * Send Netgraph message to the node (no data) and wait for reply
406 ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path,
407 int cmd, void *rsp, int rsplen)
409 struct ng_mesg *msg = NULL;
412 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
414 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, 0, M_NOWAIT);
418 ng_btsocket_hci_raw_get_token(&msg->header.token);
419 pcb->token = msg->header.token;
422 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
428 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "hcictl",
429 ng_btsocket_hci_raw_ioctl_timeout * hz);
435 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
436 bcopy(pcb->msg->data, rsp, rsplen);
440 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
443 } /* ng_btsocket_hci_raw_send_sync_ngmsg */
446 * Create control information for the packet
450 ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf **ctl,
456 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
458 if (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION) {
459 dir = (m->m_flags & M_PROTO1)? 1 : 0;
460 *ctl = sbcreatecontrol((caddr_t) &dir, sizeof(dir),
461 SCM_HCI_RAW_DIRECTION, SOL_HCI_RAW);
463 ctl = &((*ctl)->m_next);
466 if (pcb->so->so_options & SO_TIMESTAMP) {
468 *ctl = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
469 SCM_TIMESTAMP, SOL_SOCKET);
471 ctl = &((*ctl)->m_next);
473 } /* ng_btsocket_hci_raw_savctl */
476 * Raw HCI sockets data input routine
480 ng_btsocket_hci_raw_data_input(struct mbuf *nam)
482 ng_btsocket_hci_raw_pcb_p pcb = NULL;
483 struct mbuf *m0 = NULL, *m = NULL;
484 struct sockaddr_hci *sa = NULL;
489 KASSERT((nam->m_type == MT_SONAME),
490 ("%s: m_type=%d\n", __func__, nam->m_type));
491 KASSERT((m0->m_flags & M_PKTHDR),
492 ("%s: m_flags=%#x\n", __func__, m0->m_flags));
494 sa = mtod(nam, struct sockaddr_hci *);
496 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
498 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
500 mtx_lock(&pcb->pcb_mtx);
503 * If socket was bound then check address and
504 * make sure it matches.
507 if (pcb->addr.hci_node[0] != 0 &&
508 strcmp(sa->hci_node, pcb->addr.hci_node) != 0)
512 * Check packet against filters
513 * XXX do we have to call m_pullup() here?
516 if (ng_btsocket_hci_raw_filter(pcb, m0, 1) != 0)
520 * Make a copy of the packet, append to the socket's
521 * receive queue and wakeup socket. sbappendaddr()
522 * will check if socket has enough buffer space.
525 m = m_dup(m0, M_NOWAIT);
527 struct mbuf *ctl = NULL;
529 ng_btsocket_hci_raw_savctl(pcb, &ctl, m);
531 if (sbappendaddr(&pcb->so->so_rcv,
532 (struct sockaddr *) sa, m, ctl))
535 NG_BTSOCKET_HCI_RAW_INFO(
536 "%s: sbappendaddr() failed\n", __func__);
543 mtx_unlock(&pcb->pcb_mtx);
546 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
550 } /* ng_btsocket_hci_raw_data_input */
553 * Raw HCI sockets message input routine
557 ng_btsocket_hci_raw_msg_input(struct ng_mesg *msg)
559 ng_btsocket_hci_raw_pcb_p pcb = NULL;
561 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
563 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
564 mtx_lock(&pcb->pcb_mtx);
566 if (msg->header.token == pcb->token) {
570 mtx_unlock(&pcb->pcb_mtx);
571 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
576 mtx_unlock(&pcb->pcb_mtx);
579 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
581 NG_FREE_MSG(msg); /* checks for != NULL */
582 } /* ng_btsocket_hci_raw_msg_input */
585 * Raw HCI sockets input routines
589 ng_btsocket_hci_raw_input(void *context, int pending)
594 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
595 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_hci_raw_queue, item);
596 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
601 switch(item->el_flags & NGQF_TYPE) {
603 struct mbuf *m = NULL;
606 ng_btsocket_hci_raw_data_input(m);
610 struct ng_mesg *msg = NULL;
612 NGI_GET_MSG(item, msg);
613 ng_btsocket_hci_raw_msg_input(msg);
618 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
624 } /* ng_btsocket_hci_raw_input */
627 * Raw HCI sockets output routine
631 ng_btsocket_hci_raw_output(node_p node, hook_p hook, void *arg1, int arg2)
633 struct mbuf *nam = (struct mbuf *) arg1, *m = NULL;
634 struct sockaddr_hci *sa = NULL;
640 KASSERT((nam->m_type == MT_SONAME),
641 ("%s: m_type=%d\n", __func__, nam->m_type));
642 KASSERT((m->m_flags & M_PKTHDR),
643 ("%s: m_flags=%#x\n", __func__, m->m_flags));
645 sa = mtod(nam, struct sockaddr_hci *);
648 * Find downstream hook
649 * XXX For now access node hook list directly. Should be safe because
650 * we used ng_send_fn() and we should have exclusive lock on the node.
653 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
654 if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
655 NG_NODE_NOT_VALID(NG_PEER_NODE(hook)))
658 if (strcmp(sa->hci_node, NG_PEER_NODE_NAME(hook)) == 0) {
659 NG_SEND_DATA_ONLY(error, hook, m); /* sets m to NULL */
664 NG_FREE_M(nam); /* check for != NULL */
666 } /* ng_btsocket_hci_raw_output */
669 * Check frame against security and socket filters.
670 * d (direction bit) == 1 means incoming frame.
674 ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf *m, int d)
676 int type, event, opcode;
678 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
680 switch ((type = *mtod(m, u_int8_t *))) {
682 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)) {
683 opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode);
686 ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF(opcode) - 1],
687 NG_HCI_OCF(opcode) - 1))
691 if (d && !bit_test(pcb->filter.packet_mask, NG_HCI_CMD_PKT - 1))
695 case NG_HCI_ACL_DATA_PKT:
696 case NG_HCI_SCO_DATA_PKT:
697 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) ||
698 !bit_test(pcb->filter.packet_mask, type - 1) ||
703 case NG_HCI_EVENT_PKT:
707 event = mtod(m, ng_hci_event_pkt_t *)->event - 1;
709 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED))
710 if (!bit_test(ng_btsocket_hci_raw_sec_filter->events, event))
713 if (!bit_test(pcb->filter.event_mask, event))
722 } /* ng_btsocket_hci_raw_filter */
725 * Initialize everything
729 ng_btsocket_hci_raw_init(void)
734 /* Skip initialization of globals for non-default instances. */
735 if (!IS_DEFAULT_VNET(curvnet))
738 ng_btsocket_hci_raw_node = NULL;
739 ng_btsocket_hci_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
740 ng_btsocket_hci_raw_ioctl_timeout = 5;
742 /* Register Netgraph node type */
743 error = ng_newtype(&typestruct);
745 NG_BTSOCKET_HCI_RAW_ALERT(
746 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
751 /* Create Netgrapg node */
752 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
754 NG_BTSOCKET_HCI_RAW_ALERT(
755 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
757 ng_btsocket_hci_raw_node = NULL;
762 error = ng_name_node(ng_btsocket_hci_raw_node,
763 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
765 NG_BTSOCKET_HCI_RAW_ALERT(
766 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
768 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
769 ng_btsocket_hci_raw_node = NULL;
774 /* Create input queue */
775 NG_BT_ITEMQ_INIT(&ng_btsocket_hci_raw_queue, 300);
776 mtx_init(&ng_btsocket_hci_raw_queue_mtx,
777 "btsocks_hci_raw_queue_mtx", NULL, MTX_DEF);
778 TASK_INIT(&ng_btsocket_hci_raw_task, 0,
779 ng_btsocket_hci_raw_input, NULL);
781 /* Create list of sockets */
782 LIST_INIT(&ng_btsocket_hci_raw_sockets);
783 mtx_init(&ng_btsocket_hci_raw_sockets_mtx,
784 "btsocks_hci_raw_sockets_mtx", NULL, MTX_DEF);
787 ng_btsocket_hci_raw_token = 0;
788 mtx_init(&ng_btsocket_hci_raw_token_mtx,
789 "btsocks_hci_raw_token_mtx", NULL, MTX_DEF);
795 ng_btsocket_hci_raw_sec_filter =
796 malloc(sizeof(struct ng_btsocket_hci_raw_sec_filter),
797 M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
798 if (ng_btsocket_hci_raw_sec_filter == NULL) {
799 printf("%s: Could not allocate security filter!\n", __func__);
804 * XXX How paranoid can we get?
806 * Initialize security filter. If bit is set in the mask then
807 * unprivileged socket is allowed to send (receive) this command
811 /* Enable all events */
812 memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff,
813 sizeof(ng_btsocket_hci_raw_sec_filter->events)/
814 sizeof(ng_btsocket_hci_raw_sec_filter->events[0]));
816 /* Disable some critical events */
817 f = ng_btsocket_hci_raw_sec_filter->events;
818 bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
819 bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
820 bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
822 /* Commands - Link control */
823 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1];
824 bit_set(f, NG_HCI_OCF_INQUIRY - 1);
825 bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
826 bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
827 bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
828 bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
829 bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
830 bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
831 bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
833 /* Commands - Link policy */
834 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1];
835 bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
836 bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1);
838 /* Commands - Host controller and baseband */
839 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1];
840 bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
841 bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
842 bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
843 bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
844 bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
845 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1);
846 bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1);
847 bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
848 bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
849 bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
850 bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
851 bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
852 bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1);
853 bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1);
854 bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
855 bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
856 bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1);
857 bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
858 bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
859 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
860 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
862 /* Commands - Informational */
863 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1];
864 bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
865 bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
866 bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
867 bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
868 bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
870 /* Commands - Status */
871 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1];
872 bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1);
873 bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
874 bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
876 /* Commands - Testing */
877 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1];
878 bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
879 } /* ng_btsocket_hci_raw_init */
882 * Abort connection on socket
886 ng_btsocket_hci_raw_abort(struct socket *so)
888 } /* ng_btsocket_hci_raw_abort */
891 ng_btsocket_hci_raw_close(struct socket *so)
893 } /* ng_btsocket_hci_raw_close */
896 * Create new raw HCI socket
900 ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
902 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
908 if (ng_btsocket_hci_raw_node == NULL)
909 return (EPROTONOSUPPORT);
910 if (proto != BLUETOOTH_PROTO_HCI)
911 return (EPROTONOSUPPORT);
912 if (so->so_type != SOCK_RAW)
913 return (ESOCKTNOSUPPORT);
915 error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
916 NG_BTSOCKET_HCI_RAW_RECVSPACE);
920 pcb = malloc(sizeof(*pcb),
921 M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
925 so->so_pcb = (caddr_t) pcb;
928 if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
929 pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
932 * Set default socket filter. By default socket only accepts HCI
933 * Command_Complete and Command_Status event packets.
936 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
937 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
939 mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF);
941 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
942 LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
943 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
946 } /* ng_btsocket_hci_raw_attach */
949 * Bind raw HCI socket
953 ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam,
956 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
957 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
961 if (ng_btsocket_hci_raw_node == NULL)
966 if (sa->hci_family != AF_BLUETOOTH)
967 return (EAFNOSUPPORT);
968 if (sa->hci_len != sizeof(*sa))
970 if (sa->hci_node[0] == 0)
973 mtx_lock(&pcb->pcb_mtx);
974 bcopy(sa, &pcb->addr, sizeof(pcb->addr));
975 mtx_unlock(&pcb->pcb_mtx);
978 } /* ng_btsocket_hci_raw_bind */
981 * Connect raw HCI socket
985 ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam,
988 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
989 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
993 if (ng_btsocket_hci_raw_node == NULL)
998 if (sa->hci_family != AF_BLUETOOTH)
999 return (EAFNOSUPPORT);
1000 if (sa->hci_len != sizeof(*sa))
1002 if (sa->hci_node[0] == 0)
1003 return (EDESTADDRREQ);
1005 mtx_lock(&pcb->pcb_mtx);
1007 if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
1008 mtx_unlock(&pcb->pcb_mtx);
1009 return (EADDRNOTAVAIL);
1014 mtx_unlock(&pcb->pcb_mtx);
1017 } /* ng_btsocket_hci_raw_connect */
1020 * Process ioctl on socket
1024 ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
1025 struct ifnet *ifp, struct thread *td)
1027 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1028 char path[NG_NODESIZ + 1];
1029 struct ng_mesg *msg = NULL;
1034 if (ng_btsocket_hci_raw_node == NULL)
1037 mtx_lock(&pcb->pcb_mtx);
1039 /* Check if we have device name */
1040 if (pcb->addr.hci_node[0] == 0) {
1041 mtx_unlock(&pcb->pcb_mtx);
1042 return (EHOSTUNREACH);
1045 /* Check if we have pending ioctl() */
1046 if (pcb->token != 0) {
1047 mtx_unlock(&pcb->pcb_mtx);
1051 snprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1054 case SIOC_HCI_RAW_NODE_GET_STATE: {
1055 struct ng_btsocket_hci_raw_node_state *p =
1056 (struct ng_btsocket_hci_raw_node_state *) data;
1058 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1059 NGM_HCI_NODE_GET_STATE,
1060 &p->state, sizeof(p->state));
1063 case SIOC_HCI_RAW_NODE_INIT:
1064 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1065 error = ng_btsocket_hci_raw_send_ngmsg(path,
1066 NGM_HCI_NODE_INIT, NULL, 0);
1071 case SIOC_HCI_RAW_NODE_GET_DEBUG: {
1072 struct ng_btsocket_hci_raw_node_debug *p =
1073 (struct ng_btsocket_hci_raw_node_debug *) data;
1075 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1076 NGM_HCI_NODE_GET_DEBUG,
1077 &p->debug, sizeof(p->debug));
1080 case SIOC_HCI_RAW_NODE_SET_DEBUG: {
1081 struct ng_btsocket_hci_raw_node_debug *p =
1082 (struct ng_btsocket_hci_raw_node_debug *) data;
1084 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1085 error = ng_btsocket_hci_raw_send_ngmsg(path,
1086 NGM_HCI_NODE_SET_DEBUG, &p->debug,
1092 case SIOC_HCI_RAW_NODE_GET_BUFFER: {
1093 struct ng_btsocket_hci_raw_node_buffer *p =
1094 (struct ng_btsocket_hci_raw_node_buffer *) data;
1096 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1097 NGM_HCI_NODE_GET_BUFFER,
1098 &p->buffer, sizeof(p->buffer));
1101 case SIOC_HCI_RAW_NODE_GET_BDADDR: {
1102 struct ng_btsocket_hci_raw_node_bdaddr *p =
1103 (struct ng_btsocket_hci_raw_node_bdaddr *) data;
1105 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1106 NGM_HCI_NODE_GET_BDADDR,
1107 &p->bdaddr, sizeof(p->bdaddr));
1110 case SIOC_HCI_RAW_NODE_GET_FEATURES: {
1111 struct ng_btsocket_hci_raw_node_features *p =
1112 (struct ng_btsocket_hci_raw_node_features *) data;
1114 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1115 NGM_HCI_NODE_GET_FEATURES,
1116 &p->features, sizeof(p->features));
1119 case SIOC_HCI_RAW_NODE_GET_STAT: {
1120 struct ng_btsocket_hci_raw_node_stat *p =
1121 (struct ng_btsocket_hci_raw_node_stat *) data;
1123 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1124 NGM_HCI_NODE_GET_STAT,
1125 &p->stat, sizeof(p->stat));
1128 case SIOC_HCI_RAW_NODE_RESET_STAT:
1129 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1130 error = ng_btsocket_hci_raw_send_ngmsg(path,
1131 NGM_HCI_NODE_RESET_STAT, NULL, 0);
1136 case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE:
1137 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1138 error = ng_btsocket_hci_raw_send_ngmsg(path,
1139 NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE,
1145 case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE: {
1146 struct ng_btsocket_hci_raw_node_neighbor_cache *p =
1147 (struct ng_btsocket_hci_raw_node_neighbor_cache *) data;
1148 ng_hci_node_get_neighbor_cache_ep *p1 = NULL;
1149 ng_hci_node_neighbor_cache_entry_ep *p2 = NULL;
1151 if (p->num_entries <= 0 ||
1152 p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM ||
1153 p->entries == NULL) {
1158 NG_MKMESSAGE(msg, NGM_HCI_COOKIE,
1159 NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_NOWAIT);
1164 ng_btsocket_hci_raw_get_token(&msg->header.token);
1165 pcb->token = msg->header.token;
1168 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1174 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1175 PZERO|PCATCH, "hcictl",
1176 ng_btsocket_hci_raw_ioctl_timeout * hz);
1182 if (pcb->msg != NULL &&
1183 pcb->msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) {
1184 /* Return data back to user space */
1185 p1 = (ng_hci_node_get_neighbor_cache_ep *)
1187 p2 = (ng_hci_node_neighbor_cache_entry_ep *)
1190 p->num_entries = min(p->num_entries, p1->num_entries);
1191 if (p->num_entries > 0)
1192 error = copyout((caddr_t) p2,
1193 (caddr_t) p->entries,
1194 p->num_entries * sizeof(*p2));
1198 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1201 case SIOC_HCI_RAW_NODE_GET_CON_LIST: {
1202 struct ng_btsocket_hci_raw_con_list *p =
1203 (struct ng_btsocket_hci_raw_con_list *) data;
1204 ng_hci_node_con_list_ep *p1 = NULL;
1205 ng_hci_node_con_ep *p2 = NULL;
1207 if (p->num_connections == 0 ||
1208 p->num_connections > NG_HCI_MAX_CON_NUM ||
1209 p->connections == NULL) {
1214 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
1220 ng_btsocket_hci_raw_get_token(&msg->header.token);
1221 pcb->token = msg->header.token;
1224 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1230 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1231 PZERO|PCATCH, "hcictl",
1232 ng_btsocket_hci_raw_ioctl_timeout * hz);
1238 if (pcb->msg != NULL &&
1239 pcb->msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) {
1240 /* Return data back to user space */
1241 p1 = (ng_hci_node_con_list_ep *)(pcb->msg->data);
1242 p2 = (ng_hci_node_con_ep *)(p1 + 1);
1244 p->num_connections = min(p->num_connections,
1245 p1->num_connections);
1246 if (p->num_connections > 0)
1247 error = copyout((caddr_t) p2,
1248 (caddr_t) p->connections,
1249 p->num_connections * sizeof(*p2));
1253 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1256 case SIOC_HCI_RAW_NODE_GET_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 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1262 NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK,
1263 &p->policy_mask, sizeof(p->policy_mask));
1266 case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: {
1267 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1268 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1271 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1272 error = ng_btsocket_hci_raw_send_ngmsg(path,
1273 NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK,
1275 sizeof(p->policy_mask));
1280 case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: {
1281 struct ng_btsocket_hci_raw_node_packet_mask *p =
1282 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1284 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1285 NGM_HCI_NODE_GET_PACKET_MASK,
1286 &p->packet_mask, sizeof(p->packet_mask));
1289 case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: {
1290 struct ng_btsocket_hci_raw_node_packet_mask *p =
1291 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1293 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1294 error = ng_btsocket_hci_raw_send_ngmsg(path,
1295 NGM_HCI_NODE_SET_PACKET_MASK,
1297 sizeof(p->packet_mask));
1302 case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: {
1303 struct ng_btsocket_hci_raw_node_role_switch *p =
1304 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1306 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1307 NGM_HCI_NODE_GET_ROLE_SWITCH,
1308 &p->role_switch, sizeof(p->role_switch));
1311 case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: {
1312 struct ng_btsocket_hci_raw_node_role_switch *p =
1313 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1315 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1316 error = ng_btsocket_hci_raw_send_ngmsg(path,
1317 NGM_HCI_NODE_SET_ROLE_SWITCH,
1319 sizeof(p->role_switch));
1324 case SIOC_HCI_RAW_NODE_LIST_NAMES: {
1325 struct ng_btsocket_hci_raw_node_list_names *nl =
1326 (struct ng_btsocket_hci_raw_node_list_names *) data;
1327 struct nodeinfo *ni = nl->names;
1329 if (nl->num_names == 0) {
1334 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
1340 ng_btsocket_hci_raw_get_token(&msg->header.token);
1341 pcb->token = msg->header.token;
1344 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, ".:", 0);
1350 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1351 PZERO|PCATCH, "hcictl",
1352 ng_btsocket_hci_raw_ioctl_timeout * hz);
1358 if (pcb->msg != NULL && pcb->msg->header.cmd == NGM_LISTNAMES) {
1359 /* Return data back to user space */
1360 struct namelist *nl1 = (struct namelist *) pcb->msg->data;
1361 struct nodeinfo *ni1 = &nl1->nodeinfo[0];
1363 while (nl->num_names > 0 && nl1->numnames > 0) {
1364 if (strcmp(ni1->type, NG_HCI_NODE_TYPE) == 0) {
1365 error = copyout((caddr_t) ni1,
1379 nl->num_names = ni - nl->names;
1383 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1391 mtx_unlock(&pcb->pcb_mtx);
1394 } /* ng_btsocket_hci_raw_control */
1397 * Process getsockopt/setsockopt system calls
1401 ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1403 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1404 struct ng_btsocket_hci_raw_filter filter;
1409 if (ng_btsocket_hci_raw_node == NULL)
1412 if (sopt->sopt_level != SOL_HCI_RAW)
1415 mtx_lock(&pcb->pcb_mtx);
1417 switch (sopt->sopt_dir) {
1419 switch (sopt->sopt_name) {
1420 case SO_HCI_RAW_FILTER:
1421 error = sooptcopyout(sopt, &pcb->filter,
1422 sizeof(pcb->filter));
1425 case SO_HCI_RAW_DIRECTION:
1426 dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1427 error = sooptcopyout(sopt, &dir, sizeof(dir));
1437 switch (sopt->sopt_name) {
1438 case SO_HCI_RAW_FILTER:
1439 error = sooptcopyin(sopt, &filter, sizeof(filter),
1442 bcopy(&filter, &pcb->filter,
1443 sizeof(pcb->filter));
1446 case SO_HCI_RAW_DIRECTION:
1447 error = sooptcopyin(sopt, &dir, sizeof(dir),
1453 pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION;
1455 pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1469 mtx_unlock(&pcb->pcb_mtx);
1472 } /* ng_btsocket_hci_raw_ctloutput */
1475 * Detach raw HCI socket
1479 ng_btsocket_hci_raw_detach(struct socket *so)
1481 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1483 KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
1485 if (ng_btsocket_hci_raw_node == NULL)
1488 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
1489 mtx_lock(&pcb->pcb_mtx);
1491 LIST_REMOVE(pcb, next);
1493 mtx_unlock(&pcb->pcb_mtx);
1494 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
1496 mtx_destroy(&pcb->pcb_mtx);
1498 bzero(pcb, sizeof(*pcb));
1499 free(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1502 } /* ng_btsocket_hci_raw_detach */
1505 * Disconnect raw HCI socket
1509 ng_btsocket_hci_raw_disconnect(struct socket *so)
1511 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1515 if (ng_btsocket_hci_raw_node == NULL)
1518 mtx_lock(&pcb->pcb_mtx);
1519 soisdisconnected(so);
1520 mtx_unlock(&pcb->pcb_mtx);
1523 } /* ng_btsocket_hci_raw_disconnect */
1526 * Get socket peer's address
1530 ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1532 return (ng_btsocket_hci_raw_sockaddr(so, nam));
1533 } /* ng_btsocket_hci_raw_peeraddr */
1540 ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
1541 struct sockaddr *sa, struct mbuf *control, struct thread *td)
1543 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1544 struct mbuf *nam = NULL;
1547 if (ng_btsocket_hci_raw_node == NULL) {
1555 if (control != NULL) {
1560 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1561 m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1566 if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1567 if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1572 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1577 mtx_lock(&pcb->pcb_mtx);
1579 error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1581 mtx_unlock(&pcb->pcb_mtx);
1586 if (pcb->addr.hci_node[0] == 0) {
1587 mtx_unlock(&pcb->pcb_mtx);
1588 error = EDESTADDRREQ;
1592 sa = (struct sockaddr *) &pcb->addr;
1595 MGET(nam, M_NOWAIT, MT_SONAME);
1597 mtx_unlock(&pcb->pcb_mtx);
1602 nam->m_len = sizeof(struct sockaddr_hci);
1603 bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1608 mtx_unlock(&pcb->pcb_mtx);
1610 return (ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1611 ng_btsocket_hci_raw_output, nam, 0));
1613 NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1618 } /* ng_btsocket_hci_raw_send */
1621 * Get socket address
1625 ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1627 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1628 struct sockaddr_hci sa;
1632 if (ng_btsocket_hci_raw_node == NULL)
1635 bzero(&sa, sizeof(sa));
1636 sa.hci_len = sizeof(sa);
1637 sa.hci_family = AF_BLUETOOTH;
1639 mtx_lock(&pcb->pcb_mtx);
1640 strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1641 mtx_unlock(&pcb->pcb_mtx);
1643 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1645 return ((*nam == NULL)? ENOMEM : 0);
1646 } /* ng_btsocket_hci_raw_sockaddr */