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 | * | |
96c50630 | 6 | * @(#)tcp_input.c 7.2 (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; |
91039e49 | 181 | int todrop, acked, 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; | |
4b6b94ca | 440 | tp->snd_una = ti->ti_ack; |
b8977237 BJ |
441 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
442 | tp->snd_nxt = tp->snd_una; | |
4aed14e3 | 443 | tp->t_timer[TCPT_REXMT] = 0; |
2ff61f9d | 444 | tp->irs = ti->ti_seq; |
8a13b737 BJ |
445 | tcp_rcvseqinit(tp); |
446 | tp->t_flags |= TF_ACKNOW; | |
405c9168 | 447 | if (SEQ_GT(tp->snd_una, tp->iss)) { |
96c50630 | 448 | tcpstat.tcps_connects++; |
4aed14e3 | 449 | soisconnected(so); |
2ff61f9d | 450 | tp->t_state = TCPS_ESTABLISHED; |
99578149 | 451 | tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); |
405c9168 BJ |
452 | (void) tcp_reass(tp, (struct tcpiphdr *)0); |
453 | } else | |
8a13b737 | 454 | tp->t_state = TCPS_SYN_RECEIVED; |
8a13b737 BJ |
455 | |
456 | trimthenstep6: | |
457 | /* | |
4b6b94ca | 458 | * Advance ti->ti_seq to correspond to first data byte. |
8a13b737 BJ |
459 | * If data, trim to stay within window, |
460 | * dropping FIN if necessary. | |
461 | */ | |
4b6b94ca | 462 | ti->ti_seq++; |
8a13b737 BJ |
463 | if (ti->ti_len > tp->rcv_wnd) { |
464 | todrop = ti->ti_len - tp->rcv_wnd; | |
465 | m_adj(m, -todrop); | |
466 | ti->ti_len = tp->rcv_wnd; | |
bbaaf0fd | 467 | tiflags &= ~TH_FIN; |
96c50630 MK |
468 | tcpstat.tcps_rcvpackafterwin++; |
469 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
87e78f19 | 470 | } |
e832edbc | 471 | tp->snd_wl1 = ti->ti_seq - 1; |
bbaaf0fd | 472 | tp->rcv_up = ti->ti_seq; |
8a13b737 | 473 | goto step6; |
2ff61f9d | 474 | } |
87e78f19 | 475 | |
2ff61f9d BJ |
476 | /* |
477 | * States other than LISTEN or SYN_SENT. | |
478 | * First check that at least some bytes of segment are within | |
96c50630 MK |
479 | * receive window. If segment begins before rcv_nxt, |
480 | * drop leading data (and SYN); if nothing left, just ack. | |
2ff61f9d | 481 | */ |
96c50630 MK |
482 | todrop = tp->rcv_nxt - ti->ti_seq; |
483 | if (todrop > 0) { | |
484 | if (tiflags & TH_SYN) { | |
485 | tiflags &= ~TH_SYN; | |
486 | ti->ti_seq++; | |
487 | if (ti->ti_urp > 1) | |
488 | ti->ti_urp--; | |
489 | else | |
490 | tiflags &= ~TH_URG; | |
491 | todrop--; | |
492 | } | |
493 | if (todrop > ti->ti_len || | |
494 | todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { | |
495 | tcpstat.tcps_rcvduppack++; | |
496 | tcpstat.tcps_rcvdupbyte += ti->ti_len; | |
497 | goto dropafterack; | |
498 | } | |
499 | tcpstat.tcps_rcvpartduppack++; | |
500 | tcpstat.tcps_rcvpartdupbyte += todrop; | |
501 | m_adj(m, todrop); | |
502 | ti->ti_seq += todrop; | |
503 | ti->ti_len -= todrop; | |
504 | if (ti->ti_urp > todrop) | |
505 | ti->ti_urp -= todrop; | |
506 | else { | |
507 | tiflags &= ~TH_URG; | |
508 | ti->ti_urp = 0; | |
509 | } | |
510 | } | |
511 | ||
2ff61f9d BJ |
512 | if (tp->rcv_wnd == 0) { |
513 | /* | |
514 | * If window is closed can only take segments at | |
4b6b94ca | 515 | * window edge, and have to drop data and PUSH from |
2ff61f9d | 516 | * incoming segments. |
96c50630 MK |
517 | * |
518 | * If new data is received on a connection after the | |
519 | * user processes are gone, then RST the other end. | |
2ff61f9d | 520 | */ |
96c50630 MK |
521 | if ((so->so_state & SS_NOFDREF) && |
522 | tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { | |
523 | tp = tcp_close(tp); | |
524 | tcpstat.tcps_rcvafterclose++; | |
525 | goto dropwithreset; | |
526 | } | |
527 | if (tp->rcv_nxt != ti->ti_seq) { | |
528 | tcpstat.tcps_rcvpackafterwin++; | |
529 | tcpstat.tcps_rcvbyteafterwin += ti->ti_len; | |
2ff61f9d | 530 | goto dropafterack; |
96c50630 | 531 | } |
8a13b737 | 532 | if (ti->ti_len > 0) { |
96c50630 MK |
533 | if (ti->ti_len == 1) |
534 | tcpstat.tcps_rcvwinprobe++; | |
535 | else { | |
536 | tcpstat.tcps_rcvpackafterwin++; | |
537 | tcpstat.tcps_rcvbyteafterwin += ti->ti_len; | |
538 | } | |
fd5dc5f0 | 539 | m_adj(m, ti->ti_len); |
8a13b737 | 540 | ti->ti_len = 0; |
bbaaf0fd | 541 | tiflags &= ~(TH_PUSH|TH_FIN); |
87e78f19 | 542 | } |
2ff61f9d | 543 | } else { |
2ff61f9d BJ |
544 | /* |
545 | * If segment ends after window, drop trailing data | |
8a13b737 | 546 | * (and PUSH and FIN); if nothing left, just ACK. |
2ff61f9d | 547 | */ |
fd5dc5f0 BJ |
548 | todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); |
549 | if (todrop > 0) { | |
96c50630 MK |
550 | if (todrop >= ti->ti_len) { |
551 | /* | |
552 | * If a new connection request is received | |
553 | * while in TIME_WAIT, drop the old connection | |
554 | * and start over if the sequence numbers | |
555 | * are above the previous ones. | |
556 | */ | |
557 | if (tiflags & TH_SYN && | |
558 | tp->t_state == TCPS_TIME_WAIT && | |
559 | SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { | |
560 | iss = tp->rcv_nxt + TCP_ISSINCR; | |
561 | (void) tcp_close(tp); | |
562 | goto findpcb; | |
563 | } | |
564 | if (todrop == 1) | |
565 | tcpstat.tcps_rcvwinprobe++; | |
566 | else { | |
567 | tcpstat.tcps_rcvpackafterwin++; | |
568 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
569 | } | |
2ff61f9d | 570 | goto dropafterack; |
96c50630 MK |
571 | } |
572 | tcpstat.tcps_rcvpackafterwin++; | |
573 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
2ff61f9d BJ |
574 | m_adj(m, -todrop); |
575 | ti->ti_len -= todrop; | |
bbaaf0fd | 576 | tiflags &= ~(TH_PUSH|TH_FIN); |
87e78f19 | 577 | } |
87e78f19 | 578 | } |
87e78f19 | 579 | |
87e78f19 | 580 | /* |
2ff61f9d BJ |
581 | * If the RST bit is set examine the state: |
582 | * SYN_RECEIVED STATE: | |
583 | * If passive open, return to LISTEN state. | |
584 | * If active open, inform user that connection was refused. | |
585 | * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: | |
586 | * Inform user that connection was reset, and close tcb. | |
587 | * CLOSING, LAST_ACK, TIME_WAIT STATES | |
588 | * Close the tcb. | |
87e78f19 | 589 | */ |
2ff61f9d | 590 | if (tiflags&TH_RST) switch (tp->t_state) { |
4b935108 | 591 | |
2ff61f9d | 592 | case TCPS_SYN_RECEIVED: |
0e3936fa | 593 | tp = tcp_drop(tp, ECONNREFUSED); |
2ff61f9d BJ |
594 | goto drop; |
595 | ||
596 | case TCPS_ESTABLISHED: | |
597 | case TCPS_FIN_WAIT_1: | |
598 | case TCPS_FIN_WAIT_2: | |
599 | case TCPS_CLOSE_WAIT: | |
0e3936fa | 600 | tp = tcp_drop(tp, ECONNRESET); |
2ff61f9d BJ |
601 | goto drop; |
602 | ||
603 | case TCPS_CLOSING: | |
604 | case TCPS_LAST_ACK: | |
605 | case TCPS_TIME_WAIT: | |
0e3936fa | 606 | tp = tcp_close(tp); |
2ff61f9d | 607 | goto drop; |
87e78f19 | 608 | } |
87e78f19 BJ |
609 | |
610 | /* | |
2ff61f9d BJ |
611 | * If a SYN is in the window, then this is an |
612 | * error and we send an RST and drop the connection. | |
613 | */ | |
614 | if (tiflags & TH_SYN) { | |
0e3936fa | 615 | tp = tcp_drop(tp, ECONNRESET); |
8a13b737 | 616 | goto dropwithreset; |
2ff61f9d BJ |
617 | } |
618 | ||
619 | /* | |
620 | * If the ACK bit is off we drop the segment and return. | |
621 | */ | |
8a13b737 | 622 | if ((tiflags & TH_ACK) == 0) |
2ff61f9d BJ |
623 | goto drop; |
624 | ||
625 | /* | |
626 | * Ack processing. | |
87e78f19 | 627 | */ |
87e78f19 BJ |
628 | switch (tp->t_state) { |
629 | ||
2ff61f9d BJ |
630 | /* |
631 | * In SYN_RECEIVED state if the ack ACKs our SYN then enter | |
632 | * ESTABLISHED state and continue processing, othewise | |
633 | * send an RST. | |
634 | */ | |
635 | case TCPS_SYN_RECEIVED: | |
8a13b737 | 636 | if (SEQ_GT(tp->snd_una, ti->ti_ack) || |
4b6b94ca | 637 | SEQ_GT(ti->ti_ack, tp->snd_max)) |
8a13b737 | 638 | goto dropwithreset; |
4aed14e3 | 639 | tp->snd_una++; /* SYN acked */ |
b8977237 BJ |
640 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
641 | tp->snd_nxt = tp->snd_una; | |
4aed14e3 | 642 | tp->t_timer[TCPT_REXMT] = 0; |
96c50630 | 643 | tcpstat.tcps_connects++; |
8a13b737 BJ |
644 | soisconnected(so); |
645 | tp->t_state = TCPS_ESTABLISHED; | |
99578149 | 646 | tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); |
405c9168 | 647 | (void) tcp_reass(tp, (struct tcpiphdr *)0); |
4aed14e3 | 648 | tp->snd_wl1 = ti->ti_seq - 1; |
8a13b737 | 649 | /* fall into ... */ |
87e78f19 | 650 | |
2ff61f9d BJ |
651 | /* |
652 | * In ESTABLISHED state: drop duplicate ACKs; ACK out of range | |
653 | * ACKs. If the ack is in the range | |
4b6b94ca | 654 | * tp->snd_una < ti->ti_ack <= tp->snd_max |
2ff61f9d BJ |
655 | * then advance tp->snd_una to ti->ti_ack and drop |
656 | * data from the retransmission queue. If this ACK reflects | |
657 | * more up to date window information we update our window information. | |
658 | */ | |
659 | case TCPS_ESTABLISHED: | |
660 | case TCPS_FIN_WAIT_1: | |
661 | case TCPS_FIN_WAIT_2: | |
662 | case TCPS_CLOSE_WAIT: | |
663 | case TCPS_CLOSING: | |
4aed14e3 BJ |
664 | case TCPS_LAST_ACK: |
665 | case TCPS_TIME_WAIT: | |
8a13b737 | 666 | |
96c50630 MK |
667 | if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { |
668 | if (ti->ti_len == 0) | |
669 | tcpstat.tcps_rcvdupack++; | |
2ff61f9d | 670 | break; |
96c50630 MK |
671 | } |
672 | if (SEQ_GT(ti->ti_ack, tp->snd_max)) { | |
673 | tcpstat.tcps_rcvacktoomuch++; | |
2ff61f9d | 674 | goto dropafterack; |
96c50630 | 675 | } |
8a13b737 | 676 | acked = ti->ti_ack - tp->snd_una; |
96c50630 MK |
677 | tcpstat.tcps_rcvackpack++; |
678 | tcpstat.tcps_rcvackbyte += acked; | |
dd020fc8 BJ |
679 | |
680 | /* | |
681 | * If transmit timer is running and timed sequence | |
682 | * number was acked, update smoothed round trip time. | |
683 | */ | |
684 | if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { | |
96c50630 | 685 | tcpstat.tcps_rttupdated++; |
dd020fc8 BJ |
686 | if (tp->t_srtt == 0) |
687 | tp->t_srtt = tp->t_rtt; | |
688 | else | |
689 | tp->t_srtt = | |
690 | tcp_alpha * tp->t_srtt + | |
691 | (1 - tcp_alpha) * tp->t_rtt; | |
dd020fc8 BJ |
692 | tp->t_rtt = 0; |
693 | } | |
694 | ||
91039e49 MK |
695 | /* |
696 | * If all outstanding data is acked, stop retransmit | |
697 | * timer and remember to restart (more output or persist). | |
698 | * If there is more data to be acked, restart retransmit | |
699 | * timer. | |
700 | */ | |
701 | if (ti->ti_ack == tp->snd_max) { | |
4aed14e3 | 702 | tp->t_timer[TCPT_REXMT] = 0; |
91039e49 MK |
703 | needoutput = 1; |
704 | } else if (tp->t_timer[TCPT_PERSIST] == 0) { | |
4aed14e3 BJ |
705 | TCPT_RANGESET(tp->t_timer[TCPT_REXMT], |
706 | tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); | |
22856bb8 | 707 | tp->t_rxtshift = 0; |
8a13b737 | 708 | } |
1e9621b8 MK |
709 | /* |
710 | * When new data is acked, open the congestion window a bit. | |
711 | */ | |
96c50630 | 712 | tp->snd_cwnd = MIN(11 * tp->snd_cwnd / 10, 65535); |
6703c41f | 713 | if (acked > so->so_snd.sb_cc) { |
6703c41f | 714 | tp->snd_wnd -= so->so_snd.sb_cc; |
8011f5df | 715 | sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); |
96c50630 | 716 | #define ourfinisacked (acked > 0) |
6703c41f | 717 | } else { |
668cc26d | 718 | sbdrop(&so->so_snd, acked); |
6703c41f BJ |
719 | tp->snd_wnd -= acked; |
720 | acked = 0; | |
721 | } | |
5744ed2b | 722 | if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) |
22856bb8 | 723 | sowwakeup(so); |
4b6b94ca | 724 | tp->snd_una = ti->ti_ack; |
b8977237 BJ |
725 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
726 | tp->snd_nxt = tp->snd_una; | |
405c9168 | 727 | |
87e78f19 BJ |
728 | switch (tp->t_state) { |
729 | ||
2ff61f9d BJ |
730 | /* |
731 | * In FIN_WAIT_1 STATE in addition to the processing | |
732 | * for the ESTABLISHED state if our FIN is now acknowledged | |
8a13b737 | 733 | * then enter FIN_WAIT_2. |
2ff61f9d BJ |
734 | */ |
735 | case TCPS_FIN_WAIT_1: | |
fdae4427 BJ |
736 | if (ourfinisacked) { |
737 | /* | |
738 | * If we can't receive any more | |
739 | * data, then closing user can proceed. | |
a17510f3 MK |
740 | * Starting the timer is contrary to the |
741 | * specification, but if we don't get a FIN | |
742 | * we'll hang forever. | |
fdae4427 | 743 | */ |
a17510f3 | 744 | if (so->so_state & SS_CANTRCVMORE) { |
fdae4427 | 745 | soisdisconnected(so); |
a17510f3 MK |
746 | tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; |
747 | } | |
8a13b737 | 748 | tp->t_state = TCPS_FIN_WAIT_2; |
fdae4427 | 749 | } |
87e78f19 BJ |
750 | break; |
751 | ||
2ff61f9d BJ |
752 | /* |
753 | * In CLOSING STATE in addition to the processing for | |
754 | * the ESTABLISHED state if the ACK acknowledges our FIN | |
755 | * then enter the TIME-WAIT state, otherwise ignore | |
756 | * the segment. | |
757 | */ | |
758 | case TCPS_CLOSING: | |
4aed14e3 | 759 | if (ourfinisacked) { |
2ff61f9d | 760 | tp->t_state = TCPS_TIME_WAIT; |
4aed14e3 BJ |
761 | tcp_canceltimers(tp); |
762 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; | |
763 | soisdisconnected(so); | |
764 | } | |
765 | break; | |
87e78f19 | 766 | |
2ff61f9d | 767 | /* |
8a13b737 BJ |
768 | * The only thing that can arrive in LAST_ACK state |
769 | * is an acknowledgment of our FIN. If our FIN is now | |
770 | * acknowledged, delete the TCB, enter the closed state | |
771 | * and return. | |
2ff61f9d BJ |
772 | */ |
773 | case TCPS_LAST_ACK: | |
0e3936fa SL |
774 | if (ourfinisacked) |
775 | tp = tcp_close(tp); | |
2ff61f9d | 776 | goto drop; |
87e78f19 | 777 | |
2ff61f9d BJ |
778 | /* |
779 | * In TIME_WAIT state the only thing that should arrive | |
780 | * is a retransmission of the remote FIN. Acknowledge | |
781 | * it and restart the finack timer. | |
782 | */ | |
783 | case TCPS_TIME_WAIT: | |
405c9168 | 784 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
2ff61f9d | 785 | goto dropafterack; |
87e78f19 | 786 | } |
8a13b737 BJ |
787 | #undef ourfinisacked |
788 | } | |
87e78f19 | 789 | |
2ff61f9d | 790 | step6: |
4aed14e3 BJ |
791 | /* |
792 | * Update window information. | |
bbaaf0fd | 793 | * Don't look at window if no ACK: TAC's send garbage on first SYN. |
4aed14e3 | 794 | */ |
bbaaf0fd MK |
795 | if ((tiflags & TH_ACK) && |
796 | (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && | |
8e65fd66 | 797 | (SEQ_LT(tp->snd_wl2, ti->ti_ack) || |
bbaaf0fd | 798 | tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { |
96c50630 MK |
799 | /* keep track of pure window updates */ |
800 | if (ti->ti_len == 0 && | |
801 | tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) { | |
802 | tcpstat.tcps_rcvwinupd++; | |
803 | tcpstat.tcps_rcvdupack--; | |
804 | } | |
4aed14e3 BJ |
805 | tp->snd_wnd = ti->ti_win; |
806 | tp->snd_wl1 = ti->ti_seq; | |
807 | tp->snd_wl2 = ti->ti_ack; | |
18a438b6 MK |
808 | if (tp->snd_wnd > tp->max_sndwnd) |
809 | tp->max_sndwnd = tp->snd_wnd; | |
91039e49 MK |
810 | needoutput = 1; |
811 | } | |
4aed14e3 | 812 | |
2ff61f9d | 813 | /* |
b2db9217 | 814 | * Process segments with URG. |
2ff61f9d | 815 | */ |
9c811062 BJ |
816 | if ((tiflags & TH_URG) && ti->ti_urp && |
817 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
f4be5024 | 818 | /* |
bbaaf0fd | 819 | * This is a kludge, but if we receive and accept |
a5d9c993 SL |
820 | * random urgent pointers, we'll crash in |
821 | * soreceive. It's hard to imagine someone | |
822 | * actually wanting to send this much urgent data. | |
f4be5024 | 823 | */ |
2f4f574f | 824 | if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { |
f4be5024 SL |
825 | ti->ti_urp = 0; /* XXX */ |
826 | tiflags &= ~TH_URG; /* XXX */ | |
bbaaf0fd | 827 | goto dodata; /* XXX */ |
f4be5024 | 828 | } |
b2db9217 BJ |
829 | /* |
830 | * If this segment advances the known urgent pointer, | |
831 | * then mark the data stream. This should not happen | |
832 | * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since | |
833 | * a FIN has been received from the remote side. | |
834 | * In these states we ignore the URG. | |
ae6760c5 MK |
835 | * |
836 | * According to RFC961 (Assigned Protocols), | |
837 | * the urgent pointer points to the last octet | |
838 | * of urgent data. We continue, however, | |
839 | * to consider it to indicate the first octet | |
840 | * of data past the urgent section | |
841 | * as the original spec states. | |
b2db9217 BJ |
842 | */ |
843 | if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { | |
844 | tp->rcv_up = ti->ti_seq + ti->ti_urp; | |
845 | so->so_oobmark = so->so_rcv.sb_cc + | |
846 | (tp->rcv_up - tp->rcv_nxt) - 1; | |
847 | if (so->so_oobmark == 0) | |
848 | so->so_state |= SS_RCVATMARK; | |
77a4e3ca | 849 | sohasoutofband(so); |
a17510f3 | 850 | tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); |
b2db9217 BJ |
851 | } |
852 | /* | |
853 | * Remove out of band data so doesn't get presented to user. | |
854 | * This can happen independent of advancing the URG pointer, | |
855 | * but if two URG's are pending at once, some out-of-band | |
856 | * data may creep in... ick. | |
857 | */ | |
ae6760c5 MK |
858 | if (ti->ti_urp <= ti->ti_len && |
859 | (so->so_options & SO_OOBINLINE) == 0) | |
b2db9217 | 860 | tcp_pulloutofband(so, ti); |
bbaaf0fd MK |
861 | } else |
862 | /* | |
863 | * If no out of band data is expected, | |
864 | * pull receive urgent pointer along | |
865 | * with the receive window. | |
866 | */ | |
867 | if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) | |
868 | tp->rcv_up = tp->rcv_nxt; | |
869 | dodata: /* XXX */ | |
87e78f19 BJ |
870 | |
871 | /* | |
2ff61f9d BJ |
872 | * Process the segment text, merging it into the TCP sequencing queue, |
873 | * and arranging for acknowledgment of receipt if necessary. | |
874 | * This process logically involves adjusting tp->rcv_wnd as data | |
875 | * is presented to the user (this happens in tcp_usrreq.c, | |
876 | * case PRU_RCVD). If a FIN has already been received on this | |
877 | * connection then we just ignore the text. | |
87e78f19 | 878 | */ |
7984a662 MK |
879 | if ((ti->ti_len || (tiflags&TH_FIN)) && |
880 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
a17510f3 | 881 | TCP_REASS(tp, ti, m, so, tiflags); |
8b5a83bb BJ |
882 | if (tcpnodelack == 0) |
883 | tp->t_flags |= TF_DELACK; | |
884 | else | |
885 | tp->t_flags |= TF_ACKNOW; | |
18a438b6 MK |
886 | /* |
887 | * Note the amount of data that peer has sent into | |
888 | * our window, in order to estimate the sender's | |
889 | * buffer size. | |
890 | */ | |
891 | len = so->so_rcv.sb_hiwat - (tp->rcv_nxt - tp->rcv_adv); | |
892 | if (len > tp->max_rcvd) | |
893 | tp->max_rcvd = len; | |
4aed14e3 | 894 | } else { |
2b4b57cd | 895 | m_freem(m); |
e832edbc | 896 | tiflags &= ~TH_FIN; |
4aed14e3 | 897 | } |
87e78f19 BJ |
898 | |
899 | /* | |
e832edbc BJ |
900 | * If FIN is received ACK the FIN and let the user know |
901 | * that the connection is closing. | |
87e78f19 | 902 | */ |
e832edbc | 903 | if (tiflags & TH_FIN) { |
4aed14e3 BJ |
904 | if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { |
905 | socantrcvmore(so); | |
906 | tp->t_flags |= TF_ACKNOW; | |
907 | tp->rcv_nxt++; | |
908 | } | |
2ff61f9d | 909 | switch (tp->t_state) { |
87e78f19 | 910 | |
2ff61f9d BJ |
911 | /* |
912 | * In SYN_RECEIVED and ESTABLISHED STATES | |
913 | * enter the CLOSE_WAIT state. | |
53a5409e | 914 | */ |
2ff61f9d BJ |
915 | case TCPS_SYN_RECEIVED: |
916 | case TCPS_ESTABLISHED: | |
917 | tp->t_state = TCPS_CLOSE_WAIT; | |
918 | break; | |
53a5409e | 919 | |
2ff61f9d | 920 | /* |
8a13b737 BJ |
921 | * If still in FIN_WAIT_1 STATE FIN has not been acked so |
922 | * enter the CLOSING state. | |
53a5409e | 923 | */ |
2ff61f9d | 924 | case TCPS_FIN_WAIT_1: |
8a13b737 | 925 | tp->t_state = TCPS_CLOSING; |
2ff61f9d | 926 | break; |
87e78f19 | 927 | |
2ff61f9d BJ |
928 | /* |
929 | * In FIN_WAIT_2 state enter the TIME_WAIT state, | |
930 | * starting the time-wait timer, turning off the other | |
931 | * standard timers. | |
932 | */ | |
933 | case TCPS_FIN_WAIT_2: | |
4aed14e3 | 934 | tp->t_state = TCPS_TIME_WAIT; |
a6503abf | 935 | tcp_canceltimers(tp); |
405c9168 | 936 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
4aed14e3 | 937 | soisdisconnected(so); |
2ff61f9d BJ |
938 | break; |
939 | ||
53a5409e | 940 | /* |
2ff61f9d | 941 | * In TIME_WAIT state restart the 2 MSL time_wait timer. |
53a5409e | 942 | */ |
2ff61f9d | 943 | case TCPS_TIME_WAIT: |
405c9168 | 944 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
2ff61f9d | 945 | break; |
8a13b737 | 946 | } |
87e78f19 | 947 | } |
4b935108 BJ |
948 | if (so->so_options & SO_DEBUG) |
949 | tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); | |
8a13b737 BJ |
950 | |
951 | /* | |
952 | * Return any desired output. | |
953 | */ | |
91039e49 | 954 | if (needoutput || (tp->t_flags & TF_ACKNOW)) |
bbaaf0fd | 955 | (void) tcp_output(tp); |
2ff61f9d | 956 | return; |
8a13b737 | 957 | |
2ff61f9d | 958 | dropafterack: |
8a13b737 | 959 | /* |
1e977657 BJ |
960 | * Generate an ACK dropping incoming segment if it occupies |
961 | * sequence space, where the ACK reflects our state. | |
8a13b737 | 962 | */ |
ad616704 | 963 | if (tiflags & TH_RST) |
8a13b737 | 964 | goto drop; |
f3cdd721 BJ |
965 | if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) |
966 | tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); | |
8e65fd66 | 967 | tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); |
4b6b94ca | 968 | return; |
8a13b737 BJ |
969 | |
970 | dropwithreset: | |
f37c1c84 | 971 | if (om) { |
668cc26d | 972 | (void) m_free(om); |
f37c1c84 SL |
973 | om = 0; |
974 | } | |
8a13b737 | 975 | /* |
4aed14e3 | 976 | * Generate a RST, dropping incoming segment. |
8a13b737 | 977 | * Make ACK acceptable to originator of segment. |
224f3a72 | 978 | * Don't bother to respond if destination was broadcast. |
8a13b737 | 979 | */ |
224f3a72 | 980 | if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) |
8a13b737 BJ |
981 | goto drop; |
982 | if (tiflags & TH_ACK) | |
8e65fd66 | 983 | tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); |
8a13b737 BJ |
984 | else { |
985 | if (tiflags & TH_SYN) | |
986 | ti->ti_len++; | |
1e977657 BJ |
987 | tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, |
988 | TH_RST|TH_ACK); | |
8a13b737 | 989 | } |
7aa16f99 SL |
990 | /* destroy temporarily created socket */ |
991 | if (dropsocket) | |
992 | (void) soabort(so); | |
4b6b94ca | 993 | return; |
8a13b737 | 994 | |
2ff61f9d | 995 | drop: |
01b1394e SL |
996 | if (om) |
997 | (void) m_free(om); | |
8a13b737 BJ |
998 | /* |
999 | * Drop space held by incoming segment and return. | |
1000 | */ | |
f3cdd721 BJ |
1001 | if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) |
1002 | tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); | |
2ff61f9d | 1003 | m_freem(m); |
7aa16f99 SL |
1004 | /* destroy temporarily created socket */ |
1005 | if (dropsocket) | |
1006 | (void) soabort(so); | |
4b935108 | 1007 | return; |
2ff61f9d BJ |
1008 | } |
1009 | ||
99578149 | 1010 | tcp_dooptions(tp, om, ti) |
8b5a83bb BJ |
1011 | struct tcpcb *tp; |
1012 | struct mbuf *om; | |
99578149 | 1013 | struct tcpiphdr *ti; |
5e74df82 | 1014 | { |
8b5a83bb BJ |
1015 | register u_char *cp; |
1016 | int opt, optlen, cnt; | |
1017 | ||
1018 | cp = mtod(om, u_char *); | |
1019 | cnt = om->m_len; | |
1020 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
1021 | opt = cp[0]; | |
1022 | if (opt == TCPOPT_EOL) | |
1023 | break; | |
1024 | if (opt == TCPOPT_NOP) | |
1025 | optlen = 1; | |
357b20fc | 1026 | else { |
8b5a83bb | 1027 | optlen = cp[1]; |
357b20fc SL |
1028 | if (optlen <= 0) |
1029 | break; | |
1030 | } | |
8b5a83bb BJ |
1031 | switch (opt) { |
1032 | ||
1033 | default: | |
1034 | break; | |
1035 | ||
1036 | case TCPOPT_MAXSEG: | |
1037 | if (optlen != 4) | |
1038 | continue; | |
99578149 MK |
1039 | if (!(ti->ti_flags & TH_SYN)) |
1040 | continue; | |
8b5a83bb | 1041 | tp->t_maxseg = *(u_short *)(cp + 2); |
668cc26d | 1042 | tp->t_maxseg = ntohs((u_short)tp->t_maxseg); |
99578149 | 1043 | tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); |
8b5a83bb | 1044 | break; |
8b5a83bb | 1045 | } |
5e74df82 | 1046 | } |
668cc26d | 1047 | (void) m_free(om); |
5e74df82 BJ |
1048 | } |
1049 | ||
b2db9217 BJ |
1050 | /* |
1051 | * Pull out of band byte out of a segment so | |
1052 | * it doesn't appear in the user's data queue. | |
1053 | * It is still reflected in the segment length for | |
1054 | * sequencing purposes. | |
1055 | */ | |
1056 | tcp_pulloutofband(so, ti) | |
1057 | struct socket *so; | |
1058 | struct tcpiphdr *ti; | |
1059 | { | |
1060 | register struct mbuf *m; | |
1acff8ec | 1061 | int cnt = ti->ti_urp - 1; |
b2db9217 BJ |
1062 | |
1063 | m = dtom(ti); | |
1064 | while (cnt >= 0) { | |
1065 | if (m->m_len > cnt) { | |
1066 | char *cp = mtod(m, caddr_t) + cnt; | |
1067 | struct tcpcb *tp = sototcpcb(so); | |
1068 | ||
1069 | tp->t_iobc = *cp; | |
1070 | tp->t_oobflags |= TCPOOB_HAVEDATA; | |
668cc26d | 1071 | bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); |
b2db9217 BJ |
1072 | m->m_len--; |
1073 | return; | |
1074 | } | |
1075 | cnt -= m->m_len; | |
1076 | m = m->m_next; | |
1077 | if (m == 0) | |
1078 | break; | |
1079 | } | |
1080 | panic("tcp_pulloutofband"); | |
1081 | } | |
1082 | ||
99578149 MK |
1083 | /* |
1084 | * Determine a reasonable value for maxseg size. | |
1085 | * If the route is known, use one that can be handled | |
1086 | * on the given interface without forcing IP to fragment. | |
c2a1cd2c | 1087 | * If bigger than a page (CLBYTES), round down to nearest pagesize |
99578149 MK |
1088 | * to utilize pagesize mbufs. |
1089 | * If interface pointer is unavailable, or the destination isn't local, | |
c2a1cd2c MK |
1090 | * use a conservative size (512 or the default IP max size, but no more |
1091 | * than the mtu of the interface through which we route), | |
99578149 MK |
1092 | * as we can't discover anything about intervening gateways or networks. |
1093 | * | |
1094 | * This is ugly, and doesn't belong at this level, but has to happen somehow. | |
1095 | */ | |
1096 | tcp_mss(tp) | |
c2a1cd2c | 1097 | register struct tcpcb *tp; |
99578149 MK |
1098 | { |
1099 | struct route *ro; | |
1100 | struct ifnet *ifp; | |
1101 | int mss; | |
1102 | struct inpcb *inp; | |
1103 | ||
1104 | inp = tp->t_inpcb; | |
1105 | ro = &inp->inp_route; | |
1106 | if ((ro->ro_rt == (struct rtentry *)0) || | |
1107 | (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { | |
1108 | /* No route yet, so try to acquire one */ | |
1109 | if (inp->inp_faddr.s_addr != INADDR_ANY) { | |
1110 | ro->ro_dst.sa_family = AF_INET; | |
1111 | ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = | |
1112 | inp->inp_faddr; | |
1113 | rtalloc(ro); | |
1114 | } | |
1115 | if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) | |
0d115dcc | 1116 | return (TCP_MSS); |
99578149 MK |
1117 | } |
1118 | ||
1119 | mss = ifp->if_mtu - sizeof(struct tcpiphdr); | |
1120 | #if (CLBYTES & (CLBYTES - 1)) == 0 | |
1121 | if (mss > CLBYTES) | |
1122 | mss &= ~(CLBYTES-1); | |
1123 | #else | |
1124 | if (mss > CLBYTES) | |
1125 | mss = mss / CLBYTES * CLBYTES; | |
1126 | #endif | |
c2a1cd2c MK |
1127 | if (in_localaddr(inp->inp_faddr)) |
1128 | return (mss); | |
0d115dcc | 1129 | return (MIN(mss, TCP_MSS)); |
99578149 | 1130 | } |