1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Based on net/ipv4/tcp_input.c
4 * Authors: Ross Biro
5 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
6 * Mark Evans, <evansmp@uhura.aston.ac.uk>
7 * Corey Minyard <wf-rch!minyard@relay.EU.net>
8 * Florian La Roche, <flla@stud.uni-sb.de>
9 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
10 * Linus Torvalds, <torvalds@cs.helsinki.fi>
11 * Alan Cox, <gw4pts@gw4pts.ampr.org>
12 * Matthew Dillon, <dillon@apollo.west.oic.com>
13 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
14 * Jorge Cwik, <jorge@laser.satlink.net>
15 *
16 * Changes:
17 * Pedro Roque : Fast Retransmit/Recovery.
18 * Two receive queues.
19 * Retransmit queue handled by TCP.
20 * Better retransmit timer handling.
21 * New congestion avoidance.
22 * Header prediction.
23 * Variable renaming.
24 *
25 * Eric : Fast Retransmit.
26 * Randy Scott : MSS option defines.
27 * Eric Schenk : Fixes to slow start algorithm.
28 * Eric Schenk : Yet another double ACK bug.
29 * Eric Schenk : Delayed ACK bug fixes.
30 * Eric Schenk : Floyd style fast retrans war avoidance.
31 * David S. Miller : Don't allow zero congestion window.
32 * Eric Schenk : Fix retransmitter so that it sends
33 * next packet on ack of previous packet.
34 * Andi Kleen : Moved open_request checking here
35 * and process RSTs for open_requests.
36 * Andi Kleen : Better prune_queue, and other fixes.
37 * Andrey Savochkin: Fix RTT measurements in the presence of
38 * timestamps.
39 * Andrey Savochkin: Check sequence numbers correctly when
40 * removing SACKs due to in sequence incoming
41 * data segments.
42 * Andi Kleen: Make sure we never ack data there is not
43 * enough room for. Also make this condition
44 * a fatal error if it might still happen.
45 * Andi Kleen: Add tcp_measure_rcv_mss to make
46 * connections with MSS<min(MTU,ann. MSS)
47 * work without delayed acks.
48 * Andi Kleen: Process packets with PSH set in the
49 * fast path.
50 * J Hadi Salim: ECN support
51 * Andrei Gurtov,
52 * Pasi Sarolahti,
53 * Panu Kuhlberg: Experimental audit of TCP (re)transmission
54 * engine. Lots of bugs are found.
55 * Pasi Sarolahti: F-RTO for dealing with spurious RTOs
56 *
57 * Based on net\ipv4\tcp_westwood.c
58 * TCP Westwood+: end-to-end bandwidth estimation for TCP
59 *
60 * Angelo Dell'Aera: author of the first version of TCP Westwood+ in Linux 2.4
61 *
62 * Support at http://c3lab.poliba.it/index.php/Westwood
63 * Main references in literature:
64 *
65 * - Mascolo S, Casetti, M. Gerla et al.
66 * "TCP Westwood: bandwidth estimation for TCP" Proc. ACM Mobicom 2001
67 *
68 * - A. Grieco, s. Mascolo
69 * "Performance evaluation of New Reno, Vegas, Westwood+ TCP" ACM Computer
70 * Comm. Review, 2004
71 *
72 * - A. Dell'Aera, L. Grieco, S. Mascolo.
73 * "Linux 2.4 Implementation of Westwood+ TCP with Rate-Halving :
74 * A Performance Evaluation Over the Internet" (ICC 2004), Paris, June 2004
75 *
76 * Westwood+ employs end-to-end bandwidth measurement to set cwnd and
77 * ssthresh after packet loss. The probing phase is as the original Reno.
78 *
79 * Based on include\net\tcp.h
80 * Authors: Ross Biro
81 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
82 *
83 * NewIP INET
84 * An implementation of the TCP/IP protocol suite for the LINUX
85 * operating system. NewIP INET is implemented using the BSD Socket
86 * interface as the means of communication with the user level.
87 *
88 * Implementation of the Transmission Control Protocol(TCP).
89 */
90 #define pr_fmt(fmt) KBUILD_MODNAME ": [%s:%d] " fmt, __func__, __LINE__
91
92 #include <net/dst.h>
93 #include <net/tcp.h>
94 #include <net/tcp_nip.h>
95 #include <net/inet_common.h>
96 #include <linux/module.h>
97 #include <linux/sysctl.h>
98 #include <linux/kernel.h>
99 #include <linux/errqueue.h>
100 #include "tcp_nip_parameter.h"
101
102 #define FLAG_DATA 0x01 /* Incoming frame contained data. */
103 #define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */
104 #define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */
105 #define FLAG_RETRANS_DATA_ACKED 0x08 /* some of which was retransmitted. */
106 #define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */
107 #define FLAG_DATA_SACKED 0x20 /* New SACK. */
108 #define FLAG_ECE 0x40 /* ECE in this ACK */
109 #define FLAG_LOST_RETRANS 0x80 /* This ACK marks some retransmission lost */
110 #define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update */
111 #define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */
112 #define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
113 #define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
114 #define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
115 #define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */
116 #define FLAG_NO_CHALLENGE_ACK 0x8000 /* do not call tcp_send_challenge_ack() */
117
118 #define FLAG_ACKED (FLAG_DATA_ACKED | FLAG_SYN_ACKED)
119 #define FLAG_NOT_DUP (FLAG_DATA | FLAG_WIN_UPDATE | FLAG_ACKED)
120 #define FLAG_CA_ALERT (FLAG_DATA_SACKED | FLAG_ECE)
121 #define FLAG_FORWARD_PROGRESS (FLAG_ACKED | FLAG_DATA_SACKED)
122
123 #define TCP_REMNANT (TCP_FLAG_FIN | TCP_FLAG_URG | TCP_FLAG_SYN | TCP_FLAG_PSH)
124 #define TCP_HP_BITS (~(TCP_RESERVED_BITS | TCP_FLAG_PSH))
125
126 #define REXMIT_NONE 0 /* no loss recovery to do */
127 #define REXMIT_LOST 1 /* retransmit packets marked lost */
128 #define REXMIT_NEW 2 /* FRTO-style transmit of unsent/new packets */
129
130 #define TCP_MAX_MSS 1460
131
132 #define SRTT_FACTOR_FOUR_FIVE 4
133 #define SRTT_DIVISOR_FACTOR 5
134 #define BW_FACTOR_SEVEN_EIGHT 7
135 #define BW_DIVISOR_FACTOR 8
136 #define BW_MULTIPLY_FACTOR 100
137 #define HALF_DIVISOR_FACTOR 2
138
tcp_nip_fin(struct sock * sk)139 void tcp_nip_fin(struct sock *sk)
140 {
141 inet_csk_schedule_ack(sk);
142
143 sk->sk_shutdown |= RCV_SHUTDOWN;
144 sock_set_flag(sk, SOCK_DONE);
145
146 switch (sk->sk_state) {
147 case TCP_SYN_RECV:
148 case TCP_ESTABLISHED:
149 /* Move to CLOSE_WAIT */
150 tcp_set_state(sk, TCP_CLOSE_WAIT);
151 inet_csk(sk)->icsk_ack.pingpong = 1;
152 break;
153
154 case TCP_CLOSE_WAIT:
155 case TCP_CLOSING:
156 /* Received a retransmission of the FIN, do
157 * nothing.
158 */
159 break;
160 case TCP_LAST_ACK:
161 /* RFC793: Remain in the LAST-ACK state. */
162 break;
163
164 case TCP_FIN_WAIT1:
165 /* This case occurs when a simultaneous close
166 * happens, we must ack the received FIN and
167 * enter the CLOSING state.
168 */
169 tcp_nip_send_ack(sk);
170 tcp_set_state(sk, TCP_CLOSING);
171 break;
172 case TCP_FIN_WAIT2:
173 /* Received a FIN -- send ACK and enter TIME_WAIT. */
174 tcp_nip_send_ack(sk);
175 inet_csk_reset_keepalive_timer(sk, TCP_TIMEWAIT_LEN);
176 break;
177 default:
178 /* Only TCP_LISTEN and TCP_CLOSE are left, in these
179 * cases we should never reach this piece of code.
180 */
181 nip_dbg("Impossible, sk->sk_state=%d", sk->sk_state);
182 break;
183 }
184
185 if (!sock_flag(sk, SOCK_DEAD))
186 sk->sk_state_change(sk);
187 }
188
tcp_nip_drop(struct sock * sk,struct sk_buff * skb)189 static void tcp_nip_drop(struct sock *sk, struct sk_buff *skb)
190 {
191 sk_drops_add(sk, skb);
192 __kfree_skb(skb);
193 }
194
tcp_nip_overlap_handle(struct tcp_sock * tp,struct sk_buff * skb)195 static void tcp_nip_overlap_handle(struct tcp_sock *tp, struct sk_buff *skb)
196 {
197 u32 diff = tp->rcv_nxt - TCP_SKB_CB(skb)->seq;
198 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
199
200 skb->data += diff;
201 skb->len -= diff;
202 tcb->seq += diff;
203 }
204
tcp_nip_left_overlap(struct sk_buff * cur,struct sk_buff * skb)205 static void tcp_nip_left_overlap(struct sk_buff *cur, struct sk_buff *skb)
206 {
207 u32 diff = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(cur)->seq;
208 struct tcp_skb_cb *tcb = TCP_SKB_CB(cur);
209
210 cur->data += diff;
211 cur->len -= diff;
212 tcb->seq += diff;
213 }
214
tcp_nip_right_overlap(struct sk_buff * cur,struct sk_buff * skb)215 static void tcp_nip_right_overlap(struct sk_buff *cur, struct sk_buff *skb)
216 {
217 u32 diff = TCP_SKB_CB(cur)->end_seq - TCP_SKB_CB(skb)->seq;
218 struct tcp_skb_cb *tcb = TCP_SKB_CB(cur);
219 unsigned int len;
220
221 len = cur->len - diff;
222 /* At present NewIP only uses linear regions, uses skb_trim to remove end from a buffer;
223 * If the nonlinear region is also used later, use pskb_trim to remove end from a buffer;
224 */
225 skb_trim(cur, len);
226 tcb->end_seq -= diff;
227 }
228
229 /* If we update tp->rcv_nxt, also update tp->bytes_received */
tcp_nip_rcv_nxt_update(struct tcp_sock * tp,u32 seq)230 static void tcp_nip_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
231 {
232 u32 delta = seq - tp->rcv_nxt;
233
234 sock_owned_by_me((struct sock *)tp);
235 tp->bytes_received += delta;
236 WRITE_ONCE(tp->rcv_nxt, seq);
237 }
238
239 /* tcp_nip_try_coalesce - try to merge skb to prior one
240 * @sk: socket
241 * @to: prior buffer
242 * @from: buffer to add in queue
243 * @fragstolen: pointer to boolean
244 *
245 * Before queueing skb @from after @to, try to merge them
246 * to reduce overall memory use and queue lengths, if cost is small.
247 * Packets in ofo or receive queues can stay a long time.
248 * Better try to coalesce them right now to avoid future collapses.
249 * Returns true if caller should free @from instead of queueing it
250 */
tcp_nip_try_coalesce(struct sock * sk,struct sk_buff * to,struct sk_buff * from,bool * fragstolen)251 static bool tcp_nip_try_coalesce(struct sock *sk,
252 struct sk_buff *to,
253 struct sk_buff *from,
254 bool *fragstolen)
255 {
256 int delta;
257
258 *fragstolen = false;
259
260 /* Its possible this segment overlaps with prior segment in queue */
261 if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
262 return false;
263
264 if (!skb_try_coalesce(to, from, fragstolen, &delta)) {
265 nip_dbg("try to merge skb to the previous one failed");
266 return false;
267 }
268
269 atomic_add(delta, &sk->sk_rmem_alloc);
270 sk_mem_charge(sk, delta);
271 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE);
272 TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
273 TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
274 TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags;
275
276 if (TCP_SKB_CB(from)->has_rxtstamp) {
277 TCP_SKB_CB(to)->has_rxtstamp = true;
278 to->tstamp = from->tstamp;
279 skb_hwtstamps(to)->hwtstamp = skb_hwtstamps(from)->hwtstamp;
280 }
281
282 return true;
283 }
284
tcp_nip_ooo_try_coalesce(struct sock * sk,struct sk_buff * to,struct sk_buff * from,bool * fragstolen)285 static bool tcp_nip_ooo_try_coalesce(struct sock *sk,
286 struct sk_buff *to,
287 struct sk_buff *from,
288 bool *fragstolen)
289 {
290 bool res = tcp_nip_try_coalesce(sk, to, from, fragstolen);
291
292 /* In case tcp_nip_drop() is called later, update to->gso_segs */
293 if (res) {
294 u32 gso_segs = max_t(u16, 1, skb_shinfo(to)->gso_segs) +
295 max_t(u16, 1, skb_shinfo(from)->gso_segs);
296 u32 to_gso_segs = skb_shinfo(to)->gso_segs;
297
298 nip_dbg("(to)->gso_segs %u, (from)->gso_segs %u", skb_shinfo(to)->gso_segs,
299 skb_shinfo(from)->gso_segs);
300 skb_shinfo(to)->gso_segs = min_t(u32, gso_segs, TCP_NIP_WINDOW_MAX);
301 nip_dbg("gso_segs %u to %u", to_gso_segs, skb_shinfo(to)->gso_segs);
302 }
303 return res;
304 }
305
306 /* This one checks to see if we can put data from the
307 * out_of_order queue into the receive_queue.
308 */
tcp_nip_ofo_queue(struct sock * sk)309 static void tcp_nip_ofo_queue(struct sock *sk)
310 {
311 struct tcp_sock *tp = tcp_sk(sk);
312 bool fin;
313 bool fragstolen;
314 bool eaten;
315 struct sk_buff *skb;
316 struct sk_buff *tail;
317 struct rb_node *p;
318
319 p = rb_first(&tp->out_of_order_queue);
320 while (p) {
321 skb = rb_to_skb(p);
322 if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
323 nip_dbg("nodes are all after rcv_nxt");
324 break;
325 }
326
327 p = rb_next(p);
328 rb_erase(&skb->rbnode, &tp->out_of_order_queue);
329
330 if (unlikely(!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))) {
331 nip_dbg("this node is before rcv_nxt, drop skb");
332 tcp_nip_drop(sk, skb);
333 continue;
334 }
335
336 tail = skb_peek_tail(&sk->sk_receive_queue);
337 eaten = tail && tcp_nip_try_coalesce(sk, tail, skb, &fragstolen);
338 tcp_nip_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
339 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
340 if (!eaten)
341 __skb_queue_tail(&sk->sk_receive_queue, skb);
342 else
343 kfree_skb_partial(skb, fragstolen);
344
345 if (unlikely(fin)) {
346 nip_dbg("will send fin");
347 tcp_nip_fin(sk);
348 /* tcp_fin() purges tp->out_of_order_queue,
349 * so we must end this loop right now.
350 */
351 break;
352 }
353 }
354 }
355
356 /* The tcp_nip_data_queue function is responsible for receiving the socket data. For the packets
357 * whose start sequence number is after the sequence to be received by the socket and whose
358 * start sequence number is within the receiving window, current function is called to add them
359 * to the TCP out-of-order queue.
360 */
tcp_nip_data_queue_ofo(struct sock * sk,struct sk_buff * skb)361 static void tcp_nip_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
362 {
363 struct tcp_sock *tp = tcp_sk(sk);
364 struct rb_node **p;
365 struct rb_node *parent;
366 struct sk_buff *skb1;
367 struct sk_buff *skb2;
368 u32 seq;
369 u32 end_seq;
370 bool fragstolen;
371
372 if (unlikely(atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)) {
373 nip_dbg("no memory, drop pkt");
374 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFODROP);
375 sk->sk_data_ready(sk);
376 tcp_nip_drop(sk, skb);
377 return;
378 }
379
380 /* Disable header prediction. */
381 tp->pred_flags = 0;
382 /* set the ICSK_ACK_SCHED flag bit to indicate that an ACK needs to be sent. */
383 inet_csk_schedule_ack(sk);
384
385 tp->rcv_ooopack += max_t(u16, 1, skb_shinfo(skb)->gso_segs);
386 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
387 seq = TCP_SKB_CB(skb)->seq;
388 end_seq = TCP_SKB_CB(skb)->end_seq;
389
390 /* If it is the first out-of-order packet to be added, the out_of_order_queue queue is
391 * empty, insert it into the queue, and update the last skb pointer ooo_last_skb.
392 */
393 p = &tp->out_of_order_queue.rb_node;
394 if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
395 nip_dbg("add first ofo pkt");
396 rb_link_node(&skb->rbnode, NULL, p);
397 rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
398 tp->ooo_last_skb = skb;
399 goto end;
400 }
401
402 /* In the typical case, we are adding an skb to the end of the list.
403 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
404 */
405 if (tcp_nip_ooo_try_coalesce(sk, tp->ooo_last_skb, skb, &fragstolen)) {
406 coalesce_done:
407 /* fragstolen indicates that the data in the linear cache portion of the data
408 * packet is merged, but the data in the shared cache is still in use,
409 * so it cannot be released
410 */
411 nip_dbg("ofo skb coalesce done");
412 kfree_skb_partial(skb, fragstolen);
413 skb = NULL;
414 goto end;
415 }
416 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
417 if (!before(seq, TCP_SKB_CB(tp->ooo_last_skb)->end_seq)) {
418 nip_dbg("add skb after ooo_last_skb");
419 parent = &tp->ooo_last_skb->rbnode;
420 p = &parent->rb_right;
421 goto insert;
422 }
423
424 if (!before(seq, TCP_SKB_CB(tp->ooo_last_skb)->seq)) {
425 if (!after(end_seq, TCP_SKB_CB(tp->ooo_last_skb)->end_seq)) {
426 /* ooo_last_skb->seq <= seq, end_seq <= ooo_last_skb->end_seq */
427 nip_dbg("ooo_last_skb completely overlapping new skb, drop pkt");
428 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
429 tcp_nip_drop(sk, skb);
430 skb = NULL;
431 goto end;
432 }
433 tcp_nip_left_overlap(skb, tp->ooo_last_skb);
434 if (tcp_nip_ooo_try_coalesce(sk, tp->ooo_last_skb, skb, &fragstolen)) {
435 nip_dbg("ofo skb coalesce ooo_last_skb done");
436 goto coalesce_done;
437 } else {
438 nip_dbg("ofo skb coalesce ooo_last_skb failed, drop pkt");
439 tcp_nip_drop(sk, skb);
440 skb = NULL;
441 goto end;
442 }
443 }
444
445 /* Find place to insert this segment. Handle overlaps on the way. */
446 parent = NULL;
447 while (*p) {
448 parent = *p;
449 skb1 = rb_to_skb(parent);
450 if (before(seq, TCP_SKB_CB(skb1)->seq)) {
451 p = &parent->rb_left;
452 continue;
453 }
454 if (before(seq, TCP_SKB_CB(skb1)->end_seq)) {
455 if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
456 /* skb1->seq <= seq, end_seq <= skb1->end_seq */
457 nip_dbg("completely overlapping, drop pkt");
458 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
459 tcp_nip_drop(sk, skb);
460 skb = NULL;
461 goto end;
462 }
463 if (after(seq, TCP_SKB_CB(skb1)->seq)) {
464 /* skb1->seq < seq, end_seq > skb1->end_seq */
465 tcp_nip_left_overlap(skb, skb1);
466 skb2 = skb_rb_next(skb1);
467 if (before(TCP_SKB_CB(skb2)->seq, TCP_SKB_CB(skb)->end_seq))
468 tcp_nip_right_overlap(skb, skb2);
469 if (tcp_nip_ooo_try_coalesce(sk, skb1, skb, &fragstolen)) {
470 nip_dbg("partial overlap, ofo skb coalesce done");
471 goto coalesce_done;
472 } else {
473 nip_dbg("partial overlap, ofo skb coalesce failed, drop pkt");
474 tcp_nip_drop(sk, skb);
475 skb = NULL;
476 goto end;
477 }
478 } else {
479 /* skb1->seq == seq, end_seq > skb1->end_seq
480 * partial overlap, skb covers skb1, replace skb1 with skb.
481 */
482 nip_dbg("partial overlap, replace old skb node");
483 rb_replace_node(&skb1->rbnode, &skb->rbnode,
484 &tp->out_of_order_queue);
485 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
486 tcp_nip_drop(sk, skb1);
487 goto merge_right;
488 }
489 } else if (tcp_nip_ooo_try_coalesce(sk, skb1, skb, &fragstolen)) {
490 nip_dbg("ofo skb coalesce done while scan ofo queue");
491 goto coalesce_done;
492 }
493 p = &parent->rb_right;
494 }
495 insert:
496 /* Insert segment into RB tree. */
497 nip_dbg("add skb into ofo queue");
498 rb_link_node(&skb->rbnode, parent, p);
499 rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
500
501 merge_right:
502 /* Remove other segments covered by skb. */
503 while ((skb1 = skb_rb_next(skb)) != NULL) {
504 if (!after(end_seq, TCP_SKB_CB(skb1)->seq))
505 break;
506 if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
507 tcp_nip_right_overlap(skb, skb1);
508 nip_dbg("partial overlap, compress the right side of the current package");
509 break;
510 }
511 nip_dbg("del overlapping nodes on the right");
512 rb_erase(&skb1->rbnode, &tp->out_of_order_queue);
513 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
514 tcp_nip_drop(sk, skb1);
515 }
516 /* If there is no skb after us, we are the last_skb ! */
517 if (!skb1)
518 tp->ooo_last_skb = skb;
519
520 end:
521 if (skb) {
522 /* Try space compression for the skb. if the skb has enough space left in its
523 * linear space, the page fragment from its shared space can be copied into the
524 * linear space to free the page fragment. If the remaining amount of linear space
525 * is less than the length of the page fragment, or if the skb has been cloned
526 * (the page fragment is shared with other SKBS), no compression is performed.
527 */
528 skb_condense(skb);
529 skb_set_owner_r(skb, sk);
530 }
531 }
532
533 #define PKT_DISCARD_MAX 500
tcp_nip_data_queue(struct sock * sk,struct sk_buff * skb)534 static void tcp_nip_data_queue(struct sock *sk, struct sk_buff *skb)
535 {
536 int mss = tcp_nip_current_mss(sk);
537 struct tcp_sock *tp = tcp_sk(sk);
538 struct inet_connection_sock *icsk = inet_csk(sk);
539 u32 cur_win = tcp_receive_window(tp);
540 u32 seq_max = tp->rcv_nxt + cur_win;
541
542 if (mss <= 0) {
543 nip_dbg("invalid parameter, mss=%u", mss);
544 __kfree_skb(skb);
545 return;
546 }
547 /* Newip Urg_ptr is disabled. Urg_ptr is used to carry the number of discarded packets */
548 tp->snd_up = (TCP_SKB_CB(skb)->seq - tcp_sk(sk)->rcv_nxt) / mss;
549 tp->snd_up = tp->snd_up > PKT_DISCARD_MAX ? 0 : tp->snd_up;
550
551 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
552 nip_dbg("no data, only handle ack");
553 __kfree_skb(skb);
554 return;
555 }
556
557 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
558 if (cur_win == 0) {
559 nip_dbg("rcv window is 0");
560 goto out_of_window;
561 }
562 }
563
564 /* Out of window. F.e. zero window probe. */
565 if (!before(TCP_SKB_CB(skb)->seq, seq_max)) {
566 nip_dbg("out of rcv win, seq=[%u-%u], rcv_nxt=%u, seq_max=%u",
567 TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
568 tp->rcv_nxt, seq_max);
569 goto out_of_window;
570 }
571
572 if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
573 /* A retransmit, 2nd most common case. Force an immediate ack. */
574 nip_dbg("rcv retransmit pkt, seq=[%u-%u], rcv_nxt=%u",
575 TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt);
576 out_of_window:
577 inet_csk_schedule_ack(sk);
578 tcp_nip_drop(sk, skb);
579 return;
580 }
581 icsk->icsk_ack.lrcvtime = tcp_jiffies32;
582 __skb_pull(skb, tcp_hdr(skb)->doff * TCP_NUM_4);
583
584 if (cur_win == 0 || after(TCP_SKB_CB(skb)->end_seq, seq_max)) {
585 nip_dbg("win lack, drop pkt, seq=[%u-%u], seq_max=%u, rmem_alloc/rbuf=[%u:%u]",
586 TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
587 seq_max, atomic_read(&sk->sk_rmem_alloc), sk->sk_rcvbuf);
588 /* wake up processes that are blocked for lack of data */
589 sk->sk_data_ready(sk);
590 inet_csk_schedule_ack(sk);
591 tcp_nip_drop(sk, skb);
592 return;
593 }
594
595 /* case1: seq == rcv_next
596 * case2: seq -- rcv_next -- end_seq ==> rcv_next(seq) -- end_seq
597 */
598 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt ||
599 (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt) &&
600 after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))) {
601 if (TCP_SKB_CB(skb)->seq != tp->rcv_nxt)
602 tcp_nip_overlap_handle(tp, skb);
603
604 nip_dbg("packet received. seq=[%u-%u], rcv_nxt=%u, skb->len=%u",
605 TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
606 tp->rcv_nxt, skb->len);
607
608 __skb_queue_tail(&sk->sk_receive_queue, skb);
609 skb_set_owner_r(skb, sk);
610 tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
611 inet_csk_schedule_ack(sk);
612 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
613 tcp_nip_fin(sk);
614 if (!RB_EMPTY_ROOT(&tp->out_of_order_queue))
615 tcp_nip_ofo_queue(sk);
616 if (!sock_flag(sk, SOCK_DEAD))
617 sk->sk_data_ready(sk);
618 return;
619 }
620
621 nip_dbg("ofo packet received. seq=[%u-%u], rcv_nxt=%u, skb->len=%u",
622 TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
623 tp->rcv_nxt, skb->len);
624
625 tcp_nip_data_queue_ofo(sk, skb);
626 }
627
tcp_nip_push_pending_frames(struct sock * sk)628 static inline void tcp_nip_push_pending_frames(struct sock *sk)
629 {
630 if (tcp_nip_send_head(sk)) {
631 struct tcp_sock *tp = tcp_sk(sk);
632 u32 cur_mss = tcp_nip_current_mss(sk); // TCP_BASE_MSS
633
634 __tcp_nip_push_pending_frames(sk, cur_mss, tp->nonagle);
635 }
636 }
637
tcp_nip_new_space(struct sock * sk)638 static void tcp_nip_new_space(struct sock *sk)
639 {
640 sk->sk_write_space(sk);
641 }
642
tcp_nip_check_space(struct sock * sk)643 void tcp_nip_check_space(struct sock *sk)
644 {
645 /* Invoke memory barrier (annotated prior to checkpatch requirements) */
646 smp_mb();
647 if (sk->sk_socket && test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
648 tcp_nip_new_space(sk);
649 }
650
tcp_nip_data_snd_check(struct sock * sk)651 static inline void tcp_nip_data_snd_check(struct sock *sk)
652 {
653 tcp_nip_push_pending_frames(sk);
654 tcp_nip_check_space(sk);
655 }
656
657 #define TCP_NIP_DELACK_MIN (HZ / 50)
tcp_nip_send_delayed_ack(struct sock * sk)658 void tcp_nip_send_delayed_ack(struct sock *sk)
659 {
660 struct inet_connection_sock *icsk = inet_csk(sk);
661 int ato = TCP_NIP_DELACK_MIN;
662 unsigned long timeout;
663
664 icsk->icsk_ack.ato = TCP_DELACK_MIN;
665
666 /* Stay within the limit we were given */
667 timeout = jiffies + ato;
668
669 /* Use new timeout only if there wasn't a older one earlier. */
670 if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
671 if (time_before_eq(icsk->icsk_ack.timeout,
672 jiffies + (ato >> TCP_NIP_4BYTE_PAYLOAD))) {
673 nip_dbg("ok");
674 tcp_nip_send_ack(sk);
675 return;
676 }
677
678 if (!time_before(timeout, icsk->icsk_ack.timeout))
679 timeout = icsk->icsk_ack.timeout;
680 }
681
682 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
683 icsk->icsk_ack.timeout = timeout;
684 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
685 }
686
__tcp_nip_ack_snd_check(struct sock * sk,int ofo_possible)687 static void __tcp_nip_ack_snd_check(struct sock *sk, int ofo_possible)
688 {
689 struct tcp_sock *tp = tcp_sk(sk);
690 struct tcp_nip_common *ntp = &tcp_nip_sk(sk)->common;
691
692 inet_csk(sk)->icsk_ack.rcv_mss = tcp_nip_current_mss(sk); // TCP_BASE_MSS
693
694 /* More than n full frame received... */
695 if (((tp->rcv_nxt - tp->rcv_wup) > get_ack_num() * inet_csk(sk)->icsk_ack.rcv_mss &&
696 __nip_tcp_select_window(sk) >= tp->rcv_wnd) ||
697 /* We have out of order data. */
698 (ofo_possible && (!RB_EMPTY_ROOT(&tp->out_of_order_queue)))) {
699 if (ofo_possible && (!RB_EMPTY_ROOT(&tp->out_of_order_queue))) {
700 if (tp->rcv_nxt == ntp->last_rcv_nxt) {
701 ntp->dup_ack_cnt++;
702 } else {
703 ntp->dup_ack_cnt = 0;
704 ntp->last_rcv_nxt = tp->rcv_nxt;
705 }
706 if (ntp->dup_ack_cnt < get_dup_ack_snd_max())
707 tcp_nip_send_ack(sk);
708 else if (ntp->dup_ack_cnt % get_dup_ack_snd_max() == 0)
709 tcp_nip_send_ack(sk);
710 } else {
711 tcp_nip_send_ack(sk);
712 }
713 } else {
714 /* Else, send delayed ack. */
715 tcp_nip_send_delayed_ack(sk);
716 }
717 }
718
tcp_nip_ack_snd_check(struct sock * sk)719 static inline void tcp_nip_ack_snd_check(struct sock *sk)
720 {
721 if (!inet_csk_ack_scheduled(sk)) {
722 /* We sent a data segment already. */
723 nip_dbg("We sent a data segment already");
724 return;
725 }
726 __tcp_nip_ack_snd_check(sk, 1);
727 }
728
tcp_nip_snd_una_update(struct tcp_sock * tp,u32 ack)729 static void tcp_nip_snd_una_update(struct tcp_sock *tp, u32 ack)
730 {
731 u32 delta = ack - tp->snd_una;
732
733 sock_owned_by_me((struct sock *)tp);
734 tp->bytes_acked += delta;
735 tp->snd_una = ack;
736 }
737
tcp_nip_rearm_rto(struct sock * sk)738 void tcp_nip_rearm_rto(struct sock *sk)
739 {
740 struct tcp_sock *tp = tcp_sk(sk);
741 struct inet_connection_sock *icsk = inet_csk(sk);
742
743 if (!tp->packets_out) {
744 int icsk_backoff = icsk->icsk_backoff;
745
746 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
747 icsk->icsk_backoff = 0; /* V4 no modified this line */
748 nip_dbg("stop tcp retrans timer, icsk_backoff %u to %u",
749 icsk_backoff, icsk->icsk_backoff);
750 } else {
751 u32 rto = inet_csk(sk)->icsk_rto;
752
753 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto, TCP_RTO_MAX);
754 }
755 }
756
update_nip_srtt(u32 * srtt,u32 rtt_tstamp)757 static void update_nip_srtt(u32 *srtt, u32 rtt_tstamp)
758 {
759 if (*srtt == 0)
760 *srtt = rtt_tstamp;
761 else
762 *srtt = (SRTT_FACTOR_FOUR_FIVE * (*srtt) + rtt_tstamp) /
763 SRTT_DIVISOR_FACTOR;
764 ssthresh_dbg("rtt_tstamp=%u, srtt=%u", rtt_tstamp, *srtt);
765 }
766
update_pcache_rto(u32 * rto,int srtt)767 static void update_pcache_rto(u32 *rto, int srtt)
768 {
769 int cal_rto;
770 int srtt_factor;
771 int calc_srtt;
772
773 srtt_factor = get_nip_srtt_factor() <= 0 ? 1 : get_nip_srtt_factor();
774 calc_srtt = srtt + srtt / srtt_factor;
775 cal_rto = get_nip_dynamic_rto_min() >= calc_srtt ? get_nip_dynamic_rto_min() : calc_srtt;
776 *rto = get_nip_dynamic_rto_max() >= cal_rto ? cal_rto : get_nip_dynamic_rto_max();
777 }
778
update_nip_rto(u32 * icsk_rto,u32 rtt_tstamp,struct sock * sk)779 static void update_nip_rto(u32 *icsk_rto, u32 rtt_tstamp, struct sock *sk)
780 {
781 struct tcp_nip_common *ntp = &tcp_nip_sk(sk)->common;
782
783 update_nip_srtt(&ntp->nip_srtt, rtt_tstamp);
784 update_pcache_rto(icsk_rto, ntp->nip_srtt);
785 }
786
tcp_nip_clean_rtx_queue(struct sock * sk,ktime_t * skb_snd_tstamp)787 static int tcp_nip_clean_rtx_queue(struct sock *sk, ktime_t *skb_snd_tstamp)
788 {
789 struct tcp_sock *tp = tcp_sk(sk);
790 struct sk_buff *skb;
791 int flag = 0;
792 u32 rtt_tstamp;
793 struct inet_connection_sock *icsk = inet_csk(sk);
794
795 while ((skb = tcp_write_queue_head(sk)) && skb != tcp_nip_send_head(sk)) {
796 struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
797 u32 acked_pcount = 0;
798
799 if (after(scb->end_seq, tp->snd_una)) {
800 if (tcp_skb_pcount(skb) == 1 || !after(tp->snd_una, scb->seq))
801 break;
802 nip_dbg("ack error");
803 } else {
804 prefetchw(skb->next);
805 acked_pcount = tcp_skb_pcount(skb);
806 }
807
808 if (likely(!(scb->tcp_flags & TCPHDR_SYN))) {
809 flag |= FLAG_DATA_ACKED;
810 } else {
811 flag |= FLAG_SYN_ACKED;
812 tp->retrans_stamp = 0;
813 }
814
815 tp->packets_out -= acked_pcount;
816
817 if (*skb_snd_tstamp == 0)
818 *skb_snd_tstamp = skb->tstamp;
819
820 tcp_nip_modify_send_head(sk, skb);
821 tcp_unlink_write_queue(skb, sk);
822 sk_wmem_free_skb(sk, skb);
823 }
824 /* V4 no modified this line */
825 rtt_tstamp = tp->rcv_tstamp - *skb_snd_tstamp;
826 update_nip_rto(&icsk->icsk_rto, rtt_tstamp, sk);
827 if (flag & FLAG_ACKED)
828 tcp_nip_rearm_rto(sk);
829 return 0;
830 }
831
832 /* Function
833 * Allocate a connection request block that holds connection request information.
834 * At the same time, initialize the set of operations used to send ACK/RST segments
835 * during connection, so that these interfaces can be easily called during establishment.
836 * Set the socket state to TCP_NEW_SYN_RECV
837 * Parameter
838 * ops: Request the functional interface of the control block
839 * sk_listener: Transmission control block
840 * attach_listener: Whether to set cookies
841 */
ninet_reqsk_alloc(const struct request_sock_ops * ops,struct sock * sk_listener,bool attach_listener)842 struct request_sock *ninet_reqsk_alloc(const struct request_sock_ops *ops,
843 struct sock *sk_listener,
844 bool attach_listener)
845 {
846 struct request_sock *req = reqsk_alloc(ops, sk_listener,
847 attach_listener);
848
849 if (req) {
850 struct inet_request_sock *ireq = inet_rsk(req);
851
852 ireq->ireq_opt = NULL;
853 atomic64_set(&ireq->ir_cookie, 0);
854 ireq->ireq_state = TCP_NEW_SYN_RECV;
855 write_pnet(&ireq->ireq_net, sock_net(sk_listener));
856 ireq->ireq_family = sk_listener->sk_family;
857 }
858
859 return req;
860 }
861
tcp_nip_parse_mss(struct tcp_options_received * opt_rx,const struct tcphdr * th,const unsigned char * ptr,int opsize,int estab)862 void tcp_nip_parse_mss(struct tcp_options_received *opt_rx,
863 const struct tcphdr *th,
864 const unsigned char *ptr,
865 int opsize,
866 int estab)
867 {
868 if (opsize == TCPOLEN_MSS && th->syn && !estab) {
869 u16 in_mss = get_unaligned_be16(ptr);
870
871 nip_dbg("in_mss %d", in_mss);
872
873 if (in_mss) {
874 if (opt_rx->user_mss &&
875 opt_rx->user_mss < in_mss)
876 in_mss = opt_rx->user_mss;
877 opt_rx->mss_clamp = in_mss;
878 }
879 }
880 }
881
882 /* Function
883 * Look for tcp options. Normally only called on SYN and SYNACK packets.
884 * Parsing of TCP options in SKB
885 * Parameter
886 * skb: Transfer control block buffer
887 * opt_rx: Saves the structure for TCP options
888 * estab: WANTCOOKIE
889 * foc: Len field
890 */
tcp_nip_parse_options(const struct sk_buff * skb,struct tcp_options_received * opt_rx,int estab,struct tcp_fastopen_cookie * foc)891 void tcp_nip_parse_options(const struct sk_buff *skb,
892 struct tcp_options_received *opt_rx, int estab,
893 struct tcp_fastopen_cookie *foc)
894 {
895 const unsigned char *ptr;
896 const struct tcphdr *th = tcp_hdr(skb);
897 /* The length of the TCP option = Length of TCP header - The length of the TCP structure */
898 int length = (th->doff * TCP_NUM_4) - sizeof(struct tcphdr);
899
900 /* A pointer to the option position */
901 ptr = (const unsigned char *)(th + 1);
902 opt_rx->saw_tstamp = 0;
903
904 while (length > 0) {
905 int opcode = *ptr++;
906 int opsize;
907
908 switch (opcode) {
909 case TCPOPT_EOL:
910 return;
911 case TCPOPT_NOP:
912 length--;
913 continue;
914 default:
915 opsize = *ptr++;
916 if (opsize < TCP_NUM_2) /* "2 - silly options" */
917 return;
918 if (opsize > length)
919 return; /* don't parse partial options */
920 switch (opcode) {
921 case TCPOPT_MSS:
922 tcp_nip_parse_mss(opt_rx, th, ptr, opsize, estab);
923 break;
924 default:
925 break;
926 }
927 ptr += opsize - TCP_NUM_2;
928 length -= opsize;
929 }
930 }
931 }
932
tcp_nip_common_init(struct request_sock * req)933 static void tcp_nip_common_init(struct request_sock *req)
934 {
935 struct tcp_nip_request_sock *niptreq = tcp_nip_rsk(req);
936 struct tcp_nip_common *ntp = &niptreq->common;
937
938 memset(ntp, 0, sizeof(*ntp));
939 ntp->nip_ssthresh = get_nip_ssthresh_default();
940 }
941
942 /* Function
943 * Initializes the connection request block information based
944 * on the options and sequence number in the received SYN segment
945 * Parameter
946 * req: Request connection control block
947 * rx_opt: Saves the structure for TCP options
948 * skb: Transfer control block buffer.
949 * sk: transmission control block.
950 */
tcp_nip_openreq_init(struct request_sock * req,const struct tcp_options_received * rx_opt,struct sk_buff * skb,const struct sock * sk)951 static void tcp_nip_openreq_init(struct request_sock *req,
952 const struct tcp_options_received *rx_opt,
953 struct sk_buff *skb, const struct sock *sk)
954 {
955 struct inet_request_sock *ireq = inet_rsk(req);
956
957 tcp_nip_common_init(req);
958
959 req->rsk_rcv_wnd = 0;
960 tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
961 tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
962 tcp_rsk(req)->snt_synack = tcp_clock_us();
963 tcp_rsk(req)->last_oow_ack_time = 0;
964 req->mss = rx_opt->mss_clamp;
965 req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
966 ireq->tstamp_ok = rx_opt->tstamp_ok;
967 ireq->snd_wscale = rx_opt->snd_wscale;
968
969 if (get_wscale_enable()) {
970 ireq->wscale_ok = 1;
971 ireq->snd_wscale = get_wscale(); // rx_opt->snd_wscale;
972 ireq->rcv_wscale = get_wscale();
973 }
974
975 ireq->acked = 0;
976 ireq->ecn_ok = 0;
977 ireq->ir_rmt_port = tcp_hdr(skb)->source;
978 ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
979 ireq->ir_mark = sk->sk_mark;
980 }
981
982 /* Function
983 * Based on listening SOCK and REQ, create a transport control block
984 * for the new connection and initialize it.
985 * Parameter
986 * sk: the listening transmission control block.
987 * req: Request connection control block
988 * skb: Transfer control block buffer.
989 */
tcp_nip_create_openreq_child(const struct sock * sk,struct request_sock * req,struct sk_buff * skb)990 struct sock *tcp_nip_create_openreq_child(const struct sock *sk,
991 struct request_sock *req,
992 struct sk_buff *skb)
993 {
994 /* Clone a transport control block and lock the new transport control block */
995 struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC);
996
997 if (newsk) {
998 const struct inet_request_sock *ireq = inet_rsk(req);
999 struct tcp_request_sock *treq = tcp_rsk(req);
1000 struct inet_connection_sock *newicsk = inet_csk(newsk);
1001 struct tcp_sock *newtp = tcp_sk(newsk);
1002
1003 /* Now setup tcp_sock */
1004 newtp->pred_flags = 0;
1005
1006 /* The variables related to the receiving and sending serial numbers
1007 * are initialized. The second handshake sends an ACK in the SYN+ACK segment
1008 */
1009 newtp->rcv_wup = treq->rcv_isn + 1;
1010 newtp->copied_seq = treq->rcv_isn + 1;
1011 newtp->rcv_nxt = treq->rcv_isn + 1;
1012 newtp->segs_in = 1;
1013 /* The second handshake sends seq+1 in the SYN+ACK segment */
1014 newtp->snd_sml = treq->snt_isn + 1;
1015 newtp->snd_una = treq->snt_isn + 1;
1016 newtp->snd_nxt = treq->snt_isn + 1;
1017 newtp->snd_up = treq->snt_isn + 1;
1018
1019 INIT_LIST_HEAD(&newtp->tsq_node);
1020
1021 /* The ACK segment number of the send window that
1022 * received the first handshake update
1023 */
1024 tcp_init_wl(newtp, treq->rcv_isn);
1025
1026 /* Initialization of delay-related variables */
1027 minmax_reset(&newtp->rtt_min, tcp_jiffies32, ~0U);
1028 newicsk->icsk_rto = get_nip_rto() == 0 ? TCP_TIMEOUT_INIT : (HZ / get_nip_rto());
1029 newicsk->icsk_ack.lrcvtime = tcp_jiffies32;
1030
1031 /* The congestion control-related variables are initialized */
1032 newtp->packets_out = 0;
1033
1034 newtp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
1035
1036 newtp->lsndtime = tcp_jiffies32;
1037
1038 newtp->total_retrans = req->num_retrans;
1039
1040 newtp->snd_cwnd = TCP_INIT_CWND;
1041
1042 /* There's a bubble in the pipe until at least the first ACK. */
1043 newtp->app_limited = ~0U;
1044
1045 /* Initialize several timers */
1046 tcp_nip_init_xmit_timers(newsk);
1047 newtp->write_seq = treq->snt_isn + 1;
1048 newtp->pushed_seq = treq->snt_isn + 1;
1049
1050 /* TCP option correlation */
1051 newtp->rx_opt.saw_tstamp = 0;
1052
1053 newtp->rx_opt.dsack = 0;
1054 newtp->rx_opt.num_sacks = 0;
1055
1056 newtp->urg_data = 0;
1057
1058 newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
1059 newtp->window_clamp = req->rsk_window_clamp;
1060 newtp->rcv_ssthresh = req->rsk_rcv_wnd;
1061 newtp->rcv_wnd = req->rsk_rcv_wnd;
1062 newtp->rx_opt.wscale_ok = ireq->wscale_ok;
1063 if (newtp->rx_opt.wscale_ok) {
1064 newtp->rx_opt.snd_wscale = ireq->snd_wscale;
1065 newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
1066 } else {
1067 newtp->rx_opt.snd_wscale = 0;
1068 newtp->rx_opt.rcv_wscale = 0;
1069 newtp->window_clamp = min(newtp->window_clamp, TCP_NIP_WINDOW_MAX);
1070 }
1071 newtp->snd_wnd = (ntohs(tcp_hdr(skb)->window) <<
1072 newtp->rx_opt.snd_wscale);
1073 newtp->max_window = newtp->snd_wnd;
1074
1075 if (newtp->rx_opt.tstamp_ok) {
1076 newtp->rx_opt.ts_recent = req->ts_recent;
1077 newtp->rx_opt.ts_recent_stamp = get_seconds();
1078 newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
1079 } else {
1080 newtp->rx_opt.ts_recent_stamp = 0;
1081 newtp->tcp_header_len = sizeof(struct tcphdr);
1082 }
1083 newtp->tsoffset = 0;
1084
1085 /* Determines the size of the last passed segment */
1086 if (skb->len >= TCP_MSS_DEFAULT + newtp->tcp_header_len)
1087 newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;
1088 newtp->rx_opt.mss_clamp = req->mss;
1089 newtp->fastopen_req = NULL;
1090 newtp->fastopen_rsk = NULL;
1091 newtp->syn_data_acked = 0;
1092 newtp->rack.mstamp = 0;
1093 newtp->rack.advanced = 0;
1094
1095 __TCP_INC_STATS(sock_net(sk), TCP_MIB_PASSIVEOPENS);
1096 }
1097 return newsk;
1098 }
1099
tcp_nip_openreq_init_rwin(struct request_sock * req,const struct sock * sk_listener,const struct dst_entry * dst)1100 void tcp_nip_openreq_init_rwin(struct request_sock *req,
1101 const struct sock *sk_listener,
1102 const struct dst_entry *dst)
1103 {
1104 struct inet_request_sock *ireq = inet_rsk(req);
1105 const struct tcp_sock *tp = tcp_sk(sk_listener);
1106 int full_space = tcp_full_space(sk_listener);
1107 int mss;
1108 u32 window_clamp;
1109 __u8 rcv_wscale;
1110
1111 mss = tcp_mss_clamp(tp, dst_metric_advmss(dst));
1112
1113 window_clamp = READ_ONCE(tp->window_clamp);
1114 /* Set this up on the first call only */
1115 req->rsk_window_clamp = window_clamp ? : dst_metric(dst, RTAX_WINDOW);
1116
1117 /* limit the window selection if the user enforce a smaller rx buffer */
1118 if ((sk_listener->sk_userlocks & SOCK_RCVBUF_LOCK) &&
1119 (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
1120 req->rsk_window_clamp = full_space;
1121
1122 /* tcp_full_space because it is guaranteed to be the first packet */
1123 tcp_select_initial_window(sk_listener, full_space,
1124 mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
1125 &req->rsk_rcv_wnd,
1126 &req->rsk_window_clamp,
1127 0,
1128 &rcv_wscale,
1129 0);
1130 ireq->rcv_wscale = get_wscale_enable() ? get_wscale() : rcv_wscale;
1131 }
1132
1133 /* Function
1134 * A function used by the server to process client connection requests.
1135 * Parameter
1136 * rsk_ops: Functional interface to request control blocks.
1137 * af_ops: The functional interface of the TCP request block.
1138 * sk: transmission control block.
1139 * skb: Transfer control block buffer.
1140 */
_tcp_nip_conn_request(struct request_sock_ops * rsk_ops,const struct tcp_request_sock_ops * af_ops,struct sock * sk,struct sk_buff * skb)1141 int _tcp_nip_conn_request(struct request_sock_ops *rsk_ops,
1142 const struct tcp_request_sock_ops *af_ops,
1143 struct sock *sk, struct sk_buff *skb)
1144 {
1145 struct tcp_fastopen_cookie foc = { .len = -1 };
1146
1147 __u32 isn = TCP_SKB_CB(skb)->tcp_tw_isn;
1148 /* All received TCP options are resolved into this structure */
1149 struct tcp_options_received tmp_opt;
1150 struct tcp_sock *tp = tcp_sk(sk);
1151 struct dst_entry *dst = NULL;
1152 struct request_sock *req;
1153
1154 /* If the half-connection queue length has reached the upper limit,
1155 * the current request is discarded
1156 */
1157 if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
1158 nip_dbg("inet_csk_reqsk_queue_is_full");
1159 goto drop;
1160 }
1161
1162 /* If the queue holds the socket that has completed the connection (full connection queue)
1163 * The length has reached its upper limit
1164 * The current request is discarded
1165 */
1166 if (sk_acceptq_is_full(sk)) {
1167 NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
1168 nip_dbg("sk_acceptq_is_full, sk_ack_backlog=%u, sk_max_ack_backlog=%u",
1169 READ_ONCE(sk->sk_ack_backlog),
1170 READ_ONCE(sk->sk_max_ack_backlog));
1171 goto drop;
1172 }
1173
1174 /* Allocate a connection request block that holds connection request information
1175 * While initializing the connection process
1176 * The set of operations that send ACK/RST segments
1177 * These interfaces can be easily invoked during the setup process.
1178 */
1179 req = ninet_reqsk_alloc(rsk_ops, sk, true);
1180 if (!req)
1181 goto drop;
1182
1183 tcp_rsk(req)->af_specific = af_ops;
1184
1185 tcp_clear_options(&tmp_opt);
1186 /* Maximum MSS negotiated during connection establishment */
1187 tmp_opt.mss_clamp = af_ops->mss_clamp;
1188 /* The best way to do this is to prink the value of user_mss and see if it is 0 */
1189 tmp_opt.user_mss = tp->rx_opt.user_mss;
1190 /* Parsing of TCP options in SKB */
1191 tcp_nip_parse_options(skb, &tmp_opt, 0, NULL);
1192
1193 /* Tstamp_ok indicates the TIMESTAMP seen on the received SYN packet */
1194 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
1195 /* Initializes the connection request block information based on the options
1196 * and sequence number in the received SYN segment
1197 */
1198 tcp_nip_openreq_init(req, &tmp_opt, skb, sk);
1199
1200 inet_rsk(req)->ir_iif = sk->sk_bound_dev_if;
1201
1202 af_ops->init_req(req, sk, skb);
1203
1204 /* Based on the security context of the socket and packet,
1205 * this function calculates the security context of the connection
1206 * and checks whether establishing a TCP connection is permitted.
1207 */
1208 if (security_inet_conn_request(sk, skb, req))
1209 goto drop_and_free;
1210
1211 if (!isn)
1212 isn = af_ops->init_seq(skb);
1213
1214 if (!dst) {
1215 dst = af_ops->route_req(sk, NULL, req);
1216 if (!dst)
1217 goto drop_and_free;
1218 }
1219
1220 tcp_rsk(req)->snt_isn = isn;
1221 tcp_rsk(req)->txhash = net_tx_rndhash();
1222 /* Initialize the receive window */
1223 tcp_nip_openreq_init_rwin(req, sk, dst);
1224 /* Record the syn */
1225 tcp_rsk(req)->tfo_listener = false;
1226 /* Add a timer to add reQ to the ehash table */
1227 ninet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
1228
1229 af_ops->send_synack(sk, dst, NULL, req, &foc, TCP_SYNACK_NORMAL, NULL);
1230
1231 reqsk_put(req);
1232 return 0;
1233
1234 drop_and_free:
1235 reqsk_free(req);
1236 drop:
1237 tcp_listendrop(sk);
1238 return 0;
1239 }
1240
tcp_nip_paws_check(const struct tcp_options_received * rx_opt,int paws_win)1241 static inline bool tcp_nip_paws_check(const struct tcp_options_received *rx_opt,
1242 int paws_win)
1243 {
1244 if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1245 return true;
1246 if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS))
1247 return true;
1248
1249 if (!rx_opt->ts_recent)
1250 return true;
1251 return false;
1252 }
1253
tcp_nip_may_update_window(const struct tcp_sock * tp,const u32 ack,const u32 ack_seq,const u32 nwin)1254 static inline bool tcp_nip_may_update_window(const struct tcp_sock *tp,
1255 const u32 ack, const u32 ack_seq,
1256 const u32 nwin)
1257 {
1258 return after(ack, tp->snd_una) ||
1259 after(ack_seq, tp->snd_wl1) ||
1260 (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd);
1261 }
1262
tcp_nip_ack_update_window(struct sock * sk,const struct sk_buff * skb,u32 ack,u32 ack_seq)1263 static void tcp_nip_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,
1264 u32 ack_seq)
1265 {
1266 struct tcp_sock *tp = tcp_sk(sk);
1267 u32 nwin = ntohs(tcp_hdr(skb)->window);
1268
1269 if (likely(!tcp_hdr(skb)->syn))
1270 nwin <<= tp->rx_opt.snd_wscale;
1271
1272 if (tcp_nip_may_update_window(tp, ack, ack_seq, nwin)) {
1273 tcp_update_wl(tp, ack_seq);
1274
1275 if (tp->snd_wnd != nwin) {
1276 nip_dbg("snd_wnd change [%u to %u]", tp->snd_wnd, nwin);
1277 tp->snd_wnd = nwin;
1278 tp->pred_flags = 0;
1279 }
1280 }
1281 }
1282
1283 /* Check whether the ACK returned by the packet is detected
1284 * and whether the peer window is opened
1285 */
tcp_nip_ack_probe(struct sock * sk)1286 static void tcp_nip_ack_probe(struct sock *sk)
1287 {
1288 const struct tcp_sock *tp = tcp_sk(sk);
1289 struct inet_connection_sock *icsk = inet_csk(sk);
1290
1291 if (!after(TCP_SKB_CB(tcp_nip_send_head(sk))->end_seq, tcp_wnd_end(tp))) {
1292 icsk->icsk_backoff = 0;
1293 icsk->icsk_probes_tstamp = 0;
1294 nip_dbg("stop probe0 timer");
1295 inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0);
1296 /* Socket must be waked up by subsequent tcp_data_snd_check().
1297 * This function is not for random using!
1298 */
1299 } else {
1300 unsigned long when = tcp_probe0_when(sk, TCP_RTO_MAX);
1301 unsigned long base_when = tcp_probe0_base(sk);
1302 u8 icsk_backoff = inet_csk(sk)->icsk_backoff;
1303
1304 nip_dbg("start probe0 timer, when=%lu, RTO MAX=%u, base_when=%lu, backoff=%u",
1305 when, TCP_RTO_MAX, base_when, icsk_backoff);
1306 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0, when, TCP_RTO_MAX);
1307 }
1308 }
1309
1310 #define DUP_ACK 0
1311 #define NOR_ACK 1
1312 #define ACK_DEF 2
tcp_nip_ack_retrans(struct sock * sk,u32 ack,int ack_type,u32 retrans_num)1313 static void tcp_nip_ack_retrans(struct sock *sk, u32 ack, int ack_type, u32 retrans_num)
1314 {
1315 int skb_index = 0;
1316 struct tcp_sock *tp = tcp_sk(sk);
1317 struct tcp_nip_common *ntp = &tcp_nip_sk(sk)->common;
1318 struct sk_buff *skb, *tmp;
1319 const char *ack_str[ACK_DEF] = {"dup", "nor"};
1320 int index = ack_type == DUP_ACK ? DUP_ACK : NOR_ACK;
1321
1322 skb_queue_walk_safe(&sk->sk_write_queue, skb, tmp) {
1323 if (skb == tcp_nip_send_head(sk)) {
1324 ssthresh_dbg("%s ack retrans(%u) end, ack=%u, seq=%u~%u, pkt_out=%u",
1325 ack_str[index], ntp->ack_retrans_num, ack,
1326 tp->selective_acks[0].start_seq,
1327 tp->selective_acks[0].end_seq, tp->packets_out);
1328 tp->selective_acks[0].start_seq = 0;
1329 tp->selective_acks[0].end_seq = 0;
1330 ntp->ack_retrans_seq = 0;
1331 ntp->ack_retrans_num = 0;
1332 break;
1333 }
1334
1335 if (TCP_SKB_CB(skb)->seq > tp->selective_acks[0].end_seq) {
1336 ssthresh_dbg("%s ack retrans(%u) finish, ack=%u, seq=%u~%u, pkt_out=%u",
1337 ack_str[index], ntp->ack_retrans_num, ack,
1338 tp->selective_acks[0].start_seq,
1339 tp->selective_acks[0].end_seq, tp->packets_out);
1340
1341 tp->selective_acks[0].start_seq = 0;
1342 tp->selective_acks[0].end_seq = 0;
1343 ntp->ack_retrans_seq = 0;
1344 ntp->ack_retrans_num = 0;
1345 break;
1346 }
1347
1348 if (TCP_SKB_CB(skb)->seq != ntp->ack_retrans_seq)
1349 continue;
1350
1351 if (skb_index < retrans_num) {
1352 tcp_nip_retransmit_skb(sk, skb, 1);
1353 skb_index++;
1354 ntp->ack_retrans_num++;
1355 ntp->ack_retrans_seq = TCP_SKB_CB(skb)->end_seq;
1356 } else {
1357 retrans_dbg("%s ack retrans(%u) no end, ack=%u, seq=%u~%u, pkt_out=%u",
1358 ack_str[index], ntp->ack_retrans_num, ack,
1359 tp->selective_acks[0].start_seq,
1360 tp->selective_acks[0].end_seq, tp->packets_out);
1361 break;
1362 }
1363 }
1364 }
1365
1366 #define DUP_ACK_RETRANS_START_NUM 3
1367 #define DIVIDEND_UP 3
1368 #define DIVIDEND_DOWN 5
tcp_nip_dup_ack_retrans(struct sock * sk,const struct sk_buff * skb,u32 ack,u32 retrans_num)1369 static void tcp_nip_dup_ack_retrans(struct sock *sk, const struct sk_buff *skb,
1370 u32 ack, u32 retrans_num)
1371 {
1372 if (tcp_write_queue_head(sk)) {
1373 struct tcp_sock *tp = tcp_sk(sk);
1374 struct tcp_nip_common *ntp = &tcp_nip_sk(sk)->common;
1375
1376 tp->sacked_out++;
1377 if (tp->sacked_out == DUP_ACK_RETRANS_START_NUM) {
1378 /* Newip Urg_ptr is disabled. Urg_ptr is used to
1379 * carry the number of discarded packets
1380 */
1381 int mss = tcp_nip_current_mss(sk);
1382 struct tcphdr *th = (struct tcphdr *)skb->data;
1383 u16 discard_num = htons(th->urg_ptr) > PKT_DISCARD_MAX ?
1384 0 : htons(th->urg_ptr);
1385 u32 last_nip_ssthresh = ntp->nip_ssthresh;
1386
1387 if (tp->selective_acks[0].end_seq)
1388 ssthresh_dbg("last retans(%u) not end, seq=%u~%u, pkt_out=%u",
1389 ntp->ack_retrans_num,
1390 tp->selective_acks[0].start_seq,
1391 tp->selective_acks[0].end_seq,
1392 tp->packets_out);
1393
1394 tp->selective_acks[0].start_seq = ack;
1395 tp->selective_acks[0].end_seq = ack + discard_num * mss;
1396 ntp->ack_retrans_seq = ack;
1397 ntp->ack_retrans_num = 0;
1398
1399 ntp->nip_ssthresh = get_ssthresh_low();
1400 ssthresh_dbg("new dup ack, win %u to %u, discard_num=%u, seq=%u~%u",
1401 last_nip_ssthresh, ntp->nip_ssthresh, discard_num,
1402 tp->selective_acks[0].start_seq,
1403 tp->selective_acks[0].end_seq);
1404
1405 tcp_nip_ack_retrans(sk, ack, DUP_ACK, retrans_num);
1406 }
1407 }
1408 }
1409
tcp_nip_nor_ack_retrans(struct sock * sk,u32 ack,u32 retrans_num)1410 static void tcp_nip_nor_ack_retrans(struct sock *sk, u32 ack, u32 retrans_num)
1411 {
1412 struct tcp_sock *tp = tcp_sk(sk);
1413 struct tcp_nip_common *ntp = &tcp_nip_sk(sk)->common;
1414
1415 if (tp->selective_acks[0].end_seq != 0) {
1416 if (ack >= tp->selective_acks[0].end_seq) {
1417 ssthresh_dbg("nor ack retrans(%u) resume, seq=%u~%u, pkt_out=%u, ack=%u",
1418 ntp->ack_retrans_num,
1419 tp->selective_acks[0].start_seq,
1420 tp->selective_acks[0].end_seq, tp->packets_out, ack);
1421 tp->selective_acks[0].start_seq = 0;
1422 tp->selective_acks[0].end_seq = 0;
1423 ntp->ack_retrans_seq = 0;
1424 ntp->ack_retrans_num = 0;
1425
1426 tp->sacked_out = 0;
1427 return;
1428 }
1429
1430 tcp_nip_ack_retrans(sk, ack, NOR_ACK, retrans_num);
1431 }
1432
1433 tp->sacked_out = 0;
1434 }
1435
update_nip_bw(u32 * bw,const struct tcp_sock * tp,u32 rtt_tstamp)1436 static void update_nip_bw(u32 *bw, const struct tcp_sock *tp, u32 rtt_tstamp)
1437 {
1438 if (tp->snd_nxt > tp->snd_una && rtt_tstamp > 0) {
1439 u32 bw_est = (tp->snd_nxt - tp->snd_una) * BW_MULTIPLY_FACTOR / rtt_tstamp;
1440
1441 if (*bw == 0)
1442 *bw = bw_est;
1443 else
1444 *bw = (BW_FACTOR_SEVEN_EIGHT * ((*bw) / BW_DIVISOR_FACTOR) +
1445 (bw_est / BW_DIVISOR_FACTOR));
1446 }
1447 }
1448
__tcp_nip_ack_calc_ssthresh_bw(struct tcp_nip_common * ntp,u32 rtt_tstamp,u32 icsk_rto,u32 ack)1449 static void __tcp_nip_ack_calc_ssthresh_bw(struct tcp_nip_common *ntp, u32 rtt_tstamp,
1450 u32 icsk_rto, u32 ack)
1451 {
1452 ssthresh_dbg("bw %u < %u , win %u to %u, rtt=%u rto=%u, ack=%u",
1453 ntp->nip_bw, get_nip_br_max_bw(), ntp->nip_ssthresh,
1454 get_ssthresh_low(), rtt_tstamp, icsk_rto, ack);
1455
1456 ntp->nip_ssthresh = get_ssthresh_low();
1457 }
1458
__tcp_nip_ack_calc_ssthresh_rto_up(struct tcp_nip_common * ntp,u32 rtt_tstamp,u32 icsk_rto,u32 ack,int icsk_rto_last)1459 static void __tcp_nip_ack_calc_ssthresh_rto_up(struct tcp_nip_common *ntp, u32 rtt_tstamp,
1460 u32 icsk_rto, u32 ack, int icsk_rto_last)
1461 {
1462 ssthresh_dbg("rtt %u >= %u, win %u to %u, rto %u to %u, ack=%u",
1463 rtt_tstamp, get_rtt_tstamp_rto_up(),
1464 ntp->nip_ssthresh, get_ssthresh_br_max(),
1465 icsk_rto_last, icsk_rto, ack);
1466
1467 ntp->nip_ssthresh = get_ssthresh_br_max();
1468 }
1469
__tcp_nip_ack_calc_ssthresh_rtt_high(struct tcp_nip_common * ntp,u32 rtt_tstamp,u32 ack)1470 static void __tcp_nip_ack_calc_ssthresh_rtt_high(struct tcp_nip_common *ntp, u32 rtt_tstamp,
1471 u32 ack)
1472 {
1473 ssthresh_dbg("rtt %u >= %u, win %u to %u, ack=%u",
1474 rtt_tstamp, get_rtt_tstamp_high(),
1475 ntp->nip_ssthresh, get_ssthresh_low(), ack);
1476
1477 ntp->nip_ssthresh = get_ssthresh_low();
1478 }
1479
__tcp_nip_ack_calc_ssthresh_rtt_mid_high(struct tcp_nip_common * ntp,u32 rtt_tstamp,u32 ack)1480 static void __tcp_nip_ack_calc_ssthresh_rtt_mid_high(struct tcp_nip_common *ntp, u32 rtt_tstamp,
1481 u32 ack)
1482 {
1483 ssthresh_dbg("rtt %u >= %u, win %u to %u, ack=%u",
1484 rtt_tstamp, get_rtt_tstamp_mid_high(),
1485 ntp->nip_ssthresh, get_ssthresh_mid_low(), ack);
1486
1487 ntp->nip_ssthresh = get_ssthresh_mid_low();
1488 }
1489
__tcp_nip_ack_calc_ssthresh_rtt_mid_low(struct tcp_nip_common * ntp,u32 rtt_tstamp,u32 ack)1490 static void __tcp_nip_ack_calc_ssthresh_rtt_mid_low(struct tcp_nip_common *ntp, u32 rtt_tstamp,
1491 u32 ack)
1492 {
1493 u32 rtt_tstamp_scale = get_rtt_tstamp_mid_high() - rtt_tstamp;
1494 int half_mid_high = get_ssthresh_mid_high() / HALF_DIVISOR_FACTOR;
1495 u32 nip_ssthresh;
1496
1497 nip_ssthresh = half_mid_high + rtt_tstamp_scale * half_mid_high /
1498 (get_rtt_tstamp_mid_high() - get_rtt_tstamp_mid_low());
1499
1500 ntp->nip_ssthresh = ntp->nip_ssthresh > get_ssthresh_mid_high() ?
1501 half_mid_high : ntp->nip_ssthresh;
1502 nip_ssthresh = (ntp->nip_ssthresh * get_ssthresh_high_step() +
1503 nip_ssthresh) / (get_ssthresh_high_step() + 1);
1504
1505 ssthresh_dbg("rtt %u >= %u, win %u to %u, ack=%u",
1506 rtt_tstamp, get_rtt_tstamp_mid_low(),
1507 ntp->nip_ssthresh, nip_ssthresh, ack);
1508
1509 ntp->nip_ssthresh = nip_ssthresh;
1510 }
1511
__tcp_nip_ack_calc_ssthresh_default(struct tcp_nip_common * ntp,u32 rtt_tstamp,u32 ack)1512 static void __tcp_nip_ack_calc_ssthresh_default(struct tcp_nip_common *ntp, u32 rtt_tstamp,
1513 u32 ack)
1514 {
1515 u32 nip_ssthresh;
1516
1517 nip_ssthresh = (ntp->nip_ssthresh * get_ssthresh_high_step() +
1518 get_ssthresh_high()) /
1519 (get_ssthresh_high_step() + 1);
1520
1521 ssthresh_dbg("rtt %u < %u, win %u to %u, ack=%u",
1522 rtt_tstamp, get_rtt_tstamp_mid_low(),
1523 ntp->nip_ssthresh, nip_ssthresh, ack);
1524
1525 ntp->nip_ssthresh = nip_ssthresh;
1526 }
1527
__tcp_nip_ack_calc_ssthresh(struct tcp_nip_common * ntp,u32 rtt_tstamp,u32 icsk_rto,u32 ack,int icsk_rto_last)1528 static void __tcp_nip_ack_calc_ssthresh(struct tcp_nip_common *ntp, u32 rtt_tstamp,
1529 u32 icsk_rto, u32 ack, int icsk_rto_last)
1530 {
1531 if (rtt_tstamp >= get_rtt_tstamp_rto_up())
1532 __tcp_nip_ack_calc_ssthresh_rto_up(ntp, rtt_tstamp, icsk_rto,
1533 ack, icsk_rto_last);
1534 else if (rtt_tstamp >= get_rtt_tstamp_high())
1535 __tcp_nip_ack_calc_ssthresh_rtt_high(ntp, rtt_tstamp, ack);
1536 else if (rtt_tstamp >= get_rtt_tstamp_mid_high())
1537 __tcp_nip_ack_calc_ssthresh_rtt_mid_high(ntp, rtt_tstamp, ack);
1538 else if (rtt_tstamp >= get_rtt_tstamp_mid_low())
1539 __tcp_nip_ack_calc_ssthresh_rtt_mid_low(ntp, rtt_tstamp, ack);
1540 else if (rtt_tstamp != 0)
1541 __tcp_nip_ack_calc_ssthresh_default(ntp, rtt_tstamp, ack);
1542 }
1543
tcp_nip_ack_calc_ssthresh(struct sock * sk,u32 ack,int icsk_rto_last,ktime_t skb_snd_tstamp)1544 static void tcp_nip_ack_calc_ssthresh(struct sock *sk, u32 ack, int icsk_rto_last,
1545 ktime_t skb_snd_tstamp)
1546 {
1547 struct tcp_sock *tp = tcp_sk(sk);
1548 struct tcp_nip_common *ntp = &tcp_nip_sk(sk)->common;
1549 struct inet_connection_sock *icsk = inet_csk(sk);
1550 int ack_reset = ack / get_nip_ssthresh_reset();
1551
1552 if (ntp->nip_ssthresh_reset != ack_reset) {
1553 ssthresh_dbg("ack reset win %u to %u, ack=%u",
1554 ntp->nip_ssthresh, get_ssthresh_low(), ack);
1555 ntp->nip_ssthresh_reset = ack_reset;
1556 ntp->nip_ssthresh = get_ssthresh_low();
1557 } else {
1558 if (skb_snd_tstamp) {
1559 u32 rtt_tstamp = tp->rcv_tstamp - skb_snd_tstamp;
1560
1561 update_nip_bw(&ntp->nip_bw, tp, rtt_tstamp);
1562 if (ntp->nip_bw < get_nip_br_max_bw())
1563 __tcp_nip_ack_calc_ssthresh_bw(ntp, rtt_tstamp, icsk->icsk_rto,
1564 ack);
1565 else
1566 __tcp_nip_ack_calc_ssthresh(ntp, rtt_tstamp, icsk->icsk_rto,
1567 ack, icsk_rto_last);
1568 }
1569 }
1570 }
1571
__tcp_nip_oow_rate_limited(struct net * net,int mib_idx,u32 * last_oow_ack_time)1572 static bool __tcp_nip_oow_rate_limited(struct net *net, int mib_idx, u32 *last_oow_ack_time)
1573 {
1574 if (*last_oow_ack_time) {
1575 s32 elapsed = (s32)(tcp_jiffies32 - *last_oow_ack_time);
1576
1577 if (elapsed >= 0 &&
1578 elapsed < READ_ONCE(net->ipv4.sysctl_tcp_invalid_ratelimit)) {
1579 NET_INC_STATS(net, mib_idx);
1580 return true; /* rate-limited: don't send yet! */
1581 }
1582 }
1583
1584 *last_oow_ack_time = tcp_jiffies32;
1585
1586 return false; /* not rate-limited: go ahead, send dupack now! */
1587 }
1588
tcp_nip_send_challenge_ack(struct sock * sk,const struct sk_buff * skb)1589 static void tcp_nip_send_challenge_ack(struct sock *sk, const struct sk_buff *skb)
1590 {
1591 /* unprotected vars, we dont care of overwrites */
1592 static u32 nip_challenge_timestamp;
1593 static unsigned int nip_challenge_count;
1594 struct tcp_sock *tp = tcp_sk(sk);
1595 struct net *net = sock_net(sk);
1596 u32 count, now;
1597
1598 /* First check our per-socket dupack rate limit. */
1599 if (__tcp_nip_oow_rate_limited(net,
1600 LINUX_MIB_TCPACKSKIPPEDCHALLENGE,
1601 &tp->last_oow_ack_time))
1602 return;
1603
1604 /* Then check host-wide RFC 5961 rate limit. */
1605 now = jiffies / HZ;
1606 if (now != READ_ONCE(nip_challenge_timestamp)) {
1607 u32 ack_limit = READ_ONCE(net->ipv4.sysctl_tcp_challenge_ack_limit);
1608 u32 half = (ack_limit + 1) >> 1;
1609
1610 WRITE_ONCE(nip_challenge_timestamp, now);
1611 WRITE_ONCE(nip_challenge_count, half + prandom_u32_max(ack_limit));
1612 }
1613 count = READ_ONCE(nip_challenge_count);
1614 if (count > 0) {
1615 WRITE_ONCE(nip_challenge_count, count - 1);
1616 NET_INC_STATS(net, LINUX_MIB_TCPCHALLENGEACK);
1617 tcp_nip_send_ack(sk);
1618 }
1619 }
1620
tcp_nip_ack(struct sock * sk,const struct sk_buff * skb,int flag)1621 static int tcp_nip_ack(struct sock *sk, const struct sk_buff *skb, int flag)
1622 {
1623 struct tcp_sock *tp = tcp_sk(sk);
1624 struct tcp_nip_common *ntp = &tcp_nip_sk(sk)->common;
1625 struct inet_connection_sock *icsk = inet_csk(sk);
1626 u32 prior_snd_una = tp->snd_una;
1627 u32 ack_seq = TCP_SKB_CB(skb)->seq;
1628 u32 ack = TCP_SKB_CB(skb)->ack_seq;
1629 int prior_packets = tp->packets_out;
1630 ktime_t skb_snd_tstamp = 0;
1631
1632 if (before(ack, prior_snd_una)) {
1633 if (before(ack, prior_snd_una - tp->max_window)) {
1634 if (!(flag & FLAG_NO_CHALLENGE_ACK))
1635 tcp_nip_send_challenge_ack(sk, skb);
1636 return -1;
1637 }
1638 return 0;
1639 }
1640
1641 if (after(ack, tp->snd_nxt))
1642 return -1;
1643
1644 tcp_nip_ack_update_window(sk, skb, ack, ack_seq);
1645 icsk->icsk_probes_out = 0; /* probe0 cnt */
1646 ntp->nip_keepalive_out = 0; /* keepalive cnt */
1647 tp->rcv_tstamp = tcp_jiffies32;
1648
1649 /* maybe zero window probe */
1650 if (!prior_packets) {
1651 nip_dbg("no unack pkt, seq=[%u-%u], rcv_nxt=%u, ack=%u",
1652 TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt, ack);
1653 if (tcp_nip_send_head(sk))
1654 tcp_nip_ack_probe(sk);
1655 return 1;
1656 }
1657
1658 if (after(ack, prior_snd_una)) {
1659 int icsk_rto_last;
1660
1661 icsk->icsk_retransmits = 0;
1662 tp->retrans_stamp = tcp_time_stamp(tp);
1663 tp->rcv_tstamp = tcp_jiffies32;
1664 tcp_nip_snd_una_update(tp, ack);
1665
1666 icsk_rto_last = icsk->icsk_rto;
1667 tcp_nip_clean_rtx_queue(sk, &skb_snd_tstamp);
1668
1669 tcp_nip_ack_calc_ssthresh(sk, ack, icsk_rto_last, skb_snd_tstamp);
1670 tcp_nip_nor_ack_retrans(sk, ack, get_ack_retrans_num());
1671 return 1;
1672 }
1673
1674 // dup ack: ack == tp->snd_una
1675 tcp_nip_dup_ack_retrans(sk, skb, ack, get_dup_ack_retrans_num());
1676
1677 return 1;
1678 }
1679
tcp_nip_sequence(const struct tcp_sock * tp,u32 seq,u32 end_seq)1680 static inline bool tcp_nip_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq)
1681 {
1682 /* False is returned if end_seq has been received,
1683 * or if SEq is not behind the receive window
1684 */
1685 return !before(end_seq, tp->rcv_wup) &&
1686 !after(seq, tp->rcv_nxt + tcp_receive_window(tp));
1687 }
1688
1689 /* When we get a reset we do this. */
tcp_nip_reset(struct sock * sk)1690 void tcp_nip_reset(struct sock *sk)
1691 {
1692 nip_dbg("handle rst");
1693
1694 /* We want the right error as BSD sees it (and indeed as we do). */
1695 switch (sk->sk_state) {
1696 case TCP_SYN_SENT:
1697 sk->sk_err = ECONNREFUSED;
1698 break;
1699 case TCP_CLOSE_WAIT:
1700 sk->sk_err = EPIPE;
1701 break;
1702 case TCP_CLOSE:
1703 return;
1704 default:
1705 sk->sk_err = ECONNRESET;
1706 }
1707 /* This barrier is coupled with smp_rmb() in tcp_poll() */
1708 smp_wmb();
1709
1710 tcp_nip_write_queue_purge(sk);
1711 tcp_nip_done(sk);
1712
1713 if (!sock_flag(sk, SOCK_DEAD))
1714 sk->sk_error_report(sk);
1715 }
1716
1717 /* Reack some incorrect packets, because if you do not ACK these packets,
1718 * they may be retransmitted frequently
1719 */
tcp_nip_send_dupack(struct sock * sk,const struct sk_buff * skb)1720 static void tcp_nip_send_dupack(struct sock *sk, const struct sk_buff *skb)
1721 {
1722 struct tcp_sock *tp = tcp_sk(sk);
1723
1724 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
1725 before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
1726 NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
1727
1728 nip_dbg("send dup ack");
1729 tcp_nip_send_ack(sk);
1730 }
1731
tcp_nip_reset_check(const struct sock * sk,const struct sk_buff * skb)1732 static bool tcp_nip_reset_check(const struct sock *sk, const struct sk_buff *skb)
1733 {
1734 struct tcp_sock *tp = tcp_sk(sk);
1735
1736 return unlikely(TCP_SKB_CB(skb)->seq == (tp->rcv_nxt - 1) &&
1737 (1 << sk->sk_state) & (TCPF_CLOSE_WAIT | TCPF_LAST_ACK |
1738 TCPF_CLOSING));
1739 }
1740
1741 /* This function is used to process the SYN received in RST packets
1742 * and illegal SEQ packets in ESTABLISHED state. Currently only seQ checks are included
1743 */
tcp_nip_validate_incoming(struct sock * sk,struct sk_buff * skb,const struct tcphdr * th,int syn_inerr)1744 static bool tcp_nip_validate_incoming(struct sock *sk, struct sk_buff *skb,
1745 const struct tcphdr *th, int syn_inerr)
1746 {
1747 struct tcp_sock *tp = tcp_sk(sk);
1748 bool rst_seq_match = false;
1749
1750 /* Step 1: check sequence number */
1751 /* 01.Check for unexpected packets. For some probe packets,
1752 * unexpected packets do not need to be processed, but reply for an ACK.
1753 * 02.Enter this branch when the receive window is 0
1754 */
1755 if (!tcp_nip_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {
1756 nip_dbg("receive unexpected pkt, drop it. seq=[%u-%u], rec_win=[%u-%u]",
1757 TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
1758 tp->rcv_wup, tp->rcv_nxt + tcp_receive_window(tp));
1759 if (!th->rst)
1760 tcp_nip_send_dupack(sk, skb);
1761 else if (tcp_nip_reset_check(sk, skb))
1762 tcp_nip_reset(sk);
1763 goto discard;
1764 }
1765
1766 /* Step 2: check RST bit */
1767 if (th->rst) {
1768 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt || tcp_nip_reset_check(sk, skb))
1769 rst_seq_match = true;
1770 if (rst_seq_match)
1771 tcp_nip_reset(sk);
1772 goto discard;
1773 }
1774
1775 return true;
1776
1777 discard:
1778 tcp_nip_drop(sk, skb);
1779 return false;
1780 }
1781
tcp_nip_rcv_established(struct sock * sk,struct sk_buff * skb,const struct tcphdr * th,unsigned int len)1782 void tcp_nip_rcv_established(struct sock *sk, struct sk_buff *skb,
1783 const struct tcphdr *th, unsigned int len)
1784 {
1785 struct tcp_sock *tp = tcp_sk(sk);
1786
1787 tcp_mstamp_refresh(tp);
1788 if (unlikely(!rcu_access_pointer(sk->sk_rx_dst)))
1789 inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
1790
1791 if (skb->len < (th->doff << 2))
1792 return;
1793
1794 if (!tcp_nip_validate_incoming(sk, skb, th, 1))
1795 return;
1796
1797 if (tcp_nip_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0)
1798 goto discard;
1799
1800 tcp_nip_data_queue(sk, skb);
1801 tcp_nip_data_snd_check(sk);
1802 tcp_nip_ack_snd_check(sk);
1803
1804 return;
1805
1806 discard:
1807 tcp_nip_drop(sk, skb);
1808 }
1809
tcp_default_init_rwnd(u32 mss)1810 static u32 tcp_default_init_rwnd(u32 mss)
1811 {
1812 u32 init_rwnd = TCP_INIT_CWND * TCP_NUM_2;
1813
1814 if (mss > TCP_MAX_MSS)
1815 init_rwnd = max((TCP_MAX_MSS * init_rwnd) / mss, (u32)TCP_NUM_2);
1816 return init_rwnd;
1817 }
1818
tcp_nip_fixup_rcvbuf(struct sock * sk)1819 static void tcp_nip_fixup_rcvbuf(struct sock *sk)
1820 {
1821 u32 mss = TCP_BASE_MSS;
1822 int rcvmem;
1823
1824 rcvmem = TCP_NUM_2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) *
1825 tcp_default_init_rwnd(mss);
1826
1827 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf)
1828 rcvmem <<= TCP_NIP_4BYTE_PAYLOAD;
1829
1830 if (sk->sk_rcvbuf < rcvmem)
1831 sk->sk_rcvbuf = min(rcvmem,
1832 sock_net(sk)->ipv4.sysctl_tcp_rmem[TCP_ARRAY_INDEX_2]);
1833 }
1834
1835 #define TCP_NIP_SND_BUF_SIZE 30720
tcp_nip_init_buffer_space(struct sock * sk)1836 void tcp_nip_init_buffer_space(struct sock *sk)
1837 {
1838 int tcp_app_win = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_app_win);
1839 struct tcp_sock *tp = tcp_sk(sk);
1840 int maxwin;
1841
1842 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
1843 tcp_nip_fixup_rcvbuf(sk);
1844
1845 tp->rcvq_space.space = tp->rcv_wnd;
1846 tcp_mstamp_refresh(tp);
1847 tp->rcvq_space.time = jiffies;
1848 tp->rcvq_space.seq = tp->copied_seq;
1849 maxwin = tcp_full_space(sk);
1850 if (tp->window_clamp >= maxwin) {
1851 tp->window_clamp = maxwin;
1852 if (tcp_app_win && maxwin > TCP_NUM_4 * tp->advmss)
1853 tp->window_clamp = max(maxwin -
1854 (maxwin >> tcp_app_win),
1855 TCP_NUM_4 * tp->advmss);
1856 }
1857 /* Force reservation of one segment. */
1858 if (tcp_app_win &&
1859 tp->window_clamp > TCP_NUM_2 * tp->advmss &&
1860 tp->window_clamp + tp->advmss > maxwin)
1861 tp->window_clamp = max(TCP_NUM_2 * tp->advmss, maxwin - tp->advmss);
1862 tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
1863 tp->snd_cwnd_stamp = tcp_jiffies32;
1864 }
1865
tcp_nip_finish_connect(struct sock * sk,struct sk_buff * skb)1866 void tcp_nip_finish_connect(struct sock *sk, struct sk_buff *skb)
1867 {
1868 struct tcp_sock *tp = tcp_sk(sk);
1869 struct inet_connection_sock *icsk = inet_csk(sk);
1870
1871 tcp_set_state(sk, TCP_ESTABLISHED);
1872 icsk->icsk_ack.lrcvtime = tcp_jiffies32;
1873 if (skb) {
1874 icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
1875 security_inet_conn_established(sk, skb);
1876 }
1877
1878 tp->lsndtime = tcp_jiffies32;
1879
1880 tcp_nip_init_buffer_space(sk);
1881 }
1882
1883 /* Function:
1884 * A function that handles the second handshake
1885 * Parameter:
1886 * sk: transmission control block
1887 * skb: Transfer control block buffer
1888 * Th: TCP header field
1889 */
tcp_nip_rcv_synsent_state_process(struct sock * sk,struct sk_buff * skb,const struct tcphdr * th)1890 static int tcp_nip_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
1891 const struct tcphdr *th)
1892 {
1893 struct inet_connection_sock *icsk = inet_csk(sk);
1894 struct tcp_sock *tp = tcp_sk(sk);
1895 int saved_clamp = tp->rx_opt.mss_clamp;
1896
1897 /* TCP Option Parsing */
1898 tcp_nip_parse_options(skb, &tp->rx_opt, 0, NULL);
1899 /* Rcv_tsecr saves the timestamp of the last TCP segment received from the peer end */
1900 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
1901 tp->rx_opt.rcv_tsecr -= tp->tsoffset;
1902
1903 if (th->ack) {
1904 /* Whether the ACK value is between the initial send sequence number
1905 * and the next sequence number
1906 */
1907 if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
1908 after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt))
1909 goto reset_and_undo;
1910 /* Must be within the corresponding time */
1911 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
1912 !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp, tcp_time_stamp(tp))) {
1913 NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSACTIVEREJECTED);
1914 goto reset_and_undo;
1915 }
1916
1917 if (th->rst) {
1918 tcp_nip_reset(sk);
1919 goto discard;
1920 }
1921
1922 if (!th->syn)
1923 goto discard_and_undo;
1924
1925 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
1926
1927 tcp_nip_ack(sk, skb, FLAG_SLOWPATH);
1928 tp->out_of_order_queue = RB_ROOT;
1929 /* The next data number expected to be accepted is +1 */
1930 tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
1931 /* Accept the left margin of the window +1 */
1932 tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
1933 tp->snd_wnd = ntohs(th->window);
1934
1935 if (get_wscale_enable()) {
1936 tp->rx_opt.wscale_ok = 1;
1937 tp->rx_opt.snd_wscale = get_wscale();
1938 tp->rx_opt.rcv_wscale = get_wscale();
1939 }
1940
1941 if (!tp->rx_opt.wscale_ok) {
1942 tp->rx_opt.snd_wscale = 0;
1943 tp->rx_opt.rcv_wscale = 0;
1944 tp->window_clamp = min(tp->window_clamp, TCP_NIP_WINDOW_MAX);
1945 }
1946
1947 if (tp->rx_opt.saw_tstamp) {
1948 tp->rx_opt.tstamp_ok = 1;
1949 tp->tcp_header_len =
1950 sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
1951 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
1952 tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
1953 tp->rx_opt.ts_recent_stamp = get_seconds();
1954 } else {
1955 tp->tcp_header_len = sizeof(struct tcphdr);
1956 }
1957
1958 tp->copied_seq = tp->rcv_nxt;
1959 /* Invoke memory barrier (annotated prior to checkpatch requirements) */
1960 smp_mb();
1961
1962 tcp_nip_sync_mss(sk, icsk->icsk_pmtu_cookie);
1963 tcp_nip_initialize_rcv_mss(sk);
1964
1965 tcp_nip_finish_connect(sk, skb);
1966 /* Wake up the process */
1967 if (!sock_flag(sk, SOCK_DEAD)) {
1968 sk->sk_state_change(sk);
1969 rcu_read_lock();
1970 sock_wake_async(rcu_dereference(sk->sk_wq), SOCK_WAKE_IO, POLL_OUT);
1971 rcu_read_unlock();
1972 }
1973
1974 tcp_nip_send_ack(sk);
1975 return -1;
1976 discard:
1977 tcp_nip_drop(sk, skb);
1978 return 0;
1979 }
1980
1981 discard_and_undo:
1982 tcp_clear_options(&tp->rx_opt);
1983 tp->rx_opt.mss_clamp = saved_clamp;
1984 goto discard;
1985
1986 reset_and_undo:
1987 tcp_clear_options(&tp->rx_opt);
1988 tp->rx_opt.mss_clamp = saved_clamp;
1989 return 1;
1990 }
1991
1992 /* Function:
1993 * TCP processing function that is differentiated according to
1994 * different states after receiving data packets
1995 * Parameter:
1996 * sk: transmission control block
1997 * skb: Transfer control block buffer
1998 * Note: Currently this function only has code for handling the first handshake packet
1999 * Implementation of the third handshake ACK to handle the code
2000 */
tcp_nip_rcv_state_process(struct sock * sk,struct sk_buff * skb)2001 int tcp_nip_rcv_state_process(struct sock *sk, struct sk_buff *skb)
2002 {
2003 struct tcp_sock *tp = tcp_sk(sk);
2004 struct inet_connection_sock *icsk = inet_csk(sk);
2005 const struct tcphdr *th = tcp_hdr(skb);
2006 int queued = 0;
2007 bool acceptable;
2008
2009 /* Step 1: Connect handshake packet processing */
2010 switch (sk->sk_state) {
2011 case TCP_CLOSE:
2012 goto discard;
2013
2014 case TCP_LISTEN:
2015 if (th->ack)
2016 return 1;
2017
2018 if (th->rst)
2019 goto discard;
2020
2021 if (th->syn) {
2022 if (th->fin)
2023 goto discard;
2024
2025 rcu_read_lock();
2026 local_bh_disable();
2027 acceptable = icsk->icsk_af_ops->conn_request(sk, skb) >= 0;
2028 local_bh_enable();
2029 rcu_read_unlock();
2030
2031 if (!acceptable)
2032 return 1;
2033 consume_skb(skb);
2034 return 0;
2035 }
2036 goto discard;
2037 case TCP_SYN_SENT:
2038 nip_dbg("TCP_SYN_SENT");
2039 tp->rx_opt.saw_tstamp = 0;
2040 tcp_mstamp_refresh(tp);
2041 queued = tcp_nip_rcv_synsent_state_process(sk, skb, th);
2042 if (queued >= 0)
2043 return queued;
2044 __kfree_skb(skb);
2045 return 0;
2046 }
2047 tcp_mstamp_refresh(tp);
2048 tp->rx_opt.saw_tstamp = 0;
2049
2050 if (!th->ack && !th->rst && !th->syn)
2051 goto discard;
2052
2053 if (!tcp_nip_validate_incoming(sk, skb, th, 0))
2054 return 0;
2055
2056 acceptable = tcp_nip_ack(sk, skb, FLAG_SLOWPATH |
2057 FLAG_UPDATE_TS_RECENT |
2058 FLAG_NO_CHALLENGE_ACK) > 0;
2059 /* If the third handshake ACK is invalid, 1 is returned
2060 * and the SKB is discarded in tcp_nip_rcv
2061 */
2062 if (!acceptable) {
2063 if (sk->sk_state == TCP_SYN_RECV)
2064 return 1;
2065 goto discard;
2066 }
2067
2068 switch (sk->sk_state) {
2069 case TCP_SYN_RECV:
2070 tp->copied_seq = tp->rcv_nxt;
2071 tcp_nip_init_buffer_space(sk);
2072 /* Invoke memory barrier (annotated prior to checkpatch requirements) */
2073 smp_mb();
2074 tcp_set_state(sk, TCP_ESTABLISHED);
2075 nip_dbg("TCP_ESTABLISHED");
2076 sk->sk_state_change(sk);
2077
2078 /* Sets the part to be sent, and the size of the send window */
2079 tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
2080 tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
2081 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
2082
2083 tp->lsndtime = tcp_jiffies32;
2084
2085 tcp_initialize_rcv_mss(sk);
2086 break;
2087 case TCP_FIN_WAIT1: {
2088 if (tp->snd_una != tp->write_seq) {
2089 nip_dbg("tp->snd_una != tp->write_seq");
2090 break;
2091 }
2092
2093 tcp_set_state(sk, TCP_FIN_WAIT2);
2094 sk->sk_shutdown |= SEND_SHUTDOWN;
2095
2096 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
2097 after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
2098 tcp_nip_done(sk);
2099 nip_dbg("received payload packets, call tcp_nip_done");
2100 return 1;
2101 }
2102
2103 nip_dbg("TCP_FIN_WAIT1: recvd ack for fin.Wait for fin from other side");
2104 inet_csk_reset_keepalive_timer(sk, TCP_NIP_CSK_KEEPALIVE_CYCLE * HZ);
2105
2106 break;
2107 }
2108
2109 case TCP_CLOSING:
2110 if (tp->snd_una == tp->write_seq) {
2111 nip_dbg("TCP_CLOSING: recvd ack for fin.Ready to destroy");
2112 inet_csk_reset_keepalive_timer(sk, TCP_TIMEWAIT_LEN);
2113 goto discard;
2114 }
2115 break;
2116 case TCP_LAST_ACK:
2117 nip_dbg("tcp_nip_rcv_state_process_2: TCP_LAST_ACK");
2118 if (tp->snd_una == tp->write_seq) {
2119 nip_dbg("LAST_ACK: recvd ack for fin.Directly destroy");
2120 tcp_nip_done(sk);
2121 goto discard;
2122 }
2123 break;
2124 }
2125
2126 switch (sk->sk_state) {
2127 case TCP_CLOSE_WAIT:
2128 nip_dbg("into TCP_CLOSE_WAIT, rst = %d, seq = %u, end_seq = %u, rcv_nxt = %u",
2129 th->rst, TCP_SKB_CB(skb)->seq,
2130 TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
2131 fallthrough;
2132 case TCP_CLOSING:
2133 case TCP_LAST_ACK:
2134 if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
2135 nip_dbg("break in TCP_LAST_ACK");
2136 break;
2137 }
2138 nip_dbg("tcp_nip_rcv_state_process_3: TCP_LAST_ACK_2");
2139 fallthrough;
2140 case TCP_FIN_WAIT1:
2141 case TCP_FIN_WAIT2:
2142 /* Reset is required according to RFC 1122.
2143 * Do not enter the reset process temporarily
2144 */
2145 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2146 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
2147 after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
2148 tcp_nip_reset(sk);
2149 nip_dbg("call tcp_nip_reset");
2150 return 1;
2151 }
2152 }
2153 fallthrough;
2154 case TCP_ESTABLISHED:
2155 tcp_nip_data_queue(sk, skb);
2156 queued = 1;
2157 break;
2158 }
2159
2160 if (sk->sk_state != TCP_CLOSE) {
2161 tcp_nip_data_snd_check(sk);
2162 tcp_nip_ack_snd_check(sk);
2163 }
2164
2165 if (!queued) {
2166 discard:
2167 tcp_nip_drop(sk, skb);
2168 }
2169 return 0;
2170 }
2171
2172 /* Function
2173 * Initialize RCV_MSS
2174 * Parameter
2175 * sk: transmission control block
2176 */
tcp_nip_initialize_rcv_mss(struct sock * sk)2177 void tcp_nip_initialize_rcv_mss(struct sock *sk)
2178 {
2179 const struct tcp_sock *tp = tcp_sk(sk);
2180 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
2181
2182 hint = min(hint, tp->rcv_wnd / TCP_NUM_2);
2183 hint = min(hint, TCP_MSS_DEFAULT);
2184 hint = max(hint, TCP_MIN_MSS);
2185
2186 inet_csk(sk)->icsk_ack.rcv_mss = hint;
2187 }
2188
2189 /* Function
2190 * Handle the third handshake ACK and return the new control block successfully.
2191 * Is the core process for handling ACKS.
2192 * (1)Create a child control block. Note that the state of the child control
2193 * block is TCP_SYN_RECV
2194 * This is different from the TCP_NEW_SYN_RECV control block created when syn was received.
2195 * (2)Remove the request control block from the incomplete connection queue
2196 * and add it to the completed connection queue
2197 * Parameter
2198 * sk: transmission control block
2199 * skb: Transfer control block buffer
2200 * req: Request connection control block
2201 */
tcp_nip_check_req(struct sock * sk,struct sk_buff * skb,struct request_sock * req)2202 struct sock *tcp_nip_check_req(struct sock *sk, struct sk_buff *skb,
2203 struct request_sock *req)
2204 {
2205 struct tcp_options_received tmp_opt;
2206 struct sock *child;
2207 const struct tcphdr *th = tcp_hdr(skb);
2208 __be32 flg = tcp_flag_word(th) & (TCP_FLAG_RST | TCP_FLAG_SYN | TCP_FLAG_ACK);
2209 bool own_req;
2210
2211 tmp_opt.saw_tstamp = 0;
2212 /* Check whether the TCP option exists */
2213 if (th->doff > (sizeof(struct tcphdr) >> TCP_NIP_4BYTE_PAYLOAD))
2214 /* Parsing TCP options */
2215 tcp_nip_parse_options(skb, &tmp_opt, 0, NULL);
2216
2217 /* ACK but the serial number does not match,
2218 * return to the original control block, no processing outside
2219 */
2220 if ((flg & TCP_FLAG_ACK) &&
2221 (TCP_SKB_CB(skb)->ack_seq !=
2222 tcp_rsk(req)->snt_isn + 1)) {
2223 nip_dbg("ack_seq is wrong");
2224 return sk;
2225 }
2226
2227 /* The above process guarantees that there is an ACK, if not, return directly */
2228 if (!(flg & TCP_FLAG_ACK)) {
2229 nip_dbg("No TCP_FLAG_ACK");
2230 return NULL;
2231 }
2232
2233 /* The ack is valid and the child control block is created.
2234 * Note that the state of the child control block is TCP_SYN_RECV
2235 */
2236 child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL, req, &own_req);
2237 if (!child) {
2238 nip_dbg("No listen_overflow");
2239 goto listen_overflow;
2240 }
2241 nip_dbg("creat child sock successfully");
2242
2243 sock_rps_save_rxhash(child, skb);
2244 /* Calculate the time spent synack-ack in three handshakes */
2245 tcp_synack_rtt_meas(child, req);
2246 /* Delete the original control block from the incomplete queue
2247 * and add it to the completed queue
2248 */
2249 return inet_csk_complete_hashdance(sk, child, req, own_req);
2250
2251 listen_overflow:
2252 if (!sock_net(sk)->ipv4.sysctl_tcp_abort_on_overflow) {
2253 inet_rsk(req)->acked = 1;
2254 return NULL;
2255 }
2256 return NULL;
2257 }
2258
2259