Commit 64ba2eb3 authored by Luiz Augusto von Dentz's avatar Luiz Augusto von Dentz Committed by Marcel Holtmann

Bluetooth: hci_sock: Replace use of memcpy_from_msg with bt_skb_sendmsg

This makes use of bt_skb_sendmsg instead of allocating a different
buffer to be used with memcpy_from_msg which cause one extra copy.
Tested-by: default avatarTedd Ho-Jeong An <tedd.an@intel.com>
Signed-off-by: default avatarLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Signed-off-by: default avatarMarcel Holtmann <marcel@holtmann.org>
parent 4d51fb04
...@@ -1510,7 +1510,8 @@ static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, ...@@ -1510,7 +1510,8 @@ static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,
return err ? : copied; return err ? : copied;
} }
static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, void *buf, size_t msglen) static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
struct sk_buff *skb)
{ {
u8 *cp; u8 *cp;
struct mgmt_hdr *hdr; struct mgmt_hdr *hdr;
...@@ -1520,31 +1521,31 @@ static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, void *buf, ...@@ -1520,31 +1521,31 @@ static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, void *buf,
bool var_len, no_hdev; bool var_len, no_hdev;
int err; int err;
BT_DBG("got %zu bytes", msglen); BT_DBG("got %d bytes", skb->len);
if (msglen < sizeof(*hdr)) if (skb->len < sizeof(*hdr))
return -EINVAL; return -EINVAL;
hdr = buf; hdr = (void *)skb->data;
opcode = __le16_to_cpu(hdr->opcode); opcode = __le16_to_cpu(hdr->opcode);
index = __le16_to_cpu(hdr->index); index = __le16_to_cpu(hdr->index);
len = __le16_to_cpu(hdr->len); len = __le16_to_cpu(hdr->len);
if (len != msglen - sizeof(*hdr)) { if (len != skb->len - sizeof(*hdr)) {
err = -EINVAL; err = -EINVAL;
goto done; goto done;
} }
if (chan->channel == HCI_CHANNEL_CONTROL) { if (chan->channel == HCI_CHANNEL_CONTROL) {
struct sk_buff *skb; struct sk_buff *cmd;
/* Send event to monitor */ /* Send event to monitor */
skb = create_monitor_ctrl_command(sk, index, opcode, len, cmd = create_monitor_ctrl_command(sk, index, opcode, len,
buf + sizeof(*hdr)); skb->data + sizeof(*hdr));
if (skb) { if (cmd) {
hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, hci_send_to_channel(HCI_CHANNEL_MONITOR, cmd,
HCI_SOCK_TRUSTED, NULL); HCI_SOCK_TRUSTED, NULL);
kfree_skb(skb); kfree_skb(cmd);
} }
} }
...@@ -1609,13 +1610,13 @@ static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, void *buf, ...@@ -1609,13 +1610,13 @@ static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, void *buf,
if (hdev && chan->hdev_init) if (hdev && chan->hdev_init)
chan->hdev_init(sk, hdev); chan->hdev_init(sk, hdev);
cp = buf + sizeof(*hdr); cp = skb->data + sizeof(*hdr);
err = handler->func(sk, hdev, cp, len); err = handler->func(sk, hdev, cp, len);
if (err < 0) if (err < 0)
goto done; goto done;
err = msglen; err = skb->len;
done: done:
if (hdev) if (hdev)
...@@ -1624,10 +1625,10 @@ static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, void *buf, ...@@ -1624,10 +1625,10 @@ static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk, void *buf,
return err; return err;
} }
static int hci_logging_frame(struct sock *sk, void *buf, int len, unsigned int flags) static int hci_logging_frame(struct sock *sk, struct sk_buff *skb,
unsigned int flags)
{ {
struct hci_mon_hdr *hdr; struct hci_mon_hdr *hdr;
struct sk_buff *skb;
struct hci_dev *hdev; struct hci_dev *hdev;
u16 index; u16 index;
int err; int err;
...@@ -1636,21 +1637,13 @@ static int hci_logging_frame(struct sock *sk, void *buf, int len, unsigned int f ...@@ -1636,21 +1637,13 @@ static int hci_logging_frame(struct sock *sk, void *buf, int len, unsigned int f
* the priority byte, the ident length byte and at least one string * the priority byte, the ident length byte and at least one string
* terminator NUL byte. Anything shorter are invalid packets. * terminator NUL byte. Anything shorter are invalid packets.
*/ */
if (len < sizeof(*hdr) + 3) if (skb->len < sizeof(*hdr) + 3)
return -EINVAL; return -EINVAL;
skb = bt_skb_send_alloc(sk, len, flags & MSG_DONTWAIT, &err);
if (!skb)
return err;
memcpy(skb_put(skb, len), buf, len);
hdr = (void *)skb->data; hdr = (void *)skb->data;
if (__le16_to_cpu(hdr->len) != len - sizeof(*hdr)) { if (__le16_to_cpu(hdr->len) != skb->len - sizeof(*hdr))
err = -EINVAL; return -EINVAL;
goto drop;
}
if (__le16_to_cpu(hdr->opcode) == 0x0000) { if (__le16_to_cpu(hdr->opcode) == 0x0000) {
__u8 priority = skb->data[sizeof(*hdr)]; __u8 priority = skb->data[sizeof(*hdr)];
...@@ -1669,25 +1662,20 @@ static int hci_logging_frame(struct sock *sk, void *buf, int len, unsigned int f ...@@ -1669,25 +1662,20 @@ static int hci_logging_frame(struct sock *sk, void *buf, int len, unsigned int f
* The message follows the ident string (if present) and * The message follows the ident string (if present) and
* must be NUL terminated. Otherwise it is not a valid packet. * must be NUL terminated. Otherwise it is not a valid packet.
*/ */
if (priority > 7 || skb->data[len - 1] != 0x00 || if (priority > 7 || skb->data[skb->len - 1] != 0x00 ||
ident_len > len - sizeof(*hdr) - 3 || ident_len > skb->len - sizeof(*hdr) - 3 ||
skb->data[sizeof(*hdr) + ident_len + 1] != 0x00) { skb->data[sizeof(*hdr) + ident_len + 1] != 0x00)
err = -EINVAL; return -EINVAL;
goto drop;
}
} else { } else {
err = -EINVAL; return -EINVAL;
goto drop;
} }
index = __le16_to_cpu(hdr->index); index = __le16_to_cpu(hdr->index);
if (index != MGMT_INDEX_NONE) { if (index != MGMT_INDEX_NONE) {
hdev = hci_dev_get(index); hdev = hci_dev_get(index);
if (!hdev) { if (!hdev)
err = -ENODEV; return -ENODEV;
goto drop;
}
} else { } else {
hdev = NULL; hdev = NULL;
} }
...@@ -1695,13 +1683,11 @@ static int hci_logging_frame(struct sock *sk, void *buf, int len, unsigned int f ...@@ -1695,13 +1683,11 @@ static int hci_logging_frame(struct sock *sk, void *buf, int len, unsigned int f
hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING); hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING);
hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL); hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL);
err = len; err = skb->len;
if (hdev) if (hdev)
hci_dev_put(hdev); hci_dev_put(hdev);
drop:
kfree_skb(skb);
return err; return err;
} }
...@@ -1713,7 +1699,6 @@ static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, ...@@ -1713,7 +1699,6 @@ static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
struct hci_dev *hdev; struct hci_dev *hdev;
struct sk_buff *skb; struct sk_buff *skb;
int err; int err;
void *buf;
const unsigned int flags = msg->msg_flags; const unsigned int flags = msg->msg_flags;
BT_DBG("sock %p sk %p", sock, sk); BT_DBG("sock %p sk %p", sock, sk);
...@@ -1727,13 +1712,9 @@ static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, ...@@ -1727,13 +1712,9 @@ static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
if (len < 4 || len > hci_pi(sk)->mtu) if (len < 4 || len > hci_pi(sk)->mtu)
return -EINVAL; return -EINVAL;
buf = kmalloc(len, GFP_KERNEL); skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
if (!buf) if (IS_ERR(skb))
return -ENOMEM; return PTR_ERR(skb);
if (memcpy_from_msg(buf, msg, len)) {
kfree(buf);
return -EFAULT;
}
lock_sock(sk); lock_sock(sk);
...@@ -1743,39 +1724,33 @@ static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, ...@@ -1743,39 +1724,33 @@ static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
break; break;
case HCI_CHANNEL_MONITOR: case HCI_CHANNEL_MONITOR:
err = -EOPNOTSUPP; err = -EOPNOTSUPP;
goto done; goto drop;
case HCI_CHANNEL_LOGGING: case HCI_CHANNEL_LOGGING:
err = hci_logging_frame(sk, buf, len, flags); err = hci_logging_frame(sk, skb, flags);
goto done; goto drop;
default: default:
mutex_lock(&mgmt_chan_list_lock); mutex_lock(&mgmt_chan_list_lock);
chan = __hci_mgmt_chan_find(hci_pi(sk)->channel); chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
if (chan) if (chan)
err = hci_mgmt_cmd(chan, sk, buf, len); err = hci_mgmt_cmd(chan, sk, skb);
else else
err = -EINVAL; err = -EINVAL;
mutex_unlock(&mgmt_chan_list_lock); mutex_unlock(&mgmt_chan_list_lock);
goto done; goto drop;
} }
hdev = hci_hdev_from_sock(sk); hdev = hci_hdev_from_sock(sk);
if (IS_ERR(hdev)) { if (IS_ERR(hdev)) {
err = PTR_ERR(hdev); err = PTR_ERR(hdev);
goto done; goto drop;
} }
if (!test_bit(HCI_UP, &hdev->flags)) { if (!test_bit(HCI_UP, &hdev->flags)) {
err = -ENETDOWN; err = -ENETDOWN;
goto done; goto drop;
} }
skb = bt_skb_send_alloc(sk, len, flags & MSG_DONTWAIT, &err);
if (!skb)
goto done;
memcpy(skb_put(skb, len), buf, len);
hci_skb_pkt_type(skb) = skb->data[0]; hci_skb_pkt_type(skb) = skb->data[0];
skb_pull(skb, 1); skb_pull(skb, 1);
...@@ -1846,7 +1821,6 @@ static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, ...@@ -1846,7 +1821,6 @@ static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
done: done:
release_sock(sk); release_sock(sk);
kfree(buf);
return err; return err;
drop: drop:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment