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