-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathatt.c
1532 lines (1225 loc) · 33.1 KB
/
att.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
/**
* @file att.c
* @brief att protocol implementation
* @author Gilbert Brault
* @copyright Gilbert Brault 2015
* the original work comes from bluez v5.39
* value add: documenting main features
*
* @see gatt-helpers.c for pdu creation
*/
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2014 Google Inc.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "io.h"
#include "queue.h"
#include "util.h"
#include "timeout.h"
#include "bluetooth.h"
#include "uuid.h"
#include "att.h"
//#include "crypto.h"
#define ATT_MIN_PDU_LEN 1 /* At least 1 byte for the opcode. */
#define ATT_OP_CMD_MASK 0x40
#define ATT_OP_SIGNED_MASK 0x80
#define ATT_TIMEOUT_INTERVAL 30000 /* 30000 ms */
/* Length of signature in write signed packet */
#define BT_ATT_SIGNATURE_LEN 12
struct att_send_op;
/**
* ATT structure (protocol context)
*/
struct bt_att {
/// reference counter incremented by bt_att_ref, decremented by bt_att_unref
int ref_count;
/// socket
int fd;
/// io structure for low level i/o (read and write)
struct io *io;
/// true if an l2cap socket
bool io_on_l2cap;
/// i/o seurity level: Only used for non-L2CAP
int io_sec_level;
/// Queued ATT protocol requests
struct queue *req_queue;
/// Pending request state
struct att_send_op *pending_req;
/// Queued ATT protocol indications
struct queue *ind_queue;
/// Pending indication state
struct att_send_op *pending_ind;
/// Queue of PDUs ready to send
struct queue *write_queue;
/// true if already engaged in write operation
bool writer_active;
/// List of registered callbacks
struct queue *notify_list;
/// List of disconnect handlers
struct queue *disconn_list;
/// There's a pending incoming request
bool in_req;
/// buffer pointer
uint8_t *buf;
/// actual number of bytes for pdu ATT exchange
uint16_t mtu;
/// IDs for "send" ops
unsigned int next_send_id;
/// IDs for registered callbacks
unsigned int next_reg_id;
/// timeout function for callback
bt_att_timeout_func_t timeout_callback;
/// timeout function to manage data context (house keeping)
bt_att_destroy_func_t timeout_destroy;
///
void *timeout_data;
/// debug callback
bt_att_debug_func_t debug_callback;
/// data management function for debug
bt_att_destroy_func_t debug_destroy;
/// user pointer for debug
void *debug_data;
/// crypto structure
//struct bt_crypto *crypto;
/// true, requires key signature
bool ext_signed;
/// local key structure pointer
struct sign_info *local_sign;
/// remote key structure pointer
struct sign_info *remote_sign;
};
struct sign_info {
uint8_t key[16];
bt_att_counter_func_t counter;
void *user_data;
};
enum att_op_type {
ATT_OP_TYPE_REQ,
ATT_OP_TYPE_RSP,
ATT_OP_TYPE_CMD,
ATT_OP_TYPE_IND,
ATT_OP_TYPE_NOT,
ATT_OP_TYPE_CONF,
ATT_OP_TYPE_UNKNOWN,
};
static const struct {
uint8_t opcode;
enum att_op_type type;
} att_opcode_type_table[] = {
{ BT_ATT_OP_ERROR_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_MTU_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_MTU_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_FIND_INFO_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_FIND_INFO_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_FIND_BY_TYPE_VAL_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_FIND_BY_TYPE_VAL_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_READ_BY_TYPE_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_READ_BY_TYPE_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_READ_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_READ_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_READ_BLOB_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_READ_BLOB_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_READ_MULT_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_READ_MULT_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_READ_BY_GRP_TYPE_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_READ_BY_GRP_TYPE_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_WRITE_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_WRITE_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_WRITE_CMD, ATT_OP_TYPE_CMD },
{ BT_ATT_OP_SIGNED_WRITE_CMD, ATT_OP_TYPE_CMD },
{ BT_ATT_OP_PREP_WRITE_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_PREP_WRITE_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_EXEC_WRITE_REQ, ATT_OP_TYPE_REQ },
{ BT_ATT_OP_EXEC_WRITE_RSP, ATT_OP_TYPE_RSP },
{ BT_ATT_OP_HANDLE_VAL_NOT, ATT_OP_TYPE_NOT },
{ BT_ATT_OP_HANDLE_VAL_IND, ATT_OP_TYPE_IND },
{ BT_ATT_OP_HANDLE_VAL_CONF, ATT_OP_TYPE_CONF },
{ }
};
static enum att_op_type get_op_type(uint8_t opcode)
{
int i;
for (i = 0; att_opcode_type_table[i].opcode; i++) {
if (att_opcode_type_table[i].opcode == opcode)
return att_opcode_type_table[i].type;
}
return ATT_OP_TYPE_UNKNOWN;
}
static const struct {
uint8_t req_opcode;
uint8_t rsp_opcode;
} att_req_rsp_mapping_table[] = {
{ BT_ATT_OP_MTU_REQ, BT_ATT_OP_MTU_RSP },
{ BT_ATT_OP_FIND_INFO_REQ, BT_ATT_OP_FIND_INFO_RSP},
{ BT_ATT_OP_FIND_BY_TYPE_VAL_REQ, BT_ATT_OP_FIND_BY_TYPE_VAL_RSP },
{ BT_ATT_OP_READ_BY_TYPE_REQ, BT_ATT_OP_READ_BY_TYPE_RSP },
{ BT_ATT_OP_READ_REQ, BT_ATT_OP_READ_RSP },
{ BT_ATT_OP_READ_BLOB_REQ, BT_ATT_OP_READ_BLOB_RSP },
{ BT_ATT_OP_READ_MULT_REQ, BT_ATT_OP_READ_MULT_RSP },
{ BT_ATT_OP_READ_BY_GRP_TYPE_REQ, BT_ATT_OP_READ_BY_GRP_TYPE_RSP },
{ BT_ATT_OP_WRITE_REQ, BT_ATT_OP_WRITE_RSP },
{ BT_ATT_OP_PREP_WRITE_REQ, BT_ATT_OP_PREP_WRITE_RSP },
{ BT_ATT_OP_EXEC_WRITE_REQ, BT_ATT_OP_EXEC_WRITE_RSP },
{ }
};
static uint8_t get_req_opcode(uint8_t rsp_opcode)
{
int i;
for (i = 0; att_req_rsp_mapping_table[i].rsp_opcode; i++) {
if (att_req_rsp_mapping_table[i].rsp_opcode == rsp_opcode)
return att_req_rsp_mapping_table[i].req_opcode;
}
return 0;
}
struct att_send_op {
unsigned int id;
unsigned int timeout_id;
enum att_op_type type;
uint16_t opcode;
void *pdu;
uint16_t len;
bt_att_response_func_t callback;
bt_att_destroy_func_t destroy;
void *user_data;
};
/**
* @brief destroy att send operation
* calls the destroy callback with user_data as an argument
* free pdu data
*
* @param data att_send_op pointer
*/
static void destroy_att_send_op(void *data)
{
struct att_send_op *op = data;
if (op->timeout_id)
timeout_remove(op->timeout_id);
if (op->destroy)
op->destroy(op->user_data);
free(op->pdu);
free(op);
}
static void cancel_att_send_op(struct att_send_op *op)
{
if (op->destroy)
op->destroy(op->user_data);
op->user_data = NULL;
op->callback = NULL;
op->destroy = NULL;
}
struct att_notify {
unsigned int id;
uint16_t opcode;
bt_att_notify_func_t callback;
bt_att_destroy_func_t destroy;
void *user_data;
};
static void destroy_att_notify(void *data)
{
struct att_notify *notify = data;
if (notify->destroy)
notify->destroy(notify->user_data);
free(notify);
}
static bool match_notify_id(const void *a, const void *b)
{
const struct att_notify *notify = a;
unsigned int id = PTR_TO_UINT(b);
return notify->id == id;
}
struct att_disconn {
unsigned int id;
bool removed;
bt_att_disconnect_func_t callback;
bt_att_destroy_func_t destroy;
void *user_data;
};
static void destroy_att_disconn(void *data)
{
struct att_disconn *disconn = data;
if (disconn->destroy)
disconn->destroy(disconn->user_data);
free(disconn);
}
static bool match_disconn_id(const void *a, const void *b)
{
const struct att_disconn *disconn = a;
unsigned int id = PTR_TO_UINT(b);
return disconn->id == id;
}
static bool encode_pdu(struct bt_att *att, struct att_send_op *op,
const void *pdu, uint16_t length)
{
uint16_t pdu_len = 1;
struct sign_info *sign = att->local_sign;
uint32_t sign_cnt;
if (sign && (op->opcode & ATT_OP_SIGNED_MASK))
pdu_len += BT_ATT_SIGNATURE_LEN;
if (length && pdu)
pdu_len += length;
if (pdu_len > att->mtu)
return false;
op->len = pdu_len;
op->pdu = malloc(op->len);
if (!op->pdu)
return false;
((uint8_t *) op->pdu)[0] = op->opcode;
if (pdu_len > 1)
memcpy(op->pdu + 1, pdu, length);
if (!sign || !(op->opcode & ATT_OP_SIGNED_MASK))
return true;
if (!sign->counter(&sign_cnt, sign->user_data))
goto fail;
/*
if ((bt_crypto_sign_att(att->crypto, sign->key, op->pdu, 1 + length,
sign_cnt, &((uint8_t *) op->pdu)[1 + length])))
return true;
*/
util_debug(att->debug_callback, att->debug_data,
"ATT unable to generate signature");
fail:
free(op->pdu);
return false;
}
static struct att_send_op *create_att_send_op(struct bt_att *att,
uint8_t opcode,
const void *pdu,
uint16_t length,
bt_att_response_func_t callback,
void *user_data,
bt_att_destroy_func_t destroy)
{
struct att_send_op *op;
enum att_op_type op_type;
if (length && !pdu)
return NULL;
op_type = get_op_type(opcode);
if (op_type == ATT_OP_TYPE_UNKNOWN)
return NULL;
/* If the opcode corresponds to an operation type that does not elicit a
* response from the remote end, then no callback should have been
* provided, since it will never be called.
*/
if (callback && op_type != ATT_OP_TYPE_REQ && op_type != ATT_OP_TYPE_IND)
return NULL;
/* Similarly, if the operation does elicit a response then a callback
* must be provided.
*/
if (!callback && (op_type == ATT_OP_TYPE_REQ || op_type == ATT_OP_TYPE_IND))
return NULL;
op = new0(struct att_send_op, 1);
if (!op)
return NULL;
op->type = op_type;
op->opcode = opcode;
op->callback = callback;
op->destroy = destroy;
op->user_data = user_data;
if (!encode_pdu(att, op, pdu, length)) {
free(op);
return NULL;
}
return op;
}
static struct att_send_op *pick_next_send_op(struct bt_att *att)
{
struct att_send_op *op;
/* See if any operations are already in the write queue */
op = queue_pop_head(att->write_queue);
if (op)
return op;
/* If there is no pending request, pick an operation from the
* request queue.
*/
if (!att->pending_req) {
op = queue_pop_head(att->req_queue);
if (op)
return op;
}
/* There is either a request pending or no requests queued. If there is
* no pending indication, pick an operation from the indication queue.
*/
if (!att->pending_ind) {
op = queue_pop_head(att->ind_queue);
if (op)
return op;
}
return NULL;
}
struct timeout_data {
struct bt_att *att;
unsigned int id;
};
static bool timeout_cb(void *user_data)
{
struct timeout_data *timeout = user_data;
struct bt_att *att = timeout->att;
struct att_send_op *op = NULL;
if (att->pending_req && att->pending_req->id == timeout->id) {
op = att->pending_req;
att->pending_req = NULL;
} else if (att->pending_ind && att->pending_ind->id == timeout->id) {
op = att->pending_ind;
att->pending_ind = NULL;
}
if (!op)
return false;
util_debug(att->debug_callback, att->debug_data,
"Operation timed out: 0x%02x", op->opcode);
if (att->timeout_callback)
att->timeout_callback(op->id, op->opcode, att->timeout_data);
op->timeout_id = 0;
destroy_att_send_op(op);
/*
* Directly terminate the connection as required by the ATT protocol.
* This should trigger an io disconnect event which will clean up the
* io and notify the upper layer.
*/
io_shutdown(att->io);
return false;
}
static void write_watch_destroy(void *user_data)
{
struct bt_att *att = user_data;
att->writer_active = false;
}
static bool can_write_data(struct io *io, void *user_data)
{
struct bt_att *att = user_data;
struct att_send_op *op;
struct timeout_data *timeout;
ssize_t ret;
struct iovec iov;
op = pick_next_send_op(att);
if (!op)
return false;
iov.iov_base = op->pdu;
iov.iov_len = op->len;
ret = io_send(io, &iov, 1);
if (ret < 0) {
util_debug(att->debug_callback, att->debug_data,
"write failed: %s", strerror(-ret));
if (op->callback)
op->callback(BT_ATT_OP_ERROR_RSP, NULL, 0,
op->user_data);
destroy_att_send_op(op);
return true;
}
util_debug(att->debug_callback, att->debug_data,
"ATT op 0x%02x", op->opcode);
util_hexdump('<', op->pdu, ret, att->debug_callback, att->debug_data);
/* Based on the operation type, set either the pending request or the
* pending indication. If it came from the write queue, then there is
* no need to keep it around.
*/
switch (op->type) {
case ATT_OP_TYPE_REQ:
att->pending_req = op;
break;
case ATT_OP_TYPE_IND:
att->pending_ind = op;
break;
case ATT_OP_TYPE_RSP:
/* Set in_req to false to indicate that no request is pending */
att->in_req = false;
/* Fall through to the next case */
case ATT_OP_TYPE_CMD:
case ATT_OP_TYPE_NOT:
case ATT_OP_TYPE_CONF:
case ATT_OP_TYPE_UNKNOWN:
default:
destroy_att_send_op(op);
return true;
}
timeout = new0(struct timeout_data, 1);
if (!timeout)
return true;
timeout->att = att;
timeout->id = op->id;
op->timeout_id = timeout_add(ATT_TIMEOUT_INTERVAL, timeout_cb,
timeout, free);
/* Return true as there may be more operations ready to write. */
return true;
}
static void wakeup_writer(struct bt_att *att)
{
if (att->writer_active)
return;
/* Set the write handler only if there is anything that can be sent
* at all.
*/
if (queue_isempty(att->write_queue)) {
if ((att->pending_req || queue_isempty(att->req_queue)) &&
(att->pending_ind || queue_isempty(att->ind_queue)))
return;
}
if (!io_set_write_handler(att->io, can_write_data, att,
write_watch_destroy))
return;
att->writer_active = true;
}
static void disconn_handler(void *data, void *user_data)
{
struct att_disconn *disconn = data;
int err = PTR_TO_INT(user_data);
if (disconn->removed)
return;
if (disconn->callback)
disconn->callback(err, disconn->user_data);
}
static bool disconnect_cb(struct io *io, void *user_data)
{
struct bt_att *att = user_data;
int err;
socklen_t len;
len = sizeof(err);
if (getsockopt(att->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
util_debug(att->debug_callback, att->debug_data,
"Failed to obtain disconnect error: %s",
strerror(errno));
err = 0;
}
util_debug(att->debug_callback, att->debug_data,
"Physical link disconnected: %s",
strerror(err));
io_destroy(att->io);
att->io = NULL;
bt_att_cancel_all(att);
bt_att_ref(att);
queue_foreach(att->disconn_list, disconn_handler, INT_TO_PTR(err));
bt_att_unregister_all(att);
bt_att_unref(att);
return false;
}
static bool change_security(struct bt_att *att, uint8_t ecode)
{
int security;
security = bt_att_get_security(att);
if (security != BT_ATT_SECURITY_AUTO)
return false;
if (ecode == BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION &&
security < BT_ATT_SECURITY_MEDIUM)
security = BT_ATT_SECURITY_MEDIUM;
else if (ecode == BT_ATT_ERROR_AUTHENTICATION &&
security < BT_ATT_SECURITY_HIGH)
security = BT_ATT_SECURITY_HIGH;
else
return false;
return bt_att_set_security(att, security);
}
static bool handle_error_rsp(struct bt_att *att, uint8_t *pdu,
ssize_t pdu_len, uint8_t *opcode)
{
const struct bt_att_pdu_error_rsp *rsp;
struct att_send_op *op = att->pending_req;
if (pdu_len != sizeof(*rsp)) {
*opcode = 0;
return false;
}
rsp = (void *) pdu;
*opcode = rsp->opcode;
/* Attempt to change security */
if (!change_security(att, rsp->ecode))
return false;
util_debug(att->debug_callback, att->debug_data,
"Retrying operation %p", op);
att->pending_req = NULL;
/* Push operation back to request queue */
return queue_push_head(att->req_queue, op);
}
static void handle_rsp(struct bt_att *att, uint8_t opcode, uint8_t *pdu,
ssize_t pdu_len)
{
struct att_send_op *op = att->pending_req;
uint8_t req_opcode;
uint8_t rsp_opcode;
uint8_t *rsp_pdu = NULL;
uint16_t rsp_pdu_len = 0;
/*
* If no request is pending, then the response is unexpected. Disconnect
* the bearer.
*/
if (!op) {
util_debug(att->debug_callback, att->debug_data,
"Received unexpected ATT response");
io_shutdown(att->io);
return;
}
/*
* If the received response doesn't match the pending request, or if
* the request is malformed, end the current request with failure.
*/
if (opcode == BT_ATT_OP_ERROR_RSP) {
/* Return if error response cause a retry */
if (handle_error_rsp(att, pdu, pdu_len, &req_opcode)) {
wakeup_writer(att);
return;
}
} else if (!(req_opcode = get_req_opcode(opcode)))
goto fail;
if (req_opcode != op->opcode)
goto fail;
rsp_opcode = opcode;
if (pdu_len > 0) {
rsp_pdu = pdu;
rsp_pdu_len = pdu_len;
}
goto done;
fail:
util_debug(att->debug_callback, att->debug_data,
"Failed to handle response PDU; opcode: 0x%02x", opcode);
rsp_opcode = BT_ATT_OP_ERROR_RSP;
done:
if (op->callback)
op->callback(rsp_opcode, rsp_pdu, rsp_pdu_len, op->user_data);
destroy_att_send_op(op);
att->pending_req = NULL;
wakeup_writer(att);
}
static void handle_conf(struct bt_att *att, uint8_t *pdu, ssize_t pdu_len)
{
struct att_send_op *op = att->pending_ind;
/*
* Disconnect the bearer if the confirmation is unexpected or the PDU is
* invalid.
*/
if (!op || pdu_len) {
util_debug(att->debug_callback, att->debug_data,
"Received unexpected/invalid ATT confirmation");
io_shutdown(att->io);
return;
}
if (op->callback)
op->callback(BT_ATT_OP_HANDLE_VAL_CONF, NULL, 0, op->user_data);
destroy_att_send_op(op);
att->pending_ind = NULL;
wakeup_writer(att);
}
struct notify_data {
uint8_t opcode;
uint8_t *pdu;
ssize_t pdu_len;
bool handler_found;
};
static bool opcode_match(uint8_t opcode, uint8_t test_opcode)
{
enum att_op_type op_type = get_op_type(test_opcode);
if (opcode == BT_ATT_ALL_REQUESTS && (op_type == ATT_OP_TYPE_REQ ||
op_type == ATT_OP_TYPE_CMD))
return true;
return opcode == test_opcode;
}
static void respond_not_supported(struct bt_att *att, uint8_t opcode)
{
struct bt_att_pdu_error_rsp pdu;
pdu.opcode = opcode;
pdu.handle = 0x0000;
pdu.ecode = BT_ATT_ERROR_REQUEST_NOT_SUPPORTED;
bt_att_send(att, BT_ATT_OP_ERROR_RSP, &pdu, sizeof(pdu), NULL, NULL,
NULL);
}
static bool handle_signed(struct bt_att *att, uint8_t opcode, uint8_t *pdu,
ssize_t pdu_len)
{
uint8_t *signature;
uint32_t sign_cnt;
struct sign_info *sign;
/* Check if there is enough data for a signature */
if (pdu_len < 2 + BT_ATT_SIGNATURE_LEN)
goto fail;
sign = att->remote_sign;
if (!sign)
goto fail;
signature = pdu + (pdu_len - BT_ATT_SIGNATURE_LEN);
sign_cnt = get_le32(signature);
/* Validate counter */
if (!sign->counter(&sign_cnt, sign->user_data))
goto fail;
/* Generate signature and verify it */
/*
if (!bt_crypto_sign_att(att->crypto, sign->key, pdu,
pdu_len - BT_ATT_SIGNATURE_LEN, sign_cnt,
signature))
goto fail;
*/
return true;
fail:
util_debug(att->debug_callback, att->debug_data,
"ATT failed to verify signature: 0x%02x", opcode);
return false;
}
static void handle_notify(struct bt_att *att, uint8_t opcode, uint8_t *pdu,
ssize_t pdu_len)
{
const struct queue_entry *entry;
bool found;
if ((opcode & ATT_OP_SIGNED_MASK) && !att->ext_signed) {
if (!handle_signed(att, opcode, pdu, pdu_len))
return;
pdu_len -= BT_ATT_SIGNATURE_LEN;
}
bt_att_ref(att);
found = false;
entry = queue_get_entries(att->notify_list);
while (entry) {
struct att_notify *notify = entry->data;
entry = entry->next;
if (!opcode_match(notify->opcode, opcode))
continue;
found = true;
if (notify->callback)
notify->callback(opcode, pdu, pdu_len,
notify->user_data);
/* callback could remove all entries from notify list */
if (queue_isempty(att->notify_list))
break;
}
/*
* If this was a request and no handler was registered for it, respond
* with "Not Supported"
*/
if (!found && get_op_type(opcode) == ATT_OP_TYPE_REQ)
respond_not_supported(att, opcode);
bt_att_unref(att);
}
static bool can_read_data(struct io *io, void *user_data)
{
struct bt_att *att = user_data;
uint8_t opcode;
uint8_t *pdu;
ssize_t bytes_read;
bytes_read = read(att->fd, att->buf, att->mtu);
if (bytes_read < 0)
return false;
util_hexdump('>', att->buf, bytes_read,
att->debug_callback, att->debug_data);
if (bytes_read < ATT_MIN_PDU_LEN)
return true;
pdu = att->buf;
opcode = pdu[0];
bt_att_ref(att);
/* Act on the received PDU based on the opcode type */
switch (get_op_type(opcode)) {
case ATT_OP_TYPE_RSP:
util_debug(att->debug_callback, att->debug_data,
"ATT response received: 0x%02x", opcode);
handle_rsp(att, opcode, pdu + 1, bytes_read - 1);
break;
case ATT_OP_TYPE_CONF:
util_debug(att->debug_callback, att->debug_data,
"ATT confirmation received: 0x%02x", opcode);
handle_conf(att, pdu + 1, bytes_read - 1);
break;
case ATT_OP_TYPE_REQ:
/*
* If a request is currently pending, then the sequential
* protocol was violated. Disconnect the bearer, which will
* promptly notify the upper layer via disconnect handlers.
*/
if (att->in_req) {
util_debug(att->debug_callback, att->debug_data,
"Received request while another is "
"pending: 0x%02x", opcode);
io_shutdown(att->io);
bt_att_unref(att);
return false;
}
att->in_req = true;
/* Fall through to the next case */
case ATT_OP_TYPE_CMD:
case ATT_OP_TYPE_NOT:
case ATT_OP_TYPE_UNKNOWN:
case ATT_OP_TYPE_IND:
default:
/* For all other opcodes notify the upper layer of the PDU and
* let them act on it.
*/
util_debug(att->debug_callback, att->debug_data,
"ATT PDU received: 0x%02x", opcode);
handle_notify(att, opcode, pdu + 1, bytes_read - 1);
break;
}
bt_att_unref(att);
return true;
}
static bool is_io_l2cap_based(int fd)
{
int domain;
int proto;
int err;
socklen_t len;
domain = 0;
len = sizeof(domain);
err = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &len);
if (err < 0)
return false;
if (domain != AF_BLUETOOTH)
return false;
proto = 0;
len = sizeof(proto);
err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &proto, &len);
if (err < 0)
return false;
return proto == BTPROTO_L2CAP;
}
static void bt_att_free(struct bt_att *att)
{
if (att->pending_req)
destroy_att_send_op(att->pending_req);
if (att->pending_ind)
destroy_att_send_op(att->pending_ind);
io_destroy(att->io);
// bt_crypto_unref(att->crypto);
queue_destroy(att->req_queue, NULL);
queue_destroy(att->ind_queue, NULL);
queue_destroy(att->write_queue, NULL);
queue_destroy(att->notify_list, NULL);
queue_destroy(att->disconn_list, NULL);
if (att->timeout_destroy)
att->timeout_destroy(att->timeout_data);
if (att->debug_destroy)
att->debug_destroy(att->debug_data);
free(att->local_sign);
free(att->remote_sign);
free(att->buf);
free(att);
}
struct bt_att *bt_att_new(int fd, bool ext_signed)
{
struct bt_att *att;
if (fd < 0)
return NULL;
att = new0(struct bt_att, 1);
if (!att)