Skip to content

Commit 6abde0b

Browse files
vinaychelsiodavem330
authored andcommitted
crypto/chtls: IPv6 support for inline TLS
Extends support to IPv6 for Inline TLS server. Signed-off-by: Vinay Kumar Yadav <[email protected]> v1->v2: - cc'd tcp folks. v2->v3: - changed EXPORT_SYMBOL() to EXPORT_SYMBOL_GPL() Signed-off-by: David S. Miller <[email protected]>
1 parent a56772d commit 6abde0b

File tree

4 files changed

+168
-43
lines changed

4 files changed

+168
-43
lines changed

drivers/crypto/chelsio/chtls/chtls_cm.c

Lines changed: 155 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818
#include <linux/kallsyms.h>
1919
#include <linux/kprobes.h>
2020
#include <linux/if_vlan.h>
21+
#include <linux/ipv6.h>
22+
#include <net/ipv6.h>
23+
#include <net/transp_v6.h>
24+
#include <net/ip6_route.h>
2125
#include <net/inet_common.h>
2226
#include <net/tcp.h>
2327
#include <net/dst.h>
2428
#include <net/tls.h>
29+
#include <net/addrconf.h>
30+
#include <net/secure_seq.h>
2531

2632
#include "chtls.h"
2733
#include "chtls_cm.h"
34+
#include "clip_tbl.h"
2835

2936
/*
3037
* State transitions and actions for close. Note that if we are in SYN_SENT
@@ -82,15 +89,36 @@ static void chtls_sock_release(struct kref *ref)
8289
kfree(csk);
8390
}
8491

85-
static struct net_device *chtls_ipv4_netdev(struct chtls_dev *cdev,
92+
static struct net_device *chtls_find_netdev(struct chtls_dev *cdev,
8693
struct sock *sk)
8794
{
8895
struct net_device *ndev = cdev->ports[0];
96+
struct net_device *temp;
97+
int addr_type;
98+
99+
switch (sk->sk_family) {
100+
case PF_INET:
101+
if (likely(!inet_sk(sk)->inet_rcv_saddr))
102+
return ndev;
103+
ndev = ip_dev_find(&init_net, inet_sk(sk)->inet_rcv_saddr);
104+
break;
105+
case PF_INET6:
106+
addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr);
107+
if (likely(addr_type == IPV6_ADDR_ANY))
108+
return ndev;
109+
110+
for_each_netdev_rcu(&init_net, temp) {
111+
if (ipv6_chk_addr(&init_net, (struct in6_addr *)
112+
&sk->sk_v6_rcv_saddr, temp, 1)) {
113+
ndev = temp;
114+
break;
115+
}
116+
}
117+
break;
118+
default:
119+
return NULL;
120+
}
89121

90-
if (likely(!inet_sk(sk)->inet_rcv_saddr))
91-
return ndev;
92-
93-
ndev = ip_dev_find(&init_net, inet_sk(sk)->inet_rcv_saddr);
94122
if (!ndev)
95123
return NULL;
96124

@@ -446,7 +474,10 @@ void chtls_destroy_sock(struct sock *sk)
446474
free_tls_keyid(sk);
447475
kref_put(&csk->kref, chtls_sock_release);
448476
csk->cdev = NULL;
449-
sk->sk_prot = &tcp_prot;
477+
if (sk->sk_family == AF_INET)
478+
sk->sk_prot = &tcp_prot;
479+
else
480+
sk->sk_prot = &tcpv6_prot;
450481
sk->sk_prot->destroy(sk);
451482
}
452483

@@ -473,7 +504,8 @@ static void chtls_disconnect_acceptq(struct sock *listen_sk)
473504
while (*pprev) {
474505
struct request_sock *req = *pprev;
475506

476-
if (req->rsk_ops == &chtls_rsk_ops) {
507+
if (req->rsk_ops == &chtls_rsk_ops ||
508+
req->rsk_ops == &chtls_rsk_opsv6) {
477509
struct sock *child = req->sk;
478510

479511
*pprev = req->dl_next;
@@ -600,14 +632,13 @@ int chtls_listen_start(struct chtls_dev *cdev, struct sock *sk)
600632
struct listen_ctx *ctx;
601633
struct adapter *adap;
602634
struct port_info *pi;
635+
bool clip_valid;
603636
int stid;
604637
int ret;
605638

606-
if (sk->sk_family != PF_INET)
607-
return -EAGAIN;
608-
639+
clip_valid = false;
609640
rcu_read_lock();
610-
ndev = chtls_ipv4_netdev(cdev, sk);
641+
ndev = chtls_find_netdev(cdev, sk);
611642
rcu_read_unlock();
612643
if (!ndev)
613644
return -EBADF;
@@ -638,16 +669,35 @@ int chtls_listen_start(struct chtls_dev *cdev, struct sock *sk)
638669
if (!listen_hash_add(cdev, sk, stid))
639670
goto free_stid;
640671

641-
ret = cxgb4_create_server(ndev, stid,
642-
inet_sk(sk)->inet_rcv_saddr,
643-
inet_sk(sk)->inet_sport, 0,
644-
cdev->lldi->rxq_ids[0]);
672+
if (sk->sk_family == PF_INET) {
673+
ret = cxgb4_create_server(ndev, stid,
674+
inet_sk(sk)->inet_rcv_saddr,
675+
inet_sk(sk)->inet_sport, 0,
676+
cdev->lldi->rxq_ids[0]);
677+
} else {
678+
int addr_type;
679+
680+
addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr);
681+
if (addr_type != IPV6_ADDR_ANY) {
682+
ret = cxgb4_clip_get(ndev, (const u32 *)
683+
&sk->sk_v6_rcv_saddr, 1);
684+
if (ret)
685+
goto del_hash;
686+
clip_valid = true;
687+
}
688+
ret = cxgb4_create_server6(ndev, stid,
689+
&sk->sk_v6_rcv_saddr,
690+
inet_sk(sk)->inet_sport,
691+
cdev->lldi->rxq_ids[0]);
692+
}
645693
if (ret > 0)
646694
ret = net_xmit_errno(ret);
647695
if (ret)
648696
goto del_hash;
649697
return 0;
650698
del_hash:
699+
if (clip_valid)
700+
cxgb4_clip_release(ndev, (const u32 *)&sk->sk_v6_rcv_saddr, 1);
651701
listen_hash_del(cdev, sk);
652702
free_stid:
653703
cxgb4_free_stid(cdev->tids, stid, sk->sk_family);
@@ -661,6 +711,8 @@ int chtls_listen_start(struct chtls_dev *cdev, struct sock *sk)
661711
void chtls_listen_stop(struct chtls_dev *cdev, struct sock *sk)
662712
{
663713
struct listen_ctx *listen_ctx;
714+
struct chtls_sock *csk;
715+
int addr_type = 0;
664716
int stid;
665717

666718
stid = listen_hash_del(cdev, sk);
@@ -671,7 +723,16 @@ void chtls_listen_stop(struct chtls_dev *cdev, struct sock *sk)
671723
chtls_reset_synq(listen_ctx);
672724

673725
cxgb4_remove_server(cdev->lldi->ports[0], stid,
674-
cdev->lldi->rxq_ids[0], 0);
726+
cdev->lldi->rxq_ids[0], sk->sk_family == PF_INET6);
727+
728+
if (sk->sk_family == PF_INET6) {
729+
csk = rcu_dereference_sk_user_data(sk);
730+
addr_type = ipv6_addr_type((const struct in6_addr *)
731+
&sk->sk_v6_rcv_saddr);
732+
if (addr_type != IPV6_ADDR_ANY)
733+
cxgb4_clip_release(csk->egress_dev, (const u32 *)
734+
&sk->sk_v6_rcv_saddr, 1);
735+
}
675736
chtls_disconnect_acceptq(sk);
676737
}
677738

@@ -880,7 +941,10 @@ static unsigned int chtls_select_mss(const struct chtls_sock *csk,
880941
tp = tcp_sk(sk);
881942
tcpoptsz = 0;
882943

883-
iphdrsz = sizeof(struct iphdr) + sizeof(struct tcphdr);
944+
if (sk->sk_family == AF_INET6)
945+
iphdrsz = sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
946+
else
947+
iphdrsz = sizeof(struct iphdr) + sizeof(struct tcphdr);
884948
if (req->tcpopt.tstamp)
885949
tcpoptsz += round_up(TCPOLEN_TIMESTAMP, 4);
886950

@@ -1045,11 +1109,29 @@ static struct sock *chtls_recv_sock(struct sock *lsk,
10451109
if (!newsk)
10461110
goto free_oreq;
10471111

1048-
dst = inet_csk_route_child_sock(lsk, newsk, oreq);
1049-
if (!dst)
1050-
goto free_sk;
1112+
if (lsk->sk_family == AF_INET) {
1113+
dst = inet_csk_route_child_sock(lsk, newsk, oreq);
1114+
if (!dst)
1115+
goto free_sk;
10511116

1052-
n = dst_neigh_lookup(dst, &iph->saddr);
1117+
n = dst_neigh_lookup(dst, &iph->saddr);
1118+
} else {
1119+
const struct ipv6hdr *ip6h;
1120+
struct flowi6 fl6;
1121+
1122+
ip6h = (const struct ipv6hdr *)network_hdr;
1123+
memset(&fl6, 0, sizeof(fl6));
1124+
fl6.flowi6_proto = IPPROTO_TCP;
1125+
fl6.saddr = ip6h->daddr;
1126+
fl6.daddr = ip6h->saddr;
1127+
fl6.fl6_dport = inet_rsk(oreq)->ir_rmt_port;
1128+
fl6.fl6_sport = htons(inet_rsk(oreq)->ir_num);
1129+
security_req_classify_flow(oreq, flowi6_to_flowi(&fl6));
1130+
dst = ip6_dst_lookup_flow(sock_net(lsk), lsk, &fl6, NULL);
1131+
if (IS_ERR(dst))
1132+
goto free_sk;
1133+
n = dst_neigh_lookup(dst, &ip6h->saddr);
1134+
}
10531135
if (!n)
10541136
goto free_sk;
10551137

@@ -1072,9 +1154,28 @@ static struct sock *chtls_recv_sock(struct sock *lsk,
10721154
tp = tcp_sk(newsk);
10731155
newinet = inet_sk(newsk);
10741156

1075-
newinet->inet_daddr = iph->saddr;
1076-
newinet->inet_rcv_saddr = iph->daddr;
1077-
newinet->inet_saddr = iph->daddr;
1157+
if (iph->version == 0x4) {
1158+
newinet->inet_daddr = iph->saddr;
1159+
newinet->inet_rcv_saddr = iph->daddr;
1160+
newinet->inet_saddr = iph->daddr;
1161+
} else {
1162+
struct tcp6_sock *newtcp6sk = (struct tcp6_sock *)newsk;
1163+
struct inet_request_sock *treq = inet_rsk(oreq);
1164+
struct ipv6_pinfo *newnp = inet6_sk(newsk);
1165+
struct ipv6_pinfo *np = inet6_sk(lsk);
1166+
1167+
inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1168+
memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1169+
newsk->sk_v6_daddr = treq->ir_v6_rmt_addr;
1170+
newsk->sk_v6_rcv_saddr = treq->ir_v6_loc_addr;
1171+
inet6_sk(newsk)->saddr = treq->ir_v6_loc_addr;
1172+
newnp->ipv6_fl_list = NULL;
1173+
newnp->pktoptions = NULL;
1174+
newsk->sk_bound_dev_if = treq->ir_iif;
1175+
newinet->inet_opt = NULL;
1176+
newinet->inet_daddr = LOOPBACK4_IPV6;
1177+
newinet->inet_saddr = LOOPBACK4_IPV6;
1178+
}
10781179

10791180
oreq->ts_recent = PASS_OPEN_TID_G(ntohl(req->tos_stid));
10801181
sk_setup_caps(newsk, dst);
@@ -1156,6 +1257,7 @@ static void chtls_pass_accept_request(struct sock *sk,
11561257
struct sk_buff *reply_skb;
11571258
struct chtls_sock *csk;
11581259
struct chtls_dev *cdev;
1260+
struct ipv6hdr *ip6h;
11591261
struct tcphdr *tcph;
11601262
struct sock *newsk;
11611263
struct ethhdr *eh;
@@ -1196,37 +1298,50 @@ static void chtls_pass_accept_request(struct sock *sk,
11961298
if (sk_acceptq_is_full(sk))
11971299
goto reject;
11981300

1199-
oreq = inet_reqsk_alloc(&chtls_rsk_ops, sk, true);
1200-
if (!oreq)
1201-
goto reject;
1202-
1203-
oreq->rsk_rcv_wnd = 0;
1204-
oreq->rsk_window_clamp = 0;
1205-
oreq->cookie_ts = 0;
1206-
oreq->mss = 0;
1207-
oreq->ts_recent = 0;
12081301

12091302
eth_hdr_len = T6_ETH_HDR_LEN_G(ntohl(req->hdr_len));
12101303
if (eth_hdr_len == ETH_HLEN) {
12111304
eh = (struct ethhdr *)(req + 1);
12121305
iph = (struct iphdr *)(eh + 1);
1306+
ip6h = (struct ipv6hdr *)(eh + 1);
12131307
network_hdr = (void *)(eh + 1);
12141308
} else {
12151309
vlan_eh = (struct vlan_ethhdr *)(req + 1);
12161310
iph = (struct iphdr *)(vlan_eh + 1);
1311+
ip6h = (struct ipv6hdr *)(vlan_eh + 1);
12171312
network_hdr = (void *)(vlan_eh + 1);
12181313
}
1219-
if (iph->version != 0x4)
1220-
goto free_oreq;
12211314

1222-
tcph = (struct tcphdr *)(iph + 1);
1223-
skb_set_network_header(skb, (void *)iph - (void *)req);
1315+
if (iph->version == 0x4) {
1316+
tcph = (struct tcphdr *)(iph + 1);
1317+
skb_set_network_header(skb, (void *)iph - (void *)req);
1318+
oreq = inet_reqsk_alloc(&chtls_rsk_ops, sk, true);
1319+
} else {
1320+
tcph = (struct tcphdr *)(ip6h + 1);
1321+
skb_set_network_header(skb, (void *)ip6h - (void *)req);
1322+
oreq = inet_reqsk_alloc(&chtls_rsk_opsv6, sk, false);
1323+
}
1324+
1325+
if (!oreq)
1326+
goto reject;
1327+
1328+
oreq->rsk_rcv_wnd = 0;
1329+
oreq->rsk_window_clamp = 0;
1330+
oreq->cookie_ts = 0;
1331+
oreq->mss = 0;
1332+
oreq->ts_recent = 0;
12241333

12251334
tcp_rsk(oreq)->tfo_listener = false;
12261335
tcp_rsk(oreq)->rcv_isn = ntohl(tcph->seq);
12271336
chtls_set_req_port(oreq, tcph->source, tcph->dest);
1228-
chtls_set_req_addr(oreq, iph->daddr, iph->saddr);
1229-
ip_dsfield = ipv4_get_dsfield(iph);
1337+
if (iph->version == 0x4) {
1338+
chtls_set_req_addr(oreq, iph->daddr, iph->saddr);
1339+
ip_dsfield = ipv4_get_dsfield(iph);
1340+
} else {
1341+
inet_rsk(oreq)->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
1342+
inet_rsk(oreq)->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
1343+
ip_dsfield = ipv6_get_dsfield(ipv6_hdr(skb));
1344+
}
12301345
if (req->tcpopt.wsf <= 14 &&
12311346
sock_net(sk)->ipv4.sysctl_tcp_window_scaling) {
12321347
inet_rsk(oreq)->wscale_ok = 1;
@@ -1243,7 +1358,7 @@ static void chtls_pass_accept_request(struct sock *sk,
12431358

12441359
newsk = chtls_recv_sock(sk, oreq, network_hdr, req, cdev);
12451360
if (!newsk)
1246-
goto reject;
1361+
goto free_oreq;
12471362

12481363
if (chtls_get_module(newsk))
12491364
goto reject;

drivers/crypto/chelsio/chtls/chtls_cm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ enum {
7979

8080
typedef void (*defer_handler_t)(struct chtls_dev *dev, struct sk_buff *skb);
8181
extern struct request_sock_ops chtls_rsk_ops;
82+
extern struct request_sock_ops chtls_rsk_opsv6;
8283

8384
struct deferred_skb_cb {
8485
defer_handler_t handler;

drivers/crypto/chelsio/chtls/chtls_main.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <linux/net.h>
1414
#include <linux/ip.h>
1515
#include <linux/tcp.h>
16+
#include <net/ipv6.h>
17+
#include <net/transp_v6.h>
1618
#include <net/tcp.h>
1719
#include <net/tls.h>
1820

@@ -30,8 +32,8 @@ static DEFINE_MUTEX(cdev_mutex);
3032

3133
static DEFINE_MUTEX(notify_mutex);
3234
static RAW_NOTIFIER_HEAD(listen_notify_list);
33-
static struct proto chtls_cpl_prot;
34-
struct request_sock_ops chtls_rsk_ops;
35+
static struct proto chtls_cpl_prot, chtls_cpl_protv6;
36+
struct request_sock_ops chtls_rsk_ops, chtls_rsk_opsv6;
3537
static uint send_page_order = (14 - PAGE_SHIFT < 0) ? 0 : 14 - PAGE_SHIFT;
3638

3739
static void register_listen_notifier(struct notifier_block *nb)
@@ -586,7 +588,10 @@ static struct cxgb4_uld_info chtls_uld_info = {
586588

587589
void chtls_install_cpl_ops(struct sock *sk)
588590
{
589-
sk->sk_prot = &chtls_cpl_prot;
591+
if (sk->sk_family == AF_INET)
592+
sk->sk_prot = &chtls_cpl_prot;
593+
else
594+
sk->sk_prot = &chtls_cpl_protv6;
590595
}
591596

592597
static void __init chtls_init_ulp_ops(void)
@@ -603,6 +608,9 @@ static void __init chtls_init_ulp_ops(void)
603608
chtls_cpl_prot.recvmsg = chtls_recvmsg;
604609
chtls_cpl_prot.setsockopt = chtls_setsockopt;
605610
chtls_cpl_prot.getsockopt = chtls_getsockopt;
611+
chtls_cpl_protv6 = chtls_cpl_prot;
612+
chtls_init_rsk_ops(&chtls_cpl_protv6, &chtls_rsk_opsv6,
613+
&tcpv6_prot, PF_INET6);
606614
}
607615

608616
static int __init chtls_register(void)

net/ipv6/tcp_ipv6.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,7 @@ struct proto tcpv6_prot = {
21212121
#endif
21222122
.diag_destroy = tcp_abort,
21232123
};
2124+
EXPORT_SYMBOL_GPL(tcpv6_prot);
21242125

21252126
/* thinking of making this const? Don't.
21262127
* early_demux can change based on sysctl.

0 commit comments

Comments
 (0)