Skip to content

Commit f3911f7

Browse files
committed
tls: fix replacing proto_ops
We replace proto_ops whenever TLS is configured for RX. But our replacement also overrides sendpage_locked, which will crash unless TX is also configured. Similarly we plug both of those in for TLS_HW (NIC crypto offload) even tho TLS_HW has a completely different implementation for TX. Last but not least we always plug in something based on inet_stream_ops even though a few of the callbacks differ for IPv6 (getname, release, bind). Use a callback building method similar to what we do for struct proto. Fixes: c46234e ("tls: RX path for ktls") Fixes: d4ffb02 ("net/tls: enable sk_msg redirect to tls socket egress") Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 274af0f commit f3911f7

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

net/tls/tls_main.c

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static DEFINE_MUTEX(tcpv6_prot_mutex);
6161
static const struct proto *saved_tcpv4_prot;
6262
static DEFINE_MUTEX(tcpv4_prot_mutex);
6363
static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
64-
static struct proto_ops tls_sw_proto_ops;
64+
static struct proto_ops tls_proto_ops[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
6565
static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
6666
const struct proto *base);
6767

@@ -71,6 +71,8 @@ void update_sk_prot(struct sock *sk, struct tls_context *ctx)
7171

7272
WRITE_ONCE(sk->sk_prot,
7373
&tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf]);
74+
WRITE_ONCE(sk->sk_socket->ops,
75+
&tls_proto_ops[ip_ver][ctx->tx_conf][ctx->rx_conf]);
7476
}
7577

7678
int wait_on_pending_writer(struct sock *sk, long *timeo)
@@ -669,8 +671,6 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
669671
if (tx) {
670672
ctx->sk_write_space = sk->sk_write_space;
671673
sk->sk_write_space = tls_write_space;
672-
} else {
673-
sk->sk_socket->ops = &tls_sw_proto_ops;
674674
}
675675
goto out;
676676

@@ -728,6 +728,39 @@ struct tls_context *tls_ctx_create(struct sock *sk)
728728
return ctx;
729729
}
730730

731+
static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
732+
const struct proto_ops *base)
733+
{
734+
ops[TLS_BASE][TLS_BASE] = *base;
735+
736+
ops[TLS_SW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
737+
ops[TLS_SW ][TLS_BASE].sendpage_locked = tls_sw_sendpage_locked;
738+
739+
ops[TLS_BASE][TLS_SW ] = ops[TLS_BASE][TLS_BASE];
740+
ops[TLS_BASE][TLS_SW ].splice_read = tls_sw_splice_read;
741+
742+
ops[TLS_SW ][TLS_SW ] = ops[TLS_SW ][TLS_BASE];
743+
ops[TLS_SW ][TLS_SW ].splice_read = tls_sw_splice_read;
744+
745+
#ifdef CONFIG_TLS_DEVICE
746+
ops[TLS_HW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
747+
ops[TLS_HW ][TLS_BASE].sendpage_locked = NULL;
748+
749+
ops[TLS_HW ][TLS_SW ] = ops[TLS_BASE][TLS_SW ];
750+
ops[TLS_HW ][TLS_SW ].sendpage_locked = NULL;
751+
752+
ops[TLS_BASE][TLS_HW ] = ops[TLS_BASE][TLS_SW ];
753+
754+
ops[TLS_SW ][TLS_HW ] = ops[TLS_SW ][TLS_SW ];
755+
756+
ops[TLS_HW ][TLS_HW ] = ops[TLS_HW ][TLS_SW ];
757+
ops[TLS_HW ][TLS_HW ].sendpage_locked = NULL;
758+
#endif
759+
#ifdef CONFIG_TLS_TOE
760+
ops[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
761+
#endif
762+
}
763+
731764
static void tls_build_proto(struct sock *sk)
732765
{
733766
int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
@@ -739,6 +772,8 @@ static void tls_build_proto(struct sock *sk)
739772
mutex_lock(&tcpv6_prot_mutex);
740773
if (likely(prot != saved_tcpv6_prot)) {
741774
build_protos(tls_prots[TLSV6], prot);
775+
build_proto_ops(tls_proto_ops[TLSV6],
776+
sk->sk_socket->ops);
742777
smp_store_release(&saved_tcpv6_prot, prot);
743778
}
744779
mutex_unlock(&tcpv6_prot_mutex);
@@ -749,6 +784,8 @@ static void tls_build_proto(struct sock *sk)
749784
mutex_lock(&tcpv4_prot_mutex);
750785
if (likely(prot != saved_tcpv4_prot)) {
751786
build_protos(tls_prots[TLSV4], prot);
787+
build_proto_ops(tls_proto_ops[TLSV4],
788+
sk->sk_socket->ops);
752789
smp_store_release(&saved_tcpv4_prot, prot);
753790
}
754791
mutex_unlock(&tcpv4_prot_mutex);
@@ -959,10 +996,6 @@ static int __init tls_register(void)
959996
if (err)
960997
return err;
961998

962-
tls_sw_proto_ops = inet_stream_ops;
963-
tls_sw_proto_ops.splice_read = tls_sw_splice_read;
964-
tls_sw_proto_ops.sendpage_locked = tls_sw_sendpage_locked;
965-
966999
tls_device_init();
9671000
tcp_register_ulp(&tcp_tls_ulp_ops);
9681001

0 commit comments

Comments
 (0)