2 * Copyright (c) 2018 Chelsio Communications, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Written by: Atul Gupta (atul.gupta@chelsio.com)
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/socket.h>
14 #include <linux/hash.h>
16 #include <linux/net.h>
18 #include <linux/tcp.h>
25 #define DRV_NAME "chtls"
28 * chtls device management
29 * maintains a list of the chtls devices
31 static LIST_HEAD(cdev_list
);
32 static DEFINE_MUTEX(cdev_mutex
);
33 static DEFINE_MUTEX(cdev_list_lock
);
35 static DEFINE_MUTEX(notify_mutex
);
36 static RAW_NOTIFIER_HEAD(listen_notify_list
);
37 static struct proto chtls_cpl_prot
;
38 struct request_sock_ops chtls_rsk_ops
;
39 static uint send_page_order
= (14 - PAGE_SHIFT
< 0) ? 0 : 14 - PAGE_SHIFT
;
41 static void register_listen_notifier(struct notifier_block
*nb
)
43 mutex_lock(¬ify_mutex
);
44 raw_notifier_chain_register(&listen_notify_list
, nb
);
45 mutex_unlock(¬ify_mutex
);
48 static void unregister_listen_notifier(struct notifier_block
*nb
)
50 mutex_lock(¬ify_mutex
);
51 raw_notifier_chain_unregister(&listen_notify_list
, nb
);
52 mutex_unlock(¬ify_mutex
);
55 static int listen_notify_handler(struct notifier_block
*this,
56 unsigned long event
, void *data
)
58 struct chtls_listen
*clisten
;
59 int ret
= NOTIFY_DONE
;
61 clisten
= (struct chtls_listen
*)data
;
64 case CHTLS_LISTEN_START
:
65 ret
= chtls_listen_start(clisten
->cdev
, clisten
->sk
);
68 case CHTLS_LISTEN_STOP
:
69 chtls_listen_stop(clisten
->cdev
, clisten
->sk
);
76 static struct notifier_block listen_notifier
= {
77 .notifier_call
= listen_notify_handler
80 static int listen_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
82 if (likely(skb_transport_header(skb
) != skb_network_header(skb
)))
83 return tcp_v4_do_rcv(sk
, skb
);
84 BLOG_SKB_CB(skb
)->backlog_rcv(sk
, skb
);
88 static int chtls_start_listen(struct chtls_dev
*cdev
, struct sock
*sk
)
90 struct chtls_listen
*clisten
;
93 if (sk
->sk_protocol
!= IPPROTO_TCP
)
94 return -EPROTONOSUPPORT
;
96 if (sk
->sk_family
== PF_INET
&&
97 LOOPBACK(inet_sk(sk
)->inet_rcv_saddr
))
98 return -EADDRNOTAVAIL
;
100 sk
->sk_backlog_rcv
= listen_backlog_rcv
;
101 clisten
= kmalloc(sizeof(*clisten
), GFP_KERNEL
);
104 clisten
->cdev
= cdev
;
106 mutex_lock(¬ify_mutex
);
107 err
= raw_notifier_call_chain(&listen_notify_list
,
108 CHTLS_LISTEN_START
, clisten
);
109 mutex_unlock(¬ify_mutex
);
113 static void chtls_stop_listen(struct chtls_dev
*cdev
, struct sock
*sk
)
115 struct chtls_listen
*clisten
;
117 if (sk
->sk_protocol
!= IPPROTO_TCP
)
120 clisten
= kmalloc(sizeof(*clisten
), GFP_KERNEL
);
123 clisten
->cdev
= cdev
;
125 mutex_lock(¬ify_mutex
);
126 raw_notifier_call_chain(&listen_notify_list
,
127 CHTLS_LISTEN_STOP
, clisten
);
128 mutex_unlock(¬ify_mutex
);
131 static int chtls_inline_feature(struct tls_device
*dev
)
133 struct net_device
*netdev
;
134 struct chtls_dev
*cdev
;
137 cdev
= to_chtls_dev(dev
);
139 for (i
= 0; i
< cdev
->lldi
->nports
; i
++) {
140 netdev
= cdev
->ports
[i
];
141 if (netdev
->features
& NETIF_F_HW_TLS_RECORD
)
147 static int chtls_create_hash(struct tls_device
*dev
, struct sock
*sk
)
149 struct chtls_dev
*cdev
= to_chtls_dev(dev
);
151 if (sk
->sk_state
== TCP_LISTEN
)
152 return chtls_start_listen(cdev
, sk
);
156 static void chtls_destroy_hash(struct tls_device
*dev
, struct sock
*sk
)
158 struct chtls_dev
*cdev
= to_chtls_dev(dev
);
160 if (sk
->sk_state
== TCP_LISTEN
)
161 chtls_stop_listen(cdev
, sk
);
164 static void chtls_register_dev(struct chtls_dev
*cdev
)
166 struct tls_device
*tlsdev
= &cdev
->tlsdev
;
168 strlcpy(tlsdev
->name
, "chtls", TLS_DEVICE_NAME_MAX
);
169 strlcat(tlsdev
->name
, cdev
->lldi
->ports
[0]->name
,
170 TLS_DEVICE_NAME_MAX
);
171 tlsdev
->feature
= chtls_inline_feature
;
172 tlsdev
->hash
= chtls_create_hash
;
173 tlsdev
->unhash
= chtls_destroy_hash
;
174 tls_register_device(&cdev
->tlsdev
);
175 cdev
->cdev_state
= CHTLS_CDEV_STATE_UP
;
178 static void chtls_unregister_dev(struct chtls_dev
*cdev
)
180 tls_unregister_device(&cdev
->tlsdev
);
183 static void process_deferq(struct work_struct
*task_param
)
185 struct chtls_dev
*cdev
= container_of(task_param
,
186 struct chtls_dev
, deferq_task
);
189 spin_lock_bh(&cdev
->deferq
.lock
);
190 while ((skb
= __skb_dequeue(&cdev
->deferq
)) != NULL
) {
191 spin_unlock_bh(&cdev
->deferq
.lock
);
192 DEFERRED_SKB_CB(skb
)->handler(cdev
, skb
);
193 spin_lock_bh(&cdev
->deferq
.lock
);
195 spin_unlock_bh(&cdev
->deferq
.lock
);
198 static int chtls_get_skb(struct chtls_dev
*cdev
)
200 cdev
->askb
= alloc_skb(sizeof(struct tcphdr
), GFP_KERNEL
);
204 skb_put(cdev
->askb
, sizeof(struct tcphdr
));
205 skb_reset_transport_header(cdev
->askb
);
206 memset(cdev
->askb
->data
, 0, cdev
->askb
->len
);
210 static void *chtls_uld_add(const struct cxgb4_lld_info
*info
)
212 struct cxgb4_lld_info
*lldi
;
213 struct chtls_dev
*cdev
;
216 cdev
= kzalloc(sizeof(*cdev
) + info
->nports
*
217 (sizeof(struct net_device
*)), GFP_KERNEL
);
221 lldi
= kzalloc(sizeof(*lldi
), GFP_KERNEL
);
225 if (chtls_get_skb(cdev
))
230 cdev
->pdev
= lldi
->pdev
;
231 cdev
->tids
= lldi
->tids
;
232 cdev
->ports
= lldi
->ports
;
233 cdev
->mtus
= lldi
->mtus
;
234 cdev
->tids
= lldi
->tids
;
235 cdev
->pfvf
= FW_VIID_PFN_G(cxgb4_port_viid(lldi
->ports
[0]))
238 for (i
= 0; i
< (1 << RSPQ_HASH_BITS
); i
++) {
239 unsigned int size
= 64 - sizeof(struct rsp_ctrl
) - 8;
241 cdev
->rspq_skb_cache
[i
] = __alloc_skb(size
,
244 if (unlikely(!cdev
->rspq_skb_cache
[i
]))
248 idr_init(&cdev
->hwtid_idr
);
249 INIT_WORK(&cdev
->deferq_task
, process_deferq
);
250 spin_lock_init(&cdev
->listen_lock
);
251 spin_lock_init(&cdev
->idr_lock
);
252 cdev
->send_page_order
= min_t(uint
, get_order(32768),
254 cdev
->max_host_sndbuf
= 48 * 1024;
256 if (lldi
->vr
->key
.size
)
257 if (chtls_init_kmap(cdev
, lldi
))
260 mutex_lock(&cdev_mutex
);
261 list_add_tail(&cdev
->list
, &cdev_list
);
262 mutex_unlock(&cdev_mutex
);
266 for (j
= 0; j
< i
; j
++)
267 kfree_skb(cdev
->rspq_skb_cache
[j
]);
268 kfree_skb(cdev
->askb
);
277 static void chtls_free_uld(struct chtls_dev
*cdev
)
281 chtls_unregister_dev(cdev
);
282 kvfree(cdev
->kmap
.addr
);
283 idr_destroy(&cdev
->hwtid_idr
);
284 for (i
= 0; i
< (1 << RSPQ_HASH_BITS
); i
++)
285 kfree_skb(cdev
->rspq_skb_cache
[i
]);
288 kfree_skb(cdev
->askb
);
292 static void chtls_free_all_uld(void)
294 struct chtls_dev
*cdev
, *tmp
;
296 mutex_lock(&cdev_mutex
);
297 list_for_each_entry_safe(cdev
, tmp
, &cdev_list
, list
) {
298 if (cdev
->cdev_state
== CHTLS_CDEV_STATE_UP
)
299 chtls_free_uld(cdev
);
301 mutex_unlock(&cdev_mutex
);
304 static int chtls_uld_state_change(void *handle
, enum cxgb4_state new_state
)
306 struct chtls_dev
*cdev
= handle
;
310 chtls_register_dev(cdev
);
312 case CXGB4_STATE_DOWN
:
314 case CXGB4_STATE_START_RECOVERY
:
316 case CXGB4_STATE_DETACH
:
317 mutex_lock(&cdev_mutex
);
318 list_del(&cdev
->list
);
319 mutex_unlock(&cdev_mutex
);
320 chtls_free_uld(cdev
);
328 static struct sk_buff
*copy_gl_to_skb_pkt(const struct pkt_gl
*gl
,
334 /* Allocate space for cpl_pass_accpet_req which will be synthesized by
335 * driver. Once driver synthesizes cpl_pass_accpet_req the skb will go
336 * through the regular cpl_pass_accept_req processing in TOM.
338 skb
= alloc_skb(gl
->tot_len
+ sizeof(struct cpl_pass_accept_req
)
339 - pktshift
, GFP_ATOMIC
);
342 __skb_put(skb
, gl
->tot_len
+ sizeof(struct cpl_pass_accept_req
)
344 /* For now we will copy cpl_rx_pkt in the skb */
345 skb_copy_to_linear_data(skb
, rsp
, sizeof(struct cpl_rx_pkt
));
346 skb_copy_to_linear_data_offset(skb
, sizeof(struct cpl_pass_accept_req
)
348 gl
->tot_len
- pktshift
);
353 static int chtls_recv_packet(struct chtls_dev
*cdev
,
354 const struct pkt_gl
*gl
, const __be64
*rsp
)
356 unsigned int opcode
= *(u8
*)rsp
;
360 skb
= copy_gl_to_skb_pkt(gl
, rsp
, cdev
->lldi
->sge_pktshift
);
364 ret
= chtls_handlers
[opcode
](cdev
, skb
);
365 if (ret
& CPL_RET_BUF_DONE
)
371 static int chtls_recv_rsp(struct chtls_dev
*cdev
, const __be64
*rsp
)
373 unsigned long rspq_bin
;
379 len
= 64 - sizeof(struct rsp_ctrl
) - 8;
382 rspq_bin
= hash_ptr((void *)rsp
, RSPQ_HASH_BITS
);
383 skb
= cdev
->rspq_skb_cache
[rspq_bin
];
384 if (skb
&& !skb_is_nonlinear(skb
) &&
385 !skb_shared(skb
) && !skb_cloned(skb
)) {
386 refcount_inc(&skb
->users
);
387 if (refcount_read(&skb
->users
) == 2) {
389 if (skb_tailroom(skb
) >= len
)
392 refcount_dec(&skb
->users
);
394 skb
= alloc_skb(len
, GFP_ATOMIC
);
400 skb_copy_to_linear_data(skb
, rsp
, len
);
401 skb_reset_network_header(skb
);
402 skb_reset_transport_header(skb
);
403 ret
= chtls_handlers
[opcode
](cdev
, skb
);
405 if (ret
& CPL_RET_BUF_DONE
)
410 static void chtls_recv(struct chtls_dev
*cdev
,
411 struct sk_buff
**skbs
, const __be64
*rsp
)
413 struct sk_buff
*skb
= *skbs
;
419 __skb_push(skb
, sizeof(struct rss_header
));
420 skb_copy_to_linear_data(skb
, rsp
, sizeof(struct rss_header
));
422 ret
= chtls_handlers
[opcode
](cdev
, skb
);
423 if (ret
& CPL_RET_BUF_DONE
)
427 static int chtls_uld_rx_handler(void *handle
, const __be64
*rsp
,
428 const struct pkt_gl
*gl
)
430 struct chtls_dev
*cdev
= handle
;
436 if (unlikely(opcode
== CPL_RX_PKT
)) {
437 if (chtls_recv_packet(cdev
, gl
, rsp
) < 0)
443 return chtls_recv_rsp(cdev
, rsp
);
445 #define RX_PULL_LEN 128
446 skb
= cxgb4_pktgl_to_skb(gl
, RX_PULL_LEN
, RX_PULL_LEN
);
449 chtls_recv(cdev
, &skb
, rsp
);
456 static int do_chtls_getsockopt(struct sock
*sk
, char __user
*optval
,
459 struct tls_crypto_info crypto_info
= { 0 };
461 crypto_info
.version
= TLS_1_2_VERSION
;
462 if (copy_to_user(optval
, &crypto_info
, sizeof(struct tls_crypto_info
)))
467 static int chtls_getsockopt(struct sock
*sk
, int level
, int optname
,
468 char __user
*optval
, int __user
*optlen
)
470 struct tls_context
*ctx
= tls_get_ctx(sk
);
472 if (level
!= SOL_TLS
)
473 return ctx
->getsockopt(sk
, level
, optname
, optval
, optlen
);
475 return do_chtls_getsockopt(sk
, optval
, optlen
);
478 static int do_chtls_setsockopt(struct sock
*sk
, int optname
,
479 char __user
*optval
, unsigned int optlen
)
481 struct tls_crypto_info
*crypto_info
, tmp_crypto_info
;
482 struct chtls_sock
*csk
;
486 csk
= rcu_dereference_sk_user_data(sk
);
488 if (!optval
|| optlen
< sizeof(*crypto_info
)) {
493 rc
= copy_from_user(&tmp_crypto_info
, optval
, sizeof(*crypto_info
));
500 if (tmp_crypto_info
.version
!= TLS_1_2_VERSION
) {
505 crypto_info
= (struct tls_crypto_info
*)&csk
->tlshws
.crypto_info
;
507 switch (tmp_crypto_info
.cipher_type
) {
508 case TLS_CIPHER_AES_GCM_128
: {
509 /* Obtain version and type from previous copy */
510 crypto_info
[0] = tmp_crypto_info
;
511 /* Now copy the following data */
512 rc
= copy_from_user((char *)crypto_info
+ sizeof(*crypto_info
),
513 optval
+ sizeof(*crypto_info
),
514 sizeof(struct tls12_crypto_info_aes_gcm_128
)
515 - sizeof(*crypto_info
));
522 keylen
= TLS_CIPHER_AES_GCM_128_KEY_SIZE
;
523 rc
= chtls_setkey(csk
, keylen
, optname
);
534 static int chtls_setsockopt(struct sock
*sk
, int level
, int optname
,
535 char __user
*optval
, unsigned int optlen
)
537 struct tls_context
*ctx
= tls_get_ctx(sk
);
539 if (level
!= SOL_TLS
)
540 return ctx
->setsockopt(sk
, level
, optname
, optval
, optlen
);
542 return do_chtls_setsockopt(sk
, optname
, optval
, optlen
);
545 static struct cxgb4_uld_info chtls_uld_info
= {
547 .nrxq
= MAX_ULD_QSETS
,
548 .ntxq
= MAX_ULD_QSETS
,
550 .add
= chtls_uld_add
,
551 .state_change
= chtls_uld_state_change
,
552 .rx_handler
= chtls_uld_rx_handler
,
555 void chtls_install_cpl_ops(struct sock
*sk
)
557 sk
->sk_prot
= &chtls_cpl_prot
;
560 static void __init
chtls_init_ulp_ops(void)
562 chtls_cpl_prot
= tcp_prot
;
563 chtls_init_rsk_ops(&chtls_cpl_prot
, &chtls_rsk_ops
,
565 chtls_cpl_prot
.close
= chtls_close
;
566 chtls_cpl_prot
.disconnect
= chtls_disconnect
;
567 chtls_cpl_prot
.destroy
= chtls_destroy_sock
;
568 chtls_cpl_prot
.shutdown
= chtls_shutdown
;
569 chtls_cpl_prot
.sendmsg
= chtls_sendmsg
;
570 chtls_cpl_prot
.sendpage
= chtls_sendpage
;
571 chtls_cpl_prot
.recvmsg
= chtls_recvmsg
;
572 chtls_cpl_prot
.setsockopt
= chtls_setsockopt
;
573 chtls_cpl_prot
.getsockopt
= chtls_getsockopt
;
576 static int __init
chtls_register(void)
578 chtls_init_ulp_ops();
579 register_listen_notifier(&listen_notifier
);
580 cxgb4_register_uld(CXGB4_ULD_TLS
, &chtls_uld_info
);
584 static void __exit
chtls_unregister(void)
586 unregister_listen_notifier(&listen_notifier
);
587 chtls_free_all_uld();
588 cxgb4_unregister_uld(CXGB4_ULD_TLS
);
591 module_init(chtls_register
);
592 module_exit(chtls_unregister
);
594 MODULE_DESCRIPTION("Chelsio TLS Inline driver");
595 MODULE_LICENSE("GPL");
596 MODULE_AUTHOR("Chelsio Communications");
597 MODULE_VERSION(DRV_VERSION
);