Home | History | Annotate | Line # | Download | only in netinet
tcp_usrreq.c revision 1.62
      1 /*	$NetBSD: tcp_usrreq.c,v 1.62 2001/07/03 08:06:19 itojun 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 "opt_inet.h"
    105 #include "opt_ipsec.h"
    106 
    107 #include <sys/param.h>
    108 #include <sys/systm.h>
    109 #include <sys/kernel.h>
    110 #include <sys/malloc.h>
    111 #include <sys/mbuf.h>
    112 #include <sys/socket.h>
    113 #include <sys/socketvar.h>
    114 #include <sys/protosw.h>
    115 #include <sys/errno.h>
    116 #include <sys/stat.h>
    117 #include <sys/proc.h>
    118 #include <sys/ucred.h>
    119 #include <sys/domain.h>
    120 
    121 #include <uvm/uvm_extern.h>
    122 #include <sys/sysctl.h>
    123 
    124 #include <net/if.h>
    125 #include <net/route.h>
    126 
    127 #include <netinet/in.h>
    128 #include <netinet/in_systm.h>
    129 #include <netinet/in_var.h>
    130 #include <netinet/ip.h>
    131 #include <netinet/in_pcb.h>
    132 #include <netinet/ip_var.h>
    133 
    134 #ifdef INET6
    135 #ifndef INET
    136 #include <netinet/in.h>
    137 #endif
    138 #include <netinet/ip6.h>
    139 #include <netinet6/in6_pcb.h>
    140 #include <netinet6/ip6_var.h>
    141 #endif
    142 
    143 #include <netinet/tcp.h>
    144 #include <netinet/tcp_fsm.h>
    145 #include <netinet/tcp_seq.h>
    146 #include <netinet/tcp_timer.h>
    147 #include <netinet/tcp_var.h>
    148 #include <netinet/tcpip.h>
    149 #include <netinet/tcp_debug.h>
    150 
    151 #include "opt_tcp_recvspace.h"
    152 #include "opt_tcp_sendspace.h"
    153 
    154 #ifdef IPSEC
    155 #include <netinet6/ipsec.h>
    156 #endif /*IPSEC*/
    157 
    158 /*
    159  * TCP protocol interface to socket abstraction.
    160  */
    161 extern	char *tcpstates[];
    162 
    163 /*
    164  * Process a TCP user request for TCP tb.  If this is a send request
    165  * then m is the mbuf chain of send data.  If this is a timer expiration
    166  * (called from the software clock routine), then timertype tells which timer.
    167  */
    168 /*ARGSUSED*/
    169 int
    170 tcp_usrreq(so, req, m, nam, control, p)
    171 	struct socket *so;
    172 	int req;
    173 	struct mbuf *m, *nam, *control;
    174 	struct proc *p;
    175 {
    176 	struct inpcb *inp;
    177 #ifdef INET6
    178 	struct in6pcb *in6p;
    179 #endif
    180 	struct tcpcb *tp = NULL;
    181 	int s;
    182 	int error = 0;
    183 	int ostate;
    184 	int family;	/* family of the socket */
    185 
    186 	family = so->so_proto->pr_domain->dom_family;
    187 
    188 	if (req == PRU_CONTROL) {
    189 		switch (family) {
    190 #ifdef INET
    191 		case PF_INET:
    192 			return (in_control(so, (long)m, (caddr_t)nam,
    193 			    (struct ifnet *)control, p));
    194 #endif
    195 #ifdef INET6
    196 		case PF_INET6:
    197 			return (in6_control(so, (long)m, (caddr_t)nam,
    198 			    (struct ifnet *)control, p));
    199 #endif
    200 		default:
    201 			return EAFNOSUPPORT;
    202 		}
    203 	}
    204 
    205 	if (req == PRU_PURGEIF) {
    206 		switch (family) {
    207 #ifdef INET
    208 		case PF_INET:
    209 			in_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
    210 			in_purgeif((struct ifnet *)control);
    211 			in_pcbpurgeif(&tcbtable, (struct ifnet *)control);
    212 			break;
    213 #endif
    214 #ifdef INET6
    215 		case PF_INET6:
    216 			in6_pcbpurgeif0(&tcb6, (struct ifnet *)control);
    217 			in6_purgeif((struct ifnet *)control);
    218 			in6_pcbpurgeif(&tcb6, (struct ifnet *)control);
    219 			break;
    220 #endif
    221 		default:
    222 			return (EAFNOSUPPORT);
    223 		}
    224 		return (0);
    225 	}
    226 
    227 	s = splsoftnet();
    228 	switch (family) {
    229 #ifdef INET
    230 	case PF_INET:
    231 		inp = sotoinpcb(so);
    232 #ifdef INET6
    233 		in6p = NULL;
    234 #endif
    235 		break;
    236 #endif
    237 #ifdef INET6
    238 	case PF_INET6:
    239 		inp = NULL;
    240 		in6p = sotoin6pcb(so);
    241 		break;
    242 #endif
    243 	default:
    244 		splx(s);
    245 		return EAFNOSUPPORT;
    246 	}
    247 
    248 #ifdef DIAGNOSTIC
    249 #ifdef INET6
    250 	if (inp && in6p)
    251 		panic("tcp_usrreq: both inp and in6p set to non-NULL");
    252 #endif
    253 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    254 		panic("tcp_usrreq: unexpected control mbuf");
    255 #endif
    256 	/*
    257 	 * When a TCP is attached to a socket, then there will be
    258 	 * a (struct inpcb) pointed at by the socket, and this
    259 	 * structure will point at a subsidary (struct tcpcb).
    260 	 */
    261 #ifndef INET6
    262 	if (inp == 0 && req != PRU_ATTACH)
    263 #else
    264 	if ((inp == 0 && in6p == 0) && req != PRU_ATTACH)
    265 #endif
    266 	{
    267 		error = EINVAL;
    268 		goto release;
    269 	}
    270 #ifdef INET
    271 	if (inp) {
    272 		tp = intotcpcb(inp);
    273 		/* WHAT IF TP IS 0? */
    274 #ifdef KPROF
    275 		tcp_acounts[tp->t_state][req]++;
    276 #endif
    277 		ostate = tp->t_state;
    278 	}
    279 #endif
    280 #ifdef INET6
    281 	if (in6p) {
    282 		tp = in6totcpcb(in6p);
    283 		/* WHAT IF TP IS 0? */
    284 #ifdef KPROF
    285 		tcp_acounts[tp->t_state][req]++;
    286 #endif
    287 		ostate = tp->t_state;
    288 	}
    289 #endif
    290 	else
    291 		ostate = 0;
    292 
    293 	switch (req) {
    294 
    295 	/*
    296 	 * TCP attaches to socket via PRU_ATTACH, reserving space,
    297 	 * and an internet control block.
    298 	 */
    299 	case PRU_ATTACH:
    300 #ifndef INET6
    301 		if (inp != 0)
    302 #else
    303 		if (inp != 0 || in6p != 0)
    304 #endif
    305 		{
    306 			error = EISCONN;
    307 			break;
    308 		}
    309 		error = tcp_attach(so);
    310 		if (error)
    311 			break;
    312 		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    313 			so->so_linger = TCP_LINGERTIME;
    314 		tp = sototcpcb(so);
    315 		break;
    316 
    317 	/*
    318 	 * PRU_DETACH detaches the TCP protocol from the socket.
    319 	 */
    320 	case PRU_DETACH:
    321 		tp = tcp_disconnect(tp);
    322 		break;
    323 
    324 	/*
    325 	 * Give the socket an address.
    326 	 */
    327 	case PRU_BIND:
    328 		switch (family) {
    329 #ifdef INET
    330 		case PF_INET:
    331 			error = in_pcbbind(inp, nam, p);
    332 			break;
    333 #endif
    334 #ifdef INET6
    335 		case PF_INET6:
    336 			error = in6_pcbbind(in6p, nam, p);
    337 			if (!error) {
    338 				/* mapped addr case */
    339 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    340 					tp->t_family = AF_INET;
    341 				else
    342 					tp->t_family = AF_INET6;
    343 			}
    344 			break;
    345 #endif
    346 		}
    347 		break;
    348 
    349 	/*
    350 	 * Prepare to accept connections.
    351 	 */
    352 	case PRU_LISTEN:
    353 #ifdef INET
    354 		if (inp && inp->inp_lport == 0) {
    355 			error = in_pcbbind(inp, (struct mbuf *)0,
    356 			    (struct proc *)0);
    357 			if (error)
    358 				break;
    359 		}
    360 #endif
    361 #ifdef INET6
    362 		if (in6p && in6p->in6p_lport == 0) {
    363 			error = in6_pcbbind(in6p, (struct mbuf *)0,
    364 			    (struct proc *)0);
    365 			if (error)
    366 				break;
    367 		}
    368 #endif
    369 		tp->t_state = TCPS_LISTEN;
    370 		break;
    371 
    372 	/*
    373 	 * Initiate connection to peer.
    374 	 * Create a template for use in transmissions on this connection.
    375 	 * Enter SYN_SENT state, and mark socket as connecting.
    376 	 * Start keep-alive timer, and seed output sequence space.
    377 	 * Send initial segment on connection.
    378 	 */
    379 	case PRU_CONNECT:
    380 #ifdef INET
    381 		if (inp) {
    382 			if (inp->inp_lport == 0) {
    383 				error = in_pcbbind(inp, (struct mbuf *)0,
    384 				    (struct proc *)0);
    385 				if (error)
    386 					break;
    387 			}
    388 			error = in_pcbconnect(inp, nam);
    389 		}
    390 #endif
    391 #ifdef INET6
    392 		if (in6p) {
    393 			if (in6p->in6p_lport == 0) {
    394 				error = in6_pcbbind(in6p, (struct mbuf *)0,
    395 				    (struct proc *)0);
    396 				if (error)
    397 					break;
    398 			}
    399 			error = in6_pcbconnect(in6p, nam);
    400 			if (!error) {
    401 				/* mapped addr case */
    402 				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    403 					tp->t_family = AF_INET;
    404 				else
    405 					tp->t_family = AF_INET6;
    406 			}
    407 		}
    408 #endif
    409 		if (error)
    410 			break;
    411 		tp->t_template = tcp_template(tp);
    412 		if (tp->t_template == 0) {
    413 #ifdef INET
    414 			if (inp)
    415 				in_pcbdisconnect(inp);
    416 #endif
    417 #ifdef INET6
    418 			if (in6p)
    419 				in6_pcbdisconnect(in6p);
    420 #endif
    421 			error = ENOBUFS;
    422 			break;
    423 		}
    424 		/* Compute window scaling to request.  */
    425 		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
    426 		    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
    427 			tp->request_r_scale++;
    428 		soisconnecting(so);
    429 		tcpstat.tcps_connattempt++;
    430 		tp->t_state = TCPS_SYN_SENT;
    431 		TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
    432 		tp->iss = tcp_new_iss(tp, 0);
    433 		tcp_sendseqinit(tp);
    434 		error = tcp_output(tp);
    435 		break;
    436 
    437 	/*
    438 	 * Create a TCP connection between two sockets.
    439 	 */
    440 	case PRU_CONNECT2:
    441 		error = EOPNOTSUPP;
    442 		break;
    443 
    444 	/*
    445 	 * Initiate disconnect from peer.
    446 	 * If connection never passed embryonic stage, just drop;
    447 	 * else if don't need to let data drain, then can just drop anyways,
    448 	 * else have to begin TCP shutdown process: mark socket disconnecting,
    449 	 * drain unread data, state switch to reflect user close, and
    450 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
    451 	 * when peer sends FIN and acks ours.
    452 	 *
    453 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
    454 	 */
    455 	case PRU_DISCONNECT:
    456 		tp = tcp_disconnect(tp);
    457 		break;
    458 
    459 	/*
    460 	 * Accept a connection.  Essentially all the work is
    461 	 * done at higher levels; just return the address
    462 	 * of the peer, storing through addr.
    463 	 */
    464 	case PRU_ACCEPT:
    465 #ifdef INET
    466 		if (inp)
    467 			in_setpeeraddr(inp, nam);
    468 #endif
    469 #ifdef INET6
    470 		if (in6p)
    471 			in6_setpeeraddr(in6p, nam);
    472 #endif
    473 		break;
    474 
    475 	/*
    476 	 * Mark the connection as being incapable of further output.
    477 	 */
    478 	case PRU_SHUTDOWN:
    479 		socantsendmore(so);
    480 		tp = tcp_usrclosed(tp);
    481 		if (tp)
    482 			error = tcp_output(tp);
    483 		break;
    484 
    485 	/*
    486 	 * After a receive, possibly send window update to peer.
    487 	 */
    488 	case PRU_RCVD:
    489 		/*
    490 		 * soreceive() calls this function when a user receives
    491 		 * ancillary data on a listening socket. We don't call
    492 		 * tcp_output in such a case, since there is no header
    493 		 * template for a listening socket and hence the kernel
    494 		 * will panic.
    495 		 */
    496 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
    497 			(void) tcp_output(tp);
    498 		break;
    499 
    500 	/*
    501 	 * Do a send by putting data in output queue and updating urgent
    502 	 * marker if URG set.  Possibly send more data.
    503 	 */
    504 	case PRU_SEND:
    505 		if (control && control->m_len) {
    506 			m_freem(control);
    507 			m_freem(m);
    508 			error = EINVAL;
    509 			break;
    510 		}
    511 		sbappend(&so->so_snd, m);
    512 		error = tcp_output(tp);
    513 		break;
    514 
    515 	/*
    516 	 * Abort the TCP.
    517 	 */
    518 	case PRU_ABORT:
    519 		tp = tcp_drop(tp, ECONNABORTED);
    520 		break;
    521 
    522 	case PRU_SENSE:
    523 		/*
    524 		 * stat: don't bother with a blocksize.
    525 		 */
    526 		splx(s);
    527 		return (0);
    528 
    529 	case PRU_RCVOOB:
    530 		if (control && control->m_len) {
    531 			m_freem(control);
    532 			m_freem(m);
    533 			error = EINVAL;
    534 			break;
    535 		}
    536 		if ((so->so_oobmark == 0 &&
    537 		    (so->so_state & SS_RCVATMARK) == 0) ||
    538 		    so->so_options & SO_OOBINLINE ||
    539 		    tp->t_oobflags & TCPOOB_HADDATA) {
    540 			error = EINVAL;
    541 			break;
    542 		}
    543 		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
    544 			error = EWOULDBLOCK;
    545 			break;
    546 		}
    547 		m->m_len = 1;
    548 		*mtod(m, caddr_t) = tp->t_iobc;
    549 		if (((long)nam & MSG_PEEK) == 0)
    550 			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
    551 		break;
    552 
    553 	case PRU_SENDOOB:
    554 		if (sbspace(&so->so_snd) < -512) {
    555 			m_freem(m);
    556 			error = ENOBUFS;
    557 			break;
    558 		}
    559 		/*
    560 		 * According to RFC961 (Assigned Protocols),
    561 		 * the urgent pointer points to the last octet
    562 		 * of urgent data.  We continue, however,
    563 		 * to consider it to indicate the first octet
    564 		 * of data past the urgent section.
    565 		 * Otherwise, snd_up should be one lower.
    566 		 */
    567 		sbappend(&so->so_snd, m);
    568 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
    569 		tp->t_force = 1;
    570 		error = tcp_output(tp);
    571 		tp->t_force = 0;
    572 		break;
    573 
    574 	case PRU_SOCKADDR:
    575 #ifdef INET
    576 		if (inp)
    577 			in_setsockaddr(inp, nam);
    578 #endif
    579 #ifdef INET6
    580 		if (in6p)
    581 			in6_setsockaddr(in6p, nam);
    582 #endif
    583 		break;
    584 
    585 	case PRU_PEERADDR:
    586 #ifdef INET
    587 		if (inp)
    588 			in_setpeeraddr(inp, nam);
    589 #endif
    590 #ifdef INET6
    591 		if (in6p)
    592 			in6_setpeeraddr(in6p, nam);
    593 #endif
    594 		break;
    595 
    596 	/*
    597 	 * TCP slow timer went off; going through this
    598 	 * routine for tracing's sake.
    599 	 */
    600 	case PRU_SLOWTIMO:
    601 		tp = tcp_timers(tp, (long)nam);
    602 		req |= (long)nam << 8;		/* for debug's sake */
    603 		break;
    604 
    605 	default:
    606 		panic("tcp_usrreq");
    607 	}
    608 	if (tp && (so->so_options & SO_DEBUG))
    609 		tcp_trace(TA_USER, ostate, tp, NULL, req);
    610 
    611 release:
    612 	splx(s);
    613 	return (error);
    614 }
    615 
    616 int
    617 tcp_ctloutput(op, so, level, optname, mp)
    618 	int op;
    619 	struct socket *so;
    620 	int level, optname;
    621 	struct mbuf **mp;
    622 {
    623 	int error = 0, s;
    624 	struct inpcb *inp;
    625 #ifdef INET6
    626 	struct in6pcb *in6p;
    627 #endif
    628 	struct tcpcb *tp;
    629 	struct mbuf *m;
    630 	int i;
    631 	int family;	/* family of the socket */
    632 
    633 	family = so->so_proto->pr_domain->dom_family;
    634 
    635 	s = splsoftnet();
    636 	switch (family) {
    637 #ifdef INET
    638 	case PF_INET:
    639 		inp = sotoinpcb(so);
    640 #ifdef INET6
    641 		in6p = NULL;
    642 #endif
    643 		break;
    644 #endif
    645 #ifdef INET6
    646 	case PF_INET6:
    647 		inp = NULL;
    648 		in6p = sotoin6pcb(so);
    649 		break;
    650 #endif
    651 	default:
    652 		splx(s);
    653 		return EAFNOSUPPORT;
    654 	}
    655 #ifndef INET6
    656 	if (inp == NULL)
    657 #else
    658 	if (inp == NULL && in6p == NULL)
    659 #endif
    660 	{
    661 		splx(s);
    662 		if (op == PRCO_SETOPT && *mp)
    663 			(void) m_free(*mp);
    664 		return (ECONNRESET);
    665 	}
    666 	if (level != IPPROTO_TCP) {
    667 		switch (family) {
    668 #ifdef INET
    669 		case PF_INET:
    670 			error = ip_ctloutput(op, so, level, optname, mp);
    671 			break;
    672 #endif
    673 #ifdef INET6
    674 		case PF_INET6:
    675 			error = ip6_ctloutput(op, so, level, optname, mp);
    676 			break;
    677 #endif
    678 		}
    679 		splx(s);
    680 		return (error);
    681 	}
    682 	if (inp)
    683 		tp = intotcpcb(inp);
    684 #ifdef INET6
    685 	else if (in6p)
    686 		tp = in6totcpcb(in6p);
    687 #endif
    688 	else
    689 		tp = NULL;
    690 
    691 	switch (op) {
    692 
    693 	case PRCO_SETOPT:
    694 		m = *mp;
    695 		switch (optname) {
    696 
    697 		case TCP_NODELAY:
    698 			if (m == NULL || m->m_len < sizeof (int))
    699 				error = EINVAL;
    700 			else if (*mtod(m, int *))
    701 				tp->t_flags |= TF_NODELAY;
    702 			else
    703 				tp->t_flags &= ~TF_NODELAY;
    704 			break;
    705 
    706 		case TCP_MAXSEG:
    707 			if (m && (i = *mtod(m, int *)) > 0 &&
    708 			    i <= tp->t_peermss)
    709 				tp->t_peermss = i;  /* limit on send size */
    710 			else
    711 				error = EINVAL;
    712 			break;
    713 
    714 		default:
    715 			error = ENOPROTOOPT;
    716 			break;
    717 		}
    718 		if (m)
    719 			(void) m_free(m);
    720 		break;
    721 
    722 	case PRCO_GETOPT:
    723 		*mp = m = m_get(M_WAIT, MT_SOOPTS);
    724 		m->m_len = sizeof(int);
    725 
    726 		switch (optname) {
    727 		case TCP_NODELAY:
    728 			*mtod(m, int *) = tp->t_flags & TF_NODELAY;
    729 			break;
    730 		case TCP_MAXSEG:
    731 			*mtod(m, int *) = tp->t_peermss;
    732 			break;
    733 		default:
    734 			error = ENOPROTOOPT;
    735 			break;
    736 		}
    737 		break;
    738 	}
    739 	splx(s);
    740 	return (error);
    741 }
    742 
    743 #ifndef TCP_SENDSPACE
    744 #define	TCP_SENDSPACE	1024*16;
    745 #endif
    746 int	tcp_sendspace = TCP_SENDSPACE;
    747 #ifndef TCP_RECVSPACE
    748 #define	TCP_RECVSPACE	1024*16;
    749 #endif
    750 int	tcp_recvspace = TCP_RECVSPACE;
    751 
    752 /*
    753  * Attach TCP protocol to socket, allocating
    754  * internet protocol control block, tcp control block,
    755  * bufer space, and entering LISTEN state if to accept connections.
    756  */
    757 int
    758 tcp_attach(so)
    759 	struct socket *so;
    760 {
    761 	struct tcpcb *tp;
    762 	struct inpcb *inp;
    763 #ifdef INET6
    764 	struct in6pcb *in6p;
    765 #endif
    766 	int error;
    767 	int family;	/* family of the socket */
    768 
    769 	family = so->so_proto->pr_domain->dom_family;
    770 
    771 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    772 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
    773 		if (error)
    774 			return (error);
    775 	}
    776 	switch (family) {
    777 #ifdef INET
    778 	case PF_INET:
    779 		error = in_pcballoc(so, &tcbtable);
    780 		if (error)
    781 			return (error);
    782 		inp = sotoinpcb(so);
    783 #ifdef INET6
    784 		in6p = NULL;
    785 #endif
    786 		break;
    787 #endif
    788 #ifdef INET6
    789 	case PF_INET6:
    790 		error = in6_pcballoc(so, &tcb6);
    791 		if (error)
    792 			return (error);
    793 		inp = NULL;
    794 		in6p = sotoin6pcb(so);
    795 		break;
    796 #endif
    797 	default:
    798 		return EAFNOSUPPORT;
    799 	}
    800 #ifdef IPSEC
    801 #ifdef INET
    802 	if (inp) {
    803 		error = ipsec_init_policy(so, &inp->inp_sp);
    804 		if (error != 0) {
    805 			in_pcbdetach(inp);
    806 			return (error);
    807 		}
    808 	}
    809 #endif
    810 #ifdef INET6
    811 	if (in6p) {
    812 		error = ipsec_init_policy(so, &in6p->in6p_sp);
    813 		if (error != 0) {
    814 			in6_pcbdetach(in6p);
    815 			return (error);
    816 		}
    817 	}
    818 #endif
    819 #endif /*IPSEC*/
    820 	if (inp)
    821 		tp = tcp_newtcpcb(family, (void *)inp);
    822 #ifdef INET6
    823 	else if (in6p)
    824 		tp = tcp_newtcpcb(family, (void *)in6p);
    825 #endif
    826 	else
    827 		tp = NULL;
    828 
    829 	if (tp == 0) {
    830 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
    831 
    832 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
    833 #ifdef INET
    834 		if (inp)
    835 			in_pcbdetach(inp);
    836 #endif
    837 #ifdef INET6
    838 		if (in6p)
    839 			in6_pcbdetach(in6p);
    840 #endif
    841 		so->so_state |= nofd;
    842 		return (ENOBUFS);
    843 	}
    844 	tp->t_state = TCPS_CLOSED;
    845 	return (0);
    846 }
    847 
    848 /*
    849  * Initiate (or continue) disconnect.
    850  * If embryonic state, just send reset (once).
    851  * If in ``let data drain'' option and linger null, just drop.
    852  * Otherwise (hard), mark socket disconnecting and drop
    853  * current input data; switch states based on user close, and
    854  * send segment to peer (with FIN).
    855  */
    856 struct tcpcb *
    857 tcp_disconnect(tp)
    858 	struct tcpcb *tp;
    859 {
    860 	struct socket *so;
    861 
    862 	if (tp->t_inpcb)
    863 		so = tp->t_inpcb->inp_socket;
    864 #ifdef INET6
    865 	else if (tp->t_in6pcb)
    866 		so = tp->t_in6pcb->in6p_socket;
    867 #endif
    868 	else
    869 		so = NULL;
    870 
    871 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    872 		tp = tcp_close(tp);
    873 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
    874 		tp = tcp_drop(tp, 0);
    875 	else {
    876 		soisdisconnecting(so);
    877 		sbflush(&so->so_rcv);
    878 		tp = tcp_usrclosed(tp);
    879 		if (tp)
    880 			(void) tcp_output(tp);
    881 	}
    882 	return (tp);
    883 }
    884 
    885 /*
    886  * User issued close, and wish to trail through shutdown states:
    887  * if never received SYN, just forget it.  If got a SYN from peer,
    888  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
    889  * If already got a FIN from peer, then almost done; go to LAST_ACK
    890  * state.  In all other cases, have already sent FIN to peer (e.g.
    891  * after PRU_SHUTDOWN), and just have to play tedious game waiting
    892  * for peer to send FIN or not respond to keep-alives, etc.
    893  * We can let the user exit from the close as soon as the FIN is acked.
    894  */
    895 struct tcpcb *
    896 tcp_usrclosed(tp)
    897 	struct tcpcb *tp;
    898 {
    899 
    900 	switch (tp->t_state) {
    901 
    902 	case TCPS_CLOSED:
    903 	case TCPS_LISTEN:
    904 	case TCPS_SYN_SENT:
    905 		tp->t_state = TCPS_CLOSED;
    906 		tp = tcp_close(tp);
    907 		break;
    908 
    909 	case TCPS_SYN_RECEIVED:
    910 	case TCPS_ESTABLISHED:
    911 		tp->t_state = TCPS_FIN_WAIT_1;
    912 		break;
    913 
    914 	case TCPS_CLOSE_WAIT:
    915 		tp->t_state = TCPS_LAST_ACK;
    916 		break;
    917 	}
    918 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
    919 		struct socket *so;
    920 		if (tp->t_inpcb)
    921 			so = tp->t_inpcb->inp_socket;
    922 #ifdef INET6
    923 		else if (tp->t_in6pcb)
    924 			so = tp->t_in6pcb->in6p_socket;
    925 #endif
    926 		else
    927 			so = NULL;
    928 		soisdisconnected(so);
    929 		/*
    930 		 * If we are in FIN_WAIT_2, we arrived here because the
    931 		 * application did a shutdown of the send side.  Like the
    932 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
    933 		 * a full close, we start a timer to make sure sockets are
    934 		 * not left in FIN_WAIT_2 forever.
    935 		 */
    936 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0))
    937 			TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
    938 	}
    939 	return (tp);
    940 }
    941 
    942 static const struct {
    943 	 unsigned int valid : 1;
    944 	 unsigned int rdonly : 1;
    945 	 int *var;
    946 	 int val;
    947 	 } tcp_ctlvars[] = TCPCTL_VARIABLES;
    948 
    949 /*
    950  * Sysctl for tcp variables.
    951  */
    952 int
    953 tcp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    954 	int *name;
    955 	u_int namelen;
    956 	void *oldp;
    957 	size_t *oldlenp;
    958 	void *newp;
    959 	size_t newlen;
    960 {
    961 
    962 	/* All sysctl names at this level are terminal. */
    963 	if (namelen != 1)
    964 		return (ENOTDIR);
    965 
    966 	if (name[0] < sizeof(tcp_ctlvars)/sizeof(tcp_ctlvars[0])
    967 	    && tcp_ctlvars[name[0]].valid) {
    968 		if (tcp_ctlvars[name[0]].rdonly)
    969 			return (sysctl_rdint(oldp, oldlenp, newp,
    970 			    tcp_ctlvars[name[0]].val));
    971 		else
    972 			return (sysctl_int(oldp, oldlenp, newp, newlen,
    973 			    tcp_ctlvars[name[0]].var));
    974 	}
    975 
    976 	return (ENOPROTOOPT);
    977 }
    978