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_dev
*cdev
;
66 case CHTLS_LISTEN_START
:
67 case CHTLS_LISTEN_STOP
:
68 mutex_lock(&cdev_list_lock
);
69 list_for_each_entry(cdev
, &cdev_list
, list
) {
70 if (event
== CHTLS_LISTEN_START
)
71 ret
= chtls_listen_start(cdev
, sk
);
73 chtls_listen_stop(cdev
, sk
);
75 mutex_unlock(&cdev_list_lock
);
81 static struct notifier_block listen_notifier
= {
82 .notifier_call
= listen_notify_handler
85 static int listen_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
87 if (likely(skb_transport_header(skb
) != skb_network_header(skb
)))
88 return tcp_v4_do_rcv(sk
, skb
);
89 BLOG_SKB_CB(skb
)->backlog_rcv(sk
, skb
);
93 static int chtls_start_listen(struct sock
*sk
)
97 if (sk
->sk_protocol
!= IPPROTO_TCP
)
98 return -EPROTONOSUPPORT
;
100 if (sk
->sk_family
== PF_INET
&&
101 LOOPBACK(inet_sk(sk
)->inet_rcv_saddr
))
102 return -EADDRNOTAVAIL
;
104 sk
->sk_backlog_rcv
= listen_backlog_rcv
;
105 mutex_lock(¬ify_mutex
);
106 err
= raw_notifier_call_chain(&listen_notify_list
,
107 CHTLS_LISTEN_START
, sk
);
108 mutex_unlock(¬ify_mutex
);
112 static void chtls_stop_listen(struct sock
*sk
)
114 if (sk
->sk_protocol
!= IPPROTO_TCP
)
117 mutex_lock(¬ify_mutex
);
118 raw_notifier_call_chain(&listen_notify_list
,
119 CHTLS_LISTEN_STOP
, sk
);
120 mutex_unlock(¬ify_mutex
);
123 static int chtls_inline_feature(struct tls_device
*dev
)
125 struct net_device
*netdev
;
126 struct chtls_dev
*cdev
;
129 cdev
= to_chtls_dev(dev
);
131 for (i
= 0; i
< cdev
->lldi
->nports
; i
++) {
132 netdev
= cdev
->ports
[i
];
133 if (netdev
->features
& NETIF_F_HW_TLS_RECORD
)
139 static int chtls_create_hash(struct tls_device
*dev
, struct sock
*sk
)
141 if (sk
->sk_state
== TCP_LISTEN
)
142 return chtls_start_listen(sk
);
146 static void chtls_destroy_hash(struct tls_device
*dev
, struct sock
*sk
)
148 if (sk
->sk_state
== TCP_LISTEN
)
149 chtls_stop_listen(sk
);
152 static void chtls_register_dev(struct chtls_dev
*cdev
)
154 struct tls_device
*tlsdev
= &cdev
->tlsdev
;
156 strlcpy(tlsdev
->name
, "chtls", TLS_DEVICE_NAME_MAX
);
157 strlcat(tlsdev
->name
, cdev
->lldi
->ports
[0]->name
,
158 TLS_DEVICE_NAME_MAX
);
159 tlsdev
->feature
= chtls_inline_feature
;
160 tlsdev
->hash
= chtls_create_hash
;
161 tlsdev
->unhash
= chtls_destroy_hash
;
162 tls_register_device(&cdev
->tlsdev
);
163 cdev
->cdev_state
= CHTLS_CDEV_STATE_UP
;
166 static void chtls_unregister_dev(struct chtls_dev
*cdev
)
168 tls_unregister_device(&cdev
->tlsdev
);
171 static void process_deferq(struct work_struct
*task_param
)
173 struct chtls_dev
*cdev
= container_of(task_param
,
174 struct chtls_dev
, deferq_task
);
177 spin_lock_bh(&cdev
->deferq
.lock
);
178 while ((skb
= __skb_dequeue(&cdev
->deferq
)) != NULL
) {
179 spin_unlock_bh(&cdev
->deferq
.lock
);
180 DEFERRED_SKB_CB(skb
)->handler(cdev
, skb
);
181 spin_lock_bh(&cdev
->deferq
.lock
);
183 spin_unlock_bh(&cdev
->deferq
.lock
);
186 static int chtls_get_skb(struct chtls_dev
*cdev
)
188 cdev
->askb
= alloc_skb(sizeof(struct tcphdr
), GFP_KERNEL
);
192 skb_put(cdev
->askb
, sizeof(struct tcphdr
));
193 skb_reset_transport_header(cdev
->askb
);
194 memset(cdev
->askb
->data
, 0, cdev
->askb
->len
);
198 static void *chtls_uld_add(const struct cxgb4_lld_info
*info
)
200 struct cxgb4_lld_info
*lldi
;
201 struct chtls_dev
*cdev
;
204 cdev
= kzalloc(sizeof(*cdev
) + info
->nports
*
205 (sizeof(struct net_device
*)), GFP_KERNEL
);
209 lldi
= kzalloc(sizeof(*lldi
), GFP_KERNEL
);
213 if (chtls_get_skb(cdev
))
218 cdev
->pdev
= lldi
->pdev
;
219 cdev
->tids
= lldi
->tids
;
220 cdev
->ports
= lldi
->ports
;
221 cdev
->mtus
= lldi
->mtus
;
222 cdev
->tids
= lldi
->tids
;
223 cdev
->pfvf
= FW_VIID_PFN_G(cxgb4_port_viid(lldi
->ports
[0]))
226 for (i
= 0; i
< (1 << RSPQ_HASH_BITS
); i
++) {
227 unsigned int size
= 64 - sizeof(struct rsp_ctrl
) - 8;
229 cdev
->rspq_skb_cache
[i
] = __alloc_skb(size
,
232 if (unlikely(!cdev
->rspq_skb_cache
[i
]))
236 idr_init(&cdev
->hwtid_idr
);
237 INIT_WORK(&cdev
->deferq_task
, process_deferq
);
238 spin_lock_init(&cdev
->listen_lock
);
239 spin_lock_init(&cdev
->idr_lock
);
240 cdev
->send_page_order
= min_t(uint
, get_order(32768),
242 cdev
->max_host_sndbuf
= 48 * 1024;
244 if (lldi
->vr
->key
.size
)
245 if (chtls_init_kmap(cdev
, lldi
))
248 mutex_lock(&cdev_mutex
);
249 list_add_tail(&cdev
->list
, &cdev_list
);
250 mutex_unlock(&cdev_mutex
);
254 for (j
= 0; j
< i
; j
++)
255 kfree_skb(cdev
->rspq_skb_cache
[j
]);
256 kfree_skb(cdev
->askb
);
265 static void chtls_free_uld(struct chtls_dev
*cdev
)
269 chtls_unregister_dev(cdev
);
270 kvfree(cdev
->kmap
.addr
);
271 idr_destroy(&cdev
->hwtid_idr
);
272 for (i
= 0; i
< (1 << RSPQ_HASH_BITS
); i
++)
273 kfree_skb(cdev
->rspq_skb_cache
[i
]);
276 kfree_skb(cdev
->askb
);
280 static void chtls_free_all_uld(void)
282 struct chtls_dev
*cdev
, *tmp
;
284 mutex_lock(&cdev_mutex
);
285 list_for_each_entry_safe(cdev
, tmp
, &cdev_list
, list
) {
286 if (cdev
->cdev_state
== CHTLS_CDEV_STATE_UP
)
287 chtls_free_uld(cdev
);
289 mutex_unlock(&cdev_mutex
);
292 static int chtls_uld_state_change(void *handle
, enum cxgb4_state new_state
)
294 struct chtls_dev
*cdev
= handle
;
298 chtls_register_dev(cdev
);
300 case CXGB4_STATE_DOWN
:
302 case CXGB4_STATE_START_RECOVERY
:
304 case CXGB4_STATE_DETACH
:
305 mutex_lock(&cdev_mutex
);
306 list_del(&cdev
->list
);
307 mutex_unlock(&cdev_mutex
);
308 chtls_free_uld(cdev
);
316 static struct sk_buff
*copy_gl_to_skb_pkt(const struct pkt_gl
*gl
,
322 /* Allocate space for cpl_pass_accpet_req which will be synthesized by
323 * driver. Once driver synthesizes cpl_pass_accpet_req the skb will go
324 * through the regular cpl_pass_accept_req processing in TOM.
326 skb
= alloc_skb(gl
->tot_len
+ sizeof(struct cpl_pass_accept_req
)
327 - pktshift
, GFP_ATOMIC
);
330 __skb_put(skb
, gl
->tot_len
+ sizeof(struct cpl_pass_accept_req
)
332 /* For now we will copy cpl_rx_pkt in the skb */
333 skb_copy_to_linear_data(skb
, rsp
, sizeof(struct cpl_rx_pkt
));
334 skb_copy_to_linear_data_offset(skb
, sizeof(struct cpl_pass_accept_req
)
336 gl
->tot_len
- pktshift
);
341 static int chtls_recv_packet(struct chtls_dev
*cdev
,
342 const struct pkt_gl
*gl
, const __be64
*rsp
)
344 unsigned int opcode
= *(u8
*)rsp
;
348 skb
= copy_gl_to_skb_pkt(gl
, rsp
, cdev
->lldi
->sge_pktshift
);
352 ret
= chtls_handlers
[opcode
](cdev
, skb
);
353 if (ret
& CPL_RET_BUF_DONE
)
359 static int chtls_recv_rsp(struct chtls_dev
*cdev
, const __be64
*rsp
)
361 unsigned long rspq_bin
;
367 len
= 64 - sizeof(struct rsp_ctrl
) - 8;
370 rspq_bin
= hash_ptr((void *)rsp
, RSPQ_HASH_BITS
);
371 skb
= cdev
->rspq_skb_cache
[rspq_bin
];
372 if (skb
&& !skb_is_nonlinear(skb
) &&
373 !skb_shared(skb
) && !skb_cloned(skb
)) {
374 refcount_inc(&skb
->users
);
375 if (refcount_read(&skb
->users
) == 2) {
377 if (skb_tailroom(skb
) >= len
)
380 refcount_dec(&skb
->users
);
382 skb
= alloc_skb(len
, GFP_ATOMIC
);
388 skb_copy_to_linear_data(skb
, rsp
, len
);
389 skb_reset_network_header(skb
);
390 skb_reset_transport_header(skb
);
391 ret
= chtls_handlers
[opcode
](cdev
, skb
);
393 if (ret
& CPL_RET_BUF_DONE
)
398 static void chtls_recv(struct chtls_dev
*cdev
,
399 struct sk_buff
**skbs
, const __be64
*rsp
)
401 struct sk_buff
*skb
= *skbs
;
407 __skb_push(skb
, sizeof(struct rss_header
));
408 skb_copy_to_linear_data(skb
, rsp
, sizeof(struct rss_header
));
410 ret
= chtls_handlers
[opcode
](cdev
, skb
);
411 if (ret
& CPL_RET_BUF_DONE
)
415 static int chtls_uld_rx_handler(void *handle
, const __be64
*rsp
,
416 const struct pkt_gl
*gl
)
418 struct chtls_dev
*cdev
= handle
;
424 if (unlikely(opcode
== CPL_RX_PKT
)) {
425 if (chtls_recv_packet(cdev
, gl
, rsp
) < 0)
431 return chtls_recv_rsp(cdev
, rsp
);
433 #define RX_PULL_LEN 128
434 skb
= cxgb4_pktgl_to_skb(gl
, RX_PULL_LEN
, RX_PULL_LEN
);
437 chtls_recv(cdev
, &skb
, rsp
);
444 static int do_chtls_getsockopt(struct sock
*sk
, char __user
*optval
,
447 struct tls_crypto_info crypto_info
= { 0 };
449 crypto_info
.version
= TLS_1_2_VERSION
;
450 if (copy_to_user(optval
, &crypto_info
, sizeof(struct tls_crypto_info
)))
455 static int chtls_getsockopt(struct sock
*sk
, int level
, int optname
,
456 char __user
*optval
, int __user
*optlen
)
458 struct tls_context
*ctx
= tls_get_ctx(sk
);
460 if (level
!= SOL_TLS
)
461 return ctx
->getsockopt(sk
, level
, optname
, optval
, optlen
);
463 return do_chtls_getsockopt(sk
, optval
, optlen
);
466 static int do_chtls_setsockopt(struct sock
*sk
, int optname
,
467 char __user
*optval
, unsigned int optlen
)
469 struct tls_crypto_info
*crypto_info
, tmp_crypto_info
;
470 struct chtls_sock
*csk
;
474 csk
= rcu_dereference_sk_user_data(sk
);
476 if (!optval
|| optlen
< sizeof(*crypto_info
)) {
481 rc
= copy_from_user(&tmp_crypto_info
, optval
, sizeof(*crypto_info
));
488 if (tmp_crypto_info
.version
!= TLS_1_2_VERSION
) {
493 crypto_info
= (struct tls_crypto_info
*)&csk
->tlshws
.crypto_info
;
495 switch (tmp_crypto_info
.cipher_type
) {
496 case TLS_CIPHER_AES_GCM_128
: {
497 /* Obtain version and type from previous copy */
498 crypto_info
[0] = tmp_crypto_info
;
499 /* Now copy the following data */
500 rc
= copy_from_user((char *)crypto_info
+ sizeof(*crypto_info
),
501 optval
+ sizeof(*crypto_info
),
502 sizeof(struct tls12_crypto_info_aes_gcm_128
)
503 - sizeof(*crypto_info
));
510 keylen
= TLS_CIPHER_AES_GCM_128_KEY_SIZE
;
511 rc
= chtls_setkey(csk
, keylen
, optname
);
522 static int chtls_setsockopt(struct sock
*sk
, int level
, int optname
,
523 char __user
*optval
, unsigned int optlen
)
525 struct tls_context
*ctx
= tls_get_ctx(sk
);
527 if (level
!= SOL_TLS
)
528 return ctx
->setsockopt(sk
, level
, optname
, optval
, optlen
);
530 return do_chtls_setsockopt(sk
, optname
, optval
, optlen
);
533 static struct cxgb4_uld_info chtls_uld_info
= {
535 .nrxq
= MAX_ULD_QSETS
,
536 .ntxq
= MAX_ULD_QSETS
,
538 .add
= chtls_uld_add
,
539 .state_change
= chtls_uld_state_change
,
540 .rx_handler
= chtls_uld_rx_handler
,
543 void chtls_install_cpl_ops(struct sock
*sk
)
545 sk
->sk_prot
= &chtls_cpl_prot
;
548 static void __init
chtls_init_ulp_ops(void)
550 chtls_cpl_prot
= tcp_prot
;
551 chtls_init_rsk_ops(&chtls_cpl_prot
, &chtls_rsk_ops
,
553 chtls_cpl_prot
.close
= chtls_close
;
554 chtls_cpl_prot
.disconnect
= chtls_disconnect
;
555 chtls_cpl_prot
.destroy
= chtls_destroy_sock
;
556 chtls_cpl_prot
.shutdown
= chtls_shutdown
;
557 chtls_cpl_prot
.sendmsg
= chtls_sendmsg
;
558 chtls_cpl_prot
.sendpage
= chtls_sendpage
;
559 chtls_cpl_prot
.recvmsg
= chtls_recvmsg
;
560 chtls_cpl_prot
.setsockopt
= chtls_setsockopt
;
561 chtls_cpl_prot
.getsockopt
= chtls_getsockopt
;
564 static int __init
chtls_register(void)
566 chtls_init_ulp_ops();
567 register_listen_notifier(&listen_notifier
);
568 cxgb4_register_uld(CXGB4_ULD_TLS
, &chtls_uld_info
);
572 static void __exit
chtls_unregister(void)
574 unregister_listen_notifier(&listen_notifier
);
575 chtls_free_all_uld();
576 cxgb4_unregister_uld(CXGB4_ULD_TLS
);
579 module_init(chtls_register
);
580 module_exit(chtls_unregister
);
582 MODULE_DESCRIPTION("Chelsio TLS Inline driver");
583 MODULE_LICENSE("GPL");
584 MODULE_AUTHOR("Chelsio Communications");
585 MODULE_VERSION(DRV_VERSION
);