tcp_input.c revision 1.43 1 /* $NetBSD: tcp_input.c,v 1.43 1998/01/24 12:27:31 mellon Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
36 */
37
38 /*
39 * TODO list for SYN cache stuff:
40 *
41 * (a) The definition of "struct syn_cache" says:
42 *
43 * This structure should not exceeed 32 bytes.
44 *
45 * but it's 40 bytes on the Alpha. Can reduce memory use one
46 * of two ways:
47 *
48 * (1) Use a dynamically-sized hash table, and handle
49 * collisions by rehashing. Then sc_next is unnecessary.
50 *
51 * (2) Allocate syn_cache structures in pages (or some other
52 * large chunk). This would probably be desirable for
53 * maintaining locality of reference anyway.
54 *
55 * If you do this, you can change sc_next to a page/index
56 * value, and make it a 32-bit (or maybe even 16-bit)
57 * integer, thus partly obviating the need for the previous
58 * hack.
59 *
60 * It's also worth noting this this is necessary for IPv6, as well,
61 * where we use 32 bytes just for the IP addresses, so eliminating
62 * wastage is going to become more important. (BTW, has anyone
63 * integreated these changes with one fo the IPv6 status that are
64 * available?)
65 *
66 * (b) Find room for a "state" field, which is needed to keep a
67 * compressed state for TIME_WAIT TCBs. It's been noted already
68 * that this is fairly important for very high-volume web and
69 * mail servers, which use a large number of short-lived
70 * connections.
71 */
72
73 #ifndef TUBA_INCLUDE
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/errno.h>
82
83 #include <net/if.h>
84 #include <net/route.h>
85
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/ip_var.h>
91 #include <netinet/tcp.h>
92 #include <netinet/tcp_fsm.h>
93 #include <netinet/tcp_seq.h>
94 #include <netinet/tcp_timer.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet/tcpip.h>
97 #include <netinet/tcp_debug.h>
98
99 #include <machine/stdarg.h>
100
101 int tcprexmtthresh = 3;
102 struct tcpiphdr tcp_saveti;
103
104 extern u_long sb_max;
105
106 #endif /* TUBA_INCLUDE */
107 #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
108
109 /* for modulo comparisons of timestamps */
110 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
111 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
112
113 /*
114 * Macro to compute ACK transmission behavior. Delay the ACK unless
115 * the other side PUSH'd or we have already delayed an ACK (must send
116 * an ACK every two segments).
117 */
118 #define TCP_SETUP_ACK(tp, ti) \
119 do { \
120 if ((ti)->ti_flags & TH_PUSH || \
121 (tp)->t_flags & TF_DELACK) \
122 tp->t_flags |= TF_ACKNOW; \
123 else \
124 TCP_SET_DELACK(tp); \
125 } while (0)
126
127 /*
128 * Insert segment ti into reassembly queue of tcp with
129 * control block tp. Return TH_FIN if reassembly now includes
130 * a segment with FIN. The macro form does the common case inline
131 * (segment is the next to be received on an established connection,
132 * and the queue is empty), avoiding linkage into and removal
133 * from the queue and repetition of various conversions.
134 * Set DELACK for segments received in order, but ack immediately
135 * when segments are out of order (so fast retransmit can work).
136 */
137 #define TCP_REASS(tp, ti, m, so, flags) { \
138 if ((ti)->ti_seq == (tp)->rcv_nxt && \
139 (tp)->segq.lh_first == NULL && \
140 (tp)->t_state == TCPS_ESTABLISHED) { \
141 TCP_SETUP_ACK(tp, ti); \
142 (tp)->rcv_nxt += (ti)->ti_len; \
143 flags = (ti)->ti_flags & TH_FIN; \
144 tcpstat.tcps_rcvpack++;\
145 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
146 sbappend(&(so)->so_rcv, (m)); \
147 sorwakeup(so); \
148 } else { \
149 (flags) = tcp_reass((tp), (ti), (m)); \
150 tp->t_flags |= TF_ACKNOW; \
151 } \
152 }
153 #ifndef TUBA_INCLUDE
154
155 int
156 tcp_reass(tp, ti, m)
157 register struct tcpcb *tp;
158 register struct tcpiphdr *ti;
159 struct mbuf *m;
160 {
161 register struct ipqent *p, *q, *nq, *tiqe;
162 struct socket *so = tp->t_inpcb->inp_socket;
163 int flags;
164
165 /*
166 * Call with ti==0 after become established to
167 * force pre-ESTABLISHED data up to user socket.
168 */
169 if (ti == 0)
170 goto present;
171
172 /*
173 * Allocate a new queue entry, before we throw away any data.
174 * If we can't, just drop the packet. XXX
175 */
176 MALLOC(tiqe, struct ipqent *, sizeof (struct ipqent), M_IPQ, M_NOWAIT);
177 if (tiqe == NULL) {
178 tcpstat.tcps_rcvmemdrop++;
179 m_freem(m);
180 return (0);
181 }
182
183 /*
184 * Find a segment which begins after this one does.
185 */
186 for (p = NULL, q = tp->segq.lh_first; q != NULL;
187 p = q, q = q->ipqe_q.le_next)
188 if (SEQ_GT(q->ipqe_tcp->ti_seq, ti->ti_seq))
189 break;
190
191 /*
192 * If there is a preceding segment, it may provide some of
193 * our data already. If so, drop the data from the incoming
194 * segment. If it provides all of our data, drop us.
195 */
196 if (p != NULL) {
197 register struct tcpiphdr *phdr = p->ipqe_tcp;
198 register int i;
199
200 /* conversion to int (in i) handles seq wraparound */
201 i = phdr->ti_seq + phdr->ti_len - ti->ti_seq;
202 if (i > 0) {
203 if (i >= ti->ti_len) {
204 tcpstat.tcps_rcvduppack++;
205 tcpstat.tcps_rcvdupbyte += ti->ti_len;
206 m_freem(m);
207 FREE(tiqe, M_IPQ);
208 return (0);
209 }
210 m_adj(m, i);
211 ti->ti_len -= i;
212 ti->ti_seq += i;
213 }
214 }
215 tcpstat.tcps_rcvoopack++;
216 tcpstat.tcps_rcvoobyte += ti->ti_len;
217
218 /*
219 * While we overlap succeeding segments trim them or,
220 * if they are completely covered, dequeue them.
221 */
222 for (; q != NULL; q = nq) {
223 register struct tcpiphdr *qhdr = q->ipqe_tcp;
224 register int i = (ti->ti_seq + ti->ti_len) - qhdr->ti_seq;
225
226 if (i <= 0)
227 break;
228 if (i < qhdr->ti_len) {
229 qhdr->ti_seq += i;
230 qhdr->ti_len -= i;
231 m_adj(q->ipqe_m, i);
232 break;
233 }
234 nq = q->ipqe_q.le_next;
235 m_freem(q->ipqe_m);
236 LIST_REMOVE(q, ipqe_q);
237 FREE(q, M_IPQ);
238 }
239
240 /* Insert the new fragment queue entry into place. */
241 tiqe->ipqe_m = m;
242 tiqe->ipqe_tcp = ti;
243 if (p == NULL) {
244 LIST_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
245 } else {
246 LIST_INSERT_AFTER(p, tiqe, ipqe_q);
247 }
248
249 present:
250 /*
251 * Present data to user, advancing rcv_nxt through
252 * completed sequence space.
253 */
254 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
255 return (0);
256 q = tp->segq.lh_first;
257 if (q == NULL || q->ipqe_tcp->ti_seq != tp->rcv_nxt)
258 return (0);
259 if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_tcp->ti_len)
260 return (0);
261 do {
262 tp->rcv_nxt += q->ipqe_tcp->ti_len;
263 flags = q->ipqe_tcp->ti_flags & TH_FIN;
264
265 nq = q->ipqe_q.le_next;
266 LIST_REMOVE(q, ipqe_q);
267 if (so->so_state & SS_CANTRCVMORE)
268 m_freem(q->ipqe_m);
269 else
270 sbappend(&so->so_rcv, q->ipqe_m);
271 FREE(q, M_IPQ);
272 q = nq;
273 } while (q != NULL && q->ipqe_tcp->ti_seq == tp->rcv_nxt);
274 sorwakeup(so);
275 return (flags);
276 }
277
278 /*
279 * TCP input routine, follows pages 65-76 of the
280 * protocol specification dated September, 1981 very closely.
281 */
282 void
283 #if __STDC__
284 tcp_input(struct mbuf *m, ...)
285 #else
286 tcp_input(m, va_alist)
287 register struct mbuf *m;
288 #endif
289 {
290 register struct tcpiphdr *ti;
291 register struct inpcb *inp;
292 caddr_t optp = NULL;
293 int optlen = 0;
294 int len, tlen, off, hdroptlen;
295 register struct tcpcb *tp = 0;
296 register int tiflags;
297 struct socket *so = NULL;
298 int todrop, acked, ourfinisacked, needoutput = 0;
299 short ostate = 0;
300 int iss = 0;
301 u_long tiwin;
302 struct tcp_opt_info opti;
303 int iphlen;
304 va_list ap;
305
306 va_start(ap, m);
307 iphlen = va_arg(ap, int);
308 va_end(ap);
309
310 tcpstat.tcps_rcvtotal++;
311
312 opti.ts_present = 0;
313 opti.maxseg = 0;
314
315 /*
316 * Get IP and TCP header together in first mbuf.
317 * Note: IP leaves IP header in first mbuf.
318 */
319 ti = mtod(m, struct tcpiphdr *);
320 if (iphlen > sizeof (struct ip))
321 ip_stripoptions(m, (struct mbuf *)0);
322 if (m->m_len < sizeof (struct tcpiphdr)) {
323 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
324 tcpstat.tcps_rcvshort++;
325 return;
326 }
327 ti = mtod(m, struct tcpiphdr *);
328 }
329
330 /*
331 * Checksum extended TCP header and data.
332 */
333 tlen = ((struct ip *)ti)->ip_len;
334 len = sizeof (struct ip) + tlen;
335 bzero(ti->ti_x1, sizeof ti->ti_x1);
336 ti->ti_len = (u_int16_t)tlen;
337 HTONS(ti->ti_len);
338 if ((ti->ti_sum = in_cksum(m, len)) != 0) {
339 tcpstat.tcps_rcvbadsum++;
340 goto drop;
341 }
342 #endif /* TUBA_INCLUDE */
343
344 /*
345 * Check that TCP offset makes sense,
346 * pull out TCP options and adjust length. XXX
347 */
348 off = ti->ti_off << 2;
349 if (off < sizeof (struct tcphdr) || off > tlen) {
350 tcpstat.tcps_rcvbadoff++;
351 goto drop;
352 }
353 tlen -= off;
354 ti->ti_len = tlen;
355 if (off > sizeof (struct tcphdr)) {
356 if (m->m_len < sizeof(struct ip) + off) {
357 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
358 tcpstat.tcps_rcvshort++;
359 return;
360 }
361 ti = mtod(m, struct tcpiphdr *);
362 }
363 optlen = off - sizeof (struct tcphdr);
364 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
365 /*
366 * Do quick retrieval of timestamp options ("options
367 * prediction?"). If timestamp is the only option and it's
368 * formatted as recommended in RFC 1323 appendix A, we
369 * quickly get the values now and not bother calling
370 * tcp_dooptions(), etc.
371 */
372 if ((optlen == TCPOLEN_TSTAMP_APPA ||
373 (optlen > TCPOLEN_TSTAMP_APPA &&
374 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
375 *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
376 (ti->ti_flags & TH_SYN) == 0) {
377 opti.ts_present = 1;
378 opti.ts_val = ntohl(*(u_int32_t *)(optp + 4));
379 opti.ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
380 optp = NULL; /* we've parsed the options */
381 }
382 }
383 tiflags = ti->ti_flags;
384
385 /*
386 * Convert TCP protocol specific fields to host format.
387 */
388 NTOHL(ti->ti_seq);
389 NTOHL(ti->ti_ack);
390 NTOHS(ti->ti_win);
391 NTOHS(ti->ti_urp);
392
393 /*
394 * Locate pcb for segment.
395 */
396 findpcb:
397 inp = in_pcblookup_connect(&tcbtable, ti->ti_src, ti->ti_sport,
398 ti->ti_dst, ti->ti_dport);
399 if (inp == 0) {
400 ++tcpstat.tcps_pcbhashmiss;
401 inp = in_pcblookup_bind(&tcbtable, ti->ti_dst, ti->ti_dport);
402 if (inp == 0) {
403 ++tcpstat.tcps_noport;
404 goto dropwithreset;
405 }
406 }
407
408 /*
409 * If the state is CLOSED (i.e., TCB does not exist) then
410 * all data in the incoming segment is discarded.
411 * If the TCB exists but is in CLOSED state, it is embryonic,
412 * but should either do a listen or a connect soon.
413 */
414 tp = intotcpcb(inp);
415 if (tp == 0)
416 goto dropwithreset;
417 if (tp->t_state == TCPS_CLOSED)
418 goto drop;
419
420 /* Unscale the window into a 32-bit value. */
421 if ((tiflags & TH_SYN) == 0)
422 tiwin = ti->ti_win << tp->snd_scale;
423 else
424 tiwin = ti->ti_win;
425
426 so = inp->inp_socket;
427 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
428 if (so->so_options & SO_DEBUG) {
429 ostate = tp->t_state;
430 tcp_saveti = *ti;
431 }
432 if (so->so_options & SO_ACCEPTCONN) {
433 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
434 if (tiflags & TH_RST) {
435 syn_cache_reset(ti);
436 } else if ((tiflags & (TH_ACK|TH_SYN)) ==
437 (TH_ACK|TH_SYN)) {
438 /*
439 * Received a SYN,ACK. This should
440 * never happen while we are in
441 * LISTEN. Send an RST.
442 */
443 goto badsyn;
444 } else if (tiflags & TH_ACK) {
445 so = syn_cache_get(so, m);
446 if (so == NULL) {
447 /*
448 * We don't have a SYN for
449 * this ACK; send an RST.
450 */
451 goto badsyn;
452 } else if (so ==
453 (struct socket *)(-1)) {
454 /*
455 * We were unable to create
456 * the connection. If the
457 * 3-way handshake was
458 * completeed, and RST has
459 * been sent to the peer.
460 * Since the mbuf might be
461 * in use for the reply,
462 * do not free it.
463 */
464 m = NULL;
465 } else {
466 /*
467 * We have created a
468 * full-blown connection.
469 */
470 inp = sotoinpcb(so);
471 tp = intotcpcb(inp);
472 tiwin <<= tp->snd_scale;
473 goto after_listen;
474 }
475 }
476 } else {
477 /*
478 * Received a SYN.
479 */
480 if (in_hosteq(ti->ti_src, ti->ti_dst) &&
481 ti->ti_sport == ti->ti_dport) {
482 /*
483 * LISTEN socket received a SYN
484 * from itself? This can't possibly
485 * be valid; drop the packet.
486 */
487 tcpstat.tcps_badsyn++;
488 goto drop;
489 }
490 /*
491 * SYN looks ok; create compressed TCP
492 * state for it.
493 */
494 if (so->so_qlen <= so->so_qlimit &&
495 syn_cache_add(so, m, optp, optlen, &opti))
496 m = NULL;
497 }
498 goto drop;
499 }
500 }
501
502 after_listen:
503 #ifdef DIAGNOSTIC
504 /*
505 * Should not happen now that all embryonic connections
506 * are handled with compressed state.
507 */
508 if (tp->t_state == TCPS_LISTEN)
509 panic("tcp_input: TCPS_LISTEN");
510 #endif
511
512 /*
513 * Segment received on connection.
514 * Reset idle time and keep-alive timer.
515 */
516 tp->t_idle = 0;
517 if (TCPS_HAVEESTABLISHED(tp->t_state))
518 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
519
520 /*
521 * Process options.
522 */
523 if (optp)
524 tcp_dooptions(tp, optp, optlen, ti, &opti);
525
526 /*
527 * Header prediction: check for the two common cases
528 * of a uni-directional data xfer. If the packet has
529 * no control flags, is in-sequence, the window didn't
530 * change and we're not retransmitting, it's a
531 * candidate. If the length is zero and the ack moved
532 * forward, we're the sender side of the xfer. Just
533 * free the data acked & wake any higher level process
534 * that was blocked waiting for space. If the length
535 * is non-zero and the ack didn't move, we're the
536 * receiver side. If we're getting packets in-order
537 * (the reassembly queue is empty), add the data to
538 * the socket buffer and note that we need a delayed ack.
539 */
540 if (tp->t_state == TCPS_ESTABLISHED &&
541 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
542 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
543 ti->ti_seq == tp->rcv_nxt &&
544 tiwin && tiwin == tp->snd_wnd &&
545 tp->snd_nxt == tp->snd_max) {
546
547 /*
548 * If last ACK falls within this segment's sequence numbers,
549 * record the timestamp.
550 */
551 if (opti.ts_present &&
552 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
553 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
554 tp->ts_recent_age = tcp_now;
555 tp->ts_recent = opti.ts_val;
556 }
557
558 if (ti->ti_len == 0) {
559 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
560 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
561 tp->snd_cwnd >= tp->snd_wnd &&
562 tp->t_dupacks < tcprexmtthresh) {
563 /*
564 * this is a pure ack for outstanding data.
565 */
566 ++tcpstat.tcps_predack;
567 if (opti.ts_present)
568 tcp_xmit_timer(tp,
569 tcp_now-opti.ts_ecr+1);
570 else if (tp->t_rtt &&
571 SEQ_GT(ti->ti_ack, tp->t_rtseq))
572 tcp_xmit_timer(tp, tp->t_rtt);
573 acked = ti->ti_ack - tp->snd_una;
574 tcpstat.tcps_rcvackpack++;
575 tcpstat.tcps_rcvackbyte += acked;
576 sbdrop(&so->so_snd, acked);
577 tp->snd_una = ti->ti_ack;
578 m_freem(m);
579
580 /*
581 * If all outstanding data are acked, stop
582 * retransmit timer, otherwise restart timer
583 * using current (possibly backed-off) value.
584 * If process is waiting for space,
585 * wakeup/selwakeup/signal. If data
586 * are ready to send, let tcp_output
587 * decide between more output or persist.
588 */
589 if (tp->snd_una == tp->snd_max)
590 tp->t_timer[TCPT_REXMT] = 0;
591 else if (tp->t_timer[TCPT_PERSIST] == 0)
592 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
593
594 if (sb_notify(&so->so_snd))
595 sowwakeup(so);
596 if (so->so_snd.sb_cc)
597 (void) tcp_output(tp);
598 return;
599 }
600 } else if (ti->ti_ack == tp->snd_una &&
601 tp->segq.lh_first == NULL &&
602 ti->ti_len <= sbspace(&so->so_rcv)) {
603 /*
604 * this is a pure, in-sequence data packet
605 * with nothing on the reassembly queue and
606 * we have enough buffer space to take it.
607 */
608 ++tcpstat.tcps_preddat;
609 tp->rcv_nxt += ti->ti_len;
610 tcpstat.tcps_rcvpack++;
611 tcpstat.tcps_rcvbyte += ti->ti_len;
612 /*
613 * Drop TCP, IP headers and TCP options then add data
614 * to socket buffer.
615 */
616 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
617 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
618 sbappend(&so->so_rcv, m);
619 sorwakeup(so);
620 TCP_SETUP_ACK(tp, ti);
621 if (tp->t_flags & TF_ACKNOW)
622 (void) tcp_output(tp);
623 return;
624 }
625 }
626
627 /*
628 * Drop TCP, IP headers and TCP options.
629 */
630 hdroptlen = sizeof(struct tcpiphdr) + off - sizeof(struct tcphdr);
631 m->m_data += hdroptlen;
632 m->m_len -= hdroptlen;
633
634 /*
635 * Calculate amount of space in receive window,
636 * and then do TCP input processing.
637 * Receive window is amount of space in rcv queue,
638 * but not less than advertised window.
639 */
640 { int win;
641
642 win = sbspace(&so->so_rcv);
643 if (win < 0)
644 win = 0;
645 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
646 }
647
648 switch (tp->t_state) {
649
650 /*
651 * If the state is SYN_SENT:
652 * if seg contains an ACK, but not for our SYN, drop the input.
653 * if seg contains a RST, then drop the connection.
654 * if seg does not contain SYN, then drop it.
655 * Otherwise this is an acceptable SYN segment
656 * initialize tp->rcv_nxt and tp->irs
657 * if seg contains ack then advance tp->snd_una
658 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
659 * arrange for segment to be acked (eventually)
660 * continue processing rest of data/controls, beginning with URG
661 */
662 case TCPS_SYN_SENT:
663 if ((tiflags & TH_ACK) &&
664 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
665 SEQ_GT(ti->ti_ack, tp->snd_max)))
666 goto dropwithreset;
667 if (tiflags & TH_RST) {
668 if (tiflags & TH_ACK)
669 tp = tcp_drop(tp, ECONNREFUSED);
670 goto drop;
671 }
672 if ((tiflags & TH_SYN) == 0)
673 goto drop;
674 if (tiflags & TH_ACK) {
675 tp->snd_una = ti->ti_ack;
676 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
677 tp->snd_nxt = tp->snd_una;
678 }
679 tp->t_timer[TCPT_REXMT] = 0;
680 tp->irs = ti->ti_seq;
681 tcp_rcvseqinit(tp);
682 tp->t_flags |= TF_ACKNOW;
683 tcp_mss_from_peer(tp, opti.maxseg);
684 tcp_rmx_rtt(tp);
685 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
686 tcpstat.tcps_connects++;
687 soisconnected(so);
688 tcp_established(tp);
689 /* Do window scaling on this connection? */
690 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
691 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
692 tp->snd_scale = tp->requested_s_scale;
693 tp->rcv_scale = tp->request_r_scale;
694 }
695 (void) tcp_reass(tp, (struct tcpiphdr *)0,
696 (struct mbuf *)0);
697 /*
698 * if we didn't have to retransmit the SYN,
699 * use its rtt as our initial srtt & rtt var.
700 */
701 if (tp->t_rtt)
702 tcp_xmit_timer(tp, tp->t_rtt);
703 } else
704 tp->t_state = TCPS_SYN_RECEIVED;
705
706 /*
707 * Advance ti->ti_seq to correspond to first data byte.
708 * If data, trim to stay within window,
709 * dropping FIN if necessary.
710 */
711 ti->ti_seq++;
712 if (ti->ti_len > tp->rcv_wnd) {
713 todrop = ti->ti_len - tp->rcv_wnd;
714 m_adj(m, -todrop);
715 ti->ti_len = tp->rcv_wnd;
716 tiflags &= ~TH_FIN;
717 tcpstat.tcps_rcvpackafterwin++;
718 tcpstat.tcps_rcvbyteafterwin += todrop;
719 }
720 tp->snd_wl1 = ti->ti_seq - 1;
721 tp->rcv_up = ti->ti_seq;
722 goto step6;
723
724 /*
725 * If the state is SYN_RECEIVED:
726 * If seg contains an ACK, but not for our SYN, drop the input
727 * and generate an RST. See page 36, rfc793
728 */
729 case TCPS_SYN_RECEIVED:
730 if ((tiflags & TH_ACK) &&
731 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
732 SEQ_GT(ti->ti_ack, tp->snd_max)))
733 goto dropwithreset;
734 break;
735 }
736
737 /*
738 * States other than LISTEN or SYN_SENT.
739 * First check timestamp, if present.
740 * Then check that at least some bytes of segment are within
741 * receive window. If segment begins before rcv_nxt,
742 * drop leading data (and SYN); if nothing left, just ack.
743 *
744 * RFC 1323 PAWS: If we have a timestamp reply on this segment
745 * and it's less than ts_recent, drop it.
746 */
747 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
748 TSTMP_LT(opti.ts_val, tp->ts_recent)) {
749
750 /* Check to see if ts_recent is over 24 days old. */
751 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
752 /*
753 * Invalidate ts_recent. If this segment updates
754 * ts_recent, the age will be reset later and ts_recent
755 * will get a valid value. If it does not, setting
756 * ts_recent to zero will at least satisfy the
757 * requirement that zero be placed in the timestamp
758 * echo reply when ts_recent isn't valid. The
759 * age isn't reset until we get a valid ts_recent
760 * because we don't want out-of-order segments to be
761 * dropped when ts_recent is old.
762 */
763 tp->ts_recent = 0;
764 } else {
765 tcpstat.tcps_rcvduppack++;
766 tcpstat.tcps_rcvdupbyte += ti->ti_len;
767 tcpstat.tcps_pawsdrop++;
768 goto dropafterack;
769 }
770 }
771
772 todrop = tp->rcv_nxt - ti->ti_seq;
773 if (todrop > 0) {
774 if (tiflags & TH_SYN) {
775 tiflags &= ~TH_SYN;
776 ti->ti_seq++;
777 if (ti->ti_urp > 1)
778 ti->ti_urp--;
779 else {
780 tiflags &= ~TH_URG;
781 ti->ti_urp = 0;
782 }
783 todrop--;
784 }
785 if (todrop > ti->ti_len ||
786 (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
787 /*
788 * Any valid FIN must be to the left of the window.
789 * At this point the FIN must be a duplicate or
790 * out of sequence; drop it.
791 */
792 tiflags &= ~TH_FIN;
793 /*
794 * Send an ACK to resynchronize and drop any data.
795 * But keep on processing for RST or ACK.
796 */
797 tp->t_flags |= TF_ACKNOW;
798 todrop = ti->ti_len;
799 tcpstat.tcps_rcvdupbyte += todrop;
800 tcpstat.tcps_rcvduppack++;
801 } else {
802 tcpstat.tcps_rcvpartduppack++;
803 tcpstat.tcps_rcvpartdupbyte += todrop;
804 }
805 m_adj(m, todrop);
806 ti->ti_seq += todrop;
807 ti->ti_len -= todrop;
808 if (ti->ti_urp > todrop)
809 ti->ti_urp -= todrop;
810 else {
811 tiflags &= ~TH_URG;
812 ti->ti_urp = 0;
813 }
814 }
815
816 /*
817 * If new data are received on a connection after the
818 * user processes are gone, then RST the other end.
819 */
820 if ((so->so_state & SS_NOFDREF) &&
821 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
822 tp = tcp_close(tp);
823 tcpstat.tcps_rcvafterclose++;
824 goto dropwithreset;
825 }
826
827 /*
828 * If segment ends after window, drop trailing data
829 * (and PUSH and FIN); if nothing left, just ACK.
830 */
831 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
832 if (todrop > 0) {
833 tcpstat.tcps_rcvpackafterwin++;
834 if (todrop >= ti->ti_len) {
835 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
836 /*
837 * If a new connection request is received
838 * while in TIME_WAIT, drop the old connection
839 * and start over if the sequence numbers
840 * are above the previous ones.
841 */
842 if (tiflags & TH_SYN &&
843 tp->t_state == TCPS_TIME_WAIT &&
844 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
845 iss = tcp_new_iss(tp, sizeof(struct tcpcb),
846 tp->rcv_nxt);
847 tp = tcp_close(tp);
848 /*
849 * We have already advanced the mbuf
850 * pointers past the IP+TCP headers and
851 * options. Restore those pointers before
852 * attempting to use the TCP header again.
853 */
854 m->m_data -= hdroptlen;
855 m->m_len += hdroptlen;
856 goto findpcb;
857 }
858 /*
859 * If window is closed can only take segments at
860 * window edge, and have to drop data and PUSH from
861 * incoming segments. Continue processing, but
862 * remember to ack. Otherwise, drop segment
863 * and ack.
864 */
865 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
866 tp->t_flags |= TF_ACKNOW;
867 tcpstat.tcps_rcvwinprobe++;
868 } else
869 goto dropafterack;
870 } else
871 tcpstat.tcps_rcvbyteafterwin += todrop;
872 m_adj(m, -todrop);
873 ti->ti_len -= todrop;
874 tiflags &= ~(TH_PUSH|TH_FIN);
875 }
876
877 /*
878 * If last ACK falls within this segment's sequence numbers,
879 * record its timestamp.
880 */
881 if (opti.ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
882 SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
883 ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
884 tp->ts_recent_age = tcp_now;
885 tp->ts_recent = opti.ts_val;
886 }
887
888 /*
889 * If the RST bit is set examine the state:
890 * SYN_RECEIVED STATE:
891 * If passive open, return to LISTEN state.
892 * If active open, inform user that connection was refused.
893 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
894 * Inform user that connection was reset, and close tcb.
895 * CLOSING, LAST_ACK, TIME_WAIT STATES
896 * Close the tcb.
897 */
898 if (tiflags&TH_RST) switch (tp->t_state) {
899
900 case TCPS_SYN_RECEIVED:
901 so->so_error = ECONNREFUSED;
902 goto close;
903
904 case TCPS_ESTABLISHED:
905 case TCPS_FIN_WAIT_1:
906 case TCPS_FIN_WAIT_2:
907 case TCPS_CLOSE_WAIT:
908 so->so_error = ECONNRESET;
909 close:
910 tp->t_state = TCPS_CLOSED;
911 tcpstat.tcps_drops++;
912 tp = tcp_close(tp);
913 goto drop;
914
915 case TCPS_CLOSING:
916 case TCPS_LAST_ACK:
917 case TCPS_TIME_WAIT:
918 tp = tcp_close(tp);
919 goto drop;
920 }
921
922 /*
923 * If a SYN is in the window, then this is an
924 * error and we send an RST and drop the connection.
925 */
926 if (tiflags & TH_SYN) {
927 tp = tcp_drop(tp, ECONNRESET);
928 goto dropwithreset;
929 }
930
931 /*
932 * If the ACK bit is off we drop the segment and return.
933 */
934 if ((tiflags & TH_ACK) == 0)
935 goto drop;
936
937 /*
938 * Ack processing.
939 */
940 switch (tp->t_state) {
941
942 /*
943 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
944 * ESTABLISHED state and continue processing, otherwise
945 * send an RST.
946 */
947 case TCPS_SYN_RECEIVED:
948 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
949 SEQ_GT(ti->ti_ack, tp->snd_max))
950 goto dropwithreset;
951 tcpstat.tcps_connects++;
952 soisconnected(so);
953 tcp_established(tp);
954 /* Do window scaling? */
955 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
956 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
957 tp->snd_scale = tp->requested_s_scale;
958 tp->rcv_scale = tp->request_r_scale;
959 }
960 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
961 tp->snd_wl1 = ti->ti_seq - 1;
962 /* fall into ... */
963
964 /*
965 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
966 * ACKs. If the ack is in the range
967 * tp->snd_una < ti->ti_ack <= tp->snd_max
968 * then advance tp->snd_una to ti->ti_ack and drop
969 * data from the retransmission queue. If this ACK reflects
970 * more up to date window information we update our window information.
971 */
972 case TCPS_ESTABLISHED:
973 case TCPS_FIN_WAIT_1:
974 case TCPS_FIN_WAIT_2:
975 case TCPS_CLOSE_WAIT:
976 case TCPS_CLOSING:
977 case TCPS_LAST_ACK:
978 case TCPS_TIME_WAIT:
979
980 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
981 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
982 tcpstat.tcps_rcvdupack++;
983 /*
984 * If we have outstanding data (other than
985 * a window probe), this is a completely
986 * duplicate ack (ie, window info didn't
987 * change), the ack is the biggest we've
988 * seen and we've seen exactly our rexmt
989 * threshhold of them, assume a packet
990 * has been dropped and retransmit it.
991 * Kludge snd_nxt & the congestion
992 * window so we send only this one
993 * packet.
994 *
995 * We know we're losing at the current
996 * window size so do congestion avoidance
997 * (set ssthresh to half the current window
998 * and pull our congestion window back to
999 * the new ssthresh).
1000 *
1001 * Dup acks mean that packets have left the
1002 * network (they're now cached at the receiver)
1003 * so bump cwnd by the amount in the receiver
1004 * to keep a constant cwnd packets in the
1005 * network.
1006 */
1007 if (tp->t_timer[TCPT_REXMT] == 0 ||
1008 ti->ti_ack != tp->snd_una)
1009 tp->t_dupacks = 0;
1010 else if (++tp->t_dupacks == tcprexmtthresh) {
1011 tcp_seq onxt = tp->snd_nxt;
1012 u_int win =
1013 min(tp->snd_wnd, tp->snd_cwnd) /
1014 2 / tp->t_segsz;
1015
1016 if (win < 2)
1017 win = 2;
1018 tp->snd_ssthresh = win * tp->t_segsz;
1019 tp->t_timer[TCPT_REXMT] = 0;
1020 tp->t_rtt = 0;
1021 tp->snd_nxt = ti->ti_ack;
1022 tp->snd_cwnd = tp->t_segsz;
1023 (void) tcp_output(tp);
1024 tp->snd_cwnd = tp->snd_ssthresh +
1025 tp->t_segsz * tp->t_dupacks;
1026 if (SEQ_GT(onxt, tp->snd_nxt))
1027 tp->snd_nxt = onxt;
1028 goto drop;
1029 } else if (tp->t_dupacks > tcprexmtthresh) {
1030 tp->snd_cwnd += tp->t_segsz;
1031 (void) tcp_output(tp);
1032 goto drop;
1033 }
1034 } else
1035 tp->t_dupacks = 0;
1036 break;
1037 }
1038 /*
1039 * If the congestion window was inflated to account
1040 * for the other side's cached packets, retract it.
1041 */
1042 if (tp->t_dupacks >= tcprexmtthresh &&
1043 tp->snd_cwnd > tp->snd_ssthresh)
1044 tp->snd_cwnd = tp->snd_ssthresh;
1045 tp->t_dupacks = 0;
1046 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1047 tcpstat.tcps_rcvacktoomuch++;
1048 goto dropafterack;
1049 }
1050 acked = ti->ti_ack - tp->snd_una;
1051 tcpstat.tcps_rcvackpack++;
1052 tcpstat.tcps_rcvackbyte += acked;
1053
1054 /*
1055 * If we have a timestamp reply, update smoothed
1056 * round trip time. If no timestamp is present but
1057 * transmit timer is running and timed sequence
1058 * number was acked, update smoothed round trip time.
1059 * Since we now have an rtt measurement, cancel the
1060 * timer backoff (cf., Phil Karn's retransmit alg.).
1061 * Recompute the initial retransmit timer.
1062 */
1063 if (opti.ts_present)
1064 tcp_xmit_timer(tp, tcp_now - opti.ts_ecr + 1);
1065 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1066 tcp_xmit_timer(tp,tp->t_rtt);
1067
1068 /*
1069 * If all outstanding data is acked, stop retransmit
1070 * timer and remember to restart (more output or persist).
1071 * If there is more data to be acked, restart retransmit
1072 * timer, using current (possibly backed-off) value.
1073 */
1074 if (ti->ti_ack == tp->snd_max) {
1075 tp->t_timer[TCPT_REXMT] = 0;
1076 needoutput = 1;
1077 } else if (tp->t_timer[TCPT_PERSIST] == 0)
1078 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1079 /*
1080 * When new data is acked, open the congestion window.
1081 * If the window gives us less than ssthresh packets
1082 * in flight, open exponentially (segsz per packet).
1083 * Otherwise open linearly: segsz per window
1084 * (segsz^2 / cwnd per packet), plus a constant
1085 * fraction of a packet (segsz/8) to help larger windows
1086 * open quickly enough.
1087 */
1088 {
1089 register u_int cw = tp->snd_cwnd;
1090 register u_int incr = tp->t_segsz;
1091
1092 if (cw > tp->snd_ssthresh)
1093 incr = incr * incr / cw;
1094 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1095 }
1096 if (acked > so->so_snd.sb_cc) {
1097 tp->snd_wnd -= so->so_snd.sb_cc;
1098 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1099 ourfinisacked = 1;
1100 } else {
1101 sbdrop(&so->so_snd, acked);
1102 tp->snd_wnd -= acked;
1103 ourfinisacked = 0;
1104 }
1105 if (sb_notify(&so->so_snd))
1106 sowwakeup(so);
1107 tp->snd_una = ti->ti_ack;
1108 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1109 tp->snd_nxt = tp->snd_una;
1110
1111 switch (tp->t_state) {
1112
1113 /*
1114 * In FIN_WAIT_1 STATE in addition to the processing
1115 * for the ESTABLISHED state if our FIN is now acknowledged
1116 * then enter FIN_WAIT_2.
1117 */
1118 case TCPS_FIN_WAIT_1:
1119 if (ourfinisacked) {
1120 /*
1121 * If we can't receive any more
1122 * data, then closing user can proceed.
1123 * Starting the timer is contrary to the
1124 * specification, but if we don't get a FIN
1125 * we'll hang forever.
1126 */
1127 if (so->so_state & SS_CANTRCVMORE) {
1128 soisdisconnected(so);
1129 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1130 }
1131 tp->t_state = TCPS_FIN_WAIT_2;
1132 }
1133 break;
1134
1135 /*
1136 * In CLOSING STATE in addition to the processing for
1137 * the ESTABLISHED state if the ACK acknowledges our FIN
1138 * then enter the TIME-WAIT state, otherwise ignore
1139 * the segment.
1140 */
1141 case TCPS_CLOSING:
1142 if (ourfinisacked) {
1143 tp->t_state = TCPS_TIME_WAIT;
1144 tcp_canceltimers(tp);
1145 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1146 soisdisconnected(so);
1147 }
1148 break;
1149
1150 /*
1151 * In LAST_ACK, we may still be waiting for data to drain
1152 * and/or to be acked, as well as for the ack of our FIN.
1153 * If our FIN is now acknowledged, delete the TCB,
1154 * enter the closed state and return.
1155 */
1156 case TCPS_LAST_ACK:
1157 if (ourfinisacked) {
1158 tp = tcp_close(tp);
1159 goto drop;
1160 }
1161 break;
1162
1163 /*
1164 * In TIME_WAIT state the only thing that should arrive
1165 * is a retransmission of the remote FIN. Acknowledge
1166 * it and restart the finack timer.
1167 */
1168 case TCPS_TIME_WAIT:
1169 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1170 goto dropafterack;
1171 }
1172 }
1173
1174 step6:
1175 /*
1176 * Update window information.
1177 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1178 */
1179 if (((tiflags & TH_ACK) && SEQ_LT(tp->snd_wl1, ti->ti_seq)) ||
1180 (tp->snd_wl1 == ti->ti_seq && SEQ_LT(tp->snd_wl2, ti->ti_ack)) ||
1181 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)) {
1182 /* keep track of pure window updates */
1183 if (ti->ti_len == 0 &&
1184 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1185 tcpstat.tcps_rcvwinupd++;
1186 tp->snd_wnd = tiwin;
1187 tp->snd_wl1 = ti->ti_seq;
1188 tp->snd_wl2 = ti->ti_ack;
1189 if (tp->snd_wnd > tp->max_sndwnd)
1190 tp->max_sndwnd = tp->snd_wnd;
1191 needoutput = 1;
1192 }
1193
1194 /*
1195 * Process segments with URG.
1196 */
1197 if ((tiflags & TH_URG) && ti->ti_urp &&
1198 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1199 /*
1200 * This is a kludge, but if we receive and accept
1201 * random urgent pointers, we'll crash in
1202 * soreceive. It's hard to imagine someone
1203 * actually wanting to send this much urgent data.
1204 */
1205 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
1206 ti->ti_urp = 0; /* XXX */
1207 tiflags &= ~TH_URG; /* XXX */
1208 goto dodata; /* XXX */
1209 }
1210 /*
1211 * If this segment advances the known urgent pointer,
1212 * then mark the data stream. This should not happen
1213 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1214 * a FIN has been received from the remote side.
1215 * In these states we ignore the URG.
1216 *
1217 * According to RFC961 (Assigned Protocols),
1218 * the urgent pointer points to the last octet
1219 * of urgent data. We continue, however,
1220 * to consider it to indicate the first octet
1221 * of data past the urgent section as the original
1222 * spec states (in one of two places).
1223 */
1224 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1225 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1226 so->so_oobmark = so->so_rcv.sb_cc +
1227 (tp->rcv_up - tp->rcv_nxt) - 1;
1228 if (so->so_oobmark == 0)
1229 so->so_state |= SS_RCVATMARK;
1230 sohasoutofband(so);
1231 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1232 }
1233 /*
1234 * Remove out of band data so doesn't get presented to user.
1235 * This can happen independent of advancing the URG pointer,
1236 * but if two URG's are pending at once, some out-of-band
1237 * data may creep in... ick.
1238 */
1239 if (ti->ti_urp <= (u_int16_t) ti->ti_len
1240 #ifdef SO_OOBINLINE
1241 && (so->so_options & SO_OOBINLINE) == 0
1242 #endif
1243 )
1244 tcp_pulloutofband(so, ti, m);
1245 } else
1246 /*
1247 * If no out of band data is expected,
1248 * pull receive urgent pointer along
1249 * with the receive window.
1250 */
1251 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1252 tp->rcv_up = tp->rcv_nxt;
1253 dodata: /* XXX */
1254
1255 /*
1256 * Process the segment text, merging it into the TCP sequencing queue,
1257 * and arranging for acknowledgment of receipt if necessary.
1258 * This process logically involves adjusting tp->rcv_wnd as data
1259 * is presented to the user (this happens in tcp_usrreq.c,
1260 * case PRU_RCVD). If a FIN has already been received on this
1261 * connection then we just ignore the text.
1262 */
1263 if ((ti->ti_len || (tiflags & TH_FIN)) &&
1264 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1265 TCP_REASS(tp, ti, m, so, tiflags);
1266 /*
1267 * Note the amount of data that peer has sent into
1268 * our window, in order to estimate the sender's
1269 * buffer size.
1270 */
1271 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1272 } else {
1273 m_freem(m);
1274 tiflags &= ~TH_FIN;
1275 }
1276
1277 /*
1278 * If FIN is received ACK the FIN and let the user know
1279 * that the connection is closing. Ignore a FIN received before
1280 * the connection is fully established.
1281 */
1282 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
1283 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1284 socantrcvmore(so);
1285 tp->t_flags |= TF_ACKNOW;
1286 tp->rcv_nxt++;
1287 }
1288 switch (tp->t_state) {
1289
1290 /*
1291 * In ESTABLISHED STATE enter the CLOSE_WAIT state.
1292 */
1293 case TCPS_ESTABLISHED:
1294 tp->t_state = TCPS_CLOSE_WAIT;
1295 break;
1296
1297 /*
1298 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1299 * enter the CLOSING state.
1300 */
1301 case TCPS_FIN_WAIT_1:
1302 tp->t_state = TCPS_CLOSING;
1303 break;
1304
1305 /*
1306 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1307 * starting the time-wait timer, turning off the other
1308 * standard timers.
1309 */
1310 case TCPS_FIN_WAIT_2:
1311 tp->t_state = TCPS_TIME_WAIT;
1312 tcp_canceltimers(tp);
1313 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1314 soisdisconnected(so);
1315 break;
1316
1317 /*
1318 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1319 */
1320 case TCPS_TIME_WAIT:
1321 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1322 break;
1323 }
1324 }
1325 if (so->so_options & SO_DEBUG)
1326 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1327
1328 /*
1329 * Return any desired output.
1330 */
1331 if (needoutput || (tp->t_flags & TF_ACKNOW))
1332 (void) tcp_output(tp);
1333 return;
1334
1335 badsyn:
1336 /*
1337 * Received a bad SYN. Increment counters and dropwithreset.
1338 */
1339 tcpstat.tcps_badsyn++;
1340 tp = NULL;
1341 goto dropwithreset;
1342
1343 dropafterack:
1344 /*
1345 * Generate an ACK dropping incoming segment if it occupies
1346 * sequence space, where the ACK reflects our state.
1347 */
1348 if (tiflags & TH_RST)
1349 goto drop;
1350 m_freem(m);
1351 tp->t_flags |= TF_ACKNOW;
1352 (void) tcp_output(tp);
1353 return;
1354
1355 dropwithreset:
1356 /*
1357 * Generate a RST, dropping incoming segment.
1358 * Make ACK acceptable to originator of segment.
1359 * Don't bother to respond if destination was broadcast/multicast.
1360 */
1361 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1362 IN_MULTICAST(ti->ti_dst.s_addr))
1363 goto drop;
1364 if (tiflags & TH_ACK)
1365 (void)tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1366 else {
1367 if (tiflags & TH_SYN)
1368 ti->ti_len++;
1369 (void)tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1370 TH_RST|TH_ACK);
1371 }
1372 return;
1373
1374 drop:
1375 /*
1376 * Drop space held by incoming segment and return.
1377 */
1378 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1379 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1380 m_freem(m);
1381 return;
1382 #ifndef TUBA_INCLUDE
1383 }
1384
1385 void
1386 tcp_dooptions(tp, cp, cnt, ti, oi)
1387 struct tcpcb *tp;
1388 u_char *cp;
1389 int cnt;
1390 struct tcpiphdr *ti;
1391 struct tcp_opt_info *oi;
1392 {
1393 u_int16_t mss;
1394 int opt, optlen;
1395
1396 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1397 opt = cp[0];
1398 if (opt == TCPOPT_EOL)
1399 break;
1400 if (opt == TCPOPT_NOP)
1401 optlen = 1;
1402 else {
1403 optlen = cp[1];
1404 if (optlen <= 0)
1405 break;
1406 }
1407 switch (opt) {
1408
1409 default:
1410 continue;
1411
1412 case TCPOPT_MAXSEG:
1413 if (optlen != TCPOLEN_MAXSEG)
1414 continue;
1415 if (!(ti->ti_flags & TH_SYN))
1416 continue;
1417 bcopy(cp + 2, &mss, sizeof(mss));
1418 oi->maxseg = ntohs(mss);
1419 break;
1420
1421 case TCPOPT_WINDOW:
1422 if (optlen != TCPOLEN_WINDOW)
1423 continue;
1424 if (!(ti->ti_flags & TH_SYN))
1425 continue;
1426 tp->t_flags |= TF_RCVD_SCALE;
1427 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1428 break;
1429
1430 case TCPOPT_TIMESTAMP:
1431 if (optlen != TCPOLEN_TIMESTAMP)
1432 continue;
1433 oi->ts_present = 1;
1434 bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
1435 NTOHL(oi->ts_val);
1436 bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
1437 NTOHL(oi->ts_ecr);
1438
1439 /*
1440 * A timestamp received in a SYN makes
1441 * it ok to send timestamp requests and replies.
1442 */
1443 if (ti->ti_flags & TH_SYN) {
1444 tp->t_flags |= TF_RCVD_TSTMP;
1445 tp->ts_recent = oi->ts_val;
1446 tp->ts_recent_age = tcp_now;
1447 }
1448 break;
1449 }
1450 }
1451 }
1452
1453 /*
1454 * Pull out of band byte out of a segment so
1455 * it doesn't appear in the user's data queue.
1456 * It is still reflected in the segment length for
1457 * sequencing purposes.
1458 */
1459 void
1460 tcp_pulloutofband(so, ti, m)
1461 struct socket *so;
1462 struct tcpiphdr *ti;
1463 register struct mbuf *m;
1464 {
1465 int cnt = ti->ti_urp - 1;
1466
1467 while (cnt >= 0) {
1468 if (m->m_len > cnt) {
1469 char *cp = mtod(m, caddr_t) + cnt;
1470 struct tcpcb *tp = sototcpcb(so);
1471
1472 tp->t_iobc = *cp;
1473 tp->t_oobflags |= TCPOOB_HAVEDATA;
1474 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1475 m->m_len--;
1476 return;
1477 }
1478 cnt -= m->m_len;
1479 m = m->m_next;
1480 if (m == 0)
1481 break;
1482 }
1483 panic("tcp_pulloutofband");
1484 }
1485
1486 /*
1487 * Collect new round-trip time estimate
1488 * and update averages and current timeout.
1489 */
1490 void
1491 tcp_xmit_timer(tp, rtt)
1492 register struct tcpcb *tp;
1493 short rtt;
1494 {
1495 register short delta;
1496
1497 tcpstat.tcps_rttupdated++;
1498 --rtt;
1499 if (tp->t_srtt != 0) {
1500 /*
1501 * srtt is stored as fixed point with 3 bits after the
1502 * binary point (i.e., scaled by 8). The following magic
1503 * is equivalent to the smoothing algorithm in rfc793 with
1504 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1505 * point). Adjust rtt to origin 0.
1506 */
1507 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
1508 if ((tp->t_srtt += delta) <= 0)
1509 tp->t_srtt = 1 << 2;
1510 /*
1511 * We accumulate a smoothed rtt variance (actually, a
1512 * smoothed mean difference), then set the retransmit
1513 * timer to smoothed rtt + 4 times the smoothed variance.
1514 * rttvar is stored as fixed point with 2 bits after the
1515 * binary point (scaled by 4). The following is
1516 * equivalent to rfc793 smoothing with an alpha of .75
1517 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1518 * rfc793's wired-in beta.
1519 */
1520 if (delta < 0)
1521 delta = -delta;
1522 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1523 if ((tp->t_rttvar += delta) <= 0)
1524 tp->t_rttvar = 1 << 2;
1525 } else {
1526 /*
1527 * No rtt measurement yet - use the unsmoothed rtt.
1528 * Set the variance to half the rtt (so our first
1529 * retransmit happens at 3*rtt).
1530 */
1531 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
1532 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
1533 }
1534 tp->t_rtt = 0;
1535 tp->t_rxtshift = 0;
1536
1537 /*
1538 * the retransmit should happen at rtt + 4 * rttvar.
1539 * Because of the way we do the smoothing, srtt and rttvar
1540 * will each average +1/2 tick of bias. When we compute
1541 * the retransmit timer, we want 1/2 tick of rounding and
1542 * 1 extra tick because of +-1/2 tick uncertainty in the
1543 * firing of the timer. The bias will give us exactly the
1544 * 1.5 tick we need. But, because the bias is
1545 * statistical, we have to test that we don't drop below
1546 * the minimum feasible timer (which is 2 ticks).
1547 */
1548 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1549 rtt + 2, TCPTV_REXMTMAX);
1550
1551 /*
1552 * We received an ack for a packet that wasn't retransmitted;
1553 * it is probably safe to discard any error indications we've
1554 * received recently. This isn't quite right, but close enough
1555 * for now (a route might have failed after we sent a segment,
1556 * and the return path might not be symmetrical).
1557 */
1558 tp->t_softerror = 0;
1559 }
1560
1561 /*
1562 * TCP compressed state engine. Currently used to hold compressed
1563 * state for SYN_RECEIVED.
1564 */
1565
1566 u_long syn_cache_count;
1567 u_int32_t syn_hash1, syn_hash2;
1568
1569 #define SYN_HASH(sa, sp, dp) \
1570 ((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
1571 ((u_int32_t)(sp)))^syn_hash2)) \
1572 & 0x7fffffff)
1573
1574 #define eptosp(ep, e, s) ((struct s *)((char *)(ep) - \
1575 ((char *)(&((struct s *)0)->e) - (char *)0)))
1576
1577 #define SYN_CACHE_RM(sc, p, scp) { \
1578 *(p) = (sc)->sc_next; \
1579 if ((sc)->sc_next) \
1580 (sc)->sc_next->sc_timer += (sc)->sc_timer; \
1581 else { \
1582 (scp)->sch_timer_sum -= (sc)->sc_timer; \
1583 if ((scp)->sch_timer_sum <= 0) \
1584 (scp)->sch_timer_sum = -1; \
1585 /* If need be, fix up the last pointer */ \
1586 if ((scp)->sch_first) \
1587 (scp)->sch_last = eptosp(p, sc_next, syn_cache); \
1588 } \
1589 (scp)->sch_length--; \
1590 syn_cache_count--; \
1591 }
1592
1593 void
1594 syn_cache_insert(sc, prevp, headp)
1595 struct syn_cache *sc;
1596 struct syn_cache ***prevp;
1597 struct syn_cache_head **headp;
1598 {
1599 struct syn_cache_head *scp, *scp2, *sce;
1600 struct syn_cache *sc2;
1601 static u_int timeo_val;
1602 int s;
1603
1604 /* Initialize the hash secrets when adding the first entry */
1605 if (syn_cache_count == 0) {
1606 struct timeval tv;
1607 microtime(&tv);
1608 syn_hash1 = random() ^ (u_long)≻
1609 syn_hash2 = random() ^ tv.tv_usec;
1610 }
1611
1612 sc->sc_hash = SYN_HASH(&sc->sc_src, sc->sc_sport, sc->sc_dport);
1613 sc->sc_next = NULL;
1614 scp = &tcp_syn_cache[sc->sc_hash % tcp_syn_cache_size];
1615 *headp = scp;
1616
1617 /*
1618 * Make sure that we don't overflow the per-bucket
1619 * limit or the total cache size limit.
1620 */
1621 s = splsoftnet();
1622 if (scp->sch_length >= tcp_syn_bucket_limit) {
1623 tcpstat.tcps_sc_bucketoverflow++;
1624 sc2 = scp->sch_first;
1625 scp->sch_first = sc2->sc_next;
1626 FREE(sc2, M_PCB);
1627 } else if (syn_cache_count >= tcp_syn_cache_limit) {
1628 tcpstat.tcps_sc_overflowed++;
1629 /*
1630 * The cache is full. Toss the first (i.e, oldest)
1631 * element in this bucket.
1632 */
1633 scp2 = scp;
1634 if (scp2->sch_first == NULL) {
1635 sce = &tcp_syn_cache[tcp_syn_cache_size];
1636 for (++scp2; scp2 != scp; scp2++) {
1637 if (scp2 >= sce)
1638 scp2 = &tcp_syn_cache[0];
1639 if (scp2->sch_first)
1640 break;
1641 }
1642 }
1643 sc2 = scp2->sch_first;
1644 if (sc2 == NULL) {
1645 FREE(sc, M_PCB);
1646 return;
1647 }
1648 if ((scp2->sch_first = sc2->sc_next) == NULL)
1649 scp2->sch_last = NULL;
1650 else
1651 sc2->sc_next->sc_timer += sc2->sc_timer;
1652 FREE(sc2, M_PCB);
1653 } else {
1654 scp->sch_length++;
1655 syn_cache_count++;
1656 }
1657 tcpstat.tcps_sc_added++;
1658
1659 /*
1660 * Put it into the bucket.
1661 */
1662 if (scp->sch_first == NULL)
1663 *prevp = &scp->sch_first;
1664 else {
1665 *prevp = &scp->sch_last->sc_next;
1666 tcpstat.tcps_sc_collisions++;
1667 }
1668 **prevp = sc;
1669 scp->sch_last = sc;
1670
1671 /*
1672 * If the timeout value has changed
1673 * 1) force it to fit in a u_char
1674 * 2) Run the timer routine to truncate all
1675 * existing entries to the new timeout value.
1676 */
1677 if (timeo_val != tcp_syn_cache_timeo) {
1678 tcp_syn_cache_timeo = min(tcp_syn_cache_timeo, UCHAR_MAX);
1679 if (timeo_val > tcp_syn_cache_timeo)
1680 syn_cache_timer(timeo_val - tcp_syn_cache_timeo);
1681 timeo_val = tcp_syn_cache_timeo;
1682 }
1683 if (scp->sch_timer_sum > 0)
1684 sc->sc_timer = tcp_syn_cache_timeo - scp->sch_timer_sum;
1685 else {
1686 if (scp->sch_timer_sum == 0) {
1687 /*
1688 * When the bucket timer is 0, it is not in the
1689 * cache queue.
1690 */
1691 scp->sch_headq = tcp_syn_cache_first;
1692 tcp_syn_cache_first = scp;
1693 }
1694 sc->sc_timer = tcp_syn_cache_timeo;
1695 }
1696 scp->sch_timer_sum = tcp_syn_cache_timeo;
1697 splx(s);
1698 }
1699
1700 /*
1701 * Walk down the cache list, decrementing the timer of
1702 * the first element on each entry. If the timer goes
1703 * to zero, remove it and all successive entries with
1704 * a zero timer.
1705 */
1706 void
1707 syn_cache_timer(interval)
1708 int interval;
1709 {
1710 struct syn_cache_head *scp, **pscp;
1711 struct syn_cache *sc, *scn;
1712 int n, s;
1713
1714 pscp = &tcp_syn_cache_first;
1715 scp = tcp_syn_cache_first;
1716 s = splsoftnet();
1717 while (scp) {
1718 /*
1719 * Remove any empty hash buckets
1720 * from the cache queue.
1721 */
1722 if ((sc = scp->sch_first) == NULL) {
1723 *pscp = scp->sch_headq;
1724 scp->sch_headq = NULL;
1725 scp->sch_timer_sum = 0;
1726 scp->sch_first = scp->sch_last = NULL;
1727 scp->sch_length = 0;
1728 scp = *pscp;
1729 continue;
1730 }
1731
1732 scp->sch_timer_sum -= interval;
1733 if (scp->sch_timer_sum <= 0)
1734 scp->sch_timer_sum = -1;
1735 n = interval;
1736 while (sc->sc_timer <= n) {
1737 n -= sc->sc_timer;
1738 scn = sc->sc_next;
1739 tcpstat.tcps_sc_timed_out++;
1740 syn_cache_count--;
1741 FREE(sc, M_PCB);
1742 scp->sch_length--;
1743 if ((sc = scn) == NULL)
1744 break;
1745 }
1746 if ((scp->sch_first = sc) != NULL) {
1747 sc->sc_timer -= n;
1748 pscp = &scp->sch_headq;
1749 scp = scp->sch_headq;
1750 }
1751 }
1752 splx(s);
1753 }
1754
1755 /*
1756 * Find an entry in the syn cache.
1757 */
1758 struct syn_cache *
1759 syn_cache_lookup(ti, prevp, headp)
1760 struct tcpiphdr *ti;
1761 struct syn_cache ***prevp;
1762 struct syn_cache_head **headp;
1763 {
1764 struct syn_cache *sc, **prev;
1765 struct syn_cache_head *head;
1766 u_int32_t hash;
1767 int s;
1768
1769 hash = SYN_HASH(&ti->ti_src, ti->ti_sport, ti->ti_dport);
1770
1771 head = &tcp_syn_cache[hash % tcp_syn_cache_size];
1772 *headp = head;
1773 prev = &head->sch_first;
1774 s = splsoftnet();
1775 for (sc = head->sch_first; sc; prev = &sc->sc_next, sc = sc->sc_next) {
1776 if (sc->sc_hash != hash)
1777 continue;
1778 if (sc->sc_src.s_addr == ti->ti_src.s_addr &&
1779 sc->sc_sport == ti->ti_sport &&
1780 sc->sc_dport == ti->ti_dport &&
1781 sc->sc_dst.s_addr == ti->ti_dst.s_addr) {
1782 *prevp = prev;
1783 splx(s);
1784 return (sc);
1785 }
1786 }
1787 splx(s);
1788 return (NULL);
1789 }
1790
1791 /*
1792 * This function gets called when we receive an ACK for a
1793 * socket in the LISTEN state. We look up the connection
1794 * in the syn cache, and if its there, we pull it out of
1795 * the cache and turn it into a full-blown connection in
1796 * the SYN-RECEIVED state.
1797 *
1798 * The return values may not be immediately obvious, and their effects
1799 * can be subtle, so here they are:
1800 *
1801 * NULL SYN was not found in cache; caller should drop the
1802 * packet and send an RST.
1803 *
1804 * -1 We were unable to create the new connection, and are
1805 * aborting it. An ACK,RST is being sent to the peer
1806 * (unless we got screwey sequence numbners; see below),
1807 * because the 3-way handshake has been completed. Caller
1808 * should not free the mbuf, since we may be using it. If
1809 * we are not, we will free it.
1810 *
1811 * Otherwise, the return value is a pointer to the new socket
1812 * associated with the connection.
1813 */
1814 struct socket *
1815 syn_cache_get(so, m)
1816 struct socket *so;
1817 struct mbuf *m;
1818 {
1819 struct syn_cache *sc, **sc_prev;
1820 struct syn_cache_head *head;
1821 register struct inpcb *inp;
1822 register struct tcpcb *tp = 0;
1823 register struct tcpiphdr *ti;
1824 struct sockaddr_in *sin;
1825 struct mbuf *am;
1826 long win;
1827 int s;
1828
1829 ti = mtod(m, struct tcpiphdr *);
1830 s = splsoftnet();
1831 if ((sc = syn_cache_lookup(ti, &sc_prev, &head)) == NULL) {
1832 splx(s);
1833 return (NULL);
1834 }
1835
1836 win = sbspace(&so->so_rcv);
1837 if (win > TCP_MAXWIN)
1838 win = TCP_MAXWIN;
1839
1840 /*
1841 * Verify the sequence and ack numbers.
1842 */
1843 if ((ti->ti_ack != sc->sc_iss + 1) ||
1844 SEQ_LEQ(ti->ti_seq, sc->sc_irs) ||
1845 SEQ_GT(ti->ti_seq, sc->sc_irs + 1 + win)) {
1846 (void) syn_cache_respond(sc, m, ti, win, 0);
1847 splx(s);
1848 return ((struct socket *)(-1));
1849 }
1850
1851 /* Remove this cache entry */
1852 SYN_CACHE_RM(sc, sc_prev, head);
1853 splx(s);
1854
1855 /*
1856 * Ok, create the full blown connection, and set things up
1857 * as they would have been set up if we had created the
1858 * connection when the SYN arrived. If we can't create
1859 * the connection, abort it.
1860 */
1861 so = sonewconn(so, SS_ISCONNECTED);
1862 if (so == NULL)
1863 goto resetandabort;
1864
1865 inp = sotoinpcb(so);
1866 inp->inp_laddr = sc->sc_dst;
1867 inp->inp_lport = sc->sc_dport;
1868 in_pcbstate(inp, INP_BOUND);
1869 #if BSD>=43
1870 inp->inp_options = ip_srcroute();
1871 #endif
1872
1873 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
1874 if (am == NULL)
1875 goto resetandabort;
1876 am->m_len = sizeof(struct sockaddr_in);
1877 sin = mtod(am, struct sockaddr_in *);
1878 sin->sin_family = AF_INET;
1879 sin->sin_len = sizeof(*sin);
1880 sin->sin_addr = sc->sc_src;
1881 sin->sin_port = sc->sc_sport;
1882 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
1883 if (in_pcbconnect(inp, am)) {
1884 (void) m_free(am);
1885 goto resetandabort;
1886 }
1887 (void) m_free(am);
1888
1889 tp = intotcpcb(inp);
1890 if (sc->sc_request_r_scale != 15) {
1891 tp->requested_s_scale = sc->sc_requested_s_scale;
1892 tp->request_r_scale = sc->sc_request_r_scale;
1893 tp->snd_scale = sc->sc_requested_s_scale;
1894 tp->rcv_scale = sc->sc_request_r_scale;
1895 tp->t_flags |= TF_RCVD_SCALE;
1896 }
1897 if (sc->sc_tstmp)
1898 tp->t_flags |= TF_RCVD_TSTMP;
1899
1900 tp->t_template = tcp_template(tp);
1901 if (tp->t_template == 0) {
1902 tp = tcp_drop(tp, ENOBUFS); /* destroys socket */
1903 so = NULL;
1904 m_freem(m);
1905 goto abort;
1906 }
1907
1908 tp->iss = sc->sc_iss;
1909 tp->irs = sc->sc_irs;
1910 tcp_sendseqinit(tp);
1911 tcp_rcvseqinit(tp);
1912 tp->t_state = TCPS_SYN_RECEIVED;
1913 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
1914 tcpstat.tcps_accepts++;
1915
1916 /* Initialize tp->t_ourmss before we deal with the peer's! */
1917 tp->t_ourmss = sc->sc_ourmaxseg;
1918 tcp_mss_from_peer(tp, sc->sc_peermaxseg);
1919 tcp_rmx_rtt(tp);
1920 tp->snd_wl1 = sc->sc_irs;
1921 tp->rcv_up = sc->sc_irs + 1;
1922
1923 /*
1924 * This is what whould have happened in tcp_ouput() when
1925 * the SYN,ACK was sent.
1926 */
1927 tp->snd_up = tp->snd_una;
1928 tp->snd_max = tp->snd_nxt = tp->iss+1;
1929 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1930 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1931 tp->rcv_adv = tp->rcv_nxt + win;
1932 tp->last_ack_sent = tp->rcv_nxt;
1933
1934 tcpstat.tcps_sc_completed++;
1935 FREE(sc, M_PCB);
1936 return (so);
1937
1938 resetandabort:
1939 (void) tcp_respond(NULL, ti, m, ti->ti_seq+ti->ti_len,
1940 (tcp_seq)0, TH_RST|TH_ACK);
1941 abort:
1942 if (so != NULL)
1943 (void) soabort(so);
1944 FREE(sc, M_PCB);
1945 tcpstat.tcps_sc_aborted++;
1946 return ((struct socket *)(-1));
1947 }
1948
1949 /*
1950 * This function is called when we get a RST for a
1951 * non-existant connection, so that we can see if the
1952 * connection is in the syn cache. If it is, zap it.
1953 */
1954
1955 void
1956 syn_cache_reset(ti)
1957 register struct tcpiphdr *ti;
1958 {
1959 struct syn_cache *sc, **sc_prev;
1960 struct syn_cache_head *head;
1961 int s = splsoftnet();
1962
1963 if ((sc = syn_cache_lookup(ti, &sc_prev, &head)) == NULL) {
1964 splx(s);
1965 return;
1966 }
1967 if (SEQ_LT(ti->ti_seq,sc->sc_irs) ||
1968 SEQ_GT(ti->ti_seq, sc->sc_irs+1)) {
1969 splx(s);
1970 return;
1971 }
1972 SYN_CACHE_RM(sc, sc_prev, head);
1973 splx(s);
1974 tcpstat.tcps_sc_reset++;
1975 FREE(sc, M_PCB);
1976 }
1977
1978 void
1979 syn_cache_unreach(ip, th)
1980 struct ip *ip;
1981 struct tcphdr *th;
1982 {
1983 struct syn_cache *sc, **sc_prev;
1984 struct syn_cache_head *head;
1985 struct tcpiphdr ti2;
1986 int s;
1987
1988 ti2.ti_src.s_addr = ip->ip_dst.s_addr;
1989 ti2.ti_dst.s_addr = ip->ip_src.s_addr;
1990 ti2.ti_sport = th->th_dport;
1991 ti2.ti_dport = th->th_sport;
1992
1993 s = splsoftnet();
1994 if ((sc = syn_cache_lookup(&ti2, &sc_prev, &head)) == NULL) {
1995 splx(s);
1996 return;
1997 }
1998 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */
1999 if (ntohl (th->th_seq) != sc->sc_iss) {
2000 splx(s);
2001 return;
2002 }
2003 SYN_CACHE_RM(sc, sc_prev, head);
2004 splx(s);
2005 tcpstat.tcps_sc_unreach++;
2006 FREE(sc, M_PCB);
2007 }
2008
2009 /*
2010 * Given a LISTEN socket and an inbound SYN request, add
2011 * this to the syn cache, and send back a segment:
2012 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
2013 * to the source.
2014 *
2015 * XXX We don't properly handle SYN-with-data!
2016 */
2017
2018 int
2019 syn_cache_add(so, m, optp, optlen, oi)
2020 struct socket *so;
2021 struct mbuf *m;
2022 u_char *optp;
2023 int optlen;
2024 struct tcp_opt_info *oi;
2025 {
2026 register struct tcpiphdr *ti;
2027 struct tcpcb tb, *tp;
2028 long win;
2029 struct syn_cache *sc, **sc_prev;
2030 struct syn_cache_head *scp;
2031 extern int tcp_do_rfc1323;
2032
2033 tp = sototcpcb(so);
2034 ti = mtod(m, struct tcpiphdr *);
2035
2036 /*
2037 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
2038 * in_broadcast() should never return true on a received
2039 * packet with M_BCAST not set.
2040 */
2041 if (m->m_flags & (M_BCAST|M_MCAST) ||
2042 IN_MULTICAST(ti->ti_src.s_addr) ||
2043 IN_MULTICAST(ti->ti_dst.s_addr))
2044 return (0);
2045
2046 /*
2047 * Initialize some local state.
2048 */
2049 win = sbspace(&so->so_rcv);
2050 if (win > TCP_MAXWIN)
2051 win = TCP_MAXWIN;
2052
2053 if (optp) {
2054 tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
2055 tcp_dooptions(&tb, optp, optlen, ti, oi);
2056 } else
2057 tb.t_flags = 0;
2058
2059 /*
2060 * See if we already have an entry for this connection.
2061 */
2062 if ((sc = syn_cache_lookup(ti, &sc_prev, &scp)) != NULL) {
2063 tcpstat.tcps_sc_dupesyn++;
2064 if (syn_cache_respond(sc, m, ti, win, tb.ts_recent) == 0) {
2065 tcpstat.tcps_sndacks++;
2066 tcpstat.tcps_sndtotal++;
2067 }
2068 return (1);
2069 }
2070
2071 MALLOC(sc, struct syn_cache *, sizeof(*sc), M_PCB, M_NOWAIT);
2072 if (sc == NULL)
2073 return (0);
2074 /*
2075 * Fill in the cache, and put the necessary TCP
2076 * options into the reply.
2077 */
2078 sc->sc_src.s_addr = ti->ti_src.s_addr;
2079 sc->sc_dst.s_addr = ti->ti_dst.s_addr;
2080 sc->sc_sport = ti->ti_sport;
2081 sc->sc_dport = ti->ti_dport;
2082 sc->sc_irs = ti->ti_seq;
2083 sc->sc_iss = tcp_new_iss(sc, sizeof(struct syn_cache), 0);
2084 sc->sc_peermaxseg = oi->maxseg;
2085 sc->sc_ourmaxseg = tcp_mss_to_advertise(tp);
2086 sc->sc_tstmp = (tcp_do_rfc1323 && (tb.t_flags & TF_RCVD_TSTMP)) ? 1 : 0;
2087 if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2088 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2089 sc->sc_requested_s_scale = tb.requested_s_scale;
2090 sc->sc_request_r_scale = 0;
2091 while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
2092 TCP_MAXWIN << sc->sc_request_r_scale <
2093 so->so_rcv.sb_hiwat)
2094 sc->sc_request_r_scale++;
2095 } else {
2096 sc->sc_requested_s_scale = 15;
2097 sc->sc_request_r_scale = 15;
2098 }
2099 if (syn_cache_respond(sc, m, ti, win, tb.ts_recent) == 0) {
2100 syn_cache_insert(sc, &sc_prev, &scp);
2101 tcpstat.tcps_sndacks++;
2102 tcpstat.tcps_sndtotal++;
2103 } else {
2104 FREE(sc, M_PCB);
2105 tcpstat.tcps_sc_dropped++;
2106 }
2107 return (1);
2108 }
2109
2110 int
2111 syn_cache_respond(sc, m, ti, win, ts)
2112 struct syn_cache *sc;
2113 struct mbuf *m;
2114 register struct tcpiphdr *ti;
2115 long win;
2116 u_long ts;
2117 {
2118 u_int8_t *optp;
2119 int optlen;
2120
2121 /*
2122 * Tack on the TCP options. If there isn't enough trailing
2123 * space for them, move up the fixed header to make space.
2124 */
2125 optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
2126 (sc->sc_tstmp ? TCPOLEN_TSTAMP_APPA : 0);
2127 if (optlen > M_TRAILINGSPACE(m)) {
2128 if (M_LEADINGSPACE(m) >= optlen) {
2129 m->m_data -= optlen;
2130 m->m_len += optlen;
2131 } else {
2132 struct mbuf *m0 = m;
2133 if ((m = m_gethdr(M_DONTWAIT, MT_HEADER)) == NULL) {
2134 m_freem(m0);
2135 return (ENOBUFS);
2136 }
2137 MH_ALIGN(m, sizeof(*ti) + optlen);
2138 m->m_next = m0; /* this gets freed below */
2139 }
2140 ovbcopy((caddr_t)ti, mtod(m, caddr_t), sizeof(*ti));
2141 ti = mtod(m, struct tcpiphdr *);
2142 }
2143
2144 optp = (u_int8_t *)(ti + 1);
2145 optp[0] = TCPOPT_MAXSEG;
2146 optp[1] = 4;
2147 optp[2] = (sc->sc_ourmaxseg >> 8) & 0xff;
2148 optp[3] = sc->sc_ourmaxseg & 0xff;
2149 optlen = 4;
2150
2151 if (sc->sc_request_r_scale != 15) {
2152 *((u_int32_t *)(optp + optlen)) = htonl(TCPOPT_NOP << 24 |
2153 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
2154 sc->sc_request_r_scale);
2155 optlen += 4;
2156 }
2157
2158 if (sc->sc_tstmp) {
2159 u_int32_t *lp = (u_int32_t *)(optp + optlen);
2160 /* Form timestamp option as shown in appendix A of RFC 1323. */
2161 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
2162 *lp++ = htonl(tcp_now);
2163 *lp = htonl(ts);
2164 optlen += TCPOLEN_TSTAMP_APPA;
2165 }
2166
2167 /*
2168 * Toss any trailing mbufs. No need to worry about
2169 * m_len and m_pkthdr.len, since tcp_respond() will
2170 * unconditionally set them.
2171 */
2172 if (m->m_next) {
2173 m_freem(m->m_next);
2174 m->m_next = NULL;
2175 }
2176
2177 /*
2178 * Fill in the fields that tcp_respond() will not touch, and
2179 * then send the response.
2180 */
2181 ti->ti_off = (sizeof(struct tcphdr) + optlen) >> 2;
2182 ti->ti_win = htons(win);
2183 return (tcp_respond(NULL, ti, m, sc->sc_irs + 1, sc->sc_iss,
2184 TH_SYN|TH_ACK));
2185 }
2186 #endif /* TUBA_INCLUDE */
2187