Skip to content

Commit f5ffb8e

Browse files
kuba-mooSasha Levin
authored andcommitted
tls: fix replacing proto_ops
[ Upstream commit f3911f7 ] 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 <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 347ebe1 commit f5ffb8e

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)
@@ -578,8 +580,6 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
578580
if (tx) {
579581
ctx->sk_write_space = sk->sk_write_space;
580582
sk->sk_write_space = tls_write_space;
581-
} else {
582-
sk->sk_socket->ops = &tls_sw_proto_ops;
583583
}
584584
goto out;
585585

@@ -637,6 +637,39 @@ struct tls_context *tls_ctx_create(struct sock *sk)
637637
return ctx;
638638
}
639639

640+
static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
641+
const struct proto_ops *base)
642+
{
643+
ops[TLS_BASE][TLS_BASE] = *base;
644+
645+
ops[TLS_SW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
646+
ops[TLS_SW ][TLS_BASE].sendpage_locked = tls_sw_sendpage_locked;
647+
648+
ops[TLS_BASE][TLS_SW ] = ops[TLS_BASE][TLS_BASE];
649+
ops[TLS_BASE][TLS_SW ].splice_read = tls_sw_splice_read;
650+
651+
ops[TLS_SW ][TLS_SW ] = ops[TLS_SW ][TLS_BASE];
652+
ops[TLS_SW ][TLS_SW ].splice_read = tls_sw_splice_read;
653+
654+
#ifdef CONFIG_TLS_DEVICE
655+
ops[TLS_HW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
656+
ops[TLS_HW ][TLS_BASE].sendpage_locked = NULL;
657+
658+
ops[TLS_HW ][TLS_SW ] = ops[TLS_BASE][TLS_SW ];
659+
ops[TLS_HW ][TLS_SW ].sendpage_locked = NULL;
660+
661+
ops[TLS_BASE][TLS_HW ] = ops[TLS_BASE][TLS_SW ];
662+
663+
ops[TLS_SW ][TLS_HW ] = ops[TLS_SW ][TLS_SW ];
664+
665+
ops[TLS_HW ][TLS_HW ] = ops[TLS_HW ][TLS_SW ];
666+
ops[TLS_HW ][TLS_HW ].sendpage_locked = NULL;
667+
#endif
668+
#ifdef CONFIG_TLS_TOE
669+
ops[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
670+
#endif
671+
}
672+
640673
static void tls_build_proto(struct sock *sk)
641674
{
642675
int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
@@ -648,6 +681,8 @@ static void tls_build_proto(struct sock *sk)
648681
mutex_lock(&tcpv6_prot_mutex);
649682
if (likely(prot != saved_tcpv6_prot)) {
650683
build_protos(tls_prots[TLSV6], prot);
684+
build_proto_ops(tls_proto_ops[TLSV6],
685+
sk->sk_socket->ops);
651686
smp_store_release(&saved_tcpv6_prot, prot);
652687
}
653688
mutex_unlock(&tcpv6_prot_mutex);
@@ -658,6 +693,8 @@ static void tls_build_proto(struct sock *sk)
658693
mutex_lock(&tcpv4_prot_mutex);
659694
if (likely(prot != saved_tcpv4_prot)) {
660695
build_protos(tls_prots[TLSV4], prot);
696+
build_proto_ops(tls_proto_ops[TLSV4],
697+
sk->sk_socket->ops);
661698
smp_store_release(&saved_tcpv4_prot, prot);
662699
}
663700
mutex_unlock(&tcpv4_prot_mutex);
@@ -868,10 +905,6 @@ static int __init tls_register(void)
868905
if (err)
869906
return err;
870907

871-
tls_sw_proto_ops = inet_stream_ops;
872-
tls_sw_proto_ops.splice_read = tls_sw_splice_read;
873-
tls_sw_proto_ops.sendpage_locked = tls_sw_sendpage_locked;
874-
875908
tls_device_init();
876909
tcp_register_ulp(&tcp_tls_ulp_ops);
877910

0 commit comments

Comments
 (0)