2 * ng_btsocket_hci_raw.c
6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: ng_btsocket_hci_raw.c,v 1.14 2003/09/14 23:29:06 max Exp $
31 * $FreeBSD: src/sys/netgraph/bluetooth/socket/ng_btsocket_hci_raw.c,v 1.23 2006/11/06 13:42:04 rwatson Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bitstring.h>
37 #include <sys/domain.h>
38 #include <sys/endian.h>
39 #include <sys/errno.h>
40 #include <sys/filedesc.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
46 #include <sys/protosw.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
52 #include <sys/msgport2.h>
53 #include <sys/refcount.h>
54 #include <netgraph7/ng_message.h>
55 #include <netgraph7/netgraph.h>
56 #include <netgraph7/netgraph2.h>
57 #include <netgraph7/bluetooth/include/ng_bluetooth.h>
58 #include <netgraph7/bluetooth/include/ng_hci.h>
59 #include <netgraph7/bluetooth/include/ng_l2cap.h>
60 #include <netgraph7/bluetooth/include/ng_btsocket.h>
61 #include <netgraph7/bluetooth/include/ng_btsocket_hci_raw.h>
64 #ifdef NG_SEPARATE_MALLOC
65 MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_HCI_RAW, "netgraph_btsocks_hci_raw",
66 "Netgraph Bluetooth raw HCI sockets");
68 #define M_NETGRAPH_BTSOCKET_HCI_RAW M_NETGRAPH
69 #endif /* NG_SEPARATE_MALLOC */
71 /* Netgraph node methods */
72 static ng_constructor_t ng_btsocket_hci_raw_node_constructor;
73 static ng_rcvmsg_t ng_btsocket_hci_raw_node_rcvmsg;
74 static ng_shutdown_t ng_btsocket_hci_raw_node_shutdown;
75 static ng_newhook_t ng_btsocket_hci_raw_node_newhook;
76 static ng_connect_t ng_btsocket_hci_raw_node_connect;
77 static ng_rcvdata_t ng_btsocket_hci_raw_node_rcvdata;
78 static ng_disconnect_t ng_btsocket_hci_raw_node_disconnect;
80 static void ng_btsocket_hci_raw_input (void *, int);
81 static void ng_btsocket_hci_raw_output(node_p, hook_p, void *, int);
82 static void ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p,
85 static int ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p,
88 #define ng_btsocket_hci_raw_wakeup_input_task() \
89 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_hci_raw_task)
92 struct ng_btsocket_hci_raw_sec_filter {
93 bitstr_t bit_decl(events, 0xff);
94 bitstr_t bit_decl(commands[0x3f], 0x3ff);
97 /* Netgraph type descriptor */
98 static struct ng_type typestruct = {
99 .version = NG_ABI_VERSION,
100 .name = NG_BTSOCKET_HCI_RAW_NODE_TYPE,
101 .constructor = ng_btsocket_hci_raw_node_constructor,
102 .rcvmsg = ng_btsocket_hci_raw_node_rcvmsg,
103 .shutdown = ng_btsocket_hci_raw_node_shutdown,
104 .newhook = ng_btsocket_hci_raw_node_newhook,
105 .connect = ng_btsocket_hci_raw_node_connect,
106 .rcvdata = ng_btsocket_hci_raw_node_rcvdata,
107 .disconnect = ng_btsocket_hci_raw_node_disconnect,
111 extern int ifqmaxlen;
112 static u_int32_t ng_btsocket_hci_raw_debug_level;
113 static u_int32_t ng_btsocket_hci_raw_ioctl_timeout;
114 static node_p ng_btsocket_hci_raw_node;
115 static struct ng_bt_itemq ng_btsocket_hci_raw_queue;
116 static struct lock ng_btsocket_hci_raw_queue_lock;
117 static struct task ng_btsocket_hci_raw_task;
118 static LIST_HEAD(, ng_btsocket_hci_raw_pcb) ng_btsocket_hci_raw_sockets;
119 static struct lock ng_btsocket_hci_raw_sockets_lock;
120 static u_int32_t ng_btsocket_hci_raw_token;
121 static struct lock ng_btsocket_hci_raw_token_lock;
122 static struct ng_btsocket_hci_raw_sec_filter *ng_btsocket_hci_raw_sec_filter;
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) \
149 #define NG_BTSOCKET_HCI_RAW_WARN \
150 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL) \
153 #define NG_BTSOCKET_HCI_RAW_ERR \
154 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL) \
157 #define NG_BTSOCKET_HCI_RAW_ALERT \
158 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL) \
161 /****************************************************************************
162 ****************************************************************************
164 ****************************************************************************
165 ****************************************************************************/
168 * Netgraph node constructor. Do not allow to create node of this type.
172 ng_btsocket_hci_raw_node_constructor(node_p node)
175 } /* ng_btsocket_hci_raw_node_constructor */
178 * Netgraph node destructor. Just let old node go and create new fresh one.
182 ng_btsocket_hci_raw_node_shutdown(node_p node)
188 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
190 NG_BTSOCKET_HCI_RAW_ALERT(
191 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
193 ng_btsocket_hci_raw_node = NULL;
198 error = ng_name_node(ng_btsocket_hci_raw_node,
199 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
201 NG_BTSOCKET_HCI_RAW_ALERT(
202 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
204 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
205 ng_btsocket_hci_raw_node = NULL;
211 } /* ng_btsocket_hci_raw_node_shutdown */
214 * Create new hook. Just say "yes"
218 ng_btsocket_hci_raw_node_newhook(node_p node, hook_p hook, char const *name)
221 } /* ng_btsocket_hci_raw_node_newhook */
224 * Connect hook. Just say "yes"
228 ng_btsocket_hci_raw_node_connect(hook_p hook)
231 } /* ng_btsocket_hci_raw_node_connect */
238 ng_btsocket_hci_raw_node_disconnect(hook_p hook)
241 } /* ng_btsocket_hci_raw_node_disconnect */
244 * Receive control message.
245 * Make sure it is a message from HCI node and it is a response.
246 * Enqueue item and schedule input task.
250 ng_btsocket_hci_raw_node_rcvmsg(node_p node, item_p item, hook_p lasthook)
252 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
256 * Check for empty sockets list creates LOR when both sender and
257 * receiver device are connected to the same host, so remove it
262 (msg->header.typecookie == NGM_HCI_COOKIE ||
263 msg->header.typecookie == NGM_GENERIC_COOKIE) &&
264 msg->header.flags & NGF_RESP) {
265 if (msg->header.token == 0) {
270 lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_EXCLUSIVE);
271 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
272 NG_BTSOCKET_HCI_RAW_ERR(
273 "%s: Input queue is full\n", __func__);
275 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
280 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
281 error = ng_btsocket_hci_raw_wakeup_input_task();
283 lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_RELEASE);
290 } /* ng_btsocket_hci_raw_node_rcvmsg */
293 * Receive packet from the one of our hook.
294 * Prepend every packet with sockaddr_hci and record sender's node name.
295 * Enqueue item and schedule input task.
299 ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item)
301 struct mbuf *nam = NULL;
305 * Check for empty sockets list creates LOR when both sender and
306 * receiver device are connected to the same host, so remove it
310 MGET(nam, M_NOWAIT, MT_SONAME);
312 struct sockaddr_hci *sa = mtod(nam, struct sockaddr_hci *);
314 nam->m_len = sizeof(struct sockaddr_hci);
316 sa->hci_len = sizeof(*sa);
317 sa->hci_family = AF_BLUETOOTH;
318 strlcpy(sa->hci_node, NG_PEER_NODE_NAME(hook),
319 sizeof(sa->hci_node));
321 NGI_GET_M(item, nam->m_next);
324 lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_EXCLUSIVE);
325 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) {
326 NG_BTSOCKET_HCI_RAW_ERR(
327 "%s: Input queue is full\n", __func__);
329 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue);
334 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item);
335 error = ng_btsocket_hci_raw_wakeup_input_task();
337 lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_RELEASE);
339 NG_BTSOCKET_HCI_RAW_ERR(
340 "%s: Failed to allocate address mbuf\n", __func__);
347 } /* ng_btsocket_hci_raw_node_rcvdata */
349 /****************************************************************************
350 ****************************************************************************
352 ****************************************************************************
353 ****************************************************************************/
356 * Get next token. We need token to avoid theoretical race where process
357 * submits ioctl() message then interrupts ioctl() and re-submits another
358 * ioctl() on the same socket *before* first ioctl() complete.
362 ng_btsocket_hci_raw_get_token(u_int32_t *token)
364 lockmgr(&ng_btsocket_hci_raw_token_lock, LK_EXCLUSIVE);
366 if (++ ng_btsocket_hci_raw_token == 0)
367 ng_btsocket_hci_raw_token = 1;
369 *token = ng_btsocket_hci_raw_token;
371 lockmgr(&ng_btsocket_hci_raw_token_lock, LK_RELEASE);
372 } /* ng_btsocket_hci_raw_get_token */
375 * Send Netgraph message to the node - do not expect reply
379 ng_btsocket_hci_raw_send_ngmsg(char *path, int cmd, void *arg, int arglen)
381 struct ng_mesg *msg = NULL;
384 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, arglen, M_WAITOK | M_NULLOK);
388 if (arg != NULL && arglen > 0)
389 bcopy(arg, msg->data, arglen);
391 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
394 } /* ng_btsocket_hci_raw_send_ngmsg */
397 * Send Netgraph message to the node (no data) and wait for reply
401 ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path,
402 int cmd, void *rsp, int rsplen)
404 struct ng_mesg *msg = NULL;
407 KKASSERT(lockowned(&pcb->pcb_lock) != 0);
409 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, 0, M_WAITOK | M_NULLOK);
413 ng_btsocket_hci_raw_get_token(&msg->header.token);
414 pcb->token = msg->header.token;
417 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
423 error = lksleep(&pcb->msg, &pcb->pcb_lock, PCATCH, "hcictl",
424 ng_btsocket_hci_raw_ioctl_timeout * hz);
430 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
431 bcopy(pcb->msg->data, rsp, rsplen);
435 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
438 } /* ng_btsocket_hci_raw_send_sync_ngmsg */
441 * Create control information for the packet
445 ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf **ctl,
451 KKASSERT(lockowned(&pcb->pcb_lock) != 0);
453 if (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION) {
454 dir = (m->m_flags & M_PROTO1)? 1 : 0;
455 *ctl = sbcreatecontrol(&dir, sizeof(dir),
456 SCM_HCI_RAW_DIRECTION, SOL_HCI_RAW);
458 ctl = &((*ctl)->m_next);
461 if (pcb->so->so_options & SO_TIMESTAMP) {
463 *ctl = sbcreatecontrol(&tv, sizeof(tv),
464 SCM_TIMESTAMP, SOL_SOCKET);
466 ctl = &((*ctl)->m_next);
468 } /* ng_btsocket_hci_raw_savctl */
471 * Raw HCI sockets data input routine
475 ng_btsocket_hci_raw_data_input(struct mbuf *nam)
477 ng_btsocket_hci_raw_pcb_p pcb = NULL;
478 struct mbuf *m0 = NULL, *m = NULL;
479 struct sockaddr_hci *sa = NULL;
484 KASSERT((nam->m_type == MT_SONAME),
485 ("%s: m_type=%d\n", __func__, nam->m_type));
486 KASSERT((m0->m_flags & M_PKTHDR),
487 ("%s: m_flags=%#x\n", __func__, m0->m_flags));
489 sa = mtod(nam, struct sockaddr_hci *);
491 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_EXCLUSIVE);
493 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
495 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
498 * If socket was bound then check address and
499 * make sure it matches.
502 if (pcb->addr.hci_node[0] != 0 &&
503 strcmp(sa->hci_node, pcb->addr.hci_node) != 0)
507 * Check packet against filters
508 * XXX do we have to call m_pullup() here?
511 if (ng_btsocket_hci_raw_filter(pcb, m0, 1) != 0)
515 * Make a copy of the packet, append to the socket's
516 * receive queue and wakeup socket. sbappendaddr()
517 * will check if socket has enough buffer space.
520 m = m_dup(m0, M_NOWAIT);
522 struct mbuf *ctl = NULL;
524 ng_btsocket_hci_raw_savctl(pcb, &ctl, m);
526 if (sbappendaddr(&pcb->so->so_rcv.sb,
527 (struct sockaddr *) sa, m, ctl))
530 NG_BTSOCKET_HCI_RAW_INFO(
531 "%s: sbappendaddr() failed\n", __func__);
538 lockmgr(&pcb->pcb_lock, LK_RELEASE);
541 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
545 } /* ng_btsocket_hci_raw_data_input */
548 * Raw HCI sockets message input routine
552 ng_btsocket_hci_raw_msg_input(struct ng_mesg *msg)
554 ng_btsocket_hci_raw_pcb_p pcb = NULL;
556 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_EXCLUSIVE);
558 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
559 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
561 if (msg->header.token == pcb->token) {
565 lockmgr(&pcb->pcb_lock, LK_RELEASE);
566 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
571 lockmgr(&pcb->pcb_lock, LK_RELEASE);
574 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
576 NG_FREE_MSG(msg); /* checks for != NULL */
577 } /* ng_btsocket_hci_raw_msg_input */
580 * Raw HCI sockets input routines
584 ng_btsocket_hci_raw_input(void *context, int pending)
589 lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_EXCLUSIVE);
590 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_hci_raw_queue, item);
591 lockmgr(&ng_btsocket_hci_raw_queue_lock, LK_RELEASE);
596 switch(item->el_flags & NGQF_TYPE) {
598 struct mbuf *m = NULL;
601 ng_btsocket_hci_raw_data_input(m);
605 struct ng_mesg *msg = NULL;
607 NGI_GET_MSG(item, msg);
608 ng_btsocket_hci_raw_msg_input(msg);
613 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
618 ng_unref_item(item, 0);
620 } /* ng_btsocket_hci_raw_input */
623 * Raw HCI sockets output routine
627 ng_btsocket_hci_raw_output(node_p node, hook_p hook, void *arg1, int arg2)
629 struct mbuf *nam = (struct mbuf *) arg1, *m = NULL;
630 struct sockaddr_hci *sa = NULL;
636 KASSERT((nam->m_type == MT_SONAME),
637 ("%s: m_type=%d\n", __func__, nam->m_type));
638 KASSERT((m->m_flags & M_PKTHDR),
639 ("%s: m_flags=%#x\n", __func__, m->m_flags));
641 sa = mtod(nam, struct sockaddr_hci *);
644 * Find downstream hook
645 * XXX For now access node hook list directly. Should be safe because
646 * we used ng_send_fn() and we should have exclusive lock on the node.
649 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
650 if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
651 NG_NODE_NOT_VALID(NG_PEER_NODE(hook)))
654 if (strcmp(sa->hci_node, NG_PEER_NODE_NAME(hook)) == 0) {
655 NG_SEND_DATA_ONLY(error, hook, m); /* sets m to NULL */
660 NG_FREE_M(nam); /* check for != NULL */
662 } /* ng_btsocket_hci_raw_output */
665 * Check frame against security and socket filters.
666 * d (direction bit) == 1 means incoming frame.
670 ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf *m, int d)
672 int type, event, opcode;
674 KKASSERT(lockowned(&pcb->pcb_lock) != 0);
676 switch ((type = *mtod(m, u_int8_t *))) {
678 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)) {
679 opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode);
682 ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF(opcode) - 1],
683 NG_HCI_OCF(opcode) - 1))
687 if (d && !bit_test(pcb->filter.packet_mask, NG_HCI_CMD_PKT - 1))
691 case NG_HCI_ACL_DATA_PKT:
692 case NG_HCI_SCO_DATA_PKT:
693 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) ||
694 !bit_test(pcb->filter.packet_mask, type - 1) ||
699 case NG_HCI_EVENT_PKT:
703 event = mtod(m, ng_hci_event_pkt_t *)->event - 1;
705 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED))
706 if (!bit_test(ng_btsocket_hci_raw_sec_filter->events, event))
709 if (!bit_test(pcb->filter.event_mask, event))
718 } /* ng_btsocket_hci_raw_filter */
721 * Initialize everything
725 ng_btsocket_hci_raw_init(void)
730 ng_btsocket_hci_raw_node = NULL;
731 ng_btsocket_hci_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
732 ng_btsocket_hci_raw_ioctl_timeout = 5;
734 /* Register Netgraph node type */
735 error = ng_newtype(&typestruct);
737 NG_BTSOCKET_HCI_RAW_ALERT(
738 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
743 /* Create Netgrapg node */
744 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node);
746 NG_BTSOCKET_HCI_RAW_ALERT(
747 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
749 ng_btsocket_hci_raw_node = NULL;
754 error = ng_name_node(ng_btsocket_hci_raw_node,
755 NG_BTSOCKET_HCI_RAW_NODE_TYPE);
757 NG_BTSOCKET_HCI_RAW_ALERT(
758 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
760 NG_NODE_UNREF(ng_btsocket_hci_raw_node);
761 ng_btsocket_hci_raw_node = NULL;
766 /* Create input queue */
767 NG_BT_ITEMQ_INIT(&ng_btsocket_hci_raw_queue, ifqmaxlen);
768 lockinit(&ng_btsocket_hci_raw_queue_lock,
769 "btsocks_hci_raw_queue_lock", 0, 0);
770 TASK_INIT(&ng_btsocket_hci_raw_task, 0,
771 ng_btsocket_hci_raw_input, NULL);
773 /* Create list of sockets */
774 LIST_INIT(&ng_btsocket_hci_raw_sockets);
775 lockinit(&ng_btsocket_hci_raw_sockets_lock,
776 "btsocks_hci_raw_sockets_lock", 0, 0);
779 ng_btsocket_hci_raw_token = 0;
780 lockinit(&ng_btsocket_hci_raw_token_lock,
781 "btsocks_hci_raw_token_lock", 0, 0);
788 ng_btsocket_hci_raw_sec_filter = NULL;
790 ng_btsocket_hci_raw_sec_filter = kmalloc(sizeof(struct ng_btsocket_hci_raw_sec_filter),
791 M_NETGRAPH_BTSOCKET_HCI_RAW,
792 M_WAITOK | M_NULLOK | M_ZERO);
793 if (ng_btsocket_hci_raw_sec_filter == NULL) {
794 kprintf("%s: Could not allocate security filter!\n", __func__);
799 * XXX How paranoid can we get?
801 * Initialize security filter. If bit is set in the mask then
802 * unprivileged socket is allowed to send (receive) this command
806 /* Enable all events */
807 memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff,
808 sizeof(ng_btsocket_hci_raw_sec_filter->events));
810 /* Disable some critical events */
811 f = ng_btsocket_hci_raw_sec_filter->events;
812 bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
813 bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
814 bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
816 /* Commands - Link control */
817 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1];
818 bit_set(f, NG_HCI_OCF_INQUIRY - 1);
819 bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
820 bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
821 bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
822 bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
823 bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
824 bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
825 bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
827 /* Commands - Link policy */
828 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1];
829 bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
830 bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1);
832 /* Commands - Host controller and baseband */
833 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1];
834 bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
835 bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
836 bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
837 bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
838 bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
839 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1);
840 bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1);
841 bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
842 bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
843 bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
844 bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
845 bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
846 bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1);
847 bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1);
848 bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
849 bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
850 bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1);
851 bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
852 bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
853 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
854 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
856 /* Commands - Informational */
857 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1];
858 bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
859 bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
860 bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
861 bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
862 bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
864 /* Commands - Status */
865 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1];
866 bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1);
867 bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
868 bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
870 /* Commands - Testing */
871 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1];
872 bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
873 } /* ng_btsocket_hci_raw_init */
876 * Abort connection on socket
880 ng_btsocket_hci_raw_abort(netmsg_t msg)
882 } /* ng_btsocket_hci_raw_abort */
886 ng_btsocket_hci_raw_close(struct socket *so)
888 } /* ng_btsocket_hci_raw_close */
892 * Create new raw HCI socket
896 ng_btsocket_hci_raw_attach(netmsg_t msg)
898 struct socket *so = msg->attach.base.nm_so;
899 int proto = msg->attach.nm_proto;
900 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
908 if (ng_btsocket_hci_raw_node == NULL) {
909 error = EPROTONOSUPPORT;
912 if (proto != BLUETOOTH_PROTO_HCI) {
913 error = EPROTONOSUPPORT;
916 if (so->so_type != SOCK_RAW) {
917 error = ESOCKTNOSUPPORT;
921 error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
922 NG_BTSOCKET_HCI_RAW_RECVSPACE, NULL);
926 pcb = kmalloc(sizeof(*pcb), M_NETGRAPH_BTSOCKET_HCI_RAW,
927 M_WAITOK | M_NULLOK | M_ZERO);
933 so->so_pcb = (caddr_t) pcb;
936 if (curproc == NULL ||
937 caps_priv_check_self(SYSCAP_NONET_BT_RAW) == 0)
938 pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
941 * Set default socket filter. By default socket only accepts HCI
942 * Command_Complete and Command_Status event packets.
945 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
946 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
948 lockinit(&pcb->pcb_lock, "btsocks_hci_raw_pcb_lock", 0, 0);
950 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_EXCLUSIVE);
951 LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
952 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
955 lwkt_replymsg(&msg->attach.base.lmsg, error);
956 } /* ng_btsocket_hci_raw_attach */
959 * Bind raw HCI socket
963 ng_btsocket_hci_raw_bind(netmsg_t msg)
965 struct socket *so = msg->bind.base.nm_so;
966 struct sockaddr *nam = msg->bind.nm_nam;
967 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
968 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
975 if (ng_btsocket_hci_raw_node == NULL) {
984 if (sa->hci_family != AF_BLUETOOTH) {
985 error = EAFNOSUPPORT;
988 if (sa->hci_len != sizeof(*sa)) {
992 if (sa->hci_node[0] == 0) {
997 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
998 bcopy(sa, &pcb->addr, sizeof(pcb->addr));
999 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1002 lwkt_replymsg(&msg->bind.base.lmsg, error);
1003 } /* ng_btsocket_hci_raw_bind */
1006 * Connect raw HCI socket
1010 ng_btsocket_hci_raw_connect(netmsg_t msg)
1012 struct socket *so = msg->connect.base.nm_so;
1013 struct sockaddr *nam = msg->connect.nm_nam;
1014 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1015 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
1022 if (ng_btsocket_hci_raw_node == NULL) {
1031 if (sa->hci_family != AF_BLUETOOTH) {
1032 error = EAFNOSUPPORT;
1035 if (sa->hci_len != sizeof(*sa)) {
1039 if (sa->hci_node[0] == 0) {
1040 error = EDESTADDRREQ;
1044 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1046 if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
1047 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1048 error = EADDRNOTAVAIL;
1054 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1057 lwkt_replymsg(&msg->connect.base.lmsg, error);
1058 } /* ng_btsocket_hci_raw_connect */
1061 * Process ioctl on socket
1065 ng_btsocket_hci_raw_control(netmsg_t msg)
1067 struct socket *so = msg->control.base.nm_so;
1068 u_long cmd = msg->control.nm_cmd;
1069 caddr_t data = msg->control.nm_data;
1070 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1071 char path[NG_NODESIZ + 1];
1072 struct ng_mesg *ngmsg = NULL;
1079 if (ng_btsocket_hci_raw_node == NULL) {
1084 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1086 /* Check if we have device name */
1087 if (pcb->addr.hci_node[0] == 0) {
1088 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1089 error = EHOSTUNREACH;
1093 /* Check if we have pending ioctl() */
1094 if (pcb->token != 0) {
1095 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1100 ksnprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1103 case SIOC_HCI_RAW_NODE_GET_STATE: {
1104 struct ng_btsocket_hci_raw_node_state *p =
1105 (struct ng_btsocket_hci_raw_node_state *) data;
1107 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1108 NGM_HCI_NODE_GET_STATE,
1109 &p->state, sizeof(p->state));
1112 case SIOC_HCI_RAW_NODE_INIT:
1113 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1114 error = ng_btsocket_hci_raw_send_ngmsg(path,
1115 NGM_HCI_NODE_INIT, NULL, 0);
1120 case SIOC_HCI_RAW_NODE_GET_DEBUG: {
1121 struct ng_btsocket_hci_raw_node_debug *p =
1122 (struct ng_btsocket_hci_raw_node_debug *) data;
1124 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1125 NGM_HCI_NODE_GET_DEBUG,
1126 &p->debug, sizeof(p->debug));
1129 case SIOC_HCI_RAW_NODE_SET_DEBUG: {
1130 struct ng_btsocket_hci_raw_node_debug *p =
1131 (struct ng_btsocket_hci_raw_node_debug *) data;
1133 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1134 error = ng_btsocket_hci_raw_send_ngmsg(path,
1135 NGM_HCI_NODE_SET_DEBUG, &p->debug,
1141 case SIOC_HCI_RAW_NODE_GET_BUFFER: {
1142 struct ng_btsocket_hci_raw_node_buffer *p =
1143 (struct ng_btsocket_hci_raw_node_buffer *) data;
1145 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1146 NGM_HCI_NODE_GET_BUFFER,
1147 &p->buffer, sizeof(p->buffer));
1150 case SIOC_HCI_RAW_NODE_GET_BDADDR: {
1151 struct ng_btsocket_hci_raw_node_bdaddr *p =
1152 (struct ng_btsocket_hci_raw_node_bdaddr *) data;
1154 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1155 NGM_HCI_NODE_GET_BDADDR,
1156 &p->bdaddr, sizeof(p->bdaddr));
1159 case SIOC_HCI_RAW_NODE_GET_FEATURES: {
1160 struct ng_btsocket_hci_raw_node_features *p =
1161 (struct ng_btsocket_hci_raw_node_features *) data;
1163 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1164 NGM_HCI_NODE_GET_FEATURES,
1165 &p->features, sizeof(p->features));
1168 case SIOC_HCI_RAW_NODE_GET_STAT: {
1169 struct ng_btsocket_hci_raw_node_stat *p =
1170 (struct ng_btsocket_hci_raw_node_stat *) data;
1172 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1173 NGM_HCI_NODE_GET_STAT,
1174 &p->stat, sizeof(p->stat));
1177 case SIOC_HCI_RAW_NODE_RESET_STAT:
1178 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1179 error = ng_btsocket_hci_raw_send_ngmsg(path,
1180 NGM_HCI_NODE_RESET_STAT, NULL, 0);
1185 case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE:
1186 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1187 error = ng_btsocket_hci_raw_send_ngmsg(path,
1188 NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE,
1194 case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE: {
1195 struct ng_btsocket_hci_raw_node_neighbor_cache *p =
1196 (struct ng_btsocket_hci_raw_node_neighbor_cache *) data;
1197 ng_hci_node_get_neighbor_cache_ep *p1 = NULL;
1198 ng_hci_node_neighbor_cache_entry_ep *p2 = NULL;
1200 if (p->num_entries <= 0 ||
1201 p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM ||
1202 p->entries == NULL) {
1207 NG_MKMESSAGE(ngmsg, NGM_HCI_COOKIE,
1208 NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_WAITOK | M_NULLOK);
1209 if (ngmsg == NULL) {
1213 ng_btsocket_hci_raw_get_token(&ngmsg->header.token);
1214 pcb->token = ngmsg->header.token;
1217 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, ngmsg, path, 0);
1223 error = lksleep(&pcb->msg, &pcb->pcb_lock,
1225 ng_btsocket_hci_raw_ioctl_timeout * hz);
1231 if (pcb->msg != NULL &&
1232 pcb->msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) {
1233 /* Return data back to user space */
1234 p1 = (ng_hci_node_get_neighbor_cache_ep *)
1236 p2 = (ng_hci_node_neighbor_cache_entry_ep *)
1239 p->num_entries = min(p->num_entries, p1->num_entries);
1240 if (p->num_entries > 0)
1241 error = copyout((caddr_t) p2,
1242 (caddr_t) p->entries,
1243 p->num_entries * sizeof(*p2));
1247 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1250 case SIOC_HCI_RAW_NODE_GET_CON_LIST: {
1251 struct ng_btsocket_hci_raw_con_list *p =
1252 (struct ng_btsocket_hci_raw_con_list *) data;
1253 ng_hci_node_con_list_ep *p1 = NULL;
1254 ng_hci_node_con_ep *p2 = NULL;
1256 if (p->num_connections == 0 ||
1257 p->num_connections > NG_HCI_MAX_CON_NUM ||
1258 p->connections == NULL) {
1263 NG_MKMESSAGE(ngmsg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
1264 0, M_WAITOK | M_NULLOK);
1265 if (ngmsg == NULL) {
1269 ng_btsocket_hci_raw_get_token(&ngmsg->header.token);
1270 pcb->token = ngmsg->header.token;
1273 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, ngmsg, path, 0);
1279 error = lksleep(&pcb->msg, &pcb->pcb_lock,
1281 ng_btsocket_hci_raw_ioctl_timeout * hz);
1287 if (pcb->msg != NULL &&
1288 pcb->msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) {
1289 /* Return data back to user space */
1290 p1 = (ng_hci_node_con_list_ep *)(pcb->msg->data);
1291 p2 = (ng_hci_node_con_ep *)(p1 + 1);
1293 p->num_connections = min(p->num_connections,
1294 p1->num_connections);
1295 if (p->num_connections > 0)
1296 error = copyout((caddr_t) p2,
1297 (caddr_t) p->connections,
1298 p->num_connections * sizeof(*p2));
1302 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1305 case SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK: {
1306 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1307 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1310 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1311 NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK,
1312 &p->policy_mask, sizeof(p->policy_mask));
1315 case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: {
1316 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1317 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1320 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1321 error = ng_btsocket_hci_raw_send_ngmsg(path,
1322 NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK,
1324 sizeof(p->policy_mask));
1329 case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: {
1330 struct ng_btsocket_hci_raw_node_packet_mask *p =
1331 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1333 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1334 NGM_HCI_NODE_GET_PACKET_MASK,
1335 &p->packet_mask, sizeof(p->packet_mask));
1338 case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: {
1339 struct ng_btsocket_hci_raw_node_packet_mask *p =
1340 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1342 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1343 error = ng_btsocket_hci_raw_send_ngmsg(path,
1344 NGM_HCI_NODE_SET_PACKET_MASK,
1346 sizeof(p->packet_mask));
1351 case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: {
1352 struct ng_btsocket_hci_raw_node_role_switch *p =
1353 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1355 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1356 NGM_HCI_NODE_GET_ROLE_SWITCH,
1357 &p->role_switch, sizeof(p->role_switch));
1360 case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: {
1361 struct ng_btsocket_hci_raw_node_role_switch *p =
1362 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1364 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1365 error = ng_btsocket_hci_raw_send_ngmsg(path,
1366 NGM_HCI_NODE_SET_ROLE_SWITCH,
1368 sizeof(p->role_switch));
1373 case SIOC_HCI_RAW_NODE_LIST_NAMES: {
1374 struct ng_btsocket_hci_raw_node_list_names *nl =
1375 (struct ng_btsocket_hci_raw_node_list_names *) data;
1376 struct nodeinfo *ni = nl->names;
1378 if (nl->num_names == 0) {
1383 NG_MKMESSAGE(ngmsg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
1384 0, M_WAITOK | M_NULLOK);
1385 if (ngmsg == NULL) {
1389 ng_btsocket_hci_raw_get_token(&ngmsg->header.token);
1390 pcb->token = ngmsg->header.token;
1393 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, ngmsg, ".:", 0);
1399 error = lksleep(&pcb->msg, &pcb->pcb_lock,
1401 ng_btsocket_hci_raw_ioctl_timeout * hz);
1407 if (pcb->msg != NULL && pcb->msg->header.cmd == NGM_LISTNAMES) {
1408 /* Return data back to user space */
1409 struct namelist *nl1 = (struct namelist *) pcb->msg->data;
1410 struct nodeinfo *ni1 = &nl1->nodeinfo[0];
1412 while (nl->num_names > 0 && nl1->numnames > 0) {
1413 if (strcmp(ni1->type, NG_HCI_NODE_TYPE) == 0) {
1414 error = copyout((caddr_t) ni1,
1428 nl->num_names = ni - nl->names;
1432 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1440 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1443 lwkt_replymsg(&msg->control.base.lmsg, error);
1444 } /* ng_btsocket_hci_raw_control */
1447 * Process getsockopt/setsockopt system calls
1451 ng_btsocket_hci_raw_ctloutput(netmsg_t msg)
1453 struct socket *so = msg->ctloutput.base.nm_so;
1454 struct sockopt *sopt = msg->ctloutput.nm_sopt;
1455 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1456 struct ng_btsocket_hci_raw_filter filter;
1463 if (ng_btsocket_hci_raw_node == NULL) {
1468 if (sopt->sopt_level != SOL_HCI_RAW)
1471 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1473 switch (sopt->sopt_dir) {
1475 switch (sopt->sopt_name) {
1476 case SO_HCI_RAW_FILTER:
1477 error = sooptcopyout(sopt, &pcb->filter,
1478 sizeof(pcb->filter));
1481 case SO_HCI_RAW_DIRECTION:
1482 dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1483 error = sooptcopyout(sopt, &dir, sizeof(dir));
1493 switch (sopt->sopt_name) {
1494 case SO_HCI_RAW_FILTER:
1495 error = sooptcopyin(sopt, &filter, sizeof(filter),
1498 bcopy(&filter, &pcb->filter,
1499 sizeof(pcb->filter));
1502 case SO_HCI_RAW_DIRECTION:
1503 error = sooptcopyin(sopt, &dir, sizeof(dir),
1509 pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION;
1511 pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1525 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1528 lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
1529 } /* ng_btsocket_hci_raw_ctloutput */
1532 * Detach raw HCI socket
1536 ng_btsocket_hci_raw_detach(netmsg_t msg)
1538 struct socket *so = msg->detach.base.nm_so;
1539 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1542 KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
1544 if (ng_btsocket_hci_raw_node == NULL)
1547 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_EXCLUSIVE);
1548 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1550 LIST_REMOVE(pcb, next);
1552 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1553 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
1555 lockuninit(&pcb->pcb_lock);
1557 bzero(pcb, sizeof(*pcb));
1558 kfree(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1563 lwkt_replymsg(&msg->detach.base.lmsg, error);
1564 } /* ng_btsocket_hci_raw_detach */
1567 * Disconnect raw HCI socket
1571 ng_btsocket_hci_raw_disconnect(netmsg_t msg)
1573 struct socket *so = msg->disconnect.base.nm_so;
1574 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1581 if (ng_btsocket_hci_raw_node == NULL) {
1586 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1587 soisdisconnected(so);
1588 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1591 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
1592 } /* ng_btsocket_hci_raw_disconnect */
1595 * Get socket peer's address
1599 ng_btsocket_hci_raw_peeraddr(netmsg_t msg)
1601 return (ng_btsocket_hci_raw_sockaddr(msg));
1602 } /* ng_btsocket_hci_raw_peeraddr */
1609 ng_btsocket_hci_raw_send(netmsg_t msg)
1611 struct socket *so = msg->send.base.nm_so;
1612 struct mbuf *control = msg->send.nm_control;
1613 struct mbuf *m = msg->send.nm_m;
1614 struct sockaddr *sa = msg->send.nm_addr;
1615 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1616 struct mbuf *nam = NULL;
1619 if (ng_btsocket_hci_raw_node == NULL) {
1627 if (control != NULL) {
1632 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1633 m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1638 if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1639 if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1644 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1649 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1651 error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1653 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1658 if (pcb->addr.hci_node[0] == 0) {
1659 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1660 error = EDESTADDRREQ;
1664 sa = (struct sockaddr *) &pcb->addr;
1667 MGET(nam, M_NOWAIT, MT_SONAME);
1669 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1674 nam->m_len = sizeof(struct sockaddr_hci);
1675 bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1680 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1682 error = ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1683 ng_btsocket_hci_raw_output, nam, 0);
1687 NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1692 lwkt_replymsg(&msg->send.base.lmsg, error);
1693 } /* ng_btsocket_hci_raw_send */
1696 * Get socket address
1700 ng_btsocket_hci_raw_sockaddr(netmsg_t msg)
1702 struct socket *so = msg->sockaddr.base.nm_so;
1703 struct sockaddr **nam = msg->sockaddr.nm_nam;
1704 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1705 struct sockaddr_hci sa;
1712 if (ng_btsocket_hci_raw_node == NULL) {
1717 bzero(&sa, sizeof(sa));
1718 sa.hci_len = sizeof(sa);
1719 sa.hci_family = AF_BLUETOOTH;
1721 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1722 strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1723 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1725 *nam = dup_sockaddr((struct sockaddr *) &sa);
1731 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
1732 } /* ng_btsocket_hci_raw_sockaddr */