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