1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 34 * $FreeBSD$ 35 */ 36 37 #include "opt_compat.h" 38 #include "opt_tcpdebug.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/callout.h> 43 #include <sys/kernel.h> 44 #include <sys/sysctl.h> 45 #include <sys/malloc.h> 46 #include <sys/mbuf.h> 47 #include <sys/proc.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/protosw.h> 51 52 #include <vm/vm_zone.h> 53 54 #include <net/route.h> 55 #include <net/if.h> 56 57 #define _IP_VHL 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/ip.h> 61 #include <netinet/in_pcb.h> 62 #include <netinet/in_var.h> 63 #include <netinet/ip_var.h> 64 #include <netinet/tcp.h> 65 #include <netinet/tcp_fsm.h> 66 #include <netinet/tcp_seq.h> 67 #include <netinet/tcp_timer.h> 68 #include <netinet/tcp_var.h> 69 #include <netinet/tcpip.h> 70 #ifdef TCPDEBUG 71 #include <netinet/tcp_debug.h> 72 #endif 73 74 int tcp_mssdflt = TCP_MSS; 75 SYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, CTLFLAG_RW, 76 &tcp_mssdflt , 0, "Default TCP Maximum Segment Size"); 77 78 #ifdef INET6 79 int tcp_v6mssdflt = TCP6_MSS; 80 SYSCTL_INT(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt, 81 CTLFLAG_RW, &tcp_v6mssdflt , 0, ""); 82 #endif 83 84 #if 0 85 static int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 86 SYSCTL_INT(_net_inet_tcp, TCPCTL_RTTDFLT, rttdflt, CTLFLAG_RW, 87 &tcp_rttdflt , 0, "Default maximum TCP Round Trip Time"); 88 #endif 89 90 static int tcp_do_rfc1323 = 1; 91 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW, 92 &tcp_do_rfc1323 , 0, "Enable rfc1323 (high performance TCP) extensions"); 93 94 static int tcp_do_rfc1644 = 0; 95 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1644, rfc1644, CTLFLAG_RW, 96 &tcp_do_rfc1644 , 0, "Enable rfc1644 (TTCP) extensions"); 97 98 static int tcp_tcbhashsize = 0; 99 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RD, 100 &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable"); 101 102 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD, 103 &tcbinfo.ipi_count, 0, "Number of active PCBs"); 104 105 static void tcp_cleartaocache __P((void)); 106 static void tcp_notify __P((struct inpcb *, int)); 107 108 /* 109 * Target size of TCP PCB hash tables. Must be a power of two. 110 * 111 * Note that this can be overridden by the kernel environment 112 * variable net.inet.tcp.tcbhashsize 113 */ 114 #ifndef TCBHASHSIZE 115 #define TCBHASHSIZE 512 116 #endif 117 118 /* 119 * This is the actual shape of what we allocate using the zone 120 * allocator. Doing it this way allows us to protect both structures 121 * using the same generation count, and also eliminates the overhead 122 * of allocating tcpcbs separately. By hiding the structure here, 123 * we avoid changing most of the rest of the code (although it needs 124 * to be changed, eventually, for greater efficiency). 125 */ 126 #define ALIGNMENT 32 127 #define ALIGNM1 (ALIGNMENT - 1) 128 struct inp_tp { 129 union { 130 struct inpcb inp; 131 char align[(sizeof(struct inpcb) + ALIGNM1) & ~ALIGNM1]; 132 } inp_tp_u; 133 struct tcpcb tcb; 134 struct callout inp_tp_rexmt, inp_tp_persist, inp_tp_keep, inp_tp_2msl; 135 struct callout inp_tp_delack; 136 }; 137 #undef ALIGNMENT 138 #undef ALIGNM1 139 140 /* 141 * Tcp initialization 142 */ 143 void 144 tcp_init() 145 { 146 int hashsize; 147 148 tcp_iss = random(); /* wrong, but better than a constant */ 149 tcp_ccgen = 1; 150 tcp_cleartaocache(); 151 152 tcp_delacktime = TCPTV_DELACK; 153 tcp_keepinit = TCPTV_KEEP_INIT; 154 tcp_keepidle = TCPTV_KEEP_IDLE; 155 tcp_keepintvl = TCPTV_KEEPINTVL; 156 tcp_maxpersistidle = TCPTV_KEEP_IDLE; 157 tcp_msl = TCPTV_MSL; 158 159 LIST_INIT(&tcb); 160 tcbinfo.listhead = &tcb; 161 TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", TCBHASHSIZE, hashsize); 162 if (!powerof2(hashsize)) { 163 printf("WARNING: TCB hash size not a power of 2\n"); 164 hashsize = 512; /* safe default */ 165 } 166 tcp_tcbhashsize = hashsize; 167 tcbinfo.hashbase = hashinit(hashsize, M_PCB, &tcbinfo.hashmask); 168 tcbinfo.porthashbase = hashinit(hashsize, M_PCB, 169 &tcbinfo.porthashmask); 170 tcbinfo.ipi_zone = zinit("tcpcb", sizeof(struct inp_tp), maxsockets, 171 ZONE_INTERRUPT, 0); 172 173 if (max_protohdr < sizeof(struct tcpiphdr)) 174 max_protohdr = sizeof(struct tcpiphdr); 175 if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN) 176 panic("tcp_init"); 177 } 178 179 /* 180 * Create template to be used to send tcp packets on a connection. 181 * Call after host entry created, allocates an mbuf and fills 182 * in a skeletal tcp/ip header, minimizing the amount of work 183 * necessary when the connection is used. 184 */ 185 struct tcpiphdr * 186 tcp_template(tp) 187 struct tcpcb *tp; 188 { 189 register struct inpcb *inp = tp->t_inpcb; 190 register struct mbuf *m; 191 register struct tcpiphdr *n; 192 193 if ((n = tp->t_template) == 0) { 194 m = m_get(M_DONTWAIT, MT_HEADER); 195 if (m == NULL) 196 return (0); 197 m->m_len = sizeof (struct tcpiphdr); 198 n = mtod(m, struct tcpiphdr *); 199 } 200 bzero(n->ti_x1, sizeof(n->ti_x1)); 201 n->ti_pr = IPPROTO_TCP; 202 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 203 n->ti_src = inp->inp_laddr; 204 n->ti_dst = inp->inp_faddr; 205 n->ti_sport = inp->inp_lport; 206 n->ti_dport = inp->inp_fport; 207 n->ti_seq = 0; 208 n->ti_ack = 0; 209 n->ti_x2 = 0; 210 n->ti_off = 5; 211 n->ti_flags = 0; 212 n->ti_win = 0; 213 n->ti_sum = 0; 214 n->ti_urp = 0; 215 return (n); 216 } 217 218 /* 219 * Send a single message to the TCP at address specified by 220 * the given TCP/IP header. If m == 0, then we make a copy 221 * of the tcpiphdr at ti and send directly to the addressed host. 222 * This is used to force keep alive messages out using the TCP 223 * template for a connection tp->t_template. If flags are given 224 * then we send a message back to the TCP which originated the 225 * segment ti, and discard the mbuf containing it and any other 226 * attached mbufs. 227 * 228 * In any case the ack and sequence number of the transmitted 229 * segment are as specified by the parameters. 230 * 231 * NOTE: If m != NULL, then ti must point to *inside* the mbuf. 232 */ 233 void 234 tcp_respond(tp, ti, m, ack, seq, flags) 235 struct tcpcb *tp; 236 register struct tcpiphdr *ti; 237 register struct mbuf *m; 238 tcp_seq ack, seq; 239 int flags; 240 { 241 register int tlen; 242 int win = 0; 243 struct route *ro = 0; 244 struct route sro; 245 246 if (tp) { 247 if (!(flags & TH_RST)) 248 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 249 ro = &tp->t_inpcb->inp_route; 250 } else { 251 ro = &sro; 252 bzero(ro, sizeof *ro); 253 } 254 if (m == 0) { 255 m = m_gethdr(M_DONTWAIT, MT_HEADER); 256 if (m == NULL) 257 return; 258 #ifdef TCP_COMPAT_42 259 tlen = 1; 260 #else 261 tlen = 0; 262 #endif 263 m->m_data += max_linkhdr; 264 *mtod(m, struct tcpiphdr *) = *ti; 265 ti = mtod(m, struct tcpiphdr *); 266 flags = TH_ACK; 267 } else { 268 m_freem(m->m_next); 269 m->m_next = 0; 270 m->m_data = (caddr_t)ti; 271 m->m_len = sizeof (struct tcpiphdr); 272 tlen = 0; 273 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 274 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, n_long); 275 xchg(ti->ti_dport, ti->ti_sport, n_short); 276 #undef xchg 277 } 278 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 279 tlen += sizeof (struct tcpiphdr); 280 m->m_len = tlen; 281 m->m_pkthdr.len = tlen; 282 m->m_pkthdr.rcvif = (struct ifnet *) 0; 283 bzero(ti->ti_x1, sizeof(ti->ti_x1)); 284 ti->ti_seq = htonl(seq); 285 ti->ti_ack = htonl(ack); 286 ti->ti_x2 = 0; 287 ti->ti_off = sizeof (struct tcphdr) >> 2; 288 ti->ti_flags = flags; 289 if (tp) 290 ti->ti_win = htons((u_short) (win >> tp->rcv_scale)); 291 else 292 ti->ti_win = htons((u_short)win); 293 ti->ti_urp = 0; 294 ti->ti_sum = 0; 295 ti->ti_sum = in_cksum(m, tlen); 296 ((struct ip *)ti)->ip_len = tlen; 297 ((struct ip *)ti)->ip_ttl = ip_defttl; 298 #ifdef TCPDEBUG 299 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 300 tcp_trace(TA_OUTPUT, 0, tp, ti, 0); 301 #endif 302 (void) ip_output(m, NULL, ro, 0, NULL); 303 if (ro == &sro && ro->ro_rt) { 304 RTFREE(ro->ro_rt); 305 } 306 } 307 308 /* 309 * Create a new TCP control block, making an 310 * empty reassembly queue and hooking it to the argument 311 * protocol control block. The `inp' parameter must have 312 * come from the zone allocator set up in tcp_init(). 313 */ 314 struct tcpcb * 315 tcp_newtcpcb(inp) 316 struct inpcb *inp; 317 { 318 struct inp_tp *it; 319 register struct tcpcb *tp; 320 321 it = (struct inp_tp *)inp; 322 tp = &it->tcb; 323 bzero((char *) tp, sizeof(struct tcpcb)); 324 tp->t_segq = NULL; 325 tp->t_maxseg = tp->t_maxopd = tcp_mssdflt; 326 327 /* Set up our timeouts. */ 328 callout_init(tp->tt_rexmt = &it->inp_tp_rexmt); 329 callout_init(tp->tt_persist = &it->inp_tp_persist); 330 callout_init(tp->tt_keep = &it->inp_tp_keep); 331 callout_init(tp->tt_2msl = &it->inp_tp_2msl); 332 callout_init(tp->tt_delack = &it->inp_tp_delack); 333 334 if (tcp_do_rfc1323) 335 tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP); 336 if (tcp_do_rfc1644) 337 tp->t_flags |= TF_REQ_CC; 338 tp->t_inpcb = inp; /* XXX */ 339 /* 340 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 341 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives 342 * reasonable initial retransmit time. 343 */ 344 tp->t_srtt = TCPTV_SRTTBASE; 345 tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4; 346 tp->t_rttmin = TCPTV_MIN; 347 tp->t_rxtcur = TCPTV_RTOBASE; 348 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 349 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 350 tp->t_rcvtime = ticks; 351 inp->inp_ip_ttl = ip_defttl; 352 inp->inp_ppcb = (caddr_t)tp; 353 return (tp); /* XXX */ 354 } 355 356 /* 357 * Drop a TCP connection, reporting 358 * the specified error. If connection is synchronized, 359 * then send a RST to peer. 360 */ 361 struct tcpcb * 362 tcp_drop(tp, errno) 363 register struct tcpcb *tp; 364 int errno; 365 { 366 struct socket *so = tp->t_inpcb->inp_socket; 367 368 if (TCPS_HAVERCVDSYN(tp->t_state)) { 369 tp->t_state = TCPS_CLOSED; 370 (void) tcp_output(tp); 371 tcpstat.tcps_drops++; 372 } else 373 tcpstat.tcps_conndrops++; 374 if (errno == ETIMEDOUT && tp->t_softerror) 375 errno = tp->t_softerror; 376 so->so_error = errno; 377 return (tcp_close(tp)); 378 } 379 380 /* 381 * Close a TCP control block: 382 * discard all space held by the tcp 383 * discard internet protocol block 384 * wake up any sleepers 385 */ 386 struct tcpcb * 387 tcp_close(tp) 388 register struct tcpcb *tp; 389 { 390 register struct mbuf *q; 391 register struct mbuf *nq; 392 struct inpcb *inp = tp->t_inpcb; 393 struct socket *so = inp->inp_socket; 394 register struct rtentry *rt; 395 int dosavessthresh; 396 397 /* 398 * Make sure that all of our timers are stopped before we 399 * delete the PCB. 400 */ 401 callout_stop(tp->tt_rexmt); 402 callout_stop(tp->tt_persist); 403 callout_stop(tp->tt_keep); 404 callout_stop(tp->tt_2msl); 405 callout_stop(tp->tt_delack); 406 407 /* 408 * If we got enough samples through the srtt filter, 409 * save the rtt and rttvar in the routing entry. 410 * 'Enough' is arbitrarily defined as the 16 samples. 411 * 16 samples is enough for the srtt filter to converge 412 * to within 5% of the correct value; fewer samples and 413 * we could save a very bogus rtt. 414 * 415 * Don't update the default route's characteristics and don't 416 * update anything that the user "locked". 417 */ 418 if (tp->t_rttupdated >= 16 && 419 (rt = inp->inp_route.ro_rt) && 420 ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) { 421 register u_long i = 0; 422 423 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 424 i = tp->t_srtt * 425 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 426 if (rt->rt_rmx.rmx_rtt && i) 427 /* 428 * filter this update to half the old & half 429 * the new values, converting scale. 430 * See route.h and tcp_var.h for a 431 * description of the scaling constants. 432 */ 433 rt->rt_rmx.rmx_rtt = 434 (rt->rt_rmx.rmx_rtt + i) / 2; 435 else 436 rt->rt_rmx.rmx_rtt = i; 437 tcpstat.tcps_cachedrtt++; 438 } 439 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 440 i = tp->t_rttvar * 441 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 442 if (rt->rt_rmx.rmx_rttvar && i) 443 rt->rt_rmx.rmx_rttvar = 444 (rt->rt_rmx.rmx_rttvar + i) / 2; 445 else 446 rt->rt_rmx.rmx_rttvar = i; 447 tcpstat.tcps_cachedrttvar++; 448 } 449 /* 450 * The old comment here said: 451 * update the pipelimit (ssthresh) if it has been updated 452 * already or if a pipesize was specified & the threshhold 453 * got below half the pipesize. I.e., wait for bad news 454 * before we start updating, then update on both good 455 * and bad news. 456 * 457 * But we want to save the ssthresh even if no pipesize is 458 * specified explicitly in the route, because such 459 * connections still have an implicit pipesize specified 460 * by the global tcp_sendspace. In the absence of a reliable 461 * way to calculate the pipesize, it will have to do. 462 */ 463 i = tp->snd_ssthresh; 464 if (rt->rt_rmx.rmx_sendpipe != 0) 465 dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe / 2); 466 else 467 dosavessthresh = (i < so->so_snd.sb_hiwat / 2); 468 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 469 i != 0 && rt->rt_rmx.rmx_ssthresh != 0) 470 || dosavessthresh) { 471 /* 472 * convert the limit from user data bytes to 473 * packets then to packet data bytes. 474 */ 475 i = (i + tp->t_maxseg / 2) / tp->t_maxseg; 476 if (i < 2) 477 i = 2; 478 i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr)); 479 if (rt->rt_rmx.rmx_ssthresh) 480 rt->rt_rmx.rmx_ssthresh = 481 (rt->rt_rmx.rmx_ssthresh + i) / 2; 482 else 483 rt->rt_rmx.rmx_ssthresh = i; 484 tcpstat.tcps_cachedssthresh++; 485 } 486 } 487 /* free the reassembly queue, if any */ 488 for (q = tp->t_segq; q; q = nq) { 489 nq = q->m_nextpkt; 490 tp->t_segq = nq; 491 m_freem(q); 492 } 493 if (tp->t_template) 494 (void) m_free(dtom(tp->t_template)); 495 inp->inp_ppcb = NULL; 496 soisdisconnected(so); 497 in_pcbdetach(inp); 498 tcpstat.tcps_closed++; 499 return ((struct tcpcb *)0); 500 } 501 502 void 503 tcp_drain() 504 { 505 506 } 507 508 /* 509 * Notify a tcp user of an asynchronous error; 510 * store error as soft error, but wake up user 511 * (for now, won't do anything until can select for soft error). 512 */ 513 static void 514 tcp_notify(inp, error) 515 struct inpcb *inp; 516 int error; 517 { 518 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 519 register struct socket *so = inp->inp_socket; 520 521 /* 522 * Ignore some errors if we are hooked up. 523 * If connection hasn't completed, has retransmitted several times, 524 * and receives a second error, give up now. This is better 525 * than waiting a long time to establish a connection that 526 * can never complete. 527 */ 528 if (tp->t_state == TCPS_ESTABLISHED && 529 (error == EHOSTUNREACH || error == ENETUNREACH || 530 error == EHOSTDOWN)) { 531 return; 532 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 533 tp->t_softerror) 534 so->so_error = error; 535 else 536 tp->t_softerror = error; 537 wakeup((caddr_t) &so->so_timeo); 538 sorwakeup(so); 539 sowwakeup(so); 540 } 541 542 static int 543 tcp_pcblist SYSCTL_HANDLER_ARGS 544 { 545 int error, i, n, s; 546 struct inpcb *inp, **inp_list; 547 inp_gen_t gencnt; 548 struct xinpgen xig; 549 550 /* 551 * The process of preparing the TCB list is too time-consuming and 552 * resource-intensive to repeat twice on every request. 553 */ 554 if (req->oldptr == 0) { 555 n = tcbinfo.ipi_count; 556 req->oldidx = 2 * (sizeof xig) 557 + (n + n/8) * sizeof(struct xtcpcb); 558 return 0; 559 } 560 561 if (req->newptr != 0) 562 return EPERM; 563 564 /* 565 * OK, now we're committed to doing something. 566 */ 567 s = splnet(); 568 gencnt = tcbinfo.ipi_gencnt; 569 n = tcbinfo.ipi_count; 570 splx(s); 571 572 xig.xig_len = sizeof xig; 573 xig.xig_count = n; 574 xig.xig_gen = gencnt; 575 xig.xig_sogen = so_gencnt; 576 error = SYSCTL_OUT(req, &xig, sizeof xig); 577 if (error) 578 return error; 579 580 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 581 if (inp_list == 0) 582 return ENOMEM; 583 584 s = splnet(); 585 for (inp = tcbinfo.listhead->lh_first, i = 0; inp && i < n; 586 inp = inp->inp_list.le_next) { 587 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp)) 588 inp_list[i++] = inp; 589 } 590 splx(s); 591 n = i; 592 593 error = 0; 594 for (i = 0; i < n; i++) { 595 inp = inp_list[i]; 596 if (inp->inp_gencnt <= gencnt) { 597 struct xtcpcb xt; 598 caddr_t inp_ppcb; 599 xt.xt_len = sizeof xt; 600 /* XXX should avoid extra copy */ 601 bcopy(inp, &xt.xt_inp, sizeof *inp); 602 inp_ppcb = inp->inp_ppcb; 603 if (inp_ppcb != NULL) 604 bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp); 605 else 606 bzero((char *) &xt.xt_tp, sizeof xt.xt_tp); 607 if (inp->inp_socket) 608 sotoxsocket(inp->inp_socket, &xt.xt_socket); 609 error = SYSCTL_OUT(req, &xt, sizeof xt); 610 } 611 } 612 if (!error) { 613 /* 614 * Give the user an updated idea of our state. 615 * If the generation differs from what we told 616 * her before, she knows that something happened 617 * while we were processing this request, and it 618 * might be necessary to retry. 619 */ 620 s = splnet(); 621 xig.xig_gen = tcbinfo.ipi_gencnt; 622 xig.xig_sogen = so_gencnt; 623 xig.xig_count = tcbinfo.ipi_count; 624 splx(s); 625 error = SYSCTL_OUT(req, &xig, sizeof xig); 626 } 627 free(inp_list, M_TEMP); 628 return error; 629 } 630 631 SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 632 tcp_pcblist, "S,xtcpcb", "List of active TCP connections"); 633 634 static int 635 tcp_getcred SYSCTL_HANDLER_ARGS 636 { 637 struct sockaddr_in addrs[2]; 638 struct inpcb *inp; 639 int error, s; 640 641 error = suser(req->p); 642 if (error) 643 return (error); 644 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 645 if (error) 646 return (error); 647 s = splnet(); 648 inp = in_pcblookup_hash(&tcbinfo, addrs[1].sin_addr, addrs[1].sin_port, 649 addrs[0].sin_addr, addrs[0].sin_port, 0); 650 if (inp == NULL || inp->inp_socket == NULL) { 651 error = ENOENT; 652 goto out; 653 } 654 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred)); 655 out: 656 splx(s); 657 return (error); 658 } 659 660 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 661 0, 0, tcp_getcred, "S,ucred", "Get the ucred of a TCP connection"); 662 663 void 664 tcp_ctlinput(cmd, sa, vip) 665 int cmd; 666 struct sockaddr *sa; 667 void *vip; 668 { 669 register struct ip *ip = vip; 670 register struct tcphdr *th; 671 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 672 673 if (cmd == PRC_QUENCH) 674 notify = tcp_quench; 675 else if (cmd == PRC_MSGSIZE) 676 notify = tcp_mtudisc; 677 else if (!PRC_IS_REDIRECT(cmd) && 678 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)) 679 return; 680 if (ip) { 681 th = (struct tcphdr *)((caddr_t)ip 682 + (IP_VHL_HL(ip->ip_vhl) << 2)); 683 in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport, 684 cmd, notify); 685 } else 686 in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify); 687 } 688 689 /* 690 * When a source quench is received, close congestion window 691 * to one segment. We will gradually open it again as we proceed. 692 */ 693 void 694 tcp_quench(inp, errno) 695 struct inpcb *inp; 696 int errno; 697 { 698 struct tcpcb *tp = intotcpcb(inp); 699 700 if (tp) 701 tp->snd_cwnd = tp->t_maxseg; 702 } 703 704 /* 705 * When `need fragmentation' ICMP is received, update our idea of the MSS 706 * based on the new value in the route. Also nudge TCP to send something, 707 * since we know the packet we just sent was dropped. 708 * This duplicates some code in the tcp_mss() function in tcp_input.c. 709 */ 710 void 711 tcp_mtudisc(inp, errno) 712 struct inpcb *inp; 713 int errno; 714 { 715 struct tcpcb *tp = intotcpcb(inp); 716 struct rtentry *rt; 717 struct rmxp_tao *taop; 718 struct socket *so = inp->inp_socket; 719 int offered; 720 int mss; 721 722 if (tp) { 723 rt = tcp_rtlookup(inp); 724 if (!rt || !rt->rt_rmx.rmx_mtu) { 725 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt; 726 return; 727 } 728 taop = rmx_taop(rt->rt_rmx); 729 offered = taop->tao_mssopt; 730 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr); 731 if (offered) 732 mss = min(mss, offered); 733 /* 734 * XXX - The above conditional probably violates the TCP 735 * spec. The problem is that, since we don't know the 736 * other end's MSS, we are supposed to use a conservative 737 * default. But, if we do that, then MTU discovery will 738 * never actually take place, because the conservative 739 * default is much less than the MTUs typically seen 740 * on the Internet today. For the moment, we'll sweep 741 * this under the carpet. 742 * 743 * The conservative default might not actually be a problem 744 * if the only case this occurs is when sending an initial 745 * SYN with options and data to a host we've never talked 746 * to before. Then, they will reply with an MSS value which 747 * will get recorded and the new parameters should get 748 * recomputed. For Further Study. 749 */ 750 if (tp->t_maxopd <= mss) 751 return; 752 tp->t_maxopd = mss; 753 754 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 755 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 756 mss -= TCPOLEN_TSTAMP_APPA; 757 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && 758 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC) 759 mss -= TCPOLEN_CC_APPA; 760 #if (MCLBYTES & (MCLBYTES - 1)) == 0 761 if (mss > MCLBYTES) 762 mss &= ~(MCLBYTES-1); 763 #else 764 if (mss > MCLBYTES) 765 mss = mss / MCLBYTES * MCLBYTES; 766 #endif 767 if (so->so_snd.sb_hiwat < mss) 768 mss = so->so_snd.sb_hiwat; 769 770 tp->t_maxseg = mss; 771 772 tcpstat.tcps_mturesent++; 773 tp->t_rtttime = 0; 774 tp->snd_nxt = tp->snd_una; 775 tcp_output(tp); 776 } 777 } 778 779 /* 780 * Look-up the routing entry to the peer of this inpcb. If no route 781 * is found and it cannot be allocated the return NULL. This routine 782 * is called by TCP routines that access the rmx structure and by tcp_mss 783 * to get the interface MTU. 784 */ 785 struct rtentry * 786 tcp_rtlookup(inp) 787 struct inpcb *inp; 788 { 789 struct route *ro; 790 struct rtentry *rt; 791 792 ro = &inp->inp_route; 793 rt = ro->ro_rt; 794 if (rt == NULL || !(rt->rt_flags & RTF_UP)) { 795 /* No route yet, so try to acquire one */ 796 if (inp->inp_faddr.s_addr != INADDR_ANY) { 797 ro->ro_dst.sa_family = AF_INET; 798 ro->ro_dst.sa_len = sizeof(ro->ro_dst); 799 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 800 inp->inp_faddr; 801 rtalloc(ro); 802 rt = ro->ro_rt; 803 } 804 } 805 return rt; 806 } 807 808 /* 809 * Return a pointer to the cached information about the remote host. 810 * The cached information is stored in the protocol specific part of 811 * the route metrics. 812 */ 813 struct rmxp_tao * 814 tcp_gettaocache(inp) 815 struct inpcb *inp; 816 { 817 struct rtentry *rt = tcp_rtlookup(inp); 818 819 /* Make sure this is a host route and is up. */ 820 if (rt == NULL || 821 (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST)) 822 return NULL; 823 824 return rmx_taop(rt->rt_rmx); 825 } 826 827 /* 828 * Clear all the TAO cache entries, called from tcp_init. 829 * 830 * XXX 831 * This routine is just an empty one, because we assume that the routing 832 * routing tables are initialized at the same time when TCP, so there is 833 * nothing in the cache left over. 834 */ 835 static void 836 tcp_cleartaocache() 837 { 838 } 839