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