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 "../tcplp.h"
33 #include "../lib/bitmap.h"
34 #include "../lib/cbuf.h"
35 #include "tcp.h"
36 #include "tcp_fsm.h"
37 #include "tcp_seq.h"
38 #include "tcp_var.h"
39
40 /*
41 * samkumar: Segments are only reassembled within the window; data outside the
42 * window is thrown away. So, the total amount of reassembly data cannot exceed
43 * the size of the receive window.
44 *
45 * I have essentially rewritten it to use TCPlp's data structure for the
46 * reassembly buffer. I have kept the original code as a comment below this
47 * function, for reference.
48 *
49 * Looking at the usage of this function in tcp_input, this just has to set
50 * *tlenp to 0 if the received segment is already completely buffered; it does
51 * not need to update it if only part of the segment is trimmed off.
52 */
53 int
tcp_reass(struct tcpcb * tp,struct tcphdr * th,int * tlenp,otMessage * data,off_t data_offset,struct tcplp_signals * sig)54 tcp_reass(struct tcpcb* tp, struct tcphdr* th, int* tlenp, otMessage* data, off_t data_offset, struct tcplp_signals* sig)
55 {
56 size_t mergeable, written;
57 size_t offset;
58 size_t start_index;
59 size_t usedbefore;
60 int tlen;
61 size_t merged = 0;
62 int flags = 0;
63
64 /*
65 * Call with th==NULL after become established to
66 * force pre-ESTABLISHED data up to user socket.
67 */
68 if (th == NULL)
69 goto present;
70
71 tlen = *tlenp;
72
73 /* Insert the new segment queue entry into place. */
74 KASSERT(SEQ_GEQ(th->th_seq, tp->rcv_nxt), ("Adding past segment to the reassembly queue"));
75 offset = (size_t) (th->th_seq - tp->rcv_nxt);
76
77 if (cbuf_reass_count_set(&tp->recvbuf, (size_t) offset, tp->reassbmp, tlen) >= (size_t) tlen) {
78 *tlenp = 0;
79 goto present;
80 }
81 written = cbuf_reass_write(&tp->recvbuf, (size_t) offset, data, data_offset, tlen, tp->reassbmp, &start_index, cbuf_copy_from_message);
82
83 if ((th->th_flags & TH_FIN) && (tp->reass_fin_index == -1)) {
84 tp->reass_fin_index = (int16_t) (start_index + tlen);
85 }
86 KASSERT(written == tlen, ("Reassembly write out of bounds: tried to write %d, but wrote %d", tlen, (int) written));
87
88 present:
89 /*
90 * Present data to user, advancing rcv_nxt through
91 * completed sequence space.
92 */
93 mergeable = cbuf_reass_count_set(&tp->recvbuf, 0, tp->reassbmp, (size_t) 0xFFFFFFFF);
94 usedbefore = cbuf_used_space(&tp->recvbuf);
95 if (!tpiscantrcv(tp) || usedbefore == 0) {
96 /* If usedbefore == 0, but we can't receive more, then we still need to move the buffer
97 along by merging and then popping, in case we receive a FIN later on. */
98 if (tp->reass_fin_index >= 0 && cbuf_reass_within_offset(&tp->recvbuf, mergeable, (size_t) tp->reass_fin_index)) {
99 tp->reass_fin_index = -2; // So we won't consider any more FINs
100 flags = TH_FIN;
101 }
102 merged = cbuf_reass_merge(&tp->recvbuf, mergeable, tp->reassbmp);
103 KASSERT(merged == mergeable, ("Reassembly merge out of bounds: tried to merge %d, but merged %d", (int) mergeable, (int) merged));
104 if (tpiscantrcv(tp)) {
105 cbuf_pop(&tp->recvbuf, merged); // So no data really enters the buffer
106 } else if (merged > 0) {
107 sig->recvbuf_added = true;
108 }
109 } else {
110 /* If there is data in the buffer AND we can't receive more, then that must be because we received a FIN,
111 but the user hasn't yet emptied the buffer of its contents. */
112 KASSERT (tp->reass_fin_index == -2, ("Can't receive more, and data in buffer, but haven't received a FIN"));
113 }
114
115 tp->rcv_nxt += mergeable;
116
117 return flags;
118 }
119 #if 0
120 int
121 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
122 {
123 struct tseg_qent *q;
124 struct tseg_qent *p = NULL;
125 struct tseg_qent *nq;
126 struct tseg_qent *te = NULL;
127 struct socket *so = tp->t_inpcb->inp_socket;
128 char *s = NULL;
129 int flags;
130 struct tseg_qent tqs;
131
132 INP_WLOCK_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).
137 */
138
139 /*
140 * Call with th==NULL after become established to
141 * force pre-ESTABLISHED data up to user socket.
142 */
143 if (th == NULL)
144 goto present;
145
146 /*
147 * Limit the number of segments that can be queued to reduce the
148 * potential for mbuf exhaustion. For best performance, we want to be
149 * able to queue a full window's worth of segments. The size of the
150 * socket receive buffer determines our advertised window and grows
151 * automatically when socket buffer autotuning is enabled. Use it as the
152 * basis for our queue limit.
153 * Always let the missing segment through which caused this queue.
154 * NB: Access to the socket buffer is left intentionally unlocked as we
155 * can tolerate stale information here.
156 *
157 * XXXLAS: Using sbspace(so->so_rcv) instead of so->so_rcv.sb_hiwat
158 * should work but causes packets to be dropped when they shouldn't.
159 * Investigate why and re-evaluate the below limit after the behaviour
160 * is understood.
161 */
162 if ((th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) &&
163 tp->t_segqlen >= (so->so_rcv.sb_hiwat / tp->t_maxseg) + 1) {
164 TCPSTAT_INC(tcps_rcvreassfull);
165 *tlenp = 0;
166 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) {
167 log(LOG_DEBUG, "%s; %s: queue limit reached, "
168 "segment dropped\n", s, __func__);
169 free(s, M_TCPLOG);
170 }
171 m_freem(m);
172 return (0);
173 }
174
175 /*
176 * Allocate a new queue entry. If we can't, or hit the zone limit
177 * just drop the pkt.
178 *
179 * Use a temporary structure on the stack for the missing segment
180 * when the zone is exhausted. Otherwise we may get stuck.
181 */
182 te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
183 if (te == NULL) {
184 if (th->th_seq != tp->rcv_nxt || !TCPS_HAVEESTABLISHED(tp->t_state)) {
185 TCPSTAT_INC(tcps_rcvmemdrop);
186 m_freem(m);
187 *tlenp = 0;
188 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
189 NULL))) {
190 log(LOG_DEBUG, "%s; %s: global zone limit "
191 "reached, segment dropped\n", s, __func__);
192 free(s, M_TCPLOG);
193 }
194 return (0);
195 } else {
196 bzero(&tqs, sizeof(struct tseg_qent));
197 te = &tqs;
198 if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL,
199 NULL))) {
200 log(LOG_DEBUG,
201 "%s; %s: global zone limit reached, using "
202 "stack for missing segment\n", s, __func__);
203 free(s, M_TCPLOG);
204 }
205 }
206 }
207 tp->t_segqlen++;
208
209 /*
210 * Find a segment which begins after this one does.
211 */
212 LIST_FOREACH(q, &tp->t_segq, tqe_q) {
213 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
214 break;
215 p = q;
216 }
217
218 /*
219 * If there is a preceding segment, it may provide some of
220 * our data already. If so, drop the data from the incoming
221 * segment. If it provides all of our data, drop us.
222 */
223 if (p != NULL) {
224 int i;
225 /* conversion to int (in i) handles seq wraparound */
226 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
227 if (i > 0) {
228 if (i >= *tlenp) {
229 TCPSTAT_INC(tcps_rcvduppack);
230 TCPSTAT_ADD(tcps_rcvdupbyte, *tlenp);
231 m_freem(m);
232 if (te != &tqs)
233 uma_zfree(tcp_reass_zone, te);
234 tp->t_segqlen--;
235 /*
236 * Try to present any queued data
237 * at the left window edge to the user.
238 * This is needed after the 3-WHS
239 * completes.
240 */
241 goto present; /* ??? */
242 }
243 m_adj(m, i);
244 *tlenp -= i;
245 th->th_seq += i;
246 }
247 }
248 tp->t_rcvoopack++;
249 TCPSTAT_INC(tcps_rcvoopack);
250 TCPSTAT_ADD(tcps_rcvoobyte, *tlenp);
251
252 /*
253 * While we overlap succeeding segments trim them or,
254 * if they are completely covered, dequeue them.
255 */
256 while (q) {
257 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
258 if (i <= 0)
259 break;
260 if (i < q->tqe_len) {
261 q->tqe_th->th_seq += i;
262 q->tqe_len -= i;
263 m_adj(q->tqe_m, i);
264 break;
265 }
266
267 nq = LIST_NEXT(q, tqe_q);
268 LIST_REMOVE(q, tqe_q);
269 m_freem(q->tqe_m);
270 uma_zfree(tcp_reass_zone, q);
271 tp->t_segqlen--;
272 q = nq;
273 }
274
275 /* Insert the new segment queue entry into place. */
276 te->tqe_m = m;
277 te->tqe_th = th;
278 te->tqe_len = *tlenp;
279
280 if (p == NULL) {
281 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
282 } else {
283 KASSERT(te != &tqs, ("%s: temporary stack based entry not "
284 "first element in queue", __func__));
285 LIST_INSERT_AFTER(p, te, tqe_q);
286 }
287
288 present:
289 /*
290 * Present data to user, advancing rcv_nxt through
291 * completed sequence space.
292 */
293 if (!TCPS_HAVEESTABLISHED(tp->t_state))
294 return (0);
295 q = LIST_FIRST(&tp->t_segq);
296 if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
297 return (0);
298 SOCKBUF_LOCK(&so->so_rcv);
299 do {
300 tp->rcv_nxt += q->tqe_len;
301 flags = q->tqe_th->th_flags & TH_FIN;
302 nq = LIST_NEXT(q, tqe_q);
303 LIST_REMOVE(q, tqe_q);
304 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
305 m_freem(q->tqe_m);
306 else
307 sbappendstream_locked(&so->so_rcv, q->tqe_m, 0);
308 if (q != &tqs)
309 uma_zfree(tcp_reass_zone, q);
310 tp->t_segqlen--;
311 q = nq;
312 } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
313 sorwakeup_locked(so);
314 return (flags);
315 }
316 #endif
317