-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
ng_btsocket_l2cap.c
2965 lines (2390 loc) · 76.6 KB
/
ng_btsocket_l2cap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ng_btsocket_l2cap.c
*/
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: ng_btsocket_l2cap.c,v 1.16 2003/09/14 23:29:06 max Exp $
* $FreeBSD$
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bitstring.h>
#include <sys/domain.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <sys/filedesc.h>
#include <sys/ioccom.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/mutex.h>
#include <sys/protosw.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/taskqueue.h>
#include <net/vnet.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/bluetooth/include/ng_bluetooth.h>
#include <netgraph/bluetooth/include/ng_hci.h>
#include <netgraph/bluetooth/include/ng_l2cap.h>
#include <netgraph/bluetooth/include/ng_btsocket.h>
#include <netgraph/bluetooth/include/ng_btsocket_l2cap.h>
/* MALLOC define */
#ifdef NG_SEPARATE_MALLOC
static MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_L2CAP, "netgraph_btsocks_l2cap",
"Netgraph Bluetooth L2CAP sockets");
#else
#define M_NETGRAPH_BTSOCKET_L2CAP M_NETGRAPH
#endif /* NG_SEPARATE_MALLOC */
/* Netgraph node methods */
static ng_constructor_t ng_btsocket_l2cap_node_constructor;
static ng_rcvmsg_t ng_btsocket_l2cap_node_rcvmsg;
static ng_shutdown_t ng_btsocket_l2cap_node_shutdown;
static ng_newhook_t ng_btsocket_l2cap_node_newhook;
static ng_connect_t ng_btsocket_l2cap_node_connect;
static ng_rcvdata_t ng_btsocket_l2cap_node_rcvdata;
static ng_disconnect_t ng_btsocket_l2cap_node_disconnect;
static void ng_btsocket_l2cap_input (void *, int);
static void ng_btsocket_l2cap_rtclean (void *, int);
/* Netgraph type descriptor */
static struct ng_type typestruct = {
.version = NG_ABI_VERSION,
.name = NG_BTSOCKET_L2CAP_NODE_TYPE,
.constructor = ng_btsocket_l2cap_node_constructor,
.rcvmsg = ng_btsocket_l2cap_node_rcvmsg,
.shutdown = ng_btsocket_l2cap_node_shutdown,
.newhook = ng_btsocket_l2cap_node_newhook,
.connect = ng_btsocket_l2cap_node_connect,
.rcvdata = ng_btsocket_l2cap_node_rcvdata,
.disconnect = ng_btsocket_l2cap_node_disconnect,
};
/* Globals */
extern int ifqmaxlen;
static u_int32_t ng_btsocket_l2cap_debug_level;
static node_p ng_btsocket_l2cap_node;
static struct ng_bt_itemq ng_btsocket_l2cap_queue;
static struct mtx ng_btsocket_l2cap_queue_mtx;
static struct task ng_btsocket_l2cap_queue_task;
static LIST_HEAD(, ng_btsocket_l2cap_pcb) ng_btsocket_l2cap_sockets;
static struct mtx ng_btsocket_l2cap_sockets_mtx;
static LIST_HEAD(, ng_btsocket_l2cap_rtentry) ng_btsocket_l2cap_rt;
static struct mtx ng_btsocket_l2cap_rt_mtx;
static struct task ng_btsocket_l2cap_rt_task;
static struct timeval ng_btsocket_l2cap_lasttime;
static int ng_btsocket_l2cap_curpps;
/* Sysctl tree */
SYSCTL_DECL(_net_bluetooth_l2cap_sockets);
static SYSCTL_NODE(_net_bluetooth_l2cap_sockets, OID_AUTO, seq,
CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"Bluetooth SEQPACKET L2CAP sockets family");
SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, debug_level,
CTLFLAG_RW,
&ng_btsocket_l2cap_debug_level, NG_BTSOCKET_WARN_LEVEL,
"Bluetooth SEQPACKET L2CAP sockets debug level");
SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_len,
CTLFLAG_RD,
&ng_btsocket_l2cap_queue.len, 0,
"Bluetooth SEQPACKET L2CAP sockets input queue length");
SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_maxlen,
CTLFLAG_RD,
&ng_btsocket_l2cap_queue.maxlen, 0,
"Bluetooth SEQPACKET L2CAP sockets input queue max. length");
SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_drops,
CTLFLAG_RD,
&ng_btsocket_l2cap_queue.drops, 0,
"Bluetooth SEQPACKET L2CAP sockets input queue drops");
/* Debug */
#define NG_BTSOCKET_L2CAP_INFO \
if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_INFO_LEVEL && \
ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
printf
#define NG_BTSOCKET_L2CAP_WARN \
if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_WARN_LEVEL && \
ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
printf
#define NG_BTSOCKET_L2CAP_ERR \
if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_ERR_LEVEL && \
ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
printf
#define NG_BTSOCKET_L2CAP_ALERT \
if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \
ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \
printf
/*
* Netgraph message processing routines
*/
static int ng_btsocket_l2cap_process_l2ca_con_req_rsp
(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
static int ng_btsocket_l2cap_process_l2ca_con_rsp_rsp
(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
static int ng_btsocket_l2cap_process_l2ca_con_ind
(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
static int ng_btsocket_l2cap_process_l2ca_cfg_req_rsp
(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
static int ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp
(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
static int ng_btsocket_l2cap_process_l2ca_cfg_ind
(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
static int ng_btsocket_l2cap_process_l2ca_discon_rsp
(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
static int ng_btsocket_l2cap_process_l2ca_discon_ind
(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
static int ng_btsocket_l2cap_process_l2ca_write_rsp
(struct ng_mesg *, ng_btsocket_l2cap_rtentry_p);
/*
* Send L2CA_xxx messages to the lower layer
*/
static int ng_btsocket_l2cap_send_l2ca_con_req
(ng_btsocket_l2cap_pcb_p);
static int ng_btsocket_l2cap_send_l2ca_con_rsp_req
(u_int32_t, ng_btsocket_l2cap_rtentry_p, bdaddr_p, int, int, int, int);
static int ng_btsocket_l2cap_send_l2ca_cfg_req
(ng_btsocket_l2cap_pcb_p);
static int ng_btsocket_l2cap_send_l2ca_cfg_rsp
(ng_btsocket_l2cap_pcb_p);
static int ng_btsocket_l2cap_send_l2ca_discon_req
(u_int32_t, ng_btsocket_l2cap_pcb_p);
static int ng_btsocket_l2cap_send2
(ng_btsocket_l2cap_pcb_p);
/*
* Timeout processing routines
*/
static void ng_btsocket_l2cap_timeout (ng_btsocket_l2cap_pcb_p);
static void ng_btsocket_l2cap_untimeout (ng_btsocket_l2cap_pcb_p);
static void ng_btsocket_l2cap_process_timeout (void *);
/*
* Other stuff
*/
static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_addr(bdaddr_p, int);
static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_token(u_int32_t);
static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_cid (bdaddr_p, int,int);
static int ng_btsocket_l2cap_result2errno(int);
static int ng_btsock_l2cap_addrtype_to_linktype(int addrtype);
#define ng_btsocket_l2cap_wakeup_input_task() \
taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_l2cap_queue_task)
#define ng_btsocket_l2cap_wakeup_route_task() \
taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_l2cap_rt_task)
int ng_btsock_l2cap_addrtype_to_linktype(int addrtype)
{
switch(addrtype){
case BDADDR_LE_PUBLIC:
return NG_HCI_LINK_LE_PUBLIC;
case BDADDR_LE_RANDOM:
return NG_HCI_LINK_LE_RANDOM;
default:
return NG_HCI_LINK_ACL;
}
}
/*****************************************************************************
*****************************************************************************
** Netgraph node interface
*****************************************************************************
*****************************************************************************/
/*
* Netgraph node constructor. Do not allow to create node of this type.
*/
static int
ng_btsocket_l2cap_node_constructor(node_p node)
{
return (EINVAL);
} /* ng_btsocket_l2cap_node_constructor */
/*
* Do local shutdown processing. Let old node go and create new fresh one.
*/
static int
ng_btsocket_l2cap_node_shutdown(node_p node)
{
int error = 0;
NG_NODE_UNREF(node);
/* Create new node */
error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_node);
if (error != 0) {
NG_BTSOCKET_L2CAP_ALERT(
"%s: Could not create Netgraph node, error=%d\n", __func__, error);
ng_btsocket_l2cap_node = NULL;
return (error);
}
error = ng_name_node(ng_btsocket_l2cap_node,
NG_BTSOCKET_L2CAP_NODE_TYPE);
if (error != 0) {
NG_BTSOCKET_L2CAP_ALERT(
"%s: Could not name Netgraph node, error=%d\n", __func__, error);
NG_NODE_UNREF(ng_btsocket_l2cap_node);
ng_btsocket_l2cap_node = NULL;
return (error);
}
return (0);
} /* ng_btsocket_l2cap_node_shutdown */
/*
* We allow any hook to be connected to the node.
*/
static int
ng_btsocket_l2cap_node_newhook(node_p node, hook_p hook, char const *name)
{
return (0);
} /* ng_btsocket_l2cap_node_newhook */
/*
* Just say "YEP, that's OK by me!"
*/
static int
ng_btsocket_l2cap_node_connect(hook_p hook)
{
NG_HOOK_SET_PRIVATE(hook, NULL);
NG_HOOK_REF(hook); /* Keep extra reference to the hook */
#if 0
NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
NG_HOOK_FORCE_QUEUE(hook);
#endif
return (0);
} /* ng_btsocket_l2cap_node_connect */
/*
* Hook disconnection. Schedule route cleanup task
*/
static int
ng_btsocket_l2cap_node_disconnect(hook_p hook)
{
/*
* If hook has private information than we must have this hook in
* the routing table and must schedule cleaning for the routing table.
* Otherwise hook was connected but we never got "hook_info" message,
* so we have never added this hook to the routing table and it save
* to just delete it.
*/
if (NG_HOOK_PRIVATE(hook) != NULL)
return (ng_btsocket_l2cap_wakeup_route_task());
NG_HOOK_UNREF(hook); /* Remove extra reference */
return (0);
} /* ng_btsocket_l2cap_node_disconnect */
/*
* Process incoming messages
*/
static int
ng_btsocket_l2cap_node_rcvmsg(node_p node, item_p item, hook_p hook)
{
struct ng_mesg *msg = NGI_MSG(item); /* item still has message */
int error = 0;
if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) {
mtx_lock(&ng_btsocket_l2cap_queue_mtx);
if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) {
NG_BTSOCKET_L2CAP_ERR(
"%s: Input queue is full (msg)\n", __func__);
NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_queue);
NG_FREE_ITEM(item);
error = ENOBUFS;
} else {
if (hook != NULL) {
NG_HOOK_REF(hook);
NGI_SET_HOOK(item, hook);
}
NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item);
error = ng_btsocket_l2cap_wakeup_input_task();
}
mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
} else {
NG_FREE_ITEM(item);
error = EINVAL;
}
return (error);
} /* ng_btsocket_l2cap_node_rcvmsg */
/*
* Receive data on a hook
*/
static int
ng_btsocket_l2cap_node_rcvdata(hook_p hook, item_p item)
{
int error = 0;
mtx_lock(&ng_btsocket_l2cap_queue_mtx);
if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) {
NG_BTSOCKET_L2CAP_ERR(
"%s: Input queue is full (data)\n", __func__);
NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_queue);
NG_FREE_ITEM(item);
error = ENOBUFS;
} else {
NG_HOOK_REF(hook);
NGI_SET_HOOK(item, hook);
NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item);
error = ng_btsocket_l2cap_wakeup_input_task();
}
mtx_unlock(&ng_btsocket_l2cap_queue_mtx);
return (error);
} /* ng_btsocket_l2cap_node_rcvdata */
/*
* Process L2CA_Connect respose. Socket layer must have initiated connection,
* so we have to have a socket associated with message token.
*/
static int
ng_btsocket_l2cap_process_l2ca_con_req_rsp(struct ng_mesg *msg,
ng_btsocket_l2cap_rtentry_p rt)
{
ng_l2cap_l2ca_con_op *op = NULL;
ng_btsocket_l2cap_pcb_t *pcb = NULL;
int error = 0;
if (msg->header.arglen != sizeof(*op))
return (EMSGSIZE);
op = (ng_l2cap_l2ca_con_op *)(msg->data);
mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
/* Look for the socket with the token */
pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
if (pcb == NULL) {
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
mtx_lock(&pcb->pcb_mtx);
NG_BTSOCKET_L2CAP_INFO(
"%s: Got L2CA_Connect response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
"dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, status=%d, " \
"state=%d\n", __func__, msg->header.token,
pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
pcb->psm, op->lcid, op->result, op->status,
pcb->state);
if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) {
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
ng_btsocket_l2cap_untimeout(pcb);
if (op->result == NG_L2CAP_PENDING) {
ng_btsocket_l2cap_timeout(pcb);
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (0);
}
if (op->result == NG_L2CAP_SUCCESS){
if((pcb->idtype == NG_L2CAP_L2CA_IDTYPE_ATT)||
(pcb->idtype == NG_L2CAP_L2CA_IDTYPE_SMP)){
pcb->encryption = op->encryption; pcb->cid = op->lcid;
if(pcb->need_encrypt && !(pcb->encryption)){
ng_btsocket_l2cap_timeout(pcb);
pcb->state = NG_BTSOCKET_L2CAP_W4_ENC_CHANGE;
}else{
pcb->state = NG_BTSOCKET_L2CAP_OPEN;
soisconnected(pcb->so);
}
}else{
/*
* Channel is now open, so update local channel ID and
* start configuration process. Source and destination
* addresses as well as route must be already set.
*/
pcb->cid = op->lcid;
pcb->encryption = op->encryption;
error = ng_btsocket_l2cap_send_l2ca_cfg_req(pcb);
if (error != 0) {
/* Send disconnect request with "zero" token */
ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
/* ... and close the socket */
pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
soisdisconnected(pcb->so);
} else {
pcb->cfg_state = NG_BTSOCKET_L2CAP_CFG_IN_SENT;
pcb->state = NG_BTSOCKET_L2CAP_CONFIGURING;
ng_btsocket_l2cap_timeout(pcb);
}
}
} else {
/*
* We have failed to open connection, so convert result
* code to "errno" code and disconnect the socket. Channel
* already has been closed.
*/
pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
soisdisconnected(pcb->so);
}
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (error);
} /* ng_btsocket_l2cap_process_l2ca_con_req_rsp */
/*
* Process L2CA_ConnectRsp response
*/
static int
ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(struct ng_mesg *msg,
ng_btsocket_l2cap_rtentry_p rt)
{
ng_l2cap_l2ca_con_rsp_op *op = NULL;
ng_btsocket_l2cap_pcb_t *pcb = NULL;
if (msg->header.arglen != sizeof(*op))
return (EMSGSIZE);
op = (ng_l2cap_l2ca_con_rsp_op *)(msg->data);
mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
/* Look for the socket with the token */
pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
if (pcb == NULL) {
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
mtx_lock(&pcb->pcb_mtx);
NG_BTSOCKET_L2CAP_INFO(
"%s: Got L2CA_ConnectRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
"dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d\n",
__func__, msg->header.token,
pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
pcb->psm, pcb->cid, op->result, pcb->state);
if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) {
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
ng_btsocket_l2cap_untimeout(pcb);
/* Check the result and disconnect the socket on failure */
if (op->result != NG_L2CAP_SUCCESS) {
/* Close the socket - channel already closed */
pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result);
pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
soisdisconnected(pcb->so);
} else {
/* Move to CONFIGURING state and wait for CONFIG_IND */
pcb->cfg_state = 0;
pcb->state = NG_BTSOCKET_L2CAP_CONFIGURING;
ng_btsocket_l2cap_timeout(pcb);
}
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (0);
} /* ng_btsocket_process_l2ca_con_rsp_rsp */
/*
* Process L2CA_Connect indicator. Find socket that listens on address
* and PSM. Find exact or closest match. Create new socket and initiate
* connection.
*/
static int
ng_btsocket_l2cap_process_l2ca_con_ind(struct ng_mesg *msg,
ng_btsocket_l2cap_rtentry_p rt)
{
ng_l2cap_l2ca_con_ind_ip *ip = NULL;
ng_btsocket_l2cap_pcb_t *pcb = NULL, *pcb1 = NULL;
int error = 0;
u_int32_t token = 0;
u_int16_t result = 0;
if (msg->header.arglen != sizeof(*ip))
return (EMSGSIZE);
ip = (ng_l2cap_l2ca_con_ind_ip *)(msg->data);
NG_BTSOCKET_L2CAP_INFO(
"%s: Got L2CA_Connect indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
"dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, ident=%d\n",
__func__,
rt->src.b[5], rt->src.b[4], rt->src.b[3],
rt->src.b[2], rt->src.b[1], rt->src.b[0],
ip->bdaddr.b[5], ip->bdaddr.b[4], ip->bdaddr.b[3],
ip->bdaddr.b[2], ip->bdaddr.b[1], ip->bdaddr.b[0],
ip->psm, ip->lcid, ip->ident);
mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
pcb = ng_btsocket_l2cap_pcb_by_addr(&rt->src, ip->psm);
if (pcb != NULL) {
struct socket *so1;
mtx_lock(&pcb->pcb_mtx);
CURVNET_SET(pcb->so->so_vnet);
so1 = sonewconn(pcb->so, 0);
CURVNET_RESTORE();
if (so1 == NULL) {
result = NG_L2CAP_NO_RESOURCES;
goto respond;
}
/*
* If we got here than we have created new socket. So complete
* connection. If we we listening on specific address then copy
* source address from listening socket, otherwise copy source
* address from hook's routing information.
*/
pcb1 = so2l2cap_pcb(so1);
KASSERT((pcb1 != NULL),
("%s: pcb1 == NULL\n", __func__));
mtx_lock(&pcb1->pcb_mtx);
if (bcmp(&pcb->src, NG_HCI_BDADDR_ANY, sizeof(pcb->src)) != 0)
bcopy(&pcb->src, &pcb1->src, sizeof(pcb1->src));
else
bcopy(&rt->src, &pcb1->src, sizeof(pcb1->src));
pcb1->flags &= ~NG_BTSOCKET_L2CAP_CLIENT;
bcopy(&ip->bdaddr, &pcb1->dst, sizeof(pcb1->dst));
pcb1->psm = ip->psm;
pcb1->cid = ip->lcid;
pcb1->rt = rt;
/* Copy socket settings */
pcb1->imtu = pcb->imtu;
bcopy(&pcb->oflow, &pcb1->oflow, sizeof(pcb1->oflow));
pcb1->flush_timo = pcb->flush_timo;
token = pcb1->token;
} else
/* Nobody listens on requested BDADDR/PSM */
result = NG_L2CAP_PSM_NOT_SUPPORTED;
respond:
error = ng_btsocket_l2cap_send_l2ca_con_rsp_req(token, rt,
&ip->bdaddr,
ip->ident, ip->lcid,
result,ip->linktype);
if (pcb1 != NULL) {
if (error != 0) {
pcb1->so->so_error = error;
pcb1->state = NG_BTSOCKET_L2CAP_CLOSED;
soisdisconnected(pcb1->so);
} else {
pcb1->state = NG_BTSOCKET_L2CAP_CONNECTING;
soisconnecting(pcb1->so);
ng_btsocket_l2cap_timeout(pcb1);
}
mtx_unlock(&pcb1->pcb_mtx);
}
if (pcb != NULL)
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (error);
} /* ng_btsocket_l2cap_process_l2ca_con_ind */
/*Encryption Change*/
static int ng_btsocket_l2cap_process_l2ca_enc_change(struct ng_mesg *msg, ng_btsocket_l2cap_rtentry_p rt)
{
ng_l2cap_l2ca_enc_chg_op *op = NULL;
ng_btsocket_l2cap_pcb_t *pcb = NULL;
if (msg->header.arglen != sizeof(*op))
return (EMSGSIZE);
op = (ng_l2cap_l2ca_enc_chg_op *)(msg->data);
mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, op->lcid,
op->idtype);
if (pcb == NULL) {
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
mtx_lock(&pcb->pcb_mtx);
pcb->encryption = op->result;
if(pcb->need_encrypt){
ng_btsocket_l2cap_untimeout(pcb);
if(pcb->state != NG_BTSOCKET_L2CAP_W4_ENC_CHANGE){
NG_BTSOCKET_L2CAP_WARN("%s: Invalid pcb status %d",
__func__, pcb->state);
}else if(pcb->encryption){
pcb->state = NG_BTSOCKET_L2CAP_OPEN;
soisconnected(pcb->so);
}else{
pcb->so->so_error = EPERM;
ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
soisdisconnected(pcb->so);
}
}
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return 0;
}
/*
* Process L2CA_Config response
*/
static int
ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(struct ng_mesg *msg,
ng_btsocket_l2cap_rtentry_p rt)
{
ng_l2cap_l2ca_cfg_op *op = NULL;
ng_btsocket_l2cap_pcb_p pcb = NULL;
if (msg->header.arglen != sizeof(*op))
return (EMSGSIZE);
op = (ng_l2cap_l2ca_cfg_op *)(msg->data);
mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
/*
* Socket must have issued a Configure request, so we must have a
* socket that wants to be configured. Use Netgraph message token
* to find it
*/
pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
if (pcb == NULL) {
/*
* XXX FIXME what to do here? We could not find a
* socket with requested token. We even can not send
* Disconnect, because we do not know channel ID
*/
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
mtx_lock(&pcb->pcb_mtx);
NG_BTSOCKET_L2CAP_INFO(
"%s: Got L2CA_Config response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
"dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d, " \
"cfg_state=%x\n",
__func__, msg->header.token,
pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state);
if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
if (op->result == NG_L2CAP_SUCCESS) {
/*
* XXX FIXME Actually set flush and link timeout.
* Set QoS here if required. Resolve conficts (flush_timo).
* Save incoming MTU (peer's outgoing MTU) and outgoing flow
* spec.
*/
pcb->imtu = op->imtu;
bcopy(&op->oflow, &pcb->oflow, sizeof(pcb->oflow));
pcb->flush_timo = op->flush_timo;
/*
* We have configured incoming side, so record it and check
* if configuration is complete. If complete then mark socket
* as connected, otherwise wait for the peer.
*/
pcb->cfg_state &= ~NG_BTSOCKET_L2CAP_CFG_IN_SENT;
pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_IN;
if (pcb->cfg_state == NG_BTSOCKET_L2CAP_CFG_BOTH) {
/* Configuration complete - mark socket as open */
ng_btsocket_l2cap_untimeout(pcb);
pcb->state = NG_BTSOCKET_L2CAP_OPEN;
soisconnected(pcb->so);
}
} else {
/*
* Something went wrong. Could be unacceptable parameters,
* reject or unknown option. That's too bad, but we will
* not negotiate. Send Disconnect and close the channel.
*/
ng_btsocket_l2cap_untimeout(pcb);
switch (op->result) {
case NG_L2CAP_UNACCEPTABLE_PARAMS:
case NG_L2CAP_UNKNOWN_OPTION:
pcb->so->so_error = EINVAL;
break;
default:
pcb->so->so_error = ECONNRESET;
break;
}
/* Send disconnect with "zero" token */
ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
/* ... and close the socket */
pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
soisdisconnected(pcb->so);
}
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (0);
} /* ng_btsocket_l2cap_process_l2ca_cfg_req_rsp */
/*
* Process L2CA_ConfigRsp response
*/
static int
ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(struct ng_mesg *msg,
ng_btsocket_l2cap_rtentry_p rt)
{
ng_l2cap_l2ca_cfg_rsp_op *op = NULL;
ng_btsocket_l2cap_pcb_t *pcb = NULL;
int error = 0;
if (msg->header.arglen != sizeof(*op))
return (EMSGSIZE);
op = (ng_l2cap_l2ca_cfg_rsp_op *)(msg->data);
mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
/* Look for the socket with the token */
pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token);
if (pcb == NULL) {
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
mtx_lock(&pcb->pcb_mtx);
NG_BTSOCKET_L2CAP_INFO(
"%s: Got L2CA_ConfigRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \
"dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d, " \
"cfg_state=%x\n",
__func__, msg->header.token,
pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state);
if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
/* Check the result and disconnect socket of failure */
if (op->result != NG_L2CAP_SUCCESS)
goto disconnect;
/*
* Now we done with remote side configuration. Configure local
* side if we have not done it yet.
*/
pcb->cfg_state &= ~NG_BTSOCKET_L2CAP_CFG_OUT_SENT;
pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_OUT;
if (pcb->cfg_state == NG_BTSOCKET_L2CAP_CFG_BOTH) {
/* Configuration complete - mask socket as open */
ng_btsocket_l2cap_untimeout(pcb);
pcb->state = NG_BTSOCKET_L2CAP_OPEN;
soisconnected(pcb->so);
} else {
if (!(pcb->cfg_state & NG_BTSOCKET_L2CAP_CFG_IN_SENT)) {
/* Send L2CA_Config request - incoming path */
error = ng_btsocket_l2cap_send_l2ca_cfg_req(pcb);
if (error != 0)
goto disconnect;
pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_IN_SENT;
}
}
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (error);
disconnect:
ng_btsocket_l2cap_untimeout(pcb);
/* Send disconnect with "zero" token */
ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb);
/* ... and close the socket */
pcb->state = NG_BTSOCKET_L2CAP_CLOSED;
soisdisconnected(pcb->so);
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (error);
} /* ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp */
/*
* Process L2CA_Config indicator
*/
static int
ng_btsocket_l2cap_process_l2ca_cfg_ind(struct ng_mesg *msg,
ng_btsocket_l2cap_rtentry_p rt)
{
ng_l2cap_l2ca_cfg_ind_ip *ip = NULL;
ng_btsocket_l2cap_pcb_t *pcb = NULL;
int error = 0;
if (msg->header.arglen != sizeof(*ip))
return (EMSGSIZE);
ip = (ng_l2cap_l2ca_cfg_ind_ip *)(msg->data);
mtx_lock(&ng_btsocket_l2cap_sockets_mtx);
/* Check for the open socket that has given channel ID */
pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid,
NG_L2CAP_L2CA_IDTYPE_BREDR);
if (pcb == NULL) {
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
mtx_lock(&pcb->pcb_mtx);
NG_BTSOCKET_L2CAP_INFO(
"%s: Got L2CA_Config indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \
"dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, state=%d, cfg_state=%x\n",
__func__,
pcb->src.b[5], pcb->src.b[4], pcb->src.b[3],
pcb->src.b[2], pcb->src.b[1], pcb->src.b[0],
pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3],
pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0],
pcb->psm, pcb->cid, pcb->state, pcb->cfg_state);
/* XXX FIXME re-configuration on open socket */
if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) {
mtx_unlock(&pcb->pcb_mtx);
mtx_unlock(&ng_btsocket_l2cap_sockets_mtx);
return (ENOENT);
}
/*
* XXX FIXME Actually set flush and link timeout. Set QoS here if
* required. Resolve conficts (flush_timo). Note outgoing MTU (peer's
* incoming MTU) and incoming flow spec.
*/
pcb->omtu = ip->omtu;
bcopy(&ip->iflow, &pcb->iflow, sizeof(pcb->iflow));