Commit graph

1495 commits

Author SHA1 Message Date
Ulf Magnusson 0618cf00e8 Bluetooth: host: kconfig: Fix broken ref. to BT_CTLR_TX_BUFFER_SIZE
The CONFIG_ prefix was missing.

Found with a work-in-progress scripts/kconfig/lint.py check.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2020-01-25 08:15:34 -05:00
Joakim Andersson bf33338652 Bluetooth: host: Fix net buf assert on att encrypt change event
Fix assert in net_buf triggered on att encrypt change event.

	ASSERTION FAIL [net_buf_simple_headroom(buf) >= len] @
	  ZEPHYR_BASE/subsys/net/buf.c:881

This happens because when the att request was allocated, it was not
properly initialized and req->retrying was left as true.
This caused the att encrypt change handling to assume an att request
needed to be resent, starting resending with an invalid request and
request buffer.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-23 18:08:24 +02:00
Joakim Andersson 813e5bdd7a Bluetooth: host: Refactor use of #ifdef to IS_ENABLED()
Refactor use of #ifdef to IS_ENABLED() pattern for handling cancellation
of outgoing connection attempt. Reduce the amount of indentation by
combining outer if-statements.
Move handling of canceled create connection into a helper function.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 64b890463d Bluetooth: host: Fix advertiser with identity switches to RPA
Fix advertiser requested to use the identity address while privacy
feature is enabled will change to using RPA address when advertise is
resumed or when RPA timeout occurred.
RPA timeout does not need to run when advertiser is using identity.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson fbe3285bfa Bluetooth: host: Handle initiator at RPA timeout
Handle initiator role when RPA timeout expires. For direct connect
establishment procedure we make sure the RPA is refreshed when starting
initiator and limit the timeout to the RPA timeout.
For auto establishment procedure we cancel the initiator and restart it
again in the connection complete event that is generated when canceling
an initiator.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 99662c63bc Bluetooth: host: Handle starting roles with different random address
Handle starting of advertiser and scanner or initiator when advertiser
is using a different identity than the default identity to generate the
random resolvable address in the controller.
We need to handle this only for the privacy case because the random
address is set in the RPA timeout handler and not from the API.
When privacy is disabled we can return error code from the LE Set Random
Address HCI command instead.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 4876a8f39a Bluetooth: host: Handle scanner active at RPA timeout
Fix RPA timeout handling when the scanner is active. An active scanner
must be restarted at RPA timeout otherwise the Set Random Address
command will fail.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 137f704064 Bluetooth: host: Refactor initiator to common code and initiator state
Refactor the handling of sending the LE Create Connection command and
for whitelist and direct initiator to use the same host state flag
and common handling of the privacy address.
Also simplify the way we check if the procedure has already been started
when application tries to start it again.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 6c1f52dff7 Bluetooth: host: Set conn state before sending HCI command
Handle a possible race condition in the host connection state.
Set the conn state of the connection object before command is sent. This
is in case the calling function is not scheduled again before the
connection complete event arrives. In this case find_pending_conn will
not find the connection object.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson cc6b8c3a6f Bluetooth: host: Ifdef starting of background scanner in init
Starting a background scanner for auto-connection is an API that is only
available when whitelist API is not enabled.
There is currently no way to set this bit when the whitelist API is
enabled so there is not any issues with the current code, but it is
still not correct.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Joakim Andersson 0861c8c834 Bluetooth: host: Disconnect connection if no conn object is available
When receiving a connection complete event but no connection object are
available in the host something strange has happened. In this case
the controller might have a connection that cannot be controlled by the
application. It would then be sensible to disconnect this connection in
the controller.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-20 18:58:02 +02:00
Carles Cufi 6cf7ac77bf Bluetooth: Always use full path to hci_driver.h
The path to include/drivers should not be in the compiler include path
list, only include/. In order to make this possible, always explictly
refer to hci_driver.h via the drivers/bluetooth/ path and not only
bluetooth/.

Fixes #21974.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2020-01-16 19:20:39 -05:00
Wolfgang Puffitsch dbdfd2995b Bluetooth: host: Guard calls to bt_conn functions
Guard calls to bt_conn functions in bt_le_adv_start_internal with
IS_ENABLED(CONFIG_BT_PERIPHERAL) to avoid undefined symbols in builds
that do not support that role.

Signed-off-by: Wolfgang Puffitsch <wopu@demant.com>
2020-01-16 08:53:21 +01:00
Joakim Andersson ca2d3f0a89 Bluetooth: Kconfig: Fix error in buffer pool sizes
Fix error in calculation of the minimum discardable buffer size. For
the LE Advertising Report the maximum payload is 31 bytes plus
additional data in the event gives an event size of 41 bytes (given that
num_reports is 1). Since this is a meta event we need to include the
sub-event code, plus the event header of 2 bytes. Total of 44 bytes.

This is a regression from afa9c42d75 where we forgot th 1 byte for the
RSSI that is appended after the data. Easy to miss since it is not part
of the struct.

Fix error in calculation of the num complete buffer size. Here we forgot
to include the 2 byte event header.

This is a regression from 89981b07c8.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 21:19:17 +01:00
Joakim Andersson 4401b6a2f3 Bluetooth: host: Stop using existing conn object when creating new conn
Remove re-using connection objects in disconnected state when creating
directed advertiser or establishing a connection as a central using
direct connection procedure.
This makes the API mores consistent it terms of which connection roles
can be started from the disconnected callback.
This also avoids a central connection object being re-used for a
connection as a peripheral instead and vice versa.

When attempting to create a new connection the API would returning
a valid connection object if there is already an existing connection
object.
This existing connection object could be either in the process of
establishing the connection or already connected.
Returning the connection object in this would give the false impression
that the stack has initiated connection procedure, when in fact it just
returned an existing connection object.

The application has the ability to check for existing connection objects
using the bt_conn_lookup_addr_le API.

Add warning plus comment possible scenarios why the a valid connection
object might exists. Most important is to explain why a valid connection
object exists during the disconnected callback.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson 46bf20036a Bluetooth: host: Reserve conn object for connectable advertiser
Reserve conn object for undirected connectable advertiser. This means we
won't have a situation where we start a connectable advertise but will
fail to allocate a connection object for it in the connection complete
event.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson e9eebf0c40 Bluetooth: host: Reserve conn object for connecting with whitelist
Reserve a connection object when starting the auto-initiator using the
controller whitelist.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson e91f9804e6 Bluetooth: host: Fix error code when calling API before init
Fix bt_conn_create_aute_le returning the wrong error code when bt_init
has not been called yet. This is inconsistent with the rest of the API
functions.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson c635425487 Bluetooth: host: Refactor stopping directed advertiser
Refactor stopping directed advertiser to disconnect the state object
when calling advertise stop. This follows the same pattern as
bt_conn_disconnect.

Remove returning conn objects in BT_CONN_CONNECT state, this state could
only  be an initiator starting a connection in the central role.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson a45226a238 Bluetooth: host: Clear auto-conn state when failed to get conn object
Make sure that the auto-conn state is cleared correctly when we might
fail to allocate a new connection object.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson a0349689ff Bluetooth: host: Fix conn object assigned to wrong connection
Fix conn object assigned to the wrong controller connection in the
connection complete handler. This could happen when running a
directed advertiser and establishing a connection at the same time to
the same peer.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-14 14:27:31 +01:00
Joakim Andersson 89981b07c8 Bluetooth: host: Lower the size of the num_complete buffer
Set the size of the num complete buffer to the maximum possible size it
can be.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-13 17:59:00 +01:00
Joakim Andersson afa9c42d75 Bluetooth: host: Add config option for discardable buffer size.
Add option to set the size of the discardable buffer pool. This saves
memory for the MESH use case where we expect a large number of advertise
reports.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-13 17:59:00 +01:00
Trond Einar Snekvik 5b14ab4481 Bluetooth: Add dynamic le scan listener interface
Adds a way to register listeners for incoming scanner packets, in
addition to the callback passed in bt_le_scan_enable.

This allows application modules to add multiple scan packet listeners
without owning the scanner life cycle API, enabling use cases like
beacon scanning alongside Bluetooth Mesh.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2020-01-13 14:27:01 +01:00
Kim Sekkelund 9ea6e72bae Bluetooth: host: cfg_write callback to return error code
Current implementation of application's cfg_write callback only has the
possibility of returning boolean status, which in case of failure only
allows for one error code; BT_ATT_ERR_WRITE_NOT_PERMITTED.
This change makes the application able to add own security check on
characteristic subscription in the cfg_write callback and report a more
relevant error code (e.g. BT_ATT_ERR_AUTHORIZATION).

Signed-off-by: Kim Sekkelund <ksek@oticon.com>
2020-01-10 17:41:15 +01:00
Joakim Andersson c336539249 Bluetooth: host: Remove toggling advertiser on advertise data update
Remove toggling the advertise enable state when the advertiser name has
been updated. Advertise and scan response data should be used by the
controller on subsequent advertising events.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-10 17:10:46 +01:00
Luiz Augusto von Dentz 708c8bae54 Bluetooth: ATT: Use k_mem_slab for connection context
This uses k_mem_slab APIs to allocate/free ATT context instead of custom
array.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-01-09 19:20:21 +01:00
Luiz Augusto von Dentz 3be9980bd6 Bluetooth: GATT: Remove _peer member from bt_gatt_subscribe_params
This further reduce the overhead on each subscription at expense of
having a dedicated array to store subscriptions, the code now maintain
a separate list for each peer which should also scale better with large
number of subscriptions to different peers.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-01-09 19:20:21 +01:00
Luiz Augusto von Dentz d278cdc3d7 Bluetooth: GATT: Allocate request from a memory slab
This should reduce the footprint on applications that do a lot of
requests i.e have a lot of subscriptions.

Fixes #21103

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-01-09 19:20:21 +01:00
Marco Sterbik 9217c6634e bluetooth: fix removal of needed subscriptions
Providing 'tmp', which was never updated, resulted in removeal of
subscriptions from the beginning.
Using the updated 'prev' resolves this.

Signed-off-by: Marco Sterbik <madbadmax00@gmail.com>
2020-01-07 17:01:25 +01:00
Joakim Andersson 57addad856 Bluetooth: host: Use direct connection if not host resolving list
Start initiator immediately instead of scanning for device first.
If the host resolving list is used we need to go via scanner to resolve
the address.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2020-01-07 16:04:03 +01:00
Jacob Siverskog 6eef6cd946 Bluetooth: host: Rethink how unpair all works
Instead of having all (=addr NULL or BT_ADDR_LE_ANY to bt_unpair) as a
special case, iterate over all connected peers and unpair them the
regular way. This means bt_gatt_clear is called too. Doing this way
allows us to remove a lot of (now) unused code as well.

Signed-off-by: Jacob Siverskog <jacob@teenage.engineering>
2020-01-04 14:10:46 +01:00
ZhongYao Luo a94e81ba3e Bluetooth: RFCOMM: Fix length errors when payload exceeds 127
hdr->length is the length of the payload, it should be
buf->len - sizeof(*hdr) - 1 or buf->len - (sizeof(*hdr) + 1)

Signed-off-by: ZhongYao Luo <LuoZhongYao@gmail.com>
2020-01-03 10:14:59 +01:00
Luiz Augusto von Dentz 2cfef8cd77 Bluetooth: L2CAP: Fix using s16_t to represent credits
Credits are 2 octects long so an s16_t positive portion can only half to
the theorical maximum number of credits, so instead this uses u16_t and
do a bound check instead of checking for negative values.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-01-02 16:53:27 +01:00
Luiz Augusto von Dentz 4ff711d8a5 Bluetooth: L2CAP: Remove use of k_sem for credits
With the changes that introduced a queue k_sem is only used with
K_NO_WAIT which means it is no longer possible to wait/block for credits
so the usage of k_sem is no longer needed and can be safely replaced
with atomic_t just to count the available credits at a given instant.

Fixes #19922

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2020-01-02 16:53:27 +01:00
Sverre Storvold 649a813bf6 Bluetooth: host: Add calling of read_remote_version
Make remote features and remote version accesible to the application
through the bt_conn_get_remote_info object. The host will auto initiate
the procedures. If the procedures have not finished with the application
calls bt_conn_get_remote_info then EBUSY will be returned.
The procedures should finish during the first 10 connection intervals.

Signed-off-by: Sverre Storvold <Sverre.Storvold@nordicsemi.no>
Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-23 14:47:31 +02:00
Joakim Andersson 32bde4fe78 Bluetooth: host: Refactor host auto initiated LL procedures
Refactor the handling of the host auto initiated LL procedures.
This makes it easier to add new auto initiated procedures as well as
reduced the maintenance by reducing code duplication.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-23 14:47:31 +02:00
Joakim Andersson b69edc3844 Bluetooth: HCI: Check length of VS command complete
Always check that the length of the returned command complete
event for a vendor specific command matches the expected length
when the support for Zephyr VS HCI commands are uncertain.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-20 15:45:34 +01:00
Joakim Andersson a34d4afe7d Bluetooth: Host: Fix issues with host IRK handling
Fix multiple issues related to the way the host handles
Identity Information related to privacy

1. If the controller provided a public address the IRK
   for this identity would be randomly generated but not
   stored persistenly.

2. Fix the handling of the above issue which was fixed
   for the random address but would initiate settings save
   ID on every boot.

3. Fix the host not using the Vendor Specific HCI commands
   related to retrieving the Identity Root (IR) from the
   controller and using the key diversified function d1
   to generate an IRK as specified in the BT Core spec.

Make sure that a Host generated ID is only saved when it is first
generated.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-20 15:45:34 +01:00
Johan Hedberg 847f4e6ae6 Bluetooth: L2CAP: Fix checking for STATUS_OUT
The test_and_set_bit() should be checking if the flag was *not*
already set, since that's the scenario where we want to call the
status callback.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-12-19 11:28:24 +02:00
Johan Hedberg 525889c6b5 Bluetooth: L2CAP: Remove bogus NULL checks
The ch pointer is the result of a CONTAINER_OF() operation, so
checking it for NULL is pointless. Additionally, there's no place that
calls this function with chan set to NULL.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-12-19 11:28:24 +02:00
Johan Hedberg 0e91493ab4 Bluetooth: L2CAP: Make channel ops const
Make the channel ops struct const since there really isn't anything
there that needs to change at runtime. The only exception is the L2CAP
shell which was playing with the recv callback, however that can be
fixed by introducing a simple bool variable.

With tests/bluetooth/shell this reduces RAM consumption by 112 bytes
while adding only 16 bytes to flash consumption.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-12-19 11:28:24 +02:00
Luiz Augusto von Dentz 9a111df676 Bluetooth: L2CAP: Add define for signalling timeout
This adds a define for the so called RTX timeout.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz 1a7f85cb53 Bluetooth: L2CAP: Document cases where -EAGAIN is returned
This documents the special cases where -EAGAIN is returned which leads
the buffer to be queued.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz b8911a8d35 Bluetooth: L2CAP: Fix data leaks when segment cannot be sent
Now that bt_l2cap_send_cb can fail the buffer state needs to be save
and restored otherwise the data stored on it would be lost.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz bdc1eff35b Bluetooth: L2CAP: Introduce BT_L2CAP_STATUS_SHUTDOWN flag
This introduces BT_L2CAP_STATUS_SHUTDOWN which is used to indicate when
a channel has been shutdown.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz cf84216b1a Bluetooth: L2CAP: Offload processing of tx_queue to a work
This offloads the processing of tx_queue to a work so the callbacks
calling resume don't start sending packets directly which can cause
stack overflow.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz 99066db21d Bluetooth: L2CAP: Ignore packets received while disconnecting
Drop packets received while disconnecting since they would most likely
be flushed once peer respond there is no gain in keeping them on a
queue.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz 5d26693d02 Bluetooth: L2CAP: Add dedicated pool for disconnect request
This prevents disconnect request packets to not being sent due to lack
of buffers normally caused by flooding or congestion.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz aa9a1f627c Bluetooth: Use NET_BUF_RX_COUNT if NET_L2_BT is enabled
If NET_L2_BT is enabled we need enough acl_in_pool needs to be big
enough to contain a full IP packet since that is no longer processed by
RX thread buffer would be queued to syswq to reassemble the SDU.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz dd4b340205 Bluetooth: L2CAP: Use NET_BUF_TX_COUNT as fragment count
When NET_L2_BT the memory pressure for fragments can be quite high
since that would be transfering IP packets which are considerable big
so this makes our frag_pool to be of the same size as NET_BUF_TX_COUNT.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz 4b8cd200ed Bluetooth: conn: Add debug variant to buffer allocation
This enable chaning the function and line number making it easier to
debug where a buffer allocation is blocking.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Luiz Augusto von Dentz bdb8b0af70 Bluetooth: L2CAP: Queue packets when a segment could not be allocated
When a segment could not be allocated it should be possible to resume
sending it later once previous segments complete, the only exception is
when there is no previous activity and we are unable to alocate even the
very first segment which should indicate to the caller that it would
block since that only happens on syswq the caller might need to defer to
another thread or resubmit the work.

Fixes #20640

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-13 15:30:28 +02:00
Wolfgang Puffitsch b0207eb778 Bluetooth: host: Refactor checks in update_ccc for efficiency
Check peer address only if active.

Signed-off-by: Wolfgang Puffitsch <wopu@demant.com>
2019-12-12 15:26:38 +02:00
Joakim Andersson f7f63c6306 Bluetooth: host: Check for device ready for whitelist API
The whitelist API uses the controller directly through HCI commands.
Bluetooth device must have been initialized before sending HCI commands.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-12 12:19:40 +02:00
Joakim Andersson a1d73acfcd Bluetooth: HCI: Handle invalid ACL flags.
Handle invalid ACL flags in HCI transport.
Only Point to Point is supported over HCI in both directions.
Fix flushable start HCI ACL packets not allowed on LE-U connections
from Host to controller.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-11 12:34:34 +02:00
ZhongYao Luo d1aa80c049 bluetooth: l2cap_br: l2cap channel used wrong cid when removing
In bt_l2cap_br_chan, rx.cid is the local cid and tx.cid is the
remote cid. According to Core-5.0 Vol3.Part A 4.6-4.7,
l2cap_br_remove_tx_cid should be searched using tx.cid

Signed-off-by: ZhongYao Luo <LuoZhongYao@gmail.com>
2019-12-11 11:34:43 +02:00
Joakim Andersson e4136178ed Bluetooth: Host: Fix duplicate whitelist entries issue
If the whitelist already exists in the controller then the controller
should not add the device tot the whitelist and should return success.
In that case the counting of entries in the whitelist in the host will
be wrong.

Remove all whitelist counting in the host, and instead rely on the error
reported by the controller for this.
The controller should return error if the whitelist is full.
The controller should return error if use of whitelist was requested but
the whitelist was empty.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-10 17:54:10 +02:00
Joakim Andersson b30b480c7c Bluetooth: UUID: Expose bt_uuid_to_str to application
Expose the bt_uuid_to_str function as an API to the application.
This aligns this function with the bt_addr_to_str function call. This
allows the application to use this function without having to enable
the BT_DEBUG option.

Move the in-place bt_uuid_str to internal logging, this is mainly done
due to the limitation in the log_strdup that shouldn't be exposed to the
application.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-09 17:15:05 +02:00
Ulf Magnusson 87e917a925 kconfig: Remove redundant 'default n' and 'prompt' properties
Bool symbols implicitly default to 'n'.

A 'default n' can make sense e.g. in a Kconfig.defconfig file, if you
want to override a 'default y' on the base definition of the symbol. It
isn't used like that on any of these symbols though.

Also replace some

    config
    	prompt "foo"
    	bool/int

with the more common shorthand

    config
    	bool/int "foo"

See the 'Style recommendations and shorthands' section in
https://docs.zephyrproject.org/latest/guides/kconfig/index.html.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-12-09 16:14:50 +01:00
Joakim Andersson cbf666ecc3 Bluetooth: Host: Check that bluetooth device is ready
Check that the bluetooth device has in fact been initialized before
continuing with public API calls. This could lead to crashes when using
state that has not yet been initialized.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-09 16:11:19 +01:00
François Delawarde db106a2b5b bluetooth: host: Add flag to prevent client resubscription on reconnect
When set, the BT_GATT_SUBSCRIBE_FLAG_NO_RESUB flag indicates that the
subscription should not be renewed when reconnecting with the server.

This is useful if the application layer knows that the GATT server
persists subscription information.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-12-09 17:07:39 +02:00
Martin Rieva bf361aa66c Bluetooth: Host: Add CONFIG_BT_BONDING_REQUIRED flag
Added configuration for accepting pairing requests only if both devices
has bonding flag set in order to reject other devices at an early stage,
thus leaving more chance for devices expected to bond.

With the CONFIG_BT_BONDING_REQUIRED flag the device only accept pairing
requests if it has CONFIG_BT_BONMDABLE set and the device requesting
pairing has Bonding_Flags field set to Bonding (0x01) in its AuthReq.
Note: When using bt_set_bondable(false) pairing requests will be
rejected when CONFIG_BT_BONDING_REQUIRED is set.

Signed-off-by: Martin Rieva <mrrv@demant.com>
2019-12-09 17:05:32 +02:00
Kiran Paramaswaran f30bed350c Bluetooth: Host: Enable/Disable Automatic Sending of Conn Parameter update
Added a new Kconfig flag to enable/disable this feature.

Signed-off-by: Kiran Paramaswaran <kipm@oticon.com>
2019-12-09 17:04:50 +02:00
Joakim Andersson b5f70ef1c0 Bluetooth: GATT: Fix discover callback invalid pointer
Fix issues that surfaced when trying out GCC 9.2, official release name
gcc-arm-none-eabi-9-2019-q4-major, both related to invalid pointers in
GATT when declaring UUIDs in if-scope.

1. Fix the discovery callback giving an invalid pointer in the discovery
callback in two instances.
2. Fix gatt_find_type sending invalid data during discovery procedure.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-09 16:58:01 +02:00
Kiran Paramaswaran d86d37a075 Bluetooth: Host: Adding valid param check in send_conn_le_param_update()
Send connection parameter update request only if it contains the valid
range of values for connection intervals, latency and timeout.

Signed-off-by: Kiran Paramaswaran <kipm@oticon.com>
2019-12-09 16:57:38 +02:00
Jacob Siverskog 7096fa512f Bluetooth: GATT: Remove all subscriptions for connection when unpairing
Make sure all subscriptions are removed when a connection is unpaired.

Fixes #21131

Signed-off-by: Jacob Siverskog <jacob@teenage.engineering>
2019-12-04 19:14:28 +01:00
Joakim Andersson afe088247d Bluetooth: ATT: Fix ATT MTU support for larger MTUs
Fix ATT MTU size of length variables resulting in wrong length values
reported to the user. Communicating with an Android device using an MTU
of 517 resulted in write commands reported as length 2 instead.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-12-04 19:08:10 +01:00
Luiz Augusto von Dentz 5beb5b5992 Bluetooth: ATT: Fix not handling errors properly
Since bt_conn_send_cb can fail to send buffer causing it to unref this
may cause buffer leaks as the caller is not aware of the error assuming
the buffer could be sent.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-12-03 19:44:51 +01:00
Luiz Augusto von Dentz 42c5b0a7fa Bluetooth: L2CAP: Fix unrefing buffers that are queued
Processing of data received on dynamic channels is still done via syswq
so the buffer shall not be unrefed when they are queued.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-11-29 15:19:08 +02:00
Luiz Augusto von Dentz 3f43413887 Bluetooth: L2CAP: Fix not unrefing tx_buf
If the channel has a tx_buf it must be unrefed when destroying the
channel.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-11-29 15:19:08 +02:00
Johan Hedberg 1e831befea Bluetooth: host: Fix ordering of TX sent callbacks
Now that the TX callbacks happen from the system workqueue but fixed
channels get processed from the RX thread there's a risk that the
ordering of these gets messed up. This is particularly bad for ATT
when it's trying to enforce flow control.

To fix the issue store the completed TX packet information in a
per-connection list and process this list before processing any new
packets for the same connection. We still also schedule a workqueue
callback, which will simply do nothing for this list if bt_recv()
already took care of it.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-28 19:55:59 +02:00
Johan Hedberg 39291fbbbe Bluetooth: Remove usage of BT_BUF_USER_DATA_MIN
This define is not of use anymore since there's a global net_buf user
data Kconfig variable and its definition already guarantees a
sufficient minimum for Bluetooth.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-28 13:35:11 +02:00
Johan Hedberg be71e68fcb Bluetooth: L2CAP: Use fixed size for net_buf user data
The "sent" tracking doesn't have to be a signed integer. Use a fixed
size so that the consumed size doesn't change between different
architectures. Use a u16_t since bigger sizes are needed and because
this is mapped to an int function return higher up in the stack.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-28 13:35:11 +02:00
Kamil Piszczek 362f2299cd drivers: bluetooth: hci: rpmsg: fix handling of hci events
Fixed handling of HCI events in the HCI driver over RPMsg. Now,
the driver makes use of discardable buffer pool when allocating
memory for certain HCI event types (e.g. Advertising Report Event).
Applications that are flooded with Advertising Reports will run
much better after this change (e.g. Mesh applications).

Signed-off-by: Kamil Piszczek <Kamil.Piszczek@nordicsemi.no>
2019-11-28 10:55:32 +02:00
Johan Hedberg 0a925bdf9c Bluetooth: L2CAP: Process fixed channels in the RX thread
Now that the TX path and TX context (bt_conn_tx) has been redesigned
to free the contexts always in the system workqueue, it means the
system workqueue is the only context where their allocation may also
fail. This is particularly problematic with us having all L2CAP
channels (fixed & CoC alike) deferred to the system workqueue. It is
especially bad for fixed channels where being able to send responses
for SM, L2CAP signaling and ATT is critical to avoid timeouts for the
connection.

This patch moves the processing of all fixed L2CAP channels back to
the RX thread, thereby making it possible (and safe) to block while
waiting for a TX context to become available.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-27 17:44:59 +02:00
Johan Hedberg a065e5782a Bluetooth: host: Move system workqueue special case to the right place
Now that we've removed the TX allocation dependency from the TX thread
we no longer have the need to do special-casing for the system
workqueue when allocating buffers. Instead, we do have to special-case
the system workqueue when allocating TX contexts since the system
workqueue is the only place where they get freed up.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-27 17:44:59 +02:00
Johan Hedberg d8689cdc72 Bluetooth: host: Fix deadlocks with pending TX packet handling
This is a moderate redesign of the pending TX packet handling that
aims to eliminate potential deadlocks between the TX thread and the
system workqueue thread. The main changes are:

 - TX context (bt_conn_tx) is allocated during buffer allocation, i.e.
   not in the TX thread.

 - We don't allocate a TX context unless there's an associated
   callback. When there's no callback simple integer counters are used
   for tracking.

 - The TX thread is no longer responsible for TX callbacks or
   scheduling of TX callbacks. Instead, the callbacks get directly
   scheduled (k_work_submit) from the RX priority thread.

 - CONFIG_BT_CONN_TX_MAX defaults to CONFIG_BT_L2CAP_TX_BUF_COUNT,
   and in most cases wont need changing. The value now only indicates
   how many pending packets with a callback are possible.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-27 17:44:59 +02:00
Johan Hedberg 28f89478f6 Bluetooth: host: Shrink size of struct bt_conn_tx
The `node` and `work` members are never used simultaneously.
Additionally k_work already has built-in support for being in a linked
list, however a union makes this change a bit cleaner.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-27 17:44:59 +02:00
Johan Hedberg 5a8b143028 Bluetooth: host: Fix command buffer corruption
A recent patch increased struct cmd_data from 8 to 12 bytes, which is
more than the default user data for Bluetooth. We generally don't want
the core stack to require more than 8, so instead of increasing the
requirement, move the data out from the buffer into its own array with
the help of the net_buf_id() API.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-26 16:08:00 +02:00
Joakim Andersson 0bf9931c2c Bluetooth: Host: Fix create connection fails to stop scanner
Fix race condition in bt_conn_create_le for the state of the scanner in
the Host. This leads to the host issuing a create connection command
without stopping the scanner first. This leads to command disallowed and
failing to establish connection. As well as inconsistent state in the
host which does not allow to stop the running scanner.

The race condition exists because the processing of le_adv_report
handler is done before the thread that called bt_conn_create_le was
woken up to continue after the command_complete event.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-11-25 17:21:17 +01:00
Joakim Andersson 113b80ff0d Bluetooth: Host: Fix silent LE conn param update command failure
Fix command status for LE Command Param Update HCI command silently
dropped by the host without notifying the application that this command
has failed. This happens because the host does not wait for the command
status event to check the status code returned.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-11-21 17:21:01 +02:00
Joakim Andersson a961679171 Bluetooth: Host: Fix deadlock calling API functions from SMP callbacks
Fix deadlock in Bluetooth Host. Deadlock could happen from the SMP
callbacks when calling Bluetooth API functions. This is because the
callbacks was given directly from the HCI TX thread. If the calling
API function resulted in trying to send a new HCI command it would post
this HCI command to the HCI TX thread and then wait for command complete
event. This would result in the HCI TX thread blocked waiting for the
itself to process the command.

Example:
Calling bt_conn_le_conn_param_update from pairing_complete callback.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-11-21 14:51:50 +02:00
Johan Hedberg d67416321e Bluetooth: hci_raw: Add support for specifying buffer headroom
The HCI transport implemented by an application using the HCI raw
interface may have its own buffer headroom requirements. Currently the
available headroom gets completely determined by the selected HCI
driver. E.g. most of the time this is the native controller driver
which doesn't reserve any headroom at all.

To cover for the needs of HCI raw users, add a new Kconfig variable
for the apps to set to whatever they need. Correspondingly, use the
maximum of the HCI driver and HCI raw headroom requirements for the
buffer pool definitions and the headroom initializations.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-20 19:29:06 +01:00
Joakim Andersson ea7a1859af Bluetooth: GATT: Fix taking address of packed member of struct sc_data.
Fix GCC9 warning "warning: taking address of packed member of
'struct sc_data' may result in an unaligned pointer value"
Issue is that the on-air structure of sc_data was re-used for the gatt
service changed data.

Added build assert because data is stored in settings, so the structure
should be the same size to be compatible.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-11-18 13:54:15 +01:00
Sebastian Bøe 59abaf461b cmake: make the BT_HCI_TX_STACK_SIZE's prompt conditional
The BT_HCI_TX_STACK_SIZE is carefully calculated from other
options. When those options change, e.g. from a menuconfig update, it
is important that the stack size is re-calculated, but it is not when
there is a prompt.

Therefore, make the prompt conditional such that the previously set
value is only used when it has been explicitly configured to be so.

Now users can still change the value through menuconfig and prj.conf,
by also enabling <option>_WITH_PROMPT, but when the value is
calculated by the defaults, it will continue to be calculated by
defaults instead of inheriting the intial value.

This is AFAIK a novel approach, but testing has shown that it gives
the users the behaviour they want, at the cost of some boilerplate of
course. This pattern can be applied to other options if it proves to
work as intended.

Alternatively one could remove the prompt, but then it would no longer
be possible to override the value through menuconfig.

Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
2019-11-15 13:28:56 +01:00
Johan Hedberg 1df9a2e1d3 Bluetooth: hci_raw: Fix buffer init after allocation
The code was not properly taking into account CONFIG_BT_HCI_RESERVE,
which would cause buffer underruns for any HCI driver where this value
defaults to non-zero. Also, all the allocation functions use the same
pool, so we can map them simply to bt_buf_get_rx() instead of
repeating the same code.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-11-12 16:59:43 +02:00
Johann Fischer fd82d2b47c Bluetooth: l2cap: fix null pointer dereference
bt_conn_create_pdu_timeout() may return NULL if no buffer
is available, l2cap_chan_create_seg() does not check the
subsequent return value.
Fix possible null pointer dereference in l2cap_chan_create_seg()
and l2cap_chan_le_send().

Signed-off-by: Johann Fischer <j.fischer@phytec.de>
2019-11-10 19:18:53 +02:00
François Delawarde 4ca408ed6a bluetooth: host: refactor ccc settings handling
Move functions in order to avoid function prototype and use IS_ENABLED
instead of #if defined where possible.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-08 21:29:23 +02:00
François Delawarde a51986616e bluetooth: host: add ability to load CCC settings on demand
This commits adds a BT_SETTINGS_CCC_LAZY_LOADING option to allow for
CCC settings to be loaded on demand when a peer device connects in
order to reduce memory usage.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-08 21:29:23 +02:00
François Delawarde 42eb629188 bluetooth: host: refactor sc settings handling
Move functions in order to avoid function prototypes.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-08 21:29:23 +02:00
François Delawarde fd10c4c962 bluetooth: host: do not remove SC entry when peer subscribed and paired
Maintain Service Changed entry as long as the peer device is subscribed
to SC indications and bonded. This allows to save indication data for
disconnected peers peers when CCC settings are not available (loaded
on-demand).

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-08 21:29:23 +02:00
Ioannis Glaropoulos 04e66dab53 bluetooth: host: add nRF53 in HW variants
This commit adds a string representation of the
nRF53 variant in bluetooth host.

Signed-off-by: Ioannis Glaropoulos <Ioannis.Glaropoulos@nordicsemi.no>
2019-11-08 19:26:35 +01:00
Joakim Andersson 64d6d1079c Bluetooth: SMP: Add option to disallow unauthenticated re-pairing
Disallows all unauthenticated pairing attempts made by the
peer where an unauthenticated bond already exists.
This would enable cases where an attacker could copy the peer device
address to connect and start an unauthenticated pairing procedure
to replace the existing bond. Now in order to create a new bond the old
bond has to be explicitly deleted with bt_unpair.

Added option to disable this rule in order to maintain backwards
compatibility in case this behavior is accepted.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-11-08 13:42:56 +02:00
François Delawarde 129e05c03f bluetooth: host: fix missing log_strdup
Fix missing log_strdup when loading bt/name setting. It should be done
on every string which is not in read only memory.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-07 12:49:34 +02:00
Trond Einar Snekvik 2cc0263a53 Bluetooth: Reduce severity of unavoidable warnings
Reduces the severity of warnings that happen from normal behavior, or
can't be prevented by the user:
- "No ID address" in hci_core.c: Reduced to an informational warning, as
  this will always output with the expected usage. This isn't useful
  information for 99.9% of users, and pollutes the output of all samples
  using the module.
- "Composition page %u not available" in cfg_srv.c: According to the
  Mesh Profile Specification section 4.4.2.2.2, the client is expected
  to send page=0xff. Reduced to a debug message.
- "Connectable advertising deferred" in proxy.c: Gets logged every 10
  seconds when in a Mesh Proxy connection. This is not useful
  information unless the user is debugging the proxy module. Reduced to
  a debug message.

Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
2019-11-06 19:24:32 +02:00
Ulf Magnusson bd6e04411e kconfig: Clean up header comments and make them consistent
Use this short header style in all Kconfig files:

    # <description>

    # <copyright>
    # <license>

    ...

Also change all <description>s from

    # Kconfig[.extension] - Foo-related options

to just

    # Foo-related options

It's clear enough that it's about Kconfig.

The <description> cleanup was done with this command, along with some
manual cleanup (big letter at the start, etc.)

    git ls-files '*Kconfig*' | \
        xargs sed -i -E '1 s/#\s*Kconfig[\w.-]*\s*-\s*/# /'

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-11-04 17:31:27 -05:00
François Delawarde 5317008ad3 bluetooth: host: fix missing bt_conn_unref
Fix missing bt_conn_unref when using the ccc match callback.

Fixes: #20299

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-11-04 09:43:13 +01:00
Peter A. Bigot bf5817d273 Bluetooth: rework to support C++20 designated initializers
C++ does not allow chaining of data members when identifying the
designator.  Since the generic structure has only one member remove
the designator from its internal initializer.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
2019-11-03 13:24:57 +01:00
Ulf Magnusson 975de21858 kconfig: Global whitespace/consistency cleanup
Clean up space errors and use a consistent style throughout the Kconfig
files. This makes reading the Kconfig files more distraction-free, helps
with grepping, and encourages the same style getting copied around
everywhere (meaning another pass hopefully won't be needed).

Go for the most common style:

 - Indent properties with a single tab, including for choices.

   Properties on choices work exactly the same syntactically as
   properties on symbols, so not sure how the no-indentation thing
   happened.

 - Indent help texts with a tab followed by two spaces

 - Put a space between 'config' and the symbol name, not a tab. This
   also helps when grepping for definitions.

 - Do '# A comment' instead of '#A comment'

I tweaked Kconfiglib a bit to find most of the stuff.

Some help texts were reflowed to 79 columns with 'gq' in Vim as well,
though not all, because I was afraid I'd accidentally mess up
formatting.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-11-01 15:53:23 +01:00
David B. Kinder 241044f178 doc: fix misspellings in Kconfig files
Fix misspellings in Kconfig files missed during regular reviews.

Signed-off-by: David B. Kinder <david.b.kinder@intel.com>
2019-10-30 10:24:30 +01:00
Radoslaw Koppel ecf06debe6 Bluetooth: UUID: Implement UUID 128 encoder
This commit adds an macro to allow UUID 128
to be written in more user-friendly form.
UUID in 128 bit form requires an array creation.
To complicate the whole thing - it requires the array to start from LSB,
so using the readable form, we have to write it down backwards.

Old way to declare example UUID 6E400001-B5A3-F393-E0A9-E50E24DCCA9E:
 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0,
 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E

A form provided by this commit:
 BT_UUID_128_ENCODE(0x6E400001, 0xB5A3, 0xF393, 0xE0A9, 0xE50E24DCCA9E)

Signed-off-by: Radoslaw Koppel <radoslaw.koppel@nordicsemi.no>
2019-10-23 18:49:13 +03:00
Ulf Magnusson 91dc62949e Bluetooth: Settings: Fix broken CONFIG_BT_SETTINGS_USE_PRINTK check
The CONFIG_* prefix was missing, making the #ifdef always false.

Found with a script (CONFIG_BT_SETTINGS_USE_PRINTK was unused).

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-10-18 21:52:03 +03:00
Joakim Andersson d0481f6d7f Bluetooth: GATT: Fix gatt buffer leak for write commands and notify
Fix GATT buffer leak when bt_att_send returns error the allocated
buffer is never freed. Discovered case where the link was disconnected
during the function call, so when GATT checkd the link was still
connected, but ATT checkd the link was disconnected.

Fixes: #19889

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-10-18 21:22:18 +03:00
Sergiy Nikolayenko 25164654dd Bluetooth: Keys: add key overwrite feature for key storage.
Key overwrite feature allows to overwrite old pairing key
records when key storage is full and a new pairing request occurs,
or new keys are distributed. If enabled when key storage is full and
a keys storage slot is requested, the oldest keys added will be
removed. So new devices can be paired with no limitations and no need
to determine, which devices should be unpaired to free key storage
space explicitly in application. To enable the feature set
CONFIG_BT_KEYS_OVERWRITE_OLDEST=y.

Oldest keys are determined by minimum value of up-counting aging
counter. If you set CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING=y
aging counter values will be updated each time the secure connection
is established. This might increase flash wear out if at least two
secure connections are established and shut down periodically. When
the option disabled aging counter is still updated on each new secure
connection, but not stored to flash.

Signed-off-by: Sergiy Nikolayenko <sergiy_nikolayenko@jabil.com>
2019-10-17 22:20:55 +03:00
Ulf Magnusson ac9fe11f2f Kconfig: Remove copy-pasted comments on some promptless symbols
Remove the

    # Omit prompt to signify a "hidden" option

comments that appear on some symbols. They seem to have been copy-pasted
at random, as there are lots of promptless symbols that don't have them
(that's confusing in itself, because it might give the idea that the
ones with comments are special in some way).

I suspect those comments wouldn't have helped me much if I didn't know
Kconfig either. There's a lot more Kconfig documentation now too, e.g.
https://docs.zephyrproject.org/latest/guides/kconfig/index.html.

Keep some comments that give more information than the symbol having no
prompt.

Also do some minor drive-by cleanup.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-10-17 13:05:24 -05:00
Joakim Andersson 0f06c7d8e4 Bluetooth: HCI: Add function to get connection handle of connection
Add public API function to get the connection handle of the connection.
The connection handle is needed by applications that intend to send
vendor specific commands for a given connection.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-10-17 10:04:06 +02:00
Luiz Augusto von Dentz c736de05fa Bluetooth: GATT: Replace check for CONFIG_BT_SMP
Make use of bt_conn_get_security instead of accessing conn->sec_level
directly.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-10-16 12:08:25 +03:00
Joakim Andersson 8ff183eb6c Bluetooth: GATT: Fix bug in bt_gatt_attr_next unable for static handles
Fix bug in bt_gatt_attr_next when given an attribute that has static
allocation. The handle is then 0 and the function would always return
the attribute with handle 1.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-10-15 14:53:03 +02:00
Joakim Andersson 23554b00b8 Bluetooth: host: Fix whitelist for non-central bluetooth applications
Fix compilation issue when wanting to use whitelist in bluetooth
applications that does not have CONFIG_BT_CENTRAL defined.
These functions are useful even for broadcaster and observer roles.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-10-15 15:26:43 +03:00
Joakim Andersson 5229276817 Bluetooth: GATT: Add function to check if attribute has been subscribed
Add public API function in GATT to check if a specific connection has
subscribed to the given attribute.
Without this function the application has to keep track of which
connections has subscribed using the callbacks from BT_GATT_CCC_MANAGED
since the cfg_changed callback of the CCC doesn't carry connection
context.
The other alternative is for the application to parse the information
in the struct _bt_gatt_ccc object. Although this object has structure
information available to the application the structure is marked as
internal, so the application shouldn't rely on this definition.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-10-15 15:08:56 +03:00
François Delawarde 1ce95de6ae bluetooth: host: Persist Service Changed data
Add support for persisted Service Changed data, to fix the case of a
paired device not reconnecting before a reboot and thus not receiving
SC indication. It also enables support for GATT database being changed
during a firmware update.

Move Service Changed data outside of the CCC struct and make it
persistent by adding support for a bt/sc/... setting.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-10-15 10:57:08 +02:00
François Delawarde 3a2d269bb4 bluetooth: host: Introduce a config option for Service Changed
The Service Changed characteristic support should also work when the
GATT database has been modified after reboot (firmware update scenario)

This commit introduces a BT_GATT_SERVICE_CHANGED config option that is
independent from BT_GATT_CACHING and BT_GATT_DYNAMIC_DB.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-10-15 10:57:08 +02:00
Morten Priess 51e4acb8e0 bluetooth: host: Added handler for data buffer overflow
Added event handler to prio_events in hci_core.c.

Signed-off-by: Morten Priess <mtpr@oticon.com>
2019-10-07 12:43:46 +03:00
Luiz Augusto von Dentz 1ef9b20f61 Bluetooth: ATT: Replace checks for CONFIG_BT_ATT_ENFORCE_FLOW
Make use of IS_ENABLED so the compiler do its job to remove any dead
code.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-10-04 20:57:13 +03:00
Luiz Augusto von Dentz 8d2cb14b17 Bluetooth: L2CAP: Replace checks for CONFIG_BT_CENTRAL
Make use of IS_ENABLED so the compiler do its job to remove any dead
code.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-10-04 20:57:13 +03:00
Joakim Andersson 4bca0f3d33 Bluetooth: ATT: Fix disconnected ATT not releasing buffers
Fix bug in ATT reset handling, not releasing queued notification
buffers when the connection is terminated.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-10-04 10:33:55 +03:00
Andrew Boie 1b900bf546 bluetooth: monitor: don't build backend if minimal
Log backends don't exist with minimal logging enabled,
don't compile this code.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
2019-10-01 09:24:02 -04:00
Joakim Andersson 2ac5841928 Bluetooth: SMP: Handle both devices initiating security
Handle case where:
 - Peripheral sends security request after master has sent pairing
   request or started encryption procedure.
   This packet can be ignored, as long as the slave has not already
   responded with pairing response.
 - Central wants to start security after peripheral initiated security
   request, return error code busy in this case

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-30 10:29:56 -07:00
Joakim Andersson eb6ae10745 Bluetooth: SMP: Move re-encryption handling into the SMP module
Move initiating security functionality and LTK requesting into the SMP
module so that SMP can track when the connection is in the encryption
process

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-30 10:29:56 -07:00
Joakim Andersson d50b1ac301 Bluetooth: SMP: Move bt_smp_keys_check function up
Move the bt_smp_keys_check function above all usage of the function
in order to avoid prototype declaration when making the function static.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-30 10:29:56 -07:00
Joakim Andersson 3920844842 Bluetooth: Host: Fix usage of re-using Bluetooth log buffers
Fix calling bt_hex and bt_addr_le_str multiple times in the same logging
call could result in string overwritten since log_strdup is not
guaranteed to duplicate the string buffer in all logging configurations.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-30 17:47:43 +02:00
Joakim Andersson 8b976b12a8 Bluetooth: GATT: Provide correct error code when disconnected
Calling indicate or notify on a disconnected connection object would
result in the error code ENOMEM when failing to acquire buffers instead
of the expected return code ENOTCONN.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-27 20:53:23 +03:00
Kim Sekkelund 0450263393 Bluetooth: Host: Remove printk dependency from settings
Some modules use snprintk to format the settings keys. Unfortunately
snprintk is tied with printk which is very large for some embedded
systems.
To be able to have settings enabled without also enabling printk
support, change creation of settings key strings to use bin2hex, strlen
and strcpy instead.
A utility function to make decimal presentation of a byte value is
added as u8_to_dec in lib/os/dec.c
Add new Kconfig setting BT_SETTINGS_USE_PRINTK

Signed-off-by: Kim Sekkelund <ksek@oticon.com>
2019-09-25 17:36:39 +02:00
Piotr Pryga a2eb10a76d Bluetooth: host: Configurable time to run slave conn param update
Enable possibility to configure time a slave will wait until
start of connection parameters update procedure after BT connection
is established.

Signed-off-by: Piotr Pryga <piotr.pryga@nordicsemi.no>
2019-09-23 10:10:49 +02:00
Wolfgang Puffitsch bc2abd5cf3 Bluetooth: host: Fold consecutive calls to bt_rand into one
Calls to bt_rand can be expensive, folding consecutive calls into one
reduces the overhead.

Signed-off-by: Wolfgang Puffitsch <wopu@demant.com>
2019-09-20 08:22:18 +03:00
Luiz Augusto von Dentz 4f3892c0eb Bluetooth: GATT: Use check_perm to check CCC permission
This moves check_perm code under GAT and make use of it to check if CCC
write permission can be attended by the connection security level or if
it needs to be increased.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-09-19 21:12:39 +03:00
Luiz Augusto von Dentz 03b9ce487c Bluetooth: GATT: Add support to setting permission on CCCD
This adds support to set different permissions to CCCD so security can
be checked when enabling notification which conforms to:

BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G page 2360:

  '3.3.3.3 Client Characteristic Configuration

   Authentication and authorization may be required by the server to
   write the configuration descriptor.'

In addition to that also ensure that notification are not re-enabled
until the proper security level is reached to conform to the following
statement:

  '10.3.1.1 Handling of GATT indications and notifications

   A client “requests” a server to send indications and notifications
   by appropriately configuring the server via a Client Characteristic
   Configuration Descriptor. Since the configuration is persistent
   across a disconnection and reconnection, security requirements must
   be checked against the configuration upon a reconnection before
   sending indications or notifications. When a server reconnects to a
   client to send an indication or notification for which security is
   required, the server shall initiate or request encryption with the
   client prior to sending an indication or notification. If the client
   does not have an LTK indicating that the client has lost the bond,
   enabling encryption will fail.'

Fixes #17983

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-09-19 21:12:39 +03:00
Luiz Augusto von Dentz 296f92276f Bluetooth: GATT: Fix not storing SC changes
CCC storaged is no longer declared separetly so check if ccc->cfg
matches with sc_ccc_cfg no longer works so instead use the cfg_changed
callback and match against sc_ccc_cfg_changed.

Fixes #19267

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-09-19 12:58:18 +02:00
Johan Hedberg 73baaaa3dc Bluetooth: Pass local identity to bt_conn_add_le()
This makes the identity initialization consistent and actually catches
a few branches where it may not have properly happened.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-09-17 11:36:38 +03:00
Johan Hedberg 0ce7dad5b2 Bluetooth: Allow disconnected state connections to be reconnected
Make it possible to initiate new connections from within the
disconnect callback. This wasn't completely trivial since there was
connection cleanup done through deferred action using the CONN_CLEANUP
flag.

This patch moves the disconnected callbacks to be run after all
cleanup is done. We can't directly do this in the TX thread, since
that's internal, so we instead take advantage of the deferred work
support and do it using the update_work callback. Since the same
cleanup is needed also for BR/EDR connections the work definition is
moved from the LE-specific struct to the generic struct bt_conn.

A valid bt_conn object in disconnected state is a likely indication of
a connection reference leak, so there's a new BT_WARN() for this case
in bt_conn_create_le().

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-09-17 11:36:38 +03:00
Mariusz Skamra ac7f81314e Bluetooth: ATT: Fix responding to unknown ATT command
Host shall ignore the unknown ATT PDU that has Command Flag set.
Fixes regression introduced in 3b271b8455.

Fixes: GATT/SR/UNS/BI-02-C
Signed-off-by: Mariusz Skamra <mariusz.skamra@codecoup.pl>
2019-09-10 12:26:11 +03:00
Joakim Andersson 0790fdf0d7 Bluetooth: host: Add bt_conn_foreach API to iterate all connections
Add iterator function to iterate over all connection objects.
Make type a bitmap so that it can be used as a bitmask to select which
conns to receive foreach callback.
Use foreach function internally where possible.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-07 15:53:26 +03:00
Morten Priess fddd786dfe bluetooth: host: Configurable HCI stack sizes
Made BT_HCI_TX_STACK_SIZE configurable and added BT_HCI_ECC_STACK_SIZE.
These changes are necessary to allow vendors to adjust for other
achitectures.

Signed-off-by: Morten Priess <mtpr@oticon.com>
2019-09-07 15:50:43 +03:00
Joakim Andersson 6d08a30838 Bluetooth: SMP: Add missing static to internal function
Add missing static to internal SMP function

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-07 15:49:34 +03:00
Joakim Andersson 361cbea9cd Bluetooth: SMP: Fix pairing using debug keys
Fix issue when checking if SMP pairing procedure is allowed to use the
SMP debug keys. This check did not consider the case where the keys
pointer was assigned, but did not contain a valid LTK key.
This resulted in being unable to pair with debug keys without an
existing bond.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-07 15:49:34 +03:00
Joakim Andersson dcde30e5e4 Bluetooth: host: Handle return code of set_random_address
The return code of set_random_address is not always handled. This could
lead to connection using the wrong Identity address.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-07 15:48:17 +03:00
Joakim Andersson 6c2b7937c5 Bluetooth: Host: Workaround for controller advertising data bug
Add workaround for advertising data issue in the internal bluetooth
controller. The advertising data must be set after advertising
parameters in order to successfully update the advertising data after
an directed advertiser has been active.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-09-03 13:08:39 +02:00
Akshatha Harishchandra 613655e91b bluetooth: host: Replaced bt_uuid_create_le with bt_uuid_create
Endianness bug fix in bt_uuid_create function.
Replaced bt_uuid_create_le with bt_uuid_create which
handles both UUID from air and internal varaiable.
Fixed bug with endianess in case of big endian targets.

Signed-off-by: Akshatha Harishchandra <akhr@oticon.com>
2019-08-30 12:42:24 +02:00
Luiz Augusto von Dentz 5ac014aac6 Bluetooth: ATT: Fix misleading warning
When an opcode doesn't have a handle that doesn't mean it is unknown
just that it will not be handle as it could have been disabled.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-30 09:52:01 +02:00
Luiz Augusto von Dentz ac02b30923 Bluetooth: Don't run SMP callbacks on syswq
SMP shall be considered internal TX notify callbacks so they are
allowed to be run from TX thread context like the others.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-30 09:52:01 +02:00
Luiz Augusto von Dentz 5d0c6fe562 Bluetooth: ATT: Use timeout when allocating a buffer
Whenever responding or confirming use the ATT timeout to allocate a
buffer.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-30 09:52:01 +02:00
Luiz Augusto von Dentz 3b271b8455 Bluetooth: ATT: Fix att_op_get_type when client is disabled
When CONFIG_BT_GATT_CLIENT is not set att_op_get_type would return
unkown operation instead of properly return the opcode.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-30 09:52:01 +02:00
Luiz Augusto von Dentz 68bfd1422e Bluetooth: L2CAP: Fix invalid access if a buffer cannot be allocated
Since bt_l2cap_create_pdu can return NULL when used under syswq context
the code should always check its result, this also changes the timeout
to have a specific value (RTX maximum timeout) so signalling PDUs still
wait for a buffer to become available.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-30 09:52:01 +02:00
Luiz Augusto von Dentz 6a71ebf6c5 Bluetooth: SMP: Fix invalid access if a buffer cannot be allocated
In certain cases there could be no buffer available which may lead to
NULL being returned by bt_l2cap_create_pdu so instead use
bt_l2cap_create_pdu_timeout to wait with a specific timeout.

Note that this should also ensure the SMP will not be waiting for a
buffer when the remote might have timed out already.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-30 09:52:01 +02:00
Luiz Augusto von Dentz 1af31671e3 Bluetooth: conn: Add bt_conn_create_pdu_timeout
This adds bt_conn_create_pdu_timeout function which can be used
to provide a timeout when allocating a buffer.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-30 09:52:01 +02:00
Joakim Andersson 3320b51644 Bluetooth: Host: Rename long error codes
Rename AUTHENTICATION, to AUTH, since this is a well established short
form of the word.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-27 15:13:25 +02:00
Joakim Andersson 3e9888105e Bluetooth: Host: Rename API function to initiate bluetooth security.
Rename bt_conn_security to bt_conn_set_security, this makes the API
naming more consistent.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-27 15:13:25 +02:00
Joakim Andersson 1c48757d94 Bluetooth: Host: Rename security level enum
Rename security level enum, using level and number instead of low,
medium, high and fips.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-27 15:13:25 +02:00
Johan Hedberg b763a9be3a Bluetooth: Host: Fix checking for sufficient encryption key size
A security vulnerability in the Bluetooth BR/EDR Bluetooth Core
specification versions 1.0 through 5.1 has been identified as
CVE-2019-9506. The Bluetooth BR/EDR encryption key negotiation
protocol is vulnerable to packet injection that could allow an
unauthenticated user to decrease the size of the entropy of the
encryption key, potentially causing information disclosure and/or
escalation of privileges via adjacent access. There is not currently
any knowledge of this being exploited.

From Core spec erratum 11838:

A device shall enforce an encryption key with at least 128-bit
equivalent strength for all services that require Security Mode 4,
Level 4. For all other services that require encryption, a device
should enforce an encryption key with at least 56-bit equivalent
strength, irrespective of whether the remote device supports Secure
Simple Pairing.

After encryption has been enabled, the Host should check the
encryption key size using either the HCI_Read_Encryption_Key_Size
command (see [Vol 2] Part E, Section 7.5.7) or a vendor-specific
method.

Fixes #18658

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-27 14:53:18 +03:00
Johan Hedberg 7d29296047 Bluetooth: Host: Fix failure handling with security level change
In case sufficient security level wasn't reached the error propagation
was missing, which could lead to incorrect state transitions when
update_sec_level_br() returns. Return true/false and make sure to
abort any further operations in case the update fails.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2019-08-27 14:53:18 +03:00
Asbjørn Sæbø 56e6b99a69 Bluetooth: Add log_strdup() to BT_ERR string arguments
Wrap string arguments to BT_ERR in log_strdup().

Signed-off-by: Asbjørn Sæbø <asbjorn.sabo@nordicsemi.no>
2019-08-27 14:02:01 +03:00
Joakim Andersson d2c1da1335 Bluetooth: Host: Fix invalid pointer in bt_smp_pkey_ready
The storage for the public key is pub_key in hci_core.c.
When the public key event is generated the public key is copied into
this buffer, but the pointer to the event storage of the key is given
in the public key ready callback (bt_smp_pkey_ready).
SMP expects that it is safe to assign a global pointer to this variable.
In smp_init bt_pub_key_get is used to get the pointer to the public key.
In both cases SMP assigns the le_sc_pub_key to the pointer given.

This creates an issue when bt_smp_pkey_ready callback occurs after
smp_init during pairing procedure, SMP will then have a pointer to an
event buffer that has been released and contains invalid data.

Fixes: #18580

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-27 13:05:08 +03:00
Joakim Andersson 05354129e7 Bluetooth: SMP: Give security changed when rejecting LTK
Give the security changed callback when the peripheral initiated
security request and peer attempted to encrypt the connection but no LTK
match was found.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-26 13:12:49 +02:00
Joakim Andersson b35075d595 Bluetooth: SMP: Re-pairing cannot lower the security level of the bond
Make sure that a new pairing procedure with an existing bond does not
result in a security with weaker security properties.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-26 13:12:49 +02:00
Joakim Andersson 128cf42d8a Bluetooth: Host: Add option to force pairing in bt_conn_security
Add option to force the host to initiate pairing procedure even if the
host has encryption keys for the peer.
This option can be used to pair with a bonded peer that has deleted its
bonding information without deleting the keys. If new pairing results
in weaker keys the pairing will be aborted.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-26 13:12:49 +02:00
Joakim Andersson 6d4b842a10 Bluetooth: Host: Add error to security changed callback
Add security error to security_changed callback. Call this callback when
security has failed and provide current security level and error.
Reason for failure can be.
 - Pairing procedure failed, pairing aborted before link encryption.
 - Link encrypt procedure failed
 - Link key refresh procedure failed.

Fix missing bt_conn_unref on encryption key refresh with error status.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-26 13:12:49 +02:00
Joakim Andersson f1c7371494 Bluetooth: SMP: Stop new pairing early if MAX_PAIR has been reached
Stop the pairing procedure in the request phase if no storage is
available for the keys. This avoids the pairing procedure from failing
during the key distribution phase.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-26 13:12:49 +02:00
Joakim Andersson 3263f93747 Bluetooth: host: Pairing callback fail and complete for SSP
Proved the Authentication callback for pairing failed and pairing
complete when BR/EDR SSP is complete.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-26 13:12:49 +02:00
Joakim Andersson 1fb68828ef Bluetooth: SMP: Add pairing failed reason
Forward the pairing failed SMP status code to the application

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-26 13:12:49 +02:00
Joakim Andersson a378498fdc Bluetooth: SMP: Add option to treat debug keys normally during debugging
Allow SMP debug keys to behave in the same way as normal keys, in order
to debug with encryption and Bluetooth sniffer the exact way it behaves
when not using debug keys.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-26 13:12:49 +02:00
Luiz Augusto von Dentz 5f3595e47c Bluetooth: GATT: Fix using variable size storage for CCC
This removes the necessity of registering the storage for CCC and make
it part of the declaration itself.

Fixes #18547

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-22 15:14:39 +03:00
Luiz Augusto von Dentz cee271968e Bluetooth: GATT: Add support for indicating by UUID
This adds support to provide an UUID to bt_gatt_indicate so API user
don't need to hardcode the attribute offset by hand.

Fixes #18572

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-22 15:14:39 +03:00
Luiz Augusto von Dentz 2f91f1323d Bluetooth: GATT: Remove dead code
This fixes the dead code when parsing Find Information response since
the index counter can never be zero, instead it checks there are any
attributes found and if there nothing don't proceed with discovering
which was the original intent of the now dead code.

Fixes #18384

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-19 13:09:26 +02:00
Luiz Augusto von Dentz 692d7d523e Bluetooth: GATT: Fix not clearing out of sync flag
When the database changes and a client becomes change unware it should
also clear the out of sync flag as the following request should return
an error:

Core Spec 5.1 | Vol 3, Part G, 2.5.2.1 Robust Caching:

  'The error response is sent only once after the client becomes
  change-unaware, unless the client disconnects or the database changes
  again before the client becomes change-aware in which case the error
  response shall be sent again.'

Fixes: #18183

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-16 20:47:40 +03:00
Luiz Augusto von Dentz 67a2ae53ae Bluetooth: GATT: Fix assert when storying CCCs
This fixes the following crash:

ASSERTION FAIL [0] @ ZEPHYR_BASE/subsys/logging/log_core.c:180
	argument 2 in log message "%s: Stored CCCs for %s (%s)"
	missing log_strdup().

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-16 20:47:40 +03:00
Joakim Andersson c19dac1a66 Bluetooth: Host: Fix unable to connect using host resolution
Fix issue unable to connect to bonded peer when host resolution is used
either because the controller does not support privacy, or the
controller resolving list was exceeded.
In this case we need to use the RPA from the advertising report
directly, there is a small chance of the peer cycling the RPA here, in
which case the connection might be unsuccessful.

Bug introduced here: 45da629b24

Fixes: #18306

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-16 15:46:46 +03:00
Joakim Andersson a62f609cd9 Bluetooth: Host: Set Kconfig RPA timeout in controller
Use the RPA timeout provided by Kconfig in the controller

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-15 11:50:25 +02:00
Joakim Andersson d94da73a19 Bluetooth: Host: Fix RPA timer not running
The RPA timer should always be running, even with privacy supported by
the controller. In order to select an IRK to generate the private
the controller is instructed by provided an identity address.
If we want to advertise privately without providing an identity address
the host has to set the private address.

Fixes: #18150

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-15 11:50:25 +02:00
Joakim Andersson 1691e0a699 Bluetooth: Host: fix missing interval_min copy
Fix missing interval min copy in bt_conn_set_param_le. Application is
unable to override BT_GAP_INIT_CONN_INT_MIN for interval_min.

Fixes: #17789

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-14 12:04:26 +02:00
Luiz Augusto von Dentz b563f4440a Bluetooth: Dispatch internal callbacks using RX thread
This is safer now that bt_conn_create_pdu can return NULL when using
syswq which can prevent things like signalling of L2CAP and ATT layers.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-09 21:01:59 +02:00
Luiz Augusto von Dentz 6b0242cfeb Bluetooth: Fix deadlock caused by blocking on syswq
Since TX complete notification are dispatched with syswq blocking on it
can completely deadlock Bluetooth so this attempt to make it safe by
return -ENOMEM if that the current thread happens to be the syswq
thread.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-09 21:01:59 +02:00
Luiz Augusto von Dentz 4244b320e7 Bluetooth: conn: Warn when blocking with K_FOREVER
If debug is enabled print warnings when a call with K_FOREVER would have
blocked.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-08-09 21:01:59 +02:00
Joakim Andersson a463d117f6 Bluetooth: Host: Add whitelist support in Bluetooth Host API
Add whitelist support in the bluetooth host.
Supported features:
 - Advertising with whitelist on scan requests, connect request ,or both
 - Scanning with whitelist
 - Creating connections using a whitelist (Auto connection procedure).

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-09 16:26:10 +02:00
Joakim Andersson a2531e1622 Bluetooth: SMP: MITM security possible without IO when OOB data present
Allow Security level HIGH and FIPS to be initiated when we have received
OOB data for the remote device. The security property of MITM is allowed
if out of band authentication data is available.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-09 11:20:31 +02:00
Joakim Andersson 54d9ae45a5 Bluetooth: Host: Add identity addresses to conn info object
Use the src and dst naming to refer to the identity addresses of the
connection. Keep the device addresses used during connections but rename
them to local and remote instead.
Update documentation to be more descriptive.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-08 15:49:19 +02:00
Ulf Magnusson 5ef7eea6e2 Bluetooth: GATT: Kconfig: Remove redundant BT_CONN dependencies
subsys/bluetooth/host/Kconfig.gatt is already sourced within an
'if BT_CONN' in subsys/bluetooth/host/Kconfig.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-08-08 11:54:46 +02:00
Joakim Andersson 16c24ea8d2 Bluetooth: SMP: Use error code for OOB data not available
If no callback for oob data request is registered, or the user decided
to abort authentication during OOB data request, we should return the
error code for no OOB data available to indicate that the user does not
have the correct OOB, or no OOB interface at all.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-07 18:32:32 +02:00
Joakim Andersson a80ab8bcf6 Bluetooth: SMP: Fix kernel crash if auth handler is NULL
Fix kernel crash if bluetooth authentication handlers has not been
registered. The bt_auth object is then NULL, this dereference caused a
call to an invalid function pointer.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-07 18:32:32 +02:00
Krzysztof Chruscinski b785ded9fa bluetooth: host: Fix Kconfig error
Fixed error 'The int symbol BT_DISCARDABLE_BUF_COUNT is being evaluated
in a logical context somewhere'.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2019-08-07 16:12:29 +02:00
Ulf Magnusson b5b7853ad9 Bluetooth: L2CAP: Kconfig: Remove redundant BT_CONN dependency
subsys/bluetooth/host/Kconfig.l2cap is already sourced within an
'if BT_CONN' in subsys/bluetooth/host/Kconfig, so BT_DEBUG_L2CAP does
not need a 'depends on BT_CONN'.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-08-07 16:51:02 +03:00
Kim Sekkelund 87c3db9e0b Bluetooth: Host: Find by type should accept 128bit UUIDs
Find by type does only accept a UUID with the same length as the UUID
which is stored in the internal list. If a UUID is stored in the short
16 bit format then a request with 128 bit UUID will fail.
Add support for the missing formats.

Signed-off-by: Kim Sekkelund <ksek@oticon.com>
2019-08-07 15:39:11 +02:00
Joakim Andersson ccbb18c9ac Bluetooth: SMP: Fix SMP identity distributing IRK for ID 0 always
Fix an issue in the SMP identity distribution where the IRK for the
default identity was always distributed.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-06 19:47:27 +02:00
Joakim Andersson 699436a3b6 Bluetooth: Host: Fix bug in creating connection with wrong own address.
Fix bug introduced by: 45da629b24
Mistakenly set the own address type to the destination address type.
Also this uses the RPA_OR_RANDOM in case local IRKs exists in the
controller.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-06 14:28:17 +02:00
Joakim Andersson 45da629b24 Bluetooth: Host: Fix wrong init address when controller resolved address
The init addr should contain the on-air address used to establish the
connection. The dst address contains either the current RPA of the
unknown peer, or the identity address after identity information has
been exchanged.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-05 12:18:46 +02:00
Joakim Andersson c1a754f665 Bluetooth: Host: Print error codes in hex
Error codes are listed in header files and in the core spec as hex
values. Always print them in hex in debug for easier error code
checking.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-05 12:18:17 +02:00
Joakim Andersson 2b84c726c8 Bluetooth: Settings: Fix generated identity not persistently stored.
Fix an issue where the generated identity was not permanently stored.
This resulted in being unable to reconnect after bonding when using
privacy, since a new local IRK was generated on reboot.

When settings is enabled the application is responsible for loading
identities and possible creating its own identities.
When settings_load is called and no identities has been created or found
in persistent storage a new identity will be created.
Since bt init has not been finalized bt_id_create will not make a call
to bt_settings_save_id. So we need to make sure that this identity will
be stored.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-08-05 11:00:57 +02:00
Joakim Andersson 7f757e64d3 Bluetooth: Host: Move BT_DBG out of irq_lock
Don't have BT_DBG inside of irq_lock, this will create unnecessary IRQ
delays.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-07-31 17:07:50 +02:00
Joakim Andersson 1555ae8f38 Bluetooth: Keys: Fix logging assertions when enabling BT_DEBUG_KEYS.
Logging module requires strings to be duplicated using log_strdup.
Enabling CONFIG_BT_DEBUG_KEYS caused runtime assertions in the logging
module.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-07-31 16:48:54 +02:00
Mariusz Skamra a56695db9a Bluetooth: GATT: Fix byte order for database hash
Core 5.1, Vol 3, Part F, 3.3.1
"Multi-octet fields within the GATT profile shall be sent least
significant octet first (little-endian) with the exception of the
Characteristic Value field. The Characteristic Value and any fields
within it shall be little-endian unless otherwise defined in the
specification which defines the characteristic."

Fixes: GATT/SR/GAS/BV-02-C
Fixes: #17857
Signed-off-by: Mariusz Skamra <mariusz.skamra@codecoup.pl>
2019-07-31 15:25:06 +02:00
Joakim Andersson c2ee00f737 Bluetooth: SMP: Fix peripheral security request with authenticated keys
The call to bt_conn_security will send a SMP security request for
peripheral, and for central it will initiate LL encryption.
A call to bt_conn_security with no IO capabilities but authenticated
keys has been distributed, would succeed on central side, but fail on
peripheral side with error code -22.

The keys could have been either:
 - Preprogrammed
 - IO capabilities may have changed.
 - OOB bonding may been used.

Fix so that Peripheral can send a security request if the bond
information is already established.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-07-31 14:24:10 +02:00
Findlay Feng 0ea07d3072 bluetooth: Fix an undefined behavior
The original creates a pointer to a compiler-generated temporary that is
destroyed when the scope is exited. The pointer is stored in a structure
defined in an enclosing scope and is invalid by the point it's used.

The fix holds the structure in a variable with the same lifetime as
the pointer.

Signed-off-by: Findlay Feng <i@fengch.me>
2019-07-31 12:15:36 +03:00
Luiz Augusto von Dentz 29782a006b Bluetooth: GATT: Use atomic_t for subscribe flags
This makes use of atomic_t helpers to set, test and clear flags.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-07-29 12:26:18 +02:00
Luiz Augusto von Dentz 219bb6fe1f Bluetooth: GATT: Fix not canceling on unsubscribe
This introduces a new flag (BT_GATT_SUBSCRIBE_WRITE_PENDING) which is
set when a write operation requires canceling before the parameters can
be reused.

Fixes #17534

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-07-29 12:26:18 +02:00
Luiz Augusto von Dentz 662a519a17 Bluetooth: ATT: Fix bt_att_req_cancel clearing outstanding request
This adds a dummy cancel request which is used instead of NULL when
clearing and outstanding request thus prevent new request to take
place before a response is received while allowing the original request
to be reused.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
2019-07-29 12:26:18 +02:00
Joakim Andersson 4fa0825925 Bluetooth: Host: Fix advertiser state flags
Set advertisers state flags so that previous advertise params does not
affect the current advertiser state.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-07-23 13:25:15 +02:00
Vinayak Kariappa Chettimada f4eb03c538 Bluetooth: controller: Rename BT_LL_SW to BT_LL_SW_LEGACY
Rename the controller Kconfig option BT_LL_SW to
BT_LL_SW_LEGACY in preparation towards switch to new Link
Layer implementation.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
2019-07-22 14:23:42 +02:00
Joakim Andersson f8b7f2ab6a Bluetooth: smp: Add LE SC OOB support for central side
Add support for LE secure connections using OOB authentication for the
central role.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-07-16 12:44:18 +02:00
Joakim Andersson 0ac83180fd Bluetooth: host: Move address string parsing to bluetooth API
Bluetooth address parsing has been duplicated across the different
sub-shell files. Also missing parsing of identity/resolved addresses.
Move parsing of string close to parsing to string for a symmetrical API

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2019-07-16 12:44:18 +02:00
François Delawarde 88a2fb87ac bluetooth: fix GATT database hash done too many times
Submit GATT database hash as a delayed work to prevent it being run
twice if we register dynamic services.

Signed-off-by: François Delawarde <fnde@oticon.com>
2019-07-15 15:11:27 +02:00