Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit df1cb87a authored by Marcel Holtmann's avatar Marcel Holtmann
Browse files

Bluetooth: Introduce helper functions for socket cookie handling



Instead of manually allocating cookie information each time, use helper
functions for generating and releasing cookies.

Signed-off-by: default avatarMarcel Holtmann <marcel@holtmann.org>
Signed-off-by: default avatarJohan Hedberg <johan.hedberg@intel.com>
parent 3d4e2fb6
Loading
Loading
Loading
Loading
+29 −12
Original line number Original line Diff line number Diff line
@@ -84,6 +84,33 @@ u32 hci_sock_get_cookie(struct sock *sk)
	return hci_pi(sk)->cookie;
	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)
static inline int hci_test_bit(int nr, const void *addr)
{
{
	return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
	return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
@@ -753,7 +780,6 @@ static int hci_sock_release(struct socket *sock)
	struct sock *sk = sock->sk;
	struct sock *sk = sock->sk;
	struct hci_dev *hdev;
	struct hci_dev *hdev;
	struct sk_buff *skb;
	struct sk_buff *skb;
	int id;


	BT_DBG("sock %p sk %p", sock, sk);
	BT_DBG("sock %p sk %p", sock, sk);


@@ -767,8 +793,6 @@ static int hci_sock_release(struct socket *sock)
		atomic_dec(&monitor_promisc);
		atomic_dec(&monitor_promisc);
		break;
		break;
	case HCI_CHANNEL_CONTROL:
	case HCI_CHANNEL_CONTROL:
		id = hci_pi(sk)->cookie;

		/* Send event to monitor */
		/* Send event to monitor */
		skb = create_monitor_ctrl_close(sk);
		skb = create_monitor_ctrl_close(sk);
		if (skb) {
		if (skb) {
@@ -777,8 +801,7 @@ static int hci_sock_release(struct socket *sock)
			kfree_skb(skb);
			kfree_skb(skb);
		}
		}


		hci_pi(sk)->cookie = 0xffffffff;
		hci_sock_free_cookie(sk);
		ida_simple_remove(&sock_cookie_ida, id);
		break;
		break;
	}
	}


@@ -1145,14 +1168,8 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
		 */
		 */
		if (haddr.hci_channel == HCI_CHANNEL_CONTROL) {
		if (haddr.hci_channel == HCI_CHANNEL_CONTROL) {
			struct sk_buff *skb;
			struct sk_buff *skb;
			int id;


			id = ida_simple_get(&sock_cookie_ida, 1, 0, GFP_KERNEL);
			hci_sock_gen_cookie(sk);
			if (id < 0)
				id = 0xffffffff;

			hci_pi(sk)->cookie = id;
			get_task_comm(hci_pi(sk)->comm, current);


			/* Send event to monitor */
			/* Send event to monitor */
			skb = create_monitor_ctrl_open(sk);
			skb = create_monitor_ctrl_open(sk);