Commit | Line | Data |
---|---|---|
8ae0e4b4 | 1 | /* |
0880b18e | 2 | * Copyright (c) 1982, 1986 Regents of the University of California. |
8ae0e4b4 KM |
3 | * All rights reserved. The Berkeley software License Agreement |
4 | * specifies the terms and conditions for redistribution. | |
5 | * | |
a6bbda13 | 6 | * @(#)tcp_input.c 7.10 (Berkeley) %G% |
8ae0e4b4 | 7 | */ |
87e78f19 | 8 | |
20666ad3 JB |
9 | #include "param.h" |
10 | #include "systm.h" | |
11 | #include "mbuf.h" | |
12 | #include "protosw.h" | |
13 | #include "socket.h" | |
14 | #include "socketvar.h" | |
15 | #include "errno.h" | |
f4d55810 SL |
16 | |
17 | #include "../net/if.h" | |
c124e997 | 18 | #include "../net/route.h" |
f4d55810 | 19 | |
20666ad3 JB |
20 | #include "in.h" |
21 | #include "in_pcb.h" | |
22 | #include "in_systm.h" | |
23 | #include "ip.h" | |
24 | #include "ip_var.h" | |
25 | #include "tcp.h" | |
26 | #include "tcp_fsm.h" | |
27 | #include "tcp_seq.h" | |
28 | #include "tcp_timer.h" | |
29 | #include "tcp_var.h" | |
30 | #include "tcpip.h" | |
31 | #include "tcp_debug.h" | |
87e78f19 | 32 | |
22856bb8 | 33 | int tcpprintfs = 0; |
60b16fa9 | 34 | int tcpcksum = 1; |
4b935108 | 35 | struct tcpiphdr tcp_saveti; |
8b5a83bb | 36 | extern tcpnodelack; |
87e78f19 | 37 | |
4b935108 | 38 | struct tcpcb *tcp_newtcpcb(); |
a17510f3 MK |
39 | |
40 | /* | |
41 | * Insert segment ti into reassembly queue of tcp with | |
42 | * control block tp. Return TH_FIN if reassembly now includes | |
43 | * a segment with FIN. The macro form does the common case inline | |
44 | * (segment is the next to be received on an established connection, | |
45 | * and the queue is empty), avoiding linkage into and removal | |
46 | * from the queue and repetition of various conversions. | |
47 | */ | |
48 | #define TCP_REASS(tp, ti, m, so, flags) { \ | |
49 | if ((ti)->ti_seq == (tp)->rcv_nxt && \ | |
50 | (tp)->seg_next == (struct tcpiphdr *)(tp) && \ | |
51 | (tp)->t_state == TCPS_ESTABLISHED) { \ | |
52 | (tp)->rcv_nxt += (ti)->ti_len; \ | |
53 | flags = (ti)->ti_flags & TH_FIN; \ | |
96c50630 MK |
54 | tcpstat.tcps_rcvpack++;\ |
55 | tcpstat.tcps_rcvbyte += (ti)->ti_len;\ | |
a17510f3 MK |
56 | sbappend(&(so)->so_rcv, (m)); \ |
57 | sorwakeup(so); \ | |
58 | } else \ | |
59 | (flags) = tcp_reass((tp), (ti)); \ | |
60 | } | |
61 | ||
62 | tcp_reass(tp, ti) | |
63 | register struct tcpcb *tp; | |
64 | register struct tcpiphdr *ti; | |
65 | { | |
66 | register struct tcpiphdr *q; | |
67 | struct socket *so = tp->t_inpcb->inp_socket; | |
68 | struct mbuf *m; | |
69 | int flags; | |
70 | ||
71 | /* | |
72 | * Call with ti==0 after become established to | |
73 | * force pre-ESTABLISHED data up to user socket. | |
74 | */ | |
75 | if (ti == 0) | |
76 | goto present; | |
77 | ||
78 | /* | |
79 | * Find a segment which begins after this one does. | |
80 | */ | |
81 | for (q = tp->seg_next; q != (struct tcpiphdr *)tp; | |
82 | q = (struct tcpiphdr *)q->ti_next) | |
83 | if (SEQ_GT(q->ti_seq, ti->ti_seq)) | |
84 | break; | |
85 | ||
86 | /* | |
87 | * If there is a preceding segment, it may provide some of | |
88 | * our data already. If so, drop the data from the incoming | |
89 | * segment. If it provides all of our data, drop us. | |
90 | */ | |
91 | if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { | |
92 | register int i; | |
93 | q = (struct tcpiphdr *)q->ti_prev; | |
94 | /* conversion to int (in i) handles seq wraparound */ | |
95 | i = q->ti_seq + q->ti_len - ti->ti_seq; | |
96 | if (i > 0) { | |
96c50630 MK |
97 | if (i >= ti->ti_len) { |
98 | tcpstat.tcps_rcvduppack++; | |
99 | tcpstat.tcps_rcvdupbyte += ti->ti_len; | |
a17510f3 | 100 | goto drop; |
96c50630 | 101 | } |
a17510f3 MK |
102 | m_adj(dtom(ti), i); |
103 | ti->ti_len -= i; | |
104 | ti->ti_seq += i; | |
105 | } | |
106 | q = (struct tcpiphdr *)(q->ti_next); | |
107 | } | |
96c50630 MK |
108 | tcpstat.tcps_rcvoopack++; |
109 | tcpstat.tcps_rcvoobyte += ti->ti_len; | |
a17510f3 MK |
110 | |
111 | /* | |
112 | * While we overlap succeeding segments trim them or, | |
113 | * if they are completely covered, dequeue them. | |
114 | */ | |
115 | while (q != (struct tcpiphdr *)tp) { | |
116 | register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; | |
117 | if (i <= 0) | |
118 | break; | |
119 | if (i < q->ti_len) { | |
120 | q->ti_seq += i; | |
121 | q->ti_len -= i; | |
122 | m_adj(dtom(q), i); | |
123 | break; | |
124 | } | |
125 | q = (struct tcpiphdr *)q->ti_next; | |
126 | m = dtom(q->ti_prev); | |
127 | remque(q->ti_prev); | |
128 | m_freem(m); | |
129 | } | |
130 | ||
131 | /* | |
132 | * Stick new segment in its place. | |
133 | */ | |
134 | insque(ti, q->ti_prev); | |
135 | ||
136 | present: | |
137 | /* | |
138 | * Present data to user, advancing rcv_nxt through | |
139 | * completed sequence space. | |
140 | */ | |
141 | if (TCPS_HAVERCVDSYN(tp->t_state) == 0) | |
142 | return (0); | |
143 | ti = tp->seg_next; | |
144 | if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) | |
145 | return (0); | |
146 | if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) | |
147 | return (0); | |
148 | do { | |
149 | tp->rcv_nxt += ti->ti_len; | |
150 | flags = ti->ti_flags & TH_FIN; | |
151 | remque(ti); | |
152 | m = dtom(ti); | |
153 | ti = (struct tcpiphdr *)ti->ti_next; | |
154 | if (so->so_state & SS_CANTRCVMORE) | |
155 | m_freem(m); | |
156 | else | |
157 | sbappend(&so->so_rcv, m); | |
158 | } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); | |
159 | sorwakeup(so); | |
160 | return (flags); | |
161 | drop: | |
162 | m_freem(dtom(ti)); | |
163 | return (0); | |
164 | } | |
165 | ||
2ff61f9d BJ |
166 | /* |
167 | * TCP input routine, follows pages 65-76 of the | |
168 | * protocol specification dated September, 1981 very closely. | |
169 | */ | |
2b4b57cd BJ |
170 | tcp_input(m0) |
171 | struct mbuf *m0; | |
87e78f19 | 172 | { |
2b4b57cd | 173 | register struct tcpiphdr *ti; |
53a5409e | 174 | struct inpcb *inp; |
2b4b57cd | 175 | register struct mbuf *m; |
8b5a83bb | 176 | struct mbuf *om = 0; |
2b4b57cd | 177 | int len, tlen, off; |
8e65fd66 | 178 | register struct tcpcb *tp = 0; |
2b4b57cd | 179 | register int tiflags; |
d52566dd | 180 | struct socket *so; |
4859921b | 181 | int todrop, acked, ourfinisacked, needoutput = 0; |
4b935108 | 182 | short ostate; |
ebcadd38 | 183 | struct in_addr laddr; |
7aa16f99 | 184 | int dropsocket = 0; |
96c50630 | 185 | int iss = 0; |
87e78f19 | 186 | |
96c50630 | 187 | tcpstat.tcps_rcvtotal++; |
87e78f19 | 188 | /* |
4aed14e3 BJ |
189 | * Get IP and TCP header together in first mbuf. |
190 | * Note: IP leaves IP header in first mbuf. | |
87e78f19 | 191 | */ |
2b4b57cd | 192 | m = m0; |
20790db4 | 193 | ti = mtod(m, struct tcpiphdr *); |
4aed14e3 | 194 | if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) |
d63599ac | 195 | ip_stripoptions((struct ip *)ti, (struct mbuf *)0); |
6703c41f BJ |
196 | if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { |
197 | if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { | |
96c50630 | 198 | tcpstat.tcps_rcvshort++; |
6703c41f | 199 | return; |
8a13b737 BJ |
200 | } |
201 | ti = mtod(m, struct tcpiphdr *); | |
202 | } | |
87e78f19 | 203 | |
2b4b57cd | 204 | /* |
4aed14e3 | 205 | * Checksum extended TCP header and data. |
2b4b57cd BJ |
206 | */ |
207 | tlen = ((struct ip *)ti)->ip_len; | |
208 | len = sizeof (struct ip) + tlen; | |
60b16fa9 | 209 | if (tcpcksum) { |
2b4b57cd BJ |
210 | ti->ti_next = ti->ti_prev = 0; |
211 | ti->ti_x1 = 0; | |
ac83b17a | 212 | ti->ti_len = (u_short)tlen; |
668cc26d | 213 | ti->ti_len = htons((u_short)ti->ti_len); |
4b6b94ca | 214 | if (ti->ti_sum = in_cksum(m, len)) { |
ee954ff1 SL |
215 | if (tcpprintfs) |
216 | printf("tcp sum: src %x\n", ti->ti_src); | |
96c50630 | 217 | tcpstat.tcps_rcvbadsum++; |
8a13b737 | 218 | goto drop; |
87e78f19 BJ |
219 | } |
220 | } | |
221 | ||
222 | /* | |
4aed14e3 | 223 | * Check that TCP offset makes sense, |
8b5a83bb | 224 | * pull out TCP options and adjust length. |
87e78f19 | 225 | */ |
2b4b57cd | 226 | off = ti->ti_off << 2; |
4b6b94ca | 227 | if (off < sizeof (struct tcphdr) || off > tlen) { |
ee954ff1 SL |
228 | if (tcpprintfs) |
229 | printf("tcp off: src %x off %d\n", ti->ti_src, off); | |
96c50630 | 230 | tcpstat.tcps_rcvbadoff++; |
8a13b737 | 231 | goto drop; |
2b4b57cd | 232 | } |
1e977657 BJ |
233 | tlen -= off; |
234 | ti->ti_len = tlen; | |
8b5a83bb | 235 | if (off > sizeof (struct tcphdr)) { |
a17510f3 MK |
236 | if (m->m_len < sizeof(struct ip) + off) { |
237 | if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { | |
96c50630 | 238 | tcpstat.tcps_rcvshort++; |
a17510f3 MK |
239 | return; |
240 | } | |
241 | ti = mtod(m, struct tcpiphdr *); | |
8b5a83bb | 242 | } |
cce93e4b | 243 | om = m_get(M_DONTWAIT, MT_DATA); |
8b5a83bb BJ |
244 | if (om == 0) |
245 | goto drop; | |
8b5a83bb BJ |
246 | om->m_len = off - sizeof (struct tcphdr); |
247 | { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); | |
668cc26d | 248 | bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); |
8b5a83bb | 249 | m->m_len -= om->m_len; |
668cc26d SL |
250 | bcopy(op+om->m_len, op, |
251 | (unsigned)(m->m_len-sizeof (struct tcpiphdr))); | |
8b5a83bb BJ |
252 | } |
253 | } | |
2ff61f9d | 254 | tiflags = ti->ti_flags; |
2b4b57cd | 255 | |
795e0416 | 256 | /* |
669abecf | 257 | * Drop TCP and IP headers; TCP options were dropped above. |
795e0416 | 258 | */ |
669abecf KM |
259 | m->m_off += sizeof(struct tcpiphdr); |
260 | m->m_len -= sizeof(struct tcpiphdr); | |
795e0416 | 261 | |
8a13b737 | 262 | /* |
4aed14e3 | 263 | * Convert TCP protocol specific fields to host format. |
8a13b737 BJ |
264 | */ |
265 | ti->ti_seq = ntohl(ti->ti_seq); | |
266 | ti->ti_ack = ntohl(ti->ti_ack); | |
267 | ti->ti_win = ntohs(ti->ti_win); | |
268 | ti->ti_urp = ntohs(ti->ti_urp); | |
269 | ||
2b4b57cd | 270 | /* |
8075bb0e | 271 | * Locate pcb for segment. |
2b4b57cd | 272 | */ |
96c50630 | 273 | findpcb: |
2ff61f9d | 274 | inp = in_pcblookup |
ebcadd38 BJ |
275 | (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, |
276 | INPLOOKUP_WILDCARD); | |
2ff61f9d BJ |
277 | |
278 | /* | |
279 | * If the state is CLOSED (i.e., TCB does not exist) then | |
4aed14e3 | 280 | * all data in the incoming segment is discarded. |
2ff61f9d | 281 | */ |
22856bb8 | 282 | if (inp == 0) |
8a13b737 | 283 | goto dropwithreset; |
2ff61f9d | 284 | tp = intotcpcb(inp); |
22856bb8 | 285 | if (tp == 0) |
8a13b737 | 286 | goto dropwithreset; |
f1b2fa5b | 287 | so = inp->inp_socket; |
4b935108 BJ |
288 | if (so->so_options & SO_DEBUG) { |
289 | ostate = tp->t_state; | |
290 | tcp_saveti = *ti; | |
291 | } | |
ebf42a75 BJ |
292 | if (so->so_options & SO_ACCEPTCONN) { |
293 | so = sonewconn(so); | |
294 | if (so == 0) | |
295 | goto drop; | |
7aa16f99 SL |
296 | /* |
297 | * This is ugly, but .... | |
298 | * | |
299 | * Mark socket as temporary until we're | |
300 | * committed to keeping it. The code at | |
301 | * ``drop'' and ``dropwithreset'' check the | |
302 | * flag dropsocket to see if the temporary | |
303 | * socket created here should be discarded. | |
304 | * We mark the socket as discardable until | |
305 | * we're committed to it below in TCPS_LISTEN. | |
306 | */ | |
307 | dropsocket++; | |
ebf42a75 BJ |
308 | inp = (struct inpcb *)so->so_pcb; |
309 | inp->inp_laddr = ti->ti_dst; | |
310 | inp->inp_lport = ti->ti_dport; | |
a17510f3 | 311 | inp->inp_options = ip_srcroute(); |
ebf42a75 BJ |
312 | tp = intotcpcb(inp); |
313 | tp->t_state = TCPS_LISTEN; | |
314 | } | |
87e78f19 | 315 | |
405c9168 BJ |
316 | /* |
317 | * Segment received on connection. | |
318 | * Reset idle time and keep-alive timer. | |
319 | */ | |
320 | tp->t_idle = 0; | |
321 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; | |
322 | ||
8b5a83bb | 323 | /* |
99578149 MK |
324 | * Process options if not in LISTEN state, |
325 | * else do it below (after getting remote address). | |
8b5a83bb | 326 | */ |
99578149 MK |
327 | if (om && tp->t_state != TCPS_LISTEN) { |
328 | tcp_dooptions(tp, om, ti); | |
8b5a83bb BJ |
329 | om = 0; |
330 | } | |
331 | ||
87e78f19 | 332 | /* |
8a13b737 BJ |
333 | * Calculate amount of space in receive window, |
334 | * and then do TCP input processing. | |
a17510f3 MK |
335 | * Receive window is amount of space in rcv queue, |
336 | * but not less than advertised window. | |
87e78f19 | 337 | */ |
bbaaf0fd MK |
338 | { int win; |
339 | ||
340 | win = sbspace(&so->so_rcv); | |
341 | if (win < 0) | |
342 | win = 0; | |
343 | tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt)); | |
344 | } | |
2ff61f9d | 345 | |
87e78f19 BJ |
346 | switch (tp->t_state) { |
347 | ||
2ff61f9d BJ |
348 | /* |
349 | * If the state is LISTEN then ignore segment if it contains an RST. | |
350 | * If the segment contains an ACK then it is bad and send a RST. | |
351 | * If it does not contain a SYN then it is not interesting; drop it. | |
224f3a72 | 352 | * Don't bother responding if the destination was a broadcast. |
8a13b737 | 353 | * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial |
2ff61f9d | 354 | * tp->iss, and send a segment: |
8a13b737 | 355 | * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> |
2ff61f9d BJ |
356 | * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. |
357 | * Fill in remote peer address fields if not previously specified. | |
358 | * Enter SYN_RECEIVED state, and process any other fields of this | |
4aed14e3 | 359 | * segment in this state. |
2ff61f9d | 360 | */ |
8075bb0e | 361 | case TCPS_LISTEN: { |
789d2a39 | 362 | struct mbuf *am; |
8075bb0e BJ |
363 | register struct sockaddr_in *sin; |
364 | ||
2ff61f9d BJ |
365 | if (tiflags & TH_RST) |
366 | goto drop; | |
22856bb8 | 367 | if (tiflags & TH_ACK) |
8a13b737 | 368 | goto dropwithreset; |
22856bb8 | 369 | if ((tiflags & TH_SYN) == 0) |
2ff61f9d | 370 | goto drop; |
224f3a72 MK |
371 | if (in_broadcast(ti->ti_dst)) |
372 | goto drop; | |
789d2a39 SL |
373 | am = m_get(M_DONTWAIT, MT_SONAME); |
374 | if (am == NULL) | |
375 | goto drop; | |
376 | am->m_len = sizeof (struct sockaddr_in); | |
a8d3bf7f | 377 | sin = mtod(am, struct sockaddr_in *); |
8075bb0e BJ |
378 | sin->sin_family = AF_INET; |
379 | sin->sin_addr = ti->ti_src; | |
380 | sin->sin_port = ti->ti_sport; | |
ebcadd38 | 381 | laddr = inp->inp_laddr; |
789d2a39 | 382 | if (inp->inp_laddr.s_addr == INADDR_ANY) |
ebcadd38 | 383 | inp->inp_laddr = ti->ti_dst; |
a8d3bf7f | 384 | if (in_pcbconnect(inp, am)) { |
ebcadd38 | 385 | inp->inp_laddr = laddr; |
5a1f132a | 386 | (void) m_free(am); |
4aed14e3 | 387 | goto drop; |
ebcadd38 | 388 | } |
5a1f132a | 389 | (void) m_free(am); |
4aed14e3 BJ |
390 | tp->t_template = tcp_template(tp); |
391 | if (tp->t_template == 0) { | |
8011f5df | 392 | tp = tcp_drop(tp, ENOBUFS); |
a4f7ea71 | 393 | dropsocket = 0; /* socket is already gone */ |
4aed14e3 BJ |
394 | goto drop; |
395 | } | |
99578149 MK |
396 | if (om) { |
397 | tcp_dooptions(tp, om, ti); | |
398 | om = 0; | |
399 | } | |
96c50630 MK |
400 | if (iss) |
401 | tp->iss = iss; | |
402 | else | |
403 | tp->iss = tcp_iss; | |
404 | tcp_iss += TCP_ISSINCR/2; | |
2ff61f9d | 405 | tp->irs = ti->ti_seq; |
8a13b737 BJ |
406 | tcp_sendseqinit(tp); |
407 | tcp_rcvseqinit(tp); | |
bbaaf0fd | 408 | tp->t_flags |= TF_ACKNOW; |
2ff61f9d | 409 | tp->t_state = TCPS_SYN_RECEIVED; |
4aed14e3 | 410 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; |
7aa16f99 | 411 | dropsocket = 0; /* committed to socket */ |
96c50630 | 412 | tcpstat.tcps_accepts++; |
8a13b737 | 413 | goto trimthenstep6; |
8075bb0e | 414 | } |
87e78f19 | 415 | |
2ff61f9d BJ |
416 | /* |
417 | * If the state is SYN_SENT: | |
418 | * if seg contains an ACK, but not for our SYN, drop the input. | |
419 | * if seg contains a RST, then drop the connection. | |
420 | * if seg does not contain SYN, then drop it. | |
421 | * Otherwise this is an acceptable SYN segment | |
422 | * initialize tp->rcv_nxt and tp->irs | |
423 | * if seg contains ack then advance tp->snd_una | |
424 | * if SYN has been acked change to ESTABLISHED else SYN_RCVD state | |
425 | * arrange for segment to be acked (eventually) | |
426 | * continue processing rest of data/controls, beginning with URG | |
427 | */ | |
428 | case TCPS_SYN_SENT: | |
429 | if ((tiflags & TH_ACK) && | |
a17510f3 | 430 | (SEQ_LEQ(ti->ti_ack, tp->iss) || |
4b6b94ca | 431 | SEQ_GT(ti->ti_ack, tp->snd_max))) |
8a13b737 | 432 | goto dropwithreset; |
2ff61f9d | 433 | if (tiflags & TH_RST) { |
0e3936fa SL |
434 | if (tiflags & TH_ACK) |
435 | tp = tcp_drop(tp, ECONNREFUSED); | |
2ff61f9d | 436 | goto drop; |
87e78f19 | 437 | } |
2ff61f9d BJ |
438 | if ((tiflags & TH_SYN) == 0) |
439 | goto drop; | |
b57e9490 MK |
440 | if (tiflags & TH_ACK) { |
441 | tp->snd_una = ti->ti_ack; | |
442 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) | |
443 | tp->snd_nxt = tp->snd_una; | |
444 | } | |
4aed14e3 | 445 | tp->t_timer[TCPT_REXMT] = 0; |
a6bbda13 MK |
446 | /* |
447 | * If we didn't have to retransmit, | |
448 | * set the initial estimate of srtt. | |
449 | * Set the variance to half the rtt | |
450 | * (so our first retransmit happens at 2*rtt). | |
451 | */ | |
452 | if (tp->t_rtt) { | |
453 | tp->t_srtt = tp->t_rtt << 3; | |
454 | tp->t_rttvar = tp->t_rtt << 1; | |
455 | tp->t_rtt = 0; | |
456 | tp->t_rxtshift = 0; | |
457 | TCPT_RANGESET(tp->t_rxtcur, | |
458 | ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, | |
459 | TCPTV_MIN, TCPTV_REXMTMAX); | |
460 | } | |
2ff61f9d | 461 | tp->irs = ti->ti_seq; |
8a13b737 BJ |
462 | tcp_rcvseqinit(tp); |
463 | tp->t_flags |= TF_ACKNOW; | |
b57e9490 | 464 | if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { |
96c50630 | 465 | tcpstat.tcps_connects++; |
4aed14e3 | 466 | soisconnected(so); |
2ff61f9d | 467 | tp->t_state = TCPS_ESTABLISHED; |
99578149 | 468 | tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); |
405c9168 BJ |
469 | (void) tcp_reass(tp, (struct tcpiphdr *)0); |
470 | } else | |
8a13b737 | 471 | tp->t_state = TCPS_SYN_RECEIVED; |
8a13b737 BJ |
472 | |
473 | trimthenstep6: | |
474 | /* | |
4b6b94ca | 475 | * Advance ti->ti_seq to correspond to first data byte. |
8a13b737 BJ |
476 | * If data, trim to stay within window, |
477 | * dropping FIN if necessary. | |
478 | */ | |
4b6b94ca | 479 | ti->ti_seq++; |
8a13b737 BJ |
480 | if (ti->ti_len > tp->rcv_wnd) { |
481 | todrop = ti->ti_len - tp->rcv_wnd; | |
482 | m_adj(m, -todrop); | |
483 | ti->ti_len = tp->rcv_wnd; | |
bbaaf0fd | 484 | tiflags &= ~TH_FIN; |
96c50630 MK |
485 | tcpstat.tcps_rcvpackafterwin++; |
486 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
87e78f19 | 487 | } |
e832edbc | 488 | tp->snd_wl1 = ti->ti_seq - 1; |
bbaaf0fd | 489 | tp->rcv_up = ti->ti_seq; |
8a13b737 | 490 | goto step6; |
2ff61f9d | 491 | } |
87e78f19 | 492 | |
2ff61f9d BJ |
493 | /* |
494 | * States other than LISTEN or SYN_SENT. | |
495 | * First check that at least some bytes of segment are within | |
96c50630 MK |
496 | * receive window. If segment begins before rcv_nxt, |
497 | * drop leading data (and SYN); if nothing left, just ack. | |
2ff61f9d | 498 | */ |
96c50630 MK |
499 | todrop = tp->rcv_nxt - ti->ti_seq; |
500 | if (todrop > 0) { | |
501 | if (tiflags & TH_SYN) { | |
502 | tiflags &= ~TH_SYN; | |
503 | ti->ti_seq++; | |
504 | if (ti->ti_urp > 1) | |
505 | ti->ti_urp--; | |
506 | else | |
507 | tiflags &= ~TH_URG; | |
508 | todrop--; | |
509 | } | |
510 | if (todrop > ti->ti_len || | |
511 | todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { | |
39b02f3c MK |
512 | #ifdef TCP_COMPAT_42 |
513 | /* | |
514 | * Don't toss RST in response to 4.2-style keepalive. | |
515 | */ | |
516 | if (ti->ti_seq == tp->rcv_nxt - 1 && tiflags & TH_RST) | |
517 | goto do_rst; | |
518 | #endif | |
96c50630 MK |
519 | tcpstat.tcps_rcvduppack++; |
520 | tcpstat.tcps_rcvdupbyte += ti->ti_len; | |
a6bbda13 MK |
521 | todrop = ti->ti_len; |
522 | tp->t_flags |= TF_ACKNOW; | |
523 | } else { | |
524 | tcpstat.tcps_rcvpartduppack++; | |
525 | tcpstat.tcps_rcvpartdupbyte += todrop; | |
96c50630 | 526 | } |
96c50630 MK |
527 | m_adj(m, todrop); |
528 | ti->ti_seq += todrop; | |
529 | ti->ti_len -= todrop; | |
530 | if (ti->ti_urp > todrop) | |
531 | ti->ti_urp -= todrop; | |
532 | else { | |
533 | tiflags &= ~TH_URG; | |
534 | ti->ti_urp = 0; | |
535 | } | |
536 | } | |
537 | ||
2ff61f9d BJ |
538 | if (tp->rcv_wnd == 0) { |
539 | /* | |
540 | * If window is closed can only take segments at | |
4b6b94ca | 541 | * window edge, and have to drop data and PUSH from |
2ff61f9d | 542 | * incoming segments. |
96c50630 MK |
543 | * |
544 | * If new data is received on a connection after the | |
545 | * user processes are gone, then RST the other end. | |
2ff61f9d | 546 | */ |
96c50630 MK |
547 | if ((so->so_state & SS_NOFDREF) && |
548 | tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { | |
549 | tp = tcp_close(tp); | |
550 | tcpstat.tcps_rcvafterclose++; | |
551 | goto dropwithreset; | |
552 | } | |
553 | if (tp->rcv_nxt != ti->ti_seq) { | |
554 | tcpstat.tcps_rcvpackafterwin++; | |
555 | tcpstat.tcps_rcvbyteafterwin += ti->ti_len; | |
2ff61f9d | 556 | goto dropafterack; |
96c50630 | 557 | } |
8a13b737 | 558 | if (ti->ti_len > 0) { |
96c50630 MK |
559 | if (ti->ti_len == 1) |
560 | tcpstat.tcps_rcvwinprobe++; | |
561 | else { | |
562 | tcpstat.tcps_rcvpackafterwin++; | |
563 | tcpstat.tcps_rcvbyteafterwin += ti->ti_len; | |
564 | } | |
fd5dc5f0 | 565 | m_adj(m, ti->ti_len); |
8a13b737 | 566 | ti->ti_len = 0; |
bbaaf0fd | 567 | tiflags &= ~(TH_PUSH|TH_FIN); |
87e78f19 | 568 | } |
2ff61f9d | 569 | } else { |
2ff61f9d BJ |
570 | /* |
571 | * If segment ends after window, drop trailing data | |
8a13b737 | 572 | * (and PUSH and FIN); if nothing left, just ACK. |
2ff61f9d | 573 | */ |
fd5dc5f0 BJ |
574 | todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); |
575 | if (todrop > 0) { | |
96c50630 MK |
576 | if (todrop >= ti->ti_len) { |
577 | /* | |
578 | * If a new connection request is received | |
579 | * while in TIME_WAIT, drop the old connection | |
580 | * and start over if the sequence numbers | |
581 | * are above the previous ones. | |
582 | */ | |
583 | if (tiflags & TH_SYN && | |
584 | tp->t_state == TCPS_TIME_WAIT && | |
585 | SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { | |
586 | iss = tp->rcv_nxt + TCP_ISSINCR; | |
587 | (void) tcp_close(tp); | |
588 | goto findpcb; | |
589 | } | |
590 | if (todrop == 1) | |
591 | tcpstat.tcps_rcvwinprobe++; | |
592 | else { | |
593 | tcpstat.tcps_rcvpackafterwin++; | |
2e5a76f2 | 594 | tcpstat.tcps_rcvbyteafterwin += ti->ti_len; |
96c50630 | 595 | } |
2ff61f9d | 596 | goto dropafterack; |
96c50630 MK |
597 | } |
598 | tcpstat.tcps_rcvpackafterwin++; | |
599 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
2ff61f9d BJ |
600 | m_adj(m, -todrop); |
601 | ti->ti_len -= todrop; | |
bbaaf0fd | 602 | tiflags &= ~(TH_PUSH|TH_FIN); |
87e78f19 | 603 | } |
87e78f19 | 604 | } |
87e78f19 | 605 | |
39b02f3c MK |
606 | #ifdef TCP_COMPAT_42 |
607 | do_rst: | |
608 | #endif | |
87e78f19 | 609 | /* |
2ff61f9d BJ |
610 | * If the RST bit is set examine the state: |
611 | * SYN_RECEIVED STATE: | |
612 | * If passive open, return to LISTEN state. | |
613 | * If active open, inform user that connection was refused. | |
614 | * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: | |
615 | * Inform user that connection was reset, and close tcb. | |
616 | * CLOSING, LAST_ACK, TIME_WAIT STATES | |
617 | * Close the tcb. | |
87e78f19 | 618 | */ |
2ff61f9d | 619 | if (tiflags&TH_RST) switch (tp->t_state) { |
4b935108 | 620 | |
2ff61f9d | 621 | case TCPS_SYN_RECEIVED: |
0e3936fa | 622 | tp = tcp_drop(tp, ECONNREFUSED); |
2ff61f9d BJ |
623 | goto drop; |
624 | ||
625 | case TCPS_ESTABLISHED: | |
626 | case TCPS_FIN_WAIT_1: | |
627 | case TCPS_FIN_WAIT_2: | |
628 | case TCPS_CLOSE_WAIT: | |
0e3936fa | 629 | tp = tcp_drop(tp, ECONNRESET); |
2ff61f9d BJ |
630 | goto drop; |
631 | ||
632 | case TCPS_CLOSING: | |
633 | case TCPS_LAST_ACK: | |
634 | case TCPS_TIME_WAIT: | |
0e3936fa | 635 | tp = tcp_close(tp); |
2ff61f9d | 636 | goto drop; |
87e78f19 | 637 | } |
87e78f19 BJ |
638 | |
639 | /* | |
2ff61f9d BJ |
640 | * If a SYN is in the window, then this is an |
641 | * error and we send an RST and drop the connection. | |
642 | */ | |
643 | if (tiflags & TH_SYN) { | |
0e3936fa | 644 | tp = tcp_drop(tp, ECONNRESET); |
8a13b737 | 645 | goto dropwithreset; |
2ff61f9d BJ |
646 | } |
647 | ||
648 | /* | |
649 | * If the ACK bit is off we drop the segment and return. | |
650 | */ | |
8a13b737 | 651 | if ((tiflags & TH_ACK) == 0) |
2ff61f9d BJ |
652 | goto drop; |
653 | ||
654 | /* | |
655 | * Ack processing. | |
87e78f19 | 656 | */ |
87e78f19 BJ |
657 | switch (tp->t_state) { |
658 | ||
2ff61f9d BJ |
659 | /* |
660 | * In SYN_RECEIVED state if the ack ACKs our SYN then enter | |
4859921b | 661 | * ESTABLISHED state and continue processing, otherwise |
2ff61f9d BJ |
662 | * send an RST. |
663 | */ | |
664 | case TCPS_SYN_RECEIVED: | |
8a13b737 | 665 | if (SEQ_GT(tp->snd_una, ti->ti_ack) || |
4b6b94ca | 666 | SEQ_GT(ti->ti_ack, tp->snd_max)) |
8a13b737 | 667 | goto dropwithreset; |
96c50630 | 668 | tcpstat.tcps_connects++; |
8a13b737 BJ |
669 | soisconnected(so); |
670 | tp->t_state = TCPS_ESTABLISHED; | |
99578149 | 671 | tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); |
405c9168 | 672 | (void) tcp_reass(tp, (struct tcpiphdr *)0); |
4aed14e3 | 673 | tp->snd_wl1 = ti->ti_seq - 1; |
8a13b737 | 674 | /* fall into ... */ |
87e78f19 | 675 | |
2ff61f9d BJ |
676 | /* |
677 | * In ESTABLISHED state: drop duplicate ACKs; ACK out of range | |
678 | * ACKs. If the ack is in the range | |
4b6b94ca | 679 | * tp->snd_una < ti->ti_ack <= tp->snd_max |
2ff61f9d BJ |
680 | * then advance tp->snd_una to ti->ti_ack and drop |
681 | * data from the retransmission queue. If this ACK reflects | |
682 | * more up to date window information we update our window information. | |
683 | */ | |
684 | case TCPS_ESTABLISHED: | |
685 | case TCPS_FIN_WAIT_1: | |
686 | case TCPS_FIN_WAIT_2: | |
687 | case TCPS_CLOSE_WAIT: | |
688 | case TCPS_CLOSING: | |
4aed14e3 BJ |
689 | case TCPS_LAST_ACK: |
690 | case TCPS_TIME_WAIT: | |
8a13b737 | 691 | |
96c50630 MK |
692 | if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { |
693 | if (ti->ti_len == 0) | |
694 | tcpstat.tcps_rcvdupack++; | |
2ff61f9d | 695 | break; |
96c50630 MK |
696 | } |
697 | if (SEQ_GT(ti->ti_ack, tp->snd_max)) { | |
698 | tcpstat.tcps_rcvacktoomuch++; | |
2ff61f9d | 699 | goto dropafterack; |
96c50630 | 700 | } |
8a13b737 | 701 | acked = ti->ti_ack - tp->snd_una; |
96c50630 MK |
702 | tcpstat.tcps_rcvackpack++; |
703 | tcpstat.tcps_rcvackbyte += acked; | |
dd020fc8 BJ |
704 | |
705 | /* | |
706 | * If transmit timer is running and timed sequence | |
707 | * number was acked, update smoothed round trip time. | |
a6bbda13 MK |
708 | * Since we now have an rtt measurement, cancel the |
709 | * timer backoff (cf., Phil Karn's retransmit alg.). | |
710 | * Recompute the initial retransmit timer. | |
dd020fc8 BJ |
711 | */ |
712 | if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { | |
96c50630 | 713 | tcpstat.tcps_rttupdated++; |
7cc62c26 MK |
714 | if (tp->t_srtt != 0) { |
715 | register short delta; | |
716 | ||
717 | /* | |
718 | * srtt is stored as fixed point with 3 bits | |
719 | * after the binary point (i.e., scaled by 8). | |
720 | * The following magic is equivalent | |
721 | * to the smoothing algorithm in rfc793 | |
722 | * with an alpha of .875 | |
723 | * (srtt = rtt/8 + srtt*7/8 in fixed point). | |
724 | */ | |
725 | delta = tp->t_rtt - (tp->t_srtt >> 3); | |
726 | if ((tp->t_srtt += delta) <= 0) | |
727 | tp->t_srtt = 1; | |
728 | /* | |
a6bbda13 MK |
729 | * We accumulate a smoothed rtt variance |
730 | * (actually, a smoothed mean difference), | |
7cc62c26 MK |
731 | * then set the retransmit timer to smoothed |
732 | * rtt + 2 times the smoothed variance. | |
733 | * rttvar is strored as fixed point | |
734 | * with 2 bits after the binary point | |
735 | * (scaled by 4). The following is equivalent | |
736 | * to rfc793 smoothing with an alpha of .75 | |
737 | * (rttvar = rttvar*3/4 + |delta| / 4). | |
738 | * This replaces rfc793's wired-in beta. | |
739 | */ | |
740 | if (delta < 0) | |
741 | delta = -delta; | |
742 | delta -= (tp->t_rttvar >> 2); | |
743 | if ((tp->t_rttvar += delta) <= 0) | |
744 | tp->t_rttvar = 1; | |
745 | } else { | |
746 | /* | |
747 | * No rtt measurement yet - use the | |
748 | * unsmoothed rtt. Set the variance | |
749 | * to half the rtt (so our first | |
750 | * retransmit happens at 2*rtt) | |
751 | */ | |
752 | tp->t_srtt = tp->t_rtt << 3; | |
753 | tp->t_rttvar = tp->t_rtt << 1; | |
754 | } | |
dd020fc8 | 755 | tp->t_rtt = 0; |
a6bbda13 MK |
756 | tp->t_rxtshift = 0; |
757 | TCPT_RANGESET(tp->t_rxtcur, | |
758 | ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, | |
759 | TCPTV_MIN, TCPTV_REXMTMAX); | |
dd020fc8 BJ |
760 | } |
761 | ||
91039e49 MK |
762 | /* |
763 | * If all outstanding data is acked, stop retransmit | |
764 | * timer and remember to restart (more output or persist). | |
765 | * If there is more data to be acked, restart retransmit | |
a6bbda13 | 766 | * timer, using current (possibly backed-off) value. |
91039e49 MK |
767 | */ |
768 | if (ti->ti_ack == tp->snd_max) { | |
4aed14e3 | 769 | tp->t_timer[TCPT_REXMT] = 0; |
91039e49 | 770 | needoutput = 1; |
a6bbda13 MK |
771 | } else if (tp->t_timer[TCPT_PERSIST] == 0) |
772 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; | |
1e9621b8 | 773 | /* |
2e5a76f2 | 774 | * When new data is acked, open the congestion window |
a6bbda13 | 775 | * by one max-sized segment. |
1e9621b8 | 776 | */ |
2e5a76f2 | 777 | tp->snd_cwnd = MIN(tp->snd_cwnd + tp->t_maxseg, 65535); |
6703c41f | 778 | if (acked > so->so_snd.sb_cc) { |
6703c41f | 779 | tp->snd_wnd -= so->so_snd.sb_cc; |
8011f5df | 780 | sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); |
4859921b | 781 | ourfinisacked = 1; |
6703c41f | 782 | } else { |
668cc26d | 783 | sbdrop(&so->so_snd, acked); |
6703c41f | 784 | tp->snd_wnd -= acked; |
4859921b | 785 | ourfinisacked = 0; |
6703c41f | 786 | } |
5744ed2b | 787 | if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) |
22856bb8 | 788 | sowwakeup(so); |
4b6b94ca | 789 | tp->snd_una = ti->ti_ack; |
b8977237 BJ |
790 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
791 | tp->snd_nxt = tp->snd_una; | |
405c9168 | 792 | |
87e78f19 BJ |
793 | switch (tp->t_state) { |
794 | ||
2ff61f9d BJ |
795 | /* |
796 | * In FIN_WAIT_1 STATE in addition to the processing | |
797 | * for the ESTABLISHED state if our FIN is now acknowledged | |
8a13b737 | 798 | * then enter FIN_WAIT_2. |
2ff61f9d BJ |
799 | */ |
800 | case TCPS_FIN_WAIT_1: | |
fdae4427 BJ |
801 | if (ourfinisacked) { |
802 | /* | |
803 | * If we can't receive any more | |
804 | * data, then closing user can proceed. | |
a17510f3 MK |
805 | * Starting the timer is contrary to the |
806 | * specification, but if we don't get a FIN | |
807 | * we'll hang forever. | |
fdae4427 | 808 | */ |
a17510f3 | 809 | if (so->so_state & SS_CANTRCVMORE) { |
fdae4427 | 810 | soisdisconnected(so); |
a17510f3 MK |
811 | tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; |
812 | } | |
8a13b737 | 813 | tp->t_state = TCPS_FIN_WAIT_2; |
fdae4427 | 814 | } |
87e78f19 BJ |
815 | break; |
816 | ||
2ff61f9d BJ |
817 | /* |
818 | * In CLOSING STATE in addition to the processing for | |
819 | * the ESTABLISHED state if the ACK acknowledges our FIN | |
820 | * then enter the TIME-WAIT state, otherwise ignore | |
821 | * the segment. | |
822 | */ | |
823 | case TCPS_CLOSING: | |
4aed14e3 | 824 | if (ourfinisacked) { |
2ff61f9d | 825 | tp->t_state = TCPS_TIME_WAIT; |
4aed14e3 BJ |
826 | tcp_canceltimers(tp); |
827 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; | |
828 | soisdisconnected(so); | |
829 | } | |
830 | break; | |
87e78f19 | 831 | |
2ff61f9d | 832 | /* |
e20bac9c MK |
833 | * In LAST_ACK, we may still be waiting for data to drain |
834 | * and/or to be acked, as well as for the ack of our FIN. | |
835 | * If our FIN is now acknowledged, delete the TCB, | |
836 | * enter the closed state and return. | |
2ff61f9d BJ |
837 | */ |
838 | case TCPS_LAST_ACK: | |
e20bac9c | 839 | if (ourfinisacked) { |
0e3936fa | 840 | tp = tcp_close(tp); |
e20bac9c MK |
841 | goto drop; |
842 | } | |
843 | break; | |
87e78f19 | 844 | |
2ff61f9d BJ |
845 | /* |
846 | * In TIME_WAIT state the only thing that should arrive | |
847 | * is a retransmission of the remote FIN. Acknowledge | |
848 | * it and restart the finack timer. | |
849 | */ | |
850 | case TCPS_TIME_WAIT: | |
405c9168 | 851 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
2ff61f9d | 852 | goto dropafterack; |
87e78f19 | 853 | } |
8a13b737 | 854 | } |
87e78f19 | 855 | |
2ff61f9d | 856 | step6: |
4aed14e3 BJ |
857 | /* |
858 | * Update window information. | |
bbaaf0fd | 859 | * Don't look at window if no ACK: TAC's send garbage on first SYN. |
4aed14e3 | 860 | */ |
bbaaf0fd MK |
861 | if ((tiflags & TH_ACK) && |
862 | (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && | |
8e65fd66 | 863 | (SEQ_LT(tp->snd_wl2, ti->ti_ack) || |
bbaaf0fd | 864 | tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { |
96c50630 MK |
865 | /* keep track of pure window updates */ |
866 | if (ti->ti_len == 0 && | |
867 | tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) { | |
868 | tcpstat.tcps_rcvwinupd++; | |
869 | tcpstat.tcps_rcvdupack--; | |
870 | } | |
4aed14e3 BJ |
871 | tp->snd_wnd = ti->ti_win; |
872 | tp->snd_wl1 = ti->ti_seq; | |
873 | tp->snd_wl2 = ti->ti_ack; | |
18a438b6 MK |
874 | if (tp->snd_wnd > tp->max_sndwnd) |
875 | tp->max_sndwnd = tp->snd_wnd; | |
91039e49 MK |
876 | needoutput = 1; |
877 | } | |
4aed14e3 | 878 | |
2ff61f9d | 879 | /* |
b2db9217 | 880 | * Process segments with URG. |
2ff61f9d | 881 | */ |
9c811062 BJ |
882 | if ((tiflags & TH_URG) && ti->ti_urp && |
883 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
f4be5024 | 884 | /* |
bbaaf0fd | 885 | * This is a kludge, but if we receive and accept |
a5d9c993 SL |
886 | * random urgent pointers, we'll crash in |
887 | * soreceive. It's hard to imagine someone | |
888 | * actually wanting to send this much urgent data. | |
f4be5024 | 889 | */ |
2f4f574f | 890 | if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { |
f4be5024 SL |
891 | ti->ti_urp = 0; /* XXX */ |
892 | tiflags &= ~TH_URG; /* XXX */ | |
bbaaf0fd | 893 | goto dodata; /* XXX */ |
f4be5024 | 894 | } |
b2db9217 BJ |
895 | /* |
896 | * If this segment advances the known urgent pointer, | |
897 | * then mark the data stream. This should not happen | |
898 | * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since | |
899 | * a FIN has been received from the remote side. | |
900 | * In these states we ignore the URG. | |
ae6760c5 MK |
901 | * |
902 | * According to RFC961 (Assigned Protocols), | |
903 | * the urgent pointer points to the last octet | |
904 | * of urgent data. We continue, however, | |
905 | * to consider it to indicate the first octet | |
906 | * of data past the urgent section | |
907 | * as the original spec states. | |
b2db9217 BJ |
908 | */ |
909 | if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { | |
910 | tp->rcv_up = ti->ti_seq + ti->ti_urp; | |
911 | so->so_oobmark = so->so_rcv.sb_cc + | |
912 | (tp->rcv_up - tp->rcv_nxt) - 1; | |
913 | if (so->so_oobmark == 0) | |
914 | so->so_state |= SS_RCVATMARK; | |
77a4e3ca | 915 | sohasoutofband(so); |
a17510f3 | 916 | tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); |
b2db9217 BJ |
917 | } |
918 | /* | |
919 | * Remove out of band data so doesn't get presented to user. | |
920 | * This can happen independent of advancing the URG pointer, | |
921 | * but if two URG's are pending at once, some out-of-band | |
922 | * data may creep in... ick. | |
923 | */ | |
ae6760c5 MK |
924 | if (ti->ti_urp <= ti->ti_len && |
925 | (so->so_options & SO_OOBINLINE) == 0) | |
b2db9217 | 926 | tcp_pulloutofband(so, ti); |
bbaaf0fd MK |
927 | } else |
928 | /* | |
929 | * If no out of band data is expected, | |
930 | * pull receive urgent pointer along | |
931 | * with the receive window. | |
932 | */ | |
933 | if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) | |
934 | tp->rcv_up = tp->rcv_nxt; | |
935 | dodata: /* XXX */ | |
87e78f19 BJ |
936 | |
937 | /* | |
2ff61f9d BJ |
938 | * Process the segment text, merging it into the TCP sequencing queue, |
939 | * and arranging for acknowledgment of receipt if necessary. | |
940 | * This process logically involves adjusting tp->rcv_wnd as data | |
941 | * is presented to the user (this happens in tcp_usrreq.c, | |
942 | * case PRU_RCVD). If a FIN has already been received on this | |
943 | * connection then we just ignore the text. | |
87e78f19 | 944 | */ |
7984a662 MK |
945 | if ((ti->ti_len || (tiflags&TH_FIN)) && |
946 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
a17510f3 | 947 | TCP_REASS(tp, ti, m, so, tiflags); |
8b5a83bb BJ |
948 | if (tcpnodelack == 0) |
949 | tp->t_flags |= TF_DELACK; | |
950 | else | |
951 | tp->t_flags |= TF_ACKNOW; | |
18a438b6 MK |
952 | /* |
953 | * Note the amount of data that peer has sent into | |
954 | * our window, in order to estimate the sender's | |
955 | * buffer size. | |
956 | */ | |
a6bbda13 | 957 | len = tp->rcv_nxt - tp->rcv_adv; |
18a438b6 MK |
958 | if (len > tp->max_rcvd) |
959 | tp->max_rcvd = len; | |
4aed14e3 | 960 | } else { |
2b4b57cd | 961 | m_freem(m); |
e832edbc | 962 | tiflags &= ~TH_FIN; |
4aed14e3 | 963 | } |
87e78f19 BJ |
964 | |
965 | /* | |
e832edbc BJ |
966 | * If FIN is received ACK the FIN and let the user know |
967 | * that the connection is closing. | |
87e78f19 | 968 | */ |
e832edbc | 969 | if (tiflags & TH_FIN) { |
4aed14e3 BJ |
970 | if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { |
971 | socantrcvmore(so); | |
972 | tp->t_flags |= TF_ACKNOW; | |
973 | tp->rcv_nxt++; | |
974 | } | |
2ff61f9d | 975 | switch (tp->t_state) { |
87e78f19 | 976 | |
2ff61f9d BJ |
977 | /* |
978 | * In SYN_RECEIVED and ESTABLISHED STATES | |
979 | * enter the CLOSE_WAIT state. | |
53a5409e | 980 | */ |
2ff61f9d BJ |
981 | case TCPS_SYN_RECEIVED: |
982 | case TCPS_ESTABLISHED: | |
983 | tp->t_state = TCPS_CLOSE_WAIT; | |
984 | break; | |
53a5409e | 985 | |
2ff61f9d | 986 | /* |
8a13b737 BJ |
987 | * If still in FIN_WAIT_1 STATE FIN has not been acked so |
988 | * enter the CLOSING state. | |
53a5409e | 989 | */ |
2ff61f9d | 990 | case TCPS_FIN_WAIT_1: |
8a13b737 | 991 | tp->t_state = TCPS_CLOSING; |
2ff61f9d | 992 | break; |
87e78f19 | 993 | |
2ff61f9d BJ |
994 | /* |
995 | * In FIN_WAIT_2 state enter the TIME_WAIT state, | |
996 | * starting the time-wait timer, turning off the other | |
997 | * standard timers. | |
998 | */ | |
999 | case TCPS_FIN_WAIT_2: | |
4aed14e3 | 1000 | tp->t_state = TCPS_TIME_WAIT; |
a6503abf | 1001 | tcp_canceltimers(tp); |
405c9168 | 1002 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
4aed14e3 | 1003 | soisdisconnected(so); |
2ff61f9d BJ |
1004 | break; |
1005 | ||
53a5409e | 1006 | /* |
2ff61f9d | 1007 | * In TIME_WAIT state restart the 2 MSL time_wait timer. |
53a5409e | 1008 | */ |
2ff61f9d | 1009 | case TCPS_TIME_WAIT: |
405c9168 | 1010 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
2ff61f9d | 1011 | break; |
8a13b737 | 1012 | } |
87e78f19 | 1013 | } |
4b935108 BJ |
1014 | if (so->so_options & SO_DEBUG) |
1015 | tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); | |
8a13b737 BJ |
1016 | |
1017 | /* | |
1018 | * Return any desired output. | |
1019 | */ | |
91039e49 | 1020 | if (needoutput || (tp->t_flags & TF_ACKNOW)) |
bbaaf0fd | 1021 | (void) tcp_output(tp); |
2ff61f9d | 1022 | return; |
8a13b737 | 1023 | |
2ff61f9d | 1024 | dropafterack: |
8a13b737 | 1025 | /* |
1e977657 BJ |
1026 | * Generate an ACK dropping incoming segment if it occupies |
1027 | * sequence space, where the ACK reflects our state. | |
8a13b737 | 1028 | */ |
ad616704 | 1029 | if (tiflags & TH_RST) |
8a13b737 | 1030 | goto drop; |
5722bd39 | 1031 | m_freem(m); |
4859921b MK |
1032 | tp->t_flags |= TF_ACKNOW; |
1033 | (void) tcp_output(tp); | |
4b6b94ca | 1034 | return; |
8a13b737 BJ |
1035 | |
1036 | dropwithreset: | |
f37c1c84 | 1037 | if (om) { |
668cc26d | 1038 | (void) m_free(om); |
f37c1c84 SL |
1039 | om = 0; |
1040 | } | |
8a13b737 | 1041 | /* |
4aed14e3 | 1042 | * Generate a RST, dropping incoming segment. |
8a13b737 | 1043 | * Make ACK acceptable to originator of segment. |
224f3a72 | 1044 | * Don't bother to respond if destination was broadcast. |
8a13b737 | 1045 | */ |
224f3a72 | 1046 | if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) |
8a13b737 BJ |
1047 | goto drop; |
1048 | if (tiflags & TH_ACK) | |
8e65fd66 | 1049 | tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); |
8a13b737 BJ |
1050 | else { |
1051 | if (tiflags & TH_SYN) | |
1052 | ti->ti_len++; | |
1e977657 BJ |
1053 | tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, |
1054 | TH_RST|TH_ACK); | |
8a13b737 | 1055 | } |
7aa16f99 SL |
1056 | /* destroy temporarily created socket */ |
1057 | if (dropsocket) | |
1058 | (void) soabort(so); | |
4b6b94ca | 1059 | return; |
8a13b737 | 1060 | |
2ff61f9d | 1061 | drop: |
01b1394e SL |
1062 | if (om) |
1063 | (void) m_free(om); | |
8a13b737 BJ |
1064 | /* |
1065 | * Drop space held by incoming segment and return. | |
1066 | */ | |
f3cdd721 BJ |
1067 | if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) |
1068 | tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); | |
2ff61f9d | 1069 | m_freem(m); |
7aa16f99 SL |
1070 | /* destroy temporarily created socket */ |
1071 | if (dropsocket) | |
1072 | (void) soabort(so); | |
4b935108 | 1073 | return; |
2ff61f9d BJ |
1074 | } |
1075 | ||
99578149 | 1076 | tcp_dooptions(tp, om, ti) |
8b5a83bb BJ |
1077 | struct tcpcb *tp; |
1078 | struct mbuf *om; | |
99578149 | 1079 | struct tcpiphdr *ti; |
5e74df82 | 1080 | { |
8b5a83bb BJ |
1081 | register u_char *cp; |
1082 | int opt, optlen, cnt; | |
1083 | ||
1084 | cp = mtod(om, u_char *); | |
1085 | cnt = om->m_len; | |
1086 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
1087 | opt = cp[0]; | |
1088 | if (opt == TCPOPT_EOL) | |
1089 | break; | |
1090 | if (opt == TCPOPT_NOP) | |
1091 | optlen = 1; | |
357b20fc | 1092 | else { |
8b5a83bb | 1093 | optlen = cp[1]; |
357b20fc SL |
1094 | if (optlen <= 0) |
1095 | break; | |
1096 | } | |
8b5a83bb BJ |
1097 | switch (opt) { |
1098 | ||
1099 | default: | |
1100 | break; | |
1101 | ||
1102 | case TCPOPT_MAXSEG: | |
1103 | if (optlen != 4) | |
1104 | continue; | |
99578149 MK |
1105 | if (!(ti->ti_flags & TH_SYN)) |
1106 | continue; | |
8b5a83bb | 1107 | tp->t_maxseg = *(u_short *)(cp + 2); |
668cc26d | 1108 | tp->t_maxseg = ntohs((u_short)tp->t_maxseg); |
99578149 | 1109 | tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); |
8b5a83bb | 1110 | break; |
8b5a83bb | 1111 | } |
5e74df82 | 1112 | } |
668cc26d | 1113 | (void) m_free(om); |
5e74df82 BJ |
1114 | } |
1115 | ||
b2db9217 BJ |
1116 | /* |
1117 | * Pull out of band byte out of a segment so | |
1118 | * it doesn't appear in the user's data queue. | |
1119 | * It is still reflected in the segment length for | |
1120 | * sequencing purposes. | |
1121 | */ | |
1122 | tcp_pulloutofband(so, ti) | |
1123 | struct socket *so; | |
1124 | struct tcpiphdr *ti; | |
1125 | { | |
1126 | register struct mbuf *m; | |
1acff8ec | 1127 | int cnt = ti->ti_urp - 1; |
b2db9217 BJ |
1128 | |
1129 | m = dtom(ti); | |
1130 | while (cnt >= 0) { | |
1131 | if (m->m_len > cnt) { | |
1132 | char *cp = mtod(m, caddr_t) + cnt; | |
1133 | struct tcpcb *tp = sototcpcb(so); | |
1134 | ||
1135 | tp->t_iobc = *cp; | |
1136 | tp->t_oobflags |= TCPOOB_HAVEDATA; | |
668cc26d | 1137 | bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); |
b2db9217 BJ |
1138 | m->m_len--; |
1139 | return; | |
1140 | } | |
1141 | cnt -= m->m_len; | |
1142 | m = m->m_next; | |
1143 | if (m == 0) | |
1144 | break; | |
1145 | } | |
1146 | panic("tcp_pulloutofband"); | |
1147 | } | |
1148 | ||
99578149 MK |
1149 | /* |
1150 | * Determine a reasonable value for maxseg size. | |
1151 | * If the route is known, use one that can be handled | |
1152 | * on the given interface without forcing IP to fragment. | |
7cc62c26 MK |
1153 | * If bigger than an mbuf cluster (MCLBYTES), round down to nearest size |
1154 | * to utilize large mbufs. | |
99578149 | 1155 | * If interface pointer is unavailable, or the destination isn't local, |
c2a1cd2c MK |
1156 | * use a conservative size (512 or the default IP max size, but no more |
1157 | * than the mtu of the interface through which we route), | |
99578149 | 1158 | * as we can't discover anything about intervening gateways or networks. |
a6bbda13 MK |
1159 | * We also initialize the congestion/slow start window to be a single |
1160 | * segment if the destination isn't local; this information should | |
1161 | * probably all be saved with the routing entry at the transport level. | |
99578149 MK |
1162 | * |
1163 | * This is ugly, and doesn't belong at this level, but has to happen somehow. | |
1164 | */ | |
1165 | tcp_mss(tp) | |
c2a1cd2c | 1166 | register struct tcpcb *tp; |
99578149 MK |
1167 | { |
1168 | struct route *ro; | |
1169 | struct ifnet *ifp; | |
1170 | int mss; | |
1171 | struct inpcb *inp; | |
1172 | ||
1173 | inp = tp->t_inpcb; | |
1174 | ro = &inp->inp_route; | |
1175 | if ((ro->ro_rt == (struct rtentry *)0) || | |
1176 | (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { | |
1177 | /* No route yet, so try to acquire one */ | |
1178 | if (inp->inp_faddr.s_addr != INADDR_ANY) { | |
1179 | ro->ro_dst.sa_family = AF_INET; | |
1180 | ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = | |
1181 | inp->inp_faddr; | |
1182 | rtalloc(ro); | |
1183 | } | |
1184 | if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) | |
0d115dcc | 1185 | return (TCP_MSS); |
99578149 MK |
1186 | } |
1187 | ||
1188 | mss = ifp->if_mtu - sizeof(struct tcpiphdr); | |
7cc62c26 MK |
1189 | #if (MCLBYTES & (MCLBYTES - 1)) == 0 |
1190 | if (mss > MCLBYTES) | |
1191 | mss &= ~(MCLBYTES-1); | |
99578149 | 1192 | #else |
7cc62c26 MK |
1193 | if (mss > MCLBYTES) |
1194 | mss = mss / MCLBYTES * MCLBYTES; | |
99578149 | 1195 | #endif |
c2a1cd2c MK |
1196 | if (in_localaddr(inp->inp_faddr)) |
1197 | return (mss); | |
a6bbda13 MK |
1198 | mss = MIN(mss, TCP_MSS); |
1199 | tp->snd_cwnd = mss; | |
1200 | return (mss); | |
99578149 | 1201 | } |