From patchwork Tue Nov 10 12:54:21 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ralf Lici X-Patchwork-Id: 5649 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:6022:20a6:b0:11a:a05e:cb97 with SMTP id 38csp991098lad; Tue, 10 Nov 2026 04:54:44 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErHYzbC/1xfagZ1WfZpaRi2B7lB3DlkM6m0d0g/8ClDcDgsbFcx03RDtrB2aKzUlZzTGogz+QQx+ibU=@openvpn.net X-Received: by 2002:a05:7022:ea47:20b0:1ae:e7c4:9a15 with SMTP id a92af1059eb24-1b3b5f9d9fcmr3639104c88.3.1794315284197; Tue, 10 Nov 2026 04:54:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1794315284; cv=none; d=google.com; s=arc-20260327; b=HT+ya9VZzJPBvpaON4gO63CSh1S7JoCmlbY3KVT2MmVnJ6sLbwHtdg88N6GKhQa5AK sjyz3+U1mEE+dpfbdGvbcvidILWbZX1VgXFBE/Bb1mrdGrLqlv2TYdrtvQmCJ8YevX6j liIJpfsqN6GWrd3bY24/Dp7rHS6TRgM4B8JWwsnTdJTG5TSdUpSGsSqj/WyKS3mIJTKR 6VAbpRID5AW2iIMiSvifB5EGQ+CL0+eDVCsebG4TiX9YzwaxjSapskxo7EjJdMuTBw3H XPJqfnyXvCUU4+kIbNokn/YOzd0xuYOz3kv8oSZttKGm1WWlUQVYgR7v7JJU5ZJxAqWE HK4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature:dkim-signature; bh=rPrVTZ3SxqKfYxwxthtttGj+93BzlOLKZN5PHB0oDI8=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=Qp3EpGx7+csngtgh9zIlg/sWdEbIVYGynDR+bmWdJFnZC6gn4Yiz5cfD7qxi1aRTmT 0fcgtGL9+g39A7gg7NaXYPOEl6xlPKJFj+4ApEl/wHxi5JGXuCkxw7p0QyYBaMcYdIlR mtyv2nraCaZiLQPBnfdmckD8gCDQhAHdyropw6Q2GnToM++/UOBrUZZe2K0DRgEGQjKL pjv3ilHQzjzACooFhwdQkGZUt7ppke9M9TER4LXJjZb6VClSOEbBjsVadBwiD73wR+zX X5rjO5uHmW+bLmE7ERNH7PbKUC/yGj1YXIWR15SzdyXix6Sp8WGpvCHeniU/2bZ4y8yv 5s0g==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=A3YVLjEg; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=eTtVwUtp; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=F2zkchlB; dkim=neutral (body hash did not verify) header.i=@mandelbit.com header.s=MBO0001 header.b=NRdVK9Is; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id a92af1059eb24-1b3b5b5b1f1si6765087c88.26.2026.11.10.04.54.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Nov 2026 04:54:44 -0800 (PST) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=A3YVLjEg; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=eTtVwUtp; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=F2zkchlB; dkim=neutral (body hash did not verify) header.i=@mandelbit.com header.s=MBO0001 header.b=NRdVK9Is; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=rPrVTZ3SxqKfYxwxthtttGj+93BzlOLKZN5PHB0oDI8=; b=A3YVLjEgVyXMTG fQw19RD5BXBMq5Iue0hhX7n4MVNIH6A+NyYvI4sqIT1Td9lE8QZYHlEqmDn5iTDLtY5sTJB2i28Ls gqGGqY+czL7QzToOjSE7hgHozbmMhOSevD2pH79udxMDJOIrG3/vbiyAjf+35RZ2pAM+ThlGivHT/ 9v4=; Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.100.1) (envelope-from ) id 1xQlN3-000000001zu-2wQY; Tue, 10 Nov 2026 12:54:41 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.100.1) (envelope-from ) id 1xQlN2-000000001zh-37vB for openvpn-devel@lists.sourceforge.net; Tue, 10 Nov 2026 12:54:40 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Unsubscribe-Post:List-Subscribe:List-Post:List-Owner:List-Archive; bh=kPjHYoNl1bcseJRTim0VG+gVfBdpOqSPHDYIEqj20JI=; b=eTtVwUtpnNyAFQjS9I9nrI7V+s KRexAeKqjXgJz7dLacgrS/V1Z5UoA3qW6e9CNTnzZY6hVnsqSAPsn7b5qupCcCTJGRTf9+oOHcOTp OsamMeTMrFlfDS2QXMKeThhx8seyW1FGuStkg94fs8fgQpwvEttbivLHXipWlE9IQ0pA=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Unsubscribe-Post: List-Subscribe:List-Post:List-Owner:List-Archive; bh=kPjHYoNl1bcseJRTim0VG+gVfBdpOqSPHDYIEqj20JI=; b=F2zkchlBV49fbubNkQoMdTCGn2 3GPDVrmYbpNRON9WoCswo8z84t1rUzRlYYrU23eMuBa8pOXJNzDiMEmD5t29TczuNwrHKE4Cj6cNW GUG9FcaUHfZJGNvJ6CbbYxD0VirBsZHiUD9ZbYNJy4ZsNBBAml7T5bg91gAvqTHP1pyY=; Received: from mout-b-107.mailbox.org ([195.10.208.47]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.100.1) id 1xQlN1-0000000069B-0K5Q for openvpn-devel@lists.sourceforge.net; Tue, 10 Nov 2026 12:54:40 +0000 Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-b-107.mailbox.org (Postfix) with ESMTPS id 4jP66t5JdNz3y6x for ; Tue, 10 Nov 2026 13:54:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com; s=MBO0001; t=1794315270; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kPjHYoNl1bcseJRTim0VG+gVfBdpOqSPHDYIEqj20JI=; b=NRdVK9IsaCnX5D0kWqHE1XUAIbku8+TTIJuZ+cL1KChigKQt2Y3V3sik1ee4efNDZklUZb ucKDlPcfocAWNWrNVSPHIqeRxegS4y6GMO5kxiXIT2Keo2jp2vKme7o/cx6pnMdMtHj99s 3Z46AVebfqLoH6MDGIz2Wg6WMTFNQxJpBai/FDJ1CpTas318MkUA1bWxynDbbFLDbcCaqN OND8szfIaLwO+PpZ1xQ55JEiyX7kYY5od3VcP0bXHl1dH9dy48oihzntOsp7uxYVNBy7Yi MYvK5zz9ZynvnbUiVbMJvMKeoFWGMCIc+A6OJgoJePTyyTvVGUxKONWakOKByA== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of ralf@mandelbit.com designates 2001:67c:2050:b231:465::102 as permitted sender) smtp.mailfrom=ralf@mandelbit.com From: Ralf Lici To: openvpn-devel@lists.sourceforge.net Date: Tue, 10 Nov 2026 13:54:21 +0100 Message-ID: <36b08ba627b885149ef45de3b6a26c52f857387d.1794314969.git.ralf@mandelbit.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 4jP66t5JdNz3y6x X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-2.hosts.colo.sdot.me", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Register a UDP tunnel GRO receive callback for ovpn data sockets and coalesce compatible DATA_V2 records from one transport flow. Keep every encrypted record as a separate frag-list entry so the recei [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-Headers-End: 1xQlN1-0000000069B-0K5Q Subject: [Openvpn-devel] [PATCH ovpn net-next 4/5] ovpn: coalesce UDP data records with GRO X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: 1881475943278370124 X-GMAIL-MSGID: 1881475943278370124 Register a UDP tunnel GRO receive callback for ovpn data sockets and coalesce compatible DATA_V2 records from one transport flow. Keep every encrypted record as a separate frag-list entry so the receive path can authenticate it independently. Match the complete opcode, key and peer header and require compatible outer-network and checksum state. Flush on short records, differing segment geometry, existing GSO input, or the 64-record limit. Complete a held aggregate before an incompatible record from the same transport flow, preventing the new record from overtaking it. GRO socket lookup is not authoritative, so a generic UDP frag-list aggregate may later land on an ovpn socket, for example after DNAT. Consume only aggregates with standard, unmodified geometry, private head and child skbs, linear and uniform OpenVPN headers, and an outer tuple unchanged since GRO. Decline every other aggregate so UDP segments it and retries delivery one datagram at a time. Detach compatible frag lists directly instead of rebuilding standalone UDP packets whose outer headers would be discarded after decryption. Repair the aggregate head accounting, retain the outer headers needed for authenticated endpoint updates, propagate post-GRO protocol, ingress interface, priority, security mark and tc index metadata to every record, and clear the consumed outer VLAN tag. The socket also opts into the detachable GRO receive contract, since it's owned by userspace. Teardown removes callbacks only while they remain owned by ovpn and waits for readers which may already have captured one. This keeps ovpn detachable GRO state and callback lifetimes consistent if UDP_ENCAP changes the socket configuration. The GRO implementation does not depend on the linked ovpn UDP GSO transmit series, but the measurement kernel carried it so that transmit did not limit throughput. On two directly connected 100-Gbit/s mlx5 ports, five interleaved 60-second iperf3 -Z single-flow AES-128-GCM runs in each direction, after a 10-second warm-up, produced the following mean throughput: Forward Reverse Without GRO 18.721 Gbit/s 18.777 Gbit/s With GRO 21.542 Gbit/s 21.366 Gbit/s GRO improved throughput by 15.1% forward and 13.8% reverse. Mean cycles and instructions per byte across the monitored data-path CPUs fell from 4.535 to 4.207 (7.2%) and from 10.913 to 9.417 (13.7%), respectively. Link: https://lore.kernel.org/openvpn-devel/cover.1794244231.git.ralf@mandelbit.com/ Signed-off-by: Ralf Lici --- drivers/net/ovpn/udp.c | 300 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 291 insertions(+), 9 deletions(-) diff --git a/drivers/net/ovpn/udp.c b/drivers/net/ovpn/udp.c index 055cdb1bee13..98f55f178a95 100644 --- a/drivers/net/ovpn/udp.c +++ b/drivers/net/ovpn/udp.c @@ -8,11 +8,14 @@ #include #include +#include #include #include #include +#include #include #include +#include #include #include #include @@ -27,6 +30,235 @@ #include "socket.h" #include "udp.h" +/* like UDP and TCP frag-list GRO */ +#define OVPN_UDP_GRO_CNT_MAX 64 + +static bool ovpn_udp_gro_header(struct sk_buff *skb, u32 *header) +{ + const unsigned int offset = skb_gro_offset(skb); + + /* GRO replaces its frag0 pointer after holding an skb, so keep the + * openvpn header linear for later candidate comparisons + */ + if (!pskb_may_pull(skb, offset + OVPN_OPCODE_SIZE)) + return false; + + *header = get_unaligned_be32(skb->data + offset); + return true; +} + +static struct sk_buff *ovpn_udp_gro_receive_fraglist(struct sock *sk, + struct list_head *head, + struct sk_buff *skb) +{ + const unsigned int gso_size = skb_gro_len(skb); + struct sk_buff *p, *pp = NULL; + u32 header, header2; + int ret = 0, nhoff; + bool flush; + + /* A packet which cannot join an aggregate must not overtake records of + * the same flow which GRO is still holding. Complete those first, as + * the other GRO handlers do. + */ + if (NAPI_GRO_CB(skb)->flush || skb->encapsulation || + !ovpn_udp_gro_header(skb, &header) || + FIELD_GET(OVPN_OPCODE_PKTTYPE_MASK, header) != OVPN_DATA_V2 || + skb_is_gso(skb)) { + NAPI_GRO_CB(skb)->flush = 1; + list_for_each_entry(p, head, list) + if (NAPI_GRO_CB(p)->same_flow) + return p; + return NULL; + } + + /* udp_gro_receive sets encap_mark before invoking a tunnel callback, + * normally because the callback recurses into an inner protocol. ovpn + * retains complete outer UDP records instead, so clear it and use + * ordinary UDP frag-list completion. + */ + NAPI_GRO_CB(skb)->encap_mark = 0; + NAPI_GRO_CB(skb)->is_flist = 1; + + list_for_each_entry(p, head, list) { + if (!NAPI_GRO_CB(p)->same_flow) + continue; + + /* a different representation, opcode, key or peer ID completes + * the held aggregate; this record can start the next one + */ + if (NAPI_GRO_CB(p)->encap_mark || !NAPI_GRO_CB(p)->is_flist || + !ovpn_udp_gro_header(p, &header2) || header != header2) + return p; + + /* GRO has already matched the outer addresses and UDP ports; + * check the remaining outer IP fields + */ + flush = gro_receive_network_flush(udp_hdr(skb), udp_hdr(p), p); + + /* The first record determines the nominal GSO size. A shorter + * final record may follow it, but a larger record cannot. + * Checksum metadata must also be uniform because the aggregate + * exposes only one checksum state. + */ + if (gso_size > skb_shinfo(p)->gso_size || flush || + skb->ip_summed != p->ip_summed || + skb->csum_level != p->csum_level) { + pp = p; + } else { + /* skb_gro_receive_list pulls the headers already + * processed by GRO before linking this skb to the + * record list so we have to manually preserve the + * outer network header location for later handling + */ + nhoff = NAPI_GRO_CB(skb)->network_offset; + skb_set_network_header(skb, nhoff); + ret = skb_gro_receive_list(p, skb); + } + + /* complete the aggregate if the append failed, or after + * appending a shorter final record, or after reaching the + * record-count limit + */ + if (ret || gso_size != skb_shinfo(p)->gso_size || + NAPI_GRO_CB(p)->count >= OVPN_UDP_GRO_CNT_MAX) + pp = p; + + return pp; + } + + return NULL; +} + +static bool ovpn_udp_outer_match(const struct sk_buff *head, + const struct sk_buff *frag) +{ + const struct udphdr *uhf = udp_hdr(frag); + const struct udphdr *uhh = udp_hdr(head); + + /* NAT might have rewritten endpoints after GRO */ + if (unlikely(uhh->source != uhf->source || uhh->dest != uhf->dest)) + return false; + + switch (head->protocol) { + case htons(ETH_P_IP): + return ip_hdr(head)->saddr == ip_hdr(frag)->saddr && + ip_hdr(head)->daddr == ip_hdr(frag)->daddr; + case htons(ETH_P_IPV6): + return ipv6_addr_equal(&ipv6_hdr(head)->saddr, + &ipv6_hdr(frag)->saddr) && + ipv6_addr_equal(&ipv6_hdr(head)->daddr, + &ipv6_hdr(frag)->daddr); + default: + return false; + } +} + +/* validate an skb and turn it into a list of uniform OpenVPN records or return + * NULL without modifying an aggregate which UDP must segment + */ +static struct sk_buff *ovpn_udp_gso_prepare(struct sk_buff *skb, u32 header) +{ + unsigned int len = 0, truesize = 0; + struct sk_buff *list, *record; + + if (!skb_is_gso(skb)) { + skb_mark_not_on_list(skb); + goto out; + } + + list = skb_shinfo(skb)->frag_list; + if (unlikely(!list)) + return NULL; + + /* if any of the skbs is not private, ask the stack to segment */ + if (unlikely(skb_shared(skb) || skb_cloned(skb))) + return NULL; + + /* transformations after GRO only update the aggregate head: if its + * outer tuple changed, ask UDP to propagate it while segmenting + */ + if (unlikely(skb_pagelen(skb) != sizeof(struct udphdr) + + skb_shinfo(skb)->gso_size || + !ovpn_udp_outer_match(skb, list))) + return NULL; + + skb_walk_frags(skb, record) { + /* if the OpenVPN header is not uniform (batch created by + * generic GRO by coalescing unrelated packets) ask the stack + * to segment and handle each segment independently + */ + if (unlikely(skb_shared(record) || skb_cloned(record) || + skb_headlen(record) < OVPN_OPCODE_SIZE || + get_unaligned_be32(record->data) != header)) + return NULL; + + len += record->len; + truesize += record->truesize; + } + + if (unlikely(skb->data_len < len || skb->len < len || + skb->truesize < truesize)) + return NULL; + + /* detach the exclusively owned fraglist */ + skb_shinfo(skb)->frag_list = NULL; + skb->data_len -= len; + skb->truesize -= truesize; + skb->len -= len; + skb->next = list; + skb_gso_reset(skb); + +out: + /* skb_gro_receive_list has already pulled the UDP headers from child + * records, while the aggregate head still starts at UDP, so pull its + * header too before returning the list + */ + __skb_pull(skb, sizeof(struct udphdr)); + return skb; +} + +static void ovpn_udp_recv(struct ovpn_peer *peer, struct sk_buff *list) +{ + const __be16 proto = list->protocol; + const u32 priority = list->priority; +#ifdef CONFIG_NET_SCHED + const u16 tc_index = list->tc_index; +#endif +#ifdef CONFIG_NETWORK_SECMARK + const u32 secmark = list->secmark; +#endif + const int skb_iif = list->skb_iif; + struct sk_buff *skb, *next; + + skb_list_walk_safe(list, skb, next) { + /* propagate post-GRO metadata from the list head (its outer + * VLAN tag was already consumed by the receive stack) + */ + skb->protocol = proto; + skb->skb_iif = skb_iif; + skb->priority = priority; + __vlan_hwaccel_clear_tag(skb); +#ifdef CONFIG_NETWORK_SECMARK + skb->secmark = secmark; +#endif +#ifdef CONFIG_NET_SCHED + skb->tc_index = tc_index; +#endif + skb_mark_not_on_list(skb); + + /* keep the current reference alive for the next record before + * handing this one to crypto + */ + if (next && unlikely(!ovpn_peer_hold(peer))) { + DEBUG_NET_WARN_ON_ONCE(1); + kfree_skb_list(next); + next = NULL; + } + ovpn_recv(peer, skb); + } +} + /* Retrieve the corresponding ovpn object from a UDP socket * rcu_read_lock must be held on entry */ @@ -69,7 +301,9 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb) struct ovpn_socket *ovpn_sock; struct ovpn_priv *ovpn; struct ovpn_peer *peer; - u32 peer_id; + bool aggregate = false; + struct sk_buff *list; + u32 header, peer_id; u8 opcode; ovpn_sock = ovpn_socket_from_udp_sock(sk); @@ -85,6 +319,16 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb) goto drop_noovpn; } + /* only an accepted frag-list aggregate can carry OpenVPN records, and + * UDP can segment it if we hand it back + */ + if (skb_is_gso(skb)) { + if (unlikely(skb_shinfo(skb)->gso_type != + (SKB_GSO_UDP_L4 | SKB_GSO_FRAGLIST))) + goto drop; + aggregate = true; + } + /* Make sure the first 4 bytes of the skb data buffer after the UDP * header are accessible. * They are required to fetch the OP code, the key ID and the peer ID. @@ -96,7 +340,8 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb) goto drop; } - opcode = ovpn_opcode_from_skb(skb, sizeof(struct udphdr)); + header = get_unaligned_be32(skb->data + sizeof(struct udphdr)); + opcode = FIELD_GET(OVPN_OPCODE_PKTTYPE_MASK, header); if (unlikely(opcode != OVPN_DATA_V2)) { /* DATA_V1 is not supported */ if (opcode == OVPN_DATA_V1) @@ -106,7 +351,7 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb) return 1; } - peer_id = ovpn_peer_id_from_skb(skb, sizeof(struct udphdr)); + peer_id = FIELD_GET(OVPN_OPCODE_PEERID_MASK, header); /* some OpenVPN server implementations send data packets with the * peer-id set to UNDEF. In this case we skip the peer lookup by peer-id * and we try with the transport address @@ -119,12 +364,27 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb) if (unlikely(!peer)) goto drop; - /* pop off outer UDP header */ - __skb_pull(skb, sizeof(struct udphdr)); - ovpn_recv(peer, skb); + /* A generic aggregate may contain records for different consumers and + * post-GRO transformations may have changed only its head. Prepare + * only a private, unmodified aggregate with uniform opcode, key ID and + * peer ID; otherwise return it to UDP for segmentation. + */ + list = ovpn_udp_gso_prepare(skb, header); + if (unlikely(!list)) { + ovpn_peer_put(peer); + return 1; + } + + ovpn_udp_recv(peer, list); return 0; drop: + /* let UDP segment an accepted aggregate so that every datagram gets + * its own verdict + */ + if (aggregate) + return 1; + ovpn_dev_dstats_rx_dropped(ovpn->dev); drop_noovpn: kfree_skb(skb); @@ -512,8 +772,11 @@ int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock, struct socket *sock, struct udp_tunnel_sock_cfg cfg = { .sk_user_data = ovpn_sock, .encap_type = UDP_ENCAP_OVPNINUDP, + .udp_flags = BIT(UDP_FLAGS_GRO_RCV_DETACHABLE) | + BIT(UDP_FLAGS_ENCAP_ACCEPT_FRAGLIST), .encap_rcv = ovpn_udp_encap_recv, .encap_destroy = ovpn_udp_encap_destroy, + .gro_receive = ovpn_udp_gro_receive_fraglist, }; struct ovpn_socket *old_data; int ret; @@ -558,16 +821,35 @@ int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock, struct socket *sock, */ void ovpn_udp_socket_detach(struct ovpn_socket *ovpn_sock) { + struct udp_sock *up = udp_sk(ovpn_sock->sk); struct sock *sk = ovpn_sock->sk; + /* Stop accepting aggregates while encap_rcv is still available. A + * reader which sampled acceptance before it was cleared can either be + * consumed by ovpn or fall back to UDP segmentation. + */ + if (udp_test_bit(GRO_RCV_DETACHABLE, sk) && + READ_ONCE(up->gro_receive) == ovpn_udp_gro_receive_fraglist) + udp_tunnel_detach_gro(sk); + + /* UDP_ENCAP may have replaced encap_rcv while leaving other ovpn state + * attached. Clear only callbacks still owned by ovpn. The final RCU + * grace period in ovpn_socket_release drains readers which captured + * encap_rcv before this point. + */ + if (READ_ONCE(up->encap_rcv) == ovpn_udp_encap_recv) + WRITE_ONCE(up->encap_rcv, NULL); + if (READ_ONCE(up->encap_destroy) == ovpn_udp_encap_destroy) + WRITE_ONCE(up->encap_destroy, NULL); + /* Re-enable multicast loopback */ inet_set_bit(MC_LOOP, sk); /* Disable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */ inet_dec_convert_csum(sk); - WRITE_ONCE(udp_sk(sk)->encap_type, 0); - WRITE_ONCE(udp_sk(sk)->encap_rcv, NULL); - WRITE_ONCE(udp_sk(sk)->encap_destroy, NULL); + /* UDP_ENCAP might have rewritten the encap_type */ + if (READ_ONCE(up->encap_type) == UDP_ENCAP_OVPNINUDP) + WRITE_ONCE(up->encap_type, 0); rcu_assign_sk_user_data(sk, NULL); }