Bluetooth: Send control open and close messages for HCI user channels
[cascardo/linux.git] / net / bluetooth / hci_sock.c
index d37c224..48f9471 100644 (file)
@@ -84,6 +84,33 @@ u32 hci_sock_get_cookie(struct sock *sk)
        return hci_pi(sk)->cookie;
 }
 
+static bool hci_sock_gen_cookie(struct sock *sk)
+{
+       int id = hci_pi(sk)->cookie;
+
+       if (!id) {
+               id = ida_simple_get(&sock_cookie_ida, 1, 0, GFP_KERNEL);
+               if (id < 0)
+                       id = 0xffffffff;
+
+               hci_pi(sk)->cookie = id;
+               get_task_comm(hci_pi(sk)->comm, current);
+               return true;
+       }
+
+       return false;
+}
+
+static void hci_sock_free_cookie(struct sock *sk)
+{
+       int id = hci_pi(sk)->cookie;
+
+       if (id) {
+               hci_pi(sk)->cookie = 0xffffffff;
+               ida_simple_remove(&sock_cookie_ida, id);
+       }
+}
+
 static inline int hci_test_bit(int nr, const void *addr)
 {
        return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
@@ -452,15 +479,38 @@ static struct sk_buff *create_monitor_ctrl_open(struct sock *sk)
 {
        struct hci_mon_hdr *hdr;
        struct sk_buff *skb;
-       u16 format = 0x0002;
+       u16 format;
        u8 ver[3];
        u32 flags;
 
+       /* No message needed when cookie is not present */
+       if (!hci_pi(sk)->cookie)
+               return NULL;
+
+       switch (hci_pi(sk)->channel) {
+       case HCI_CHANNEL_RAW:
+               format = 0x0000;
+               ver[0] = BT_SUBSYS_VERSION;
+               put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
+               break;
+       case HCI_CHANNEL_USER:
+               format = 0x0001;
+               ver[0] = BT_SUBSYS_VERSION;
+               put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
+               break;
+       case HCI_CHANNEL_CONTROL:
+               format = 0x0002;
+               mgmt_fill_version_info(ver);
+               break;
+       default:
+               /* No message for unsupported format */
+               return NULL;
+       }
+
        skb = bt_skb_alloc(14 + TASK_COMM_LEN , GFP_ATOMIC);
        if (!skb)
                return NULL;
 
-       mgmt_fill_version_info(ver);
        flags = hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) ? 0x1 : 0x0;
 
        put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
@@ -474,7 +524,10 @@ static struct sk_buff *create_monitor_ctrl_open(struct sock *sk)
 
        hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
        hdr->opcode = cpu_to_le16(HCI_MON_CTRL_OPEN);
-       hdr->index = cpu_to_le16(HCI_DEV_NONE);
+       if (hci_pi(sk)->hdev)
+               hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
+       else
+               hdr->index = cpu_to_le16(HCI_DEV_NONE);
        hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
 
        return skb;
@@ -485,6 +538,20 @@ static struct sk_buff *create_monitor_ctrl_close(struct sock *sk)
        struct hci_mon_hdr *hdr;
        struct sk_buff *skb;
 
+       /* No message needed when cookie is not present */
+       if (!hci_pi(sk)->cookie)
+               return NULL;
+
+       switch (hci_pi(sk)->channel) {
+       case HCI_CHANNEL_RAW:
+       case HCI_CHANNEL_USER:
+       case HCI_CHANNEL_CONTROL:
+               break;
+       default:
+               /* No message for unsupported format */
+               return NULL;
+       }
+
        skb = bt_skb_alloc(4, GFP_ATOMIC);
        if (!skb)
                return NULL;
@@ -495,7 +562,10 @@ static struct sk_buff *create_monitor_ctrl_close(struct sock *sk)
 
        hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
        hdr->opcode = cpu_to_le16(HCI_MON_CTRL_CLOSE);
-       hdr->index = cpu_to_le16(HCI_DEV_NONE);
+       if (hci_pi(sk)->hdev)
+               hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
+       else
+               hdr->index = cpu_to_le16(HCI_DEV_NONE);
        hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
 
        return skb;
@@ -611,9 +681,6 @@ static void send_monitor_control_replay(struct sock *mon_sk)
        sk_for_each(sk, &hci_sk_list.head) {
                struct sk_buff *skb;
 
-               if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
-                       continue;
-
                skb = create_monitor_ctrl_open(sk);
                if (!skb)
                        continue;
@@ -753,7 +820,6 @@ static int hci_sock_release(struct socket *sock)
        struct sock *sk = sock->sk;
        struct hci_dev *hdev;
        struct sk_buff *skb;
-       int id;
 
        BT_DBG("sock %p sk %p", sock, sk);
 
@@ -766,9 +832,9 @@ static int hci_sock_release(struct socket *sock)
        case HCI_CHANNEL_MONITOR:
                atomic_dec(&monitor_promisc);
                break;
+       case HCI_CHANNEL_RAW:
+       case HCI_CHANNEL_USER:
        case HCI_CHANNEL_CONTROL:
-               id = hci_pi(sk)->cookie;
-
                /* Send event to monitor */
                skb = create_monitor_ctrl_close(sk);
                if (skb) {
@@ -777,8 +843,7 @@ static int hci_sock_release(struct socket *sock)
                        kfree_skb(skb);
                }
 
-               hci_pi(sk)->cookie = 0xffffffff;
-               ida_simple_remove(&sock_cookie_ida, id);
+               hci_sock_free_cookie(sk);
                break;
        }
 
@@ -907,6 +972,27 @@ static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
                goto done;
        }
 
+       /* When calling an ioctl on an unbound raw socket, then ensure
+        * that the monitor gets informed. Ensure that the resulting event
+        * is only send once by checking if the cookie exists or not. The
+        * socket cookie will be only ever generated once for the lifetime
+        * of a given socket.
+        */
+       if (hci_sock_gen_cookie(sk)) {
+               struct sk_buff *skb;
+
+               if (capable(CAP_NET_ADMIN))
+                       hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
+
+               /* Send event to monitor */
+               skb = create_monitor_ctrl_open(sk);
+               if (skb) {
+                       hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
+                                           HCI_SOCK_TRUSTED, NULL);
+                       kfree_skb(skb);
+               }
+       }
+
        release_sock(sk);
 
        switch (cmd) {
@@ -970,6 +1056,7 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
        struct sockaddr_hci haddr;
        struct sock *sk = sock->sk;
        struct hci_dev *hdev = NULL;
+       struct sk_buff *skb;
        int len, err = 0;
 
        BT_DBG("sock %p sk %p", sock, sk);
@@ -1008,7 +1095,35 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
                        atomic_inc(&hdev->promisc);
                }
 
+               hci_pi(sk)->channel = haddr.hci_channel;
+
+               if (!hci_sock_gen_cookie(sk)) {
+                       /* In the case when a cookie has already been assigned,
+                        * then there has been already an ioctl issued against
+                        * an unbound socket and with that triggerd an open
+                        * notification. Send a close notification first to
+                        * allow the state transition to bounded.
+                        */
+                       skb = create_monitor_ctrl_close(sk);
+                       if (skb) {
+                               hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
+                                                   HCI_SOCK_TRUSTED, NULL);
+                               kfree_skb(skb);
+                       }
+               }
+
+               if (capable(CAP_NET_ADMIN))
+                       hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
+
                hci_pi(sk)->hdev = hdev;
+
+               /* Send event to monitor */
+               skb = create_monitor_ctrl_open(sk);
+               if (skb) {
+                       hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
+                                           HCI_SOCK_TRUSTED, NULL);
+                       kfree_skb(skb);
+               }
                break;
 
        case HCI_CHANNEL_USER:
@@ -1070,9 +1185,38 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
                        }
                }
 
-               atomic_inc(&hdev->promisc);
+               hci_pi(sk)->channel = haddr.hci_channel;
+
+               if (!hci_sock_gen_cookie(sk)) {
+                       /* In the case when a cookie has already been assigned,
+                        * this socket will transition from a raw socket into
+                        * an user channel socket. For a clean transition, send
+                        * the close notification first.
+                        */
+                       skb = create_monitor_ctrl_close(sk);
+                       if (skb) {
+                               hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
+                                                   HCI_SOCK_TRUSTED, NULL);
+                               kfree_skb(skb);
+                       }
+               }
+
+               /* The user channel is restricted to CAP_NET_ADMIN
+                * capabilities and with that implicitly trusted.
+                */
+               hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
 
                hci_pi(sk)->hdev = hdev;
+
+               /* Send event to monitor */
+               skb = create_monitor_ctrl_open(sk);
+               if (skb) {
+                       hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
+                                           HCI_SOCK_TRUSTED, NULL);
+                       kfree_skb(skb);
+               }
+
+               atomic_inc(&hdev->promisc);
                break;
 
        case HCI_CHANNEL_MONITOR:
@@ -1086,6 +1230,8 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
                        goto done;
                }
 
+               hci_pi(sk)->channel = haddr.hci_channel;
+
                /* The monitor interface is restricted to CAP_NET_RAW
                 * capabilities and with that implicitly trusted.
                 */
@@ -1094,8 +1240,8 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
                send_monitor_note(sk, "Linux version %s (%s)",
                                  init_utsname()->release,
                                  init_utsname()->machine);
-               send_monitor_note(sk, "Bluetooth subsystem version %s",
-                                 BT_SUBSYS_VERSION);
+               send_monitor_note(sk, "Bluetooth subsystem version %u.%u",
+                                 BT_SUBSYS_VERSION, BT_SUBSYS_REVISION);
                send_monitor_replay(sk);
                send_monitor_control_replay(sk);
 
@@ -1112,6 +1258,8 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
                        err = -EPERM;
                        goto done;
                }
+
+               hci_pi(sk)->channel = haddr.hci_channel;
                break;
 
        default:
@@ -1133,6 +1281,8 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
                if (capable(CAP_NET_ADMIN))
                        hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
 
+               hci_pi(sk)->channel = haddr.hci_channel;
+
                /* At the moment the index and unconfigured index events
                 * are enabled unconditionally. Setting them on each
                 * socket when binding keeps this functionality. They
@@ -1143,16 +1293,21 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
                 * received by untrusted users. Example for such events
                 * are changes to settings, class of device, name etc.
                 */
-               if (haddr.hci_channel == HCI_CHANNEL_CONTROL) {
-                       struct sk_buff *skb;
-                       int id;
-
-                       id = ida_simple_get(&sock_cookie_ida, 1, 0, GFP_KERNEL);
-                       if (id < 0)
-                               id = 0xffffffff;
-
-                       hci_pi(sk)->cookie = id;
-                       get_task_comm(hci_pi(sk)->comm, current);
+               if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) {
+                       if (!hci_sock_gen_cookie(sk)) {
+                               /* In the case when a cookie has already been
+                                * assigned, this socket will transtion from
+                                * a raw socket into a control socket. To
+                                * allow for a clean transtion, send the
+                                * close notification first.
+                                */
+                               skb = create_monitor_ctrl_close(sk);
+                               if (skb) {
+                                       hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
+                                                           HCI_SOCK_TRUSTED, NULL);
+                                       kfree_skb(skb);
+                               }
+                       }
 
                        /* Send event to monitor */
                        skb = create_monitor_ctrl_open(sk);
@@ -1172,8 +1327,6 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
                break;
        }
 
-
-       hci_pi(sk)->channel = haddr.hci_channel;
        sk->sk_state = BT_BOUND;
 
 done: