Home | History | Annotate | Line # | Download | only in netinet
tcp_usrreq.c revision 1.80
      1 /*	$NetBSD: tcp_usrreq.c,v 1.80 2003/06/29 12:00:47 simonb Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     38  * Facility, NASA Ames Research Center.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1988, 1993, 1995
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *	This product includes software developed by the University of
     84  *	California, Berkeley and its contributors.
     85  * 4. Neither the name of the University nor the names of its contributors
     86  *    may be used to endorse or promote products derived from this software
     87  *    without specific prior written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     91  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     92  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     93  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     94  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     95  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     96  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     97  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     98  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     99  * SUCH DAMAGE.
    100  *
    101  *	@(#)tcp_usrreq.c	8.5 (Berkeley) 6/21/95
    102  */
    103 
    104 #include <sys/cdefs.h>
    105 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.80 2003/06/29 12:00:47 simonb Exp $");
    106 
    107 #include "opt_inet.h"
    108 #include "opt_ipsec.h"
    109 #include "opt_tcp_debug.h"
    110 #include "opt_mbuftrace.h"
    111 
    112 #include <sys/param.h>
    113 #include <sys/systm.h>
    114 #include <sys/kernel.h>
    115 #include <sys/malloc.h>
    116 #include <sys/mbuf.h>
    117 #include <sys/socket.h>
    118 #include <sys/socketvar.h>
    119 #include <sys/protosw.h>
    120 #include <sys/errno.h>
    121 #include <sys/stat.h>
    122 #include <sys/proc.h>
    123 #include <sys/domain.h>
    124 #include <sys/sysctl.h>
    125 
    126 #include <net/if.h>
    127 #include <net/route.h>
    128 
    129 #include <netinet/in.h>
    130 #include <netinet/in_systm.h>
    131 #include <netinet/in_var.h>
    132 #include <netinet/ip.h>
    133 #include <netinet/in_pcb.h>
    134 #include <netinet/ip_var.h>
    135 
    136 #ifdef INET6
    137 #ifndef INET
    138 #include <netinet/in.h>
    139 #endif
    140 #include <netinet/ip6.h>
    141 #include <netinet6/in6_pcb.h>
    142 #include <netinet6/ip6_var.h>
    143 #endif
    144 
    145 #include <netinet/tcp.h>
    146 #include <netinet/tcp_fsm.h>
    147 #include <netinet/tcp_seq.h>
    148 #include <netinet/tcp_timer.h>
    149 #include <netinet/tcp_var.h>
    150 #include <netinet/tcpip.h>
    151 #include <netinet/tcp_debug.h>
    152 
    153 #include "opt_tcp_space.h"
    154 
    155 #ifdef IPSEC
    156 #include <netinet6/ipsec.h>
    157 #endif /*IPSEC*/
    158 
    159 /*
    160  * TCP protocol interface to socket abstraction.
    161  */
    162 extern	char *tcpstates[];
    163 
    164 static int tcp_sysctl_ident(int *, u_int, void *, size_t *, void *, size_t);
    165 
    166 /*
    167  * Process a TCP user request for TCP tb.  If this is a send request
    168  * then m is the mbuf chain of send data.  If this is a timer expiration
    169  * (called from the software clock routine), then timertype tells which timer.
    170  */
    171 /*ARGSUSED*/
    172 int
    173 tcp_usrreq(so, req, m, nam, control, l)
    174 	struct socket *so;
    175 	int req;
    176 	struct mbuf *m, *nam, *control;
    177 	struct lwp *l;
    178 {
    179 	struct proc *p;
    180 	struct inpcb *inp;
    181 #ifdef INET6
    182 	struct in6pcb *in6p;
    183 #endif
    184 	struct tcpcb *tp = NULL;
    185 	int s;
    186 	int error = 0;
    187 #ifdef TCP_DEBUG
    188 	int ostate = 0;
    189 #endif
    190 	int family;	/* family of the socket */
    191 
    192 	p = l ? l->l_proc : NULL;
    193 	family = so->so_proto->pr_domain->dom_family;
    194 
    195 	if (req == PRU_CONTROL) {
    196 		switch (family) {
    197 #ifdef INET
    198 		case PF_INET:
    199 			return (in_control(so, (long)m, (caddr_t)nam,
    200 			    (struct ifnet *)control, p));
    201 #endif
    202 #ifdef INET6
    203 		case PF_INET6:
    204 			return (in6_control(so, (long)m, (caddr_t)nam,
    205 			    (struct ifnet *)control, p));
    206 #endif
    207 		default:
    208 			return EAFNOSUPPORT;
    209 		}
    210 	}
    211 
    212 	if (req == PRU_PURGEIF) {
    213 		switch (family) {
    214 #ifdef INET
    215 		case PF_INET:
    216 			in_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
    217 			in_purgeif((struct ifnet *)control);
    218 			in_pcbpurgeif(&tcbtable, (struct ifnet *)control);
    219 			break;
    220 #endif
    221 #ifdef INET6
    222 		case PF_INET6:
    223 			in6_pcbpurgeif0(&tcb6, (struct ifnet *)control);
    224 			in6_purgeif((struct ifnet *)control);
    225 			in6_pcbpurgeif(&tcb6, (struct ifnet *)control);
    226 			break;
    227 #endif
    228 		default:
    229 			return (EAFNOSUPPORT);
    230 		}
    231 		return (0);
    232 	}
    233 
    234 	s = splsoftnet();
    235 	switch (family) {
    236 #ifdef INET
    237 	case PF_INET:
    238 		inp = sotoinpcb(so);
    239 #ifdef INET6
    240 		in6p = NULL;
    241 #endif
    242 		break;
    243 #endif
    244 #ifdef INET6
    245 	case PF_INET6:
    246 		inp = NULL;
    247 		in6p = sotoin6pcb(so);
    248 		break;
    249 #endif
    250 	default:
    251 		splx(s);
    252 		return EAFNOSUPPORT;
    253 	}
    254 
    255 #ifdef DIAGNOSTIC
    256 #ifdef INET6
    257 	if (inp && in6p)
    258 		panic("tcp_usrreq: both inp and in6p set to non-NULL");
    259 #endif
    260 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    261 		panic("tcp_usrreq: unexpected control mbuf");
    262 #endif
    263 	/*
    264 	 * When a TCP is attached to a socket, then there will be
    265 	 * a (struct inpcb) pointed at by the socket, and this
    266 	 * structure will point at a subsidary (struct tcpcb).
    267 	 */
    268 #ifndef INET6
    269 	if (inp == 0 && req != PRU_ATTACH)
    270 #else
    271 	if ((inp == 0 && in6p == 0) && req != PRU_ATTACH)
    272 #endif
    273 	{
    274 		error = EINVAL;
    275 		goto release;
    276 	}
    277 #ifdef INET
    278 	if (inp) {
    279 		tp = intotcpcb(inp);
    280 		/* WHAT IF TP IS 0? */
    281 #ifdef KPROF
    282 		tcp_acounts[tp->t_state][req]++;
    283 #endif
    284 #ifdef TCP_DEBUG
    285 		ostate = tp->t_state;
    286 #endif
    287 	}
    288 #endif
    289 #ifdef INET6
    290 	if (in6p) {
    291 		tp = in6totcpcb(in6p);
    292 		/* WHAT IF TP IS 0? */
    293 #ifdef KPROF
    294 		tcp_acounts[tp->t_state][req]++;
    295 #endif
    296 #ifdef TCP_DEBUG
    297 		ostate = tp->t_state;
    298 #endif
    299 	}
    300 #endif
    301 
    302 	switch (req) {
    303 
    304 	/*
    305 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
    306 	 * and an internet control block.
    307 	 */
    308 	case PRU_ATTACH:
    309 #ifndef INET6
    310 		if (inp != 0)
    311 #else
    312 		if (inp != 0 || in6p != 0)
    313 #endif
    314 		{
    315 			error = EISCONN;
    316 			break;
    317 		}
    318 		error = tcp_attach(so);
    319 		if (error)
    320 			break;
    321 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    322 			so->so_linger = TCP_LINGERTIME;
    323 		tp = sototcpcb(so);
    324 		break;
    325 
    326 	/*
    327 	 * PRU_DETACH detaches the TCP protocol from the socket.
    328 	 */
    329 	case PRU_DETACH:
    330 		tp = tcp_disconnect(tp);
    331 		break;
    332 
    333 	/*
    334 	 * Give the socket an address.
    335 	 */
    336 	case PRU_BIND:
    337 		switch (family) {
    338 #ifdef INET
    339 		case PF_INET:
    340 			error = in_pcbbind(inp, nam, p);
    341 			break;
    342 #endif
    343 #ifdef INET6
    344 		case PF_INET6:
    345 			error = in6_pcbbind(in6p, nam, p);
    346 			if (!error) {
    347 				/* mapped addr case */
    348 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    349 					tp->t_family = AF_INET;
    350 				else
    351 					tp->t_family = AF_INET6;
    352 			}
    353 			break;
    354 #endif
    355 		}
    356 		break;
    357 
    358 	/*
    359 	 * Prepare to accept connections.
    360 	 */
    361 	case PRU_LISTEN:
    362 #ifdef INET
    363 		if (inp && inp->inp_lport == 0) {
    364 			error = in_pcbbind(inp, (struct mbuf *)0,
    365 			    (struct proc *)0);
    366 			if (error)
    367 				break;
    368 		}
    369 #endif
    370 #ifdef INET6
    371 		if (in6p && in6p->in6p_lport == 0) {
    372 			error = in6_pcbbind(in6p, (struct mbuf *)0,
    373 			    (struct proc *)0);
    374 			if (error)
    375 				break;
    376 		}
    377 #endif
    378 		tp->t_state = TCPS_LISTEN;
    379 		break;
    380 
    381 	/*
    382 	 * Initiate connection to peer.
    383 	 * Create a template for use in transmissions on this connection.
    384 	 * Enter SYN_SENT state, and mark socket as connecting.
    385 	 * Start keep-alive timer, and seed output sequence space.
    386 	 * Send initial segment on connection.
    387 	 */
    388 	case PRU_CONNECT:
    389 #ifdef INET
    390 		if (inp) {
    391 			if (inp->inp_lport == 0) {
    392 				error = in_pcbbind(inp, (struct mbuf *)0,
    393 				    (struct proc *)0);
    394 				if (error)
    395 					break;
    396 			}
    397 			error = in_pcbconnect(inp, nam);
    398 		}
    399 #endif
    400 #ifdef INET6
    401 		if (in6p) {
    402 			if (in6p->in6p_lport == 0) {
    403 				error = in6_pcbbind(in6p, (struct mbuf *)0,
    404 				    (struct proc *)0);
    405 				if (error)
    406 					break;
    407 			}
    408 			error = in6_pcbconnect(in6p, nam);
    409 			if (!error) {
    410 				/* mapped addr case */
    411 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    412 					tp->t_family = AF_INET;
    413 				else
    414 					tp->t_family = AF_INET6;
    415 			}
    416 		}
    417 #endif
    418 		if (error)
    419 			break;
    420 		tp->t_template = tcp_template(tp);
    421 		if (tp->t_template == 0) {
    422 #ifdef INET
    423 			if (inp)
    424 				in_pcbdisconnect(inp);
    425 #endif
    426 #ifdef INET6
    427 			if (in6p)
    428 				in6_pcbdisconnect(in6p);
    429 #endif
    430 			error = ENOBUFS;
    431 			break;
    432 		}
    433 		/* Compute window scaling to request.  */
    434 		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    435 		    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
    436 			tp->request_r_scale++;
    437 		soisconnecting(so);
    438 		tcpstat.tcps_connattempt++;
    439 		tp->t_state = TCPS_SYN_SENT;
    440 		TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
    441 		tp->iss = tcp_new_iss(tp, 0);
    442 		tcp_sendseqinit(tp);
    443 		error = tcp_output(tp);
    444 		break;
    445 
    446 	/*
    447 	 * Create a TCP connection between two sockets.
    448 	 */
    449 	case PRU_CONNECT2:
    450 		error = EOPNOTSUPP;
    451 		break;
    452 
    453 	/*
    454 	 * Initiate disconnect from peer.
    455 	 * If connection never passed embryonic stage, just drop;
    456 	 * else if don't need to let data drain, then can just drop anyways,
    457 	 * else have to begin TCP shutdown process: mark socket disconnecting,
    458 	 * drain unread data, state switch to reflect user close, and
    459 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
    460 	 * when peer sends FIN and acks ours.
    461 	 *
    462 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
    463 	 */
    464 	case PRU_DISCONNECT:
    465 		tp = tcp_disconnect(tp);
    466 		break;
    467 
    468 	/*
    469 	 * Accept a connection.  Essentially all the work is
    470 	 * done at higher levels; just return the address
    471 	 * of the peer, storing through addr.
    472 	 */
    473 	case PRU_ACCEPT:
    474 #ifdef INET
    475 		if (inp)
    476 			in_setpeeraddr(inp, nam);
    477 #endif
    478 #ifdef INET6
    479 		if (in6p)
    480 			in6_setpeeraddr(in6p, nam);
    481 #endif
    482 		break;
    483 
    484 	/*
    485 	 * Mark the connection as being incapable of further output.
    486 	 */
    487 	case PRU_SHUTDOWN:
    488 		socantsendmore(so);
    489 		tp = tcp_usrclosed(tp);
    490 		if (tp)
    491 			error = tcp_output(tp);
    492 		break;
    493 
    494 	/*
    495 	 * After a receive, possibly send window update to peer.
    496 	 */
    497 	case PRU_RCVD:
    498 		/*
    499 		 * soreceive() calls this function when a user receives
    500 		 * ancillary data on a listening socket. We don't call
    501 		 * tcp_output in such a case, since there is no header
    502 		 * template for a listening socket and hence the kernel
    503 		 * will panic.
    504 		 */
    505 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
    506 			(void) tcp_output(tp);
    507 		break;
    508 
    509 	/*
    510 	 * Do a send by putting data in output queue and updating urgent
    511 	 * marker if URG set.  Possibly send more data.
    512 	 */
    513 	case PRU_SEND:
    514 		if (control && control->m_len) {
    515 			m_freem(control);
    516 			m_freem(m);
    517 			error = EINVAL;
    518 			break;
    519 		}
    520 		sbappendstream(&so->so_snd, m);
    521 		error = tcp_output(tp);
    522 		break;
    523 
    524 	/*
    525 	 * Abort the TCP.
    526 	 */
    527 	case PRU_ABORT:
    528 		tp = tcp_drop(tp, ECONNABORTED);
    529 		break;
    530 
    531 	case PRU_SENSE:
    532 		/*
    533 		 * stat: don't bother with a blocksize.
    534 		 */
    535 		splx(s);
    536 		return (0);
    537 
    538 	case PRU_RCVOOB:
    539 		if (control && control->m_len) {
    540 			m_freem(control);
    541 			m_freem(m);
    542 			error = EINVAL;
    543 			break;
    544 		}
    545 		if ((so->so_oobmark == 0 &&
    546 		    (so->so_state & SS_RCVATMARK) == 0) ||
    547 		    so->so_options & SO_OOBINLINE ||
    548 		    tp->t_oobflags & TCPOOB_HADDATA) {
    549 			error = EINVAL;
    550 			break;
    551 		}
    552 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
    553 			error = EWOULDBLOCK;
    554 			break;
    555 		}
    556 		m->m_len = 1;
    557 		*mtod(m, caddr_t) = tp->t_iobc;
    558 		if (((long)nam & MSG_PEEK) == 0)
    559 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
    560 		break;
    561 
    562 	case PRU_SENDOOB:
    563 		if (sbspace(&so->so_snd) < -512) {
    564 			m_freem(m);
    565 			error = ENOBUFS;
    566 			break;
    567 		}
    568 		/*
    569 		 * According to RFC961 (Assigned Protocols),
    570 		 * the urgent pointer points to the last octet
    571 		 * of urgent data.  We continue, however,
    572 		 * to consider it to indicate the first octet
    573 		 * of data past the urgent section.
    574 		 * Otherwise, snd_up should be one lower.
    575 		 */
    576 		sbappendstream(&so->so_snd, m);
    577 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
    578 		tp->t_force = 1;
    579 		error = tcp_output(tp);
    580 		tp->t_force = 0;
    581 		break;
    582 
    583 	case PRU_SOCKADDR:
    584 #ifdef INET
    585 		if (inp)
    586 			in_setsockaddr(inp, nam);
    587 #endif
    588 #ifdef INET6
    589 		if (in6p)
    590 			in6_setsockaddr(in6p, nam);
    591 #endif
    592 		break;
    593 
    594 	case PRU_PEERADDR:
    595 #ifdef INET
    596 		if (inp)
    597 			in_setpeeraddr(inp, nam);
    598 #endif
    599 #ifdef INET6
    600 		if (in6p)
    601 			in6_setpeeraddr(in6p, nam);
    602 #endif
    603 		break;
    604 
    605 	default:
    606 		panic("tcp_usrreq");
    607 	}
    608 #ifdef TCP_DEBUG
    609 	if (tp && (so->so_options & SO_DEBUG))
    610 		tcp_trace(TA_USER, ostate, tp, NULL, req);
    611 #endif
    612 
    613 release:
    614 	splx(s);
    615 	return (error);
    616 }
    617 
    618 int
    619 tcp_ctloutput(op, so, level, optname, mp)
    620 	int op;
    621 	struct socket *so;
    622 	int level, optname;
    623 	struct mbuf **mp;
    624 {
    625 	int error = 0, s;
    626 	struct inpcb *inp;
    627 #ifdef INET6
    628 	struct in6pcb *in6p;
    629 #endif
    630 	struct tcpcb *tp;
    631 	struct mbuf *m;
    632 	int i;
    633 	int family;	/* family of the socket */
    634 
    635 	family = so->so_proto->pr_domain->dom_family;
    636 
    637 	s = splsoftnet();
    638 	switch (family) {
    639 #ifdef INET
    640 	case PF_INET:
    641 		inp = sotoinpcb(so);
    642 #ifdef INET6
    643 		in6p = NULL;
    644 #endif
    645 		break;
    646 #endif
    647 #ifdef INET6
    648 	case PF_INET6:
    649 		inp = NULL;
    650 		in6p = sotoin6pcb(so);
    651 		break;
    652 #endif
    653 	default:
    654 		splx(s);
    655 		return EAFNOSUPPORT;
    656 	}
    657 #ifndef INET6
    658 	if (inp == NULL)
    659 #else
    660 	if (inp == NULL && in6p == NULL)
    661 #endif
    662 	{
    663 		splx(s);
    664 		if (op == PRCO_SETOPT && *mp)
    665 			(void) m_free(*mp);
    666 		return (ECONNRESET);
    667 	}
    668 	if (level != IPPROTO_TCP) {
    669 		switch (family) {
    670 #ifdef INET
    671 		case PF_INET:
    672 			error = ip_ctloutput(op, so, level, optname, mp);
    673 			break;
    674 #endif
    675 #ifdef INET6
    676 		case PF_INET6:
    677 			error = ip6_ctloutput(op, so, level, optname, mp);
    678 			break;
    679 #endif
    680 		}
    681 		splx(s);
    682 		return (error);
    683 	}
    684 	if (inp)
    685 		tp = intotcpcb(inp);
    686 #ifdef INET6
    687 	else if (in6p)
    688 		tp = in6totcpcb(in6p);
    689 #endif
    690 	else
    691 		tp = NULL;
    692 
    693 	switch (op) {
    694 
    695 	case PRCO_SETOPT:
    696 		m = *mp;
    697 		switch (optname) {
    698 
    699 		case TCP_NODELAY:
    700 			if (m == NULL || m->m_len < sizeof (int))
    701 				error = EINVAL;
    702 			else if (*mtod(m, int *))
    703 				tp->t_flags |= TF_NODELAY;
    704 			else
    705 				tp->t_flags &= ~TF_NODELAY;
    706 			break;
    707 
    708 		case TCP_MAXSEG:
    709 			if (m && (i = *mtod(m, int *)) > 0 &&
    710 			    i <= tp->t_peermss)
    711 				tp->t_peermss = i;  /* limit on send size */
    712 			else
    713 				error = EINVAL;
    714 			break;
    715 
    716 		default:
    717 			error = ENOPROTOOPT;
    718 			break;
    719 		}
    720 		if (m)
    721 			(void) m_free(m);
    722 		break;
    723 
    724 	case PRCO_GETOPT:
    725 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
    726 		m->m_len = sizeof(int);
    727 		MCLAIM(m, so->so_mowner);
    728 
    729 		switch (optname) {
    730 		case TCP_NODELAY:
    731 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
    732 			break;
    733 		case TCP_MAXSEG:
    734 			*mtod(m, int *) = tp->t_peermss;
    735 			break;
    736 		default:
    737 			error = ENOPROTOOPT;
    738 			break;
    739 		}
    740 		break;
    741 	}
    742 	splx(s);
    743 	return (error);
    744 }
    745 
    746 #ifndef TCP_SENDSPACE
    747 #define	TCP_SENDSPACE	1024*16
    748 #endif
    749 int	tcp_sendspace = TCP_SENDSPACE;
    750 #ifndef TCP_RECVSPACE
    751 #define	TCP_RECVSPACE	1024*16
    752 #endif
    753 int	tcp_recvspace = TCP_RECVSPACE;
    754 
    755 /*
    756  * Attach TCP protocol to socket, allocating
    757  * internet protocol control block, tcp control block,
    758  * bufer space, and entering LISTEN state if to accept connections.
    759  */
    760 int
    761 tcp_attach(so)
    762 	struct socket *so;
    763 {
    764 	struct tcpcb *tp;
    765 	struct inpcb *inp;
    766 #ifdef INET6
    767 	struct in6pcb *in6p;
    768 #endif
    769 	int error;
    770 	int family;	/* family of the socket */
    771 
    772 	family = so->so_proto->pr_domain->dom_family;
    773 
    774 #ifdef MBUFTRACE
    775 	so->so_mowner = &tcp_mowner;
    776 	so->so_rcv.sb_mowner = &tcp_rx_mowner;
    777 	so->so_snd.sb_mowner = &tcp_tx_mowner;
    778 #endif
    779 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    780 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
    781 		if (error)
    782 			return (error);
    783 	}
    784 	switch (family) {
    785 #ifdef INET
    786 	case PF_INET:
    787 		error = in_pcballoc(so, &tcbtable);
    788 		if (error)
    789 			return (error);
    790 		inp = sotoinpcb(so);
    791 #ifdef INET6
    792 		in6p = NULL;
    793 #endif
    794 		break;
    795 #endif
    796 #ifdef INET6
    797 	case PF_INET6:
    798 		error = in6_pcballoc(so, &tcb6);
    799 		if (error)
    800 			return (error);
    801 		inp = NULL;
    802 		in6p = sotoin6pcb(so);
    803 		break;
    804 #endif
    805 	default:
    806 		return EAFNOSUPPORT;
    807 	}
    808 	if (inp)
    809 		tp = tcp_newtcpcb(family, (void *)inp);
    810 #ifdef INET6
    811 	else if (in6p)
    812 		tp = tcp_newtcpcb(family, (void *)in6p);
    813 #endif
    814 	else
    815 		tp = NULL;
    816 
    817 	if (tp == 0) {
    818 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
    819 
    820 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
    821 #ifdef INET
    822 		if (inp)
    823 			in_pcbdetach(inp);
    824 #endif
    825 #ifdef INET6
    826 		if (in6p)
    827 			in6_pcbdetach(in6p);
    828 #endif
    829 		so->so_state |= nofd;
    830 		return (ENOBUFS);
    831 	}
    832 	tp->t_state = TCPS_CLOSED;
    833 	return (0);
    834 }
    835 
    836 /*
    837  * Initiate (or continue) disconnect.
    838  * If embryonic state, just send reset (once).
    839  * If in ``let data drain'' option and linger null, just drop.
    840  * Otherwise (hard), mark socket disconnecting and drop
    841  * current input data; switch states based on user close, and
    842  * send segment to peer (with FIN).
    843  */
    844 struct tcpcb *
    845 tcp_disconnect(tp)
    846 	struct tcpcb *tp;
    847 {
    848 	struct socket *so;
    849 
    850 	if (tp->t_inpcb)
    851 		so = tp->t_inpcb->inp_socket;
    852 #ifdef INET6
    853 	else if (tp->t_in6pcb)
    854 		so = tp->t_in6pcb->in6p_socket;
    855 #endif
    856 	else
    857 		so = NULL;
    858 
    859 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    860 		tp = tcp_close(tp);
    861 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    862 		tp = tcp_drop(tp, 0);
    863 	else {
    864 		soisdisconnecting(so);
    865 		sbflush(&so->so_rcv);
    866 		tp = tcp_usrclosed(tp);
    867 		if (tp)
    868 			(void) tcp_output(tp);
    869 	}
    870 	return (tp);
    871 }
    872 
    873 /*
    874  * User issued close, and wish to trail through shutdown states:
    875  * if never received SYN, just forget it.  If got a SYN from peer,
    876  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
    877  * If already got a FIN from peer, then almost done; go to LAST_ACK
    878  * state.  In all other cases, have already sent FIN to peer (e.g.
    879  * after PRU_SHUTDOWN), and just have to play tedious game waiting
    880  * for peer to send FIN or not respond to keep-alives, etc.
    881  * We can let the user exit from the close as soon as the FIN is acked.
    882  */
    883 struct tcpcb *
    884 tcp_usrclosed(tp)
    885 	struct tcpcb *tp;
    886 {
    887 
    888 	switch (tp->t_state) {
    889 
    890 	case TCPS_CLOSED:
    891 	case TCPS_LISTEN:
    892 	case TCPS_SYN_SENT:
    893 		tp->t_state = TCPS_CLOSED;
    894 		tp = tcp_close(tp);
    895 		break;
    896 
    897 	case TCPS_SYN_RECEIVED:
    898 	case TCPS_ESTABLISHED:
    899 		tp->t_state = TCPS_FIN_WAIT_1;
    900 		break;
    901 
    902 	case TCPS_CLOSE_WAIT:
    903 		tp->t_state = TCPS_LAST_ACK;
    904 		break;
    905 	}
    906 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
    907 		struct socket *so;
    908 		if (tp->t_inpcb)
    909 			so = tp->t_inpcb->inp_socket;
    910 #ifdef INET6
    911 		else if (tp->t_in6pcb)
    912 			so = tp->t_in6pcb->in6p_socket;
    913 #endif
    914 		else
    915 			so = NULL;
    916 		soisdisconnected(so);
    917 		/*
    918 		 * If we are in FIN_WAIT_2, we arrived here because the
    919 		 * application did a shutdown of the send side.  Like the
    920 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
    921 		 * a full close, we start a timer to make sure sockets are
    922 		 * not left in FIN_WAIT_2 forever.
    923 		 */
    924 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0))
    925 			TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
    926 	}
    927 	return (tp);
    928 }
    929 
    930 static const struct {
    931 	 unsigned int valid : 1;
    932 	 unsigned int rdonly : 1;
    933 	 int *var;
    934 	 int val;
    935 	 } tcp_ctlvars[] = TCPCTL_VARIABLES;
    936 
    937 /*
    938  * Sysctl for tcp variables.
    939  */
    940 int
    941 tcp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    942 	int *name;
    943 	u_int namelen;
    944 	void *oldp;
    945 	size_t *oldlenp;
    946 	void *newp;
    947 	size_t newlen;
    948 {
    949 	int error, saved_value = 0;
    950 
    951 	if (name[0] == TCPCTL_IDENT)
    952 		return tcp_sysctl_ident(&name[1], namelen - 1, oldp, oldlenp,
    953 		    newp, newlen);
    954 
    955 	/* All remaining sysctl names at this level are terminal. */
    956 	if (namelen != 1)
    957 		return (ENOTDIR);
    958 
    959 	if (name[0] < sizeof(tcp_ctlvars)/sizeof(tcp_ctlvars[0])
    960 	    && tcp_ctlvars[name[0]].valid) {
    961 		if (tcp_ctlvars[name[0]].rdonly) {
    962 			return (sysctl_rdint(oldp, oldlenp, newp,
    963 			    tcp_ctlvars[name[0]].val));
    964 		} else {
    965 			switch (name[0]) {
    966 			case TCPCTL_MSSDFLT:
    967 				saved_value = tcp_mssdflt;
    968 				break;
    969 			}
    970 			error = sysctl_int(oldp, oldlenp, newp, newlen,
    971 			    tcp_ctlvars[name[0]].var);
    972 			if (error)
    973 				return (error);
    974 			switch (name[0]) {
    975 			case TCPCTL_MSSDFLT:
    976 				if (tcp_mssdflt < 32) {
    977 					tcp_mssdflt = saved_value;
    978 					return (EINVAL);
    979 				}
    980 				break;
    981 			}
    982 			return (0);
    983 		}
    984 	}
    985 
    986 	return (ENOPROTOOPT);
    987 }
    988 
    989 
    990 static int
    991 tcp_sysctl_ident(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    992     void *newp, size_t newlen)
    993 {
    994 	struct inpcb *inb;
    995 	struct in_addr laddr, raddr;
    996 	u_int lport, rport;
    997 	uid_t uid;
    998 	int error;
    999 
   1000 	if (*oldlenp != sizeof(uid_t))
   1001 		return ENOMEM;
   1002 	if (!oldp || *oldlenp != sizeof(uid_t))
   1003 		return ENOMEM;
   1004 	if (namelen != 4)
   1005 		return EINVAL;
   1006 
   1007 	raddr.s_addr = (uint32_t)name[0];
   1008 	rport = (u_int)name[1];
   1009 	laddr.s_addr = (uint32_t)name[2];
   1010 	lport = (u_int)name[3];
   1011 
   1012 	inb = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport);
   1013 	if (inb) {
   1014 		struct socket *sockp = inb->inp_socket;
   1015 		if (sockp)
   1016 			uid = sockp->so_uid;
   1017 		else
   1018 			return ESRCH;
   1019 	} else
   1020 		return ESRCH;
   1021 
   1022 	if ((error = copyout(&uid, oldp, sizeof(uid))) != 0)
   1023 		return error;
   1024 
   1025 	return 0;
   1026 }
   1027