diff options
author | Michał Narajowski <michal.narajowski@codecoup.pl> | 2016-10-19 04:20:27 -0400 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2016-10-19 12:42:37 -0400 |
commit | f61851f64b171a684f5a1fa78325756dbbaadadc (patch) | |
tree | a6296b4aecf3903a33de96608eef7c27bcbd1663 /net/bluetooth | |
parent | 5e2bd93b8fcac8c0cf83f189d996831fb21f2db3 (diff) |
Bluetooth: Fix append max 11 bytes of name to scan rsp data
Append maximum of 10 + 1 bytes of name to scan response data.
Complete name is appended only if exists and is <= 10 characters.
Else append short name if exists or shorten complete name if not.
This makes sure name is consistent across multiple advertising
instances.
Signed-off-by: Michał Narajowski <michal.narajowski@codecoup.pl>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Diffstat (limited to 'net/bluetooth')
-rw-r--r-- | net/bluetooth/hci_request.c | 49 | ||||
-rw-r--r-- | net/bluetooth/hci_request.h | 2 | ||||
-rw-r--r-- | net/bluetooth/mgmt.c | 26 |
3 files changed, 42 insertions, 35 deletions
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c index e2288421fe6b..1015d9c8d97d 100644 --- a/net/bluetooth/hci_request.c +++ b/net/bluetooth/hci_request.c | |||
@@ -969,41 +969,38 @@ void __hci_req_enable_advertising(struct hci_request *req) | |||
969 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); | 969 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
970 | } | 970 | } |
971 | 971 | ||
972 | static u8 append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len) | 972 | u8 append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len) |
973 | { | 973 | { |
974 | size_t complete_len; | ||
975 | size_t short_len; | 974 | size_t short_len; |
976 | int max_len; | 975 | size_t complete_len; |
977 | |||
978 | max_len = HCI_MAX_AD_LENGTH - ad_len - 2; | ||
979 | complete_len = strlen(hdev->dev_name); | ||
980 | short_len = strlen(hdev->short_name); | ||
981 | |||
982 | /* no space left for name */ | ||
983 | if (max_len < 1) | ||
984 | return ad_len; | ||
985 | 976 | ||
986 | /* no name set */ | 977 | /* no space left for name (+ NULL + type + len) */ |
987 | if (!complete_len) | 978 | if ((HCI_MAX_AD_LENGTH - ad_len) < HCI_MAX_SHORT_NAME_LENGTH + 3) |
988 | return ad_len; | 979 | return ad_len; |
989 | 980 | ||
990 | /* complete name fits and is eq to max short name len or smaller */ | 981 | /* use complete name if present and fits */ |
991 | if (complete_len <= max_len && | 982 | complete_len = strlen(hdev->dev_name); |
992 | complete_len <= HCI_MAX_SHORT_NAME_LENGTH) { | 983 | if (complete_len && complete_len <= HCI_MAX_SHORT_NAME_LENGTH) |
993 | return eir_append_data(ptr, ad_len, EIR_NAME_COMPLETE, | 984 | return eir_append_data(ptr, ad_len, EIR_NAME_COMPLETE, |
994 | hdev->dev_name, complete_len); | 985 | hdev->dev_name, complete_len + 1); |
995 | } | ||
996 | 986 | ||
997 | /* short name set and fits */ | 987 | /* use short name if present */ |
998 | if (short_len && short_len <= max_len) { | 988 | short_len = strlen(hdev->short_name); |
989 | if (short_len) | ||
999 | return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, | 990 | return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, |
1000 | hdev->short_name, short_len); | 991 | hdev->short_name, short_len + 1); |
1001 | } | ||
1002 | 992 | ||
1003 | /* no short name set so shorten complete name */ | 993 | /* use shortened full name if present, we already know that name |
1004 | if (!short_len) { | 994 | * is longer then HCI_MAX_SHORT_NAME_LENGTH |
1005 | return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, | 995 | */ |
1006 | hdev->dev_name, max_len); | 996 | if (complete_len) { |
997 | u8 name[HCI_MAX_SHORT_NAME_LENGTH + 1]; | ||
998 | |||
999 | memcpy(name, hdev->dev_name, HCI_MAX_SHORT_NAME_LENGTH); | ||
1000 | name[HCI_MAX_SHORT_NAME_LENGTH] = '\0'; | ||
1001 | |||
1002 | return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, name, | ||
1003 | sizeof(name)); | ||
1007 | } | 1004 | } |
1008 | 1005 | ||
1009 | return ad_len; | 1006 | return ad_len; |
diff --git a/net/bluetooth/hci_request.h b/net/bluetooth/hci_request.h index 6b06629245a8..dde77bd59f91 100644 --- a/net/bluetooth/hci_request.h +++ b/net/bluetooth/hci_request.h | |||
@@ -106,6 +106,8 @@ static inline void hci_update_background_scan(struct hci_dev *hdev) | |||
106 | void hci_request_setup(struct hci_dev *hdev); | 106 | void hci_request_setup(struct hci_dev *hdev); |
107 | void hci_request_cancel_all(struct hci_dev *hdev); | 107 | void hci_request_cancel_all(struct hci_dev *hdev); |
108 | 108 | ||
109 | u8 append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len); | ||
110 | |||
109 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, | 111 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, |
110 | u8 *data, u8 data_len) | 112 | u8 *data, u8 data_len) |
111 | { | 113 | { |
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c index 736038085feb..1fba2a03f8ae 100644 --- a/net/bluetooth/mgmt.c +++ b/net/bluetooth/mgmt.c | |||
@@ -6017,7 +6017,15 @@ static int read_adv_features(struct sock *sk, struct hci_dev *hdev, | |||
6017 | return err; | 6017 | return err; |
6018 | } | 6018 | } |
6019 | 6019 | ||
6020 | static u8 tlv_data_max_len(u32 adv_flags, bool is_adv_data) | 6020 | static u8 calculate_name_len(struct hci_dev *hdev) |
6021 | { | ||
6022 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; | ||
6023 | |||
6024 | return append_local_name(hdev, buf, 0); | ||
6025 | } | ||
6026 | |||
6027 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, | ||
6028 | bool is_adv_data) | ||
6021 | { | 6029 | { |
6022 | u8 max_len = HCI_MAX_AD_LENGTH; | 6030 | u8 max_len = HCI_MAX_AD_LENGTH; |
6023 | 6031 | ||
@@ -6030,9 +6038,8 @@ static u8 tlv_data_max_len(u32 adv_flags, bool is_adv_data) | |||
6030 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) | 6038 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
6031 | max_len -= 3; | 6039 | max_len -= 3; |
6032 | } else { | 6040 | } else { |
6033 | /* at least 1 byte of name should fit in */ | ||
6034 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) | 6041 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
6035 | max_len -= 3; | 6042 | max_len -= calculate_name_len(hdev); |
6036 | 6043 | ||
6037 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) | 6044 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
6038 | max_len -= 4; | 6045 | max_len -= 4; |
@@ -6063,12 +6070,13 @@ static bool appearance_managed(u32 adv_flags) | |||
6063 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; | 6070 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
6064 | } | 6071 | } |
6065 | 6072 | ||
6066 | static bool tlv_data_is_valid(u32 adv_flags, u8 *data, u8 len, bool is_adv_data) | 6073 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
6074 | u8 len, bool is_adv_data) | ||
6067 | { | 6075 | { |
6068 | int i, cur_len; | 6076 | int i, cur_len; |
6069 | u8 max_len; | 6077 | u8 max_len; |
6070 | 6078 | ||
6071 | max_len = tlv_data_max_len(adv_flags, is_adv_data); | 6079 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
6072 | 6080 | ||
6073 | if (len > max_len) | 6081 | if (len > max_len) |
6074 | return false; | 6082 | return false; |
@@ -6215,8 +6223,8 @@ static int add_advertising(struct sock *sk, struct hci_dev *hdev, | |||
6215 | goto unlock; | 6223 | goto unlock; |
6216 | } | 6224 | } |
6217 | 6225 | ||
6218 | if (!tlv_data_is_valid(flags, cp->data, cp->adv_data_len, true) || | 6226 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
6219 | !tlv_data_is_valid(flags, cp->data + cp->adv_data_len, | 6227 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
6220 | cp->scan_rsp_len, false)) { | 6228 | cp->scan_rsp_len, false)) { |
6221 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, | 6229 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
6222 | MGMT_STATUS_INVALID_PARAMS); | 6230 | MGMT_STATUS_INVALID_PARAMS); |
@@ -6429,8 +6437,8 @@ static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, | |||
6429 | 6437 | ||
6430 | rp.instance = cp->instance; | 6438 | rp.instance = cp->instance; |
6431 | rp.flags = cp->flags; | 6439 | rp.flags = cp->flags; |
6432 | rp.max_adv_data_len = tlv_data_max_len(flags, true); | 6440 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
6433 | rp.max_scan_rsp_len = tlv_data_max_len(flags, false); | 6441 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
6434 | 6442 | ||
6435 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, | 6443 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
6436 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); | 6444 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |