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