1 /*-
2 * Copyright (c) 2016-2020 Netflix, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 */
26 /*
27 * Author: Randall Stewart <rrs@netflix.com>
28 * This work is based on the ACM Queue paper
29 * BBR - Congestion Based Congestion Control
30 * and also numerous discussions with Neal, Yuchung and Van.
31 */
32
33 #include <sys/cdefs.h>
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
37 #include "opt_tcpdebug.h"
38 #include "opt_ratelimit.h"
39 #include "opt_kern_tls.h"
40 #include <sys/param.h>
41 #include <sys/arb.h>
42 #include <sys/module.h>
43 #include <sys/kernel.h>
44 #ifdef TCP_HHOOK
45 #include <sys/hhook.h>
46 #endif
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/proc.h>
50 #include <sys/qmath.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #ifdef KERN_TLS
54 #include <sys/ktls.h>
55 #endif
56 #include <sys/sysctl.h>
57 #include <sys/systm.h>
58 #include <sys/tree.h>
59 #ifdef NETFLIX_STATS
60 #include <sys/stats.h> /* Must come after qmath.h and tree.h */
61 #endif
62 #include <sys/refcount.h>
63 #include <sys/queue.h>
64 #include <sys/smp.h>
65 #include <sys/kthread.h>
66 #include <sys/lock.h>
67 #include <sys/mutex.h>
68 #include <sys/tim_filter.h>
69 #include <sys/time.h>
70 #include <vm/uma.h>
71 #include <sys/kern_prefetch.h>
72
73 #include <net/route.h>
74 #include <net/vnet.h>
75 #include <net/ethernet.h>
76 #include <net/bpf.h>
77
78 #define TCPSTATES /* for logging */
79
80 #include <netinet/in.h>
81 #include <netinet/in_kdtrace.h>
82 #include <netinet/in_pcb.h>
83 #include <netinet/ip.h>
84 #include <netinet/ip_icmp.h> /* required for icmp_var.h */
85 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
86 #include <netinet/ip_var.h>
87 #include <netinet/ip6.h>
88 #include <netinet6/in6_pcb.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet/tcp.h>
91 #include <netinet/tcp_fsm.h>
92 #include <netinet/tcp_seq.h>
93 #include <netinet/tcp_timer.h>
94 #include <netinet/tcp_var.h>
95 #include <netinet/tcpip.h>
96 #include <netinet/tcp_hpts.h>
97 #include <netinet/tcp_lro.h>
98 #include <netinet/cc/cc.h>
99 #include <netinet/tcp_log_buf.h>
100 #ifdef TCPDEBUG
101 #include <netinet/tcp_debug.h>
102 #endif /* TCPDEBUG */
103 #ifdef TCP_OFFLOAD
104 #include <netinet/tcp_offload.h>
105 #endif
106 #ifdef INET6
107 #include <netinet6/tcp6_var.h>
108 #endif
109 #include <netinet/tcp_fastopen.h>
110
111 #include <netipsec/ipsec_support.h>
112 #include <net/if.h>
113 #include <net/if_var.h>
114
115 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
116 #include <netipsec/ipsec.h>
117 #include <netipsec/ipsec6.h>
118 #endif /* IPSEC */
119
120 #include <netinet/udp.h>
121 #include <netinet/udp_var.h>
122 #include <machine/in_cksum.h>
123
124 #ifdef MAC
125 #include <security/mac/mac_framework.h>
126 #endif
127 #include "rack_bbr_common.h"
128
129 /*
130 * Common TCP Functions - These are shared by borth
131 * rack and BBR.
132 */
133 #ifdef KERN_TLS
134 uint32_t
ctf_get_opt_tls_size(struct socket * so,uint32_t rwnd)135 ctf_get_opt_tls_size(struct socket *so, uint32_t rwnd)
136 {
137 struct ktls_session *tls;
138 uint32_t len;
139
140 again:
141 tls = so->so_snd.sb_tls_info;
142 len = tls->params.max_frame_len; /* max tls payload */
143 len += tls->params.tls_hlen; /* tls header len */
144 len += tls->params.tls_tlen; /* tls trailer len */
145 if ((len * 4) > rwnd) {
146 /*
147 * Stroke this will suck counter and what
148 * else should we do Drew? From the
149 * TCP perspective I am not sure
150 * what should be done...
151 */
152 if (tls->params.max_frame_len > 4096) {
153 tls->params.max_frame_len -= 4096;
154 if (tls->params.max_frame_len < 4096)
155 tls->params.max_frame_len = 4096;
156 goto again;
157 }
158 }
159 return (len);
160 }
161 #endif
162
163 static int
ctf_get_enet_type(struct ifnet * ifp,struct mbuf * m)164 ctf_get_enet_type(struct ifnet *ifp, struct mbuf *m)
165 {
166 struct ether_header *eh;
167 #ifdef INET6
168 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
169 #endif
170 #ifdef INET
171 struct ip *ip = NULL; /* Keep compiler happy. */
172 #endif
173 #if defined(INET) || defined(INET6)
174 struct tcphdr *th;
175 int32_t tlen;
176 uint16_t drop_hdrlen;
177 #endif
178 uint16_t etype;
179 #ifdef INET
180 uint8_t iptos;
181 #endif
182
183 /* Is it the easy way? */
184 if (m->m_flags & M_LRO_EHDRSTRP)
185 return (m->m_pkthdr.lro_etype);
186 /*
187 * Ok this is the old style call, the ethernet header is here.
188 * This also means no checksum or BPF were done. This
189 * can happen if the race to setup the inp fails and
190 * LRO sees no INP at packet input, but by the time
191 * we queue the packets an INP gets there. Its rare
192 * but it can occur so we will handle it. Note that
193 * this means duplicated work but with the rarity of it
194 * its not worth worrying about.
195 */
196 /* Let the BPF see the packet */
197 if (bpf_peers_present(ifp->if_bpf))
198 ETHER_BPF_MTAP(ifp, m);
199 /* Now the csum */
200 eh = mtod(m, struct ether_header *);
201 etype = ntohs(eh->ether_type);
202 m_adj(m, sizeof(*eh));
203 switch (etype) {
204 #ifdef INET6
205 case ETHERTYPE_IPV6:
206 {
207 if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
208 m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
209 if (m == NULL) {
210 KMOD_TCPSTAT_INC(tcps_rcvshort);
211 m_freem(m);
212 return (-1);
213 }
214 }
215 ip6 = (struct ip6_hdr *)(eh + 1);
216 th = (struct tcphdr *)(ip6 + 1);
217 drop_hdrlen = sizeof(*ip6);
218 tlen = ntohs(ip6->ip6_plen);
219 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
220 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
221 th->th_sum = m->m_pkthdr.csum_data;
222 else
223 th->th_sum = in6_cksum_pseudo(ip6, tlen,
224 IPPROTO_TCP,
225 m->m_pkthdr.csum_data);
226 th->th_sum ^= 0xffff;
227 } else
228 th->th_sum = in6_cksum(m, IPPROTO_TCP, drop_hdrlen, tlen);
229 if (th->th_sum) {
230 KMOD_TCPSTAT_INC(tcps_rcvbadsum);
231 m_freem(m);
232 return (-1);
233 }
234 return (etype);
235 }
236 #endif
237 #ifdef INET
238 case ETHERTYPE_IP:
239 {
240 if (m->m_len < sizeof (struct tcpiphdr)) {
241 m = m_pullup(m, sizeof (struct tcpiphdr));
242 if (m == NULL) {
243 KMOD_TCPSTAT_INC(tcps_rcvshort);
244 m_freem(m);
245 return (-1);
246 }
247 }
248 ip = (struct ip *)(eh + 1);
249 th = (struct tcphdr *)(ip + 1);
250 drop_hdrlen = sizeof(*ip);
251 iptos = ip->ip_tos;
252 tlen = ntohs(ip->ip_len) - sizeof(struct ip);
253 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
254 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
255 th->th_sum = m->m_pkthdr.csum_data;
256 else
257 th->th_sum = in_pseudo(ip->ip_src.s_addr,
258 ip->ip_dst.s_addr,
259 htonl(m->m_pkthdr.csum_data + tlen + IPPROTO_TCP));
260 th->th_sum ^= 0xffff;
261 } else {
262 int len;
263 struct ipovly *ipov = (struct ipovly *)ip;
264 /*
265 * Checksum extended TCP header and data.
266 */
267 len = drop_hdrlen + tlen;
268 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
269 ipov->ih_len = htons(tlen);
270 th->th_sum = in_cksum(m, len);
271 /* Reset length for SDT probes. */
272 ip->ip_len = htons(len);
273 /* Reset TOS bits */
274 ip->ip_tos = iptos;
275 /* Re-initialization for later version check */
276 ip->ip_v = IPVERSION;
277 ip->ip_hl = sizeof(*ip) >> 2;
278 }
279 if (th->th_sum) {
280 KMOD_TCPSTAT_INC(tcps_rcvbadsum);
281 m_freem(m);
282 return (-1);
283 }
284 break;
285 }
286 #endif
287 };
288 return (etype);
289 }
290
291 /*
292 * The function ctf_process_inbound_raw() is used by
293 * transport developers to do the steps needed to
294 * support MBUF Queuing i.e. the flags in
295 * inp->inp_flags2:
296 *
297 * - INP_SUPPORTS_MBUFQ
298 * - INP_MBUF_QUEUE_READY
299 * - INP_DONT_SACK_QUEUE
300 * - INP_MBUF_ACKCMP
301 *
302 * These flags help control how LRO will deliver
303 * packets to the transport. You first set in inp_flags2
304 * the INP_SUPPORTS_MBUFQ to tell the LRO code that you
305 * will gladly take a queue of packets instead of a compressed
306 * single packet. You also set in your t_fb pointer the
307 * tfb_do_queued_segments to point to ctf_process_inbound_raw.
308 *
309 * This then gets you lists of inbound ACK's/Data instead
310 * of a condensed compressed ACK/DATA packet. Why would you
311 * want that? This will get you access to all the arrival
312 * times of at least LRO and possibly at the Hardware (if
313 * the interface card supports that) of the actual ACK/DATA.
314 * In some transport designs this is important since knowing
315 * the actual time we got the packet is useful information.
316 *
317 * A new special type of mbuf may also be supported by the transport
318 * if it has set the INP_MBUF_ACKCMP flag. If its set, LRO will
319 * possibly create a M_ACKCMP type mbuf. This is a mbuf with
320 * an array of "acks". One thing also to note is that when this
321 * occurs a subsequent LRO may find at the back of the untouched
322 * mbuf queue chain a M_ACKCMP and append on to it. This means
323 * that until the transport pulls in the mbuf chain queued
324 * for it more ack's may get on the mbufs that were already
325 * delivered. There currently is a limit of 6 acks condensed
326 * into 1 mbuf which means often when this is occuring, we
327 * don't get that effect but it does happen.
328 *
329 * Now there are some interesting Caveats that the transport
330 * designer needs to take into account when using this feature.
331 *
332 * 1) It is used with HPTS and pacing, when the pacing timer
333 * for output calls it will first call the input.
334 * 2) When you set INP_MBUF_QUEUE_READY this tells LRO
335 * queue normal packets, I am busy pacing out data and
336 * will process the queued packets before my tfb_tcp_output
337 * call from pacing. If a non-normal packet arrives, (e.g. sack)
338 * you will be awoken immediately.
339 * 3) Finally you can add the INP_DONT_SACK_QUEUE to not even
340 * be awoken if a SACK has arrived. You would do this when
341 * you were not only running a pacing for output timer
342 * but a Rack timer as well i.e. you know you are in recovery
343 * and are in the process (via the timers) of dealing with
344 * the loss.
345 *
346 * Now a critical thing you must be aware of here is that the
347 * use of the flags has a far greater scope then just your
348 * typical LRO. Why? Well thats because in the normal compressed
349 * LRO case at the end of a driver interupt all packets are going
350 * to get presented to the transport no matter if there is one
351 * or 100. With the MBUF_QUEUE model, this is not true. You will
352 * only be awoken to process the queue of packets when:
353 * a) The flags discussed above allow it.
354 * <or>
355 * b) You exceed a ack or data limit (by default the
356 * ack limit is infinity (64k acks) and the data
357 * limit is 64k of new TCP data)
358 * <or>
359 * c) The push bit has been set by the peer
360 */
361
362 int
ctf_process_inbound_raw(struct tcpcb * tp,struct socket * so,struct mbuf * m,int has_pkt)363 ctf_process_inbound_raw(struct tcpcb *tp, struct socket *so, struct mbuf *m, int has_pkt)
364 {
365 /*
366 * We are passed a raw change of mbuf packets
367 * that arrived in LRO. They are linked via
368 * the m_nextpkt link in the pkt-headers.
369 *
370 * We process each one by:
371 * a) saving off the next
372 * b) stripping off the ether-header
373 * c) formulating the arguments for
374 * the tfb_tcp_hpts_do_segment
375 * d) calling each mbuf to tfb_tcp_hpts_do_segment
376 * after adjusting the time to match the arrival time.
377 * Note that the LRO code assures no IP options are present.
378 *
379 * The symantics for calling tfb_tcp_hpts_do_segment are the
380 * following:
381 * 1) It returns 0 if all went well and you (the caller) need
382 * to release the lock.
383 * 2) If nxt_pkt is set, then the function will surpress calls
384 * to tfb_tcp_output() since you are promising to call again
385 * with another packet.
386 * 3) If it returns 1, then you must free all the packets being
387 * shipped in, the tcb has been destroyed (or about to be destroyed).
388 */
389 struct mbuf *m_save;
390 struct tcphdr *th;
391 #ifdef INET6
392 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
393 #endif
394 #ifdef INET
395 struct ip *ip = NULL; /* Keep compiler happy. */
396 #endif
397 struct ifnet *ifp;
398 struct timeval tv;
399 struct inpcb *inp;
400 int32_t retval, nxt_pkt, tlen, off;
401 int etype = 0;
402 uint16_t drop_hdrlen;
403 uint8_t iptos;
404
405 NET_EPOCH_ASSERT();
406 KASSERT(m != NULL, ("ctf_process_inbound_raw: m == NULL"));
407 ifp = m_rcvif(m);
408 KASSERT(ifp != NULL, ("ctf_process_inbound_raw: ifp == NULL"));
409 CURVNET_SET(ifp->if_vnet);
410 tcp_get_usecs(&tv);
411 while (m) {
412 m_save = m->m_nextpkt;
413 m->m_nextpkt = NULL;
414 if ((m->m_flags & M_ACKCMP) == 0) {
415 /* Now lets get the ether header */
416 etype = ctf_get_enet_type(ifp, m);
417 if (etype == -1) {
418 /* Skip this packet it was freed by checksum */
419 goto skipped_pkt;
420 }
421 KASSERT(((etype == ETHERTYPE_IPV6) || (etype == ETHERTYPE_IP)),
422 ("tp:%p m:%p etype:0x%x -- not IP or IPv6", tp, m, etype));
423 /* Trim off the ethernet header */
424 switch (etype) {
425 #ifdef INET6
426 case ETHERTYPE_IPV6:
427 ip6 = mtod(m, struct ip6_hdr *);
428 th = (struct tcphdr *)(ip6 + 1);
429 tlen = ntohs(ip6->ip6_plen);
430 drop_hdrlen = sizeof(*ip6);
431 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
432 break;
433 #endif
434 #ifdef INET
435 case ETHERTYPE_IP:
436 ip = mtod(m, struct ip *);
437 th = (struct tcphdr *)(ip + 1);
438 drop_hdrlen = sizeof(*ip);
439 iptos = ip->ip_tos;
440 tlen = ntohs(ip->ip_len) - sizeof(struct ip);
441 break;
442 #endif
443 } /* end switch */
444 /*
445 * Convert TCP protocol specific fields to host format.
446 */
447 tcp_fields_to_host(th);
448 off = th->th_off << 2;
449 if (off < sizeof (struct tcphdr) || off > tlen) {
450 printf("off:%d < hdrlen:%zu || > tlen:%u -- dump\n",
451 off,
452 sizeof(struct tcphdr),
453 tlen);
454 KMOD_TCPSTAT_INC(tcps_rcvbadoff);
455 m_freem(m);
456 goto skipped_pkt;
457 }
458 tlen -= off;
459 drop_hdrlen += off;
460 /*
461 * Now lets setup the timeval to be when we should
462 * have been called (if we can).
463 */
464 m->m_pkthdr.lro_nsegs = 1;
465 /* Now what about next packet? */
466 } else {
467 /*
468 * This mbuf is an array of acks that have
469 * been compressed. We assert the inp has
470 * the flag set to enable this!
471 */
472 KASSERT((tp->t_inpcb->inp_flags2 & INP_MBUF_ACKCMP),
473 ("tp:%p inp:%p no INP_MBUF_ACKCMP flags?", tp, tp->t_inpcb));
474 tlen = 0;
475 drop_hdrlen = 0;
476 th = NULL;
477 iptos = 0;
478 }
479 tcp_get_usecs(&tv);
480 if (m_save || has_pkt)
481 nxt_pkt = 1;
482 else
483 nxt_pkt = 0;
484 if ((m->m_flags & M_ACKCMP) == 0)
485 KMOD_TCPSTAT_INC(tcps_rcvtotal);
486 else
487 KMOD_TCPSTAT_ADD(tcps_rcvtotal, (m->m_len / sizeof(struct tcp_ackent)));
488 inp = tp->t_inpcb;
489 INP_WLOCK_ASSERT(inp);
490 retval = (*tp->t_fb->tfb_do_segment_nounlock)(m, th, so, tp, drop_hdrlen, tlen,
491 iptos, nxt_pkt, &tv);
492 if (retval) {
493 /* We lost the lock and tcb probably */
494 m = m_save;
495 while(m) {
496 m_save = m->m_nextpkt;
497 m->m_nextpkt = NULL;
498 m_freem(m);
499 m = m_save;
500 }
501 CURVNET_RESTORE();
502 INP_UNLOCK_ASSERT(inp);
503 return (retval);
504 }
505 skipped_pkt:
506 m = m_save;
507 }
508 CURVNET_RESTORE();
509 return (0);
510 }
511
512 int
ctf_do_queued_segments(struct socket * so,struct tcpcb * tp,int have_pkt)513 ctf_do_queued_segments(struct socket *so, struct tcpcb *tp, int have_pkt)
514 {
515 struct mbuf *m;
516
517 /* First lets see if we have old packets */
518 if (tp->t_in_pkt) {
519 m = tp->t_in_pkt;
520 tp->t_in_pkt = NULL;
521 tp->t_tail_pkt = NULL;
522 if (ctf_process_inbound_raw(tp, so, m, have_pkt)) {
523 /* We lost the tcpcb (maybe a RST came in)? */
524 return(1);
525 }
526 }
527 return (0);
528 }
529
530 uint32_t
ctf_outstanding(struct tcpcb * tp)531 ctf_outstanding(struct tcpcb *tp)
532 {
533 uint32_t bytes_out;
534
535 bytes_out = tp->snd_max - tp->snd_una;
536 if (tp->t_state < TCPS_ESTABLISHED)
537 bytes_out++;
538 if (tp->t_flags & TF_SENTFIN)
539 bytes_out++;
540 return (bytes_out);
541 }
542
543 uint32_t
ctf_flight_size(struct tcpcb * tp,uint32_t rc_sacked)544 ctf_flight_size(struct tcpcb *tp, uint32_t rc_sacked)
545 {
546 if (rc_sacked <= ctf_outstanding(tp))
547 return(ctf_outstanding(tp) - rc_sacked);
548 else {
549 return (0);
550 }
551 }
552
553 void
ctf_do_dropwithreset(struct mbuf * m,struct tcpcb * tp,struct tcphdr * th,int32_t rstreason,int32_t tlen)554 ctf_do_dropwithreset(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th,
555 int32_t rstreason, int32_t tlen)
556 {
557 if (tp != NULL) {
558 tcp_dropwithreset(m, th, tp, tlen, rstreason);
559 INP_WUNLOCK(tp->t_inpcb);
560 } else
561 tcp_dropwithreset(m, th, NULL, tlen, rstreason);
562 }
563
564 void
ctf_ack_war_checks(struct tcpcb * tp,uint32_t * ts,uint32_t * cnt)565 ctf_ack_war_checks(struct tcpcb *tp, uint32_t *ts, uint32_t *cnt)
566 {
567 if ((ts != NULL) && (cnt != NULL) &&
568 (tcp_ack_war_time_window > 0) &&
569 (tcp_ack_war_cnt > 0)) {
570 /* We are possibly doing ack war prevention */
571 uint32_t cts;
572
573 /*
574 * We use a msec tick here which gives us
575 * roughly 49 days. We don't need the
576 * precision of a microsecond timestamp which
577 * would only give us hours.
578 */
579 cts = tcp_ts_getticks();
580 if (TSTMP_LT((*ts), cts)) {
581 /* Timestamp is in the past */
582 *cnt = 0;
583 *ts = (cts + tcp_ack_war_time_window);
584 }
585 if (*cnt < tcp_ack_war_cnt) {
586 *cnt = (*cnt + 1);
587 tp->t_flags |= TF_ACKNOW;
588 } else
589 tp->t_flags &= ~TF_ACKNOW;
590 } else
591 tp->t_flags |= TF_ACKNOW;
592 }
593
594 /*
595 * ctf_drop_checks returns 1 for you should not proceed. It places
596 * in ret_val what should be returned 1/0 by the caller. The 1 indicates
597 * that the TCB is unlocked and probably dropped. The 0 indicates the
598 * TCB is still valid and locked.
599 */
600 int
_ctf_drop_checks(struct tcpopt * to,struct mbuf * m,struct tcphdr * th,struct tcpcb * tp,int32_t * tlenp,int32_t * thf,int32_t * drop_hdrlen,int32_t * ret_val,uint32_t * ts,uint32_t * cnt)601 _ctf_drop_checks(struct tcpopt *to, struct mbuf *m, struct tcphdr *th,
602 struct tcpcb *tp, int32_t *tlenp,
603 int32_t *thf, int32_t *drop_hdrlen, int32_t *ret_val,
604 uint32_t *ts, uint32_t *cnt)
605 {
606 int32_t todrop;
607 int32_t thflags;
608 int32_t tlen;
609
610 thflags = *thf;
611 tlen = *tlenp;
612 todrop = tp->rcv_nxt - th->th_seq;
613 if (todrop > 0) {
614 if (thflags & TH_SYN) {
615 thflags &= ~TH_SYN;
616 th->th_seq++;
617 if (th->th_urp > 1)
618 th->th_urp--;
619 else
620 thflags &= ~TH_URG;
621 todrop--;
622 }
623 /*
624 * Following if statement from Stevens, vol. 2, p. 960.
625 */
626 if (todrop > tlen
627 || (todrop == tlen && (thflags & TH_FIN) == 0)) {
628 /*
629 * Any valid FIN must be to the left of the window.
630 * At this point the FIN must be a duplicate or out
631 * of sequence; drop it.
632 */
633 thflags &= ~TH_FIN;
634 /*
635 * Send an ACK to resynchronize and drop any data.
636 * But keep on processing for RST or ACK.
637 */
638 ctf_ack_war_checks(tp, ts, cnt);
639 todrop = tlen;
640 KMOD_TCPSTAT_INC(tcps_rcvduppack);
641 KMOD_TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
642 } else {
643 KMOD_TCPSTAT_INC(tcps_rcvpartduppack);
644 KMOD_TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
645 }
646 /*
647 * DSACK - add SACK block for dropped range
648 */
649 if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
650 /*
651 * ACK now, as the next in-sequence segment
652 * will clear the DSACK block again
653 */
654 ctf_ack_war_checks(tp, ts, cnt);
655 if (tp->t_flags & TF_ACKNOW)
656 tcp_update_sack_list(tp, th->th_seq,
657 th->th_seq + todrop);
658 }
659 *drop_hdrlen += todrop; /* drop from the top afterwards */
660 th->th_seq += todrop;
661 tlen -= todrop;
662 if (th->th_urp > todrop)
663 th->th_urp -= todrop;
664 else {
665 thflags &= ~TH_URG;
666 th->th_urp = 0;
667 }
668 }
669 /*
670 * If segment ends after window, drop trailing data (and PUSH and
671 * FIN); if nothing left, just ACK.
672 */
673 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
674 if (todrop > 0) {
675 KMOD_TCPSTAT_INC(tcps_rcvpackafterwin);
676 if (todrop >= tlen) {
677 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
678 /*
679 * If window is closed can only take segments at
680 * window edge, and have to drop data and PUSH from
681 * incoming segments. Continue processing, but
682 * remember to ack. Otherwise, drop segment and
683 * ack.
684 */
685 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
686 ctf_ack_war_checks(tp, ts, cnt);
687 KMOD_TCPSTAT_INC(tcps_rcvwinprobe);
688 } else {
689 __ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val, ts, cnt);
690 return (1);
691 }
692 } else
693 KMOD_TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
694 m_adj(m, -todrop);
695 tlen -= todrop;
696 thflags &= ~(TH_PUSH | TH_FIN);
697 }
698 *thf = thflags;
699 *tlenp = tlen;
700 return (0);
701 }
702
703 /*
704 * The value in ret_val informs the caller
705 * if we dropped the tcb (and lock) or not.
706 * 1 = we dropped it, 0 = the TCB is still locked
707 * and valid.
708 */
709 void
__ctf_do_dropafterack(struct mbuf * m,struct tcpcb * tp,struct tcphdr * th,int32_t thflags,int32_t tlen,int32_t * ret_val,uint32_t * ts,uint32_t * cnt)710 __ctf_do_dropafterack(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th, int32_t thflags, int32_t tlen, int32_t *ret_val, uint32_t *ts, uint32_t *cnt)
711 {
712 /*
713 * Generate an ACK dropping incoming segment if it occupies sequence
714 * space, where the ACK reflects our state.
715 *
716 * We can now skip the test for the RST flag since all paths to this
717 * code happen after packets containing RST have been dropped.
718 *
719 * In the SYN-RECEIVED state, don't send an ACK unless the segment
720 * we received passes the SYN-RECEIVED ACK test. If it fails send a
721 * RST. This breaks the loop in the "LAND" DoS attack, and also
722 * prevents an ACK storm between two listening ports that have been
723 * sent forged SYN segments, each with the source address of the
724 * other.
725 */
726 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
727 (SEQ_GT(tp->snd_una, th->th_ack) ||
728 SEQ_GT(th->th_ack, tp->snd_max))) {
729 *ret_val = 1;
730 ctf_do_dropwithreset(m, tp, th, BANDLIM_RST_OPENPORT, tlen);
731 return;
732 } else
733 *ret_val = 0;
734 ctf_ack_war_checks(tp, ts, cnt);
735 if (m)
736 m_freem(m);
737 }
738
739 void
ctf_do_drop(struct mbuf * m,struct tcpcb * tp)740 ctf_do_drop(struct mbuf *m, struct tcpcb *tp)
741 {
742
743 /*
744 * Drop space held by incoming segment and return.
745 */
746 if (tp != NULL)
747 INP_WUNLOCK(tp->t_inpcb);
748 if (m)
749 m_freem(m);
750 }
751
752 int
ctf_process_rst(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp)753 ctf_process_rst(struct mbuf *m, struct tcphdr *th, struct socket *so, struct tcpcb *tp)
754 {
755 /*
756 * RFC5961 Section 3.2
757 *
758 * - RST drops connection only if SEG.SEQ == RCV.NXT. - If RST is in
759 * window, we send challenge ACK.
760 *
761 * Note: to take into account delayed ACKs, we should test against
762 * last_ack_sent instead of rcv_nxt. Note 2: we handle special case
763 * of closed window, not covered by the RFC.
764 */
765 int dropped = 0;
766
767 if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
768 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
769 (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
770 KASSERT(tp->t_state != TCPS_SYN_SENT,
771 ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
772 __func__, th, tp));
773
774 if (V_tcp_insecure_rst ||
775 (tp->last_ack_sent == th->th_seq) ||
776 (tp->rcv_nxt == th->th_seq)) {
777 KMOD_TCPSTAT_INC(tcps_drops);
778 /* Drop the connection. */
779 switch (tp->t_state) {
780 case TCPS_SYN_RECEIVED:
781 so->so_error = ECONNREFUSED;
782 goto close;
783 case TCPS_ESTABLISHED:
784 case TCPS_FIN_WAIT_1:
785 case TCPS_FIN_WAIT_2:
786 case TCPS_CLOSE_WAIT:
787 case TCPS_CLOSING:
788 case TCPS_LAST_ACK:
789 so->so_error = ECONNRESET;
790 close:
791 tcp_state_change(tp, TCPS_CLOSED);
792 /* FALLTHROUGH */
793 default:
794 tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST);
795 tp = tcp_close(tp);
796 }
797 dropped = 1;
798 ctf_do_drop(m, tp);
799 } else {
800 KMOD_TCPSTAT_INC(tcps_badrst);
801 /* Send challenge ACK. */
802 tcp_respond(tp, mtod(m, void *), th, m,
803 tp->rcv_nxt, tp->snd_nxt, TH_ACK);
804 tp->last_ack_sent = tp->rcv_nxt;
805 }
806 } else {
807 m_freem(m);
808 }
809 return (dropped);
810 }
811
812 /*
813 * The value in ret_val informs the caller
814 * if we dropped the tcb (and lock) or not.
815 * 1 = we dropped it, 0 = the TCB is still locked
816 * and valid.
817 */
818 void
ctf_challenge_ack(struct mbuf * m,struct tcphdr * th,struct tcpcb * tp,int32_t * ret_val)819 ctf_challenge_ack(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, int32_t * ret_val)
820 {
821
822 NET_EPOCH_ASSERT();
823
824 KMOD_TCPSTAT_INC(tcps_badsyn);
825 if (V_tcp_insecure_syn &&
826 SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
827 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
828 tp = tcp_drop(tp, ECONNRESET);
829 *ret_val = 1;
830 ctf_do_drop(m, tp);
831 } else {
832 /* Send challenge ACK. */
833 tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
834 tp->snd_nxt, TH_ACK);
835 tp->last_ack_sent = tp->rcv_nxt;
836 m = NULL;
837 *ret_val = 0;
838 ctf_do_drop(m, NULL);
839 }
840 }
841
842 /*
843 * ctf_ts_check returns 1 for you should not proceed, the state
844 * machine should return. It places in ret_val what should
845 * be returned 1/0 by the caller (hpts_do_segment). The 1 indicates
846 * that the TCB is unlocked and probably dropped. The 0 indicates the
847 * TCB is still valid and locked.
848 */
849 int
ctf_ts_check(struct mbuf * m,struct tcphdr * th,struct tcpcb * tp,int32_t tlen,int32_t thflags,int32_t * ret_val)850 ctf_ts_check(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
851 int32_t tlen, int32_t thflags, int32_t * ret_val)
852 {
853
854 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
855 /*
856 * Invalidate ts_recent. If this segment updates ts_recent,
857 * the age will be reset later and ts_recent will get a
858 * valid value. If it does not, setting ts_recent to zero
859 * will at least satisfy the requirement that zero be placed
860 * in the timestamp echo reply when ts_recent isn't valid.
861 * The age isn't reset until we get a valid ts_recent
862 * because we don't want out-of-order segments to be dropped
863 * when ts_recent is old.
864 */
865 tp->ts_recent = 0;
866 } else {
867 KMOD_TCPSTAT_INC(tcps_rcvduppack);
868 KMOD_TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
869 KMOD_TCPSTAT_INC(tcps_pawsdrop);
870 *ret_val = 0;
871 if (tlen) {
872 ctf_do_dropafterack(m, tp, th, thflags, tlen, ret_val);
873 } else {
874 ctf_do_drop(m, NULL);
875 }
876 return (1);
877 }
878 return (0);
879 }
880
881 int
ctf_ts_check_ac(struct tcpcb * tp,int32_t thflags)882 ctf_ts_check_ac(struct tcpcb *tp, int32_t thflags)
883 {
884
885 if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
886 /*
887 * Invalidate ts_recent. If this segment updates ts_recent,
888 * the age will be reset later and ts_recent will get a
889 * valid value. If it does not, setting ts_recent to zero
890 * will at least satisfy the requirement that zero be placed
891 * in the timestamp echo reply when ts_recent isn't valid.
892 * The age isn't reset until we get a valid ts_recent
893 * because we don't want out-of-order segments to be dropped
894 * when ts_recent is old.
895 */
896 tp->ts_recent = 0;
897 } else {
898 KMOD_TCPSTAT_INC(tcps_rcvduppack);
899 KMOD_TCPSTAT_INC(tcps_pawsdrop);
900 return (1);
901 }
902 return (0);
903 }
904
905
906
907 void
ctf_calc_rwin(struct socket * so,struct tcpcb * tp)908 ctf_calc_rwin(struct socket *so, struct tcpcb *tp)
909 {
910 int32_t win;
911
912 /*
913 * Calculate amount of space in receive window, and then do TCP
914 * input processing. Receive window is amount of space in rcv queue,
915 * but not less than advertised window.
916 */
917 win = sbspace(&so->so_rcv);
918 if (win < 0)
919 win = 0;
920 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
921 }
922
923 void
ctf_do_dropwithreset_conn(struct mbuf * m,struct tcpcb * tp,struct tcphdr * th,int32_t rstreason,int32_t tlen)924 ctf_do_dropwithreset_conn(struct mbuf *m, struct tcpcb *tp, struct tcphdr *th,
925 int32_t rstreason, int32_t tlen)
926 {
927
928 if (tp->t_inpcb) {
929 tcp_set_inp_to_drop(tp->t_inpcb, ETIMEDOUT);
930 }
931 tcp_dropwithreset(m, th, tp, tlen, rstreason);
932 INP_WUNLOCK(tp->t_inpcb);
933 }
934
935 uint32_t
ctf_fixed_maxseg(struct tcpcb * tp)936 ctf_fixed_maxseg(struct tcpcb *tp)
937 {
938 return (tcp_fixed_maxseg(tp));
939 }
940
941 void
ctf_log_sack_filter(struct tcpcb * tp,int num_sack_blks,struct sackblk * sack_blocks)942 ctf_log_sack_filter(struct tcpcb *tp, int num_sack_blks, struct sackblk *sack_blocks)
943 {
944 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
945 union tcp_log_stackspecific log;
946 struct timeval tv;
947
948 memset(&log, 0, sizeof(log));
949 log.u_bbr.timeStamp = tcp_get_usecs(&tv);
950 log.u_bbr.flex8 = num_sack_blks;
951 if (num_sack_blks > 0) {
952 log.u_bbr.flex1 = sack_blocks[0].start;
953 log.u_bbr.flex2 = sack_blocks[0].end;
954 }
955 if (num_sack_blks > 1) {
956 log.u_bbr.flex3 = sack_blocks[1].start;
957 log.u_bbr.flex4 = sack_blocks[1].end;
958 }
959 if (num_sack_blks > 2) {
960 log.u_bbr.flex5 = sack_blocks[2].start;
961 log.u_bbr.flex6 = sack_blocks[2].end;
962 }
963 if (num_sack_blks > 3) {
964 log.u_bbr.applimited = sack_blocks[3].start;
965 log.u_bbr.pkts_out = sack_blocks[3].end;
966 }
967 TCP_LOG_EVENTP(tp, NULL,
968 &tp->t_inpcb->inp_socket->so_rcv,
969 &tp->t_inpcb->inp_socket->so_snd,
970 TCP_SACK_FILTER_RES, 0,
971 0, &log, false, &tv);
972 }
973 }
974
975 uint32_t
ctf_decay_count(uint32_t count,uint32_t decay)976 ctf_decay_count(uint32_t count, uint32_t decay)
977 {
978 /*
979 * Given a count, decay it by a set percentage. The
980 * percentage is in thousands i.e. 100% = 1000,
981 * 19.3% = 193.
982 */
983 uint64_t perc_count, decay_per;
984 uint32_t decayed_count;
985 if (decay > 1000) {
986 /* We don't raise it */
987 return (count);
988 }
989 perc_count = count;
990 decay_per = decay;
991 perc_count *= decay_per;
992 perc_count /= 1000;
993 /*
994 * So now perc_count holds the
995 * count decay value.
996 */
997 decayed_count = count - (uint32_t)perc_count;
998 return(decayed_count);
999 }
1000
1001 int32_t
ctf_progress_timeout_check(struct tcpcb * tp,bool log)1002 ctf_progress_timeout_check(struct tcpcb *tp, bool log)
1003 {
1004 if (tp->t_maxunacktime && tp->t_acktime && TSTMP_GT(ticks, tp->t_acktime)) {
1005 if ((ticks - tp->t_acktime) >= tp->t_maxunacktime) {
1006 /*
1007 * There is an assumption that the caller
1008 * will drop the connection so we will
1009 * increment the counters here.
1010 */
1011 if (log)
1012 tcp_log_end_status(tp, TCP_EI_STATUS_PROGRESS);
1013 #ifdef NETFLIX_STATS
1014 KMOD_TCPSTAT_INC(tcps_progdrops);
1015 #endif
1016 return (1);
1017 }
1018 }
1019 return (0);
1020 }
1021