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 <bitstring.h> /* XXX */
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((caddr_t) &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((caddr_t) &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)/
809 sizeof(ng_btsocket_hci_raw_sec_filter->events[0]));
811 /* Disable some critical events */
812 f = ng_btsocket_hci_raw_sec_filter->events;
813 bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
814 bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
815 bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
817 /* Commands - Link control */
818 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1];
819 bit_set(f, NG_HCI_OCF_INQUIRY - 1);
820 bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
821 bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
822 bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
823 bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
824 bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
825 bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
826 bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
828 /* Commands - Link policy */
829 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1];
830 bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
831 bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1);
833 /* Commands - Host controller and baseband */
834 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1];
835 bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
836 bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
837 bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
838 bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
839 bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
840 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1);
841 bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1);
842 bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
843 bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
844 bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
845 bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
846 bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
847 bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1);
848 bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1);
849 bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
850 bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
851 bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1);
852 bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
853 bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
854 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
855 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
857 /* Commands - Informational */
858 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1];
859 bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
860 bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
861 bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
862 bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
863 bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
865 /* Commands - Status */
866 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1];
867 bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1);
868 bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
869 bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
871 /* Commands - Testing */
872 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1];
873 bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
874 } /* ng_btsocket_hci_raw_init */
877 * Abort connection on socket
881 ng_btsocket_hci_raw_abort(netmsg_t msg)
883 } /* ng_btsocket_hci_raw_abort */
887 ng_btsocket_hci_raw_close(struct socket *so)
889 } /* ng_btsocket_hci_raw_close */
893 * Create new raw HCI socket
897 ng_btsocket_hci_raw_attach(netmsg_t msg)
899 struct socket *so = msg->attach.base.nm_so;
900 int proto = msg->attach.nm_proto;
901 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
909 if (ng_btsocket_hci_raw_node == NULL) {
910 error = EPROTONOSUPPORT;
913 if (proto != BLUETOOTH_PROTO_HCI) {
914 error = EPROTONOSUPPORT;
917 if (so->so_type != SOCK_RAW) {
918 error = ESOCKTNOSUPPORT;
922 error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
923 NG_BTSOCKET_HCI_RAW_RECVSPACE, NULL);
927 pcb = kmalloc(sizeof(*pcb), M_NETGRAPH_BTSOCKET_HCI_RAW,
928 M_WAITOK | M_NULLOK | M_ZERO);
934 so->so_pcb = (caddr_t) pcb;
937 if (curproc == NULL ||
938 priv_check(curthread, PRIV_NETBLUETOOTH_RAW) == 0)
939 pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED;
942 * Set default socket filter. By default socket only accepts HCI
943 * Command_Complete and Command_Status event packets.
946 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1);
947 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1);
949 lockinit(&pcb->pcb_lock, "btsocks_hci_raw_pcb_lock", 0, 0);
951 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_EXCLUSIVE);
952 LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
953 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
956 lwkt_replymsg(&msg->attach.base.lmsg, error);
957 } /* ng_btsocket_hci_raw_attach */
960 * Bind raw HCI socket
964 ng_btsocket_hci_raw_bind(netmsg_t msg)
966 struct socket *so = msg->bind.base.nm_so;
967 struct sockaddr *nam = msg->bind.nm_nam;
968 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
969 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
976 if (ng_btsocket_hci_raw_node == NULL) {
985 if (sa->hci_family != AF_BLUETOOTH) {
986 error = EAFNOSUPPORT;
989 if (sa->hci_len != sizeof(*sa)) {
993 if (sa->hci_node[0] == 0) {
998 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
999 bcopy(sa, &pcb->addr, sizeof(pcb->addr));
1000 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1003 lwkt_replymsg(&msg->bind.base.lmsg, error);
1004 } /* ng_btsocket_hci_raw_bind */
1007 * Connect raw HCI socket
1011 ng_btsocket_hci_raw_connect(netmsg_t msg)
1013 struct socket *so = msg->connect.base.nm_so;
1014 struct sockaddr *nam = msg->connect.nm_nam;
1015 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1016 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
1023 if (ng_btsocket_hci_raw_node == NULL) {
1032 if (sa->hci_family != AF_BLUETOOTH) {
1033 error = EAFNOSUPPORT;
1036 if (sa->hci_len != sizeof(*sa)) {
1040 if (sa->hci_node[0] == 0) {
1041 error = EDESTADDRREQ;
1045 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1047 if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
1048 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1049 error = EADDRNOTAVAIL;
1055 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1058 lwkt_replymsg(&msg->connect.base.lmsg, error);
1059 } /* ng_btsocket_hci_raw_connect */
1062 * Process ioctl on socket
1066 ng_btsocket_hci_raw_control(netmsg_t msg)
1068 struct socket *so = msg->control.base.nm_so;
1069 u_long cmd = msg->control.nm_cmd;
1070 caddr_t data = msg->control.nm_data;
1071 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1072 char path[NG_NODESIZ + 1];
1073 struct ng_mesg *ngmsg = NULL;
1080 if (ng_btsocket_hci_raw_node == NULL) {
1085 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1087 /* Check if we have device name */
1088 if (pcb->addr.hci_node[0] == 0) {
1089 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1090 error = EHOSTUNREACH;
1094 /* Check if we have pending ioctl() */
1095 if (pcb->token != 0) {
1096 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1101 ksnprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1104 case SIOC_HCI_RAW_NODE_GET_STATE: {
1105 struct ng_btsocket_hci_raw_node_state *p =
1106 (struct ng_btsocket_hci_raw_node_state *) data;
1108 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1109 NGM_HCI_NODE_GET_STATE,
1110 &p->state, sizeof(p->state));
1113 case SIOC_HCI_RAW_NODE_INIT:
1114 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1115 error = ng_btsocket_hci_raw_send_ngmsg(path,
1116 NGM_HCI_NODE_INIT, NULL, 0);
1121 case SIOC_HCI_RAW_NODE_GET_DEBUG: {
1122 struct ng_btsocket_hci_raw_node_debug *p =
1123 (struct ng_btsocket_hci_raw_node_debug *) data;
1125 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1126 NGM_HCI_NODE_GET_DEBUG,
1127 &p->debug, sizeof(p->debug));
1130 case SIOC_HCI_RAW_NODE_SET_DEBUG: {
1131 struct ng_btsocket_hci_raw_node_debug *p =
1132 (struct ng_btsocket_hci_raw_node_debug *) data;
1134 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1135 error = ng_btsocket_hci_raw_send_ngmsg(path,
1136 NGM_HCI_NODE_SET_DEBUG, &p->debug,
1142 case SIOC_HCI_RAW_NODE_GET_BUFFER: {
1143 struct ng_btsocket_hci_raw_node_buffer *p =
1144 (struct ng_btsocket_hci_raw_node_buffer *) data;
1146 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1147 NGM_HCI_NODE_GET_BUFFER,
1148 &p->buffer, sizeof(p->buffer));
1151 case SIOC_HCI_RAW_NODE_GET_BDADDR: {
1152 struct ng_btsocket_hci_raw_node_bdaddr *p =
1153 (struct ng_btsocket_hci_raw_node_bdaddr *) data;
1155 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1156 NGM_HCI_NODE_GET_BDADDR,
1157 &p->bdaddr, sizeof(p->bdaddr));
1160 case SIOC_HCI_RAW_NODE_GET_FEATURES: {
1161 struct ng_btsocket_hci_raw_node_features *p =
1162 (struct ng_btsocket_hci_raw_node_features *) data;
1164 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1165 NGM_HCI_NODE_GET_FEATURES,
1166 &p->features, sizeof(p->features));
1169 case SIOC_HCI_RAW_NODE_GET_STAT: {
1170 struct ng_btsocket_hci_raw_node_stat *p =
1171 (struct ng_btsocket_hci_raw_node_stat *) data;
1173 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1174 NGM_HCI_NODE_GET_STAT,
1175 &p->stat, sizeof(p->stat));
1178 case SIOC_HCI_RAW_NODE_RESET_STAT:
1179 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1180 error = ng_btsocket_hci_raw_send_ngmsg(path,
1181 NGM_HCI_NODE_RESET_STAT, NULL, 0);
1186 case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE:
1187 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1188 error = ng_btsocket_hci_raw_send_ngmsg(path,
1189 NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE,
1195 case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE: {
1196 struct ng_btsocket_hci_raw_node_neighbor_cache *p =
1197 (struct ng_btsocket_hci_raw_node_neighbor_cache *) data;
1198 ng_hci_node_get_neighbor_cache_ep *p1 = NULL;
1199 ng_hci_node_neighbor_cache_entry_ep *p2 = NULL;
1201 if (p->num_entries <= 0 ||
1202 p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM ||
1203 p->entries == NULL) {
1208 NG_MKMESSAGE(ngmsg, NGM_HCI_COOKIE,
1209 NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_WAITOK | M_NULLOK);
1210 if (ngmsg == NULL) {
1214 ng_btsocket_hci_raw_get_token(&ngmsg->header.token);
1215 pcb->token = ngmsg->header.token;
1218 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, ngmsg, path, 0);
1224 error = lksleep(&pcb->msg, &pcb->pcb_lock,
1226 ng_btsocket_hci_raw_ioctl_timeout * hz);
1232 if (pcb->msg != NULL &&
1233 pcb->msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) {
1234 /* Return data back to user space */
1235 p1 = (ng_hci_node_get_neighbor_cache_ep *)
1237 p2 = (ng_hci_node_neighbor_cache_entry_ep *)
1240 p->num_entries = min(p->num_entries, p1->num_entries);
1241 if (p->num_entries > 0)
1242 error = copyout((caddr_t) p2,
1243 (caddr_t) p->entries,
1244 p->num_entries * sizeof(*p2));
1248 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1251 case SIOC_HCI_RAW_NODE_GET_CON_LIST: {
1252 struct ng_btsocket_hci_raw_con_list *p =
1253 (struct ng_btsocket_hci_raw_con_list *) data;
1254 ng_hci_node_con_list_ep *p1 = NULL;
1255 ng_hci_node_con_ep *p2 = NULL;
1257 if (p->num_connections == 0 ||
1258 p->num_connections > NG_HCI_MAX_CON_NUM ||
1259 p->connections == NULL) {
1264 NG_MKMESSAGE(ngmsg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST,
1265 0, M_WAITOK | M_NULLOK);
1266 if (ngmsg == NULL) {
1270 ng_btsocket_hci_raw_get_token(&ngmsg->header.token);
1271 pcb->token = ngmsg->header.token;
1274 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, ngmsg, path, 0);
1280 error = lksleep(&pcb->msg, &pcb->pcb_lock,
1282 ng_btsocket_hci_raw_ioctl_timeout * hz);
1288 if (pcb->msg != NULL &&
1289 pcb->msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) {
1290 /* Return data back to user space */
1291 p1 = (ng_hci_node_con_list_ep *)(pcb->msg->data);
1292 p2 = (ng_hci_node_con_ep *)(p1 + 1);
1294 p->num_connections = min(p->num_connections,
1295 p1->num_connections);
1296 if (p->num_connections > 0)
1297 error = copyout((caddr_t) p2,
1298 (caddr_t) p->connections,
1299 p->num_connections * sizeof(*p2));
1303 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1306 case SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK: {
1307 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1308 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1311 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1312 NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK,
1313 &p->policy_mask, sizeof(p->policy_mask));
1316 case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: {
1317 struct ng_btsocket_hci_raw_node_link_policy_mask *p =
1318 (struct ng_btsocket_hci_raw_node_link_policy_mask *)
1321 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1322 error = ng_btsocket_hci_raw_send_ngmsg(path,
1323 NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK,
1325 sizeof(p->policy_mask));
1330 case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: {
1331 struct ng_btsocket_hci_raw_node_packet_mask *p =
1332 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1334 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1335 NGM_HCI_NODE_GET_PACKET_MASK,
1336 &p->packet_mask, sizeof(p->packet_mask));
1339 case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: {
1340 struct ng_btsocket_hci_raw_node_packet_mask *p =
1341 (struct ng_btsocket_hci_raw_node_packet_mask *) data;
1343 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1344 error = ng_btsocket_hci_raw_send_ngmsg(path,
1345 NGM_HCI_NODE_SET_PACKET_MASK,
1347 sizeof(p->packet_mask));
1352 case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: {
1353 struct ng_btsocket_hci_raw_node_role_switch *p =
1354 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1356 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1357 NGM_HCI_NODE_GET_ROLE_SWITCH,
1358 &p->role_switch, sizeof(p->role_switch));
1361 case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: {
1362 struct ng_btsocket_hci_raw_node_role_switch *p =
1363 (struct ng_btsocket_hci_raw_node_role_switch *) data;
1365 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)
1366 error = ng_btsocket_hci_raw_send_ngmsg(path,
1367 NGM_HCI_NODE_SET_ROLE_SWITCH,
1369 sizeof(p->role_switch));
1374 case SIOC_HCI_RAW_NODE_LIST_NAMES: {
1375 struct ng_btsocket_hci_raw_node_list_names *nl =
1376 (struct ng_btsocket_hci_raw_node_list_names *) data;
1377 struct nodeinfo *ni = nl->names;
1379 if (nl->num_names == 0) {
1384 NG_MKMESSAGE(ngmsg, NGM_GENERIC_COOKIE, NGM_LISTNAMES,
1385 0, M_WAITOK | M_NULLOK);
1386 if (ngmsg == NULL) {
1390 ng_btsocket_hci_raw_get_token(&ngmsg->header.token);
1391 pcb->token = ngmsg->header.token;
1394 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, ngmsg, ".:", 0);
1400 error = lksleep(&pcb->msg, &pcb->pcb_lock,
1402 ng_btsocket_hci_raw_ioctl_timeout * hz);
1408 if (pcb->msg != NULL && pcb->msg->header.cmd == NGM_LISTNAMES) {
1409 /* Return data back to user space */
1410 struct namelist *nl1 = (struct namelist *) pcb->msg->data;
1411 struct nodeinfo *ni1 = &nl1->nodeinfo[0];
1413 while (nl->num_names > 0 && nl1->numnames > 0) {
1414 if (strcmp(ni1->type, NG_HCI_NODE_TYPE) == 0) {
1415 error = copyout((caddr_t) ni1,
1429 nl->num_names = ni - nl->names;
1433 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1441 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1444 lwkt_replymsg(&msg->control.base.lmsg, error);
1445 } /* ng_btsocket_hci_raw_control */
1448 * Process getsockopt/setsockopt system calls
1452 ng_btsocket_hci_raw_ctloutput(netmsg_t msg)
1454 struct socket *so = msg->ctloutput.base.nm_so;
1455 struct sockopt *sopt = msg->ctloutput.nm_sopt;
1456 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1457 struct ng_btsocket_hci_raw_filter filter;
1464 if (ng_btsocket_hci_raw_node == NULL) {
1469 if (sopt->sopt_level != SOL_HCI_RAW)
1472 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1474 switch (sopt->sopt_dir) {
1476 switch (sopt->sopt_name) {
1477 case SO_HCI_RAW_FILTER:
1478 error = sooptcopyout(sopt, &pcb->filter,
1479 sizeof(pcb->filter));
1482 case SO_HCI_RAW_DIRECTION:
1483 dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1484 error = sooptcopyout(sopt, &dir, sizeof(dir));
1494 switch (sopt->sopt_name) {
1495 case SO_HCI_RAW_FILTER:
1496 error = sooptcopyin(sopt, &filter, sizeof(filter),
1499 bcopy(&filter, &pcb->filter,
1500 sizeof(pcb->filter));
1503 case SO_HCI_RAW_DIRECTION:
1504 error = sooptcopyin(sopt, &dir, sizeof(dir),
1510 pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION;
1512 pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1526 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1529 lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
1530 } /* ng_btsocket_hci_raw_ctloutput */
1533 * Detach raw HCI socket
1537 ng_btsocket_hci_raw_detach(netmsg_t msg)
1539 struct socket *so = msg->detach.base.nm_so;
1540 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1543 KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
1545 if (ng_btsocket_hci_raw_node == NULL)
1548 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_EXCLUSIVE);
1549 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1551 LIST_REMOVE(pcb, next);
1553 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1554 lockmgr(&ng_btsocket_hci_raw_sockets_lock, LK_RELEASE);
1556 lockuninit(&pcb->pcb_lock);
1558 bzero(pcb, sizeof(*pcb));
1559 kfree(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1564 lwkt_replymsg(&msg->detach.base.lmsg, error);
1565 } /* ng_btsocket_hci_raw_detach */
1568 * Disconnect raw HCI socket
1572 ng_btsocket_hci_raw_disconnect(netmsg_t msg)
1574 struct socket *so = msg->disconnect.base.nm_so;
1575 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1582 if (ng_btsocket_hci_raw_node == NULL) {
1587 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1588 soisdisconnected(so);
1589 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1592 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
1593 } /* ng_btsocket_hci_raw_disconnect */
1596 * Get socket peer's address
1600 ng_btsocket_hci_raw_peeraddr(netmsg_t msg)
1602 return (ng_btsocket_hci_raw_sockaddr(msg));
1603 } /* ng_btsocket_hci_raw_peeraddr */
1610 ng_btsocket_hci_raw_send(netmsg_t msg)
1612 struct socket *so = msg->send.base.nm_so;
1613 struct mbuf *control = msg->send.nm_control;
1614 struct mbuf *m = msg->send.nm_m;
1615 struct sockaddr *sa = msg->send.nm_addr;
1616 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1617 struct mbuf *nam = NULL;
1620 if (ng_btsocket_hci_raw_node == NULL) {
1628 if (control != NULL) {
1633 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1634 m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1639 if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1640 if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1645 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1650 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1652 error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1654 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1659 if (pcb->addr.hci_node[0] == 0) {
1660 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1661 error = EDESTADDRREQ;
1665 sa = (struct sockaddr *) &pcb->addr;
1668 MGET(nam, M_NOWAIT, MT_SONAME);
1670 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1675 nam->m_len = sizeof(struct sockaddr_hci);
1676 bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1681 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1683 error = ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1684 ng_btsocket_hci_raw_output, nam, 0);
1688 NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1693 lwkt_replymsg(&msg->send.base.lmsg, error);
1694 } /* ng_btsocket_hci_raw_send */
1697 * Get socket address
1701 ng_btsocket_hci_raw_sockaddr(netmsg_t msg)
1703 struct socket *so = msg->sockaddr.base.nm_so;
1704 struct sockaddr **nam = msg->sockaddr.nm_nam;
1705 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so);
1706 struct sockaddr_hci sa;
1713 if (ng_btsocket_hci_raw_node == NULL) {
1718 bzero(&sa, sizeof(sa));
1719 sa.hci_len = sizeof(sa);
1720 sa.hci_family = AF_BLUETOOTH;
1722 lockmgr(&pcb->pcb_lock, LK_EXCLUSIVE);
1723 strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1724 lockmgr(&pcb->pcb_lock, LK_RELEASE);
1726 *nam = dup_sockaddr((struct sockaddr *) &sa);
1732 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
1733 } /* ng_btsocket_hci_raw_sockaddr */