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