tcp_reass.c 31 KB

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