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