···231231 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);232232}233233234234-void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],234234+void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,235235 __u8 ltk[16])236236{237237 struct hci_dev *hdev = conn->hdev;···242242 memset(&cp, 0, sizeof(cp));243243244244 cp.handle = cpu_to_le16(conn->handle);245245- memcpy(cp.ltk, ltk, sizeof(cp.ltk));245245+ cp.rand = rand;246246 cp.ediv = ediv;247247- memcpy(cp.rand, rand, sizeof(cp.rand));247247+ memcpy(cp.ltk, ltk, sizeof(cp.ltk));248248249249 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);250250}···363363 &conn->dst);364364}365365366366+static void le_conn_timeout(struct work_struct *work)367367+{368368+ struct hci_conn *conn = container_of(work, struct hci_conn,369369+ le_conn_timeout.work);370370+371371+ BT_DBG("");372372+373373+ hci_le_create_connection_cancel(conn);374374+}375375+366376struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)367377{368378 struct hci_conn *conn;···420410 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);421411 INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);422412 INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);413413+ INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);423414424415 atomic_set(&conn->refcnt, 0);425416···453442 /* Unacked frames */454443 hdev->acl_cnt += conn->sent;455444 } else if (conn->type == LE_LINK) {445445+ cancel_delayed_work_sync(&conn->le_conn_timeout);446446+456447 if (hdev->le_pkts)457448 hdev->le_cnt += conn->sent;458449 else···528515EXPORT_SYMBOL(hci_get_route);529516530517/* This function requires the caller holds hdev->lock */531531-static void le_conn_failed(struct hci_conn *conn, u8 status)518518+void hci_le_conn_failed(struct hci_conn *conn, u8 status)532519{533520 struct hci_dev *hdev = conn->hdev;534521···540527 hci_proto_connect_cfm(conn, status);541528542529 hci_conn_del(conn);530530+531531+ /* Since we may have temporarily stopped the background scanning in532532+ * favor of connection establishment, we should restart it.533533+ */534534+ hci_update_background_scan(hdev);543535}544536545537static void create_le_conn_complete(struct hci_dev *hdev, u8 status)···563545 if (!conn)564546 goto done;565547566566- le_conn_failed(conn, status);548548+ hci_le_conn_failed(conn, status);567549568550done:569551 hci_dev_unlock(hdev);570552}571553572572-static int hci_create_le_conn(struct hci_conn *conn)554554+static void hci_req_add_le_create_conn(struct hci_request *req,555555+ struct hci_conn *conn)573556{574574- struct hci_dev *hdev = conn->hdev;575557 struct hci_cp_le_create_conn cp;576576- struct hci_request req;577577- int err;578578-579579- hci_req_init(&req, hdev);558558+ struct hci_dev *hdev = conn->hdev;559559+ u8 own_addr_type;580560581561 memset(&cp, 0, sizeof(cp));562562+563563+ /* Update random address, but set require_privacy to false so564564+ * that we never connect with an unresolvable address.565565+ */566566+ if (hci_update_random_address(req, false, &own_addr_type))567567+ return;568568+569569+ /* Save the address type used for this connnection attempt so we able570570+ * to retrieve this information if we need it.571571+ */572572+ conn->src_type = own_addr_type;573573+582574 cp.scan_interval = cpu_to_le16(hdev->le_scan_interval);583575 cp.scan_window = cpu_to_le16(hdev->le_scan_window);584576 bacpy(&cp.peer_addr, &conn->dst);585577 cp.peer_addr_type = conn->dst_type;586586- cp.own_address_type = conn->src_type;578578+ cp.own_address_type = own_addr_type;587579 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);588580 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);589581 cp.supervision_timeout = __constant_cpu_to_le16(0x002a);590582 cp.min_ce_len = __constant_cpu_to_le16(0x0000);591583 cp.max_ce_len = __constant_cpu_to_le16(0x0000);592584593593- hci_req_add(&req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);585585+ hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);594586595595- err = hci_req_run(&req, create_le_conn_complete);596596- if (err) {597597- hci_conn_del(conn);598598- return err;599599- }600600-601601- return 0;587587+ conn->state = BT_CONNECT;602588}603589604604-static struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,605605- u8 dst_type, u8 sec_level, u8 auth_type)590590+struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,591591+ u8 dst_type, u8 sec_level, u8 auth_type)606592{607593 struct hci_conn_params *params;608594 struct hci_conn *conn;609595 struct smp_irk *irk;596596+ struct hci_request req;610597 int err;611598612599 if (test_bit(HCI_ADVERTISING, &hdev->flags))···640617 if (conn)641618 return ERR_PTR(-EBUSY);642619643643- /* Convert from L2CAP channel address type to HCI address type */644644- if (dst_type == BDADDR_LE_PUBLIC)645645- dst_type = ADDR_LE_DEV_PUBLIC;646646- else647647- dst_type = ADDR_LE_DEV_RANDOM;648648-649620 /* When given an identity address with existing identity650621 * resolving key, the connection needs to be established651622 * to a resolvable random address.···664647 return ERR_PTR(-ENOMEM);665648666649 conn->dst_type = dst_type;667667- conn->src_type = hdev->own_addr_type;668650669669- conn->state = BT_CONNECT;670651 conn->out = true;671652 conn->link_mode |= HCI_LM_MASTER;672653 conn->sec_level = BT_SECURITY_LOW;···680665 conn->le_conn_max_interval = hdev->le_conn_max_interval;681666 }682667683683- err = hci_create_le_conn(conn);684684- if (err)668668+ hci_req_init(&req, hdev);669669+670670+ /* If controller is scanning, we stop it since some controllers are671671+ * not able to scan and connect at the same time. Also set the672672+ * HCI_LE_SCAN_INTERRUPTED flag so that the command complete673673+ * handler for scan disabling knows to set the correct discovery674674+ * state.675675+ */676676+ if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {677677+ hci_req_add_le_scan_disable(&req);678678+ set_bit(HCI_LE_SCAN_INTERRUPTED, &hdev->dev_flags);679679+ }680680+681681+ hci_req_add_le_create_conn(&req, conn);682682+683683+ err = hci_req_run(&req, create_le_conn_complete);684684+ if (err) {685685+ hci_conn_del(conn);685686 return ERR_PTR(err);687687+ }686688687689done:688690 hci_conn_hold(conn);689691 return conn;690692}691693692692-static struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,693693- u8 sec_level, u8 auth_type)694694+struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,695695+ u8 sec_level, u8 auth_type)694696{695697 struct hci_conn *acl;696698···774742 }775743776744 return sco;777777-}778778-779779-/* Create SCO, ACL or LE connection. */780780-struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst,781781- __u8 dst_type, __u8 sec_level, __u8 auth_type)782782-{783783- BT_DBG("%s dst %pMR type 0x%x", hdev->name, dst, type);784784-785785- switch (type) {786786- case LE_LINK:787787- return hci_connect_le(hdev, dst, dst_type, sec_level, auth_type);788788- case ACL_LINK:789789- return hci_connect_acl(hdev, dst, sec_level, auth_type);790790- }791791-792792- return ERR_PTR(-EINVAL);793745}794746795747/* Check link security requirement */
+638-43
net/bluetooth/hci_core.c
···492492DEFINE_SIMPLE_ATTRIBUTE(idle_timeout_fops, idle_timeout_get,493493 idle_timeout_set, "%llu\n");494494495495+static int rpa_timeout_set(void *data, u64 val)496496+{497497+ struct hci_dev *hdev = data;498498+499499+ /* Require the RPA timeout to be at least 30 seconds and at most500500+ * 24 hours.501501+ */502502+ if (val < 30 || val > (60 * 60 * 24))503503+ return -EINVAL;504504+505505+ hci_dev_lock(hdev);506506+ hdev->rpa_timeout = val;507507+ hci_dev_unlock(hdev);508508+509509+ return 0;510510+}511511+512512+static int rpa_timeout_get(void *data, u64 *val)513513+{514514+ struct hci_dev *hdev = data;515515+516516+ hci_dev_lock(hdev);517517+ *val = hdev->rpa_timeout;518518+ hci_dev_unlock(hdev);519519+520520+ return 0;521521+}522522+523523+DEFINE_SIMPLE_ATTRIBUTE(rpa_timeout_fops, rpa_timeout_get,524524+ rpa_timeout_set, "%llu\n");525525+495526static int sniff_min_interval_set(void *data, u64 val)496527{497528 struct hci_dev *hdev = data;···578547579548DEFINE_SIMPLE_ATTRIBUTE(sniff_max_interval_fops, sniff_max_interval_get,580549 sniff_max_interval_set, "%llu\n");550550+551551+static int identity_show(struct seq_file *f, void *p)552552+{553553+ struct hci_dev *hdev = f->private;554554+ bdaddr_t addr;555555+ u8 addr_type;556556+557557+ hci_dev_lock(hdev);558558+559559+ hci_copy_identity_address(hdev, &addr, &addr_type);560560+561561+ seq_printf(f, "%pMR (type %u) %*phN %pMR\n", &addr, addr_type,562562+ 16, hdev->irk, &hdev->rpa);563563+564564+ hci_dev_unlock(hdev);565565+566566+ return 0;567567+}568568+569569+static int identity_open(struct inode *inode, struct file *file)570570+{571571+ return single_open(file, identity_show, inode->i_private);572572+}573573+574574+static const struct file_operations identity_fops = {575575+ .open = identity_open,576576+ .read = seq_read,577577+ .llseek = seq_lseek,578578+ .release = single_release,579579+};581580582581static int random_address_show(struct seq_file *f, void *p)583582{···702641 .llseek = default_llseek,703642};704643644644+static int white_list_show(struct seq_file *f, void *ptr)645645+{646646+ struct hci_dev *hdev = f->private;647647+ struct bdaddr_list *b;648648+649649+ hci_dev_lock(hdev);650650+ list_for_each_entry(b, &hdev->le_white_list, list)651651+ seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);652652+ hci_dev_unlock(hdev);653653+654654+ return 0;655655+}656656+657657+static int white_list_open(struct inode *inode, struct file *file)658658+{659659+ return single_open(file, white_list_show, inode->i_private);660660+}661661+662662+static const struct file_operations white_list_fops = {663663+ .open = white_list_open,664664+ .read = seq_read,665665+ .llseek = seq_lseek,666666+ .release = single_release,667667+};668668+705669static int identity_resolving_keys_show(struct seq_file *f, void *ptr)706670{707671 struct hci_dev *hdev = f->private;···765679 hci_dev_lock(hdev);766680 list_for_each_safe(p, n, &hdev->long_term_keys) {767681 struct smp_ltk *ltk = list_entry(p, struct smp_ltk, list);768768- seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %*phN %*phN\n",682682+ seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %.16llx %*phN\n",769683 <k->bdaddr, ltk->bdaddr_type, ltk->authenticated,770684 ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv),771771- 8, ltk->rand, 16, ltk->val);685685+ __le64_to_cpu(ltk->rand), 16, ltk->val);772686 }773687 hci_dev_unlock(hdev);774688···912826 .read = lowpan_read,913827 .write = lowpan_write,914828 .llseek = default_llseek,829829+};830830+831831+static int le_auto_conn_show(struct seq_file *sf, void *ptr)832832+{833833+ struct hci_dev *hdev = sf->private;834834+ struct hci_conn_params *p;835835+836836+ hci_dev_lock(hdev);837837+838838+ list_for_each_entry(p, &hdev->le_conn_params, list) {839839+ seq_printf(sf, "%pMR %u %u\n", &p->addr, p->addr_type,840840+ p->auto_connect);841841+ }842842+843843+ hci_dev_unlock(hdev);844844+845845+ return 0;846846+}847847+848848+static int le_auto_conn_open(struct inode *inode, struct file *file)849849+{850850+ return single_open(file, le_auto_conn_show, inode->i_private);851851+}852852+853853+static ssize_t le_auto_conn_write(struct file *file, const char __user *data,854854+ size_t count, loff_t *offset)855855+{856856+ struct seq_file *sf = file->private_data;857857+ struct hci_dev *hdev = sf->private;858858+ u8 auto_connect = 0;859859+ bdaddr_t addr;860860+ u8 addr_type;861861+ char *buf;862862+ int err = 0;863863+ int n;864864+865865+ /* Don't allow partial write */866866+ if (*offset != 0)867867+ return -EINVAL;868868+869869+ if (count < 3)870870+ return -EINVAL;871871+872872+ buf = kzalloc(count, GFP_KERNEL);873873+ if (!buf)874874+ return -ENOMEM;875875+876876+ if (copy_from_user(buf, data, count)) {877877+ err = -EFAULT;878878+ goto done;879879+ }880880+881881+ if (memcmp(buf, "add", 3) == 0) {882882+ n = sscanf(&buf[4], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu %hhu",883883+ &addr.b[5], &addr.b[4], &addr.b[3], &addr.b[2],884884+ &addr.b[1], &addr.b[0], &addr_type,885885+ &auto_connect);886886+887887+ if (n < 7) {888888+ err = -EINVAL;889889+ goto done;890890+ }891891+892892+ hci_dev_lock(hdev);893893+ err = hci_conn_params_add(hdev, &addr, addr_type, auto_connect,894894+ hdev->le_conn_min_interval,895895+ hdev->le_conn_max_interval);896896+ hci_dev_unlock(hdev);897897+898898+ if (err)899899+ goto done;900900+ } else if (memcmp(buf, "del", 3) == 0) {901901+ n = sscanf(&buf[4], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",902902+ &addr.b[5], &addr.b[4], &addr.b[3], &addr.b[2],903903+ &addr.b[1], &addr.b[0], &addr_type);904904+905905+ if (n < 7) {906906+ err = -EINVAL;907907+ goto done;908908+ }909909+910910+ hci_dev_lock(hdev);911911+ hci_conn_params_del(hdev, &addr, addr_type);912912+ hci_dev_unlock(hdev);913913+ } else if (memcmp(buf, "clr", 3) == 0) {914914+ hci_dev_lock(hdev);915915+ hci_conn_params_clear(hdev);916916+ hci_pend_le_conns_clear(hdev);917917+ hci_update_background_scan(hdev);918918+ hci_dev_unlock(hdev);919919+ } else {920920+ err = -EINVAL;921921+ }922922+923923+done:924924+ kfree(buf);925925+926926+ if (err)927927+ return err;928928+ else929929+ return count;930930+}931931+932932+static const struct file_operations le_auto_conn_fops = {933933+ .open = le_auto_conn_open,934934+ .read = seq_read,935935+ .write = le_auto_conn_write,936936+ .llseek = seq_lseek,937937+ .release = single_release,915938};916939917940/* ---- HCI requests ---- */···13711176 /* Read LE Local Supported Features */13721177 hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL);1373117811791179+ /* Read LE Supported States */11801180+ hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL);11811181+13741182 /* Read LE Advertising Channel TX Power */13751183 hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL);1376118413771185 /* Read LE White List Size */13781186 hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL);1379118713801380- /* Read LE Supported States */13811381- hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL);11881188+ /* Clear LE White List */11891189+ hci_req_add(req, HCI_OP_LE_CLEAR_WHITE_LIST, 0, NULL);1382119013831191 /* LE-only controllers have LE implicitly enabled */13841192 if (!lmp_bredr_capable(hdev))···16731475 if (hdev->commands[5] & 0x10)16741476 hci_setup_link_policy(req);1675147716761676- if (lmp_le_capable(hdev)) {16771677- /* If the controller has a public BD_ADDR, then by default16781678- * use that one. If this is a LE only controller without16791679- * a public address, default to the random address.16801680- *16811681- * For debugging purposes it is possible to force16821682- * controllers with a public address to use the16831683- * random address instead.16841684- */16851685- if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dev_flags) ||16861686- !bacmp(&hdev->bdaddr, BDADDR_ANY))16871687- hdev->own_addr_type = ADDR_LE_DEV_RANDOM;16881688- else16891689- hdev->own_addr_type = ADDR_LE_DEV_PUBLIC;16901690-14781478+ if (lmp_le_capable(hdev))16911479 hci_set_le_support(req);16921692- }1693148016941481 /* Read features beyond page 1 if available */16951482 for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) {···17911608 }1792160917931610 if (lmp_le_capable(hdev)) {16111611+ debugfs_create_file("identity", 0400, hdev->debugfs,16121612+ hdev, &identity_fops);16131613+ debugfs_create_file("rpa_timeout", 0644, hdev->debugfs,16141614+ hdev, &rpa_timeout_fops);17941615 debugfs_create_file("random_address", 0444, hdev->debugfs,17951616 hdev, &random_address_fops);17961617 debugfs_create_file("static_address", 0444, hdev->debugfs,···1811162418121625 debugfs_create_u8("white_list_size", 0444, hdev->debugfs,18131626 &hdev->le_white_list_size);16271627+ debugfs_create_file("white_list", 0444, hdev->debugfs, hdev,16281628+ &white_list_fops);18141629 debugfs_create_file("identity_resolving_keys", 0400,18151630 hdev->debugfs, hdev,18161631 &identity_resolving_keys_fops);···18261637 hdev, &adv_channel_map_fops);18271638 debugfs_create_file("6lowpan", 0644, hdev->debugfs, hdev,18281639 &lowpan_debugfs_fops);16401640+ debugfs_create_file("le_auto_conn", 0644, hdev->debugfs, hdev,16411641+ &le_auto_conn_fops);18291642 }1830164318311644 return 0;···1920172919211730 switch (state) {19221731 case DISCOVERY_STOPPED:17321732+ hci_update_background_scan(hdev);17331733+19231734 if (hdev->discovery.state != DISCOVERY_STARTING)19241735 mgmt_discovering(hdev, 0);19251736 break;···2295210222962103 if (!ret) {22972104 hci_dev_hold(hdev);21052105+ set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);22982106 set_bit(HCI_UP, &hdev->flags);22992107 hci_notify(hdev, HCI_DEV_UP);23002108 if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&···2394220023952201 cancel_delayed_work_sync(&hdev->le_scan_disable);2396220222032203+ if (test_bit(HCI_MGMT, &hdev->dev_flags))22042204+ cancel_delayed_work_sync(&hdev->rpa_expired);22052205+23972206 hci_dev_lock(hdev);23982207 hci_inquiry_cache_flush(hdev);23992208 hci_conn_hash_flush(hdev);22092209+ hci_pend_le_conns_clear(hdev);24002210 hci_dev_unlock(hdev);2401221124022212 hci_notify(hdev, HCI_DEV_DOWN);···29212723 return false;29222724}2923272529242924-struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8],27262726+struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, __le64 rand,29252727 bool master)29262728{29272729 struct smp_ltk *k;2928273029292731 list_for_each_entry(k, &hdev->long_term_keys, list) {29302930- if (k->ediv != ediv ||29312931- memcmp(rand, k->rand, sizeof(k->rand)))27322732+ if (k->ediv != ediv || k->rand != rand)29322733 continue;2933273429342735 if (ltk_type_master(k->type) != master)···3045284830462849struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,30472850 u8 addr_type, u8 type, u8 authenticated,30483048- u8 tk[16], u8 enc_size, __le16 ediv, u8 rand[8])28512851+ u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand)30492852{30502853 struct smp_ltk *key, *old_key;30512854 bool master = ltk_type_master(type);···30652868 memcpy(key->val, tk, sizeof(key->val));30662869 key->authenticated = authenticated;30672870 key->ediv = ediv;28712871+ key->rand = rand;30682872 key->enc_size = enc_size;30692873 key->type = type;30703070- memcpy(key->rand, rand, sizeof(key->rand));3071287430722875 return key;30732876}···32673070 return NULL;32683071}3269307232703270-void hci_blacklist_clear(struct hci_dev *hdev)30733073+static void hci_blacklist_clear(struct hci_dev *hdev)32713074{32723075 struct list_head *p, *n;32733076···33203123 return mgmt_device_unblocked(hdev, bdaddr, type);33213124}3322312531263126+struct bdaddr_list *hci_white_list_lookup(struct hci_dev *hdev,31273127+ bdaddr_t *bdaddr, u8 type)31283128+{31293129+ struct bdaddr_list *b;31303130+31313131+ list_for_each_entry(b, &hdev->le_white_list, list) {31323132+ if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type)31333133+ return b;31343134+ }31353135+31363136+ return NULL;31373137+}31383138+31393139+void hci_white_list_clear(struct hci_dev *hdev)31403140+{31413141+ struct list_head *p, *n;31423142+31433143+ list_for_each_safe(p, n, &hdev->le_white_list) {31443144+ struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list);31453145+31463146+ list_del(p);31473147+ kfree(b);31483148+ }31493149+}31503150+31513151+int hci_white_list_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)31523152+{31533153+ struct bdaddr_list *entry;31543154+31553155+ if (!bacmp(bdaddr, BDADDR_ANY))31563156+ return -EBADF;31573157+31583158+ entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);31593159+ if (!entry)31603160+ return -ENOMEM;31613161+31623162+ bacpy(&entry->bdaddr, bdaddr);31633163+ entry->bdaddr_type = type;31643164+31653165+ list_add(&entry->list, &hdev->le_white_list);31663166+31673167+ return 0;31683168+}31693169+31703170+int hci_white_list_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)31713171+{31723172+ struct bdaddr_list *entry;31733173+31743174+ if (!bacmp(bdaddr, BDADDR_ANY))31753175+ return -EBADF;31763176+31773177+ entry = hci_white_list_lookup(hdev, bdaddr, type);31783178+ if (!entry)31793179+ return -ENOENT;31803180+31813181+ list_del(&entry->list);31823182+ kfree(entry);31833183+31843184+ return 0;31853185+}31863186+33233187/* This function requires the caller holds hdev->lock */33243188struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,33253189 bdaddr_t *addr, u8 addr_type)···33973139 return NULL;33983140}3399314131423142+static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)31433143+{31443144+ struct hci_conn *conn;31453145+31463146+ conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);31473147+ if (!conn)31483148+ return false;31493149+31503150+ if (conn->dst_type != type)31513151+ return false;31523152+31533153+ if (conn->state != BT_CONNECTED)31543154+ return false;31553155+31563156+ return true;31573157+}31583158+31593159+static bool is_identity_address(bdaddr_t *addr, u8 addr_type)31603160+{31613161+ if (addr_type == ADDR_LE_DEV_PUBLIC)31623162+ return true;31633163+31643164+ /* Check for Random Static address type */31653165+ if ((addr->b[5] & 0xc0) == 0xc0)31663166+ return true;31673167+31683168+ return false;31693169+}31703170+34003171/* This function requires the caller holds hdev->lock */34013401-void hci_conn_params_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type,34023402- u16 conn_min_interval, u16 conn_max_interval)31723172+int hci_conn_params_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type,31733173+ u8 auto_connect, u16 conn_min_interval,31743174+ u16 conn_max_interval)34033175{34043176 struct hci_conn_params *params;3405317731783178+ if (!is_identity_address(addr, addr_type))31793179+ return -EINVAL;31803180+34063181 params = hci_conn_params_lookup(hdev, addr, addr_type);34073407- if (params) {34083408- params->conn_min_interval = conn_min_interval;34093409- params->conn_max_interval = conn_max_interval;34103410- return;34113411- }31823182+ if (params)31833183+ goto update;3412318434133185 params = kzalloc(sizeof(*params), GFP_KERNEL);34143186 if (!params) {34153187 BT_ERR("Out of memory");34163416- return;31883188+ return -ENOMEM;34173189 }3418319034193191 bacpy(¶ms->addr, addr);34203192 params->addr_type = addr_type;34213421- params->conn_min_interval = conn_min_interval;34223422- params->conn_max_interval = conn_max_interval;3423319334243194 list_add(¶ms->list, &hdev->le_conn_params);3425319534263426- BT_DBG("addr %pMR (type %u) conn_min_interval 0x%.4x "34273427- "conn_max_interval 0x%.4x", addr, addr_type, conn_min_interval,34283428- conn_max_interval);31963196+update:31973197+ params->conn_min_interval = conn_min_interval;31983198+ params->conn_max_interval = conn_max_interval;31993199+ params->auto_connect = auto_connect;32003200+32013201+ switch (auto_connect) {32023202+ case HCI_AUTO_CONN_DISABLED:32033203+ case HCI_AUTO_CONN_LINK_LOSS:32043204+ hci_pend_le_conn_del(hdev, addr, addr_type);32053205+ break;32063206+ case HCI_AUTO_CONN_ALWAYS:32073207+ if (!is_connected(hdev, addr, addr_type))32083208+ hci_pend_le_conn_add(hdev, addr, addr_type);32093209+ break;32103210+ }32113211+32123212+ BT_DBG("addr %pMR (type %u) auto_connect %u conn_min_interval 0x%.4x "32133213+ "conn_max_interval 0x%.4x", addr, addr_type, auto_connect,32143214+ conn_min_interval, conn_max_interval);32153215+32163216+ return 0;34293217}3430321834313219/* This function requires the caller holds hdev->lock */···34823178 params = hci_conn_params_lookup(hdev, addr, addr_type);34833179 if (!params)34843180 return;31813181+31823182+ hci_pend_le_conn_del(hdev, addr, addr_type);3485318334863184 list_del(¶ms->list);34873185 kfree(params);···35023196 }3503319735043198 BT_DBG("All LE connection parameters were removed");31993199+}32003200+32013201+/* This function requires the caller holds hdev->lock */32023202+struct bdaddr_list *hci_pend_le_conn_lookup(struct hci_dev *hdev,32033203+ bdaddr_t *addr, u8 addr_type)32043204+{32053205+ struct bdaddr_list *entry;32063206+32073207+ list_for_each_entry(entry, &hdev->pend_le_conns, list) {32083208+ if (bacmp(&entry->bdaddr, addr) == 0 &&32093209+ entry->bdaddr_type == addr_type)32103210+ return entry;32113211+ }32123212+32133213+ return NULL;32143214+}32153215+32163216+/* This function requires the caller holds hdev->lock */32173217+void hci_pend_le_conn_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)32183218+{32193219+ struct bdaddr_list *entry;32203220+32213221+ entry = hci_pend_le_conn_lookup(hdev, addr, addr_type);32223222+ if (entry)32233223+ goto done;32243224+32253225+ entry = kzalloc(sizeof(*entry), GFP_KERNEL);32263226+ if (!entry) {32273227+ BT_ERR("Out of memory");32283228+ return;32293229+ }32303230+32313231+ bacpy(&entry->bdaddr, addr);32323232+ entry->bdaddr_type = addr_type;32333233+32343234+ list_add(&entry->list, &hdev->pend_le_conns);32353235+32363236+ BT_DBG("addr %pMR (type %u)", addr, addr_type);32373237+32383238+done:32393239+ hci_update_background_scan(hdev);32403240+}32413241+32423242+/* This function requires the caller holds hdev->lock */32433243+void hci_pend_le_conn_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)32443244+{32453245+ struct bdaddr_list *entry;32463246+32473247+ entry = hci_pend_le_conn_lookup(hdev, addr, addr_type);32483248+ if (!entry)32493249+ goto done;32503250+32513251+ list_del(&entry->list);32523252+ kfree(entry);32533253+32543254+ BT_DBG("addr %pMR (type %u)", addr, addr_type);32553255+32563256+done:32573257+ hci_update_background_scan(hdev);32583258+}32593259+32603260+/* This function requires the caller holds hdev->lock */32613261+void hci_pend_le_conns_clear(struct hci_dev *hdev)32623262+{32633263+ struct bdaddr_list *entry, *tmp;32643264+32653265+ list_for_each_entry_safe(entry, tmp, &hdev->pend_le_conns, list) {32663266+ list_del(&entry->list);32673267+ kfree(entry);32683268+ }32693269+32703270+ BT_DBG("All LE pending connections cleared");35053271}3506327235073273static void inquiry_complete(struct hci_dev *hdev, u8 status)···36353257{36363258 struct hci_dev *hdev = container_of(work, struct hci_dev,36373259 le_scan_disable.work);36383638- struct hci_cp_le_set_scan_enable cp;36393260 struct hci_request req;36403261 int err;36413262···3642326536433266 hci_req_init(&req, hdev);3644326736453645- memset(&cp, 0, sizeof(cp));36463646- cp.enable = LE_SCAN_DISABLE;36473647- hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);32683268+ hci_req_add_le_scan_disable(&req);3648326936493270 err = hci_req_run(&req, le_scan_disable_work_complete);36503271 if (err)36513272 BT_ERR("Disable LE scanning request failed: err %d", err);32733273+}32743274+32753275+static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)32763276+{32773277+ struct hci_dev *hdev = req->hdev;32783278+32793279+ /* If we're advertising or initiating an LE connection we can't32803280+ * go ahead and change the random address at this time. This is32813281+ * because the eventual initiator address used for the32823282+ * subsequently created connection will be undefined (some32833283+ * controllers use the new address and others the one we had32843284+ * when the operation started).32853285+ *32863286+ * In this kind of scenario skip the update and let the random32873287+ * address be updated at the next cycle.32883288+ */32893289+ if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) ||32903290+ hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) {32913291+ BT_DBG("Deferring random address update");32923292+ return;32933293+ }32943294+32953295+ hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa);32963296+}32973297+32983298+int hci_update_random_address(struct hci_request *req, bool require_privacy,32993299+ u8 *own_addr_type)33003300+{33013301+ struct hci_dev *hdev = req->hdev;33023302+ int err;33033303+33043304+ /* If privacy is enabled use a resolvable private address. If33053305+ * current RPA has expired or there is something else than33063306+ * the current RPA in use, then generate a new one.33073307+ */33083308+ if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) {33093309+ int to;33103310+33113311+ *own_addr_type = ADDR_LE_DEV_RANDOM;33123312+33133313+ if (!test_and_clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags) &&33143314+ !bacmp(&hdev->random_addr, &hdev->rpa))33153315+ return 0;33163316+33173317+ err = smp_generate_rpa(hdev->tfm_aes, hdev->irk, &hdev->rpa);33183318+ if (err < 0) {33193319+ BT_ERR("%s failed to generate new RPA", hdev->name);33203320+ return err;33213321+ }33223322+33233323+ set_random_addr(req, &hdev->rpa);33243324+33253325+ to = msecs_to_jiffies(hdev->rpa_timeout * 1000);33263326+ queue_delayed_work(hdev->workqueue, &hdev->rpa_expired, to);33273327+33283328+ return 0;33293329+ }33303330+33313331+ /* In case of required privacy without resolvable private address,33323332+ * use an unresolvable private address. This is useful for active33333333+ * scanning and non-connectable advertising.33343334+ */33353335+ if (require_privacy) {33363336+ bdaddr_t urpa;33373337+33383338+ get_random_bytes(&urpa, 6);33393339+ urpa.b[5] &= 0x3f; /* Clear two most significant bits */33403340+33413341+ *own_addr_type = ADDR_LE_DEV_RANDOM;33423342+ set_random_addr(req, &urpa);33433343+ return 0;33443344+ }33453345+33463346+ /* If forcing static address is in use or there is no public33473347+ * address use the static address as random address (but skip33483348+ * the HCI command if the current random address is already the33493349+ * static one.33503350+ */33513351+ if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dev_flags) ||33523352+ !bacmp(&hdev->bdaddr, BDADDR_ANY)) {33533353+ *own_addr_type = ADDR_LE_DEV_RANDOM;33543354+ if (bacmp(&hdev->static_addr, &hdev->random_addr))33553355+ hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,33563356+ &hdev->static_addr);33573357+ return 0;33583358+ }33593359+33603360+ /* Neither privacy nor static address is being used so use a33613361+ * public address.33623362+ */33633363+ *own_addr_type = ADDR_LE_DEV_PUBLIC;33643364+33653365+ return 0;33663366+}33673367+33683368+/* Copy the Identity Address of the controller.33693369+ *33703370+ * If the controller has a public BD_ADDR, then by default use that one.33713371+ * If this is a LE only controller without a public address, default to33723372+ * the static random address.33733373+ *33743374+ * For debugging purposes it is possible to force controllers with a33753375+ * public address to use the static random address instead.33763376+ */33773377+void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,33783378+ u8 *bdaddr_type)33793379+{33803380+ if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dev_flags) ||33813381+ !bacmp(&hdev->bdaddr, BDADDR_ANY)) {33823382+ bacpy(bdaddr, &hdev->static_addr);33833383+ *bdaddr_type = ADDR_LE_DEV_RANDOM;33843384+ } else {33853385+ bacpy(bdaddr, &hdev->bdaddr);33863386+ *bdaddr_type = ADDR_LE_DEV_PUBLIC;33873387+ }36523388}3653338936543390/* Alloc HCI device */···37903300 hdev->le_conn_min_interval = 0x0028;37913301 hdev->le_conn_max_interval = 0x0038;3792330233033303+ hdev->rpa_timeout = HCI_DEFAULT_RPA_TIMEOUT;33043304+37933305 mutex_init(&hdev->lock);37943306 mutex_init(&hdev->req_lock);37953307···38023310 INIT_LIST_HEAD(&hdev->long_term_keys);38033311 INIT_LIST_HEAD(&hdev->identity_resolving_keys);38043312 INIT_LIST_HEAD(&hdev->remote_oob_data);33133313+ INIT_LIST_HEAD(&hdev->le_white_list);38053314 INIT_LIST_HEAD(&hdev->le_conn_params);33153315+ INIT_LIST_HEAD(&hdev->pend_le_conns);38063316 INIT_LIST_HEAD(&hdev->conn_hash.list);3807331738083318 INIT_WORK(&hdev->rx_work, hci_rx_work);···40053511 hci_smp_ltks_clear(hdev);40063512 hci_smp_irks_clear(hdev);40073513 hci_remote_oob_data_clear(hdev);35143514+ hci_white_list_clear(hdev);40083515 hci_conn_params_clear(hdev);35163516+ hci_pend_le_conns_clear(hdev);40093517 hci_dev_unlock(hdev);4010351840113519 hci_dev_put(hdev);···52344738 queue_work(hdev->workqueue, &hdev->cmd_work);52354739 }52364740 }47414741+}47424742+47434743+void hci_req_add_le_scan_disable(struct hci_request *req)47444744+{47454745+ struct hci_cp_le_set_scan_enable cp;47464746+47474747+ memset(&cp, 0, sizeof(cp));47484748+ cp.enable = LE_SCAN_DISABLE;47494749+ hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);47504750+}47514751+47524752+void hci_req_add_le_passive_scan(struct hci_request *req)47534753+{47544754+ struct hci_cp_le_set_scan_param param_cp;47554755+ struct hci_cp_le_set_scan_enable enable_cp;47564756+ struct hci_dev *hdev = req->hdev;47574757+ u8 own_addr_type;47584758+47594759+ /* Set require_privacy to true to avoid identification from47604760+ * unknown peer devices. Since this is passive scanning, no47614761+ * SCAN_REQ using the local identity should be sent. Mandating47624762+ * privacy is just an extra precaution.47634763+ */47644764+ if (hci_update_random_address(req, true, &own_addr_type))47654765+ return;47664766+47674767+ memset(¶m_cp, 0, sizeof(param_cp));47684768+ param_cp.type = LE_SCAN_PASSIVE;47694769+ param_cp.interval = cpu_to_le16(hdev->le_scan_interval);47704770+ param_cp.window = cpu_to_le16(hdev->le_scan_window);47714771+ param_cp.own_address_type = own_addr_type;47724772+ hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),47734773+ ¶m_cp);47744774+47754775+ memset(&enable_cp, 0, sizeof(enable_cp));47764776+ enable_cp.enable = LE_SCAN_ENABLE;47774777+ enable_cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;47784778+ hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),47794779+ &enable_cp);47804780+}47814781+47824782+static void update_background_scan_complete(struct hci_dev *hdev, u8 status)47834783+{47844784+ if (status)47854785+ BT_DBG("HCI request failed to update background scanning: "47864786+ "status 0x%2.2x", status);47874787+}47884788+47894789+/* This function controls the background scanning based on hdev->pend_le_conns47904790+ * list. If there are pending LE connection we start the background scanning,47914791+ * otherwise we stop it.47924792+ *47934793+ * This function requires the caller holds hdev->lock.47944794+ */47954795+void hci_update_background_scan(struct hci_dev *hdev)47964796+{47974797+ struct hci_request req;47984798+ struct hci_conn *conn;47994799+ int err;48004800+48014801+ hci_req_init(&req, hdev);48024802+48034803+ if (list_empty(&hdev->pend_le_conns)) {48044804+ /* If there is no pending LE connections, we should stop48054805+ * the background scanning.48064806+ */48074807+48084808+ /* If controller is not scanning we are done. */48094809+ if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))48104810+ return;48114811+48124812+ hci_req_add_le_scan_disable(&req);48134813+48144814+ BT_DBG("%s stopping background scanning", hdev->name);48154815+ } else {48164816+ /* If there is at least one pending LE connection, we should48174817+ * keep the background scan running.48184818+ */48194819+48204820+ /* If controller is already scanning we are done. */48214821+ if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))48224822+ return;48234823+48244824+ /* If controller is connecting, we should not start scanning48254825+ * since some controllers are not able to scan and connect at48264826+ * the same time.48274827+ */48284828+ conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);48294829+ if (conn)48304830+ return;48314831+48324832+ hci_req_add_le_passive_scan(&req);48334833+48344834+ BT_DBG("%s starting background scanning", hdev->name);48354835+ }48364836+48374837+ err = hci_req_run(&req, update_background_scan_complete);48384838+ if (err)48394839+ BT_ERR("Failed to run HCI request: err %d", err);52374840}
+259-15
net/bluetooth/hci_event.c
···991991992992 hci_dev_lock(hdev);993993994994- if (!status) {995995- if (*sent)996996- set_bit(HCI_ADVERTISING, &hdev->dev_flags);997997- else998998- clear_bit(HCI_ADVERTISING, &hdev->dev_flags);999999- }994994+ if (!status)995995+ mgmt_advertising(hdev, *sent);10009961001997 hci_dev_unlock(hdev);1002998}···10181022 break;1019102310201024 case LE_SCAN_DISABLE:10251025+ /* Cancel this timer so that we don't try to disable scanning10261026+ * when it's already disabled.10271027+ */10281028+ cancel_delayed_work(&hdev->le_scan_disable);10291029+10211030 clear_bit(HCI_LE_SCAN, &hdev->dev_flags);10311031+ /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we10321032+ * interrupted scanning due to a connect request. Mark10331033+ * therefore discovery as stopped.10341034+ */10351035+ if (test_and_clear_bit(HCI_LE_SCAN_INTERRUPTED,10361036+ &hdev->dev_flags))10371037+ hci_discovery_set_state(hdev, DISCOVERY_STOPPED);10221038 break;1023103910241040 default:···1048104010491041 if (!rp->status)10501042 hdev->le_white_list_size = rp->size;10431043+}10441044+10451045+static void hci_cc_le_clear_white_list(struct hci_dev *hdev,10461046+ struct sk_buff *skb)10471047+{10481048+ __u8 status = *((__u8 *) skb->data);10491049+10501050+ BT_DBG("%s status 0x%2.2x", hdev->name, status);10511051+10521052+ if (!status)10531053+ hci_white_list_clear(hdev);10541054+}10551055+10561056+static void hci_cc_le_add_to_white_list(struct hci_dev *hdev,10571057+ struct sk_buff *skb)10581058+{10591059+ struct hci_cp_le_add_to_white_list *sent;10601060+ __u8 status = *((__u8 *) skb->data);10611061+10621062+ BT_DBG("%s status 0x%2.2x", hdev->name, status);10631063+10641064+ sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_WHITE_LIST);10651065+ if (!sent)10661066+ return;10671067+10681068+ if (!status)10691069+ hci_white_list_add(hdev, &sent->bdaddr, sent->bdaddr_type);10701070+}10711071+10721072+static void hci_cc_le_del_from_white_list(struct hci_dev *hdev,10731073+ struct sk_buff *skb)10741074+{10751075+ struct hci_cp_le_del_from_white_list *sent;10761076+ __u8 status = *((__u8 *) skb->data);10771077+10781078+ BT_DBG("%s status 0x%2.2x", hdev->name, status);10791079+10801080+ sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_WHITE_LIST);10811081+ if (!sent)10821082+ return;10831083+10841084+ if (!status)10851085+ hci_white_list_del(hdev, &sent->bdaddr, sent->bdaddr_type);10511086}1052108710531088static void hci_cc_le_read_supported_states(struct hci_dev *hdev,···11311080 else11321081 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;11331082 }10831083+}10841084+10851085+static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)10861086+{10871087+ struct hci_cp_le_set_adv_param *cp;10881088+ u8 status = *((u8 *) skb->data);10891089+10901090+ BT_DBG("%s status 0x%2.2x", hdev->name, status);10911091+10921092+ if (status)10931093+ return;10941094+10951095+ cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);10961096+ if (!cp)10971097+ return;10981098+10991099+ hci_dev_lock(hdev);11001100+ hdev->adv_addr_type = cp->own_address_type;11011101+ hci_dev_unlock(hdev);11341102}1135110311361104static void hci_cc_write_remote_amp_assoc(struct hci_dev *hdev,···16531583 amp_write_remote_assoc(hdev, cp->phy_handle);16541584}1655158515861586+static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)15871587+{15881588+ struct hci_cp_le_create_conn *cp;15891589+ struct hci_conn *conn;15901590+15911591+ BT_DBG("%s status 0x%2.2x", hdev->name, status);15921592+15931593+ /* All connection failure handling is taken care of by the15941594+ * hci_le_conn_failed function which is triggered by the HCI15951595+ * request completion callbacks used for connecting.15961596+ */15971597+ if (status)15981598+ return;15991599+16001600+ cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);16011601+ if (!cp)16021602+ return;16031603+16041604+ hci_dev_lock(hdev);16051605+16061606+ conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->peer_addr);16071607+ if (!conn)16081608+ goto unlock;16091609+16101610+ /* Store the initiator and responder address information which16111611+ * is needed for SMP. These values will not change during the16121612+ * lifetime of the connection.16131613+ */16141614+ conn->init_addr_type = cp->own_address_type;16151615+ if (cp->own_address_type == ADDR_LE_DEV_RANDOM)16161616+ bacpy(&conn->init_addr, &hdev->random_addr);16171617+ else16181618+ bacpy(&conn->init_addr, &hdev->bdaddr);16191619+16201620+ conn->resp_addr_type = cp->peer_addr_type;16211621+ bacpy(&conn->resp_addr, &cp->peer_addr);16221622+16231623+ /* We don't want the connection attempt to stick around16241624+ * indefinitely since LE doesn't have a page timeout concept16251625+ * like BR/EDR. Set a timer for any connection that doesn't use16261626+ * the white list for connecting.16271627+ */16281628+ if (cp->filter_policy == HCI_LE_USE_PEER_ADDR)16291629+ queue_delayed_work(conn->hdev->workqueue,16301630+ &conn->le_conn_timeout,16311631+ HCI_LE_CONN_TIMEOUT);16321632+16331633+unlock:16341634+ hci_dev_unlock(hdev);16351635+}16361636+16561637static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)16571638{16581639 __u8 status = *((__u8 *) skb->data);···19661845{19671846 struct hci_ev_disconn_complete *ev = (void *) skb->data;19681847 u8 reason = hci_to_mgmt_reason(ev->reason);18481848+ struct hci_conn_params *params;19691849 struct hci_conn *conn;18501850+ bool mgmt_connected;19701851 u8 type;1971185219721853 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);···1987186419881865 conn->state = BT_CLOSED;1989186619901990- if (test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))19911991- mgmt_device_disconnected(hdev, &conn->dst, conn->type,19921992- conn->dst_type, reason);18671867+ mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);18681868+ mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,18691869+ reason, mgmt_connected);1993187019941871 if (conn->type == ACL_LINK && conn->flush_key)19951872 hci_remove_link_key(hdev, &conn->dst);18731873+18741874+ params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);18751875+ if (params) {18761876+ switch (params->auto_connect) {18771877+ case HCI_AUTO_CONN_LINK_LOSS:18781878+ if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)18791879+ break;18801880+ /* Fall through */18811881+18821882+ case HCI_AUTO_CONN_ALWAYS:18831883+ hci_pend_le_conn_add(hdev, &conn->dst, conn->dst_type);18841884+ break;18851885+18861886+ default:18871887+ break;18881888+ }18891889+ }1996189019971891 type = conn->type;19981892···24842344 hci_cc_le_read_white_list_size(hdev, skb);24852345 break;2486234623472347+ case HCI_OP_LE_CLEAR_WHITE_LIST:23482348+ hci_cc_le_clear_white_list(hdev, skb);23492349+ break;23502350+23512351+ case HCI_OP_LE_ADD_TO_WHITE_LIST:23522352+ hci_cc_le_add_to_white_list(hdev, skb);23532353+ break;23542354+23552355+ case HCI_OP_LE_DEL_FROM_WHITE_LIST:23562356+ hci_cc_le_del_from_white_list(hdev, skb);23572357+ break;23582358+24872359 case HCI_OP_LE_READ_SUPPORTED_STATES:24882360 hci_cc_le_read_supported_states(hdev, skb);24892361 break;2490236224912363 case HCI_OP_WRITE_LE_HOST_SUPPORTED:24922364 hci_cc_write_le_host_supported(hdev, skb);23652365+ break;23662366+23672367+ case HCI_OP_LE_SET_ADV_PARAM:23682368+ hci_cc_set_adv_param(hdev, skb);24932369 break;2494237024952371 case HCI_OP_WRITE_REMOTE_AMP_ASSOC:···2593243725942438 case HCI_OP_ACCEPT_PHY_LINK:25952439 hci_cs_accept_phylink(hdev, ev->status);24402440+ break;24412441+24422442+ case HCI_OP_LE_CREATE_CONN:24432443+ hci_cs_le_create_conn(hdev, ev->status);25962444 break;2597244525982446 default:···37833623 conn->out = true;37843624 conn->link_mode |= HCI_LM_MASTER;37853625 }36263626+36273627+ /* If we didn't have a hci_conn object previously36283628+ * but we're in master role this must be something36293629+ * initiated using a white list. Since white list based36303630+ * connections are not "first class citizens" we don't36313631+ * have full tracking of them. Therefore, we go ahead36323632+ * with a "best effort" approach of determining the36333633+ * initiator address based on the HCI_PRIVACY flag.36343634+ */36353635+ if (conn->out) {36363636+ conn->resp_addr_type = ev->bdaddr_type;36373637+ bacpy(&conn->resp_addr, &ev->bdaddr);36383638+ if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) {36393639+ conn->init_addr_type = ADDR_LE_DEV_RANDOM;36403640+ bacpy(&conn->init_addr, &hdev->rpa);36413641+ } else {36423642+ hci_copy_identity_address(hdev,36433643+ &conn->init_addr,36443644+ &conn->init_addr_type);36453645+ }36463646+ } else {36473647+ /* Set the responder (our side) address type based on36483648+ * the advertising address type.36493649+ */36503650+ conn->resp_addr_type = hdev->adv_addr_type;36513651+ if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM)36523652+ bacpy(&conn->resp_addr, &hdev->random_addr);36533653+ else36543654+ bacpy(&conn->resp_addr, &hdev->bdaddr);36553655+36563656+ conn->init_addr_type = ev->bdaddr_type;36573657+ bacpy(&conn->init_addr, &ev->bdaddr);36583658+ }36593659+ } else {36603660+ cancel_delayed_work(&conn->le_conn_timeout);37863661 }36623662+36633663+ /* Ensure that the hci_conn contains the identity address type36643664+ * regardless of which address the connection was made with.36653665+ */36663666+ hci_copy_identity_address(hdev, &conn->src, &conn->src_type);3787366737883668 /* Lookup the identity address from the stored connection37893669 * address and address type.···38413641 }3842364238433643 if (ev->status) {38443844- mgmt_connect_failed(hdev, &conn->dst, conn->type,38453845- conn->dst_type, ev->status);38463846- hci_proto_connect_cfm(conn, ev->status);38473847- conn->state = BT_CLOSED;38483848- hci_conn_del(conn);36443644+ hci_le_conn_failed(conn, ev->status);38493645 goto unlock;38503646 }38513647···3860366438613665 hci_proto_connect_cfm(conn, ev->status);3862366636673667+ hci_pend_le_conn_del(hdev, &conn->dst, conn->dst_type);36683668+38633669unlock:38643670 hci_dev_unlock(hdev);36713671+}36723672+36733673+/* This function requires the caller holds hdev->lock */36743674+static void check_pending_le_conn(struct hci_dev *hdev, bdaddr_t *addr,36753675+ u8 addr_type)36763676+{36773677+ struct hci_conn *conn;36783678+ struct smp_irk *irk;36793679+36803680+ /* If this is a resolvable address, we should resolve it and then36813681+ * update address and address type variables.36823682+ */36833683+ irk = hci_get_irk(hdev, addr, addr_type);36843684+ if (irk) {36853685+ addr = &irk->bdaddr;36863686+ addr_type = irk->addr_type;36873687+ }36883688+36893689+ if (!hci_pend_le_conn_lookup(hdev, addr, addr_type))36903690+ return;36913691+36923692+ conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,36933693+ HCI_AT_NO_BONDING);36943694+ if (!IS_ERR(conn))36953695+ return;36963696+36973697+ switch (PTR_ERR(conn)) {36983698+ case -EBUSY:36993699+ /* If hci_connect() returns -EBUSY it means there is already37003700+ * an LE connection attempt going on. Since controllers don't37013701+ * support more than one connection attempt at the time, we37023702+ * don't consider this an error case.37033703+ */37043704+ break;37053705+ default:37063706+ BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));37073707+ }38653708}3866370938673710static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)···39093674 void *ptr = &skb->data[1];39103675 s8 rssi;3911367636773677+ hci_dev_lock(hdev);36783678+39123679 while (num_reports--) {39133680 struct hci_ev_le_advertising_info *ev = ptr;36813681+36823682+ if (ev->evt_type == LE_ADV_IND ||36833683+ ev->evt_type == LE_ADV_DIRECT_IND)36843684+ check_pending_le_conn(hdev, &ev->bdaddr,36853685+ ev->bdaddr_type);3914368639153687 rssi = ev->data[ev->length];39163688 mgmt_device_found(hdev, &ev->bdaddr, LE_LINK, ev->bdaddr_type,···3925368339263684 ptr += sizeof(*ev) + ev->length + 1;39273685 }36863686+36873687+ hci_dev_unlock(hdev);39283688}3929368939303690static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)···39453701 if (conn == NULL)39463702 goto not_found;3947370339483948- ltk = hci_find_ltk(hdev, ev->ediv, ev->random, conn->out);37043704+ ltk = hci_find_ltk(hdev, ev->ediv, ev->rand, conn->out);39493705 if (ltk == NULL)39503706 goto not_found;39513707
+30-7
net/bluetooth/l2cap_core.c
···24342434 if (IS_ERR(skb))24352435 return PTR_ERR(skb);2436243624372437+ /* Channel lock is released before requesting new skb and then24382438+ * reacquired thus we need to recheck channel state.24392439+ */24402440+ if (chan->state != BT_CONNECTED) {24412441+ kfree_skb(skb);24422442+ return -ENOTCONN;24432443+ }24442444+24372445 l2cap_do_send(chan, skb);24382446 return len;24392447 }···24902482 skb = l2cap_create_basic_pdu(chan, msg, len, priority);24912483 if (IS_ERR(skb))24922484 return PTR_ERR(skb);24852485+24862486+ /* Channel lock is released before requesting new skb and then24872487+ * reacquired thus we need to recheck channel state.24882488+ */24892489+ if (chan->state != BT_CONNECTED) {24902490+ kfree_skb(skb);24912491+ return -ENOTCONN;24922492+ }2493249324942494 l2cap_do_send(chan, skb);24952495 err = len;···7108709271097093 auth_type = l2cap_get_auth_type(chan);7110709471117111- if (bdaddr_type_is_le(dst_type))71127112- hcon = hci_connect(hdev, LE_LINK, dst, dst_type,71137113- chan->sec_level, auth_type);71147114- else71157115- hcon = hci_connect(hdev, ACL_LINK, dst, dst_type,71167116- chan->sec_level, auth_type);70957095+ if (bdaddr_type_is_le(dst_type)) {70967096+ /* Convert from L2CAP channel address type to HCI address type70977097+ */70987098+ if (dst_type == BDADDR_LE_PUBLIC)70997099+ dst_type = ADDR_LE_DEV_PUBLIC;71007100+ else71017101+ dst_type = ADDR_LE_DEV_RANDOM;71027102+71037103+ hcon = hci_connect_le(hdev, dst, dst_type, chan->sec_level,71047104+ auth_type);71057105+ } else {71067106+ hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type);71077107+ }7117710871187109 if (IS_ERR(hcon)) {71197110 err = PTR_ERR(hcon);···7274725172757252 if (hcon->type == LE_LINK) {72767253 if (!status && encrypt)72777277- smp_distribute_keys(conn, 0);72547254+ smp_distribute_keys(conn);72787255 cancel_delayed_work(&conn->security_timer);72797256 }72807257
+347-76
net/bluetooth/mgmt.c
···8181 MGMT_OP_SET_SCAN_PARAMS,8282 MGMT_OP_SET_SECURE_CONN,8383 MGMT_OP_SET_DEBUG_KEYS,8484+ MGMT_OP_SET_PRIVACY,8485 MGMT_OP_LOAD_IRKS,8586};8687···107106 MGMT_EV_DEVICE_UNBLOCKED,108107 MGMT_EV_DEVICE_UNPAIRED,109108 MGMT_EV_PASSKEY_NOTIFY,109109+ MGMT_EV_NEW_IRK,110110};111111112112#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)···391389 if (lmp_le_capable(hdev)) {392390 settings |= MGMT_SETTING_LE;393391 settings |= MGMT_SETTING_ADVERTISING;392392+ settings |= MGMT_SETTING_PRIVACY;394393 }395394396395 return settings;···439436440437 if (test_bit(HCI_DEBUG_KEYS, &hdev->dev_flags))441438 settings |= MGMT_SETTING_DEBUG_KEYS;439439+440440+ if (test_bit(HCI_PRIVACY, &hdev->dev_flags))441441+ settings |= MGMT_SETTING_PRIVACY;442442443443 return settings;444444}···817811 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);818812}819813814814+static bool get_connectable(struct hci_dev *hdev)815815+{816816+ struct pending_cmd *cmd;817817+818818+ /* If there's a pending mgmt command the flag will not yet have819819+ * it's final value, so check for this first.820820+ */821821+ cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);822822+ if (cmd) {823823+ struct mgmt_mode *cp = cmd->param;824824+ return cp->val;825825+ }826826+827827+ return test_bit(HCI_CONNECTABLE, &hdev->dev_flags);828828+}829829+830830+static void enable_advertising(struct hci_request *req)831831+{832832+ struct hci_dev *hdev = req->hdev;833833+ struct hci_cp_le_set_adv_param cp;834834+ u8 own_addr_type, enable = 0x01;835835+ bool connectable;836836+837837+ /* Clear the HCI_ADVERTISING bit temporarily so that the838838+ * hci_update_random_address knows that it's safe to go ahead839839+ * and write a new random address. The flag will be set back on840840+ * as soon as the SET_ADV_ENABLE HCI command completes.841841+ */842842+ clear_bit(HCI_ADVERTISING, &hdev->dev_flags);843843+844844+ connectable = get_connectable(hdev);845845+846846+ /* Set require_privacy to true only when non-connectable847847+ * advertising is used. In that case it is fine to use a848848+ * non-resolvable private address.849849+ */850850+ if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)851851+ return;852852+853853+ memset(&cp, 0, sizeof(cp));854854+ cp.min_interval = __constant_cpu_to_le16(0x0800);855855+ cp.max_interval = __constant_cpu_to_le16(0x0800);856856+ cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;857857+ cp.own_address_type = own_addr_type;858858+ cp.channel_map = hdev->le_adv_channel_map;859859+860860+ hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);861861+862862+ hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);863863+}864864+865865+static void disable_advertising(struct hci_request *req)866866+{867867+ u8 enable = 0x00;868868+869869+ hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);870870+}871871+820872static void service_cache_off(struct work_struct *work)821873{822874 struct hci_dev *hdev = container_of(work, struct hci_dev,···896832 hci_req_run(&req, NULL);897833}898834835835+static void rpa_expired(struct work_struct *work)836836+{837837+ struct hci_dev *hdev = container_of(work, struct hci_dev,838838+ rpa_expired.work);839839+ struct hci_request req;840840+841841+ BT_DBG("");842842+843843+ set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);844844+845845+ if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) ||846846+ hci_conn_num(hdev, LE_LINK) > 0)847847+ return;848848+849849+ /* The generation of a new RPA and programming it into the850850+ * controller happens in the enable_advertising() function.851851+ */852852+853853+ hci_req_init(&req, hdev);854854+855855+ disable_advertising(&req);856856+ enable_advertising(&req);857857+858858+ hci_req_run(&req, NULL);859859+}860860+899861static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)900862{901863 if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags))902864 return;903865904866 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);867867+ INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);905868906869 /* Non-mgmt controlled devices get this bit set907870 * implicitly so that pairing works for them, however···1034943 sizeof(settings));1035944}1036945946946+static void clean_up_hci_complete(struct hci_dev *hdev, u8 status)947947+{948948+ BT_DBG("%s status 0x%02x", hdev->name, status);949949+950950+ if (hci_conn_count(hdev) == 0) {951951+ cancel_delayed_work(&hdev->power_off);952952+ queue_work(hdev->req_workqueue, &hdev->power_off.work);953953+ }954954+}955955+956956+static int clean_up_hci_state(struct hci_dev *hdev)957957+{958958+ struct hci_request req;959959+ struct hci_conn *conn;960960+961961+ hci_req_init(&req, hdev);962962+963963+ if (test_bit(HCI_ISCAN, &hdev->flags) ||964964+ test_bit(HCI_PSCAN, &hdev->flags)) {965965+ u8 scan = 0x00;966966+ hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);967967+ }968968+969969+ if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))970970+ disable_advertising(&req);971971+972972+ if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {973973+ hci_req_add_le_scan_disable(&req);974974+ }975975+976976+ list_for_each_entry(conn, &hdev->conn_hash.list, list) {977977+ struct hci_cp_disconnect dc;978978+ struct hci_cp_reject_conn_req rej;979979+980980+ switch (conn->state) {981981+ case BT_CONNECTED:982982+ case BT_CONFIG:983983+ dc.handle = cpu_to_le16(conn->handle);984984+ dc.reason = 0x15; /* Terminated due to Power Off */985985+ hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc);986986+ break;987987+ case BT_CONNECT:988988+ if (conn->type == LE_LINK)989989+ hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL,990990+ 0, NULL);991991+ else if (conn->type == ACL_LINK)992992+ hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL,993993+ 6, &conn->dst);994994+ break;995995+ case BT_CONNECT2:996996+ bacpy(&rej.bdaddr, &conn->dst);997997+ rej.reason = 0x15; /* Terminated due to Power Off */998998+ if (conn->type == ACL_LINK)999999+ hci_req_add(&req, HCI_OP_REJECT_CONN_REQ,10001000+ sizeof(rej), &rej);10011001+ else if (conn->type == SCO_LINK)10021002+ hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ,10031003+ sizeof(rej), &rej);10041004+ break;10051005+ }10061006+ }10071007+10081008+ return hci_req_run(&req, clean_up_hci_complete);10091009+}10101010+10371011static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,10381012 u16 len)10391013{···1142986 goto failed;1143987 }114498811451145- if (cp->val)989989+ if (cp->val) {1146990 queue_work(hdev->req_workqueue, &hdev->power_on);11471147- else11481148- queue_work(hdev->req_workqueue, &hdev->power_off.work);991991+ err = 0;992992+ } else {993993+ /* Disconnect connections, stop scans, etc */994994+ err = clean_up_hci_state(hdev);995995+ if (!err)996996+ queue_delayed_work(hdev->req_workqueue, &hdev->power_off,997997+ HCI_POWER_OFF_TIMEOUT);114999811501150- err = 0;999999+ /* ENODATA means there were no HCI commands queued */10001000+ if (err == -ENODATA) {10011001+ cancel_delayed_work(&hdev->power_off);10021002+ queue_work(hdev->req_workqueue, &hdev->power_off.work);10031003+ err = 0;10041004+ }10051005+ }1151100611521007failed:11531008 hci_dev_unlock(hdev);···1509134215101343 if (hdev->page_scan_type != type)15111344 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);15121512-}15131513-15141514-static u8 get_adv_type(struct hci_dev *hdev)15151515-{15161516- struct pending_cmd *cmd;15171517- bool connectable;15181518-15191519- /* If there's a pending mgmt command the flag will not yet have15201520- * it's final value, so check for this first.15211521- */15221522- cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);15231523- if (cmd) {15241524- struct mgmt_mode *cp = cmd->param;15251525- connectable = !!cp->val;15261526- } else {15271527- connectable = test_bit(HCI_CONNECTABLE, &hdev->dev_flags);15281528- }15291529-15301530- return connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;15311531-}15321532-15331533-static void enable_advertising(struct hci_request *req)15341534-{15351535- struct hci_dev *hdev = req->hdev;15361536- struct hci_cp_le_set_adv_param cp;15371537- u8 enable = 0x01;15381538-15391539- memset(&cp, 0, sizeof(cp));15401540- cp.min_interval = __constant_cpu_to_le16(0x0800);15411541- cp.max_interval = __constant_cpu_to_le16(0x0800);15421542- cp.type = get_adv_type(hdev);15431543- cp.own_address_type = hdev->own_addr_type;15441544- cp.channel_map = hdev->le_adv_channel_map;15451545-15461546- hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);15471547-15481548- hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);15491549-}15501550-15511551-static void disable_advertising(struct hci_request *req)15521552-{15531553- u8 enable = 0x00;15541554-15551555- hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);15561345}1557134615581347static void set_connectable_complete(struct hci_dev *hdev, u8 status)···2453233024542331 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);2455233223332333+ hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);23342334+24562335 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);24572336 }24582337···28542729 else28552730 auth_type = HCI_AT_DEDICATED_BONDING_MITM;2856273128572857- if (cp->addr.type == BDADDR_BREDR)28582858- conn = hci_connect(hdev, ACL_LINK, &cp->addr.bdaddr,28592859- cp->addr.type, sec_level, auth_type);28602860- else28612861- conn = hci_connect(hdev, LE_LINK, &cp->addr.bdaddr,28622862- cp->addr.type, sec_level, auth_type);27322732+ if (cp->addr.type == BDADDR_BREDR) {27332733+ conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,27342734+ auth_type);27352735+ } else {27362736+ u8 addr_type;27372737+27382738+ /* Convert from L2CAP channel address type to HCI address type27392739+ */27402740+ if (cp->addr.type == BDADDR_LE_PUBLIC)27412741+ addr_type = ADDR_LE_DEV_PUBLIC;27422742+ else27432743+ addr_type = ADDR_LE_DEV_RANDOM;27442744+27452745+ conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type,27462746+ sec_level, auth_type);27472747+ }2863274828642749 if (IS_ERR(conn)) {28652750 int status;···33933258 struct hci_request req;33943259 /* General inquiry access code (GIAC) */33953260 u8 lap[3] = { 0x33, 0x8b, 0x9e };33963396- u8 status;32613261+ u8 status, own_addr_type;33973262 int err;3398326333993264 BT_DBG("%s", hdev->name);···34783343 goto failed;34793344 }3480334534813481- if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) {33463346+ /* If controller is scanning, it means the background scanning33473347+ * is running. Thus, we should temporarily stop it in order to33483348+ * set the discovery scanning parameters.33493349+ */33503350+ if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))33513351+ hci_req_add_le_scan_disable(&req);33523352+33533353+ memset(¶m_cp, 0, sizeof(param_cp));33543354+33553355+ /* All active scans will be done with either a resolvable33563356+ * private address (when privacy feature has been enabled)33573357+ * or unresolvable private address.33583358+ */33593359+ err = hci_update_random_address(&req, true, &own_addr_type);33603360+ if (err < 0) {34823361 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,34833483- MGMT_STATUS_BUSY);33623362+ MGMT_STATUS_FAILED);34843363 mgmt_pending_remove(cmd);34853364 goto failed;34863365 }3487336634883488- memset(¶m_cp, 0, sizeof(param_cp));34893367 param_cp.type = LE_SCAN_ACTIVE;34903368 param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT);34913369 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);34923492- param_cp.own_address_type = hdev->own_addr_type;33703370+ param_cp.own_address_type = own_addr_type;34933371 hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),34943372 ¶m_cp);34953373···35723424 struct hci_cp_remote_name_req_cancel cp;35733425 struct inquiry_entry *e;35743426 struct hci_request req;35753575- struct hci_cp_le_set_scan_enable enable_cp;35763427 int err;3577342835783429 BT_DBG("%s", hdev->name);···36073460 } else {36083461 cancel_delayed_work(&hdev->le_scan_disable);3609346236103610- memset(&enable_cp, 0, sizeof(enable_cp));36113611- enable_cp.enable = LE_SCAN_DISABLE;36123612- hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE,36133613- sizeof(enable_cp), &enable_cp);34633463+ hci_req_add_le_scan_disable(&req);36143464 }3615346536163466 break;···36643520 hci_dev_lock(hdev);3665352136663522 if (!hci_discovery_active(hdev)) {36673667- err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME,36683668- MGMT_STATUS_FAILED);35233523+ err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,35243524+ MGMT_STATUS_FAILED, &cp->addr,35253525+ sizeof(cp->addr));36693526 goto failed;36703527 }3671352836723529 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);36733530 if (!e) {36743674- err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME,36753675- MGMT_STATUS_INVALID_PARAMS);35313531+ err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,35323532+ MGMT_STATUS_INVALID_PARAMS, &cp->addr,35333533+ sizeof(cp->addr));36763534 goto failed;36773535 }36783536···39623816 hdev->le_scan_window = window;3963381739643818 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0);38193819+38203820+ /* If background scan is running, restart it so new parameters are38213821+ * loaded.38223822+ */38233823+ if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) &&38243824+ hdev->discovery.state == DISCOVERY_STOPPED) {38253825+ struct hci_request req;38263826+38273827+ hci_req_init(&req, hdev);38283828+38293829+ hci_req_add_le_scan_disable(&req);38303830+ hci_req_add_le_passive_scan(&req);38313831+38323832+ hci_req_run(&req, NULL);38333833+ }3965383439663835 hci_dev_unlock(hdev);39673836···43434182 return err;43444183}4345418441854185+static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,41864186+ u16 len)41874187+{41884188+ struct mgmt_cp_set_privacy *cp = cp_data;41894189+ bool changed;41904190+ int err;41914191+41924192+ BT_DBG("request for %s", hdev->name);41934193+41944194+ if (!lmp_le_capable(hdev))41954195+ return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,41964196+ MGMT_STATUS_NOT_SUPPORTED);41974197+41984198+ if (cp->privacy != 0x00 && cp->privacy != 0x01)41994199+ return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,42004200+ MGMT_STATUS_INVALID_PARAMS);42014201+42024202+ if (hdev_is_powered(hdev))42034203+ return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,42044204+ MGMT_STATUS_REJECTED);42054205+42064206+ hci_dev_lock(hdev);42074207+42084208+ /* If user space supports this command it is also expected to42094209+ * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.42104210+ */42114211+ set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags);42124212+42134213+ if (cp->privacy) {42144214+ changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags);42154215+ memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));42164216+ set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);42174217+ } else {42184218+ changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags);42194219+ memset(hdev->irk, 0, sizeof(hdev->irk));42204220+ clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);42214221+ }42224222+42234223+ err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);42244224+ if (err < 0)42254225+ goto unlock;42264226+42274227+ if (changed)42284228+ err = new_settings(hdev, sk);42294229+42304230+unlock:42314231+ hci_dev_unlock(hdev);42324232+ return err;42334233+}42344234+43464235static bool irk_is_valid(struct mgmt_irk_info *irk)43474236{43484237 switch (irk->addr.type) {···46074396 { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE },46084397 { set_secure_conn, false, MGMT_SETTING_SIZE },46094398 { set_debug_keys, false, MGMT_SETTING_SIZE },46104610- { },43994399+ { set_privacy, false, MGMT_SET_PRIVACY_SIZE },46114400 { load_irks, true, MGMT_LOAD_IRKS_SIZE },46124401};46134402···47254514 mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL);47264515}4727451645174517+/* This function requires the caller holds hdev->lock */45184518+static void restart_le_auto_conns(struct hci_dev *hdev)45194519+{45204520+ struct hci_conn_params *p;45214521+45224522+ list_for_each_entry(p, &hdev->le_conn_params, list) {45234523+ if (p->auto_connect == HCI_AUTO_CONN_ALWAYS)45244524+ hci_pend_le_conn_add(hdev, &p->addr, p->addr_type);45254525+ }45264526+}45274527+47284528static void powered_complete(struct hci_dev *hdev, u8 status)47294529{47304530 struct cmd_lookup match = { NULL, hdev };···47434521 BT_DBG("status 0x%02x", status);4744452247454523 hci_dev_lock(hdev);45244524+45254525+ restart_le_auto_conns(hdev);4746452647474527 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);47484528···47874563 }4788456447894565 if (lmp_le_capable(hdev)) {47904790- /* Set random address to static address if configured */47914791- if (bacmp(&hdev->static_addr, BDADDR_ANY))47924792- hci_req_add(&req, HCI_OP_LE_SET_RANDOM_ADDR, 6,47934793- &hdev->static_addr);47944794-47954566 /* Make sure the controller has a good default for47964567 * advertising data. This also applies to the case47974568 * where BR/EDR was toggled during the AUTO_OFF phase.···49124693 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev))49134694 return;4914469546964696+ /* Powering off may clear the scan mode - don't let that interfere */46974697+ if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))46984698+ return;46994699+49154700 if (discoverable) {49164701 changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags);49174702 } else {···49494726 if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev))49504727 return;4951472847294729+ /* Powering off may clear the scan mode - don't let that interfere */47304730+ if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))47314731+ return;47324732+49524733 if (connectable)49534734 changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags);49544735 else···4960473349614734 if (changed)49624735 new_settings(hdev, NULL);47364736+}47374737+47384738+void mgmt_advertising(struct hci_dev *hdev, u8 advertising)47394739+{47404740+ /* Powering off may stop advertising - don't let that interfere */47414741+ if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))47424742+ return;47434743+47444744+ if (advertising)47454745+ set_bit(HCI_ADVERTISING, &hdev->dev_flags);47464746+ else47474747+ clear_bit(HCI_ADVERTISING, &hdev->dev_flags);49634748}4964474949654750void mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status)···50324793 ev.key.type = key->authenticated;50334794 ev.key.enc_size = key->enc_size;50344795 ev.key.ediv = key->ediv;47964796+ ev.key.rand = key->rand;5035479750364798 if (key->type == HCI_SMP_LTK)50374799 ev.key.master = 1;5038480050395039- memcpy(ev.key.rand, key->rand, sizeof(key->rand));50404801 memcpy(ev.key.val, key->val, sizeof(key->val));5041480250424803 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);···51464907}5147490851484909void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,51495149- u8 link_type, u8 addr_type, u8 reason)49104910+ u8 link_type, u8 addr_type, u8 reason,49114911+ bool mgmt_connected)51504912{51514913 struct mgmt_ev_device_disconnected ev;49144914+ struct pending_cmd *power_off;51524915 struct sock *sk = NULL;49164916+49174917+ power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);49184918+ if (power_off) {49194919+ struct mgmt_mode *cp = power_off->param;49204920+49214921+ /* The connection is still in hci_conn_hash so test for 149224922+ * instead of 0 to know if this is the last one.49234923+ */49244924+ if (!cp->val && hci_conn_count(hdev) == 1) {49254925+ cancel_delayed_work(&hdev->power_off);49264926+ queue_work(hdev->req_workqueue, &hdev->power_off.work);49274927+ }49284928+ }49294929+49304930+ if (!mgmt_connected)49314931+ return;5153493251544933 if (link_type != ACL_LINK && link_type != LE_LINK)51554934 return;···52234966 u8 addr_type, u8 status)52244967{52254968 struct mgmt_ev_connect_failed ev;49694969+ struct pending_cmd *power_off;49704970+49714971+ power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);49724972+ if (power_off) {49734973+ struct mgmt_mode *cp = power_off->param;49744974+49754975+ /* The connection is still in hci_conn_hash so test for 149764976+ * instead of 0 to know if this is the last one.49774977+ */49784978+ if (!cp->val && hci_conn_count(hdev) == 1) {49794979+ cancel_delayed_work(&hdev->power_off);49804980+ queue_work(hdev->req_workqueue, &hdev->power_off.work);49814981+ }49824982+ }5226498352274984 bacpy(&ev.addr.bdaddr, bdaddr);52284985 ev.addr.type = link_to_bdaddr(link_type, addr_type);
+136-44
net/bluetooth/smp.c
···124124 return !memcmp(bdaddr->b, hash, 3);125125}126126127127+int smp_generate_rpa(struct crypto_blkcipher *tfm, u8 irk[16], bdaddr_t *rpa)128128+{129129+ int err;130130+131131+ get_random_bytes(&rpa->b[3], 3);132132+133133+ rpa->b[5] &= 0x3f; /* Clear two most significant bits */134134+ rpa->b[5] |= 0x40; /* Set second most significant bit */135135+136136+ err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);137137+ if (err < 0)138138+ return err;139139+140140+ BT_DBG("RPA %pMR", rpa);141141+142142+ return 0;143143+}144144+127145static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],128146 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia,129147 u8 _rat, bdaddr_t *ra, u8 res[16])···282264283265 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))284266 remote_dist |= SMP_DIST_ID_KEY;267267+268268+ if (test_bit(HCI_PRIVACY, &hdev->dev_flags))269269+ local_dist |= SMP_DIST_ID_KEY;285270286271 if (rsp == NULL) {287272 req->io_capability = conn->hcon->io_capability;···445424 /* Prevent mutual access to hdev->tfm_aes */446425 hci_dev_lock(hdev);447426448448- if (conn->hcon->out)449449- ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,450450- conn->hcon->src_type, &conn->hcon->src,451451- conn->hcon->dst_type, &conn->hcon->dst, res);452452- else453453- ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,454454- conn->hcon->dst_type, &conn->hcon->dst,455455- conn->hcon->src_type, &conn->hcon->src, res);427427+ ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,428428+ conn->hcon->init_addr_type, &conn->hcon->init_addr,429429+ conn->hcon->resp_addr_type, &conn->hcon->resp_addr, res);456430457431 hci_dev_unlock(hdev);458432···487471 /* Prevent mutual access to hdev->tfm_aes */488472 hci_dev_lock(hdev);489473490490- if (hcon->out)491491- ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,492492- hcon->src_type, &hcon->src,493493- hcon->dst_type, &hcon->dst, res);494494- else495495- ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,496496- hcon->dst_type, &hcon->dst,497497- hcon->src_type, &hcon->src, res);474474+ ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,475475+ hcon->init_addr_type, &hcon->init_addr,476476+ hcon->resp_addr_type, &hcon->resp_addr, res);498477499478 hci_dev_unlock(hdev);500479···507496 }508497509498 if (hcon->out) {510510- u8 stk[16], rand[8];511511- __le16 ediv;512512-513513- memset(rand, 0, sizeof(rand));514514- ediv = 0;499499+ u8 stk[16];500500+ __le64 rand = 0;501501+ __le16 ediv = 0;515502516503 smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, key);517504 swap128(key, stk);···525516 hci_le_start_enc(hcon, ediv, rand, stk);526517 hcon->enc_key_size = smp->enc_key_size;527518 } else {528528- u8 stk[16], r[16], rand[8];529529- __le16 ediv;530530-531531- memset(rand, 0, sizeof(rand));532532- ediv = 0;519519+ u8 stk[16], r[16];520520+ __le64 rand = 0;521521+ __le16 ediv = 0;533522534523 swap128(smp->prnd, r);535524 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r);···549542 smp_failure(conn, reason);550543}551544545545+static void smp_reencrypt(struct work_struct *work)546546+{547547+ struct smp_chan *smp = container_of(work, struct smp_chan,548548+ reencrypt.work);549549+ struct l2cap_conn *conn = smp->conn;550550+ struct hci_conn *hcon = conn->hcon;551551+ struct smp_ltk *ltk = smp->ltk;552552+553553+ BT_DBG("");554554+555555+ hci_le_start_enc(hcon, ltk->ediv, ltk->rand, ltk->val);556556+ hcon->enc_key_size = ltk->enc_size;557557+}558558+552559static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)553560{554561 struct smp_chan *smp;···573552574553 INIT_WORK(&smp->confirm, confirm_work);575554 INIT_WORK(&smp->random, random_work);555555+ INIT_DELAYED_WORK(&smp->reencrypt, smp_reencrypt);576556577557 smp->conn = conn;578558 conn->smp_chan = smp;···591569592570 BUG_ON(!smp);593571572572+ cancel_delayed_work_sync(&smp->reencrypt);573573+594574 complete = test_bit(SMP_FLAG_COMPLETE, &smp->smp_flags);595575 mgmt_smp_complete(conn->hcon, complete);576576+577577+ /* If pairing failed clean up any keys we might have */578578+ if (!complete) {579579+ if (smp->ltk) {580580+ list_del(&smp->ltk->list);581581+ kfree(smp->ltk);582582+ }583583+584584+ if (smp->slave_ltk) {585585+ list_del(&smp->slave_ltk->list);586586+ kfree(smp->slave_ltk);587587+ }588588+589589+ if (smp->remote_irk) {590590+ list_del(&smp->remote_irk->list);591591+ kfree(smp->remote_irk);592592+ }593593+ }596594597595 kfree(smp);598596 conn->smp_chan = NULL;···969927 if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))970928 return 0;971929930930+ /* Mark the information as received */931931+ smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;932932+972933 skb_pull(skb, sizeof(*rp));973934974935 hci_dev_lock(hdev);···981936 rp->ediv, rp->rand);982937 smp->ltk = ltk;983938 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))984984- smp_distribute_keys(conn, 1);939939+ smp_distribute_keys(conn);985940 hci_dev_unlock(hdev);986941987942 return 0;···1025980 if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))1026981 return 0;1027982983983+ /* Mark the information as received */984984+ smp->remote_key_dist &= ~SMP_DIST_ID_KEY;985985+1028986 skb_pull(skb, sizeof(*info));987987+988988+ /* Strictly speaking the Core Specification (4.1) allows sending989989+ * an empty address which would force us to rely on just the IRK990990+ * as "identity information". However, since such991991+ * implementations are not known of and in order to not over992992+ * complicate our implementation, simply pretend that we never993993+ * received an IRK for such a device.994994+ */995995+ if (!bacmp(&info->bdaddr, BDADDR_ANY)) {996996+ BT_ERR("Ignoring IRK with no identity address");997997+ smp_distribute_keys(conn);998998+ return 0;999999+ }1029100010301001 bacpy(&smp->id_addr, &info->bdaddr);10311002 smp->id_addr_type = info->addr_type;···106099910611000 l2cap_conn_update_id_addr(hcon);1062100110631063- smp_distribute_keys(conn, 1);10021002+ smp_distribute_keys(conn);1064100310651004 return 0;10661005}···11891128 }11901129}1191113011921192-int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)11311131+int smp_distribute_keys(struct l2cap_conn *conn)11931132{11941133 struct smp_cmd_pairing *req, *rsp;11951134 struct smp_chan *smp = conn->smp_chan;11351135+ struct hci_conn *hcon = conn->hcon;11361136+ struct hci_dev *hdev = hcon->hdev;11371137+ bool ltk_encrypt;11961138 __u8 *keydist;1197113911981198- BT_DBG("conn %p force %d", conn, force);11401140+ BT_DBG("conn %p", conn);1199114112001200- if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags))11421142+ if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))12011143 return 0;1202114412031145 rsp = (void *) &smp->prsp[1];1204114612051147 /* The responder sends its keys first */12061206- if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07))11481148+ if (hcon->out && (smp->remote_key_dist & 0x07))12071149 return 0;1208115012091151 req = (void *) &smp->preq[1];1210115212111211- if (conn->hcon->out) {11531153+ if (hcon->out) {12121154 keydist = &rsp->init_key_dist;12131155 *keydist &= req->init_key_dist;12141156 } else {···12241160 if (*keydist & SMP_DIST_ENC_KEY) {12251161 struct smp_cmd_encrypt_info enc;12261162 struct smp_cmd_master_ident ident;12271227- struct hci_conn *hcon = conn->hcon;12281163 struct smp_ltk *ltk;12291164 u8 authenticated;12301165 __le16 ediv;11661166+ __le64 rand;1231116712321168 get_random_bytes(enc.ltk, sizeof(enc.ltk));12331169 get_random_bytes(&ediv, sizeof(ediv));12341234- get_random_bytes(ident.rand, sizeof(ident.rand));11701170+ get_random_bytes(&rand, sizeof(rand));1235117112361172 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);1237117312381174 authenticated = hcon->sec_level == BT_SECURITY_HIGH;12391239- ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,11751175+ ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,12401176 HCI_SMP_LTK_SLAVE, authenticated, enc.ltk,12411241- smp->enc_key_size, ediv, ident.rand);11771177+ smp->enc_key_size, ediv, rand);12421178 smp->slave_ltk = ltk;1243117912441180 ident.ediv = ediv;11811181+ ident.rand = rand;1245118212461183 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);12471184···12531188 struct smp_cmd_ident_addr_info addrinfo;12541189 struct smp_cmd_ident_info idinfo;1255119012561256- /* Send a dummy key */12571257- get_random_bytes(idinfo.irk, sizeof(idinfo.irk));11911191+ memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));1258119212591193 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);1260119412611261- /* Just public address */12621262- memset(&addrinfo, 0, sizeof(addrinfo));12631263- bacpy(&addrinfo.bdaddr, &conn->hcon->src);11951195+ /* The hci_conn contains the local identity address11961196+ * after the connection has been established.11971197+ *11981198+ * This is true even when the connection has been11991199+ * established using a resolvable random address.12001200+ */12011201+ bacpy(&addrinfo.bdaddr, &hcon->src);12021202+ addrinfo.addr_type = hcon->src_type;1264120312651204 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),12661205 &addrinfo);···12831214 *keydist &= ~SMP_DIST_SIGN;12841215 }1285121612861286- if (conn->hcon->out || force || !(rsp->init_key_dist & 0x07)) {12871287- clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags);12171217+ /* If there are still keys to be received wait for them */12181218+ if ((smp->remote_key_dist & 0x07))12191219+ return 0;12201220+12211221+ /* Check if we should try to re-encrypt the link with the LTK.12221222+ * SMP_FLAG_LTK_ENCRYPT flag is used to track whether we've12231223+ * already tried this (in which case we shouldn't try again).12241224+ *12251225+ * The request will trigger an encryption key refresh event12261226+ * which will cause a call to auth_cfm and eventually lead to12271227+ * l2cap_core.c calling this smp_distribute_keys function again12281228+ * and thereby completing the process.12291229+ */12301230+ if (smp->ltk)12311231+ ltk_encrypt = !test_and_set_bit(SMP_FLAG_LTK_ENCRYPT,12321232+ &smp->smp_flags);12331233+ else12341234+ ltk_encrypt = false;12351235+12361236+ /* Re-encrypt the link with LTK if possible */12371237+ if (ltk_encrypt && hcon->out) {12381238+ queue_delayed_work(hdev->req_workqueue, &smp->reencrypt,12391239+ SMP_REENCRYPT_TIMEOUT);12401240+ } else {12411241+ clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags);12881242 cancel_delayed_work_sync(&conn->security_timer);12891243 set_bit(SMP_FLAG_COMPLETE, &smp->smp_flags);12901244 smp_notify_keys(conn);