From: Ralf Lici ovpn_encrypt_post updates peer TX stats and last_sent as soon as an encrypted skb is handed to the TCP transport. However, the skb may still be dropped locally: when the per-peer output queue is full, when another output skb is still busy, or later, when the skbs queued while the socket was owned by userspace are flushed by the release callback. Make the TCP send helpers return an error when they cannot consume the skb, so that callers account and free the drop, and update peer TX stats and last_sent for TCP only once the packet has been sent. Fixes: 11851cbd60ea ("ovpn: implement TCP transport") Cc: stable@vger.kernel.org Signed-off-by: Ralf Lici [antonio: account peer TX stats and last_sent once the packet is sent, to also cover skbs dropped from the output queue] Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/io.c | 12 +++++--- drivers/net/ovpn/skb.h | 2 ++ drivers/net/ovpn/tcp.c | 67 ++++++++++++++++++++++++++++++++---------- drivers/net/ovpn/tcp.h | 10 ++----- 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/drivers/net/ovpn/io.c b/drivers/net/ovpn/io.c index a11ad979e9f3..fc1f5aeefb41 100644 --- a/drivers/net/ovpn/io.c +++ b/drivers/net/ovpn/io.c @@ -284,18 +284,22 @@ void ovpn_encrypt_post(void *data, int ret) ret = ovpn_udp_send_skb(peer, sock->sk, skb); if (unlikely(ret < 0)) goto err_unlock; + + ovpn_peer_stats_increment_tx(&peer->link_stats, orig_len); + /* keep track of last sent packet for keepalive */ + WRITE_ONCE(peer->last_sent, ktime_get_boottime_seconds()); break; case IPPROTO_TCP: - ovpn_tcp_send_skb(peer, sock->sk, skb); + /* TCP accounts the packet once it is actually sent */ + ret = ovpn_tcp_send_skb(peer, sock->sk, skb); + if (unlikely(ret < 0)) + goto err_unlock; break; default: /* no transport configured yet */ goto err_unlock; } - ovpn_peer_stats_increment_tx(&peer->link_stats, orig_len); - /* keep track of last sent packet for keepalive */ - WRITE_ONCE(peer->last_sent, ktime_get_boottime_seconds()); /* skb passed down the stack - don't free it */ skb = NULL; err_unlock: diff --git a/drivers/net/ovpn/skb.h b/drivers/net/ovpn/skb.h index 4fb7ea025426..578e1268bbc3 100644 --- a/drivers/net/ovpn/skb.h +++ b/drivers/net/ovpn/skb.h @@ -26,6 +26,7 @@ * containing the IV, the scatter gather list and the aead request * @payload_offset: offset in the skb where the payload starts * @nosignal: whether this skb should be sent with the MSG_NOSIGNAL flag (TCP) + * @data: whether this skb is an encrypted data packet (TCP) */ struct ovpn_cb { struct ovpn_peer *peer; @@ -33,6 +34,7 @@ struct ovpn_cb { void *crypto_tmp; unsigned int payload_offset; bool nosignal; + bool data; }; static inline struct ovpn_cb *ovpn_skb_cb(struct sk_buff *skb) diff --git a/drivers/net/ovpn/tcp.c b/drivers/net/ovpn/tcp.c index 00cab8d2aa6a..dbdb6eeb3c08 100644 --- a/drivers/net/ovpn/tcp.c +++ b/drivers/net/ovpn/tcp.c @@ -297,6 +297,17 @@ static void ovpn_tcp_send_sock(struct ovpn_peer *peer, struct sock *sk) dev_dstats_tx_add(peer->ovpn->dev, skb->len - skb_inner_network_offset(skb)); local_bh_enable(); + + if (ovpn_skb_cb(skb)->data) { + /* exclude the length prefix, like UDP excludes its + * header + */ + ovpn_peer_stats_increment_tx(&peer->link_stats, + skb->len - sizeof(u16)); + /* keep track of last sent packet for keepalive */ + WRITE_ONCE(peer->last_sent, + ktime_get_boottime_seconds()); + } } kfree_skb(peer->tcp.out_msg.skb); @@ -320,45 +331,57 @@ void ovpn_tcp_tx_work(struct work_struct *work) release_sock(sock->sk); } -static void ovpn_tcp_send_sock_skb(struct ovpn_peer *peer, struct sock *sk, - struct sk_buff *skb) +static int ovpn_tcp_send_sock_skb(struct ovpn_peer *peer, struct sock *sk, + struct sk_buff *skb) { if (peer->tcp.out_msg.skb) ovpn_tcp_send_sock(peer, sk); - if (peer->tcp.out_msg.skb) { - ovpn_dev_dstats_tx_dropped(peer->ovpn->dev); - kfree_skb(skb); - return; - } + if (peer->tcp.out_msg.skb) + return -EBUSY; peer->tcp.out_msg.skb = skb; peer->tcp.out_msg.len = skb->len; peer->tcp.out_msg.offset = 0; ovpn_tcp_send_sock(peer, sk); + return 0; } -void ovpn_tcp_send_skb(struct ovpn_peer *peer, struct sock *sk, - struct sk_buff *skb) +/** + * ovpn_tcp_send_skb - Prepare skb and enqueue it for sending to peer + * @peer: destination peer + * @sk: transport socket + * @skb: packet to send + * + * Prepends the skb payload length, as required by the OpenVPN protocol in + * order to extract packets from the TCP stream on the receiver side. + * + * Return: 0 on success or a negative error code otherwise. On failure, the + * caller retains ownership of @skb. + */ +int ovpn_tcp_send_skb(struct ovpn_peer *peer, struct sock *sk, + struct sk_buff *skb) { u16 len = skb->len; + int ret = 0; + ovpn_skb_cb(skb)->data = true; *(__be16 *)__skb_push(skb, sizeof(u16)) = htons(len); spin_lock_nested(&sk->sk_lock.slock, OVPN_TCP_DEPTH_NESTING); if (sock_owned_by_user(sk)) { if (skb_queue_len(&peer->tcp.out_queue) >= READ_ONCE(net_hotdata.max_backlog)) { - ovpn_dev_dstats_tx_dropped(peer->ovpn->dev); - kfree_skb(skb); + ret = -ENOBUFS; goto unlock; } __skb_queue_tail(&peer->tcp.out_queue, skb); } else { - ovpn_tcp_send_sock_skb(peer, sk, skb); + ret = ovpn_tcp_send_sock_skb(peer, sk, skb); } unlock: spin_unlock(&sk->sk_lock.slock); + return ret; } static void ovpn_tcp_release(struct sock *sk) @@ -367,6 +390,7 @@ static void ovpn_tcp_release(struct sock *sk) struct ovpn_socket *sock; struct ovpn_peer *peer; struct sk_buff *skb; + int ret; rcu_read_lock(); sock = rcu_dereference_sk_user_data(sk); @@ -389,8 +413,13 @@ static void ovpn_tcp_release(struct sock *sk) __skb_queue_head_init(&queue); skb_queue_splice_init(&peer->tcp.out_queue, &queue); - while ((skb = __skb_dequeue(&queue))) - ovpn_tcp_send_sock_skb(peer, sk, skb); + while ((skb = __skb_dequeue(&queue))) { + ret = ovpn_tcp_send_sock_skb(peer, sk, skb); + if (unlikely(ret < 0)) { + ovpn_dev_dstats_tx_dropped(peer->ovpn->dev); + kfree_skb(skb); + } + } peer->tcp.sk_cb.prot->release_cb(sk); ovpn_peer_put(peer); @@ -450,8 +479,14 @@ static int ovpn_tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) /* userspace messages are not encrypted: account them entirely */ skb_reset_inner_network_header(skb); ovpn_skb_cb(skb)->nosignal = msg->msg_flags & MSG_NOSIGNAL; - ovpn_tcp_send_sock_skb(peer, sk, skb); - ret = size; + ret = ovpn_tcp_send_sock_skb(peer, sk, skb); + if (unlikely(ret < 0)) { + ovpn_dev_dstats_tx_dropped(peer->ovpn->dev); + kfree_skb(skb); + } else { + ret = size; + } + peer_free: release_sock(sk); ovpn_peer_put(peer); diff --git a/drivers/net/ovpn/tcp.h b/drivers/net/ovpn/tcp.h index a3aa3570ae5e..957f7f7db6c8 100644 --- a/drivers/net/ovpn/tcp.h +++ b/drivers/net/ovpn/tcp.h @@ -24,14 +24,8 @@ int ovpn_tcp_socket_attach(struct ovpn_socket *ovpn_sock, void ovpn_tcp_socket_detach(struct ovpn_socket *ovpn_sock); void ovpn_tcp_socket_wait_finish(struct ovpn_socket *sock); -/* Prepare skb and enqueue it for sending to peer. - * - * Preparation consist in prepending the skb payload with its size. - * Required by the OpenVPN protocol in order to extract packets from - * the TCP stream on the receiver side. - */ -void ovpn_tcp_send_skb(struct ovpn_peer *peer, struct sock *sk, - struct sk_buff *skb); +int ovpn_tcp_send_skb(struct ovpn_peer *peer, struct sock *sk, + struct sk_buff *skb); void ovpn_tcp_tx_work(struct work_struct *work); #endif /* _NET_OVPN_TCP_H_ */