net: introduce QUIC infrastructure and core subcomponents
Subsystems (7)
v7 cover net: introduce QUIC infrastructure and core subcomponents
Introduction
============
The QUIC protocol, defined in RFC 9000, is a secure, multiplexed transport built on top of UDP. It enables low-latency connection establishment, stream-based communication with flow control, and supports connection migration across network paths, while ensuring confidentiality, integrity, and availability.
This implementation introduces QUIC support in Linux Kernel, offering several key advantages:
- In-Kernel QUIC Support for Subsystems: Enables kernel subsystems such as SMB and NFS to operate over QUIC with minimal changes. Once the handshake is complete via the net/handshake APIs, data exchange proceeds over standard in-kernel transport interfaces.
- Standard Socket API Semantics: Implements core socket operations (listen(), accept(), connect(), sendmsg(), recvmsg(), close(), getsockopt(), setsockopt(), getsockname(), and getpeername()), allowing user space to interact with QUIC sockets in a familiar, POSIX-compliant way.
- ALPN-Based Connection Dispatching: Supports in-kernel ALPN (Application-Layer Protocol Negotiation) routing, allowing demultiplexing of QUIC connections across different user-space processes based on the ALPN identifiers.
- Performance Enhancements: Handles all control messages in-kernel to reduce syscall overhead, incorporates zero-copy mechanisms such as sendfile() minimize data movement, and is also structured to support future crypto hardware offloads.
This implementation offers fundamental support for the following RFCs:
- RFC9000 - QUIC: A UDP-Based Multiplexed and Secure Transport
- RFC9001 - Using TLS to Secure QUIC
- RFC9002 - QUIC Loss Detection and Congestion Control
- RFC9221 - An Unreliable Datagram Extension to QUIC
- RFC9287 - Greasing the QUIC Bit
- RFC9368 - Compatible Version Negotiation for QUIC
- RFC9369 - QUIC Version 2
The socket APIs for QUIC follow the RFC draft [1]:
- The Sockets API Extensions for In-kernel QUIC Implementations
Implementation
==============
The central design is to implement QUIC within the kernel while delegating the handshake to userspace.
Only the processing and creation of raw TLS Handshake Messages are handled in userspace, facilitated by a TLS library like GnuTLS. These messages are exchanged between kernel and userspace via sendmsg() and recvmsg(), with cryptographic details conveyed through control messages (cmsg).
The entire QUIC protocol, aside from the TLS Handshake Messages processing and creation, is managed within the kernel. Rather than using a Upper Layer Protocol (ULP) layer, this implementation establishes a socket of type IPPROTO_QUIC (similar to IPPROTO_MPTCP), operating over UDP tunnels.
For kernel consumers, they can initiate a handshake request from the kernel to userspace using the existing net/handshake netlink. The userspace component, such as tlshd service [2], then manages the processing of the QUIC handshake request.
- Handshake Architecture:
┌──────┐ ┌──────┐
│ APP1 │ │ APP2 │ ...
└──────┘ └──────┘
┌──────────────────────────────────────────┐
│ {quic_client/server_handshake()} │<─────────────┐
└──────────────────────────────────────────┘ ┌─────────────┐
{send/recvmsg()} {set/getsockopt()} │ tlshd │
[CMSG handshake_info] [SOCKOPT_CRYPTO_SECRET] └─────────────┘
[SOCKOPT_TRANSPORT_PARAM_EXT] │ ^
│ ^ │ ^ │ │
Userspace │ │ │ │ │ │
──────────────│─│──────────────────│─│──────────────────│───│───────
Kernel │ │ │ │ │ │
v │ v │ v │
┌──────────────────┬───────────────────────┐ ┌─────────────┐
│ protocol, timer, │ socket (IPPROTO_QUIC) │<──┐ │ handshake │
│ ├───────────────────────┤ │ │netlink APIs │
│ common, family, │ outqueue | inqueue │ │ └─────────────┘
│ ├───────────────────────┤ │ │ │
│ stream, connid, │ frame │ │ ┌─────┐ ┌─────┐
│ ├───────────────────────┤ │ │ │ │ │
│ path, pnspace, │ packet │ │───│ SMB │ │ NFS │...
│ ├───────────────────────┤ │ │ │ │ │
│ cong, crypto │ UDP tunnels │ │ └─────┘ └─────┘
└──────────────────┴───────────────────────┘ └──────┴───────┘- User Data Architecture:
┌──────┐ ┌──────┐
│ APP1 │ │ APP2 │ ...
└──────┘ └──────┘
{send/recvmsg()} {set/getsockopt()} {recvmsg()}
[CMSG stream_info] [SOCKOPT_KEY_UPDATE] [EVENT conn update]
[SOCKOPT_CONNECTION_MIGRATION] [EVENT stream update]
[SOCKOPT_STREAM_OPEN/RESET/STOP]
│ ^ │ ^ ^
Userspace │ │ │ │ │
──────────────│─│───────────────│─│─────────────────────│───────────
Kernel │ │ │ │ │
v │ v │ ┌──────────────────┘
┌──────────────────┬───────────────────────┐
│ protocol, timer, │ socket (IPPROTO_QUIC) │<──┐{kernel_send/recvmsg()}
│ ├───────────────────────┤ │{kernel_set/getsockopt()}
│ common, family, │ outqueue | inqueue │ │{kernel_recvmsg()}
│ ├───────────────────────┤ │
│ stream, connid, │ frame │ │ ┌─────┐ ┌─────┐
│ ├───────────────────────┤ │ │ │ │ │
│ path, pnspace, │ packet │ │───│ SMB │ │ NFS │...
│ ├───────────────────────┤ │ │ │ │ │
│ cong, crypto │ UDP tunnels │ │ └─────┘ └─────┘
└──────────────────┴───────────────────────┘ └──────┴───────┘Interface
=========
This implementation supports a mapping of QUIC into sockets APIs. Similar to TCP and SCTP, a typical Server and Client use the following system call sequence to communicate:
Client Server
────────────────────────────────────────────────────────────────────── sockfd = socket(IPPROTO_QUIC) listenfd = socket(IPPROTO_QUIC) bind(sockfd) bind(listenfd)
listen(listenfd)
connect(sockfd)
quic_client_handshake(sockfd)
sockfd = accept(listenfd) quic_server_handshake(sockfd, cert)
sendmsg(sockfd) recvmsg(sockfd)
close(sockfd) close(sockfd)
close(listenfd)
Please note that quic_client_handshake() and quic_server_handshake() functions are currently sourced from libquic [3]. These functions are responsible for receiving and processing the raw TLS handshake messages until the completion of the handshake process.
For utilization by kernel consumers, it is essential to have tlshd service [2] installed and running in userspace. This service receives and manages kernel handshake requests for kernel sockets. In the kernel, the APIs closely resemble those used in userspace:
Client Server
────────────────────────────────────────────────────────────────────────
__sock_create(IPPROTO_QUIC, &sock) __sock_create(IPPROTO_QUIC, &sock)
kernel_bind(sock) kernel_bind(sock)
kernel_listen(sock)
kernel_connect(sock)
tls_client_hello_x509(args:{sock})
kernel_accept(sock, &newsock)
tls_server_hello_x509(args:{newsock}) kernel_sendmsg(sock) kernel_recvmsg(newsock)
sock_release(sock) sock_release(newsock)
sock_release(sock)
Please be aware that tls_client_hello_x509() and tls_server_hello_x509() are APIs from net/handshake/. They are used to dispatch the handshake request to the userspace tlshd service and subsequently block until the handshake process is completed.
Use Cases
=========
- Samba
Stefan Metzmacher has integrated Linux QUIC into Samba for both client and server roles [4].
- tlshd
The tlshd daemon [2] facilitates Linux QUIC handshake requests from kernel sockets. This is essential for enabling protocols like SMB and NFS over QUIC.
- curl
Linux QUIC is being integrated into curl [5] for HTTP/3. Example usage:
# curl --http3-only https://nghttp2.org:4433/ # curl --http3-only https://www.google.com/ # curl --http3-only https://facebook.com/ # curl --http3-only https://outlook.office.com/ # curl --http3-only https://cloudflare-quic.com/
- httpd-portable
Moritz Buhl has deployed an HTTP/3 server over Linux QUIC [6] that is accessible via Firefox and curl:
- NetPerfMeter
The latest NetPerfMeter release supports Linux QUIC and can be used to run performance evaluations [10].
Test Coverage
=============
The Coverage (gcov) of Functional and Interop Tests:
- Functional Tests
The libquic self-tests (make check) pass on all major architectures:
x86_64, i386, s390x, aarch64, ppc64le.
- Interop tests
Interoperability was validated using the QUIC Interop Runner [7] against all major userland QUIC stacks. Results are available at:
- Fuzzing via Syzkaller
Syzkaller has been running kernel fuzzing with QUIC for weeks using tests/syzkaller/ in libquic [3]..
- Performance Testing
Performance was benchmarked using iperf [8] over a 100G NIC with using various MTUs and packet sizes:
- QUIC vs. kTLS:
UNIT size:1024 size:4096 size:16384 size:65536
Gbits/sec QUIC | kTLS QUIC | kTLS QUIC | kTLS QUIC | kTLS
────────────────────────────────────────────────────────────────────
mtu:1500 2.27 | 3.26 3.02 | 6.97 3.36 | 9.74 3.48 | 10.8
────────────────────────────────────────────────────────────────────
mtu:9000 3.66 | 3.72 5.87 | 8.92 7.03 | 11.2 8.04 | 11.4 - QUIC(disable_1rtt_encryption) vs. TCP:
UNIT size:1024 size:4096 size:16384 size:65536
Gbits/sec QUIC | TCP QUIC | TCP QUIC | TCP QUIC | TCP
────────────────────────────────────────────────────────────────────
mtu:1500 3.09 | 4.59 4.46 | 14.2 5.07 | 21.3 5.18 | 23.9
────────────────────────────────────────────────────────────────────
mtu:9000 4.60 | 4.65 8.41 | 14.0 11.3 | 28.9 13.5 | 39.2The performance gap between QUIC and kTLS may be attributed to:
- The absence of Generic Segmentation Offload (GSO) for QUIC.
- An additional data copy on the transmission (TX) path.
- Extra encryption required for header protection in QUIC.
- A longer header length for the stream data in QUIC.
Patches
=======
Note: This implementation is organized into five parts and submitted across two patchsets for review. This patchset includes Parts 1–2, while Parts 3–5 will be submitted in a subsequent patchset. For complete series, see [9].
1. Infrastructure (2):
net: define IPPROTO_QUIC and SOL_QUIC constants
net: build socket infrastructure for QUIC protocol
2. Subcomponents (14):
quic: provide common utilities and data structures
quic: provide family ops for address and protocol
quic: provide quic.h header files for kernel and userspace
quic: add stream management
quic: add connection id management
quic: add path management
quic: add congestion control
quic: add packet number space
quic: add crypto key derivation and installation
quic: add crypto packet encryption and decryption
quic: add timer management
quic: add frame encoder and decoder base
quic: add packet builder base
quic: add packet parser base
3. Data Processing (7):
quic: implement outqueue transmission and flow control
quic: implement outqueue sack and retransmission
quic: implement inqueue receiving and flow control
quic: implement frame creation functions
quic: implement frame processing functions
quic: implement packet creation functions
quic: implement packet processing functions
4. Socket APIs (6):
quic: support bind/listen/connect/accept/close()
quic: support sendmsg() and recvmsg()
quic: support socket options related to interaction after handshake
quic: support socket options related to settings prior to handshake
quic: support socket options related to setup during handshake
quic: support socket ioctls and socket dump via procfs
5. Documentation and Selftests (3):
quic: create sample test using handshake APIs for kernel consumers
Documentation: describe QUIC protocol interface in quic.rst
selftests: net: add tests for QUIC protocol
Notice: The QUIC module is currently labeled as "EXPERIMENTAL".
All contributors are recognized in the respective patches with the tag of 'Signed-off-by:'. Special thanks to Moritz Buhl and Stefan Metzmacher whose practical use cases and insightful feedback, which have been instrumental in shaping the design and advancing the development.
References
==========
[1] https://datatracker.ietf.org/doc/html/draft-lxin-quic-socket-apis [2] https://github.com/oracle/ktls-utils [3] https://github.com/lxin/quic [4] https://gitlab.com/samba-team/samba/-/merge_requests/4019 [5] https://github.com/moritzbuhl/curl/tree/linux_curl [6] https://github.com/moritzbuhl/httpd-portable [7] https://github.com/quic-interop/quic-interop-runner [8] https://github.com/lxin/iperf [9] https://github.com/lxin/net-next/commits/quic/
[10] https://www.nntb.no/~dreibh/netperfmeter/
Changes in v2-v7: See individual patch changelogs for details.
Xin Long (16):
net: define IPPROTO_QUIC and SOL_QUIC constants
net: build socket infrastructure for QUIC protocol
quic: provide common utilities and data structures
quic: provide family ops for address and protocol
quic: provide quic.h header files for kernel and userspace
quic: add stream management
quic: add connection id management
quic: add path management
quic: add congestion control
quic: add packet number space
quic: add crypto key derivation and installation
quic: add crypto packet encryption and decryption
quic: add timer management
quic: add frame encoder and decoder base
quic: add packet builder base
quic: add packet parser base
Documentation/networking/ip-sysctl.rst | 52 + MAINTAINERS | 9 + include/linux/quic.h | 20 + include/linux/socket.h | 1 + include/uapi/linux/in.h | 2 + include/uapi/linux/quic.h | 235 +++++ net/Kconfig | 1 + net/Makefile | 1 + net/quic/Kconfig | 36 + net/quic/Makefile | 9 + net/quic/common.c | 583 +++++++++++ net/quic/common.h | 204 ++++ net/quic/cong.c | 307 ++++++ net/quic/cong.h | 120 +++ net/quic/connid.c | 225 +++++ net/quic/connid.h | 163 ++++ net/quic/crypto.c | 1222 ++++++++++++++++++++++++ net/quic/crypto.h | 83 ++ net/quic/family.c | 372 ++++++++ net/quic/family.h | 33 + net/quic/frame.c | 561 +++++++++++ net/quic/frame.h | 195 ++++ net/quic/packet.c | 957 +++++++++++++++++++ net/quic/packet.h | 130 +++ net/quic/path.c | 524 ++++++++++ net/quic/path.h | 172 ++++ net/quic/pnspace.c | 225 +++++ net/quic/pnspace.h | 150 +++ net/quic/protocol.c | 421 ++++++++ net/quic/protocol.h | 62 ++ net/quic/socket.c | 436 +++++++++ net/quic/socket.h | 214 +++++ net/quic/stream.c | 400 ++++++++ net/quic/stream.h | 119 +++ net/quic/timer.c | 196 ++++ net/quic/timer.h | 47 + 36 files changed, 8487 insertions(+) create mode 100644 include/linux/quic.h create mode 100644 include/uapi/linux/quic.h create mode 100644 net/quic/Kconfig create mode 100644 net/quic/Makefile create mode 100644 net/quic/common.c create mode 100644 net/quic/common.h create mode 100644 net/quic/cong.c create mode 100644 net/quic/cong.h create mode 100644 net/quic/connid.c create mode 100644 net/quic/connid.h create mode 100644 net/quic/crypto.c create mode 100644 net/quic/crypto.h create mode 100644 net/quic/family.c create mode 100644 net/quic/family.h create mode 100644 net/quic/frame.c create mode 100644 net/quic/frame.h create mode 100644 net/quic/packet.c create mode 100644 net/quic/packet.h create mode 100644 net/quic/path.c create mode 100644 net/quic/path.h create mode 100644 net/quic/pnspace.c create mode 100644 net/quic/pnspace.h create mode 100644 net/quic/protocol.c create mode 100644 net/quic/protocol.h create mode 100644 net/quic/socket.c create mode 100644 net/quic/socket.h create mode 100644 net/quic/stream.c create mode 100644 net/quic/stream.h create mode 100644 net/quic/timer.c create mode 100644 net/quic/timer.h
Signature · 1 line
2.47.1
PATCH v7 1/16 net: define IPPROTO_QUIC and SOL_QUIC constants
This patch adds IPPROTO_QUIC and SOL_QUIC constants to the networking subsystem. These definitions are essential for applications to set socket options and protocol identifiers related to the QUIC protocol.
QUIC does not possess a protocol number allocated from IANA, and like IPPROTO_MPTCP, IPPROTO_QUIC is merely a value used when opening a QUIC socket with:
socket(AF_INET, SOCK_STREAM, IPPROTO_QUIC);
Note we did not opt for UDP ULP for QUIC implementation due to several considerations:
- QUIC's connection Migration requires at least 2 UDP sockets for one QUIC connection at the same time, not to mention the multipath feature in one of its draft RFCs.
- In-Kernel QUIC, as a Transport Protocol, wants to provide users with the TCP or SCTP like Socket APIs, like connect()/listen()/accept()... Note that a single UDP socket might even be used for multiple QUIC connections.
The use of IPPROTO_QUIC type sockets over UDP tunnel will effectively address these challenges and provides a more flexible and scalable solution.
--- include/linux/socket.h | 1 + include/uapi/linux/in.h | 2 ++ 2 files changed, 3 insertions(+)
include/linux/socket.h +1 -0
@@ -401,6 +401,7 @@ struct ucred {#define SOL_MCTP 285#define SOL_SMC 286#define SOL_VSOCK 287 +#define SOL_QUIC 288/* IPX options */ #define IPX_TYPE 1
include/uapi/linux/in.h +2 -0
@@ -85,6 +85,8 @@ enum {#define IPPROTO_RAW IPPROTO_RAW IPPROTO_SMC = 256, /* Shared Memory Communications */#define IPPROTO_SMC IPPROTO_SMC + IPPROTO_QUIC = 261, /* A UDP-Based Multiplexed and Secure Transport */ +#define IPPROTO_QUIC IPPROTO_QUIC IPPROTO_MPTCP = 262, /* Multipath TCP connection */#define IPPROTO_MPTCP IPPROTO_MPTCPIPPROTO_MAX
Signature · 1 line
2.47.1
PATCH v7 2/16 net: build socket infrastructure for QUIC protocol
In reply to Xin LongThis patch lays the groundwork for QUIC socket support in the kernel. It defines the core structures and protocol hooks needed to create QUIC sockets, without implementing any protocol behavior at this stage.
Basic integration is included to allow building the module via CONFIG_IP_QUIC=m.
This provides the scaffolding necessary for adding actual QUIC socket behavior in follow-up patches.
---
v3:
- Kconfig: add 'default n' for IP_QUIC (reported by Paolo).
- quic_disconnect(): return -EOPNOTSUPP (suggested by Paolo).
- quic_init/destroy_sock(): drop local_bh_disable/enable() calls (noted by Paolo).
- sysctl: add alpn_demux option to en/disable ALPN-based demux.
- SNMP: remove SNMP_MIB_SENTINEL, switch to snmp_get_cpu_field_batch_cnt() to align with latest net-next changes.
v4:
- Remove unnecessary READ_ONCE() in quic_inet_connect() (reported by
Paolo).
v5:
- Update the type of the parameter 'addr' in quic_inet_connect(), quic_connect(), and quic_bind() to match the latest net-next changes.
- Define quic_is_serv() to reuse sk->sk_max_ack_backlog for server-side
detection; path->serv will be deleted in a later patch.
- Use MODULE_ALIAS_NET_PF_PROTO instead of MODULE_ALIAS (suggested by
Stefan).
- Add the missing Documentation entry for the new sysctl options (noted
by Paolo).
- Add the missing MAINTAINERS entry for the QUIC PROTOCOL (noted by
Jakub).v6:
- Relocate the QUIC PROTOCOL MAINTAINERS entry to its proper section (noted by Jakub).
v7:
- Replace #ifdef CONFIG_XXX with #if IS_ENABLED(CONFIG_XXX) (noted by
Paolo). --- Documentation/networking/ip-sysctl.rst | 52 ++++ MAINTAINERS | 7 + net/Kconfig | 1 + net/Makefile | 1 + net/quic/Kconfig | 36 +++ net/quic/Makefile | 8 + net/quic/protocol.c | 377 +++++++++++++++++++++++++ net/quic/protocol.h | 56 ++++ net/quic/socket.c | 207 ++++++++++++++ net/quic/socket.h | 89 ++++++ 10 files changed, 834 insertions(+) create mode 100644 net/quic/Kconfig create mode 100644 net/quic/Makefile create mode 100644 net/quic/protocol.c create mode 100644 net/quic/protocol.h create mode 100644 net/quic/socket.c create mode 100644 net/quic/socket.h
Documentation/networking/ip-sysctl.rst +52 -0
@@ -3768,6 +3768,58 @@ l3mdev_accept - BOOLEAN Default: 1 (enabled) +``/proc/sys/net/quic/*`` Variables +=================================== + +quic_mem - vector of 3 LONGs: min, pressure, max + Number of pages allowed for queueing by all QUIC sockets. + + min: below this number of pages QUIC is not bothered about its + memory appetite. + + pressure: when amount of memory allocated by QUIC exceeds this number + of pages, QUIC moderates its memory consumption and enters memory + pressure mode, which is exited when memory consumption falls + under "min". + + max: number of pages allowed for queueing by all QUIC sockets. + + Defaults are calculated at boot time from amount of available + memory. + +quic_rmem - vector of 3 INTEGERs: min, default, max + Only the first value ("min") is used, "default" and "max" are + ignored. + + min: Minimal size of receive buffer used by QUIC sockets. + It is guaranteed to each QUIC socket, even under moderate memory + pressure. + + Default: 4K + +quic_wmem - vector of 3 INTEGERs: min, default, max + Only the first value ("min") is used, "default" and "max" are + ignored. + + min: Amount of memory reserved for send buffers for QUIC sockets. + Each QUIC socket has rights to use it due to fact of its birth. + + Default: 4K + +alpn_demux - BOOLEAN + Enable or disable ALPN-based demultiplexing for incoming QUIC + connections. When enabled, the kernel can route incoming QUIC + connections to different sockets based on the Application-Layer + Protocol Negotiation (ALPN) value in the initial handshake. + + Possible values: + + - 0 (disabled) + - 1 (enabled) + + Default: 0 (disabled) + + ``/proc/sys/net/core/*`` ========================
MAINTAINERS +7 -0
@@ -21656,6 +21656,13 @@ L: [email protected] S: Maintained F: drivers/net/wireless/quantenna/ +QUIC PROTOCOL +M: Xin Long <[email protected]> +L: [email protected] +S: Maintained +W: https://github.com/lxin/quic +F: net/quic/ + RADEON and AMDGPU DRM DRIVERS M: Alex Deucher <[email protected]> M: Christian König <[email protected]>
net/Kconfig +1 -0
@@ -251,6 +251,7 @@ source "net/bridge/netfilter/Kconfig" endif # if NETFILTER +source "net/quic/Kconfig" source "net/sctp/Kconfig" source "net/rds/Kconfig" source "net/tipc/Kconfig"
net/Makefile +1 -0
@@ -43,6 +43,7 @@ obj-$(CONFIG_PHONET) += phonet/ ifneq ($(CONFIG_VLAN_8021Q),) obj-y += 8021q/ endif +obj-$(CONFIG_IP_QUIC) += quic/ obj-$(CONFIG_IP_SCTP) += sctp/ obj-$(CONFIG_RDS) += rds/ obj-$(CONFIG_WIRELESS) += wireless/
net/quic/Kconfig new file +36 -0
@@ -0,0 +1,36 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# QUIC configuration +# + +menuconfig IP_QUIC + tristate "QUIC: A UDP-Based Multiplexed and Secure Transport (Experimental)" + depends on INET + depends on IPV6 + select CRYPTO + select CRYPTO_HMAC + select CRYPTO_HKDF + select CRYPTO_AES + select CRYPTO_GCM + select CRYPTO_CCM + select CRYPTO_CHACHA20POLY1305 + select NET_UDP_TUNNEL + default n + help + QUIC: A UDP-Based Multiplexed and Secure Transport + + From rfc9000 <https://www.rfc-editor.org/rfc/rfc9000.html>. + + QUIC provides applications with flow-controlled streams for structured + communication, low-latency connection establishment, and network path + migration. QUIC includes security measures that ensure + confidentiality, integrity, and availability in a range of deployment + circumstances. Accompanying documents describe the integration of + TLS for key negotiation, loss detection, and an exemplary congestion + control algorithm. + + To compile this protocol support as a module, choose M here: the + module will be called quic. Debug messages are handled by the + kernel's dynamic debugging framework. + + If in doubt, say N.
net/quic/Makefile new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Makefile for QUIC support code. +# + +obj-$(CONFIG_IP_QUIC) += quic.o + +quic-y := protocol.o socket.o
net/quic/protocol.c new file +377 -0
377 lines not on this page · raw message
net/quic/protocol.h new file +56 -0
56 lines not on this page · raw message
net/quic/socket.c new file +207 -0
207 lines not on this page · raw message
net/quic/socket.h new file +89 -0
89 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v7 3/16 quic: provide common utilities and data structures
In reply to Xin LongThis patch provides foundational data structures and utilities used throughout the QUIC stack.
It introduces packet header types, connection ID support, and address handling. Hash tables are added to manage socket lookup and connection ID mapping.
A flexible binary data type is provided, along with helpers for parsing, matching, and memory management. Helpers for encoding and decoding transport parameters and frames are also included.
---
v3:
- Rework hashtables: split into two types and size them based on totalram_pages(), similar to SCTP (reported by Paolo).
- struct quic_shash_table: use rwlock instead of spinlock.
- quic_data_from/to_string(): add safety and common-case checks (noted by Paolo).
v4:
- Handle the error returned by quic_hash_tables_init() properly (reported by Simon).
- Use vmalloc() to simplify hashtable allocations (suggested by Paolo).
- Replace rwlock_t with spinlock_t and use hlist_nulls_head in quic_shash_head for lockless lookup/access (suggested by Paolo).
- Define QUIC_PN_BITS to replace a magical number in quic_get_num() (reported by Paolo)
- Rename several hash-related functions:
* quic_(listen_)sock_hash() → quic_(listen_)sock_head()
* quic_(listen_)sock_head() → quic_(listen_)sock_hash()
* quic_shash() → quic_addr_hash()
* quic_ahash() → call its code directly in quic_sock_hash().
- Include net in the hash calculations in quic_listen_sock_hash() and quic_udp_sock_head(), and include len in quic_source_conn_id_head().
v5:
- Use u64 for skb_cb->time and u32 for skb_cb->seqno, and introduce quic_ktime_get_us() to obtain timestamps in microseconds.
- Remove skb_cb->number_max and reuse number as the largest previously
seen, and add skb_cb->crypto_ctx for async crypto context freeing.
- Add case 8 to quic_put_int(), which will be used to pack a u64 timestamp into the token in a later patch.
v6:
- Since transport_header is no longer set for QUIC, use skb->data instead in quic_hdr() and quic_hshdr(). As the UDP header can now be accessed via udp_hdr(), drop udph_offset from struct quic_skb_cb.
- Note for AI reviews: although ticket/token/alpn are not initialized in quic_init_sock(), it is safe to kfree() them in quic_destroy_sock() because they are always set to NULL (via sk_alloc() in inet/6_create() or memset(0) in quic_accept() in a later patchset).
v7:
- Add the missing check len < length in quic_data_has/match() (reported by AI review).
--- net/quic/Makefile | 2 +- net/quic/common.c | 583 ++++++++++++++++++++++++++++++++++++++++++++ net/quic/common.h | 204 ++++++++++++++++ net/quic/protocol.c | 7 + net/quic/socket.c | 4 + net/quic/socket.h | 21 ++ 6 files changed, 820 insertions(+), 1 deletion(-) create mode 100644 net/quic/common.c create mode 100644 net/quic/common.h
net/quic/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o -quic-y := protocol.o socket.o +quic-y := common.o protocol.o socket.o
net/quic/common.c new file +583 -0
583 lines not on this page · raw message
net/quic/common.h new file +204 -0
204 lines not on this page · raw message
net/quic/protocol.c +7 -0
@@ -334,6 +334,10 @@ static __init int quic_init(void)if (err) goto err_percpu_counter; + err =quic_hash_tables_init (); +if (err) + goto err_hash; + err =register_pernet_subsys (&quic_net_ops);if (err) goto err_def_ops;
@@ -351,6 +355,8 @@ static __init int quic_init(void) err_protosw:unregister_pernet_subsys (&quic_net_ops); err_def_ops: +quic_hash_tables_destroy (); +err_hash:percpu_counter_destroy (&quic_sockets_allocated); err_percpu_counter:return err;
@@ -363,6 +369,7 @@ static __exit void quic_exit(void)#endif quic_protosw_exit ();unregister_pernet_subsys (&quic_net_ops); +quic_hash_tables_destroy ();percpu_counter_destroy (&quic_sockets_allocated);pr_info ("quic: exit\n" ); }
net/quic/socket.c +4 -0
@@ -52,6 +52,10 @@ static int quic_init_sock(struct sock *sk)static void quic_destroy_sock (struct sock *sk) { +quic_data_free (quic_ticket (sk)); +quic_data_free (quic_token (sk)); +quic_data_free (quic_alpn (sk)); +sk_sockets_allocated_dec (sk);sock_prot_inuse_add (sock_net (sk), sk->sk_prot,-1 ); }
net/quic/socket.h +21 -0
@@ -10,6 +10,8 @@#include <net/udp_tunnel.h> +#include "common.h" +#include "protocol.h" extern struct proto quic_prot;
@@ -25,6 +27,10 @@ enum quic_state {struct quic_sock {struct inet_sock inet;struct list_head reqs; + +struct quic_data ticket; +struct quic_data token; +struct quic_data alpn; };struct quic6_sock {
@@ -42,6 +48,21 @@ static inline struct list_head *quic_reqs(const struct sock *sk)return &quic_sk (sk)->reqs; } +static inline struct quic_data *quic_token (const struct sock *sk) +{ +return &quic_sk (sk)->token; +} + +static inline struct quic_data *quic_ticket (const struct sock *sk) +{ +return &quic_sk (sk)->ticket; +} + +static inline struct quic_data *quic_alpn (const struct sock *sk) +{ +return &quic_sk (sk)->alpn; +} +static inline bool quic_is_serv (const struct sock *sk) {return !!sk->sk_max_ack_backlog;
Signature · 1 line
2.47.1
PATCH v7 4/16 quic: provide family ops for address and protocol
In reply to Xin LongIntroduce QUIC address and protocol family operations to handle IPv4/IPv6 specifics consistently, similar to SCTP. The new quic_family.{c,h} provide helpers for routing, skb transmit handling, address parsing and comparison and UDP socket config initializing etc.
This consolidates protocol-family logic and enables cleaner dual-stack support in the QUIC socket implementation.
---
v2:
- Add more checks for addrs in .get_user_addr() and .get_pref_addr().
- Consider sk_bound_dev_if in .udp_conf_init() and .flow_route() to support vrf.
v3:
- Remove quic_addr_family/proto_ops abstraction; use if statements to reduce indirect call overhead (suggested by Paolo).
- quic_v6_set_sk_addr(): add quic_v6_copy_sk_addr() helper to avoid duplicate code (noted by Paolo).
- quic_v4_flow_route(): use flowi4_dscp per latest net-next changes.
v4:
- Remove unnecessary _fl variable from flow_route() functions (noted by Paolo).
- Fix coding style of ?: operator (noted by Paolo).
v5:
- Remove several unused functions from this patch series (suggested by Paolo):
* quic_seq_dump_addr()
* quic_get_msg_ecn()
* quic_get_user_addr()
* quic_get_pref_addr()
* quic_set_pref_addr()
* quic_set_sk_addr()
* quic_set_sk_ecn()
- Replace the sa->v4/v6.sin_family checks with quic_v4/v6_is_any_addr() in quic_v4/v6_flow_route() (suggested by Paolo).
- Introduce quic_v4_match_v6_addr() to simplify family-mismatch checks between sk and addr in quic_v6_cmp_sk_addr() (notied by Paolo).
v6:
- Use udp_hdr(skb) to access UDP header in quic_v4/6_get_msg_addrs(), as transport_header is no longer reset for QUIC.
--- net/quic/Makefile | 2 +- net/quic/family.c | 372 ++++++++++++++++++++++++++++++++++++++++++++ net/quic/family.h | 33 ++++ net/quic/protocol.c | 2 +- net/quic/socket.c | 4 +- net/quic/socket.h | 1 + 6 files changed, 410 insertions(+), 4 deletions(-) create mode 100644 net/quic/family.c create mode 100644 net/quic/family.h
net/quic/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o -quic-y := common.o protocol.o socket.o +quic-y := common.o family.o protocol.o socket.o
net/quic/family.c new file +372 -0
372 lines not on this page · raw message
net/quic/family.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* QUIC kernel implementation +* (C) Copyright Red Hat Corp. 2023 +* +* This file is part of the QUIC kernel implementation +* +* Written or modified by: +* Xin Long <[email protected]> +*/ + +#define QUIC_PORT_LEN 2 +#define QUIC_ADDR4_LEN 4 +#define QUIC_ADDR6_LEN 16 + +#define QUIC_PREF_ADDR_LEN (QUIC_ADDR4_LEN + QUIC_PORT_LEN + QUIC_ADDR6_LEN + QUIC_PORT_LEN) + +int quic_is_any_addr (union quic_addr *a); +u32 quic_encap_len (union quic_addr *a); + +void quic_lower_xmit (struct sock *sk,struct sk_buff *skb,union quic_addr *da,struct flowi *fl); +int quic_flow_route (struct sock *sk,union quic_addr *da,union quic_addr *sa,struct flowi *fl); +void quic_udp_conf_init (struct sock *sk,struct udp_port_cfg *conf,union quic_addr *a); + +void quic_get_msg_addrs (struct sk_buff *skb,union quic_addr *da,union quic_addr *sa); +int quic_get_mtu_info (struct sk_buff *skb,u32 *info); + +bool quic_cmp_sk_addr (struct sock *sk,union quic_addr *a,union quic_addr *addr); +int quic_get_sk_addr (struct socket *sock,struct sockaddr *a,bool peer); + +int quic_common_setsockopt (struct sock *sk,int level,int optname,sockptr_t optval, +unsigned int optlen); +int quic_common_getsockopt (struct sock *sk,int level,int optname,char __user *optval, +int __user *optlen);
net/quic/protocol.c +1 -1
@@ -45,7 +45,7 @@ static int quic_inet_listen(struct socket *sock, int backlog)static int quic_inet_getname (struct socket *sock,struct sockaddr *uaddr,int peer) { -return -EOPNOTSUPP ; +return quic_get_sk_addr (sock, uaddr, peer); }static __poll_t quic_inet_poll (struct file *file,struct socket *sock,poll_table *wait)
net/quic/socket.c +2 -2
@@ -116,7 +116,7 @@ static int quic_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, unsigned int optlen) {if (level !=SOL_QUIC ) -return -EOPNOTSUPP ; +return quic_common_setsockopt (sk, level, optname, optval, optlen);return quic_do_setsockopt (sk, optname, optval, optlen); }
@@ -130,7 +130,7 @@ static int quic_getsockopt(struct sock *sk, int level, int optname, char __user *optval, int __user *optlen) {if (level !=SOL_QUIC ) -return -EOPNOTSUPP ; +return quic_common_getsockopt (sk, level, optname, optval, optlen);return quic_do_getsockopt (sk, optname,USER_SOCKPTR (optval),USER_SOCKPTR (optlen)); }
net/quic/socket.h +1 -0
@@ -11,6 +11,7 @@#include <net/udp_tunnel.h> #include "common.h" +#include "family.h" #include "protocol.h"
Signature · 1 line
2.47.1
PATCH v7 5/16 quic: provide quic.h header files for kernel and userspace
In reply to Xin LongThis commit adds quic.h to include/uapi/linux, providing the necessary definitions for the QUIC socket API. Exporting this header allows both user space applications and kernel subsystems to access QUIC-related control messages, socket options, and event/notification interfaces.
Since kernel_get/setsockopt() is no longer available to kernel consumers, a corresponding internal header, include/linux/quic.h, is added. This exposes quic_do_get/setsockopt() to handle QUIC socket options directly for kernel subsystems.
Detailed descriptions of these structures are available in [1], and will be also provided when adding corresponding socket interfaces in the later patches.
[1] https://datatracker.ietf.org/doc/html/draft-lxin-quic-socket-apis
---
v2:
- Fix a kernel API description warning, found by Jakub.
- Replace uintN_t with __uN, capitalize _UAPI_LINUX_QUIC_H, and assign explicit values for QUIC_TRANSPORT_ERROR_ enum in UAPI quic.h, suggested by David Howells.
v4:
- Use MSG_QUIC_ prefix for MSG_* flags to avoid conflicts with other protocols, such as MSG_NOTIFICATION in SCTP (reported by Thomas).
- Remove QUIC_CONG_ALG_CUBIC; only NEW RENO congestion control is supported in this version.
v5:
- Add include/linux/quic.h and include/uapi/linux/quic.h to the QUIC PROTOCOL entry in MAINTAINERS.
v6:
- Fix the copy/pasted the uAPI path for SCTP to the QUIC entry (noted by Jakub).
v7:
- Expose quic_do_get/setsockopt() instead of quic_kernel_get/setsockopt() (suggested by Paolo).
--- MAINTAINERS | 2 + include/linux/quic.h | 20 ++++ include/uapi/linux/quic.h | 235 ++++++++++++++++++++++++++++++++++++++ net/quic/socket.c | 32 +++++- net/quic/socket.h | 7 ++ 5 files changed, 294 insertions(+), 2 deletions(-) create mode 100644 include/linux/quic.h create mode 100644 include/uapi/linux/quic.h
MAINTAINERS +2 -0
@@ -21661,6 +21661,8 @@ M: Xin Long <[email protected]> L: [email protected] S: Maintained W: https://github.com/lxin/quic +F: include/linux/quic.h +F: include/uapi/linux/quic.h F: net/quic/ RADEON and AMDGPU DRM DRIVERS
include/linux/quic.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* QUIC kernel implementation +* (C) Copyright Red Hat Corp. 2023 +* +* This file is part of the QUIC kernel implementation +* +* Written or modified by: +* Xin Long <[email protected]> +*/ + +#ifndef _LINUX_QUIC_H +#define _LINUX_QUIC_H + +#include <linux/sockptr.h> +#include <uapi/linux/quic.h> + +int quic_do_setsockopt (struct sock *sk,int optname,sockptr_t optval,unsigned int optlen); +int quic_do_getsockopt (struct sock *sk,int optname,sockptr_t optval,sockptr_t optlen); + +#endif
include/uapi/linux/quic.h new file +235 -0
235 lines not on this page · raw message
net/quic/socket.c +30 -2
@@ -107,10 +107,24 @@ static void quic_close(struct sock *sk, long timeout)sk_common_release (sk); } -static int quic_do_setsockopt (struct sock *sk,int optname,sockptr_t optval,unsigned int optlen) +/** +* quic_do_setsockopt - set a QUIC socket option +* @sk: socket to configure +* @optname: option name (QUIC-level) +* @optval: user buffer containing the option value +* @optlen: size of the option value +* +* Sets a QUIC socket option on a given socket. +* +* Return: +* - On success, 0 is returned. +* - On error, a negative error value is returned. +*/ +int quic_do_setsockopt (struct sock *sk,int optname,sockptr_t optval,unsigned int optlen) {return -EOPNOTSUPP ; } +EXPORT_SYMBOL_GPL (quic_do_setsockopt);static int quic_setsockopt (struct sock *sk,int level,int optname,sockptr_t optval,unsigned int optlen)
@@ -121,10 +135,24 @@ static int quic_setsockopt(struct sock *sk, int level, int optname,return quic_do_setsockopt (sk, optname, optval, optlen); } -static int quic_do_getsockopt (struct sock *sk,int optname,sockptr_t optval,sockptr_t optlen) +/** +* quic_do_getsockopt - get a QUIC socket option +* @sk: socket to query +* @optname: option name (QUIC-level) +* @optval: user buffer to receive the option value +* @optlen: in/out parameter for buffer size; updated with actual length on return +* +* Gets a QUIC socket option from a given socket. +* +* Return: +* - On success, 0 is returned. +* - On error, a negative error value is returned. +*/ +int quic_do_getsockopt (struct sock *sk,int optname,sockptr_t optval,sockptr_t optlen) {return -EOPNOTSUPP ; } +EXPORT_SYMBOL_GPL (quic_do_getsockopt);static int quic_getsockopt (struct sock *sk,int level,int optname,char __user *optval,int __user *optlen)
net/quic/socket.h +7 -0
@@ -9,6 +9,7 @@*/ #include <net/udp_tunnel.h> +#include <linux/quic.h> #include "common.h" #include "family.h"
@@ -29,6 +30,7 @@ struct quic_sock {struct inet_sock inet;struct list_head reqs; +struct quic_config config;struct quic_data ticket;struct quic_data token;struct quic_data alpn;
@@ -49,6 +51,11 @@ static inline struct list_head *quic_reqs(const struct sock *sk)return &quic_sk (sk)->reqs; } +static inline struct quic_config *quic_config (const struct sock *sk) +{ +return &quic_sk (sk)->config; +} +static inline struct quic_data *quic_token (const struct sock *sk) {return &quic_sk (sk)->token;
Signature · 1 line
2.47.1
Re: [PATCH net-next v7 05/16] quic: provide quic.h header files for kernel and userspace
Quoted 14 lines · Xin Long
This commit adds
quic.htoinclude/uapi/linux, providing the necessary definitions for the QUIC socket API. Exporting this header allows both user space applications and kernel subsystems to access QUIC-related control messages, socket options, and event/notification interfaces.Since kernel_get/setsockopt() is no longer available to kernel consumers, a corresponding internal header,
include/linux/quic.h, is added. This exposes quic_do_get/setsockopt() to handle QUIC socket options directly for kernel subsystems.Detailed descriptions of these structures are available in [1], and will be also provided when adding corresponding socket interfaces in the later patches.
Quoted 6 lines
[1] https://datatracker.ietf.org/doc/html/draft-lxin-quic-socket-apis
Signed-off-by Tyler Fanelli <[email protected]>Signed-off-by Stefan Metzmacher <[email protected]>Signed-off-by Thomas Dreibholz <[email protected]>Signed-off-by Xin Long <[email protected]>
PATCH v7 6/16 quic: add stream management
In reply to Xin LongThis patch introduces 'struct quic_stream_table' for managing QUIC streams, each represented by 'struct quic_stream'.
It implements mechanisms for acquiring and releasing streams on both the send and receive paths, ensuring efficient lifecycle management during transmission and reception.
- quic_stream_get(): Acquire a send-side stream by ID and flags during TX path, or a receive-side stream by ID during RX path.
- quic_stream_put(): Release a send-side stream when sending is done, or a receive-side stream when receiving is done.
It includes logic to detect when stream ID limits are reached and when control frames should be sent to update or request limits from the peer.
- quic_stream_id_exceeds(): Check a stream ID would exceed local (recv) or peer (send) limits.
- quic_stream_max_streams_update(): Determines whether a MAX_STREAMS_UNI/BIDI frame should be sent to the peer.
Note stream hash table is per socket, the operations on it are always protected by the sock lock.
---
v3:
- Merge send/recv stream helpers into unified functions to reduce code:
* quic_stream_id_send/recv() → quic_stream_id_valid()
* quic_stream_id_send/recv_closed() → quic_stream_id_closed()
* quic_stream_id_send/recv_exceeds() → quic_stream_id_exceeds() (pointed out by Paolo).
- Clarify in changelog that stream hash table is always protected by sock lock (suggested by Paolo).
- quic_stream_init/free(): adjust for new hashtable type; call quic_stream_delete() in quic_stream_free() to avoid open-coded logic.
- Receiving streams: delete stream only when fully read or reset, instead of when no data was received. Prevents freeing a stream while a FIN with no data is still queued.
v4:
- Replace struct quic_shash_table with struct hlist_head for the stream hashtable. Since they are protected by the socket lock, no per-chain lock is needed.
- Initialize stream to NULL in stream creation functions to avoid warnings from Smatch (reported by Simon).
- Allocate send streams with GFP_KERNEL_ACCOUNT and receive streams with GFP_ATOMIC | __GFP_ACCOUNT for memory accounting (suggested by Paolo).
v5:
- Introduce struct quic_stream_limits to merge quic_stream_send_create() and quic_stream_recv_create(), and to simplify quic_stream_get_param() (suggested by Paolo).
- Annotate the sock-lock requirement for quic_stream_send/recv_get() and quic_stream_send/recv_put() (notied by Paolo).
- Add quic_stream_bidi_put() to deduplicate the common logic between quic_stream_send_put() and quic_stream_recv_put().
- Remove the unnecessary check when incrementing
streams->send.next_bidi/uni_stream_id in quic_stream_create().
- Remove the unused 'is_serv' parameter from quic_stream_get_param().
v7:
- Free the allocated streams on error path in quic_stream_create() (noted by Paolo).
- Merge quic_stream_send_get/put() and quic_stream_recv_get/put() helpers to quic_stream_get/put() (suggested by Paolo).
- Add more comments in quic_stream_id_exceeds() and quic_stream_create().
--- net/quic/Makefile | 2 +- net/quic/socket.c | 5 + net/quic/socket.h | 8 + net/quic/stream.c | 400 ++++++++++++++++++++++++++++++++++++++++++++++ net/quic/stream.h | 119 ++++++++++++++ 5 files changed, 533 insertions(+), 1 deletion(-) create mode 100644 net/quic/stream.c create mode 100644 net/quic/stream.h
net/quic/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o -quic-y := common.o family.o protocol.o socket.o +quic-y := common.o family.o protocol.o socket.o stream.o
net/quic/socket.c +5 -0
@@ -41,6 +41,9 @@ static int quic_init_sock(struct sock *sk) sk->sk_write_space = quic_write_space;sock_set_flag (sk,SOCK_USE_WRITE_QUEUE ); +if (quic_stream_init (quic_streams (sk))) +return -ENOMEM ; +WRITE_ONCE (sk->sk_sndbuf,READ_ONCE (sysctl_quic_wmem[1 ]));WRITE_ONCE (sk->sk_rcvbuf,READ_ONCE (sysctl_quic_rmem[1 ]));
@@ -52,6 +55,8 @@ static int quic_init_sock(struct sock *sk)static void quic_destroy_sock (struct sock *sk) { +quic_stream_free (quic_streams (sk)); +quic_data_free (quic_ticket (sk));quic_data_free (quic_token (sk));quic_data_free (quic_alpn (sk));
net/quic/socket.h +8 -0
@@ -13,6 +13,7 @@#include "common.h" #include "family.h" +#include "stream.h" #include "protocol.h"
@@ -34,6 +35,8 @@ struct quic_sock {struct quic_data ticket;struct quic_data token;struct quic_data alpn; + +struct quic_stream_table streams; };struct quic6_sock {
@@ -71,6 +74,11 @@ static inline struct quic_data *quic_alpn(const struct sock *sk)return &quic_sk (sk)->alpn; } +static inline struct quic_stream_table *quic_streams (const struct sock *sk) +{ +return &quic_sk (sk)->streams; +} +static inline bool quic_is_serv (const struct sock *sk) {return !!sk->sk_max_ack_backlog;
net/quic/stream.c new file +400 -0
400 lines not on this page · raw message
net/quic/stream.h new file +119 -0
119 lines not on this page · raw message
Signature · 1 line
2.47.1
Re: [PATCH net-next v7 06/16] quic: add stream management
Quoted 5 lines · Xin Long
net/quic/stream.h new file +5
-0+struct quic_stream { +struct hlist_node node; +s64 id;/* Stream ID as defined in RFC 9000 Section 2.1 */ +struct { +/* Sending-side stream level flow control */
net/quic/stream.h line 46
Quoted 10 lines
net/quic/stream.h new file +10
-0+u64 last_max_bytes;/* Maximum send offset advertised by peer at last update */ +u64 max_bytes;/* Current maximum offset we are allowed to send to */ +u64 bytes;/* Bytes already sent to peer */ + +u32 errcode;/* Application error code to send in RESET_STREAM */ +u32 frags;/* Number of sent STREAM frames not yet acknowledged */ +u8 state;/* Send stream state, per rfc9000#section-3.1 */ + + u8 data_blocked:1; /* True if flow control blocks sending more data */ + u8 done:1; /* True if application indicated end of stream (FIN sent) */
Minor nit: AFAICS with the current struct layout the bitfield above does not save any space, compared to plain u8 and will lead to worse code.
Quoted 6 lines
net/quic/stream.h new file +6
-0+ } send; +struct { +/* Receiving-side stream level flow control */ +u64 max_bytes;/* Maximum offset peer is allowed to send to */ +u64 window;/* Remaining receive window before advertise a new limit */ +u64 bytes;/* Bytes consumed by application from the stream */
net/quic/stream.h line 62
Quoted 10 lines
net/quic/stream.h new file +10
-0+ +u64 highest;/* Highest received offset */ +u64 offset;/* Offset up to which data is in buffer or consumed */ +u64 finalsz;/* Final size of the stream if FIN received */ + +u32 frags;/* Number of received STREAM frames pending reassembly */ +u8 state;/* Receive stream state, per rfc9000#section-3.2 */ + + u8 stop_sent:1; /* True if STOP_SENDING has been sent */ + u8 done:1; /* True if FIN received and final size validated */
... same here...
Quoted 11 lines
net/quic/stream.h new file +11
-0+ } recv; +}; + +struct quic_stream_limits { +/* Stream limit parameters defined in rfc9000#section-18.2 */ +u64 max_stream_data_bidi_remote;/* initial_max_stream_data_bidi_remote */ +u64 max_stream_data_bidi_local;/* initial_max_stream_data_bidi_local */ +u64 max_stream_data_uni;/* initial_max_stream_data_uni */ +u64 max_streams_bidi;/* initial_max_streams_bidi */ +u64 max_streams_uni;/* initial_max_streams_uni */ +
net/quic/stream.h line 83
Quoted 10 lines
net/quic/stream.h new file +10
-0+s64 next_bidi_stream_id;/* Next bidi stream ID to open or accept */ +s64 next_uni_stream_id;/* Next uni stream ID to open or accept */ +s64 max_bidi_stream_id;/* Highest allowed bidi stream ID */ +s64 max_uni_stream_id;/* Highest allowed uni stream ID */ +s64 active_stream_id;/* Most recently opened stream ID */ + + u8 bidi_blocked:1; /* STREAMS_BLOCKED_BIDI sent, awaiting ACK */ + u8 uni_blocked:1; /* STREAMS_BLOCKED_UNI sent, awaiting ACK */ + u8 bidi_pending:1; /* MAX_STREAMS_BIDI needs to be sent */ + u8 uni_pending:1; /* MAX_STREAMS_UNI needs to be sent */
... and here.
Other than that LGTM. With the bitfield replaced with plain u8 feel free to add my
Re: [PATCH net-next v7 06/16] quic: add stream management
Quoted 12 lines · Paolo Abeni
Quoted 10 lines · Xin Long
net/quic/stream.h new file +10
-0+struct quic_stream { +struct hlist_node node; +s64 id;/* Stream ID as defined in RFC 9000 Section 2.1 */ +struct { +/* Sending-side stream level flow control */ +u64 last_max_bytes;/* Maximum send offset advertised by peer at last update */ +u64 max_bytes;/* Current maximum offset we are allowed to send to */ +u64 bytes;/* Bytes already sent to peer */ + +u32 errcode;/* Application error code to send in RESET_STREAM */
Quoted 9 lines
net/quic/stream.h line 46
Quoted 5 lines
net/quic/stream.h new file +5
-0+u32 frags;/* Number of sent STREAM frames not yet acknowledged */ +u8 state;/* Send stream state, per rfc9000#section-3.1 */ + + u8 data_blocked:1; /* True if flow control blocks sending more data */ + u8 done:1; /* True if application indicated end of stream (FIN sent) */Minor nit: AFAICS with the current struct layout the bitfield above does not save any space, compared to plain u8 and will lead to worse code.
Makes sense, will change to plain u8.
Thanks.
Quoted 48 lines
Quoted 6 lines
net/quic/stream.h new file +6
-0+ } send; +struct { +/* Receiving-side stream level flow control */ +u64 max_bytes;/* Maximum offset peer is allowed to send to */ +u64 window;/* Remaining receive window before advertise a new limit */ +u64 bytes;/* Bytes consumed by application from the stream */ net/quic/stream.h line 62
Quoted 10 lines
net/quic/stream.h new file +10
-0+ +u64 highest;/* Highest received offset */ +u64 offset;/* Offset up to which data is in buffer or consumed */ +u64 finalsz;/* Final size of the stream if FIN received */ + +u32 frags;/* Number of received STREAM frames pending reassembly */ +u8 state;/* Receive stream state, per rfc9000#section-3.2 */ + + u8 stop_sent:1; /* True if STOP_SENDING has been sent */ + u8 done:1; /* True if FIN received and final size validated */... same here...
Quoted 11 lines
net/quic/stream.h new file +11
-0+ } recv; +}; + +struct quic_stream_limits { +/* Stream limit parameters defined in rfc9000#section-18.2 */ +u64 max_stream_data_bidi_remote;/* initial_max_stream_data_bidi_remote */ +u64 max_stream_data_bidi_local;/* initial_max_stream_data_bidi_local */ +u64 max_stream_data_uni;/* initial_max_stream_data_uni */ +u64 max_streams_bidi;/* initial_max_streams_bidi */ +u64 max_streams_uni;/* initial_max_streams_uni */ +net/quic/stream.h line 83
Quoted 10 lines
net/quic/stream.h new file +10
-0+s64 next_bidi_stream_id;/* Next bidi stream ID to open or accept */ +s64 next_uni_stream_id;/* Next uni stream ID to open or accept */ +s64 max_bidi_stream_id;/* Highest allowed bidi stream ID */ +s64 max_uni_stream_id;/* Highest allowed uni stream ID */ +s64 active_stream_id;/* Most recently opened stream ID */ + + u8 bidi_blocked:1; /* STREAMS_BLOCKED_BIDI sent, awaiting ACK */ + u8 uni_blocked:1; /* STREAMS_BLOCKED_UNI sent, awaiting ACK */ + u8 bidi_pending:1; /* MAX_STREAMS_BIDI needs to be sent */ + u8 uni_pending:1; /* MAX_STREAMS_UNI needs to be sent */... and here.
Other than that LGTM. With the bitfield replaced with plain u8 feel free to add my
Acked-by Paolo Abeni <[email protected]>
PATCH v7 7/16 quic: add connection id management
In reply to Xin LongThis patch introduces 'struct quic_conn_id_set' for managing Connection IDs (CIDs), which are represented by 'struct quic_source_conn_id' and 'struct quic_dest_conn_id'.
It provides helpers to add and remove CIDs from the set, and handles insertion of source CIDs into the global connection ID hash table when necessary.
- quic_conn_id_add(): Add a new Connection ID to the set, and inserts it to conn_id hash table if it is a source conn_id.
- quic_conn_id_remove(): Remove connection IDs the set with sequence numbers less than or equal to a number.
It also adds utilities to look up CIDs by value or sequence number, search the global hash table for incoming packets, and check for stateless reset tokens among destination CIDs. These functions are essential for RX path connection lookup and stateless reset processing.
- quic_conn_id_find(): Find a Connection ID in the set by seq number.
- quic_conn_id_lookup(): Lookup a Connection ID from global hash table using the ID value, typically used for socket lookup on the RX path.
- quic_conn_id_token_exists(): Check if a stateless reset token exists in any dest Connection ID (used during stateless reset processing).
Note source/dest conn_id set is per socket, the operations on it are always pretected by the sock lock.
---
v3:
- Clarify in changelog that conn_id set is always protected by sock lock (suggested by Paolo).
- Adjust global source conn_id hashtable operations for the new hashtable type.
v4:
- Replace struct hlist_node with hlist_nulls_node for the node in struct quic_source_conn_id to support lockless lookup.
v7:
- Break the loop earlier if common->number > number in quic_conn_id_remove/find() (suggested by Paolo).
- Add a comment in quic_conn_id_first_number().
--- net/quic/Makefile | 2 +- net/quic/connid.c | 225 ++++++++++++++++++++++++++++++++++++++++++++++ net/quic/connid.h | 163 +++++++++++++++++++++++++++++++++ net/quic/socket.c | 6 ++ net/quic/socket.h | 13 +++ 5 files changed, 408 insertions(+), 1 deletion(-) create mode 100644 net/quic/connid.c create mode 100644 net/quic/connid.h
net/quic/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o -quic-y := common.o family.o protocol.o socket.o stream.o +quic-y := common.o family.o protocol.o socket.o stream.o connid.o
net/quic/connid.c new file +225 -0
225 lines not on this page · raw message
net/quic/connid.h new file +163 -0
163 lines not on this page · raw message
net/quic/socket.c +6 -0
@@ -41,6 +41,9 @@ static int quic_init_sock(struct sock *sk) sk->sk_write_space = quic_write_space;sock_set_flag (sk,SOCK_USE_WRITE_QUEUE ); +quic_conn_id_set_init (quic_source (sk),1 ); +quic_conn_id_set_init (quic_dest (sk),0 ); +if (quic_stream_init (quic_streams (sk)))return -ENOMEM ;
@@ -55,6 +58,9 @@ static int quic_init_sock(struct sock *sk)static void quic_destroy_sock (struct sock *sk) { +quic_conn_id_set_free (quic_source (sk)); +quic_conn_id_set_free (quic_dest (sk)); +quic_stream_free (quic_streams (sk));quic_data_free (quic_ticket (sk));
net/quic/socket.h +13 -0
@@ -14,6 +14,7 @@#include "common.h" #include "family.h" #include "stream.h" +#include "connid.h" #include "protocol.h"
@@ -37,6 +38,8 @@ struct quic_sock {struct quic_data alpn;struct quic_stream_table streams; +struct quic_conn_id_set source; +struct quic_conn_id_set dest; };struct quic6_sock {
@@ -79,6 +82,16 @@ static inline struct quic_stream_table *quic_streams(const struct sock *sk)return &quic_sk (sk)->streams; } +static inline struct quic_conn_id_set *quic_source (const struct sock *sk) +{ +return &quic_sk (sk)->source; +} + +static inline struct quic_conn_id_set *quic_dest (const struct sock *sk) +{ +return &quic_sk (sk)->dest; +} +static inline bool quic_is_serv (const struct sock *sk) {return !!sk->sk_max_ack_backlog;
Signature · 1 line
2.47.1
PATCH v7 8/16 quic: add path management
In reply to Xin LongThis patch introduces 'quic_path_group' for managing paths, represented by 'struct quic_path'. A connection may use two paths simultaneously for connection migration.
Each path is associated with a UDP tunnel socket (sk), and a single UDP tunnel socket can be related to multiple paths from different sockets. These UDP tunnel sockets are wrapped in 'quic_udp_sock' structures and stored in a hash table.
It includes mechanisms to bind and unbind paths, detect alternative paths for migration, and swap paths to support seamless transition between networks.
- quic_path_bind(): Bind a path to a port and associate it with a UDP sk.
- quic_path_unbind(): Unbind a path from a port and disassociate it from a UDP sk.
- quic_path_swap(): Swap two paths to facilitate connection migration.
- quic_path_detect_alt(): Determine if a packet is using an alternative path, used for connection migration.
It also integrates basic support for Packetization Layer Path MTU
Discovery (PLPMTUD), using PING frames and ICMP feedback to adjust path MTU and handle probe confirmation or resets during routing changes.
- quic_path_pl_recv(): state transition and pmtu update after the probe packet is acked.
- quic_path_pl_toobig(): state transition and pmtu update after receiving a toobig or needfrag icmp packet.
- quic_path_pl_send(): state transition and pmtu update after sending a probe packet.
- quic_path_pl_reset(): restart the probing when path routing changes.
- quic_path_pl_confirm(): check if probe packet gets acked.
---
v3:
- Fix annotation in quic_udp_sock_lookup() (noted by Paolo).
- Use inet_sk_get_local_port_range() instead of inet_get_local_port_range() (suggested by Paolo).
- Adjust global UDP tunnel socket hashtable operations for the new hashtable type.
- Delete quic_workqueue; use system_wq for UDP tunnel socket destroy.
v4:
- Cache UDP tunnel socket pointer and its source address in struct quic_path for RCU-protected lookup/access.
- Return -EAGAIN instead of -EINVAL in quic_path_bind() when UDP socket is being released in workqueue.
- Move udp_tunnel_sock_release() out of the mutex_lock to avoid a warning of lockdep in quic_udp_sock_put_work().
- Introduce quic_wq for UDP socket release work, so all pending works can be flushed before destroying the hashtable in quic_exit().
v5:
- Rename quic_path_free() to quic_path_unbind() (suggested by Paolo).
- Remove the 'serv' member from struct quic_path_group, since quic_is_serv() defined in a previous patch now uses sk->sk_max_ack_backlog for server-side detection.
- Use quic_ktime_get_us() to set skb_cb->time, as RTT is measured in microseconds and jiffies_to_usecs() is not accurate enough.
v6:
- Do not reset transport_header for QUIC in quic_udp_rcv(), allowing
removal of udph_offset and enabling access to the UDP header via
udp_hdr(); Pull skb->data in quic_udp_rcv() to allow access to the
QUIC header via skb->data.v7:
- Pass udp sk to quic_path_rcv() and move the call to skb_linearize() and skb_set_owner_sk_safe() to .quic_path_rcv().
- Delete the call to skb_linearize() and skb_set_owner_sk_safe() from quic_udp_err(), as it should not change skb in .encap_err_lookup() (noted by AI review).
--- net/quic/Makefile | 2 +- net/quic/path.c | 524 ++++++++++++++++++++++++++++++++++++++++++++ net/quic/path.h | 172 +++++++++++++++ net/quic/protocol.c | 11 + net/quic/socket.c | 3 + net/quic/socket.h | 7 + 6 files changed, 718 insertions(+), 1 deletion(-) create mode 100644 net/quic/path.c create mode 100644 net/quic/path.h
net/quic/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o -quic-y := common.o family.o protocol.o socket.o stream.o connid.o +quic-y := common.o family.o protocol.o socket.o stream.o connid.o path.o
net/quic/path.c new file +524 -0
524 lines not on this page · raw message
net/quic/path.h new file +172 -0
172 lines not on this page · raw message
net/quic/protocol.c +11 -0
@@ -21,6 +21,7 @@static unsigned int quic_net_id__read_mostly ;struct percpu_counter quic_sockets_allocated; +struct workqueue_struct *quic_wq;long sysctl_quic_mem[3 ];int sysctl_quic_rmem[3 ];
@@ -338,6 +339,12 @@ static __init int quic_init(void)if (err) goto err_hash; + quic_wq =create_workqueue ("quic_workqueue" ); +if (!quic_wq) { + err = -ENOMEM ; + goto err_wq; + } + err =register_pernet_subsys (&quic_net_ops);if (err) goto err_def_ops;
@@ -355,6 +362,8 @@ static __init int quic_init(void) err_protosw:unregister_pernet_subsys (&quic_net_ops); err_def_ops: +destroy_workqueue (quic_wq); +err_wq:quic_hash_tables_destroy (); err_hash:percpu_counter_destroy (&quic_sockets_allocated);
@@ -369,6 +378,8 @@ static __exit void quic_exit(void)#endif quic_protosw_exit ();unregister_pernet_subsys (&quic_net_ops); +flush_workqueue (quic_wq); +destroy_workqueue (quic_wq);quic_hash_tables_destroy ();percpu_counter_destroy (&quic_sockets_allocated);pr_info ("quic: exit\n" );
net/quic/socket.c +3 -0
@@ -58,6 +58,9 @@ static int quic_init_sock(struct sock *sk)static void quic_destroy_sock (struct sock *sk) { +quic_path_unbind (sk,quic_paths (sk),0 ); +quic_path_unbind (sk,quic_paths (sk),1 ); +quic_conn_id_set_free (quic_source (sk));quic_conn_id_set_free (quic_dest (sk));
net/quic/socket.h +7 -0
@@ -15,6 +15,7 @@#include "family.h" #include "stream.h" #include "connid.h" +#include "path.h" #include "protocol.h"
@@ -40,6 +41,7 @@ struct quic_sock {struct quic_stream_table streams;struct quic_conn_id_set source;struct quic_conn_id_set dest; +struct quic_path_group paths; };struct quic6_sock {
@@ -92,6 +94,11 @@ static inline struct quic_conn_id_set *quic_dest(const struct sock *sk)return &quic_sk (sk)->dest; } +static inline struct quic_path_group *quic_paths (const struct sock *sk) +{ +return &quic_sk (sk)->paths; +} +static inline bool quic_is_serv (const struct sock *sk) {return !!sk->sk_max_ack_backlog;
Signature · 1 line
2.47.1
Paolo Abeni
maintainerRe: [PATCH net-next v7 08/16] quic: add path management
Quoted 11 lines · Xin Long
net/quic/path.c new file +10
-0@@ -0,0 +1,524 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* QUIC kernel implementation +* (C) Copyright Red Hat Corp. 2023 +* +* This file is part of the QUIC kernel implementation +* +* Initialization/cleanup for QUIC protocol support. +* +* Written or modified by: +* Xin Long <[email protected]>
net/quic/path.c line 20
Quoted 10 lines
net/quic/path.c new file +10
-0+*/ + +#include <net/udp_tunnel.h> +#include <linux/quic.h> + +#include "common.h" +#include "family.h" +#include "path.h" + +static int (*quic_path_rcv)(struct sock *sk,struct sk_buff *skb,u8 err);
It's unclear why an indirect call is needed here. At least some explanation is needed in the commit message, possibly you could call directly a static function.
net/quic/path.c line 31
Quoted 11 lines
net/quic/path.c new file +11
-0+ +static int quic_udp_rcv (struct sock *sk,struct sk_buff *skb) +{ +memset (skb->cb,0 ,sizeof (skb->cb)); +QUIC_SKB_CB (skb)->seqno =-1 ; +QUIC_SKB_CB (skb)->time =quic_ktime_get_us (); + +skb_pull (skb,sizeof (struct udphdr )); +skb_dst_force (skb); +quic_path_rcv (sk, skb,0 ); +return 0 ;
Why not:
return quic_path_rcv(sk, skb, 0);
?
Quoted 36 lines
net/quic/path.c new file +36
-0+static struct quic_udp_sock *quic_udp_sock_create (struct sock *sk,union quic_addr *a) +{ +struct udp_tunnel_sock_cfg tuncfg = {}; +struct udp_port_cfg udp_conf = {}; +struct net *net =sock_net (sk); +struct quic_uhash_head *head; +struct quic_udp_sock *us; +struct socket *sock; + + us =kzalloc (sizeof (*us),GFP_KERNEL ); +if (!us) +return NULL ; + +quic_udp_conf_init (sk, &udp_conf, a); +if (udp_sock_create (net, &udp_conf, &sock)) { +pr_debug ("%s: failed to create udp sock\n" , __func__); +kfree (us); +return NULL ; + } + + tuncfg.encap_type =1 ; + tuncfg.encap_rcv = quic_udp_rcv; + tuncfg.encap_err_lookup = quic_udp_err; +setup_udp_tunnel_sock (net, sock, &tuncfg); + +refcount_set (&us->refcnt,1 ); + us->sk = sock->sk; +memcpy (&us->addr, a,sizeof (*a)); + us->bind_ifindex = sk->sk_bound_dev_if; + + head =quic_udp_sock_head (net,ntohs (a->v4.sin_port)); +hlist_add_head (&us->node, &head->head); +INIT_WORK (&us->work, quic_udp_sock_put_work); + +return us; +}
net/quic/path.c line 110
Quoted 10 lines
net/quic/path.c new file +10
-0+ +static bool quic_udp_sock_get (struct quic_udp_sock *us) +{ +return refcount_inc_not_zero (&us->refcnt); +} + +static void quic_udp_sock_put (struct quic_udp_sock *us) +{ +if (refcount_dec_and_test (&us->refcnt)) +queue_work (quic_wq, &us->work);
Why using a workqueue here? AFAICS all the caller are in process context. Is that to break a possible deadlock due to nested mutex? Likely a comment on the refcount/locking scheme would help.
/P
Re: [PATCH net-next v7 08/16] quic: add path management
Quoted 19 lines · Paolo Abeni
Quoted 17 lines · Xin Long
net/quic/path.c new file +16
-0@@ -0,0 +1,524 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* QUIC kernel implementation +* (C) Copyright Red Hat Corp. 2023 +* +* This file is part of the QUIC kernel implementation +* +* Initialization/cleanup for QUIC protocol support. +* +* Written or modified by: +* Xin Long <[email protected]> +*/ + +#include <net/udp_tunnel.h> +#include <linux/quic.h> + +#include "common.h"
Quoted 9 lines
net/quic/path.c line 20
Quoted 4 lines
net/quic/path.c new file +4
-0+#include "family.h" +#include "path.h" + +static int (*quic_path_rcv)(struct sock *sk,struct sk_buff *skb,u8 err);It's unclear why an indirect call is needed here. At least some explanation is needed in the commit message, possibly you could call directly a static function.
This patchset makes the path subcomponent more independent from the core implementation. Aside from a few shared helper functions, it doesn't rely on code outside the subcomponent, in particular socket.c and packet.c.
Other subcomponents, such as stream, connid, pnspace, cong, crypto, common, and family follow the same approach. They expose APIs to the core QUIC logic and don’t see the main process.
Will leave an explanation here.
Quoted 13 lines
net/quic/path.c line 31
Quoted 11 lines
net/quic/path.c new file +11
-0+ +static int quic_udp_rcv (struct sock *sk,struct sk_buff *skb) +{ +memset (skb->cb,0 ,sizeof (skb->cb)); +QUIC_SKB_CB (skb)->seqno =-1 ; +QUIC_SKB_CB (skb)->time =quic_ktime_get_us (); + +skb_pull (skb,sizeof (struct udphdr )); +skb_dst_force (skb); +quic_path_rcv (sk, skb,0 ); +return 0 ;Why not:
Quoted 2 lines
return quic_path_rcv(sk, skb, 0);?
I checked the udp tunnel users:
- bareudp: bareudp_udp_encap_recv()
- vxlan: vxlan_rcv()
- geneve: geneve_udp_encap_recv()
- tipc: tipc_udp_recv()
- sctp: sctp_udp_rcv()
they all are returning 0 in .encap_udp(), I think because they all take care of the
skb freeing in their err path already.
is it safe to return error to UDP stack if the skb is already freed? Do you know?
Quoted 43 lines
Quoted 43 lines
net/quic/path.c new file +42
-0+static struct quic_udp_sock *quic_udp_sock_create (struct sock *sk,union quic_addr *a) +{ +struct udp_tunnel_sock_cfg tuncfg = {}; +struct udp_port_cfg udp_conf = {}; +struct net *net =sock_net (sk); +struct quic_uhash_head *head; +struct quic_udp_sock *us; +struct socket *sock; + + us =kzalloc (sizeof (*us),GFP_KERNEL ); +if (!us) +return NULL ; + +quic_udp_conf_init (sk, &udp_conf, a); +if (udp_sock_create (net, &udp_conf, &sock)) { +pr_debug ("%s: failed to create udp sock\n" , __func__); +kfree (us); +return NULL ; + } + + tuncfg.encap_type =1 ; + tuncfg.encap_rcv = quic_udp_rcv; + tuncfg.encap_err_lookup = quic_udp_err; +setup_udp_tunnel_sock (net, sock, &tuncfg); + +refcount_set (&us->refcnt,1 ); + us->sk = sock->sk; +memcpy (&us->addr, a,sizeof (*a)); + us->bind_ifindex = sk->sk_bound_dev_if; + + head =quic_udp_sock_head (net,ntohs (a->v4.sin_port)); +hlist_add_head (&us->node, &head->head); +INIT_WORK (&us->work, quic_udp_sock_put_work); + +return us; +} + +static bool quic_udp_sock_get (struct quic_udp_sock *us) +{ +return refcount_inc_not_zero (&us->refcnt); +} +
Quoted 9 lines
net/quic/path.c line 110
Quoted 4 lines
net/quic/path.c new file +4
-0+static void quic_udp_sock_put (struct quic_udp_sock *us) +{ +if (refcount_dec_and_test (&us->refcnt)) +queue_work (quic_wq, &us->work);Why using a workqueue here? AFAICS all the caller are in process context. Is that to break a possible deadlock due to nested mutex? Likely a comment on the refcount/locking scheme would help.
quic_udp_sock_put() will also be called in the RX path via quic_path_unbind() for the connection migration (after changing to a new path.), which is in the patchset-2.
I will leave a comment for an explanation here.
Thanks.
Re: [PATCH net-next v7 08/16] quic: add path management
Quoted 35 lines · Xin Long
Quoted 19 lines · Paolo Abeni
Quoted 17 lines · Xin Long
net/quic/path.c new file +16
-0@@ -0,0 +1,524 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* QUIC kernel implementation +* (C) Copyright Red Hat Corp. 2023 +* +* This file is part of the QUIC kernel implementation +* +* Initialization/cleanup for QUIC protocol support. +* +* Written or modified by: +* Xin Long <[email protected]> +*/ + +#include <net/udp_tunnel.h> +#include <linux/quic.h> + +#include "common.h" Quoted 9 lines
net/quic/path.c line 20
Quoted 4 lines
net/quic/path.c new file +4
-0+#include "family.h" +#include "path.h" + +static int (*quic_path_rcv)(struct sock *sk,struct sk_buff *skb,u8 err);It's unclear why an indirect call is needed here. At least some explanation is needed in the commit message, possibly you could call directly a static function.
This patchset makes the path subcomponent more independent from the core implementation. Aside from a few shared helper functions, it doesn't rely on code outside the subcomponent, in particular
socket.candpacket.c.
Quoted 6 lines
Other subcomponents, such as stream, connid, pnspace, cong, crypto, common, and family follow the same approach. They expose APIs to the core QUIC logic and don’t see the main process.
Will leave an explanation here.
To achieve this, instead of introducing an indirect call, we can use an 'extern' declaration of quic_packet_rcv() once it is introduced. Before that point, the code simply falls back to calling kfree_skb(skb).
Will update it.
Thanks.
Quoted 89 lines
Quoted 13 lines
net/quic/path.c line 31
Quoted 11 lines
net/quic/path.c new file +11
-0+ +static int quic_udp_rcv (struct sock *sk,struct sk_buff *skb) +{ +memset (skb->cb,0 ,sizeof (skb->cb)); +QUIC_SKB_CB (skb)->seqno =-1 ; +QUIC_SKB_CB (skb)->time =quic_ktime_get_us (); + +skb_pull (skb,sizeof (struct udphdr )); +skb_dst_force (skb); +quic_path_rcv (sk, skb,0 ); +return 0 ;Why not:
Quoted 2 lines
return quic_path_rcv(sk, skb, 0);?
I checked the udp tunnel users:
- bareudp:
bareudp_udp_encap_recv()
- vxlan:vxlan_rcv()
- geneve:geneve_udp_encap_recv()
- tipc:tipc_udp_recv()
- sctp:sctp_udp_rcv()they all are returning 0 in .encap_udp(), I think because they all take care of the
skb freeing in their err path already.is it safe to return error to UDP stack if the skb is already freed? Do you know?
Quoted 43 lines
Quoted 43 lines
net/quic/path.c new file +42
-0+static struct quic_udp_sock *quic_udp_sock_create (struct sock *sk,union quic_addr *a) +{ +struct udp_tunnel_sock_cfg tuncfg = {}; +struct udp_port_cfg udp_conf = {}; +struct net *net =sock_net (sk); +struct quic_uhash_head *head; +struct quic_udp_sock *us; +struct socket *sock; + + us =kzalloc (sizeof (*us),GFP_KERNEL ); +if (!us) +return NULL ; + +quic_udp_conf_init (sk, &udp_conf, a); +if (udp_sock_create (net, &udp_conf, &sock)) { +pr_debug ("%s: failed to create udp sock\n" , __func__); +kfree (us); +return NULL ; + } + + tuncfg.encap_type =1 ; + tuncfg.encap_rcv = quic_udp_rcv; + tuncfg.encap_err_lookup = quic_udp_err; +setup_udp_tunnel_sock (net, sock, &tuncfg); + +refcount_set (&us->refcnt,1 ); + us->sk = sock->sk; +memcpy (&us->addr, a,sizeof (*a)); + us->bind_ifindex = sk->sk_bound_dev_if; + + head =quic_udp_sock_head (net,ntohs (a->v4.sin_port)); +hlist_add_head (&us->node, &head->head); +INIT_WORK (&us->work, quic_udp_sock_put_work); + +return us; +} + +static bool quic_udp_sock_get (struct quic_udp_sock *us) +{ +return refcount_inc_not_zero (&us->refcnt); +} +Quoted 9 lines
net/quic/path.c line 110
Quoted 4 lines
net/quic/path.c new file +4
-0+static void quic_udp_sock_put (struct quic_udp_sock *us) +{ +if (refcount_dec_and_test (&us->refcnt)) +queue_work (quic_wq, &us->work);Why using a workqueue here? AFAICS all the caller are in process context. Is that to break a possible deadlock due to nested mutex? Likely a comment on the refcount/locking scheme would help.
quic_udp_sock_put()will also be called in the RX path viaquic_path_unbind()for the connection migration (after changing to a new path.), which is in the patchset-2.I will leave a comment for an explanation here.
Thanks.
PATCH v7 9/16 quic: add congestion control
In reply to Xin LongThis patch introduces 'quic_cong' for RTT measurement and congestion control. The 'quic_cong_ops' is added to define the congestion control algorithm.
It implements a congestion control state machine with slow start, congestion avoidance, and recovery phases, and currently introduces the New Reno algorithm only.
The implementation updates RTT estimates when packets are acknowledged, reacts to loss and ECN signals, and adjusts the congestion window accordingly during packet transmission and acknowledgment processing.
- quic_cong_rtt_update(): Performs RTT measurement, invoked when a packet is acknowledged by the largest number in the ACK frame.
- quic_cong_on_packet_acked(): Invoked when a packet is acknowledged.
- quic_cong_on_packet_lost(): Invoked when a packet is marked as lost.
- quic_cong_on_process_ecn(): Invoked when an ACK_ECN frame is received.
- quic_cong_on_packet_sent(): Invoked when a packet is transmitted.
- quic_cong_on_ack_recv(): Invoked when an ACK frame is received.
---
v4:
- Remove the CUBIC congestion algorithm support for this version (suggested by Paolo).
v5:
- Do not update the pacing rate when !cong->smoothed_rtt in quic_cong_pace_update() (suggested by Paolo).
- Change timestamp variables from u32 to u64, as RTT is measured in microseconds and u64 provides sufficient precision for timestamps in microsecond.
--- net/quic/Makefile | 3 +- net/quic/cong.c | 307 ++++++++++++++++++++++++++++++++++++++++++++++ net/quic/cong.h | 120 ++++++++++++++++++ net/quic/socket.c | 1 + net/quic/socket.h | 7 ++ 5 files changed, 437 insertions(+), 1 deletion(-) create mode 100644 net/quic/cong.c create mode 100644 net/quic/cong.h
net/quic/Makefile +2 -1
@@ -5,4 +5,5 @@ obj-$(CONFIG_IP_QUIC) += quic.o -quic-y := common.o family.o protocol.o socket.o stream.o connid.o path.o +quic-y := common.o family.o protocol.o socket.o stream.o connid.o path.o \ + cong.o
net/quic/cong.c new file +307 -0
307 lines not on this page · raw message
net/quic/cong.h new file +120 -0
120 lines not on this page · raw message
net/quic/socket.c +1 -0
@@ -43,6 +43,7 @@ static int quic_init_sock(struct sock *sk)quic_conn_id_set_init (quic_source (sk),1 );quic_conn_id_set_init (quic_dest (sk),0 ); +quic_cong_init (quic_cong (sk));if (quic_stream_init (quic_streams (sk)))return -ENOMEM ;
net/quic/socket.h +7 -0
@@ -16,6 +16,7 @@#include "stream.h" #include "connid.h" #include "path.h" +#include "cong.h" #include "protocol.h"
@@ -42,6 +43,7 @@ struct quic_sock {struct quic_conn_id_set source;struct quic_conn_id_set dest;struct quic_path_group paths; +struct quic_cong cong; };struct quic6_sock {
@@ -104,6 +106,11 @@ static inline bool quic_is_serv(const struct sock *sk)return !!sk->sk_max_ack_backlog; } +static inline struct quic_cong *quic_cong (const struct sock *sk) +{ +return &quic_sk (sk)->cong; +} +static inline bool quic_is_establishing (struct sock *sk) {return sk->sk_state ==QUIC_SS_ESTABLISHING ;
Signature · 1 line
2.47.1
PATCH v7 10/16 quic: add packet number space
In reply to Xin LongThis patch introduces 'quic_pnspace', which manages per packet number space members.
It maintains the next packet number to assign, tracks the total length of frames currently in flight, and records the time when the next packet may be considered lost. It also keeps track of the largest acknowledged packet number, the time it was acknowledged, and when the most recent ack eliciting packet was sent. These fields are useful for loss detection, RTT estimation, and congestion control.
To support ACK frame generation, quic_pnspace includes a packet number acknowledgment map (pn_ack_map) that tracks received packet numbers. Supporting functions are provided to validate and mark received packet numbers and compute the number of gap blocks needed during ACK frame construction.
- quic_pnspace_check(): Validates a received packet number.
- quic_pnspace_mark(): Marks a received packet number in the ACK map.
- quic_pnspace_num_gabs(): Returns the gap ACK blocks for constructing ACK frames.
Note QUIC uses separate packet number spaces for each encryption level (APP, INITIAL, HANDSHAKE, EARLY) except EARLY and all generations of APP keys use the same packet number space, as describe in rfc9002#section-4.1.
---
v5:
- Change timestamp variables from u32 to u64 and use quic_ktime_get_us() to set max_pn_acked_time, as jiffies_to_usecs() is not accurate enough.
- Reorder some members in quic_pnspace to reduce 32-bit holes (noted by Paolo).
v6:
- Note for AI reviews: it's safe to do cast (u16)(pn - space->base_pn) in quic_pnspace_mark(), as the pn < base_pn + QUIC_PN_MAP_SIZE (4096) validation is always done in quic_pnspace_check(), which will always be called before quic_pnspace_mark() in a later patchset.
- Note for AI reviews: failures in quic_pnspace_init() do not result in a pn_map leak in quic_init_sock(), because quic_destroy_sock() is always called to free it in err path, either via inet/6_create() or through quic_accept() in a later patchset.
--- net/quic/Makefile | 2 +- net/quic/pnspace.c | 225 +++++++++++++++++++++++++++++++++++++++++++++ net/quic/pnspace.h | 150 ++++++++++++++++++++++++++++++ net/quic/socket.c | 12 +++ net/quic/socket.h | 7 ++ 5 files changed, 395 insertions(+), 1 deletion(-) create mode 100644 net/quic/pnspace.c create mode 100644 net/quic/pnspace.h
net/quic/Makefile +1 -1
@@ -6,4 +6,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o quic-y := common.o family.o protocol.o socket.o stream.o connid.o path.o \ - cong.o + cong.o pnspace.o
net/quic/pnspace.c new file +225 -0
225 lines not on this page · raw message
net/quic/pnspace.h new file +150 -0
150 lines not on this page · raw message
net/quic/socket.c +12 -0
@@ -37,6 +37,8 @@ static void quic_write_space(struct sock *sk)static int quic_init_sock (struct sock *sk) { +u8 i; + sk->sk_destruct = inet_sock_destruct; sk->sk_write_space = quic_write_space;sock_set_flag (sk,SOCK_USE_WRITE_QUEUE );
@@ -48,6 +50,11 @@ static int quic_init_sock(struct sock *sk)if (quic_stream_init (quic_streams (sk)))return -ENOMEM ; +for (i =0 ; i <QUIC_PNSPACE_MAX ; i++) { +if (quic_pnspace_init (quic_pnspace (sk, i))) +return -ENOMEM ; + } +WRITE_ONCE (sk->sk_sndbuf,READ_ONCE (sysctl_quic_wmem[1 ]));WRITE_ONCE (sk->sk_rcvbuf,READ_ONCE (sysctl_quic_rmem[1 ]));
@@ -59,6 +66,11 @@ static int quic_init_sock(struct sock *sk)static void quic_destroy_sock (struct sock *sk) { +u8 i; + +for (i =0 ; i <QUIC_PNSPACE_MAX ; i++) +quic_pnspace_free (quic_pnspace (sk, i)); +quic_path_unbind (sk,quic_paths (sk),0 );quic_path_unbind (sk,quic_paths (sk),1 );
net/quic/socket.h +7 -0
@@ -12,6 +12,7 @@#include <linux/quic.h> #include "common.h" +#include "pnspace.h" #include "family.h" #include "stream.h" #include "connid.h"
@@ -44,6 +45,7 @@ struct quic_sock {struct quic_conn_id_set dest;struct quic_path_group paths;struct quic_cong cong; +struct quic_pnspace space[QUIC_PNSPACE_MAX ]; };struct quic6_sock {
@@ -111,6 +113,11 @@ static inline struct quic_cong *quic_cong(const struct sock *sk)return &quic_sk (sk)->cong; } +static inline struct quic_pnspace *quic_pnspace (const struct sock *sk,u8 level) +{ +return &quic_sk (sk)->space[level %QUIC_CRYPTO_EARLY ]; +} +static inline bool quic_is_establishing (struct sock *sk) {return sk->sk_state ==QUIC_SS_ESTABLISHING ;
Signature · 1 line
2.47.1
PATCH v7 11/16 quic: add crypto key derivation and installation
In reply to Xin LongThis patch introduces 'quic_crypto', a component responsible for QUIC encryption key derivation and installation across the various key
levels: Initial, Handshake, 0-RTT (Early), and 1-RTT (Application).
It provides helpers to derive and install initial secrets, set traffic secrets and install the corresponding keys, and perform key updates to enable forward secrecy. Additionally, it implements stateless reset token generation, used to support connection reset without state.
- quic_crypto_initial_keys_install(): Derive and install initial keys.
- quic_crypto_set_cipher(): Allocate all transforms based on the cipher type provided.
- quic_crypto_set_secret(): Set the traffic secret and install derived keys.
- quic_crypto_key_update(): Rekey and install new keys to the !phase side.
- quic_crypto_generate_stateless_reset_token(): Generate token for stateless reset.
These mechanisms are essential for establishing and maintaining secure communication throughout the QUIC connection lifecycle.
---
v3:
- Remove lock from quic_net, since Initial packet decryption for ALPN will be handled serially in a workqueue when ALPN demux is enabled.
v4:
- Use local cipher pointer in quic_crypto_set_secret() to avoid a warning from Smatch.
v5:
- Change the timestamp variables from u32 to u64, which provides sufficient precision for timestamps in microsecond.
--- net/quic/Makefile | 2 +- net/quic/crypto.c | 560 ++++++++++++++++++++++++++++++++++++++++++++ net/quic/crypto.h | 73 ++++++ net/quic/protocol.c | 11 + net/quic/protocol.h | 1 + net/quic/socket.c | 2 + net/quic/socket.h | 7 + 7 files changed, 655 insertions(+), 1 deletion(-) create mode 100644 net/quic/crypto.c create mode 100644 net/quic/crypto.h
net/quic/Makefile +1 -1
@@ -6,4 +6,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o quic-y := common.o family.o protocol.o socket.o stream.o connid.o path.o \ - cong.o pnspace.o + cong.o pnspace.o crypto.o
net/quic/crypto.c new file +560 -0
560 lines not on this page · raw message
net/quic/crypto.h new file +73 -0
73 lines not on this page · raw message
net/quic/protocol.c +11 -0
@@ -266,9 +266,17 @@ static int __net_init quic_net_init(struct net *net)if (!qn->stat)return -ENOMEM ; + err =quic_crypto_set_cipher (&qn->crypto,TLS_CIPHER_AES_GCM_128 ,CRYPTO_ALG_ASYNC ); +if (err) { +free_percpu (qn->stat); + qn->stat =NULL ; +return err; + } +#if IS_ENABLED (CONFIG_PROC_FS ) err =quic_net_proc_init (net);if (err) { +quic_crypto_free (&qn->crypto);free_percpu (qn->stat); qn->stat =NULL ; }
@@ -283,6 +291,7 @@ static void __net_exit quic_net_exit(struct net *net)#if IS_ENABLED (CONFIG_PROC_FS )quic_net_proc_exit (net);#endif +quic_crypto_free (&qn->crypto);free_percpu (qn->stat); qn->stat =NULL ; }
@@ -331,6 +340,8 @@ static __init int quic_init(void) sysctl_quic_wmem[1 ] =16 *1024 ; sysctl_quic_wmem[2 ] =max (64 *1024 , max_share); +quic_crypto_init (); + err =percpu_counter_init (&quic_sockets_allocated,0 ,GFP_KERNEL );if (err) goto err_percpu_counter;
net/quic/protocol.h +1 -0
@@ -48,6 +48,7 @@ struct quic_net {#if IS_ENABLED (CONFIG_PROC_FS )struct proc_dir_entry *proc_net;/* procfs entry for dumping QUIC socket stats */ #endif +struct quic_crypto crypto;/* Context for decrypting Initial packets for ALPN */ };struct quic_net *quic_net (struct net *net);
net/quic/socket.c +2 -0
@@ -70,6 +70,8 @@ static void quic_destroy_sock(struct sock *sk)for (i =0 ; i <QUIC_PNSPACE_MAX ; i++)quic_pnspace_free (quic_pnspace (sk, i)); +for (i =0 ; i <QUIC_CRYPTO_MAX ; i++) +quic_crypto_free (quic_crypto (sk, i));quic_path_unbind (sk,quic_paths (sk),0 );quic_path_unbind (sk,quic_paths (sk),1 );
net/quic/socket.h +7 -0
@@ -16,6 +16,7 @@#include "family.h" #include "stream.h" #include "connid.h" +#include "crypto.h" #include "path.h" #include "cong.h"
@@ -46,6 +47,7 @@ struct quic_sock {struct quic_path_group paths;struct quic_cong cong;struct quic_pnspace space[QUIC_PNSPACE_MAX ]; +struct quic_crypto crypto[QUIC_CRYPTO_MAX ]; };struct quic6_sock {
@@ -118,6 +120,11 @@ static inline struct quic_pnspace *quic_pnspace(const struct sock *sk, u8 level)return &quic_sk (sk)->space[level %QUIC_CRYPTO_EARLY ]; } +static inline struct quic_crypto *quic_crypto (const struct sock *sk,u8 level) +{ +return &quic_sk (sk)->crypto[level]; +} +static inline bool quic_is_establishing (struct sock *sk) {return sk->sk_state ==QUIC_SS_ESTABLISHING ;
Signature · 1 line
2.47.1
PATCH v7 12/16 quic: add crypto packet encryption and decryption
In reply to Xin LongThis patch adds core support for packet-level encryption and decryption using AEAD, including both payload protection and QUIC header protection. It introduces helpers to encrypt packets before transmission and to remove header protection and decrypt payloads upon reception, in line with QUIC's cryptographic requirements.
- quic_crypto_encrypt(): Perform header protection and payload encryption (TX).
- quic_crypto_decrypt(): Perform header protection removal and payload decryption (RX).
The patch also includes support for Retry token handling. It provides helpers to compute the Retry integrity tag, generate tokens for address validation, and verify tokens received from clients during the handshake phase.
- quic_crypto_get_retry_tag(): Compute tag for Retry packets.
- quic_crypto_generate_token(): Generate retry token.
- quic_crypto_verify_token(): Verify retry token.
These additions establish the cryptographic primitives necessary for secure QUIC packet exchange and address validation.
---
v3:
- quic_crypto_decrypt(): return -EKEYREVOKED to defer key updates to the workqueue when the packet is not marked backlog, since quic_crypto_key_update()/crypto_aead_setkey() must run in process context.
- Only perform header decryption if !cb->number_len to avoid double decryption when a key-update packet (with flipped key_phase) re-enters quic_crypto_decrypt() from the workqueue.
v4:
- skb_dst_force() is already called in in quic_udp_rcv() on recv path, so remove its call from quic_crypto_payload_decrypt(), which may be called without RCU protection.
- Remove the incorrect (void *) cast to quic_crypto_done.
v5:
- Use skb_cb->crypto_ctx for async crypto context freeing, which is
safer than using skb_shinfo(skb)->destructor_arg.
- skb_cb->number_max is removed and number is reused as the largest previously seen and update quic_crypto_get_header() accordingly.
- Change timestamp variables from u32 to u64 and use quic_ktime_get_us() for current timestamps, as jiffies_to_usecs() is not accurate enough.
v6:
- Rename quic_crypto_get_header() to quic_crypto_get_number(), move key_phase parsing out of it, check cb->length when parsing packet number, and update all callers.
- Use hdr->pnl + 1 instead of (*p & QUIC_PN_LEN_BITS_MASK) + 1 to get packet number length, and remove the unnecessary the len variable and QUIC_PN_LEN_BITS_MASK macro from quic_crypto_header_decrypt().
--- net/quic/crypto.c | 662 ++++++++++++++++++++++++++++++++++++++++++++++ net/quic/crypto.h | 10 + 2 files changed, 672 insertions(+)
net/quic/crypto.c +662 -0
@@ -207,6 +207,333 @@ static int quic_crypto_rx_keys_derive_and_install(struct quic_crypto *crypto)return err; } +static void *quic_crypto_skcipher_mem_alloc (struct crypto_skcipher *tfm,u32 mask_size, +u8 **iv,struct skcipher_request **req) +{ +unsigned int iv_size, req_size; +unsigned int len; +u8 *mem; + + iv_size =crypto_skcipher_ivsize (tfm); + req_size =sizeof (**req) +crypto_skcipher_reqsize (tfm); + + len = mask_size; + len += iv_size; + len +=crypto_skcipher_alignmask (tfm) & ~(crypto_tfm_ctx_alignment () -1 ); + len =ALIGN (len,crypto_tfm_ctx_alignment ()); + len += req_size; + + mem =kzalloc (len,GFP_ATOMIC ); +if (!mem) +return NULL ; + + *iv = (u8 *)PTR_ALIGN (mem + mask_size,crypto_skcipher_alignmask (tfm) +1 ); + *req = (struct skcipher_request *)PTR_ALIGN (*iv + iv_size, +crypto_tfm_ctx_alignment ()); + +return (void *)mem; +} + +#define QUIC_SAMPLE_LEN 16 + +#define QUIC_HEADER_FORM_BIT 0x80 +#define QUIC_LONG_HEADER_MASK 0x0f +#define QUIC_SHORT_HEADER_MASK 0x1f + +/* Header Protection. */ +static int quic_crypto_header_encrypt (struct crypto_skcipher *tfm,struct sk_buff *skb,bool chacha) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +struct skcipher_request *req; +struct scatterlist sg; +u8 *mask, *iv, *p; +int err, i; + + mask =quic_crypto_skcipher_mem_alloc (tfm,QUIC_SAMPLE_LEN , &iv, &req); +if (!mask) +return -ENOMEM ; + +/* rfc9001#section-5.4.2: Header Protection Sample: +* +* # pn_offset is the start of the Packet Number field. +* sample_offset = pn_offset + 4 +* +* sample = packet[sample_offset..sample_offset+sample_length] +* +* rfc9001#section-5.4.3: AES-Based Header Protection: +* +* header_protection(hp_key, sample): +* mask = AES-ECB(hp_key, sample) +* +* rfc9001#section-5.4.4: ChaCha20-Based Header Protection: +* +* header_protection(hp_key, sample): +* counter = sample[0..3] +* nonce = sample[4..15] +* mask = ChaCha20(hp_key, counter, nonce, {0,0,0,0,0}) +*/ +memcpy ((chacha ? iv : mask), skb->data + cb->number_offset +QUIC_PN_MAX_LEN , +QUIC_SAMPLE_LEN ); +sg_init_one (&sg, mask,QUIC_SAMPLE_LEN ); +skcipher_request_set_tfm (req, tfm); +skcipher_request_set_crypt (req, &sg, &sg,QUIC_SAMPLE_LEN , iv); + err =crypto_skcipher_encrypt (req); +if (err) + goto err; + +/* rfc9001#section-5.4.1: +* +* mask = header_protection(hp_key, sample) +* +* pn_length = (packet[0] & 0x03) + 1 +* if (packet[0] & 0x80) == 0x80: +* # Long header: 4 bits masked +* packet[0] ^= mask[0] & 0x0f +* else: +* # Short header: 5 bits masked +* packet[0] ^= mask[0] & 0x1f +* +* # pn_offset is the start of the Packet Number field. +* packet[pn_offset:pn_offset+pn_length] ^= mask[1:1+pn_length] +*/ + p = skb->data; + *p = (u8)(*p ^ (mask[0 ] & (((*p &QUIC_HEADER_FORM_BIT ) ==QUIC_HEADER_FORM_BIT ) ? +QUIC_LONG_HEADER_MASK :QUIC_SHORT_HEADER_MASK ))); + p = skb->data + cb->number_offset; +for (i =1 ; i <= cb->number_len; i++) + *p++ ^= mask[i]; +err: +kfree_sensitive (mask); +return err; +} + +/* Extracts and reconstructs the packet number from an incoming QUIC packet. */ +static int quic_crypto_get_number (struct sk_buff *skb) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +s64 number_max = cb->number; +u32 len = cb->length; +u8 *p; + +/* rfc9000#section-17.1: +* +* Once header protection is removed, the packet number is decoded by finding the packet +* number value that is closest to the next expected packet. The next expected packet is +* the highest received packet number plus one. +*/ + p = (u8 *)quic_hdr (skb) + cb->number_offset; +if (!quic_get_int (&p, &len, &cb->number, cb->number_len)) +return -EINVAL ; + cb->number =quic_get_num (number_max, cb->number, cb->number_len); +return 0 ; +} + +static int quic_crypto_header_decrypt (struct crypto_skcipher *tfm,struct sk_buff *skb,bool chacha) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +struct quichdr *hdr =quic_hdr (skb); +struct skcipher_request *req; +struct scatterlist sg; +u8 *mask, *iv, *p; +int err, i; + + mask =quic_crypto_skcipher_mem_alloc (tfm,QUIC_SAMPLE_LEN , &iv, &req); +if (!mask) +return -ENOMEM ; + +if (cb->length <QUIC_PN_MAX_LEN +QUIC_SAMPLE_LEN ) { + err = -EINVAL ; + goto err; + } + +/* Similar logic to quic_crypto_header_encrypt(). */ + p = (u8 *)hdr + cb->number_offset; +memcpy ((chacha ? iv : mask), p +QUIC_PN_MAX_LEN ,QUIC_SAMPLE_LEN ); +sg_init_one (&sg, mask,QUIC_SAMPLE_LEN ); +skcipher_request_set_tfm (req, tfm); +skcipher_request_set_crypt (req, &sg, &sg,QUIC_SAMPLE_LEN , iv); + err =crypto_skcipher_encrypt (req); +if (err) + goto err; + + p = (u8 *)hdr; + *p = (u8)(*p ^ (mask[0 ] & (((*p &QUIC_HEADER_FORM_BIT ) ==QUIC_HEADER_FORM_BIT ) ? +QUIC_LONG_HEADER_MASK :QUIC_SHORT_HEADER_MASK ))); + cb->number_len = hdr->pnl +1 ; + cb->key_phase = hdr->key; + p += cb->number_offset; +for (i =0 ; i < cb->number_len; ++i) + *(p + i) = *((u8 *)hdr + cb->number_offset + i) ^ mask[i +1 ]; + err =quic_crypto_get_number (skb); + +err: +kfree_sensitive (mask); +return err; +} + +static void *quic_crypto_aead_mem_alloc (struct crypto_aead *tfm,u32 ctx_size, +u8 **iv,struct aead_request **req, +struct scatterlist **sg,u32 nsg) +{ +unsigned int iv_size, req_size; +unsigned int len; +u8 *mem; + + iv_size =crypto_aead_ivsize (tfm); + req_size =sizeof (**req) +crypto_aead_reqsize (tfm); + + len = ctx_size; + len += iv_size; + len +=crypto_aead_alignmask (tfm) & ~(crypto_tfm_ctx_alignment () -1 ); + len =ALIGN (len,crypto_tfm_ctx_alignment ()); + len += req_size; + len =ALIGN (len, __alignof__(struct scatterlist )); + len += nsg *sizeof (**sg); + + mem =kzalloc (len,GFP_ATOMIC ); +if (!mem) +return NULL ; + + *iv = (u8 *)PTR_ALIGN (mem + ctx_size,crypto_aead_alignmask (tfm) +1 ); + *req = (struct aead_request *)PTR_ALIGN (*iv + iv_size, +crypto_tfm_ctx_alignment ()); + *sg = (struct scatterlist *)PTR_ALIGN ((u8 *)*req + req_size, + __alignof__(struct scatterlist )); + +return (void *)mem; +} + +static void quic_crypto_done (void *data,int err) +{ +struct sk_buff *skb = data; + +kfree_sensitive (QUIC_SKB_CB (skb)->crypto_ctx); +QUIC_SKB_CB (skb)->crypto_done (skb, err); +} + +/* AEAD Usage. */ +static int quic_crypto_payload_encrypt (struct crypto_aead *tfm,struct sk_buff *skb, +u8 *tx_iv,bool ccm) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +struct quichdr *hdr =quic_hdr (skb); +u8 *iv, i, nonce[QUIC_IV_LEN ]; +struct aead_request *req; +struct sk_buff *trailer; +struct scatterlist *sg; +u32 nsg, hlen, len; +void *ctx; +__be64 n; +int err; + + len = skb->len; + err =skb_cow_data (skb,QUIC_TAG_LEN , &trailer); +if (err <0 ) +return err; + nsg = (u32 )err; +pskb_put (skb, trailer,QUIC_TAG_LEN ); + hdr->key = cb->key_phase; + + ctx =quic_crypto_aead_mem_alloc (tfm,0 , &iv, &req, &sg, nsg); +if (!ctx) +return -ENOMEM ; + +sg_init_table (sg, nsg); + err =skb_to_sgvec (skb, sg,0 , (int )skb->len); +if (err <0 ) + goto err; + +/* rfc9001#section-5.3: +* +* The associated data, A, for the AEAD is the contents of the QUIC header, +* starting from the first byte of either the short or long header, up to and +* including the unprotected packet number. +* +* The nonce, N, is formed by combining the packet protection IV with the packet +* number. The 62 bits of the reconstructed QUIC packet number in network byte +* order are left-padded with zeros to the size of the IV. The exclusive OR of the +* padded packet number and the IV forms the AEAD nonce. +*/ + hlen = cb->number_offset + cb->number_len; +memcpy (nonce, tx_iv,QUIC_IV_LEN ); + n =cpu_to_be64 (cb->number); +for (i =0 ; i <sizeof (n); i++) + nonce[QUIC_IV_LEN -sizeof (n) + i] ^= ((u8 *)&n)[i]; + +/* For CCM based ciphers, first byte of IV is a constant. */ + iv[0 ] =TLS_AES_CCM_IV_B0_BYTE ; +memcpy (&iv[ccm], nonce,QUIC_IV_LEN ); +aead_request_set_tfm (req, tfm); +aead_request_set_ad (req, hlen); +aead_request_set_crypt (req, sg, sg, len - hlen, iv); +aead_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , quic_crypto_done, skb); + + cb->crypto_ctx = ctx;/* Set crypto_ctx for async free in quic_crypto_done(). */ + err =crypto_aead_encrypt (req); +if (err == -EINPROGRESS ) { +memzero_explicit (nonce,sizeof (nonce)); +return err; + } + +err: +kfree_sensitive (ctx); +memzero_explicit (nonce,sizeof (nonce)); +return err; +} + +static int quic_crypto_payload_decrypt (struct crypto_aead *tfm,struct sk_buff *skb, +u8 *rx_iv,bool ccm) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +u8 *iv, i, nonce[QUIC_IV_LEN ]; +struct aead_request *req; +struct sk_buff *trailer; +int nsg, hlen, len, err; +struct scatterlist *sg; +void *ctx; +__be64 n; + + len = cb->length + cb->number_offset; + hlen = cb->number_offset + cb->number_len; +if (len - hlen <QUIC_TAG_LEN ) +return -EINVAL ; + nsg =skb_cow_data (skb,0 , &trailer); +if (nsg <0 ) +return nsg; + ctx =quic_crypto_aead_mem_alloc (tfm,0 , &iv, &req, &sg, nsg); +if (!ctx) +return -ENOMEM ; + +sg_init_table (sg, nsg); + err =skb_to_sgvec (skb, sg,0 , len); +if (err <0 ) + goto err; + +/* Similar logic to quic_crypto_payload_encrypt(). */ +memcpy (nonce, rx_iv,QUIC_IV_LEN ); + n =cpu_to_be64 (cb->number); +for (i =0 ; i <sizeof (n); i++) + nonce[QUIC_IV_LEN -sizeof (n) + i] ^= ((u8 *)&n)[i]; + + iv[0 ] =TLS_AES_CCM_IV_B0_BYTE ; +memcpy (&iv[ccm], nonce,QUIC_IV_LEN ); +aead_request_set_tfm (req, tfm); +aead_request_set_ad (req, hlen); +aead_request_set_crypt (req, sg, sg, len - hlen, iv); +aead_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , quic_crypto_done, skb); + + cb->crypto_ctx = ctx; + err =crypto_aead_decrypt (req); +if (err == -EINPROGRESS ) { +memzero_explicit (nonce,sizeof (nonce)); +return err; + } +err: +kfree_sensitive (ctx); +memzero_explicit (nonce,sizeof (nonce)); +return err; +} +#define QUIC_CIPHER_MIN TLS_CIPHER_AES_GCM_128#define QUIC_CIPHER_MAX TLS_CIPHER_CHACHA20_POLY1305
@@ -231,6 +558,137 @@ static struct quic_cipher ciphers[QUIC_CIPHER_MAX + 1 - QUIC_CIPHER_MIN] = { "rfc7539(chacha20,poly1305)", "chacha20", "hmac(sha256)"), }; +static bool quic_crypto_is_cipher_ccm (struct quic_crypto *crypto) +{ +return crypto->cipher_type ==TLS_CIPHER_AES_CCM_128 ; +} + +static bool quic_crypto_is_cipher_chacha (struct quic_crypto *crypto) +{ +return crypto->cipher_type ==TLS_CIPHER_CHACHA20_POLY1305 ; +} + +/* Encrypts a QUIC packet before transmission. This function performs AEAD encryption of +* the packet payload and applies header protection. It handles key phase tracking and key +* update timing.. +* +* Return: 0 on success, or a negative error code. +*/ +int quic_crypto_encrypt (struct quic_crypto *crypto,struct sk_buff *skb) +{ +u8 *iv, cha, ccm, phase = crypto->key_phase; +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +int err; + + cb->key_phase = phase; + iv = crypto->tx_iv[phase]; +/* Packet payload is already encrypted (e.g., resumed from async), proceed to header +* protection only. +*/ +if (cb->resume) + goto out; + +/* If a key update is pending and this is the first packet using the new key, save the +* current time. Later used to clear old keys after some time has passed (see +* quic_crypto_decrypt()). +*/ +if (crypto->key_pending && !crypto->key_update_send_time) + crypto->key_update_send_time =quic_ktime_get_us (); + + ccm =quic_crypto_is_cipher_ccm (crypto); + err =quic_crypto_payload_encrypt (crypto->tx_tfm[phase], skb, iv, ccm); +if (err) +return err; +out: + cha =quic_crypto_is_cipher_chacha (crypto); +return quic_crypto_header_encrypt (crypto->tx_hp_tfm, skb, cha); +} + +/* Decrypts a QUIC packet after reception. This function removes header protection, +* decrypts the payload, and processes any key updates if the key phase bit changes. +* +* Return: 0 on success, or a negative error code. +*/ +int quic_crypto_decrypt (struct quic_crypto *crypto,struct sk_buff *skb) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +u8 *iv, cha, ccm, phase; +int err =0 ; +u64 time; + +/* Payload was decrypted asynchronously. Proceed with parsing packet number and key +* phase. +*/ +if (cb->resume) { + err =quic_crypto_get_number (skb); +if (err) +return err; + goto out; + } +if (!cb->number_len) {/* Packet header not yet decrypted. */ + cha =quic_crypto_is_cipher_chacha (crypto); + err =quic_crypto_header_decrypt (crypto->rx_hp_tfm, skb, cha); +if (err) { +pr_debug ("%s: hd decrypt err %d\n" , __func__, err); +return err; + } + } + +/* rfc9001#section-6: +* +* The Key Phase bit allows a recipient to detect a change in keying material without +* needing to receive the first packet that triggered the change. An endpoint that +* notices a changed Key Phase bit updates keys and decrypts the packet that contains +* the changed value. +*/ +if (cb->key_phase != crypto->key_phase && !crypto->key_pending) { +if (!crypto->send_ready)/* Not ready for key update. */ +return -EINVAL ; +if (!cb->backlog)/* Key update must be done in process context. */ +return -EKEYREVOKED ; + err =quic_crypto_key_update (crypto);/* Perform a key update. */ +if (err) { + cb->errcode =QUIC_TRANSPORT_ERROR_KEY_UPDATE ; +return err; + } + cb->key_update =1 ;/* Mark packet as triggering key update. */ + } + + phase = cb->key_phase; + iv = crypto->rx_iv[phase]; + ccm =quic_crypto_is_cipher_ccm (crypto); + err =quic_crypto_payload_decrypt (crypto->rx_tfm[phase], skb, iv, ccm); +if (err) { +if (err == -EINPROGRESS ) +return err; +/* When using the old keys can not decrypt the packets, the peer might +* start another key_update. Thus, clear the last key_pending so that +* next packets will trigger the new key-update. +*/ +if (crypto->key_pending && cb->key_phase != crypto->key_phase) { + crypto->key_pending =0 ; + crypto->key_update_time =0 ; + } +return err; + } + +out: +/* rfc9001#section-6.1: +* +* An endpoint MUST retain old keys until it has successfully unprotected a +* packet sent using the new keys. An endpoint SHOULD retain old keys for +* some time after unprotecting a packet sent using the new keys. +*/ +if (crypto->key_pending && cb->key_phase == crypto->key_phase) { + time = crypto->key_update_send_time; +if (time &&quic_ktime_get_us () - time >= crypto->key_update_time) { + crypto->key_pending =0 ; + crypto->key_update_time =0 ; + } + } +return err; +} +int quic_crypto_set_cipher (struct quic_crypto *crypto,u32 type,u8 flag) {struct quic_cipher *cipher;
@@ -516,6 +974,210 @@ int quic_crypto_initial_keys_install(struct quic_crypto *crypto, struct quic_conreturn err; } +#define QUIC_RETRY_KEY_V1 "\xbe\x0c\x69\x0b\x9f\x66\x57\x5a\x1d\x76\x6b\x54\xe3\x68\xc8\x4e" +#define QUIC_RETRY_KEY_V2 "\x8f\xb4\xb0\x1b\x56\xac\x48\xe2\x60\xfb\xcb\xce\xad\x7c\xcc\x92" + +#define QUIC_RETRY_NONCE_V1 "\x46\x15\x99\xd3\x5d\x63\x2b\xf2\x23\x98\x25\xbb" +#define QUIC_RETRY_NONCE_V2 "\xd8\x69\x69\xbc\x2d\x7c\x6d\x99\x90\xef\xb0\x4a" + +/* Retry Packet Integrity. */ +int quic_crypto_get_retry_tag (struct quic_crypto *crypto,struct sk_buff *skb, +struct quic_conn_id *odcid,u32 version,u8 *tag) +{ +struct crypto_aead *tfm = crypto->tag_tfm; +u8 *pseudo_retry, *p, *iv, *key; +struct aead_request *req; +struct scatterlist *sg; +u32 plen; +int err; + +/* rfc9001#section-5.8: +* +* The Retry Integrity Tag is a 128-bit field that is computed as the output of +* AEAD_AES_128_GCM used with the following inputs: +* +* - The secret key, K, is 128 bits equal to 0xbe0c690b9f66575a1d766b54e368c84e. +* - The nonce, N, is 96 bits equal to 0x461599d35d632bf2239825bb. +* - The plaintext, P, is empty. +* - The associated data, A, is the contents of the Retry Pseudo-Packet, +* +* The Retry Pseudo-Packet is not sent over the wire. It is computed by taking the +* transmitted Retry packet, removing the Retry Integrity Tag, and prepending the +* two following fields: ODCID Length + Original Destination Connection ID (ODCID). +*/ + err =crypto_aead_setauthsize (tfm,QUIC_TAG_LEN ); +if (err) +return err; + key =QUIC_RETRY_KEY_V1 ; +if (version ==QUIC_VERSION_V2 ) + key =QUIC_RETRY_KEY_V2 ; + err =crypto_aead_setkey (tfm, key,TLS_CIPHER_AES_GCM_128_KEY_SIZE ); +if (err) +return err; + + plen =1 + odcid->len + skb->len -QUIC_TAG_LEN ; + pseudo_retry =quic_crypto_aead_mem_alloc (tfm, plen +QUIC_TAG_LEN , &iv, &req, &sg,1 ); +if (!pseudo_retry) +return -ENOMEM ; + + p = pseudo_retry; + p =quic_put_int (p, odcid->len,1 ); + p =quic_put_data (p, odcid->data, odcid->len); + p =quic_put_data (p, skb->data, skb->len -QUIC_TAG_LEN ); +sg_init_one (sg, pseudo_retry, plen +QUIC_TAG_LEN ); + +memcpy (iv,QUIC_RETRY_NONCE_V1 ,QUIC_IV_LEN ); +if (version ==QUIC_VERSION_V2 ) +memcpy (iv,QUIC_RETRY_NONCE_V2 ,QUIC_IV_LEN ); +aead_request_set_tfm (req, tfm); +aead_request_set_ad (req, plen); +aead_request_set_crypt (req, sg, sg,0 , iv); + err =crypto_aead_encrypt (req); +if (!err) +memcpy (tag, p,QUIC_TAG_LEN ); +kfree_sensitive (pseudo_retry); +return err; +} + +/* Generate a token for Retry or address validation. +* +* Builds a token with the format: [client address][timestamp][original DCID][auth tag] +* +* Encrypts the token (excluding the first flag byte) using AES-GCM with a key and IV +* derived via HKDF. The original DCID is stored to be recovered later from a Client +* Initial packet. Ensures the token is bound to the client address and time, preventing +* reuse or tampering. +* +* Returns 0 on success or a negative error code on failure. +*/ +int quic_crypto_generate_token (struct quic_crypto *crypto,void *addr,u32 addrlen, +struct quic_conn_id *conn_id,u8 *token,u32 *tlen) +{ +u8 key[TLS_CIPHER_AES_GCM_128_KEY_SIZE ], iv[QUIC_IV_LEN ]; +struct crypto_aead *tfm = crypto->tag_tfm; +u8 *retry_token =NULL , *tx_iv, *p; +struct quic_data srt = {}, k, i; +u64 ts =quic_ktime_get_us (); +struct aead_request *req; +struct scatterlist *sg; +int err, len; + +quic_data (&srt, quic_random_data,QUIC_RANDOM_DATA_LEN ); +quic_data (&k, key,TLS_CIPHER_AES_GCM_128_KEY_SIZE ); +quic_data (&i, iv,QUIC_IV_LEN ); + err =quic_crypto_keys_derive (crypto->secret_tfm, &srt, &k, &i,NULL ,QUIC_VERSION_V1 ); +if (err) + goto out; + err =crypto_aead_setauthsize (tfm,QUIC_TAG_LEN ); +if (err) + goto out; + err =crypto_aead_setkey (tfm, key,TLS_CIPHER_AES_GCM_128_KEY_SIZE ); +if (err) + goto out; + token++; + len = addrlen +sizeof (ts) + conn_id->len +QUIC_TAG_LEN ; + retry_token =quic_crypto_aead_mem_alloc (tfm, len, &tx_iv, &req, &sg,1 ); +if (!retry_token) { + err = -ENOMEM ; + goto out; + } + + p = retry_token; + p =quic_put_data (p, addr, addrlen); + p =quic_put_int (p, ts,sizeof (ts)); +quic_put_data (p, conn_id->data, conn_id->len); +sg_init_one (sg, retry_token, len); +aead_request_set_tfm (req, tfm); +aead_request_set_ad (req, addrlen); +aead_request_set_crypt (req, sg, sg, len - addrlen -QUIC_TAG_LEN , iv); + err =crypto_aead_encrypt (req); +if (err) + goto out; +memcpy (token, retry_token, len); + *tlen = len +1 ; +out: +kfree_sensitive (retry_token); +memzero_explicit (key,sizeof (key)); +memzero_explicit (iv,sizeof (iv)); +return err; +} + +/* Validate a Retry or address validation token. +* +* Decrypts the token using derived key and IV. Checks that the decrypted address matches +* the provided address, validates the embedded timestamp against current time with a +* version-specific timeout. If applicable, it extracts and returns the original +* destination connection ID (ODCID) for Retry packets. +* +* Returns 0 if the token is valid, -EINVAL if invalid, or another negative error code. +*/ +int quic_crypto_verify_token (struct quic_crypto *crypto,void *addr,u32 addrlen, +struct quic_conn_id *conn_id,u8 *token,u32 len) +{ +u64 t, ts =quic_ktime_get_us (), timeout =QUIC_TOKEN_TIMEOUT_RETRY ; +u8 key[TLS_CIPHER_AES_GCM_128_KEY_SIZE ], iv[QUIC_IV_LEN ]; +u8 *retry_token =NULL , *rx_iv, *p, flag = *token; +struct crypto_aead *tfm = crypto->tag_tfm; +struct quic_data srt = {}, k, i; +struct aead_request *req; +struct scatterlist *sg; +int err; + +if (len <sizeof (flag) + addrlen +sizeof (ts) +QUIC_TAG_LEN ) +return -EINVAL ; +quic_data (&srt, quic_random_data,QUIC_RANDOM_DATA_LEN ); +quic_data (&k, key,TLS_CIPHER_AES_GCM_128_KEY_SIZE ); +quic_data (&i, iv,QUIC_IV_LEN ); + err =quic_crypto_keys_derive (crypto->secret_tfm, &srt, &k, &i,NULL ,QUIC_VERSION_V1 ); +if (err) + goto out; + err =crypto_aead_setauthsize (tfm,QUIC_TAG_LEN ); +if (err) + goto out; + err =crypto_aead_setkey (tfm, key,TLS_CIPHER_AES_GCM_128_KEY_SIZE ); +if (err) + goto out; + len--; + token++; + retry_token =quic_crypto_aead_mem_alloc (tfm, len, &rx_iv, &req, &sg,1 ); +if (!retry_token) { + err = -ENOMEM ; + goto out; + } + +memcpy (retry_token, token, len); +sg_init_one (sg, retry_token, len); +aead_request_set_tfm (req, tfm); +aead_request_set_ad (req, addrlen); +aead_request_set_crypt (req, sg, sg, len - addrlen, iv); + err =crypto_aead_decrypt (req); +if (err) + goto out; + + err = -EINVAL ; + p = retry_token; +if (memcmp (p, addr, addrlen)) + goto out; + p += addrlen; + len -= addrlen; +if (flag ==QUIC_TOKEN_FLAG_REGULAR ) + timeout =QUIC_TOKEN_TIMEOUT_REGULAR ; +if (!quic_get_int (&p, &len, &t,sizeof (ts)) || t + timeout < ts) + goto out; + len -=QUIC_TAG_LEN ; +if (len >QUIC_CONN_ID_MAX_LEN ) + goto out; + +if (flag ==QUIC_TOKEN_FLAG_RETRY ) +quic_conn_id_update (conn_id, p, len); + err =0 ; +out: +kfree_sensitive (retry_token); +memzero_explicit (key,sizeof (key)); +memzero_explicit (iv,sizeof (iv)); +return err; +} +/* Generate a derived key using HKDF-Extract and HKDF-Expand with a given label. */ static int quic_crypto_generate_key (struct quic_crypto *crypto,void *data,u32 len,char *label,u8 *token,u32 key_len)
net/quic/crypto.h +10 -0
@@ -62,6 +62,9 @@ int quic_crypto_get_secret(struct quic_crypto *crypto, struct quic_crypto_secretint quic_crypto_set_cipher (struct quic_crypto *crypto,u32 type,u8 flag);int quic_crypto_key_update (struct quic_crypto *crypto); +int quic_crypto_encrypt (struct quic_crypto *crypto,struct sk_buff *skb); +int quic_crypto_decrypt (struct quic_crypto *crypto,struct sk_buff *skb); +int quic_crypto_initial_keys_install (struct quic_crypto *crypto,struct quic_conn_id *conn_id,u32 version,bool is_serv); int quic_crypto_generate_session_ticket_key(struct quic_crypto *crypto, void *data,
@@ -69,5 +72,12 @@ int quic_crypto_generate_session_ticket_key(struct quic_crypto *crypto, void *daint quic_crypto_generate_stateless_reset_token (struct quic_crypto *crypto,void *data,u32 len,u8 *key,u32 key_len); +int quic_crypto_generate_token (struct quic_crypto *crypto,void *addr,u32 addrlen, +struct quic_conn_id *conn_id,u8 *token,u32 *tlen); +int quic_crypto_get_retry_tag (struct quic_crypto *crypto,struct sk_buff *skb, +struct quic_conn_id *odcid,u32 version,u8 *tag); +int quic_crypto_verify_token (struct quic_crypto *crypto,void *addr,u32 addrlen, +struct quic_conn_id *conn_id,u8 *token,u32 len); +void quic_crypto_free (struct quic_crypto *crypto);void quic_crypto_init (void );
Signature · 1 line
2.47.1
PATCH v7 13/16 quic: add timer management
In reply to Xin LongThis patch introduces 'quic_timer' to unify and manage the five main timers used in QUIC: loss detection, delayed ACK, path validation, PMTU probing, and pacing. These timers are critical for driving retransmissions, connection liveness, and flow control.
Each timer type is initialized, started, reset, or stopped using a common set of operations.
- quic_timer_reset(): Reset a timer with type and timeout
- quic_timer_start(): Start a timer with type and timeout
- quic_timer_stop(): Stop a timer with type
Although handler functions for each timer are defined, they are currently placeholders; their logic will be implemented in upcoming patches for packet transmission and outqueue handling.
Deferred timer actions are also integrated through quic_release_cb(), which dispatches to the appropriate handler when timers expire.
---
v5:
- Rename QUIC_TSQ_DEFERRED to QUIC_PACE_DEFERRED.
--- net/quic/Makefile | 2 +- net/quic/socket.c | 33 ++++++++ net/quic/socket.h | 33 ++++++++ net/quic/timer.c | 196 ++++++++++++++++++++++++++++++++++++++++++++++ net/quic/timer.h | 47 +++++++++++ 5 files changed, 310 insertions(+), 1 deletion(-) create mode 100644 net/quic/timer.c create mode 100644 net/quic/timer.h
net/quic/Makefile +1 -1
@@ -6,4 +6,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o quic-y := common.o family.o protocol.o socket.o stream.o connid.o path.o \ - cong.o pnspace.o crypto.o + cong.o pnspace.o crypto.o timer.o
net/quic/socket.c +33 -0
@@ -47,6 +47,8 @@ static int quic_init_sock(struct sock *sk)quic_conn_id_set_init (quic_dest (sk),0 );quic_cong_init (quic_cong (sk)); +quic_timer_init (sk); +if (quic_stream_init (quic_streams (sk)))return -ENOMEM ;
@@ -68,6 +70,8 @@ static void quic_destroy_sock(struct sock *sk) {u8 i; +quic_timer_free (sk); +for (i =0 ; i <QUIC_PNSPACE_MAX ; i++)quic_pnspace_free (quic_pnspace (sk, i));for (i =0 ; i <QUIC_CRYPTO_MAX ; i++)
@@ -194,6 +198,35 @@ static int quic_getsockopt(struct sock *sk, int level, int optname,static void quic_release_cb (struct sock *sk) { +/* Similar to tcp_release_cb(). */ +unsigned long nflags, flags =smp_load_acquire (&sk->sk_tsq_flags); + +do { +if (!(flags &QUIC_DEFERRED_ALL )) +return ; + nflags = flags & ~QUIC_DEFERRED_ALL ; + }while (!try_cmpxchg (&sk->sk_tsq_flags, &flags, nflags)); + +if (flags &QUIC_F_LOSS_DEFERRED ) { +quic_timer_loss_handler (sk); +__sock_put (sk); + } +if (flags &QUIC_F_SACK_DEFERRED ) { +quic_timer_sack_handler (sk); +__sock_put (sk); + } +if (flags &QUIC_F_PATH_DEFERRED ) { +quic_timer_path_handler (sk); +__sock_put (sk); + } +if (flags &QUIC_F_PMTU_DEFERRED ) { +quic_timer_pmtu_handler (sk); +__sock_put (sk); + } +if (flags &QUIC_F_PACE_DEFERRED ) { +quic_timer_pace_handler (sk); +__sock_put (sk); + } }static int quic_disconnect (struct sock *sk,int flags)
net/quic/socket.h +33 -0
@@ -21,6 +21,7 @@#include "cong.h" #include "protocol.h" +#include "timer.h" extern struct proto quic_prot;extern struct proto quicv6_prot;
@@ -32,6 +33,31 @@ enum quic_state { QUIC_SS_ESTABLISHED = TCP_ESTABLISHED, }; +enum quic_tsq_enum { +QUIC_MTU_REDUCED_DEFERRED , +QUIC_LOSS_DEFERRED , +QUIC_SACK_DEFERRED , +QUIC_PATH_DEFERRED , +QUIC_PMTU_DEFERRED , +QUIC_PACE_DEFERRED , +}; + +enum quic_tsq_flags { +QUIC_F_MTU_REDUCED_DEFERRED =BIT (QUIC_MTU_REDUCED_DEFERRED ), +QUIC_F_LOSS_DEFERRED =BIT (QUIC_LOSS_DEFERRED ), +QUIC_F_SACK_DEFERRED =BIT (QUIC_SACK_DEFERRED ), +QUIC_F_PATH_DEFERRED =BIT (QUIC_PATH_DEFERRED ), +QUIC_F_PMTU_DEFERRED =BIT (QUIC_PMTU_DEFERRED ), +QUIC_F_PACE_DEFERRED =BIT (QUIC_PACE_DEFERRED ), +}; + +#define QUIC_DEFERRED_ALL (QUIC_F_MTU_REDUCED_DEFERRED | \ + QUIC_F_LOSS_DEFERRED | \ + QUIC_F_SACK_DEFERRED | \ + QUIC_F_PATH_DEFERRED | \ + QUIC_F_PMTU_DEFERRED | \ + QUIC_F_PACE_DEFERRED) +struct quic_sock {struct inet_sock inet;struct list_head reqs;
@@ -48,6 +74,8 @@ struct quic_sock {struct quic_cong cong;struct quic_pnspace space[QUIC_PNSPACE_MAX ];struct quic_crypto crypto[QUIC_CRYPTO_MAX ]; + +struct quic_timer timers[QUIC_TIMER_MAX ]; };struct quic6_sock {
@@ -125,6 +153,11 @@ static inline struct quic_crypto *quic_crypto(const struct sock *sk, u8 level)return &quic_sk (sk)->crypto[level]; } +static inline void *quic_timer (const struct sock *sk,u8 type) +{ +return (void *)&quic_sk (sk)->timers[type]; +} +static inline bool quic_is_establishing (struct sock *sk) {return sk->sk_state ==QUIC_SS_ESTABLISHING ;
net/quic/timer.c new file +196 -0
196 lines not on this page · raw message
net/quic/timer.h new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v7 14/16 quic: add frame encoder and decoder base
In reply to Xin LongThis patch introduces 'quic_frame' to represent QUIC frames and 'quic_frame_ops' to define the associated operations for encoding, processing, and acknowledgment.
This abstraction sets the foundation for flexible and modular frame handling. While core operations are defined, actual implementation will follow in subsequent patches once packet handling and inqueue/outqueue infrastructure are in place.
The patch introduces hooks for invoking frame-specific logic:
- quic_frame_create(): Invoke the .create operation of the frame.
- quic_frame_process(): Invoke the .process operation of the frame.
- quic_frame_ack(): Invoke the .ack operation of the frame.
To manage frame lifecycles, reference counting is used, supported by
- quic_frame_get(): Increment the reference count of a frame.
- quic_frame_put(): Decrement the reference count of a frame.
- quic_frame_alloc(): Allocate a frame and set its data.
Frames are allocated through quic_frame_alloc(), and a dedicated kmem_cache (quic_frame_cachep) is added to optimize memory usage.
For STREAM frames, additional data can be appended using
- quic_frame_stream_append(): Append more data to a STREAM frame.
---
v5:
- Use !frame->type to detect RX in quic_frame_free(), and frame->skb shares a union with frame->flist used only on TX. Add a comment for this in quic_frame_free().
--- net/quic/Makefile | 2 +- net/quic/frame.c | 561 ++++++++++++++++++++++++++++++++++++++++++++ net/quic/frame.h | 195 +++++++++++++++ net/quic/protocol.c | 9 + net/quic/protocol.h | 1 + net/quic/socket.h | 2 + 6 files changed, 769 insertions(+), 1 deletion(-) create mode 100644 net/quic/frame.c create mode 100644 net/quic/frame.h
net/quic/Makefile +1 -1
@@ -6,4 +6,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o quic-y := common.o family.o protocol.o socket.o stream.o connid.o path.o \ - cong.o pnspace.o crypto.o timer.o + cong.o pnspace.o crypto.o timer.o frame.o
net/quic/frame.c new file +561 -0
561 lines not on this page · raw message
net/quic/frame.h new file +195 -0
195 lines not on this page · raw message
net/quic/protocol.c +9 -0
@@ -20,6 +20,7 @@static unsigned int quic_net_id__read_mostly ; +struct kmem_cache *quic_frame_cachep__read_mostly ;struct percpu_counter quic_sockets_allocated;struct workqueue_struct *quic_wq;
@@ -342,6 +343,11 @@ static __init int quic_init(void)quic_crypto_init (); + quic_frame_cachep =kmem_cache_create ("quic_frame" ,sizeof (struct quic_frame ), +0 ,SLAB_HWCACHE_ALIGN ,NULL ); +if (!quic_frame_cachep) + goto err; + err =percpu_counter_init (&quic_sockets_allocated,0 ,GFP_KERNEL );if (err) goto err_percpu_counter;
@@ -379,6 +385,8 @@ static __init int quic_init(void) err_hash:percpu_counter_destroy (&quic_sockets_allocated); err_percpu_counter: +kmem_cache_destroy (quic_frame_cachep); +err:return err; }
@@ -393,6 +401,7 @@ static __exit void quic_exit(void)destroy_workqueue (quic_wq);quic_hash_tables_destroy ();percpu_counter_destroy (&quic_sockets_allocated); +kmem_cache_destroy (quic_frame_cachep);pr_info ("quic: exit\n" ); }
net/quic/protocol.h +1 -0
@@ -8,6 +8,7 @@* Xin Long <[email protected]> */ +extern struct kmem_cache *quic_frame_cachep__read_mostly ;extern struct percpu_counter quic_sockets_allocated;extern long sysctl_quic_mem[3 ];
net/quic/socket.h +2 -0
@@ -20,6 +20,8 @@#include "path.h" #include "cong.h" +#include "frame.h" +#include "protocol.h" #include "timer.h"
Signature · 1 line
2.47.1
PATCH v7 15/16 quic: add packet builder base
In reply to Xin LongThis patch introduces 'quic_packet' to handle packing of QUIC packets on the transmit (TX) path.
It provides functionality for frame packing and packet construction. The packet configuration includes setting the path, calculating overhead, and verifying routing. Frames are appended to the packet before it is created with the queued frames.
Once assembled, the packet is encrypted, bundled, and sent out. There is also support to flush the packet when no additional frames remain. Functions to create application (short) and handshake (long) packets are currently placeholders for future implementation.
- quic_packet_config(): Set the path, compute overhead, and verify routing.
- quic_packet_tail(): Append a frame to the packet for transmission.
- quic_packet_create_and_xmit(): Create and send the packet with the queued frames.
- quic_packet_xmit(): Encrypt, bundle, and send out the packet.
- quic_packet_flush(): Send the packet if there's nothing left to bundle.
---
v3:
- Adjust global connection and listen socket hashtable operations based on the new hashtable type.
- Introduce quic_packet_backlog_schedule() to enqueue Initial packets to quic_net.backlog_list and defer their decryption for ALPN demux to quic_packet_backlog_work() on quic_net.work, since quic_crypto_initial_keys_install()/crypto_aead_setkey() must run in process context.
v4:
- Update quic_(listen_)sock_lookup() to support lockless socket lookup using hlist_nulls_node APIs.
- Use quic_wq for QUIC packet backlog processing work.
v5:
- Rename quic_packet_create() to quic_packet_create_and_xmit() (suggested by Paolo).
- Move the packet parser base code to a separate patch, keeping only the packet builder base in this patch (suggested by Paolo).
- Change sent_time timestamp from u32 to u64 to improve accuracy.
--- net/quic/Makefile | 2 +- net/quic/packet.c | 313 ++++++++++++++++++++++++++++++++++++++++++++++ net/quic/packet.h | 121 ++++++++++++++++++ net/quic/socket.c | 1 + net/quic/socket.h | 7 ++ 5 files changed, 443 insertions(+), 1 deletion(-) create mode 100644 net/quic/packet.c create mode 100644 net/quic/packet.h
net/quic/Makefile +1 -1
@@ -6,4 +6,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o quic-y := common.o family.o protocol.o socket.o stream.o connid.o path.o \ - cong.o pnspace.o crypto.o timer.o frame.o + cong.o pnspace.o crypto.o timer.o frame.o packet.o
net/quic/packet.c new file +313 -0
313 lines not on this page · raw message
net/quic/packet.h new file +121 -0
121 lines not on this page · raw message
net/quic/socket.c +1 -0
@@ -48,6 +48,7 @@ static int quic_init_sock(struct sock *sk)quic_cong_init (quic_cong (sk));quic_timer_init (sk); +quic_packet_init (sk);if (quic_stream_init (quic_streams (sk)))return -ENOMEM ;
net/quic/socket.h +7 -0
@@ -20,6 +20,7 @@#include "path.h" #include "cong.h" +#include "packet.h" #include "frame.h" #include "protocol.h"
@@ -77,6 +78,7 @@ struct quic_sock {struct quic_pnspace space[QUIC_PNSPACE_MAX ];struct quic_crypto crypto[QUIC_CRYPTO_MAX ]; +struct quic_packet packet;struct quic_timer timers[QUIC_TIMER_MAX ]; };
@@ -155,6 +157,11 @@ static inline struct quic_crypto *quic_crypto(const struct sock *sk, u8 level)return &quic_sk (sk)->crypto[level]; } +static inline struct quic_packet *quic_packet (const struct sock *sk) +{ +return &quic_sk (sk)->packet; +} +static inline void *quic_timer (const struct sock *sk,u8 type) {return (void *)&quic_sk (sk)->timers[type];
Signature · 1 line
2.47.1
Paolo Abeni
maintainerRe: [PATCH net-next v7 15/16] quic: add packet builder base
net/quic/packet.c line 29
Quoted 9 lines · Xin Long
net/quic/packet.c new file +9
-0+static struct sk_buff *quic_packet_handshake_create (struct sock *sk) +{ +struct quic_packet *packet =quic_packet (sk); +struct quic_frame *frame, *next; + +/* Free all frames for now, and future patches will implement the actual creation logic. */ +list_for_each_entry_safe (frame, next, &packet->frame_list, list) { +list_del (&frame->list); +quic_frame_put (frame);
If you leave this function body empty and do the same for quic_packet_app_create(), you could additionally strip patch 14 from this series and avoid leaving several function defined there as unused.
/P
Re: [PATCH net-next v7 15/16] quic: add packet builder base
Quoted 16 lines · Paolo Abeni
net/quic/packet.c line 29
Quoted 9 lines · Xin Long
net/quic/packet.c new file +9
-0+static struct sk_buff *quic_packet_handshake_create (struct sock *sk) +{ +struct quic_packet *packet =quic_packet (sk); +struct quic_frame *frame, *next; + +/* Free all frames for now, and future patches will implement the actual creation logic. */ +list_for_each_entry_safe (frame, next, &packet->frame_list, list) { +list_del (&frame->list); +quic_frame_put (frame);If you leave this function body empty and do the same for
quic_packet_app_create(), you could additionally strip patch 14 from this series and avoid leaving several function defined there as unused.
I will give it a try, to move patch 14 to patchset-2.
Thanks.
PATCH v7 16/16 quic: add packet parser base
In reply to Xin LongThis patch usess 'quic_packet' to handle packing of QUIC packets on the receive (RX) path.
It introduces mechanisms to parse the ALPN from client Initial packets to determine the correct listener socket. Received packets are then routed and processed accordingly. Similar to the TX path, handling for application and handshake packets is not yet implemented.
- quic_packet_parse_alpn()`: Parse the ALPN from a client Initial packet, then locate the appropriate listener using the ALPN.
- quic_packet_rcv(): Locate the appropriate socket to handle the packet via quic_packet_process().
- quic_packet_process()`: Process the received packet.
In addition to packet flow, this patch adds support for ICMP-based MTU updates by locating the relevant socket and updating the stored PMTU accordingly.
- quic_packet_rcv_err_pmtu(): Find the socket and update the PMTU via quic_packet_mss_update().
---
v5:
- In quic_packet_rcv_err(), remove the unnecessary quic_is_listen() check and move quic_get_mtu_info() out of sock lock (suggested by Paolo).
- Replace cancel_work_sync() to disable_work_sync() (suggested by
Paolo).
v6:
- Fix the loop using skb_dequeue() in quic_packet_backlog_work(), and kfree_skb() when sk is not found (reported by AI Reviews).
- Remove skb_pull() from quic_packet_rcv(), since it is now handled
in quic_path_rcv().
- Note for AI reviews: add if (dst) check in quic_packet_rcv_err_pmtu(), although quic_packet_route() >= 0 already guarantees it is not NULL.
- Note for AI reviews: it is safe to do *plen -= QUIC_HLEN in quic_packet_get_version_and_connid(), since quic_packet_get_sock() already checks if (skb->len < QUIC_HLEN).
- Note for AI reviews: cb->length - cb->number_len - QUIC_TAG_LEN cannot underflow, because quic_crypto_header_decrypt() already checks if (cb->length < QUIC_PN_MAX_LEN + QUIC_SAMPLE_LEN).
- Note for AI reviews: the cast (u16)length in quic_packet_parse_alpn() is safe, as there is a prior check if (length > (u16)len); len is skb->len, which cannot exceed U16_MAX.
- Note for AI reviews: it's correct to do if (flags & QUIC_F_MTU_REDUCED_DEFERRED) in quic_release_cb(), since QUIC_MTU_REDUCED_DEFERRED is the bit used with test_and_set_bit().
- Note for AI reviews: move skb_cb->backlog = 1 before adding skb to backlog, although it's safe to write skb_cb after adding to backlog with sk_lock.slock, as skb dequeue from backlog requires sk_lock.slock.
v7:
- Pass udp sk to quic_packet_rcv(), quic_packet_rcv_err() and
quic_sock_lookup().
- Move the call to skb_linearize() and skb_set_owner_sk_safe() to
.quic_path_rcv()/quic_packet_rcv().
---
net/quic/packet.c | 644 ++++++++++++++++++++++++++++++++++++++++++++
net/quic/packet.h | 9 +
net/quic/protocol.c | 6 +
net/quic/protocol.h | 4 +
net/quic/socket.c | 134 +++++++++
net/quic/socket.h | 5 +
6 files changed, 802 insertions(+)net/quic/packet.c +644 -0
@@ -14,6 +14,650 @@#define QUIC_HLEN 1 +#define QUIC_LONG_HLEN (dcid, scid) \ + (QUIC_HLEN + QUIC_VERSION_LEN + 1 + (dcid)->len + 1 + (scid)->len) + +#define QUIC_VERSION_NUM 2 + +/* Supported QUIC versions and their compatible versions. Used for Compatible Version +* Negotiation in rfc9368#section-2.3. +*/ +static u32 quic_versions[QUIC_VERSION_NUM ][4 ] = { +/* Version, Compatible Versions */ + {QUIC_VERSION_V1 ,QUIC_VERSION_V2 ,QUIC_VERSION_V1 ,0 }, + {QUIC_VERSION_V2 ,QUIC_VERSION_V2 ,QUIC_VERSION_V1 ,0 }, +}; + +/* Get the compatible version list for a given QUIC version. */ +u32 *quic_packet_compatible_versions (u32 version) +{ +u8 i; + +for (i =0 ; i <QUIC_VERSION_NUM ; i++) +if (version == quic_versions[i][0 ]) +return quic_versions[i]; +return NULL ; +} + +/* Convert version-specific type to internal standard packet type. */ +static u8 quic_packet_version_get_type (u32 version,u8 type) +{ +if (version ==QUIC_VERSION_V1 ) +return type; + +switch (type) { +case QUIC_PACKET_INITIAL_V2 : +return QUIC_PACKET_INITIAL ; +case QUIC_PACKET_0RTT_V2 : +return QUIC_PACKET_0RTT ; +case QUIC_PACKET_HANDSHAKE_V2 : +return QUIC_PACKET_HANDSHAKE ; +case QUIC_PACKET_RETRY_V2 : +return QUIC_PACKET_RETRY ; +default : +return -1 ; + } +return -1 ; +} + +/* Parse QUIC version and connection IDs (DCID and SCID) from a Long header packet buffer. */ +static int quic_packet_get_version_and_connid (struct quic_conn_id *dcid,struct quic_conn_id *scid, +u32 *version,u8 **pp,u32 *plen) +{ +u64 len, v; + + *pp +=QUIC_HLEN ; + *plen -=QUIC_HLEN ; + +if (!quic_get_int (pp, plen, &v,QUIC_VERSION_LEN )) +return -EINVAL ; + *version = v; + +if (!quic_get_int (pp, plen, &len,1 ) || + len > *plen || len >QUIC_CONN_ID_MAX_LEN ) +return -EINVAL ; +quic_conn_id_update (dcid, *pp, len); + *plen -= len; + *pp += len; + +if (!quic_get_int (pp, plen, &len,1 ) || + len > *plen || len >QUIC_CONN_ID_MAX_LEN ) +return -EINVAL ; +quic_conn_id_update (scid, *pp, len); + *plen -= len; + *pp += len; +return 0 ; +} + +/* Change the QUIC version for the connection. +* +* Frees existing initial crypto keys and installs new initial keys compatible with the new +* version. +*/ +static int quic_packet_version_change (struct sock *sk,struct quic_conn_id *dcid,u32 version) +{ +struct quic_crypto *crypto =quic_crypto (sk,QUIC_CRYPTO_INITIAL ); + +if (quic_crypto_initial_keys_install (crypto, dcid, version,quic_is_serv (sk))) +return -1 ; + +quic_packet (sk)->version = version; +return 0 ; +} + +/* Select the best compatible QUIC version from offered list. +* +* Considers the local preferred version, currently chosen version, and versions offered by +* the peer. Selects the best compatible version based on client/server role and updates the +* connection version accordingly. +*/ +int quic_packet_select_version (struct sock *sk,u32 *versions,u8 count) +{ +struct quic_packet *packet =quic_packet (sk); +struct quic_config *c =quic_config (sk); +u8 i, pref_found =0 , ch_found =0 ; +u32 preferred, chosen, best =0 ; + + preferred = c->version ?:QUIC_VERSION_V1 ; + chosen = packet->version; + +for (i =0 ; i < count; i++) { +if (!quic_packet_compatible_versions (versions[i])) +continue ; +if (preferred == versions[i]) + pref_found =1 ; +if (chosen == versions[i]) + ch_found =1 ; +if (best < versions[i])/* Track highest offered version. */ + best = versions[i]; + } + +if (!pref_found && !ch_found && !best) +return -1 ; + +if (quic_is_serv (sk)) {/* Server prefers preferred version if offered, else chosen. */ +if (pref_found) + best = preferred; +else if (ch_found) + best = chosen; + }else {/* Client prefers chosen version, else preferred. */ +if (ch_found) + best = chosen; +else if (pref_found) + best = preferred; + } + +if (packet->version == best) +return 0 ; + +/* Change to selected best version. */ +return quic_packet_version_change (sk, &quic_paths (sk)->orig_dcid, best); +} + +/* Extracts a QUIC token from a buffer in the Client Initial packet. */ +static int quic_packet_get_token (struct quic_data *token,u8 **pp,u32 *plen) +{ +u64 len; + +if (!quic_get_var (pp, plen, &len) || len > *plen) +return -EINVAL ; +quic_data (token, *pp, len); + *plen -= len; + *pp += len; +return 0 ; +} + +/* Process PMTU reduction event on a QUIC socket. */ +void quic_packet_rcv_err_pmtu (struct sock *sk) +{ +struct quic_path_group *paths =quic_paths (sk); +struct quic_packet *packet =quic_packet (sk); +struct quic_config *c =quic_config (sk); +u32 pathmtu, info, taglen; +struct dst_entry *dst; +bool reset_timer; + +if (!ip_sk_accept_pmtu (sk)) +return ; + + info =clamp (paths->mtu_info,QUIC_PATH_MIN_PMTU ,QUIC_PATH_MAX_PMTU ); +/* If PLPMTUD is not enabled, update MSS using the route and ICMP info. */ +if (!c->plpmtud_probe_interval) { +if (quic_packet_route (sk) <0 ) +return ; + + dst =__sk_dst_get (sk); +if (dst) + dst->ops->update_pmtu (dst, sk,NULL , info, true); +quic_packet_mss_update (sk, info - packet->hlen); +return ; + } +/* PLPMTUD is enabled: adjust to smaller PMTU, subtract headers and AEAD tag. Also +* notify the QUIC path layer for possible state changes and probing. +*/ + taglen =quic_packet_taglen (packet); + info = info - packet->hlen - taglen; + pathmtu =quic_path_pl_toobig (paths, info, &reset_timer); +if (reset_timer) +quic_timer_reset (sk,QUIC_TIMER_PMTU , c->plpmtud_probe_interval); +if (pathmtu) +quic_packet_mss_update (sk, pathmtu + taglen); +} + +/* Handle ICMP Toobig packet and update QUIC socket path MTU. */ +static int quic_packet_rcv_err (struct sock *sk,struct sk_buff *skb) +{ +union quic_addr daddr, saddr; +u32 info; + +/* All we can do is lookup the matching QUIC socket by addresses. */ +quic_get_msg_addrs (skb, &saddr, &daddr); + sk =quic_sock_lookup (skb, &daddr, &saddr, sk,NULL ); +if (!sk) +return -ENOENT ; + +if (quic_get_mtu_info (skb, &info)) { +sock_put (sk); +return 0 ; + } + +/* Success: update socket path MTU info. */ +bh_lock_sock (sk); +quic_paths (sk)->mtu_info = info; +if (sock_owned_by_user (sk)) { +/* Socket is in use by userspace context. Defer MTU processing to later via +* tasklet. Ensure the socket is not dropped before deferral. +*/ +if (!test_and_set_bit (QUIC_MTU_REDUCED_DEFERRED , &sk->sk_tsq_flags)) +sock_hold (sk); + goto out; + } +/* Otherwise, process the MTU reduction now. */ +quic_packet_rcv_err_pmtu (sk); +out: +bh_unlock_sock (sk); +sock_put (sk); +return 1 ; +} + +#define QUIC_PACKET_BACKLOG_MAX 4096 + +/* Queue a packet for later processing when sleeping is allowed. */ +static int quic_packet_backlog_schedule (struct net *net,struct sk_buff *skb) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +struct quic_net *qn =quic_net (net); + +if (cb->backlog) +return 0 ; + +if (skb_queue_len_lockless (&qn->backlog_list) >=QUIC_PACKET_BACKLOG_MAX ) { +QUIC_INC_STATS (net,QUIC_MIB_PKT_RCVDROP ); +kfree_skb (skb); +return -1 ; + } + + cb->backlog =1 ; +skb_queue_tail (&qn->backlog_list, skb); +queue_work (quic_wq, &qn->work); +return 1 ; +} + +#define TLS_MT_CLIENT_HELLO 1 +#define TLS_EXT_alpn 16 + +/* TLS Client Hello Msg: +* +* uint16 ProtocolVersion; +* opaque Random[32]; +* uint8 CipherSuite[2]; +* +* struct { +* ExtensionType extension_type; +* opaque extension_data<0..2^16-1>; +* } Extension; +* +* struct { +* ProtocolVersion legacy_version = 0x0303; +* Random rand; +* opaque legacy_session_id<0..32>; +* CipherSuite cipher_suites<2..2^16-2>; +* opaque legacy_compression_methods<1..2^8-1>; +* Extension extensions<8..2^16-1>; +* } ClientHello; +*/ + +#define TLS_CH_RANDOM_LEN 32 +#define TLS_CH_VERSION_LEN 2 + +/* Extract ALPN data from a TLS ClientHello message. +* +* Parses the TLS ClientHello handshake message to find the ALPN (Application Layer Protocol +* Negotiation) TLS extension. It validates the TLS ClientHello structure, including version, +* random, session ID, cipher suites, compression methods, and extensions. Once the ALPN +* extension is found, the ALPN protocols list is extracted and stored in @alpn. +* +* Return: 0 on success or no ALPN found, a negative error code on failed parsing. +*/ +static int quic_packet_get_alpn (struct quic_data *alpn,u8 *p,u32 len) +{ +int err = -EINVAL , found =0 ; +u64 length, type; + +/* Verify handshake message type (ClientHello) and its length. */ +if (!quic_get_int (&p, &len, &type,1 ) || type !=TLS_MT_CLIENT_HELLO ) +return err; +if (!quic_get_int (&p, &len, &length,3 ) || + length <TLS_CH_RANDOM_LEN +TLS_CH_VERSION_LEN ) +return err; +if (len > (u32 )length)/* Limit len to handshake message length if larger. */ + len = length; +/* Skip legacy_version (2 bytes) + random (32 bytes). */ + p +=TLS_CH_RANDOM_LEN +TLS_CH_VERSION_LEN ; + len -=TLS_CH_RANDOM_LEN +TLS_CH_VERSION_LEN ; +/* legacy_session_id_len must be zero (QUIC requirement). */ +if (!quic_get_int (&p, &len, &length,1 ) || length) +return err; + +/* Skip cipher_suites (2 bytes length + variable data). */ +if (!quic_get_int (&p, &len, &length,2 ) || length > (u64 )len) +return err; + len -= length; + p += length; + +/* Skip legacy_compression_methods (1 byte length + variable data). */ +if (!quic_get_int (&p, &len, &length,1 ) || length > (u64 )len) +return err; + len -= length; + p += length; + +if (!quic_get_int (&p, &len, &length,2 ))/* Read TLS extensions length (2 bytes). */ +return err; +if (len > (u32 )length)/* Limit len to extensions length if larger. */ + len = length; +while (len >4 ) {/* Iterate over extensions to find ALPN (type TLS_EXT_alpn). */ +if (!quic_get_int (&p, &len, &type,2 )) +break ; +if (!quic_get_int (&p, &len, &length,2 )) +break ; +if (len < (u32 )length)/* Incomplete TLS extensions. */ +return 0 ; +if (type == TLS_EXT_alpn) {/* Found ALPN extension. */ + len = length; + found =1 ; +break ; + } +/* Skip non-ALPN extensions. */ + p += length; + len -= length; + } +if (!found) {/* no ALPN extension found: set alpn->len = 0 and alpn->data = p. */ +quic_data (alpn, p,0 ); +return 0 ; + } + +/* Parse ALPN protocols list length (2 bytes). */ +if (!quic_get_int (&p, &len, &length,2 ) || length > (u64 )len) +return err; +quic_data (alpn, p, length);/* Store ALPN protocols list in alpn->data. */ + len = length; +while (len) {/* Validate ALPN protocols list format. */ +if (!quic_get_int (&p, &len, &length,1 ) || length > (u64 )len) { +/* Malformed ALPN entry: set alpn->len = 0 and alpn->data = NULL. */ +quic_data (alpn,NULL ,0 ); +return err; + } + len -= length; + p += length; + } +pr_debug ("%s: alpn_len: %d\n" , __func__, alpn->len); +return 0 ; +} + +/* Parse ALPN from a QUIC Initial packet. +* +* This function processes a QUIC Initial packet to extract the ALPN from the TLS ClientHello +* message inside the QUIC CRYPTO frame. It verifies packet type, version compatibility, +* decrypts the packet payload, and locates the CRYPTO frame to parse the TLS ClientHello. +* Finally, it calls quic_packet_get_alpn() to extract the ALPN extension data. +* +* Return: 0 on success or no ALPN found, a negative error code on failed parsing. +*/ +static int quic_packet_parse_alpn (struct sk_buff *skb,struct quic_data *alpn) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +struct net *net =sock_net (skb->sk); +u8 *p = skb->data, *data, type; +struct quic_conn_id dcid, scid; +u32 len = skb->len, version; +struct quic_crypto *crypto; +struct quic_data token; +u64 offset, length; +int err = -EINVAL ; + +if (!sysctl_quic_alpn_demux) +return 0 ; +if (quic_packet_get_version_and_connid (&dcid, &scid, &version, &p, &len)) +return err; +if (!quic_packet_compatible_versions (version)) +return 0 ; +/* Only parse Initial packets. */ + type =quic_packet_version_get_type (version,quic_hshdr (skb)->type); +if (type !=QUIC_PACKET_INITIAL ) +return 0 ; +if (quic_packet_get_token (&token, &p, &len)) +return err; +if (!quic_get_var (&p, &len, &length) || length > (u64 )len) +return err; +if (!cb->backlog) {/* skb_get() needed as caller will free skb on this path. */ +quic_packet_backlog_schedule (net,skb_get (skb)); +return err; + } + cb->length = (u16 )length; +/* Copy skb data for restoring in case of decrypt failure. */ + data =kmemdup (skb->data, skb->len,GFP_ATOMIC ); +if (!data) +return -ENOMEM ; + +/* Install initial keys for packet decryption to crypto. */ + crypto = &quic_net (net)->crypto; + err =quic_crypto_initial_keys_install (crypto, &dcid, version,1 ); +if (err) + goto out; + cb->number_offset = (u16)(p - skb->data); + err =quic_crypto_decrypt (crypto, skb); +if (err) { +QUIC_INC_STATS (net,QUIC_MIB_PKT_DECDROP ); +/* Restore original data on decrypt failure. */ +memcpy (skb->data, data, skb->len); + goto out; + } + +QUIC_INC_STATS (net,QUIC_MIB_PKT_DECFASTPATHS ); + cb->resume =1 ;/* Mark this packet as already decrypted. */ + +/* Find the QUIC CRYPTO frame. */ + p += cb->number_len; + len = cb->length - cb->number_len -QUIC_TAG_LEN ; +for (; len && !(*p); p++, len--)/* Skip the padding frame. */ + ; +if (!len-- || *p++ !=QUIC_FRAME_CRYPTO ) + goto out; +if (!quic_get_var (&p, &len, &offset) || offset) + goto out; +if (!quic_get_var (&p, &len, &length) || length > (u64 )len) + goto out; + +/* Parse the TLS CLIENT_HELLO message. */ + err =quic_packet_get_alpn (alpn, p, length); + +out: +kfree (data); +return err; +} + +/* Extract the Destination Connection ID (DCID) from a QUIC Long header packet. */ +int quic_packet_get_dcid (struct quic_conn_id *dcid,struct sk_buff *skb) +{ +u32 plen = skb->len; +u8 *p = skb->data; +u64 len; + +if (plen <QUIC_HLEN +QUIC_VERSION_LEN ) +return -EINVAL ; + plen -= (QUIC_HLEN +QUIC_VERSION_LEN ); + p += (QUIC_HLEN +QUIC_VERSION_LEN ); + +if (!quic_get_int (&p, &plen, &len,1 ) || + len > plen || len >QUIC_CONN_ID_MAX_LEN ) +return -EINVAL ; +quic_conn_id_update (dcid, p, len); +return 0 ; +} + +/* Lookup listening socket for Client Initial packet (in process context). */ +static struct sock *quic_packet_get_listen_sock (struct sk_buff *skb) +{ +union quic_addr daddr, saddr; +struct quic_data alpns = {}; +struct sock *sk; + +quic_get_msg_addrs (skb, &daddr, &saddr); + +if (quic_packet_parse_alpn (skb, &alpns)) +return NULL ; + +local_bh_disable (); + sk =quic_listen_sock_lookup (skb, &daddr, &saddr, &alpns); +local_bh_enable (); + +return sk; +} + +/* Determine the QUIC socket associated with an incoming packet. */ +static struct sock *quic_packet_get_sock (struct sk_buff *skb) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +struct net *net =sock_net (skb->sk); +struct quic_conn_id dcid, *conn_id; +union quic_addr daddr, saddr; +struct quic_data alpns = {}; +struct sock *sk =NULL ; + +if (skb->len <QUIC_HLEN ) +return NULL ; + +if (!quic_hdr (skb)->form) {/* Short header path. */ +if (skb->len <QUIC_HLEN +QUIC_CONN_ID_DEF_LEN ) +return NULL ; +/* Fast path: look up QUIC connection by fixed-length DCID +* (Currently, only source CIDs of size QUIC_CONN_ID_DEF_LEN are used). +*/ + conn_id =quic_conn_id_lookup (net, skb->data +QUIC_HLEN , +QUIC_CONN_ID_DEF_LEN ); +if (conn_id) { + cb->seqno =quic_conn_id_number (conn_id); +return quic_conn_id_sk (conn_id);/* Return associated socket. */ + } + +/* Fallback: listener socket lookup +* (May be used to send a stateless reset from a listen socket). +*/ +quic_get_msg_addrs (skb, &daddr, &saddr); + sk =quic_listen_sock_lookup (skb, &daddr, &saddr, &alpns); +if (sk) +return sk; +/* Final fallback: address-based connection lookup +* (May be used to receive a stateless reset). +*/ +return quic_sock_lookup (skb, &daddr, &saddr, skb->sk,NULL ); + } + +/* Long header path. */ +if (quic_packet_get_dcid (&dcid, skb)) +return NULL ; +/* Fast path: look up QUIC connection by parsed DCID. */ + conn_id =quic_conn_id_lookup (net, dcid.data, dcid.len); +if (conn_id) { + cb->seqno =quic_conn_id_number (conn_id); +return quic_conn_id_sk (conn_id);/* Return associated socket. */ + } + +/* Fallback: address + DCID lookup +* (May be used for 0-RTT or a follow-up Client Initial packet). +*/ +quic_get_msg_addrs (skb, &daddr, &saddr); + sk =quic_sock_lookup (skb, &daddr, &saddr, skb->sk, &dcid); +if (sk) +return sk; +/* Final fallback: listener socket lookup +* (Used for receiving the first Client Initial packet). +*/ +if (quic_packet_parse_alpn (skb, &alpns)) +return NULL ; +return quic_listen_sock_lookup (skb, &daddr, &saddr, &alpns); +} + +/* Entry point for processing received QUIC packets. */ +int quic_packet_rcv (struct sock *sk,struct sk_buff *skb,u8 err) +{ +struct net *net =sock_net (sk); + +if (unlikely (err)) +return quic_packet_rcv_err (sk, skb); + +/* Save the UDP socket to skb->sk for later QUIC socket lookup. */ +if (skb_linearize (skb) || !skb_set_owner_sk_safe (skb, sk)) { +QUIC_INC_STATS (net,QUIC_MIB_PKT_RCVDROP ); + goto err; + } + +/* Look up socket from socket or connection IDs hash tables. */ + sk =quic_packet_get_sock (skb); +if (!sk) + goto err; + +bh_lock_sock (sk); +if (sock_owned_by_user (sk)) { +/* Socket is busy (owned by user context): queue to backlog. */ +QUIC_SKB_CB (skb)->backlog =1 ; +if (sk_add_backlog (sk, skb,READ_ONCE (sk->sk_rcvbuf))) { +QUIC_INC_STATS (net,QUIC_MIB_PKT_RCVDROP ); +bh_unlock_sock (sk); +sock_put (sk); + goto err; + } +QUIC_INC_STATS (net,QUIC_MIB_PKT_RCVBACKLOGS ); + }else { +/* Socket not busy: process immediately. */ +QUIC_INC_STATS (net,QUIC_MIB_PKT_RCVFASTPATHS ); + sk->sk_backlog_rcv (sk, skb);/* quic_packet_process(). */ + } +bh_unlock_sock (sk); +sock_put (sk); +return 0 ; + +err: +kfree_skb (skb); +return -EINVAL ; +} + +static int quic_packet_listen_process (struct sock *sk,struct sk_buff *skb) +{ +kfree_skb (skb); +return -EOPNOTSUPP ; +} + +static int quic_packet_handshake_process (struct sock *sk,struct sk_buff *skb) +{ +kfree_skb (skb); +return -EOPNOTSUPP ; +} + +static int quic_packet_app_process (struct sock *sk,struct sk_buff *skb) +{ +kfree_skb (skb); +return -EOPNOTSUPP ; +} + +int quic_packet_process (struct sock *sk,struct sk_buff *skb) +{ +if (quic_is_closed (sk)) { +kfree_skb (skb); +return 0 ; + } + +if (quic_is_listen (sk)) +return quic_packet_listen_process (sk, skb); + +if (quic_hdr (skb)->form) +return quic_packet_handshake_process (sk, skb); + +return quic_packet_app_process (sk, skb); +} + +/* Work function to process packets in the backlog queue. */ +void quic_packet_backlog_work (struct work_struct *work) +{ + struct quic_net *qn = container_of(work, struct quic_net, work); +struct sk_buff_head *head = &qn->backlog_list; +struct sk_buff *skb; +struct sock *sk; + +while ((skb =skb_dequeue (head)) !=NULL ) { + sk =quic_packet_get_listen_sock (skb); +if (!sk) { +kfree_skb (skb); +continue ; + } + +lock_sock (sk); +quic_packet_process (sk, skb); +release_sock (sk); +sock_put (sk); + } +} +/* Make these fixed for easy coding. */ #define QUIC_PACKET_NUMBER_LEN QUIC_PN_MAX_LEN#define QUIC_PACKET_LENGTH_LEN 4
net/quic/packet.h +9 -0
@@ -110,6 +110,7 @@ static inline void quic_packet_reset(struct quic_packet *packet) }int quic_packet_tail (struct sock *sk,struct quic_frame *frame); +int quic_packet_process (struct sock *sk,struct sk_buff *skb);int quic_packet_config (struct sock *sk,u8 level,u8 path);int quic_packet_xmit (struct sock *sk,struct sk_buff *skb);
@@ -119,3 +120,11 @@ int quic_packet_route(struct sock *sk);void quic_packet_mss_update (struct sock *sk,u32 mss);void quic_packet_flush (struct sock *sk);void quic_packet_init (struct sock *sk); + +int quic_packet_get_dcid (struct quic_conn_id *dcid,struct sk_buff *skb); +int quic_packet_select_version (struct sock *sk,u32 *versions,u8 count); +u32 *quic_packet_compatible_versions (u32 version); + +int quic_packet_rcv (struct sock *sk,struct sk_buff *skb,u8 err); +void quic_packet_backlog_work (struct work_struct *work); +void quic_packet_rcv_err_pmtu (struct sock *sk);
net/quic/protocol.c +6 -0
@@ -274,6 +274,9 @@ static int __net_init quic_net_init(struct net *net)return err; } +INIT_WORK (&qn->work, quic_packet_backlog_work); +skb_queue_head_init (&qn->backlog_list); +#if IS_ENABLED (CONFIG_PROC_FS ) err =quic_net_proc_init (net);if (err) {
@@ -292,6 +295,8 @@ static void __net_exit quic_net_exit(struct net *net)#if IS_ENABLED (CONFIG_PROC_FS )quic_net_proc_exit (net);#endif +skb_queue_purge (&qn->backlog_list); +disable_work_sync (&qn->work);quic_crypto_free (&qn->crypto);free_percpu (qn->stat); qn->stat =NULL ;
@@ -341,6 +346,7 @@ static __init int quic_init(void) sysctl_quic_wmem[1 ] =16 *1024 ; sysctl_quic_wmem[2 ] =max (64 *1024 , max_share); +quic_path_init (quic_packet_rcv);quic_crypto_init (); quic_frame_cachep = kmem_cache_create("quic_frame", sizeof(struct quic_frame),
net/quic/protocol.h +4 -0
@@ -50,6 +50,10 @@ struct quic_net {struct proc_dir_entry *proc_net;/* procfs entry for dumping QUIC socket stats */ #endif struct quic_crypto crypto;/* Context for decrypting Initial packets for ALPN */ + +/* Queue of packets deferred for processing in process context */ +struct sk_buff_head backlog_list; +struct work_struct work;/* Work scheduled to drain and process backlog_list */ };struct quic_net *quic_net (struct net *net);
net/quic/socket.c +134 -0
158 lines not on this page · raw message
net/quic/socket.h +5 -0
@@ -207,3 +207,8 @@ static inline void quic_set_state(struct sock *sk, int state)inet_sk_set_state (sk, state); sk->sk_state_change (sk); } + +struct sock *quic_listen_sock_lookup (struct sk_buff *skb,union quic_addr *sa,union quic_addr *da, +struct quic_data *alpns); +struct sock *quic_sock_lookup (struct sk_buff *skb,union quic_addr *sa,union quic_addr *da, +struct sock *usk,struct quic_conn_id *dcid);
Signature · 1 line
2.47.1
Re: [PATCH net-next v7 16/16] quic: add packet parser base
Am 15.01.26 um 16:11 schrieb Xin Long:
Quoted 448 lines
This patch usess '
quic_packet' to handle packing of QUIC packets on the receive (RX) path.It introduces mechanisms to parse the ALPN from client Initial packets to determine the correct listener socket. Received packets are then routed and processed accordingly. Similar to the TX path, handling for application and handshake packets is not yet implemented.
- quic_packet_parse_alpn()`: Parse the ALPN from a client Initial packet, then locate the appropriate listener using the ALPN.
-
quic_packet_rcv(): Locate the appropriate socket to handle the packet viaquic_packet_process().- quic_packet_process()`: Process the received packet.
In addition to packet flow, this patch adds support for ICMP-based MTU updates by locating the relevant socket and updating the stored PMTU accordingly.
-
quic_packet_rcv_err_pmtu(): Find the socket and update the PMTU viaquic_packet_mss_update().Signed-off-by Xin Long <[email protected]>---
v5:
- Inquic_packet_rcv_err(), remove the unnecessaryquic_is_listen()check and movequic_get_mtu_info()out of sock lock (suggested by Paolo).
- Replacecancel_work_sync()todisable_work_sync()(suggested byPaolo).v6:
- Fix the loop usingskb_dequeue()inquic_packet_backlog_work(), andkfree_skb()when sk is not found (reported by AI Reviews).
- Removeskb_pull()fromquic_packet_rcv(), since it is now handledin quic_path_rcv().- Note for AI reviews: add if (dst) check in
quic_packet_rcv_err_pmtu(), althoughquic_packet_route()>= 0 already guarantees it is not NULL.
- Note for AI reviews: it is safe to do *plen -=QUIC_HLENinquic_packet_get_version_and_connid(), sincequic_packet_get_sock()already checks if (skb->len<QUIC_HLEN).
- Note for AI reviews:cb->length-cb->number_len-QUIC_TAG_LENcannot underflow, becausequic_crypto_header_decrypt()already checks if (cb->length<QUIC_PN_MAX_LEN+QUIC_SAMPLE_LEN).
- Note for AI reviews: the cast (u16)length inquic_packet_parse_alpn()is safe, as there is a prior check if (length > (u16)len); len isskb->len, which cannot exceedU16_MAX.
- Note for AI reviews: it's correct to do if (flags &QUIC_F_MTU_REDUCED_DEFERRED) inquic_release_cb(), sinceQUIC_MTU_REDUCED_DEFERREDis the bit used withtest_and_set_bit().
- Note for AI reviews: moveskb_cb->backlog= 1 before adding skb to backlog, although it's safe to writeskb_cbafter adding to backlog with sk_lock.slock, as skb dequeue from backlog requires sk_lock.slock.
v7:
- Pass udp sk toquic_packet_rcv(),quic_packet_rcv_err()andquic_sock_lookup(). - Move the call to skb_linearize() and skb_set_owner_sk_safe() to .quic_path_rcv()/quic_packet_rcv(). --- net/quic/packet.c | 644 ++++++++++++++++++++++++++++++++++++++++++++ net/quic/packet.h | 9 + net/quic/protocol.c | 6 + net/quic/protocol.h | 4 + net/quic/socket.c | 134 +++++++++ net/quic/socket.h | 5 + 6 files changed, 802 insertions(+)net/quic/packet.c +373
-0@@ -14,6 +14,650 @@#define QUIC_HLEN 1 +#define QUIC_LONG_HLEN (dcid, scid) \ + (QUIC_HLEN + QUIC_VERSION_LEN + 1 + (dcid)->len + 1 + (scid)->len) + +#define QUIC_VERSION_NUM 2 + +/* Supported QUIC versions and their compatible versions. Used for Compatible Version +* Negotiation in rfc9368#section-2.3. +*/ +static u32 quic_versions[QUIC_VERSION_NUM ][4 ] = { +/* Version, Compatible Versions */ + {QUIC_VERSION_V1 ,QUIC_VERSION_V2 ,QUIC_VERSION_V1 ,0 }, + {QUIC_VERSION_V2 ,QUIC_VERSION_V2 ,QUIC_VERSION_V1 ,0 }, +}; + +/* Get the compatible version list for a given QUIC version. */ +u32 *quic_packet_compatible_versions (u32 version) +{ +u8 i; + +for (i =0 ; i <QUIC_VERSION_NUM ; i++) +if (version == quic_versions[i][0 ]) +return quic_versions[i]; +return NULL ; +} + +/* Convert version-specific type to internal standard packet type. */ +static u8 quic_packet_version_get_type (u32 version,u8 type) +{ +if (version ==QUIC_VERSION_V1 ) +return type; + +switch (type) { +case QUIC_PACKET_INITIAL_V2 : +return QUIC_PACKET_INITIAL ; +case QUIC_PACKET_0RTT_V2 : +return QUIC_PACKET_0RTT ; +case QUIC_PACKET_HANDSHAKE_V2 : +return QUIC_PACKET_HANDSHAKE ; +case QUIC_PACKET_RETRY_V2 : +return QUIC_PACKET_RETRY ; +default : +return -1 ; + } +return -1 ; +} + +/* Parse QUIC version and connection IDs (DCID and SCID) from a Long header packet buffer. */ +static int quic_packet_get_version_and_connid (struct quic_conn_id *dcid,struct quic_conn_id *scid, +u32 *version,u8 **pp,u32 *plen) +{ +u64 len, v; + + *pp +=QUIC_HLEN ; + *plen -=QUIC_HLEN ; + +if (!quic_get_int (pp, plen, &v,QUIC_VERSION_LEN )) +return -EINVAL ; + *version = v; + +if (!quic_get_int (pp, plen, &len,1 ) || + len > *plen || len >QUIC_CONN_ID_MAX_LEN ) +return -EINVAL ; +quic_conn_id_update (dcid, *pp, len); + *plen -= len; + *pp += len; + +if (!quic_get_int (pp, plen, &len,1 ) || + len > *plen || len >QUIC_CONN_ID_MAX_LEN ) +return -EINVAL ; +quic_conn_id_update (scid, *pp, len); + *plen -= len; + *pp += len; +return 0 ; +} + +/* Change the QUIC version for the connection. +* +* Frees existing initial crypto keys and installs new initial keys compatible with the new +* version. +*/ +static int quic_packet_version_change (struct sock *sk,struct quic_conn_id *dcid,u32 version) +{ +struct quic_crypto *crypto =quic_crypto (sk,QUIC_CRYPTO_INITIAL ); + +if (quic_crypto_initial_keys_install (crypto, dcid, version,quic_is_serv (sk))) +return -1 ; + +quic_packet (sk)->version = version; +return 0 ; +} + +/* Select the best compatible QUIC version from offered list. +* +* Considers the local preferred version, currently chosen version, and versions offered by +* the peer. Selects the best compatible version based on client/server role and updates the +* connection version accordingly. +*/ +int quic_packet_select_version (struct sock *sk,u32 *versions,u8 count) +{ +struct quic_packet *packet =quic_packet (sk); +struct quic_config *c =quic_config (sk); +u8 i, pref_found =0 , ch_found =0 ; +u32 preferred, chosen, best =0 ; + + preferred = c->version ?:QUIC_VERSION_V1 ; + chosen = packet->version; + +for (i =0 ; i < count; i++) { +if (!quic_packet_compatible_versions (versions[i])) +continue ; +if (preferred == versions[i]) + pref_found =1 ; +if (chosen == versions[i]) + ch_found =1 ; +if (best < versions[i])/* Track highest offered version. */ + best = versions[i]; + } + +if (!pref_found && !ch_found && !best) +return -1 ; + +if (quic_is_serv (sk)) {/* Server prefers preferred version if offered, else chosen. */ +if (pref_found) + best = preferred; +else if (ch_found) + best = chosen; + }else {/* Client prefers chosen version, else preferred. */ +if (ch_found) + best = chosen; +else if (pref_found) + best = preferred; + } + +if (packet->version == best) +return 0 ; + +/* Change to selected best version. */ +return quic_packet_version_change (sk, &quic_paths (sk)->orig_dcid, best); +} + +/* Extracts a QUIC token from a buffer in the Client Initial packet. */ +static int quic_packet_get_token (struct quic_data *token,u8 **pp,u32 *plen) +{ +u64 len; + +if (!quic_get_var (pp, plen, &len) || len > *plen) +return -EINVAL ; +quic_data (token, *pp, len); + *plen -= len; + *pp += len; +return 0 ; +} + +/* Process PMTU reduction event on a QUIC socket. */ +void quic_packet_rcv_err_pmtu (struct sock *sk) +{ +struct quic_path_group *paths =quic_paths (sk); +struct quic_packet *packet =quic_packet (sk); +struct quic_config *c =quic_config (sk); +u32 pathmtu, info, taglen; +struct dst_entry *dst; +bool reset_timer; + +if (!ip_sk_accept_pmtu (sk)) +return ; + + info =clamp (paths->mtu_info,QUIC_PATH_MIN_PMTU ,QUIC_PATH_MAX_PMTU ); +/* If PLPMTUD is not enabled, update MSS using the route and ICMP info. */ +if (!c->plpmtud_probe_interval) { +if (quic_packet_route (sk) <0 ) +return ; + + dst =__sk_dst_get (sk); +if (dst) + dst->ops->update_pmtu (dst, sk,NULL , info, true); +quic_packet_mss_update (sk, info - packet->hlen); +return ; + } +/* PLPMTUD is enabled: adjust to smaller PMTU, subtract headers and AEAD tag. Also +* notify the QUIC path layer for possible state changes and probing. +*/ + taglen =quic_packet_taglen (packet); + info = info - packet->hlen - taglen; + pathmtu =quic_path_pl_toobig (paths, info, &reset_timer); +if (reset_timer) +quic_timer_reset (sk,QUIC_TIMER_PMTU , c->plpmtud_probe_interval); +if (pathmtu) +quic_packet_mss_update (sk, pathmtu + taglen); +} + +/* Handle ICMP Toobig packet and update QUIC socket path MTU. */ +static int quic_packet_rcv_err (struct sock *sk,struct sk_buff *skb) +{ +union quic_addr daddr, saddr; +u32 info; + +/* All we can do is lookup the matching QUIC socket by addresses. */ +quic_get_msg_addrs (skb, &saddr, &daddr); + sk =quic_sock_lookup (skb, &daddr, &saddr, sk,NULL ); +if (!sk) +return -ENOENT ; + +if (quic_get_mtu_info (skb, &info)) { +sock_put (sk); +return 0 ; + } + +/* Success: update socket path MTU info. */ +bh_lock_sock (sk); +quic_paths (sk)->mtu_info = info; +if (sock_owned_by_user (sk)) { +/* Socket is in use by userspace context. Defer MTU processing to later via +* tasklet. Ensure the socket is not dropped before deferral. +*/ +if (!test_and_set_bit (QUIC_MTU_REDUCED_DEFERRED , &sk->sk_tsq_flags)) +sock_hold (sk); + goto out; + } +/* Otherwise, process the MTU reduction now. */ +quic_packet_rcv_err_pmtu (sk); +out: +bh_unlock_sock (sk); +sock_put (sk); +return 1 ; +} + +#define QUIC_PACKET_BACKLOG_MAX 4096 + +/* Queue a packet for later processing when sleeping is allowed. */ +static int quic_packet_backlog_schedule (struct net *net,struct sk_buff *skb) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +struct quic_net *qn =quic_net (net); + +if (cb->backlog) +return 0 ; + +if (skb_queue_len_lockless (&qn->backlog_list) >=QUIC_PACKET_BACKLOG_MAX ) { +QUIC_INC_STATS (net,QUIC_MIB_PKT_RCVDROP ); +kfree_skb (skb); +return -1 ; + } + + cb->backlog =1 ; +skb_queue_tail (&qn->backlog_list, skb); +queue_work (quic_wq, &qn->work); +return 1 ; +} + +#define TLS_MT_CLIENT_HELLO 1 +#define TLS_EXT_alpn 16 + +/* TLS Client Hello Msg: +* +* uint16 ProtocolVersion; +* opaque Random[32]; +* uint8 CipherSuite[2]; +* +* struct { +* ExtensionType extension_type; +* opaque extension_data<0..2^16-1>; +* } Extension; +* +* struct { +* ProtocolVersion legacy_version = 0x0303; +* Random rand; +* opaque legacy_session_id<0..32>; +* CipherSuite cipher_suites<2..2^16-2>; +* opaque legacy_compression_methods<1..2^8-1>; +* Extension extensions<8..2^16-1>; +* } ClientHello; +*/ + +#define TLS_CH_RANDOM_LEN 32 +#define TLS_CH_VERSION_LEN 2 + +/* Extract ALPN data from a TLS ClientHello message. +* +* Parses the TLS ClientHello handshake message to find the ALPN (Application Layer Protocol +* Negotiation) TLS extension. It validates the TLS ClientHello structure, including version, +* random, session ID, cipher suites, compression methods, and extensions. Once the ALPN +* extension is found, the ALPN protocols list is extracted and stored in @alpn. +* +* Return: 0 on success or no ALPN found, a negative error code on failed parsing. +*/ +static int quic_packet_get_alpn (struct quic_data *alpn,u8 *p,u32 len) +{ +int err = -EINVAL , found =0 ; +u64 length, type; + +/* Verify handshake message type (ClientHello) and its length. */ +if (!quic_get_int (&p, &len, &type,1 ) || type !=TLS_MT_CLIENT_HELLO ) +return err; +if (!quic_get_int (&p, &len, &length,3 ) || + length <TLS_CH_RANDOM_LEN +TLS_CH_VERSION_LEN ) +return err; +if (len > (u32 )length)/* Limit len to handshake message length if larger. */ + len = length; +/* Skip legacy_version (2 bytes) + random (32 bytes). */ + p +=TLS_CH_RANDOM_LEN +TLS_CH_VERSION_LEN ; + len -=TLS_CH_RANDOM_LEN +TLS_CH_VERSION_LEN ; +/* legacy_session_id_len must be zero (QUIC requirement). */ +if (!quic_get_int (&p, &len, &length,1 ) || length) +return err; + +/* Skip cipher_suites (2 bytes length + variable data). */ +if (!quic_get_int (&p, &len, &length,2 ) || length > (u64 )len) +return err; + len -= length; + p += length; + +/* Skip legacy_compression_methods (1 byte length + variable data). */ +if (!quic_get_int (&p, &len, &length,1 ) || length > (u64 )len) +return err; + len -= length; + p += length; + +if (!quic_get_int (&p, &len, &length,2 ))/* Read TLS extensions length (2 bytes). */ +return err; +if (len > (u32 )length)/* Limit len to extensions length if larger. */ + len = length; +while (len >4 ) {/* Iterate over extensions to find ALPN (type TLS_EXT_alpn). */ +if (!quic_get_int (&p, &len, &type,2 )) +break ; +if (!quic_get_int (&p, &len, &length,2 )) +break ; +if (len < (u32 )length)/* Incomplete TLS extensions. */ +return 0 ; +if (type == TLS_EXT_alpn) {/* Found ALPN extension. */ + len = length; + found =1 ; +break ; + } +/* Skip non-ALPN extensions. */ + p += length; + len -= length; + } +if (!found) {/* no ALPN extension found: set alpn->len = 0 and alpn->data = p. */ +quic_data (alpn, p,0 ); +return 0 ; + } + +/* Parse ALPN protocols list length (2 bytes). */ +if (!quic_get_int (&p, &len, &length,2 ) || length > (u64 )len) +return err; +quic_data (alpn, p, length);/* Store ALPN protocols list in alpn->data. */ + len = length; +while (len) {/* Validate ALPN protocols list format. */ +if (!quic_get_int (&p, &len, &length,1 ) || length > (u64 )len) { +/* Malformed ALPN entry: set alpn->len = 0 and alpn->data = NULL. */ +quic_data (alpn,NULL ,0 ); +return err; + } + len -= length; + p += length; + } +pr_debug ("%s: alpn_len: %d\n" , __func__, alpn->len); +return 0 ; +} + +/* Parse ALPN from a QUIC Initial packet. +* +* This function processes a QUIC Initial packet to extract the ALPN from the TLS ClientHello +* message inside the QUIC CRYPTO frame. It verifies packet type, version compatibility, +* decrypts the packet payload, and locates the CRYPTO frame to parse the TLS ClientHello. +* Finally, it calls quic_packet_get_alpn() to extract the ALPN extension data. +* +* Return: 0 on success or no ALPN found, a negative error code on failed parsing. +*/ +static int quic_packet_parse_alpn (struct sk_buff *skb,struct quic_data *alpn) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +struct net *net =sock_net (skb->sk);
net/quic/packet.c line 399
Quoted 10 lines
net/quic/packet.c +10
-0+u8 *p = skb->data, *data, type; +struct quic_conn_id dcid, scid; +u32 len = skb->len, version; +struct quic_crypto *crypto; +struct quic_data token; +u64 offset, length; +int err = -EINVAL ; + +if (!sysctl_quic_alpn_demux) +return 0 ;
Can this be made dynamic, turning it on if someone listens on a socket with QUIC_SOCKOPT_ALPN set?
Otherwise I guess it silently doesn't work
and needs administrator interaction.
Thanks!
metze
Re: [PATCH net-next v7 16/16] quic: add packet parser base
Quoted 464 lines · Stefan Metzmacher
Am 15.01.26 um 16:11 schrieb Xin Long:
Quoted 448 lines
This patch usess '
quic_packet' to handle packing of QUIC packets on the receive (RX) path.It introduces mechanisms to parse the ALPN from client Initial packets to determine the correct listener socket. Received packets are then routed and processed accordingly. Similar to the TX path, handling for application and handshake packets is not yet implemented.
- quic_packet_parse_alpn()`: Parse the ALPN from a client Initial packet, then locate the appropriate listener using the ALPN.
-
quic_packet_rcv(): Locate the appropriate socket to handle the packet viaquic_packet_process().- quic_packet_process()`: Process the received packet.
In addition to packet flow, this patch adds support for ICMP-based MTU updates by locating the relevant socket and updating the stored PMTU accordingly.
-
quic_packet_rcv_err_pmtu(): Find the socket and update the PMTU viaquic_packet_mss_update().Signed-off-by Xin Long <[email protected]>---
v5:
- Inquic_packet_rcv_err(), remove the unnecessaryquic_is_listen()check and movequic_get_mtu_info()out of sock lock (suggested by Paolo).
- Replacecancel_work_sync()todisable_work_sync()(suggested byPaolo).v6:
- Fix the loop usingskb_dequeue()inquic_packet_backlog_work(), andkfree_skb()when sk is not found (reported by AI Reviews).
- Removeskb_pull()fromquic_packet_rcv(), since it is now handledin quic_path_rcv().- Note for AI reviews: add if (dst) check in
quic_packet_rcv_err_pmtu(), althoughquic_packet_route()>= 0 already guarantees it is not NULL.
- Note for AI reviews: it is safe to do *plen -=QUIC_HLENinquic_packet_get_version_and_connid(), sincequic_packet_get_sock()already checks if (skb->len<QUIC_HLEN).
- Note for AI reviews:cb->length-cb->number_len-QUIC_TAG_LENcannot underflow, becausequic_crypto_header_decrypt()already checks if (cb->length<QUIC_PN_MAX_LEN+QUIC_SAMPLE_LEN).
- Note for AI reviews: the cast (u16)length inquic_packet_parse_alpn()is safe, as there is a prior check if (length > (u16)len); len isskb->len, which cannot exceedU16_MAX.
- Note for AI reviews: it's correct to do if (flags &QUIC_F_MTU_REDUCED_DEFERRED) inquic_release_cb(), sinceQUIC_MTU_REDUCED_DEFERREDis the bit used withtest_and_set_bit().
- Note for AI reviews: moveskb_cb->backlog= 1 before adding skb to backlog, although it's safe to writeskb_cbafter adding to backlog with sk_lock.slock, as skb dequeue from backlog requires sk_lock.slock.
v7:
- Pass udp sk toquic_packet_rcv(),quic_packet_rcv_err()andquic_sock_lookup(). - Move the call to skb_linearize() and skb_set_owner_sk_safe() to .quic_path_rcv()/quic_packet_rcv(). --- net/quic/packet.c | 644 ++++++++++++++++++++++++++++++++++++++++++++ net/quic/packet.h | 9 + net/quic/protocol.c | 6 + net/quic/protocol.h | 4 + net/quic/socket.c | 134 +++++++++ net/quic/socket.h | 5 + 6 files changed, 802 insertions(+)net/quic/packet.c +373
-0@@ -14,6 +14,650 @@#define QUIC_HLEN 1 +#define QUIC_LONG_HLEN (dcid, scid) \ + (QUIC_HLEN + QUIC_VERSION_LEN + 1 + (dcid)->len + 1 + (scid)->len) + +#define QUIC_VERSION_NUM 2 + +/* Supported QUIC versions and their compatible versions. Used for Compatible Version +* Negotiation in rfc9368#section-2.3. +*/ +static u32 quic_versions[QUIC_VERSION_NUM ][4 ] = { +/* Version, Compatible Versions */ + {QUIC_VERSION_V1 ,QUIC_VERSION_V2 ,QUIC_VERSION_V1 ,0 }, + {QUIC_VERSION_V2 ,QUIC_VERSION_V2 ,QUIC_VERSION_V1 ,0 }, +}; + +/* Get the compatible version list for a given QUIC version. */ +u32 *quic_packet_compatible_versions (u32 version) +{ +u8 i; + +for (i =0 ; i <QUIC_VERSION_NUM ; i++) +if (version == quic_versions[i][0 ]) +return quic_versions[i]; +return NULL ; +} + +/* Convert version-specific type to internal standard packet type. */ +static u8 quic_packet_version_get_type (u32 version,u8 type) +{ +if (version ==QUIC_VERSION_V1 ) +return type; + +switch (type) { +case QUIC_PACKET_INITIAL_V2 : +return QUIC_PACKET_INITIAL ; +case QUIC_PACKET_0RTT_V2 : +return QUIC_PACKET_0RTT ; +case QUIC_PACKET_HANDSHAKE_V2 : +return QUIC_PACKET_HANDSHAKE ; +case QUIC_PACKET_RETRY_V2 : +return QUIC_PACKET_RETRY ; +default : +return -1 ; + } +return -1 ; +} + +/* Parse QUIC version and connection IDs (DCID and SCID) from a Long header packet buffer. */ +static int quic_packet_get_version_and_connid (struct quic_conn_id *dcid,struct quic_conn_id *scid, +u32 *version,u8 **pp,u32 *plen) +{ +u64 len, v; + + *pp +=QUIC_HLEN ; + *plen -=QUIC_HLEN ; + +if (!quic_get_int (pp, plen, &v,QUIC_VERSION_LEN )) +return -EINVAL ; + *version = v; + +if (!quic_get_int (pp, plen, &len,1 ) || + len > *plen || len >QUIC_CONN_ID_MAX_LEN ) +return -EINVAL ; +quic_conn_id_update (dcid, *pp, len); + *plen -= len; + *pp += len; + +if (!quic_get_int (pp, plen, &len,1 ) || + len > *plen || len >QUIC_CONN_ID_MAX_LEN ) +return -EINVAL ; +quic_conn_id_update (scid, *pp, len); + *plen -= len; + *pp += len; +return 0 ; +} + +/* Change the QUIC version for the connection. +* +* Frees existing initial crypto keys and installs new initial keys compatible with the new +* version. +*/ +static int quic_packet_version_change (struct sock *sk,struct quic_conn_id *dcid,u32 version) +{ +struct quic_crypto *crypto =quic_crypto (sk,QUIC_CRYPTO_INITIAL ); + +if (quic_crypto_initial_keys_install (crypto, dcid, version,quic_is_serv (sk))) +return -1 ; + +quic_packet (sk)->version = version; +return 0 ; +} + +/* Select the best compatible QUIC version from offered list. +* +* Considers the local preferred version, currently chosen version, and versions offered by +* the peer. Selects the best compatible version based on client/server role and updates the +* connection version accordingly. +*/ +int quic_packet_select_version (struct sock *sk,u32 *versions,u8 count) +{ +struct quic_packet *packet =quic_packet (sk); +struct quic_config *c =quic_config (sk); +u8 i, pref_found =0 , ch_found =0 ; +u32 preferred, chosen, best =0 ; + + preferred = c->version ?:QUIC_VERSION_V1 ; + chosen = packet->version; + +for (i =0 ; i < count; i++) { +if (!quic_packet_compatible_versions (versions[i])) +continue ; +if (preferred == versions[i]) + pref_found =1 ; +if (chosen == versions[i]) + ch_found =1 ; +if (best < versions[i])/* Track highest offered version. */ + best = versions[i]; + } + +if (!pref_found && !ch_found && !best) +return -1 ; + +if (quic_is_serv (sk)) {/* Server prefers preferred version if offered, else chosen. */ +if (pref_found) + best = preferred; +else if (ch_found) + best = chosen; + }else {/* Client prefers chosen version, else preferred. */ +if (ch_found) + best = chosen; +else if (pref_found) + best = preferred; + } + +if (packet->version == best) +return 0 ; + +/* Change to selected best version. */ +return quic_packet_version_change (sk, &quic_paths (sk)->orig_dcid, best); +} + +/* Extracts a QUIC token from a buffer in the Client Initial packet. */ +static int quic_packet_get_token (struct quic_data *token,u8 **pp,u32 *plen) +{ +u64 len; + +if (!quic_get_var (pp, plen, &len) || len > *plen) +return -EINVAL ; +quic_data (token, *pp, len); + *plen -= len; + *pp += len; +return 0 ; +} + +/* Process PMTU reduction event on a QUIC socket. */ +void quic_packet_rcv_err_pmtu (struct sock *sk) +{ +struct quic_path_group *paths =quic_paths (sk); +struct quic_packet *packet =quic_packet (sk); +struct quic_config *c =quic_config (sk); +u32 pathmtu, info, taglen; +struct dst_entry *dst; +bool reset_timer; + +if (!ip_sk_accept_pmtu (sk)) +return ; + + info =clamp (paths->mtu_info,QUIC_PATH_MIN_PMTU ,QUIC_PATH_MAX_PMTU ); +/* If PLPMTUD is not enabled, update MSS using the route and ICMP info. */ +if (!c->plpmtud_probe_interval) { +if (quic_packet_route (sk) <0 ) +return ; + + dst =__sk_dst_get (sk); +if (dst) + dst->ops->update_pmtu (dst, sk,NULL , info, true); +quic_packet_mss_update (sk, info - packet->hlen); +return ; + } +/* PLPMTUD is enabled: adjust to smaller PMTU, subtract headers and AEAD tag. Also +* notify the QUIC path layer for possible state changes and probing. +*/ + taglen =quic_packet_taglen (packet); + info = info - packet->hlen - taglen; + pathmtu =quic_path_pl_toobig (paths, info, &reset_timer); +if (reset_timer) +quic_timer_reset (sk,QUIC_TIMER_PMTU , c->plpmtud_probe_interval); +if (pathmtu) +quic_packet_mss_update (sk, pathmtu + taglen); +} + +/* Handle ICMP Toobig packet and update QUIC socket path MTU. */ +static int quic_packet_rcv_err (struct sock *sk,struct sk_buff *skb) +{ +union quic_addr daddr, saddr; +u32 info; + +/* All we can do is lookup the matching QUIC socket by addresses. */ +quic_get_msg_addrs (skb, &saddr, &daddr); + sk =quic_sock_lookup (skb, &daddr, &saddr, sk,NULL ); +if (!sk) +return -ENOENT ; + +if (quic_get_mtu_info (skb, &info)) { +sock_put (sk); +return 0 ; + } + +/* Success: update socket path MTU info. */ +bh_lock_sock (sk); +quic_paths (sk)->mtu_info = info; +if (sock_owned_by_user (sk)) { +/* Socket is in use by userspace context. Defer MTU processing to later via +* tasklet. Ensure the socket is not dropped before deferral. +*/ +if (!test_and_set_bit (QUIC_MTU_REDUCED_DEFERRED , &sk->sk_tsq_flags)) +sock_hold (sk); + goto out; + } +/* Otherwise, process the MTU reduction now. */ +quic_packet_rcv_err_pmtu (sk); +out: +bh_unlock_sock (sk); +sock_put (sk); +return 1 ; +} + +#define QUIC_PACKET_BACKLOG_MAX 4096 + +/* Queue a packet for later processing when sleeping is allowed. */ +static int quic_packet_backlog_schedule (struct net *net,struct sk_buff *skb) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +struct quic_net *qn =quic_net (net); + +if (cb->backlog) +return 0 ; + +if (skb_queue_len_lockless (&qn->backlog_list) >=QUIC_PACKET_BACKLOG_MAX ) { +QUIC_INC_STATS (net,QUIC_MIB_PKT_RCVDROP ); +kfree_skb (skb); +return -1 ; + } + + cb->backlog =1 ; +skb_queue_tail (&qn->backlog_list, skb); +queue_work (quic_wq, &qn->work); +return 1 ; +} + +#define TLS_MT_CLIENT_HELLO 1 +#define TLS_EXT_alpn 16 + +/* TLS Client Hello Msg: +* +* uint16 ProtocolVersion; +* opaque Random[32]; +* uint8 CipherSuite[2]; +* +* struct { +* ExtensionType extension_type; +* opaque extension_data<0..2^16-1>; +* } Extension; +* +* struct { +* ProtocolVersion legacy_version = 0x0303; +* Random rand; +* opaque legacy_session_id<0..32>; +* CipherSuite cipher_suites<2..2^16-2>; +* opaque legacy_compression_methods<1..2^8-1>; +* Extension extensions<8..2^16-1>; +* } ClientHello; +*/ + +#define TLS_CH_RANDOM_LEN 32 +#define TLS_CH_VERSION_LEN 2 + +/* Extract ALPN data from a TLS ClientHello message. +* +* Parses the TLS ClientHello handshake message to find the ALPN (Application Layer Protocol +* Negotiation) TLS extension. It validates the TLS ClientHello structure, including version, +* random, session ID, cipher suites, compression methods, and extensions. Once the ALPN +* extension is found, the ALPN protocols list is extracted and stored in @alpn. +* +* Return: 0 on success or no ALPN found, a negative error code on failed parsing. +*/ +static int quic_packet_get_alpn (struct quic_data *alpn,u8 *p,u32 len) +{ +int err = -EINVAL , found =0 ; +u64 length, type; + +/* Verify handshake message type (ClientHello) and its length. */ +if (!quic_get_int (&p, &len, &type,1 ) || type !=TLS_MT_CLIENT_HELLO ) +return err; +if (!quic_get_int (&p, &len, &length,3 ) || + length <TLS_CH_RANDOM_LEN +TLS_CH_VERSION_LEN ) +return err; +if (len > (u32 )length)/* Limit len to handshake message length if larger. */ + len = length; +/* Skip legacy_version (2 bytes) + random (32 bytes). */ + p +=TLS_CH_RANDOM_LEN +TLS_CH_VERSION_LEN ; + len -=TLS_CH_RANDOM_LEN +TLS_CH_VERSION_LEN ; +/* legacy_session_id_len must be zero (QUIC requirement). */ +if (!quic_get_int (&p, &len, &length,1 ) || length) +return err; + +/* Skip cipher_suites (2 bytes length + variable data). */ +if (!quic_get_int (&p, &len, &length,2 ) || length > (u64 )len) +return err; + len -= length; + p += length; + +/* Skip legacy_compression_methods (1 byte length + variable data). */ +if (!quic_get_int (&p, &len, &length,1 ) || length > (u64 )len) +return err; + len -= length; + p += length; + +if (!quic_get_int (&p, &len, &length,2 ))/* Read TLS extensions length (2 bytes). */ +return err; +if (len > (u32 )length)/* Limit len to extensions length if larger. */ + len = length; +while (len >4 ) {/* Iterate over extensions to find ALPN (type TLS_EXT_alpn). */ +if (!quic_get_int (&p, &len, &type,2 )) +break ; +if (!quic_get_int (&p, &len, &length,2 )) +break ; +if (len < (u32 )length)/* Incomplete TLS extensions. */ +return 0 ; +if (type == TLS_EXT_alpn) {/* Found ALPN extension. */ + len = length; + found =1 ; +break ; + } +/* Skip non-ALPN extensions. */ + p += length; + len -= length; + } +if (!found) {/* no ALPN extension found: set alpn->len = 0 and alpn->data = p. */ +quic_data (alpn, p,0 ); +return 0 ; + } + +/* Parse ALPN protocols list length (2 bytes). */ +if (!quic_get_int (&p, &len, &length,2 ) || length > (u64 )len) +return err; +quic_data (alpn, p, length);/* Store ALPN protocols list in alpn->data. */ + len = length; +while (len) {/* Validate ALPN protocols list format. */ +if (!quic_get_int (&p, &len, &length,1 ) || length > (u64 )len) { +/* Malformed ALPN entry: set alpn->len = 0 and alpn->data = NULL. */ +quic_data (alpn,NULL ,0 ); +return err; + } + len -= length; + p += length; + } +pr_debug ("%s: alpn_len: %d\n" , __func__, alpn->len); +return 0 ; +} + +/* Parse ALPN from a QUIC Initial packet. +* +* This function processes a QUIC Initial packet to extract the ALPN from the TLS ClientHello +* message inside the QUIC CRYPTO frame. It verifies packet type, version compatibility, +* decrypts the packet payload, and locates the CRYPTO frame to parse the TLS ClientHello. +* Finally, it calls quic_packet_get_alpn() to extract the ALPN extension data. +* +* Return: 0 on success or no ALPN found, a negative error code on failed parsing. +*/ +static int quic_packet_parse_alpn (struct sk_buff *skb,struct quic_data *alpn) +{ +struct quic_skb_cb *cb =QUIC_SKB_CB (skb); +struct net *net =sock_net (skb->sk);net/quic/packet.c line 399
Quoted 10 lines
net/quic/packet.c +10
-0+u8 *p = skb->data, *data, type; +struct quic_conn_id dcid, scid; +u32 len = skb->len, version; +struct quic_crypto *crypto; +struct quic_data token; +u64 offset, length; +int err = -EINVAL ; + +if (!sysctl_quic_alpn_demux) +return 0 ;Can this be made dynamic, turning it on if someone listens on a socket with
QUIC_SOCKOPT_ALPNset?
Quoted 3 lines
Otherwise I guess it silently doesn't work
and needs administrator interaction.
Makes sense to me.
I will replace this with a static_key when adding QUIC_SOCKOPT_ALPN socket options in patchset-2:
if (!static_branch_unlikely(&quic_alpn_demux_key))
return 0;static_branch_inc() and static_branch_dec() it in quic_hash() and quic_unhash() if alpn is set for listening sockets.
Thanks.