tcp_timewait.c revision 75619
1/* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3 * The Regents of the University of California. 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 * 33 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 34 * $FreeBSD: head/sys/netinet/tcp_timewait.c 75619 2001-04-17 18:08:01Z kris $ 35 */ 36 37#include "opt_compat.h" 38#include "opt_inet6.h" 39#include "opt_ipsec.h" 40#include "opt_tcpdebug.h" 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/callout.h> 45#include <sys/kernel.h> 46#include <sys/sysctl.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#ifdef INET6 50#include <sys/domain.h> 51#endif 52#include <sys/proc.h> 53#include <sys/socket.h> 54#include <sys/socketvar.h> 55#include <sys/protosw.h> 56#include <sys/random.h> 57 58#include <vm/vm_zone.h> 59 60#include <net/route.h> 61#include <net/if.h> 62 63#define _IP_VHL 64#include <netinet/in.h> 65#include <netinet/in_systm.h> 66#include <netinet/ip.h> 67#ifdef INET6 68#include <netinet/ip6.h> 69#endif 70#include <netinet/in_pcb.h> 71#ifdef INET6 72#include <netinet6/in6_pcb.h> 73#endif 74#include <netinet/in_var.h> 75#include <netinet/ip_var.h> 76#ifdef INET6 77#include <netinet6/ip6_var.h> 78#endif 79#include <netinet/tcp.h> 80#include <netinet/tcp_fsm.h> 81#include <netinet/tcp_seq.h> 82#include <netinet/tcp_timer.h> 83#include <netinet/tcp_var.h> 84#ifdef INET6 85#include <netinet6/tcp6_var.h> 86#endif 87#include <netinet/tcpip.h> 88#ifdef TCPDEBUG 89#include <netinet/tcp_debug.h> 90#endif 91#include <netinet6/ip6protosw.h> 92 93#ifdef IPSEC 94#include <netinet6/ipsec.h> 95#ifdef INET6 96#include <netinet6/ipsec6.h> 97#endif 98#endif /*IPSEC*/ 99 100#include <machine/in_cksum.h> 101 102int tcp_mssdflt = TCP_MSS; 103SYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, CTLFLAG_RW, 104 &tcp_mssdflt , 0, "Default TCP Maximum Segment Size"); 105 106#ifdef INET6 107int tcp_v6mssdflt = TCP6_MSS; 108SYSCTL_INT(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt, 109 CTLFLAG_RW, &tcp_v6mssdflt , 0, 110 "Default TCP Maximum Segment Size for IPv6"); 111#endif 112 113#if 0 114static int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 115SYSCTL_INT(_net_inet_tcp, TCPCTL_RTTDFLT, rttdflt, CTLFLAG_RW, 116 &tcp_rttdflt , 0, "Default maximum TCP Round Trip Time"); 117#endif 118 119static int tcp_do_rfc1323 = 1; 120SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW, 121 &tcp_do_rfc1323 , 0, "Enable rfc1323 (high performance TCP) extensions"); 122 123static int tcp_do_rfc1644 = 0; 124SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1644, rfc1644, CTLFLAG_RW, 125 &tcp_do_rfc1644 , 0, "Enable rfc1644 (TTCP) extensions"); 126 127static int tcp_tcbhashsize = 0; 128SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RD, 129 &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable"); 130 131static int do_tcpdrain = 1; 132SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0, 133 "Enable tcp_drain routine for extra help when low on mbufs"); 134 135SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD, 136 &tcbinfo.ipi_count, 0, "Number of active PCBs"); 137 138static int icmp_may_rst = 1; 139SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_RW, &icmp_may_rst, 0, 140 "Certain ICMP unreachable messages may abort connections in SYN_SENT"); 141 142static void tcp_cleartaocache __P((void)); 143static void tcp_notify __P((struct inpcb *, int)); 144 145/* 146 * Target size of TCP PCB hash tables. Must be a power of two. 147 * 148 * Note that this can be overridden by the kernel environment 149 * variable net.inet.tcp.tcbhashsize 150 */ 151#ifndef TCBHASHSIZE 152#define TCBHASHSIZE 512 153#endif 154 155/* 156 * This is the actual shape of what we allocate using the zone 157 * allocator. Doing it this way allows us to protect both structures 158 * using the same generation count, and also eliminates the overhead 159 * of allocating tcpcbs separately. By hiding the structure here, 160 * we avoid changing most of the rest of the code (although it needs 161 * to be changed, eventually, for greater efficiency). 162 */ 163#define ALIGNMENT 32 164#define ALIGNM1 (ALIGNMENT - 1) 165struct inp_tp { 166 union { 167 struct inpcb inp; 168 char align[(sizeof(struct inpcb) + ALIGNM1) & ~ALIGNM1]; 169 } inp_tp_u; 170 struct tcpcb tcb; 171 struct callout inp_tp_rexmt, inp_tp_persist, inp_tp_keep, inp_tp_2msl; 172 struct callout inp_tp_delack; 173}; 174#undef ALIGNMENT 175#undef ALIGNM1 176 177/* 178 * Tcp initialization 179 */ 180void 181tcp_init() 182{ 183 int hashsize; 184 185#ifdef TCP_COMPAT_42 186 tcp_iss = 1; /* wrong */ 187#endif /* TCP_COMPAT_42 */ 188 tcp_ccgen = 1; 189 tcp_cleartaocache(); 190 191 tcp_delacktime = TCPTV_DELACK; 192 tcp_keepinit = TCPTV_KEEP_INIT; 193 tcp_keepidle = TCPTV_KEEP_IDLE; 194 tcp_keepintvl = TCPTV_KEEPINTVL; 195 tcp_maxpersistidle = TCPTV_KEEP_IDLE; 196 tcp_msl = TCPTV_MSL; 197 198 LIST_INIT(&tcb); 199 tcbinfo.listhead = &tcb; 200 TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", TCBHASHSIZE, hashsize); 201 if (!powerof2(hashsize)) { 202 printf("WARNING: TCB hash size not a power of 2\n"); 203 hashsize = 512; /* safe default */ 204 } 205 tcp_tcbhashsize = hashsize; 206 tcbinfo.hashbase = hashinit(hashsize, M_PCB, &tcbinfo.hashmask); 207 tcbinfo.porthashbase = hashinit(hashsize, M_PCB, 208 &tcbinfo.porthashmask); 209 tcbinfo.ipi_zone = zinit("tcpcb", sizeof(struct inp_tp), maxsockets, 210 ZONE_INTERRUPT, 0); 211#ifdef INET6 212#define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)) 213#else /* INET6 */ 214#define TCP_MINPROTOHDR (sizeof(struct tcpiphdr)) 215#endif /* INET6 */ 216 if (max_protohdr < TCP_MINPROTOHDR) 217 max_protohdr = TCP_MINPROTOHDR; 218 if (max_linkhdr + TCP_MINPROTOHDR > MHLEN) 219 panic("tcp_init"); 220#undef TCP_MINPROTOHDR 221} 222 223/* 224 * Create template to be used to send tcp packets on a connection. 225 * Call after host entry created, allocates an mbuf and fills 226 * in a skeletal tcp/ip header, minimizing the amount of work 227 * necessary when the connection is used. 228 */ 229struct tcptemp * 230tcp_template(tp) 231 struct tcpcb *tp; 232{ 233 register struct inpcb *inp = tp->t_inpcb; 234 register struct mbuf *m; 235 register struct tcptemp *n; 236 237 if ((n = tp->t_template) == 0) { 238 m = m_get(M_DONTWAIT, MT_HEADER); 239 if (m == NULL) 240 return (0); 241 m->m_len = sizeof (struct tcptemp); 242 n = mtod(m, struct tcptemp *); 243 } 244#ifdef INET6 245 if ((inp->inp_vflag & INP_IPV6) != 0) { 246 register struct ip6_hdr *ip6; 247 248 ip6 = (struct ip6_hdr *)n->tt_ipgen; 249 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 250 (inp->in6p_flowinfo & IPV6_FLOWINFO_MASK); 251 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 252 (IPV6_VERSION & IPV6_VERSION_MASK); 253 ip6->ip6_nxt = IPPROTO_TCP; 254 ip6->ip6_plen = sizeof(struct tcphdr); 255 ip6->ip6_src = inp->in6p_laddr; 256 ip6->ip6_dst = inp->in6p_faddr; 257 n->tt_t.th_sum = 0; 258 } else 259#endif 260 { 261 struct ip *ip = (struct ip *)n->tt_ipgen; 262 263 bzero(ip, sizeof(struct ip)); /* XXX overkill? */ 264 ip->ip_vhl = IP_VHL_BORING; 265 ip->ip_p = IPPROTO_TCP; 266 ip->ip_src = inp->inp_laddr; 267 ip->ip_dst = inp->inp_faddr; 268 n->tt_t.th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 269 htons(sizeof(struct tcphdr) + IPPROTO_TCP)); 270 } 271 n->tt_t.th_sport = inp->inp_lport; 272 n->tt_t.th_dport = inp->inp_fport; 273 n->tt_t.th_seq = 0; 274 n->tt_t.th_ack = 0; 275 n->tt_t.th_x2 = 0; 276 n->tt_t.th_off = 5; 277 n->tt_t.th_flags = 0; 278 n->tt_t.th_win = 0; 279 n->tt_t.th_urp = 0; 280 return (n); 281} 282 283/* 284 * Send a single message to the TCP at address specified by 285 * the given TCP/IP header. If m == 0, then we make a copy 286 * of the tcpiphdr at ti and send directly to the addressed host. 287 * This is used to force keep alive messages out using the TCP 288 * template for a connection tp->t_template. If flags are given 289 * then we send a message back to the TCP which originated the 290 * segment ti, and discard the mbuf containing it and any other 291 * attached mbufs. 292 * 293 * In any case the ack and sequence number of the transmitted 294 * segment are as specified by the parameters. 295 * 296 * NOTE: If m != NULL, then ti must point to *inside* the mbuf. 297 */ 298void 299tcp_respond(tp, ipgen, th, m, ack, seq, flags) 300 struct tcpcb *tp; 301 void *ipgen; 302 register struct tcphdr *th; 303 register struct mbuf *m; 304 tcp_seq ack, seq; 305 int flags; 306{ 307 register int tlen; 308 int win = 0; 309 struct route *ro = 0; 310 struct route sro; 311 struct ip *ip; 312 struct tcphdr *nth; 313#ifdef INET6 314 struct route_in6 *ro6 = 0; 315 struct route_in6 sro6; 316 struct ip6_hdr *ip6; 317 int isipv6; 318#endif /* INET6 */ 319 int ipflags = 0; 320 321#ifdef INET6 322 isipv6 = IP_VHL_V(((struct ip *)ipgen)->ip_vhl) == 6; 323 ip6 = ipgen; 324#endif /* INET6 */ 325 ip = ipgen; 326 327 if (tp) { 328 if (!(flags & TH_RST)) { 329 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 330 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 331 win = (long)TCP_MAXWIN << tp->rcv_scale; 332 } 333#ifdef INET6 334 if (isipv6) 335 ro6 = &tp->t_inpcb->in6p_route; 336 else 337#endif /* INET6 */ 338 ro = &tp->t_inpcb->inp_route; 339 } else { 340#ifdef INET6 341 if (isipv6) { 342 ro6 = &sro6; 343 bzero(ro6, sizeof *ro6); 344 } else 345#endif /* INET6 */ 346 { 347 ro = &sro; 348 bzero(ro, sizeof *ro); 349 } 350 } 351 if (m == 0) { 352 m = m_gethdr(M_DONTWAIT, MT_HEADER); 353 if (m == NULL) 354 return; 355#ifdef TCP_COMPAT_42 356 tlen = 1; 357#else 358 tlen = 0; 359#endif 360 m->m_data += max_linkhdr; 361#ifdef INET6 362 if (isipv6) { 363 bcopy((caddr_t)ip6, mtod(m, caddr_t), 364 sizeof(struct ip6_hdr)); 365 ip6 = mtod(m, struct ip6_hdr *); 366 nth = (struct tcphdr *)(ip6 + 1); 367 } else 368#endif /* INET6 */ 369 { 370 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 371 ip = mtod(m, struct ip *); 372 nth = (struct tcphdr *)(ip + 1); 373 } 374 bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr)); 375 flags = TH_ACK; 376 } else { 377 m_freem(m->m_next); 378 m->m_next = 0; 379 m->m_data = (caddr_t)ipgen; 380 /* m_len is set later */ 381 tlen = 0; 382#define xchg(a,b,type) { type t; t=a; a=b; b=t; } 383#ifdef INET6 384 if (isipv6) { 385 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); 386 nth = (struct tcphdr *)(ip6 + 1); 387 } else 388#endif /* INET6 */ 389 { 390 xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, n_long); 391 nth = (struct tcphdr *)(ip + 1); 392 } 393 if (th != nth) { 394 /* 395 * this is usually a case when an extension header 396 * exists between the IPv6 header and the 397 * TCP header. 398 */ 399 nth->th_sport = th->th_sport; 400 nth->th_dport = th->th_dport; 401 } 402 xchg(nth->th_dport, nth->th_sport, n_short); 403#undef xchg 404 } 405#ifdef INET6 406 if (isipv6) { 407 ip6->ip6_plen = htons((u_short)(sizeof (struct tcphdr) + 408 tlen)); 409 tlen += sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 410 } else 411#endif 412 { 413 tlen += sizeof (struct tcpiphdr); 414 ip->ip_len = tlen; 415 ip->ip_ttl = ip_defttl; 416 } 417 m->m_len = tlen; 418 m->m_pkthdr.len = tlen; 419 m->m_pkthdr.rcvif = (struct ifnet *) 0; 420 nth->th_seq = htonl(seq); 421 nth->th_ack = htonl(ack); 422 nth->th_x2 = 0; 423 nth->th_off = sizeof (struct tcphdr) >> 2; 424 nth->th_flags = flags; 425 if (tp) 426 nth->th_win = htons((u_short) (win >> tp->rcv_scale)); 427 else 428 nth->th_win = htons((u_short)win); 429 nth->th_urp = 0; 430#ifdef INET6 431 if (isipv6) { 432 nth->th_sum = 0; 433 nth->th_sum = in6_cksum(m, IPPROTO_TCP, 434 sizeof(struct ip6_hdr), 435 tlen - sizeof(struct ip6_hdr)); 436 ip6->ip6_hlim = in6_selecthlim(tp ? tp->t_inpcb : NULL, 437 ro6 && ro6->ro_rt ? 438 ro6->ro_rt->rt_ifp : 439 NULL); 440 } else 441#endif /* INET6 */ 442 { 443 nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 444 htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p))); 445 m->m_pkthdr.csum_flags = CSUM_TCP; 446 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 447 } 448#ifdef TCPDEBUG 449 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 450 tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0); 451#endif 452#ifdef IPSEC 453 ipsec_setsocket(m, tp ? tp->t_inpcb->inp_socket : NULL); 454#endif 455#ifdef INET6 456 if (isipv6) { 457 (void)ip6_output(m, NULL, ro6, ipflags, NULL, NULL); 458 if (ro6 == &sro6 && ro6->ro_rt) { 459 RTFREE(ro6->ro_rt); 460 ro6->ro_rt = NULL; 461 } 462 } else 463#endif /* INET6 */ 464 { 465 (void) ip_output(m, NULL, ro, ipflags, NULL); 466 if (ro == &sro && ro->ro_rt) { 467 RTFREE(ro->ro_rt); 468 ro->ro_rt = NULL; 469 } 470 } 471} 472 473/* 474 * Create a new TCP control block, making an 475 * empty reassembly queue and hooking it to the argument 476 * protocol control block. The `inp' parameter must have 477 * come from the zone allocator set up in tcp_init(). 478 */ 479struct tcpcb * 480tcp_newtcpcb(inp) 481 struct inpcb *inp; 482{ 483 struct inp_tp *it; 484 register struct tcpcb *tp; 485#ifdef INET6 486 int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 487#endif /* INET6 */ 488 489 it = (struct inp_tp *)inp; 490 tp = &it->tcb; 491 bzero((char *) tp, sizeof(struct tcpcb)); 492 LIST_INIT(&tp->t_segq); 493 tp->t_maxseg = tp->t_maxopd = 494#ifdef INET6 495 isipv6 ? tcp_v6mssdflt : 496#endif /* INET6 */ 497 tcp_mssdflt; 498 499 /* Set up our timeouts. */ 500 callout_init(tp->tt_rexmt = &it->inp_tp_rexmt, 0); 501 callout_init(tp->tt_persist = &it->inp_tp_persist, 0); 502 callout_init(tp->tt_keep = &it->inp_tp_keep, 0); 503 callout_init(tp->tt_2msl = &it->inp_tp_2msl, 0); 504 callout_init(tp->tt_delack = &it->inp_tp_delack, 0); 505 506 if (tcp_do_rfc1323) 507 tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP); 508 if (tcp_do_rfc1644) 509 tp->t_flags |= TF_REQ_CC; 510 tp->t_inpcb = inp; /* XXX */ 511 /* 512 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 513 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives 514 * reasonable initial retransmit time. 515 */ 516 tp->t_srtt = TCPTV_SRTTBASE; 517 tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4; 518 tp->t_rttmin = TCPTV_MIN; 519 tp->t_rxtcur = TCPTV_RTOBASE; 520 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 521 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 522 tp->t_rcvtime = ticks; 523 /* 524 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 525 * because the socket may be bound to an IPv6 wildcard address, 526 * which may match an IPv4-mapped IPv6 address. 527 */ 528 inp->inp_ip_ttl = ip_defttl; 529 inp->inp_ppcb = (caddr_t)tp; 530 return (tp); /* XXX */ 531} 532 533/* 534 * Drop a TCP connection, reporting 535 * the specified error. If connection is synchronized, 536 * then send a RST to peer. 537 */ 538struct tcpcb * 539tcp_drop(tp, errno) 540 register struct tcpcb *tp; 541 int errno; 542{ 543 struct socket *so = tp->t_inpcb->inp_socket; 544 545 if (TCPS_HAVERCVDSYN(tp->t_state)) { 546 tp->t_state = TCPS_CLOSED; 547 (void) tcp_output(tp); 548 tcpstat.tcps_drops++; 549 } else 550 tcpstat.tcps_conndrops++; 551 if (errno == ETIMEDOUT && tp->t_softerror) 552 errno = tp->t_softerror; 553 so->so_error = errno; 554 return (tcp_close(tp)); 555} 556 557/* 558 * Close a TCP control block: 559 * discard all space held by the tcp 560 * discard internet protocol block 561 * wake up any sleepers 562 */ 563struct tcpcb * 564tcp_close(tp) 565 register struct tcpcb *tp; 566{ 567 register struct tseg_qent *q; 568 struct inpcb *inp = tp->t_inpcb; 569 struct socket *so = inp->inp_socket; 570#ifdef INET6 571 int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 572#endif /* INET6 */ 573 register struct rtentry *rt; 574 int dosavessthresh; 575 576 /* 577 * Make sure that all of our timers are stopped before we 578 * delete the PCB. 579 */ 580 callout_stop(tp->tt_rexmt); 581 callout_stop(tp->tt_persist); 582 callout_stop(tp->tt_keep); 583 callout_stop(tp->tt_2msl); 584 callout_stop(tp->tt_delack); 585 586 /* 587 * If we got enough samples through the srtt filter, 588 * save the rtt and rttvar in the routing entry. 589 * 'Enough' is arbitrarily defined as the 16 samples. 590 * 16 samples is enough for the srtt filter to converge 591 * to within 5% of the correct value; fewer samples and 592 * we could save a very bogus rtt. 593 * 594 * Don't update the default route's characteristics and don't 595 * update anything that the user "locked". 596 */ 597 if (tp->t_rttupdated >= 16) { 598 register u_long i = 0; 599#ifdef INET6 600 if (isipv6) { 601 struct sockaddr_in6 *sin6; 602 603 if ((rt = inp->in6p_route.ro_rt) == NULL) 604 goto no_valid_rt; 605 sin6 = (struct sockaddr_in6 *)rt_key(rt); 606 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 607 goto no_valid_rt; 608 } 609 else 610#endif /* INET6 */ 611 if ((rt = inp->inp_route.ro_rt) == NULL || 612 ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr 613 == INADDR_ANY) 614 goto no_valid_rt; 615 616 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 617 i = tp->t_srtt * 618 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 619 if (rt->rt_rmx.rmx_rtt && i) 620 /* 621 * filter this update to half the old & half 622 * the new values, converting scale. 623 * See route.h and tcp_var.h for a 624 * description of the scaling constants. 625 */ 626 rt->rt_rmx.rmx_rtt = 627 (rt->rt_rmx.rmx_rtt + i) / 2; 628 else 629 rt->rt_rmx.rmx_rtt = i; 630 tcpstat.tcps_cachedrtt++; 631 } 632 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 633 i = tp->t_rttvar * 634 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 635 if (rt->rt_rmx.rmx_rttvar && i) 636 rt->rt_rmx.rmx_rttvar = 637 (rt->rt_rmx.rmx_rttvar + i) / 2; 638 else 639 rt->rt_rmx.rmx_rttvar = i; 640 tcpstat.tcps_cachedrttvar++; 641 } 642 /* 643 * The old comment here said: 644 * update the pipelimit (ssthresh) if it has been updated 645 * already or if a pipesize was specified & the threshhold 646 * got below half the pipesize. I.e., wait for bad news 647 * before we start updating, then update on both good 648 * and bad news. 649 * 650 * But we want to save the ssthresh even if no pipesize is 651 * specified explicitly in the route, because such 652 * connections still have an implicit pipesize specified 653 * by the global tcp_sendspace. In the absence of a reliable 654 * way to calculate the pipesize, it will have to do. 655 */ 656 i = tp->snd_ssthresh; 657 if (rt->rt_rmx.rmx_sendpipe != 0) 658 dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe / 2); 659 else 660 dosavessthresh = (i < so->so_snd.sb_hiwat / 2); 661 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 662 i != 0 && rt->rt_rmx.rmx_ssthresh != 0) 663 || dosavessthresh) { 664 /* 665 * convert the limit from user data bytes to 666 * packets then to packet data bytes. 667 */ 668 i = (i + tp->t_maxseg / 2) / tp->t_maxseg; 669 if (i < 2) 670 i = 2; 671 i *= (u_long)(tp->t_maxseg + 672#ifdef INET6 673 (isipv6 ? sizeof (struct ip6_hdr) + 674 sizeof (struct tcphdr) : 675#endif 676 sizeof (struct tcpiphdr) 677#ifdef INET6 678 ) 679#endif 680 ); 681 if (rt->rt_rmx.rmx_ssthresh) 682 rt->rt_rmx.rmx_ssthresh = 683 (rt->rt_rmx.rmx_ssthresh + i) / 2; 684 else 685 rt->rt_rmx.rmx_ssthresh = i; 686 tcpstat.tcps_cachedssthresh++; 687 } 688 } 689 rt = inp->inp_route.ro_rt; 690 if (rt) { 691 /* 692 * mark route for deletion if no information is 693 * cached. 694 */ 695 if ((tp->t_flags & TF_LQ_OVERFLOW) && 696 ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0)){ 697 if (rt->rt_rmx.rmx_rtt == 0) 698 rt->rt_flags |= RTF_DELCLONE; 699 } 700 } 701 no_valid_rt: 702 /* free the reassembly queue, if any */ 703 while((q = LIST_FIRST(&tp->t_segq)) != NULL) { 704 LIST_REMOVE(q, tqe_q); 705 m_freem(q->tqe_m); 706 FREE(q, M_TSEGQ); 707 } 708 if (tp->t_template) 709 (void) m_free(dtom(tp->t_template)); 710 inp->inp_ppcb = NULL; 711 soisdisconnected(so); 712#ifdef INET6 713 if (INP_CHECK_SOCKAF(so, AF_INET6)) 714 in6_pcbdetach(inp); 715 else 716#endif /* INET6 */ 717 in_pcbdetach(inp); 718 tcpstat.tcps_closed++; 719 return ((struct tcpcb *)0); 720} 721 722void 723tcp_drain() 724{ 725 if (do_tcpdrain) 726 { 727 struct inpcb *inpb; 728 struct tcpcb *tcpb; 729 struct tseg_qent *te; 730 731 /* 732 * Walk the tcpbs, if existing, and flush the reassembly queue, 733 * if there is one... 734 * XXX: The "Net/3" implementation doesn't imply that the TCP 735 * reassembly queue should be flushed, but in a situation 736 * where we're really low on mbufs, this is potentially 737 * usefull. 738 */ 739 LIST_FOREACH(inpb, tcbinfo.listhead, inp_list) { 740 if ((tcpb = intotcpcb(inpb))) { 741 while ((te = LIST_FIRST(&tcpb->t_segq)) 742 != NULL) { 743 LIST_REMOVE(te, tqe_q); 744 m_freem(te->tqe_m); 745 FREE(te, M_TSEGQ); 746 } 747 } 748 } 749 } 750} 751 752/* 753 * Notify a tcp user of an asynchronous error; 754 * store error as soft error, but wake up user 755 * (for now, won't do anything until can select for soft error). 756 * 757 * Do not wake up user since there currently is no mechanism for 758 * reporting soft errors (yet - a kqueue filter may be added). 759 */ 760static void 761tcp_notify(inp, error) 762 struct inpcb *inp; 763 int error; 764{ 765 struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 766 767 /* 768 * Ignore some errors if we are hooked up. 769 * If connection hasn't completed, has retransmitted several times, 770 * and receives a second error, give up now. This is better 771 * than waiting a long time to establish a connection that 772 * can never complete. 773 */ 774 if (tp->t_state == TCPS_ESTABLISHED && 775 (error == EHOSTUNREACH || error == ENETUNREACH || 776 error == EHOSTDOWN)) { 777 return; 778 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 779 tp->t_softerror) 780 tcp_drop(tp, error); 781 else 782 tp->t_softerror = error; 783#if 0 784 wakeup((caddr_t) &so->so_timeo); 785 sorwakeup(so); 786 sowwakeup(so); 787#endif 788} 789 790static int 791tcp_pcblist(SYSCTL_HANDLER_ARGS) 792{ 793 int error, i, n, s; 794 struct inpcb *inp, **inp_list; 795 inp_gen_t gencnt; 796 struct xinpgen xig; 797 798 /* 799 * The process of preparing the TCB list is too time-consuming and 800 * resource-intensive to repeat twice on every request. 801 */ 802 if (req->oldptr == 0) { 803 n = tcbinfo.ipi_count; 804 req->oldidx = 2 * (sizeof xig) 805 + (n + n/8) * sizeof(struct xtcpcb); 806 return 0; 807 } 808 809 if (req->newptr != 0) 810 return EPERM; 811 812 /* 813 * OK, now we're committed to doing something. 814 */ 815 s = splnet(); 816 gencnt = tcbinfo.ipi_gencnt; 817 n = tcbinfo.ipi_count; 818 splx(s); 819 820 xig.xig_len = sizeof xig; 821 xig.xig_count = n; 822 xig.xig_gen = gencnt; 823 xig.xig_sogen = so_gencnt; 824 error = SYSCTL_OUT(req, &xig, sizeof xig); 825 if (error) 826 return error; 827 828 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 829 if (inp_list == 0) 830 return ENOMEM; 831 832 s = splnet(); 833 for (inp = LIST_FIRST(tcbinfo.listhead), i = 0; inp && i < n; 834 inp = LIST_NEXT(inp, inp_list)) { 835 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp)) 836 inp_list[i++] = inp; 837 } 838 splx(s); 839 n = i; 840 841 error = 0; 842 for (i = 0; i < n; i++) { 843 inp = inp_list[i]; 844 if (inp->inp_gencnt <= gencnt) { 845 struct xtcpcb xt; 846 caddr_t inp_ppcb; 847 xt.xt_len = sizeof xt; 848 /* XXX should avoid extra copy */ 849 bcopy(inp, &xt.xt_inp, sizeof *inp); 850 inp_ppcb = inp->inp_ppcb; 851 if (inp_ppcb != NULL) 852 bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp); 853 else 854 bzero((char *) &xt.xt_tp, sizeof xt.xt_tp); 855 if (inp->inp_socket) 856 sotoxsocket(inp->inp_socket, &xt.xt_socket); 857 error = SYSCTL_OUT(req, &xt, sizeof xt); 858 } 859 } 860 if (!error) { 861 /* 862 * Give the user an updated idea of our state. 863 * If the generation differs from what we told 864 * her before, she knows that something happened 865 * while we were processing this request, and it 866 * might be necessary to retry. 867 */ 868 s = splnet(); 869 xig.xig_gen = tcbinfo.ipi_gencnt; 870 xig.xig_sogen = so_gencnt; 871 xig.xig_count = tcbinfo.ipi_count; 872 splx(s); 873 error = SYSCTL_OUT(req, &xig, sizeof xig); 874 } 875 free(inp_list, M_TEMP); 876 return error; 877} 878 879SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 880 tcp_pcblist, "S,xtcpcb", "List of active TCP connections"); 881 882static int 883tcp_getcred(SYSCTL_HANDLER_ARGS) 884{ 885 struct xucred xuc; 886 struct sockaddr_in addrs[2]; 887 struct inpcb *inp; 888 int error, s; 889 890 error = suser(req->p); 891 if (error) 892 return (error); 893 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 894 if (error) 895 return (error); 896 s = splnet(); 897 inp = in_pcblookup_hash(&tcbinfo, addrs[1].sin_addr, addrs[1].sin_port, 898 addrs[0].sin_addr, addrs[0].sin_port, 0, NULL); 899 if (inp == NULL || inp->inp_socket == NULL) { 900 error = ENOENT; 901 goto out; 902 } 903 bzero(&xuc, sizeof(xuc)); 904 xuc.cr_uid = inp->inp_socket->so_cred->cr_uid; 905 xuc.cr_ngroups = inp->inp_socket->so_cred->cr_ngroups; 906 bcopy(inp->inp_socket->so_cred->cr_groups, xuc.cr_groups, 907 sizeof(xuc.cr_groups)); 908 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 909out: 910 splx(s); 911 return (error); 912} 913 914SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 915 0, 0, tcp_getcred, "S,xucred", "Get the xucred of a TCP connection"); 916 917#ifdef INET6 918static int 919tcp6_getcred(SYSCTL_HANDLER_ARGS) 920{ 921 struct xucred xuc; 922 struct sockaddr_in6 addrs[2]; 923 struct inpcb *inp; 924 int error, s, mapped = 0; 925 926 error = suser(req->p); 927 if (error) 928 return (error); 929 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 930 if (error) 931 return (error); 932 if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) { 933 if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr)) 934 mapped = 1; 935 else 936 return (EINVAL); 937 } 938 s = splnet(); 939 if (mapped == 1) 940 inp = in_pcblookup_hash(&tcbinfo, 941 *(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12], 942 addrs[1].sin6_port, 943 *(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12], 944 addrs[0].sin6_port, 945 0, NULL); 946 else 947 inp = in6_pcblookup_hash(&tcbinfo, &addrs[1].sin6_addr, 948 addrs[1].sin6_port, 949 &addrs[0].sin6_addr, addrs[0].sin6_port, 950 0, NULL); 951 if (inp == NULL || inp->inp_socket == NULL) { 952 error = ENOENT; 953 goto out; 954 } 955 bzero(&xuc, sizeof(xuc)); 956 xuc.cr_uid = inp->inp_socket->so_cred->cr_uid; 957 xuc.cr_ngroups = inp->inp_socket->so_cred->cr_ngroups; 958 bcopy(inp->inp_socket->so_cred->cr_groups, xuc.cr_groups, 959 sizeof(xuc.cr_groups)); 960 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 961out: 962 splx(s); 963 return (error); 964} 965 966SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 967 0, 0, 968 tcp6_getcred, "S,xucred", "Get the xucred of a TCP6 connection"); 969#endif 970 971 972void 973tcp_ctlinput(cmd, sa, vip) 974 int cmd; 975 struct sockaddr *sa; 976 void *vip; 977{ 978 struct ip *ip = vip; 979 struct tcphdr *th; 980 struct in_addr faddr; 981 struct inpcb *inp; 982 struct tcpcb *tp; 983 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 984 tcp_seq icmp_seq; 985 int s; 986 987 faddr = ((struct sockaddr_in *)sa)->sin_addr; 988 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 989 return; 990 991 if (cmd == PRC_QUENCH) 992 notify = tcp_quench; 993 else if (icmp_may_rst && (cmd == PRC_UNREACH_ADMIN_PROHIB || 994 cmd == PRC_UNREACH_PORT) && ip) 995 notify = tcp_drop_syn_sent; 996 else if (cmd == PRC_MSGSIZE) 997 notify = tcp_mtudisc; 998 else if (PRC_IS_REDIRECT(cmd)) { 999 ip = 0; 1000 notify = in_rtchange; 1001 } else if (cmd == PRC_HOSTDEAD) 1002 ip = 0; 1003 else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0) 1004 return; 1005 if (ip) { 1006 s = splnet(); 1007 th = (struct tcphdr *)((caddr_t)ip 1008 + (IP_VHL_HL(ip->ip_vhl) << 2)); 1009 inp = in_pcblookup_hash(&tcbinfo, faddr, th->th_dport, 1010 ip->ip_src, th->th_sport, 0, NULL); 1011 if (inp != NULL && inp->inp_socket != NULL) { 1012 icmp_seq = htonl(th->th_seq); 1013 tp = intotcpcb(inp); 1014 if (SEQ_GEQ(icmp_seq, tp->snd_una) && 1015 SEQ_LT(icmp_seq, tp->snd_max)) 1016 (*notify)(inp, inetctlerrmap[cmd]); 1017 } 1018 splx(s); 1019 } else 1020 in_pcbnotifyall(&tcb, faddr, inetctlerrmap[cmd], notify); 1021} 1022 1023#ifdef INET6 1024void 1025tcp6_ctlinput(cmd, sa, d) 1026 int cmd; 1027 struct sockaddr *sa; 1028 void *d; 1029{ 1030 register struct tcphdr *thp; 1031 struct tcphdr th; 1032 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 1033 struct sockaddr_in6 sa6; 1034 struct ip6_hdr *ip6; 1035 struct mbuf *m; 1036 int off; 1037 1038 if (sa->sa_family != AF_INET6 || 1039 sa->sa_len != sizeof(struct sockaddr_in6)) 1040 return; 1041 1042 if (cmd == PRC_QUENCH) 1043 notify = tcp_quench; 1044 else if (cmd == PRC_MSGSIZE) 1045 notify = tcp_mtudisc; 1046 else if (!PRC_IS_REDIRECT(cmd) && 1047 ((unsigned)cmd > PRC_NCMDS || inet6ctlerrmap[cmd] == 0)) 1048 return; 1049 1050 /* if the parameter is from icmp6, decode it. */ 1051 if (d != NULL) { 1052 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d; 1053 m = ip6cp->ip6c_m; 1054 ip6 = ip6cp->ip6c_ip6; 1055 off = ip6cp->ip6c_off; 1056 } else { 1057 m = NULL; 1058 ip6 = NULL; 1059 off = 0; /* fool gcc */ 1060 } 1061 1062 /* 1063 * Translate addresses into internal form. 1064 * Sa check if it is AF_INET6 is done at the top of this funciton. 1065 */ 1066 sa6 = *(struct sockaddr_in6 *)sa; 1067 if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) != 0 && m != NULL && 1068 m->m_pkthdr.rcvif != NULL) 1069 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); 1070 1071 if (ip6) { 1072 /* 1073 * XXX: We assume that when IPV6 is non NULL, 1074 * M and OFF are valid. 1075 */ 1076 struct in6_addr s; 1077 1078 /* translate addresses into internal form */ 1079 memcpy(&s, &ip6->ip6_src, sizeof(s)); 1080 if (IN6_IS_ADDR_LINKLOCAL(&s) != 0 && m != NULL && 1081 m->m_pkthdr.rcvif != NULL) 1082 s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); 1083 1084 /* check if we can safely examine src and dst ports */ 1085 if (m->m_pkthdr.len < off + sizeof(th)) 1086 return; 1087 1088 if (m->m_len < off + sizeof(th)) { 1089 /* 1090 * this should be rare case 1091 * because now MINCLSIZE is "(MHLEN + 1)", 1092 * so we compromise on this copy... 1093 */ 1094 m_copydata(m, off, sizeof(th), (caddr_t)&th); 1095 thp = &th; 1096 } else 1097 thp = (struct tcphdr *)(mtod(m, caddr_t) + off); 1098 in6_pcbnotify(&tcb, (struct sockaddr *)&sa6, thp->th_dport, 1099 &s, thp->th_sport, cmd, notify); 1100 } else 1101 in6_pcbnotify(&tcb, (struct sockaddr *)&sa6, 0, &zeroin6_addr, 1102 0, cmd, notify); 1103} 1104#endif /* INET6 */ 1105 1106#define TCP_RNDISS_ROUNDS 16 1107#define TCP_RNDISS_OUT 7200 1108#define TCP_RNDISS_MAX 30000 1109 1110u_int8_t tcp_rndiss_sbox[128]; 1111u_int16_t tcp_rndiss_msb; 1112u_int16_t tcp_rndiss_cnt; 1113long tcp_rndiss_reseed; 1114 1115u_int16_t 1116tcp_rndiss_encrypt(val) 1117 u_int16_t val; 1118{ 1119 u_int16_t sum = 0, i; 1120 1121 for (i = 0; i < TCP_RNDISS_ROUNDS; i++) { 1122 sum += 0x79b9; 1123 val ^= ((u_int16_t)tcp_rndiss_sbox[(val^sum) & 0x7f]) << 7; 1124 val = ((val & 0xff) << 7) | (val >> 8); 1125 } 1126 1127 return val; 1128} 1129 1130void 1131tcp_rndiss_init() 1132{ 1133 struct timeval time; 1134 1135 getmicrotime(&time); 1136 read_random(tcp_rndiss_sbox, sizeof(tcp_rndiss_sbox)); 1137 1138 tcp_rndiss_reseed = time.tv_sec + TCP_RNDISS_OUT; 1139 tcp_rndiss_msb = tcp_rndiss_msb == 0x8000 ? 0 : 0x8000; 1140 tcp_rndiss_cnt = 0; 1141} 1142 1143tcp_seq 1144tcp_rndiss_next() 1145{ 1146 u_int16_t tmp; 1147 struct timeval time; 1148 1149 getmicrotime(&time); 1150 1151 if (tcp_rndiss_cnt >= TCP_RNDISS_MAX || 1152 time.tv_sec > tcp_rndiss_reseed) 1153 tcp_rndiss_init(); 1154 1155 read_random(&tmp, sizeof(tmp)); 1156 1157 /* (tmp & 0x7fff) ensures a 32768 byte gap between ISS */ 1158 return ((tcp_rndiss_encrypt(tcp_rndiss_cnt++) | tcp_rndiss_msb) <<16) | 1159 (tmp & 0x7fff); 1160} 1161 1162 1163/* 1164 * When a source quench is received, close congestion window 1165 * to one segment. We will gradually open it again as we proceed. 1166 */ 1167void 1168tcp_quench(inp, errno) 1169 struct inpcb *inp; 1170 int errno; 1171{ 1172 struct tcpcb *tp = intotcpcb(inp); 1173 1174 if (tp) 1175 tp->snd_cwnd = tp->t_maxseg; 1176} 1177 1178/* 1179 * When a specific ICMP unreachable message is received and the 1180 * connection state is SYN-SENT, drop the connection. This behavior 1181 * is controlled by the icmp_may_rst sysctl. 1182 */ 1183void 1184tcp_drop_syn_sent(inp, errno) 1185 struct inpcb *inp; 1186 int errno; 1187{ 1188 struct tcpcb *tp = intotcpcb(inp); 1189 1190 if (tp && tp->t_state == TCPS_SYN_SENT) 1191 tcp_drop(tp, errno); 1192} 1193 1194/* 1195 * When `need fragmentation' ICMP is received, update our idea of the MSS 1196 * based on the new value in the route. Also nudge TCP to send something, 1197 * since we know the packet we just sent was dropped. 1198 * This duplicates some code in the tcp_mss() function in tcp_input.c. 1199 */ 1200void 1201tcp_mtudisc(inp, errno) 1202 struct inpcb *inp; 1203 int errno; 1204{ 1205 struct tcpcb *tp = intotcpcb(inp); 1206 struct rtentry *rt; 1207 struct rmxp_tao *taop; 1208 struct socket *so = inp->inp_socket; 1209 int offered; 1210 int mss; 1211#ifdef INET6 1212 int isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 1213#endif /* INET6 */ 1214 1215 if (tp) { 1216#ifdef INET6 1217 if (isipv6) 1218 rt = tcp_rtlookup6(inp); 1219 else 1220#endif /* INET6 */ 1221 rt = tcp_rtlookup(inp); 1222 if (!rt || !rt->rt_rmx.rmx_mtu) { 1223 tp->t_maxopd = tp->t_maxseg = 1224#ifdef INET6 1225 isipv6 ? tcp_v6mssdflt : 1226#endif /* INET6 */ 1227 tcp_mssdflt; 1228 return; 1229 } 1230 taop = rmx_taop(rt->rt_rmx); 1231 offered = taop->tao_mssopt; 1232 mss = rt->rt_rmx.rmx_mtu - 1233#ifdef INET6 1234 (isipv6 ? 1235 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) : 1236#endif /* INET6 */ 1237 sizeof(struct tcpiphdr) 1238#ifdef INET6 1239 ) 1240#endif /* INET6 */ 1241 ; 1242 1243 if (offered) 1244 mss = min(mss, offered); 1245 /* 1246 * XXX - The above conditional probably violates the TCP 1247 * spec. The problem is that, since we don't know the 1248 * other end's MSS, we are supposed to use a conservative 1249 * default. But, if we do that, then MTU discovery will 1250 * never actually take place, because the conservative 1251 * default is much less than the MTUs typically seen 1252 * on the Internet today. For the moment, we'll sweep 1253 * this under the carpet. 1254 * 1255 * The conservative default might not actually be a problem 1256 * if the only case this occurs is when sending an initial 1257 * SYN with options and data to a host we've never talked 1258 * to before. Then, they will reply with an MSS value which 1259 * will get recorded and the new parameters should get 1260 * recomputed. For Further Study. 1261 */ 1262 if (tp->t_maxopd <= mss) 1263 return; 1264 tp->t_maxopd = mss; 1265 1266 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 1267 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 1268 mss -= TCPOLEN_TSTAMP_APPA; 1269 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 1270 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC) 1271 mss -= TCPOLEN_CC_APPA; 1272#if (MCLBYTES & (MCLBYTES - 1)) == 0 1273 if (mss > MCLBYTES) 1274 mss &= ~(MCLBYTES-1); 1275#else 1276 if (mss > MCLBYTES) 1277 mss = mss / MCLBYTES * MCLBYTES; 1278#endif 1279 if (so->so_snd.sb_hiwat < mss) 1280 mss = so->so_snd.sb_hiwat; 1281 1282 tp->t_maxseg = mss; 1283 1284 tcpstat.tcps_mturesent++; 1285 tp->t_rtttime = 0; 1286 tp->snd_nxt = tp->snd_una; 1287 tcp_output(tp); 1288 } 1289} 1290 1291/* 1292 * Look-up the routing entry to the peer of this inpcb. If no route 1293 * is found and it cannot be allocated the return NULL. This routine 1294 * is called by TCP routines that access the rmx structure and by tcp_mss 1295 * to get the interface MTU. 1296 */ 1297struct rtentry * 1298tcp_rtlookup(inp) 1299 struct inpcb *inp; 1300{ 1301 struct route *ro; 1302 struct rtentry *rt; 1303 1304 ro = &inp->inp_route; 1305 rt = ro->ro_rt; 1306 if (rt == NULL || !(rt->rt_flags & RTF_UP)) { 1307 /* No route yet, so try to acquire one */ 1308 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1309 ro->ro_dst.sa_family = AF_INET; 1310 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 1311 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 1312 inp->inp_faddr; 1313 rtalloc(ro); 1314 rt = ro->ro_rt; 1315 } 1316 } 1317 return rt; 1318} 1319 1320#ifdef INET6 1321struct rtentry * 1322tcp_rtlookup6(inp) 1323 struct inpcb *inp; 1324{ 1325 struct route_in6 *ro6; 1326 struct rtentry *rt; 1327 1328 ro6 = &inp->in6p_route; 1329 rt = ro6->ro_rt; 1330 if (rt == NULL || !(rt->rt_flags & RTF_UP)) { 1331 /* No route yet, so try to acquire one */ 1332 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 1333 ro6->ro_dst.sin6_family = AF_INET6; 1334 ro6->ro_dst.sin6_len = sizeof(ro6->ro_dst); 1335 ro6->ro_dst.sin6_addr = inp->in6p_faddr; 1336 rtalloc((struct route *)ro6); 1337 rt = ro6->ro_rt; 1338 } 1339 } 1340 return rt; 1341} 1342#endif /* INET6 */ 1343 1344#ifdef IPSEC 1345/* compute ESP/AH header size for TCP, including outer IP header. */ 1346size_t 1347ipsec_hdrsiz_tcp(tp) 1348 struct tcpcb *tp; 1349{ 1350 struct inpcb *inp; 1351 struct mbuf *m; 1352 size_t hdrsiz; 1353 struct ip *ip; 1354#ifdef INET6 1355 struct ip6_hdr *ip6; 1356#endif /* INET6 */ 1357 struct tcphdr *th; 1358 1359 if (!tp || !tp->t_template || !(inp = tp->t_inpcb)) 1360 return 0; 1361 MGETHDR(m, M_DONTWAIT, MT_DATA); 1362 if (!m) 1363 return 0; 1364 1365#ifdef INET6 1366 if ((inp->inp_vflag & INP_IPV6) != 0) { 1367 ip6 = mtod(m, struct ip6_hdr *); 1368 th = (struct tcphdr *)(ip6 + 1); 1369 m->m_pkthdr.len = m->m_len = 1370 sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 1371 bcopy((caddr_t)tp->t_template->tt_ipgen, (caddr_t)ip6, 1372 sizeof(struct ip6_hdr)); 1373 bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th, 1374 sizeof(struct tcphdr)); 1375 hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); 1376 } else 1377#endif /* INET6 */ 1378 { 1379 ip = mtod(m, struct ip *); 1380 th = (struct tcphdr *)(ip + 1); 1381 m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr); 1382 bcopy((caddr_t)tp->t_template->tt_ipgen, (caddr_t)ip, 1383 sizeof(struct ip)); 1384 bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th, 1385 sizeof(struct tcphdr)); 1386 hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); 1387 } 1388 1389 m_free(m); 1390 return hdrsiz; 1391} 1392#endif /*IPSEC*/ 1393 1394/* 1395 * Return a pointer to the cached information about the remote host. 1396 * The cached information is stored in the protocol specific part of 1397 * the route metrics. 1398 */ 1399struct rmxp_tao * 1400tcp_gettaocache(inp) 1401 struct inpcb *inp; 1402{ 1403 struct rtentry *rt; 1404 1405#ifdef INET6 1406 if ((inp->inp_vflag & INP_IPV6) != 0) 1407 rt = tcp_rtlookup6(inp); 1408 else 1409#endif /* INET6 */ 1410 rt = tcp_rtlookup(inp); 1411 1412 /* Make sure this is a host route and is up. */ 1413 if (rt == NULL || 1414 (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST)) 1415 return NULL; 1416 1417 return rmx_taop(rt->rt_rmx); 1418} 1419 1420/* 1421 * Clear all the TAO cache entries, called from tcp_init. 1422 * 1423 * XXX 1424 * This routine is just an empty one, because we assume that the routing 1425 * routing tables are initialized at the same time when TCP, so there is 1426 * nothing in the cache left over. 1427 */ 1428static void 1429tcp_cleartaocache() 1430{ 1431} 1432