Commit Graph

586 Commits

Author SHA1 Message Date
Iulia Tanasescu
fb09ce5cfd btdev: Add support for syncing to multiple BIGs
This adds support for synchronizing to multiple BIGs.
2024-11-05 09:09:23 -05:00
Iulia Tanasescu
30951c983d btdev: Add support for syncing to multiple PA trains
This adds support for synchronizing to multiple PA trains.
2024-11-05 09:09:22 -05:00
Ismael Luceno
cd1c6b666f Fix missing inclusion of <limits.h>
Needed for PATH_MAX.
2024-09-19 13:01:07 -04:00
Bastien Nocera
1d73dc6a1a emulator: Fix integer truncation warnings
Error: INTEGER_OVERFLOW (CWE-190): [#def1] [important]
emulator/amp.c:693:2: cast_overflow: Truncation due to cast operation on "(remain_assoc_len > 248) ? 248 : remain_assoc_len" from 32 to 16 bits.
emulator/amp.c:693:2: overflow_assign: "fragment_len" is assigned from "(remain_assoc_len > 248) ? 248 : remain_assoc_len".
emulator/amp.c:698:2: overflow_sink: "fragment_len", which might have overflowed, is passed to "memcpy(rsp.assoc_fragment, amp->local_assoc + len_so_far, fragment_len)". [Note: The source code implementation of the function has been overridden by a builtin model.]
696|	rsp.phy_handle = cmd->phy_handle;
697|	rsp.remain_assoc_len = cpu_to_le16(remain_assoc_len);
698|->	memcpy(rsp.assoc_fragment, amp->local_assoc + len_so_far,
699|							fragment_len);
700|

Error: INTEGER_OVERFLOW (CWE-190): [#def2] [important]
emulator/amp.c:701:2: cast_overflow: Truncation due to cast operation on "4 + fragment_len" from 32 to 8 bits.
emulator/amp.c:701:2: overflow_sink: "4 + fragment_len", which might have overflowed, is passed to "cmd_complete(amp, 5130, &rsp, 4 + fragment_len)".
699|							fragment_len);
700|
701|->	cmd_complete(amp, BT_HCI_CMD_READ_LOCAL_AMP_ASSOC,
702|						&rsp, 4 + fragment_len);
703|   }

Error: INTEGER_OVERFLOW (CWE-190): [#def4] [important]
emulator/bthost.c:3309:3: cast_overflow: Truncation due to cast operation on "len - offset" from 32 to 8 bits.
emulator/bthost.c:3309:3: overflow_assign: "cp->data_len" is assigned from "len - offset".
emulator/bthost.c:3317:2: overflow_sink: "cp->data_len", which might have overflowed, is passed to "memcpy(cp->data, data + offset, cp->data_len)". [Note: The source code implementation of the function has been overridden by a builtin model.]
3315|		}
3316|
3317|->		memcpy(cp->data, data + offset, cp->data_len);
3318|
3319|		send_command(bthost, BT_HCI_CMD_LE_SET_PA_DATA, buf,
2024-07-08 10:20:02 -04:00
Luiz Augusto von Dentz
b6f8c0024a bthost: Introduce bthost_add_l2cap_server_custom
This introduces bthost_add_l2cap_server_custom which can be used to
define custom values for MTU, MPS and credits.
2024-06-26 15:39:31 -04:00
Pauli Virtanen
9b21ce6f79 bthost: handle client L2CAP conn in LE credit based mode
Allow bthost hooks to receive data from L2CAP LE credit based
connections. Handle LE credit header when receiving, and reassemble
received SDU.

Handle L2CAP LE credit header also in bthost_send_cid.
2024-04-04 14:46:34 -04:00
Pauli Virtanen
7158101e6c btdev: set nonzero SCO mtu & max pkt
Set nonzero max pkt count, so that kernel can transmit data.
The request & accept/reject flow is not emulated yet.
2024-04-04 14:46:33 -04:00
Luiz Augusto von Dentz
f74f6a5cfd btdev: Fix framenting PA reports
PA reports can carry up to 247 bytes of data instead of just 31 bytes:

> HCI Event: LE Meta Event (0x3e) plen 46
      LE Periodic Advertising Report (0x0f)
        Sync handle: 1
        TX power: 127 dbm (0x7f)
        RSSI: not available (0x7f)
        CTE Type: No Constant Tone Extension (0xff)
        Data status: Complete
        Data length: 0x26
        25 16 51 18 28 00 00 01 01 06 00 00 00 00 11 02  %.Q.(...........
        01 03 02 02 01 05 03 01 00 00 00 03 04 28 00 04  .............(..
        03 02 02 00 01 00
2024-04-02 16:28:48 -04:00
Luiz Augusto von Dentz
8c8ac6f13c bthost: Add bthost_set_base
This adds bthost_set_base which takes create of creating the PA data
using bt_ad to add BAA service data and then generating the PA data
which is then passed to bthost_set_pa_data.
2024-04-02 16:28:48 -04:00
Iulia Tanasescu
32a3591b70 bthost: Add support for Set PA data
This adds bthost_set_pa_data.
2024-04-02 15:56:42 -04:00
Luiz Augusto von Dentz
ade2b97597 btdev: Fix not resetting big_handle on HCI_Reset
HCI_Reset shall cleanup BIG state including if the handle has been set.
2024-02-27 11:45:53 -05:00
Luiz Augusto von Dentz
f65cd924f2 btdev: Fix UAF on page_timeout
When the page_timeout is run perhaps the btdev had been freed already so
it needs to be checked if it still valid.
2024-02-26 09:53:46 -05:00
Luiz Augusto von Dentz
f3c35ed835 btdev: Fix crash on page_timeout
If remote device cannot be found after page_timeout then the code shall
not proceed to call pending_conn_del.
2024-02-15 09:30:56 -05:00
Iulia Tanasescu
7c49568a27 btdev: Fix set PA data array overflow
This fixes an array overflow that can happen if the user issues the
LE Set Periodic Advertising Data command with data length exceeding
31 bytes.

The PA data set by the user is copied in an array of fixed length
(31 bytes). However, the data length might exceed 31 bytes. This will
cause an array overflow when the PA data is later processed (for
instance, when sending PA reports).

According to specification, the data length provided at LE Set Periodic
Advertising Data command can be maximum 252 bytes. The stored data len
should also be true to the length copied in the array.
2024-02-13 15:27:27 -05:00
Jonas Dreßler
d5c1c0e80f emulator/btdev: Send page timeout after 5.12 secs delay
Real bluetooth adapters wouldn't send the page timeout immediately
when trying to page a device, instead it would take a few seconds.

Try to behave more realistically in the emulator and send the page
timeout after 5.12 seconds, which is the default page timeout.
2024-02-06 16:57:03 -05:00
Luiz Augusto von Dentz
de8c249f78 btdev: Fix crash on BIG Sync Terminate command
When receiving BIG Sync Terminate command the following crash could be
observed:

Invalid read of size 8
   at 0x15FC9F: cmd_big_term_sync (btdev.c:6417)
   by 0x15D19D: run_cmd (btdev.c:7269)
   by 0x16E7DF: process_cmd (btdev.c:7425)
   by 0x16E7DF: btdev_receive_h4 (btdev.c:7516)
   by 0x143AB9: vhci_read_callback (vhci.c:77)
   by 0x185AAE: io_callback (io-mainloop.c:110)
   by 0x187158: mainloop_run (mainloop.c:106)
   by 0x188357: mainloop_run_with_signal (mainloop-notify.c:188)
   by 0x4EA6B89: (below main) (in /usr/lib64/libc.so.6)
 Address 0x53cf678 is 8 bytes inside a block of size 16 free'd
   at 0x4845B2C: free (vg_replace_malloc.c:985)
   by 0x17E0FE: queue_remove (queue.c:256)
   by 0x1497F0: conn_remove (btdev.c:537)
   by 0x15FC8A: cmd_big_term_sync (btdev.c:6427)
   by 0x15D19D: run_cmd (btdev.c:7269)
   by 0x16E7DF: process_cmd (btdev.c:7425)
   by 0x16E7DF: btdev_receive_h4 (btdev.c:7516)
   by 0x143AB9: vhci_read_callback (vhci.c:77)
   by 0x185AAE: io_callback (io-mainloop.c:110)
   by 0x187158: mainloop_run (mainloop.c:106)
   by 0x188357: mainloop_run_with_signal (mainloop-notify.c:188)
   by 0x4EA6B89: (below main) (in /usr/lib64/libc.so.6)
2024-02-02 10:10:43 -05:00
Luiz Augusto von Dentz
460dd92431 btdev: Fix not clearing le_pa_sync_handle on reset
le_pa_sync_handle needs to be clear on reset otherwise the coce won't
be able to handle Create PA Sync commands.
2024-01-25 09:19:53 -05:00
Emil Velikov
4d88fd2fab emulator: const annotate rfcomm_crc_table[] 2024-01-19 15:44:09 -05:00
Iulia Tanasescu
b94e33f90a hciemu: Add support for setting emulator bdaddr
This adds support for the user to explicitly set a desired bdaddr to
a client emulator.
2023-10-05 12:16:54 -07:00
Arkadiusz Bokowy
a2d47ef052 vhci: Check whether vhci open setup succeeded
Due to race condition in the vhci kernel driver, we might read not a
vendor response packet, but a HCI reset command. This extra check will
ensure that kernel driver behaves correctly. Otherwise, the HCI setup
process will fail, because our controller will not respond to "missing"
HCI reset command. In result the virtual HCI will be DOWN and without
initialized Bluetooth address, e.g:

> hciconfig
hci2:   Type: Primary  Bus: Virtual
        BD Address: 00:AA:01:01:00:02  ACL MTU: 192:1  SCO MTU: 0:0
        UP RUNNING
        RX bytes:0 acl:0 sco:0 events:66 errors:0
        TX bytes:3086 acl:0 sco:0 commands:66 errors:0

hci1:   Type: Primary  Bus: Virtual
        BD Address: 00:00:00:00:00:00  ACL MTU: 0:0  SCO MTU: 0:0
        DOWN
        RX bytes:0 acl:0 sco:0 events:0 errors:0
        TX bytes:8 acl:0 sco:0 commands:1 errors:0

> dmesg
[1754256.640122] Bluetooth: MGMT ver 1.22
[1754263.023806] Bluetooth: MGMT ver 1.22
[1754265.043775] Bluetooth: hci1: Opcode 0x c03 failed: -110
2023-09-20 13:24:57 -07:00
Arkadiusz Bokowy
7f788a2c51 hciemu: Call btdev_receive_h4 unconditionally
Calling btdev_receive_h4 without prior logic will allow this function
to print some debug logs. E.g.: in case when the received packet type
is not supported.

> Bluetooth emulator ver 5.66
> vhci0: > ff 00 01 00                                      ....
> vhci0: Unsupported packet 0xff
2023-09-19 12:15:34 -07:00
Arkadiusz Bokowy
eb9eaf01d1 emulator: Skip new line when using util_debug
The debug callback vhci_debug() already prints new line after each debug
string. Explicit new line in the util_debug call causes double new line
in the output.
2023-09-19 12:15:34 -07:00
Pauli Virtanen
bfcc3f7bf4 hciemu: Add hciemu_flush_client_events for ordering VHCI vs bthost
bthost commands execute with a delay because events may be sitting in
the socketpair queues.  This makes it difficult to ensure, when writing
tests, that certain bthost commands have been handled, before any new
events from VHCI are processed.

To make event ordering possible, add a function that flushes client
bthost/btdev socketpair queues, before processing any new VHCI events.
This is done by pausing VHCI input processing until the kernel
socketpair queues are empty.
2023-08-21 11:24:45 -07:00
Pauli Virtanen
dbe5220054 vhci: Add function to pause processing input from vhci
Add function to pause (temporarily) reading data from vhci.  This can be
used to synchronously pause handling of btdev commands from the kernel.
2023-08-21 11:24:45 -07:00
Pauli Virtanen
459b095c66 btdev: fix CIG ID on Set CIG Parameters error response
Set CIG Parameters shall return correct CIG ID in Command_Complete also
when it errors.
2023-08-07 16:19:48 -07:00
Pauli Virtanen
23a7d40030 btdev: fix Command Status command opcodes for Setup Sync Conn
The command opcode should be the CMD, not EVT.
2023-08-02 14:00:38 -07:00
Pauli Virtanen
5815a92423 btdev: check error conditions for HCI_Create_Connection_Cancel
Create Connection Cancel shall return Command Complete with error status
when there is no Create Connection that can be canceled.  In these
cases, we should not send a (spurious) Connection Complete event.

Fix by keeping a list of pending Create Connection commands, and
returning command errors if there is none pending at the moment.
2023-08-02 14:00:38 -07:00
Luiz Augusto von Dentz
92bb384f5f bthost: Add support to set ISO Packet Status
This adds support to set ISO Packet Status to bthost_send_iso.
2023-07-14 11:12:39 -07:00
Luiz Augusto von Dentz
3f018d52d4 btdev: Fix CIS Established Event parameters
ISO Interval and Transport Latency were inverted.
2023-06-23 14:16:02 -07:00
Luiz Augusto von Dentz
97a07d8485 btdev: Fix LE CIS Established Event
LE CIS Established Event Transport Latency shall follow the formula from
the spec:

 BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 6, Part G
 page 3050:

 Transport_Latency_C_To_P = CIG_Sync_Delay + FT_C_To_P ×
 ISO_Interval - SDU_Interval_C_To_P
 Transport_Latency_P_To_C = CIG_Sync_Delay + FT_P_To_C ×
 ISO_Interval - SDU_Interval_P_To_C
2023-06-22 12:06:17 -07:00
Luiz Augusto von Dentz
2f3479337f btdev: Fix CIS Establish ISO Interval
ISO Interval is actually using 1.25 ms slots so it needs to be properly
converted.
2023-06-21 14:35:04 -07:00
Luiz Augusto von Dentz
2f52afa21a btdev: Fix build error
This fixes the following build error:

emulator/btdev.c: In function ‘le_cis_estabilished’:
emulator/btdev.c:5947:22: error: ‘cis_idx’ may be used
uninitialized [-Werror=maybe-uninitialized]
 5947 |         int cig_idx, cis_idx;
      |                      ^~~~~~~
2023-06-13 13:31:13 -07:00
Iulia Tanasescu
97edc78bd8 btdev: Support multiple BIS
This adds support for creating a BIG with multiple BISes.
2023-05-30 11:34:19 -07:00
Pauli Virtanen
d214fe5f75 btdev: check LE Create CIS error conditions
Check LE Create CIS input parameter are valid and return correct status
codes (Core v5.3 Vol 4 Part E Sec. 7.8.99).

On current bluetooth-next kernel, this results to

ISO AC 6(i) - Success                                Failed
ISO AC 7(i) - Success                                Failed
ISO AC 8(i) - Success                                Failed
ISO AC 9(i) - Success                                Failed
ISO AC 11(i) - Success                               Failed

as in these tests the kernel is sending new Create CIS commands before
it has seen all events from the previous, which is not allowed:

< HCI Command: LE Create Co.. (0x08|0x0064) plen 9  #129 [hci0]
        Number of CIS: 2
        CIS Handle: 257
        ACL Handle: 42
        CIS Handle: 258
        ACL Handle: 42
> HCI Event: Command Status (0x0f) plen 4           #130 [hci0]
      LE Create Connected Isochronous Stream (0x08|0x0064) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 29           #131 [hci0]
      LE Connected Isochronous Stream Established (0x19)
        Status: Success (0x00)
        Connection Handle: 257
        ...
< HCI Command: LE Setup Is.. (0x08|0x006e) plen 13  #132 [hci0]
        ...
> HCI Event: Command Complete (0x0e) plen 6         #133 [hci0]
      LE Setup Isochronous Data Path (0x08|0x006e) ncmd 1
        ...
< HCI Command: LE Create Co.. (0x08|0x0064) plen 5  #134 [hci0]
        Number of CIS: 1
        CIS Handle: 258
        ACL Handle: 42
> HCI Event: Command Status (0x0f) plen 4           #135 [hci0]
      LE Create Connected Isochronous Stream (0x08|0x0064) ncmd 1
        Status: ACL Connection Already Exists (0x0b)
> HCI Event: LE Meta Event (0x3e) plen 29           #136 [hci0]
      LE Connected Isochronous Stream Established (0x19)
        Status: Success (0x00)
        Connection Handle: 258
        ...

The emulator uses Already Exists error code here, not Command
Disallowed, since the Established events are logically generated
immediately after the first status event, even though the kernel hasn't
yet processed them.
2023-05-30 11:18:13 -07:00
Pauli Virtanen
a8b927e347 btdev: fix inactive CIG configurable status
CIG shall not be configurable after the first Create CIS until Remove
CIG is issued (Core v5.3 Vol 6 Part B Sec. 4.5.14.3).  We currently have
it configurable in the inactive state (Create CIS done and all CIS
closed), which is incorrect.

Track CIG state and allow reconfigure only in nonexistent/configured
state, i.e., when no CIS have been created yet.
2023-05-30 11:18:13 -07:00
Pauli Virtanen
0c32cfdf94 btdev: report right reason for local Disconnect complete
Disconnect initiated by local host should get Connection Terminated by
Local Host (0x16) as termination reason.
2023-05-22 11:56:28 -07:00
Pauli Virtanen
678265f37c btdev: support multiple CIG
Add support for more than one CIG simultaneously.
2023-05-22 11:56:27 -07:00
Iulia Tanasescu
b56479f4f5 tools/iso-tester: Add Broadcast tests for encrypted BIG
This adds the following tests for encrypted broadcast:

ISO Broadcaster Encrypted - Success
ISO Broadcaster Receiver Encrypted - Success
2023-04-19 13:46:00 -07:00
Luiz Augusto von Dentz
57b67987d4 btdev: Fix not setting CIS parameters properly
The code was assuming only index 0 was to be used which doesn't work
when there are multiple CIS being programmed with different parameters.
2023-04-12 17:39:03 -07:00
Iulia Tanasescu
c4d9b99db5 Split bt_iso_qos into dedicated structures
Split bt_iso_qos into dedicated unicast and broadcast
structures and add additional broadcast parameters.
2023-03-31 14:33:28 -07:00
Manish Mandlik
8bd2f29617 vhci: Add support to trigger devcoredump and read the dump file
Add vhci support to trigger the hci devcoredump by writing to
force_devcoredump debugfs entry and read the generated devcoredump
file.
2023-03-30 10:46:21 -07:00
Pauli Virtanen
80a45cb6a2 bthost: Allow sending ISO packets with sequence number and timestamp
Change bthost_send_iso to take packet sequence number and timestamp, and
allow it to send timestamped HCI ISO data packets.

Currently, btdev passes through ISO packets, so this can also be used to
test RX timestamping.
2023-02-27 11:24:57 -08:00
Luiz Augusto von Dentz
33b84917ee bthost: Add callback to accept ISO connections
This enables setting an accept callback which can return reject
reason if the connection shall not be accepted.
2022-12-06 13:31:58 -08:00
Luiz Augusto von Dentz
c2bf151374 vhci: Add function to interact with force_static_address
This adds functions that can be used to set debugfs
force_static_address.
2022-10-10 13:06:16 -07:00
Luiz Augusto von Dentz
c94cdbc6d7 btdev: Add support for setting bdaddr
This adds btdev_set_bdaddr so it is possible to set an arbritrary
address.
2022-10-10 13:06:16 -07:00
Luiz Augusto von Dentz
5bf220eb3b bthost: Add destroy callback to bthost_add_iso_hook
This adds a destroy callback to bthost_add_iso_hook so its user can
detect when the hook is freed when the connection is disconnected.
2022-08-22 14:59:26 -07:00
Luiz Augusto von Dentz
bbbd0512c9 bthost: Add qos support to bthost_set_cig_params
This enables setting QoS other then the mandatory 16_2_1.
2022-08-05 15:50:56 -07:00
Luiz Augusto von Dentz
163de8e231 btdev: Fix not checking if a CIG has any active CIS
Bluetooth Core specification says the CIG must be in configurable state
in order to accept the SetCIGParameters:

BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2553:

 'If the Host issues this command when the CIG is not in the
  configurable state, the Controller shall return the error
  code Command Disallowed (0x0C).'
2022-07-11 09:34:45 -07:00
Luiz Augusto von Dentz
c75ff36b77 btdev: Fix not removing connection and advertising set on reset
This makes sure that all connections and advertising sets are cleanup
on reset.
2022-05-02 14:06:12 -07:00
Luiz Augusto von Dentz
03602479bd btdev: Fix CIS Established
CIS Established was using the ISO latency instead of SDU interval for
transport latency.
2022-04-14 17:05:23 -07:00