blob: 931b96c220af973f450cf0b34a37924d40656e9f [file] [log] [blame]
Atul Guptacc35c88a2018-03-31 21:41:59 +05301/*
2 * Copyright (c) 2018 Chelsio Communications, Inc.
3 *
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.
7 *
8 * Written by: Atul Gupta (atul.gupta@chelsio.com)
9 */
10
11#include <linux/module.h>
12#include <linux/list.h>
13#include <linux/workqueue.h>
14#include <linux/skbuff.h>
15#include <linux/timer.h>
16#include <linux/notifier.h>
17#include <linux/inetdevice.h>
18#include <linux/ip.h>
19#include <linux/tcp.h>
20#include <linux/sched/signal.h>
21#include <linux/kallsyms.h>
22#include <linux/kprobes.h>
23#include <linux/if_vlan.h>
Atul Gupta0c3a16b2018-12-11 02:20:53 -080024#include <net/inet_common.h>
Atul Guptacc35c88a2018-03-31 21:41:59 +053025#include <net/tcp.h>
26#include <net/dst.h>
27
28#include "chtls.h"
29#include "chtls_cm.h"
30
31/*
32 * State transitions and actions for close. Note that if we are in SYN_SENT
33 * we remain in that state as we cannot control a connection while it's in
34 * SYN_SENT; such connections are allowed to establish and are then aborted.
35 */
36static unsigned char new_state[16] = {
37 /* current state: new state: action: */
38 /* (Invalid) */ TCP_CLOSE,
39 /* TCP_ESTABLISHED */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
40 /* TCP_SYN_SENT */ TCP_SYN_SENT,
41 /* TCP_SYN_RECV */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
42 /* TCP_FIN_WAIT1 */ TCP_FIN_WAIT1,
43 /* TCP_FIN_WAIT2 */ TCP_FIN_WAIT2,
44 /* TCP_TIME_WAIT */ TCP_CLOSE,
45 /* TCP_CLOSE */ TCP_CLOSE,
46 /* TCP_CLOSE_WAIT */ TCP_LAST_ACK | TCP_ACTION_FIN,
47 /* TCP_LAST_ACK */ TCP_LAST_ACK,
48 /* TCP_LISTEN */ TCP_CLOSE,
49 /* TCP_CLOSING */ TCP_CLOSING,
50};
51
52static struct chtls_sock *chtls_sock_create(struct chtls_dev *cdev)
53{
54 struct chtls_sock *csk = kzalloc(sizeof(*csk), GFP_ATOMIC);
55
56 if (!csk)
57 return NULL;
58
59 csk->txdata_skb_cache = alloc_skb(TXDATA_SKB_LEN, GFP_ATOMIC);
60 if (!csk->txdata_skb_cache) {
61 kfree(csk);
62 return NULL;
63 }
64
65 kref_init(&csk->kref);
66 csk->cdev = cdev;
67 skb_queue_head_init(&csk->txq);
68 csk->wr_skb_head = NULL;
69 csk->wr_skb_tail = NULL;
70 csk->mss = MAX_MSS;
71 csk->tlshws.ofld = 1;
72 csk->tlshws.txkey = -1;
73 csk->tlshws.rxkey = -1;
74 csk->tlshws.mfs = TLS_MFS;
75 skb_queue_head_init(&csk->tlshws.sk_recv_queue);
76 return csk;
77}
78
79static void chtls_sock_release(struct kref *ref)
80{
81 struct chtls_sock *csk =
82 container_of(ref, struct chtls_sock, kref);
83
84 kfree(csk);
85}
86
87static struct net_device *chtls_ipv4_netdev(struct chtls_dev *cdev,
88 struct sock *sk)
89{
90 struct net_device *ndev = cdev->ports[0];
91
92 if (likely(!inet_sk(sk)->inet_rcv_saddr))
93 return ndev;
94
95 ndev = ip_dev_find(&init_net, inet_sk(sk)->inet_rcv_saddr);
96 if (!ndev)
97 return NULL;
98
99 if (is_vlan_dev(ndev))
100 return vlan_dev_real_dev(ndev);
101 return ndev;
102}
103
104static void assign_rxopt(struct sock *sk, unsigned int opt)
105{
106 const struct chtls_dev *cdev;
107 struct chtls_sock *csk;
108 struct tcp_sock *tp;
109
110 csk = rcu_dereference_sk_user_data(sk);
111 tp = tcp_sk(sk);
112
113 cdev = csk->cdev;
114 tp->tcp_header_len = sizeof(struct tcphdr);
115 tp->rx_opt.mss_clamp = cdev->mtus[TCPOPT_MSS_G(opt)] - 40;
116 tp->mss_cache = tp->rx_opt.mss_clamp;
117 tp->rx_opt.tstamp_ok = TCPOPT_TSTAMP_G(opt);
118 tp->rx_opt.snd_wscale = TCPOPT_SACK_G(opt);
119 tp->rx_opt.wscale_ok = TCPOPT_WSCALE_OK_G(opt);
120 SND_WSCALE(tp) = TCPOPT_SND_WSCALE_G(opt);
121 if (!tp->rx_opt.wscale_ok)
122 tp->rx_opt.rcv_wscale = 0;
123 if (tp->rx_opt.tstamp_ok) {
124 tp->tcp_header_len += TCPOLEN_TSTAMP_ALIGNED;
125 tp->rx_opt.mss_clamp -= TCPOLEN_TSTAMP_ALIGNED;
126 } else if (csk->opt2 & TSTAMPS_EN_F) {
127 csk->opt2 &= ~TSTAMPS_EN_F;
128 csk->mtu_idx = TCPOPT_MSS_G(opt);
129 }
130}
131
132static void chtls_purge_receive_queue(struct sock *sk)
133{
134 struct sk_buff *skb;
135
136 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
137 skb_dst_set(skb, (void *)NULL);
138 kfree_skb(skb);
139 }
140}
141
142static void chtls_purge_write_queue(struct sock *sk)
143{
144 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
145 struct sk_buff *skb;
146
147 while ((skb = __skb_dequeue(&csk->txq))) {
148 sk->sk_wmem_queued -= skb->truesize;
149 __kfree_skb(skb);
150 }
151}
152
153static void chtls_purge_recv_queue(struct sock *sk)
154{
155 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
156 struct chtls_hws *tlsk = &csk->tlshws;
157 struct sk_buff *skb;
158
159 while ((skb = __skb_dequeue(&tlsk->sk_recv_queue)) != NULL) {
160 skb_dst_set(skb, NULL);
161 kfree_skb(skb);
162 }
163}
164
165static void abort_arp_failure(void *handle, struct sk_buff *skb)
166{
167 struct cpl_abort_req *req = cplhdr(skb);
168 struct chtls_dev *cdev;
169
170 cdev = (struct chtls_dev *)handle;
171 req->cmd = CPL_ABORT_NO_RST;
172 cxgb4_ofld_send(cdev->lldi->ports[0], skb);
173}
174
175static struct sk_buff *alloc_ctrl_skb(struct sk_buff *skb, int len)
176{
177 if (likely(skb && !skb_shared(skb) && !skb_cloned(skb))) {
178 __skb_trim(skb, 0);
179 refcount_add(2, &skb->users);
180 } else {
181 skb = alloc_skb(len, GFP_KERNEL | __GFP_NOFAIL);
182 }
183 return skb;
184}
185
186static void chtls_send_abort(struct sock *sk, int mode, struct sk_buff *skb)
187{
188 struct cpl_abort_req *req;
189 struct chtls_sock *csk;
190 struct tcp_sock *tp;
191
192 csk = rcu_dereference_sk_user_data(sk);
193 tp = tcp_sk(sk);
194
195 if (!skb)
196 skb = alloc_ctrl_skb(csk->txdata_skb_cache, sizeof(*req));
197
198 req = (struct cpl_abort_req *)skb_put(skb, sizeof(*req));
199 INIT_TP_WR_CPL(req, CPL_ABORT_REQ, csk->tid);
200 skb_set_queue_mapping(skb, (csk->txq_idx << 1) | CPL_PRIORITY_DATA);
201 req->rsvd0 = htonl(tp->snd_nxt);
202 req->rsvd1 = !csk_flag_nochk(csk, CSK_TX_DATA_SENT);
203 req->cmd = mode;
204 t4_set_arp_err_handler(skb, csk->cdev, abort_arp_failure);
205 send_or_defer(sk, tp, skb, mode == CPL_ABORT_SEND_RST);
206}
207
208static void chtls_send_reset(struct sock *sk, int mode, struct sk_buff *skb)
209{
210 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
211
212 if (unlikely(csk_flag_nochk(csk, CSK_ABORT_SHUTDOWN) ||
213 !csk->cdev)) {
214 if (sk->sk_state == TCP_SYN_RECV)
215 csk_set_flag(csk, CSK_RST_ABORTED);
216 goto out;
217 }
218
219 if (!csk_flag_nochk(csk, CSK_TX_DATA_SENT)) {
220 struct tcp_sock *tp = tcp_sk(sk);
221
222 if (send_tx_flowc_wr(sk, 0, tp->snd_nxt, tp->rcv_nxt) < 0)
223 WARN_ONCE(1, "send tx flowc error");
224 csk_set_flag(csk, CSK_TX_DATA_SENT);
225 }
226
227 csk_set_flag(csk, CSK_ABORT_RPL_PENDING);
228 chtls_purge_write_queue(sk);
229
230 csk_set_flag(csk, CSK_ABORT_SHUTDOWN);
231 if (sk->sk_state != TCP_SYN_RECV)
232 chtls_send_abort(sk, mode, skb);
233 else
234 goto out;
235
236 return;
237out:
zhong jiangce1294d2018-09-20 17:57:16 +0800238 kfree_skb(skb);
Atul Guptacc35c88a2018-03-31 21:41:59 +0530239}
240
241static void release_tcp_port(struct sock *sk)
242{
243 if (inet_csk(sk)->icsk_bind_hash)
244 inet_put_port(sk);
245}
246
247static void tcp_uncork(struct sock *sk)
248{
249 struct tcp_sock *tp = tcp_sk(sk);
250
251 if (tp->nonagle & TCP_NAGLE_CORK) {
252 tp->nonagle &= ~TCP_NAGLE_CORK;
253 chtls_tcp_push(sk, 0);
254 }
255}
256
257static void chtls_close_conn(struct sock *sk)
258{
259 struct cpl_close_con_req *req;
260 struct chtls_sock *csk;
261 struct sk_buff *skb;
262 unsigned int tid;
263 unsigned int len;
264
265 len = roundup(sizeof(struct cpl_close_con_req), 16);
266 csk = rcu_dereference_sk_user_data(sk);
267 tid = csk->tid;
268
269 skb = alloc_skb(len, GFP_KERNEL | __GFP_NOFAIL);
270 req = (struct cpl_close_con_req *)__skb_put(skb, len);
271 memset(req, 0, len);
272 req->wr.wr_hi = htonl(FW_WR_OP_V(FW_TP_WR) |
273 FW_WR_IMMDLEN_V(sizeof(*req) -
274 sizeof(req->wr)));
275 req->wr.wr_mid = htonl(FW_WR_LEN16_V(DIV_ROUND_UP(sizeof(*req), 16)) |
276 FW_WR_FLOWID_V(tid));
277
278 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
279
280 tcp_uncork(sk);
281 skb_entail(sk, skb, ULPCB_FLAG_NO_HDR | ULPCB_FLAG_NO_APPEND);
282 if (sk->sk_state != TCP_SYN_SENT)
283 chtls_push_frames(csk, 1);
284}
285
286/*
287 * Perform a state transition during close and return the actions indicated
288 * for the transition. Do not make this function inline, the main reason
289 * it exists at all is to avoid multiple inlining of tcp_set_state.
290 */
291static int make_close_transition(struct sock *sk)
292{
293 int next = (int)new_state[sk->sk_state];
294
295 tcp_set_state(sk, next & TCP_STATE_MASK);
296 return next & TCP_ACTION_FIN;
297}
298
299void chtls_close(struct sock *sk, long timeout)
300{
301 int data_lost, prev_state;
302 struct chtls_sock *csk;
303
304 csk = rcu_dereference_sk_user_data(sk);
305
306 lock_sock(sk);
307 sk->sk_shutdown |= SHUTDOWN_MASK;
308
309 data_lost = skb_queue_len(&sk->sk_receive_queue);
310 data_lost |= skb_queue_len(&csk->tlshws.sk_recv_queue);
311 chtls_purge_recv_queue(sk);
312 chtls_purge_receive_queue(sk);
313
314 if (sk->sk_state == TCP_CLOSE) {
315 goto wait;
316 } else if (data_lost || sk->sk_state == TCP_SYN_SENT) {
317 chtls_send_reset(sk, CPL_ABORT_SEND_RST, NULL);
318 release_tcp_port(sk);
319 goto unlock;
320 } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
321 sk->sk_prot->disconnect(sk, 0);
322 } else if (make_close_transition(sk)) {
323 chtls_close_conn(sk);
324 }
325wait:
326 if (timeout)
327 sk_stream_wait_close(sk, timeout);
328
329unlock:
330 prev_state = sk->sk_state;
331 sock_hold(sk);
332 sock_orphan(sk);
333
334 release_sock(sk);
335
336 local_bh_disable();
337 bh_lock_sock(sk);
338
339 if (prev_state != TCP_CLOSE && sk->sk_state == TCP_CLOSE)
340 goto out;
341
342 if (sk->sk_state == TCP_FIN_WAIT2 && tcp_sk(sk)->linger2 < 0 &&
343 !csk_flag(sk, CSK_ABORT_SHUTDOWN)) {
344 struct sk_buff *skb;
345
346 skb = alloc_skb(sizeof(struct cpl_abort_req), GFP_ATOMIC);
347 if (skb)
348 chtls_send_reset(sk, CPL_ABORT_SEND_RST, skb);
349 }
350
351 if (sk->sk_state == TCP_CLOSE)
352 inet_csk_destroy_sock(sk);
353
354out:
355 bh_unlock_sock(sk);
356 local_bh_enable();
357 sock_put(sk);
358}
359
360/*
361 * Wait until a socket enters on of the given states.
362 */
363static int wait_for_states(struct sock *sk, unsigned int states)
364{
365 DECLARE_WAITQUEUE(wait, current);
366 struct socket_wq _sk_wq;
367 long current_timeo;
368 int err = 0;
369
370 current_timeo = 200;
371
372 /*
373 * We want this to work even when there's no associated struct socket.
374 * In that case we provide a temporary wait_queue_head_t.
375 */
376 if (!sk->sk_wq) {
377 init_waitqueue_head(&_sk_wq.wait);
378 _sk_wq.fasync_list = NULL;
379 init_rcu_head_on_stack(&_sk_wq.rcu);
380 RCU_INIT_POINTER(sk->sk_wq, &_sk_wq);
381 }
382
383 add_wait_queue(sk_sleep(sk), &wait);
384 while (!sk_in_state(sk, states)) {
385 if (!current_timeo) {
386 err = -EBUSY;
387 break;
388 }
389 if (signal_pending(current)) {
390 err = sock_intr_errno(current_timeo);
391 break;
392 }
393 set_current_state(TASK_UNINTERRUPTIBLE);
394 release_sock(sk);
395 if (!sk_in_state(sk, states))
396 current_timeo = schedule_timeout(current_timeo);
397 __set_current_state(TASK_RUNNING);
398 lock_sock(sk);
399 }
400 remove_wait_queue(sk_sleep(sk), &wait);
401
402 if (rcu_dereference(sk->sk_wq) == &_sk_wq)
403 sk->sk_wq = NULL;
404 return err;
405}
406
407int chtls_disconnect(struct sock *sk, int flags)
408{
Atul Guptacc35c88a2018-03-31 21:41:59 +0530409 struct tcp_sock *tp;
410 int err;
411
412 tp = tcp_sk(sk);
Atul Guptacc35c88a2018-03-31 21:41:59 +0530413 chtls_purge_recv_queue(sk);
414 chtls_purge_receive_queue(sk);
415 chtls_purge_write_queue(sk);
416
417 if (sk->sk_state != TCP_CLOSE) {
418 sk->sk_err = ECONNRESET;
419 chtls_send_reset(sk, CPL_ABORT_SEND_RST, NULL);
420 err = wait_for_states(sk, TCPF_CLOSE);
421 if (err)
422 return err;
423 }
424 chtls_purge_recv_queue(sk);
425 chtls_purge_receive_queue(sk);
426 tp->max_window = 0xFFFF << (tp->rx_opt.snd_wscale);
427 return tcp_disconnect(sk, flags);
428}
429
430#define SHUTDOWN_ELIGIBLE_STATE (TCPF_ESTABLISHED | \
431 TCPF_SYN_RECV | TCPF_CLOSE_WAIT)
432void chtls_shutdown(struct sock *sk, int how)
433{
434 if ((how & SEND_SHUTDOWN) &&
435 sk_in_state(sk, SHUTDOWN_ELIGIBLE_STATE) &&
436 make_close_transition(sk))
437 chtls_close_conn(sk);
438}
439
440void chtls_destroy_sock(struct sock *sk)
441{
442 struct chtls_sock *csk;
443
444 csk = rcu_dereference_sk_user_data(sk);
445 chtls_purge_recv_queue(sk);
446 csk->ulp_mode = ULP_MODE_NONE;
447 chtls_purge_write_queue(sk);
448 free_tls_keyid(sk);
449 kref_put(&csk->kref, chtls_sock_release);
450 sk->sk_prot = &tcp_prot;
451 sk->sk_prot->destroy(sk);
452}
453
454static void reset_listen_child(struct sock *child)
455{
456 struct chtls_sock *csk = rcu_dereference_sk_user_data(child);
457 struct sk_buff *skb;
458
459 skb = alloc_ctrl_skb(csk->txdata_skb_cache,
460 sizeof(struct cpl_abort_req));
461
462 chtls_send_reset(child, CPL_ABORT_SEND_RST, skb);
463 sock_orphan(child);
464 INC_ORPHAN_COUNT(child);
465 if (child->sk_state == TCP_CLOSE)
466 inet_csk_destroy_sock(child);
467}
468
469static void chtls_disconnect_acceptq(struct sock *listen_sk)
470{
471 struct request_sock **pprev;
472
473 pprev = ACCEPT_QUEUE(listen_sk);
474 while (*pprev) {
475 struct request_sock *req = *pprev;
476
477 if (req->rsk_ops == &chtls_rsk_ops) {
478 struct sock *child = req->sk;
479
480 *pprev = req->dl_next;
481 sk_acceptq_removed(listen_sk);
482 reqsk_put(req);
483 sock_hold(child);
484 local_bh_disable();
485 bh_lock_sock(child);
486 release_tcp_port(child);
487 reset_listen_child(child);
488 bh_unlock_sock(child);
489 local_bh_enable();
490 sock_put(child);
491 } else {
492 pprev = &req->dl_next;
493 }
494 }
495}
496
497static int listen_hashfn(const struct sock *sk)
498{
499 return ((unsigned long)sk >> 10) & (LISTEN_INFO_HASH_SIZE - 1);
500}
501
502static struct listen_info *listen_hash_add(struct chtls_dev *cdev,
503 struct sock *sk,
504 unsigned int stid)
505{
506 struct listen_info *p = kmalloc(sizeof(*p), GFP_KERNEL);
507
508 if (p) {
509 int key = listen_hashfn(sk);
510
511 p->sk = sk;
512 p->stid = stid;
513 spin_lock(&cdev->listen_lock);
514 p->next = cdev->listen_hash_tab[key];
515 cdev->listen_hash_tab[key] = p;
516 spin_unlock(&cdev->listen_lock);
517 }
518 return p;
519}
520
521static int listen_hash_find(struct chtls_dev *cdev,
522 struct sock *sk)
523{
524 struct listen_info *p;
525 int stid = -1;
526 int key;
527
528 key = listen_hashfn(sk);
529
530 spin_lock(&cdev->listen_lock);
531 for (p = cdev->listen_hash_tab[key]; p; p = p->next)
532 if (p->sk == sk) {
533 stid = p->stid;
534 break;
535 }
536 spin_unlock(&cdev->listen_lock);
537 return stid;
538}
539
540static int listen_hash_del(struct chtls_dev *cdev,
541 struct sock *sk)
542{
543 struct listen_info *p, **prev;
544 int stid = -1;
545 int key;
546
547 key = listen_hashfn(sk);
548 prev = &cdev->listen_hash_tab[key];
549
550 spin_lock(&cdev->listen_lock);
551 for (p = *prev; p; prev = &p->next, p = p->next)
552 if (p->sk == sk) {
553 stid = p->stid;
554 *prev = p->next;
555 kfree(p);
556 break;
557 }
558 spin_unlock(&cdev->listen_lock);
559 return stid;
560}
561
562static void cleanup_syn_rcv_conn(struct sock *child, struct sock *parent)
563{
564 struct request_sock *req;
565 struct chtls_sock *csk;
566
567 csk = rcu_dereference_sk_user_data(child);
568 req = csk->passive_reap_next;
569
570 reqsk_queue_removed(&inet_csk(parent)->icsk_accept_queue, req);
571 __skb_unlink((struct sk_buff *)&csk->synq, &csk->listen_ctx->synq);
572 chtls_reqsk_free(req);
573 csk->passive_reap_next = NULL;
574}
575
576static void chtls_reset_synq(struct listen_ctx *listen_ctx)
577{
578 struct sock *listen_sk = listen_ctx->lsk;
579
580 while (!skb_queue_empty(&listen_ctx->synq)) {
581 struct chtls_sock *csk =
582 container_of((struct synq *)__skb_dequeue
583 (&listen_ctx->synq), struct chtls_sock, synq);
584 struct sock *child = csk->sk;
585
586 cleanup_syn_rcv_conn(child, listen_sk);
587 sock_hold(child);
588 local_bh_disable();
589 bh_lock_sock(child);
590 release_tcp_port(child);
591 reset_listen_child(child);
592 bh_unlock_sock(child);
593 local_bh_enable();
594 sock_put(child);
595 }
596}
597
598int chtls_listen_start(struct chtls_dev *cdev, struct sock *sk)
599{
600 struct net_device *ndev;
601 struct listen_ctx *ctx;
602 struct adapter *adap;
603 struct port_info *pi;
604 int stid;
605 int ret;
606
607 if (sk->sk_family != PF_INET)
608 return -EAGAIN;
609
610 rcu_read_lock();
611 ndev = chtls_ipv4_netdev(cdev, sk);
612 rcu_read_unlock();
613 if (!ndev)
614 return -EBADF;
615
616 pi = netdev_priv(ndev);
617 adap = pi->adapter;
618 if (!(adap->flags & FULL_INIT_DONE))
619 return -EBADF;
620
621 if (listen_hash_find(cdev, sk) >= 0) /* already have it */
622 return -EADDRINUSE;
623
624 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
625 if (!ctx)
626 return -ENOMEM;
627
628 __module_get(THIS_MODULE);
629 ctx->lsk = sk;
630 ctx->cdev = cdev;
631 ctx->state = T4_LISTEN_START_PENDING;
632 skb_queue_head_init(&ctx->synq);
633
634 stid = cxgb4_alloc_stid(cdev->tids, sk->sk_family, ctx);
635 if (stid < 0)
636 goto free_ctx;
637
638 sock_hold(sk);
639 if (!listen_hash_add(cdev, sk, stid))
640 goto free_stid;
641
642 ret = cxgb4_create_server(ndev, stid,
643 inet_sk(sk)->inet_rcv_saddr,
644 inet_sk(sk)->inet_sport, 0,
645 cdev->lldi->rxq_ids[0]);
646 if (ret > 0)
647 ret = net_xmit_errno(ret);
648 if (ret)
649 goto del_hash;
650 return 0;
651del_hash:
652 listen_hash_del(cdev, sk);
653free_stid:
654 cxgb4_free_stid(cdev->tids, stid, sk->sk_family);
655 sock_put(sk);
656free_ctx:
657 kfree(ctx);
658 module_put(THIS_MODULE);
659 return -EBADF;
660}
661
662void chtls_listen_stop(struct chtls_dev *cdev, struct sock *sk)
663{
664 struct listen_ctx *listen_ctx;
665 int stid;
666
667 stid = listen_hash_del(cdev, sk);
668 if (stid < 0)
669 return;
670
671 listen_ctx = (struct listen_ctx *)lookup_stid(cdev->tids, stid);
672 chtls_reset_synq(listen_ctx);
673
674 cxgb4_remove_server(cdev->lldi->ports[0], stid,
675 cdev->lldi->rxq_ids[0], 0);
676 chtls_disconnect_acceptq(sk);
677}
678
679static int chtls_pass_open_rpl(struct chtls_dev *cdev, struct sk_buff *skb)
680{
681 struct cpl_pass_open_rpl *rpl = cplhdr(skb) + RSS_HDR;
682 unsigned int stid = GET_TID(rpl);
683 struct listen_ctx *listen_ctx;
684
685 listen_ctx = (struct listen_ctx *)lookup_stid(cdev->tids, stid);
686 if (!listen_ctx)
687 return CPL_RET_BUF_DONE;
688
689 if (listen_ctx->state == T4_LISTEN_START_PENDING) {
690 listen_ctx->state = T4_LISTEN_STARTED;
691 return CPL_RET_BUF_DONE;
692 }
693
694 if (rpl->status != CPL_ERR_NONE) {
695 pr_info("Unexpected PASS_OPEN_RPL status %u for STID %u\n",
696 rpl->status, stid);
697 return CPL_RET_BUF_DONE;
698 }
699 cxgb4_free_stid(cdev->tids, stid, listen_ctx->lsk->sk_family);
700 sock_put(listen_ctx->lsk);
701 kfree(listen_ctx);
702 module_put(THIS_MODULE);
703
704 return 0;
705}
706
707static int chtls_close_listsrv_rpl(struct chtls_dev *cdev, struct sk_buff *skb)
708{
709 struct cpl_close_listsvr_rpl *rpl = cplhdr(skb) + RSS_HDR;
710 struct listen_ctx *listen_ctx;
711 unsigned int stid;
712 void *data;
713
714 stid = GET_TID(rpl);
715 data = lookup_stid(cdev->tids, stid);
716 listen_ctx = (struct listen_ctx *)data;
717
718 if (rpl->status != CPL_ERR_NONE) {
719 pr_info("Unexpected CLOSE_LISTSRV_RPL status %u for STID %u\n",
720 rpl->status, stid);
721 return CPL_RET_BUF_DONE;
722 }
723
724 cxgb4_free_stid(cdev->tids, stid, listen_ctx->lsk->sk_family);
725 sock_put(listen_ctx->lsk);
726 kfree(listen_ctx);
727 module_put(THIS_MODULE);
728
729 return 0;
730}
731
732static void chtls_release_resources(struct sock *sk)
733{
734 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
735 struct chtls_dev *cdev = csk->cdev;
736 unsigned int tid = csk->tid;
737 struct tid_info *tids;
738
739 if (!cdev)
740 return;
741
742 tids = cdev->tids;
743 kfree_skb(csk->txdata_skb_cache);
744 csk->txdata_skb_cache = NULL;
745
746 if (csk->l2t_entry) {
747 cxgb4_l2t_release(csk->l2t_entry);
748 csk->l2t_entry = NULL;
749 }
750
751 cxgb4_remove_tid(tids, csk->port_id, tid, sk->sk_family);
752 sock_put(sk);
753}
754
755static void chtls_conn_done(struct sock *sk)
756{
757 if (sock_flag(sk, SOCK_DEAD))
758 chtls_purge_receive_queue(sk);
759 sk_wakeup_sleepers(sk, 0);
760 tcp_done(sk);
761}
762
763static void do_abort_syn_rcv(struct sock *child, struct sock *parent)
764{
765 /*
766 * If the server is still open we clean up the child connection,
767 * otherwise the server already did the clean up as it was purging
768 * its SYN queue and the skb was just sitting in its backlog.
769 */
770 if (likely(parent->sk_state == TCP_LISTEN)) {
771 cleanup_syn_rcv_conn(child, parent);
772 /* Without the below call to sock_orphan,
773 * we leak the socket resource with syn_flood test
774 * as inet_csk_destroy_sock will not be called
775 * in tcp_done since SOCK_DEAD flag is not set.
776 * Kernel handles this differently where new socket is
777 * created only after 3 way handshake is done.
778 */
779 sock_orphan(child);
780 percpu_counter_inc((child)->sk_prot->orphan_count);
781 chtls_release_resources(child);
782 chtls_conn_done(child);
783 } else {
784 if (csk_flag(child, CSK_RST_ABORTED)) {
785 chtls_release_resources(child);
786 chtls_conn_done(child);
787 }
788 }
789}
790
791static void pass_open_abort(struct sock *child, struct sock *parent,
792 struct sk_buff *skb)
793{
794 do_abort_syn_rcv(child, parent);
795 kfree_skb(skb);
796}
797
798static void bl_pass_open_abort(struct sock *lsk, struct sk_buff *skb)
799{
800 pass_open_abort(skb->sk, lsk, skb);
801}
802
803static void chtls_pass_open_arp_failure(struct sock *sk,
804 struct sk_buff *skb)
805{
806 const struct request_sock *oreq;
807 struct chtls_sock *csk;
808 struct chtls_dev *cdev;
809 struct sock *parent;
810 void *data;
811
812 csk = rcu_dereference_sk_user_data(sk);
813 cdev = csk->cdev;
814
815 /*
816 * If the connection is being aborted due to the parent listening
817 * socket going away there's nothing to do, the ABORT_REQ will close
818 * the connection.
819 */
820 if (csk_flag(sk, CSK_ABORT_RPL_PENDING)) {
821 kfree_skb(skb);
822 return;
823 }
824
825 oreq = csk->passive_reap_next;
826 data = lookup_stid(cdev->tids, oreq->ts_recent);
827 parent = ((struct listen_ctx *)data)->lsk;
828
829 bh_lock_sock(parent);
830 if (!sock_owned_by_user(parent)) {
831 pass_open_abort(sk, parent, skb);
832 } else {
833 BLOG_SKB_CB(skb)->backlog_rcv = bl_pass_open_abort;
834 __sk_add_backlog(parent, skb);
835 }
836 bh_unlock_sock(parent);
837}
838
839static void chtls_accept_rpl_arp_failure(void *handle,
840 struct sk_buff *skb)
841{
842 struct sock *sk = (struct sock *)handle;
843
844 sock_hold(sk);
845 process_cpl_msg(chtls_pass_open_arp_failure, sk, skb);
846 sock_put(sk);
847}
848
849static unsigned int chtls_select_mss(const struct chtls_sock *csk,
850 unsigned int pmtu,
851 struct cpl_pass_accept_req *req)
852{
853 struct chtls_dev *cdev;
854 struct dst_entry *dst;
855 unsigned int tcpoptsz;
856 unsigned int iphdrsz;
857 unsigned int mtu_idx;
858 struct tcp_sock *tp;
859 unsigned int mss;
860 struct sock *sk;
861
862 mss = ntohs(req->tcpopt.mss);
863 sk = csk->sk;
864 dst = __sk_dst_get(sk);
865 cdev = csk->cdev;
866 tp = tcp_sk(sk);
867 tcpoptsz = 0;
868
869 iphdrsz = sizeof(struct iphdr) + sizeof(struct tcphdr);
870 if (req->tcpopt.tstamp)
871 tcpoptsz += round_up(TCPOLEN_TIMESTAMP, 4);
872
873 tp->advmss = dst_metric_advmss(dst);
874 if (USER_MSS(tp) && tp->advmss > USER_MSS(tp))
875 tp->advmss = USER_MSS(tp);
876 if (tp->advmss > pmtu - iphdrsz)
877 tp->advmss = pmtu - iphdrsz;
878 if (mss && tp->advmss > mss)
879 tp->advmss = mss;
880
881 tp->advmss = cxgb4_best_aligned_mtu(cdev->lldi->mtus,
882 iphdrsz + tcpoptsz,
883 tp->advmss - tcpoptsz,
884 8, &mtu_idx);
885 tp->advmss -= iphdrsz;
886
887 inet_csk(sk)->icsk_pmtu_cookie = pmtu;
888 return mtu_idx;
889}
890
Atul Guptacc35c88a2018-03-31 21:41:59 +0530891static unsigned int select_rcv_wscale(int space, int wscale_ok, int win_clamp)
892{
893 int wscale = 0;
894
895 if (space > MAX_RCV_WND)
896 space = MAX_RCV_WND;
897 if (win_clamp && win_clamp < space)
898 space = win_clamp;
899
900 if (wscale_ok) {
901 while (wscale < 14 && (65535 << wscale) < space)
902 wscale++;
903 }
904 return wscale;
905}
906
907static void chtls_pass_accept_rpl(struct sk_buff *skb,
908 struct cpl_pass_accept_req *req,
909 unsigned int tid)
910
911{
912 struct cpl_t5_pass_accept_rpl *rpl5;
913 struct cxgb4_lld_info *lldi;
914 const struct tcphdr *tcph;
915 const struct tcp_sock *tp;
916 struct chtls_sock *csk;
917 unsigned int len;
918 struct sock *sk;
919 u32 opt2, hlen;
920 u64 opt0;
921
922 sk = skb->sk;
923 tp = tcp_sk(sk);
924 csk = sk->sk_user_data;
925 csk->tid = tid;
926 lldi = csk->cdev->lldi;
927 len = roundup(sizeof(*rpl5), 16);
928
929 rpl5 = __skb_put_zero(skb, len);
930 INIT_TP_WR(rpl5, tid);
931
932 OPCODE_TID(rpl5) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
933 csk->tid));
934 csk->mtu_idx = chtls_select_mss(csk, dst_mtu(__sk_dst_get(sk)),
935 req);
936 opt0 = TCAM_BYPASS_F |
Atul Gupta0c3a16b2018-12-11 02:20:53 -0800937 WND_SCALE_V(RCV_WSCALE(tp)) |
Atul Guptacc35c88a2018-03-31 21:41:59 +0530938 MSS_IDX_V(csk->mtu_idx) |
939 L2T_IDX_V(csk->l2t_entry->idx) |
940 NAGLE_V(!(tp->nonagle & TCP_NAGLE_OFF)) |
941 TX_CHAN_V(csk->tx_chan) |
942 SMAC_SEL_V(csk->smac_idx) |
943 DSCP_V(csk->tos >> 2) |
944 ULP_MODE_V(ULP_MODE_TLS) |
945 RCV_BUFSIZ_V(min(tp->rcv_wnd >> 10, RCV_BUFSIZ_M));
946
947 opt2 = RX_CHANNEL_V(0) |
948 RSS_QUEUE_VALID_F | RSS_QUEUE_V(csk->rss_qid);
949
950 if (!is_t5(lldi->adapter_type))
951 opt2 |= RX_FC_DISABLE_F;
952 if (req->tcpopt.tstamp)
953 opt2 |= TSTAMPS_EN_F;
954 if (req->tcpopt.sack)
955 opt2 |= SACK_EN_F;
956 hlen = ntohl(req->hdr_len);
957
958 tcph = (struct tcphdr *)((u8 *)(req + 1) +
959 T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen));
960 if (tcph->ece && tcph->cwr)
961 opt2 |= CCTRL_ECN_V(1);
962 opt2 |= CONG_CNTRL_V(CONG_ALG_NEWRENO);
963 opt2 |= T5_ISS_F;
964 opt2 |= T5_OPT_2_VALID_F;
965 rpl5->opt0 = cpu_to_be64(opt0);
966 rpl5->opt2 = cpu_to_be32(opt2);
967 rpl5->iss = cpu_to_be32((prandom_u32() & ~7UL) - 1);
968 set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->port_id);
969 t4_set_arp_err_handler(skb, sk, chtls_accept_rpl_arp_failure);
970 cxgb4_l2t_send(csk->egress_dev, skb, csk->l2t_entry);
971}
972
973static void inet_inherit_port(struct inet_hashinfo *hash_info,
974 struct sock *lsk, struct sock *newsk)
975{
976 local_bh_disable();
977 __inet_inherit_port(lsk, newsk);
978 local_bh_enable();
979}
980
981static int chtls_backlog_rcv(struct sock *sk, struct sk_buff *skb)
982{
983 if (skb->protocol) {
984 kfree_skb(skb);
985 return 0;
986 }
987 BLOG_SKB_CB(skb)->backlog_rcv(sk, skb);
988 return 0;
989}
990
Atul Gupta0c3a16b2018-12-11 02:20:53 -0800991static void chtls_set_tcp_window(struct chtls_sock *csk)
992{
993 struct net_device *ndev = csk->egress_dev;
994 struct port_info *pi = netdev_priv(ndev);
995 unsigned int linkspeed;
996 u8 scale;
997
998 linkspeed = pi->link_cfg.speed;
999 scale = linkspeed / SPEED_10000;
1000#define CHTLS_10G_RCVWIN (256 * 1024)
1001 csk->rcv_win = CHTLS_10G_RCVWIN;
1002 if (scale)
1003 csk->rcv_win *= scale;
1004#define CHTLS_10G_SNDWIN (256 * 1024)
1005 csk->snd_win = CHTLS_10G_SNDWIN;
1006 if (scale)
1007 csk->snd_win *= scale;
1008}
1009
Atul Guptacc35c88a2018-03-31 21:41:59 +05301010static struct sock *chtls_recv_sock(struct sock *lsk,
1011 struct request_sock *oreq,
1012 void *network_hdr,
1013 const struct cpl_pass_accept_req *req,
1014 struct chtls_dev *cdev)
1015{
Atul Guptacc35c88a2018-03-31 21:41:59 +05301016 struct inet_sock *newinet;
1017 const struct iphdr *iph;
1018 struct net_device *ndev;
1019 struct chtls_sock *csk;
1020 struct dst_entry *dst;
1021 struct neighbour *n;
1022 struct tcp_sock *tp;
1023 struct sock *newsk;
1024 u16 port_id;
1025 int rxq_idx;
1026 int step;
1027
1028 iph = (const struct iphdr *)network_hdr;
1029 newsk = tcp_create_openreq_child(lsk, oreq, cdev->askb);
1030 if (!newsk)
1031 goto free_oreq;
1032
1033 dst = inet_csk_route_child_sock(lsk, newsk, oreq);
1034 if (!dst)
1035 goto free_sk;
1036
Atul Guptacc35c88a2018-03-31 21:41:59 +05301037 n = dst_neigh_lookup(dst, &iph->saddr);
1038 if (!n)
1039 goto free_sk;
1040
1041 ndev = n->dev;
1042 if (!ndev)
1043 goto free_dst;
1044 port_id = cxgb4_port_idx(ndev);
1045
1046 csk = chtls_sock_create(cdev);
1047 if (!csk)
1048 goto free_dst;
1049
1050 csk->l2t_entry = cxgb4_l2t_get(cdev->lldi->l2t, n, ndev, 0);
1051 if (!csk->l2t_entry)
1052 goto free_csk;
1053
1054 newsk->sk_user_data = csk;
1055 newsk->sk_backlog_rcv = chtls_backlog_rcv;
1056
1057 tp = tcp_sk(newsk);
1058 newinet = inet_sk(newsk);
1059
1060 newinet->inet_daddr = iph->saddr;
1061 newinet->inet_rcv_saddr = iph->daddr;
1062 newinet->inet_saddr = iph->daddr;
1063
1064 oreq->ts_recent = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1065 sk_setup_caps(newsk, dst);
1066 csk->sk = newsk;
1067 csk->passive_reap_next = oreq;
1068 csk->tx_chan = cxgb4_port_chan(ndev);
1069 csk->port_id = port_id;
1070 csk->egress_dev = ndev;
1071 csk->tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
Atul Gupta0c3a16b2018-12-11 02:20:53 -08001072 chtls_set_tcp_window(csk);
1073 tp->rcv_wnd = csk->rcv_win;
1074 csk->sndbuf = csk->snd_win;
Atul Guptacc35c88a2018-03-31 21:41:59 +05301075 csk->ulp_mode = ULP_MODE_TLS;
1076 step = cdev->lldi->nrxq / cdev->lldi->nchan;
1077 csk->rss_qid = cdev->lldi->rxq_ids[port_id * step];
1078 rxq_idx = port_id * step;
1079 csk->txq_idx = (rxq_idx < cdev->lldi->ntxq) ? rxq_idx :
1080 port_id * step;
1081 csk->sndbuf = newsk->sk_sndbuf;
1082 csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi->adapter_type,
1083 cxgb4_port_viid(ndev));
Atul Guptacc35c88a2018-03-31 21:41:59 +05301084 RCV_WSCALE(tp) = select_rcv_wscale(tcp_full_space(newsk),
Atul Gupta0c3a16b2018-12-11 02:20:53 -08001085 sock_net(newsk)->
1086 ipv4.sysctl_tcp_window_scaling,
Atul Guptacc35c88a2018-03-31 21:41:59 +05301087 tp->window_clamp);
1088 neigh_release(n);
1089 inet_inherit_port(&tcp_hashinfo, lsk, newsk);
1090 csk_set_flag(csk, CSK_CONN_INLINE);
1091 bh_unlock_sock(newsk); /* tcp_create_openreq_child ->sk_clone_lock */
1092
1093 return newsk;
1094free_csk:
1095 chtls_sock_release(&csk->kref);
1096free_dst:
1097 dst_release(dst);
1098free_sk:
1099 inet_csk_prepare_forced_close(newsk);
1100 tcp_done(newsk);
1101free_oreq:
1102 chtls_reqsk_free(oreq);
1103 return NULL;
1104}
1105
1106/*
1107 * Populate a TID_RELEASE WR. The skb must be already propely sized.
1108 */
1109static void mk_tid_release(struct sk_buff *skb,
1110 unsigned int chan, unsigned int tid)
1111{
1112 struct cpl_tid_release *req;
1113 unsigned int len;
1114
1115 len = roundup(sizeof(struct cpl_tid_release), 16);
1116 req = (struct cpl_tid_release *)__skb_put(skb, len);
1117 memset(req, 0, len);
1118 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1119 INIT_TP_WR_CPL(req, CPL_TID_RELEASE, tid);
1120}
1121
1122static int chtls_get_module(struct sock *sk)
1123{
1124 struct inet_connection_sock *icsk = inet_csk(sk);
1125
1126 if (!try_module_get(icsk->icsk_ulp_ops->owner))
1127 return -1;
1128
1129 return 0;
1130}
1131
1132static void chtls_pass_accept_request(struct sock *sk,
1133 struct sk_buff *skb)
1134{
1135 struct cpl_t5_pass_accept_rpl *rpl;
1136 struct cpl_pass_accept_req *req;
1137 struct listen_ctx *listen_ctx;
Atul Gupta0c3a16b2018-12-11 02:20:53 -08001138 struct vlan_ethhdr *vlan_eh;
Atul Guptacc35c88a2018-03-31 21:41:59 +05301139 struct request_sock *oreq;
1140 struct sk_buff *reply_skb;
1141 struct chtls_sock *csk;
1142 struct chtls_dev *cdev;
1143 struct tcphdr *tcph;
1144 struct sock *newsk;
1145 struct ethhdr *eh;
1146 struct iphdr *iph;
1147 void *network_hdr;
1148 unsigned int stid;
1149 unsigned int len;
1150 unsigned int tid;
Atul Gupta0c3a16b2018-12-11 02:20:53 -08001151 bool th_ecn, ect;
1152 __u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */
1153 u16 eth_hdr_len;
1154 bool ecn_ok;
Atul Guptacc35c88a2018-03-31 21:41:59 +05301155
1156 req = cplhdr(skb) + RSS_HDR;
1157 tid = GET_TID(req);
1158 cdev = BLOG_SKB_CB(skb)->cdev;
1159 newsk = lookup_tid(cdev->tids, tid);
1160 stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1161 if (newsk) {
1162 pr_info("tid (%d) already in use\n", tid);
1163 return;
1164 }
1165
1166 len = roundup(sizeof(*rpl), 16);
1167 reply_skb = alloc_skb(len, GFP_ATOMIC);
1168 if (!reply_skb) {
1169 cxgb4_remove_tid(cdev->tids, 0, tid, sk->sk_family);
1170 kfree_skb(skb);
1171 return;
1172 }
1173
1174 if (sk->sk_state != TCP_LISTEN)
1175 goto reject;
1176
1177 if (inet_csk_reqsk_queue_is_full(sk))
1178 goto reject;
1179
1180 if (sk_acceptq_is_full(sk))
1181 goto reject;
1182
1183 oreq = inet_reqsk_alloc(&chtls_rsk_ops, sk, true);
1184 if (!oreq)
1185 goto reject;
1186
1187 oreq->rsk_rcv_wnd = 0;
1188 oreq->rsk_window_clamp = 0;
1189 oreq->cookie_ts = 0;
1190 oreq->mss = 0;
1191 oreq->ts_recent = 0;
1192
Atul Gupta0c3a16b2018-12-11 02:20:53 -08001193 eth_hdr_len = T6_ETH_HDR_LEN_G(ntohl(req->hdr_len));
1194 if (eth_hdr_len == ETH_HLEN) {
1195 eh = (struct ethhdr *)(req + 1);
1196 iph = (struct iphdr *)(eh + 1);
1197 network_hdr = (void *)(eh + 1);
1198 } else {
1199 vlan_eh = (struct vlan_ethhdr *)(req + 1);
1200 iph = (struct iphdr *)(vlan_eh + 1);
1201 network_hdr = (void *)(vlan_eh + 1);
1202 }
Atul Guptacc35c88a2018-03-31 21:41:59 +05301203 if (iph->version != 0x4)
1204 goto free_oreq;
1205
Atul Guptacc35c88a2018-03-31 21:41:59 +05301206 tcph = (struct tcphdr *)(iph + 1);
Atul Gupta0c3a16b2018-12-11 02:20:53 -08001207 skb_set_network_header(skb, (void *)iph - (void *)req);
Atul Guptacc35c88a2018-03-31 21:41:59 +05301208
1209 tcp_rsk(oreq)->tfo_listener = false;
1210 tcp_rsk(oreq)->rcv_isn = ntohl(tcph->seq);
1211 chtls_set_req_port(oreq, tcph->source, tcph->dest);
Atul Guptacc35c88a2018-03-31 21:41:59 +05301212 chtls_set_req_addr(oreq, iph->daddr, iph->saddr);
Atul Gupta0c3a16b2018-12-11 02:20:53 -08001213 ip_dsfield = ipv4_get_dsfield(iph);
1214 if (req->tcpopt.wsf <= 14 &&
1215 sock_net(sk)->ipv4.sysctl_tcp_window_scaling) {
Atul Guptacc35c88a2018-03-31 21:41:59 +05301216 inet_rsk(oreq)->wscale_ok = 1;
1217 inet_rsk(oreq)->snd_wscale = req->tcpopt.wsf;
1218 }
1219 inet_rsk(oreq)->ir_iif = sk->sk_bound_dev_if;
Atul Gupta0c3a16b2018-12-11 02:20:53 -08001220 th_ecn = tcph->ece && tcph->cwr;
1221 if (th_ecn) {
1222 ect = !INET_ECN_is_not_ect(ip_dsfield);
1223 ecn_ok = sock_net(sk)->ipv4.sysctl_tcp_ecn;
1224 if ((!ect && ecn_ok) || tcp_ca_needs_ecn(sk))
1225 inet_rsk(oreq)->ecn_ok = 1;
1226 }
Atul Guptacc35c88a2018-03-31 21:41:59 +05301227
1228 newsk = chtls_recv_sock(sk, oreq, network_hdr, req, cdev);
1229 if (!newsk)
1230 goto reject;
1231
1232 if (chtls_get_module(newsk))
1233 goto reject;
1234 inet_csk_reqsk_queue_added(sk);
1235 reply_skb->sk = newsk;
1236 chtls_install_cpl_ops(newsk);
1237 cxgb4_insert_tid(cdev->tids, newsk, tid, newsk->sk_family);
1238 csk = rcu_dereference_sk_user_data(newsk);
1239 listen_ctx = (struct listen_ctx *)lookup_stid(cdev->tids, stid);
1240 csk->listen_ctx = listen_ctx;
1241 __skb_queue_tail(&listen_ctx->synq, (struct sk_buff *)&csk->synq);
1242 chtls_pass_accept_rpl(reply_skb, req, tid);
1243 kfree_skb(skb);
1244 return;
1245
1246free_oreq:
1247 chtls_reqsk_free(oreq);
1248reject:
1249 mk_tid_release(reply_skb, 0, tid);
1250 cxgb4_ofld_send(cdev->lldi->ports[0], reply_skb);
1251 kfree_skb(skb);
1252}
1253
1254/*
1255 * Handle a CPL_PASS_ACCEPT_REQ message.
1256 */
1257static int chtls_pass_accept_req(struct chtls_dev *cdev, struct sk_buff *skb)
1258{
1259 struct cpl_pass_accept_req *req = cplhdr(skb) + RSS_HDR;
1260 struct listen_ctx *ctx;
1261 unsigned int stid;
1262 unsigned int tid;
1263 struct sock *lsk;
1264 void *data;
1265
1266 stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1267 tid = GET_TID(req);
1268
1269 data = lookup_stid(cdev->tids, stid);
1270 if (!data)
1271 return 1;
1272
1273 ctx = (struct listen_ctx *)data;
1274 lsk = ctx->lsk;
1275
1276 if (unlikely(tid >= cdev->tids->ntids)) {
1277 pr_info("passive open TID %u too large\n", tid);
1278 return 1;
1279 }
1280
1281 BLOG_SKB_CB(skb)->cdev = cdev;
1282 process_cpl_msg(chtls_pass_accept_request, lsk, skb);
1283 return 0;
1284}
1285
1286/*
1287 * Completes some final bits of initialization for just established connections
1288 * and changes their state to TCP_ESTABLISHED.
1289 *
1290 * snd_isn here is the ISN after the SYN, i.e., the true ISN + 1.
1291 */
1292static void make_established(struct sock *sk, u32 snd_isn, unsigned int opt)
1293{
1294 struct tcp_sock *tp = tcp_sk(sk);
1295
1296 tp->pushed_seq = snd_isn;
1297 tp->write_seq = snd_isn;
1298 tp->snd_nxt = snd_isn;
1299 tp->snd_una = snd_isn;
1300 inet_sk(sk)->inet_id = tp->write_seq ^ jiffies;
1301 assign_rxopt(sk, opt);
1302
1303 if (tp->rcv_wnd > (RCV_BUFSIZ_M << 10))
1304 tp->rcv_wup -= tp->rcv_wnd - (RCV_BUFSIZ_M << 10);
1305
1306 smp_mb();
1307 tcp_set_state(sk, TCP_ESTABLISHED);
1308}
1309
1310static void chtls_abort_conn(struct sock *sk, struct sk_buff *skb)
1311{
1312 struct sk_buff *abort_skb;
1313
1314 abort_skb = alloc_skb(sizeof(struct cpl_abort_req), GFP_ATOMIC);
1315 if (abort_skb)
1316 chtls_send_reset(sk, CPL_ABORT_SEND_RST, abort_skb);
1317}
1318
1319static struct sock *reap_list;
1320static DEFINE_SPINLOCK(reap_list_lock);
1321
1322/*
1323 * Process the reap list.
1324 */
1325DECLARE_TASK_FUNC(process_reap_list, task_param)
1326{
1327 spin_lock_bh(&reap_list_lock);
1328 while (reap_list) {
1329 struct sock *sk = reap_list;
1330 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
1331
1332 reap_list = csk->passive_reap_next;
1333 csk->passive_reap_next = NULL;
1334 spin_unlock(&reap_list_lock);
1335 sock_hold(sk);
1336
1337 bh_lock_sock(sk);
1338 chtls_abort_conn(sk, NULL);
1339 sock_orphan(sk);
1340 if (sk->sk_state == TCP_CLOSE)
1341 inet_csk_destroy_sock(sk);
1342 bh_unlock_sock(sk);
1343 sock_put(sk);
1344 spin_lock(&reap_list_lock);
1345 }
1346 spin_unlock_bh(&reap_list_lock);
1347}
1348
1349static DECLARE_WORK(reap_task, process_reap_list);
1350
1351static void add_to_reap_list(struct sock *sk)
1352{
1353 struct chtls_sock *csk = sk->sk_user_data;
1354
1355 local_bh_disable();
1356 bh_lock_sock(sk);
1357 release_tcp_port(sk); /* release the port immediately */
1358
1359 spin_lock(&reap_list_lock);
1360 csk->passive_reap_next = reap_list;
1361 reap_list = sk;
1362 if (!csk->passive_reap_next)
1363 schedule_work(&reap_task);
1364 spin_unlock(&reap_list_lock);
1365 bh_unlock_sock(sk);
1366 local_bh_enable();
1367}
1368
1369static void add_pass_open_to_parent(struct sock *child, struct sock *lsk,
1370 struct chtls_dev *cdev)
1371{
1372 struct request_sock *oreq;
1373 struct chtls_sock *csk;
1374
1375 if (lsk->sk_state != TCP_LISTEN)
1376 return;
1377
1378 csk = child->sk_user_data;
1379 oreq = csk->passive_reap_next;
1380 csk->passive_reap_next = NULL;
1381
1382 reqsk_queue_removed(&inet_csk(lsk)->icsk_accept_queue, oreq);
1383 __skb_unlink((struct sk_buff *)&csk->synq, &csk->listen_ctx->synq);
1384
1385 if (sk_acceptq_is_full(lsk)) {
1386 chtls_reqsk_free(oreq);
1387 add_to_reap_list(child);
1388 } else {
1389 refcount_set(&oreq->rsk_refcnt, 1);
1390 inet_csk_reqsk_queue_add(lsk, oreq, child);
1391 lsk->sk_data_ready(lsk);
1392 }
1393}
1394
1395static void bl_add_pass_open_to_parent(struct sock *lsk, struct sk_buff *skb)
1396{
1397 struct sock *child = skb->sk;
1398
1399 skb->sk = NULL;
1400 add_pass_open_to_parent(child, lsk, BLOG_SKB_CB(skb)->cdev);
1401 kfree_skb(skb);
1402}
1403
1404static int chtls_pass_establish(struct chtls_dev *cdev, struct sk_buff *skb)
1405{
1406 struct cpl_pass_establish *req = cplhdr(skb) + RSS_HDR;
1407 struct chtls_sock *csk;
1408 struct sock *lsk, *sk;
1409 unsigned int hwtid;
1410
1411 hwtid = GET_TID(req);
1412 sk = lookup_tid(cdev->tids, hwtid);
1413 if (!sk)
1414 return (CPL_RET_UNKNOWN_TID | CPL_RET_BUF_DONE);
1415
1416 bh_lock_sock(sk);
1417 if (unlikely(sock_owned_by_user(sk))) {
1418 kfree_skb(skb);
1419 } else {
1420 unsigned int stid;
1421 void *data;
1422
1423 csk = sk->sk_user_data;
1424 csk->wr_max_credits = 64;
1425 csk->wr_credits = 64;
1426 csk->wr_unacked = 0;
1427 make_established(sk, ntohl(req->snd_isn), ntohs(req->tcp_opt));
1428 stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1429 sk->sk_state_change(sk);
1430 if (unlikely(sk->sk_socket))
1431 sk_wake_async(sk, 0, POLL_OUT);
1432
1433 data = lookup_stid(cdev->tids, stid);
1434 lsk = ((struct listen_ctx *)data)->lsk;
1435
1436 bh_lock_sock(lsk);
1437 if (unlikely(skb_queue_empty(&csk->listen_ctx->synq))) {
1438 /* removed from synq */
1439 bh_unlock_sock(lsk);
1440 kfree_skb(skb);
1441 goto unlock;
1442 }
1443
1444 if (likely(!sock_owned_by_user(lsk))) {
1445 kfree_skb(skb);
1446 add_pass_open_to_parent(sk, lsk, cdev);
1447 } else {
1448 skb->sk = sk;
1449 BLOG_SKB_CB(skb)->cdev = cdev;
1450 BLOG_SKB_CB(skb)->backlog_rcv =
1451 bl_add_pass_open_to_parent;
1452 __sk_add_backlog(lsk, skb);
1453 }
1454 bh_unlock_sock(lsk);
1455 }
1456unlock:
1457 bh_unlock_sock(sk);
1458 return 0;
1459}
1460
1461/*
1462 * Handle receipt of an urgent pointer.
1463 */
1464static void handle_urg_ptr(struct sock *sk, u32 urg_seq)
1465{
1466 struct tcp_sock *tp = tcp_sk(sk);
1467
1468 urg_seq--;
1469 if (tp->urg_data && !after(urg_seq, tp->urg_seq))
1470 return; /* duplicate pointer */
1471
1472 sk_send_sigurg(sk);
1473 if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
1474 !sock_flag(sk, SOCK_URGINLINE) &&
1475 tp->copied_seq != tp->rcv_nxt) {
1476 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
1477
1478 tp->copied_seq++;
1479 if (skb && tp->copied_seq - ULP_SKB_CB(skb)->seq >= skb->len)
1480 chtls_free_skb(sk, skb);
1481 }
1482
1483 tp->urg_data = TCP_URG_NOTYET;
1484 tp->urg_seq = urg_seq;
1485}
1486
1487static void check_sk_callbacks(struct chtls_sock *csk)
1488{
1489 struct sock *sk = csk->sk;
1490
1491 if (unlikely(sk->sk_user_data &&
1492 !csk_flag_nochk(csk, CSK_CALLBACKS_CHKD)))
1493 csk_set_flag(csk, CSK_CALLBACKS_CHKD);
1494}
1495
1496/*
1497 * Handles Rx data that arrives in a state where the socket isn't accepting
1498 * new data.
1499 */
1500static void handle_excess_rx(struct sock *sk, struct sk_buff *skb)
1501{
1502 if (!csk_flag(sk, CSK_ABORT_SHUTDOWN))
1503 chtls_abort_conn(sk, skb);
1504
1505 kfree_skb(skb);
1506}
1507
1508static void chtls_recv_data(struct sock *sk, struct sk_buff *skb)
1509{
1510 struct cpl_rx_data *hdr = cplhdr(skb) + RSS_HDR;
1511 struct chtls_sock *csk;
1512 struct tcp_sock *tp;
1513
1514 csk = rcu_dereference_sk_user_data(sk);
1515 tp = tcp_sk(sk);
1516
1517 if (unlikely(sk->sk_shutdown & RCV_SHUTDOWN)) {
1518 handle_excess_rx(sk, skb);
1519 return;
1520 }
1521
1522 ULP_SKB_CB(skb)->seq = ntohl(hdr->seq);
1523 ULP_SKB_CB(skb)->psh = hdr->psh;
1524 skb_ulp_mode(skb) = ULP_MODE_NONE;
1525
1526 skb_reset_transport_header(skb);
1527 __skb_pull(skb, sizeof(*hdr) + RSS_HDR);
1528 if (!skb->data_len)
1529 __skb_trim(skb, ntohs(hdr->len));
1530
1531 if (unlikely(hdr->urg))
1532 handle_urg_ptr(sk, tp->rcv_nxt + ntohs(hdr->urg));
1533 if (unlikely(tp->urg_data == TCP_URG_NOTYET &&
1534 tp->urg_seq - tp->rcv_nxt < skb->len))
1535 tp->urg_data = TCP_URG_VALID |
1536 skb->data[tp->urg_seq - tp->rcv_nxt];
1537
1538 if (unlikely(hdr->dack_mode != csk->delack_mode)) {
1539 csk->delack_mode = hdr->dack_mode;
1540 csk->delack_seq = tp->rcv_nxt;
1541 }
1542
1543 tcp_hdr(skb)->fin = 0;
1544 tp->rcv_nxt += skb->len;
1545
1546 __skb_queue_tail(&sk->sk_receive_queue, skb);
1547
1548 if (!sock_flag(sk, SOCK_DEAD)) {
1549 check_sk_callbacks(csk);
1550 sk->sk_data_ready(sk);
1551 }
1552}
1553
1554static int chtls_rx_data(struct chtls_dev *cdev, struct sk_buff *skb)
1555{
1556 struct cpl_rx_data *req = cplhdr(skb) + RSS_HDR;
1557 unsigned int hwtid = GET_TID(req);
1558 struct sock *sk;
1559
1560 sk = lookup_tid(cdev->tids, hwtid);
Gustavo A. R. Silva3d8ccf92018-04-03 15:09:12 -05001561 if (unlikely(!sk)) {
1562 pr_err("can't find conn. for hwtid %u.\n", hwtid);
1563 return -EINVAL;
1564 }
Atul Guptacc35c88a2018-03-31 21:41:59 +05301565 skb_dst_set(skb, NULL);
1566 process_cpl_msg(chtls_recv_data, sk, skb);
1567 return 0;
1568}
1569
1570static void chtls_recv_pdu(struct sock *sk, struct sk_buff *skb)
1571{
1572 struct cpl_tls_data *hdr = cplhdr(skb);
1573 struct chtls_sock *csk;
1574 struct chtls_hws *tlsk;
1575 struct tcp_sock *tp;
1576
1577 csk = rcu_dereference_sk_user_data(sk);
1578 tlsk = &csk->tlshws;
1579 tp = tcp_sk(sk);
1580
1581 if (unlikely(sk->sk_shutdown & RCV_SHUTDOWN)) {
1582 handle_excess_rx(sk, skb);
1583 return;
1584 }
1585
1586 ULP_SKB_CB(skb)->seq = ntohl(hdr->seq);
1587 ULP_SKB_CB(skb)->flags = 0;
1588 skb_ulp_mode(skb) = ULP_MODE_TLS;
1589
1590 skb_reset_transport_header(skb);
1591 __skb_pull(skb, sizeof(*hdr));
1592 if (!skb->data_len)
1593 __skb_trim(skb,
1594 CPL_TLS_DATA_LENGTH_G(ntohl(hdr->length_pkd)));
1595
1596 if (unlikely(tp->urg_data == TCP_URG_NOTYET && tp->urg_seq -
1597 tp->rcv_nxt < skb->len))
1598 tp->urg_data = TCP_URG_VALID |
1599 skb->data[tp->urg_seq - tp->rcv_nxt];
1600
1601 tcp_hdr(skb)->fin = 0;
1602 tlsk->pldlen = CPL_TLS_DATA_LENGTH_G(ntohl(hdr->length_pkd));
1603 __skb_queue_tail(&tlsk->sk_recv_queue, skb);
1604}
1605
1606static int chtls_rx_pdu(struct chtls_dev *cdev, struct sk_buff *skb)
1607{
1608 struct cpl_tls_data *req = cplhdr(skb);
1609 unsigned int hwtid = GET_TID(req);
1610 struct sock *sk;
1611
1612 sk = lookup_tid(cdev->tids, hwtid);
Gustavo A. R. Silva3d8ccf92018-04-03 15:09:12 -05001613 if (unlikely(!sk)) {
1614 pr_err("can't find conn. for hwtid %u.\n", hwtid);
1615 return -EINVAL;
1616 }
Atul Guptacc35c88a2018-03-31 21:41:59 +05301617 skb_dst_set(skb, NULL);
1618 process_cpl_msg(chtls_recv_pdu, sk, skb);
1619 return 0;
1620}
1621
1622static void chtls_set_hdrlen(struct sk_buff *skb, unsigned int nlen)
1623{
1624 struct tlsrx_cmp_hdr *tls_cmp_hdr = cplhdr(skb);
1625
1626 skb->hdr_len = ntohs((__force __be16)tls_cmp_hdr->length);
1627 tls_cmp_hdr->length = ntohs((__force __be16)nlen);
1628}
1629
1630static void chtls_rx_hdr(struct sock *sk, struct sk_buff *skb)
1631{
Atul Gupta17a7d242018-05-14 16:41:38 +05301632 struct tlsrx_cmp_hdr *tls_hdr_pkt;
1633 struct cpl_rx_tls_cmp *cmp_cpl;
Atul Guptacc35c88a2018-03-31 21:41:59 +05301634 struct sk_buff *skb_rec;
1635 struct chtls_sock *csk;
1636 struct chtls_hws *tlsk;
1637 struct tcp_sock *tp;
1638
Atul Gupta17a7d242018-05-14 16:41:38 +05301639 cmp_cpl = cplhdr(skb);
Atul Guptacc35c88a2018-03-31 21:41:59 +05301640 csk = rcu_dereference_sk_user_data(sk);
1641 tlsk = &csk->tlshws;
1642 tp = tcp_sk(sk);
1643
1644 ULP_SKB_CB(skb)->seq = ntohl(cmp_cpl->seq);
1645 ULP_SKB_CB(skb)->flags = 0;
1646
1647 skb_reset_transport_header(skb);
1648 __skb_pull(skb, sizeof(*cmp_cpl));
Atul Gupta17a7d242018-05-14 16:41:38 +05301649 tls_hdr_pkt = (struct tlsrx_cmp_hdr *)skb->data;
1650 if (tls_hdr_pkt->res_to_mac_error & TLSRX_HDR_PKT_ERROR_M)
1651 tls_hdr_pkt->type = CONTENT_TYPE_ERROR;
Atul Guptacc35c88a2018-03-31 21:41:59 +05301652 if (!skb->data_len)
Atul Gupta17a7d242018-05-14 16:41:38 +05301653 __skb_trim(skb, TLS_HEADER_LENGTH);
Atul Guptacc35c88a2018-03-31 21:41:59 +05301654
1655 tp->rcv_nxt +=
1656 CPL_RX_TLS_CMP_PDULENGTH_G(ntohl(cmp_cpl->pdulength_length));
1657
Atul Gupta17a7d242018-05-14 16:41:38 +05301658 ULP_SKB_CB(skb)->flags |= ULPCB_FLAG_TLS_HDR;
Atul Guptacc35c88a2018-03-31 21:41:59 +05301659 skb_rec = __skb_dequeue(&tlsk->sk_recv_queue);
1660 if (!skb_rec) {
Atul Guptacc35c88a2018-03-31 21:41:59 +05301661 __skb_queue_tail(&sk->sk_receive_queue, skb);
1662 } else {
1663 chtls_set_hdrlen(skb, tlsk->pldlen);
1664 tlsk->pldlen = 0;
1665 __skb_queue_tail(&sk->sk_receive_queue, skb);
1666 __skb_queue_tail(&sk->sk_receive_queue, skb_rec);
1667 }
1668
1669 if (!sock_flag(sk, SOCK_DEAD)) {
1670 check_sk_callbacks(csk);
1671 sk->sk_data_ready(sk);
1672 }
1673}
1674
1675static int chtls_rx_cmp(struct chtls_dev *cdev, struct sk_buff *skb)
1676{
1677 struct cpl_rx_tls_cmp *req = cplhdr(skb);
1678 unsigned int hwtid = GET_TID(req);
1679 struct sock *sk;
1680
1681 sk = lookup_tid(cdev->tids, hwtid);
Gustavo A. R. Silva3d8ccf92018-04-03 15:09:12 -05001682 if (unlikely(!sk)) {
1683 pr_err("can't find conn. for hwtid %u.\n", hwtid);
1684 return -EINVAL;
1685 }
Atul Guptacc35c88a2018-03-31 21:41:59 +05301686 skb_dst_set(skb, NULL);
1687 process_cpl_msg(chtls_rx_hdr, sk, skb);
1688
1689 return 0;
1690}
1691
1692static void chtls_timewait(struct sock *sk)
1693{
1694 struct tcp_sock *tp = tcp_sk(sk);
1695
1696 tp->rcv_nxt++;
Arnd Bergmanncca9bab2018-07-11 12:16:12 +02001697 tp->rx_opt.ts_recent_stamp = ktime_get_seconds();
Atul Guptacc35c88a2018-03-31 21:41:59 +05301698 tp->srtt_us = 0;
1699 tcp_time_wait(sk, TCP_TIME_WAIT, 0);
1700}
1701
1702static void chtls_peer_close(struct sock *sk, struct sk_buff *skb)
1703{
1704 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
1705
1706 sk->sk_shutdown |= RCV_SHUTDOWN;
1707 sock_set_flag(sk, SOCK_DONE);
1708
1709 switch (sk->sk_state) {
1710 case TCP_SYN_RECV:
1711 case TCP_ESTABLISHED:
1712 tcp_set_state(sk, TCP_CLOSE_WAIT);
1713 break;
1714 case TCP_FIN_WAIT1:
1715 tcp_set_state(sk, TCP_CLOSING);
1716 break;
1717 case TCP_FIN_WAIT2:
1718 chtls_release_resources(sk);
1719 if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING))
1720 chtls_conn_done(sk);
1721 else
1722 chtls_timewait(sk);
1723 break;
1724 default:
1725 pr_info("cpl_peer_close in bad state %d\n", sk->sk_state);
1726 }
1727
1728 if (!sock_flag(sk, SOCK_DEAD)) {
1729 sk->sk_state_change(sk);
1730 /* Do not send POLL_HUP for half duplex close. */
1731
1732 if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
1733 sk->sk_state == TCP_CLOSE)
1734 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
1735 else
1736 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
1737 }
1738}
1739
1740static void chtls_close_con_rpl(struct sock *sk, struct sk_buff *skb)
1741{
1742 struct cpl_close_con_rpl *rpl = cplhdr(skb) + RSS_HDR;
1743 struct chtls_sock *csk;
1744 struct tcp_sock *tp;
1745
1746 csk = rcu_dereference_sk_user_data(sk);
1747 tp = tcp_sk(sk);
1748
1749 tp->snd_una = ntohl(rpl->snd_nxt) - 1; /* exclude FIN */
1750
1751 switch (sk->sk_state) {
1752 case TCP_CLOSING:
1753 chtls_release_resources(sk);
1754 if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING))
1755 chtls_conn_done(sk);
1756 else
1757 chtls_timewait(sk);
1758 break;
1759 case TCP_LAST_ACK:
1760 chtls_release_resources(sk);
1761 chtls_conn_done(sk);
1762 break;
1763 case TCP_FIN_WAIT1:
1764 tcp_set_state(sk, TCP_FIN_WAIT2);
1765 sk->sk_shutdown |= SEND_SHUTDOWN;
1766
1767 if (!sock_flag(sk, SOCK_DEAD))
1768 sk->sk_state_change(sk);
1769 else if (tcp_sk(sk)->linger2 < 0 &&
1770 !csk_flag_nochk(csk, CSK_ABORT_SHUTDOWN))
1771 chtls_abort_conn(sk, skb);
1772 break;
1773 default:
1774 pr_info("close_con_rpl in bad state %d\n", sk->sk_state);
1775 }
1776 kfree_skb(skb);
1777}
1778
1779static struct sk_buff *get_cpl_skb(struct sk_buff *skb,
1780 size_t len, gfp_t gfp)
1781{
1782 if (likely(!skb_is_nonlinear(skb) && !skb_cloned(skb))) {
1783 WARN_ONCE(skb->len < len, "skb alloc error");
1784 __skb_trim(skb, len);
1785 skb_get(skb);
1786 } else {
1787 skb = alloc_skb(len, gfp);
1788 if (skb)
1789 __skb_put(skb, len);
1790 }
1791 return skb;
1792}
1793
1794static void set_abort_rpl_wr(struct sk_buff *skb, unsigned int tid,
1795 int cmd)
1796{
1797 struct cpl_abort_rpl *rpl = cplhdr(skb);
1798
1799 INIT_TP_WR_CPL(rpl, CPL_ABORT_RPL, tid);
1800 rpl->cmd = cmd;
1801}
1802
1803static void send_defer_abort_rpl(struct chtls_dev *cdev, struct sk_buff *skb)
1804{
1805 struct cpl_abort_req_rss *req = cplhdr(skb);
1806 struct sk_buff *reply_skb;
1807
1808 reply_skb = alloc_skb(sizeof(struct cpl_abort_rpl),
1809 GFP_KERNEL | __GFP_NOFAIL);
1810 __skb_put(reply_skb, sizeof(struct cpl_abort_rpl));
1811 set_abort_rpl_wr(reply_skb, GET_TID(req),
1812 (req->status & CPL_ABORT_NO_RST));
1813 set_wr_txq(reply_skb, CPL_PRIORITY_DATA, req->status >> 1);
1814 cxgb4_ofld_send(cdev->lldi->ports[0], reply_skb);
1815 kfree_skb(skb);
1816}
1817
1818static void send_abort_rpl(struct sock *sk, struct sk_buff *skb,
1819 struct chtls_dev *cdev, int status, int queue)
1820{
1821 struct cpl_abort_req_rss *req = cplhdr(skb);
1822 struct sk_buff *reply_skb;
1823 struct chtls_sock *csk;
1824
1825 csk = rcu_dereference_sk_user_data(sk);
1826
1827 reply_skb = alloc_skb(sizeof(struct cpl_abort_rpl),
1828 GFP_KERNEL);
1829
1830 if (!reply_skb) {
1831 req->status = (queue << 1);
1832 send_defer_abort_rpl(cdev, skb);
1833 return;
1834 }
1835
1836 set_abort_rpl_wr(reply_skb, GET_TID(req), status);
1837 kfree_skb(skb);
1838
1839 set_wr_txq(reply_skb, CPL_PRIORITY_DATA, queue);
1840 if (csk_conn_inline(csk)) {
1841 struct l2t_entry *e = csk->l2t_entry;
1842
1843 if (e && sk->sk_state != TCP_SYN_RECV) {
1844 cxgb4_l2t_send(csk->egress_dev, reply_skb, e);
1845 return;
1846 }
1847 }
1848 cxgb4_ofld_send(cdev->lldi->ports[0], reply_skb);
1849}
1850
1851/*
1852 * Add an skb to the deferred skb queue for processing from process context.
1853 */
1854static void t4_defer_reply(struct sk_buff *skb, struct chtls_dev *cdev,
1855 defer_handler_t handler)
1856{
1857 DEFERRED_SKB_CB(skb)->handler = handler;
1858 spin_lock_bh(&cdev->deferq.lock);
1859 __skb_queue_tail(&cdev->deferq, skb);
1860 if (skb_queue_len(&cdev->deferq) == 1)
1861 schedule_work(&cdev->deferq_task);
1862 spin_unlock_bh(&cdev->deferq.lock);
1863}
1864
1865static void chtls_send_abort_rpl(struct sock *sk, struct sk_buff *skb,
1866 struct chtls_dev *cdev,
1867 int status, int queue)
1868{
1869 struct cpl_abort_req_rss *req = cplhdr(skb) + RSS_HDR;
1870 struct sk_buff *reply_skb;
1871 struct chtls_sock *csk;
1872 unsigned int tid;
1873
1874 csk = rcu_dereference_sk_user_data(sk);
1875 tid = GET_TID(req);
1876
1877 reply_skb = get_cpl_skb(skb, sizeof(struct cpl_abort_rpl), gfp_any());
1878 if (!reply_skb) {
1879 req->status = (queue << 1) | status;
1880 t4_defer_reply(skb, cdev, send_defer_abort_rpl);
1881 return;
1882 }
1883
1884 set_abort_rpl_wr(reply_skb, tid, status);
1885 set_wr_txq(reply_skb, CPL_PRIORITY_DATA, queue);
1886 if (csk_conn_inline(csk)) {
1887 struct l2t_entry *e = csk->l2t_entry;
1888
1889 if (e && sk->sk_state != TCP_SYN_RECV) {
1890 cxgb4_l2t_send(csk->egress_dev, reply_skb, e);
1891 return;
1892 }
1893 }
1894 cxgb4_ofld_send(cdev->lldi->ports[0], reply_skb);
1895 kfree_skb(skb);
1896}
1897
1898/*
1899 * This is run from a listener's backlog to abort a child connection in
1900 * SYN_RCV state (i.e., one on the listener's SYN queue).
1901 */
1902static void bl_abort_syn_rcv(struct sock *lsk, struct sk_buff *skb)
1903{
1904 struct chtls_sock *csk;
1905 struct sock *child;
1906 int queue;
1907
1908 child = skb->sk;
1909 csk = rcu_dereference_sk_user_data(child);
1910 queue = csk->txq_idx;
1911
1912 skb->sk = NULL;
1913 do_abort_syn_rcv(child, lsk);
1914 send_abort_rpl(child, skb, BLOG_SKB_CB(skb)->cdev,
1915 CPL_ABORT_NO_RST, queue);
1916}
1917
1918static int abort_syn_rcv(struct sock *sk, struct sk_buff *skb)
1919{
1920 const struct request_sock *oreq;
1921 struct listen_ctx *listen_ctx;
1922 struct chtls_sock *csk;
1923 struct chtls_dev *cdev;
1924 struct sock *psk;
1925 void *ctx;
1926
1927 csk = sk->sk_user_data;
1928 oreq = csk->passive_reap_next;
1929 cdev = csk->cdev;
1930
1931 if (!oreq)
1932 return -1;
1933
1934 ctx = lookup_stid(cdev->tids, oreq->ts_recent);
1935 if (!ctx)
1936 return -1;
1937
1938 listen_ctx = (struct listen_ctx *)ctx;
1939 psk = listen_ctx->lsk;
1940
1941 bh_lock_sock(psk);
1942 if (!sock_owned_by_user(psk)) {
1943 int queue = csk->txq_idx;
1944
1945 do_abort_syn_rcv(sk, psk);
1946 send_abort_rpl(sk, skb, cdev, CPL_ABORT_NO_RST, queue);
1947 } else {
1948 skb->sk = sk;
1949 BLOG_SKB_CB(skb)->backlog_rcv = bl_abort_syn_rcv;
1950 __sk_add_backlog(psk, skb);
1951 }
1952 bh_unlock_sock(psk);
1953 return 0;
1954}
1955
1956static void chtls_abort_req_rss(struct sock *sk, struct sk_buff *skb)
1957{
1958 const struct cpl_abort_req_rss *req = cplhdr(skb) + RSS_HDR;
1959 struct chtls_sock *csk = sk->sk_user_data;
1960 int rst_status = CPL_ABORT_NO_RST;
1961 int queue = csk->txq_idx;
1962
1963 if (is_neg_adv(req->status)) {
1964 if (sk->sk_state == TCP_SYN_RECV)
1965 chtls_set_tcb_tflag(sk, 0, 0);
1966
1967 kfree_skb(skb);
1968 return;
1969 }
1970
1971 csk_reset_flag(csk, CSK_ABORT_REQ_RCVD);
1972
1973 if (!csk_flag_nochk(csk, CSK_ABORT_SHUTDOWN) &&
1974 !csk_flag_nochk(csk, CSK_TX_DATA_SENT)) {
1975 struct tcp_sock *tp = tcp_sk(sk);
1976
1977 if (send_tx_flowc_wr(sk, 0, tp->snd_nxt, tp->rcv_nxt) < 0)
1978 WARN_ONCE(1, "send_tx_flowc error");
1979 csk_set_flag(csk, CSK_TX_DATA_SENT);
1980 }
1981
1982 csk_set_flag(csk, CSK_ABORT_SHUTDOWN);
1983
1984 if (!csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING)) {
1985 sk->sk_err = ETIMEDOUT;
1986
1987 if (!sock_flag(sk, SOCK_DEAD))
1988 sk->sk_error_report(sk);
1989
1990 if (sk->sk_state == TCP_SYN_RECV && !abort_syn_rcv(sk, skb))
1991 return;
1992
1993 chtls_release_resources(sk);
1994 chtls_conn_done(sk);
1995 }
1996
1997 chtls_send_abort_rpl(sk, skb, csk->cdev, rst_status, queue);
1998}
1999
2000static void chtls_abort_rpl_rss(struct sock *sk, struct sk_buff *skb)
2001{
2002 struct cpl_abort_rpl_rss *rpl = cplhdr(skb) + RSS_HDR;
2003 struct chtls_sock *csk;
2004 struct chtls_dev *cdev;
2005
2006 csk = rcu_dereference_sk_user_data(sk);
2007 cdev = csk->cdev;
2008
2009 if (csk_flag_nochk(csk, CSK_ABORT_RPL_PENDING)) {
2010 csk_reset_flag(csk, CSK_ABORT_RPL_PENDING);
2011 if (!csk_flag_nochk(csk, CSK_ABORT_REQ_RCVD)) {
2012 if (sk->sk_state == TCP_SYN_SENT) {
2013 cxgb4_remove_tid(cdev->tids,
2014 csk->port_id,
2015 GET_TID(rpl),
2016 sk->sk_family);
2017 sock_put(sk);
2018 }
2019 chtls_release_resources(sk);
2020 chtls_conn_done(sk);
2021 }
2022 }
2023 kfree_skb(skb);
2024}
2025
2026static int chtls_conn_cpl(struct chtls_dev *cdev, struct sk_buff *skb)
2027{
2028 struct cpl_peer_close *req = cplhdr(skb) + RSS_HDR;
2029 void (*fn)(struct sock *sk, struct sk_buff *skb);
2030 unsigned int hwtid = GET_TID(req);
2031 struct sock *sk;
2032 u8 opcode;
2033
2034 opcode = ((const struct rss_header *)cplhdr(skb))->opcode;
2035
2036 sk = lookup_tid(cdev->tids, hwtid);
2037 if (!sk)
2038 goto rel_skb;
2039
2040 switch (opcode) {
2041 case CPL_PEER_CLOSE:
2042 fn = chtls_peer_close;
2043 break;
2044 case CPL_CLOSE_CON_RPL:
2045 fn = chtls_close_con_rpl;
2046 break;
2047 case CPL_ABORT_REQ_RSS:
2048 fn = chtls_abort_req_rss;
2049 break;
2050 case CPL_ABORT_RPL_RSS:
2051 fn = chtls_abort_rpl_rss;
2052 break;
2053 default:
2054 goto rel_skb;
2055 }
2056
2057 process_cpl_msg(fn, sk, skb);
2058 return 0;
2059
2060rel_skb:
2061 kfree_skb(skb);
2062 return 0;
2063}
2064
2065static struct sk_buff *dequeue_wr(struct sock *sk)
2066{
2067 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
2068 struct sk_buff *skb = csk->wr_skb_head;
2069
2070 if (likely(skb)) {
2071 /* Don't bother clearing the tail */
2072 csk->wr_skb_head = WR_SKB_CB(skb)->next_wr;
2073 WR_SKB_CB(skb)->next_wr = NULL;
2074 }
2075 return skb;
2076}
2077
2078static void chtls_rx_ack(struct sock *sk, struct sk_buff *skb)
2079{
2080 struct cpl_fw4_ack *hdr = cplhdr(skb) + RSS_HDR;
2081 struct chtls_sock *csk = sk->sk_user_data;
2082 struct tcp_sock *tp = tcp_sk(sk);
2083 u32 credits = hdr->credits;
2084 u32 snd_una;
2085
2086 snd_una = ntohl(hdr->snd_una);
2087 csk->wr_credits += credits;
2088
2089 if (csk->wr_unacked > csk->wr_max_credits - csk->wr_credits)
2090 csk->wr_unacked = csk->wr_max_credits - csk->wr_credits;
2091
2092 while (credits) {
2093 struct sk_buff *pskb = csk->wr_skb_head;
2094 u32 csum;
2095
2096 if (unlikely(!pskb)) {
2097 if (csk->wr_nondata)
2098 csk->wr_nondata -= credits;
2099 break;
2100 }
2101 csum = (__force u32)pskb->csum;
2102 if (unlikely(credits < csum)) {
2103 pskb->csum = (__force __wsum)(csum - credits);
2104 break;
2105 }
2106 dequeue_wr(sk);
2107 credits -= csum;
2108 kfree_skb(pskb);
2109 }
2110 if (hdr->seq_vld & CPL_FW4_ACK_FLAGS_SEQVAL) {
2111 if (unlikely(before(snd_una, tp->snd_una))) {
2112 kfree_skb(skb);
2113 return;
2114 }
2115
2116 if (tp->snd_una != snd_una) {
2117 tp->snd_una = snd_una;
2118 tp->rcv_tstamp = tcp_time_stamp(tp);
2119 if (tp->snd_una == tp->snd_nxt &&
2120 !csk_flag_nochk(csk, CSK_TX_FAILOVER))
2121 csk_reset_flag(csk, CSK_TX_WAIT_IDLE);
2122 }
2123 }
2124
2125 if (hdr->seq_vld & CPL_FW4_ACK_FLAGS_CH) {
2126 unsigned int fclen16 = roundup(failover_flowc_wr_len, 16);
2127
2128 csk->wr_credits -= fclen16;
2129 csk_reset_flag(csk, CSK_TX_WAIT_IDLE);
2130 csk_reset_flag(csk, CSK_TX_FAILOVER);
2131 }
2132 if (skb_queue_len(&csk->txq) && chtls_push_frames(csk, 0))
2133 sk->sk_write_space(sk);
2134
2135 kfree_skb(skb);
2136}
2137
2138static int chtls_wr_ack(struct chtls_dev *cdev, struct sk_buff *skb)
2139{
2140 struct cpl_fw4_ack *rpl = cplhdr(skb) + RSS_HDR;
2141 unsigned int hwtid = GET_TID(rpl);
2142 struct sock *sk;
2143
2144 sk = lookup_tid(cdev->tids, hwtid);
Gustavo A. R. Silva3d8ccf92018-04-03 15:09:12 -05002145 if (unlikely(!sk)) {
2146 pr_err("can't find conn. for hwtid %u.\n", hwtid);
2147 return -EINVAL;
2148 }
Atul Guptacc35c88a2018-03-31 21:41:59 +05302149 process_cpl_msg(chtls_rx_ack, sk, skb);
2150
2151 return 0;
2152}
2153
2154chtls_handler_func chtls_handlers[NUM_CPL_CMDS] = {
2155 [CPL_PASS_OPEN_RPL] = chtls_pass_open_rpl,
2156 [CPL_CLOSE_LISTSRV_RPL] = chtls_close_listsrv_rpl,
2157 [CPL_PASS_ACCEPT_REQ] = chtls_pass_accept_req,
2158 [CPL_PASS_ESTABLISH] = chtls_pass_establish,
2159 [CPL_RX_DATA] = chtls_rx_data,
2160 [CPL_TLS_DATA] = chtls_rx_pdu,
2161 [CPL_RX_TLS_CMP] = chtls_rx_cmp,
2162 [CPL_PEER_CLOSE] = chtls_conn_cpl,
2163 [CPL_CLOSE_CON_RPL] = chtls_conn_cpl,
2164 [CPL_ABORT_REQ_RSS] = chtls_conn_cpl,
2165 [CPL_ABORT_RPL_RSS] = chtls_conn_cpl,
2166 [CPL_FW4_ACK] = chtls_wr_ack,
2167};