blob: 6fc23905c72931f973c680fee0d47a73292aa03f [file] [log] [blame]
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
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.
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070013 * 3. Neither the name of the University nor the names of its contributors
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080014 * 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.5 (Berkeley) 4/10/94
30 * tcp_input.c,v 1.10 1994/10/13 18:36:32 wollman Exp
31 */
32
33/*
34 * Changes and additions relating to SLiRP
35 * Copyright (c) 1995 Danny Gasparovski.
36 *
37 * Please read the file COPYRIGHT for the
38 * terms and conditions of the copyright.
39 */
40
41#include <slirp.h>
42#include "ip_icmp.h"
43
44struct socket tcb;
45
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070046#define TCPREXMTTHRESH 3
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080047struct socket *tcp_last_so = &tcb;
48
49tcp_seq tcp_iss; /* tcp initial send seq # */
50
51#define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
52
53/* for modulo comparisons of timestamps */
54#define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
55#define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
56
57/*
58 * Insert segment ti into reassembly queue of tcp with
59 * control block tp. Return TH_FIN if reassembly now includes
60 * a segment with FIN. The macro form does the common case inline
61 * (segment is the next to be received on an established connection,
62 * and the queue is empty), avoiding linkage into and removal
63 * from the queue and repetition of various conversions.
64 * Set DELACK for segments received in order, but ack immediately
65 * when segments are out of order (so fast retransmit can work).
66 */
67#ifdef TCP_ACK_HACK
68#define TCP_REASS(tp, ti, m, so, flags) {\
69 if ((ti)->ti_seq == (tp)->rcv_nxt && \
70 tcpfrag_list_empty(tp) && \
71 (tp)->t_state == TCPS_ESTABLISHED) {\
72 if (ti->ti_flags & TH_PUSH) \
73 tp->t_flags |= TF_ACKNOW; \
74 else \
75 tp->t_flags |= TF_DELACK; \
76 (tp)->rcv_nxt += (ti)->ti_len; \
77 flags = (ti)->ti_flags & TH_FIN; \
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070078 STAT(tcpstat.tcps_rcvpack++); \
79 STAT(tcpstat.tcps_rcvbyte += (ti)->ti_len); \
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080080 if (so->so_emu) { \
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070081 if (tcp_emu((so),(m))) sbappend((so), (m)); \
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080082 } else \
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070083 sbappend((so), (m)); \
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080084/* sorwakeup(so); */ \
85 } else {\
86 (flags) = tcp_reass((tp), (ti), (m)); \
87 tp->t_flags |= TF_ACKNOW; \
88 } \
89}
90#else
91#define TCP_REASS(tp, ti, m, so, flags) { \
92 if ((ti)->ti_seq == (tp)->rcv_nxt && \
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070093 tcpfrag_list_empty(tp) && \
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080094 (tp)->t_state == TCPS_ESTABLISHED) { \
95 tp->t_flags |= TF_DELACK; \
96 (tp)->rcv_nxt += (ti)->ti_len; \
97 flags = (ti)->ti_flags & TH_FIN; \
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070098 STAT(tcpstat.tcps_rcvpack++); \
99 STAT(tcpstat.tcps_rcvbyte += (ti)->ti_len); \
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800100 if (so->so_emu) { \
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700101 if (tcp_emu((so),(m))) sbappend(so, (m)); \
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800102 } else \
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700103 sbappend((so), (m)); \
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800104/* sorwakeup(so); */ \
105 } else { \
106 (flags) = tcp_reass((tp), (ti), (m)); \
107 tp->t_flags |= TF_ACKNOW; \
108 } \
109}
110#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700111static void tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt,
112 struct tcpiphdr *ti);
113static void tcp_xmit_timer(register struct tcpcb *tp, int rtt);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800114
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700115static int
116tcp_reass(register struct tcpcb *tp, register struct tcpiphdr *ti,
117 struct mbuf *m)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800118{
119 register struct tcpiphdr *q;
120 struct socket *so = tp->t_socket;
121 int flags;
122
123 /*
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700124 * Call with ti==NULL after become established to
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800125 * force pre-ESTABLISHED data up to user socket.
126 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700127 if (ti == NULL)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800128 goto present;
129
130 /*
131 * Find a segment which begins after this one does.
132 */
133 for (q = tcpfrag_list_first(tp); !tcpfrag_list_end(q, tp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700134 q = tcpiphdr_next(q))
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800135 if (SEQ_GT(q->ti_seq, ti->ti_seq))
136 break;
137
138 /*
139 * If there is a preceding segment, it may provide some of
140 * our data already. If so, drop the data from the incoming
141 * segment. If it provides all of our data, drop us.
142 */
143 if (!tcpfrag_list_end(tcpiphdr_prev(q), tp)) {
144 register int i;
145 q = tcpiphdr_prev(q);
146 /* conversion to int (in i) handles seq wraparound */
147 i = q->ti_seq + q->ti_len - ti->ti_seq;
148 if (i > 0) {
149 if (i >= ti->ti_len) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700150 STAT(tcpstat.tcps_rcvduppack++);
151 STAT(tcpstat.tcps_rcvdupbyte += ti->ti_len);
152 m_freem(m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800153 /*
154 * Try to present any queued data
155 * at the left window edge to the user.
156 * This is needed after the 3-WHS
157 * completes.
158 */
159 goto present; /* ??? */
160 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700161 m_adj(m, i);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800162 ti->ti_len -= i;
163 ti->ti_seq += i;
164 }
165 q = tcpiphdr_next(q);
166 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700167 STAT(tcpstat.tcps_rcvoopack++);
168 STAT(tcpstat.tcps_rcvoobyte += ti->ti_len);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800169 ti->ti_mbuf = m;
170
171 /*
172 * While we overlap succeeding segments trim them or,
173 * if they are completely covered, dequeue them.
174 */
175 while (!tcpfrag_list_end(q, tp)) {
176 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
177 if (i <= 0)
178 break;
179 if (i < q->ti_len) {
180 q->ti_seq += i;
181 q->ti_len -= i;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700182 m_adj(q->ti_mbuf, i);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800183 break;
184 }
185 q = tcpiphdr_next(q);
186 m = tcpiphdr_prev(q)->ti_mbuf;
187 remque(tcpiphdr2qlink(tcpiphdr_prev(q)));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700188 m_freem(m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800189 }
190
191 /*
192 * Stick new segment in its place.
193 */
194 insque(tcpiphdr2qlink(ti), tcpiphdr2qlink(tcpiphdr_prev(q)));
195
196present:
197 /*
198 * Present data to user, advancing rcv_nxt through
199 * completed sequence space.
200 */
201 if (!TCPS_HAVEESTABLISHED(tp->t_state))
202 return (0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700203 ti = tcpfrag_list_first(tp);
204 if (tcpfrag_list_end(ti, tp) || ti->ti_seq != tp->rcv_nxt)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800205 return (0);
206 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
207 return (0);
208 do {
209 tp->rcv_nxt += ti->ti_len;
210 flags = ti->ti_flags & TH_FIN;
211 remque(tcpiphdr2qlink(ti));
212 m = ti->ti_mbuf;
213 ti = tcpiphdr_next(ti);
214/* if (so->so_state & SS_FCANTRCVMORE) */
215 if (so->so_state & SS_FCANTSENDMORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700216 m_freem(m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800217 else {
218 if (so->so_emu) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700219 if (tcp_emu(so,m)) sbappend(so, m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800220 } else
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700221 sbappend(so, m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800222 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700223 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800224/* sorwakeup(so); */
225 return (flags);
226}
227
228/*
229 * TCP input routine, follows pages 65-76 of the
230 * protocol specification dated September, 1981 very closely.
231 */
232void
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700233tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800234{
235 struct ip save_ip, *ip;
236 register struct tcpiphdr *ti;
237 caddr_t optp = NULL;
238 int optlen = 0;
239 int len, tlen, off;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700240 register struct tcpcb *tp = NULL;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800241 register int tiflags;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700242 struct socket *so = NULL;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800243 int todrop, acked, ourfinisacked, needoutput = 0;
244/* int dropsocket = 0; */
245 int iss = 0;
246 u_long tiwin;
247 int ret;
248/* int ts_present = 0; */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700249 struct ex_list *ex_ptr;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800250
251 DEBUG_CALL("tcp_input");
252 DEBUG_ARGS((dfd," m = %8lx iphlen = %2d inso = %lx\n",
253 (long )m, iphlen, (long )inso ));
254
255 /*
256 * If called with m == 0, then we're continuing the connect
257 */
258 if (m == NULL) {
259 so = inso;
260
261 /* Re-set a few variables */
262 tp = sototcpcb(so);
263 m = so->so_m;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700264 so->so_m = NULL;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800265 ti = so->so_ti;
266 tiwin = ti->ti_win;
267 tiflags = ti->ti_flags;
268
269 goto cont_conn;
270 }
271
272
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700273 STAT(tcpstat.tcps_rcvtotal++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800274 /*
275 * Get IP and TCP header together in first mbuf.
276 * Note: IP leaves IP header in first mbuf.
277 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700278 ti = mtod(m, struct tcpiphdr *);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800279 if (iphlen > sizeof(struct ip )) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700280 ip_stripoptions(m, (struct mbuf *)0);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800281 iphlen=sizeof(struct ip );
282 }
283 /* XXX Check if too short */
284
285
286 /*
287 * Save a copy of the IP header in case we want restore it
288 * for sending an ICMP error message in response.
289 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700290 ip=mtod(m, struct ip *);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800291 save_ip = *ip;
292 save_ip.ip_len+= iphlen;
293
294 /*
295 * Checksum extended TCP header and data.
296 */
297 tlen = ((struct ip *)ti)->ip_len;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700298 tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
299 memset(&ti->ti_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800300 ti->ti_x1 = 0;
301 ti->ti_len = htons((u_int16_t)tlen);
302 len = sizeof(struct ip ) + tlen;
303 /* keep checksum for ICMP reply
304 * ti->ti_sum = cksum(m, len);
305 * if (ti->ti_sum) { */
306 if(cksum(m, len)) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700307 STAT(tcpstat.tcps_rcvbadsum++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800308 goto drop;
309 }
310
311 /*
312 * Check that TCP offset makes sense,
313 * pull out TCP options and adjust length. XXX
314 */
315 off = ti->ti_off << 2;
316 if (off < sizeof (struct tcphdr) || off > tlen) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700317 STAT(tcpstat.tcps_rcvbadoff++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800318 goto drop;
319 }
320 tlen -= off;
321 ti->ti_len = tlen;
322 if (off > sizeof (struct tcphdr)) {
323 optlen = off - sizeof (struct tcphdr);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700324 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800325
326 /*
327 * Do quick retrieval of timestamp options ("options
328 * prediction?"). If timestamp is the only option and it's
329 * formatted as recommended in RFC 1323 appendix A, we
330 * quickly get the values now and not bother calling
331 * tcp_dooptions(), etc.
332 */
333/* if ((optlen == TCPOLEN_TSTAMP_APPA ||
334 * (optlen > TCPOLEN_TSTAMP_APPA &&
335 * optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
336 * *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
337 * (ti->ti_flags & TH_SYN) == 0) {
338 * ts_present = 1;
339 * ts_val = ntohl(*(u_int32_t *)(optp + 4));
340 * ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
341 * optp = NULL; / * we've parsed the options * /
342 * }
343 */
344 }
345 tiflags = ti->ti_flags;
346
347 /*
348 * Convert TCP protocol specific fields to host format.
349 */
350 NTOHL(ti->ti_seq);
351 NTOHL(ti->ti_ack);
352 NTOHS(ti->ti_win);
353 NTOHS(ti->ti_urp);
354
355 /*
356 * Drop TCP, IP headers and TCP options.
357 */
358 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
359 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
360
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700361 if (slirp_restrict) {
362 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
363 if (ex_ptr->ex_fport == port_geth(ti->ti_dport) &&
364 (ip_geth(ti->ti_dst) & 0xff) == ex_ptr->ex_addr)
365 break;
366
367 if (!ex_ptr)
368 goto drop;
369 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800370 /*
371 * Locate pcb for segment.
372 */
373findso:
374 so = tcp_last_so;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700375 {
376 uint32_t srcip = ip_geth(ti->ti_src);
377 uint32_t dstip = ip_geth(ti->ti_dst);
378 uint16_t dstport = port_geth(ti->ti_dport);
379 uint16_t srcport = port_geth(ti->ti_sport);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800380
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700381 if (so->so_faddr_port != dstport ||
382 so->so_laddr_port != srcport ||
383 so->so_laddr_ip != srcip ||
384 so->so_faddr_ip != dstip) {
385 so = solookup(&tcb, srcip, srcport, dstip, dstport);
386 if (so)
387 tcp_last_so = so;
388 STAT(tcpstat.tcps_socachemiss++);
389 }
390 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800391 /*
392 * If the state is CLOSED (i.e., TCB does not exist) then
393 * all data in the incoming segment is discarded.
394 * If the TCB exists but is in CLOSED state, it is embryonic,
395 * but should either do a listen or a connect soon.
396 *
397 * state == CLOSED means we've done socreate() but haven't
398 * attached it to a protocol yet...
399 *
400 * XXX If a TCB does not exist, and the TH_SYN flag is
401 * the only flag set, then create a session, mark it
402 * as if it was LISTENING, and continue...
403 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700404 if (so == NULL) {
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800405 if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
406 goto dropwithreset;
407
408 if ((so = socreate()) == NULL)
409 goto dropwithreset;
410 if (tcp_attach(so) < 0) {
411 free(so); /* Not sofree (if it failed, it's not insqued) */
412 goto dropwithreset;
413 }
414
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700415 sbreserve(&so->so_snd, TCP_SNDSPACE);
416 sbreserve(&so->so_rcv, TCP_RCVSPACE);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800417
418 /* tcp_last_so = so; */ /* XXX ? */
419 /* tp = sototcpcb(so); */
420
421 so->so_laddr_ip = ip_geth(ti->ti_src);
422 so->so_laddr_port = port_geth(ti->ti_sport);
423 so->so_faddr_ip = ip_geth(ti->ti_dst);
424 so->so_faddr_port = port_geth(ti->ti_dport);
425
426 if ((so->so_iptos = tcp_tos(so)) == 0)
427 so->so_iptos = ((struct ip *)ti)->ip_tos;
428
429 tp = sototcpcb(so);
430 tp->t_state = TCPS_LISTEN;
431 }
432
433 /*
434 * If this is a still-connecting socket, this probably
435 * a retransmit of the SYN. Whether it's a retransmit SYN
436 * or something else, we nuke it.
437 */
438 if (so->so_state & SS_ISFCONNECTING)
439 goto drop;
440
441 tp = sototcpcb(so);
442
443 /* XXX Should never fail */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700444 if (tp == NULL)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800445 goto dropwithreset;
446 if (tp->t_state == TCPS_CLOSED)
447 goto drop;
448
449 /* Unscale the window into a 32-bit value. */
450/* if ((tiflags & TH_SYN) == 0)
451 * tiwin = ti->ti_win << tp->snd_scale;
452 * else
453 */
454 tiwin = ti->ti_win;
455
456 /*
457 * Segment received on connection.
458 * Reset idle time and keep-alive timer.
459 */
460 tp->t_idle = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700461 if (SO_OPTIONS)
462 tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800463 else
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700464 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800465
466 /*
467 * Process options if not in LISTEN state,
468 * else do it below (after getting remote address).
469 */
470 if (optp && tp->t_state != TCPS_LISTEN)
471 tcp_dooptions(tp, (u_char *)optp, optlen, ti);
472/* , */
473/* &ts_present, &ts_val, &ts_ecr); */
474
475 /*
476 * Header prediction: check for the two common cases
477 * of a uni-directional data xfer. If the packet has
478 * no control flags, is in-sequence, the window didn't
479 * change and we're not retransmitting, it's a
480 * candidate. If the length is zero and the ack moved
481 * forward, we're the sender side of the xfer. Just
482 * free the data acked & wake any higher level process
483 * that was blocked waiting for space. If the length
484 * is non-zero and the ack didn't move, we're the
485 * receiver side. If we're getting packets in-order
486 * (the reassembly queue is empty), add the data to
487 * the socket buffer and note that we need a delayed ack.
488 *
489 * XXX Some of these tests are not needed
490 * eg: the tiwin == tp->snd_wnd prevents many more
491 * predictions.. with no *real* advantage..
492 */
493 if (tp->t_state == TCPS_ESTABLISHED &&
494 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
495/* (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && */
496 ti->ti_seq == tp->rcv_nxt &&
497 tiwin && tiwin == tp->snd_wnd &&
498 tp->snd_nxt == tp->snd_max) {
499 /*
500 * If last ACK falls within this segment's sequence numbers,
501 * record the timestamp.
502 */
503/* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
504 * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
505 * tp->ts_recent_age = tcp_now;
506 * tp->ts_recent = ts_val;
507 * }
508 */
509 if (ti->ti_len == 0) {
510 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
511 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
512 tp->snd_cwnd >= tp->snd_wnd) {
513 /*
514 * this is a pure ack for outstanding data.
515 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700516 STAT(tcpstat.tcps_predack++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800517/* if (ts_present)
518 * tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
519 * else
520 */ if (tp->t_rtt &&
521 SEQ_GT(ti->ti_ack, tp->t_rtseq))
522 tcp_xmit_timer(tp, tp->t_rtt);
523 acked = ti->ti_ack - tp->snd_una;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700524 STAT(tcpstat.tcps_rcvackpack++);
525 STAT(tcpstat.tcps_rcvackbyte += acked);
526 sbdrop(&so->so_snd, acked);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800527 tp->snd_una = ti->ti_ack;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700528 m_freem(m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800529
530 /*
531 * If all outstanding data are acked, stop
532 * retransmit timer, otherwise restart timer
533 * using current (possibly backed-off) value.
534 * If process is waiting for space,
535 * wakeup/selwakeup/signal. If data
536 * are ready to send, let tcp_output
537 * decide between more output or persist.
538 */
539 if (tp->snd_una == tp->snd_max)
540 tp->t_timer[TCPT_REXMT] = 0;
541 else if (tp->t_timer[TCPT_PERSIST] == 0)
542 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
543
544 /*
545 * There's room in so_snd, sowwakup will read()
546 * from the socket if we can
547 */
548/* if (so->so_snd.sb_flags & SB_NOTIFY)
549 * sowwakeup(so);
550 */
551 /*
552 * This is called because sowwakeup might have
553 * put data into so_snd. Since we don't so sowwakeup,
554 * we don't need this.. XXX???
555 */
556 if (so->so_snd.sb_cc)
557 (void) tcp_output(tp);
558
559 return;
560 }
561 } else if (ti->ti_ack == tp->snd_una &&
562 tcpfrag_list_empty(tp) &&
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700563 ti->ti_len <= sbspace(&so->so_rcv)) {
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800564 /*
565 * this is a pure, in-sequence data packet
566 * with nothing on the reassembly queue and
567 * we have enough buffer space to take it.
568 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700569 STAT(tcpstat.tcps_preddat++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800570 tp->rcv_nxt += ti->ti_len;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700571 STAT(tcpstat.tcps_rcvpack++);
572 STAT(tcpstat.tcps_rcvbyte += ti->ti_len);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800573 /*
574 * Add data to socket buffer.
575 */
576 if (so->so_emu) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700577 if (tcp_emu(so,m)) sbappend(so, m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800578 } else
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700579 sbappend(so, m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800580
581 /*
582 * XXX This is called when data arrives. Later, check
583 * if we can actually write() to the socket
584 * XXX Need to check? It's be NON_BLOCKING
585 */
586/* sorwakeup(so); */
587
588 /*
589 * If this is a short packet, then ACK now - with Nagel
590 * congestion avoidance sender won't send more until
591 * he gets an ACK.
592 *
593 * It is better to not delay acks at all to maximize
594 * TCP throughput. See RFC 2581.
595 */
596 tp->t_flags |= TF_ACKNOW;
597 tcp_output(tp);
598 return;
599 }
600 } /* header prediction */
601 /*
602 * Calculate amount of space in receive window,
603 * and then do TCP input processing.
604 * Receive window is amount of space in rcv queue,
605 * but not less than advertised window.
606 */
607 { int win;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700608 win = sbspace(&so->so_rcv);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800609 if (win < 0)
610 win = 0;
611 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
612 }
613
614 switch (tp->t_state) {
615
616 /*
617 * If the state is LISTEN then ignore segment if it contains an RST.
618 * If the segment contains an ACK then it is bad and send a RST.
619 * If it does not contain a SYN then it is not interesting; drop it.
620 * Don't bother responding if the destination was a broadcast.
621 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
622 * tp->iss, and send a segment:
623 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
624 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
625 * Fill in remote peer address fields if not previously specified.
626 * Enter SYN_RECEIVED state, and process any other fields of this
627 * segment in this state.
628 */
629 case TCPS_LISTEN: {
630
631 if (tiflags & TH_RST)
632 goto drop;
633 if (tiflags & TH_ACK)
634 goto dropwithreset;
635 if ((tiflags & TH_SYN) == 0)
636 goto drop;
637
638 /*
639 * This has way too many gotos...
640 * But a bit of spaghetti code never hurt anybody :)
641 */
642
643 /*
644 * If this is destined for the control address, then flag to
645 * tcp_ctl once connected, otherwise connect
646 */
647 if ((so->so_faddr_ip & 0xffffff00) == special_addr_ip) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700648 int lastbyte=so->so_faddr_ip & 0xff;
649 if (lastbyte!=CTL_ALIAS && lastbyte!=CTL_DNS) {
650#if 0
651 if(lastbyte==CTL_CMD || lastbyte==CTL_EXEC) {
652 /* Command or exec adress */
653 so->so_state |= SS_CTL;
654 } else
655#endif
656 {
657 /* May be an add exec */
658 for(ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
659 if(ex_ptr->ex_fport == so->so_faddr_port &&
660 lastbyte == ex_ptr->ex_addr) {
661 so->so_state |= SS_CTL;
662 break;
663 }
664 }
665 }
666 if(so->so_state & SS_CTL) goto cont_input;
667 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800668 /* CTL_ALIAS: Do nothing, tcp_fconnect will be called on it */
669 }
670
671 if (so->so_emu & EMU_NOCONNECT) {
672 so->so_emu &= ~EMU_NOCONNECT;
673 goto cont_input;
674 }
675
David 'Digit' Turnerc0052462014-02-25 18:39:29 +0100676 if((tcp_fconnect(so) == -1) && (errno != EINPROGRESS) &&
David 'Digit' Turnerce0f4b02010-03-25 11:11:29 -0700677 (errno != EWOULDBLOCK) && (errno != EAGAIN)) {
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800678 u_char code=ICMP_UNREACH_NET;
679 DEBUG_MISC((dfd," tcp fconnect errno = %d-%s\n",
680 errno,errno_str));
681 if(errno == ECONNREFUSED) {
682 /* ACK the SYN, send RST to refuse the connection */
683 tcp_respond(tp, ti, m, ti->ti_seq+1, (tcp_seq)0,
684 TH_RST|TH_ACK);
685 } else {
686 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
687 HTONL(ti->ti_seq); /* restore tcp header */
688 HTONL(ti->ti_ack);
689 HTONS(ti->ti_win);
690 HTONS(ti->ti_urp);
691 m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
692 m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
693 *ip=save_ip;
694 icmp_error(m, ICMP_UNREACH,code, 0,errno_str);
695 }
696 tp = tcp_close(tp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700697 m_free(m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800698 } else {
699 /*
700 * Haven't connected yet, save the current mbuf
701 * and ti, and return
702 * XXX Some OS's don't tell us whether the connect()
703 * succeeded or not. So we must time it out.
704 */
705 so->so_m = m;
706 so->so_ti = ti;
707 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
708 tp->t_state = TCPS_SYN_RECEIVED;
709 }
710 return;
711
712 cont_conn:
713 /* m==NULL
714 * Check if the connect succeeded
715 */
716 if (so->so_state & SS_NOFDREF) {
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800717 tp = tcp_close(tp);
718 goto dropwithreset;
719 }
720 cont_input:
721 tcp_template(tp);
722
723 if (optp)
724 tcp_dooptions(tp, (u_char *)optp, optlen, ti);
725 /* , */
726 /* &ts_present, &ts_val, &ts_ecr); */
727
728 if (iss)
729 tp->iss = iss;
730 else
731 tp->iss = tcp_iss;
732 tcp_iss += TCP_ISSINCR/2;
733 tp->irs = ti->ti_seq;
734 tcp_sendseqinit(tp);
735 tcp_rcvseqinit(tp);
736 tp->t_flags |= TF_ACKNOW;
737 tp->t_state = TCPS_SYN_RECEIVED;
738 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700739 STAT(tcpstat.tcps_accepts++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800740 goto trimthenstep6;
741 } /* case TCPS_LISTEN */
742
743 /*
744 * If the state is SYN_SENT:
745 * if seg contains an ACK, but not for our SYN, drop the input.
746 * if seg contains a RST, then drop the connection.
747 * if seg does not contain SYN, then drop it.
748 * Otherwise this is an acceptable SYN segment
749 * initialize tp->rcv_nxt and tp->irs
750 * if seg contains ack then advance tp->snd_una
751 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
752 * arrange for segment to be acked (eventually)
753 * continue processing rest of data/controls, beginning with URG
754 */
755 case TCPS_SYN_SENT:
756 if ((tiflags & TH_ACK) &&
757 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
758 SEQ_GT(ti->ti_ack, tp->snd_max)))
759 goto dropwithreset;
760
761 if (tiflags & TH_RST) {
762 if (tiflags & TH_ACK)
763 tp = tcp_drop(tp,0); /* XXX Check t_softerror! */
764 goto drop;
765 }
766
767 if ((tiflags & TH_SYN) == 0)
768 goto drop;
769 if (tiflags & TH_ACK) {
770 tp->snd_una = ti->ti_ack;
771 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
772 tp->snd_nxt = tp->snd_una;
773 }
774
775 tp->t_timer[TCPT_REXMT] = 0;
776 tp->irs = ti->ti_seq;
777 tcp_rcvseqinit(tp);
778 tp->t_flags |= TF_ACKNOW;
779 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700780 STAT(tcpstat.tcps_connects++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800781 soisfconnected(so);
782 tp->t_state = TCPS_ESTABLISHED;
783
784 /* Do window scaling on this connection? */
785/* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
786 * (TF_RCVD_SCALE|TF_REQ_SCALE)) {
787 * tp->snd_scale = tp->requested_s_scale;
788 * tp->rcv_scale = tp->request_r_scale;
789 * }
790 */
791 (void) tcp_reass(tp, (struct tcpiphdr *)0,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700792 (struct mbuf *)0);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800793 /*
794 * if we didn't have to retransmit the SYN,
795 * use its rtt as our initial srtt & rtt var.
796 */
797 if (tp->t_rtt)
798 tcp_xmit_timer(tp, tp->t_rtt);
799 } else
800 tp->t_state = TCPS_SYN_RECEIVED;
801
802trimthenstep6:
803 /*
804 * Advance ti->ti_seq to correspond to first data byte.
805 * If data, trim to stay within window,
806 * dropping FIN if necessary.
807 */
808 ti->ti_seq++;
809 if (ti->ti_len > tp->rcv_wnd) {
810 todrop = ti->ti_len - tp->rcv_wnd;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700811 m_adj(m, -todrop);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800812 ti->ti_len = tp->rcv_wnd;
813 tiflags &= ~TH_FIN;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700814 STAT(tcpstat.tcps_rcvpackafterwin++);
815 STAT(tcpstat.tcps_rcvbyteafterwin += todrop);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800816 }
817 tp->snd_wl1 = ti->ti_seq - 1;
818 tp->rcv_up = ti->ti_seq;
819 goto step6;
820 } /* switch tp->t_state */
821 /*
822 * States other than LISTEN or SYN_SENT.
823 * First check timestamp, if present.
824 * Then check that at least some bytes of segment are within
825 * receive window. If segment begins before rcv_nxt,
826 * drop leading data (and SYN); if nothing left, just ack.
827 *
828 * RFC 1323 PAWS: If we have a timestamp reply on this segment
829 * and it's less than ts_recent, drop it.
830 */
831/* if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
832 * TSTMP_LT(ts_val, tp->ts_recent)) {
833 *
834 */ /* Check to see if ts_recent is over 24 days old. */
835/* if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
836 */ /*
837 * * Invalidate ts_recent. If this segment updates
838 * * ts_recent, the age will be reset later and ts_recent
839 * * will get a valid value. If it does not, setting
840 * * ts_recent to zero will at least satisfy the
841 * * requirement that zero be placed in the timestamp
842 * * echo reply when ts_recent isn't valid. The
843 * * age isn't reset until we get a valid ts_recent
844 * * because we don't want out-of-order segments to be
845 * * dropped when ts_recent is old.
846 * */
847/* tp->ts_recent = 0;
848 * } else {
849 * tcpstat.tcps_rcvduppack++;
850 * tcpstat.tcps_rcvdupbyte += ti->ti_len;
851 * tcpstat.tcps_pawsdrop++;
852 * goto dropafterack;
853 * }
854 * }
855 */
856
857 todrop = tp->rcv_nxt - ti->ti_seq;
858 if (todrop > 0) {
859 if (tiflags & TH_SYN) {
860 tiflags &= ~TH_SYN;
861 ti->ti_seq++;
862 if (ti->ti_urp > 1)
863 ti->ti_urp--;
864 else
865 tiflags &= ~TH_URG;
866 todrop--;
867 }
868 /*
869 * Following if statement from Stevens, vol. 2, p. 960.
870 */
871 if (todrop > ti->ti_len
872 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
873 /*
874 * Any valid FIN must be to the left of the window.
875 * At this point the FIN must be a duplicate or out
876 * of sequence; drop it.
877 */
878 tiflags &= ~TH_FIN;
879
880 /*
881 * Send an ACK to resynchronize and drop any data.
882 * But keep on processing for RST or ACK.
883 */
884 tp->t_flags |= TF_ACKNOW;
885 todrop = ti->ti_len;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700886 STAT(tcpstat.tcps_rcvduppack++);
887 STAT(tcpstat.tcps_rcvdupbyte += todrop);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800888 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700889 STAT(tcpstat.tcps_rcvpartduppack++);
890 STAT(tcpstat.tcps_rcvpartdupbyte += todrop);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800891 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700892 m_adj(m, todrop);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800893 ti->ti_seq += todrop;
894 ti->ti_len -= todrop;
895 if (ti->ti_urp > todrop)
896 ti->ti_urp -= todrop;
897 else {
898 tiflags &= ~TH_URG;
899 ti->ti_urp = 0;
900 }
901 }
902 /*
903 * If new data are received on a connection after the
904 * user processes are gone, then RST the other end.
905 */
906 if ((so->so_state & SS_NOFDREF) &&
907 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
908 tp = tcp_close(tp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700909 STAT(tcpstat.tcps_rcvafterclose++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800910 goto dropwithreset;
911 }
912
913 /*
914 * If segment ends after window, drop trailing data
915 * (and PUSH and FIN); if nothing left, just ACK.
916 */
917 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
918 if (todrop > 0) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700919 STAT(tcpstat.tcps_rcvpackafterwin++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800920 if (todrop >= ti->ti_len) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700921 STAT(tcpstat.tcps_rcvbyteafterwin += ti->ti_len);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800922 /*
923 * If a new connection request is received
924 * while in TIME_WAIT, drop the old connection
925 * and start over if the sequence numbers
926 * are above the previous ones.
927 */
928 if (tiflags & TH_SYN &&
929 tp->t_state == TCPS_TIME_WAIT &&
930 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
931 iss = tp->rcv_nxt + TCP_ISSINCR;
932 tp = tcp_close(tp);
933 goto findso;
934 }
935 /*
936 * If window is closed can only take segments at
937 * window edge, and have to drop data and PUSH from
938 * incoming segments. Continue processing, but
939 * remember to ack. Otherwise, drop segment
940 * and ack.
941 */
942 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
943 tp->t_flags |= TF_ACKNOW;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700944 STAT(tcpstat.tcps_rcvwinprobe++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800945 } else
946 goto dropafterack;
947 } else
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700948 STAT(tcpstat.tcps_rcvbyteafterwin += todrop);
949 m_adj(m, -todrop);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800950 ti->ti_len -= todrop;
951 tiflags &= ~(TH_PUSH|TH_FIN);
952 }
953
954 /*
955 * If last ACK falls within this segment's sequence numbers,
956 * record its timestamp.
957 */
958/* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
959 * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
960 * ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
961 * tp->ts_recent_age = tcp_now;
962 * tp->ts_recent = ts_val;
963 * }
964 */
965
966 /*
967 * If the RST bit is set examine the state:
968 * SYN_RECEIVED STATE:
969 * If passive open, return to LISTEN state.
970 * If active open, inform user that connection was refused.
971 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
972 * Inform user that connection was reset, and close tcb.
973 * CLOSING, LAST_ACK, TIME_WAIT STATES
974 * Close the tcb.
975 */
976 if (tiflags&TH_RST) switch (tp->t_state) {
977
978 case TCPS_SYN_RECEIVED:
979/* so->so_error = ECONNREFUSED; */
980 goto close;
981
982 case TCPS_ESTABLISHED:
983 case TCPS_FIN_WAIT_1:
984 case TCPS_FIN_WAIT_2:
985 case TCPS_CLOSE_WAIT:
986/* so->so_error = ECONNRESET; */
987 close:
988 tp->t_state = TCPS_CLOSED;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700989 STAT(tcpstat.tcps_drops++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800990 tp = tcp_close(tp);
991 goto drop;
992
993 case TCPS_CLOSING:
994 case TCPS_LAST_ACK:
995 case TCPS_TIME_WAIT:
996 tp = tcp_close(tp);
997 goto drop;
998 }
999
1000 /*
1001 * If a SYN is in the window, then this is an
1002 * error and we send an RST and drop the connection.
1003 */
1004 if (tiflags & TH_SYN) {
1005 tp = tcp_drop(tp,0);
1006 goto dropwithreset;
1007 }
1008
1009 /*
1010 * If the ACK bit is off we drop the segment and return.
1011 */
1012 if ((tiflags & TH_ACK) == 0) goto drop;
1013
1014 /*
1015 * Ack processing.
1016 */
1017 switch (tp->t_state) {
1018 /*
1019 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1020 * ESTABLISHED state and continue processing, otherwise
1021 * send an RST. una<=ack<=max
1022 */
1023 case TCPS_SYN_RECEIVED:
1024
1025 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
1026 SEQ_GT(ti->ti_ack, tp->snd_max))
1027 goto dropwithreset;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001028 STAT(tcpstat.tcps_connects++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001029 tp->t_state = TCPS_ESTABLISHED;
1030 /*
1031 * The sent SYN is ack'ed with our sequence number +1
1032 * The first data byte already in the buffer will get
1033 * lost if no correction is made. This is only needed for
1034 * SS_CTL since the buffer is empty otherwise.
1035 * tp->snd_una++; or:
1036 */
1037 tp->snd_una=ti->ti_ack;
1038 if (so->so_state & SS_CTL) {
1039 /* So tcp_ctl reports the right state */
1040 ret = tcp_ctl(so);
1041 if (ret == 1) {
1042 soisfconnected(so);
1043 so->so_state &= ~SS_CTL; /* success XXX */
1044 } else if (ret == 2) {
1045 so->so_state = SS_NOFDREF; /* CTL_CMD */
1046 } else {
1047 needoutput = 1;
1048 tp->t_state = TCPS_FIN_WAIT_1;
1049 }
1050 } else {
1051 soisfconnected(so);
1052 }
1053
1054 /* Do window scaling? */
1055/* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1056 * (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1057 * tp->snd_scale = tp->requested_s_scale;
1058 * tp->rcv_scale = tp->request_r_scale;
1059 * }
1060 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001061 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001062 tp->snd_wl1 = ti->ti_seq - 1;
1063 /* Avoid ack processing; snd_una==ti_ack => dup ack */
1064 goto synrx_to_est;
1065 /* fall into ... */
1066
1067 /*
1068 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1069 * ACKs. If the ack is in the range
1070 * tp->snd_una < ti->ti_ack <= tp->snd_max
1071 * then advance tp->snd_una to ti->ti_ack and drop
1072 * data from the retransmission queue. If this ACK reflects
1073 * more up to date window information we update our window information.
1074 */
1075 case TCPS_ESTABLISHED:
1076 case TCPS_FIN_WAIT_1:
1077 case TCPS_FIN_WAIT_2:
1078 case TCPS_CLOSE_WAIT:
1079 case TCPS_CLOSING:
1080 case TCPS_LAST_ACK:
1081 case TCPS_TIME_WAIT:
1082
1083 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1084 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001085 STAT(tcpstat.tcps_rcvdupack++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001086 DEBUG_MISC((dfd," dup ack m = %lx so = %lx \n",
1087 (long )m, (long )so));
1088 /*
1089 * If we have outstanding data (other than
1090 * a window probe), this is a completely
1091 * duplicate ack (ie, window info didn't
1092 * change), the ack is the biggest we've
1093 * seen and we've seen exactly our rexmt
1094 * threshold of them, assume a packet
1095 * has been dropped and retransmit it.
1096 * Kludge snd_nxt & the congestion
1097 * window so we send only this one
1098 * packet.
1099 *
1100 * We know we're losing at the current
1101 * window size so do congestion avoidance
1102 * (set ssthresh to half the current window
1103 * and pull our congestion window back to
1104 * the new ssthresh).
1105 *
1106 * Dup acks mean that packets have left the
1107 * network (they're now cached at the receiver)
1108 * so bump cwnd by the amount in the receiver
1109 * to keep a constant cwnd packets in the
1110 * network.
1111 */
1112 if (tp->t_timer[TCPT_REXMT] == 0 ||
1113 ti->ti_ack != tp->snd_una)
1114 tp->t_dupacks = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001115 else if (++tp->t_dupacks == TCPREXMTTHRESH) {
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001116 tcp_seq onxt = tp->snd_nxt;
1117 u_int win =
1118 min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1119 tp->t_maxseg;
1120
1121 if (win < 2)
1122 win = 2;
1123 tp->snd_ssthresh = win * tp->t_maxseg;
1124 tp->t_timer[TCPT_REXMT] = 0;
1125 tp->t_rtt = 0;
1126 tp->snd_nxt = ti->ti_ack;
1127 tp->snd_cwnd = tp->t_maxseg;
1128 (void) tcp_output(tp);
1129 tp->snd_cwnd = tp->snd_ssthresh +
1130 tp->t_maxseg * tp->t_dupacks;
1131 if (SEQ_GT(onxt, tp->snd_nxt))
1132 tp->snd_nxt = onxt;
1133 goto drop;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001134 } else if (tp->t_dupacks > TCPREXMTTHRESH) {
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001135 tp->snd_cwnd += tp->t_maxseg;
1136 (void) tcp_output(tp);
1137 goto drop;
1138 }
1139 } else
1140 tp->t_dupacks = 0;
1141 break;
1142 }
1143 synrx_to_est:
1144 /*
1145 * If the congestion window was inflated to account
1146 * for the other side's cached packets, retract it.
1147 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001148 if (tp->t_dupacks > TCPREXMTTHRESH &&
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001149 tp->snd_cwnd > tp->snd_ssthresh)
1150 tp->snd_cwnd = tp->snd_ssthresh;
1151 tp->t_dupacks = 0;
1152 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001153 STAT(tcpstat.tcps_rcvacktoomuch++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001154 goto dropafterack;
1155 }
1156 acked = ti->ti_ack - tp->snd_una;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001157 STAT(tcpstat.tcps_rcvackpack++);
1158 STAT(tcpstat.tcps_rcvackbyte += acked);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001159
1160 /*
1161 * If we have a timestamp reply, update smoothed
1162 * round trip time. If no timestamp is present but
1163 * transmit timer is running and timed sequence
1164 * number was acked, update smoothed round trip time.
1165 * Since we now have an rtt measurement, cancel the
1166 * timer backoff (cf., Phil Karn's retransmit alg.).
1167 * Recompute the initial retransmit timer.
1168 */
1169/* if (ts_present)
1170 * tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
1171 * else
1172 */
1173 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1174 tcp_xmit_timer(tp,tp->t_rtt);
1175
1176 /*
1177 * If all outstanding data is acked, stop retransmit
1178 * timer and remember to restart (more output or persist).
1179 * If there is more data to be acked, restart retransmit
1180 * timer, using current (possibly backed-off) value.
1181 */
1182 if (ti->ti_ack == tp->snd_max) {
1183 tp->t_timer[TCPT_REXMT] = 0;
1184 needoutput = 1;
1185 } else if (tp->t_timer[TCPT_PERSIST] == 0)
1186 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1187 /*
1188 * When new data is acked, open the congestion window.
1189 * If the window gives us less than ssthresh packets
1190 * in flight, open exponentially (maxseg per packet).
1191 * Otherwise open linearly: maxseg per window
1192 * (maxseg^2 / cwnd per packet).
1193 */
1194 {
1195 register u_int cw = tp->snd_cwnd;
1196 register u_int incr = tp->t_maxseg;
1197
1198 if (cw > tp->snd_ssthresh)
1199 incr = incr * incr / cw;
1200 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1201 }
1202 if (acked > so->so_snd.sb_cc) {
1203 tp->snd_wnd -= so->so_snd.sb_cc;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001204 sbdrop(&so->so_snd, (int )so->so_snd.sb_cc);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001205 ourfinisacked = 1;
1206 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001207 sbdrop(&so->so_snd, acked);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001208 tp->snd_wnd -= acked;
1209 ourfinisacked = 0;
1210 }
1211 /*
1212 * XXX sowwakup is called when data is acked and there's room for
1213 * for more data... it should read() the socket
1214 */
1215/* if (so->so_snd.sb_flags & SB_NOTIFY)
1216 * sowwakeup(so);
1217 */
1218 tp->snd_una = ti->ti_ack;
1219 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1220 tp->snd_nxt = tp->snd_una;
1221
1222 switch (tp->t_state) {
1223
1224 /*
1225 * In FIN_WAIT_1 STATE in addition to the processing
1226 * for the ESTABLISHED state if our FIN is now acknowledged
1227 * then enter FIN_WAIT_2.
1228 */
1229 case TCPS_FIN_WAIT_1:
1230 if (ourfinisacked) {
1231 /*
1232 * If we can't receive any more
1233 * data, then closing user can proceed.
1234 * Starting the timer is contrary to the
1235 * specification, but if we don't get a FIN
1236 * we'll hang forever.
1237 */
1238 if (so->so_state & SS_FCANTRCVMORE) {
1239 soisfdisconnected(so);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001240 tp->t_timer[TCPT_2MSL] = TCP_MAXIDLE;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001241 }
1242 tp->t_state = TCPS_FIN_WAIT_2;
1243 }
1244 break;
1245
1246 /*
1247 * In CLOSING STATE in addition to the processing for
1248 * the ESTABLISHED state if the ACK acknowledges our FIN
1249 * then enter the TIME-WAIT state, otherwise ignore
1250 * the segment.
1251 */
1252 case TCPS_CLOSING:
1253 if (ourfinisacked) {
1254 tp->t_state = TCPS_TIME_WAIT;
1255 tcp_canceltimers(tp);
1256 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1257 soisfdisconnected(so);
1258 }
1259 break;
1260
1261 /*
1262 * In LAST_ACK, we may still be waiting for data to drain
1263 * and/or to be acked, as well as for the ack of our FIN.
1264 * If our FIN is now acknowledged, delete the TCB,
1265 * enter the closed state and return.
1266 */
1267 case TCPS_LAST_ACK:
1268 if (ourfinisacked) {
1269 tp = tcp_close(tp);
1270 goto drop;
1271 }
1272 break;
1273
1274 /*
1275 * In TIME_WAIT state the only thing that should arrive
1276 * is a retransmission of the remote FIN. Acknowledge
1277 * it and restart the finack timer.
1278 */
1279 case TCPS_TIME_WAIT:
1280 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1281 goto dropafterack;
1282 }
1283 } /* switch(tp->t_state) */
1284
1285step6:
1286 /*
1287 * Update window information.
1288 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1289 */
1290 if ((tiflags & TH_ACK) &&
1291 (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1292 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1293 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1294 /* keep track of pure window updates */
1295 if (ti->ti_len == 0 &&
1296 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001297 STAT(tcpstat.tcps_rcvwinupd++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001298 tp->snd_wnd = tiwin;
1299 tp->snd_wl1 = ti->ti_seq;
1300 tp->snd_wl2 = ti->ti_ack;
1301 if (tp->snd_wnd > tp->max_sndwnd)
1302 tp->max_sndwnd = tp->snd_wnd;
1303 needoutput = 1;
1304 }
1305
1306 /*
1307 * Process segments with URG.
1308 */
1309 if ((tiflags & TH_URG) && ti->ti_urp &&
1310 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1311 /*
1312 * This is a kludge, but if we receive and accept
1313 * random urgent pointers, we'll crash in
1314 * soreceive. It's hard to imagine someone
1315 * actually wanting to send this much urgent data.
1316 */
1317 if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen) {
1318 ti->ti_urp = 0;
1319 tiflags &= ~TH_URG;
1320 goto dodata;
1321 }
1322 /*
1323 * If this segment advances the known urgent pointer,
1324 * then mark the data stream. This should not happen
1325 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1326 * a FIN has been received from the remote side.
1327 * In these states we ignore the URG.
1328 *
1329 * According to RFC961 (Assigned Protocols),
1330 * the urgent pointer points to the last octet
1331 * of urgent data. We continue, however,
1332 * to consider it to indicate the first octet
1333 * of data past the urgent section as the original
1334 * spec states (in one of two places).
1335 */
1336 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1337 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1338 so->so_urgc = so->so_rcv.sb_cc +
1339 (tp->rcv_up - tp->rcv_nxt); /* -1; */
1340 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1341
1342 }
1343 } else
1344 /*
1345 * If no out of band data is expected,
1346 * pull receive urgent pointer along
1347 * with the receive window.
1348 */
1349 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1350 tp->rcv_up = tp->rcv_nxt;
1351dodata:
1352
1353 /*
1354 * Process the segment text, merging it into the TCP sequencing queue,
1355 * and arranging for acknowledgment of receipt if necessary.
1356 * This process logically involves adjusting tp->rcv_wnd as data
1357 * is presented to the user (this happens in tcp_usrreq.c,
1358 * case PRU_RCVD). If a FIN has already been received on this
1359 * connection then we just ignore the text.
1360 */
1361 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1362 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1363 TCP_REASS(tp, ti, m, so, tiflags);
1364 /*
1365 * Note the amount of data that peer has sent into
1366 * our window, in order to estimate the sender's
1367 * buffer size.
1368 */
1369 len = so->so_rcv.sb_datalen - (tp->rcv_adv - tp->rcv_nxt);
1370 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001371 m_free(m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001372 tiflags &= ~TH_FIN;
1373 }
1374
1375 /*
1376 * If FIN is received ACK the FIN and let the user know
1377 * that the connection is closing.
1378 */
1379 if (tiflags & TH_FIN) {
1380 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1381 /*
1382 * If we receive a FIN we can't send more data,
1383 * set it SS_FDRAIN
1384 * Shutdown the socket if there is no rx data in the
1385 * buffer.
1386 * soread() is called on completion of shutdown() and
1387 * will got to TCPS_LAST_ACK, and use tcp_output()
1388 * to send the FIN.
1389 */
1390/* sofcantrcvmore(so); */
1391 sofwdrain(so);
1392
1393 tp->t_flags |= TF_ACKNOW;
1394 tp->rcv_nxt++;
1395 }
1396 switch (tp->t_state) {
1397
1398 /*
1399 * In SYN_RECEIVED and ESTABLISHED STATES
1400 * enter the CLOSE_WAIT state.
1401 */
1402 case TCPS_SYN_RECEIVED:
1403 case TCPS_ESTABLISHED:
1404 if(so->so_emu == EMU_CTL) /* no shutdown on socket */
1405 tp->t_state = TCPS_LAST_ACK;
1406 else
1407 tp->t_state = TCPS_CLOSE_WAIT;
1408 break;
1409
1410 /*
1411 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1412 * enter the CLOSING state.
1413 */
1414 case TCPS_FIN_WAIT_1:
1415 tp->t_state = TCPS_CLOSING;
1416 break;
1417
1418 /*
1419 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1420 * starting the time-wait timer, turning off the other
1421 * standard timers.
1422 */
1423 case TCPS_FIN_WAIT_2:
1424 tp->t_state = TCPS_TIME_WAIT;
1425 tcp_canceltimers(tp);
1426 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1427 soisfdisconnected(so);
1428 break;
1429
1430 /*
1431 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1432 */
1433 case TCPS_TIME_WAIT:
1434 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1435 break;
1436 }
1437 }
1438
1439 /*
1440 * If this is a small packet, then ACK now - with Nagel
1441 * congestion avoidance sender won't send more until
1442 * he gets an ACK.
1443 *
1444 * See above.
1445 */
1446/* if (ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg) {
1447 */
1448/* if ((ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg &&
1449 * (so->so_iptos & IPTOS_LOWDELAY) == 0) ||
1450 * ((so->so_iptos & IPTOS_LOWDELAY) &&
1451 * ((struct tcpiphdr_2 *)ti)->first_char == (char)27)) {
1452 */
1453 if (ti->ti_len && (unsigned)ti->ti_len <= 5 &&
1454 ((struct tcpiphdr_2 *)ti)->first_char == (char)27) {
1455 tp->t_flags |= TF_ACKNOW;
1456 }
1457
1458 /*
1459 * Return any desired output.
1460 */
1461 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
1462 (void) tcp_output(tp);
1463 }
1464 return;
1465
1466dropafterack:
1467 /*
1468 * Generate an ACK dropping incoming segment if it occupies
1469 * sequence space, where the ACK reflects our state.
1470 */
1471 if (tiflags & TH_RST)
1472 goto drop;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001473 m_freem(m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001474 tp->t_flags |= TF_ACKNOW;
1475 (void) tcp_output(tp);
1476 return;
1477
1478dropwithreset:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001479 /* reuses m if m!=NULL, m_free() unnecessary */
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001480 if (tiflags & TH_ACK)
1481 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1482 else {
1483 if (tiflags & TH_SYN) ti->ti_len++;
1484 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1485 TH_RST|TH_ACK);
1486 }
1487
1488 return;
1489
1490drop:
1491 /*
1492 * Drop space held by incoming segment and return.
1493 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001494 m_free(m);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001495
1496 return;
1497}
1498
1499 /* , ts_present, ts_val, ts_ecr) */
1500/* int *ts_present;
1501 * u_int32_t *ts_val, *ts_ecr;
1502 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001503static void
1504tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001505{
1506 u_int16_t mss;
1507 int opt, optlen;
1508
1509 DEBUG_CALL("tcp_dooptions");
1510 DEBUG_ARGS((dfd," tp = %lx cnt=%i \n", (long )tp, cnt));
1511
1512 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1513 opt = cp[0];
1514 if (opt == TCPOPT_EOL)
1515 break;
1516 if (opt == TCPOPT_NOP)
1517 optlen = 1;
1518 else {
1519 optlen = cp[1];
1520 if (optlen <= 0)
1521 break;
1522 }
1523 switch (opt) {
1524
1525 default:
1526 continue;
1527
1528 case TCPOPT_MAXSEG:
1529 if (optlen != TCPOLEN_MAXSEG)
1530 continue;
1531 if (!(ti->ti_flags & TH_SYN))
1532 continue;
1533 memcpy((char *) &mss, (char *) cp + 2, sizeof(mss));
1534 NTOHS(mss);
1535 (void) tcp_mss(tp, mss); /* sets t_maxseg */
1536 break;
1537
1538/* case TCPOPT_WINDOW:
1539 * if (optlen != TCPOLEN_WINDOW)
1540 * continue;
1541 * if (!(ti->ti_flags & TH_SYN))
1542 * continue;
1543 * tp->t_flags |= TF_RCVD_SCALE;
1544 * tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1545 * break;
1546 */
1547/* case TCPOPT_TIMESTAMP:
1548 * if (optlen != TCPOLEN_TIMESTAMP)
1549 * continue;
1550 * *ts_present = 1;
1551 * memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val));
1552 * NTOHL(*ts_val);
1553 * memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr));
1554 * NTOHL(*ts_ecr);
1555 *
1556 */ /*
1557 * * A timestamp received in a SYN makes
1558 * * it ok to send timestamp requests and replies.
1559 * */
1560/* if (ti->ti_flags & TH_SYN) {
1561 * tp->t_flags |= TF_RCVD_TSTMP;
1562 * tp->ts_recent = *ts_val;
1563 * tp->ts_recent_age = tcp_now;
1564 * }
1565 */ break;
1566 }
1567 }
1568}
1569
1570
1571/*
1572 * Pull out of band byte out of a segment so
1573 * it doesn't appear in the user's data queue.
1574 * It is still reflected in the segment length for
1575 * sequencing purposes.
1576 */
1577
1578#ifdef notdef
1579
1580void
1581tcp_pulloutofband(so, ti, m)
1582 struct socket *so;
1583 struct tcpiphdr *ti;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001584 register struct mbuf *m;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001585{
1586 int cnt = ti->ti_urp - 1;
1587
1588 while (cnt >= 0) {
1589 if (m->m_len > cnt) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001590 char *cp = mtod(m, caddr_t) + cnt;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001591 struct tcpcb *tp = sototcpcb(so);
1592
1593 tp->t_iobc = *cp;
1594 tp->t_oobflags |= TCPOOB_HAVEDATA;
1595 memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1));
1596 m->m_len--;
1597 return;
1598 }
1599 cnt -= m->m_len;
1600 m = m->m_next; /* XXX WRONG! Fix it! */
1601 if (m == 0)
1602 break;
1603 }
1604 panic("tcp_pulloutofband");
1605}
1606
1607#endif /* notdef */
1608
1609/*
1610 * Collect new round-trip time estimate
1611 * and update averages and current timeout.
1612 */
1613
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001614static void
1615tcp_xmit_timer(register struct tcpcb *tp, int rtt)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001616{
1617 register short delta;
1618
1619 DEBUG_CALL("tcp_xmit_timer");
1620 DEBUG_ARG("tp = %lx", (long)tp);
1621 DEBUG_ARG("rtt = %d", rtt);
1622
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001623 STAT(tcpstat.tcps_rttupdated++);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001624 if (tp->t_srtt != 0) {
1625 /*
1626 * srtt is stored as fixed point with 3 bits after the
1627 * binary point (i.e., scaled by 8). The following magic
1628 * is equivalent to the smoothing algorithm in rfc793 with
1629 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1630 * point). Adjust rtt to origin 0.
1631 */
1632 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1633 if ((tp->t_srtt += delta) <= 0)
1634 tp->t_srtt = 1;
1635 /*
1636 * We accumulate a smoothed rtt variance (actually, a
1637 * smoothed mean difference), then set the retransmit
1638 * timer to smoothed rtt + 4 times the smoothed variance.
1639 * rttvar is stored as fixed point with 2 bits after the
1640 * binary point (scaled by 4). The following is
1641 * equivalent to rfc793 smoothing with an alpha of .75
1642 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1643 * rfc793's wired-in beta.
1644 */
1645 if (delta < 0)
1646 delta = -delta;
1647 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1648 if ((tp->t_rttvar += delta) <= 0)
1649 tp->t_rttvar = 1;
1650 } else {
1651 /*
1652 * No rtt measurement yet - use the unsmoothed rtt.
1653 * Set the variance to half the rtt (so our first
1654 * retransmit happens at 3*rtt).
1655 */
1656 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1657 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1658 }
1659 tp->t_rtt = 0;
1660 tp->t_rxtshift = 0;
1661
1662 /*
1663 * the retransmit should happen at rtt + 4 * rttvar.
1664 * Because of the way we do the smoothing, srtt and rttvar
1665 * will each average +1/2 tick of bias. When we compute
1666 * the retransmit timer, we want 1/2 tick of rounding and
1667 * 1 extra tick because of +-1/2 tick uncertainty in the
1668 * firing of the timer. The bias will give us exactly the
1669 * 1.5 tick we need. But, because the bias is
1670 * statistical, we have to test that we don't drop below
1671 * the minimum feasible timer (which is 2 ticks).
1672 */
1673 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1674 (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
1675
1676 /*
1677 * We received an ack for a packet that wasn't retransmitted;
1678 * it is probably safe to discard any error indications we've
1679 * received recently. This isn't quite right, but close enough
1680 * for now (a route might have failed after we sent a segment,
1681 * and the return path might not be symmetrical).
1682 */
1683 tp->t_softerror = 0;
1684}
1685
1686/*
1687 * Determine a reasonable value for maxseg size.
1688 * If the route is known, check route for mtu.
1689 * If none, use an mss that can be handled on the outgoing
1690 * interface without forcing IP to fragment; if bigger than
1691 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1692 * to utilize large mbufs. If no route is found, route has no mtu,
1693 * or the destination isn't local, use a default, hopefully conservative
1694 * size (usually 512 or the default IP max size, but no more than the mtu
1695 * of the interface), as we can't discover anything about intervening
1696 * gateways or networks. We also initialize the congestion/slow start
1697 * window to be a single segment if the destination isn't local.
1698 * While looking at the routing entry, we also initialize other path-dependent
1699 * parameters from pre-set or cached values in the routing entry.
1700 */
1701
1702int
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001703tcp_mss(struct tcpcb *tp, u_int offer)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001704{
1705 struct socket *so = tp->t_socket;
1706 int mss;
1707
1708 DEBUG_CALL("tcp_mss");
1709 DEBUG_ARG("tp = %lx", (long)tp);
1710 DEBUG_ARG("offer = %d", offer);
1711
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001712 mss = min(IF_MTU, IF_MRU) - sizeof(struct tcpiphdr);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001713 if (offer)
1714 mss = min(mss, offer);
1715 mss = max(mss, 32);
1716 if (mss < tp->t_maxseg || offer != 0)
1717 tp->t_maxseg = mss;
1718
1719 tp->snd_cwnd = mss;
1720
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001721 sbreserve(&so->so_snd, TCP_SNDSPACE + ((TCP_SNDSPACE % mss) ?
1722 (mss - (TCP_SNDSPACE % mss)) :
1723 0));
1724 sbreserve(&so->so_rcv, TCP_RCVSPACE + ((TCP_RCVSPACE % mss) ?
1725 (mss - (TCP_RCVSPACE % mss)) :
1726 0));
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001727
1728 DEBUG_MISC((dfd, " returning mss = %d\n", mss));
1729
1730 return mss;
1731}