FreeBSD kernel netgraph code
ng_btsocket_hci_raw.c
Go to the documentation of this file.
1/*
2 * ng_btsocket_hci_raw.c
3 */
4
5/*-
6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7 *
8 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $Id: ng_btsocket_hci_raw.c,v 1.14 2003/09/14 23:29:06 max Exp $
33 * $FreeBSD$
34 */
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/bitstring.h>
39#include <sys/domain.h>
40#include <sys/endian.h>
41#include <sys/errno.h>
42#include <sys/filedesc.h>
43#include <sys/ioccom.h>
44#include <sys/kernel.h>
45#include <sys/lock.h>
46#include <sys/malloc.h>
47#include <sys/mbuf.h>
48#include <sys/mutex.h>
49#include <sys/priv.h>
50#include <sys/protosw.h>
51#include <sys/queue.h>
52#include <sys/socket.h>
53#include <sys/socketvar.h>
54#include <sys/sysctl.h>
55#include <sys/taskqueue.h>
56
57#include <net/vnet.h>
58
59#include <netgraph/ng_message.h>
60#include <netgraph/netgraph.h>
66
67/* MALLOC define */
68#ifdef NG_SEPARATE_MALLOC
69static MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_HCI_RAW, "netgraph_btsocks_hci_raw",
70 "Netgraph Bluetooth raw HCI sockets");
71#else
72#define M_NETGRAPH_BTSOCKET_HCI_RAW M_NETGRAPH
73#endif /* NG_SEPARATE_MALLOC */
74
75/* Netgraph node methods */
83
84static void ng_btsocket_hci_raw_input (void *, int);
85static void ng_btsocket_hci_raw_output(node_p, hook_p, void *, int);
87 struct mbuf **,
88 struct mbuf *);
90 struct mbuf *, int);
91
92#define ng_btsocket_hci_raw_wakeup_input_task() \
93 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_hci_raw_task)
94
95/* Security filter */
97 bitstr_t bit_decl(events, 0xff);
98 bitstr_t bit_decl(commands[0x3f], 0x3ff);
99};
100
101/* Netgraph type descriptor */
102static struct ng_type typestruct = {
112};
113
114/* Globals */
120static struct task ng_btsocket_hci_raw_task;
121static LIST_HEAD(, ng_btsocket_hci_raw_pcb) ng_btsocket_hci_raw_sockets;
122static struct mtx ng_btsocket_hci_raw_sockets_mtx;
123static u_int32_t ng_btsocket_hci_raw_token;
124static struct mtx ng_btsocket_hci_raw_token_mtx;
126static struct timeval ng_btsocket_hci_raw_lasttime;
127static int ng_btsocket_hci_raw_curpps;
128
129/* Sysctl tree */
130SYSCTL_DECL(_net_bluetooth_hci_sockets);
131static SYSCTL_NODE(_net_bluetooth_hci_sockets, OID_AUTO, raw,
132 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
133 "Bluetooth raw HCI sockets family");
134SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, debug_level, CTLFLAG_RW,
136 "Bluetooth raw HCI sockets debug level");
137SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, ioctl_timeout, CTLFLAG_RW,
139 "Bluetooth raw HCI sockets ioctl timeout");
140SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_len, CTLFLAG_RD,
142 "Bluetooth raw HCI sockets input queue length");
143SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_maxlen, CTLFLAG_RD,
145 "Bluetooth raw HCI sockets input queue max. length");
146SYSCTL_UINT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_drops, CTLFLAG_RD,
148 "Bluetooth raw HCI sockets input queue drops");
149
150/* Debug */
151#define NG_BTSOCKET_HCI_RAW_INFO \
152 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
153 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
154 printf
155
156#define NG_BTSOCKET_HCI_RAW_WARN \
157 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
158 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
159 printf
160
161#define NG_BTSOCKET_HCI_RAW_ERR \
162 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
163 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
164 printf
165
166#define NG_BTSOCKET_HCI_RAW_ALERT \
167 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
168 ppsratecheck(&ng_btsocket_hci_raw_lasttime, &ng_btsocket_hci_raw_curpps, 1)) \
169 printf
170
171/****************************************************************************
172 ****************************************************************************
173 ** Netgraph specific
174 ****************************************************************************
175 ****************************************************************************/
176
177/*
178 * Netgraph node constructor. Do not allow to create node of this type.
179 */
180
181static int
183{
184 return (EINVAL);
185} /* ng_btsocket_hci_raw_node_constructor */
186
187/*
188 * Netgraph node destructor. Just let old node go and create new fresh one.
189 */
190
191static int
193{
194 int error = 0;
195
196 NG_NODE_UNREF(node);
197
199 if (error != 0) {
201"%s: Could not create Netgraph node, error=%d\n", __func__, error);
202
204
205 return (ENOMEM);
206 }
207
210 if (error != 0) {
212"%s: Could not name Netgraph node, error=%d\n", __func__, error);
213
216
217 return (EINVAL);
218 }
219
220 return (0);
221} /* ng_btsocket_hci_raw_node_shutdown */
222
223/*
224 * Create new hook. Just say "yes"
225 */
226
227static int
229{
230 return (0);
231} /* ng_btsocket_hci_raw_node_newhook */
232
233/*
234 * Connect hook. Just say "yes"
235 */
236
237static int
239{
240 return (0);
241} /* ng_btsocket_hci_raw_node_connect */
242
243/*
244 * Disconnect hook
245 */
246
247static int
249{
250 return (0);
251} /* ng_btsocket_hci_raw_node_disconnect */
252
253/*
254 * Receive control message.
255 * Make sure it is a message from HCI node and it is a response.
256 * Enqueue item and schedule input task.
257 */
258
259static int
261{
262 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
263 int error = 0;
264
265 /*
266 * Check for empty sockets list creates LOR when both sender and
267 * receiver device are connected to the same host, so remove it
268 * for now
269 */
270
271 if (msg != NULL &&
274 msg->header.flags & NGF_RESP) {
275 if (msg->header.token == 0) {
276 NG_FREE_ITEM(item);
277 return (0);
278 }
279
283"%s: Input queue is full\n", __func__);
284
286 NG_FREE_ITEM(item);
287 error = ENOBUFS;
288 } else {
291 }
293 } else {
294 NG_FREE_ITEM(item);
295 error = EINVAL;
296 }
297
298 return (error);
299} /* ng_btsocket_hci_raw_node_rcvmsg */
300
301/*
302 * Receive packet from the one of our hook.
303 * Prepend every packet with sockaddr_hci and record sender's node name.
304 * Enqueue item and schedule input task.
305 */
306
307static int
309{
310 struct mbuf *nam = NULL;
311 int error;
312
313 /*
314 * Check for empty sockets list creates LOR when both sender and
315 * receiver device are connected to the same host, so remove it
316 * for now
317 */
318
319 MGET(nam, M_NOWAIT, MT_SONAME);
320 if (nam != NULL) {
321 struct sockaddr_hci *sa = mtod(nam, struct sockaddr_hci *);
322
323 nam->m_len = sizeof(struct sockaddr_hci);
324
325 sa->hci_len = sizeof(*sa);
326 sa->hci_family = AF_BLUETOOTH;
327 strlcpy(sa->hci_node, NG_PEER_NODE_NAME(hook),
328 sizeof(sa->hci_node));
329
330 NGI_GET_M(item, nam->m_next);
331 NGI_M(item) = nam;
332
336"%s: Input queue is full\n", __func__);
337
339 NG_FREE_ITEM(item);
340 error = ENOBUFS;
341 } else {
344 }
346 } else {
348"%s: Failed to allocate address mbuf\n", __func__);
349
350 NG_FREE_ITEM(item);
351 error = ENOBUFS;
352 }
353
354 return (error);
355} /* ng_btsocket_hci_raw_node_rcvdata */
356
357/****************************************************************************
358 ****************************************************************************
359 ** Sockets specific
360 ****************************************************************************
361 ****************************************************************************/
362
363/*
364 * Get next token. We need token to avoid theoretical race where process
365 * submits ioctl() message then interrupts ioctl() and re-submits another
366 * ioctl() on the same socket *before* first ioctl() complete.
367 */
368
369static void
371{
372 mtx_lock(&ng_btsocket_hci_raw_token_mtx);
373
374 if (++ ng_btsocket_hci_raw_token == 0)
375 ng_btsocket_hci_raw_token = 1;
376
377 *token = ng_btsocket_hci_raw_token;
378
379 mtx_unlock(&ng_btsocket_hci_raw_token_mtx);
380} /* ng_btsocket_hci_raw_get_token */
381
382/*
383 * Send Netgraph message to the node - do not expect reply
384 */
385
386static int
387ng_btsocket_hci_raw_send_ngmsg(char *path, int cmd, void *arg, int arglen)
388{
389 struct ng_mesg *msg = NULL;
390 int error = 0;
391
392 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, arglen, M_NOWAIT);
393 if (msg == NULL)
394 return (ENOMEM);
395
396 if (arg != NULL && arglen > 0)
397 bcopy(arg, msg->data, arglen);
398
399 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
400
401 return (error);
402} /* ng_btsocket_hci_raw_send_ngmsg */
403
404/*
405 * Send Netgraph message to the node (no data) and wait for reply
406 */
407
408static int
410 int cmd, void *rsp, int rsplen)
411{
412 struct ng_mesg *msg = NULL;
413 int error = 0;
414
415 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
416
417 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, 0, M_NOWAIT);
418 if (msg == NULL)
419 return (ENOMEM);
420
422 pcb->token = msg->header.token;
423 pcb->msg = NULL;
424
425 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
426 if (error != 0) {
427 pcb->token = 0;
428 return (error);
429 }
430
431 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "hcictl",
433 pcb->token = 0;
434
435 if (error != 0)
436 return (error);
437
438 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
439 bcopy(pcb->msg->data, rsp, rsplen);
440 else
441 error = EINVAL;
442
443 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
444
445 return (0);
446} /* ng_btsocket_hci_raw_send_sync_ngmsg */
447
448/*
449 * Create control information for the packet
450 */
451
452static void
454 struct mbuf *m)
455{
456 int dir;
457 struct timeval tv;
458
459 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
460
462 dir = (m->m_flags & M_PROTO1)? 1 : 0;
463 *ctl = sbcreatecontrol((caddr_t) &dir, sizeof(dir),
465 if (*ctl != NULL)
466 ctl = &((*ctl)->m_next);
467 }
468
469 if (pcb->so->so_options & SO_TIMESTAMP) {
470 microtime(&tv);
471 *ctl = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
472 SCM_TIMESTAMP, SOL_SOCKET);
473 if (*ctl != NULL)
474 ctl = &((*ctl)->m_next);
475 }
476} /* ng_btsocket_hci_raw_savctl */
477
478/*
479 * Raw HCI sockets data input routine
480 */
481
482static void
484{
485 ng_btsocket_hci_raw_pcb_p pcb = NULL;
486 struct mbuf *m0 = NULL, *m = NULL;
487 struct sockaddr_hci *sa = NULL;
488
489 m0 = nam->m_next;
490 nam->m_next = NULL;
491
492 KASSERT((nam->m_type == MT_SONAME),
493 ("%s: m_type=%d\n", __func__, nam->m_type));
494 KASSERT((m0->m_flags & M_PKTHDR),
495 ("%s: m_flags=%#x\n", __func__, m0->m_flags));
496
497 sa = mtod(nam, struct sockaddr_hci *);
498
499 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
500
501 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
502 mtx_lock(&pcb->pcb_mtx);
503
504 /*
505 * If socket was bound then check address and
506 * make sure it matches.
507 */
508
509 if (pcb->addr.hci_node[0] != 0 &&
510 strcmp(sa->hci_node, pcb->addr.hci_node) != 0)
511 goto next;
512
513 /*
514 * Check packet against filters
515 * XXX do we have to call m_pullup() here?
516 */
517
518 if (ng_btsocket_hci_raw_filter(pcb, m0, 1) != 0)
519 goto next;
520
521 /*
522 * Make a copy of the packet, append to the socket's
523 * receive queue and wakeup socket. sbappendaddr()
524 * will check if socket has enough buffer space.
525 */
526
527 m = m_dup(m0, M_NOWAIT);
528 if (m != NULL) {
529 struct mbuf *ctl = NULL;
530
531 ng_btsocket_hci_raw_savctl(pcb, &ctl, m);
532
533 if (sbappendaddr(&pcb->so->so_rcv,
534 (struct sockaddr *) sa, m, ctl))
535 sorwakeup(pcb->so);
536 else {
538"%s: sbappendaddr() failed\n", __func__);
539
540 NG_FREE_M(m);
541 NG_FREE_M(ctl);
542 soroverflow(pcb->so);
543 }
544 }
545next:
546 mtx_unlock(&pcb->pcb_mtx);
547 }
548
549 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
550
551 NG_FREE_M(nam);
552 NG_FREE_M(m0);
553} /* ng_btsocket_hci_raw_data_input */
554
555/*
556 * Raw HCI sockets message input routine
557 */
558
559static void
561{
562 ng_btsocket_hci_raw_pcb_p pcb = NULL;
563
564 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
565
566 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) {
567 mtx_lock(&pcb->pcb_mtx);
568
569 if (msg->header.token == pcb->token) {
570 pcb->msg = msg;
571 wakeup(&pcb->msg);
572
573 mtx_unlock(&pcb->pcb_mtx);
574 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
575
576 return;
577 }
578
579 mtx_unlock(&pcb->pcb_mtx);
580 }
581
582 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
583
584 NG_FREE_MSG(msg); /* checks for != NULL */
585} /* ng_btsocket_hci_raw_msg_input */
586
587/*
588 * Raw HCI sockets input routines
589 */
590
591static void
592ng_btsocket_hci_raw_input(void *context, int pending)
593{
594 item_p item = NULL;
595
596 for (;;) {
600
601 if (item == NULL)
602 break;
603
604 switch(item->el_flags & NGQF_TYPE) {
605 case NGQF_DATA: {
606 struct mbuf *m = NULL;
607
608 NGI_GET_M(item, m);
610 } break;
611
612 case NGQF_MESG: {
613 struct ng_mesg *msg = NULL;
614
615 NGI_GET_MSG(item, msg);
617 } break;
618
619 default:
620 KASSERT(0,
621("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
622 break;
623 }
624
625 NG_FREE_ITEM(item);
626 }
627} /* ng_btsocket_hci_raw_input */
628
629/*
630 * Raw HCI sockets output routine
631 */
632
633static void
634ng_btsocket_hci_raw_output(node_p node, hook_p hook, void *arg1, int arg2)
635{
636 struct mbuf *nam = (struct mbuf *) arg1, *m = NULL;
637 struct sockaddr_hci *sa = NULL;
638 int error;
639
640 m = nam->m_next;
641 nam->m_next = NULL;
642
643 KASSERT((nam->m_type == MT_SONAME),
644 ("%s: m_type=%d\n", __func__, nam->m_type));
645 KASSERT((m->m_flags & M_PKTHDR),
646 ("%s: m_flags=%#x\n", __func__, m->m_flags));
647
648 sa = mtod(nam, struct sockaddr_hci *);
649
650 /*
651 * Find downstream hook
652 * XXX For now access node hook list directly. Should be safe because
653 * we used ng_send_fn() and we should have exclusive lock on the node.
654 */
655
656 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
657 if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
659 continue;
660
661 if (strcmp(sa->hci_node, NG_PEER_NODE_NAME(hook)) == 0) {
662 NG_SEND_DATA_ONLY(error, hook, m); /* sets m to NULL */
663 break;
664 }
665 }
666
667 NG_FREE_M(nam); /* check for != NULL */
668 NG_FREE_M(m);
669} /* ng_btsocket_hci_raw_output */
670
671/*
672 * Check frame against security and socket filters.
673 * d (direction bit) == 1 means incoming frame.
674 */
675
676static int
678{
679 int type, event, opcode;
680
681 mtx_assert(&pcb->pcb_mtx, MA_OWNED);
682
683 switch ((type = *mtod(m, u_int8_t *))) {
684 case NG_HCI_CMD_PKT:
686 opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode);
687
688 if (!bit_test(
690NG_HCI_OCF(opcode) - 1))
691 return (EPERM);
692 }
693
694 if (d && !bit_test(pcb->filter.packet_mask, NG_HCI_CMD_PKT - 1))
695 return (EPERM);
696 break;
697
701 !bit_test(pcb->filter.packet_mask, type - 1) ||
702 !d)
703 return (EPERM);
704 break;
705
706 case NG_HCI_EVENT_PKT:
707 if (!d)
708 return (EINVAL);
709
710 event = mtod(m, ng_hci_event_pkt_t *)->event - 1;
711
713 if (!bit_test(ng_btsocket_hci_raw_sec_filter->events, event))
714 return (EPERM);
715
716 if (!bit_test(pcb->filter.event_mask, event))
717 return (EPERM);
718 break;
719
720 default:
721 return (EINVAL);
722 }
723
724 return (0);
725} /* ng_btsocket_hci_raw_filter */
726
727/*
728 * Initialize everything
729 */
730
731static void
732ng_btsocket_hci_raw_init(void *arg __unused)
733{
734 bitstr_t *f = NULL;
735 int error = 0;
736
740
741 /* Register Netgraph node type */
742 error = ng_newtype(&typestruct);
743 if (error != 0) {
745"%s: Could not register Netgraph node type, error=%d\n", __func__, error);
746
747 return;
748 }
749
750 /* Create Netgrapg node */
752 if (error != 0) {
754"%s: Could not create Netgraph node, error=%d\n", __func__, error);
755
757
758 return;
759 }
760
763 if (error != 0) {
765"%s: Could not name Netgraph node, error=%d\n", __func__, error);
766
769
770 return;
771 }
772
773 /* Create input queue */
776 "btsocks_hci_raw_queue_mtx", NULL, MTX_DEF);
777 TASK_INIT(&ng_btsocket_hci_raw_task, 0,
779
780 /* Create list of sockets */
781 LIST_INIT(&ng_btsocket_hci_raw_sockets);
782 mtx_init(&ng_btsocket_hci_raw_sockets_mtx,
783 "btsocks_hci_raw_sockets_mtx", NULL, MTX_DEF);
784
785 /* Tokens */
786 ng_btsocket_hci_raw_token = 0;
787 mtx_init(&ng_btsocket_hci_raw_token_mtx,
788 "btsocks_hci_raw_token_mtx", NULL, MTX_DEF);
789
790 /*
791 * Security filter
792 * XXX never free()ed
793 */
795 malloc(sizeof(struct ng_btsocket_hci_raw_sec_filter),
796 M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
797 if (ng_btsocket_hci_raw_sec_filter == NULL) {
798 printf("%s: Could not allocate security filter!\n", __func__);
799 return;
800 }
801
802 /*
803 * XXX How paranoid can we get?
804 *
805 * Initialize security filter. If bit is set in the mask then
806 * unprivileged socket is allowed to send (receive) this command
807 * (event).
808 */
809
810 /* Enable all events */
811 memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff,
812 sizeof(ng_btsocket_hci_raw_sec_filter->events)/
813 sizeof(ng_btsocket_hci_raw_sec_filter->events[0]));
814
815 /* Disable some critical events */
817 bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1);
818 bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1);
819 bit_clear(f, NG_HCI_EVENT_VENDOR - 1);
820
821 /* Commands - Link control */
823 bit_set(f, NG_HCI_OCF_INQUIRY - 1);
824 bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1);
825 bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1);
826 bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1);
827 bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1);
828 bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1);
829 bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1);
830 bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1);
831
832 /* Commands - Link policy */
834 bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1);
836
837 /* Commands - Host controller and baseband */
839 bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1);
840 bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1);
841 bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1);
842 bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1);
843 bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1);
846 bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1);
847 bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1);
848 bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1);
849 bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1);
850 bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1);
853 bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1);
854 bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1);
856 bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1);
857 bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1);
858 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1);
859 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1);
861
862 /* Commands - Informational */
864 bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1);
865 bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1);
866 bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1);
867 bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1);
868 bit_set(f, NG_HCI_OCF_READ_BDADDR - 1);
869
870 /* Commands - Status */
873 bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1);
874 bit_set(f, NG_HCI_OCF_READ_RSSI - 1);
875
876 /* Commands - Testing */
878 bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1);
879 /*Commands - LE*/
881 bit_set(f, NG_HCI_OCF_LE_SET_SCAN_ENABLE - 1);
882 bit_set(f, NG_HCI_OCF_LE_SET_SCAN_PARAMETERS - 1);
884 bit_set(f, NG_HCI_OCF_LE_READ_BUFFER_SIZE - 1);
886
887} /* ng_btsocket_hci_raw_init */
888SYSINIT(ng_btsocket_hci_raw_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
890
891/*
892 * Abort connection on socket
893 */
894
895void
897{
898} /* ng_btsocket_hci_raw_abort */
899
900void
902{
903} /* ng_btsocket_hci_raw_close */
904
905/*
906 * Create new raw HCI socket
907 */
908
909int
910ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
911{
913 int error = 0;
914
915 if (pcb != NULL)
916 return (EISCONN);
917
918 if (ng_btsocket_hci_raw_node == NULL)
919 return (EPROTONOSUPPORT);
920 if (proto != BLUETOOTH_PROTO_HCI)
921 return (EPROTONOSUPPORT);
922 if (so->so_type != SOCK_RAW)
923 return (ESOCKTNOSUPPORT);
924
925 error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE,
927 if (error != 0)
928 return (error);
929
930 pcb = malloc(sizeof(*pcb),
931 M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO);
932 if (pcb == NULL)
933 return (ENOMEM);
934
935 so->so_pcb = (caddr_t) pcb;
936 pcb->so = so;
937
938 if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
940
941 /*
942 * Set default socket filter. By default socket only accepts HCI
943 * Command_Complete and Command_Status event packets.
944 */
945
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);
948
949 mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF);
950
951 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
952 LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next);
953 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
954
955 return (0);
956} /* ng_btsocket_hci_raw_attach */
957
958/*
959 * Bind raw HCI socket
960 */
961
962int
963ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam,
964 struct thread *td)
965{
967 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
968
969 if (pcb == NULL)
970 return (EINVAL);
971 if (ng_btsocket_hci_raw_node == NULL)
972 return (EINVAL);
973
974 if (sa == NULL)
975 return (EINVAL);
976 if (sa->hci_family != AF_BLUETOOTH)
977 return (EAFNOSUPPORT);
978 if (sa->hci_len != sizeof(*sa))
979 return (EINVAL);
980 if (sa->hci_node[0] == 0)
981 return (EINVAL);
982
983 mtx_lock(&pcb->pcb_mtx);
984 bcopy(sa, &pcb->addr, sizeof(pcb->addr));
985 mtx_unlock(&pcb->pcb_mtx);
986
987 return (0);
988} /* ng_btsocket_hci_raw_bind */
989
990/*
991 * Connect raw HCI socket
992 */
993
994int
995ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam,
996 struct thread *td)
997{
999 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam;
1000
1001 if (pcb == NULL)
1002 return (EINVAL);
1003 if (ng_btsocket_hci_raw_node == NULL)
1004 return (EINVAL);
1005
1006 if (sa == NULL)
1007 return (EINVAL);
1008 if (sa->hci_family != AF_BLUETOOTH)
1009 return (EAFNOSUPPORT);
1010 if (sa->hci_len != sizeof(*sa))
1011 return (EINVAL);
1012 if (sa->hci_node[0] == 0)
1013 return (EDESTADDRREQ);
1014
1015 mtx_lock(&pcb->pcb_mtx);
1016
1017 if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) {
1018 mtx_unlock(&pcb->pcb_mtx);
1019 return (EADDRNOTAVAIL);
1020 }
1021
1022 soisconnected(so);
1023
1024 mtx_unlock(&pcb->pcb_mtx);
1025
1026 return (0);
1027} /* ng_btsocket_hci_raw_connect */
1028
1029/*
1030 * Process ioctl on socket
1031 */
1032
1033int
1034ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data,
1035 struct ifnet *ifp, struct thread *td)
1036{
1038 char path[NG_NODESIZ + 1];
1039 struct ng_mesg *msg = NULL;
1040 int error = 0;
1041
1042 if (pcb == NULL)
1043 return (EINVAL);
1044 if (ng_btsocket_hci_raw_node == NULL)
1045 return (EINVAL);
1046
1047 mtx_lock(&pcb->pcb_mtx);
1048
1049 /* Check if we have device name */
1050 if (pcb->addr.hci_node[0] == 0) {
1051 mtx_unlock(&pcb->pcb_mtx);
1052 return (EHOSTUNREACH);
1053 }
1054
1055 /* Check if we have pending ioctl() */
1056 if (pcb->token != 0) {
1057 mtx_unlock(&pcb->pcb_mtx);
1058 return (EBUSY);
1059 }
1060
1061 snprintf(path, sizeof(path), "%s:", pcb->addr.hci_node);
1062
1063 switch (cmd) {
1067
1068 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1070 &p->state, sizeof(p->state));
1071 } break;
1072
1075 error = ng_btsocket_hci_raw_send_ngmsg(path,
1076 NGM_HCI_NODE_INIT, NULL, 0);
1077 else
1078 error = EPERM;
1079 break;
1080
1084
1085 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1087 &p->debug, sizeof(p->debug));
1088 } break;
1089
1093
1095 error = ng_btsocket_hci_raw_send_ngmsg(path,
1097 sizeof(p->debug));
1098 else
1099 error = EPERM;
1100 } break;
1101
1105
1106 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1108 &p->buffer, sizeof(p->buffer));
1109 } break;
1110
1114
1115 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1117 &p->bdaddr, sizeof(p->bdaddr));
1118 } break;
1119
1123
1124 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1126 &p->features, sizeof(p->features));
1127 } break;
1128
1132
1133 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1135 &p->stat, sizeof(p->stat));
1136 } break;
1137
1140 error = ng_btsocket_hci_raw_send_ngmsg(path,
1141 NGM_HCI_NODE_RESET_STAT, NULL, 0);
1142 else
1143 error = EPERM;
1144 break;
1145
1148 error = ng_btsocket_hci_raw_send_ngmsg(path,
1150 NULL, 0);
1151 else
1152 error = EPERM;
1153 break;
1154
1160
1161 if (p->num_entries <= 0 ||
1163 p->entries == NULL) {
1164 mtx_unlock(&pcb->pcb_mtx);
1165 return (EINVAL);
1166 }
1167
1169 NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_NOWAIT);
1170 if (msg == NULL) {
1171 mtx_unlock(&pcb->pcb_mtx);
1172 return (ENOMEM);
1173 }
1175 pcb->token = msg->header.token;
1176 pcb->msg = NULL;
1177
1178 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1179 if (error != 0) {
1180 pcb->token = 0;
1181 mtx_unlock(&pcb->pcb_mtx);
1182 return (error);
1183 }
1184
1185 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1186 PZERO|PCATCH, "hcictl",
1188 pcb->token = 0;
1189
1190 if (error != 0) {
1191 mtx_unlock(&pcb->pcb_mtx);
1192 return (error);
1193 }
1194
1195 msg = pcb->msg;
1196 pcb->msg = NULL;
1197
1198 mtx_unlock(&pcb->pcb_mtx);
1199
1200 if (msg != NULL &&
1202 /* Return data back to user space */
1204 p2 = (ng_hci_node_neighbor_cache_entry_ep *)(p1 + 1);
1205
1206 p->num_entries = min(p->num_entries, p1->num_entries);
1207 if (p->num_entries > 0)
1208 error = copyout((caddr_t) p2,
1209 (caddr_t) p->entries,
1210 p->num_entries * sizeof(*p2));
1211 } else
1212 error = EINVAL;
1213
1214 NG_FREE_MSG(msg); /* checks for != NULL */
1215 return (error);
1216 } /* NOTREACHED */
1217
1219 struct ng_btsocket_hci_raw_con_list *p =
1221 ng_hci_node_con_list_ep *p1 = NULL;
1222 ng_hci_node_con_ep *p2 = NULL;
1223
1224 if (p->num_connections == 0 ||
1226 p->connections == NULL) {
1227 mtx_unlock(&pcb->pcb_mtx);
1228 return (EINVAL);
1229 }
1230
1232 0, M_NOWAIT);
1233 if (msg == NULL) {
1234 mtx_unlock(&pcb->pcb_mtx);
1235 return (ENOMEM);
1236 }
1238 pcb->token = msg->header.token;
1239 pcb->msg = NULL;
1240
1241 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0);
1242 if (error != 0) {
1243 pcb->token = 0;
1244 mtx_unlock(&pcb->pcb_mtx);
1245 return (error);
1246 }
1247
1248 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1249 PZERO|PCATCH, "hcictl",
1251 pcb->token = 0;
1252
1253 if (error != 0) {
1254 mtx_unlock(&pcb->pcb_mtx);
1255 return (error);
1256 }
1257
1258 msg = pcb->msg;
1259 pcb->msg = NULL;
1260
1261 mtx_unlock(&pcb->pcb_mtx);
1262
1263 if (msg != NULL &&
1265 /* Return data back to user space */
1266 p1 = (ng_hci_node_con_list_ep *)(msg->data);
1267 p2 = (ng_hci_node_con_ep *)(p1 + 1);
1268
1270 p1->num_connections);
1271 if (p->num_connections > 0)
1272 error = copyout((caddr_t) p2,
1273 (caddr_t) p->connections,
1274 p->num_connections * sizeof(*p2));
1275 } else
1276 error = EINVAL;
1277
1278 NG_FREE_MSG(msg); /* checks for != NULL */
1279 return (error);
1280 } /* NOTREACHED */
1281
1285 data;
1286
1287 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1289 &p->policy_mask, sizeof(p->policy_mask));
1290 } break;
1291
1295 data;
1296
1298 error = ng_btsocket_hci_raw_send_ngmsg(path,
1300 &p->policy_mask,
1301 sizeof(p->policy_mask));
1302 else
1303 error = EPERM;
1304 } break;
1305
1309
1310 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1312 &p->packet_mask, sizeof(p->packet_mask));
1313 } break;
1314
1318
1320 error = ng_btsocket_hci_raw_send_ngmsg(path,
1322 &p->packet_mask,
1323 sizeof(p->packet_mask));
1324 else
1325 error = EPERM;
1326 } break;
1327
1331
1332 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path,
1334 &p->role_switch, sizeof(p->role_switch));
1335 } break;
1336
1340
1342 error = ng_btsocket_hci_raw_send_ngmsg(path,
1344 &p->role_switch,
1345 sizeof(p->role_switch));
1346 else
1347 error = EPERM;
1348 } break;
1349
1353 struct nodeinfo *ni = nl->names;
1354
1355 if (nl->num_names == 0) {
1356 mtx_unlock(&pcb->pcb_mtx);
1357 return (EINVAL);
1358 }
1359
1361 0, M_NOWAIT);
1362 if (msg == NULL) {
1363 mtx_unlock(&pcb->pcb_mtx);
1364 return (ENOMEM);
1365 }
1367 pcb->token = msg->header.token;
1368 pcb->msg = NULL;
1369
1370 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, ".:", 0);
1371 if (error != 0) {
1372 pcb->token = 0;
1373 mtx_unlock(&pcb->pcb_mtx);
1374 return (error);
1375 }
1376
1377 error = msleep(&pcb->msg, &pcb->pcb_mtx,
1378 PZERO|PCATCH, "hcictl",
1380 pcb->token = 0;
1381
1382 if (error != 0) {
1383 mtx_unlock(&pcb->pcb_mtx);
1384 return (error);
1385 }
1386
1387 msg = pcb->msg;
1388 pcb->msg = NULL;
1389
1390 mtx_unlock(&pcb->pcb_mtx);
1391
1392 if (msg != NULL && msg->header.cmd == NGM_LISTNAMES) {
1393 /* Return data back to user space */
1394 struct namelist *nl1 = (struct namelist *) msg->data;
1395 struct nodeinfo *ni1 = &nl1->nodeinfo[0];
1396
1397 while (nl->num_names > 0 && nl1->numnames > 0) {
1398 if (strcmp(ni1->type, NG_HCI_NODE_TYPE) == 0) {
1399 error = copyout((caddr_t) ni1,
1400 (caddr_t) ni,
1401 sizeof(*ni));
1402 if (error != 0)
1403 break;
1404
1405 nl->num_names --;
1406 ni ++;
1407 }
1408
1409 nl1->numnames --;
1410 ni1 ++;
1411 }
1412
1413 nl->num_names = ni - nl->names;
1414 } else
1415 error = EINVAL;
1416
1417 NG_FREE_MSG(msg); /* checks for != NULL */
1418 return (error);
1419 } /* NOTREACHED */
1420
1421 default:
1422 error = EINVAL;
1423 break;
1424 }
1425
1426 mtx_unlock(&pcb->pcb_mtx);
1427
1428 return (error);
1429} /* ng_btsocket_hci_raw_control */
1430
1431/*
1432 * Process getsockopt/setsockopt system calls
1433 */
1434
1435int
1436ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1437{
1440 int error = 0, dir;
1441
1442 if (pcb == NULL)
1443 return (EINVAL);
1444 if (ng_btsocket_hci_raw_node == NULL)
1445 return (EINVAL);
1446
1447 if (sopt->sopt_level != SOL_HCI_RAW)
1448 return (0);
1449
1450 mtx_lock(&pcb->pcb_mtx);
1451
1452 switch (sopt->sopt_dir) {
1453 case SOPT_GET:
1454 switch (sopt->sopt_name) {
1455 case SO_HCI_RAW_FILTER:
1456 error = sooptcopyout(sopt, &pcb->filter,
1457 sizeof(pcb->filter));
1458 break;
1459
1461 dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0;
1462 error = sooptcopyout(sopt, &dir, sizeof(dir));
1463 break;
1464
1465 default:
1466 error = EINVAL;
1467 break;
1468 }
1469 break;
1470
1471 case SOPT_SET:
1472 switch (sopt->sopt_name) {
1473 case SO_HCI_RAW_FILTER:
1474 error = sooptcopyin(sopt, &filter, sizeof(filter),
1475 sizeof(filter));
1476 if (error == 0)
1477 bcopy(&filter, &pcb->filter,
1478 sizeof(pcb->filter));
1479 break;
1480
1482 error = sooptcopyin(sopt, &dir, sizeof(dir),
1483 sizeof(dir));
1484 if (error != 0)
1485 break;
1486
1487 if (dir)
1489 else
1490 pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION;
1491 break;
1492
1493 default:
1494 error = EINVAL;
1495 break;
1496 }
1497 break;
1498
1499 default:
1500 error = EINVAL;
1501 break;
1502 }
1503
1504 mtx_unlock(&pcb->pcb_mtx);
1505
1506 return (error);
1507} /* ng_btsocket_hci_raw_ctloutput */
1508
1509/*
1510 * Detach raw HCI socket
1511 */
1512
1513void
1515{
1517
1518 KASSERT(pcb != NULL, ("ng_btsocket_hci_raw_detach: pcb == NULL"));
1519
1520 if (ng_btsocket_hci_raw_node == NULL)
1521 return;
1522
1523 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx);
1524 mtx_lock(&pcb->pcb_mtx);
1525
1526 LIST_REMOVE(pcb, next);
1527
1528 mtx_unlock(&pcb->pcb_mtx);
1529 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx);
1530
1531 mtx_destroy(&pcb->pcb_mtx);
1532
1533 bzero(pcb, sizeof(*pcb));
1534 free(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW);
1535
1536 so->so_pcb = NULL;
1537} /* ng_btsocket_hci_raw_detach */
1538
1539/*
1540 * Disconnect raw HCI socket
1541 */
1542
1543int
1545{
1547
1548 if (pcb == NULL)
1549 return (EINVAL);
1550 if (ng_btsocket_hci_raw_node == NULL)
1551 return (EINVAL);
1552
1553 mtx_lock(&pcb->pcb_mtx);
1554 soisdisconnected(so);
1555 mtx_unlock(&pcb->pcb_mtx);
1556
1557 return (0);
1558} /* ng_btsocket_hci_raw_disconnect */
1559
1560/*
1561 * Get socket peer's address
1562 */
1563
1564int
1565ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1566{
1567 return (ng_btsocket_hci_raw_sockaddr(so, nam));
1568} /* ng_btsocket_hci_raw_peeraddr */
1569
1570/*
1571 * Send data
1572 */
1573
1574int
1575ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m,
1576 struct sockaddr *sa, struct mbuf *control, struct thread *td)
1577{
1579 struct mbuf *nam = NULL;
1580 int error = 0;
1581
1582 if (ng_btsocket_hci_raw_node == NULL) {
1583 error = ENETDOWN;
1584 goto drop;
1585 }
1586 if (pcb == NULL) {
1587 error = EINVAL;
1588 goto drop;
1589 }
1590 if (control != NULL) {
1591 error = EINVAL;
1592 goto drop;
1593 }
1594
1595 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) ||
1596 m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) {
1597 error = EMSGSIZE;
1598 goto drop;
1599 }
1600
1601 if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) {
1602 if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) {
1603 error = ENOBUFS;
1604 goto drop;
1605 }
1606 }
1607 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) {
1608 error = ENOTSUP;
1609 goto drop;
1610 }
1611
1612 mtx_lock(&pcb->pcb_mtx);
1613
1614 error = ng_btsocket_hci_raw_filter(pcb, m, 0);
1615 if (error != 0) {
1616 mtx_unlock(&pcb->pcb_mtx);
1617 goto drop;
1618 }
1619
1620 if (sa == NULL) {
1621 if (pcb->addr.hci_node[0] == 0) {
1622 mtx_unlock(&pcb->pcb_mtx);
1623 error = EDESTADDRREQ;
1624 goto drop;
1625 }
1626
1627 sa = (struct sockaddr *) &pcb->addr;
1628 }
1629
1630 MGET(nam, M_NOWAIT, MT_SONAME);
1631 if (nam == NULL) {
1632 mtx_unlock(&pcb->pcb_mtx);
1633 error = ENOBUFS;
1634 goto drop;
1635 }
1636
1637 nam->m_len = sizeof(struct sockaddr_hci);
1638 bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci));
1639
1640 nam->m_next = m;
1641 m = NULL;
1642
1643 mtx_unlock(&pcb->pcb_mtx);
1644
1645 return (ng_send_fn(ng_btsocket_hci_raw_node, NULL,
1647drop:
1648 NG_FREE_M(control); /* NG_FREE_M checks for != NULL */
1649 NG_FREE_M(nam);
1650 NG_FREE_M(m);
1651
1652 return (error);
1653} /* ng_btsocket_hci_raw_send */
1654
1655/*
1656 * Get socket address
1657 */
1658
1659int
1660ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1661{
1663 struct sockaddr_hci sa;
1664
1665 if (pcb == NULL)
1666 return (EINVAL);
1667 if (ng_btsocket_hci_raw_node == NULL)
1668 return (EINVAL);
1669
1670 bzero(&sa, sizeof(sa));
1671 sa.hci_len = sizeof(sa);
1672 sa.hci_family = AF_BLUETOOTH;
1673
1674 mtx_lock(&pcb->pcb_mtx);
1675 strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node));
1676 mtx_unlock(&pcb->pcb_mtx);
1677
1678 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1679
1680 return ((*nam == NULL)? ENOMEM : 0);
1681} /* ng_btsocket_hci_raw_sockaddr */
uint8_t flags
Definition: netflow.h:14
#define NGI_M(i)
Definition: netgraph.h:833
int ng_connect_t(hook_p hook)
Definition: netgraph.h:104
#define NGI_MSG(i)
Definition: netgraph.h:834
#define NG_PEER_NODE(hook)
Definition: netgraph.h:167
#define NG_FREE_M(m)
Definition: netgraph.h:946
#define NG_HOOK_NOT_VALID(hook)
Definition: netgraph.h:337
#define NG_NODE_NOT_VALID(node)
Definition: netgraph.h:611
int ng_rcvmsg_t(node_p node, item_p item, hook_p lasthook)
Definition: netgraph.h:105
#define NGQF_TYPE
Definition: netgraph.h:668
#define NGQF_DATA
Definition: netgraph.h:670
int ng_disconnect_t(hook_p hook)
Definition: netgraph.h:107
int ng_newtype(struct ng_type *tp)
Definition: ng_base.c:1272
#define NG_NODE_UNREF(node)
Definition: netgraph.h:607
#define NG_SEND_MSG_PATH(error, here, msg, path, retaddr)
Definition: netgraph.h:974
#define NG_PEER_NODE_NAME(hook)
Definition: netgraph.h:169
#define NG_SEND_DATA_ONLY(error, hook, m)
Definition: netgraph.h:932
#define NGQF_MESG
Definition: netgraph.h:669
int ng_rcvdata_t(hook_p hook, item_p item)
Definition: netgraph.h:106
int ng_shutdown_t(node_p node)
Definition: netgraph.h:101
#define NG_FREE_ITEM(item)
Definition: netgraph.h:847
int ng_make_node_common(struct ng_type *typep, node_p *nodep)
Definition: ng_base.c:642
int ng_name_node(node_p node, const char *name)
Definition: ng_base.c:852
int ng_constructor_t(node_p node)
Definition: netgraph.h:99
#define NGI_GET_M(i, m)
Definition: netgraph.h:852
int ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void *arg1, int arg2)
Definition: ng_base.c:3700
#define NG_FREE_MSG(msg)
Definition: netgraph.h:938
#define NG_ABI_VERSION
Definition: netgraph.h:77
#define NGI_GET_MSG(i, m)
Definition: netgraph.h:858
int ng_newhook_t(node_p node, hook_p hook, const char *name)
Definition: netgraph.h:102
SYSCTL_NODE(_net, OID_AUTO, bluetooth, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "Bluetooth family")
SYSCTL_UINT(_net_bluetooth_hci, OID_AUTO, max_neighbor_age, CTLFLAG_RW, &bluetooth_hci_max_neighbor_age_value, 600, "Maximal HCI neighbor cache entry age (sec)")
#define NG_BT_ITEMQ_ENQUEUE(q, i)
Definition: ng_bluetooth.h:184
#define NG_BT_ITEMQ_FULL(q)
Definition: ng_bluetooth.h:180
#define NG_BT_ITEMQ_INIT(q, _maxlen)
Definition: ng_bluetooth.h:163
#define NG_BT_ITEMQ_DEQUEUE(q, i)
Definition: ng_bluetooth.h:190
#define NG_BT_ITEMQ_DROP(q)
Definition: ng_bluetooth.h:182
#define SIOC_HCI_RAW_NODE_GET_BDADDR
Definition: ng_btsocket.h:116
#define SIOC_HCI_RAW_NODE_SET_PACKET_MASK
Definition: ng_btsocket.h:180
#define SO_HCI_RAW_FILTER
Definition: ng_btsocket.h:61
#define SIOC_HCI_RAW_NODE_RESET_STAT
Definition: ng_btsocket.h:137
#define SIOC_HCI_RAW_NODE_GET_PACKET_MASK
Definition: ng_btsocket.h:177
#define SIOC_HCI_RAW_NODE_INIT
Definition: ng_btsocket.h:90
#define NG_BTSOCKET_HCI_RAW_NODE_TYPE
Definition: ng_btsocket.h:361
#define BLUETOOTH_PROTO_HCI
Definition: ng_btsocket.h:43
#define SOL_HCI_RAW
Definition: ng_btsocket.h:59
#define SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK
Definition: ng_btsocket.h:169
#define SCM_HCI_RAW_DIRECTION
Definition: ng_btsocket.h:63
#define SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE
Definition: ng_btsocket.h:149
#define SIOC_HCI_RAW_NODE_GET_STAT
Definition: ng_btsocket.h:132
#define SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH
Definition: ng_btsocket.h:191
#define SIOC_HCI_RAW_NODE_GET_CON_LIST
Definition: ng_btsocket.h:158
#define SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH
Definition: ng_btsocket.h:188
#define SIOC_HCI_RAW_NODE_GET_DEBUG
Definition: ng_btsocket.h:97
#define SIOC_HCI_RAW_NODE_SET_DEBUG
Definition: ng_btsocket.h:100
#define SIOC_HCI_RAW_NODE_GET_BUFFER
Definition: ng_btsocket.h:108
#define NG_BTSOCKET_WARN_LEVEL
Definition: ng_btsocket.h:372
#define SO_HCI_RAW_DIRECTION
Definition: ng_btsocket.h:62
#define SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE
Definition: ng_btsocket.h:141
#define SIOC_HCI_RAW_NODE_LIST_NAMES
Definition: ng_btsocket.h:200
#define SIOC_HCI_RAW_NODE_GET_FEATURES
Definition: ng_btsocket.h:124
#define SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK
Definition: ng_btsocket.h:166
#define SIOC_HCI_RAW_NODE_GET_STATE
Definition: ng_btsocket.h:85
static LIST_HEAD(ng_btsocket_hci_raw_pcb)
int ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
static void ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p, struct mbuf **, struct mbuf *)
static ng_rcvdata_t ng_btsocket_hci_raw_node_rcvdata
static void ng_btsocket_hci_raw_output(node_p, hook_p, void *, int)
#define NG_BTSOCKET_HCI_RAW_ERR
static int ng_btsocket_hci_raw_send_ngmsg(char *path, int cmd, void *arg, int arglen)
int ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *sa, struct mbuf *control, struct thread *td)
static ng_disconnect_t ng_btsocket_hci_raw_node_disconnect
static ng_constructor_t ng_btsocket_hci_raw_node_constructor
static ng_connect_t ng_btsocket_hci_raw_node_connect
int ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt)
static void ng_btsocket_hci_raw_msg_input(struct ng_mesg *msg)
static int ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path, int cmd, void *rsp, int rsplen)
int ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
static void ng_btsocket_hci_raw_get_token(u_int32_t *token)
void ng_btsocket_hci_raw_abort(struct socket *so)
int ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
#define ng_btsocket_hci_raw_wakeup_input_task()
static void ng_btsocket_hci_raw_data_input(struct mbuf *nam)
void ng_btsocket_hci_raw_detach(struct socket *so)
#define NG_BTSOCKET_HCI_RAW_INFO
static node_p ng_btsocket_hci_raw_node
static struct mtx ng_btsocket_hci_raw_queue_mtx
static struct task ng_btsocket_hci_raw_task
int ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam)
int ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td)
static struct ng_type typestruct
static struct ng_bt_itemq ng_btsocket_hci_raw_queue
static u_int32_t ng_btsocket_hci_raw_debug_level
static ng_shutdown_t ng_btsocket_hci_raw_node_shutdown
static ng_newhook_t ng_btsocket_hci_raw_node_newhook
SYSINIT(ng_btsocket_hci_raw_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ng_btsocket_hci_raw_init, NULL)
int ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam)
#define M_NETGRAPH_BTSOCKET_HCI_RAW
static int ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p, struct mbuf *, int)
static u_int32_t ng_btsocket_hci_raw_ioctl_timeout
#define NG_BTSOCKET_HCI_RAW_ALERT
static void ng_btsocket_hci_raw_input(void *, int)
void ng_btsocket_hci_raw_close(struct socket *so)
static ng_rcvmsg_t ng_btsocket_hci_raw_node_rcvmsg
static void ng_btsocket_hci_raw_init(void *arg __unused)
int ng_btsocket_hci_raw_disconnect(struct socket *so)
#define NG_BTSOCKET_HCI_RAW_RECVSPACE
#define NG_BTSOCKET_HCI_RAW_SENDSPACE
#define NG_BTSOCKET_HCI_RAW_PRIVILEGED
#define so2hci_raw_pcb(so)
#define NG_BTSOCKET_HCI_RAW_DIRECTION
u_int8_t type
u_int8_t control
MALLOC_DEFINE(M_NG_CCATM, "ng_ccatm", "netgraph uni api node")
#define NG_HCI_OCF_LE_READ_WHITE_LIST_SIZE
Definition: ng_hci.h:1636
#define NG_HCI_OCF_EXIT_PERIODIC_INQUIRY
Definition: ng_hci.h:719
#define NG_HCI_OCF_READ_SUPPORTED_IAC_NUM
Definition: ng_hci.h:1324
#define NG_HCI_OCF_READ_PAGE_TIMO
Definition: ng_hci.h:1084
#define NG_HCI_EVENT_COMMAND_STATUS
Definition: ng_hci.h:1918
#define NG_HCI_OCF_PERIODIC_INQUIRY
Definition: ng_hci.h:708
#define NG_HCI_OCF_LE_READ_LOCAL_SUPPORTED_FEATURES
Definition: ng_hci.h:1545
#define NG_HCI_OGF_TESTING
Definition: ng_hci.h:1502
#define NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY
Definition: ng_hci.h:1236
#define NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS
Definition: ng_hci.h:1222
#define NG_HCI_OCF_READ_COUNTRY_CODE
Definition: ng_hci.h:1433
#define NG_HCI_OGF_LE
Definition: ng_hci.h:1528
#define NG_HCI_OCF_READ_AUTH_ENABLE
Definition: ng_hci.h:1144
#define NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK
Definition: ng_hci.h:674
#define NG_HCI_OCF_READ_LOCAL_NAME
Definition: ng_hci.h:1063
#define NG_HCI_CMD_PKT
Definition: ng_hci.h:408
#define NG_HCI_OCF_READ_CLOCK_OFFSET
Definition: ng_hci.h:858
#define NG_HCI_OCF_READ_AUTO_FLUSH_TIMO
Definition: ng_hci.h:1200
#define NGM_HCI_NODE_SET_DEBUG
Definition: ng_hci.h:580
#define NG_HCI_OGF(op)
Definition: ng_hci.h:382
#define NGM_HCI_NODE_GET_BUFFER
Definition: ng_hci.h:584
#define NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY
Definition: ng_hci.h:1112
#define NG_HCI_MAX_NEIGHBOR_NUM
Definition: ng_hci.h:636
#define NG_HCI_OCF_READ_BUFFER_SIZE
Definition: ng_hci.h:1424
#define NG_HCI_OCF_REMOTE_NAME_REQ
Definition: ng_hci.h:837
#define NG_HCI_OCF_READ_LINK_POLICY_SETTINGS
Definition: ng_hci.h:940
#define NGM_HCI_NODE_GET_STAT
Definition: ng_hci.h:603
#define NG_HCI_OGF_STATUS
Definition: ng_hci.h:1451
#define NGM_HCI_NODE_INIT
Definition: ng_hci.h:575
#define NG_HCI_OCF_READ_LE_HOST_SUPPORTED
Definition: ng_hci.h:1378
#define NG_HCI_EVENT_COMMAND_COMPL
Definition: ng_hci.h:1911
#define NG_HCI_OCF_LE_SET_SCAN_PARAMETERS
Definition: ng_hci.h:1600
#define NG_HCI_OCF_LE_READ_BUFFER_SIZE
Definition: ng_hci.h:1537
#define NG_HCI_OGF_LINK_POLICY
Definition: ng_hci.h:870
#define NG_HCI_OCF_READ_PAGE_SCAN_PERIOD
Definition: ng_hci.h:1350
#define NG_HCI_OCF_INQUIRY_CANCEL
Definition: ng_hci.h:704
#define NG_HCI_OCF_GET_LINK_QUALITY
Definition: ng_hci.h:1474
#define NG_HCI_OCF_READ_UNIT_CLASS
Definition: ng_hci.h:1172
#define NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY
Definition: ng_hci.h:1128
#define NG_HCI_OCF_READ_LOOPBACK_MODE
Definition: ng_hci.h:1504
#define NGM_HCI_NODE_RESET_STAT
Definition: ng_hci.h:615
#define NGM_HCI_NODE_SET_PACKET_MASK
Definition: ng_hci.h:679
#define NG_HCI_EVENT_VENDOR
Definition: ng_hci.h:2135
#define NG_HCI_ACL_DATA_PKT
Definition: ng_hci.h:417
#define NG_HCI_OCF_READ_FAILED_CONTACT_CNTR
Definition: ng_hci.h:1453
#define NG_HCI_OCF_READ_REMOTE_VER_INFO
Definition: ng_hci.h:852
#define NGM_HCI_NODE_GET_ROLE_SWITCH
Definition: ng_hci.h:682
#define NGM_HCI_NODE_GET_CON_LIST
Definition: ng_hci.h:639
#define NG_HCI_OCF_LE_SET_SCAN_ENABLE
Definition: ng_hci.h:1610
#define NGM_HCI_COOKIE
Definition: ng_hci.h:60
#define NGM_HCI_NODE_GET_NEIGHBOR_CACHE
Definition: ng_hci.h:620
#define NG_HCI_OCF_ROLE_DISCOVERY
Definition: ng_hci.h:922
#define NG_HCI_OCF_READ_IAC_LAP
Definition: ng_hci.h:1331
#define NG_HCI_CMD_PKT_SIZE
Definition: ng_hci.h:409
#define NG_HCI_OCF_READ_LOCAL_FEATURES
Definition: ng_hci.h:1418
#define NGM_HCI_NODE_SET_ROLE_SWITCH
Definition: ng_hci.h:683
#define NGM_HCI_NODE_GET_STATE
Definition: ng_hci.h:571
#define NG_HCI_OCF_READ_ENCRYPTION_MODE
Definition: ng_hci.h:1158
#define NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK
Definition: ng_hci.h:675
#define NG_HCI_OCF_READ_REMOTE_FEATURES
Definition: ng_hci.h:846
#define NGM_HCI_NODE_GET_DEBUG
Definition: ng_hci.h:579
#define NG_HCI_OCF_READ_SCAN_ENABLE
Definition: ng_hci.h:1098
#define NG_HCI_OGF_LINK_CONTROL
Definition: ng_hci.h:694
#define NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE
Definition: ng_hci.h:618
#define NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO
Definition: ng_hci.h:1302
#define NG_HCI_OCF_READ_XMIT_LEVEL
Definition: ng_hci.h:1250
#define NGM_HCI_NODE_GET_PACKET_MASK
Definition: ng_hci.h:678
#define NG_HCI_MAX_CON_NUM
Definition: ng_hci.h:657
#define NG_HCI_OCF_READ_CON_ACCEPT_TIMO
Definition: ng_hci.h:1070
#define NG_HCI_OCF_READ_PAGE_SCAN
Definition: ng_hci.h:1364
#define NGM_HCI_NODE_GET_FEATURES
Definition: ng_hci.h:600
#define NG_HCI_OCF_READ_SCO_FLOW_CONTROL
Definition: ng_hci.h:1262
#define NG_HCI_EVENT_RETURN_LINK_KEYS
Definition: ng_hci.h:1958
#define NG_HCI_NODE_TYPE
Definition: ng_hci.h:59
#define NG_HCI_OCF_READ_PIN_TYPE
Definition: ng_hci.h:1002
#define NG_HCI_SCO_DATA_PKT
Definition: ng_hci.h:426
#define NG_HCI_EVENT_LINK_KEY_NOTIFICATION
Definition: ng_hci.h:1976
#define NG_HCI_OGF_INFO
Definition: ng_hci.h:1400
#define NG_HCI_EVENT_PKT
Definition: ng_hci.h:435
#define NG_HCI_OCF_READ_VOICE_SETTINGS
Definition: ng_hci.h:1186
#define NG_HCI_OCF_READ_BDADDR
Definition: ng_hci.h:1439
#define NG_HCI_OCF(op)
Definition: ng_hci.h:381
#define NGM_HCI_NODE_GET_BDADDR
Definition: ng_hci.h:596
#define NG_HCI_OCF_READ_LOCAL_VER
Definition: ng_hci.h:1402
#define NG_HCI_OCF_INQUIRY
Definition: ng_hci.h:696
#define NG_HCI_OCF_READ_RSSI
Definition: ng_hci.h:1485
#define NG_HCI_OGF_HC_BASEBAND
Definition: ng_hci.h:968
#define min(a, b)
Definition: ng_hci_cmds.c:60
#define NG_NODESIZ
Definition: ng_message.h:50
#define NGM_GENERIC_COOKIE
Definition: ng_message.h:113
#define NGF_RESP
Definition: ng_message.h:101
#define NG_MKMESSAGE(msg, cookie, cmdid, len, how)
Definition: ng_message.h:378
@ NGM_LISTNAMES
Definition: ng_message.h:128
char *const name
Definition: ng_ppp.c:261
cmd
Definition: ng_pppoe.h:74
uint8_t data[]
Definition: ng_ubt_var.h:2
uint16_t opcode
Definition: ng_ubt_var.h:0
uint8_t event
Definition: ng_ubt_var.h:0
Definition: ng_etf.c:135
u_int32_t numnames
Definition: ng_message.h:268
struct nodeinfo nodeinfo[]
Definition: ng_message.h:269
u_int32_t maxlen
Definition: ng_bluetooth.h:157
u_int32_t drops
Definition: ng_bluetooth.h:158
u_int32_t len
Definition: ng_bluetooth.h:156
ng_hci_node_con_ep * connections
Definition: ng_btsocket.h:156
ng_hci_node_buffer_ep buffer
Definition: ng_btsocket.h:106
ng_hci_node_debug_ep debug
Definition: ng_btsocket.h:95
u_int8_t features[NG_HCI_FEATURES_SIZE]
Definition: ng_btsocket.h:122
ng_hci_node_neighbor_cache_entry_ep * entries
Definition: ng_btsocket.h:147
ng_hci_node_packet_mask_ep packet_mask
Definition: ng_btsocket.h:175
ng_hci_node_role_switch_ep role_switch
Definition: ng_btsocket.h:186
ng_hci_node_stat_ep stat
Definition: ng_btsocket.h:130
ng_hci_node_state_ep state
Definition: ng_btsocket.h:83
struct sockaddr_hci addr
struct ng_btsocket_hci_raw_filter filter
bitstr_t bit_decl(events, 0xff)
bitstr_t bit_decl(commands[0x3f], 0x3ff)
u_int32_t num_connections
Definition: ng_hci.h:641
Definition: ng_hci.h:625
u_long el_flags
Definition: netgraph.h:636
u_int32_t token
Definition: ng_message.h:65
u_int32_t typecookie
Definition: ng_message.h:66
u_int32_t flags
Definition: ng_message.h:64
struct ng_mesg::ng_msghdr header
char data[]
Definition: ng_message.h:69
u_int32_t version
Definition: netgraph.h:1077
u_char hci_len
Definition: ng_btsocket.h:53
char hci_node[32]
Definition: ng_btsocket.h:55
u_char hci_family
Definition: ng_btsocket.h:54