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