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 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_inet.h" 36 #include "opt_inet6.h" 37 #include "opt_tcpdebug.h" 38 39 #include <sys/param.h> 40 #include <sys/kernel.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 #include <sys/sysctl.h> 46 #include <sys/syslog.h> 47 #include <sys/systm.h> 48 #include <sys/vimage.h> 49 50 #include <vm/uma.h> 51 52 #include <net/if.h> 53 #include <net/route.h> 54 55 #include <netinet/in.h> 56 #include <netinet/in_pcb.h> 57 #include <netinet/in_systm.h> 58 #include <netinet/in_var.h> 59 #include <netinet/ip.h> 60 #include <netinet/ip_var.h> 61 #include <netinet/ip_options.h> 62 #include <netinet/ip6.h> 63 #include <netinet6/in6_pcb.h> 64 #include <netinet6/ip6_var.h> 65 #include <netinet6/nd6.h> 66 #include <netinet/tcp.h> 67 #include <netinet/tcp_fsm.h> 68 #include <netinet/tcp_seq.h> 69 #include <netinet/tcp_timer.h> 70 #include <netinet/tcp_var.h> 71 #include <netinet6/tcp6_var.h> 72 #include <netinet/tcpip.h> 73 #ifdef TCPDEBUG 74 #include <netinet/tcp_debug.h> 75 #endif /* TCPDEBUG */ 76 77 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0, 78 "TCP Segment Reassembly Queue"); 79 80 static int tcp_reass_maxseg = 0; 81 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_reass, OID_AUTO, maxsegments, 82 CTLFLAG_RDTUN, tcp_reass_maxseg, 0, 83 "Global maximum number of TCP Segments in Reassembly Queue"); 84 85 int tcp_reass_qsize = 0; 86 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_reass, OID_AUTO, cursegments, 87 CTLFLAG_RD, tcp_reass_qsize, 0, 88 "Global number of TCP Segments currently in Reassembly Queue"); 89 90 static int tcp_reass_maxqlen = 48; 91 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_reass, OID_AUTO, maxqlen, 92 CTLFLAG_RW, tcp_reass_maxqlen, 0, 93 "Maximum number of TCP Segments per individual Reassembly Queue"); 94 95 static int tcp_reass_overflows = 0; 96 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_reass, OID_AUTO, overflows, 97 CTLFLAG_RD, tcp_reass_overflows, 0, 98 "Global number of TCP Segment Reassembly Queue Overflows"); 99 100 /* Initialize TCP reassembly queue */ 101 static void 102 tcp_reass_zone_change(void *tag) 103 { 104 INIT_VNET_INET(curvnet); 105 106 V_tcp_reass_maxseg = nmbclusters / 16; 107 uma_zone_set_max(tcp_reass_zone, V_tcp_reass_maxseg); 108 } 109 110 uma_zone_t tcp_reass_zone; 111 112 void 113 tcp_reass_init(void) 114 { 115 INIT_VNET_INET(curvnet); 116 117 V_tcp_reass_maxseg = nmbclusters / 16; 118 TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", 119 &V_tcp_reass_maxseg); 120 tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent), 121 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 122 uma_zone_set_max(tcp_reass_zone, V_tcp_reass_maxseg); 123 EVENTHANDLER_REGISTER(nmbclusters_change, 124 tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY); 125 } 126 127 int 128 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m) 129 { 130 INIT_VNET_INET(curvnet); 131 struct tseg_qent *q; 132 struct tseg_qent *p = NULL; 133 struct tseg_qent *nq; 134 struct tseg_qent *te = NULL; 135 struct socket *so = tp->t_inpcb->inp_socket; 136 int flags; 137 138 INP_WLOCK_ASSERT(tp->t_inpcb); 139 140 /* 141 * XXX: tcp_reass() is rather inefficient with its data structures 142 * and should be rewritten (see NetBSD for optimizations). While 143 * doing that it should move to its own file tcp_reass.c. 144 */ 145 146 /* 147 * Call with th==NULL after become established to 148 * force pre-ESTABLISHED data up to user socket. 149 */ 150 if (th == NULL) 151 goto present; 152 153 /* 154 * Limit the number of segments in the reassembly queue to prevent 155 * holding on to too many segments (and thus running out of mbufs). 156 * Make sure to let the missing segment through which caused this 157 * queue. Always keep one global queue entry spare to be able to 158 * process the missing segment. 159 */ 160 if (th->th_seq != tp->rcv_nxt && 161 (V_tcp_reass_qsize + 1 >= V_tcp_reass_maxseg || 162 tp->t_segqlen >= V_tcp_reass_maxqlen)) { 163 V_tcp_reass_overflows++; 164 V_tcpstat.tcps_rcvmemdrop++; 165 m_freem(m); 166 *tlenp = 0; 167 return (0); 168 } 169 170 /* 171 * Allocate a new queue entry. If we can't, or hit the zone limit 172 * just drop the pkt. 173 */ 174 te = uma_zalloc(tcp_reass_zone, M_NOWAIT); 175 if (te == NULL) { 176 V_tcpstat.tcps_rcvmemdrop++; 177 m_freem(m); 178 *tlenp = 0; 179 return (0); 180 } 181 tp->t_segqlen++; 182 V_tcp_reass_qsize++; 183 184 /* 185 * Find a segment which begins after this one does. 186 */ 187 LIST_FOREACH(q, &tp->t_segq, tqe_q) { 188 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 189 break; 190 p = q; 191 } 192 193 /* 194 * If there is a preceding segment, it may provide some of 195 * our data already. If so, drop the data from the incoming 196 * segment. If it provides all of our data, drop us. 197 */ 198 if (p != NULL) { 199 int i; 200 /* conversion to int (in i) handles seq wraparound */ 201 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 202 if (i > 0) { 203 if (i >= *tlenp) { 204 V_tcpstat.tcps_rcvduppack++; 205 V_tcpstat.tcps_rcvdupbyte += *tlenp; 206 m_freem(m); 207 uma_zfree(tcp_reass_zone, te); 208 tp->t_segqlen--; 209 V_tcp_reass_qsize--; 210 /* 211 * Try to present any queued data 212 * at the left window edge to the user. 213 * This is needed after the 3-WHS 214 * completes. 215 */ 216 goto present; /* ??? */ 217 } 218 m_adj(m, i); 219 *tlenp -= i; 220 th->th_seq += i; 221 } 222 } 223 V_tcpstat.tcps_rcvoopack++; 224 V_tcpstat.tcps_rcvoobyte += *tlenp; 225 226 /* 227 * While we overlap succeeding segments trim them or, 228 * if they are completely covered, dequeue them. 229 */ 230 while (q) { 231 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 232 if (i <= 0) 233 break; 234 if (i < q->tqe_len) { 235 q->tqe_th->th_seq += i; 236 q->tqe_len -= i; 237 m_adj(q->tqe_m, i); 238 break; 239 } 240 241 nq = LIST_NEXT(q, tqe_q); 242 LIST_REMOVE(q, tqe_q); 243 m_freem(q->tqe_m); 244 uma_zfree(tcp_reass_zone, q); 245 tp->t_segqlen--; 246 V_tcp_reass_qsize--; 247 q = nq; 248 } 249 250 /* Insert the new segment queue entry into place. */ 251 te->tqe_m = m; 252 te->tqe_th = th; 253 te->tqe_len = *tlenp; 254 255 if (p == NULL) { 256 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 257 } else { 258 LIST_INSERT_AFTER(p, te, tqe_q); 259 } 260 261 present: 262 /* 263 * Present data to user, advancing rcv_nxt through 264 * completed sequence space. 265 */ 266 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 267 return (0); 268 q = LIST_FIRST(&tp->t_segq); 269 if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 270 return (0); 271 SOCKBUF_LOCK(&so->so_rcv); 272 do { 273 tp->rcv_nxt += q->tqe_len; 274 flags = q->tqe_th->th_flags & TH_FIN; 275 nq = LIST_NEXT(q, tqe_q); 276 LIST_REMOVE(q, tqe_q); 277 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 278 m_freem(q->tqe_m); 279 else 280 sbappendstream_locked(&so->so_rcv, q->tqe_m); 281 uma_zfree(tcp_reass_zone, q); 282 tp->t_segqlen--; 283 V_tcp_reass_qsize--; 284 q = nq; 285 } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 286 ND6_HINT(tp); 287 sorwakeup_locked(so); 288 return (flags); 289 } 290