1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 30 * $FreeBSD$ 31 */ 32 33 #include "opt_inet.h" 34 #include "opt_inet6.h" 35 #include "opt_tcpdebug.h" 36 37 #include <sys/param.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/sysctl.h> 44 #include <sys/syslog.h> 45 #include <sys/systm.h> 46 47 #include <vm/uma.h> 48 49 #include <net/if.h> 50 #include <net/route.h> 51 52 #include <netinet/in.h> 53 #include <netinet/in_pcb.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/in_var.h> 56 #include <netinet/ip.h> 57 #include <netinet/ip_var.h> 58 #include <netinet/ip_options.h> 59 #include <netinet/ip6.h> 60 #include <netinet6/in6_pcb.h> 61 #include <netinet6/ip6_var.h> 62 #include <netinet6/nd6.h> 63 #include <netinet/tcp.h> 64 #include <netinet/tcp_fsm.h> 65 #include <netinet/tcp_seq.h> 66 #include <netinet/tcp_timer.h> 67 #include <netinet/tcp_var.h> 68 #include <netinet6/tcp6_var.h> 69 #include <netinet/tcpip.h> 70 #ifdef TCPDEBUG 71 #include <netinet/tcp_debug.h> 72 #endif /* TCPDEBUG */ 73 74 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0, 75 "TCP Segment Reassembly Queue"); 76 77 static int tcp_reass_maxseg = 0; 78 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN, 79 &tcp_reass_maxseg, 0, 80 "Global maximum number of TCP Segments in Reassembly Queue"); 81 82 int tcp_reass_qsize = 0; 83 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD, 84 &tcp_reass_qsize, 0, 85 "Global number of TCP Segments currently in Reassembly Queue"); 86 87 static int tcp_reass_maxqlen = 48; 88 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW, 89 &tcp_reass_maxqlen, 0, 90 "Maximum number of TCP Segments per individual Reassembly Queue"); 91 92 static int tcp_reass_overflows = 0; 93 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD, 94 &tcp_reass_overflows, 0, 95 "Global number of TCP Segment Reassembly Queue Overflows"); 96 97 /* Initialize TCP reassembly queue */ 98 static void 99 tcp_reass_zone_change(void *tag) 100 { 101 102 tcp_reass_maxseg = nmbclusters / 16; 103 uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg); 104 } 105 106 uma_zone_t tcp_reass_zone; 107 108 void 109 tcp_reass_init(void) 110 { 111 112 tcp_reass_maxseg = nmbclusters / 16; 113 TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", 114 &tcp_reass_maxseg); 115 tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent), 116 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 117 uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg); 118 EVENTHANDLER_REGISTER(nmbclusters_change, 119 tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY); 120 } 121 122 int 123 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m) 124 { 125 struct tseg_qent *q; 126 struct tseg_qent *p = NULL; 127 struct tseg_qent *nq; 128 struct tseg_qent *te = NULL; 129 struct socket *so = tp->t_inpcb->inp_socket; 130 int flags; 131 132 INP_LOCK_ASSERT(tp->t_inpcb); 133 134 /* 135 * XXX: tcp_reass() is rather inefficient with its data structures 136 * and should be rewritten (see NetBSD for optimizations). While 137 * doing that it should move to its own file tcp_reass.c. 138 */ 139 140 /* 141 * Call with th==NULL after become established to 142 * force pre-ESTABLISHED data up to user socket. 143 */ 144 if (th == NULL) 145 goto present; 146 147 /* 148 * Limit the number of segments in the reassembly queue to prevent 149 * holding on to too many segments (and thus running out of mbufs). 150 * Make sure to let the missing segment through which caused this 151 * queue. Always keep one global queue entry spare to be able to 152 * process the missing segment. 153 */ 154 if (th->th_seq != tp->rcv_nxt && 155 (tcp_reass_qsize + 1 >= tcp_reass_maxseg || 156 tp->t_segqlen >= tcp_reass_maxqlen)) { 157 tcp_reass_overflows++; 158 tcpstat.tcps_rcvmemdrop++; 159 m_freem(m); 160 *tlenp = 0; 161 return (0); 162 } 163 164 /* 165 * Allocate a new queue entry. If we can't, or hit the zone limit 166 * just drop the pkt. 167 */ 168 te = uma_zalloc(tcp_reass_zone, M_NOWAIT); 169 if (te == NULL) { 170 tcpstat.tcps_rcvmemdrop++; 171 m_freem(m); 172 *tlenp = 0; 173 return (0); 174 } 175 tp->t_segqlen++; 176 tcp_reass_qsize++; 177 178 /* 179 * Find a segment which begins after this one does. 180 */ 181 LIST_FOREACH(q, &tp->t_segq, tqe_q) { 182 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 183 break; 184 p = q; 185 } 186 187 /* 188 * If there is a preceding segment, it may provide some of 189 * our data already. If so, drop the data from the incoming 190 * segment. If it provides all of our data, drop us. 191 */ 192 if (p != NULL) { 193 int i; 194 /* conversion to int (in i) handles seq wraparound */ 195 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 196 if (i > 0) { 197 if (i >= *tlenp) { 198 tcpstat.tcps_rcvduppack++; 199 tcpstat.tcps_rcvdupbyte += *tlenp; 200 m_freem(m); 201 uma_zfree(tcp_reass_zone, te); 202 tp->t_segqlen--; 203 tcp_reass_qsize--; 204 /* 205 * Try to present any queued data 206 * at the left window edge to the user. 207 * This is needed after the 3-WHS 208 * completes. 209 */ 210 goto present; /* ??? */ 211 } 212 m_adj(m, i); 213 *tlenp -= i; 214 th->th_seq += i; 215 } 216 } 217 tcpstat.tcps_rcvoopack++; 218 tcpstat.tcps_rcvoobyte += *tlenp; 219 220 /* 221 * While we overlap succeeding segments trim them or, 222 * if they are completely covered, dequeue them. 223 */ 224 while (q) { 225 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 226 if (i <= 0) 227 break; 228 if (i < q->tqe_len) { 229 q->tqe_th->th_seq += i; 230 q->tqe_len -= i; 231 m_adj(q->tqe_m, i); 232 break; 233 } 234 235 nq = LIST_NEXT(q, tqe_q); 236 LIST_REMOVE(q, tqe_q); 237 m_freem(q->tqe_m); 238 uma_zfree(tcp_reass_zone, q); 239 tp->t_segqlen--; 240 tcp_reass_qsize--; 241 q = nq; 242 } 243 244 /* Insert the new segment queue entry into place. */ 245 te->tqe_m = m; 246 te->tqe_th = th; 247 te->tqe_len = *tlenp; 248 249 if (p == NULL) { 250 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 251 } else { 252 LIST_INSERT_AFTER(p, te, tqe_q); 253 } 254 255 present: 256 /* 257 * Present data to user, advancing rcv_nxt through 258 * completed sequence space. 259 */ 260 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 261 return (0); 262 q = LIST_FIRST(&tp->t_segq); 263 if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 264 return (0); 265 SOCKBUF_LOCK(&so->so_rcv); 266 do { 267 tp->rcv_nxt += q->tqe_len; 268 flags = q->tqe_th->th_flags & TH_FIN; 269 nq = LIST_NEXT(q, tqe_q); 270 LIST_REMOVE(q, tqe_q); 271 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 272 m_freem(q->tqe_m); 273 else 274 sbappendstream_locked(&so->so_rcv, q->tqe_m); 275 uma_zfree(tcp_reass_zone, q); 276 tp->t_segqlen--; 277 tcp_reass_qsize--; 278 q = nq; 279 } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 280 ND6_HINT(tp); 281 sorwakeup_locked(so); 282 return (flags); 283 } 284