tcp_input.c revision 1.5 1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * from: @(#)tcp_input.c 7.25 (Berkeley) 6/30/90
34 * $Id: tcp_input.c,v 1.5 1994/01/08 23:07:18 mycroft Exp $
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/select.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/errno.h>
46
47 #include <net/if.h>
48 #include <net/route.h>
49
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/tcp.h>
56 #include <netinet/tcp_fsm.h>
57 #include <netinet/tcp_seq.h>
58 #include <netinet/tcp_timer.h>
59 #include <netinet/tcp_var.h>
60 #include <netinet/tcpip.h>
61 #include <netinet/tcp_debug.h>
62
63 int tcprexmtthresh = 3;
64 int tcppredack; /* XXX debugging: times hdr predict ok for acks */
65 int tcppreddat; /* XXX # times header prediction ok for data packets */
66 int tcppcbcachemiss;
67 struct tcpiphdr tcp_saveti;
68 struct inpcb *tcp_last_inpcb = &tcb;
69
70 struct tcpcb *tcp_newtcpcb();
71
72 /*
73 * Insert segment ti into reassembly queue of tcp with
74 * control block tp. Return TH_FIN if reassembly now includes
75 * a segment with FIN. The macro form does the common case inline
76 * (segment is the next to be received on an established connection,
77 * and the queue is empty), avoiding linkage into and removal
78 * from the queue and repetition of various conversions.
79 * Set DELACK for segments received in order, but ack immediately
80 * when segments are out of order (so fast retransmit can work).
81 */
82 #define TCP_REASS(tp, ti, m, so, flags) { \
83 if ((ti)->ti_seq == (tp)->rcv_nxt && \
84 (tp)->seg_next == (struct tcpiphdr *)(tp) && \
85 (tp)->t_state == TCPS_ESTABLISHED) { \
86 tp->t_flags |= TF_DELACK; \
87 (tp)->rcv_nxt += (ti)->ti_len; \
88 flags = (ti)->ti_flags & TH_FIN; \
89 tcpstat.tcps_rcvpack++;\
90 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
91 sbappend(&(so)->so_rcv, (m)); \
92 sorwakeup(so); \
93 } else { \
94 (flags) = tcp_reass((tp), (ti), (m)); \
95 tp->t_flags |= TF_ACKNOW; \
96 } \
97 }
98
99 int
100 tcp_reass(tp, ti, m)
101 register struct tcpcb *tp;
102 register struct tcpiphdr *ti;
103 struct mbuf *m;
104 {
105 register struct tcpiphdr *q;
106 struct socket *so = tp->t_inpcb->inp_socket;
107 int flags;
108
109 /*
110 * Call with ti==0 after become established to
111 * force pre-ESTABLISHED data up to user socket.
112 */
113 if (ti == 0)
114 goto present;
115
116 /*
117 * Find a segment which begins after this one does.
118 */
119 for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
120 q = (struct tcpiphdr *)q->ti_next)
121 if (SEQ_GT(q->ti_seq, ti->ti_seq))
122 break;
123
124 /*
125 * If there is a preceding segment, it may provide some of
126 * our data already. If so, drop the data from the incoming
127 * segment. If it provides all of our data, drop us.
128 */
129 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
130 register int i;
131 q = (struct tcpiphdr *)q->ti_prev;
132 /* conversion to int (in i) handles seq wraparound */
133 i = q->ti_seq + q->ti_len - ti->ti_seq;
134 if (i > 0) {
135 if (i >= ti->ti_len) {
136 tcpstat.tcps_rcvduppack++;
137 tcpstat.tcps_rcvdupbyte += ti->ti_len;
138 m_freem(m);
139 return (0);
140 }
141 m_adj(m, i);
142 ti->ti_len -= i;
143 ti->ti_seq += i;
144 }
145 q = (struct tcpiphdr *)(q->ti_next);
146 }
147 tcpstat.tcps_rcvoopack++;
148 tcpstat.tcps_rcvoobyte += ti->ti_len;
149 REASS_MBUF(ti) = m; /* XXX */
150
151 /*
152 * While we overlap succeeding segments trim them or,
153 * if they are completely covered, dequeue them.
154 */
155 while (q != (struct tcpiphdr *)tp) {
156 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
157 if (i <= 0)
158 break;
159 if (i < q->ti_len) {
160 q->ti_seq += i;
161 q->ti_len -= i;
162 m_adj(REASS_MBUF(q), i);
163 break;
164 }
165 q = (struct tcpiphdr *)q->ti_next;
166 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
167 remque(q->ti_prev);
168 m_freem(m);
169 }
170
171 /*
172 * Stick new segment in its place.
173 */
174 insque(ti, q->ti_prev);
175
176 present:
177 /*
178 * Present data to user, advancing rcv_nxt through
179 * completed sequence space.
180 */
181 if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
182 return (0);
183 ti = tp->seg_next;
184 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
185 return (0);
186 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
187 return (0);
188 do {
189 tp->rcv_nxt += ti->ti_len;
190 flags = ti->ti_flags & TH_FIN;
191 remque(ti);
192 m = REASS_MBUF(ti);
193 ti = (struct tcpiphdr *)ti->ti_next;
194 if (so->so_state & SS_CANTRCVMORE)
195 m_freem(m);
196 else
197 sbappend(&so->so_rcv, m);
198 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
199 sorwakeup(so);
200 return (flags);
201 }
202
203 /*
204 * TCP input routine, follows pages 65-76 of the
205 * protocol specification dated September, 1981 very closely.
206 */
207 void
208 tcp_input(m, iphlen)
209 register struct mbuf *m;
210 int iphlen;
211 {
212 register struct tcpiphdr *ti;
213 register struct inpcb *inp;
214 struct mbuf *om = 0;
215 int len, tlen, off;
216 register struct tcpcb *tp = 0;
217 register int tiflags;
218 struct socket *so;
219 int todrop, acked, ourfinisacked, needoutput = 0;
220 short ostate;
221 struct in_addr laddr;
222 int dropsocket = 0;
223 int iss = 0;
224
225 tcpstat.tcps_rcvtotal++;
226 /*
227 * Get IP and TCP header together in first mbuf.
228 * Note: IP leaves IP header in first mbuf.
229 */
230 ti = mtod(m, struct tcpiphdr *);
231 if (iphlen > sizeof (struct ip))
232 ip_stripoptions(m, (struct mbuf *)0);
233 if (m->m_len < sizeof (struct tcpiphdr)) {
234 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
235 tcpstat.tcps_rcvshort++;
236 return;
237 }
238 ti = mtod(m, struct tcpiphdr *);
239 }
240
241 /*
242 * Checksum extended TCP header and data.
243 */
244 tlen = ((struct ip *)ti)->ip_len;
245 len = sizeof (struct ip) + tlen;
246 ti->ti_next = ti->ti_prev = 0;
247 ti->ti_x1 = 0;
248 ti->ti_len = (u_short)tlen;
249 HTONS(ti->ti_len);
250 if (ti->ti_sum = in_cksum(m, len)) {
251 tcpstat.tcps_rcvbadsum++;
252 goto drop;
253 }
254
255 /*
256 * Check that TCP offset makes sense,
257 * pull out TCP options and adjust length. XXX
258 */
259 off = ti->ti_off << 2;
260 if (off < sizeof (struct tcphdr) || off > tlen) {
261 tcpstat.tcps_rcvbadoff++;
262 goto drop;
263 }
264 tlen -= off;
265 ti->ti_len = tlen;
266 if (off > sizeof (struct tcphdr)) {
267 if (m->m_len < sizeof(struct ip) + off) {
268 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
269 tcpstat.tcps_rcvshort++;
270 return;
271 }
272 ti = mtod(m, struct tcpiphdr *);
273 }
274 om = m_get(M_DONTWAIT, MT_DATA);
275 if (om == 0)
276 goto drop;
277 om->m_len = off - sizeof (struct tcphdr);
278 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
279 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
280 m->m_len -= om->m_len;
281 m->m_pkthdr.len -= om->m_len;
282 bcopy(op+om->m_len, op,
283 (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
284 }
285 }
286 tiflags = ti->ti_flags;
287
288 /*
289 * Convert TCP protocol specific fields to host format.
290 */
291 NTOHL(ti->ti_seq);
292 NTOHL(ti->ti_ack);
293 NTOHS(ti->ti_win);
294 NTOHS(ti->ti_urp);
295
296 /*
297 * Locate pcb for segment.
298 */
299 findpcb:
300 inp = tcp_last_inpcb;
301 if (inp->inp_lport != ti->ti_dport ||
302 inp->inp_fport != ti->ti_sport ||
303 inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
304 inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
305 inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
306 ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
307 if (inp)
308 tcp_last_inpcb = inp;
309 ++tcppcbcachemiss;
310 }
311
312 /*
313 * If the state is CLOSED (i.e., TCB does not exist) then
314 * all data in the incoming segment is discarded.
315 * If the TCB exists but is in CLOSED state, it is embryonic,
316 * but should either do a listen or a connect soon.
317 */
318 if (inp == 0)
319 goto dropwithreset;
320 tp = intotcpcb(inp);
321 if (tp == 0)
322 goto dropwithreset;
323 if (tp->t_state == TCPS_CLOSED)
324 goto drop;
325 so = inp->inp_socket;
326 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
327 if (so->so_options & SO_DEBUG) {
328 ostate = tp->t_state;
329 tcp_saveti = *ti;
330 }
331 if (so->so_options & SO_ACCEPTCONN) {
332 so = sonewconn(so, 0);
333 if (so == 0)
334 goto drop;
335 /*
336 * This is ugly, but ....
337 *
338 * Mark socket as temporary until we're
339 * committed to keeping it. The code at
340 * ``drop'' and ``dropwithreset'' check the
341 * flag dropsocket to see if the temporary
342 * socket created here should be discarded.
343 * We mark the socket as discardable until
344 * we're committed to it below in TCPS_LISTEN.
345 */
346 dropsocket++;
347 inp = (struct inpcb *)so->so_pcb;
348 inp->inp_laddr = ti->ti_dst;
349 inp->inp_lport = ti->ti_dport;
350 #if BSD>=43
351 inp->inp_options = ip_srcroute();
352 #endif
353 tp = intotcpcb(inp);
354 tp->t_state = TCPS_LISTEN;
355 }
356 }
357
358 /*
359 * Segment received on connection.
360 * Reset idle time and keep-alive timer.
361 */
362 tp->t_idle = 0;
363 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
364
365 /*
366 * Process options if not in LISTEN state,
367 * else do it below (after getting remote address).
368 */
369 if (om && tp->t_state != TCPS_LISTEN) {
370 tcp_dooptions(tp, om, ti);
371 om = 0;
372 }
373 /*
374 * Header prediction: check for the two common cases
375 * of a uni-directional data xfer. If the packet has
376 * no control flags, is in-sequence, the window didn't
377 * change and we're not retransmitting, it's a
378 * candidate. If the length is zero and the ack moved
379 * forward, we're the sender side of the xfer. Just
380 * free the data acked & wake any higher level process
381 * that was blocked waiting for space. If the length
382 * is non-zero and the ack didn't move, we're the
383 * receiver side. If we're getting packets in-order
384 * (the reassembly queue is empty), add the data to
385 * the socket buffer and note that we need a delayed ack.
386 */
387 if (tp->t_state == TCPS_ESTABLISHED &&
388 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
389 ti->ti_seq == tp->rcv_nxt &&
390 ti->ti_win && ti->ti_win == tp->snd_wnd &&
391 tp->snd_nxt == tp->snd_max) {
392 if (ti->ti_len == 0) {
393 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
394 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
395 tp->snd_cwnd >= tp->snd_wnd) {
396 /*
397 * this is a pure ack for outstanding data.
398 */
399 ++tcppredack;
400 if (tp->t_rtt && SEQ_GT(ti->ti_ack,tp->t_rtseq))
401 tcp_xmit_timer(tp);
402 acked = ti->ti_ack - tp->snd_una;
403 tcpstat.tcps_rcvackpack++;
404 tcpstat.tcps_rcvackbyte += acked;
405 sbdrop(&so->so_snd, acked);
406 tp->snd_una = ti->ti_ack;
407 m_freem(m);
408
409 /*
410 * If all outstanding data are acked, stop
411 * retransmit timer, otherwise restart timer
412 * using current (possibly backed-off) value.
413 * If process is waiting for space,
414 * wakeup/selwakeup/signal. If data
415 * are ready to send, let tcp_output
416 * decide between more output or persist.
417 */
418 if (tp->snd_una == tp->snd_max)
419 tp->t_timer[TCPT_REXMT] = 0;
420 else if (tp->t_timer[TCPT_PERSIST] == 0)
421 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
422
423 if (so->so_snd.sb_flags & SB_NOTIFY)
424 sowwakeup(so);
425 if (so->so_snd.sb_cc)
426 (void) tcp_output(tp);
427 return;
428 }
429 } else if (ti->ti_ack == tp->snd_una &&
430 tp->seg_next == (struct tcpiphdr *)tp &&
431 ti->ti_len <= sbspace(&so->so_rcv)) {
432 /*
433 * this is a pure, in-sequence data packet
434 * with nothing on the reassembly queue and
435 * we have enough buffer space to take it.
436 */
437 ++tcppreddat;
438 tp->rcv_nxt += ti->ti_len;
439 tcpstat.tcps_rcvpack++;
440 tcpstat.tcps_rcvbyte += ti->ti_len;
441 /*
442 * Drop TCP and IP headers then add data
443 * to socket buffer
444 */
445 m->m_data += sizeof(struct tcpiphdr);
446 m->m_len -= sizeof(struct tcpiphdr);
447 sbappend(&so->so_rcv, m);
448 sorwakeup(so);
449 tp->t_flags |= TF_DELACK;
450 return;
451 }
452 }
453
454 /*
455 * Drop TCP and IP headers; TCP options were dropped above.
456 */
457 m->m_data += sizeof(struct tcpiphdr);
458 m->m_len -= sizeof(struct tcpiphdr);
459
460 /*
461 * Calculate amount of space in receive window,
462 * and then do TCP input processing.
463 * Receive window is amount of space in rcv queue,
464 * but not less than advertised window.
465 */
466 { int win;
467
468 win = sbspace(&so->so_rcv);
469 if (win < 0)
470 win = 0;
471 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
472 }
473
474 switch (tp->t_state) {
475
476 /*
477 * If the state is LISTEN then ignore segment if it contains an RST.
478 * If the segment contains an ACK then it is bad and send a RST.
479 * If it does not contain a SYN then it is not interesting; drop it.
480 * Don't bother responding if the destination was a broadcast.
481 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
482 * tp->iss, and send a segment:
483 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
484 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
485 * Fill in remote peer address fields if not previously specified.
486 * Enter SYN_RECEIVED state, and process any other fields of this
487 * segment in this state.
488 */
489 case TCPS_LISTEN: {
490 struct mbuf *am;
491 register struct sockaddr_in *sin;
492
493 if (tiflags & TH_RST)
494 goto drop;
495 if (tiflags & TH_ACK)
496 goto dropwithreset;
497 if ((tiflags & TH_SYN) == 0)
498 goto drop;
499 if (m->m_flags & M_BCAST)
500 goto drop;
501 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
502 if (am == NULL)
503 goto drop;
504 am->m_len = sizeof (struct sockaddr_in);
505 sin = mtod(am, struct sockaddr_in *);
506 sin->sin_family = AF_INET;
507 sin->sin_len = sizeof(*sin);
508 sin->sin_addr = ti->ti_src;
509 sin->sin_port = ti->ti_sport;
510 laddr = inp->inp_laddr;
511 if (inp->inp_laddr.s_addr == INADDR_ANY)
512 inp->inp_laddr = ti->ti_dst;
513 if (in_pcbconnect(inp, am)) {
514 inp->inp_laddr = laddr;
515 (void) m_free(am);
516 goto drop;
517 }
518 (void) m_free(am);
519 tp->t_template = tcp_template(tp);
520 if (tp->t_template == 0) {
521 tp = tcp_drop(tp, ENOBUFS);
522 dropsocket = 0; /* socket is already gone */
523 goto drop;
524 }
525 if (om) {
526 tcp_dooptions(tp, om, ti);
527 om = 0;
528 }
529 if (iss)
530 tp->iss = iss;
531 else
532 tp->iss = tcp_iss;
533 tcp_iss += TCP_ISSINCR/2;
534 tp->irs = ti->ti_seq;
535 tcp_sendseqinit(tp);
536 tcp_rcvseqinit(tp);
537 tp->t_flags |= TF_ACKNOW;
538 tp->t_state = TCPS_SYN_RECEIVED;
539 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
540 dropsocket = 0; /* committed to socket */
541 tcpstat.tcps_accepts++;
542 goto trimthenstep6;
543 }
544
545 /*
546 * If the state is SYN_SENT:
547 * if seg contains an ACK, but not for our SYN, drop the input.
548 * if seg contains a RST, then drop the connection.
549 * if seg does not contain SYN, then drop it.
550 * Otherwise this is an acceptable SYN segment
551 * initialize tp->rcv_nxt and tp->irs
552 * if seg contains ack then advance tp->snd_una
553 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
554 * arrange for segment to be acked (eventually)
555 * continue processing rest of data/controls, beginning with URG
556 */
557 case TCPS_SYN_SENT:
558 if ((tiflags & TH_ACK) &&
559 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
560 SEQ_GT(ti->ti_ack, tp->snd_max)))
561 goto dropwithreset;
562 if (tiflags & TH_RST) {
563 if (tiflags & TH_ACK)
564 tp = tcp_drop(tp, ECONNREFUSED);
565 goto drop;
566 }
567 if ((tiflags & TH_SYN) == 0)
568 goto drop;
569 if (tiflags & TH_ACK) {
570 tp->snd_una = ti->ti_ack;
571 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
572 tp->snd_nxt = tp->snd_una;
573 }
574 tp->t_timer[TCPT_REXMT] = 0;
575 tp->irs = ti->ti_seq;
576 tcp_rcvseqinit(tp);
577 tp->t_flags |= TF_ACKNOW;
578 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
579 tcpstat.tcps_connects++;
580 soisconnected(so);
581 tp->t_state = TCPS_ESTABLISHED;
582 (void) tcp_reass(tp, (struct tcpiphdr *)0,
583 (struct mbuf *)0);
584 /*
585 * if we didn't have to retransmit the SYN,
586 * use its rtt as our initial srtt & rtt var.
587 */
588 if (tp->t_rtt)
589 tcp_xmit_timer(tp);
590 } else
591 tp->t_state = TCPS_SYN_RECEIVED;
592
593 trimthenstep6:
594 /*
595 * Advance ti->ti_seq to correspond to first data byte.
596 * If data, trim to stay within window,
597 * dropping FIN if necessary.
598 */
599 ti->ti_seq++;
600 if (ti->ti_len > tp->rcv_wnd) {
601 todrop = ti->ti_len - tp->rcv_wnd;
602 m_adj(m, -todrop);
603 ti->ti_len = tp->rcv_wnd;
604 tiflags &= ~TH_FIN;
605 tcpstat.tcps_rcvpackafterwin++;
606 tcpstat.tcps_rcvbyteafterwin += todrop;
607 }
608 tp->snd_wl1 = ti->ti_seq - 1;
609 tp->rcv_up = ti->ti_seq;
610 goto step6;
611 }
612
613 /*
614 * States other than LISTEN or SYN_SENT.
615 * First check that at least some bytes of segment are within
616 * receive window. If segment begins before rcv_nxt,
617 * drop leading data (and SYN); if nothing left, just ack.
618 */
619 todrop = tp->rcv_nxt - ti->ti_seq;
620 if (todrop > 0) {
621 if (tiflags & TH_SYN) {
622 tiflags &= ~TH_SYN;
623 ti->ti_seq++;
624 if (ti->ti_urp > 1)
625 ti->ti_urp--;
626 else
627 tiflags &= ~TH_URG;
628 todrop--;
629 }
630 if (todrop > ti->ti_len ||
631 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
632 tcpstat.tcps_rcvduppack++;
633 tcpstat.tcps_rcvdupbyte += ti->ti_len;
634 /*
635 * If segment is just one to the left of the window,
636 * check two special cases:
637 * 1. Don't toss RST in response to 4.2-style keepalive.
638 * 2. If the only thing to drop is a FIN, we can drop
639 * it, but check the ACK or we will get into FIN
640 * wars if our FINs crossed (both CLOSING).
641 * In either case, send ACK to resynchronize,
642 * but keep on processing for RST or ACK.
643 */
644 if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
645 #ifdef TCP_COMPAT_42
646 || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
647 #endif
648 ) {
649 todrop = ti->ti_len;
650 tiflags &= ~TH_FIN;
651 tp->t_flags |= TF_ACKNOW;
652 } else {
653 /*
654 * Handle the case when a bound socket connects
655 * to itself. Allow packets with a SYN and
656 * an ACK to continue with the processing.
657 */
658 if (todrop != 0 || (tiflags & TH_ACK) == 0)
659 goto dropafterack;
660 }
661 } else {
662 tcpstat.tcps_rcvpartduppack++;
663 tcpstat.tcps_rcvpartdupbyte += todrop;
664 }
665 m_adj(m, todrop);
666 ti->ti_seq += todrop;
667 ti->ti_len -= todrop;
668 if (ti->ti_urp > todrop)
669 ti->ti_urp -= todrop;
670 else {
671 tiflags &= ~TH_URG;
672 ti->ti_urp = 0;
673 }
674 }
675
676 /*
677 * If new data are received on a connection after the
678 * user processes are gone, then RST the other end.
679 */
680 if ((so->so_state & SS_NOFDREF) &&
681 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
682 tp = tcp_close(tp);
683 tcpstat.tcps_rcvafterclose++;
684 goto dropwithreset;
685 }
686
687 /*
688 * If segment ends after window, drop trailing data
689 * (and PUSH and FIN); if nothing left, just ACK.
690 */
691 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
692 if (todrop > 0) {
693 tcpstat.tcps_rcvpackafterwin++;
694 if (todrop >= ti->ti_len) {
695 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
696 /*
697 * If a new connection request is received
698 * while in TIME_WAIT, drop the old connection
699 * and start over if the sequence numbers
700 * are above the previous ones.
701 */
702 if (tiflags & TH_SYN &&
703 tp->t_state == TCPS_TIME_WAIT &&
704 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
705 iss = tp->rcv_nxt + TCP_ISSINCR;
706 tp = tcp_close(tp);
707 goto findpcb;
708 }
709 /*
710 * If window is closed can only take segments at
711 * window edge, and have to drop data and PUSH from
712 * incoming segments. Continue processing, but
713 * remember to ack. Otherwise, drop segment
714 * and ack.
715 */
716 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
717 tp->t_flags |= TF_ACKNOW;
718 tcpstat.tcps_rcvwinprobe++;
719 } else
720 goto dropafterack;
721 } else
722 tcpstat.tcps_rcvbyteafterwin += todrop;
723 m_adj(m, -todrop);
724 ti->ti_len -= todrop;
725 tiflags &= ~(TH_PUSH|TH_FIN);
726 }
727
728 /*
729 * If the RST bit is set examine the state:
730 * SYN_RECEIVED STATE:
731 * If passive open, return to LISTEN state.
732 * If active open, inform user that connection was refused.
733 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
734 * Inform user that connection was reset, and close tcb.
735 * CLOSING, LAST_ACK, TIME_WAIT STATES
736 * Close the tcb.
737 */
738 if (tiflags&TH_RST) switch (tp->t_state) {
739
740 case TCPS_SYN_RECEIVED:
741 so->so_error = ECONNREFUSED;
742 goto close;
743
744 case TCPS_ESTABLISHED:
745 case TCPS_FIN_WAIT_1:
746 case TCPS_FIN_WAIT_2:
747 case TCPS_CLOSE_WAIT:
748 so->so_error = ECONNRESET;
749 close:
750 tp->t_state = TCPS_CLOSED;
751 tcpstat.tcps_drops++;
752 tp = tcp_close(tp);
753 goto drop;
754
755 case TCPS_CLOSING:
756 case TCPS_LAST_ACK:
757 case TCPS_TIME_WAIT:
758 tp = tcp_close(tp);
759 goto drop;
760 }
761
762 /*
763 * If a SYN is in the window, then this is an
764 * error and we send an RST and drop the connection.
765 */
766 if (tiflags & TH_SYN) {
767 tp = tcp_drop(tp, ECONNRESET);
768 goto dropwithreset;
769 }
770
771 /*
772 * If the ACK bit is off we drop the segment and return.
773 */
774 if ((tiflags & TH_ACK) == 0)
775 goto drop;
776
777 /*
778 * Ack processing.
779 */
780 switch (tp->t_state) {
781
782 /*
783 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
784 * ESTABLISHED state and continue processing, otherwise
785 * send an RST.
786 */
787 case TCPS_SYN_RECEIVED:
788 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
789 SEQ_GT(ti->ti_ack, tp->snd_max))
790 goto dropwithreset;
791 tcpstat.tcps_connects++;
792 soisconnected(so);
793 tp->t_state = TCPS_ESTABLISHED;
794 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
795 tp->snd_wl1 = ti->ti_seq - 1;
796 /* fall into ... */
797
798 /*
799 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
800 * ACKs. If the ack is in the range
801 * tp->snd_una < ti->ti_ack <= tp->snd_max
802 * then advance tp->snd_una to ti->ti_ack and drop
803 * data from the retransmission queue. If this ACK reflects
804 * more up to date window information we update our window information.
805 */
806 case TCPS_ESTABLISHED:
807 case TCPS_FIN_WAIT_1:
808 case TCPS_FIN_WAIT_2:
809 case TCPS_CLOSE_WAIT:
810 case TCPS_CLOSING:
811 case TCPS_LAST_ACK:
812 case TCPS_TIME_WAIT:
813
814 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
815 if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
816 tcpstat.tcps_rcvdupack++;
817 /*
818 * If we have outstanding data (other than
819 * a window probe), this is a completely
820 * duplicate ack (ie, window info didn't
821 * change), the ack is the biggest we've
822 * seen and we've seen exactly our rexmt
823 * threshhold of them, assume a packet
824 * has been dropped and retransmit it.
825 * Kludge snd_nxt & the congestion
826 * window so we send only this one
827 * packet.
828 *
829 * We know we're losing at the current
830 * window size so do congestion avoidance
831 * (set ssthresh to half the current window
832 * and pull our congestion window back to
833 * the new ssthresh).
834 *
835 * Dup acks mean that packets have left the
836 * network (they're now cached at the receiver)
837 * so bump cwnd by the amount in the receiver
838 * to keep a constant cwnd packets in the
839 * network.
840 */
841 if (tp->t_timer[TCPT_REXMT] == 0 ||
842 ti->ti_ack != tp->snd_una)
843 tp->t_dupacks = 0;
844 else if (++tp->t_dupacks == tcprexmtthresh) {
845 tcp_seq onxt = tp->snd_nxt;
846 u_int win =
847 min(tp->snd_wnd, tp->snd_cwnd) / 2 /
848 tp->t_maxseg;
849
850 if (win < 2)
851 win = 2;
852 tp->snd_ssthresh = win * tp->t_maxseg;
853 tp->t_timer[TCPT_REXMT] = 0;
854 tp->t_rtt = 0;
855 tp->snd_nxt = ti->ti_ack;
856 tp->snd_cwnd = tp->t_maxseg;
857 (void) tcp_output(tp);
858 tp->snd_cwnd = tp->snd_ssthresh +
859 tp->t_maxseg * tp->t_dupacks;
860 if (SEQ_GT(onxt, tp->snd_nxt))
861 tp->snd_nxt = onxt;
862 goto drop;
863 } else if (tp->t_dupacks > tcprexmtthresh) {
864 tp->snd_cwnd += tp->t_maxseg;
865 (void) tcp_output(tp);
866 goto drop;
867 }
868 } else
869 tp->t_dupacks = 0;
870 break;
871 }
872 /*
873 * If the congestion window was inflated to account
874 * for the other side's cached packets, retract it.
875 */
876 if (tp->t_dupacks > tcprexmtthresh &&
877 tp->snd_cwnd > tp->snd_ssthresh)
878 tp->snd_cwnd = tp->snd_ssthresh;
879 tp->t_dupacks = 0;
880 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
881 tcpstat.tcps_rcvacktoomuch++;
882 goto dropafterack;
883 }
884 acked = ti->ti_ack - tp->snd_una;
885 tcpstat.tcps_rcvackpack++;
886 tcpstat.tcps_rcvackbyte += acked;
887
888 /*
889 * If transmit timer is running and timed sequence
890 * number was acked, update smoothed round trip time.
891 * Since we now have an rtt measurement, cancel the
892 * timer backoff (cf., Phil Karn's retransmit alg.).
893 * Recompute the initial retransmit timer.
894 */
895 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
896 tcp_xmit_timer(tp);
897
898 /*
899 * If all outstanding data is acked, stop retransmit
900 * timer and remember to restart (more output or persist).
901 * If there is more data to be acked, restart retransmit
902 * timer, using current (possibly backed-off) value.
903 */
904 if (ti->ti_ack == tp->snd_max) {
905 tp->t_timer[TCPT_REXMT] = 0;
906 needoutput = 1;
907 } else if (tp->t_timer[TCPT_PERSIST] == 0)
908 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
909 /*
910 * When new data is acked, open the congestion window.
911 * If the window gives us less than ssthresh packets
912 * in flight, open exponentially (maxseg per packet).
913 * Otherwise open linearly: maxseg per window
914 * (maxseg^2 / cwnd per packet), plus a constant
915 * fraction of a packet (maxseg/8) to help larger windows
916 * open quickly enough.
917 */
918 {
919 register u_int cw = tp->snd_cwnd;
920 register u_int incr = tp->t_maxseg;
921
922 if (cw > tp->snd_ssthresh)
923 incr = incr * incr / cw + incr / 8;
924 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN);
925 }
926 if (acked > so->so_snd.sb_cc) {
927 tp->snd_wnd -= so->so_snd.sb_cc;
928 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
929 ourfinisacked = 1;
930 } else {
931 sbdrop(&so->so_snd, acked);
932 tp->snd_wnd -= acked;
933 ourfinisacked = 0;
934 }
935 if (so->so_snd.sb_flags & SB_NOTIFY)
936 sowwakeup(so);
937 tp->snd_una = ti->ti_ack;
938 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
939 tp->snd_nxt = tp->snd_una;
940
941 switch (tp->t_state) {
942
943 /*
944 * In FIN_WAIT_1 STATE in addition to the processing
945 * for the ESTABLISHED state if our FIN is now acknowledged
946 * then enter FIN_WAIT_2.
947 */
948 case TCPS_FIN_WAIT_1:
949 if (ourfinisacked) {
950 /*
951 * If we can't receive any more
952 * data, then closing user can proceed.
953 * Starting the timer is contrary to the
954 * specification, but if we don't get a FIN
955 * we'll hang forever.
956 */
957 if (so->so_state & SS_CANTRCVMORE) {
958 soisdisconnected(so);
959 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
960 }
961 tp->t_state = TCPS_FIN_WAIT_2;
962 }
963 break;
964
965 /*
966 * In CLOSING STATE in addition to the processing for
967 * the ESTABLISHED state if the ACK acknowledges our FIN
968 * then enter the TIME-WAIT state, otherwise ignore
969 * the segment.
970 */
971 case TCPS_CLOSING:
972 if (ourfinisacked) {
973 tp->t_state = TCPS_TIME_WAIT;
974 tcp_canceltimers(tp);
975 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
976 soisdisconnected(so);
977 }
978 break;
979
980 /*
981 * In LAST_ACK, we may still be waiting for data to drain
982 * and/or to be acked, as well as for the ack of our FIN.
983 * If our FIN is now acknowledged, delete the TCB,
984 * enter the closed state and return.
985 */
986 case TCPS_LAST_ACK:
987 if (ourfinisacked) {
988 tp = tcp_close(tp);
989 goto drop;
990 }
991 break;
992
993 /*
994 * In TIME_WAIT state the only thing that should arrive
995 * is a retransmission of the remote FIN. Acknowledge
996 * it and restart the finack timer.
997 */
998 case TCPS_TIME_WAIT:
999 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1000 goto dropafterack;
1001 }
1002 }
1003
1004 step6:
1005 /*
1006 * Update window information.
1007 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1008 */
1009 if ((tiflags & TH_ACK) &&
1010 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
1011 (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1012 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
1013 /* keep track of pure window updates */
1014 if (ti->ti_len == 0 &&
1015 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
1016 tcpstat.tcps_rcvwinupd++;
1017 tp->snd_wnd = ti->ti_win;
1018 tp->snd_wl1 = ti->ti_seq;
1019 tp->snd_wl2 = ti->ti_ack;
1020 if (tp->snd_wnd > tp->max_sndwnd)
1021 tp->max_sndwnd = tp->snd_wnd;
1022 needoutput = 1;
1023 }
1024
1025 /*
1026 * Process segments with URG.
1027 */
1028 if ((tiflags & TH_URG) && ti->ti_urp &&
1029 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1030 /*
1031 * This is a kludge, but if we receive and accept
1032 * random urgent pointers, we'll crash in
1033 * soreceive. It's hard to imagine someone
1034 * actually wanting to send this much urgent data.
1035 */
1036 if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
1037 ti->ti_urp = 0; /* XXX */
1038 tiflags &= ~TH_URG; /* XXX */
1039 goto dodata; /* XXX */
1040 }
1041 /*
1042 * If this segment advances the known urgent pointer,
1043 * then mark the data stream. This should not happen
1044 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1045 * a FIN has been received from the remote side.
1046 * In these states we ignore the URG.
1047 *
1048 * According to RFC961 (Assigned Protocols),
1049 * the urgent pointer points to the last octet
1050 * of urgent data. We continue, however,
1051 * to consider it to indicate the first octet
1052 * of data past the urgent section as the original
1053 * spec states (in one of two places).
1054 */
1055 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1056 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1057 so->so_oobmark = so->so_rcv.sb_cc +
1058 (tp->rcv_up - tp->rcv_nxt) - 1;
1059 if (so->so_oobmark == 0)
1060 so->so_state |= SS_RCVATMARK;
1061 sohasoutofband(so);
1062 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1063 }
1064 /*
1065 * Remove out of band data so doesn't get presented to user.
1066 * This can happen independent of advancing the URG pointer,
1067 * but if two URG's are pending at once, some out-of-band
1068 * data may creep in... ick.
1069 */
1070 if (ti->ti_urp <= ti->ti_len
1071 #ifdef SO_OOBINLINE
1072 && (so->so_options & SO_OOBINLINE) == 0
1073 #endif
1074 )
1075 tcp_pulloutofband(so, ti, m);
1076 } else
1077 /*
1078 * If no out of band data is expected,
1079 * pull receive urgent pointer along
1080 * with the receive window.
1081 */
1082 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1083 tp->rcv_up = tp->rcv_nxt;
1084 dodata: /* XXX */
1085
1086 /*
1087 * Process the segment text, merging it into the TCP sequencing queue,
1088 * and arranging for acknowledgment of receipt if necessary.
1089 * This process logically involves adjusting tp->rcv_wnd as data
1090 * is presented to the user (this happens in tcp_usrreq.c,
1091 * case PRU_RCVD). If a FIN has already been received on this
1092 * connection then we just ignore the text.
1093 */
1094 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1095 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1096 TCP_REASS(tp, ti, m, so, tiflags);
1097 /*
1098 * Note the amount of data that peer has sent into
1099 * our window, in order to estimate the sender's
1100 * buffer size.
1101 */
1102 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1103 } else {
1104 m_freem(m);
1105 tiflags &= ~TH_FIN;
1106 }
1107
1108 /*
1109 * If FIN is received ACK the FIN and let the user know
1110 * that the connection is closing.
1111 */
1112 if (tiflags & TH_FIN) {
1113 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1114 socantrcvmore(so);
1115 tp->t_flags |= TF_ACKNOW;
1116 tp->rcv_nxt++;
1117 }
1118 switch (tp->t_state) {
1119
1120 /*
1121 * In SYN_RECEIVED and ESTABLISHED STATES
1122 * enter the CLOSE_WAIT state.
1123 */
1124 case TCPS_SYN_RECEIVED:
1125 case TCPS_ESTABLISHED:
1126 tp->t_state = TCPS_CLOSE_WAIT;
1127 break;
1128
1129 /*
1130 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1131 * enter the CLOSING state.
1132 */
1133 case TCPS_FIN_WAIT_1:
1134 tp->t_state = TCPS_CLOSING;
1135 break;
1136
1137 /*
1138 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1139 * starting the time-wait timer, turning off the other
1140 * standard timers.
1141 */
1142 case TCPS_FIN_WAIT_2:
1143 tp->t_state = TCPS_TIME_WAIT;
1144 tcp_canceltimers(tp);
1145 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1146 soisdisconnected(so);
1147 break;
1148
1149 /*
1150 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1151 */
1152 case TCPS_TIME_WAIT:
1153 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1154 break;
1155 }
1156 }
1157 if (so->so_options & SO_DEBUG)
1158 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1159
1160 /*
1161 * Return any desired output.
1162 */
1163 if (needoutput || (tp->t_flags & TF_ACKNOW))
1164 (void) tcp_output(tp);
1165 return;
1166
1167 dropafterack:
1168 /*
1169 * Generate an ACK dropping incoming segment if it occupies
1170 * sequence space, where the ACK reflects our state.
1171 */
1172 if (tiflags & TH_RST)
1173 goto drop;
1174 m_freem(m);
1175 tp->t_flags |= TF_ACKNOW;
1176 (void) tcp_output(tp);
1177 return;
1178
1179 dropwithreset:
1180 if (om) {
1181 (void) m_free(om);
1182 om = 0;
1183 }
1184 /*
1185 * Generate a RST, dropping incoming segment.
1186 * Make ACK acceptable to originator of segment.
1187 * Don't bother to respond if destination was broadcast.
1188 */
1189 if ((tiflags & TH_RST) || m->m_flags & M_BCAST)
1190 goto drop;
1191 if (tiflags & TH_ACK)
1192 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1193 else {
1194 if (tiflags & TH_SYN)
1195 ti->ti_len++;
1196 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1197 TH_RST|TH_ACK);
1198 }
1199 /* destroy temporarily created socket */
1200 if (dropsocket)
1201 (void) soabort(so);
1202 return;
1203
1204 drop:
1205 if (om)
1206 (void) m_free(om);
1207 /*
1208 * Drop space held by incoming segment and return.
1209 */
1210 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1211 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1212 m_freem(m);
1213 /* destroy temporarily created socket */
1214 if (dropsocket)
1215 (void) soabort(so);
1216 return;
1217 }
1218
1219 void
1220 tcp_dooptions(tp, om, ti)
1221 struct tcpcb *tp;
1222 struct mbuf *om;
1223 struct tcpiphdr *ti;
1224 {
1225 register u_char *cp;
1226 u_short mss;
1227 int opt, optlen, cnt;
1228
1229 cp = mtod(om, u_char *);
1230 cnt = om->m_len;
1231 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1232 opt = cp[0];
1233 if (opt == TCPOPT_EOL)
1234 break;
1235 if (opt == TCPOPT_NOP)
1236 optlen = 1;
1237 else {
1238 optlen = cp[1];
1239 if (optlen <= 0)
1240 break;
1241 }
1242 switch (opt) {
1243
1244 default:
1245 continue;
1246
1247 case TCPOPT_MAXSEG:
1248 if (optlen != 4)
1249 continue;
1250 if (!(ti->ti_flags & TH_SYN))
1251 continue;
1252 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1253 NTOHS(mss);
1254 (void) tcp_mss(tp, mss); /* sets t_maxseg */
1255 break;
1256 }
1257 }
1258 (void) m_free(om);
1259 }
1260
1261 /*
1262 * Pull out of band byte out of a segment so
1263 * it doesn't appear in the user's data queue.
1264 * It is still reflected in the segment length for
1265 * sequencing purposes.
1266 */
1267 void
1268 tcp_pulloutofband(so, ti, m)
1269 struct socket *so;
1270 struct tcpiphdr *ti;
1271 register struct mbuf *m;
1272 {
1273 int cnt = ti->ti_urp - 1;
1274
1275 while (cnt >= 0) {
1276 if (m->m_len > cnt) {
1277 char *cp = mtod(m, caddr_t) + cnt;
1278 struct tcpcb *tp = sototcpcb(so);
1279
1280 tp->t_iobc = *cp;
1281 tp->t_oobflags |= TCPOOB_HAVEDATA;
1282 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1283 m->m_len--;
1284 return;
1285 }
1286 cnt -= m->m_len;
1287 m = m->m_next;
1288 if (m == 0)
1289 break;
1290 }
1291 panic("tcp_pulloutofband");
1292 }
1293
1294 /*
1295 * Collect new round-trip time estimate
1296 * and update averages and current timeout.
1297 */
1298 void
1299 tcp_xmit_timer(tp)
1300 register struct tcpcb *tp;
1301 {
1302 register short delta;
1303
1304 tcpstat.tcps_rttupdated++;
1305 if (tp->t_srtt != 0) {
1306 /*
1307 * srtt is stored as fixed point with 3 bits after the
1308 * binary point (i.e., scaled by 8). The following magic
1309 * is equivalent to the smoothing algorithm in rfc793 with
1310 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1311 * point). Adjust t_rtt to origin 0.
1312 */
1313 delta = tp->t_rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1314 if ((tp->t_srtt += delta) <= 0)
1315 tp->t_srtt = 1;
1316 /*
1317 * We accumulate a smoothed rtt variance (actually, a
1318 * smoothed mean difference), then set the retransmit
1319 * timer to smoothed rtt + 4 times the smoothed variance.
1320 * rttvar is stored as fixed point with 2 bits after the
1321 * binary point (scaled by 4). The following is
1322 * equivalent to rfc793 smoothing with an alpha of .75
1323 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1324 * rfc793's wired-in beta.
1325 */
1326 if (delta < 0)
1327 delta = -delta;
1328 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1329 if ((tp->t_rttvar += delta) <= 0)
1330 tp->t_rttvar = 1;
1331 } else {
1332 /*
1333 * No rtt measurement yet - use the unsmoothed rtt.
1334 * Set the variance to half the rtt (so our first
1335 * retransmit happens at 2*rtt)
1336 */
1337 tp->t_srtt = tp->t_rtt << TCP_RTT_SHIFT;
1338 tp->t_rttvar = tp->t_rtt << (TCP_RTTVAR_SHIFT - 1);
1339 }
1340 tp->t_rtt = 0;
1341 tp->t_rxtshift = 0;
1342
1343 /*
1344 * the retransmit should happen at rtt + 4 * rttvar.
1345 * Because of the way we do the smoothing, srtt and rttvar
1346 * will each average +1/2 tick of bias. When we compute
1347 * the retransmit timer, we want 1/2 tick of rounding and
1348 * 1 extra tick because of +-1/2 tick uncertainty in the
1349 * firing of the timer. The bias will give us exactly the
1350 * 1.5 tick we need. But, because the bias is
1351 * statistical, we have to test that we don't drop below
1352 * the minimum feasible timer (which is 2 ticks).
1353 */
1354 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1355 tp->t_rttmin, TCPTV_REXMTMAX);
1356
1357 /*
1358 * We received an ack for a packet that wasn't retransmitted;
1359 * it is probably safe to discard any error indications we've
1360 * received recently. This isn't quite right, but close enough
1361 * for now (a route might have failed after we sent a segment,
1362 * and the return path might not be symmetrical).
1363 */
1364 tp->t_softerror = 0;
1365 }
1366
1367 /*
1368 * Determine a reasonable value for maxseg size.
1369 * If the route is known, check route for mtu.
1370 * If none, use an mss that can be handled on the outgoing
1371 * interface without forcing IP to fragment; if bigger than
1372 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1373 * to utilize large mbufs. If no route is found, route has no mtu,
1374 * or the destination isn't local, use a default, hopefully conservative
1375 * size (usually 512 or the default IP max size, but no more than the mtu
1376 * of the interface), as we can't discover anything about intervening
1377 * gateways or networks. We also initialize the congestion/slow start
1378 * window to be a single segment if the destination isn't local.
1379 * While looking at the routing entry, we also initialize other path-dependent
1380 * parameters from pre-set or cached values in the routing entry.
1381 */
1382 int
1383 tcp_mss(tp, offer)
1384 register struct tcpcb *tp;
1385 u_short offer;
1386 {
1387 struct route *ro;
1388 register struct rtentry *rt;
1389 struct ifnet *ifp;
1390 register int rtt, mss;
1391 u_long bufsize;
1392 struct inpcb *inp;
1393 struct socket *so;
1394 extern int tcp_mssdflt, tcp_rttdflt;
1395
1396 inp = tp->t_inpcb;
1397 ro = &inp->inp_route;
1398
1399 if ((rt = ro->ro_rt) == (struct rtentry *)0) {
1400 /* No route yet, so try to acquire one */
1401 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1402 ro->ro_dst.sa_family = AF_INET;
1403 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
1404 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1405 inp->inp_faddr;
1406 rtalloc(ro);
1407 }
1408 if ((rt = ro->ro_rt) == (struct rtentry *)0)
1409 return (tcp_mssdflt);
1410 }
1411 ifp = rt->rt_ifp;
1412 so = inp->inp_socket;
1413
1414 #ifdef RTV_MTU /* if route characteristics exist ... */
1415 /*
1416 * While we're here, check if there's an initial rtt
1417 * or rttvar. Convert from the route-table units
1418 * to scaled multiples of the slow timeout timer.
1419 */
1420 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1421 if (rt->rt_rmx.rmx_locks & RTV_MTU)
1422 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
1423 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
1424 if (rt->rt_rmx.rmx_rttvar)
1425 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1426 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
1427 else
1428 /* default variation is +- 1 rtt */
1429 tp->t_rttvar =
1430 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
1431 TCPT_RANGESET(tp->t_rxtcur,
1432 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
1433 tp->t_rttmin, TCPTV_REXMTMAX);
1434 }
1435 /*
1436 * if there's an mtu associated with the route, use it
1437 */
1438 if (rt->rt_rmx.rmx_mtu)
1439 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
1440 else
1441 #endif /* RTV_MTU */
1442 {
1443 mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1444 #if (MCLBYTES & (MCLBYTES - 1)) == 0
1445 if (mss > MCLBYTES)
1446 mss &= ~(MCLBYTES-1);
1447 #else
1448 if (mss > MCLBYTES)
1449 mss = mss / MCLBYTES * MCLBYTES;
1450 #endif
1451 if (!in_localaddr(inp->inp_faddr))
1452 mss = min(mss, tcp_mssdflt);
1453 }
1454 /*
1455 * The current mss, t_maxseg, is initialized to the default value.
1456 * If we compute a smaller value, reduce the current mss.
1457 * If we compute a larger value, return it for use in sending
1458 * a max seg size option, but don't store it for use
1459 * unless we received an offer at least that large from peer.
1460 * However, do not accept offers under 32 bytes.
1461 */
1462 if (offer)
1463 mss = min(mss, offer);
1464 mss = max(mss, 32); /* sanity */
1465 if (mss < tp->t_maxseg || offer != 0) {
1466 /*
1467 * If there's a pipesize, change the socket buffer
1468 * to that size. Make the socket buffers an integral
1469 * number of mss units; if the mss is larger than
1470 * the socket buffer, decrease the mss.
1471 */
1472 #ifdef RTV_SPIPE
1473 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
1474 #endif
1475 bufsize = so->so_snd.sb_hiwat;
1476 if (bufsize < mss)
1477 mss = bufsize;
1478 else {
1479 bufsize = min(bufsize, SB_MAX) / mss * mss;
1480 (void) sbreserve(&so->so_snd, bufsize);
1481 }
1482 tp->t_maxseg = mss;
1483
1484 #ifdef RTV_RPIPE
1485 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
1486 #endif
1487 bufsize = so->so_rcv.sb_hiwat;
1488 if (bufsize > mss) {
1489 bufsize = min(bufsize, SB_MAX) / mss * mss;
1490 (void) sbreserve(&so->so_rcv, bufsize);
1491 }
1492 }
1493 tp->snd_cwnd = mss;
1494
1495 #ifdef RTV_SSTHRESH
1496 if (rt->rt_rmx.rmx_ssthresh) {
1497 /*
1498 * There's some sort of gateway or interface
1499 * buffer limit on the path. Use this to set
1500 * the slow start threshhold, but set the
1501 * threshold to no less than 2*mss.
1502 */
1503 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1504 }
1505 #endif /* RTV_MTU */
1506 return (mss);
1507 }
1508