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