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