Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / net / bluetooth / hci_conn.c
index 99cd8d9..ea7f031 100644 (file)
 #include <net/bluetooth/bluetooth.h>
 #include <net/bluetooth/hci_core.h>
 
+static void hci_le_connect(struct hci_conn *conn)
+{
+       struct hci_dev *hdev = conn->hdev;
+       struct hci_cp_le_create_conn cp;
+
+       conn->state = BT_CONNECT;
+       conn->out = 1;
+       conn->link_mode |= HCI_LM_MASTER;
+       conn->sec_level = BT_SECURITY_LOW;
+
+       memset(&cp, 0, sizeof(cp));
+       cp.scan_interval = cpu_to_le16(0x0004);
+       cp.scan_window = cpu_to_le16(0x0004);
+       bacpy(&cp.peer_addr, &conn->dst);
+       cp.peer_addr_type = conn->dst_type;
+       cp.conn_interval_min = cpu_to_le16(0x0008);
+       cp.conn_interval_max = cpu_to_le16(0x0100);
+       cp.supervision_timeout = cpu_to_le16(0x0064);
+       cp.min_ce_len = cpu_to_le16(0x0001);
+       cp.max_ce_len = cpu_to_le16(0x0001);
+
+       hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
+}
+
+static void hci_le_connect_cancel(struct hci_conn *conn)
+{
+       hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL);
+}
+
 void hci_acl_connect(struct hci_conn *conn)
 {
        struct hci_dev *hdev = conn->hdev;
@@ -156,6 +185,75 @@ void hci_setup_sync(struct hci_conn *conn, __u16 handle)
        hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp);
 }
 
+void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
+                                       u16 latency, u16 to_multiplier)
+{
+       struct hci_cp_le_conn_update cp;
+       struct hci_dev *hdev = conn->hdev;
+
+       memset(&cp, 0, sizeof(cp));
+
+       cp.handle               = cpu_to_le16(conn->handle);
+       cp.conn_interval_min    = cpu_to_le16(min);
+       cp.conn_interval_max    = cpu_to_le16(max);
+       cp.conn_latency         = cpu_to_le16(latency);
+       cp.supervision_timeout  = cpu_to_le16(to_multiplier);
+       cp.min_ce_len           = cpu_to_le16(0x0001);
+       cp.max_ce_len           = cpu_to_le16(0x0001);
+
+       hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
+}
+EXPORT_SYMBOL(hci_le_conn_update);
+
+void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
+                                                       __u8 ltk[16])
+{
+       struct hci_dev *hdev = conn->hdev;
+       struct hci_cp_le_start_enc cp;
+
+       BT_DBG("%p", conn);
+
+       memset(&cp, 0, sizeof(cp));
+
+       cp.handle = cpu_to_le16(conn->handle);
+       memcpy(cp.ltk, ltk, sizeof(cp.ltk));
+       cp.ediv = ediv;
+       memcpy(cp.rand, rand, sizeof(rand));
+
+       hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
+}
+EXPORT_SYMBOL(hci_le_start_enc);
+
+void hci_le_ltk_reply(struct hci_conn *conn, u8 ltk[16])
+{
+       struct hci_dev *hdev = conn->hdev;
+       struct hci_cp_le_ltk_reply cp;
+
+       BT_DBG("%p", conn);
+
+       memset(&cp, 0, sizeof(cp));
+
+       cp.handle = cpu_to_le16(conn->handle);
+       memcpy(cp.ltk, ltk, sizeof(ltk));
+
+       hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
+}
+EXPORT_SYMBOL(hci_le_ltk_reply);
+
+void hci_le_ltk_neg_reply(struct hci_conn *conn)
+{
+       struct hci_dev *hdev = conn->hdev;
+       struct hci_cp_le_ltk_neg_reply cp;
+
+       BT_DBG("%p", conn);
+
+       memset(&cp, 0, sizeof(cp));
+
+       cp.handle = cpu_to_le16(conn->handle);
+
+       hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(cp), &cp);
+}
+
 /* Device _must_ be locked */
 void hci_sco_setup(struct hci_conn *conn, __u8 status)
 {
@@ -193,8 +291,12 @@ static void hci_conn_timeout(unsigned long arg)
        switch (conn->state) {
        case BT_CONNECT:
        case BT_CONNECT2:
-               if (conn->type == ACL_LINK && conn->out)
-                       hci_acl_connect_cancel(conn);
+               if (conn->out) {
+                       if (conn->type == ACL_LINK)
+                               hci_acl_connect_cancel(conn);
+                       else if (conn->type == LE_LINK)
+                               hci_le_connect_cancel(conn);
+               }
                break;
        case BT_CONFIG:
        case BT_CONNECTED:
@@ -218,6 +320,19 @@ static void hci_conn_idle(unsigned long arg)
        hci_conn_enter_sniff_mode(conn);
 }
 
+static void hci_conn_auto_accept(unsigned long arg)
+{
+       struct hci_conn *conn = (void *) arg;
+       struct hci_dev *hdev = conn->hdev;
+
+       hci_dev_lock(hdev);
+
+       hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
+                                                               &conn->dst);
+
+       hci_dev_unlock(hdev);
+}
+
 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
 {
        struct hci_conn *conn;
@@ -234,6 +349,9 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
        conn->mode  = HCI_CM_ACTIVE;
        conn->state = BT_OPEN;
        conn->auth_type = HCI_AT_GENERAL_BONDING;
+       conn->io_capability = hdev->io_capability;
+       conn->remote_auth = 0xff;
+       conn->key_type = 0xff;
 
        conn->power_save = 1;
        conn->disc_timeout = HCI_DISCONN_TIMEOUT;
@@ -258,6 +376,8 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
 
        setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
        setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
+       setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept,
+                                                       (unsigned long) conn);
 
        atomic_set(&conn->refcnt, 0);
 
@@ -288,6 +408,8 @@ int hci_conn_del(struct hci_conn *conn)
 
        del_timer(&conn->disc_timer);
 
+       del_timer(&conn->auto_accept_timer);
+
        if (conn->type == ACL_LINK) {
                struct hci_conn *sco = conn->link;
                if (sco)
@@ -295,6 +417,11 @@ int hci_conn_del(struct hci_conn *conn)
 
                /* Unacked frames */
                hdev->acl_cnt += conn->sent;
+       } else if (conn->type == LE_LINK) {
+               if (hdev->le_pkts)
+                       hdev->le_cnt += conn->sent;
+               else
+                       hdev->acl_cnt += conn->sent;
        } else {
                struct hci_conn *acl = conn->link;
                if (acl) {
@@ -317,6 +444,9 @@ int hci_conn_del(struct hci_conn *conn)
 
        hci_dev_put(hdev);
 
+       if (conn->handle == 0)
+               kfree(conn);
+
        return 0;
 }
 
@@ -360,15 +490,40 @@ struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
 }
 EXPORT_SYMBOL(hci_get_route);
 
-/* Create SCO or ACL connection.
+/* Create SCO, ACL or LE connection.
  * Device _must_ be locked */
 struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8 sec_level, __u8 auth_type)
 {
        struct hci_conn *acl;
        struct hci_conn *sco;
+       struct hci_conn *le;
 
        BT_DBG("%s dst %s", hdev->name, batostr(dst));
 
+       if (type == LE_LINK) {
+               struct adv_entry *entry;
+
+               le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
+               if (le)
+                       return ERR_PTR(-EBUSY);
+
+               entry = hci_find_adv_entry(hdev, dst);
+               if (!entry)
+                       return ERR_PTR(-EHOSTUNREACH);
+
+               le = hci_conn_add(hdev, LE_LINK, dst);
+               if (!le)
+                       return ERR_PTR(-ENOMEM);
+
+               le->dst_type = entry->bdaddr_type;
+
+               hci_le_connect(le);
+
+               hci_conn_hold(le);
+
+               return le;
+       }
+
        acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
        if (!acl) {
                acl = hci_conn_add(hdev, ACL_LINK, dst);
@@ -405,7 +560,7 @@ struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8
        if (acl->state == BT_CONNECTED &&
                        (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
                acl->power_save = 1;
-               hci_conn_enter_active_mode(acl);
+               hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
 
                if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
                        /* defer SCO setup until mode change completed */
@@ -456,41 +611,98 @@ static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
                cp.handle = cpu_to_le16(conn->handle);
                hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
                                                        sizeof(cp), &cp);
+               if (conn->key_type != 0xff)
+                       set_bit(HCI_CONN_REAUTH_PEND, &conn->pend);
        }
 
        return 0;
 }
 
+/* Encrypt the the link */
+static void hci_conn_encrypt(struct hci_conn *conn)
+{
+       BT_DBG("conn %p", conn);
+
+       if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
+               struct hci_cp_set_conn_encrypt cp;
+               cp.handle  = cpu_to_le16(conn->handle);
+               cp.encrypt = 0x01;
+               hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
+                                                                       &cp);
+       }
+}
+
 /* Enable security */
 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
 {
        BT_DBG("conn %p", conn);
 
+       /* For sdp we don't need the link key. */
        if (sec_level == BT_SECURITY_SDP)
                return 1;
 
+       /* For non 2.1 devices and low security level we don't need the link
+          key. */
        if (sec_level == BT_SECURITY_LOW &&
                                (!conn->ssp_mode || !conn->hdev->ssp_mode))
                return 1;
 
-       if (conn->link_mode & HCI_LM_ENCRYPT)
-               return hci_conn_auth(conn, sec_level, auth_type);
+       /* For other security levels we need the link key. */
+       if (!(conn->link_mode & HCI_LM_AUTH))
+               goto auth;
+
+       /* An authenticated combination key has sufficient security for any
+          security level. */
+       if (conn->key_type == HCI_LK_AUTH_COMBINATION)
+               goto encrypt;
+
+       /* An unauthenticated combination key has sufficient security for
+          security level 1 and 2. */
+       if (conn->key_type == HCI_LK_UNAUTH_COMBINATION &&
+                       (sec_level == BT_SECURITY_MEDIUM ||
+                       sec_level == BT_SECURITY_LOW))
+               goto encrypt;
+
+       /* A combination key has always sufficient security for the security
+          levels 1 or 2. High security level requires the combination key
+          is generated using maximum PIN code length (16).
+          For pre 2.1 units. */
+       if (conn->key_type == HCI_LK_COMBINATION &&
+                       (sec_level != BT_SECURITY_HIGH ||
+                       conn->pin_length == 16))
+               goto encrypt;
+
+auth:
+       if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
+               return 0;
 
-       if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
+       if (!hci_conn_auth(conn, sec_level, auth_type))
                return 0;
 
-       if (hci_conn_auth(conn, sec_level, auth_type)) {
-               struct hci_cp_set_conn_encrypt cp;
-               cp.handle  = cpu_to_le16(conn->handle);
-               cp.encrypt = 1;
-               hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
-                                                       sizeof(cp), &cp);
-       }
+encrypt:
+       if (conn->link_mode & HCI_LM_ENCRYPT)
+               return 1;
 
+       hci_conn_encrypt(conn);
        return 0;
 }
 EXPORT_SYMBOL(hci_conn_security);
 
+/* Check secure link requirement */
+int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
+{
+       BT_DBG("conn %p", conn);
+
+       if (sec_level != BT_SECURITY_HIGH)
+               return 1; /* Accept if non-secure is required */
+
+       if (conn->sec_level == BT_SECURITY_HIGH)
+               return 1;
+
+       return 0; /* Reject not secure link */
+}
+EXPORT_SYMBOL(hci_conn_check_secure);
+
 /* Change link key */
 int hci_conn_change_link_key(struct hci_conn *conn)
 {
@@ -527,7 +739,7 @@ int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
 EXPORT_SYMBOL(hci_conn_switch_role);
 
 /* Enter active mode */
-void hci_conn_enter_active_mode(struct hci_conn *conn)
+void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
 {
        struct hci_dev *hdev = conn->hdev;
 
@@ -536,7 +748,10 @@ void hci_conn_enter_active_mode(struct hci_conn *conn)
        if (test_bit(HCI_RAW, &hdev->flags))
                return;
 
-       if (conn->mode != HCI_CM_SNIFF || !conn->power_save)
+       if (conn->mode != HCI_CM_SNIFF)
+               goto timer;
+
+       if (!conn->power_save && !force_active)
                goto timer;
 
        if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {