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