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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_compat.h" 36 #include "opt_inet.h" 37 #include "opt_inet6.h" 38 #include "opt_ipsec.h" 39 #include "opt_tcpdebug.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/callout.h> 44 #include <sys/hhook.h> 45 #include <sys/kernel.h> 46 #include <sys/khelp.h> 47 #include <sys/sysctl.h> 48 #include <sys/jail.h> 49 #include <sys/malloc.h> 50 #include <sys/mbuf.h> 51 #ifdef INET6 52 #include <sys/domain.h> 53 #endif 54 #include <sys/priv.h> 55 #include <sys/proc.h> 56 #include <sys/socket.h> 57 #include <sys/socketvar.h> 58 #include <sys/protosw.h> 59 #include <sys/random.h> 60 61 #include <vm/uma.h> 62 63 #include <net/route.h> 64 #include <net/if.h> 65 #include <net/vnet.h> 66 67 #include <netinet/cc.h> 68 #include <netinet/in.h> 69 #include <netinet/in_systm.h> 70 #include <netinet/ip.h> 71 #ifdef INET6 72 #include <netinet/ip6.h> 73 #endif 74 #include <netinet/in_pcb.h> 75 #ifdef INET6 76 #include <netinet6/in6_pcb.h> 77 #endif 78 #include <netinet/in_var.h> 79 #include <netinet/ip_var.h> 80 #ifdef INET6 81 #include <netinet6/ip6_var.h> 82 #include <netinet6/scope6_var.h> 83 #include <netinet6/nd6.h> 84 #endif 85 #include <netinet/ip_icmp.h> 86 #include <netinet/tcp_fsm.h> 87 #include <netinet/tcp_seq.h> 88 #include <netinet/tcp_timer.h> 89 #include <netinet/tcp_var.h> 90 #include <netinet/tcp_syncache.h> 91 #include <netinet/tcp_offload.h> 92 #ifdef INET6 93 #include <netinet6/tcp6_var.h> 94 #endif 95 #include <netinet/tcpip.h> 96 #ifdef TCPDEBUG 97 #include <netinet/tcp_debug.h> 98 #endif 99 #include <netinet6/ip6protosw.h> 100 101 #ifdef IPSEC 102 #include <netipsec/ipsec.h> 103 #include <netipsec/xform.h> 104 #ifdef INET6 105 #include <netipsec/ipsec6.h> 106 #endif 107 #include <netipsec/key.h> 108 #include <sys/syslog.h> 109 #endif /*IPSEC*/ 110 111 #include <machine/in_cksum.h> 112 #include <sys/md5.h> 113 114 #include <security/mac/mac_framework.h> 115 116 VNET_DEFINE(int, tcp_mssdflt) = TCP_MSS; 117 #ifdef INET6 118 VNET_DEFINE(int, tcp_v6mssdflt) = TCP6_MSS; 119 #endif 120 121 static int 122 sysctl_net_inet_tcp_mss_check(SYSCTL_HANDLER_ARGS) 123 { 124 int error, new; 125 126 new = V_tcp_mssdflt; 127 error = sysctl_handle_int(oidp, &new, 0, req); 128 if (error == 0 && req->newptr) { 129 if (new < TCP_MINMSS) 130 error = EINVAL; 131 else 132 V_tcp_mssdflt = new; 133 } 134 return (error); 135 } 136 137 SYSCTL_VNET_PROC(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, 138 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(tcp_mssdflt), 0, 139 &sysctl_net_inet_tcp_mss_check, "I", 140 "Default TCP Maximum Segment Size"); 141 142 #ifdef INET6 143 static int 144 sysctl_net_inet_tcp_mss_v6_check(SYSCTL_HANDLER_ARGS) 145 { 146 int error, new; 147 148 new = V_tcp_v6mssdflt; 149 error = sysctl_handle_int(oidp, &new, 0, req); 150 if (error == 0 && req->newptr) { 151 if (new < TCP_MINMSS) 152 error = EINVAL; 153 else 154 V_tcp_v6mssdflt = new; 155 } 156 return (error); 157 } 158 159 SYSCTL_VNET_PROC(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt, 160 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(tcp_v6mssdflt), 0, 161 &sysctl_net_inet_tcp_mss_v6_check, "I", 162 "Default TCP Maximum Segment Size for IPv6"); 163 #endif 164 165 /* 166 * Minimum MSS we accept and use. This prevents DoS attacks where 167 * we are forced to a ridiculous low MSS like 20 and send hundreds 168 * of packets instead of one. The effect scales with the available 169 * bandwidth and quickly saturates the CPU and network interface 170 * with packet generation and sending. Set to zero to disable MINMSS 171 * checking. This setting prevents us from sending too small packets. 172 */ 173 VNET_DEFINE(int, tcp_minmss) = TCP_MINMSS; 174 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, minmss, CTLFLAG_RW, 175 &VNET_NAME(tcp_minmss), 0, 176 "Minmum TCP Maximum Segment Size"); 177 178 VNET_DEFINE(int, tcp_do_rfc1323) = 1; 179 SYSCTL_VNET_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW, 180 &VNET_NAME(tcp_do_rfc1323), 0, 181 "Enable rfc1323 (high performance TCP) extensions"); 182 183 static int tcp_log_debug = 0; 184 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_debug, CTLFLAG_RW, 185 &tcp_log_debug, 0, "Log errors caused by incoming TCP segments"); 186 187 static int tcp_tcbhashsize = 0; 188 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RDTUN, 189 &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable"); 190 191 static int do_tcpdrain = 1; 192 SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0, 193 "Enable tcp_drain routine for extra help when low on mbufs"); 194 195 SYSCTL_VNET_UINT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD, 196 &VNET_NAME(tcbinfo.ipi_count), 0, "Number of active PCBs"); 197 198 static VNET_DEFINE(int, icmp_may_rst) = 1; 199 #define V_icmp_may_rst VNET(icmp_may_rst) 200 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_RW, 201 &VNET_NAME(icmp_may_rst), 0, 202 "Certain ICMP unreachable messages may abort connections in SYN_SENT"); 203 204 static VNET_DEFINE(int, tcp_isn_reseed_interval) = 0; 205 #define V_tcp_isn_reseed_interval VNET(tcp_isn_reseed_interval) 206 SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, CTLFLAG_RW, 207 &VNET_NAME(tcp_isn_reseed_interval), 0, 208 "Seconds between reseeding of ISN secret"); 209 210 #ifdef TCP_SORECEIVE_STREAM 211 static int tcp_soreceive_stream = 0; 212 SYSCTL_INT(_net_inet_tcp, OID_AUTO, soreceive_stream, CTLFLAG_RDTUN, 213 &tcp_soreceive_stream, 0, "Using soreceive_stream for TCP sockets"); 214 #endif 215 216 VNET_DEFINE(uma_zone_t, sack_hole_zone); 217 #define V_sack_hole_zone VNET(sack_hole_zone) 218 219 VNET_DEFINE(struct hhook_head *, tcp_hhh[HHOOK_TCP_LAST+1]); 220 221 static struct inpcb *tcp_notify(struct inpcb *, int); 222 static void tcp_isn_tick(void *); 223 static char * tcp_log_addr(struct in_conninfo *inc, struct tcphdr *th, 224 void *ip4hdr, const void *ip6hdr); 225 226 /* 227 * Target size of TCP PCB hash tables. Must be a power of two. 228 * 229 * Note that this can be overridden by the kernel environment 230 * variable net.inet.tcp.tcbhashsize 231 */ 232 #ifndef TCBHASHSIZE 233 #define TCBHASHSIZE 512 234 #endif 235 236 /* 237 * XXX 238 * Callouts should be moved into struct tcp directly. They are currently 239 * separate because the tcpcb structure is exported to userland for sysctl 240 * parsing purposes, which do not know about callouts. 241 */ 242 struct tcpcb_mem { 243 struct tcpcb tcb; 244 struct tcp_timer tt; 245 struct cc_var ccv; 246 struct osd osd; 247 }; 248 249 static VNET_DEFINE(uma_zone_t, tcpcb_zone); 250 #define V_tcpcb_zone VNET(tcpcb_zone) 251 252 MALLOC_DEFINE(M_TCPLOG, "tcplog", "TCP address and flags print buffers"); 253 struct callout isn_callout; 254 static struct mtx isn_mtx; 255 256 #define ISN_LOCK_INIT() mtx_init(&isn_mtx, "isn_mtx", NULL, MTX_DEF) 257 #define ISN_LOCK() mtx_lock(&isn_mtx) 258 #define ISN_UNLOCK() mtx_unlock(&isn_mtx) 259 260 /* 261 * TCP initialization. 262 */ 263 static void 264 tcp_zone_change(void *tag) 265 { 266 267 uma_zone_set_max(V_tcbinfo.ipi_zone, maxsockets); 268 uma_zone_set_max(V_tcpcb_zone, maxsockets); 269 tcp_tw_zone_change(); 270 } 271 272 static int 273 tcp_inpcb_init(void *mem, int size, int flags) 274 { 275 struct inpcb *inp = mem; 276 277 INP_LOCK_INIT(inp, "inp", "tcpinp"); 278 return (0); 279 } 280 281 void 282 tcp_init(void) 283 { 284 int hashsize; 285 286 if (hhook_head_register(HHOOK_TYPE_TCP, HHOOK_TCP_EST_IN, 287 &V_tcp_hhh[HHOOK_TCP_EST_IN], HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0) 288 printf("%s: WARNING: unable to register helper hook\n", __func__); 289 if (hhook_head_register(HHOOK_TYPE_TCP, HHOOK_TCP_EST_OUT, 290 &V_tcp_hhh[HHOOK_TCP_EST_OUT], HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0) 291 printf("%s: WARNING: unable to register helper hook\n", __func__); 292 293 hashsize = TCBHASHSIZE; 294 TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", &hashsize); 295 if (!powerof2(hashsize)) { 296 printf("WARNING: TCB hash size not a power of 2\n"); 297 hashsize = 512; /* safe default */ 298 } 299 in_pcbinfo_init(&V_tcbinfo, "tcp", &V_tcb, hashsize, hashsize, 300 "tcp_inpcb", tcp_inpcb_init, NULL, UMA_ZONE_NOFREE); 301 302 /* 303 * These have to be type stable for the benefit of the timers. 304 */ 305 V_tcpcb_zone = uma_zcreate("tcpcb", sizeof(struct tcpcb_mem), 306 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 307 uma_zone_set_max(V_tcpcb_zone, maxsockets); 308 309 tcp_tw_init(); 310 syncache_init(); 311 tcp_hc_init(); 312 tcp_reass_init(); 313 314 TUNABLE_INT_FETCH("net.inet.tcp.sack.enable", &V_tcp_do_sack); 315 V_sack_hole_zone = uma_zcreate("sackhole", sizeof(struct sackhole), 316 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 317 318 /* Skip initialization of globals for non-default instances. */ 319 if (!IS_DEFAULT_VNET(curvnet)) 320 return; 321 322 /* XXX virtualize those bellow? */ 323 tcp_delacktime = TCPTV_DELACK; 324 tcp_keepinit = TCPTV_KEEP_INIT; 325 tcp_keepidle = TCPTV_KEEP_IDLE; 326 tcp_keepintvl = TCPTV_KEEPINTVL; 327 tcp_maxpersistidle = TCPTV_KEEP_IDLE; 328 tcp_msl = TCPTV_MSL; 329 tcp_rexmit_min = TCPTV_MIN; 330 if (tcp_rexmit_min < 1) 331 tcp_rexmit_min = 1; 332 tcp_rexmit_slop = TCPTV_CPU_VAR; 333 tcp_finwait2_timeout = TCPTV_FINWAIT2_TIMEOUT; 334 tcp_tcbhashsize = hashsize; 335 336 #ifdef TCP_SORECEIVE_STREAM 337 TUNABLE_INT_FETCH("net.inet.tcp.soreceive_stream", &tcp_soreceive_stream); 338 if (tcp_soreceive_stream) { 339 tcp_usrreqs.pru_soreceive = soreceive_stream; 340 tcp6_usrreqs.pru_soreceive = soreceive_stream; 341 } 342 #endif 343 344 #ifdef INET6 345 #define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)) 346 #else /* INET6 */ 347 #define TCP_MINPROTOHDR (sizeof(struct tcpiphdr)) 348 #endif /* INET6 */ 349 if (max_protohdr < TCP_MINPROTOHDR) 350 max_protohdr = TCP_MINPROTOHDR; 351 if (max_linkhdr + TCP_MINPROTOHDR > MHLEN) 352 panic("tcp_init"); 353 #undef TCP_MINPROTOHDR 354 355 ISN_LOCK_INIT(); 356 callout_init(&isn_callout, CALLOUT_MPSAFE); 357 callout_reset(&isn_callout, hz/100, tcp_isn_tick, NULL); 358 EVENTHANDLER_REGISTER(shutdown_pre_sync, tcp_fini, NULL, 359 SHUTDOWN_PRI_DEFAULT); 360 EVENTHANDLER_REGISTER(maxsockets_change, tcp_zone_change, NULL, 361 EVENTHANDLER_PRI_ANY); 362 } 363 364 #ifdef VIMAGE 365 void 366 tcp_destroy(void) 367 { 368 369 tcp_reass_destroy(); 370 tcp_hc_destroy(); 371 syncache_destroy(); 372 tcp_tw_destroy(); 373 in_pcbinfo_destroy(&V_tcbinfo); 374 uma_zdestroy(V_sack_hole_zone); 375 uma_zdestroy(V_tcpcb_zone); 376 } 377 #endif 378 379 void 380 tcp_fini(void *xtp) 381 { 382 383 callout_stop(&isn_callout); 384 } 385 386 /* 387 * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb. 388 * tcp_template used to store this data in mbufs, but we now recopy it out 389 * of the tcpcb each time to conserve mbufs. 390 */ 391 void 392 tcpip_fillheaders(struct inpcb *inp, void *ip_ptr, void *tcp_ptr) 393 { 394 struct tcphdr *th = (struct tcphdr *)tcp_ptr; 395 396 INP_WLOCK_ASSERT(inp); 397 398 #ifdef INET6 399 if ((inp->inp_vflag & INP_IPV6) != 0) { 400 struct ip6_hdr *ip6; 401 402 ip6 = (struct ip6_hdr *)ip_ptr; 403 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 404 (inp->inp_flow & IPV6_FLOWINFO_MASK); 405 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 406 (IPV6_VERSION & IPV6_VERSION_MASK); 407 ip6->ip6_nxt = IPPROTO_TCP; 408 ip6->ip6_plen = htons(sizeof(struct tcphdr)); 409 ip6->ip6_src = inp->in6p_laddr; 410 ip6->ip6_dst = inp->in6p_faddr; 411 } else 412 #endif 413 { 414 struct ip *ip; 415 416 ip = (struct ip *)ip_ptr; 417 ip->ip_v = IPVERSION; 418 ip->ip_hl = 5; 419 ip->ip_tos = inp->inp_ip_tos; 420 ip->ip_len = 0; 421 ip->ip_id = 0; 422 ip->ip_off = 0; 423 ip->ip_ttl = inp->inp_ip_ttl; 424 ip->ip_sum = 0; 425 ip->ip_p = IPPROTO_TCP; 426 ip->ip_src = inp->inp_laddr; 427 ip->ip_dst = inp->inp_faddr; 428 } 429 th->th_sport = inp->inp_lport; 430 th->th_dport = inp->inp_fport; 431 th->th_seq = 0; 432 th->th_ack = 0; 433 th->th_x2 = 0; 434 th->th_off = 5; 435 th->th_flags = 0; 436 th->th_win = 0; 437 th->th_urp = 0; 438 th->th_sum = 0; /* in_pseudo() is called later for ipv4 */ 439 } 440 441 /* 442 * Create template to be used to send tcp packets on a connection. 443 * Allocates an mbuf and fills in a skeletal tcp/ip header. The only 444 * use for this function is in keepalives, which use tcp_respond. 445 */ 446 struct tcptemp * 447 tcpip_maketemplate(struct inpcb *inp) 448 { 449 struct tcptemp *t; 450 451 t = malloc(sizeof(*t), M_TEMP, M_NOWAIT); 452 if (t == NULL) 453 return (NULL); 454 tcpip_fillheaders(inp, (void *)&t->tt_ipgen, (void *)&t->tt_t); 455 return (t); 456 } 457 458 /* 459 * Send a single message to the TCP at address specified by 460 * the given TCP/IP header. If m == NULL, then we make a copy 461 * of the tcpiphdr at ti and send directly to the addressed host. 462 * This is used to force keep alive messages out using the TCP 463 * template for a connection. If flags are given then we send 464 * a message back to the TCP which originated the * segment ti, 465 * and discard the mbuf containing it and any other attached mbufs. 466 * 467 * In any case the ack and sequence number of the transmitted 468 * segment are as specified by the parameters. 469 * 470 * NOTE: If m != NULL, then ti must point to *inside* the mbuf. 471 */ 472 void 473 tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m, 474 tcp_seq ack, tcp_seq seq, int flags) 475 { 476 int tlen; 477 int win = 0; 478 struct ip *ip; 479 struct tcphdr *nth; 480 #ifdef INET6 481 struct ip6_hdr *ip6; 482 int isipv6; 483 #endif /* INET6 */ 484 int ipflags = 0; 485 struct inpcb *inp; 486 487 KASSERT(tp != NULL || m != NULL, ("tcp_respond: tp and m both NULL")); 488 489 #ifdef INET6 490 isipv6 = ((struct ip *)ipgen)->ip_v == 6; 491 ip6 = ipgen; 492 #endif /* INET6 */ 493 ip = ipgen; 494 495 if (tp != NULL) { 496 inp = tp->t_inpcb; 497 KASSERT(inp != NULL, ("tcp control block w/o inpcb")); 498 INP_WLOCK_ASSERT(inp); 499 } else 500 inp = NULL; 501 502 if (tp != NULL) { 503 if (!(flags & TH_RST)) { 504 win = sbspace(&inp->inp_socket->so_rcv); 505 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 506 win = (long)TCP_MAXWIN << tp->rcv_scale; 507 } 508 } 509 if (m == NULL) { 510 m = m_gethdr(M_DONTWAIT, MT_DATA); 511 if (m == NULL) 512 return; 513 tlen = 0; 514 m->m_data += max_linkhdr; 515 #ifdef INET6 516 if (isipv6) { 517 bcopy((caddr_t)ip6, mtod(m, caddr_t), 518 sizeof(struct ip6_hdr)); 519 ip6 = mtod(m, struct ip6_hdr *); 520 nth = (struct tcphdr *)(ip6 + 1); 521 } else 522 #endif /* INET6 */ 523 { 524 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 525 ip = mtod(m, struct ip *); 526 nth = (struct tcphdr *)(ip + 1); 527 } 528 bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr)); 529 flags = TH_ACK; 530 } else { 531 /* 532 * reuse the mbuf. 533 * XXX MRT We inherrit the FIB, which is lucky. 534 */ 535 m_freem(m->m_next); 536 m->m_next = NULL; 537 m->m_data = (caddr_t)ipgen; 538 /* m_len is set later */ 539 tlen = 0; 540 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 541 #ifdef INET6 542 if (isipv6) { 543 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); 544 nth = (struct tcphdr *)(ip6 + 1); 545 } else 546 #endif /* INET6 */ 547 { 548 xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, uint32_t); 549 nth = (struct tcphdr *)(ip + 1); 550 } 551 if (th != nth) { 552 /* 553 * this is usually a case when an extension header 554 * exists between the IPv6 header and the 555 * TCP header. 556 */ 557 nth->th_sport = th->th_sport; 558 nth->th_dport = th->th_dport; 559 } 560 xchg(nth->th_dport, nth->th_sport, uint16_t); 561 #undef xchg 562 } 563 #ifdef INET6 564 if (isipv6) { 565 ip6->ip6_flow = 0; 566 ip6->ip6_vfc = IPV6_VERSION; 567 ip6->ip6_nxt = IPPROTO_TCP; 568 ip6->ip6_plen = htons((u_short)(sizeof (struct tcphdr) + 569 tlen)); 570 tlen += sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 571 } else 572 #endif 573 { 574 tlen += sizeof (struct tcpiphdr); 575 ip->ip_len = tlen; 576 ip->ip_ttl = V_ip_defttl; 577 if (V_path_mtu_discovery) 578 ip->ip_off |= IP_DF; 579 } 580 m->m_len = tlen; 581 m->m_pkthdr.len = tlen; 582 m->m_pkthdr.rcvif = NULL; 583 #ifdef MAC 584 if (inp != NULL) { 585 /* 586 * Packet is associated with a socket, so allow the 587 * label of the response to reflect the socket label. 588 */ 589 INP_WLOCK_ASSERT(inp); 590 mac_inpcb_create_mbuf(inp, m); 591 } else { 592 /* 593 * Packet is not associated with a socket, so possibly 594 * update the label in place. 595 */ 596 mac_netinet_tcp_reply(m); 597 } 598 #endif 599 nth->th_seq = htonl(seq); 600 nth->th_ack = htonl(ack); 601 nth->th_x2 = 0; 602 nth->th_off = sizeof (struct tcphdr) >> 2; 603 nth->th_flags = flags; 604 if (tp != NULL) 605 nth->th_win = htons((u_short) (win >> tp->rcv_scale)); 606 else 607 nth->th_win = htons((u_short)win); 608 nth->th_urp = 0; 609 #ifdef INET6 610 if (isipv6) { 611 nth->th_sum = 0; 612 nth->th_sum = in6_cksum(m, IPPROTO_TCP, 613 sizeof(struct ip6_hdr), 614 tlen - sizeof(struct ip6_hdr)); 615 ip6->ip6_hlim = in6_selecthlim(tp != NULL ? tp->t_inpcb : 616 NULL, NULL); 617 } else 618 #endif /* INET6 */ 619 { 620 nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 621 htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p))); 622 m->m_pkthdr.csum_flags = CSUM_TCP; 623 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 624 } 625 #ifdef TCPDEBUG 626 if (tp == NULL || (inp->inp_socket->so_options & SO_DEBUG)) 627 tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0); 628 #endif 629 #ifdef INET6 630 if (isipv6) 631 (void) ip6_output(m, NULL, NULL, ipflags, NULL, NULL, inp); 632 else 633 #endif /* INET6 */ 634 (void) ip_output(m, NULL, NULL, ipflags, NULL, inp); 635 } 636 637 /* 638 * Create a new TCP control block, making an 639 * empty reassembly queue and hooking it to the argument 640 * protocol control block. The `inp' parameter must have 641 * come from the zone allocator set up in tcp_init(). 642 */ 643 struct tcpcb * 644 tcp_newtcpcb(struct inpcb *inp) 645 { 646 struct tcpcb_mem *tm; 647 struct tcpcb *tp; 648 #ifdef INET6 649 int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 650 #endif /* INET6 */ 651 652 tm = uma_zalloc(V_tcpcb_zone, M_NOWAIT | M_ZERO); 653 if (tm == NULL) 654 return (NULL); 655 tp = &tm->tcb; 656 657 /* Initialise cc_var struct for this tcpcb. */ 658 tp->ccv = &tm->ccv; 659 tp->ccv->type = IPPROTO_TCP; 660 tp->ccv->ccvc.tcp = tp; 661 662 /* 663 * Use the current system default CC algorithm. 664 */ 665 CC_LIST_RLOCK(); 666 KASSERT(!STAILQ_EMPTY(&cc_list), ("cc_list is empty!")); 667 CC_ALGO(tp) = CC_DEFAULT(); 668 CC_LIST_RUNLOCK(); 669 670 if (CC_ALGO(tp)->cb_init != NULL) 671 if (CC_ALGO(tp)->cb_init(tp->ccv) > 0) { 672 uma_zfree(V_tcpcb_zone, tm); 673 return (NULL); 674 } 675 676 tp->osd = &tm->osd; 677 if (khelp_init_osd(HELPER_CLASS_TCP, tp->osd)) { 678 uma_zfree(V_tcpcb_zone, tm); 679 return (NULL); 680 } 681 682 #ifdef VIMAGE 683 tp->t_vnet = inp->inp_vnet; 684 #endif 685 tp->t_timers = &tm->tt; 686 /* LIST_INIT(&tp->t_segq); */ /* XXX covered by M_ZERO */ 687 tp->t_maxseg = tp->t_maxopd = 688 #ifdef INET6 689 isipv6 ? V_tcp_v6mssdflt : 690 #endif /* INET6 */ 691 V_tcp_mssdflt; 692 693 /* Set up our timeouts. */ 694 callout_init(&tp->t_timers->tt_rexmt, CALLOUT_MPSAFE); 695 callout_init(&tp->t_timers->tt_persist, CALLOUT_MPSAFE); 696 callout_init(&tp->t_timers->tt_keep, CALLOUT_MPSAFE); 697 callout_init(&tp->t_timers->tt_2msl, CALLOUT_MPSAFE); 698 callout_init(&tp->t_timers->tt_delack, CALLOUT_MPSAFE); 699 700 if (V_tcp_do_rfc1323) 701 tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP); 702 if (V_tcp_do_sack) 703 tp->t_flags |= TF_SACK_PERMIT; 704 TAILQ_INIT(&tp->snd_holes); 705 tp->t_inpcb = inp; /* XXX */ 706 /* 707 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 708 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives 709 * reasonable initial retransmit time. 710 */ 711 tp->t_srtt = TCPTV_SRTTBASE; 712 tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4; 713 tp->t_rttmin = tcp_rexmit_min; 714 tp->t_rxtcur = TCPTV_RTOBASE; 715 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 716 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 717 tp->t_rcvtime = ticks; 718 /* 719 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 720 * because the socket may be bound to an IPv6 wildcard address, 721 * which may match an IPv4-mapped IPv6 address. 722 */ 723 inp->inp_ip_ttl = V_ip_defttl; 724 inp->inp_ppcb = tp; 725 return (tp); /* XXX */ 726 } 727 728 /* 729 * Switch the congestion control algorithm back to NewReno for any active 730 * control blocks using an algorithm which is about to go away. 731 * This ensures the CC framework can allow the unload to proceed without leaving 732 * any dangling pointers which would trigger a panic. 733 * Returning non-zero would inform the CC framework that something went wrong 734 * and it would be unsafe to allow the unload to proceed. However, there is no 735 * way for this to occur with this implementation so we always return zero. 736 */ 737 int 738 tcp_ccalgounload(struct cc_algo *unload_algo) 739 { 740 struct cc_algo *tmpalgo; 741 struct inpcb *inp; 742 struct tcpcb *tp; 743 VNET_ITERATOR_DECL(vnet_iter); 744 745 /* 746 * Check all active control blocks across all network stacks and change 747 * any that are using "unload_algo" back to NewReno. If "unload_algo" 748 * requires cleanup code to be run, call it. 749 */ 750 VNET_LIST_RLOCK(); 751 VNET_FOREACH(vnet_iter) { 752 CURVNET_SET(vnet_iter); 753 INP_INFO_RLOCK(&V_tcbinfo); 754 /* 755 * New connections already part way through being initialised 756 * with the CC algo we're removing will not race with this code 757 * because the INP_INFO_WLOCK is held during initialisation. We 758 * therefore don't enter the loop below until the connection 759 * list has stabilised. 760 */ 761 LIST_FOREACH(inp, &V_tcb, inp_list) { 762 INP_WLOCK(inp); 763 /* Important to skip tcptw structs. */ 764 if (!(inp->inp_flags & INP_TIMEWAIT) && 765 (tp = intotcpcb(inp)) != NULL) { 766 /* 767 * By holding INP_WLOCK here, we are assured 768 * that the connection is not currently 769 * executing inside the CC module's functions 770 * i.e. it is safe to make the switch back to 771 * NewReno. 772 */ 773 if (CC_ALGO(tp) == unload_algo) { 774 tmpalgo = CC_ALGO(tp); 775 /* NewReno does not require any init. */ 776 CC_ALGO(tp) = &newreno_cc_algo; 777 if (tmpalgo->cb_destroy != NULL) 778 tmpalgo->cb_destroy(tp->ccv); 779 } 780 } 781 INP_WUNLOCK(inp); 782 } 783 INP_INFO_RUNLOCK(&V_tcbinfo); 784 CURVNET_RESTORE(); 785 } 786 VNET_LIST_RUNLOCK(); 787 788 return (0); 789 } 790 791 /* 792 * Drop a TCP connection, reporting 793 * the specified error. If connection is synchronized, 794 * then send a RST to peer. 795 */ 796 struct tcpcb * 797 tcp_drop(struct tcpcb *tp, int errno) 798 { 799 struct socket *so = tp->t_inpcb->inp_socket; 800 801 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 802 INP_WLOCK_ASSERT(tp->t_inpcb); 803 804 if (TCPS_HAVERCVDSYN(tp->t_state)) { 805 tp->t_state = TCPS_CLOSED; 806 (void) tcp_output_reset(tp); 807 TCPSTAT_INC(tcps_drops); 808 } else 809 TCPSTAT_INC(tcps_conndrops); 810 if (errno == ETIMEDOUT && tp->t_softerror) 811 errno = tp->t_softerror; 812 so->so_error = errno; 813 return (tcp_close(tp)); 814 } 815 816 void 817 tcp_discardcb(struct tcpcb *tp) 818 { 819 struct inpcb *inp = tp->t_inpcb; 820 struct socket *so = inp->inp_socket; 821 #ifdef INET6 822 int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 823 #endif /* INET6 */ 824 825 INP_WLOCK_ASSERT(inp); 826 827 /* 828 * Make sure that all of our timers are stopped before we delete the 829 * PCB. 830 * 831 * XXXRW: Really, we would like to use callout_drain() here in order 832 * to avoid races experienced in tcp_timer.c where a timer is already 833 * executing at this point. However, we can't, both because we're 834 * running in a context where we can't sleep, and also because we 835 * hold locks required by the timers. What we instead need to do is 836 * test to see if callout_drain() is required, and if so, defer some 837 * portion of the remainder of tcp_discardcb() to an asynchronous 838 * context that can callout_drain() and then continue. Some care 839 * will be required to ensure that no further processing takes place 840 * on the tcpcb, even though it hasn't been freed (a flag?). 841 */ 842 callout_stop(&tp->t_timers->tt_rexmt); 843 callout_stop(&tp->t_timers->tt_persist); 844 callout_stop(&tp->t_timers->tt_keep); 845 callout_stop(&tp->t_timers->tt_2msl); 846 callout_stop(&tp->t_timers->tt_delack); 847 848 /* 849 * If we got enough samples through the srtt filter, 850 * save the rtt and rttvar in the routing entry. 851 * 'Enough' is arbitrarily defined as 4 rtt samples. 852 * 4 samples is enough for the srtt filter to converge 853 * to within enough % of the correct value; fewer samples 854 * and we could save a bogus rtt. The danger is not high 855 * as tcp quickly recovers from everything. 856 * XXX: Works very well but needs some more statistics! 857 */ 858 if (tp->t_rttupdated >= 4) { 859 struct hc_metrics_lite metrics; 860 u_long ssthresh; 861 862 bzero(&metrics, sizeof(metrics)); 863 /* 864 * Update the ssthresh always when the conditions below 865 * are satisfied. This gives us better new start value 866 * for the congestion avoidance for new connections. 867 * ssthresh is only set if packet loss occured on a session. 868 * 869 * XXXRW: 'so' may be NULL here, and/or socket buffer may be 870 * being torn down. Ideally this code would not use 'so'. 871 */ 872 ssthresh = tp->snd_ssthresh; 873 if (ssthresh != 0 && ssthresh < so->so_snd.sb_hiwat / 2) { 874 /* 875 * convert the limit from user data bytes to 876 * packets then to packet data bytes. 877 */ 878 ssthresh = (ssthresh + tp->t_maxseg / 2) / tp->t_maxseg; 879 if (ssthresh < 2) 880 ssthresh = 2; 881 ssthresh *= (u_long)(tp->t_maxseg + 882 #ifdef INET6 883 (isipv6 ? sizeof (struct ip6_hdr) + 884 sizeof (struct tcphdr) : 885 #endif 886 sizeof (struct tcpiphdr) 887 #ifdef INET6 888 ) 889 #endif 890 ); 891 } else 892 ssthresh = 0; 893 metrics.rmx_ssthresh = ssthresh; 894 895 metrics.rmx_rtt = tp->t_srtt; 896 metrics.rmx_rttvar = tp->t_rttvar; 897 metrics.rmx_cwnd = tp->snd_cwnd; 898 metrics.rmx_sendpipe = 0; 899 metrics.rmx_recvpipe = 0; 900 901 tcp_hc_update(&inp->inp_inc, &metrics); 902 } 903 904 /* free the reassembly queue, if any */ 905 tcp_reass_flush(tp); 906 /* Disconnect offload device, if any. */ 907 tcp_offload_detach(tp); 908 909 tcp_free_sackholes(tp); 910 911 /* Allow the CC algorithm to clean up after itself. */ 912 if (CC_ALGO(tp)->cb_destroy != NULL) 913 CC_ALGO(tp)->cb_destroy(tp->ccv); 914 915 khelp_destroy_osd(tp->osd); 916 917 CC_ALGO(tp) = NULL; 918 inp->inp_ppcb = NULL; 919 tp->t_inpcb = NULL; 920 uma_zfree(V_tcpcb_zone, tp); 921 } 922 923 /* 924 * Attempt to close a TCP control block, marking it as dropped, and freeing 925 * the socket if we hold the only reference. 926 */ 927 struct tcpcb * 928 tcp_close(struct tcpcb *tp) 929 { 930 struct inpcb *inp = tp->t_inpcb; 931 struct socket *so; 932 933 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 934 INP_WLOCK_ASSERT(inp); 935 936 /* Notify any offload devices of listener close */ 937 if (tp->t_state == TCPS_LISTEN) 938 tcp_offload_listen_close(tp); 939 in_pcbdrop(inp); 940 TCPSTAT_INC(tcps_closed); 941 KASSERT(inp->inp_socket != NULL, ("tcp_close: inp_socket NULL")); 942 so = inp->inp_socket; 943 soisdisconnected(so); 944 if (inp->inp_flags & INP_SOCKREF) { 945 KASSERT(so->so_state & SS_PROTOREF, 946 ("tcp_close: !SS_PROTOREF")); 947 inp->inp_flags &= ~INP_SOCKREF; 948 INP_WUNLOCK(inp); 949 ACCEPT_LOCK(); 950 SOCK_LOCK(so); 951 so->so_state &= ~SS_PROTOREF; 952 sofree(so); 953 return (NULL); 954 } 955 return (tp); 956 } 957 958 void 959 tcp_drain(void) 960 { 961 VNET_ITERATOR_DECL(vnet_iter); 962 963 if (!do_tcpdrain) 964 return; 965 966 VNET_LIST_RLOCK_NOSLEEP(); 967 VNET_FOREACH(vnet_iter) { 968 CURVNET_SET(vnet_iter); 969 struct inpcb *inpb; 970 struct tcpcb *tcpb; 971 972 /* 973 * Walk the tcpbs, if existing, and flush the reassembly queue, 974 * if there is one... 975 * XXX: The "Net/3" implementation doesn't imply that the TCP 976 * reassembly queue should be flushed, but in a situation 977 * where we're really low on mbufs, this is potentially 978 * usefull. 979 */ 980 INP_INFO_RLOCK(&V_tcbinfo); 981 LIST_FOREACH(inpb, V_tcbinfo.ipi_listhead, inp_list) { 982 if (inpb->inp_flags & INP_TIMEWAIT) 983 continue; 984 INP_WLOCK(inpb); 985 if ((tcpb = intotcpcb(inpb)) != NULL) { 986 tcp_reass_flush(tcpb); 987 tcp_clean_sackreport(tcpb); 988 } 989 INP_WUNLOCK(inpb); 990 } 991 INP_INFO_RUNLOCK(&V_tcbinfo); 992 CURVNET_RESTORE(); 993 } 994 VNET_LIST_RUNLOCK_NOSLEEP(); 995 } 996 997 /* 998 * Notify a tcp user of an asynchronous error; 999 * store error as soft error, but wake up user 1000 * (for now, won't do anything until can select for soft error). 1001 * 1002 * Do not wake up user since there currently is no mechanism for 1003 * reporting soft errors (yet - a kqueue filter may be added). 1004 */ 1005 static struct inpcb * 1006 tcp_notify(struct inpcb *inp, int error) 1007 { 1008 struct tcpcb *tp; 1009 1010 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1011 INP_WLOCK_ASSERT(inp); 1012 1013 if ((inp->inp_flags & INP_TIMEWAIT) || 1014 (inp->inp_flags & INP_DROPPED)) 1015 return (inp); 1016 1017 tp = intotcpcb(inp); 1018 KASSERT(tp != NULL, ("tcp_notify: tp == NULL")); 1019 1020 /* 1021 * Ignore some errors if we are hooked up. 1022 * If connection hasn't completed, has retransmitted several times, 1023 * and receives a second error, give up now. This is better 1024 * than waiting a long time to establish a connection that 1025 * can never complete. 1026 */ 1027 if (tp->t_state == TCPS_ESTABLISHED && 1028 (error == EHOSTUNREACH || error == ENETUNREACH || 1029 error == EHOSTDOWN)) { 1030 return (inp); 1031 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 1032 tp->t_softerror) { 1033 tp = tcp_drop(tp, error); 1034 if (tp != NULL) 1035 return (inp); 1036 else 1037 return (NULL); 1038 } else { 1039 tp->t_softerror = error; 1040 return (inp); 1041 } 1042 #if 0 1043 wakeup( &so->so_timeo); 1044 sorwakeup(so); 1045 sowwakeup(so); 1046 #endif 1047 } 1048 1049 static int 1050 tcp_pcblist(SYSCTL_HANDLER_ARGS) 1051 { 1052 int error, i, m, n, pcb_count; 1053 struct inpcb *inp, **inp_list; 1054 inp_gen_t gencnt; 1055 struct xinpgen xig; 1056 1057 /* 1058 * The process of preparing the TCB list is too time-consuming and 1059 * resource-intensive to repeat twice on every request. 1060 */ 1061 if (req->oldptr == NULL) { 1062 n = V_tcbinfo.ipi_count + syncache_pcbcount(); 1063 n += imax(n / 8, 10); 1064 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xtcpcb); 1065 return (0); 1066 } 1067 1068 if (req->newptr != NULL) 1069 return (EPERM); 1070 1071 /* 1072 * OK, now we're committed to doing something. 1073 */ 1074 INP_INFO_RLOCK(&V_tcbinfo); 1075 gencnt = V_tcbinfo.ipi_gencnt; 1076 n = V_tcbinfo.ipi_count; 1077 INP_INFO_RUNLOCK(&V_tcbinfo); 1078 1079 m = syncache_pcbcount(); 1080 1081 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 1082 + (n + m) * sizeof(struct xtcpcb)); 1083 if (error != 0) 1084 return (error); 1085 1086 xig.xig_len = sizeof xig; 1087 xig.xig_count = n + m; 1088 xig.xig_gen = gencnt; 1089 xig.xig_sogen = so_gencnt; 1090 error = SYSCTL_OUT(req, &xig, sizeof xig); 1091 if (error) 1092 return (error); 1093 1094 error = syncache_pcblist(req, m, &pcb_count); 1095 if (error) 1096 return (error); 1097 1098 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 1099 if (inp_list == NULL) 1100 return (ENOMEM); 1101 1102 INP_INFO_RLOCK(&V_tcbinfo); 1103 for (inp = LIST_FIRST(V_tcbinfo.ipi_listhead), i = 0; 1104 inp != NULL && i < n; inp = LIST_NEXT(inp, inp_list)) { 1105 INP_WLOCK(inp); 1106 if (inp->inp_gencnt <= gencnt) { 1107 /* 1108 * XXX: This use of cr_cansee(), introduced with 1109 * TCP state changes, is not quite right, but for 1110 * now, better than nothing. 1111 */ 1112 if (inp->inp_flags & INP_TIMEWAIT) { 1113 if (intotw(inp) != NULL) 1114 error = cr_cansee(req->td->td_ucred, 1115 intotw(inp)->tw_cred); 1116 else 1117 error = EINVAL; /* Skip this inp. */ 1118 } else 1119 error = cr_canseeinpcb(req->td->td_ucred, inp); 1120 if (error == 0) { 1121 in_pcbref(inp); 1122 inp_list[i++] = inp; 1123 } 1124 } 1125 INP_WUNLOCK(inp); 1126 } 1127 INP_INFO_RUNLOCK(&V_tcbinfo); 1128 n = i; 1129 1130 error = 0; 1131 for (i = 0; i < n; i++) { 1132 inp = inp_list[i]; 1133 INP_RLOCK(inp); 1134 if (inp->inp_gencnt <= gencnt) { 1135 struct xtcpcb xt; 1136 void *inp_ppcb; 1137 1138 bzero(&xt, sizeof(xt)); 1139 xt.xt_len = sizeof xt; 1140 /* XXX should avoid extra copy */ 1141 bcopy(inp, &xt.xt_inp, sizeof *inp); 1142 inp_ppcb = inp->inp_ppcb; 1143 if (inp_ppcb == NULL) 1144 bzero((char *) &xt.xt_tp, sizeof xt.xt_tp); 1145 else if (inp->inp_flags & INP_TIMEWAIT) { 1146 bzero((char *) &xt.xt_tp, sizeof xt.xt_tp); 1147 xt.xt_tp.t_state = TCPS_TIME_WAIT; 1148 } else { 1149 bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp); 1150 if (xt.xt_tp.t_timers) 1151 tcp_timer_to_xtimer(&xt.xt_tp, xt.xt_tp.t_timers, &xt.xt_timer); 1152 } 1153 if (inp->inp_socket != NULL) 1154 sotoxsocket(inp->inp_socket, &xt.xt_socket); 1155 else { 1156 bzero(&xt.xt_socket, sizeof xt.xt_socket); 1157 xt.xt_socket.xso_protocol = IPPROTO_TCP; 1158 } 1159 xt.xt_inp.inp_gencnt = inp->inp_gencnt; 1160 INP_RUNLOCK(inp); 1161 error = SYSCTL_OUT(req, &xt, sizeof xt); 1162 } else 1163 INP_RUNLOCK(inp); 1164 } 1165 INP_INFO_WLOCK(&V_tcbinfo); 1166 for (i = 0; i < n; i++) { 1167 inp = inp_list[i]; 1168 INP_WLOCK(inp); 1169 if (!in_pcbrele(inp)) 1170 INP_WUNLOCK(inp); 1171 } 1172 INP_INFO_WUNLOCK(&V_tcbinfo); 1173 1174 if (!error) { 1175 /* 1176 * Give the user an updated idea of our state. 1177 * If the generation differs from what we told 1178 * her before, she knows that something happened 1179 * while we were processing this request, and it 1180 * might be necessary to retry. 1181 */ 1182 INP_INFO_RLOCK(&V_tcbinfo); 1183 xig.xig_gen = V_tcbinfo.ipi_gencnt; 1184 xig.xig_sogen = so_gencnt; 1185 xig.xig_count = V_tcbinfo.ipi_count + pcb_count; 1186 INP_INFO_RUNLOCK(&V_tcbinfo); 1187 error = SYSCTL_OUT(req, &xig, sizeof xig); 1188 } 1189 free(inp_list, M_TEMP); 1190 return (error); 1191 } 1192 1193 SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, 1194 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0, 1195 tcp_pcblist, "S,xtcpcb", "List of active TCP connections"); 1196 1197 static int 1198 tcp_getcred(SYSCTL_HANDLER_ARGS) 1199 { 1200 struct xucred xuc; 1201 struct sockaddr_in addrs[2]; 1202 struct inpcb *inp; 1203 int error; 1204 1205 error = priv_check(req->td, PRIV_NETINET_GETCRED); 1206 if (error) 1207 return (error); 1208 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 1209 if (error) 1210 return (error); 1211 INP_INFO_RLOCK(&V_tcbinfo); 1212 inp = in_pcblookup_hash(&V_tcbinfo, addrs[1].sin_addr, 1213 addrs[1].sin_port, addrs[0].sin_addr, addrs[0].sin_port, 0, NULL); 1214 if (inp != NULL) { 1215 INP_RLOCK(inp); 1216 INP_INFO_RUNLOCK(&V_tcbinfo); 1217 if (inp->inp_socket == NULL) 1218 error = ENOENT; 1219 if (error == 0) 1220 error = cr_canseeinpcb(req->td->td_ucred, inp); 1221 if (error == 0) 1222 cru2x(inp->inp_cred, &xuc); 1223 INP_RUNLOCK(inp); 1224 } else { 1225 INP_INFO_RUNLOCK(&V_tcbinfo); 1226 error = ENOENT; 1227 } 1228 if (error == 0) 1229 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 1230 return (error); 1231 } 1232 1233 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, 1234 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 1235 tcp_getcred, "S,xucred", "Get the xucred of a TCP connection"); 1236 1237 #ifdef INET6 1238 static int 1239 tcp6_getcred(SYSCTL_HANDLER_ARGS) 1240 { 1241 struct xucred xuc; 1242 struct sockaddr_in6 addrs[2]; 1243 struct inpcb *inp; 1244 int error, mapped = 0; 1245 1246 error = priv_check(req->td, PRIV_NETINET_GETCRED); 1247 if (error) 1248 return (error); 1249 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 1250 if (error) 1251 return (error); 1252 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 || 1253 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) { 1254 return (error); 1255 } 1256 if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) { 1257 if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr)) 1258 mapped = 1; 1259 else 1260 return (EINVAL); 1261 } 1262 1263 INP_INFO_RLOCK(&V_tcbinfo); 1264 if (mapped == 1) 1265 inp = in_pcblookup_hash(&V_tcbinfo, 1266 *(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12], 1267 addrs[1].sin6_port, 1268 *(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12], 1269 addrs[0].sin6_port, 1270 0, NULL); 1271 else 1272 inp = in6_pcblookup_hash(&V_tcbinfo, 1273 &addrs[1].sin6_addr, addrs[1].sin6_port, 1274 &addrs[0].sin6_addr, addrs[0].sin6_port, 0, NULL); 1275 if (inp != NULL) { 1276 INP_RLOCK(inp); 1277 INP_INFO_RUNLOCK(&V_tcbinfo); 1278 if (inp->inp_socket == NULL) 1279 error = ENOENT; 1280 if (error == 0) 1281 error = cr_canseeinpcb(req->td->td_ucred, inp); 1282 if (error == 0) 1283 cru2x(inp->inp_cred, &xuc); 1284 INP_RUNLOCK(inp); 1285 } else { 1286 INP_INFO_RUNLOCK(&V_tcbinfo); 1287 error = ENOENT; 1288 } 1289 if (error == 0) 1290 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 1291 return (error); 1292 } 1293 1294 SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, 1295 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 1296 tcp6_getcred, "S,xucred", "Get the xucred of a TCP6 connection"); 1297 #endif 1298 1299 1300 void 1301 tcp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 1302 { 1303 struct ip *ip = vip; 1304 struct tcphdr *th; 1305 struct in_addr faddr; 1306 struct inpcb *inp; 1307 struct tcpcb *tp; 1308 struct inpcb *(*notify)(struct inpcb *, int) = tcp_notify; 1309 struct icmp *icp; 1310 struct in_conninfo inc; 1311 tcp_seq icmp_tcp_seq; 1312 int mtu; 1313 1314 faddr = ((struct sockaddr_in *)sa)->sin_addr; 1315 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 1316 return; 1317 1318 if (cmd == PRC_MSGSIZE) 1319 notify = tcp_mtudisc; 1320 else if (V_icmp_may_rst && (cmd == PRC_UNREACH_ADMIN_PROHIB || 1321 cmd == PRC_UNREACH_PORT || cmd == PRC_TIMXCEED_INTRANS) && ip) 1322 notify = tcp_drop_syn_sent; 1323 /* 1324 * Redirects don't need to be handled up here. 1325 */ 1326 else if (PRC_IS_REDIRECT(cmd)) 1327 return; 1328 /* 1329 * Source quench is depreciated. 1330 */ 1331 else if (cmd == PRC_QUENCH) 1332 return; 1333 /* 1334 * Hostdead is ugly because it goes linearly through all PCBs. 1335 * XXX: We never get this from ICMP, otherwise it makes an 1336 * excellent DoS attack on machines with many connections. 1337 */ 1338 else if (cmd == PRC_HOSTDEAD) 1339 ip = NULL; 1340 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 1341 return; 1342 if (ip != NULL) { 1343 icp = (struct icmp *)((caddr_t)ip 1344 - offsetof(struct icmp, icmp_ip)); 1345 th = (struct tcphdr *)((caddr_t)ip 1346 + (ip->ip_hl << 2)); 1347 INP_INFO_WLOCK(&V_tcbinfo); 1348 inp = in_pcblookup_hash(&V_tcbinfo, faddr, th->th_dport, 1349 ip->ip_src, th->th_sport, 0, NULL); 1350 if (inp != NULL) { 1351 INP_WLOCK(inp); 1352 if (!(inp->inp_flags & INP_TIMEWAIT) && 1353 !(inp->inp_flags & INP_DROPPED) && 1354 !(inp->inp_socket == NULL)) { 1355 icmp_tcp_seq = htonl(th->th_seq); 1356 tp = intotcpcb(inp); 1357 if (SEQ_GEQ(icmp_tcp_seq, tp->snd_una) && 1358 SEQ_LT(icmp_tcp_seq, tp->snd_max)) { 1359 if (cmd == PRC_MSGSIZE) { 1360 /* 1361 * MTU discovery: 1362 * If we got a needfrag set the MTU 1363 * in the route to the suggested new 1364 * value (if given) and then notify. 1365 */ 1366 bzero(&inc, sizeof(inc)); 1367 inc.inc_faddr = faddr; 1368 inc.inc_fibnum = 1369 inp->inp_inc.inc_fibnum; 1370 1371 mtu = ntohs(icp->icmp_nextmtu); 1372 /* 1373 * If no alternative MTU was 1374 * proposed, try the next smaller 1375 * one. ip->ip_len has already 1376 * been swapped in icmp_input(). 1377 */ 1378 if (!mtu) 1379 mtu = ip_next_mtu(ip->ip_len, 1380 1); 1381 if (mtu < V_tcp_minmss 1382 + sizeof(struct tcpiphdr)) 1383 mtu = V_tcp_minmss 1384 + sizeof(struct tcpiphdr); 1385 /* 1386 * Only cache the MTU if it 1387 * is smaller than the interface 1388 * or route MTU. tcp_mtudisc() 1389 * will do right thing by itself. 1390 */ 1391 if (mtu <= tcp_maxmtu(&inc, NULL)) 1392 tcp_hc_updatemtu(&inc, mtu); 1393 } 1394 1395 inp = (*notify)(inp, inetctlerrmap[cmd]); 1396 } 1397 } 1398 if (inp != NULL) 1399 INP_WUNLOCK(inp); 1400 } else { 1401 bzero(&inc, sizeof(inc)); 1402 inc.inc_fport = th->th_dport; 1403 inc.inc_lport = th->th_sport; 1404 inc.inc_faddr = faddr; 1405 inc.inc_laddr = ip->ip_src; 1406 syncache_unreach(&inc, th); 1407 } 1408 INP_INFO_WUNLOCK(&V_tcbinfo); 1409 } else 1410 in_pcbnotifyall(&V_tcbinfo, faddr, inetctlerrmap[cmd], notify); 1411 } 1412 1413 #ifdef INET6 1414 void 1415 tcp6_ctlinput(int cmd, struct sockaddr *sa, void *d) 1416 { 1417 struct tcphdr th; 1418 struct inpcb *(*notify)(struct inpcb *, int) = tcp_notify; 1419 struct ip6_hdr *ip6; 1420 struct mbuf *m; 1421 struct ip6ctlparam *ip6cp = NULL; 1422 const struct sockaddr_in6 *sa6_src = NULL; 1423 int off; 1424 struct tcp_portonly { 1425 u_int16_t th_sport; 1426 u_int16_t th_dport; 1427 } *thp; 1428 1429 if (sa->sa_family != AF_INET6 || 1430 sa->sa_len != sizeof(struct sockaddr_in6)) 1431 return; 1432 1433 if (cmd == PRC_MSGSIZE) 1434 notify = tcp_mtudisc; 1435 else if (!PRC_IS_REDIRECT(cmd) && 1436 ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0)) 1437 return; 1438 /* Source quench is depreciated. */ 1439 else if (cmd == PRC_QUENCH) 1440 return; 1441 1442 /* if the parameter is from icmp6, decode it. */ 1443 if (d != NULL) { 1444 ip6cp = (struct ip6ctlparam *)d; 1445 m = ip6cp->ip6c_m; 1446 ip6 = ip6cp->ip6c_ip6; 1447 off = ip6cp->ip6c_off; 1448 sa6_src = ip6cp->ip6c_src; 1449 } else { 1450 m = NULL; 1451 ip6 = NULL; 1452 off = 0; /* fool gcc */ 1453 sa6_src = &sa6_any; 1454 } 1455 1456 if (ip6 != NULL) { 1457 struct in_conninfo inc; 1458 /* 1459 * XXX: We assume that when IPV6 is non NULL, 1460 * M and OFF are valid. 1461 */ 1462 1463 /* check if we can safely examine src and dst ports */ 1464 if (m->m_pkthdr.len < off + sizeof(*thp)) 1465 return; 1466 1467 bzero(&th, sizeof(th)); 1468 m_copydata(m, off, sizeof(*thp), (caddr_t)&th); 1469 1470 in6_pcbnotify(&V_tcbinfo, sa, th.th_dport, 1471 (struct sockaddr *)ip6cp->ip6c_src, 1472 th.th_sport, cmd, NULL, notify); 1473 1474 bzero(&inc, sizeof(inc)); 1475 inc.inc_fport = th.th_dport; 1476 inc.inc_lport = th.th_sport; 1477 inc.inc6_faddr = ((struct sockaddr_in6 *)sa)->sin6_addr; 1478 inc.inc6_laddr = ip6cp->ip6c_src->sin6_addr; 1479 inc.inc_flags |= INC_ISIPV6; 1480 INP_INFO_WLOCK(&V_tcbinfo); 1481 syncache_unreach(&inc, &th); 1482 INP_INFO_WUNLOCK(&V_tcbinfo); 1483 } else 1484 in6_pcbnotify(&V_tcbinfo, sa, 0, (const struct sockaddr *)sa6_src, 1485 0, cmd, NULL, notify); 1486 } 1487 #endif /* INET6 */ 1488 1489 1490 /* 1491 * Following is where TCP initial sequence number generation occurs. 1492 * 1493 * There are two places where we must use initial sequence numbers: 1494 * 1. In SYN-ACK packets. 1495 * 2. In SYN packets. 1496 * 1497 * All ISNs for SYN-ACK packets are generated by the syncache. See 1498 * tcp_syncache.c for details. 1499 * 1500 * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling 1501 * depends on this property. In addition, these ISNs should be 1502 * unguessable so as to prevent connection hijacking. To satisfy 1503 * the requirements of this situation, the algorithm outlined in 1504 * RFC 1948 is used, with only small modifications. 1505 * 1506 * Implementation details: 1507 * 1508 * Time is based off the system timer, and is corrected so that it 1509 * increases by one megabyte per second. This allows for proper 1510 * recycling on high speed LANs while still leaving over an hour 1511 * before rollover. 1512 * 1513 * As reading the *exact* system time is too expensive to be done 1514 * whenever setting up a TCP connection, we increment the time 1515 * offset in two ways. First, a small random positive increment 1516 * is added to isn_offset for each connection that is set up. 1517 * Second, the function tcp_isn_tick fires once per clock tick 1518 * and increments isn_offset as necessary so that sequence numbers 1519 * are incremented at approximately ISN_BYTES_PER_SECOND. The 1520 * random positive increments serve only to ensure that the same 1521 * exact sequence number is never sent out twice (as could otherwise 1522 * happen when a port is recycled in less than the system tick 1523 * interval.) 1524 * 1525 * net.inet.tcp.isn_reseed_interval controls the number of seconds 1526 * between seeding of isn_secret. This is normally set to zero, 1527 * as reseeding should not be necessary. 1528 * 1529 * Locking of the global variables isn_secret, isn_last_reseed, isn_offset, 1530 * isn_offset_old, and isn_ctx is performed using the TCP pcbinfo lock. In 1531 * general, this means holding an exclusive (write) lock. 1532 */ 1533 1534 #define ISN_BYTES_PER_SECOND 1048576 1535 #define ISN_STATIC_INCREMENT 4096 1536 #define ISN_RANDOM_INCREMENT (4096 - 1) 1537 1538 static VNET_DEFINE(u_char, isn_secret[32]); 1539 static VNET_DEFINE(int, isn_last_reseed); 1540 static VNET_DEFINE(u_int32_t, isn_offset); 1541 static VNET_DEFINE(u_int32_t, isn_offset_old); 1542 1543 #define V_isn_secret VNET(isn_secret) 1544 #define V_isn_last_reseed VNET(isn_last_reseed) 1545 #define V_isn_offset VNET(isn_offset) 1546 #define V_isn_offset_old VNET(isn_offset_old) 1547 1548 tcp_seq 1549 tcp_new_isn(struct tcpcb *tp) 1550 { 1551 MD5_CTX isn_ctx; 1552 u_int32_t md5_buffer[4]; 1553 tcp_seq new_isn; 1554 1555 INP_WLOCK_ASSERT(tp->t_inpcb); 1556 1557 ISN_LOCK(); 1558 /* Seed if this is the first use, reseed if requested. */ 1559 if ((V_isn_last_reseed == 0) || ((V_tcp_isn_reseed_interval > 0) && 1560 (((u_int)V_isn_last_reseed + (u_int)V_tcp_isn_reseed_interval*hz) 1561 < (u_int)ticks))) { 1562 read_random(&V_isn_secret, sizeof(V_isn_secret)); 1563 V_isn_last_reseed = ticks; 1564 } 1565 1566 /* Compute the md5 hash and return the ISN. */ 1567 MD5Init(&isn_ctx); 1568 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_fport, sizeof(u_short)); 1569 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_lport, sizeof(u_short)); 1570 #ifdef INET6 1571 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) { 1572 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_faddr, 1573 sizeof(struct in6_addr)); 1574 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_laddr, 1575 sizeof(struct in6_addr)); 1576 } else 1577 #endif 1578 { 1579 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_faddr, 1580 sizeof(struct in_addr)); 1581 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_laddr, 1582 sizeof(struct in_addr)); 1583 } 1584 MD5Update(&isn_ctx, (u_char *) &V_isn_secret, sizeof(V_isn_secret)); 1585 MD5Final((u_char *) &md5_buffer, &isn_ctx); 1586 new_isn = (tcp_seq) md5_buffer[0]; 1587 V_isn_offset += ISN_STATIC_INCREMENT + 1588 (arc4random() & ISN_RANDOM_INCREMENT); 1589 new_isn += V_isn_offset; 1590 ISN_UNLOCK(); 1591 return (new_isn); 1592 } 1593 1594 /* 1595 * Increment the offset to the next ISN_BYTES_PER_SECOND / 100 boundary 1596 * to keep time flowing at a relatively constant rate. If the random 1597 * increments have already pushed us past the projected offset, do nothing. 1598 */ 1599 static void 1600 tcp_isn_tick(void *xtp) 1601 { 1602 VNET_ITERATOR_DECL(vnet_iter); 1603 u_int32_t projected_offset; 1604 1605 VNET_LIST_RLOCK_NOSLEEP(); 1606 ISN_LOCK(); 1607 VNET_FOREACH(vnet_iter) { 1608 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS */ 1609 projected_offset = 1610 V_isn_offset_old + ISN_BYTES_PER_SECOND / 100; 1611 1612 if (SEQ_GT(projected_offset, V_isn_offset)) 1613 V_isn_offset = projected_offset; 1614 1615 V_isn_offset_old = V_isn_offset; 1616 CURVNET_RESTORE(); 1617 } 1618 ISN_UNLOCK(); 1619 VNET_LIST_RUNLOCK_NOSLEEP(); 1620 callout_reset(&isn_callout, hz/100, tcp_isn_tick, NULL); 1621 } 1622 1623 /* 1624 * When a specific ICMP unreachable message is received and the 1625 * connection state is SYN-SENT, drop the connection. This behavior 1626 * is controlled by the icmp_may_rst sysctl. 1627 */ 1628 struct inpcb * 1629 tcp_drop_syn_sent(struct inpcb *inp, int errno) 1630 { 1631 struct tcpcb *tp; 1632 1633 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1634 INP_WLOCK_ASSERT(inp); 1635 1636 if ((inp->inp_flags & INP_TIMEWAIT) || 1637 (inp->inp_flags & INP_DROPPED)) 1638 return (inp); 1639 1640 tp = intotcpcb(inp); 1641 if (tp->t_state != TCPS_SYN_SENT) 1642 return (inp); 1643 1644 tp = tcp_drop(tp, errno); 1645 if (tp != NULL) 1646 return (inp); 1647 else 1648 return (NULL); 1649 } 1650 1651 /* 1652 * When `need fragmentation' ICMP is received, update our idea of the MSS 1653 * based on the new value in the route. Also nudge TCP to send something, 1654 * since we know the packet we just sent was dropped. 1655 * This duplicates some code in the tcp_mss() function in tcp_input.c. 1656 */ 1657 struct inpcb * 1658 tcp_mtudisc(struct inpcb *inp, int errno) 1659 { 1660 struct tcpcb *tp; 1661 struct socket *so; 1662 1663 INP_WLOCK_ASSERT(inp); 1664 if ((inp->inp_flags & INP_TIMEWAIT) || 1665 (inp->inp_flags & INP_DROPPED)) 1666 return (inp); 1667 1668 tp = intotcpcb(inp); 1669 KASSERT(tp != NULL, ("tcp_mtudisc: tp == NULL")); 1670 1671 tcp_mss_update(tp, -1, NULL, NULL); 1672 1673 so = inp->inp_socket; 1674 SOCKBUF_LOCK(&so->so_snd); 1675 /* If the mss is larger than the socket buffer, decrease the mss. */ 1676 if (so->so_snd.sb_hiwat < tp->t_maxseg) 1677 tp->t_maxseg = so->so_snd.sb_hiwat; 1678 SOCKBUF_UNLOCK(&so->so_snd); 1679 1680 TCPSTAT_INC(tcps_mturesent); 1681 tp->t_rtttime = 0; 1682 tp->snd_nxt = tp->snd_una; 1683 tcp_free_sackholes(tp); 1684 tp->snd_recover = tp->snd_max; 1685 if (tp->t_flags & TF_SACK_PERMIT) 1686 EXIT_FASTRECOVERY(tp->t_flags); 1687 tcp_output_send(tp); 1688 return (inp); 1689 } 1690 1691 /* 1692 * Look-up the routing entry to the peer of this inpcb. If no route 1693 * is found and it cannot be allocated, then return 0. This routine 1694 * is called by TCP routines that access the rmx structure and by 1695 * tcp_mss_update to get the peer/interface MTU. 1696 */ 1697 u_long 1698 tcp_maxmtu(struct in_conninfo *inc, int *flags) 1699 { 1700 struct route sro; 1701 struct sockaddr_in *dst; 1702 struct ifnet *ifp; 1703 u_long maxmtu = 0; 1704 1705 KASSERT(inc != NULL, ("tcp_maxmtu with NULL in_conninfo pointer")); 1706 1707 bzero(&sro, sizeof(sro)); 1708 if (inc->inc_faddr.s_addr != INADDR_ANY) { 1709 dst = (struct sockaddr_in *)&sro.ro_dst; 1710 dst->sin_family = AF_INET; 1711 dst->sin_len = sizeof(*dst); 1712 dst->sin_addr = inc->inc_faddr; 1713 in_rtalloc_ign(&sro, 0, inc->inc_fibnum); 1714 } 1715 if (sro.ro_rt != NULL) { 1716 ifp = sro.ro_rt->rt_ifp; 1717 if (sro.ro_rt->rt_rmx.rmx_mtu == 0) 1718 maxmtu = ifp->if_mtu; 1719 else 1720 maxmtu = min(sro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu); 1721 1722 /* Report additional interface capabilities. */ 1723 if (flags != NULL) { 1724 if (ifp->if_capenable & IFCAP_TSO4 && 1725 ifp->if_hwassist & CSUM_TSO) 1726 *flags |= CSUM_TSO; 1727 } 1728 RTFREE(sro.ro_rt); 1729 } 1730 return (maxmtu); 1731 } 1732 1733 #ifdef INET6 1734 u_long 1735 tcp_maxmtu6(struct in_conninfo *inc, int *flags) 1736 { 1737 struct route_in6 sro6; 1738 struct ifnet *ifp; 1739 u_long maxmtu = 0; 1740 1741 KASSERT(inc != NULL, ("tcp_maxmtu6 with NULL in_conninfo pointer")); 1742 1743 bzero(&sro6, sizeof(sro6)); 1744 if (!IN6_IS_ADDR_UNSPECIFIED(&inc->inc6_faddr)) { 1745 sro6.ro_dst.sin6_family = AF_INET6; 1746 sro6.ro_dst.sin6_len = sizeof(struct sockaddr_in6); 1747 sro6.ro_dst.sin6_addr = inc->inc6_faddr; 1748 rtalloc_ign((struct route *)&sro6, 0); 1749 } 1750 if (sro6.ro_rt != NULL) { 1751 ifp = sro6.ro_rt->rt_ifp; 1752 if (sro6.ro_rt->rt_rmx.rmx_mtu == 0) 1753 maxmtu = IN6_LINKMTU(sro6.ro_rt->rt_ifp); 1754 else 1755 maxmtu = min(sro6.ro_rt->rt_rmx.rmx_mtu, 1756 IN6_LINKMTU(sro6.ro_rt->rt_ifp)); 1757 1758 /* Report additional interface capabilities. */ 1759 if (flags != NULL) { 1760 if (ifp->if_capenable & IFCAP_TSO6 && 1761 ifp->if_hwassist & CSUM_TSO) 1762 *flags |= CSUM_TSO; 1763 } 1764 RTFREE(sro6.ro_rt); 1765 } 1766 1767 return (maxmtu); 1768 } 1769 #endif /* INET6 */ 1770 1771 #ifdef IPSEC 1772 /* compute ESP/AH header size for TCP, including outer IP header. */ 1773 size_t 1774 ipsec_hdrsiz_tcp(struct tcpcb *tp) 1775 { 1776 struct inpcb *inp; 1777 struct mbuf *m; 1778 size_t hdrsiz; 1779 struct ip *ip; 1780 #ifdef INET6 1781 struct ip6_hdr *ip6; 1782 #endif 1783 struct tcphdr *th; 1784 1785 if ((tp == NULL) || ((inp = tp->t_inpcb) == NULL)) 1786 return (0); 1787 MGETHDR(m, M_DONTWAIT, MT_DATA); 1788 if (!m) 1789 return (0); 1790 1791 #ifdef INET6 1792 if ((inp->inp_vflag & INP_IPV6) != 0) { 1793 ip6 = mtod(m, struct ip6_hdr *); 1794 th = (struct tcphdr *)(ip6 + 1); 1795 m->m_pkthdr.len = m->m_len = 1796 sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 1797 tcpip_fillheaders(inp, ip6, th); 1798 hdrsiz = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); 1799 } else 1800 #endif /* INET6 */ 1801 { 1802 ip = mtod(m, struct ip *); 1803 th = (struct tcphdr *)(ip + 1); 1804 m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr); 1805 tcpip_fillheaders(inp, ip, th); 1806 hdrsiz = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); 1807 } 1808 1809 m_free(m); 1810 return (hdrsiz); 1811 } 1812 #endif /* IPSEC */ 1813 1814 #ifdef TCP_SIGNATURE 1815 /* 1816 * Callback function invoked by m_apply() to digest TCP segment data 1817 * contained within an mbuf chain. 1818 */ 1819 static int 1820 tcp_signature_apply(void *fstate, void *data, u_int len) 1821 { 1822 1823 MD5Update(fstate, (u_char *)data, len); 1824 return (0); 1825 } 1826 1827 /* 1828 * Compute TCP-MD5 hash of a TCP segment. (RFC2385) 1829 * 1830 * Parameters: 1831 * m pointer to head of mbuf chain 1832 * _unused 1833 * len length of TCP segment data, excluding options 1834 * optlen length of TCP segment options 1835 * buf pointer to storage for computed MD5 digest 1836 * direction direction of flow (IPSEC_DIR_INBOUND or OUTBOUND) 1837 * 1838 * We do this over ip, tcphdr, segment data, and the key in the SADB. 1839 * When called from tcp_input(), we can be sure that th_sum has been 1840 * zeroed out and verified already. 1841 * 1842 * Return 0 if successful, otherwise return -1. 1843 * 1844 * XXX The key is retrieved from the system's PF_KEY SADB, by keying a 1845 * search with the destination IP address, and a 'magic SPI' to be 1846 * determined by the application. This is hardcoded elsewhere to 1179 1847 * right now. Another branch of this code exists which uses the SPD to 1848 * specify per-application flows but it is unstable. 1849 */ 1850 int 1851 tcp_signature_compute(struct mbuf *m, int _unused, int len, int optlen, 1852 u_char *buf, u_int direction) 1853 { 1854 union sockaddr_union dst; 1855 struct ippseudo ippseudo; 1856 MD5_CTX ctx; 1857 int doff; 1858 struct ip *ip; 1859 struct ipovly *ipovly; 1860 struct secasvar *sav; 1861 struct tcphdr *th; 1862 #ifdef INET6 1863 struct ip6_hdr *ip6; 1864 struct in6_addr in6; 1865 char ip6buf[INET6_ADDRSTRLEN]; 1866 uint32_t plen; 1867 uint16_t nhdr; 1868 #endif 1869 u_short savecsum; 1870 1871 KASSERT(m != NULL, ("NULL mbuf chain")); 1872 KASSERT(buf != NULL, ("NULL signature pointer")); 1873 1874 /* Extract the destination from the IP header in the mbuf. */ 1875 bzero(&dst, sizeof(union sockaddr_union)); 1876 ip = mtod(m, struct ip *); 1877 #ifdef INET6 1878 ip6 = NULL; /* Make the compiler happy. */ 1879 #endif 1880 switch (ip->ip_v) { 1881 case IPVERSION: 1882 dst.sa.sa_len = sizeof(struct sockaddr_in); 1883 dst.sa.sa_family = AF_INET; 1884 dst.sin.sin_addr = (direction == IPSEC_DIR_INBOUND) ? 1885 ip->ip_src : ip->ip_dst; 1886 break; 1887 #ifdef INET6 1888 case (IPV6_VERSION >> 4): 1889 ip6 = mtod(m, struct ip6_hdr *); 1890 dst.sa.sa_len = sizeof(struct sockaddr_in6); 1891 dst.sa.sa_family = AF_INET6; 1892 dst.sin6.sin6_addr = (direction == IPSEC_DIR_INBOUND) ? 1893 ip6->ip6_src : ip6->ip6_dst; 1894 break; 1895 #endif 1896 default: 1897 return (EINVAL); 1898 /* NOTREACHED */ 1899 break; 1900 } 1901 1902 /* Look up an SADB entry which matches the address of the peer. */ 1903 sav = KEY_ALLOCSA(&dst, IPPROTO_TCP, htonl(TCP_SIG_SPI)); 1904 if (sav == NULL) { 1905 ipseclog((LOG_ERR, "%s: SADB lookup failed for %s\n", __func__, 1906 (ip->ip_v == IPVERSION) ? inet_ntoa(dst.sin.sin_addr) : 1907 #ifdef INET6 1908 (ip->ip_v == (IPV6_VERSION >> 4)) ? 1909 ip6_sprintf(ip6buf, &dst.sin6.sin6_addr) : 1910 #endif 1911 "(unsupported)")); 1912 return (EINVAL); 1913 } 1914 1915 MD5Init(&ctx); 1916 /* 1917 * Step 1: Update MD5 hash with IP(v6) pseudo-header. 1918 * 1919 * XXX The ippseudo header MUST be digested in network byte order, 1920 * or else we'll fail the regression test. Assume all fields we've 1921 * been doing arithmetic on have been in host byte order. 1922 * XXX One cannot depend on ipovly->ih_len here. When called from 1923 * tcp_output(), the underlying ip_len member has not yet been set. 1924 */ 1925 switch (ip->ip_v) { 1926 case IPVERSION: 1927 ipovly = (struct ipovly *)ip; 1928 ippseudo.ippseudo_src = ipovly->ih_src; 1929 ippseudo.ippseudo_dst = ipovly->ih_dst; 1930 ippseudo.ippseudo_pad = 0; 1931 ippseudo.ippseudo_p = IPPROTO_TCP; 1932 ippseudo.ippseudo_len = htons(len + sizeof(struct tcphdr) + 1933 optlen); 1934 MD5Update(&ctx, (char *)&ippseudo, sizeof(struct ippseudo)); 1935 1936 th = (struct tcphdr *)((u_char *)ip + sizeof(struct ip)); 1937 doff = sizeof(struct ip) + sizeof(struct tcphdr) + optlen; 1938 break; 1939 #ifdef INET6 1940 /* 1941 * RFC 2385, 2.0 Proposal 1942 * For IPv6, the pseudo-header is as described in RFC 2460, namely the 1943 * 128-bit source IPv6 address, 128-bit destination IPv6 address, zero- 1944 * extended next header value (to form 32 bits), and 32-bit segment 1945 * length. 1946 * Note: Upper-Layer Packet Length comes before Next Header. 1947 */ 1948 case (IPV6_VERSION >> 4): 1949 in6 = ip6->ip6_src; 1950 in6_clearscope(&in6); 1951 MD5Update(&ctx, (char *)&in6, sizeof(struct in6_addr)); 1952 in6 = ip6->ip6_dst; 1953 in6_clearscope(&in6); 1954 MD5Update(&ctx, (char *)&in6, sizeof(struct in6_addr)); 1955 plen = htonl(len + sizeof(struct tcphdr) + optlen); 1956 MD5Update(&ctx, (char *)&plen, sizeof(uint32_t)); 1957 nhdr = 0; 1958 MD5Update(&ctx, (char *)&nhdr, sizeof(uint8_t)); 1959 MD5Update(&ctx, (char *)&nhdr, sizeof(uint8_t)); 1960 MD5Update(&ctx, (char *)&nhdr, sizeof(uint8_t)); 1961 nhdr = IPPROTO_TCP; 1962 MD5Update(&ctx, (char *)&nhdr, sizeof(uint8_t)); 1963 1964 th = (struct tcphdr *)((u_char *)ip6 + sizeof(struct ip6_hdr)); 1965 doff = sizeof(struct ip6_hdr) + sizeof(struct tcphdr) + optlen; 1966 break; 1967 #endif 1968 default: 1969 return (EINVAL); 1970 /* NOTREACHED */ 1971 break; 1972 } 1973 1974 1975 /* 1976 * Step 2: Update MD5 hash with TCP header, excluding options. 1977 * The TCP checksum must be set to zero. 1978 */ 1979 savecsum = th->th_sum; 1980 th->th_sum = 0; 1981 MD5Update(&ctx, (char *)th, sizeof(struct tcphdr)); 1982 th->th_sum = savecsum; 1983 1984 /* 1985 * Step 3: Update MD5 hash with TCP segment data. 1986 * Use m_apply() to avoid an early m_pullup(). 1987 */ 1988 if (len > 0) 1989 m_apply(m, doff, len, tcp_signature_apply, &ctx); 1990 1991 /* 1992 * Step 4: Update MD5 hash with shared secret. 1993 */ 1994 MD5Update(&ctx, sav->key_auth->key_data, _KEYLEN(sav->key_auth)); 1995 MD5Final(buf, &ctx); 1996 1997 key_sa_recordxfer(sav, m); 1998 KEY_FREESAV(&sav); 1999 return (0); 2000 } 2001 #endif /* TCP_SIGNATURE */ 2002 2003 static int 2004 sysctl_drop(SYSCTL_HANDLER_ARGS) 2005 { 2006 /* addrs[0] is a foreign socket, addrs[1] is a local one. */ 2007 struct sockaddr_storage addrs[2]; 2008 struct inpcb *inp; 2009 struct tcpcb *tp; 2010 struct tcptw *tw; 2011 struct sockaddr_in *fin, *lin; 2012 #ifdef INET6 2013 struct sockaddr_in6 *fin6, *lin6; 2014 #endif 2015 int error; 2016 2017 inp = NULL; 2018 fin = lin = NULL; 2019 #ifdef INET6 2020 fin6 = lin6 = NULL; 2021 #endif 2022 error = 0; 2023 2024 if (req->oldptr != NULL || req->oldlen != 0) 2025 return (EINVAL); 2026 if (req->newptr == NULL) 2027 return (EPERM); 2028 if (req->newlen < sizeof(addrs)) 2029 return (ENOMEM); 2030 error = SYSCTL_IN(req, &addrs, sizeof(addrs)); 2031 if (error) 2032 return (error); 2033 2034 switch (addrs[0].ss_family) { 2035 #ifdef INET6 2036 case AF_INET6: 2037 fin6 = (struct sockaddr_in6 *)&addrs[0]; 2038 lin6 = (struct sockaddr_in6 *)&addrs[1]; 2039 if (fin6->sin6_len != sizeof(struct sockaddr_in6) || 2040 lin6->sin6_len != sizeof(struct sockaddr_in6)) 2041 return (EINVAL); 2042 if (IN6_IS_ADDR_V4MAPPED(&fin6->sin6_addr)) { 2043 if (!IN6_IS_ADDR_V4MAPPED(&lin6->sin6_addr)) 2044 return (EINVAL); 2045 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[0]); 2046 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[1]); 2047 fin = (struct sockaddr_in *)&addrs[0]; 2048 lin = (struct sockaddr_in *)&addrs[1]; 2049 break; 2050 } 2051 error = sa6_embedscope(fin6, V_ip6_use_defzone); 2052 if (error) 2053 return (error); 2054 error = sa6_embedscope(lin6, V_ip6_use_defzone); 2055 if (error) 2056 return (error); 2057 break; 2058 #endif 2059 case AF_INET: 2060 fin = (struct sockaddr_in *)&addrs[0]; 2061 lin = (struct sockaddr_in *)&addrs[1]; 2062 if (fin->sin_len != sizeof(struct sockaddr_in) || 2063 lin->sin_len != sizeof(struct sockaddr_in)) 2064 return (EINVAL); 2065 break; 2066 default: 2067 return (EINVAL); 2068 } 2069 INP_INFO_WLOCK(&V_tcbinfo); 2070 switch (addrs[0].ss_family) { 2071 #ifdef INET6 2072 case AF_INET6: 2073 inp = in6_pcblookup_hash(&V_tcbinfo, &fin6->sin6_addr, 2074 fin6->sin6_port, &lin6->sin6_addr, lin6->sin6_port, 0, 2075 NULL); 2076 break; 2077 #endif 2078 case AF_INET: 2079 inp = in_pcblookup_hash(&V_tcbinfo, fin->sin_addr, 2080 fin->sin_port, lin->sin_addr, lin->sin_port, 0, NULL); 2081 break; 2082 } 2083 if (inp != NULL) { 2084 INP_WLOCK(inp); 2085 if (inp->inp_flags & INP_TIMEWAIT) { 2086 /* 2087 * XXXRW: There currently exists a state where an 2088 * inpcb is present, but its timewait state has been 2089 * discarded. For now, don't allow dropping of this 2090 * type of inpcb. 2091 */ 2092 tw = intotw(inp); 2093 if (tw != NULL) 2094 tcp_twclose(tw, 0); 2095 else 2096 INP_WUNLOCK(inp); 2097 } else if (!(inp->inp_flags & INP_DROPPED) && 2098 !(inp->inp_socket->so_options & SO_ACCEPTCONN)) { 2099 tp = intotcpcb(inp); 2100 tp = tcp_drop(tp, ECONNABORTED); 2101 if (tp != NULL) 2102 INP_WUNLOCK(inp); 2103 } else 2104 INP_WUNLOCK(inp); 2105 } else 2106 error = ESRCH; 2107 INP_INFO_WUNLOCK(&V_tcbinfo); 2108 return (error); 2109 } 2110 2111 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DROP, drop, 2112 CTLTYPE_STRUCT|CTLFLAG_WR|CTLFLAG_SKIP, NULL, 2113 0, sysctl_drop, "", "Drop TCP connection"); 2114 2115 /* 2116 * Generate a standardized TCP log line for use throughout the 2117 * tcp subsystem. Memory allocation is done with M_NOWAIT to 2118 * allow use in the interrupt context. 2119 * 2120 * NB: The caller MUST free(s, M_TCPLOG) the returned string. 2121 * NB: The function may return NULL if memory allocation failed. 2122 * 2123 * Due to header inclusion and ordering limitations the struct ip 2124 * and ip6_hdr pointers have to be passed as void pointers. 2125 */ 2126 char * 2127 tcp_log_vain(struct in_conninfo *inc, struct tcphdr *th, void *ip4hdr, 2128 const void *ip6hdr) 2129 { 2130 2131 /* Is logging enabled? */ 2132 if (tcp_log_in_vain == 0) 2133 return (NULL); 2134 2135 return (tcp_log_addr(inc, th, ip4hdr, ip6hdr)); 2136 } 2137 2138 char * 2139 tcp_log_addrs(struct in_conninfo *inc, struct tcphdr *th, void *ip4hdr, 2140 const void *ip6hdr) 2141 { 2142 2143 /* Is logging enabled? */ 2144 if (tcp_log_debug == 0) 2145 return (NULL); 2146 2147 return (tcp_log_addr(inc, th, ip4hdr, ip6hdr)); 2148 } 2149 2150 static char * 2151 tcp_log_addr(struct in_conninfo *inc, struct tcphdr *th, void *ip4hdr, 2152 const void *ip6hdr) 2153 { 2154 char *s, *sp; 2155 size_t size; 2156 struct ip *ip; 2157 #ifdef INET6 2158 const struct ip6_hdr *ip6; 2159 2160 ip6 = (const struct ip6_hdr *)ip6hdr; 2161 #endif /* INET6 */ 2162 ip = (struct ip *)ip4hdr; 2163 2164 /* 2165 * The log line looks like this: 2166 * "TCP: [1.2.3.4]:50332 to [1.2.3.4]:80 tcpflags 0x2<SYN>" 2167 */ 2168 size = sizeof("TCP: []:12345 to []:12345 tcpflags 0x2<>") + 2169 sizeof(PRINT_TH_FLAGS) + 1 + 2170 #ifdef INET6 2171 2 * INET6_ADDRSTRLEN; 2172 #else 2173 2 * INET_ADDRSTRLEN; 2174 #endif /* INET6 */ 2175 2176 s = malloc(size, M_TCPLOG, M_ZERO|M_NOWAIT); 2177 if (s == NULL) 2178 return (NULL); 2179 2180 strcat(s, "TCP: ["); 2181 sp = s + strlen(s); 2182 2183 if (inc && ((inc->inc_flags & INC_ISIPV6) == 0)) { 2184 inet_ntoa_r(inc->inc_faddr, sp); 2185 sp = s + strlen(s); 2186 sprintf(sp, "]:%i to [", ntohs(inc->inc_fport)); 2187 sp = s + strlen(s); 2188 inet_ntoa_r(inc->inc_laddr, sp); 2189 sp = s + strlen(s); 2190 sprintf(sp, "]:%i", ntohs(inc->inc_lport)); 2191 #ifdef INET6 2192 } else if (inc) { 2193 ip6_sprintf(sp, &inc->inc6_faddr); 2194 sp = s + strlen(s); 2195 sprintf(sp, "]:%i to [", ntohs(inc->inc_fport)); 2196 sp = s + strlen(s); 2197 ip6_sprintf(sp, &inc->inc6_laddr); 2198 sp = s + strlen(s); 2199 sprintf(sp, "]:%i", ntohs(inc->inc_lport)); 2200 } else if (ip6 && th) { 2201 ip6_sprintf(sp, &ip6->ip6_src); 2202 sp = s + strlen(s); 2203 sprintf(sp, "]:%i to [", ntohs(th->th_sport)); 2204 sp = s + strlen(s); 2205 ip6_sprintf(sp, &ip6->ip6_dst); 2206 sp = s + strlen(s); 2207 sprintf(sp, "]:%i", ntohs(th->th_dport)); 2208 #endif /* INET6 */ 2209 } else if (ip && th) { 2210 inet_ntoa_r(ip->ip_src, sp); 2211 sp = s + strlen(s); 2212 sprintf(sp, "]:%i to [", ntohs(th->th_sport)); 2213 sp = s + strlen(s); 2214 inet_ntoa_r(ip->ip_dst, sp); 2215 sp = s + strlen(s); 2216 sprintf(sp, "]:%i", ntohs(th->th_dport)); 2217 } else { 2218 free(s, M_TCPLOG); 2219 return (NULL); 2220 } 2221 sp = s + strlen(s); 2222 if (th) 2223 sprintf(sp, " tcpflags 0x%b", th->th_flags, PRINT_TH_FLAGS); 2224 if (*(s + size - 1) != '\0') 2225 panic("%s: string too long", __func__); 2226 return (s); 2227 } 2228