On 12/4/26 12:19, Sabrina Dubroca wrote:
> I'm struggling to parse this and see what hooks into what.
Sorry, here it is in terms of functions. When the handshake completes, net/handshake calls one inline helper from include/net/tls.h: tls_set_handshake_ops(sk, ops, priv, tx_rec_limit); It stores ops, priv and the limit in the tls_context. ops has three callbacks into net/handshake. From then on only kTLS calls; it calls them with the socket locked. RX: tls_check_pending_rekey() already finds received handshake records. If ops are set, it passes the record to ops->rx_record(). net/handshake parses the KeyUpdate and returns flags: "consumed", "install the next RX key", "send a KeyUpdate". For the RX key, kTLS calls ops->next_key() and installs the result with tls_set_sw_offload(), as your user space rekey does. The record does not reach the consumer. TX: in tls_sw_sendmsg_locked(), before a new data record, kTLS checks two values inline: the "send a KeyUpdate" flag from above, and rec_seq >= tx_rec_limit. A normal record costs these two comparisons and no call. If either is true, kTLS calls ops->tx_record() for the KeyUpdate message, sends it as a handshake record with the current key, waits for the encryption, then calls ops->next_key() and installs the next TX key.
> I wonder how you still get 200 lines in net/tls
Counting C code only (no comments, no blank lines), it is 212 lines. What remains is record layer work that has to run inside kTLS, with tx_lock and the socket lock held: - TX: the KeyUpdate must go out at a record boundary, after the pending data record is closed, and the key switch must follow with no other record in between. This uses tls_sw_sendmsg_locked() and tls_encrypt_async_wait(). - RX: the KeyUpdate record must be taken out of the receive path (recvmsg, read_sock, splice_read) and the RX key installed before the next record is decrypted. - the ops struct, the context fields and the inline setter. net/handshake decides and computes: when to send, the message, HKDF, the next secrets. It cannot do the sending or the key switch itself. Those steps need kTLS's static functions and kTLS's locks. Also, net/handshake is built in and TLS can be a module, so net/handshake can only reach kTLS through inline helpers or callbacks. If code is easier to review, I can post the prototype as an RFC. Martin