1 // SPDX-License-Identifier: LGPL-2.1-or-later
4 * BlueZ - Bluetooth protocol stack for Linux
6 * Copyright (C) 2011-2014 Intel Corporation
7 * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
21 #include "lib/bluetooth.h"
24 #include "src/shared/util.h"
25 #include "src/shared/queue.h"
38 #define L2CAP_MODE_BASIC 0x00
39 #define L2CAP_MODE_RETRANS 0x01
40 #define L2CAP_MODE_FLOWCTL 0x02
41 #define L2CAP_MODE_ERTM 0x03
42 #define L2CAP_MODE_STREAMING 0x04
43 #define L2CAP_MODE_LE_FLOWCTL 0x80
44 #define L2CAP_MODE_ECRED 0x81
46 /* L2CAP Control Field bit masks */
47 #define L2CAP_CTRL_SAR_MASK 0xC000
48 #define L2CAP_CTRL_REQSEQ_MASK 0x3F00
49 #define L2CAP_CTRL_TXSEQ_MASK 0x007E
50 #define L2CAP_CTRL_SUPERVISE_MASK 0x000C
52 #define L2CAP_CTRL_RETRANS 0x0080
53 #define L2CAP_CTRL_FINAL 0x0080
54 #define L2CAP_CTRL_POLL 0x0010
55 #define L2CAP_CTRL_FRAME_TYPE 0x0001 /* I- or S-Frame */
57 #define L2CAP_CTRL_TXSEQ_SHIFT 1
58 #define L2CAP_CTRL_SUPER_SHIFT 2
59 #define L2CAP_CTRL_REQSEQ_SHIFT 8
60 #define L2CAP_CTRL_SAR_SHIFT 14
62 #define L2CAP_EXT_CTRL_TXSEQ_MASK 0xFFFC0000
63 #define L2CAP_EXT_CTRL_SAR_MASK 0x00030000
64 #define L2CAP_EXT_CTRL_SUPERVISE_MASK 0x00030000
65 #define L2CAP_EXT_CTRL_REQSEQ_MASK 0x0000FFFC
67 #define L2CAP_EXT_CTRL_POLL 0x00040000
68 #define L2CAP_EXT_CTRL_FINAL 0x00000002
69 #define L2CAP_EXT_CTRL_FRAME_TYPE 0x00000001 /* I- or S-Frame */
71 #define L2CAP_EXT_CTRL_REQSEQ_SHIFT 2
72 #define L2CAP_EXT_CTRL_SAR_SHIFT 16
73 #define L2CAP_EXT_CTRL_SUPER_SHIFT 16
74 #define L2CAP_EXT_CTRL_TXSEQ_SHIFT 18
76 /* L2CAP Supervisory Function */
77 #define L2CAP_SUPER_RR 0x00
78 #define L2CAP_SUPER_REJ 0x01
79 #define L2CAP_SUPER_RNR 0x02
80 #define L2CAP_SUPER_SREJ 0x03
82 /* L2CAP Segmentation and Reassembly */
83 #define L2CAP_SAR_UNSEGMENTED 0x00
84 #define L2CAP_SAR_START 0x01
85 #define L2CAP_SAR_END 0x02
86 #define L2CAP_SAR_CONTINUE 0x03
102 struct packet_latency tx_l;
105 static struct chan_data chan_list[MAX_CHAN];
107 static void assign_scid(const struct l2cap_frame *frame, uint16_t scid,
108 uint16_t psm, uint8_t mode, uint8_t ctrlid)
116 for (i = 0; i < MAX_CHAN; i++) {
117 if (n < 0 && chan_list[i].handle == 0x0000) {
122 if (chan_list[i].index != frame->index)
125 if (chan_list[i].handle != frame->handle)
128 if (chan_list[i].psm == psm)
131 /* Don't break on match - we still need to go through all
132 * channels to find proper seq_num.
135 if (chan_list[i].dcid == scid)
138 if (chan_list[i].scid == scid)
146 memset(&chan_list[n], 0, sizeof(chan_list[n]));
147 chan_list[n].index = frame->index;
148 chan_list[n].handle = frame->handle;
149 chan_list[n].ident = frame->ident;
152 chan_list[n].dcid = scid;
154 chan_list[n].scid = scid;
156 chan_list[n].psm = psm;
157 chan_list[n].ctrlid = ctrlid;
158 chan_list[n].mode = mode;
160 chan_list[n].seq_num = seq_num;
163 static void release_scid(const struct l2cap_frame *frame, uint16_t scid)
167 for (i = 0; i < MAX_CHAN; i++) {
168 if (chan_list[i].index != frame->index)
171 if (chan_list[i].handle != frame->handle)
175 if (chan_list[i].scid == scid) {
176 chan_list[i].handle = 0;
180 if (chan_list[i].dcid == scid) {
181 chan_list[i].handle = 0;
188 static void assign_dcid(const struct l2cap_frame *frame, uint16_t dcid,
193 for (i = 0; i < MAX_CHAN; i++) {
194 if (chan_list[i].index != frame->index)
197 if (chan_list[i].handle != frame->handle)
200 if (frame->ident != 0 && chan_list[i].ident != frame->ident)
205 if (chan_list[i].scid == scid) {
206 chan_list[i].dcid = dcid;
210 if (chan_list[i].scid && !chan_list[i].dcid) {
211 chan_list[i].dcid = dcid;
217 if (chan_list[i].dcid == scid) {
218 chan_list[i].scid = dcid;
222 if (chan_list[i].dcid && !chan_list[i].scid) {
223 chan_list[i].scid = dcid;
231 static void assign_mode(const struct l2cap_frame *frame,
232 uint8_t mode, uint16_t dcid)
236 for (i = 0; i < MAX_CHAN; i++) {
237 if (chan_list[i].index != frame->index)
240 if (chan_list[i].handle != frame->handle)
244 if (chan_list[i].scid == dcid) {
245 chan_list[i].mode = mode;
249 if (chan_list[i].dcid == dcid) {
250 chan_list[i].mode = mode;
257 static int get_chan_data_index(const struct l2cap_frame *frame)
261 for (i = 0; i < MAX_CHAN; i++) {
262 if (chan_list[i].index != frame->index &&
263 chan_list[i].ctrlid == 0)
266 if (chan_list[i].ctrlid != 0 &&
267 chan_list[i].ctrlid != frame->index)
270 if (chan_list[i].handle != frame->handle)
274 if (chan_list[i].scid == frame->cid)
277 if (chan_list[i].dcid == frame->cid)
285 static struct chan_data *get_chan(const struct l2cap_frame *frame)
289 if (frame->chan != UINT16_MAX)
290 return &chan_list[frame->chan];
292 i = get_chan_data_index(frame);
296 return &chan_list[i];
299 static uint16_t get_psm(const struct l2cap_frame *frame)
301 struct chan_data *data = get_chan(frame);
309 static uint8_t get_mode(const struct l2cap_frame *frame)
311 struct chan_data *data = get_chan(frame);
319 static uint8_t get_seq_num(const struct l2cap_frame *frame)
321 struct chan_data *data = get_chan(frame);
326 return data->seq_num;
329 static void assign_ext_ctrl(const struct l2cap_frame *frame,
330 uint8_t ext_ctrl, uint16_t dcid)
334 for (i = 0; i < MAX_CHAN; i++) {
335 if (chan_list[i].index != frame->index)
338 if (chan_list[i].handle != frame->handle)
342 if (chan_list[i].scid == dcid) {
343 chan_list[i].ext_ctrl = ext_ctrl;
347 if (chan_list[i].dcid == dcid) {
348 chan_list[i].ext_ctrl = ext_ctrl;
355 static uint8_t get_ext_ctrl(const struct l2cap_frame *frame)
357 struct chan_data *data = get_chan(frame);
362 return data->ext_ctrl;
365 static char *sar2str(uint8_t sar)
368 case L2CAP_SAR_UNSEGMENTED:
369 return "Unsegmented";
370 case L2CAP_SAR_START:
374 case L2CAP_SAR_CONTINUE:
375 return "Continuation";
381 static char *supervisory2str(uint8_t supervisory)
383 switch (supervisory) {
385 return "Receiver Ready (RR)";
386 case L2CAP_SUPER_REJ:
387 return "Reject (REJ)";
388 case L2CAP_SUPER_RNR:
389 return "Receiver Not Ready (RNR)";
390 case L2CAP_SUPER_SREJ:
391 return "Select Reject (SREJ)";
393 return "Bad Supervisory";
397 static char *mode2str(uint8_t mode)
400 case L2CAP_MODE_BASIC:
402 case L2CAP_MODE_RETRANS:
403 return "Retransmission";
404 case L2CAP_MODE_FLOWCTL:
405 return "Flow Control";
406 case L2CAP_MODE_ERTM:
407 return "Enhanced Retransmission";
408 case L2CAP_MODE_STREAMING:
410 case L2CAP_MODE_LE_FLOWCTL:
411 return "LE Flow Control";
412 case L2CAP_MODE_ECRED:
413 return "Enhanced Credit";
419 static void l2cap_ctrl_ext_parse(struct l2cap_frame *frame, uint32_t ctrl)
422 ctrl & L2CAP_EXT_CTRL_FRAME_TYPE ? "S-frame" : "I-frame");
424 if (ctrl & L2CAP_EXT_CTRL_FRAME_TYPE) {
426 supervisory2str((ctrl & L2CAP_EXT_CTRL_SUPERVISE_MASK) >>
427 L2CAP_EXT_CTRL_SUPER_SHIFT));
429 if (ctrl & L2CAP_EXT_CTRL_POLL)
432 uint8_t sar = (ctrl & L2CAP_EXT_CTRL_SAR_MASK) >>
433 L2CAP_EXT_CTRL_SAR_SHIFT;
434 printf(" %s", sar2str(sar));
435 if (sar == L2CAP_SAR_START) {
438 if (!l2cap_frame_get_le16(frame, &len))
441 printf(" (len %d)", len);
443 printf(" TxSeq %d", (ctrl & L2CAP_EXT_CTRL_TXSEQ_MASK) >>
444 L2CAP_EXT_CTRL_TXSEQ_SHIFT);
447 printf(" ReqSeq %d", (ctrl & L2CAP_EXT_CTRL_REQSEQ_MASK) >>
448 L2CAP_EXT_CTRL_REQSEQ_SHIFT);
450 if (ctrl & L2CAP_EXT_CTRL_FINAL)
454 static void l2cap_ctrl_parse(struct l2cap_frame *frame, uint32_t ctrl)
457 ctrl & L2CAP_CTRL_FRAME_TYPE ? "S-frame" : "I-frame");
461 supervisory2str((ctrl & L2CAP_CTRL_SUPERVISE_MASK) >>
462 L2CAP_CTRL_SUPER_SHIFT));
464 if (ctrl & L2CAP_CTRL_POLL)
469 sar = (ctrl & L2CAP_CTRL_SAR_MASK) >> L2CAP_CTRL_SAR_SHIFT;
470 printf(" %s", sar2str(sar));
471 if (sar == L2CAP_SAR_START) {
474 if (!l2cap_frame_get_le16(frame, &len))
477 printf(" (len %d)", len);
479 printf(" TxSeq %d", (ctrl & L2CAP_CTRL_TXSEQ_MASK) >>
480 L2CAP_CTRL_TXSEQ_SHIFT);
483 printf(" ReqSeq %d", (ctrl & L2CAP_CTRL_REQSEQ_MASK) >>
484 L2CAP_CTRL_REQSEQ_SHIFT);
486 if (ctrl & L2CAP_CTRL_FINAL)
499 static struct index_data index_list[MAX_INDEX][2];
501 static void clear_fragment_buffer(uint16_t index, bool in)
503 free(index_list[index][in].frag_buf);
504 index_list[index][in].frag_buf = NULL;
505 index_list[index][in].frag_pos = 0;
506 index_list[index][in].frag_len = 0;
509 static void print_psm(uint16_t psm)
511 print_field("PSM: %d (0x%4.4x)", le16_to_cpu(psm), le16_to_cpu(psm));
514 static void print_cid(const char *type, uint16_t cid)
516 print_field("%s CID: %d", type, le16_to_cpu(cid));
519 static void print_reject_reason(uint16_t reason)
523 switch (le16_to_cpu(reason)) {
525 str = "Command not understood";
528 str = "Signaling MTU exceeded";
531 str = "Invalid CID in request";
538 print_field("Reason: %s (0x%4.4x)", str, le16_to_cpu(reason));
541 static void print_conn_result(uint16_t result)
545 switch (le16_to_cpu(result)) {
547 str = "Connection successful";
550 str = "Connection pending";
553 str = "Connection refused - PSM not supported";
556 str = "Connection refused - security block";
559 str = "Connection refused - no resources available";
562 str = "Connection refused - Invalid Source CID";
565 str = "Connection refused - Source CID already allocated";
572 print_field("Result: %s (0x%4.4x)", str, le16_to_cpu(result));
575 static void print_le_conn_result(uint16_t result)
579 switch (le16_to_cpu(result)) {
581 str = "Connection successful";
584 str = "Connection refused - PSM not supported";
587 str = "Connection refused - no resources available";
590 str = "Connection refused - insufficient authentication";
593 str = "Connection refused - insufficient authorization";
596 str = "Connection refused - insufficient encryption key size";
599 str = "Connection refused - insufficient encryption";
602 str = "Connection refused - Invalid Source CID";
605 str = "Connection refused - Source CID already allocated";
608 str = "Connection refused - unacceptable parameters";
615 print_field("Result: %s (0x%4.4x)", str, le16_to_cpu(result));
618 static void print_create_chan_result(uint16_t result)
622 switch (le16_to_cpu(result)) {
624 str = "Connection successful";
627 str = "Connection pending";
630 str = "Connection refused - PSM not supported";
633 str = "Connection refused - security block";
636 str = "Connection refused - no resources available";
639 str = "Connection refused - Controller ID not supported";
642 str = "Connection refused - Invalid Source CID";
645 str = "Connection refused - Source CID already allocated";
652 print_field("Result: %s (0x%4.4x)", str, le16_to_cpu(result));
655 static void print_conn_status(uint16_t status)
659 switch (le16_to_cpu(status)) {
661 str = "No further information available";
664 str = "Authentication pending";
667 str = "Authorization pending";
674 print_field("Status: %s (0x%4.4x)", str, le16_to_cpu(status));
677 static void print_config_flags(uint16_t flags)
681 if (le16_to_cpu(flags) & 0x0001)
682 str = " (continuation)";
686 print_field("Flags: 0x%4.4x%s", le16_to_cpu(flags), str);
689 static void print_config_result(uint16_t result)
693 switch (le16_to_cpu(result)) {
698 str = "Failure - unacceptable parameters";
701 str = "Failure - rejected";
704 str = "Failure - unknown options";
710 str = "Failure - flow spec rejected";
717 print_field("Result: %s (0x%4.4x)", str, le16_to_cpu(result));
724 } options_table[] = {
725 { 0x01, 2, "Maximum Transmission Unit" },
726 { 0x02, 2, "Flush Timeout" },
727 { 0x03, 22, "Quality of Service" },
728 { 0x04, 9, "Retransmission and Flow Control" },
729 { 0x05, 1, "Frame Check Sequence" },
730 { 0x06, 16, "Extended Flow Specification" },
731 { 0x07, 2, "Extended Window Size" },
735 static void print_config_options(const struct l2cap_frame *frame,
736 uint8_t offset, uint16_t cid, bool response)
738 const uint8_t *data = frame->data + offset;
739 uint16_t size = frame->size - offset;
740 uint16_t consumed = 0;
742 while (consumed < size - 2) {
743 const char *str = "Unknown";
744 uint8_t type = data[consumed] & 0x7f;
745 uint8_t hint = data[consumed] & 0x80;
746 uint8_t len = data[consumed + 1];
747 uint8_t expect_len = 0;
750 for (i = 0; options_table[i].str; i++) {
751 if (options_table[i].type == type) {
752 str = options_table[i].str;
753 expect_len = options_table[i].len;
758 print_field("Option: %s (0x%2.2x) [%s]", str, type,
759 hint ? "hint" : "mandatory");
761 if (expect_len == 0) {
766 if (len != expect_len) {
767 print_text(COLOR_ERROR, "wrong option size (%d != %d)",
775 print_field(" MTU: %d",
776 get_le16(data + consumed + 2));
779 print_field(" Flush timeout: %d",
780 get_le16(data + consumed + 2));
783 switch (data[consumed + 3]) {
797 print_field(" Flags: 0x%2.2x", data[consumed + 2]);
798 print_field(" Service type: %s (0x%2.2x)",
799 str, data[consumed + 3]);
800 print_field(" Token rate: 0x%8.8x",
801 get_le32(data + consumed + 4));
802 print_field(" Token bucket size: 0x%8.8x",
803 get_le32(data + consumed + 8));
804 print_field(" Peak bandwidth: 0x%8.8x",
805 get_le32(data + consumed + 12));
806 print_field(" Latency: 0x%8.8x",
807 get_le32(data + consumed + 16));
808 print_field(" Delay variation: 0x%8.8x",
809 get_le32(data + consumed + 20));
813 assign_mode(frame, data[consumed + 2], cid);
815 print_field(" Mode: %s (0x%2.2x)",
816 mode2str(data[consumed + 2]),
818 print_field(" TX window size: %d", data[consumed + 3]);
819 print_field(" Max transmit: %d", data[consumed + 4]);
820 print_field(" Retransmission timeout: %d",
821 get_le16(data + consumed + 5));
822 print_field(" Monitor timeout: %d",
823 get_le16(data + consumed + 7));
824 print_field(" Maximum PDU size: %d",
825 get_le16(data + consumed + 9));
828 switch (data[consumed + 2]) {
839 print_field(" FCS: %s (0x%2.2d)",
840 str, data[consumed + 2]);
843 switch (data[consumed + 3]) {
857 print_field(" Identifier: 0x%2.2x",
859 print_field(" Service type: %s (0x%2.2x)",
860 str, data[consumed + 3]);
861 print_field(" Maximum SDU size: 0x%4.4x",
862 get_le16(data + consumed + 4));
863 print_field(" SDU inter-arrival time: 0x%8.8x",
864 get_le32(data + consumed + 6));
865 print_field(" Access latency: 0x%8.8x",
866 get_le32(data + consumed + 10));
867 print_field(" Flush timeout: 0x%8.8x",
868 get_le32(data + consumed + 14));
871 print_field(" Extended window size: %d",
872 get_le16(data + consumed + 2));
873 assign_ext_ctrl(frame, 1, cid);
876 packet_hexdump(data + consumed + 2, len);
884 packet_hexdump(data + consumed, size - consumed);
887 static void print_info_type(uint16_t type)
891 switch (le16_to_cpu(type)) {
893 str = "Connectionless MTU";
896 str = "Extended features supported";
899 str = "Fixed channels supported";
906 print_field("Type: %s (0x%4.4x)", str, le16_to_cpu(type));
909 static void print_info_result(uint16_t result)
913 switch (le16_to_cpu(result)) {
918 str = "Not supported";
925 print_field("Result: %s (0x%4.4x)", str, le16_to_cpu(result));
928 static const struct bitfield_data features_table[] = {
929 { 0, "Flow control mode" },
930 { 1, "Retransmission mode" },
931 { 2, "Bi-directional QoS" },
932 { 3, "Enhanced Retransmission Mode" },
933 { 4, "Streaming Mode" },
935 { 6, "Extended Flow Specification for BR/EDR" },
936 { 7, "Fixed Channels" },
937 { 8, "Extended Window Size" },
938 { 9, "Unicast Connectionless Data Reception" },
939 { 31, "Reserved for feature mask extension" },
943 static void print_features(uint32_t features)
947 print_field("Features: 0x%8.8x", features);
949 mask = print_bitfield(2, features, features_table);
951 print_field(" Unknown features (0x%8.8x)", mask);
954 static const struct bitfield_data channels_table[] = {
955 { 0x0000, "Null identifier" },
956 { 0x0001, "L2CAP Signaling (BR/EDR)" },
957 { 0x0002, "Connectionless reception" },
958 { 0x0003, "AMP Manager Protocol" },
959 { 0x0004, "Attribute Protocol" },
960 { 0x0005, "L2CAP Signaling (LE)" },
961 { 0x0006, "Security Manager (LE)" },
962 { 0x0007, "Security Manager (BR/EDR)" },
963 { 0x003f, "AMP Test Manager" },
967 static void print_channels(uint64_t channels)
971 print_field("Channels: 0x%16.16" PRIx64, channels);
973 mask = print_bitfield(2, channels, channels_table);
975 print_field(" Unknown channels (0x%8.8" PRIx64 ")", mask);
978 static void print_move_result(uint16_t result)
982 switch (le16_to_cpu(result)) {
984 str = "Move success";
987 str = "Move pending";
990 str = "Move refused - Controller ID not supported";
993 str = "Move refused - new Controller ID is same";
996 str = "Move refused - Configuration not supported";
999 str = "Move refused - Move Channel collision";
1002 str = "Move refused - Channel not allowed to be moved";
1009 print_field("Result: %s (0x%4.4x)", str, le16_to_cpu(result));
1012 static void print_move_cfm_result(uint16_t result)
1016 switch (le16_to_cpu(result)) {
1018 str = "Move success - both sides succeed";
1021 str = "Move failure - one or both sides refuse";
1028 print_field("Result: %s (0x%4.4x)", str, le16_to_cpu(result));
1031 static void print_conn_param_result(uint16_t result)
1035 switch (le16_to_cpu(result)) {
1037 str = "Connection Parameters accepted";
1040 str = "Connection Parameters rejected";
1047 print_field("Result: %s (0x%4.4x)", str, le16_to_cpu(result));
1050 static void sig_cmd_reject(const struct l2cap_frame *frame)
1052 const struct bt_l2cap_pdu_cmd_reject *pdu = frame->data;
1053 const void *data = frame->data;
1054 uint16_t size = frame->size;
1055 uint16_t scid, dcid;
1057 print_reject_reason(pdu->reason);
1059 data += sizeof(*pdu);
1060 size -= sizeof(*pdu);
1062 switch (le16_to_cpu(pdu->reason)) {
1065 print_text(COLOR_ERROR, "invalid data size");
1066 packet_hexdump(data, size);
1072 print_text(COLOR_ERROR, "invalid data size");
1073 packet_hexdump(data, size);
1076 print_field("MTU: %d", get_le16(data));
1080 print_text(COLOR_ERROR, "invalid data size");
1081 packet_hexdump(data, size);
1084 dcid = get_le16(data);
1085 scid = get_le16(data + 2);
1086 print_cid("Destination", cpu_to_le16(dcid));
1087 print_cid("Source", cpu_to_le16(scid));
1090 packet_hexdump(data, size);
1095 static void sig_conn_req(const struct l2cap_frame *frame)
1097 const struct bt_l2cap_pdu_conn_req *pdu = frame->data;
1099 print_psm(pdu->psm);
1100 print_cid("Source", pdu->scid);
1102 assign_scid(frame, le16_to_cpu(pdu->scid), le16_to_cpu(pdu->psm),
1103 L2CAP_MODE_BASIC, 0);
1106 static void sig_conn_rsp(const struct l2cap_frame *frame)
1108 const struct bt_l2cap_pdu_conn_rsp *pdu = frame->data;
1110 print_cid("Destination", pdu->dcid);
1111 print_cid("Source", pdu->scid);
1112 print_conn_result(pdu->result);
1113 print_conn_status(pdu->status);
1115 assign_dcid(frame, le16_to_cpu(pdu->dcid), le16_to_cpu(pdu->scid));
1118 static void sig_config_req(const struct l2cap_frame *frame)
1120 const struct bt_l2cap_pdu_config_req *pdu = frame->data;
1122 print_cid("Destination", pdu->dcid);
1123 print_config_flags(pdu->flags);
1124 print_config_options(frame, 4, le16_to_cpu(pdu->dcid), false);
1127 static void sig_config_rsp(const struct l2cap_frame *frame)
1129 const struct bt_l2cap_pdu_config_rsp *pdu = frame->data;
1131 print_cid("Source", pdu->scid);
1132 print_config_flags(pdu->flags);
1133 print_config_result(pdu->result);
1134 print_config_options(frame, 6, le16_to_cpu(pdu->scid), true);
1137 static void sig_disconn_req(const struct l2cap_frame *frame)
1139 const struct bt_l2cap_pdu_disconn_req *pdu = frame->data;
1141 print_cid("Destination", pdu->dcid);
1142 print_cid("Source", pdu->scid);
1145 static void sig_disconn_rsp(const struct l2cap_frame *frame)
1147 const struct bt_l2cap_pdu_disconn_rsp *pdu = frame->data;
1149 print_cid("Destination", pdu->dcid);
1150 print_cid("Source", pdu->scid);
1152 release_scid(frame, le16_to_cpu(pdu->scid));
1155 static void sig_echo_req(const struct l2cap_frame *frame)
1157 packet_hexdump(frame->data, frame->size);
1160 static void sig_echo_rsp(const struct l2cap_frame *frame)
1162 packet_hexdump(frame->data, frame->size);
1165 static void sig_info_req(const struct l2cap_frame *frame)
1167 const struct bt_l2cap_pdu_info_req *pdu = frame->data;
1169 print_info_type(pdu->type);
1172 static void sig_info_rsp(const struct l2cap_frame *frame)
1174 const struct bt_l2cap_pdu_info_rsp *pdu = frame->data;
1175 const void *data = frame->data;
1176 uint16_t size = frame->size;
1178 print_info_type(pdu->type);
1179 print_info_result(pdu->result);
1181 data += sizeof(*pdu);
1182 size -= sizeof(*pdu);
1184 if (le16_to_cpu(pdu->result) != 0x0000) {
1186 print_text(COLOR_ERROR, "invalid data size");
1187 packet_hexdump(data, size);
1192 switch (le16_to_cpu(pdu->type)) {
1195 print_text(COLOR_ERROR, "invalid data size");
1196 packet_hexdump(data, size);
1199 print_field("MTU: %d", get_le16(data));
1203 print_text(COLOR_ERROR, "invalid data size");
1204 packet_hexdump(data, size);
1207 print_features(get_le32(data));
1211 print_text(COLOR_ERROR, "invalid data size");
1212 packet_hexdump(data, size);
1215 print_channels(get_le64(data));
1218 packet_hexdump(data, size);
1223 static void sig_create_chan_req(const struct l2cap_frame *frame)
1225 const struct bt_l2cap_pdu_create_chan_req *pdu = frame->data;
1227 print_psm(pdu->psm);
1228 print_cid("Source", pdu->scid);
1229 print_field("Controller ID: %d", pdu->ctrlid);
1231 assign_scid(frame, le16_to_cpu(pdu->scid), le16_to_cpu(pdu->psm),
1232 L2CAP_MODE_BASIC, pdu->ctrlid);
1235 static void sig_create_chan_rsp(const struct l2cap_frame *frame)
1237 const struct bt_l2cap_pdu_create_chan_rsp *pdu = frame->data;
1239 print_cid("Destination", pdu->dcid);
1240 print_cid("Source", pdu->scid);
1241 print_create_chan_result(pdu->result);
1242 print_conn_status(pdu->status);
1244 assign_dcid(frame, le16_to_cpu(pdu->dcid), le16_to_cpu(pdu->scid));
1247 static void sig_move_chan_req(const struct l2cap_frame *frame)
1249 const struct bt_l2cap_pdu_move_chan_req *pdu = frame->data;
1251 print_cid("Initiator", pdu->icid);
1252 print_field("Controller ID: %d", pdu->ctrlid);
1255 static void sig_move_chan_rsp(const struct l2cap_frame *frame)
1257 const struct bt_l2cap_pdu_move_chan_rsp *pdu = frame->data;
1259 print_cid("Initiator", pdu->icid);
1260 print_move_result(pdu->result);
1263 static void sig_move_chan_cfm(const struct l2cap_frame *frame)
1265 const struct bt_l2cap_pdu_move_chan_cfm *pdu = frame->data;
1267 print_cid("Initiator", pdu->icid);
1268 print_move_cfm_result(pdu->result);
1271 static void sig_move_chan_cfm_rsp(const struct l2cap_frame *frame)
1273 const struct bt_l2cap_pdu_move_chan_cfm_rsp *pdu = frame->data;
1275 print_cid("Initiator", pdu->icid);
1278 static void sig_conn_param_req(const struct l2cap_frame *frame)
1280 const struct bt_l2cap_pdu_conn_param_req *pdu = frame->data;
1282 print_field("Min interval: %d", le16_to_cpu(pdu->min_interval));
1283 print_field("Max interval: %d", le16_to_cpu(pdu->max_interval));
1284 print_field("Peripheral latency: %d", le16_to_cpu(pdu->latency));
1285 print_field("Timeout multiplier: %d", le16_to_cpu(pdu->timeout));
1288 static void sig_conn_param_rsp(const struct l2cap_frame *frame)
1290 const struct bt_l2cap_pdu_conn_param_rsp *pdu = frame->data;
1292 print_conn_param_result(pdu->result);
1295 static void sig_le_conn_req(const struct l2cap_frame *frame)
1297 const struct bt_l2cap_pdu_le_conn_req *pdu = frame->data;
1299 print_psm(pdu->psm);
1300 print_cid("Source", pdu->scid);
1301 print_field("MTU: %u", le16_to_cpu(pdu->mtu));
1302 print_field("MPS: %u", le16_to_cpu(pdu->mps));
1303 print_field("Credits: %u", le16_to_cpu(pdu->credits));
1305 assign_scid(frame, le16_to_cpu(pdu->scid), le16_to_cpu(pdu->psm),
1306 L2CAP_MODE_LE_FLOWCTL, 0);
1309 static void sig_le_conn_rsp(const struct l2cap_frame *frame)
1311 const struct bt_l2cap_pdu_le_conn_rsp *pdu = frame->data;
1313 print_cid("Destination", pdu->dcid);
1314 print_field("MTU: %u", le16_to_cpu(pdu->mtu));
1315 print_field("MPS: %u", le16_to_cpu(pdu->mps));
1316 print_field("Credits: %u", le16_to_cpu(pdu->credits));
1317 print_le_conn_result(pdu->result);
1319 assign_dcid(frame, le16_to_cpu(pdu->dcid), 0);
1322 static void sig_le_flowctl_creds(const struct l2cap_frame *frame)
1324 const struct bt_l2cap_pdu_le_flowctl_creds *pdu = frame->data;
1326 print_cid("Source", pdu->cid);
1327 print_field("Credits: %u", le16_to_cpu(pdu->credits));
1330 static void sig_ecred_conn_req(const struct l2cap_frame *frame)
1332 const struct bt_l2cap_pdu_ecred_conn_req *pdu = frame->data;
1335 l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));
1337 print_psm(pdu->psm);
1338 print_field("MTU: %u", le16_to_cpu(pdu->mtu));
1339 print_field("MPS: %u", le16_to_cpu(pdu->mps));
1340 print_field("Credits: %u", le16_to_cpu(pdu->credits));
1342 while (l2cap_frame_get_le16((void *)frame, &scid)) {
1343 print_cid("Source", scid);
1344 assign_scid(frame, scid, le16_to_cpu(pdu->psm),
1345 L2CAP_MODE_ECRED, 0);
1349 static void print_ecred_conn_result(uint16_t result)
1353 switch (le16_to_cpu(result)) {
1355 str = "Connection successful";
1358 str = "Connection refused - PSM not supported";
1361 str = "Some connections refused – not enough resources "
1365 str = "All Connections refused - insufficient authentication";
1368 str = "All Connections refused - insufficient authorization";
1371 str = "All Connection refused - insufficient encryption key "
1375 str = "All Connections refused - insufficient encryption";
1378 str = "Some Connections refused - Invalid Source CID";
1381 str = "Some Connections refused - Source CID already allocated";
1384 str = "All Connections refused - unacceptable parameters";
1391 print_field("Result: %s (0x%4.4x)", str, le16_to_cpu(result));
1394 static void sig_ecred_conn_rsp(const struct l2cap_frame *frame)
1396 const struct bt_l2cap_pdu_ecred_conn_rsp *pdu = frame->data;
1399 l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));
1401 print_field("MTU: %u", le16_to_cpu(pdu->mtu));
1402 print_field("MPS: %u", le16_to_cpu(pdu->mps));
1403 print_field("Credits: %u", le16_to_cpu(pdu->credits));
1404 print_ecred_conn_result(pdu->result);
1406 while (l2cap_frame_get_le16((void *)frame, &dcid)) {
1407 print_cid("Destination", dcid);
1408 assign_dcid(frame, dcid, 0);
1412 static void sig_ecred_reconf_req(const struct l2cap_frame *frame)
1414 const struct bt_l2cap_pdu_ecred_reconf_req *pdu = frame->data;
1417 l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));
1419 print_field("MTU: %u", le16_to_cpu(pdu->mtu));
1420 print_field("MPS: %u", le16_to_cpu(pdu->mps));
1422 while (l2cap_frame_get_le16((void *)frame, &scid))
1423 print_cid("Source", scid);
1426 static void print_ecred_reconf_result(uint16_t result)
1430 switch (le16_to_cpu(result)) {
1432 str = "Reconfiguration successful";
1435 str = "Reconfiguration failed - reduction in size of MTU not "
1439 str = "Reconfiguration failed - reduction in size of MPS not "
1440 "allowed for more than one channel at a time";
1446 print_field("Result: %s (0x%4.4x)", str, le16_to_cpu(result));
1449 static void sig_ecred_reconf_rsp(const struct l2cap_frame *frame)
1451 const struct bt_l2cap_pdu_ecred_reconf_rsp *pdu = frame->data;
1453 print_ecred_reconf_result(pdu->result);
1456 struct sig_opcode_data {
1459 void (*func) (const struct l2cap_frame *frame);
1465 { BT_L2CAP_PDU_ECRED_CONN_REQ, \
1466 "Enhanced Credit Connection Request", \
1467 sig_ecred_conn_req, sizeof(struct bt_l2cap_pdu_ecred_conn_req), \
1469 { BT_L2CAP_PDU_ECRED_CONN_RSP, \
1470 "Enhanced Credit Connection Response", \
1471 sig_ecred_conn_rsp, sizeof(struct bt_l2cap_pdu_ecred_conn_rsp), \
1473 { BT_L2CAP_PDU_ECRED_RECONF_REQ, \
1474 "Enhanced Credit Reconfigure Request", \
1475 sig_ecred_reconf_req, sizeof(struct bt_l2cap_pdu_ecred_reconf_req), \
1477 { BT_L2CAP_PDU_ECRED_RECONF_RSP, \
1478 "Enhanced Credit Reconfigure Respond", \
1479 sig_ecred_reconf_rsp, sizeof(struct bt_l2cap_pdu_ecred_reconf_rsp), \
1482 static const struct sig_opcode_data bredr_sig_opcode_table[] = {
1483 { 0x01, "Command Reject",
1484 sig_cmd_reject, 2, false },
1485 { 0x02, "Connection Request",
1486 sig_conn_req, 4, true },
1487 { 0x03, "Connection Response",
1488 sig_conn_rsp, 8, true },
1489 { 0x04, "Configure Request",
1490 sig_config_req, 4, false },
1491 { 0x05, "Configure Response",
1492 sig_config_rsp, 6, false },
1493 { 0x06, "Disconnection Request",
1494 sig_disconn_req, 4, true },
1495 { 0x07, "Disconnection Response",
1496 sig_disconn_rsp, 4, true },
1497 { 0x08, "Echo Request",
1498 sig_echo_req, 0, false },
1499 { 0x09, "Echo Response",
1500 sig_echo_rsp, 0, false },
1501 { 0x0a, "Information Request",
1502 sig_info_req, 2, true },
1503 { 0x0b, "Information Response",
1504 sig_info_rsp, 4, false },
1505 { 0x0c, "Create Channel Request",
1506 sig_create_chan_req, 5, true },
1507 { 0x0d, "Create Channel Response",
1508 sig_create_chan_rsp, 8, true },
1509 { 0x0e, "Move Channel Request",
1510 sig_move_chan_req, 3, true },
1511 { 0x0f, "Move Channel Response",
1512 sig_move_chan_rsp, 4, true },
1513 { 0x10, "Move Channel Confirmation",
1514 sig_move_chan_cfm, 4, true },
1515 { 0x11, "Move Channel Confirmation Response",
1516 sig_move_chan_cfm_rsp, 2, true },
1521 static const struct sig_opcode_data le_sig_opcode_table[] = {
1522 { 0x01, "Command Reject",
1523 sig_cmd_reject, 2, false },
1524 { 0x06, "Disconnection Request",
1525 sig_disconn_req, 4, true },
1526 { 0x07, "Disconnection Response",
1527 sig_disconn_rsp, 4, true },
1528 { 0x12, "Connection Parameter Update Request",
1529 sig_conn_param_req, 8, true },
1530 { 0x13, "Connection Parameter Update Response",
1531 sig_conn_param_rsp, 2, true },
1532 { 0x14, "LE Connection Request",
1533 sig_le_conn_req, 10, true },
1534 { 0x15, "LE Connection Response",
1535 sig_le_conn_rsp, 10, true },
1536 { 0x16, "LE Flow Control Credit",
1537 sig_le_flowctl_creds, 4, true },
1542 static void l2cap_queue_frame(struct l2cap_frame *frame)
1544 struct packet_conn_data *conn;
1545 struct l2cap_frame *tx;
1547 conn = packet_get_conn_data(frame->handle);
1552 conn->chan_q = queue_new();
1554 tx = new0(struct l2cap_frame, 1);
1555 memcpy(tx, frame, sizeof(*frame));
1556 queue_push_tail(conn->chan_q, tx);
1559 void l2cap_frame_init(struct l2cap_frame *frame, uint16_t index, bool in,
1560 uint16_t handle, uint8_t ident,
1561 uint16_t cid, uint16_t psm,
1562 const void *data, uint16_t size)
1564 frame->index = index;
1566 frame->handle = handle;
1567 frame->ident = ident;
1571 frame->chan = get_chan_data_index(frame);
1572 frame->psm = psm ? psm : get_psm(frame);
1573 frame->mode = get_mode(frame);
1574 frame->seq_num = psm ? 1 : get_seq_num(frame);
1577 l2cap_queue_frame(frame);
1580 static void bredr_sig_packet(uint16_t index, bool in, uint16_t handle,
1581 uint16_t cid, const void *data, uint16_t size)
1583 struct l2cap_frame frame;
1586 const struct bt_l2cap_hdr_sig *hdr = data;
1587 const struct sig_opcode_data *opcode_data = NULL;
1588 const char *opcode_color, *opcode_str;
1593 print_text(COLOR_ERROR, "malformed signal packet");
1594 packet_hexdump(data, size);
1598 len = le16_to_cpu(hdr->len);
1604 print_text(COLOR_ERROR, "invalid signal packet size");
1605 packet_hexdump(data, size);
1609 for (i = 0; bredr_sig_opcode_table[i].str; i++) {
1610 if (bredr_sig_opcode_table[i].opcode == hdr->code) {
1611 opcode_data = &bredr_sig_opcode_table[i];
1617 if (opcode_data->func) {
1619 opcode_color = COLOR_MAGENTA;
1621 opcode_color = COLOR_BLUE;
1623 opcode_color = COLOR_WHITE_BG;
1624 opcode_str = opcode_data->str;
1626 opcode_color = COLOR_WHITE_BG;
1627 opcode_str = "Unknown";
1630 print_indent(6, opcode_color, "L2CAP: ", opcode_str,
1632 " (0x%2.2x) ident %d len %d",
1633 hdr->code, hdr->ident, len);
1635 if (!opcode_data || !opcode_data->func) {
1636 packet_hexdump(data, len);
1642 if (opcode_data->fixed) {
1643 if (len != opcode_data->size) {
1644 print_text(COLOR_ERROR, "invalid size");
1645 packet_hexdump(data, len);
1651 if (len < opcode_data->size) {
1652 print_text(COLOR_ERROR, "too short packet");
1653 packet_hexdump(data, size);
1660 l2cap_frame_init(&frame, index, in, handle, hdr->ident, cid, 0,
1662 opcode_data->func(&frame);
1668 packet_hexdump(data, size);
1671 static void le_sig_packet(uint16_t index, bool in, uint16_t handle,
1672 uint16_t cid, const void *data, uint16_t size)
1674 struct l2cap_frame frame;
1675 const struct bt_l2cap_hdr_sig *hdr = data;
1676 const struct sig_opcode_data *opcode_data = NULL;
1677 const char *opcode_color, *opcode_str;
1682 print_text(COLOR_ERROR, "malformed signal packet");
1683 packet_hexdump(data, size);
1687 len = le16_to_cpu(hdr->len);
1693 print_text(COLOR_ERROR, "invalid signal packet size");
1694 packet_hexdump(data, size);
1698 for (i = 0; le_sig_opcode_table[i].str; i++) {
1699 if (le_sig_opcode_table[i].opcode == hdr->code) {
1700 opcode_data = &le_sig_opcode_table[i];
1706 if (opcode_data->func) {
1708 opcode_color = COLOR_MAGENTA;
1710 opcode_color = COLOR_BLUE;
1712 opcode_color = COLOR_WHITE_BG;
1713 opcode_str = opcode_data->str;
1715 opcode_color = COLOR_WHITE_BG;
1716 opcode_str = "Unknown";
1719 print_indent(6, opcode_color, "LE L2CAP: ", opcode_str, COLOR_OFF,
1720 " (0x%2.2x) ident %d len %d",
1721 hdr->code, hdr->ident, len);
1723 if (!opcode_data || !opcode_data->func) {
1724 packet_hexdump(data, len);
1728 if (opcode_data->fixed) {
1729 if (len != opcode_data->size) {
1730 print_text(COLOR_ERROR, "invalid size");
1731 packet_hexdump(data, len);
1735 if (len < opcode_data->size) {
1736 print_text(COLOR_ERROR, "too short packet");
1737 packet_hexdump(data, size);
1742 l2cap_frame_init(&frame, index, in, handle, hdr->ident, cid, 0,
1744 opcode_data->func(&frame);
1747 static void connless_packet(uint16_t index, bool in, uint16_t handle,
1748 uint16_t cid, const void *data, uint16_t size)
1750 struct l2cap_frame frame;
1751 const struct bt_l2cap_hdr_connless *hdr = data;
1755 print_text(COLOR_ERROR, "malformed connectionless packet");
1756 packet_hexdump(data, size);
1760 psm = le16_to_cpu(hdr->psm);
1765 print_indent(6, COLOR_CYAN, "L2CAP: Connectionless", "", COLOR_OFF,
1766 " len %d [PSM %d]", size, psm);
1770 packet_hexdump(data, size);
1774 l2cap_frame_init(&frame, index, in, handle, 0, cid, 0, data, size);
1777 static void print_controller_list(const uint8_t *data, uint16_t size)
1782 print_field("Controller ID: %d", data[0]);
1786 str = "Primary BR/EDR Controller";
1789 str = "802.11 AMP Controller";
1796 print_field(" Type: %s (0x%2.2x)", str, data[1]);
1803 str = "Bluetooth only";
1806 str = "No capacity";
1809 str = "Low capacity";
1812 str = "Medium capacity";
1815 str = "High capacity";
1818 str = "Full capacity";
1825 print_field(" Status: %s (0x%2.2x)", str, data[2]);
1831 packet_hexdump(data, size);
1834 static void amp_cmd_reject(const struct l2cap_frame *frame)
1836 const struct bt_l2cap_amp_cmd_reject *pdu = frame->data;
1838 print_field("Reason: 0x%4.4x", le16_to_cpu(pdu->reason));
1841 static void amp_discover_req(const struct l2cap_frame *frame)
1843 const struct bt_l2cap_amp_discover_req *pdu = frame->data;
1845 print_field("MTU/MPS size: %d", le16_to_cpu(pdu->size));
1846 print_field("Extended feature mask: 0x%4.4x",
1847 le16_to_cpu(pdu->features));
1850 static void amp_discover_rsp(const struct l2cap_frame *frame)
1852 const struct bt_l2cap_amp_discover_rsp *pdu = frame->data;
1854 print_field("MTU/MPS size: %d", le16_to_cpu(pdu->size));
1855 print_field("Extended feature mask: 0x%4.4x",
1856 le16_to_cpu(pdu->features));
1858 print_controller_list(frame->data + 4, frame->size - 4);
1861 static void amp_change_notify(const struct l2cap_frame *frame)
1863 print_controller_list(frame->data, frame->size);
1866 static void amp_change_response(const struct l2cap_frame *frame)
1870 static void amp_get_info_req(const struct l2cap_frame *frame)
1872 const struct bt_l2cap_amp_get_info_req *pdu = frame->data;
1874 print_field("Controller ID: %d", pdu->ctrlid);
1877 static void amp_get_info_rsp(const struct l2cap_frame *frame)
1879 const struct bt_l2cap_amp_get_info_rsp *pdu = frame->data;
1882 print_field("Controller ID: %d", pdu->ctrlid);
1884 switch (pdu->status) {
1889 str = "Invalid Controller ID";
1896 print_field("Status: %s (0x%2.2x)", str, pdu->status);
1898 print_field("Total bandwidth: %d kbps", le32_to_cpu(pdu->total_bw));
1899 print_field("Max guaranteed bandwidth: %d kbps",
1900 le32_to_cpu(pdu->max_bw));
1901 print_field("Min latency: %d", le32_to_cpu(pdu->min_latency));
1903 print_field("PAL capabilities: 0x%4.4x", le16_to_cpu(pdu->pal_cap));
1904 print_field("Max ASSOC length: %d", le16_to_cpu(pdu->max_assoc_len));
1907 static void amp_get_assoc_req(const struct l2cap_frame *frame)
1909 const struct bt_l2cap_amp_get_assoc_req *pdu = frame->data;
1911 print_field("Controller ID: %d", pdu->ctrlid);
1914 static void amp_get_assoc_rsp(const struct l2cap_frame *frame)
1916 const struct bt_l2cap_amp_get_assoc_rsp *pdu = frame->data;
1919 print_field("Controller ID: %d", pdu->ctrlid);
1921 switch (pdu->status) {
1926 str = "Invalid Controller ID";
1933 print_field("Status: %s (0x%2.2x)", str, pdu->status);
1935 packet_hexdump(frame->data + 2, frame->size - 2);
1938 static void amp_create_phy_link_req(const struct l2cap_frame *frame)
1940 const struct bt_l2cap_amp_create_phy_link_req *pdu = frame->data;
1942 print_field("Local controller ID: %d", pdu->local_ctrlid);
1943 print_field("Remote controller ID: %d", pdu->remote_ctrlid);
1945 packet_hexdump(frame->data + 2, frame->size - 2);
1948 static void amp_create_phy_link_rsp(const struct l2cap_frame *frame)
1950 const struct bt_l2cap_amp_create_phy_link_rsp *pdu = frame->data;
1953 print_field("Local controller ID: %d", pdu->local_ctrlid);
1954 print_field("Remote controller ID: %d", pdu->remote_ctrlid);
1956 switch (pdu->status) {
1961 str = "Invalid Controller ID";
1964 str = "Failed - Unable to start link creation";
1967 str = "Failed - Collision occurred";
1970 str = "Failed - Disconnected link packet received";
1973 str = "Failed - Link already exists";
1976 str = "Failed - Security violation";
1983 print_field("Status: %s (0x%2.2x)", str, pdu->status);
1986 static void amp_disconn_phy_link_req(const struct l2cap_frame *frame)
1988 const struct bt_l2cap_amp_disconn_phy_link_req *pdu = frame->data;
1990 print_field("Local controller ID: %d", pdu->local_ctrlid);
1991 print_field("Remote controller ID: %d", pdu->remote_ctrlid);
1994 static void amp_disconn_phy_link_rsp(const struct l2cap_frame *frame)
1996 const struct bt_l2cap_amp_disconn_phy_link_rsp *pdu = frame->data;
1999 print_field("Local controller ID: %d", pdu->local_ctrlid);
2000 print_field("Remote controller ID: %d", pdu->remote_ctrlid);
2002 switch (pdu->status) {
2007 str = "Invalid Controller ID";
2010 str = "Failed - No link exists";
2017 print_field("Status: %s (0x%2.2x)", str, pdu->status);
2020 struct amp_opcode_data {
2023 void (*func) (const struct l2cap_frame *frame);
2028 static const struct amp_opcode_data amp_opcode_table[] = {
2029 { 0x01, "Command Reject",
2030 amp_cmd_reject, 2, false },
2031 { 0x02, "Discover Request",
2032 amp_discover_req, 4, true },
2033 { 0x03, "Discover Response",
2034 amp_discover_rsp, 7, false },
2035 { 0x04, "Change Notify",
2036 amp_change_notify, 3, false },
2037 { 0x05, "Change Response",
2038 amp_change_response, 0, true },
2039 { 0x06, "Get Info Request",
2040 amp_get_info_req, 1, true },
2041 { 0x07, "Get Info Response",
2042 amp_get_info_rsp, 18, true },
2043 { 0x08, "Get Assoc Request",
2044 amp_get_assoc_req, 1, true },
2045 { 0x09, "Get Assoc Response",
2046 amp_get_assoc_rsp, 2, false },
2047 { 0x0a, "Create Physical Link Request",
2048 amp_create_phy_link_req, 2, false },
2049 { 0x0b, "Create Physical Link Response",
2050 amp_create_phy_link_rsp, 3, true },
2051 { 0x0c, "Disconnect Physical Link Request",
2052 amp_disconn_phy_link_req, 2, true },
2053 { 0x0d, "Disconnect Physical Link Response",
2054 amp_disconn_phy_link_rsp, 3, true },
2058 static void amp_packet(uint16_t index, bool in, uint16_t handle,
2059 uint16_t cid, const void *data, uint16_t size)
2061 struct l2cap_frame frame;
2062 uint16_t control, fcs, len;
2063 uint8_t opcode, ident;
2064 const struct amp_opcode_data *opcode_data = NULL;
2065 const char *opcode_color, *opcode_str;
2069 print_text(COLOR_ERROR, "malformed info frame packet");
2070 packet_hexdump(data, size);
2074 control = get_le16(data);
2075 fcs = get_le16(data + size - 2);
2077 print_indent(6, COLOR_CYAN, "Channel:", "", COLOR_OFF,
2078 " %d dlen %d control 0x%4.4x fcs 0x%4.4x",
2079 3, size, control, fcs);
2085 print_text(COLOR_ERROR, "malformed manager packet");
2086 packet_hexdump(data, size);
2090 opcode = *((const uint8_t *) (data + 2));
2091 ident = *((const uint8_t *) (data + 3));
2092 len = get_le16(data + 4);
2094 if (len != size - 8) {
2095 print_text(COLOR_ERROR, "invalid manager packet size");
2096 packet_hexdump(data + 2, size - 4);
2100 for (i = 0; amp_opcode_table[i].str; i++) {
2101 if (amp_opcode_table[i].opcode == opcode) {
2102 opcode_data = &_opcode_table[i];
2108 if (opcode_data->func) {
2110 opcode_color = COLOR_MAGENTA;
2112 opcode_color = COLOR_BLUE;
2114 opcode_color = COLOR_WHITE_BG;
2115 opcode_str = opcode_data->str;
2117 opcode_color = COLOR_WHITE_BG;
2118 opcode_str = "Unknown";
2121 print_indent(6, opcode_color, "AMP: ", opcode_str, COLOR_OFF,
2122 " (0x%2.2x) ident %d len %d", opcode, ident, len);
2124 if (!opcode_data || !opcode_data->func) {
2125 packet_hexdump(data + 6, size - 8);
2129 if (opcode_data->fixed) {
2130 if (len != opcode_data->size) {
2131 print_text(COLOR_ERROR, "invalid size");
2132 packet_hexdump(data + 6, size - 8);
2136 if (len < opcode_data->size) {
2137 print_text(COLOR_ERROR, "too short packet");
2138 packet_hexdump(data + 6, size - 8);
2143 l2cap_frame_init(&frame, index, in, handle, 0, cid, 0, data + 6, len);
2144 opcode_data->func(&frame);
2147 static void print_smp_io_capa(uint8_t io_capa)
2153 str = "DisplayOnly";
2156 str = "DisplayYesNo";
2159 str = "KeyboardOnly";
2162 str = "NoInputNoOutput";
2165 str = "KeyboardDisplay";
2172 print_field("IO capability: %s (0x%2.2x)", str, io_capa);
2175 static void print_smp_oob_data(uint8_t oob_data)
2181 str = "Authentication data not present";
2184 str = "Authentication data from remote device present";
2191 print_field("OOB data: %s (0x%2.2x)", str, oob_data);
2194 static void print_smp_auth_req(uint8_t auth_req)
2196 const char *bond, *mitm, *sc, *kp, *ct2;
2198 switch (auth_req & 0x03) {
2200 bond = "No bonding";
2210 if (auth_req & 0x04)
2215 if (auth_req & 0x08)
2220 if (auth_req & 0x10)
2223 kp = "No Keypresses";
2225 if (auth_req & 0x20)
2230 print_field("Authentication requirement: %s, %s, %s, %s%s (0x%2.2x)",
2231 bond, mitm, sc, kp, ct2, auth_req);
2234 static void print_smp_key_dist(const char *label, uint8_t dist)
2238 if (!(dist & 0x07)) {
2239 strcpy(str, "<none> ");
2243 strcat(str, "EncKey ");
2245 strcat(str, "IdKey ");
2247 strcat(str, "Sign ");
2249 strcat(str, "LinkKey ");
2252 print_field("%s: %s(0x%2.2x)", label, str, dist);
2255 static void smp_pairing_request(const struct l2cap_frame *frame)
2257 const struct bt_l2cap_smp_pairing_request *pdu = frame->data;
2259 print_smp_io_capa(pdu->io_capa);
2260 print_smp_oob_data(pdu->oob_data);
2261 print_smp_auth_req(pdu->auth_req);
2263 print_field("Max encryption key size: %d", pdu->max_key_size);
2264 print_smp_key_dist("Initiator key distribution", pdu->init_key_dist);
2265 print_smp_key_dist("Responder key distribution", pdu->resp_key_dist);
2268 static void smp_pairing_response(const struct l2cap_frame *frame)
2270 const struct bt_l2cap_smp_pairing_response *pdu = frame->data;
2272 print_smp_io_capa(pdu->io_capa);
2273 print_smp_oob_data(pdu->oob_data);
2274 print_smp_auth_req(pdu->auth_req);
2276 print_field("Max encryption key size: %d", pdu->max_key_size);
2277 print_smp_key_dist("Initiator key distribution", pdu->init_key_dist);
2278 print_smp_key_dist("Responder key distribution", pdu->resp_key_dist);
2281 static void smp_pairing_confirm(const struct l2cap_frame *frame)
2283 const struct bt_l2cap_smp_pairing_confirm *pdu = frame->data;
2285 print_hex_field("Confim value", pdu->value, 16);
2288 static void smp_pairing_random(const struct l2cap_frame *frame)
2290 const struct bt_l2cap_smp_pairing_random *pdu = frame->data;
2292 print_hex_field("Random value", pdu->value, 16);
2295 static void smp_pairing_failed(const struct l2cap_frame *frame)
2297 const struct bt_l2cap_smp_pairing_failed *pdu = frame->data;
2300 switch (pdu->reason) {
2302 str = "Passkey entry failed";
2305 str = "OOB not available";
2308 str = "Authentication requirements";
2311 str = "Confirm value failed";
2314 str = "Pairing not supported";
2317 str = "Encryption key size";
2320 str = "Command not supported";
2323 str = "Unspecified reason";
2326 str = "Repeated attempts";
2329 str = "Invalid parameters";
2332 str = "DHKey check failed";
2335 str = "Numeric comparison failed";
2338 str = "BR/EDR pairing in progress";
2341 str = "Cross-transport Key Derivation/Generation not allowed";
2348 print_field("Reason: %s (0x%2.2x)", str, pdu->reason);
2351 static void smp_encrypt_info(const struct l2cap_frame *frame)
2353 const struct bt_l2cap_smp_encrypt_info *pdu = frame->data;
2355 print_hex_field("Long term key", pdu->ltk, 16);
2358 static void smp_central_ident(const struct l2cap_frame *frame)
2360 const struct bt_l2cap_smp_central_ident *pdu = frame->data;
2362 print_field("EDIV: 0x%4.4x", le16_to_cpu(pdu->ediv));
2363 print_field("Rand: 0x%16.16" PRIx64, le64_to_cpu(pdu->rand));
2366 static void smp_ident_info(const struct l2cap_frame *frame)
2368 const struct bt_l2cap_smp_ident_info *pdu = frame->data;
2370 print_hex_field("Identity resolving key", pdu->irk, 16);
2372 keys_update_identity_key(pdu->irk);
2375 static void smp_ident_addr_info(const struct l2cap_frame *frame)
2377 const struct bt_l2cap_smp_ident_addr_info *pdu = frame->data;
2379 packet_print_addr("Address", pdu->addr, pdu->addr_type);
2381 keys_update_identity_addr(pdu->addr, pdu->addr_type);
2384 static void smp_signing_info(const struct l2cap_frame *frame)
2386 const struct bt_l2cap_smp_signing_info *pdu = frame->data;
2388 print_hex_field("Signature key", pdu->csrk, 16);
2391 static void smp_security_request(const struct l2cap_frame *frame)
2393 const struct bt_l2cap_smp_security_request *pdu = frame->data;
2395 print_smp_auth_req(pdu->auth_req);
2398 static void smp_pairing_public_key(const struct l2cap_frame *frame)
2400 const struct bt_l2cap_smp_public_key *pdu = frame->data;
2402 print_hex_field("X", pdu->x, 32);
2403 print_hex_field("Y", pdu->y, 32);
2406 static void smp_pairing_dhkey_check(const struct l2cap_frame *frame)
2408 const struct bt_l2cap_smp_dhkey_check *pdu = frame->data;
2410 print_hex_field("E", pdu->e, 16);
2413 static void smp_pairing_keypress_notification(const struct l2cap_frame *frame)
2415 const struct bt_l2cap_smp_keypress_notify *pdu = frame->data;
2418 switch (pdu->type) {
2420 str = "Passkey entry started";
2423 str = "Passkey digit entered";
2426 str = "Passkey digit erased";
2429 str = "Passkey cleared";
2432 str = "Passkey entry completed";
2439 print_field("Type: %s (0x%2.2x)", str, pdu->type);
2442 struct smp_opcode_data {
2445 void (*func) (const struct l2cap_frame *frame);
2450 static const struct smp_opcode_data smp_opcode_table[] = {
2451 { 0x01, "Pairing Request",
2452 smp_pairing_request, 6, true },
2453 { 0x02, "Pairing Response",
2454 smp_pairing_response, 6, true },
2455 { 0x03, "Pairing Confirm",
2456 smp_pairing_confirm, 16, true },
2457 { 0x04, "Pairing Random",
2458 smp_pairing_random, 16, true },
2459 { 0x05, "Pairing Failed",
2460 smp_pairing_failed, 1, true },
2461 { 0x06, "Encryption Information",
2462 smp_encrypt_info, 16, true },
2463 { 0x07, "Central Identification",
2464 smp_central_ident, 10, true },
2465 { 0x08, "Identity Information",
2466 smp_ident_info, 16, true },
2467 { 0x09, "Identity Address Information",
2468 smp_ident_addr_info, 7, true },
2469 { 0x0a, "Signing Information",
2470 smp_signing_info, 16, true },
2471 { 0x0b, "Security Request",
2472 smp_security_request, 1, true },
2473 { 0x0c, "Pairing Public Key",
2474 smp_pairing_public_key, 64, true },
2475 { 0x0d, "Pairing DHKey Check",
2476 smp_pairing_dhkey_check, 16, true },
2477 { 0x0e, "Pairing Keypress Notification",
2478 smp_pairing_keypress_notification, 1, true },
2482 static void smp_packet(uint16_t index, bool in, uint16_t handle,
2483 uint16_t cid, const void *data, uint16_t size)
2485 struct l2cap_frame frame;
2486 uint8_t opcode = *((const uint8_t *) data);
2487 const struct smp_opcode_data *opcode_data = NULL;
2488 const char *opcode_color, *opcode_str;
2492 print_text(COLOR_ERROR, "malformed attribute packet");
2493 packet_hexdump(data, size);
2497 for (i = 0; smp_opcode_table[i].str; i++) {
2498 if (smp_opcode_table[i].opcode == opcode) {
2499 opcode_data = &smp_opcode_table[i];
2505 if (opcode_data->func) {
2507 opcode_color = COLOR_MAGENTA;
2509 opcode_color = COLOR_BLUE;
2511 opcode_color = COLOR_WHITE_BG;
2512 opcode_str = opcode_data->str;
2514 opcode_color = COLOR_WHITE_BG;
2515 opcode_str = "Unknown";
2518 print_indent(6, opcode_color, cid == 0x0006 ? "SMP: " : "BR/EDR SMP: ",
2519 opcode_str, COLOR_OFF, " (0x%2.2x) len %d",
2522 if (!opcode_data || !opcode_data->func) {
2523 packet_hexdump(data + 1, size - 1);
2527 if (opcode_data->fixed) {
2528 if (size - 1 != opcode_data->size) {
2529 print_text(COLOR_ERROR, "invalid size");
2530 packet_hexdump(data + 1, size - 1);
2534 if (size - 1 < opcode_data->size) {
2535 print_text(COLOR_ERROR, "too short packet");
2536 packet_hexdump(data + 1, size - 1);
2541 l2cap_frame_init(&frame, index, in, handle, 0, cid, 0,
2542 data + 1, size - 1);
2543 opcode_data->func(&frame);
2546 void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
2547 uint16_t psm, const void *data, uint16_t size)
2549 struct l2cap_frame frame;
2550 struct chan_data *chan;
2551 uint32_t ctrl32 = 0;
2552 uint16_t ctrl16 = 0;
2557 bredr_sig_packet(index, in, handle, cid, data, size);
2560 connless_packet(index, in, handle, cid, data, size);
2563 amp_packet(index, in, handle, cid, data, size);
2566 att_packet(index, in, handle, cid, data, size);
2569 le_sig_packet(index, in, handle, cid, data, size);
2573 smp_packet(index, in, handle, cid, data, size);
2576 l2cap_frame_init(&frame, index, in, handle, 0, cid, psm,
2579 switch (frame.mode) {
2580 case L2CAP_MODE_LE_FLOWCTL:
2581 case L2CAP_MODE_ECRED:
2582 chan = get_chan(&frame);
2587 if (!l2cap_frame_get_le16(&frame, &chan->sdu))
2590 print_indent(6, COLOR_CYAN, "Channel:", "",
2591 COLOR_OFF, " %d len %d sdu %d"
2592 " [PSM %d mode %s (0x%02x)] {chan %d}",
2593 cid, size, chan->sdu, frame.psm,
2594 mode2str(frame.mode), frame.mode,
2596 chan->sdu -= frame.size;
2598 case L2CAP_MODE_BASIC:
2599 print_indent(6, COLOR_CYAN, "Channel:", "", COLOR_OFF,
2600 " %d len %d [PSM %d mode %s (0x%02x)] "
2601 "{chan %d}", cid, size, frame.psm,
2602 mode2str(frame.mode), frame.mode,
2606 ext_ctrl = get_ext_ctrl(&frame);
2609 if (!l2cap_frame_get_le32(&frame, &ctrl32))
2612 print_indent(6, COLOR_CYAN, "Channel:", "",
2613 COLOR_OFF, " %d len %d"
2615 " [PSM %d mode %s (0x%02x)] "
2616 "{chan %d}", cid, size, ctrl32,
2617 frame.psm, mode2str(frame.mode),
2618 frame.mode, frame.chan);
2620 l2cap_ctrl_ext_parse(&frame, ctrl32);
2622 if (!l2cap_frame_get_le16(&frame, &ctrl16))
2625 print_indent(6, COLOR_CYAN, "Channel:", "",
2626 COLOR_OFF, " %d len %d"
2628 " [PSM %d mode %s (0x%02x)] "
2629 "{chan %d}", cid, size, ctrl16,
2630 frame.psm, mode2str(frame.mode),
2631 frame.mode, frame.chan);
2633 l2cap_ctrl_parse(&frame, ctrl16);
2640 switch (frame.psm) {
2645 rfcomm_packet(&frame);
2648 bnep_packet(&frame);
2651 att_packet(index, in, handle, cid, data, size);
2654 att_packet(index, in, handle, cid, data + 2, size - 2);
2658 avctp_packet(&frame);
2661 avdtp_packet(&frame);
2664 packet_hexdump(data, size);
2671 void l2cap_packet(uint16_t index, bool in, uint16_t handle, uint8_t flags,
2672 const void *data, uint16_t size)
2674 const struct bt_l2cap_hdr *hdr = data;
2677 if (index > MAX_INDEX - 1) {
2678 print_text(COLOR_ERROR, "controller index too large");
2679 packet_hexdump(data, size);
2684 case 0x00: /* start of a non-automatically-flushable PDU */
2685 case 0x02: /* start of an automatically-flushable PDU */
2686 if (index_list[index][in].frag_len) {
2687 print_text(COLOR_ERROR, "unexpected start frame");
2688 packet_hexdump(data, size);
2689 clear_fragment_buffer(index, in);
2693 if (size < sizeof(*hdr)) {
2694 print_text(COLOR_ERROR, "frame too short");
2695 packet_hexdump(data, size);
2699 len = le16_to_cpu(hdr->len);
2700 cid = le16_to_cpu(hdr->cid);
2702 data += sizeof(*hdr);
2703 size -= sizeof(*hdr);
2706 /* complete frame */
2707 l2cap_frame(index, in, handle, cid, 0, data, len);
2712 print_text(COLOR_ERROR, "frame too long");
2713 packet_hexdump(data, size);
2717 index_list[index][in].frag_buf = malloc(len);
2718 if (!index_list[index][in].frag_buf) {
2719 print_text(COLOR_ERROR, "failed buffer allocation");
2720 packet_hexdump(data, size);
2724 memcpy(index_list[index][in].frag_buf, data, size);
2725 index_list[index][in].frag_pos = size;
2726 index_list[index][in].frag_len = len - size;
2727 index_list[index][in].frag_cid = cid;
2730 case 0x01: /* continuing fragment */
2731 if (!index_list[index][in].frag_len) {
2732 print_text(COLOR_ERROR, "unexpected continuation");
2733 packet_hexdump(data, size);
2737 if (size > index_list[index][in].frag_len) {
2738 print_text(COLOR_ERROR, "fragment too long");
2739 packet_hexdump(data, size);
2740 clear_fragment_buffer(index, in);
2744 memcpy(index_list[index][in].frag_buf +
2745 index_list[index][in].frag_pos, data, size);
2746 index_list[index][in].frag_pos += size;
2747 index_list[index][in].frag_len -= size;
2749 if (!index_list[index][in].frag_len) {
2750 /* complete frame */
2751 l2cap_frame(index, in, handle,
2752 index_list[index][in].frag_cid, 0,
2753 index_list[index][in].frag_buf,
2754 index_list[index][in].frag_pos);
2755 clear_fragment_buffer(index, in);
2760 case 0x03: /* complete automatically-flushable PDU */
2761 if (index_list[index][in].frag_len) {
2762 print_text(COLOR_ERROR, "unexpected complete frame");
2763 packet_hexdump(data, size);
2764 clear_fragment_buffer(index, in);
2768 if (size < sizeof(*hdr)) {
2769 print_text(COLOR_ERROR, "frame too short");
2770 packet_hexdump(data, size);
2774 len = le16_to_cpu(hdr->len);
2775 cid = le16_to_cpu(hdr->cid);
2777 data += sizeof(*hdr);
2778 size -= sizeof(*hdr);
2781 print_text(COLOR_ERROR, "wrong frame size");
2782 packet_hexdump(data, size);
2786 /* complete frame */
2787 l2cap_frame(index, in, handle, cid, 0, data, len);
2791 print_text(COLOR_ERROR, "invalid packet flags (0x%2.2x)",
2793 packet_hexdump(data, size);
2798 void l2cap_dequeue_frame(struct timeval *delta, struct packet_conn_data *conn)
2800 struct l2cap_frame *frame;
2801 struct chan_data *chan;
2803 frame = queue_pop_head(conn->chan_q);
2807 chan = get_chan(frame);
2811 packet_latency_add(&chan->tx_l, delta);
2813 print_field("Channel: %d [PSM %d mode %s (0x%02x)] {chan %d}",
2814 frame->cid, frame->psm, mode2str(frame->mode),
2815 frame->mode, frame->chan);
2817 print_field("Channel Latency: %lld msec (%lld-%lld msec ~%lld msec)",
2818 TV_MSEC(*delta), TV_MSEC(chan->tx_l.min),
2819 TV_MSEC(chan->tx_l.max), TV_MSEC(chan->tx_l.med));