1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_inet.h" 38 #include "opt_inet6.h" 39 40 /* For debugging we want counters and BB logging */ 41 /* #define TCP_REASS_COUNTERS 1 */ 42 /* #define TCP_REASS_LOGGING 1 */ 43 44 #include <sys/param.h> 45 #include <sys/kernel.h> 46 #include <sys/eventhandler.h> 47 #include <sys/malloc.h> 48 #include <sys/mbuf.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/sysctl.h> 52 #include <sys/syslog.h> 53 #include <sys/systm.h> 54 55 #include <vm/uma.h> 56 57 #include <net/if.h> 58 #include <net/if_var.h> 59 #include <net/route.h> 60 #include <net/vnet.h> 61 62 #include <netinet/in.h> 63 #include <netinet/in_pcb.h> 64 #include <netinet/in_systm.h> 65 #include <netinet/in_var.h> 66 #include <netinet/ip.h> 67 #include <netinet/ip_var.h> 68 #include <netinet/ip_options.h> 69 #include <netinet/ip6.h> 70 #include <netinet6/in6_pcb.h> 71 #include <netinet6/ip6_var.h> 72 #include <netinet6/nd6.h> 73 #include <netinet/tcp.h> 74 #include <netinet/tcp_fsm.h> 75 #include <netinet/tcp_seq.h> 76 #include <netinet/tcp_timer.h> 77 #include <netinet/tcp_var.h> 78 #ifdef TCP_REASS_LOGGING 79 #include <netinet/tcp_log_buf.h> 80 #include <netinet/tcp_hpts.h> 81 #endif 82 #include <netinet/tcpip.h> 83 84 #define TCP_R_LOG_ADD 1 85 #define TCP_R_LOG_LIMIT_REACHED 2 86 #define TCP_R_LOG_APPEND 3 87 #define TCP_R_LOG_PREPEND 4 88 #define TCP_R_LOG_REPLACE 5 89 #define TCP_R_LOG_MERGE_INTO 6 90 #define TCP_R_LOG_NEW_ENTRY 7 91 #define TCP_R_LOG_READ 8 92 #define TCP_R_LOG_ZERO 9 93 #define TCP_R_LOG_DUMP 10 94 #define TCP_R_LOG_TRIM 11 95 96 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, 97 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 98 "TCP Segment Reassembly Queue"); 99 100 static SYSCTL_NODE(_net_inet_tcp_reass, OID_AUTO, stats, 101 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 102 "TCP Segment Reassembly stats"); 103 104 static int tcp_reass_maxseg = 0; 105 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN, 106 &tcp_reass_maxseg, 0, 107 "Global maximum number of TCP Segments in Reassembly Queue"); 108 109 static uma_zone_t tcp_reass_zone; 110 SYSCTL_UMA_CUR(_net_inet_tcp_reass, OID_AUTO, cursegments, 0, 111 &tcp_reass_zone, 112 "Global number of TCP Segments currently in Reassembly Queue"); 113 114 static u_int tcp_reass_maxqueuelen = 100; 115 SYSCTL_UINT(_net_inet_tcp_reass, OID_AUTO, maxqueuelen, CTLFLAG_RWTUN, 116 &tcp_reass_maxqueuelen, 0, 117 "Maximum number of TCP Segments per Reassembly Queue"); 118 119 static int tcp_new_limits = 0; 120 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, new_limit, CTLFLAG_RWTUN, 121 &tcp_new_limits, 0, 122 "Do we use the new limit method we are discussing?"); 123 124 static u_int tcp_reass_queue_guard = 16; 125 SYSCTL_UINT(_net_inet_tcp_reass, OID_AUTO, queueguard, CTLFLAG_RWTUN, 126 &tcp_reass_queue_guard, 16, 127 "Number of TCP Segments in Reassembly Queue where we flip over to guard mode"); 128 129 #ifdef TCP_REASS_COUNTERS 130 131 counter_u64_t reass_entry; 132 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, entry, CTLFLAG_RD, 133 &reass_entry, "A segment entered reassembly "); 134 135 counter_u64_t reass_path1; 136 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, path1, CTLFLAG_RD, 137 &reass_path1, "Took path 1"); 138 139 counter_u64_t reass_path2; 140 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, path2, CTLFLAG_RD, 141 &reass_path2, "Took path 2"); 142 143 counter_u64_t reass_path3; 144 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, path3, CTLFLAG_RD, 145 &reass_path3, "Took path 3"); 146 147 counter_u64_t reass_path4; 148 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, path4, CTLFLAG_RD, 149 &reass_path4, "Took path 4"); 150 151 counter_u64_t reass_path5; 152 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, path5, CTLFLAG_RD, 153 &reass_path5, "Took path 5"); 154 155 counter_u64_t reass_path6; 156 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, path6, CTLFLAG_RD, 157 &reass_path6, "Took path 6"); 158 159 counter_u64_t reass_path7; 160 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, path7, CTLFLAG_RD, 161 &reass_path7, "Took path 7"); 162 163 counter_u64_t reass_fullwalk; 164 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, fullwalk, CTLFLAG_RD, 165 &reass_fullwalk, "Took a full walk "); 166 167 counter_u64_t reass_nospace; 168 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, nospace, CTLFLAG_RD, 169 &reass_nospace, "Had no mbuf capacity "); 170 171 counter_u64_t merge_fwd; 172 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, merge_fwd, CTLFLAG_RD, 173 &merge_fwd, "Ran merge fwd"); 174 175 counter_u64_t merge_into; 176 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, merge_into, CTLFLAG_RD, 177 &merge_into, "Ran merge into"); 178 179 counter_u64_t tcp_zero_input; 180 SYSCTL_COUNTER_U64(_net_inet_tcp_reass_stats, OID_AUTO, zero_input, CTLFLAG_RD, 181 &tcp_zero_input, "The reassembly buffer saw a zero len segment etc"); 182 183 #endif 184 185 /* Initialize TCP reassembly queue */ 186 static void 187 tcp_reass_zone_change(void *tag) 188 { 189 190 /* Set the zone limit and read back the effective value. */ 191 tcp_reass_maxseg = nmbclusters / 16; 192 tcp_reass_maxseg = uma_zone_set_max(tcp_reass_zone, 193 tcp_reass_maxseg); 194 } 195 196 #ifdef TCP_REASS_LOGGING 197 198 static void 199 tcp_log_reassm(struct tcpcb *tp, struct tseg_qent *q, struct tseg_qent *p, 200 tcp_seq seq, int len, uint8_t action, int instance) 201 { 202 struct socket *so = tptosocket(tp); 203 uint32_t cts; 204 struct timeval tv; 205 206 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 207 union tcp_log_stackspecific log; 208 209 memset(&log, 0, sizeof(log)); 210 cts = tcp_get_usecs(&tv); 211 log.u_bbr.flex1 = seq; 212 log.u_bbr.cur_del_rate = (uint64_t)q; 213 log.u_bbr.delRate = (uint64_t)p; 214 if (q != NULL) { 215 log.u_bbr.flex2 = q->tqe_start; 216 log.u_bbr.flex3 = q->tqe_len; 217 log.u_bbr.flex4 = q->tqe_mbuf_cnt; 218 log.u_bbr.hptsi_gain = q->tqe_flags; 219 } 220 if (p != NULL) { 221 log.u_bbr.flex5 = p->tqe_start; 222 log.u_bbr.pkts_out = p->tqe_len; 223 log.u_bbr.epoch = p->tqe_mbuf_cnt; 224 log.u_bbr.cwnd_gain = p->tqe_flags; 225 } 226 log.u_bbr.flex6 = tp->t_segqmbuflen; 227 log.u_bbr.flex7 = instance; 228 log.u_bbr.flex8 = action; 229 log.u_bbr.timeStamp = cts; 230 TCP_LOG_EVENTP(tp, NULL, &so->so_rcv, &so->so_snd, 231 TCP_LOG_REASS, 0, 232 len, &log, false, &tv); 233 } 234 } 235 236 static void 237 tcp_reass_log_dump(struct tcpcb *tp) 238 { 239 struct tseg_qent *q; 240 241 if (tp->t_logstate != TCP_LOG_STATE_OFF) { 242 TAILQ_FOREACH(q, &tp->t_segq, tqe_q) { 243 tcp_log_reassm(tp, q, NULL, q->tqe_start, q->tqe_len, TCP_R_LOG_DUMP, 0); 244 } 245 }; 246 } 247 248 static void 249 tcp_reass_log_new_in(struct tcpcb *tp, tcp_seq seq, int len, struct mbuf *m, 250 int logval, struct tseg_qent *q) 251 { 252 int cnt; 253 struct mbuf *t; 254 255 cnt = 0; 256 t = m; 257 while (t) { 258 cnt += t->m_len; 259 t = t->m_next; 260 } 261 tcp_log_reassm(tp, q, NULL, seq, len, logval, cnt); 262 } 263 264 #endif 265 266 void 267 tcp_reass_global_init(void) 268 { 269 270 tcp_reass_maxseg = nmbclusters / 16; 271 TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", 272 &tcp_reass_maxseg); 273 tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent), 274 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 275 /* Set the zone limit and read back the effective value. */ 276 tcp_reass_maxseg = uma_zone_set_max(tcp_reass_zone, 277 tcp_reass_maxseg); 278 #ifdef TCP_REASS_COUNTERS 279 reass_path1 = counter_u64_alloc(M_WAITOK); 280 reass_path2 = counter_u64_alloc(M_WAITOK); 281 reass_path3 = counter_u64_alloc(M_WAITOK); 282 reass_path4 = counter_u64_alloc(M_WAITOK); 283 reass_path5 = counter_u64_alloc(M_WAITOK); 284 reass_path6 = counter_u64_alloc(M_WAITOK); 285 reass_path7 = counter_u64_alloc(M_WAITOK); 286 reass_fullwalk = counter_u64_alloc(M_WAITOK); 287 reass_nospace = counter_u64_alloc(M_WAITOK); 288 reass_entry = counter_u64_alloc(M_WAITOK); 289 merge_fwd = counter_u64_alloc(M_WAITOK); 290 merge_into = counter_u64_alloc(M_WAITOK); 291 tcp_zero_input = counter_u64_alloc(M_WAITOK); 292 #endif 293 EVENTHANDLER_REGISTER(nmbclusters_change, 294 tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY); 295 296 } 297 298 void 299 tcp_reass_flush(struct tcpcb *tp) 300 { 301 struct tseg_qent *qe; 302 303 INP_WLOCK_ASSERT(tptoinpcb(tp)); 304 305 while ((qe = TAILQ_FIRST(&tp->t_segq)) != NULL) { 306 TAILQ_REMOVE(&tp->t_segq, qe, tqe_q); 307 m_freem(qe->tqe_m); 308 uma_zfree(tcp_reass_zone, qe); 309 tp->t_segqlen--; 310 } 311 tp->t_segqmbuflen = 0; 312 KASSERT((tp->t_segqlen == 0), 313 ("TCP reass queue %p segment count is %d instead of 0 after flush.", 314 tp, tp->t_segqlen)); 315 } 316 317 static void 318 tcp_reass_append(struct tcpcb *tp, struct tseg_qent *last, 319 struct mbuf *m, struct tcphdr *th, int tlen, 320 struct mbuf *mlast, int lenofoh) 321 { 322 323 #ifdef TCP_REASS_LOGGING 324 tcp_log_reassm(tp, last, NULL, th->th_seq, tlen, TCP_R_LOG_APPEND, 0); 325 #endif 326 last->tqe_len += tlen; 327 last->tqe_m->m_pkthdr.len += tlen; 328 /* Preserve the FIN bit if its there */ 329 last->tqe_flags |= (tcp_get_flags(th) & TH_FIN); 330 last->tqe_last->m_next = m; 331 last->tqe_last = mlast; 332 last->tqe_mbuf_cnt += lenofoh; 333 tp->t_rcvoopack++; 334 TCPSTAT_INC(tcps_rcvoopack); 335 TCPSTAT_ADD(tcps_rcvoobyte, tlen); 336 #ifdef TCP_REASS_LOGGING 337 tcp_reass_log_new_in(tp, last->tqe_start, lenofoh, last->tqe_m, 338 TCP_R_LOG_APPEND, 339 last); 340 #endif 341 } 342 343 static void 344 tcp_reass_prepend(struct tcpcb *tp, struct tseg_qent *first, struct mbuf *m, struct tcphdr *th, 345 int tlen, struct mbuf *mlast, int lenofoh) 346 { 347 int i; 348 349 #ifdef TCP_REASS_LOGGING 350 tcp_log_reassm(tp, first, NULL, th->th_seq, tlen, TCP_R_LOG_PREPEND, 0); 351 #endif 352 if (SEQ_GT((th->th_seq + tlen), first->tqe_start)) { 353 /* The new data overlaps into the old */ 354 i = (th->th_seq + tlen) - first->tqe_start; 355 #ifdef TCP_REASS_LOGGING 356 tcp_log_reassm(tp, first, NULL, 0, i, TCP_R_LOG_TRIM, 1); 357 #endif 358 m_adj(first->tqe_m, i); 359 first->tqe_len -= i; 360 first->tqe_start += i; 361 } 362 /* Ok now setup our chain to point to the old first */ 363 mlast->m_next = first->tqe_m; 364 first->tqe_m = m; 365 first->tqe_len += tlen; 366 first->tqe_start = th->th_seq; 367 first->tqe_m->m_pkthdr.len = first->tqe_len; 368 first->tqe_mbuf_cnt += lenofoh; 369 tp->t_rcvoopack++; 370 TCPSTAT_INC(tcps_rcvoopack); 371 TCPSTAT_ADD(tcps_rcvoobyte, tlen); 372 #ifdef TCP_REASS_LOGGING 373 tcp_reass_log_new_in(tp, first->tqe_start, lenofoh, first->tqe_m, 374 TCP_R_LOG_PREPEND, 375 first); 376 #endif 377 } 378 379 static void 380 tcp_reass_replace(struct tcpcb *tp, struct tseg_qent *q, struct mbuf *m, 381 tcp_seq seq, int len, struct mbuf *mlast, int mbufoh, uint16_t flags) 382 { 383 /* 384 * Free the data in q, and replace 385 * it with the new segment. 386 */ 387 int len_dif; 388 389 #ifdef TCP_REASS_LOGGING 390 tcp_log_reassm(tp, q, NULL, seq, len, TCP_R_LOG_REPLACE, 0); 391 #endif 392 m_freem(q->tqe_m); 393 KASSERT(tp->t_segqmbuflen >= q->tqe_mbuf_cnt, 394 ("Tp:%p seg queue goes negative", tp)); 395 tp->t_segqmbuflen -= q->tqe_mbuf_cnt; 396 q->tqe_mbuf_cnt = mbufoh; 397 q->tqe_m = m; 398 q->tqe_last = mlast; 399 q->tqe_start = seq; 400 if (len > q->tqe_len) 401 len_dif = len - q->tqe_len; 402 else 403 len_dif = 0; 404 tp->t_rcvoopack++; 405 TCPSTAT_INC(tcps_rcvoopack); 406 TCPSTAT_ADD(tcps_rcvoobyte, len_dif); 407 q->tqe_len = len; 408 q->tqe_flags = (flags & TH_FIN); 409 q->tqe_m->m_pkthdr.len = q->tqe_len; 410 tp->t_segqmbuflen += mbufoh; 411 412 } 413 414 static void 415 tcp_reass_merge_into(struct tcpcb *tp, struct tseg_qent *ent, 416 struct tseg_qent *q) 417 { 418 /* 419 * Merge q into ent and free q from the list. 420 */ 421 #ifdef TCP_REASS_LOGGING 422 tcp_log_reassm(tp, q, ent, 0, 0, TCP_R_LOG_MERGE_INTO, 0); 423 #endif 424 #ifdef TCP_REASS_COUNTERS 425 counter_u64_add(merge_into, 1); 426 #endif 427 ent->tqe_last->m_next = q->tqe_m; 428 ent->tqe_last = q->tqe_last; 429 ent->tqe_len += q->tqe_len; 430 ent->tqe_mbuf_cnt += q->tqe_mbuf_cnt; 431 ent->tqe_m->m_pkthdr.len += q->tqe_len; 432 ent->tqe_flags |= (q->tqe_flags & TH_FIN); 433 TAILQ_REMOVE(&tp->t_segq, q, tqe_q); 434 uma_zfree(tcp_reass_zone, q); 435 tp->t_segqlen--; 436 437 } 438 439 static void 440 tcp_reass_merge_forward(struct tcpcb *tp, struct tseg_qent *ent) 441 { 442 struct tseg_qent *q, *qtmp; 443 int i; 444 tcp_seq max; 445 /* 446 * Given an entry merge forward anyplace 447 * that ent overlaps forward. 448 */ 449 450 max = ent->tqe_start + ent->tqe_len; 451 q = TAILQ_NEXT(ent, tqe_q); 452 if (q == NULL) { 453 /* Nothing left */ 454 return; 455 } 456 TAILQ_FOREACH_FROM_SAFE(q, &tp->t_segq, tqe_q, qtmp) { 457 if (SEQ_GT(q->tqe_start, max)) { 458 /* Beyond q */ 459 break; 460 } 461 /* We have some or all that are overlapping */ 462 if (SEQ_GEQ(max, (q->tqe_start + q->tqe_len))) { 463 /* It consumes it all */ 464 tp->t_segqmbuflen -= q->tqe_mbuf_cnt; 465 m_freem(q->tqe_m); 466 TAILQ_REMOVE(&tp->t_segq, q, tqe_q); 467 uma_zfree(tcp_reass_zone, q); 468 tp->t_segqlen--; 469 continue; 470 } 471 /* 472 * Trim the q entry to dovetail to this one 473 * and then merge q into ent updating max 474 * in the process. 475 */ 476 i = max - q->tqe_start; 477 #ifdef TCP_REASS_LOGGING 478 tcp_log_reassm(tp, q, NULL, 0, i, TCP_R_LOG_TRIM, 2); 479 #endif 480 m_adj(q->tqe_m, i); 481 q->tqe_len -= i; 482 q->tqe_start += i; 483 tcp_reass_merge_into(tp, ent, q); 484 max = ent->tqe_start + ent->tqe_len; 485 } 486 #ifdef TCP_REASS_COUNTERS 487 counter_u64_add(merge_fwd, 1); 488 #endif 489 } 490 491 static int 492 tcp_reass_overhead_of_chain(struct mbuf *m, struct mbuf **mlast) 493 { 494 int len = MSIZE; 495 496 if (m->m_flags & M_EXT) 497 len += m->m_ext.ext_size; 498 while (m->m_next != NULL) { 499 m = m->m_next; 500 len += MSIZE; 501 if (m->m_flags & M_EXT) 502 len += m->m_ext.ext_size; 503 } 504 *mlast = m; 505 return (len); 506 } 507 508 /* 509 * NOTE!!! the new tcp-reassembly code *must not* use 510 * m_adj() with a negative index. That alters the chain 511 * of mbufs (by possibly chopping trailing mbufs). At 512 * the front of tcp_reass we count the mbuf overhead 513 * and setup the tail pointer. If we use m_adj(m, -5) 514 * we could corrupt the tail pointer. Currently the 515 * code only uses m_adj(m, postive-num). If this 516 * changes appropriate changes to update mlast would 517 * be needed. 518 */ 519 int 520 tcp_reass(struct tcpcb *tp, struct tcphdr *th, tcp_seq *seq_start, 521 int *tlenp, struct mbuf *m) 522 { 523 struct tseg_qent *q, *last, *first; 524 struct tseg_qent *p = NULL; 525 struct tseg_qent *nq = NULL; 526 struct tseg_qent *te = NULL; 527 struct mbuf *mlast = NULL; 528 struct inpcb *inp = tptoinpcb(tp); 529 struct socket *so = tptosocket(tp); 530 struct sockbuf *sb = &so->so_rcv; 531 char *s = NULL; 532 int flags, i, lenofoh; 533 534 INP_WLOCK_ASSERT(inp); 535 /* 536 * XXX: tcp_reass() is rather inefficient with its data structures 537 * and should be rewritten (see NetBSD for optimizations). 538 */ 539 540 KASSERT(th == NULL || (seq_start != NULL && tlenp != NULL), 541 ("tcp_reass called with illegal parameter combination " 542 "(tp=%p, th=%p, seq_start=%p, tlenp=%p, m=%p)", 543 tp, th, seq_start, tlenp, m)); 544 /* 545 * Call with th==NULL after become established to 546 * force pre-ESTABLISHED data up to user socket. 547 */ 548 if (th == NULL) 549 goto present; 550 KASSERT(SEQ_GEQ(th->th_seq, tp->rcv_nxt), 551 ("Attempt to add old entry to reassembly queue (th=%p, tp=%p)", 552 th, tp)); 553 #ifdef TCP_REASS_LOGGING 554 tcp_reass_log_new_in(tp, th->th_seq, *tlenp, m, TCP_R_LOG_ADD, NULL); 555 #endif 556 #ifdef TCP_REASS_COUNTERS 557 counter_u64_add(reass_entry, 1); 558 #endif 559 /* 560 * Check for zero length data. 561 */ 562 if ((*tlenp == 0) && ((tcp_get_flags(th) & TH_FIN) == 0)) { 563 /* 564 * A zero length segment does no 565 * one any good. We could check 566 * the rcv_nxt <-> rcv_wnd but thats 567 * already done for us by the caller. 568 */ 569 strip_fin: 570 #ifdef TCP_REASS_COUNTERS 571 counter_u64_add(tcp_zero_input, 1); 572 #endif 573 m_freem(m); 574 #ifdef TCP_REASS_LOGGING 575 tcp_reass_log_dump(tp); 576 #endif 577 return (0); 578 } else if ((*tlenp == 0) && 579 (tcp_get_flags(th) & TH_FIN) && 580 !TCPS_HAVEESTABLISHED(tp->t_state)) { 581 /* 582 * We have not established, and we 583 * have a FIN and no data. Lets treat 584 * this as the same as if the FIN were 585 * not present. We don't want to save 586 * the FIN bit in a reassembly buffer 587 * we want to get established first before 588 * we do that (the peer will retransmit). 589 */ 590 goto strip_fin; 591 } 592 /* 593 * Will it fit? 594 */ 595 lenofoh = tcp_reass_overhead_of_chain(m, &mlast); 596 if ((th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) && 597 (sb->sb_mbcnt + tp->t_segqmbuflen + lenofoh) > sb->sb_mbmax) { 598 /* No room */ 599 TCPSTAT_INC(tcps_rcvreassfull); 600 #ifdef TCP_REASS_COUNTERS 601 counter_u64_add(reass_nospace, 1); 602 #endif 603 #ifdef TCP_REASS_LOGGING 604 tcp_log_reassm(tp, NULL, NULL, th->th_seq, lenofoh, TCP_R_LOG_LIMIT_REACHED, 0); 605 #endif 606 if ((s = tcp_log_addrs(&inp->inp_inc, th, NULL, NULL))) { 607 log(LOG_DEBUG, "%s; %s: mbuf count limit reached, " 608 "segment dropped\n", s, __func__); 609 free(s, M_TCPLOG); 610 } 611 m_freem(m); 612 *tlenp = 0; 613 #ifdef TCP_REASS_LOGGING 614 tcp_reass_log_dump(tp); 615 #endif 616 return (0); 617 } 618 /* 619 * First lets deal with two common cases, the 620 * segment appends to the back of our collected 621 * segments. Or the segment is the next in line. 622 */ 623 last = TAILQ_LAST_FAST(&tp->t_segq, tseg_qent, tqe_q); 624 if (last != NULL) { 625 if ((tcp_get_flags(th) & TH_FIN) && 626 SEQ_LT((th->th_seq + *tlenp), (last->tqe_start + last->tqe_len))) { 627 /* 628 * Someone is trying to game us, dump 629 * the segment. 630 */ 631 *tlenp = 0; 632 m_freem(m); 633 return (0); 634 } 635 if ((SEQ_GEQ(th->th_seq, last->tqe_start)) && 636 (SEQ_GEQ((last->tqe_start + last->tqe_len), th->th_seq))) { 637 /* Common case, trailing segment is added */ 638 /** 639 * +--last 640 * v 641 * reassembly buffer |---| |---| |---| 642 * new segment |---| 643 */ 644 #ifdef TCP_REASS_COUNTERS 645 counter_u64_add(reass_path1, 1); 646 #endif 647 if (SEQ_GT((last->tqe_start + last->tqe_len), th->th_seq)) { 648 i = (last->tqe_start + last->tqe_len) - th->th_seq; 649 if (i < *tlenp) { 650 #ifdef TCP_REASS_LOGGING 651 tcp_log_reassm(tp, last, NULL, 0, i, TCP_R_LOG_TRIM, 3); 652 th->th_seq += i; 653 #endif 654 m_adj(m, i); 655 *tlenp -= i; 656 } else { 657 /* Complete overlap */ 658 TCPSTAT_INC(tcps_rcvduppack); 659 TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp); 660 m_freem(m); 661 *tlenp = last->tqe_len; 662 *seq_start = last->tqe_start; 663 return (0); 664 } 665 } 666 if (last->tqe_flags & TH_FIN) { 667 /* 668 * We have data after the FIN on the last? 669 */ 670 *tlenp = 0; 671 m_freem(m); 672 return(0); 673 } 674 tcp_reass_append(tp, last, m, th, *tlenp, mlast, lenofoh); 675 tp->t_segqmbuflen += lenofoh; 676 *seq_start = last->tqe_start; 677 *tlenp = last->tqe_len; 678 return (0); 679 } else if (SEQ_GT(th->th_seq, (last->tqe_start + last->tqe_len))) { 680 /* 681 * Second common case, we missed 682 * another one and have something more 683 * for the end. 684 */ 685 /** 686 * +--last 687 * v 688 * reassembly buffer |---| |---| |---| 689 * new segment |---| 690 */ 691 if (last->tqe_flags & TH_FIN) { 692 /* 693 * We have data after the FIN on the last? 694 */ 695 *tlenp = 0; 696 m_freem(m); 697 return(0); 698 } 699 #ifdef TCP_REASS_COUNTERS 700 counter_u64_add(reass_path2, 1); 701 #endif 702 p = last; 703 goto new_entry; 704 } 705 } else { 706 /* First segment (it's NULL). */ 707 goto new_entry; 708 } 709 first = TAILQ_FIRST(&tp->t_segq); 710 if (SEQ_LT(th->th_seq, first->tqe_start) && 711 SEQ_GEQ((th->th_seq + *tlenp),first->tqe_start) && 712 SEQ_LT((th->th_seq + *tlenp), (first->tqe_start + first->tqe_len))) { 713 /* 714 * The head of the queue is prepended by this and 715 * it may be the one I want most. 716 */ 717 /** 718 * first-------+ 719 * v 720 * rea: |---| |---| |---| 721 * new: |---| 722 * Note the case we do not deal with here is: 723 * rea= |---| |---| |---| 724 * new= |----| 725 * Due to the fact that it could be 726 * new |--------------------| 727 * And we might need to merge forward. 728 */ 729 #ifdef INVARIANTS 730 struct mbuf *firstmbuf; 731 #endif 732 733 #ifdef TCP_REASS_COUNTERS 734 counter_u64_add(reass_path3, 1); 735 #endif 736 if (SEQ_LT(th->th_seq, tp->rcv_nxt)) { 737 /* 738 * The resend was even before 739 * what we have. We need to trim it. 740 * Note TSNH (it should be trimmed 741 * before the call to tcp_reass()). 742 */ 743 #ifdef INVARIANTS 744 panic("th->th_seq:%u rcv_nxt:%u tp:%p not pre-trimmed", 745 th->th_seq, tp->rcv_nxt, tp); 746 #else 747 i = tp->rcv_nxt - th->th_seq; 748 #ifdef TCP_REASS_LOGGING 749 tcp_log_reassm(tp, first, NULL, 0, i, TCP_R_LOG_TRIM, 4); 750 #endif 751 m_adj(m, i); 752 th->th_seq += i; 753 *tlenp -= i; 754 #endif 755 } 756 #ifdef INVARIANTS 757 firstmbuf = first->tqe_m; 758 #endif 759 tcp_reass_prepend(tp, first, m, th, *tlenp, mlast, lenofoh); 760 #ifdef INVARIANTS 761 if (firstmbuf == first->tqe_m) { 762 panic("First stayed same m:%p foobar:%p first->tqe_m:%p tp:%p first:%p", 763 m, firstmbuf, first->tqe_m, tp, first); 764 } else if (first->tqe_m != m) { 765 panic("First did not change to m:%p foobar:%p first->tqe_m:%p tp:%p first:%p", 766 m, firstmbuf, first->tqe_m, tp, first); 767 } 768 #endif 769 tp->t_segqmbuflen += lenofoh; 770 *seq_start = first->tqe_start; 771 *tlenp = first->tqe_len; 772 goto present; 773 } else if (SEQ_LT((th->th_seq + *tlenp), first->tqe_start)) { 774 /* New segment is before our earliest segment. */ 775 /** 776 * first---->+ 777 * v 778 * rea= |---| .... 779 * new" |---| 780 * 781 */ 782 goto new_entry; 783 } 784 /* 785 * Find a segment which begins after this one does. 786 */ 787 #ifdef TCP_REASS_COUNTERS 788 counter_u64_add(reass_fullwalk, 1); 789 #endif 790 TAILQ_FOREACH(q, &tp->t_segq, tqe_q) { 791 if (SEQ_GT(q->tqe_start, th->th_seq)) 792 break; 793 } 794 p = TAILQ_PREV(q, tsegqe_head, tqe_q); 795 /** 796 * Now is this fit just in-between only? 797 * i.e.: 798 * p---+ +----q 799 * v v 800 * res= |--| |--| |--| 801 * nee |-| 802 */ 803 if (SEQ_LT((th->th_seq + *tlenp), q->tqe_start) && 804 ((p == NULL) || (SEQ_GT(th->th_seq, (p->tqe_start + p->tqe_len))))) { 805 /* Yep no overlap */ 806 goto new_entry; 807 } 808 /** 809 * If we reach here we have some (possibly all) overlap 810 * such as: 811 * res= |--| |--| |--| 812 * new= |----| 813 * or new= |-----------------| 814 * or new= |--------| 815 * or new= |---| 816 * or new= |-----------| 817 */ 818 if ((p != NULL) && 819 (SEQ_LEQ(th->th_seq, (p->tqe_start + p->tqe_len)))) { 820 /* conversion to int (in i) handles seq wraparound */ 821 822 #ifdef TCP_REASS_COUNTERS 823 counter_u64_add(reass_path4, 1); 824 #endif 825 i = p->tqe_start + p->tqe_len - th->th_seq; 826 if (i >= 0) { 827 if (i >= *tlenp) { 828 /** 829 * prev seg---->+ 830 * v 831 * reassembly buffer |---| 832 * new segment |-| 833 */ 834 TCPSTAT_INC(tcps_rcvduppack); 835 TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp); 836 *tlenp = p->tqe_len; 837 *seq_start = p->tqe_start; 838 m_freem(m); 839 /* 840 * Try to present any queued data 841 * at the left window edge to the user. 842 * This is needed after the 3-WHS 843 * completes. Note this probably 844 * will not work and we will return. 845 */ 846 return (0); 847 } 848 if (i > 0) { 849 /** 850 * prev seg---->+ 851 * v 852 * reassembly buffer |---| 853 * new segment |-----| 854 */ 855 #ifdef TCP_REASS_COUNTERS 856 counter_u64_add(reass_path5, 1); 857 #endif 858 #ifdef TCP_REASS_LOGGING 859 tcp_log_reassm(tp, p, NULL, 0, i, TCP_R_LOG_TRIM, 5); 860 #endif 861 m_adj(m, i); 862 *tlenp -= i; 863 th->th_seq += i; 864 } 865 } 866 if (th->th_seq == (p->tqe_start + p->tqe_len)) { 867 /* 868 * If dovetails in with this one 869 * append it. 870 */ 871 /** 872 * prev seg---->+ 873 * v 874 * reassembly buffer |--| |---| 875 * new segment |--| 876 * (note: it was trimmed above if it overlapped) 877 */ 878 tcp_reass_append(tp, p, m, th, *tlenp, mlast, lenofoh); 879 tp->t_segqmbuflen += lenofoh; 880 } else { 881 #ifdef INVARIANTS 882 panic("Impossible cut th_seq:%u p->seq:%u(%d) p:%p tp:%p", 883 th->th_seq, p->tqe_start, p->tqe_len, 884 p, tp); 885 #endif 886 *tlenp = 0; 887 m_freem(m); 888 return (0); 889 } 890 q = p; 891 } else { 892 /* 893 * The new data runs over the 894 * top of previously sack'd data (in q). 895 * It may be partially overlapping, or 896 * it may overlap the entire segment. 897 */ 898 #ifdef TCP_REASS_COUNTERS 899 counter_u64_add(reass_path6, 1); 900 #endif 901 if (SEQ_GEQ((th->th_seq + *tlenp), (q->tqe_start + q->tqe_len))) { 902 /* It consumes it all */ 903 /** 904 * next seg---->+ 905 * v 906 * reassembly buffer |--| |---| 907 * new segment |----------| 908 */ 909 #ifdef TCP_REASS_COUNTERS 910 counter_u64_add(reass_path7, 1); 911 #endif 912 tcp_reass_replace(tp, q, m, th->th_seq, *tlenp, mlast, lenofoh, tcp_get_flags(th)); 913 } else { 914 /* 915 * We just need to prepend the data 916 * to this. It does not overrun 917 * the end. 918 */ 919 /** 920 * next seg---->+ 921 * v 922 * reassembly buffer |--| |---| 923 * new segment |----------| 924 */ 925 tcp_reass_prepend(tp, q, m, th, *tlenp, mlast, lenofoh); 926 tp->t_segqmbuflen += lenofoh; 927 } 928 } 929 /* Now does it go further than that? */ 930 tcp_reass_merge_forward(tp, q); 931 *seq_start = q->tqe_start; 932 *tlenp = q->tqe_len; 933 goto present; 934 935 /* 936 * When we reach here we can't combine it 937 * with any existing segment. 938 * 939 * Limit the number of segments that can be queued to reduce the 940 * potential for mbuf exhaustion. For best performance, we want to be 941 * able to queue a full window's worth of segments. The size of the 942 * socket receive buffer determines our advertised window and grows 943 * automatically when socket buffer autotuning is enabled. Use it as the 944 * basis for our queue limit. 945 * 946 * However, allow the user to specify a ceiling for the number of 947 * segments in each queue. 948 * 949 * Always let the missing segment through which caused this queue. 950 * NB: Access to the socket buffer is left intentionally unlocked as we 951 * can tolerate stale information here. 952 * 953 * XXXLAS: Using sbspace(so->so_rcv) instead of so->so_rcv.sb_hiwat 954 * should work but causes packets to be dropped when they shouldn't. 955 * Investigate why and re-evaluate the below limit after the behaviour 956 * is understood. 957 */ 958 new_entry: 959 if (th->th_seq == tp->rcv_nxt && TCPS_HAVEESTABLISHED(tp->t_state)) { 960 tp->rcv_nxt += *tlenp; 961 flags = tcp_get_flags(th) & TH_FIN; 962 TCPSTAT_INC(tcps_rcvoopack); 963 TCPSTAT_ADD(tcps_rcvoobyte, *tlenp); 964 SOCKBUF_LOCK(&so->so_rcv); 965 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 966 m_freem(m); 967 } else { 968 sbappendstream_locked(&so->so_rcv, m, 0); 969 } 970 tp->t_flags |= TF_WAKESOR; 971 return (flags); 972 } 973 if (tcp_new_limits) { 974 if ((tp->t_segqlen > tcp_reass_queue_guard) && 975 (*tlenp < MSIZE)) { 976 /* 977 * This is really a lie, we are not full but 978 * are getting a segment that is above 979 * guard threshold. If it is and its below 980 * a mbuf size (256) we drop it if it 981 * can't fill in some place. 982 */ 983 TCPSTAT_INC(tcps_rcvreassfull); 984 *tlenp = 0; 985 if ((s = tcp_log_addrs(&inp->inp_inc, th, NULL, NULL))) { 986 log(LOG_DEBUG, "%s; %s: queue limit reached, " 987 "segment dropped\n", s, __func__); 988 free(s, M_TCPLOG); 989 } 990 m_freem(m); 991 #ifdef TCP_REASS_LOGGING 992 tcp_reass_log_dump(tp); 993 #endif 994 return (0); 995 } 996 } else { 997 if (tp->t_segqlen >= min((so->so_rcv.sb_hiwat / tp->t_maxseg) + 1, 998 tcp_reass_maxqueuelen)) { 999 TCPSTAT_INC(tcps_rcvreassfull); 1000 *tlenp = 0; 1001 if ((s = tcp_log_addrs(&inp->inp_inc, th, NULL, NULL))) { 1002 log(LOG_DEBUG, "%s; %s: queue limit reached, " 1003 "segment dropped\n", s, __func__); 1004 free(s, M_TCPLOG); 1005 } 1006 m_freem(m); 1007 #ifdef TCP_REASS_LOGGING 1008 tcp_reass_log_dump(tp); 1009 #endif 1010 return (0); 1011 } 1012 } 1013 /* 1014 * Allocate a new queue entry. If we can't, or hit the zone limit 1015 * just drop the pkt. 1016 */ 1017 te = uma_zalloc(tcp_reass_zone, M_NOWAIT); 1018 if (te == NULL) { 1019 TCPSTAT_INC(tcps_rcvmemdrop); 1020 m_freem(m); 1021 *tlenp = 0; 1022 if ((s = tcp_log_addrs(&inp->inp_inc, th, NULL, NULL))) { 1023 log(LOG_DEBUG, "%s; %s: global zone limit " 1024 "reached, segment dropped\n", s, __func__); 1025 free(s, M_TCPLOG); 1026 } 1027 return (0); 1028 } 1029 tp->t_segqlen++; 1030 tp->t_rcvoopack++; 1031 TCPSTAT_INC(tcps_rcvoopack); 1032 TCPSTAT_ADD(tcps_rcvoobyte, *tlenp); 1033 /* Insert the new segment queue entry into place. */ 1034 te->tqe_m = m; 1035 te->tqe_flags = tcp_get_flags(th); 1036 te->tqe_len = *tlenp; 1037 te->tqe_start = th->th_seq; 1038 te->tqe_last = mlast; 1039 te->tqe_mbuf_cnt = lenofoh; 1040 tp->t_segqmbuflen += te->tqe_mbuf_cnt; 1041 if (p == NULL) { 1042 TAILQ_INSERT_HEAD(&tp->t_segq, te, tqe_q); 1043 } else { 1044 TAILQ_INSERT_AFTER(&tp->t_segq, p, te, tqe_q); 1045 } 1046 #ifdef TCP_REASS_LOGGING 1047 tcp_reass_log_new_in(tp, th->th_seq, *tlenp, m, TCP_R_LOG_NEW_ENTRY, te); 1048 #endif 1049 present: 1050 /* 1051 * Present data to user, advancing rcv_nxt through 1052 * completed sequence space. 1053 */ 1054 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 1055 return (0); 1056 q = TAILQ_FIRST(&tp->t_segq); 1057 KASSERT(q == NULL || SEQ_GEQ(q->tqe_start, tp->rcv_nxt), 1058 ("Reassembly queue for %p has stale entry at head", tp)); 1059 if (!q || q->tqe_start != tp->rcv_nxt) { 1060 #ifdef TCP_REASS_LOGGING 1061 tcp_reass_log_dump(tp); 1062 #endif 1063 return (0); 1064 } 1065 SOCKBUF_LOCK(&so->so_rcv); 1066 do { 1067 tp->rcv_nxt += q->tqe_len; 1068 flags = q->tqe_flags & TH_FIN; 1069 nq = TAILQ_NEXT(q, tqe_q); 1070 TAILQ_REMOVE(&tp->t_segq, q, tqe_q); 1071 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1072 m_freem(q->tqe_m); 1073 } else { 1074 #ifdef TCP_REASS_LOGGING 1075 tcp_reass_log_new_in(tp, q->tqe_start, q->tqe_len, q->tqe_m, TCP_R_LOG_READ, q); 1076 if (th != NULL) { 1077 tcp_log_reassm(tp, q, NULL, th->th_seq, *tlenp, TCP_R_LOG_READ, 1); 1078 } else { 1079 tcp_log_reassm(tp, q, NULL, 0, 0, TCP_R_LOG_READ, 1); 1080 } 1081 #endif 1082 sbappendstream_locked(&so->so_rcv, q->tqe_m, 0); 1083 } 1084 #ifdef TCP_REASS_LOGGING 1085 if (th != NULL) { 1086 tcp_log_reassm(tp, q, NULL, th->th_seq, *tlenp, TCP_R_LOG_READ, 2); 1087 } else { 1088 tcp_log_reassm(tp, q, NULL, 0, 0, TCP_R_LOG_READ, 2); 1089 } 1090 #endif 1091 KASSERT(tp->t_segqmbuflen >= q->tqe_mbuf_cnt, 1092 ("tp:%p seg queue goes negative", tp)); 1093 tp->t_segqmbuflen -= q->tqe_mbuf_cnt; 1094 uma_zfree(tcp_reass_zone, q); 1095 tp->t_segqlen--; 1096 q = nq; 1097 } while (q && q->tqe_start == tp->rcv_nxt); 1098 if (TAILQ_EMPTY(&tp->t_segq) && 1099 (tp->t_segqmbuflen != 0)) { 1100 #ifdef INVARIANTS 1101 panic("tp:%p segq:%p len:%d queue empty", 1102 tp, &tp->t_segq, tp->t_segqmbuflen); 1103 #else 1104 #ifdef TCP_REASS_LOGGING 1105 if (th != NULL) { 1106 tcp_log_reassm(tp, NULL, NULL, th->th_seq, *tlenp, TCP_R_LOG_ZERO, 0); 1107 } else { 1108 tcp_log_reassm(tp, NULL, NULL, 0, 0, TCP_R_LOG_ZERO, 0); 1109 } 1110 #endif 1111 tp->t_segqmbuflen = 0; 1112 #endif 1113 } 1114 #ifdef TCP_REASS_LOGGING 1115 tcp_reass_log_dump(tp); 1116 #endif 1117 tp->t_flags |= TF_WAKESOR; 1118 return (flags); 1119 } 1120