Merge Linus master into drm-next
[firefly-linux-kernel-4.4.55.git] / net / bluetooth / mgmt.c
index f95937c47b9e294adf2b067f55c8121e72415481..7fd87e7135b52753c0bcefd58cb4a290c57c77ba 100644 (file)
@@ -941,30 +941,122 @@ static u8 get_adv_discov_flags(struct hci_dev *hdev)
        return 0;
 }
 
-static u8 create_default_adv_data(struct hci_dev *hdev, u8 *ptr)
+static u8 get_current_adv_instance(struct hci_dev *hdev)
+{
+       /* The "Set Advertising" setting supersedes the "Add Advertising"
+        * setting. Here we set the advertising data based on which
+        * setting was set. When neither apply, default to the global settings,
+        * represented by instance "0".
+        */
+       if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
+           !hci_dev_test_flag(hdev, HCI_ADVERTISING))
+               return 0x01;
+
+       return 0x00;
+}
+
+static bool get_connectable(struct hci_dev *hdev)
+{
+       struct mgmt_pending_cmd *cmd;
+
+       /* If there's a pending mgmt command the flag will not yet have
+        * it's final value, so check for this first.
+        */
+       cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
+       if (cmd) {
+               struct mgmt_mode *cp = cmd->param;
+
+               return cp->val;
+       }
+
+       return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
+}
+
+static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
+{
+       u32 flags;
+
+       if (instance > 0x01)
+               return 0;
+
+       if (instance == 0x01)
+               return hdev->adv_instance.flags;
+
+       /* Instance 0 always manages the "Tx Power" and "Flags" fields */
+       flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
+
+       /* For instance 0, the HCI_ADVERTISING_CONNECTABLE setting corresponds
+        * to the "connectable" instance flag.
+        */
+       if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
+               flags |= MGMT_ADV_FLAG_CONNECTABLE;
+
+       return flags;
+}
+
+static u8 get_adv_instance_scan_rsp_len(struct hci_dev *hdev, u8 instance)
+{
+       /* Ignore instance 0 and other unsupported instances */
+       if (instance != 0x01)
+               return 0;
+
+       /* TODO: Take into account the "appearance" and "local-name" flags here.
+        * These are currently being ignored as they are not supported.
+        */
+       return hdev->adv_instance.scan_rsp_len;
+}
+
+static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
 {
        u8 ad_len = 0, flags = 0;
+       u32 instance_flags = get_adv_instance_flags(hdev, instance);
 
-       flags |= get_adv_discov_flags(hdev);
+       /* The Add Advertising command allows userspace to set both the general
+        * and limited discoverable flags.
+        */
+       if (instance_flags & MGMT_ADV_FLAG_DISCOV)
+               flags |= LE_AD_GENERAL;
 
-       if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
-               flags |= LE_AD_NO_BREDR;
+       if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
+               flags |= LE_AD_LIMITED;
+
+       if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
+               /* If a discovery flag wasn't provided, simply use the global
+                * settings.
+                */
+               if (!flags)
+                       flags |= get_adv_discov_flags(hdev);
 
-       if (flags) {
-               BT_DBG("adv flags 0x%02x", flags);
+               if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
+                       flags |= LE_AD_NO_BREDR;
 
-               ptr[0] = 2;
-               ptr[1] = EIR_FLAGS;
-               ptr[2] = flags;
+               /* If flags would still be empty, then there is no need to
+                * include the "Flags" AD field".
+                */
+               if (flags) {
+                       ptr[0] = 0x02;
+                       ptr[1] = EIR_FLAGS;
+                       ptr[2] = flags;
 
-               ad_len += 3;
-               ptr += 3;
+                       ad_len += 3;
+                       ptr += 3;
+               }
        }
 
-       if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) {
-               ptr[0] = 2;
+       if (instance) {
+               memcpy(ptr, hdev->adv_instance.adv_data,
+                      hdev->adv_instance.adv_data_len);
+
+               ad_len += hdev->adv_instance.adv_data_len;
+               ptr += hdev->adv_instance.adv_data_len;
+       }
+
+       /* Provide Tx Power only if we can provide a valid value for it */
+       if (hdev->adv_tx_power != HCI_TX_POWER_INVALID &&
+           (instance_flags & MGMT_ADV_FLAG_TX_POWER)) {
+               ptr[0] = 0x02;
                ptr[1] = EIR_TX_POWER;
-               ptr[2] = (u8) hdev->adv_tx_power;
+               ptr[2] = (u8)hdev->adv_tx_power;
 
                ad_len += 3;
                ptr += 3;
@@ -973,17 +1065,6 @@ static u8 create_default_adv_data(struct hci_dev *hdev, u8 *ptr)
        return ad_len;
 }
 
-static u8 create_instance_adv_data(struct hci_dev *hdev, u8 *ptr)
-{
-       /* TODO: Set the appropriate entries based on advertising instance flags
-        * here once flags other than 0 are supported.
-        */
-       memcpy(ptr, hdev->adv_instance.adv_data,
-              hdev->adv_instance.adv_data_len);
-
-       return hdev->adv_instance.adv_data_len;
-}
-
 static void update_adv_data_for_instance(struct hci_request *req, u8 instance)
 {
        struct hci_dev *hdev = req->hdev;
@@ -995,10 +1076,7 @@ static void update_adv_data_for_instance(struct hci_request *req, u8 instance)
 
        memset(&cp, 0, sizeof(cp));
 
-       if (instance)
-               len = create_instance_adv_data(hdev, cp.data);
-       else
-               len = create_default_adv_data(hdev, cp.data);
+       len = create_instance_adv_data(hdev, instance, cp.data);
 
        /* There's nothing to do if the data hasn't changed */
        if (hdev->adv_data_len == len &&
@@ -1016,18 +1094,7 @@ static void update_adv_data_for_instance(struct hci_request *req, u8 instance)
 static void update_adv_data(struct hci_request *req)
 {
        struct hci_dev *hdev = req->hdev;
-       u8 instance;
-
-       /* The "Set Advertising" setting supersedes the "Add Advertising"
-        * setting. Here we set the advertising data based on which
-        * setting was set. When neither apply, default to the global settings,
-        * represented by instance "0".
-        */
-       if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
-           !hci_dev_test_flag(hdev, HCI_ADVERTISING))
-               instance = 0x01;
-       else
-               instance = 0x00;
+       u8 instance = get_current_adv_instance(hdev);
 
        update_adv_data_for_instance(req, instance);
 }
@@ -1159,22 +1226,6 @@ static void update_class(struct hci_request *req)
        hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
 }
 
-static bool get_connectable(struct hci_dev *hdev)
-{
-       struct mgmt_pending_cmd *cmd;
-
-       /* If there's a pending mgmt command the flag will not yet have
-        * it's final value, so check for this first.
-        */
-       cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
-       if (cmd) {
-               struct mgmt_mode *cp = cmd->param;
-               return cp->val;
-       }
-
-       return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
-}
-
 static void disable_advertising(struct hci_request *req)
 {
        u8 enable = 0x00;
@@ -1188,6 +1239,8 @@ static void enable_advertising(struct hci_request *req)
        struct hci_cp_le_set_adv_param cp;
        u8 own_addr_type, enable = 0x01;
        bool connectable;
+       u8 instance;
+       u32 flags;
 
        if (hci_conn_num(hdev, LE_LINK) > 0)
                return;
@@ -1202,10 +1255,14 @@ static void enable_advertising(struct hci_request *req)
         */
        hci_dev_clear_flag(hdev, HCI_LE_ADV);
 
-       if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
-               connectable = true;
-       else
-               connectable = get_connectable(hdev);
+       instance = get_current_adv_instance(hdev);
+       flags = get_adv_instance_flags(hdev, instance);
+
+       /* If the "connectable" instance flag was not set, then choose between
+        * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
+        */
+       connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
+                     get_connectable(hdev);
 
        /* Set require_privacy to true only when non-connectable
         * advertising is used. In that case it is fine to use a
@@ -1217,7 +1274,14 @@ static void enable_advertising(struct hci_request *req)
        memset(&cp, 0, sizeof(cp));
        cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
        cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
-       cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;
+
+       if (connectable)
+               cp.type = LE_ADV_IND;
+       else if (get_adv_instance_scan_rsp_len(hdev, instance))
+               cp.type = LE_ADV_SCAN_IND;
+       else
+               cp.type = LE_ADV_NONCONN_IND;
+
        cp.own_address_type = own_addr_type;
        cp.channel_map = hdev->le_adv_channel_map;
 
@@ -2051,7 +2115,8 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
 
 no_scan_update:
        /* Update the advertising parameters if necessary */
-       if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
+       if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
+           hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
                enable_advertising(&req);
 
        err = hci_req_run(&req, set_connectable_complete);
@@ -3720,10 +3785,70 @@ failed:
        return err;
 }
 
+static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
+                                        u16 opcode, struct sk_buff *skb)
+{
+       struct mgmt_rp_read_local_oob_data mgmt_rp;
+       size_t rp_size = sizeof(mgmt_rp);
+       struct mgmt_pending_cmd *cmd;
+
+       BT_DBG("%s status %u", hdev->name, status);
+
+       cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
+       if (!cmd)
+               return;
+
+       if (status || !skb) {
+               mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
+                               status ? mgmt_status(status) : MGMT_STATUS_FAILED);
+               goto remove;
+       }
+
+       memset(&mgmt_rp, 0, sizeof(mgmt_rp));
+
+       if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
+               struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
+
+               if (skb->len < sizeof(*rp)) {
+                       mgmt_cmd_status(cmd->sk, hdev->id,
+                                       MGMT_OP_READ_LOCAL_OOB_DATA,
+                                       MGMT_STATUS_FAILED);
+                       goto remove;
+               }
+
+               memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
+               memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
+
+               rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
+       } else {
+               struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
+
+               if (skb->len < sizeof(*rp)) {
+                       mgmt_cmd_status(cmd->sk, hdev->id,
+                                       MGMT_OP_READ_LOCAL_OOB_DATA,
+                                       MGMT_STATUS_FAILED);
+                       goto remove;
+               }
+
+               memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
+               memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
+
+               memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
+               memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
+       }
+
+       mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
+                         MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
+
+remove:
+       mgmt_pending_remove(cmd);
+}
+
 static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
                               void *data, u16 data_len)
 {
        struct mgmt_pending_cmd *cmd;
+       struct hci_request req;
        int err;
 
        BT_DBG("%s", hdev->name);
@@ -3754,12 +3879,14 @@ static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
                goto unlock;
        }
 
+       hci_req_init(&req, hdev);
+
        if (bredr_sc_enabled(hdev))
-               err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA,
-                                  0, NULL);
+               hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
        else
-               err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
+               hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
 
+       err = hci_req_run_skb(&req, read_local_oob_data_complete);
        if (err < 0)
                mgmt_pending_remove(cmd);
 
@@ -6339,6 +6466,145 @@ static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
        return eir_len;
 }
 
+static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
+                                            u16 opcode, struct sk_buff *skb)
+{
+       const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
+       struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
+       u8 *h192, *r192, *h256, *r256;
+       struct mgmt_pending_cmd *cmd;
+       u16 eir_len;
+       int err;
+
+       BT_DBG("%s status %u", hdev->name, status);
+
+       cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
+       if (!cmd)
+               return;
+
+       mgmt_cp = cmd->param;
+
+       if (status) {
+               status = mgmt_status(status);
+               eir_len = 0;
+
+               h192 = NULL;
+               r192 = NULL;
+               h256 = NULL;
+               r256 = NULL;
+       } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
+               struct hci_rp_read_local_oob_data *rp;
+
+               if (skb->len != sizeof(*rp)) {
+                       status = MGMT_STATUS_FAILED;
+                       eir_len = 0;
+               } else {
+                       status = MGMT_STATUS_SUCCESS;
+                       rp = (void *)skb->data;
+
+                       eir_len = 5 + 18 + 18;
+                       h192 = rp->hash;
+                       r192 = rp->rand;
+                       h256 = NULL;
+                       r256 = NULL;
+               }
+       } else {
+               struct hci_rp_read_local_oob_ext_data *rp;
+
+               if (skb->len != sizeof(*rp)) {
+                       status = MGMT_STATUS_FAILED;
+                       eir_len = 0;
+               } else {
+                       status = MGMT_STATUS_SUCCESS;
+                       rp = (void *)skb->data;
+
+                       if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
+                               eir_len = 5 + 18 + 18;
+                               h192 = NULL;
+                               r192 = NULL;
+                       } else {
+                               eir_len = 5 + 18 + 18 + 18 + 18;
+                               h192 = rp->hash192;
+                               r192 = rp->rand192;
+                       }
+
+                       h256 = rp->hash256;
+                       r256 = rp->rand256;
+               }
+       }
+
+       mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
+       if (!mgmt_rp)
+               goto done;
+
+       if (status)
+               goto send_rsp;
+
+       eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
+                                 hdev->dev_class, 3);
+
+       if (h192 && r192) {
+               eir_len = eir_append_data(mgmt_rp->eir, eir_len,
+                                         EIR_SSP_HASH_C192, h192, 16);
+               eir_len = eir_append_data(mgmt_rp->eir, eir_len,
+                                         EIR_SSP_RAND_R192, r192, 16);
+       }
+
+       if (h256 && r256) {
+               eir_len = eir_append_data(mgmt_rp->eir, eir_len,
+                                         EIR_SSP_HASH_C256, h256, 16);
+               eir_len = eir_append_data(mgmt_rp->eir, eir_len,
+                                         EIR_SSP_RAND_R256, r256, 16);
+       }
+
+send_rsp:
+       mgmt_rp->type = mgmt_cp->type;
+       mgmt_rp->eir_len = cpu_to_le16(eir_len);
+
+       err = mgmt_cmd_complete(cmd->sk, hdev->id,
+                               MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
+                               mgmt_rp, sizeof(*mgmt_rp) + eir_len);
+       if (err < 0 || status)
+               goto done;
+
+       hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
+
+       err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
+                                mgmt_rp, sizeof(*mgmt_rp) + eir_len,
+                                HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
+done:
+       kfree(mgmt_rp);
+       mgmt_pending_remove(cmd);
+}
+
+static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
+                                 struct mgmt_cp_read_local_oob_ext_data *cp)
+{
+       struct mgmt_pending_cmd *cmd;
+       struct hci_request req;
+       int err;
+
+       cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
+                              cp, sizeof(*cp));
+       if (!cmd)
+               return -ENOMEM;
+
+       hci_req_init(&req, hdev);
+
+       if (bredr_sc_enabled(hdev))
+               hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
+       else
+               hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
+
+       err = hci_req_run_skb(&req, read_local_oob_ext_data_complete);
+       if (err < 0) {
+               mgmt_pending_remove(cmd);
+               return err;
+       }
+
+       return 0;
+}
+
 static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
                                   void *data, u16 data_len)
 {
@@ -6351,71 +6617,87 @@ static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
 
        BT_DBG("%s", hdev->name);
 
-       if (!hdev_is_powered(hdev))
-               return mgmt_cmd_complete(sk, hdev->id,
-                                        MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
-                                        MGMT_STATUS_NOT_POWERED,
-                                        &cp->type, sizeof(cp->type));
-
-       switch (cp->type) {
-       case BIT(BDADDR_BREDR):
-               status = mgmt_bredr_support(hdev);
-               if (status)
-                       return mgmt_cmd_complete(sk, hdev->id,
-                                                MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
-                                                status, &cp->type,
-                                                sizeof(cp->type));
-               eir_len = 5;
-               break;
-       case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
-               status = mgmt_le_support(hdev);
-               if (status)
-                       return mgmt_cmd_complete(sk, hdev->id,
-                                                MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
-                                                status, &cp->type,
-                                                sizeof(cp->type));
-               eir_len = 9 + 3 + 18 + 18 + 3;
-               break;
-       default:
-               return mgmt_cmd_complete(sk, hdev->id,
-                                        MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
-                                        MGMT_STATUS_INVALID_PARAMS,
-                                        &cp->type, sizeof(cp->type));
+       if (hdev_is_powered(hdev)) {
+               switch (cp->type) {
+               case BIT(BDADDR_BREDR):
+                       status = mgmt_bredr_support(hdev);
+                       if (status)
+                               eir_len = 0;
+                       else
+                               eir_len = 5;
+                       break;
+               case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
+                       status = mgmt_le_support(hdev);
+                       if (status)
+                               eir_len = 0;
+                       else
+                               eir_len = 9 + 3 + 18 + 18 + 3;
+                       break;
+               default:
+                       status = MGMT_STATUS_INVALID_PARAMS;
+                       eir_len = 0;
+                       break;
+               }
+       } else {
+               status = MGMT_STATUS_NOT_POWERED;
+               eir_len = 0;
        }
 
-       hci_dev_lock(hdev);
-
        rp_len = sizeof(*rp) + eir_len;
        rp = kmalloc(rp_len, GFP_ATOMIC);
-       if (!rp) {
-               hci_dev_unlock(hdev);
+       if (!rp)
                return -ENOMEM;
-       }
+
+       if (status)
+               goto complete;
+
+       hci_dev_lock(hdev);
 
        eir_len = 0;
        switch (cp->type) {
        case BIT(BDADDR_BREDR):
-               eir_len = eir_append_data(rp->eir, eir_len, EIR_CLASS_OF_DEV,
-                                         hdev->dev_class, 3);
+               if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
+                       err = read_local_ssp_oob_req(hdev, sk, cp);
+                       hci_dev_unlock(hdev);
+                       if (!err)
+                               goto done;
+
+                       status = MGMT_STATUS_FAILED;
+                       goto complete;
+               } else {
+                       eir_len = eir_append_data(rp->eir, eir_len,
+                                                 EIR_CLASS_OF_DEV,
+                                                 hdev->dev_class, 3);
+               }
                break;
        case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
                if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
                    smp_generate_oob(hdev, hash, rand) < 0) {
                        hci_dev_unlock(hdev);
-                       err = mgmt_cmd_complete(sk, hdev->id,
-                                               MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
-                                               MGMT_STATUS_FAILED,
-                                               &cp->type, sizeof(cp->type));
-                       goto done;
+                       status = MGMT_STATUS_FAILED;
+                       goto complete;
                }
 
+               /* This should return the active RPA, but since the RPA
+                * is only programmed on demand, it is really hard to fill
+                * this in at the moment. For now disallow retrieving
+                * local out-of-band data when privacy is in use.
+                *
+                * Returning the identity address will not help here since
+                * pairing happens before the identity resolving key is
+                * known and thus the connection establishment happens
+                * based on the RPA and not the identity address.
+                */
                if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
-                       memcpy(addr, &hdev->rpa, 6);
-                       addr[6] = 0x01;
-               } else if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
-                          !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
-                          (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
-                           bacmp(&hdev->static_addr, BDADDR_ANY))) {
+                       hci_dev_unlock(hdev);
+                       status = MGMT_STATUS_REJECTED;
+                       goto complete;
+               }
+
+               if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
+                  !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
+                  (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
+                   bacmp(&hdev->static_addr, BDADDR_ANY))) {
                        memcpy(addr, &hdev->static_addr, 6);
                        addr[6] = 0x01;
                } else {
@@ -6454,16 +6736,19 @@ static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
                break;
        }
 
-       rp->type = cp->type;
-       rp->eir_len = cpu_to_le16(eir_len);
-
        hci_dev_unlock(hdev);
 
        hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
 
+       status = MGMT_STATUS_SUCCESS;
+
+complete:
+       rp->type = cp->type;
+       rp->eir_len = cpu_to_le16(eir_len);
+
        err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
-                               MGMT_STATUS_SUCCESS, rp, sizeof(*rp) + eir_len);
-       if (err < 0)
+                               status, rp, sizeof(*rp) + eir_len);
+       if (err < 0 || status)
                goto done;
 
        err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
@@ -6476,6 +6761,21 @@ done:
        return err;
 }
 
+static u32 get_supported_adv_flags(struct hci_dev *hdev)
+{
+       u32 flags = 0;
+
+       flags |= MGMT_ADV_FLAG_CONNECTABLE;
+       flags |= MGMT_ADV_FLAG_DISCOV;
+       flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
+       flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
+
+       if (hdev->adv_tx_power != HCI_TX_POWER_INVALID)
+               flags |= MGMT_ADV_FLAG_TX_POWER;
+
+       return flags;
+}
+
 static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
                             void *data, u16 data_len)
 {
@@ -6483,9 +6783,14 @@ static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
        size_t rp_len;
        int err;
        bool instance;
+       u32 supported_flags;
 
        BT_DBG("%s", hdev->name);
 
+       if (!lmp_le_capable(hdev))
+               return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
+                                      MGMT_STATUS_REJECTED);
+
        hci_dev_lock(hdev);
 
        rp_len = sizeof(*rp);
@@ -6503,7 +6808,9 @@ static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
                return -ENOMEM;
        }
 
-       rp->supported_flags = cpu_to_le32(0);
+       supported_flags = get_supported_adv_flags(hdev);
+
+       rp->supported_flags = cpu_to_le32(supported_flags);
        rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
        rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
        rp->max_instances = 1;
@@ -6529,12 +6836,24 @@ static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
 }
 
 static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
-                             u8 len)
+                             u8 len, bool is_adv_data)
 {
        u8 max_len = HCI_MAX_AD_LENGTH;
        int i, cur_len;
+       bool flags_managed = false;
+       bool tx_power_managed = false;
+       u32 flags_params = MGMT_ADV_FLAG_DISCOV | MGMT_ADV_FLAG_LIMITED_DISCOV |
+                          MGMT_ADV_FLAG_MANAGED_FLAGS;
+
+       if (is_adv_data && (adv_flags & flags_params)) {
+               flags_managed = true;
+               max_len -= 3;
+       }
 
-       /* TODO: Correctly reduce len based on adv_flags. */
+       if (is_adv_data && (adv_flags & MGMT_ADV_FLAG_TX_POWER)) {
+               tx_power_managed = true;
+               max_len -= 3;
+       }
 
        if (len > max_len)
                return false;
@@ -6543,6 +6862,12 @@ static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
        for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
                cur_len = data[i];
 
+               if (flags_managed && data[i + 1] == EIR_FLAGS)
+                       return false;
+
+               if (tx_power_managed && data[i + 1] == EIR_TX_POWER)
+                       return false;
+
                /* If the current field length would exceed the total data
                 * length, then it's invalid.
                 */
@@ -6607,6 +6932,7 @@ static int add_advertising(struct sock *sk, struct hci_dev *hdev,
        struct mgmt_cp_add_advertising *cp = data;
        struct mgmt_rp_add_advertising rp;
        u32 flags;
+       u32 supported_flags;
        u8 status;
        u16 timeout;
        int err;
@@ -6623,10 +6949,11 @@ static int add_advertising(struct sock *sk, struct hci_dev *hdev,
        flags = __le32_to_cpu(cp->flags);
        timeout = __le16_to_cpu(cp->timeout);
 
-       /* The current implementation only supports adding one instance and
-        * doesn't support flags.
+       /* The current implementation only supports adding one instance and only
+        * a subset of the specified flags.
         */
-       if (cp->instance != 0x01 || flags)
+       supported_flags = get_supported_adv_flags(hdev);
+       if (cp->instance != 0x01 || (flags & ~supported_flags))
                return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
                                       MGMT_STATUS_INVALID_PARAMS);
 
@@ -6646,9 +6973,9 @@ static int add_advertising(struct sock *sk, struct hci_dev *hdev,
                goto unlock;
        }
 
-       if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len) ||
+       if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
            !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
-                              cp->scan_rsp_len)) {
+                              cp->scan_rsp_len, false)) {
                err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
                                      MGMT_STATUS_INVALID_PARAMS);
                goto unlock;
@@ -7820,43 +8147,6 @@ void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
                           cmd ? cmd->sk : NULL);
 }
 
-void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192,
-                                      u8 *rand192, u8 *hash256, u8 *rand256,
-                                      u8 status)
-{
-       struct mgmt_pending_cmd *cmd;
-
-       BT_DBG("%s status %u", hdev->name, status);
-
-       cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
-       if (!cmd)
-               return;
-
-       if (status) {
-               mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
-                               mgmt_status(status));
-       } else {
-               struct mgmt_rp_read_local_oob_data rp;
-               size_t rp_size = sizeof(rp);
-
-               memcpy(rp.hash192, hash192, sizeof(rp.hash192));
-               memcpy(rp.rand192, rand192, sizeof(rp.rand192));
-
-               if (bredr_sc_enabled(hdev) && hash256 && rand256) {
-                       memcpy(rp.hash256, hash256, sizeof(rp.hash256));
-                       memcpy(rp.rand256, rand256, sizeof(rp.rand256));
-               } else {
-                       rp_size -= sizeof(rp.hash256) + sizeof(rp.rand256);
-               }
-
-               mgmt_cmd_complete(cmd->sk, hdev->id,
-                                 MGMT_OP_READ_LOCAL_OOB_DATA, 0,
-                                 &rp, rp_size);
-       }
-
-       mgmt_pending_remove(cmd);
-}
-
 static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
 {
        int i;