tcp_subr.c revision 1.93 1 /* $NetBSD: tcp_subr.c,v 1.93 2000/09/19 18:21:41 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
38 * Facility, NASA Ames Research Center.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
71 * The Regents of the University of California. All rights reserved.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software
82 * must display the following acknowledgement:
83 * This product includes software developed by the University of
84 * California, Berkeley and its contributors.
85 * 4. Neither the name of the University nor the names of its contributors
86 * may be used to endorse or promote products derived from this software
87 * without specific prior written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE.
100 *
101 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
102 */
103
104 #include "opt_inet.h"
105 #include "opt_ipsec.h"
106 #include "opt_tcp_compat_42.h"
107 #include "rnd.h"
108
109 #include <sys/param.h>
110 #include <sys/proc.h>
111 #include <sys/systm.h>
112 #include <sys/malloc.h>
113 #include <sys/mbuf.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/protosw.h>
117 #include <sys/errno.h>
118 #include <sys/kernel.h>
119 #include <sys/pool.h>
120 #if NRND > 0
121 #include <sys/rnd.h>
122 #endif
123
124 #include <net/route.h>
125 #include <net/if.h>
126
127 #include <netinet/in.h>
128 #include <netinet/in_systm.h>
129 #include <netinet/ip.h>
130 #include <netinet/in_pcb.h>
131 #include <netinet/ip_var.h>
132 #include <netinet/ip_icmp.h>
133
134 #ifdef INET6
135 #ifndef INET
136 #include <netinet/in.h>
137 #endif
138 #include <netinet/ip6.h>
139 #include <netinet6/in6_pcb.h>
140 #include <netinet6/ip6_var.h>
141 #include <netinet6/in6_var.h>
142 #include <netinet6/ip6protosw.h>
143 #endif
144
145 #include <netinet/tcp.h>
146 #include <netinet/tcp_fsm.h>
147 #include <netinet/tcp_seq.h>
148 #include <netinet/tcp_timer.h>
149 #include <netinet/tcp_var.h>
150 #include <netinet/tcpip.h>
151
152 #ifdef IPSEC
153 #include <netinet6/ipsec.h>
154 #endif /*IPSEC*/
155
156 #ifdef INET6
157 struct in6pcb tcb6;
158 #endif
159
160 /* patchable/settable parameters for tcp */
161 int tcp_mssdflt = TCP_MSS;
162 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
163 int tcp_do_rfc1323 = 1; /* window scaling / timestamps (obsolete) */
164 int tcp_do_sack = 1; /* selective acknowledgement */
165 int tcp_do_win_scale = 1; /* RFC1323 window scaling */
166 int tcp_do_timestamps = 1; /* RFC1323 timestamps */
167 int tcp_do_newreno = 0; /* Use the New Reno algorithms */
168 int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */
169 int tcp_init_win = 1;
170 int tcp_mss_ifmtu = 0;
171 #ifdef TCP_COMPAT_42
172 int tcp_compat_42 = 1;
173 #else
174 int tcp_compat_42 = 0;
175 #endif
176
177 #ifndef TCBHASHSIZE
178 #define TCBHASHSIZE 128
179 #endif
180 int tcbhashsize = TCBHASHSIZE;
181
182 int tcp_freeq __P((struct tcpcb *));
183
184 struct pool tcpcb_pool;
185
186 /*
187 * Tcp initialization
188 */
189 void
190 tcp_init()
191 {
192 int hlen;
193
194 pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl",
195 0, NULL, NULL, M_PCB);
196 in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
197 #ifdef INET6
198 tcb6.in6p_next = tcb6.in6p_prev = &tcb6;
199 #endif
200 LIST_INIT(&tcp_delacks);
201
202 hlen = sizeof(struct ip) + sizeof(struct tcphdr);
203 #ifdef INET6
204 if (sizeof(struct ip) < sizeof(struct ip6_hdr))
205 hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
206 #endif
207 if (max_protohdr < hlen)
208 max_protohdr = hlen;
209 if (max_linkhdr + hlen > MHLEN)
210 panic("tcp_init");
211
212 /* Initialize the compressed state engine. */
213 syn_cache_init();
214 }
215
216 /*
217 * Create template to be used to send tcp packets on a connection.
218 * Call after host entry created, allocates an mbuf and fills
219 * in a skeletal tcp/ip header, minimizing the amount of work
220 * necessary when the connection is used.
221 */
222 struct mbuf *
223 tcp_template(tp)
224 struct tcpcb *tp;
225 {
226 struct inpcb *inp = tp->t_inpcb;
227 #ifdef INET6
228 struct in6pcb *in6p = tp->t_in6pcb;
229 #endif
230 struct tcphdr *n;
231 struct mbuf *m;
232 int hlen;
233
234 switch (tp->t_family) {
235 case AF_INET:
236 hlen = sizeof(struct ip);
237 if (inp)
238 break;
239 #ifdef INET6
240 if (in6p) {
241 /* mapped addr case */
242 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
243 && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
244 break;
245 }
246 #endif
247 return NULL; /*EINVAL*/
248 #ifdef INET6
249 case AF_INET6:
250 hlen = sizeof(struct ip6_hdr);
251 if (in6p) {
252 /* more sainty check? */
253 break;
254 }
255 return NULL; /*EINVAL*/
256 #endif
257 default:
258 hlen = 0; /*pacify gcc*/
259 return NULL; /*EAFNOSUPPORT*/
260 }
261 #ifdef DIAGNOSTIC
262 if (hlen + sizeof(struct tcphdr) > MCLBYTES)
263 panic("mclbytes too small for t_template");
264 #endif
265 m = tp->t_template;
266 if (m && m->m_len == hlen + sizeof(struct tcphdr))
267 ;
268 else {
269 if (m)
270 m_freem(m);
271 m = tp->t_template = NULL;
272 MGETHDR(m, M_DONTWAIT, MT_HEADER);
273 if (m && hlen + sizeof(struct tcphdr) > MHLEN) {
274 MCLGET(m, M_DONTWAIT);
275 if ((m->m_flags & M_EXT) == 0) {
276 m_free(m);
277 m = NULL;
278 }
279 }
280 if (m == NULL)
281 return NULL;
282 m->m_pkthdr.len = m->m_len = hlen + sizeof(struct tcphdr);
283 }
284 bzero(mtod(m, caddr_t), m->m_len);
285 switch (tp->t_family) {
286 case AF_INET:
287 {
288 struct ipovly *ipov;
289 mtod(m, struct ip *)->ip_v = 4;
290 ipov = mtod(m, struct ipovly *);
291 ipov->ih_pr = IPPROTO_TCP;
292 ipov->ih_len = htons(sizeof(struct tcphdr));
293 if (inp) {
294 ipov->ih_src = inp->inp_laddr;
295 ipov->ih_dst = inp->inp_faddr;
296 }
297 #ifdef INET6
298 else if (in6p) {
299 /* mapped addr case */
300 bcopy(&in6p->in6p_laddr.s6_addr32[3], &ipov->ih_src,
301 sizeof(ipov->ih_src));
302 bcopy(&in6p->in6p_faddr.s6_addr32[3], &ipov->ih_dst,
303 sizeof(ipov->ih_dst));
304 }
305 #endif
306 break;
307 }
308 #ifdef INET6
309 case AF_INET6:
310 {
311 struct ip6_hdr *ip6;
312 mtod(m, struct ip *)->ip_v = 6;
313 ip6 = mtod(m, struct ip6_hdr *);
314 ip6->ip6_nxt = IPPROTO_TCP;
315 ip6->ip6_plen = htons(sizeof(struct tcphdr));
316 ip6->ip6_src = in6p->in6p_laddr;
317 ip6->ip6_dst = in6p->in6p_faddr;
318 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
319 if (ip6_auto_flowlabel) {
320 ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK;
321 ip6->ip6_flow |=
322 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
323 }
324 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
325 ip6->ip6_vfc |= IPV6_VERSION;
326 break;
327 }
328 #endif
329 }
330 n = (struct tcphdr *)(mtod(m, caddr_t) + hlen);
331 if (inp) {
332 n->th_sport = inp->inp_lport;
333 n->th_dport = inp->inp_fport;
334 }
335 #ifdef INET6
336 else if (in6p) {
337 n->th_sport = in6p->in6p_lport;
338 n->th_dport = in6p->in6p_fport;
339 }
340 #endif
341 n->th_seq = 0;
342 n->th_ack = 0;
343 n->th_x2 = 0;
344 n->th_off = 5;
345 n->th_flags = 0;
346 n->th_win = 0;
347 n->th_sum = 0;
348 n->th_urp = 0;
349 return (m);
350 }
351
352 /*
353 * Send a single message to the TCP at address specified by
354 * the given TCP/IP header. If m == 0, then we make a copy
355 * of the tcpiphdr at ti and send directly to the addressed host.
356 * This is used to force keep alive messages out using the TCP
357 * template for a connection tp->t_template. If flags are given
358 * then we send a message back to the TCP which originated the
359 * segment ti, and discard the mbuf containing it and any other
360 * attached mbufs.
361 *
362 * In any case the ack and sequence number of the transmitted
363 * segment are as specified by the parameters.
364 */
365 int
366 tcp_respond(tp, template, m, th0, ack, seq, flags)
367 struct tcpcb *tp;
368 struct mbuf *template;
369 struct mbuf *m;
370 struct tcphdr *th0;
371 tcp_seq ack, seq;
372 int flags;
373 {
374 #ifndef INET6
375 struct route iproute;
376 #else
377 struct route_in6 iproute; /* sizeof(route_in6) > sizeof(route) */
378 #endif
379 struct route *ro;
380 struct rtentry *rt;
381 int error, tlen, win = 0;
382 int hlen;
383 struct ip *ip;
384 #ifdef INET6
385 struct ip6_hdr *ip6;
386 #endif
387 int family; /* family on packet, not inpcb/in6pcb! */
388 struct tcphdr *th;
389
390 if (tp != NULL && (flags & TH_RST) == 0) {
391 if (tp->t_inpcb)
392 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
393 #ifdef INET6
394 else if (tp->t_in6pcb)
395 win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv);
396 #endif
397 }
398
399 ip = NULL;
400 #ifdef INET6
401 ip6 = NULL;
402 #endif
403 if (m == 0) {
404 if (!template)
405 return EINVAL;
406
407 /* get family information from template */
408 switch (mtod(template, struct ip *)->ip_v) {
409 case 4:
410 family = AF_INET;
411 hlen = sizeof(struct ip);
412 break;
413 #ifdef INET6
414 case 6:
415 family = AF_INET6;
416 hlen = sizeof(struct ip6_hdr);
417 break;
418 #endif
419 default:
420 return EAFNOSUPPORT;
421 }
422
423 MGETHDR(m, M_DONTWAIT, MT_HEADER);
424 if (m) {
425 MCLGET(m, M_DONTWAIT);
426 if ((m->m_flags & M_EXT) == 0) {
427 m_free(m);
428 m = NULL;
429 }
430 }
431 if (m == NULL)
432 return (ENOBUFS);
433
434 if (tcp_compat_42)
435 tlen = 1;
436 else
437 tlen = 0;
438
439 m->m_data += max_linkhdr;
440 bcopy(mtod(template, caddr_t), mtod(m, caddr_t),
441 template->m_len);
442 switch (family) {
443 case AF_INET:
444 ip = mtod(m, struct ip *);
445 th = (struct tcphdr *)(ip + 1);
446 break;
447 #ifdef INET6
448 case AF_INET6:
449 ip6 = mtod(m, struct ip6_hdr *);
450 th = (struct tcphdr *)(ip6 + 1);
451 break;
452 #endif
453 #if 0
454 default:
455 /* noone will visit here */
456 m_freem(m);
457 return EAFNOSUPPORT;
458 #endif
459 }
460 flags = TH_ACK;
461 } else {
462
463 if ((m->m_flags & M_PKTHDR) == 0) {
464 #if 0
465 printf("non PKTHDR to tcp_respond\n");
466 #endif
467 m_freem(m);
468 return EINVAL;
469 }
470 #ifdef DIAGNOSTIC
471 if (!th0)
472 panic("th0 == NULL in tcp_respond");
473 #endif
474
475 /* get family information from m */
476 switch (mtod(m, struct ip *)->ip_v) {
477 case 4:
478 family = AF_INET;
479 hlen = sizeof(struct ip);
480 ip = mtod(m, struct ip *);
481 break;
482 #ifdef INET6
483 case 6:
484 family = AF_INET6;
485 hlen = sizeof(struct ip6_hdr);
486 ip6 = mtod(m, struct ip6_hdr *);
487 break;
488 #endif
489 default:
490 m_freem(m);
491 return EAFNOSUPPORT;
492 }
493 if ((flags & TH_SYN) == 0 || sizeof(*th0) > (th0->th_off << 2))
494 tlen = sizeof(*th0);
495 else
496 tlen = th0->th_off << 2;
497
498 if (m->m_len > hlen + tlen && (m->m_flags & M_EXT) == 0 &&
499 mtod(m, caddr_t) + hlen == (caddr_t)th0) {
500 m->m_len = hlen + tlen;
501 m_freem(m->m_next);
502 m->m_next = NULL;
503 } else {
504 struct mbuf *n;
505
506 #ifdef DIAGNOSTIC
507 if (max_linkhdr + hlen + tlen > MCLBYTES) {
508 m_freem(m);
509 return EMSGSIZE;
510 }
511 #endif
512 MGETHDR(n, M_DONTWAIT, MT_HEADER);
513 if (n && max_linkhdr + hlen + tlen > MHLEN) {
514 MCLGET(n, M_DONTWAIT);
515 if ((n->m_flags & M_EXT) == 0) {
516 m_freem(n);
517 n = NULL;
518 }
519 }
520 if (!n) {
521 m_freem(m);
522 return ENOBUFS;
523 }
524
525 n->m_data += max_linkhdr;
526 n->m_len = hlen + tlen;
527 m_copyback(n, 0, hlen, mtod(m, caddr_t));
528 m_copyback(n, hlen, tlen, (caddr_t)th0);
529
530 m_freem(m);
531 m = n;
532 n = NULL;
533 }
534
535 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
536 switch (family) {
537 case AF_INET:
538 ip = mtod(m, struct ip *);
539 th = (struct tcphdr *)(ip + 1);
540 ip->ip_p = IPPROTO_TCP;
541 xchg(ip->ip_dst, ip->ip_src, struct in_addr);
542 ip->ip_p = IPPROTO_TCP;
543 break;
544 #ifdef INET6
545 case AF_INET6:
546 ip6 = mtod(m, struct ip6_hdr *);
547 th = (struct tcphdr *)(ip6 + 1);
548 ip6->ip6_nxt = IPPROTO_TCP;
549 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
550 ip6->ip6_nxt = IPPROTO_TCP;
551 break;
552 #endif
553 #if 0
554 default:
555 /* noone will visit here */
556 m_freem(m);
557 return EAFNOSUPPORT;
558 #endif
559 }
560 xchg(th->th_dport, th->th_sport, u_int16_t);
561 #undef xchg
562 tlen = 0; /*be friendly with the following code*/
563 }
564 th->th_seq = htonl(seq);
565 th->th_ack = htonl(ack);
566 th->th_x2 = 0;
567 if ((flags & TH_SYN) == 0) {
568 if (tp)
569 win >>= tp->rcv_scale;
570 if (win > TCP_MAXWIN)
571 win = TCP_MAXWIN;
572 th->th_win = htons((u_int16_t)win);
573 th->th_off = sizeof (struct tcphdr) >> 2;
574 tlen += sizeof(*th);
575 } else
576 tlen += th->th_off << 2;
577 m->m_len = hlen + tlen;
578 m->m_pkthdr.len = hlen + tlen;
579 m->m_pkthdr.rcvif = (struct ifnet *) 0;
580 th->th_flags = flags;
581 th->th_urp = 0;
582
583 switch (family) {
584 case AF_INET:
585 {
586 struct ipovly *ipov = (struct ipovly *)ip;
587 bzero(ipov->ih_x1, sizeof ipov->ih_x1);
588 ipov->ih_len = htons((u_int16_t)tlen);
589
590 th->th_sum = 0;
591 th->th_sum = in_cksum(m, hlen + tlen);
592 ip->ip_len = hlen + tlen; /*will be flipped on output*/
593 ip->ip_ttl = ip_defttl;
594 break;
595 }
596 #ifdef INET6
597 case AF_INET6:
598 {
599 th->th_sum = 0;
600 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
601 tlen);
602 ip6->ip6_plen = ntohs(tlen);
603 if (tp && tp->t_in6pcb) {
604 struct ifnet *oifp;
605 ro = (struct route *)&tp->t_in6pcb->in6p_route;
606 oifp = ro->ro_rt ? ro->ro_rt->rt_ifp : NULL;
607 ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb, oifp);
608 } else
609 ip6->ip6_hlim = ip6_defhlim;
610 ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
611 if (ip6_auto_flowlabel) {
612 ip6->ip6_flow |=
613 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
614 }
615 break;
616 }
617 #endif
618 }
619
620 #ifdef IPSEC
621 ipsec_setsocket(m, NULL);
622 #endif /*IPSEC*/
623
624 /*
625 * If we're doing Path MTU discovery, we need to set DF unless
626 * the route's MTU is locked. If we lack a route, we need to
627 * look it up now.
628 *
629 * ip_output() could do this for us, but it's convenient to just
630 * do it here unconditionally.
631 */
632 if (tp != NULL && tp->t_inpcb != NULL) {
633 ro = &tp->t_inpcb->inp_route;
634 #ifdef IPSEC
635 ipsec_setsocket(m, tp->t_inpcb->inp_socket);
636 #endif
637 #ifdef DIAGNOSTIC
638 if (family != AF_INET)
639 panic("tcp_respond: address family mismatch");
640 if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) {
641 panic("tcp_respond: ip_dst %x != inp_faddr %x",
642 ntohl(ip->ip_dst.s_addr),
643 ntohl(tp->t_inpcb->inp_faddr.s_addr));
644 }
645 #endif
646 }
647 #ifdef INET6
648 else if (tp != NULL && tp->t_in6pcb != NULL) {
649 ro = (struct route *)&tp->t_in6pcb->in6p_route;
650 #ifdef IPSEC
651 ipsec_setsocket(m, tp->t_in6pcb->in6p_socket);
652 #endif
653 #ifdef DIAGNOSTIC
654 if (family == AF_INET) {
655 if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr))
656 panic("tcp_respond: not mapped addr");
657 if (bcmp(&ip->ip_dst,
658 &tp->t_in6pcb->in6p_faddr.s6_addr32[3],
659 sizeof(ip->ip_dst)) != 0) {
660 panic("tcp_respond: ip_dst != in6p_faddr");
661 }
662 } else if (family == AF_INET6) {
663 if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &tp->t_in6pcb->in6p_faddr))
664 panic("tcp_respond: ip6_dst != in6p_faddr");
665 } else
666 panic("tcp_respond: address family mismatch");
667 #endif
668 }
669 #endif
670 else {
671 ro = (struct route *)&iproute;
672 bzero(ro, sizeof(iproute));
673 }
674 if ((rt = ro->ro_rt) == NULL || (rt->rt_flags & RTF_UP) == 0) {
675 if (ro->ro_rt != NULL) {
676 RTFREE(ro->ro_rt);
677 ro->ro_rt = NULL;
678 }
679 switch (family) {
680 case AF_INET:
681 {
682 struct sockaddr_in *dst;
683 dst = satosin(&ro->ro_dst);
684 dst->sin_family = AF_INET;
685 dst->sin_len = sizeof(*dst);
686 dst->sin_addr = ip->ip_dst;
687 break;
688 }
689 #ifdef INET6
690 case AF_INET6:
691 {
692 struct sockaddr_in6 *dst;
693 dst = satosin6(&ro->ro_dst);
694 bzero(dst, sizeof(*dst));
695 dst->sin6_family = AF_INET6;
696 dst->sin6_len = sizeof(*dst);
697 dst->sin6_addr = ip6->ip6_dst;
698 break;
699 }
700 #endif
701 }
702 rtalloc(ro);
703 if ((rt = ro->ro_rt) == NULL) {
704 m_freem(m);
705 switch (family) {
706 case AF_INET:
707 ipstat.ips_noroute++;
708 break;
709 #ifdef INET6
710 case AF_INET6:
711 ip6stat.ip6s_noroute++;
712 break;
713 #endif
714 }
715 return (EHOSTUNREACH);
716 }
717 }
718 switch (family) {
719 case AF_INET:
720 if (ip_mtudisc != 0 && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
721 ip->ip_off |= IP_DF;
722
723 error = ip_output(m, NULL, ro, 0, NULL);
724 break;
725 #ifdef INET6
726 case AF_INET6:
727 error = ip6_output(m, NULL, (struct route_in6 *)ro, 0, NULL,
728 NULL);
729 break;
730 #endif
731 default:
732 error = EAFNOSUPPORT;
733 break;
734 }
735
736 if (ro == (struct route *)&iproute) {
737 RTFREE(ro->ro_rt);
738 ro->ro_rt = NULL;
739 }
740
741 return (error);
742 }
743
744 /*
745 * Create a new TCP control block, making an
746 * empty reassembly queue and hooking it to the argument
747 * protocol control block.
748 */
749 struct tcpcb *
750 tcp_newtcpcb(family, aux)
751 int family; /* selects inpcb, or in6pcb */
752 void *aux;
753 {
754 struct tcpcb *tp;
755
756 switch (family) {
757 case PF_INET:
758 break;
759 #ifdef INET6
760 case PF_INET6:
761 break;
762 #endif
763 default:
764 return NULL;
765 }
766
767 tp = pool_get(&tcpcb_pool, PR_NOWAIT);
768 if (tp == NULL)
769 return (NULL);
770 bzero((caddr_t)tp, sizeof(struct tcpcb));
771 LIST_INIT(&tp->segq);
772 LIST_INIT(&tp->timeq);
773 tp->t_family = family; /* may be overridden later on */
774 tp->t_peermss = tcp_mssdflt;
775 tp->t_ourmss = tcp_mssdflt;
776 tp->t_segsz = tcp_mssdflt;
777 LIST_INIT(&tp->t_sc);
778
779 tp->t_flags = 0;
780 if (tcp_do_rfc1323 && tcp_do_win_scale)
781 tp->t_flags |= TF_REQ_SCALE;
782 if (tcp_do_rfc1323 && tcp_do_timestamps)
783 tp->t_flags |= TF_REQ_TSTMP;
784 if (tcp_do_sack == 2)
785 tp->t_flags |= TF_WILL_SACK;
786 else if (tcp_do_sack == 1)
787 tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK;
788 tp->t_flags |= TF_CANT_TXSACK;
789 switch (family) {
790 case PF_INET:
791 tp->t_inpcb = (struct inpcb *)aux;
792 break;
793 #ifdef INET6
794 case PF_INET6:
795 tp->t_in6pcb = (struct in6pcb *)aux;
796 break;
797 #endif
798 }
799 /*
800 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
801 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
802 * reasonable initial retransmit time.
803 */
804 tp->t_srtt = TCPTV_SRTTBASE;
805 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
806 tp->t_rttmin = TCPTV_MIN;
807 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
808 TCPTV_MIN, TCPTV_REXMTMAX);
809 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
810 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
811 if (family == AF_INET) {
812 struct inpcb *inp = (struct inpcb *)aux;
813 inp->inp_ip.ip_ttl = ip_defttl;
814 inp->inp_ppcb = (caddr_t)tp;
815 }
816 #ifdef INET6
817 else if (family == AF_INET6) {
818 struct in6pcb *in6p = (struct in6pcb *)aux;
819 in6p->in6p_ip6.ip6_hlim = in6_selecthlim(in6p,
820 in6p->in6p_route.ro_rt ? in6p->in6p_route.ro_rt->rt_ifp
821 : NULL);
822 in6p->in6p_ppcb = (caddr_t)tp;
823 }
824 #endif
825 return (tp);
826 }
827
828 /*
829 * Drop a TCP connection, reporting
830 * the specified error. If connection is synchronized,
831 * then send a RST to peer.
832 */
833 struct tcpcb *
834 tcp_drop(tp, errno)
835 struct tcpcb *tp;
836 int errno;
837 {
838 struct socket *so;
839
840 if (tp->t_inpcb)
841 so = tp->t_inpcb->inp_socket;
842 #ifdef INET6
843 else if (tp->t_in6pcb)
844 so = tp->t_in6pcb->in6p_socket;
845 #endif
846 else
847 return NULL;
848
849 if (TCPS_HAVERCVDSYN(tp->t_state)) {
850 tp->t_state = TCPS_CLOSED;
851 (void) tcp_output(tp);
852 tcpstat.tcps_drops++;
853 } else
854 tcpstat.tcps_conndrops++;
855 if (errno == ETIMEDOUT && tp->t_softerror)
856 errno = tp->t_softerror;
857 so->so_error = errno;
858 return (tcp_close(tp));
859 }
860
861 /*
862 * Close a TCP control block:
863 * discard all space held by the tcp
864 * discard internet protocol block
865 * wake up any sleepers
866 */
867 struct tcpcb *
868 tcp_close(tp)
869 struct tcpcb *tp;
870 {
871 struct inpcb *inp;
872 #ifdef INET6
873 struct in6pcb *in6p;
874 #endif
875 struct socket *so;
876 #ifdef RTV_RTT
877 struct rtentry *rt;
878 #endif
879 struct route *ro;
880
881 inp = tp->t_inpcb;
882 #ifdef INET6
883 in6p = tp->t_in6pcb;
884 #endif
885 so = NULL;
886 ro = NULL;
887 if (inp) {
888 so = inp->inp_socket;
889 ro = &inp->inp_route;
890 }
891 #ifdef INET6
892 else if (in6p) {
893 so = in6p->in6p_socket;
894 ro = (struct route *)&in6p->in6p_route;
895 }
896 #endif
897
898 #ifdef RTV_RTT
899 /*
900 * If we sent enough data to get some meaningful characteristics,
901 * save them in the routing entry. 'Enough' is arbitrarily
902 * defined as the sendpipesize (default 4K) * 16. This would
903 * give us 16 rtt samples assuming we only get one sample per
904 * window (the usual case on a long haul net). 16 samples is
905 * enough for the srtt filter to converge to within 5% of the correct
906 * value; fewer samples and we could save a very bogus rtt.
907 *
908 * Don't update the default route's characteristics and don't
909 * update anything that the user "locked".
910 */
911 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
912 ro && (rt = ro->ro_rt) &&
913 !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
914 u_long i = 0;
915
916 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
917 i = tp->t_srtt *
918 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
919 if (rt->rt_rmx.rmx_rtt && i)
920 /*
921 * filter this update to half the old & half
922 * the new values, converting scale.
923 * See route.h and tcp_var.h for a
924 * description of the scaling constants.
925 */
926 rt->rt_rmx.rmx_rtt =
927 (rt->rt_rmx.rmx_rtt + i) / 2;
928 else
929 rt->rt_rmx.rmx_rtt = i;
930 }
931 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
932 i = tp->t_rttvar *
933 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
934 if (rt->rt_rmx.rmx_rttvar && i)
935 rt->rt_rmx.rmx_rttvar =
936 (rt->rt_rmx.rmx_rttvar + i) / 2;
937 else
938 rt->rt_rmx.rmx_rttvar = i;
939 }
940 /*
941 * update the pipelimit (ssthresh) if it has been updated
942 * already or if a pipesize was specified & the threshhold
943 * got below half the pipesize. I.e., wait for bad news
944 * before we start updating, then update on both good
945 * and bad news.
946 */
947 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
948 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
949 i < (rt->rt_rmx.rmx_sendpipe / 2)) {
950 /*
951 * convert the limit from user data bytes to
952 * packets then to packet data bytes.
953 */
954 i = (i + tp->t_segsz / 2) / tp->t_segsz;
955 if (i < 2)
956 i = 2;
957 i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
958 if (rt->rt_rmx.rmx_ssthresh)
959 rt->rt_rmx.rmx_ssthresh =
960 (rt->rt_rmx.rmx_ssthresh + i) / 2;
961 else
962 rt->rt_rmx.rmx_ssthresh = i;
963 }
964 }
965 #endif /* RTV_RTT */
966 /* free the reassembly queue, if any */
967 TCP_REASS_LOCK(tp);
968 (void) tcp_freeq(tp);
969 TCP_REASS_UNLOCK(tp);
970
971 TCP_CLEAR_DELACK(tp);
972 syn_cache_cleanup(tp);
973
974 if (tp->t_template) {
975 m_free(tp->t_template);
976 tp->t_template = NULL;
977 }
978 pool_put(&tcpcb_pool, tp);
979 if (inp) {
980 inp->inp_ppcb = 0;
981 soisdisconnected(so);
982 in_pcbdetach(inp);
983 }
984 #ifdef INET6
985 else if (in6p) {
986 in6p->in6p_ppcb = 0;
987 soisdisconnected(so);
988 in6_pcbdetach(in6p);
989 }
990 #endif
991 tcpstat.tcps_closed++;
992 return ((struct tcpcb *)0);
993 }
994
995 int
996 tcp_freeq(tp)
997 struct tcpcb *tp;
998 {
999 struct ipqent *qe;
1000 int rv = 0;
1001 #ifdef TCPREASS_DEBUG
1002 int i = 0;
1003 #endif
1004
1005 TCP_REASS_LOCK_CHECK(tp);
1006
1007 while ((qe = tp->segq.lh_first) != NULL) {
1008 #ifdef TCPREASS_DEBUG
1009 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
1010 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
1011 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
1012 #endif
1013 LIST_REMOVE(qe, ipqe_q);
1014 LIST_REMOVE(qe, ipqe_timeq);
1015 m_freem(qe->ipqe_m);
1016 pool_put(&ipqent_pool, qe);
1017 rv = 1;
1018 }
1019 return (rv);
1020 }
1021
1022 /*
1023 * Protocol drain routine. Called when memory is in short supply.
1024 */
1025 void
1026 tcp_drain()
1027 {
1028 struct inpcb *inp;
1029 struct tcpcb *tp;
1030
1031 /*
1032 * Free the sequence queue of all TCP connections.
1033 */
1034 inp = tcbtable.inpt_queue.cqh_first;
1035 if (inp) /* XXX */
1036 for (; inp != (struct inpcb *)&tcbtable.inpt_queue;
1037 inp = inp->inp_queue.cqe_next) {
1038 if ((tp = intotcpcb(inp)) != NULL) {
1039 /*
1040 * We may be called from a device's interrupt
1041 * context. If the tcpcb is already busy,
1042 * just bail out now.
1043 */
1044 if (tcp_reass_lock_try(tp) == 0)
1045 continue;
1046 if (tcp_freeq(tp))
1047 tcpstat.tcps_connsdrained++;
1048 TCP_REASS_UNLOCK(tp);
1049 }
1050 }
1051 }
1052
1053 /*
1054 * Notify a tcp user of an asynchronous error;
1055 * store error as soft error, but wake up user
1056 * (for now, won't do anything until can select for soft error).
1057 */
1058 void
1059 tcp_notify(inp, error)
1060 struct inpcb *inp;
1061 int error;
1062 {
1063 struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
1064 struct socket *so = inp->inp_socket;
1065
1066 /*
1067 * Ignore some errors if we are hooked up.
1068 * If connection hasn't completed, has retransmitted several times,
1069 * and receives a second error, give up now. This is better
1070 * than waiting a long time to establish a connection that
1071 * can never complete.
1072 */
1073 if (tp->t_state == TCPS_ESTABLISHED &&
1074 (error == EHOSTUNREACH || error == ENETUNREACH ||
1075 error == EHOSTDOWN)) {
1076 return;
1077 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1078 tp->t_rxtshift > 3 && tp->t_softerror)
1079 so->so_error = error;
1080 else
1081 tp->t_softerror = error;
1082 wakeup((caddr_t) &so->so_timeo);
1083 sorwakeup(so);
1084 sowwakeup(so);
1085 }
1086
1087 #if defined(INET6) && !defined(TCP6)
1088 void
1089 tcp6_notify(in6p, error)
1090 struct in6pcb *in6p;
1091 int error;
1092 {
1093 struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb;
1094 struct socket *so = in6p->in6p_socket;
1095
1096 /*
1097 * Ignore some errors if we are hooked up.
1098 * If connection hasn't completed, has retransmitted several times,
1099 * and receives a second error, give up now. This is better
1100 * than waiting a long time to establish a connection that
1101 * can never complete.
1102 */
1103 if (tp->t_state == TCPS_ESTABLISHED &&
1104 (error == EHOSTUNREACH || error == ENETUNREACH ||
1105 error == EHOSTDOWN)) {
1106 return;
1107 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
1108 tp->t_rxtshift > 3 && tp->t_softerror)
1109 so->so_error = error;
1110 else
1111 tp->t_softerror = error;
1112 wakeup((caddr_t) &so->so_timeo);
1113 sorwakeup(so);
1114 sowwakeup(so);
1115 }
1116 #endif
1117
1118 #if defined(INET6) && !defined(TCP6)
1119 void
1120 tcp6_ctlinput(cmd, sa, d)
1121 int cmd;
1122 struct sockaddr *sa;
1123 void *d;
1124 {
1125 struct tcphdr *thp;
1126 struct tcphdr th;
1127 void (*notify) __P((struct in6pcb *, int)) = tcp6_notify;
1128 int nmatch;
1129 struct sockaddr_in6 sa6;
1130 struct ip6_hdr *ip6;
1131 struct mbuf *m;
1132 int off;
1133
1134 if (sa->sa_family != AF_INET6 ||
1135 sa->sa_len != sizeof(struct sockaddr_in6))
1136 return;
1137 if ((unsigned)cmd >= PRC_NCMDS)
1138 return;
1139 else if (cmd == PRC_QUENCH) {
1140 /* XXX there's no PRC_QUENCH in IPv6 */
1141 notify = tcp6_quench;
1142 } else if (PRC_IS_REDIRECT(cmd))
1143 notify = in6_rtchange, d = NULL;
1144 else if (cmd == PRC_MSGSIZE)
1145 notify = tcp6_mtudisc, d = NULL;
1146 else if (cmd == PRC_HOSTDEAD)
1147 d = NULL;
1148 else if (inet6ctlerrmap[cmd] == 0)
1149 return;
1150
1151 /* if the parameter is from icmp6, decode it. */
1152 if (d != NULL) {
1153 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
1154 m = ip6cp->ip6c_m;
1155 ip6 = ip6cp->ip6c_ip6;
1156 off = ip6cp->ip6c_off;
1157 } else {
1158 m = NULL;
1159 ip6 = NULL;
1160 }
1161
1162 /* translate addresses into internal form */
1163 sa6 = *(struct sockaddr_in6 *)sa;
1164 if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) && m && m->m_pkthdr.rcvif)
1165 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1166
1167 if (ip6) {
1168 /*
1169 * XXX: We assume that when ip6 is non NULL,
1170 * M and OFF are valid.
1171 */
1172 struct in6_addr s;
1173
1174 /* translate addresses into internal form */
1175 memcpy(&s, &ip6->ip6_src, sizeof(s));
1176 if (IN6_IS_ADDR_LINKLOCAL(&s))
1177 s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1178
1179 if (m->m_len < off + sizeof(th)) {
1180 /*
1181 * this should be rare case,
1182 * so we compromise on this copy...
1183 */
1184 m_copydata(m, off, sizeof(th), (caddr_t)&th);
1185 thp = &th;
1186 } else
1187 thp = (struct tcphdr *)(mtod(m, caddr_t) + off);
1188 nmatch = in6_pcbnotify(&tcb6, (struct sockaddr *)&sa6,
1189 thp->th_dport, &s, thp->th_sport, cmd, notify);
1190 if (nmatch == 0 && syn_cache_count &&
1191 (inet6ctlerrmap[cmd] == EHOSTUNREACH ||
1192 inet6ctlerrmap[cmd] == ENETUNREACH ||
1193 inet6ctlerrmap[cmd] == EHOSTDOWN)) {
1194 struct sockaddr_in6 sin6;
1195 bzero(&sin6, sizeof(sin6));
1196 sin6.sin6_len = sizeof(sin6);
1197 sin6.sin6_family = AF_INET6;
1198 sin6.sin6_port = thp->th_sport;
1199 sin6.sin6_addr = s;
1200 syn_cache_unreach((struct sockaddr *)&sin6, sa, thp);
1201 }
1202 } else {
1203 (void) in6_pcbnotify(&tcb6, (struct sockaddr *)&sa6, 0,
1204 &zeroin6_addr, 0, cmd, notify);
1205 }
1206 }
1207 #endif
1208
1209 /* assumes that ip header and tcp header are contiguous on mbuf */
1210 void *
1211 tcp_ctlinput(cmd, sa, v)
1212 int cmd;
1213 struct sockaddr *sa;
1214 void *v;
1215 {
1216 struct ip *ip = v;
1217 struct tcphdr *th;
1218 extern int inetctlerrmap[];
1219 void (*notify) __P((struct inpcb *, int)) = tcp_notify;
1220 int errno;
1221 int nmatch;
1222
1223 if (sa->sa_family != AF_INET ||
1224 sa->sa_len != sizeof(struct sockaddr_in))
1225 return NULL;
1226 if ((unsigned)cmd >= PRC_NCMDS)
1227 return NULL;
1228 errno = inetctlerrmap[cmd];
1229 if (cmd == PRC_QUENCH)
1230 notify = tcp_quench;
1231 else if (PRC_IS_REDIRECT(cmd))
1232 notify = in_rtchange, ip = 0;
1233 else if (cmd == PRC_MSGSIZE && ip_mtudisc)
1234 notify = tcp_mtudisc, ip = 0;
1235 else if (cmd == PRC_HOSTDEAD)
1236 ip = 0;
1237 else if (errno == 0)
1238 return NULL;
1239 if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) {
1240 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1241 nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
1242 th->th_dport, ip->ip_src, th->th_sport, errno, notify);
1243 if (nmatch == 0 && syn_cache_count &&
1244 (inetctlerrmap[cmd] == EHOSTUNREACH ||
1245 inetctlerrmap[cmd] == ENETUNREACH ||
1246 inetctlerrmap[cmd] == EHOSTDOWN)) {
1247 struct sockaddr_in sin;
1248 bzero(&sin, sizeof(sin));
1249 sin.sin_len = sizeof(sin);
1250 sin.sin_family = AF_INET;
1251 sin.sin_port = th->th_sport;
1252 sin.sin_addr = ip->ip_src;
1253 syn_cache_unreach((struct sockaddr *)&sin, sa, th);
1254 }
1255
1256 /* XXX mapped address case */
1257 }
1258 else {
1259 (void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
1260 notify);
1261 }
1262 return NULL;
1263 }
1264
1265 /*
1266 * When a source quence is received, we are being notifed of congestion.
1267 * Close the congestion window down to the Loss Window (one segment).
1268 * We will gradually open it again as we proceed.
1269 */
1270 void
1271 tcp_quench(inp, errno)
1272 struct inpcb *inp;
1273 int errno;
1274 {
1275 struct tcpcb *tp = intotcpcb(inp);
1276
1277 if (tp)
1278 tp->snd_cwnd = tp->t_segsz;
1279 }
1280
1281 #if defined(INET6) && !defined(TCP6)
1282 void
1283 tcp6_quench(in6p, errno)
1284 struct in6pcb *in6p;
1285 int errno;
1286 {
1287 struct tcpcb *tp = in6totcpcb(in6p);
1288
1289 if (tp)
1290 tp->snd_cwnd = tp->t_segsz;
1291 }
1292 #endif
1293
1294 /*
1295 * On receipt of path MTU corrections, flush old route and replace it
1296 * with the new one. Retransmit all unacknowledged packets, to ensure
1297 * that all packets will be received.
1298 */
1299 void
1300 tcp_mtudisc(inp, errno)
1301 struct inpcb *inp;
1302 int errno;
1303 {
1304 struct tcpcb *tp = intotcpcb(inp);
1305 struct rtentry *rt = in_pcbrtentry(inp);
1306
1307 if (tp != 0) {
1308 if (rt != 0) {
1309 /*
1310 * If this was not a host route, remove and realloc.
1311 */
1312 if ((rt->rt_flags & RTF_HOST) == 0) {
1313 in_rtchange(inp, errno);
1314 if ((rt = in_pcbrtentry(inp)) == 0)
1315 return;
1316 }
1317
1318 /*
1319 * Slow start out of the error condition. We
1320 * use the MTU because we know it's smaller
1321 * than the previously transmitted segment.
1322 *
1323 * Note: This is more conservative than the
1324 * suggestion in draft-floyd-incr-init-win-03.
1325 */
1326 if (rt->rt_rmx.rmx_mtu != 0)
1327 tp->snd_cwnd =
1328 TCP_INITIAL_WINDOW(tcp_init_win,
1329 rt->rt_rmx.rmx_mtu);
1330 }
1331
1332 /*
1333 * Resend unacknowledged packets.
1334 */
1335 tp->snd_nxt = tp->snd_una;
1336 tcp_output(tp);
1337 }
1338 }
1339
1340 #if defined(INET6) && !defined(TCP6)
1341 void
1342 tcp6_mtudisc(in6p, errno)
1343 struct in6pcb *in6p;
1344 int errno;
1345 {
1346 struct tcpcb *tp = in6totcpcb(in6p);
1347 struct rtentry *rt = in6_pcbrtentry(in6p);
1348
1349 if (tp != 0) {
1350 if (rt != 0) {
1351 /*
1352 * If this was not a host route, remove and realloc.
1353 */
1354 if ((rt->rt_flags & RTF_HOST) == 0) {
1355 in6_rtchange(in6p, errno);
1356 if ((rt = in6_pcbrtentry(in6p)) == 0)
1357 return;
1358 }
1359
1360 /*
1361 * Slow start out of the error condition. We
1362 * use the MTU because we know it's smaller
1363 * than the previously transmitted segment.
1364 *
1365 * Note: This is more conservative than the
1366 * suggestion in draft-floyd-incr-init-win-03.
1367 */
1368 if (rt->rt_rmx.rmx_mtu != 0)
1369 tp->snd_cwnd =
1370 TCP_INITIAL_WINDOW(tcp_init_win,
1371 rt->rt_rmx.rmx_mtu);
1372 }
1373
1374 /*
1375 * Resend unacknowledged packets.
1376 */
1377 tp->snd_nxt = tp->snd_una;
1378 tcp_output(tp);
1379 }
1380 }
1381 #endif
1382
1383 /*
1384 * Compute the MSS to advertise to the peer. Called only during
1385 * the 3-way handshake. If we are the server (peer initiated
1386 * connection), we are called with a pointer to the interface
1387 * on which the SYN packet arrived. If we are the client (we
1388 * initiated connection), we are called with a pointer to the
1389 * interface out which this connection should go.
1390 *
1391 * NOTE: Do not subtract IP option/extension header size nor IPsec
1392 * header size from MSS advertisement. MSS option must hold the maximum
1393 * segment size we can accept, so it must always be:
1394 * max(if mtu) - ip header - tcp header
1395 */
1396 u_long
1397 tcp_mss_to_advertise(ifp, af)
1398 const struct ifnet *ifp;
1399 int af;
1400 {
1401 extern u_long in_maxmtu;
1402 u_long mss = 0;
1403 u_long hdrsiz;
1404
1405 /*
1406 * In order to avoid defeating path MTU discovery on the peer,
1407 * we advertise the max MTU of all attached networks as our MSS,
1408 * per RFC 1191, section 3.1.
1409 *
1410 * We provide the option to advertise just the MTU of
1411 * the interface on which we hope this connection will
1412 * be receiving. If we are responding to a SYN, we
1413 * will have a pretty good idea about this, but when
1414 * initiating a connection there is a bit more doubt.
1415 *
1416 * We also need to ensure that loopback has a large enough
1417 * MSS, as the loopback MTU is never included in in_maxmtu.
1418 */
1419
1420 if (ifp != NULL)
1421 mss = ifp->if_mtu;
1422
1423 if (tcp_mss_ifmtu == 0)
1424 mss = max(in_maxmtu, mss);
1425
1426 switch (af) {
1427 case AF_INET:
1428 hdrsiz = sizeof(struct ip);
1429 break;
1430 #ifdef INET6
1431 case AF_INET6:
1432 hdrsiz = sizeof(struct ip6_hdr);
1433 break;
1434 #endif
1435 default:
1436 hdrsiz = 0;
1437 break;
1438 }
1439 hdrsiz += sizeof(struct tcphdr);
1440 if (mss > hdrsiz)
1441 mss -= hdrsiz;
1442
1443 mss = max(tcp_mssdflt, mss);
1444 return (mss);
1445 }
1446
1447 /*
1448 * Set connection variables based on the peer's advertised MSS.
1449 * We are passed the TCPCB for the actual connection. If we
1450 * are the server, we are called by the compressed state engine
1451 * when the 3-way handshake is complete. If we are the client,
1452 * we are called when we recieve the SYN,ACK from the server.
1453 *
1454 * NOTE: Our advertised MSS value must be initialized in the TCPCB
1455 * before this routine is called!
1456 */
1457 void
1458 tcp_mss_from_peer(tp, offer)
1459 struct tcpcb *tp;
1460 int offer;
1461 {
1462 struct socket *so;
1463 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1464 struct rtentry *rt;
1465 #endif
1466 u_long bufsize;
1467 int mss;
1468
1469 so = NULL;
1470 rt = NULL;
1471 if (tp->t_inpcb) {
1472 so = tp->t_inpcb->inp_socket;
1473 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1474 rt = in_pcbrtentry(tp->t_inpcb);
1475 #endif
1476 }
1477 #ifdef INET6
1478 else if (tp->t_in6pcb) {
1479 so = tp->t_in6pcb->in6p_socket;
1480 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
1481 #ifdef TCP6
1482 rt = NULL;
1483 #else
1484 rt = in6_pcbrtentry(tp->t_in6pcb);
1485 #endif
1486 #endif
1487 }
1488 #endif
1489
1490 /*
1491 * As per RFC1122, use the default MSS value, unless they
1492 * sent us an offer. Do not accept offers less than 32 bytes.
1493 */
1494 mss = tcp_mssdflt;
1495 if (offer)
1496 mss = offer;
1497 mss = max(mss, 32); /* sanity */
1498 tp->t_peermss = mss;
1499 mss -= tcp_optlen(tp);
1500 if (tp->t_inpcb)
1501 mss -= ip_optlen(tp->t_inpcb);
1502 #ifdef INET6
1503 else if (tp->t_in6pcb)
1504 mss -= ip6_optlen(tp->t_in6pcb);
1505 #endif
1506
1507 /*
1508 * If there's a pipesize, change the socket buffer to that size.
1509 * Make the socket buffer an integral number of MSS units. If
1510 * the MSS is larger than the socket buffer, artificially decrease
1511 * the MSS.
1512 */
1513 #ifdef RTV_SPIPE
1514 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
1515 bufsize = rt->rt_rmx.rmx_sendpipe;
1516 else
1517 #endif
1518 bufsize = so->so_snd.sb_hiwat;
1519 if (bufsize < mss)
1520 mss = bufsize;
1521 else {
1522 bufsize = roundup(bufsize, mss);
1523 if (bufsize > sb_max)
1524 bufsize = sb_max;
1525 (void) sbreserve(&so->so_snd, bufsize);
1526 }
1527 tp->t_segsz = mss;
1528
1529 #ifdef RTV_SSTHRESH
1530 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
1531 /*
1532 * There's some sort of gateway or interface buffer
1533 * limit on the path. Use this to set the slow
1534 * start threshold, but set the threshold to no less
1535 * than 2 * MSS.
1536 */
1537 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1538 }
1539 #endif
1540 }
1541
1542 /*
1543 * Processing necessary when a TCP connection is established.
1544 */
1545 void
1546 tcp_established(tp)
1547 struct tcpcb *tp;
1548 {
1549 struct socket *so;
1550 #ifdef RTV_RPIPE
1551 struct rtentry *rt;
1552 #endif
1553 u_long bufsize;
1554
1555 so = NULL;
1556 rt = NULL;
1557 if (tp->t_inpcb) {
1558 so = tp->t_inpcb->inp_socket;
1559 #if defined(RTV_RPIPE)
1560 rt = in_pcbrtentry(tp->t_inpcb);
1561 #endif
1562 }
1563 #ifdef INET6
1564 else if (tp->t_in6pcb) {
1565 so = tp->t_in6pcb->in6p_socket;
1566 #if defined(RTV_RPIPE)
1567 #ifdef TCP6
1568 rt = NULL;
1569 #else
1570 rt = in6_pcbrtentry(tp->t_in6pcb);
1571 #endif
1572 #endif
1573 }
1574 #endif
1575
1576 tp->t_state = TCPS_ESTABLISHED;
1577 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
1578
1579 #ifdef RTV_RPIPE
1580 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
1581 bufsize = rt->rt_rmx.rmx_recvpipe;
1582 else
1583 #endif
1584 bufsize = so->so_rcv.sb_hiwat;
1585 if (bufsize > tp->t_ourmss) {
1586 bufsize = roundup(bufsize, tp->t_ourmss);
1587 if (bufsize > sb_max)
1588 bufsize = sb_max;
1589 (void) sbreserve(&so->so_rcv, bufsize);
1590 }
1591 }
1592
1593 /*
1594 * Check if there's an initial rtt or rttvar. Convert from the
1595 * route-table units to scaled multiples of the slow timeout timer.
1596 * Called only during the 3-way handshake.
1597 */
1598 void
1599 tcp_rmx_rtt(tp)
1600 struct tcpcb *tp;
1601 {
1602 #ifdef RTV_RTT
1603 struct rtentry *rt = NULL;
1604 int rtt;
1605
1606 if (tp->t_inpcb)
1607 rt = in_pcbrtentry(tp->t_inpcb);
1608 #ifdef INET6
1609 else if (tp->t_in6pcb) {
1610 #ifdef TCP6
1611 rt = NULL;
1612 #else
1613 rt = in6_pcbrtentry(tp->t_in6pcb);
1614 #endif
1615 }
1616 #endif
1617 if (rt == NULL)
1618 return;
1619
1620 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1621 /*
1622 * XXX The lock bit for MTU indicates that the value
1623 * is also a minimum value; this is subject to time.
1624 */
1625 if (rt->rt_rmx.rmx_locks & RTV_RTT)
1626 TCPT_RANGESET(tp->t_rttmin,
1627 rtt / (RTM_RTTUNIT / PR_SLOWHZ),
1628 TCPTV_MIN, TCPTV_REXMTMAX);
1629 tp->t_srtt = rtt /
1630 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
1631 if (rt->rt_rmx.rmx_rttvar) {
1632 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1633 ((RTM_RTTUNIT / PR_SLOWHZ) >>
1634 (TCP_RTTVAR_SHIFT + 2));
1635 } else {
1636 /* Default variation is +- 1 rtt */
1637 tp->t_rttvar =
1638 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
1639 }
1640 TCPT_RANGESET(tp->t_rxtcur,
1641 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
1642 tp->t_rttmin, TCPTV_REXMTMAX);
1643 }
1644 #endif
1645 }
1646
1647 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */
1648
1649 /*
1650 * Get a new sequence value given a tcp control block
1651 */
1652 tcp_seq
1653 tcp_new_iss(tp, len, addin)
1654 void *tp;
1655 u_long len;
1656 tcp_seq addin;
1657 {
1658 tcp_seq tcp_iss;
1659
1660 /*
1661 * Randomize.
1662 */
1663 #if NRND > 0
1664 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
1665 #else
1666 tcp_iss = random();
1667 #endif
1668
1669 /*
1670 * If we were asked to add some amount to a known value,
1671 * we will take a random value obtained above, mask off the upper
1672 * bits, and add in the known value. We also add in a constant to
1673 * ensure that we are at least a certain distance from the original
1674 * value.
1675 *
1676 * This is used when an old connection is in timed wait
1677 * and we have a new one coming in, for instance.
1678 */
1679 if (addin != 0) {
1680 #ifdef TCPISS_DEBUG
1681 printf("Random %08x, ", tcp_iss);
1682 #endif
1683 tcp_iss &= TCP_ISS_RANDOM_MASK;
1684 tcp_iss += addin + TCP_ISSINCR;
1685 #ifdef TCPISS_DEBUG
1686 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
1687 #endif
1688 } else {
1689 tcp_iss &= TCP_ISS_RANDOM_MASK;
1690 tcp_iss += tcp_iss_seq;
1691 tcp_iss_seq += TCP_ISSINCR;
1692 #ifdef TCPISS_DEBUG
1693 printf("ISS %08x\n", tcp_iss);
1694 #endif
1695 }
1696
1697 if (tcp_compat_42) {
1698 /*
1699 * Limit it to the positive range for really old TCP
1700 * implementations.
1701 */
1702 if (tcp_iss >= 0x80000000)
1703 tcp_iss &= 0x7fffffff; /* XXX */
1704 }
1705
1706 return tcp_iss;
1707 }
1708
1709 #ifdef IPSEC
1710 /* compute ESP/AH header size for TCP, including outer IP header. */
1711 size_t
1712 ipsec4_hdrsiz_tcp(tp)
1713 struct tcpcb *tp;
1714 {
1715 struct inpcb *inp;
1716 size_t hdrsiz;
1717
1718 /* XXX mapped addr case (tp->t_in6pcb) */
1719 if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
1720 return 0;
1721 switch (tp->t_family) {
1722 case AF_INET:
1723 /* XXX: should use currect direction. */
1724 hdrsiz = ipsec4_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp);
1725 break;
1726 default:
1727 hdrsiz = 0;
1728 break;
1729 }
1730
1731 return hdrsiz;
1732 }
1733
1734 #if defined(INET6) && !defined(TCP6)
1735 size_t
1736 ipsec6_hdrsiz_tcp(tp)
1737 struct tcpcb *tp;
1738 {
1739 struct in6pcb *in6p;
1740 size_t hdrsiz;
1741
1742 if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb))
1743 return 0;
1744 switch (tp->t_family) {
1745 case AF_INET6:
1746 /* XXX: should use currect direction. */
1747 hdrsiz = ipsec6_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, in6p);
1748 break;
1749 case AF_INET:
1750 /* mapped address case - tricky */
1751 default:
1752 hdrsiz = 0;
1753 break;
1754 }
1755
1756 return hdrsiz;
1757 }
1758 #endif
1759 #endif /*IPSEC*/
1760
1761 /*
1762 * Determine the length of the TCP options for this connection.
1763 *
1764 * XXX: What do we do for SACK, when we add that? Just reserve
1765 * all of the space? Otherwise we can't exactly be incrementing
1766 * cwnd by an amount that varies depending on the amount we last
1767 * had to SACK!
1768 */
1769
1770 u_int
1771 tcp_optlen(tp)
1772 struct tcpcb *tp;
1773 {
1774 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
1775 (TF_REQ_TSTMP | TF_RCVD_TSTMP))
1776 return TCPOLEN_TSTAMP_APPA;
1777 else
1778 return 0;
1779 }
1780