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>
54 #include <netgraph/ng_message.h>
55 #include <netgraph/netgraph.h>
56 #include <netgraph/bluetooth/include/ng_bluetooth.h>
57 #include <netgraph/bluetooth/include/ng_hci.h>
58 #include <netgraph/bluetooth/include/ng_l2cap.h>
59 #include <netgraph/bluetooth/include/ng_btsocket.h>
60 #include <netgraph/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 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;
121 static struct timeval ng_btsocket_hci_raw_lasttime;
122 static int ng_btsocket_hci_raw_curpps;
125 SYSCTL_DECL(_net_bluetooth_hci_sockets);
126 SYSCTL_NODE(_net_bluetooth_hci_sockets, OID_AUTO, raw, CTLFLAG_RW,
127 0, "Bluetooth raw HCI sockets family");
128 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, debug_level, CTLFLAG_RW,
129 &ng_btsocket_hci_raw_debug_level, NG_BTSOCKET_WARN_LEVEL,
130 "Bluetooth raw HCI sockets debug level");
131 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, ioctl_timeout, CTLFLAG_RW,
132 &ng_btsocket_hci_raw_ioctl_timeout, 5,
133 "Bluetooth raw HCI sockets ioctl timeout");
134 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_len, CTLFLAG_RD,
135 &ng_btsocket_hci_raw_queue.len, 0,
136 "Bluetooth raw HCI sockets input queue length");
137 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_maxlen, CTLFLAG_RD,
138 &ng_btsocket_hci_raw_queue.maxlen, 0,
139 "Bluetooth raw HCI sockets input queue max. length");
140 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_drops, CTLFLAG_RD,
141 &ng_btsocket_hci_raw_queue.drops, 0,
142 "Bluetooth raw HCI sockets input queue drops");
145 #define NG_BTSOCKET_HCI_RAW_INFO \
146 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
147 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
150 #define NG_BTSOCKET_HCI_RAW_WARN \
151 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
152 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
155 #define NG_BTSOCKET_HCI_RAW_ERR \
156 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
157 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
160 #define NG_BTSOCKET_HCI_RAW_ALERT \
161 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
162 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
165 /****************************************************************************
166 ****************************************************************************
168 ****************************************************************************
169 ****************************************************************************/
172 * Netgraph node constructor. Do not allow to create node of this type.
176 ng_btsocket_hci_raw_node_constructor(node_p node)
179 } /* ng_btsocket_hci_raw_node_constructor */
182 * Netgraph node destructor. Just let old node go and create new fresh one.
186 ng_btsocket_hci_raw_node_shutdown(node_p node)
192 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
194 NG_BTSOCKET_HCI_RAW_ALERT(
195 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
197 ng_btsocket_hci_raw_node = NULL;
202 error = ng_name_node(ng_btsocket_hci_raw_node,
203 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
205 NG_BTSOCKET_HCI_RAW_ALERT(
206 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
208 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
209 ng_btsocket_hci_raw_node = NULL;
215 } /* ng_btsocket_hci_raw_node_shutdown */
218 * Create new hook. Just say "yes"
222 ng_btsocket_hci_raw_node_newhook(node_p node, hook_p hook, char const *name)
225 } /* ng_btsocket_hci_raw_node_newhook */
228 * Connect hook. Just say "yes"
232 ng_btsocket_hci_raw_node_connect(hook_p hook)
235 } /* ng_btsocket_hci_raw_node_connect */
242 ng_btsocket_hci_raw_node_disconnect(hook_p hook)
245 } /* ng_btsocket_hci_raw_node_disconnect */
248 * Receive control message.
249 * Make sure it is a message from HCI node and it is a response.
250 * Enqueue item and schedule input task.
254 ng_btsocket_hci_raw_node_rcvmsg(node_p node, item_p item, hook_p lasthook)
256 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
260 * Check for empty sockets list creates LOR when both sender and
261 * receiver device are connected to the same host, so remove it
266 (msg->header.typecookie == NGM_HCI_COOKIE ||
267 msg->header.typecookie == NGM_GENERIC_COOKIE) &&
268 msg->header.flags & NGF_RESP) {
269 if (msg->header.token == 0) {
274 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
275 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
276 NG_BTSOCKET_HCI_RAW_ERR(
277 "%s: Input queue is full\n", __func__);
279 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
283 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
284 error = ng_btsocket_hci_raw_wakeup_input_task();
286 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
293 } /* ng_btsocket_hci_raw_node_rcvmsg */
296 * Receive packet from the one of our hook.
297 * Prepend every packet with sockaddr_hci and record sender's node name.
298 * Enqueue item and schedule input task.
302 ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item)
304 struct mbuf *nam = NULL;
308 * Check for empty sockets list creates LOR when both sender and
309 * receiver device are connected to the same host, so remove it
313 MGET(nam, M_DONTWAIT, MT_SONAME);
315 struct sockaddr_hci *sa = mtod(nam, struct sockaddr_hci *);
317 nam->m_len = sizeof(struct sockaddr_hci);
319 sa->hci_len = sizeof(*sa);
320 sa->hci_family = AF_BLUETOOTH;
321 strlcpy(sa->hci_node, NG_PEER_NODE_NAME(hook),
322 sizeof(sa->hci_node));
324 NGI_GET_M(item, nam->m_next);
327 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
328 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
329 NG_BTSOCKET_HCI_RAW_ERR(
330 "%s: Input queue is full\n", __func__);
332 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
336 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
337 error = ng_btsocket_hci_raw_wakeup_input_task();
339 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
341 NG_BTSOCKET_HCI_RAW_ERR(
342 "%s: Failed to allocate address mbuf\n", __func__);
349 } /* ng_btsocket_hci_raw_node_rcvdata */
351 /****************************************************************************
352 ****************************************************************************
354 ****************************************************************************
355 ****************************************************************************/
358 * Get next token. We need token to avoid theoretical race where process
359 * submits ioctl() message then interrupts ioctl() and re-submits another
360 * ioctl() on the same socket *before* first ioctl() complete.
364 ng_btsocket_hci_raw_get_token(u_int32_t *token)
366 mtx_lock(&ng_btsocket_hci_raw_token_mtx);
368 if (++ ng_btsocket_hci_raw_token == 0)
369 ng_btsocket_hci_raw_token = 1;
371 *token = ng_btsocket_hci_raw_token;
373 mtx_unlock(&ng_btsocket_hci_raw_token_mtx);
374 } /* ng_btsocket_hci_raw_get_token */
377 * Send Netgraph message to the node - do not expect reply
381 ng_btsocket_hci_raw_send_ngmsg(char *path, int cmd, void *arg, int arglen)
383 struct ng_mesg *msg = NULL;
386 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, arglen, M_NOWAIT);
390 if (arg != NULL && arglen > 0)
391 bcopy(arg, msg->data, arglen);
393 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
396 } /* ng_btsocket_hci_raw_send_ngmsg */
399 * Send Netgraph message to the node (no data) and wait for reply
403 ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path,
404 int cmd, void *rsp, int rsplen)
406 struct ng_mesg *msg = NULL;
409 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
411 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, 0, M_NOWAIT);
415 ng_btsocket_hci_raw_get_token(&msg->header.token);
416 pcb->token = msg->header.token;
419 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
425 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "hcictl",
426 ng_btsocket_hci_raw_ioctl_timeout * hz);
432 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
433 bcopy(pcb->msg->data, rsp, rsplen);
437 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
440 } /* ng_btsocket_hci_raw_send_sync_ngmsg */
443 * Create control information for the packet
447 ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf **ctl,
453 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
455 if (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION) {
456 dir = (m->m_flags & M_PROTO1)? 1 : 0;
457 *ctl = sbcreatecontrol((caddr_t) &dir, sizeof(dir),
458 SCM_HCI_RAW_DIRECTION, SOL_HCI_RAW);
460 ctl = &((*ctl)->m_next);
463 if (pcb->so->so_options & SO_TIMESTAMP) {
465 *ctl = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
466 SCM_TIMESTAMP, SOL_SOCKET);
468 ctl = &((*ctl)->m_next);
470 } /* ng_btsocket_hci_raw_savctl */
473 * Raw HCI sockets data input routine
477 ng_btsocket_hci_raw_data_input(struct mbuf *nam)
479 ng_btsocket_hci_raw_pcb_p pcb = NULL;
480 struct mbuf *m0 = NULL, *m = NULL;
481 struct sockaddr_hci *sa = NULL;
486 KASSERT((nam->m_type == MT_SONAME),
487 ("%s: m_type=%d\n", __func__, nam->m_type));
488 KASSERT((m0->m_flags & M_PKTHDR),
489 ("%s: m_flags=%#x\n", __func__, m0->m_flags));
491 sa = mtod(nam, struct sockaddr_hci *);
493 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
495 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
497 mtx_lock(&pcb->pcb_mtx);
500 * If socket was bound then check address and
501 * make sure it matches.
504 if (pcb->addr.hci_node[0] != 0 &&
505 strcmp(sa->hci_node, pcb->addr.hci_node) != 0)
509 * Check packet against filters
510 * XXX do we have to call m_pullup() here?
513 if (ng_btsocket_hci_raw_filter(pcb, m0, 1) != 0)
517 * Make a copy of the packet, append to the socket's
518 * receive queue and wakeup socket. sbappendaddr()
519 * will check if socket has enough buffer space.
522 m = m_dup(m0, M_DONTWAIT);
524 struct mbuf *ctl = NULL;
526 ng_btsocket_hci_raw_savctl(pcb, &ctl, m);
528 if (sbappendaddr(&pcb->so->so_rcv,
529 (struct sockaddr *) sa, m, ctl))
532 NG_BTSOCKET_HCI_RAW_INFO(
533 "%s: sbappendaddr() failed\n", __func__);
540 mtx_unlock(&pcb->pcb_mtx);
543 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
547 } /* ng_btsocket_hci_raw_data_input */
550 * Raw HCI sockets message input routine
554 ng_btsocket_hci_raw_msg_input(struct ng_mesg *msg)
556 ng_btsocket_hci_raw_pcb_p pcb = NULL;
558 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
560 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
561 mtx_lock(&pcb->pcb_mtx);
563 if (msg->header.token == pcb->token) {
567 mtx_unlock(&pcb->pcb_mtx);
568 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
573 mtx_unlock(&pcb->pcb_mtx);
576 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
578 NG_FREE_MSG(msg); /* checks for != NULL */
579 } /* ng_btsocket_hci_raw_msg_input */
582 * Raw HCI sockets input routines
586 ng_btsocket_hci_raw_input(void *context, int pending)
591 mtx_lock(&ng_btsocket_hci_raw_queue_mtx);
592 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_hci_raw_queue, item);
593 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx);
598 switch(item->el_flags & NGQF_TYPE) {
600 struct mbuf *m = NULL;
603 ng_btsocket_hci_raw_data_input(m);
607 struct ng_mesg *msg = NULL;
609 NGI_GET_MSG(item, msg);
610 ng_btsocket_hci_raw_msg_input(msg);
615 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
621 } /* ng_btsocket_hci_raw_input */
624 * Raw HCI sockets output routine
628 ng_btsocket_hci_raw_output(node_p node, hook_p hook, void *arg1, int arg2)
630 struct mbuf *nam = (struct mbuf *) arg1, *m = NULL;
631 struct sockaddr_hci *sa = NULL;
637 KASSERT((nam->m_type == MT_SONAME),
638 ("%s: m_type=%d\n", __func__, nam->m_type));
639 KASSERT((m->m_flags & M_PKTHDR),
640 ("%s: m_flags=%#x\n", __func__, m->m_flags));
642 sa = mtod(nam, struct sockaddr_hci *);
645 * Find downstream hook
646 * XXX For now access node hook list directly. Should be safe because
647 * we used ng_send_fn() and we should have exclusive lock on the node.
650 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
651 if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
652 NG_NODE_NOT_VALID(NG_PEER_NODE(hook)))
655 if (strcmp(sa->hci_node, NG_PEER_NODE_NAME(hook)) == 0) {
656 NG_SEND_DATA_ONLY(error, hook, m); /* sets m to NULL */
661 NG_FREE_M(nam); /* check for != NULL */
663 } /* ng_btsocket_hci_raw_output */
666 * Check frame against security and socket filters.
667 * d (direction bit) == 1 means incoming frame.
671 ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf *m, int d)
673 int type, event, opcode;
675 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
677 switch ((type = *mtod(m, u_int8_t *))) {
679 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)) {
680 opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode);
683 ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF(opcode) - 1],
684 NG_HCI_OCF(opcode) - 1))
688 if (d && !bit_test(pcb->filter.packet_mask, NG_HCI_CMD_PKT - 1))
692 case NG_HCI_ACL_DATA_PKT:
693 case NG_HCI_SCO_DATA_PKT:
694 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) ||
695 !bit_test(pcb->filter.packet_mask, type - 1) ||
700 case NG_HCI_EVENT_PKT:
704 event = mtod(m, ng_hci_event_pkt_t *)->event - 1;
706 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED))
707 if (!bit_test(ng_btsocket_hci_raw_sec_filter->events, event))
710 if (!bit_test(pcb->filter.event_mask, event))
719 } /* ng_btsocket_hci_raw_filter */
722 * Initialize everything
726 ng_btsocket_hci_raw_init(void)
731 ng_btsocket_hci_raw_node = NULL;
732 ng_btsocket_hci_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
733 ng_btsocket_hci_raw_ioctl_timeout = 5;
735 /* Register Netgraph node type */
736 error = ng_newtype(&typestruct);
738 NG_BTSOCKET_HCI_RAW_ALERT(
739 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
744 /* Create Netgrapg node */
745 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
747 NG_BTSOCKET_HCI_RAW_ALERT(
748 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
750 ng_btsocket_hci_raw_node = NULL;
755 error = ng_name_node(ng_btsocket_hci_raw_node,
756 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
758 NG_BTSOCKET_HCI_RAW_ALERT(
759 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
761 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
762 ng_btsocket_hci_raw_node = NULL;
767 /* Create input queue */
768 NG_BT_ITEMQ_INIT(&ng_btsocket_hci_raw_queue, 300);
769 mtx_init(&ng_btsocket_hci_raw_queue_mtx,
770 "btsocks_hci_raw_queue_mtx", NULL, MTX_DEF);
771 TASK_INIT(&ng_btsocket_hci_raw_task, 0,
772 ng_btsocket_hci_raw_input, NULL);
774 /* Create list of sockets */
775 LIST_INIT(&ng_btsocket_hci_raw_sockets);
776 mtx_init(&ng_btsocket_hci_raw_sockets_mtx,
777 "btsocks_hci_raw_sockets_mtx", NULL, MTX_DEF);
780 ng_btsocket_hci_raw_token = 0;
781 mtx_init(&ng_btsocket_hci_raw_token_mtx,
782 "btsocks_hci_raw_token_mtx", NULL, MTX_DEF);
789 ng_btsocket_hci_raw_sec_filter = NULL;
791 MALLOC(ng_btsocket_hci_raw_sec_filter,
792 struct ng_btsocket_hci_raw_sec_filter *,
793 sizeof(struct ng_btsocket_hci_raw_sec_filter),
794 M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
795 if (ng_btsocket_hci_raw_sec_filter == NULL) {
796 printf("%s: Could not allocate security filter!\n", __func__);
801 * XXX How paranoid can we get?
803 * Initialize security filter. If bit is set in the mask then
804 * unprivileged socket is allowed to send (receive) this command
808 /* Enable all events */
809 memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff,
810 sizeof(ng_btsocket_hci_raw_sec_filter->events)/
811 sizeof(ng_btsocket_hci_raw_sec_filter->events[0]));
813 /* Disable some critical events */
814 f = ng_btsocket_hci_raw_sec_filter->events;
815 bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
816 bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
817 bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
819 /* Commands - Link control */
820 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1];
821 bit_set(f, NG_HCI_OCF_INQUIRY - 1);
822 bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
823 bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
824 bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
825 bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
826 bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
827 bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
828 bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
830 /* Commands - Link policy */
831 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1];
832 bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
833 bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1);
835 /* Commands - Host controller and baseband */
836 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1];
837 bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
838 bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
839 bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
840 bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
841 bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
842 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1);
843 bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1);
844 bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
845 bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
846 bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
847 bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
848 bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
849 bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1);
850 bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1);
851 bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
852 bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
853 bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1);
854 bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
855 bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
856 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
857 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
859 /* Commands - Informational */
860 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1];
861 bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
862 bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
863 bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
864 bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
865 bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
867 /* Commands - Status */
868 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1];
869 bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1);
870 bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
871 bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
873 /* Commands - Testing */
874 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1];
875 bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
876 } /* ng_btsocket_hci_raw_init */
879 * Abort connection on socket
883 ng_btsocket_hci_raw_abort(struct socket *so)
885 } /* ng_btsocket_hci_raw_abort */
888 ng_btsocket_hci_raw_close(struct socket *so)
890 } /* ng_btsocket_hci_raw_close */
893 * Create new raw HCI socket
897 ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
899 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
905 if (ng_btsocket_hci_raw_node == NULL)
906 return (EPROTONOSUPPORT);
907 if (proto != BLUETOOTH_PROTO_HCI)
908 return (EPROTONOSUPPORT);
909 if (so->so_type != SOCK_RAW)
910 return (ESOCKTNOSUPPORT);
912 error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
913 NG_BTSOCKET_HCI_RAW_RECVSPACE);
917 MALLOC(pcb, ng_btsocket_hci_raw_pcb_p, sizeof(*pcb),
918 M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
922 so->so_pcb = (caddr_t) pcb;
925 if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
926 pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
929 * Set default socket filter. By default socket only accepts HCI
930 * Command_Complete and Command_Status event packets.
933 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
934 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
936 mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF);
938 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
939 LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
940 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
943 } /* ng_btsocket_hci_raw_attach */
946 * Bind raw HCI socket
950 ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam,
953 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
954 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
958 if (ng_btsocket_hci_raw_node == NULL)
963 if (sa->hci_family != AF_BLUETOOTH)
964 return (EAFNOSUPPORT);
965 if (sa->hci_len != sizeof(*sa))
967 if (sa->hci_node[0] == 0)
970 mtx_lock(&pcb->pcb_mtx);
971 bcopy(sa, &pcb->addr, sizeof(pcb->addr));
972 mtx_unlock(&pcb->pcb_mtx);
975 } /* ng_btsocket_hci_raw_bind */
978 * Connect raw HCI socket
982 ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam,
985 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
986 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
990 if (ng_btsocket_hci_raw_node == NULL)
995 if (sa->hci_family != AF_BLUETOOTH)
996 return (EAFNOSUPPORT);
997 if (sa->hci_len != sizeof(*sa))
999 if (sa->hci_node[0] == 0)
1000 return (EDESTADDRREQ);
1002 mtx_lock(&pcb->pcb_mtx);
1004 if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
1005 mtx_unlock(&pcb->pcb_mtx);
1006 return (EADDRNOTAVAIL);
1011 mtx_unlock(&pcb->pcb_mtx);
1014 } /* ng_btsocket_hci_raw_connect */
1017 * Process ioctl on socket
1021 ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
1022 struct ifnet *ifp, struct thread *td)
1024 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1025 char path[NG_NODESIZ + 1];
1026 struct ng_mesg *msg = NULL;
1031 if (ng_btsocket_hci_raw_node == NULL)
1034 mtx_lock(&pcb->pcb_mtx);
1036 /* Check if we have device name */
1037 if (pcb->addr.hci_node[0] == 0) {
1038 mtx_unlock(&pcb->pcb_mtx);
1039 return (EHOSTUNREACH);
1042 /* Check if we have pending ioctl() */
1043 if (pcb->token != 0) {
1044 mtx_unlock(&pcb->pcb_mtx);
1048 snprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1051 case SIOC_HCI_RAW_NODE_GET_STATE: {
1052 struct ng_btsocket_hci_raw_node_state *p =
1053 (struct ng_btsocket_hci_raw_node_state *) data;
1055 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1056 NGM_HCI_NODE_GET_STATE,
1057 &p->state, sizeof(p->state));
1060 case SIOC_HCI_RAW_NODE_INIT:
1061 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1062 error = ng_btsocket_hci_raw_send_ngmsg(path,
1063 NGM_HCI_NODE_INIT, NULL, 0);
1068 case SIOC_HCI_RAW_NODE_GET_DEBUG: {
1069 struct ng_btsocket_hci_raw_node_debug *p =
1070 (struct ng_btsocket_hci_raw_node_debug *) data;
1072 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1073 NGM_HCI_NODE_GET_DEBUG,
1074 &p->debug, sizeof(p->debug));
1077 case SIOC_HCI_RAW_NODE_SET_DEBUG: {
1078 struct ng_btsocket_hci_raw_node_debug *p =
1079 (struct ng_btsocket_hci_raw_node_debug *) data;
1081 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1082 error = ng_btsocket_hci_raw_send_ngmsg(path,
1083 NGM_HCI_NODE_SET_DEBUG, &p->debug,
1089 case SIOC_HCI_RAW_NODE_GET_BUFFER: {
1090 struct ng_btsocket_hci_raw_node_buffer *p =
1091 (struct ng_btsocket_hci_raw_node_buffer *) data;
1093 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1094 NGM_HCI_NODE_GET_BUFFER,
1095 &p->buffer, sizeof(p->buffer));
1098 case SIOC_HCI_RAW_NODE_GET_BDADDR: {
1099 struct ng_btsocket_hci_raw_node_bdaddr *p =
1100 (struct ng_btsocket_hci_raw_node_bdaddr *) data;
1102 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1103 NGM_HCI_NODE_GET_BDADDR,
1104 &p->bdaddr, sizeof(p->bdaddr));
1107 case SIOC_HCI_RAW_NODE_GET_FEATURES: {
1108 struct ng_btsocket_hci_raw_node_features *p =
1109 (struct ng_btsocket_hci_raw_node_features *) data;
1111 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1112 NGM_HCI_NODE_GET_FEATURES,
1113 &p->features, sizeof(p->features));
1116 case SIOC_HCI_RAW_NODE_GET_STAT: {
1117 struct ng_btsocket_hci_raw_node_stat *p =
1118 (struct ng_btsocket_hci_raw_node_stat *) data;
1120 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1121 NGM_HCI_NODE_GET_STAT,
1122 &p->stat, sizeof(p->stat));
1125 case SIOC_HCI_RAW_NODE_RESET_STAT:
1126 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1127 error = ng_btsocket_hci_raw_send_ngmsg(path,
1128 NGM_HCI_NODE_RESET_STAT, NULL, 0);
1133 case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE:
1134 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1135 error = ng_btsocket_hci_raw_send_ngmsg(path,
1136 NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE,
1142 case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE: {
1143 struct ng_btsocket_hci_raw_node_neighbor_cache *p =
1144 (struct ng_btsocket_hci_raw_node_neighbor_cache *) data;
1145 ng_hci_node_get_neighbor_cache_ep *p1 = NULL;
1146 ng_hci_node_neighbor_cache_entry_ep *p2 = NULL;
1148 if (p->num_entries <= 0 ||
1149 p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM ||
1150 p->entries == NULL) {
1155 NG_MKMESSAGE(msg, NGM_HCI_COOKIE,
1156 NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_NOWAIT);
1161 ng_btsocket_hci_raw_get_token(&msg->header.token);
1162 pcb->token = msg->header.token;
1165 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1171 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1172 PZERO|PCATCH, "hcictl",
1173 ng_btsocket_hci_raw_ioctl_timeout * hz);
1179 if (pcb->msg != NULL &&
1180 pcb->msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) {
1181 /* Return data back to user space */
1182 p1 = (ng_hci_node_get_neighbor_cache_ep *)
1184 p2 = (ng_hci_node_neighbor_cache_entry_ep *)
1187 p->num_entries = min(p->num_entries, p1->num_entries);
1188 if (p->num_entries > 0)
1189 error = copyout((caddr_t) p2,
1190 (caddr_t) p->entries,
1191 p->num_entries * sizeof(*p2));
1195 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1198 case SIOC_HCI_RAW_NODE_GET_CON_LIST: {
1199 struct ng_btsocket_hci_raw_con_list *p =
1200 (struct ng_btsocket_hci_raw_con_list *) data;
1201 ng_hci_node_con_list_ep *p1 = NULL;
1202 ng_hci_node_con_ep *p2 = NULL;
1204 if (p->num_connections == 0 ||
1205 p->num_connections > NG_HCI_MAX_CON_NUM ||
1206 p->connections == NULL) {
1211 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
1217 ng_btsocket_hci_raw_get_token(&msg->header.token);
1218 pcb->token = msg->header.token;
1221 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1227 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1228 PZERO|PCATCH, "hcictl",
1229 ng_btsocket_hci_raw_ioctl_timeout * hz);
1235 if (pcb->msg != NULL &&
1236 pcb->msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) {
1237 /* Return data back to user space */
1238 p1 = (ng_hci_node_con_list_ep *)(pcb->msg->data);
1239 p2 = (ng_hci_node_con_ep *)(p1 + 1);
1241 p->num_connections = min(p->num_connections,
1242 p1->num_connections);
1243 if (p->num_connections > 0)
1244 error = copyout((caddr_t) p2,
1245 (caddr_t) p->connections,
1246 p->num_connections * sizeof(*p2));
1250 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1253 case SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK: {
1254 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1255 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1258 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1259 NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK,
1260 &p->policy_mask, sizeof(p->policy_mask));
1263 case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: {
1264 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1265 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1268 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1269 error = ng_btsocket_hci_raw_send_ngmsg(path,
1270 NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK,
1272 sizeof(p->policy_mask));
1277 case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: {
1278 struct ng_btsocket_hci_raw_node_packet_mask *p =
1279 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1281 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1282 NGM_HCI_NODE_GET_PACKET_MASK,
1283 &p->packet_mask, sizeof(p->packet_mask));
1286 case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: {
1287 struct ng_btsocket_hci_raw_node_packet_mask *p =
1288 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1290 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1291 error = ng_btsocket_hci_raw_send_ngmsg(path,
1292 NGM_HCI_NODE_SET_PACKET_MASK,
1294 sizeof(p->packet_mask));
1299 case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: {
1300 struct ng_btsocket_hci_raw_node_role_switch *p =
1301 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1303 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1304 NGM_HCI_NODE_GET_ROLE_SWITCH,
1305 &p->role_switch, sizeof(p->role_switch));
1308 case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: {
1309 struct ng_btsocket_hci_raw_node_role_switch *p =
1310 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1312 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1313 error = ng_btsocket_hci_raw_send_ngmsg(path,
1314 NGM_HCI_NODE_SET_ROLE_SWITCH,
1316 sizeof(p->role_switch));
1321 case SIOC_HCI_RAW_NODE_LIST_NAMES: {
1322 struct ng_btsocket_hci_raw_node_list_names *nl =
1323 (struct ng_btsocket_hci_raw_node_list_names *) data;
1324 struct nodeinfo *ni = nl->names;
1326 if (nl->num_names == 0) {
1331 NG_MKMESSAGE(msg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
1337 ng_btsocket_hci_raw_get_token(&msg->header.token);
1338 pcb->token = msg->header.token;
1341 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, ".:", 0);
1347 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1348 PZERO|PCATCH, "hcictl",
1349 ng_btsocket_hci_raw_ioctl_timeout * hz);
1355 if (pcb->msg != NULL && pcb->msg->header.cmd == NGM_LISTNAMES) {
1356 /* Return data back to user space */
1357 struct namelist *nl1 = (struct namelist *) pcb->msg->data;
1358 struct nodeinfo *ni1 = &nl1->nodeinfo[0];
1360 while (nl->num_names > 0 && nl1->numnames > 0) {
1361 if (strcmp(ni1->type, NG_HCI_NODE_TYPE) == 0) {
1362 error = copyout((caddr_t) ni1,
1376 nl->num_names = ni - nl->names;
1380 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1388 mtx_unlock(&pcb->pcb_mtx);
1391 } /* ng_btsocket_hci_raw_control */
1394 * Process getsockopt/setsockopt system calls
1398 ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1400 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1401 struct ng_btsocket_hci_raw_filter filter;
1406 if (ng_btsocket_hci_raw_node == NULL)
1409 if (sopt->sopt_level != SOL_HCI_RAW)
1412 mtx_lock(&pcb->pcb_mtx);
1414 switch (sopt->sopt_dir) {
1416 switch (sopt->sopt_name) {
1417 case SO_HCI_RAW_FILTER:
1418 error = sooptcopyout(sopt, &pcb->filter,
1419 sizeof(pcb->filter));
1422 case SO_HCI_RAW_DIRECTION:
1423 dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1424 error = sooptcopyout(sopt, &dir, sizeof(dir));
1434 switch (sopt->sopt_name) {
1435 case SO_HCI_RAW_FILTER:
1436 error = sooptcopyin(sopt, &filter, sizeof(filter),
1439 bcopy(&filter, &pcb->filter,
1440 sizeof(pcb->filter));
1443 case SO_HCI_RAW_DIRECTION:
1444 error = sooptcopyin(sopt, &dir, sizeof(dir),
1450 pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION;
1452 pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1466 mtx_unlock(&pcb->pcb_mtx);
1469 } /* ng_btsocket_hci_raw_ctloutput */
1472 * Detach raw HCI socket
1476 ng_btsocket_hci_raw_detach(struct socket *so)
1478 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1480 KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
1482 if (ng_btsocket_hci_raw_node == NULL)
1485 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
1486 mtx_lock(&pcb->pcb_mtx);
1488 LIST_REMOVE(pcb, next);
1490 mtx_unlock(&pcb->pcb_mtx);
1491 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
1493 mtx_destroy(&pcb->pcb_mtx);
1495 bzero(pcb, sizeof(*pcb));
1496 FREE(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1499 } /* ng_btsocket_hci_raw_detach */
1502 * Disconnect raw HCI socket
1506 ng_btsocket_hci_raw_disconnect(struct socket *so)
1508 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1512 if (ng_btsocket_hci_raw_node == NULL)
1515 mtx_lock(&pcb->pcb_mtx);
1516 soisdisconnected(so);
1517 mtx_unlock(&pcb->pcb_mtx);
1520 } /* ng_btsocket_hci_raw_disconnect */
1523 * Get socket peer's address
1527 ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1529 return (ng_btsocket_hci_raw_sockaddr(so, nam));
1530 } /* ng_btsocket_hci_raw_peeraddr */
1537 ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
1538 struct sockaddr *sa, struct mbuf *control, struct thread *td)
1540 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1541 struct mbuf *nam = NULL;
1544 if (ng_btsocket_hci_raw_node == NULL) {
1552 if (control != NULL) {
1557 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1558 m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1563 if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1564 if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1569 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1574 mtx_lock(&pcb->pcb_mtx);
1576 error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1578 mtx_unlock(&pcb->pcb_mtx);
1583 if (pcb->addr.hci_node[0] == 0) {
1584 mtx_unlock(&pcb->pcb_mtx);
1585 error = EDESTADDRREQ;
1589 sa = (struct sockaddr *) &pcb->addr;
1592 MGET(nam, M_DONTWAIT, MT_SONAME);
1594 mtx_unlock(&pcb->pcb_mtx);
1599 nam->m_len = sizeof(struct sockaddr_hci);
1600 bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1605 mtx_unlock(&pcb->pcb_mtx);
1607 return (ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1608 ng_btsocket_hci_raw_output, nam, 0));
1610 NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1615 } /* ng_btsocket_hci_raw_send */
1618 * Get socket address
1622 ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1624 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1625 struct sockaddr_hci sa;
1629 if (ng_btsocket_hci_raw_node == NULL)
1632 bzero(&sa, sizeof(sa));
1633 sa.hci_len = sizeof(sa);
1634 sa.hci_family = AF_BLUETOOTH;
1636 mtx_lock(&pcb->pcb_mtx);
1637 strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1638 mtx_unlock(&pcb->pcb_mtx);
1640 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1642 return ((*nam == NULL)? ENOMEM : 0);
1643 } /* ng_btsocket_hci_raw_sockaddr */