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 49 #include <vm/uma.h> 50 51 #include <net/if.h> 52 #include <net/if_var.h> 53 #include <net/route.h> 54 #include <net/vnet.h> 55 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 #include <netinet6/tcp6_var.h> 73 #include <netinet/tcpip.h> 74 #ifdef TCPDEBUG 75 #include <netinet/tcp_debug.h> 76 #endif /* TCPDEBUG */ 77 78 static int tcp_reass_sysctl_qsize(SYSCTL_HANDLER_ARGS); 79 80 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0, 81 "TCP Segment Reassembly Queue"); 82 83 static VNET_DEFINE(int, tcp_reass_maxseg) = 0; 84 #define V_tcp_reass_maxseg VNET(tcp_reass_maxseg) 85 SYSCTL_VNET_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN, 86 &VNET_NAME(tcp_reass_maxseg), 0, 87 "Global maximum number of TCP Segments in Reassembly Queue"); 88 89 SYSCTL_VNET_PROC(_net_inet_tcp_reass, OID_AUTO, cursegments, 90 (CTLTYPE_INT | CTLFLAG_RD), NULL, 0, &tcp_reass_sysctl_qsize, "I", 91 "Global number of TCP Segments currently in Reassembly Queue"); 92 93 static VNET_DEFINE(int, tcp_reass_overflows) = 0; 94 #define V_tcp_reass_overflows VNET(tcp_reass_overflows) 95 SYSCTL_VNET_INT(_net_inet_tcp_reass, OID_AUTO, overflows, 96 CTLTYPE_INT | CTLFLAG_RD, 97 &VNET_NAME(tcp_reass_overflows), 0, 98 "Global number of TCP Segment Reassembly Queue Overflows"); 99 100 static VNET_DEFINE(uma_zone_t, tcp_reass_zone); 101 #define V_tcp_reass_zone VNET(tcp_reass_zone) 102 103 /* Initialize TCP reassembly queue */ 104 static void 105 tcp_reass_zone_change(void *tag) 106 { 107 108 /* Set the zone limit and read back the effective value. */ 109 V_tcp_reass_maxseg = nmbclusters / 16; 110 V_tcp_reass_maxseg = uma_zone_set_max(V_tcp_reass_zone, 111 V_tcp_reass_maxseg); 112 } 113 114 void 115 tcp_reass_init(void) 116 { 117 118 V_tcp_reass_maxseg = nmbclusters / 16; 119 TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", 120 &V_tcp_reass_maxseg); 121 V_tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent), 122 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 123 /* Set the zone limit and read back the effective value. */ 124 V_tcp_reass_maxseg = uma_zone_set_max(V_tcp_reass_zone, 125 V_tcp_reass_maxseg); 126 EVENTHANDLER_REGISTER(nmbclusters_change, 127 tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY); 128 } 129 130 #ifdef VIMAGE 131 void 132 tcp_reass_destroy(void) 133 { 134 135 uma_zdestroy(V_tcp_reass_zone); 136 } 137 #endif 138 139 void 140 tcp_reass_flush(struct tcpcb *tp) 141 { 142 struct tseg_qent *qe; 143 144 INP_WLOCK_ASSERT(tp->t_inpcb); 145 146 while ((qe = LIST_FIRST(&tp->t_segq)) != NULL) { 147 LIST_REMOVE(qe, tqe_q); 148 m_freem(qe->tqe_m); 149 uma_zfree(V_tcp_reass_zone, qe); 150 tp->t_segqlen--; 151 } 152 153 KASSERT((tp->t_segqlen == 0), 154 ("TCP reass queue %p segment count is %d instead of 0 after flush.", 155 tp, tp->t_segqlen)); 156 } 157 158 static int 159 tcp_reass_sysctl_qsize(SYSCTL_HANDLER_ARGS) 160 { 161 int qsize; 162 163 qsize = uma_zone_get_cur(V_tcp_reass_zone); 164 return (sysctl_handle_int(oidp, &qsize, 0, req)); 165 } 166 167 int 168 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m) 169 { 170 struct tseg_qent *q; 171 struct tseg_qent *p = NULL; 172 struct tseg_qent *nq; 173 struct tseg_qent *te = NULL; 174 struct socket *so = tp->t_inpcb->inp_socket; 175 char *s = NULL; 176 int flags; 177 struct tseg_qent tqs; 178 179 INP_WLOCK_ASSERT(tp->t_inpcb); 180 181 /* 182 * XXX: tcp_reass() is rather inefficient with its data structures 183 * and should be rewritten (see NetBSD for optimizations). 184 */ 185 186 /* 187 * Call with th==NULL after become established to 188 * force pre-ESTABLISHED data up to user socket. 189 */ 190 if (th == NULL) 191 goto present; 192 193 /* 194 * Limit the number of segments that can be queued to reduce the 195 * potential for mbuf exhaustion. For best performance, we want to be 196 * able to queue a full window's worth of segments. The size of the 197 * socket receive buffer determines our advertised window and grows 198 * automatically when socket buffer autotuning is enabled. Use it as the 199 * basis for our queue limit. 200 * Always let the missing segment through which caused this queue. 201 * NB: Access to the socket buffer is left intentionally unlocked as we 202 * can tolerate stale information here. 203 * 204 * XXXLAS: Using sbspace(so->so_rcv) instead of so->so_rcv.sb_hiwat 205 * should work but causes packets to be dropped when they shouldn't. 206 * Investigate why and re-evaluate the below limit after the behaviour 207 * is understood. 208 */ 209 if (th->th_seq != tp->rcv_nxt && 210 tp->t_segqlen >= (so->so_rcv.sb_hiwat / tp->t_maxseg) + 1) { 211 V_tcp_reass_overflows++; 212 TCPSTAT_INC(tcps_rcvmemdrop); 213 m_freem(m); 214 *tlenp = 0; 215 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 216 log(LOG_DEBUG, "%s; %s: queue limit reached, " 217 "segment dropped\n", s, __func__); 218 free(s, M_TCPLOG); 219 } 220 return (0); 221 } 222 223 /* 224 * Allocate a new queue entry. If we can't, or hit the zone limit 225 * just drop the pkt. 226 * 227 * Use a temporary structure on the stack for the missing segment 228 * when the zone is exhausted. Otherwise we may get stuck. 229 */ 230 te = uma_zalloc(V_tcp_reass_zone, M_NOWAIT); 231 if (te == NULL) { 232 if (th->th_seq != tp->rcv_nxt) { 233 TCPSTAT_INC(tcps_rcvmemdrop); 234 m_freem(m); 235 *tlenp = 0; 236 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, 237 NULL))) { 238 log(LOG_DEBUG, "%s; %s: global zone limit " 239 "reached, segment dropped\n", s, __func__); 240 free(s, M_TCPLOG); 241 } 242 return (0); 243 } else { 244 bzero(&tqs, sizeof(struct tseg_qent)); 245 te = &tqs; 246 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, 247 NULL))) { 248 log(LOG_DEBUG, 249 "%s; %s: global zone limit reached, using " 250 "stack for missing segment\n", s, __func__); 251 free(s, M_TCPLOG); 252 } 253 } 254 } 255 tp->t_segqlen++; 256 257 /* 258 * Find a segment which begins after this one does. 259 */ 260 LIST_FOREACH(q, &tp->t_segq, tqe_q) { 261 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) 262 break; 263 p = q; 264 } 265 266 /* 267 * If there is a preceding segment, it may provide some of 268 * our data already. If so, drop the data from the incoming 269 * segment. If it provides all of our data, drop us. 270 */ 271 if (p != NULL) { 272 int i; 273 /* conversion to int (in i) handles seq wraparound */ 274 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; 275 if (i > 0) { 276 if (i >= *tlenp) { 277 TCPSTAT_INC(tcps_rcvduppack); 278 TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp); 279 m_freem(m); 280 uma_zfree(V_tcp_reass_zone, te); 281 tp->t_segqlen--; 282 /* 283 * Try to present any queued data 284 * at the left window edge to the user. 285 * This is needed after the 3-WHS 286 * completes. 287 */ 288 goto present; /* ??? */ 289 } 290 m_adj(m, i); 291 *tlenp -= i; 292 th->th_seq += i; 293 } 294 } 295 tp->t_rcvoopack++; 296 TCPSTAT_INC(tcps_rcvoopack); 297 TCPSTAT_ADD(tcps_rcvoobyte, *tlenp); 298 299 /* 300 * While we overlap succeeding segments trim them or, 301 * if they are completely covered, dequeue them. 302 */ 303 while (q) { 304 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; 305 if (i <= 0) 306 break; 307 if (i < q->tqe_len) { 308 q->tqe_th->th_seq += i; 309 q->tqe_len -= i; 310 m_adj(q->tqe_m, i); 311 break; 312 } 313 314 nq = LIST_NEXT(q, tqe_q); 315 LIST_REMOVE(q, tqe_q); 316 m_freem(q->tqe_m); 317 uma_zfree(V_tcp_reass_zone, q); 318 tp->t_segqlen--; 319 q = nq; 320 } 321 322 /* Insert the new segment queue entry into place. */ 323 te->tqe_m = m; 324 te->tqe_th = th; 325 te->tqe_len = *tlenp; 326 327 if (p == NULL) { 328 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); 329 } else { 330 KASSERT(te != &tqs, ("%s: temporary stack based entry not " 331 "first element in queue", __func__)); 332 LIST_INSERT_AFTER(p, te, tqe_q); 333 } 334 335 present: 336 /* 337 * Present data to user, advancing rcv_nxt through 338 * completed sequence space. 339 */ 340 if (!TCPS_HAVEESTABLISHED(tp->t_state)) 341 return (0); 342 q = LIST_FIRST(&tp->t_segq); 343 if (!q || q->tqe_th->th_seq != tp->rcv_nxt) 344 return (0); 345 SOCKBUF_LOCK(&so->so_rcv); 346 do { 347 tp->rcv_nxt += q->tqe_len; 348 flags = q->tqe_th->th_flags & TH_FIN; 349 nq = LIST_NEXT(q, tqe_q); 350 LIST_REMOVE(q, tqe_q); 351 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 352 m_freem(q->tqe_m); 353 else 354 sbappendstream_locked(&so->so_rcv, q->tqe_m); 355 if (q != &tqs) 356 uma_zfree(V_tcp_reass_zone, q); 357 tp->t_segqlen--; 358 q = nq; 359 } while (q && q->tqe_th->th_seq == tp->rcv_nxt); 360 ND6_HINT(tp); 361 sorwakeup_locked(so); 362 return (flags); 363 } 364