]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/netgraph/bluetooth/socket/ng_btsocket_l2cap_raw.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / netgraph / bluetooth / socket / ng_btsocket_l2cap_raw.c
1 /*
2  * ng_btsocket_l2cap_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_l2cap_raw.c,v 1.12 2003/09/14 23:29:06 max Exp $
31  * $FreeBSD$
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bitstring.h>
37 #include <sys/domain.h>
38 #include <sys/errno.h>
39 #include <sys/filedesc.h>
40 #include <sys/ioccom.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/mutex.h>
46 #include <sys/priv.h>
47 #include <sys/protosw.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/taskqueue.h>
53 #include <netgraph/ng_message.h>
54 #include <netgraph/netgraph.h>
55 #include <netgraph/bluetooth/include/ng_bluetooth.h>
56 #include <netgraph/bluetooth/include/ng_hci.h>
57 #include <netgraph/bluetooth/include/ng_l2cap.h>
58 #include <netgraph/bluetooth/include/ng_btsocket.h>
59 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
60
61 /* MALLOC define */
62 #ifdef NG_SEPARATE_MALLOC
63 MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_L2CAP_RAW, "netgraph_btsocks_l2cap_raw",
64                 "Netgraph Bluetooth raw L2CAP sockets");
65 #else
66 #define M_NETGRAPH_BTSOCKET_L2CAP_RAW M_NETGRAPH
67 #endif /* NG_SEPARATE_MALLOC */
68
69 /* Netgraph node methods */
70 static ng_constructor_t ng_btsocket_l2cap_raw_node_constructor;
71 static ng_rcvmsg_t      ng_btsocket_l2cap_raw_node_rcvmsg;
72 static ng_shutdown_t    ng_btsocket_l2cap_raw_node_shutdown;
73 static ng_newhook_t     ng_btsocket_l2cap_raw_node_newhook;
74 static ng_connect_t     ng_btsocket_l2cap_raw_node_connect;
75 static ng_rcvdata_t     ng_btsocket_l2cap_raw_node_rcvdata;
76 static ng_disconnect_t  ng_btsocket_l2cap_raw_node_disconnect;
77
78 static void             ng_btsocket_l2cap_raw_input     (void *, int);
79 static void             ng_btsocket_l2cap_raw_rtclean   (void *, int);
80 static void             ng_btsocket_l2cap_raw_get_token (u_int32_t *);
81
82 static int              ng_btsocket_l2cap_raw_send_ngmsg
83                                 (hook_p, int, void *, int);
84 static int              ng_btsocket_l2cap_raw_send_sync_ngmsg
85                                 (ng_btsocket_l2cap_raw_pcb_p, int, void *, int);
86
87 #define ng_btsocket_l2cap_raw_wakeup_input_task() \
88         taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_raw_queue_task)
89
90 #define ng_btsocket_l2cap_raw_wakeup_route_task() \
91         taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_raw_rt_task)
92
93 /* Netgraph type descriptor */
94 static struct ng_type   typestruct = {
95         .version =      NG_ABI_VERSION,
96         .name =         NG_BTSOCKET_L2CAP_RAW_NODE_TYPE,
97         .constructor =  ng_btsocket_l2cap_raw_node_constructor,
98         .rcvmsg =       ng_btsocket_l2cap_raw_node_rcvmsg,
99         .shutdown =     ng_btsocket_l2cap_raw_node_shutdown,
100         .newhook =      ng_btsocket_l2cap_raw_node_newhook,
101         .connect =      ng_btsocket_l2cap_raw_node_connect,
102         .rcvdata =      ng_btsocket_l2cap_raw_node_rcvdata,
103         .disconnect =   ng_btsocket_l2cap_raw_node_disconnect,
104 };
105
106 /* Globals */
107 extern int                                      ifqmaxlen;
108 static u_int32_t                                ng_btsocket_l2cap_raw_debug_level;
109 static u_int32_t                                ng_btsocket_l2cap_raw_ioctl_timeout;
110 static node_p                                   ng_btsocket_l2cap_raw_node;
111 static struct ng_bt_itemq                       ng_btsocket_l2cap_raw_queue;
112 static struct mtx                               ng_btsocket_l2cap_raw_queue_mtx;
113 static struct task                              ng_btsocket_l2cap_raw_queue_task;
114 static LIST_HEAD(, ng_btsocket_l2cap_raw_pcb)   ng_btsocket_l2cap_raw_sockets;
115 static struct mtx                               ng_btsocket_l2cap_raw_sockets_mtx;
116 static u_int32_t                                ng_btsocket_l2cap_raw_token;
117 static struct mtx                               ng_btsocket_l2cap_raw_token_mtx;
118 static LIST_HEAD(, ng_btsocket_l2cap_rtentry)   ng_btsocket_l2cap_raw_rt;
119 static struct mtx                               ng_btsocket_l2cap_raw_rt_mtx;
120 static struct task                              ng_btsocket_l2cap_raw_rt_task;
121 static struct timeval                           ng_btsocket_l2cap_raw_lasttime;
122 static int                                      ng_btsocket_l2cap_raw_curpps;
123
124 /* Sysctl tree */
125 SYSCTL_DECL(_net_bluetooth_l2cap_sockets);
126 SYSCTL_NODE(_net_bluetooth_l2cap_sockets, OID_AUTO, raw, CTLFLAG_RW,
127         0, "Bluetooth raw L2CAP sockets family");
128 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, debug_level,
129         CTLFLAG_RW,
130         &ng_btsocket_l2cap_raw_debug_level, NG_BTSOCKET_WARN_LEVEL,
131         "Bluetooth raw L2CAP sockets debug level");
132 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, ioctl_timeout,
133         CTLFLAG_RW,
134         &ng_btsocket_l2cap_raw_ioctl_timeout, 5,
135         "Bluetooth raw L2CAP sockets ioctl timeout");
136 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_len, 
137         CTLFLAG_RD,
138         &ng_btsocket_l2cap_raw_queue.len, 0,
139         "Bluetooth raw L2CAP sockets input queue length");
140 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_maxlen, 
141         CTLFLAG_RD,
142         &ng_btsocket_l2cap_raw_queue.maxlen, 0,
143         "Bluetooth raw L2CAP sockets input queue max. length");
144 SYSCTL_INT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_drops, 
145         CTLFLAG_RD,
146         &ng_btsocket_l2cap_raw_queue.drops, 0,
147         "Bluetooth raw L2CAP sockets input queue drops");
148
149 /* Debug */
150 #define NG_BTSOCKET_L2CAP_RAW_INFO \
151         if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
152             ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
153                 printf
154
155 #define NG_BTSOCKET_L2CAP_RAW_WARN \
156         if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
157             ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
158                 printf
159
160 #define NG_BTSOCKET_L2CAP_RAW_ERR \
161         if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
162             ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
163                 printf
164
165 #define NG_BTSOCKET_L2CAP_RAW_ALERT \
166         if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
167             ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \
168                 printf
169
170 /*****************************************************************************
171  *****************************************************************************
172  **                        Netgraph node interface
173  *****************************************************************************
174  *****************************************************************************/
175
176 /*
177  * Netgraph node constructor. Do not allow to create node of this type.
178  */
179
180 static int
181 ng_btsocket_l2cap_raw_node_constructor(node_p node)
182 {
183         return (EINVAL);
184 } /* ng_btsocket_l2cap_raw_node_constructor */
185
186 /*
187  * Do local shutdown processing. Let old node go and create new fresh one.
188  */
189
190 static int
191 ng_btsocket_l2cap_raw_node_shutdown(node_p node)
192 {
193         int     error = 0;
194
195         NG_NODE_UNREF(node);
196
197         /* Create new node */
198         error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_raw_node);
199         if (error != 0) {
200                 NG_BTSOCKET_L2CAP_RAW_ALERT(
201 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
202
203                 ng_btsocket_l2cap_raw_node = NULL;
204
205                 return (error);
206         }
207
208         error = ng_name_node(ng_btsocket_l2cap_raw_node,
209                                 NG_BTSOCKET_L2CAP_RAW_NODE_TYPE);
210         if (error != 0) {
211                 NG_BTSOCKET_L2CAP_RAW_ALERT(
212 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
213
214                 NG_NODE_UNREF(ng_btsocket_l2cap_raw_node);
215                 ng_btsocket_l2cap_raw_node = NULL;
216
217                 return (error);
218         }
219                 
220         return (0);
221 } /* ng_btsocket_l2cap_raw_node_shutdown */
222
223 /*
224  * We allow any hook to be connected to the node.
225  */
226
227 static int
228 ng_btsocket_l2cap_raw_node_newhook(node_p node, hook_p hook, char const *name)
229 {
230         return (0);
231 } /* ng_btsocket_l2cap_raw_node_newhook */
232
233 /* 
234  * Just say "YEP, that's OK by me!"
235  */
236
237 static int
238 ng_btsocket_l2cap_raw_node_connect(hook_p hook)
239 {
240         NG_HOOK_SET_PRIVATE(hook, NULL);
241         NG_HOOK_REF(hook); /* Keep extra reference to the hook */
242
243         return (0);
244 } /* ng_btsocket_l2cap_raw_node_connect */
245
246 /*
247  * Hook disconnection. Schedule route cleanup task
248  */
249
250 static int
251 ng_btsocket_l2cap_raw_node_disconnect(hook_p hook)
252 {
253         /*
254          * If hook has private information than we must have this hook in
255          * the routing table and must schedule cleaning for the routing table.
256          * Otherwise hook was connected but we never got "hook_info" message,
257          * so we have never added this hook to the routing table and it save
258          * to just delete it.
259          */
260
261         if (NG_HOOK_PRIVATE(hook) != NULL)
262                 return (ng_btsocket_l2cap_raw_wakeup_route_task());
263
264         NG_HOOK_UNREF(hook); /* Remove extra reference */
265
266         return (0);
267 } /* ng_btsocket_l2cap_raw_node_disconnect */
268
269 /*
270  * Process incoming messages 
271  */
272
273 static int
274 ng_btsocket_l2cap_raw_node_rcvmsg(node_p node, item_p item, hook_p hook)
275 {
276         struct ng_mesg  *msg = NGI_MSG(item); /* item still has message */
277         int              error = 0;
278
279         if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) {
280
281                 /*
282                  * NGM_L2CAP_NODE_HOOK_INFO is special message initiated by
283                  * L2CAP layer. Ignore all other messages if they are not
284                  * replies or token is zero 
285                  */
286
287                 if (msg->header.cmd != NGM_L2CAP_NODE_HOOK_INFO) {
288                         if (msg->header.token == 0 ||
289                             !(msg->header.flags & NGF_RESP)) {
290                                 NG_FREE_ITEM(item);
291                                 return (0);
292                         }
293                 }
294
295                 mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx);
296                 if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_raw_queue)) {
297                         NG_BTSOCKET_L2CAP_RAW_ERR(
298 "%s: Input queue is full\n", __func__);
299
300                         NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_raw_queue);
301                         NG_FREE_ITEM(item);
302                         error = ENOBUFS;
303                 } else {
304                         if (hook != NULL) {
305                                 NG_HOOK_REF(hook);
306                                 NGI_SET_HOOK(item, hook);
307                         }
308
309                         NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_raw_queue, item);
310                         error = ng_btsocket_l2cap_raw_wakeup_input_task();
311                 }
312                 mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx);
313         } else {
314                 NG_FREE_ITEM(item);
315                 error = EINVAL;
316         }
317
318         return (error);
319 } /* ng_btsocket_l2cap_raw_node_rcvmsg */
320
321 /*
322  * Receive data on a hook
323  */
324
325 static int
326 ng_btsocket_l2cap_raw_node_rcvdata(hook_p hook, item_p item)
327 {
328         NG_FREE_ITEM(item);
329
330         return (EINVAL);
331 } /* ng_btsocket_l2cap_raw_node_rcvdata */
332
333 /*****************************************************************************
334  *****************************************************************************
335  **                              Socket interface
336  *****************************************************************************
337  *****************************************************************************/
338
339 /*
340  * L2CAP sockets input routine
341  */
342
343 static void
344 ng_btsocket_l2cap_raw_input(void *context, int pending)
345 {
346         item_p           item = NULL;
347         hook_p           hook = NULL;
348         struct ng_mesg  *msg = NULL;
349
350         for (;;) {
351                 mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx);
352                 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_raw_queue, item);
353                 mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx);
354
355                 if (item == NULL)
356                         break;
357
358                 KASSERT((item->el_flags & NGQF_TYPE) == NGQF_MESG,
359 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE)));
360
361                 NGI_GET_MSG(item, msg);
362                 NGI_GET_HOOK(item, hook);
363                 NG_FREE_ITEM(item);
364
365                 switch (msg->header.cmd) {
366                 case NGM_L2CAP_NODE_HOOK_INFO: {
367                         ng_btsocket_l2cap_rtentry_t     *rt = NULL;
368
369                         if (hook == NULL || NG_HOOK_NOT_VALID(hook) ||
370                             msg->header.arglen != sizeof(bdaddr_t))
371                                 break;
372
373                         if (bcmp(msg->data, NG_HCI_BDADDR_ANY,
374                                         sizeof(bdaddr_t)) == 0)
375                                 break;
376
377                         rt = (ng_btsocket_l2cap_rtentry_t *) 
378                                 NG_HOOK_PRIVATE(hook);
379                         if (rt == NULL) {
380                                 MALLOC(rt, ng_btsocket_l2cap_rtentry_p, 
381                                         sizeof(*rt),
382                                         M_NETGRAPH_BTSOCKET_L2CAP_RAW,
383                                         M_NOWAIT|M_ZERO);
384                                 if (rt == NULL)
385                                         break;
386
387                                 NG_HOOK_SET_PRIVATE(hook, rt);
388
389                                 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
390
391                                 LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_rt, 
392                                         rt, next);
393                         } else
394                                 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
395                 
396                         bcopy(msg->data, &rt->src, sizeof(rt->src));
397                         rt->hook = hook;
398
399                         NG_BTSOCKET_L2CAP_RAW_INFO(
400 "%s: Updating hook \"%s\", src bdaddr=%x:%x:%x:%x:%x:%x\n",
401                                 __func__, NG_HOOK_NAME(hook), 
402                                 rt->src.b[5], rt->src.b[4], rt->src.b[3],
403                                 rt->src.b[2], rt->src.b[1], rt->src.b[0]);
404
405                         mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
406                         } break;
407
408                 case NGM_L2CAP_NODE_GET_FLAGS:
409                 case NGM_L2CAP_NODE_GET_DEBUG:
410                 case NGM_L2CAP_NODE_GET_CON_LIST:
411                 case NGM_L2CAP_NODE_GET_CHAN_LIST:
412                 case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO:
413                 case NGM_L2CAP_L2CA_PING:
414                 case NGM_L2CAP_L2CA_GET_INFO: {
415                         ng_btsocket_l2cap_raw_pcb_p     pcb = NULL;
416
417                         mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
418
419                         LIST_FOREACH(pcb,&ng_btsocket_l2cap_raw_sockets,next) {
420                                 mtx_lock(&pcb->pcb_mtx);
421
422                                 if (pcb->token == msg->header.token) {
423                                         pcb->msg = msg;
424                                         msg = NULL;
425                                         wakeup(&pcb->msg);
426                                         mtx_unlock(&pcb->pcb_mtx);
427                                         break;
428                                 }
429
430                                 mtx_unlock(&pcb->pcb_mtx);
431                         }
432
433                         mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
434                         } break;
435
436                 default:
437                         NG_BTSOCKET_L2CAP_RAW_WARN(
438 "%s: Unknown message, cmd=%d\n", __func__, msg->header.cmd);
439                         break;
440                 }
441
442                 if (hook != NULL)
443                         NG_HOOK_UNREF(hook); /* remove extra reference */
444
445                 NG_FREE_MSG(msg); /* Checks for msg != NULL */
446         }
447 } /* ng_btsocket_l2cap_raw_input */
448
449 /*
450  * Route cleanup task. Gets scheduled when hook is disconnected. Here we 
451  * will find all sockets that use "invalid" hook and disconnect them.
452  */
453
454 static void
455 ng_btsocket_l2cap_raw_rtclean(void *context, int pending)
456 {
457         ng_btsocket_l2cap_raw_pcb_p     pcb = NULL;
458         ng_btsocket_l2cap_rtentry_p     rt = NULL;
459
460         /*
461          * First disconnect all sockets that use "invalid" hook
462          */
463
464         mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
465
466         LIST_FOREACH(pcb, &ng_btsocket_l2cap_raw_sockets, next) {
467                 mtx_lock(&pcb->pcb_mtx);
468
469                 if (pcb->rt != NULL &&
470                     pcb->rt->hook != NULL && NG_HOOK_NOT_VALID(pcb->rt->hook)) {
471                         if (pcb->so != NULL &&
472                             pcb->so->so_state & SS_ISCONNECTED)
473                                 soisdisconnected(pcb->so);
474
475                         pcb->rt = NULL;
476                 }
477
478                 mtx_unlock(&pcb->pcb_mtx);
479         }
480
481         mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
482
483         /*
484          * Now cleanup routing table
485          */
486
487         mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
488
489         for (rt = LIST_FIRST(&ng_btsocket_l2cap_raw_rt); rt != NULL; ) {
490                 ng_btsocket_l2cap_rtentry_p     rt_next = LIST_NEXT(rt, next);
491
492                 if (rt->hook != NULL && NG_HOOK_NOT_VALID(rt->hook)) {
493                         LIST_REMOVE(rt, next);
494
495                         NG_HOOK_SET_PRIVATE(rt->hook, NULL);
496                         NG_HOOK_UNREF(rt->hook); /* Remove extra reference */
497
498                         bzero(rt, sizeof(*rt));
499                         FREE(rt, M_NETGRAPH_BTSOCKET_L2CAP_RAW);
500                 }
501
502                 rt = rt_next;
503         }
504
505         mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
506 } /* ng_btsocket_l2cap_raw_rtclean */
507
508 /*
509  * Initialize everything
510  */
511
512 void
513 ng_btsocket_l2cap_raw_init(void)
514 {
515         int     error = 0;
516
517         ng_btsocket_l2cap_raw_node = NULL;
518         ng_btsocket_l2cap_raw_debug_level = NG_BTSOCKET_WARN_LEVEL;
519         ng_btsocket_l2cap_raw_ioctl_timeout = 5;
520
521         /* Register Netgraph node type */
522         error = ng_newtype(&typestruct);
523         if (error != 0) {
524                 NG_BTSOCKET_L2CAP_RAW_ALERT(
525 "%s: Could not register Netgraph node type, error=%d\n", __func__, error);
526
527                 return;
528         }
529
530         /* Create Netgrapg node */
531         error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_raw_node);
532         if (error != 0) {
533                 NG_BTSOCKET_L2CAP_RAW_ALERT(
534 "%s: Could not create Netgraph node, error=%d\n", __func__, error);
535
536                 ng_btsocket_l2cap_raw_node = NULL;
537
538                 return;
539         }
540
541         error = ng_name_node(ng_btsocket_l2cap_raw_node,
542                                 NG_BTSOCKET_L2CAP_RAW_NODE_TYPE);
543         if (error != 0) {
544                 NG_BTSOCKET_L2CAP_RAW_ALERT(
545 "%s: Could not name Netgraph node, error=%d\n", __func__, error);
546
547                 NG_NODE_UNREF(ng_btsocket_l2cap_raw_node);
548                 ng_btsocket_l2cap_raw_node = NULL;
549
550                 return;
551         }
552
553         /* Create input queue */
554         NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_raw_queue, ifqmaxlen);
555         mtx_init(&ng_btsocket_l2cap_raw_queue_mtx,
556                 "btsocks_l2cap_raw_queue_mtx", NULL, MTX_DEF);
557         TASK_INIT(&ng_btsocket_l2cap_raw_queue_task, 0,
558                 ng_btsocket_l2cap_raw_input, NULL);
559
560         /* Create list of sockets */
561         LIST_INIT(&ng_btsocket_l2cap_raw_sockets);
562         mtx_init(&ng_btsocket_l2cap_raw_sockets_mtx,
563                 "btsocks_l2cap_raw_sockets_mtx", NULL, MTX_DEF);
564
565         /* Tokens */
566         ng_btsocket_l2cap_raw_token = 0;
567         mtx_init(&ng_btsocket_l2cap_raw_token_mtx,
568                 "btsocks_l2cap_raw_token_mtx", NULL, MTX_DEF);
569
570         /* Routing table */
571         LIST_INIT(&ng_btsocket_l2cap_raw_rt);
572         mtx_init(&ng_btsocket_l2cap_raw_rt_mtx,
573                 "btsocks_l2cap_raw_rt_mtx", NULL, MTX_DEF);
574         TASK_INIT(&ng_btsocket_l2cap_raw_rt_task, 0,
575                 ng_btsocket_l2cap_raw_rtclean, NULL);
576 } /* ng_btsocket_l2cap_raw_init */
577
578 /*
579  * Abort connection on socket
580  */
581
582 void
583 ng_btsocket_l2cap_raw_abort(struct socket *so)
584 {
585
586         (void)ng_btsocket_l2cap_raw_disconnect(so);
587 } /* ng_btsocket_l2cap_raw_abort */
588
589 void
590 ng_btsocket_l2cap_raw_close(struct socket *so)
591 {
592
593         (void)ng_btsocket_l2cap_raw_disconnect(so);
594 } /* ng_btsocket_l2cap_raw_close */
595
596 /*
597  * Create and attach new socket
598  */
599
600 int
601 ng_btsocket_l2cap_raw_attach(struct socket *so, int proto, struct thread *td)
602 {
603         ng_btsocket_l2cap_raw_pcb_p     pcb = so2l2cap_raw_pcb(so);
604         int                             error;
605
606         if (pcb != NULL)
607                 return (EISCONN);
608
609         if (ng_btsocket_l2cap_raw_node == NULL) 
610                 return (EPROTONOSUPPORT);
611         if (so->so_type != SOCK_RAW)
612                 return (ESOCKTNOSUPPORT);
613
614         /* Reserve send and receive space if it is not reserved yet */
615         error = soreserve(so, NG_BTSOCKET_L2CAP_RAW_SENDSPACE,
616                         NG_BTSOCKET_L2CAP_RAW_RECVSPACE);
617         if (error != 0)
618                 return (error);
619
620         /* Allocate the PCB */
621         MALLOC(pcb, ng_btsocket_l2cap_raw_pcb_p, sizeof(*pcb),
622                 M_NETGRAPH_BTSOCKET_L2CAP_RAW, M_NOWAIT|M_ZERO);
623         if (pcb == NULL)
624                 return (ENOMEM);
625
626         /* Link the PCB and the socket */
627         so->so_pcb = (caddr_t) pcb;
628         pcb->so = so;
629
630         if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0)
631                 pcb->flags |= NG_BTSOCKET_L2CAP_RAW_PRIVILEGED;
632
633         mtx_init(&pcb->pcb_mtx, "btsocks_l2cap_raw_pcb_mtx", NULL, MTX_DEF);
634
635         /* Add the PCB to the list */
636         mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
637         LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_sockets, pcb, next);
638         mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
639
640         return (0);
641 } /* ng_btsocket_l2cap_raw_attach */
642
643 /*
644  * Bind socket
645  */
646
647 int
648 ng_btsocket_l2cap_raw_bind(struct socket *so, struct sockaddr *nam, 
649                 struct thread *td)
650 {
651         ng_btsocket_l2cap_raw_pcb_t     *pcb = so2l2cap_raw_pcb(so);
652         struct sockaddr_l2cap           *sa = (struct sockaddr_l2cap *) nam;
653         ng_btsocket_l2cap_rtentry_t     *rt = NULL;
654
655         if (pcb == NULL)
656                 return (EINVAL);
657         if (ng_btsocket_l2cap_raw_node == NULL) 
658                 return (EINVAL);
659
660         if (sa == NULL)
661                 return (EINVAL);
662         if (sa->l2cap_family != AF_BLUETOOTH)
663                 return (EAFNOSUPPORT);
664         if (sa->l2cap_len != sizeof(*sa))
665                 return (EINVAL);
666
667         if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY,
668                                 sizeof(sa->l2cap_bdaddr)) != 0) {
669                 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
670
671                 LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) {
672                         if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
673                                 continue;
674
675                         if (bcmp(&sa->l2cap_bdaddr, &rt->src,
676                                         sizeof(rt->src)) == 0)
677                                 break;
678                 }
679
680                 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
681
682                 if (rt == NULL)
683                         return (ENETDOWN);
684         } else
685                 rt = NULL;
686
687         mtx_lock(&pcb->pcb_mtx);
688         bcopy(&sa->l2cap_bdaddr, &pcb->src, sizeof(pcb->src));
689         pcb->rt = rt;
690         mtx_unlock(&pcb->pcb_mtx);
691
692         return (0);
693 } /* ng_btsocket_l2cap_raw_bind */
694
695 /*
696  * Connect socket
697  */
698
699 int
700 ng_btsocket_l2cap_raw_connect(struct socket *so, struct sockaddr *nam, 
701                 struct thread *td)
702 {
703         ng_btsocket_l2cap_raw_pcb_t     *pcb = so2l2cap_raw_pcb(so);
704         struct sockaddr_l2cap           *sa = (struct sockaddr_l2cap *) nam;
705         ng_btsocket_l2cap_rtentry_t     *rt = NULL;
706         int                              error;
707
708         if (pcb == NULL)
709                 return (EINVAL);
710         if (ng_btsocket_l2cap_raw_node == NULL) 
711                 return (EINVAL);
712
713         if (sa == NULL)
714                 return (EINVAL);
715         if (sa->l2cap_family != AF_BLUETOOTH)
716                 return (EAFNOSUPPORT);
717         if (sa->l2cap_len != sizeof(*sa))
718                 return (EINVAL);
719         if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0)
720                 return (EINVAL);
721
722         mtx_lock(&pcb->pcb_mtx);
723
724         bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst));
725
726         if (bcmp(&pcb->src, &pcb->dst, sizeof(pcb->src)) == 0) {
727                 mtx_unlock(&pcb->pcb_mtx);
728
729                 return (EADDRNOTAVAIL);
730         }
731
732         /*
733          * If there is route already - use it
734          */
735
736         if (pcb->rt != NULL) {
737                 soisconnected(so);
738                 mtx_unlock(&pcb->pcb_mtx);
739
740                 return (0);
741         }
742         
743         /*
744          * Find the first hook that does not match specified destination address
745          */
746
747         mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx);
748
749         LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) {
750                 if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook))
751                         continue;
752
753                 if (bcmp(&pcb->dst, &rt->src, sizeof(rt->src)) != 0)
754                         break;
755         }
756
757         if (rt != NULL) {
758                 soisconnected(so);
759
760                 pcb->rt = rt;
761                 bcopy(&rt->src, &pcb->src, sizeof(pcb->src));
762
763                 error = 0;
764         } else
765                 error = ENETDOWN;
766
767         mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx);
768         mtx_unlock(&pcb->pcb_mtx);
769
770         return  (error);
771 } /* ng_btsocket_l2cap_raw_connect */
772
773 /*
774  * Process ioctl's calls on socket
775  */
776
777 int
778 ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data,
779                 struct ifnet *ifp, struct thread *td)
780 {
781         ng_btsocket_l2cap_raw_pcb_p      pcb = so2l2cap_raw_pcb(so);
782         struct ng_mesg                  *msg = NULL;
783         int                              error = 0;
784
785         if (pcb == NULL)
786                 return (EINVAL);
787         if (ng_btsocket_l2cap_raw_node == NULL)
788                 return (EINVAL);
789
790         mtx_lock(&pcb->pcb_mtx);
791
792         /* Check if we route info */
793         if (pcb->rt == NULL) {
794                 mtx_unlock(&pcb->pcb_mtx);
795                 return (EHOSTUNREACH);
796         }
797
798         /* Check if we have pending ioctl() */
799         if (pcb->token != 0) {
800                 mtx_unlock(&pcb->pcb_mtx);
801                 return (EBUSY);
802         }  
803
804         switch (cmd) {
805         case SIOC_L2CAP_NODE_GET_FLAGS: {
806                 struct ng_btsocket_l2cap_raw_node_flags *p =
807                         (struct ng_btsocket_l2cap_raw_node_flags *) data;
808
809                 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
810                                 NGM_L2CAP_NODE_GET_FLAGS,
811                                 &p->flags, sizeof(p->flags));
812                 } break;
813
814         case SIOC_L2CAP_NODE_GET_DEBUG: {
815                 struct ng_btsocket_l2cap_raw_node_debug *p =
816                         (struct ng_btsocket_l2cap_raw_node_debug *) data;
817
818                 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
819                                 NGM_L2CAP_NODE_GET_DEBUG,
820                                 &p->debug, sizeof(p->debug));
821                 } break;
822
823         case SIOC_L2CAP_NODE_SET_DEBUG: {
824                 struct ng_btsocket_l2cap_raw_node_debug *p = 
825                         (struct ng_btsocket_l2cap_raw_node_debug *) data;
826
827                 if (pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)
828                         error = ng_btsocket_l2cap_raw_send_ngmsg(pcb->rt->hook,
829                                         NGM_L2CAP_NODE_SET_DEBUG,
830                                         &p->debug, sizeof(p->debug));
831                 else
832                         error = EPERM;
833                 } break;
834
835         case SIOC_L2CAP_NODE_GET_CON_LIST: {
836                 struct ng_btsocket_l2cap_raw_con_list   *p =
837                         (struct ng_btsocket_l2cap_raw_con_list *) data;
838                 ng_l2cap_node_con_list_ep               *p1 = NULL;
839                 ng_l2cap_node_con_ep                    *p2 = NULL;
840  
841                 if (p->num_connections == 0 ||
842                     p->num_connections > NG_L2CAP_MAX_CON_NUM ||
843                     p->connections == NULL) {
844                         error = EINVAL;
845                         break;
846                 }
847
848                 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_NODE_GET_CON_LIST,
849                         0, M_NOWAIT);
850                 if (msg == NULL) {
851                         error = ENOMEM;
852                         break;
853                 }
854                 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
855                 pcb->token = msg->header.token;
856                 pcb->msg = NULL;
857
858                 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
859                         pcb->rt->hook, 0);
860                 if (error != 0) {
861                         pcb->token = 0;
862                         break;
863                 }
864
865                 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
866                                 ng_btsocket_l2cap_raw_ioctl_timeout * hz);
867                 pcb->token = 0;
868
869                 if (error != 0)
870                         break;
871
872                 if (pcb->msg != NULL &&
873                     pcb->msg->header.cmd == NGM_L2CAP_NODE_GET_CON_LIST) {
874                         /* Return data back to user space */
875                         p1 = (ng_l2cap_node_con_list_ep *)(pcb->msg->data);
876                         p2 = (ng_l2cap_node_con_ep *)(p1 + 1);
877
878                         p->num_connections = min(p->num_connections,
879                                                 p1->num_connections);
880                         if (p->num_connections > 0)
881                                 error = copyout((caddr_t) p2, 
882                                         (caddr_t) p->connections,
883                                         p->num_connections * sizeof(*p2));
884                 } else
885                         error = EINVAL;
886
887                 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
888                 } break;
889
890         case SIOC_L2CAP_NODE_GET_CHAN_LIST: {
891                 struct ng_btsocket_l2cap_raw_chan_list  *p =
892                         (struct ng_btsocket_l2cap_raw_chan_list *) data;
893                 ng_l2cap_node_chan_list_ep              *p1 = NULL;
894                 ng_l2cap_node_chan_ep                   *p2 = NULL;
895  
896                 if (p->num_channels == 0 ||
897                     p->num_channels > NG_L2CAP_MAX_CHAN_NUM ||
898                     p->channels == NULL) {
899                         error = EINVAL;
900                         break;
901                 }
902  
903                 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
904                         NGM_L2CAP_NODE_GET_CHAN_LIST, 0, M_NOWAIT);
905                 if (msg == NULL) {
906                         error = ENOMEM;
907                         break;
908                 }
909                 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
910                 pcb->token = msg->header.token;
911                 pcb->msg = NULL;
912
913                 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
914                         pcb->rt->hook, 0);
915                 if (error != 0) {
916                         pcb->token = 0;
917                         break;
918                 }
919
920                 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
921                                 ng_btsocket_l2cap_raw_ioctl_timeout * hz);
922                 pcb->token = 0;
923
924                 if (error != 0)
925                         break;
926
927                 if (pcb->msg != NULL &&
928                     pcb->msg->header.cmd == NGM_L2CAP_NODE_GET_CHAN_LIST) {
929                         /* Return data back to user space */
930                         p1 = (ng_l2cap_node_chan_list_ep *)(pcb->msg->data);
931                         p2 = (ng_l2cap_node_chan_ep *)(p1 + 1);
932
933                         p->num_channels = min(p->num_channels, 
934                                                 p1->num_channels);
935                         if (p->num_channels > 0)
936                                 error = copyout((caddr_t) p2, 
937                                                 (caddr_t) p->channels,
938                                                 p->num_channels * sizeof(*p2));
939                 } else
940                         error = EINVAL;
941
942                 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
943                 } break;
944
945         case SIOC_L2CAP_L2CA_PING: {
946                 struct ng_btsocket_l2cap_raw_ping       *p = 
947                         (struct ng_btsocket_l2cap_raw_ping *) data;
948                 ng_l2cap_l2ca_ping_ip                   *ip = NULL;
949                 ng_l2cap_l2ca_ping_op                   *op = NULL;
950
951                 if ((p->echo_size != 0 && p->echo_data == NULL) ||
952                      p->echo_size > NG_L2CAP_MAX_ECHO_SIZE) {
953                         error = EINVAL;
954                         break;
955                 }
956
957                 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
958                         NGM_L2CAP_L2CA_PING, sizeof(*ip) + p->echo_size,
959                         M_NOWAIT);
960                 if (msg == NULL) {
961                         error = ENOMEM;
962                         break;
963                 }
964                 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
965                 pcb->token = msg->header.token;
966                 pcb->msg = NULL;
967
968                 ip = (ng_l2cap_l2ca_ping_ip *)(msg->data);
969                 bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
970                 ip->echo_size = p->echo_size;
971
972                 if (ip->echo_size > 0) {
973                         error = copyin(p->echo_data, ip + 1, p->echo_size);
974                         if (error != 0) {
975                                 NG_FREE_MSG(msg);
976                                 pcb->token = 0;
977                                 break;
978                         }
979                 }
980
981                 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
982                         pcb->rt->hook, 0);
983                 if (error != 0) {
984                         pcb->token = 0;
985                         break;
986                 }
987
988                 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
989                                 bluetooth_l2cap_rtx_timeout());
990                 pcb->token = 0;
991
992                 if (error != 0)
993                         break;
994
995                 if (pcb->msg != NULL &&
996                     pcb->msg->header.cmd == NGM_L2CAP_L2CA_PING) {
997                         /* Return data back to the user space */
998                         op = (ng_l2cap_l2ca_ping_op *)(pcb->msg->data);
999                         p->result = op->result;
1000                         p->echo_size = min(p->echo_size, op->echo_size);
1001
1002                         if (p->echo_size > 0)
1003                                 error = copyout(op + 1, p->echo_data, 
1004                                                 p->echo_size);
1005                 } else
1006                         error = EINVAL;
1007
1008                 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1009                 } break;
1010
1011         case SIOC_L2CAP_L2CA_GET_INFO: {
1012                 struct ng_btsocket_l2cap_raw_get_info   *p = 
1013                         (struct ng_btsocket_l2cap_raw_get_info *) data;
1014                 ng_l2cap_l2ca_get_info_ip               *ip = NULL;
1015                 ng_l2cap_l2ca_get_info_op               *op = NULL;
1016
1017                 if (!(pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)) {
1018                         error = EPERM;
1019                         break;
1020                 }
1021
1022                 if (p->info_size != 0 && p->info_data == NULL) {
1023                         error = EINVAL;
1024                         break;
1025                 }
1026
1027                 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE,
1028                         NGM_L2CAP_L2CA_GET_INFO, sizeof(*ip) + p->info_size,
1029                         M_NOWAIT);
1030                 if (msg == NULL) {
1031                         error = ENOMEM;
1032                         break;
1033                 }
1034                 ng_btsocket_l2cap_raw_get_token(&msg->header.token);
1035                 pcb->token = msg->header.token;
1036                 pcb->msg = NULL;
1037
1038                 ip = (ng_l2cap_l2ca_get_info_ip *)(msg->data);
1039                 bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr));
1040                 ip->info_type = p->info_type;
1041
1042                 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
1043                         pcb->rt->hook, 0);
1044                 if (error != 0) {
1045                         pcb->token = 0;
1046                         break;
1047                 }
1048
1049                 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
1050                                 bluetooth_l2cap_rtx_timeout());
1051                 pcb->token = 0;
1052
1053                 if (error != 0)
1054                         break;
1055
1056                 if (pcb->msg != NULL &&
1057                     pcb->msg->header.cmd == NGM_L2CAP_L2CA_GET_INFO) {
1058                         /* Return data back to the user space */
1059                         op = (ng_l2cap_l2ca_get_info_op *)(pcb->msg->data);
1060                         p->result = op->result;
1061                         p->info_size = min(p->info_size, op->info_size);
1062
1063                         if (p->info_size > 0)
1064                                 error = copyout(op + 1, p->info_data, 
1065                                                 p->info_size);
1066                 } else
1067                         error = EINVAL;
1068
1069                 NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1070                 } break;
1071
1072         case SIOC_L2CAP_NODE_GET_AUTO_DISCON_TIMO: {
1073                 struct ng_btsocket_l2cap_raw_auto_discon_timo   *p =
1074                         (struct ng_btsocket_l2cap_raw_auto_discon_timo *) data;
1075
1076                 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb,
1077                                 NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO,
1078                                 &p->timeout, sizeof(p->timeout));
1079                 } break;
1080
1081         case SIOC_L2CAP_NODE_SET_AUTO_DISCON_TIMO: {
1082                 struct ng_btsocket_l2cap_raw_auto_discon_timo   *p =
1083                         (struct ng_btsocket_l2cap_raw_auto_discon_timo *) data;
1084
1085                 if (pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)
1086                         error = ng_btsocket_l2cap_raw_send_ngmsg(pcb->rt->hook,
1087                                         NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO,
1088                                         &p->timeout, sizeof(p->timeout));
1089                 else
1090                         error = EPERM;
1091                 } break;
1092
1093         default:
1094                 error = EINVAL;
1095                 break;
1096         }
1097
1098         mtx_unlock(&pcb->pcb_mtx);
1099
1100         return (error);
1101 } /* ng_btsocket_l2cap_raw_control */
1102
1103 /*
1104  * Detach and destroy socket
1105  */
1106
1107 void
1108 ng_btsocket_l2cap_raw_detach(struct socket *so)
1109 {
1110         ng_btsocket_l2cap_raw_pcb_p     pcb = so2l2cap_raw_pcb(so);
1111
1112         KASSERT(pcb != NULL, ("nt_btsocket_l2cap_raw_detach: pcb == NULL"));
1113         if (ng_btsocket_l2cap_raw_node == NULL) 
1114                 return;
1115
1116         mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx);
1117         mtx_lock(&pcb->pcb_mtx);
1118
1119         LIST_REMOVE(pcb, next);
1120
1121         mtx_unlock(&pcb->pcb_mtx);
1122         mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx);
1123
1124         mtx_destroy(&pcb->pcb_mtx);
1125
1126         bzero(pcb, sizeof(*pcb));
1127         FREE(pcb, M_NETGRAPH_BTSOCKET_L2CAP_RAW);
1128
1129         so->so_pcb = NULL;
1130 } /* ng_btsocket_l2cap_raw_detach */
1131
1132 /*
1133  * Disconnect socket
1134  */
1135
1136 int
1137 ng_btsocket_l2cap_raw_disconnect(struct socket *so)
1138 {
1139         ng_btsocket_l2cap_raw_pcb_p     pcb = so2l2cap_raw_pcb(so);
1140
1141         if (pcb == NULL)
1142                 return (EINVAL);
1143         if (ng_btsocket_l2cap_raw_node == NULL)
1144                 return (EINVAL);
1145
1146         mtx_lock(&pcb->pcb_mtx);
1147         pcb->rt = NULL;
1148         soisdisconnected(so);
1149         mtx_unlock(&pcb->pcb_mtx);
1150
1151         return (0);
1152 } /* ng_btsocket_l2cap_raw_disconnect */
1153
1154 /*
1155  * Get peer address
1156  */
1157
1158 int
1159 ng_btsocket_l2cap_raw_peeraddr(struct socket *so, struct sockaddr **nam)
1160 {
1161         ng_btsocket_l2cap_raw_pcb_p     pcb = so2l2cap_raw_pcb(so);
1162         struct sockaddr_l2cap           sa;
1163
1164         if (pcb == NULL)
1165                 return (EINVAL);
1166         if (ng_btsocket_l2cap_raw_node == NULL) 
1167                 return (EINVAL);
1168
1169         mtx_lock(&pcb->pcb_mtx);
1170         bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
1171         mtx_unlock(&pcb->pcb_mtx);
1172
1173         sa.l2cap_psm = 0;
1174         sa.l2cap_len = sizeof(sa);
1175         sa.l2cap_family = AF_BLUETOOTH;
1176
1177         *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1178
1179         return ((*nam == NULL)? ENOMEM : 0);
1180 } /* ng_btsocket_l2cap_raw_peeraddr */
1181
1182 /*
1183  * Send data to socket
1184  */
1185
1186 int
1187 ng_btsocket_l2cap_raw_send(struct socket *so, int flags, struct mbuf *m,
1188                 struct sockaddr *nam, struct mbuf *control, struct thread *td)
1189 {
1190         NG_FREE_M(m); /* Checks for m != NULL */
1191         NG_FREE_M(control);
1192
1193         return (EOPNOTSUPP);
1194 } /* ng_btsocket_l2cap_raw_send */
1195
1196 /*
1197  * Get socket address
1198  */
1199
1200 int
1201 ng_btsocket_l2cap_raw_sockaddr(struct socket *so, struct sockaddr **nam)
1202 {
1203         ng_btsocket_l2cap_raw_pcb_p     pcb = so2l2cap_raw_pcb(so);
1204         struct sockaddr_l2cap           sa;
1205
1206         if (pcb == NULL)
1207                 return (EINVAL);
1208         if (ng_btsocket_l2cap_raw_node == NULL) 
1209                 return (EINVAL);
1210
1211         mtx_lock(&pcb->pcb_mtx);
1212         bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr));
1213         mtx_unlock(&pcb->pcb_mtx);
1214
1215         sa.l2cap_psm = 0;
1216         sa.l2cap_len = sizeof(sa);
1217         sa.l2cap_family = AF_BLUETOOTH;
1218
1219         *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT);
1220
1221         return ((*nam == NULL)? ENOMEM : 0);
1222 } /* ng_btsocket_l2cap_raw_sockaddr */
1223
1224 /*
1225  * Get next token
1226  */
1227
1228 static void
1229 ng_btsocket_l2cap_raw_get_token(u_int32_t *token)
1230 {
1231         mtx_lock(&ng_btsocket_l2cap_raw_token_mtx);
1232   
1233         if (++ ng_btsocket_l2cap_raw_token == 0)
1234                 ng_btsocket_l2cap_raw_token = 1;
1235  
1236         *token = ng_btsocket_l2cap_raw_token;
1237  
1238         mtx_unlock(&ng_btsocket_l2cap_raw_token_mtx);
1239 } /* ng_btsocket_l2cap_raw_get_token */
1240
1241 /*
1242  * Send Netgraph message to the node - do not expect reply
1243  */
1244
1245 static int
1246 ng_btsocket_l2cap_raw_send_ngmsg(hook_p hook, int cmd, void *arg, int arglen)
1247 {
1248         struct ng_mesg  *msg = NULL;
1249         int              error = 0;
1250
1251         NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, arglen, M_NOWAIT);
1252         if (msg == NULL)
1253                 return (ENOMEM);
1254
1255         if (arg != NULL && arglen > 0)
1256                 bcopy(arg, msg->data, arglen);
1257
1258         NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg, hook, 0);
1259
1260         return (error);
1261 } /* ng_btsocket_l2cap_raw_send_ngmsg */
1262
1263 /*
1264  * Send Netgraph message to the node (no data) and wait for reply
1265  */
1266
1267 static int
1268 ng_btsocket_l2cap_raw_send_sync_ngmsg(ng_btsocket_l2cap_raw_pcb_p pcb,
1269                 int cmd, void *rsp, int rsplen)
1270 {
1271         struct ng_mesg  *msg = NULL;
1272         int              error = 0;
1273
1274         mtx_assert(&pcb->pcb_mtx, MA_OWNED);
1275
1276         NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, 0, M_NOWAIT);
1277         if (msg == NULL)
1278                 return (ENOMEM);
1279
1280         ng_btsocket_l2cap_raw_get_token(&msg->header.token);
1281         pcb->token = msg->header.token;
1282         pcb->msg = NULL;
1283
1284         NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg,
1285                 pcb->rt->hook, 0);
1286         if (error != 0) {
1287                 pcb->token = 0;
1288                 return (error);
1289         }
1290
1291         error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl",
1292                         ng_btsocket_l2cap_raw_ioctl_timeout * hz);
1293         pcb->token = 0;
1294
1295         if (error != 0)
1296                 return (error);
1297
1298         if (pcb->msg != NULL && pcb->msg->header.cmd == cmd)
1299                 bcopy(pcb->msg->data, rsp, rsplen);
1300         else
1301                 error = EINVAL;
1302
1303         NG_FREE_MSG(pcb->msg); /* checks for != NULL */
1304
1305         return (0);
1306 } /* ng_btsocket_l2cap_raw_send_sync_ngmsg */
1307