xref: /freebsd/sys/netinet/tcp_usrreq.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.
4  * Copyright (c) 2006 Robert N. M. Watson
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  * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
32  * $FreeBSD$
33  */
34 
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 #include "opt_tcpdebug.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <sys/mbuf.h>
45 #ifdef INET6
46 #include <sys/domain.h>
47 #endif /* INET6 */
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/proc.h>
52 #include <sys/jail.h>
53 
54 #include <net/if.h>
55 #include <net/route.h>
56 
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #ifdef INET6
60 #include <netinet/ip6.h>
61 #endif
62 #include <netinet/in_pcb.h>
63 #ifdef INET6
64 #include <netinet6/in6_pcb.h>
65 #endif
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
68 #ifdef INET6
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/scope6_var.h>
71 #endif
72 #include <netinet/tcp.h>
73 #include <netinet/tcp_fsm.h>
74 #include <netinet/tcp_seq.h>
75 #include <netinet/tcp_timer.h>
76 #include <netinet/tcp_var.h>
77 #include <netinet/tcpip.h>
78 #ifdef TCPDEBUG
79 #include <netinet/tcp_debug.h>
80 #endif
81 
82 /*
83  * TCP protocol interface to socket abstraction.
84  */
85 extern	char *tcpstates[];	/* XXX ??? */
86 
87 static int	tcp_attach(struct socket *);
88 static int	tcp_connect(struct tcpcb *, struct sockaddr *,
89 		    struct thread *td);
90 #ifdef INET6
91 static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
92 		    struct thread *td);
93 #endif /* INET6 */
94 static void	tcp_disconnect(struct tcpcb *);
95 static void	tcp_usrclosed(struct tcpcb *);
96 static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
97 
98 #ifdef TCPDEBUG
99 #define	TCPDEBUG0	int ostate = 0
100 #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
101 #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
102 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
103 #else
104 #define	TCPDEBUG0
105 #define	TCPDEBUG1()
106 #define	TCPDEBUG2(req)
107 #endif
108 
109 /*
110  * TCP attaches to socket via pru_attach(), reserving space,
111  * and an internet control block.
112  */
113 static int
114 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
115 {
116 	struct inpcb *inp;
117 	struct tcpcb *tp = NULL;
118 	int error;
119 	TCPDEBUG0;
120 
121 	inp = sotoinpcb(so);
122 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
123 	TCPDEBUG1();
124 
125 	error = tcp_attach(so);
126 	if (error)
127 		goto out;
128 
129 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
130 		so->so_linger = TCP_LINGERTIME;
131 
132 	inp = sotoinpcb(so);
133 	tp = intotcpcb(inp);
134 out:
135 	TCPDEBUG2(PRU_ATTACH);
136 	return error;
137 }
138 
139 /*
140  * tcp_detach() releases any protocol state that can be reasonably released
141  * when a socket shutdown is requested, and is a shared code path for
142  * tcp_usr_detach() and tcp_usr_abort(), the two socket close entry points.
143  *
144  * Accepts pcbinfo, inpcb locked, will unlock the inpcb (if needed) on
145  * return.
146  */
147 static void
148 tcp_detach(struct socket *so, struct inpcb *inp)
149 {
150 	struct tcpcb *tp;
151 #ifdef INET6
152 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
153 #endif
154 
155 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
156 	INP_LOCK_ASSERT(inp);
157 
158 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
159 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
160 
161 	if (inp->inp_vflag & INP_TIMEWAIT) {
162 		if (inp->inp_vflag & INP_DROPPED) {
163 			/*
164 			 * Connection was in time wait and has been dropped;
165 			 * the calling path is either via tcp_twclose(), or
166 			 * as a result of an eventual soclose() after
167 			 * tcp_twclose() has been called.  In either case,
168 			 * tcp_twclose() has detached the tcptw from the
169 			 * inpcb, so we just detach and free the inpcb.
170 			 *
171 			 * XXXRW: Would it be cleaner to free the tcptw
172 			 * here?
173 			 */
174 #ifdef INET6
175 			if (isipv6) {
176 				in6_pcbdetach(inp);
177 				in6_pcbfree(inp);
178 			} else {
179 #endif
180 				in_pcbdetach(inp);
181 				in_pcbfree(inp);
182 #ifdef INET6
183 			}
184 #endif
185 		} else {
186 			/*
187 			 * Connection is in time wait and has not yet been
188 			 * dropped; allow the socket to be discarded, but
189 			 * need to keep inpcb until end of time wait.
190 			 */
191 #ifdef INET6
192 			if (isipv6)
193 				in6_pcbdetach(inp);
194 			else
195 #endif
196 				in_pcbdetach(inp);
197 			INP_UNLOCK(inp);
198 		}
199 	} else {
200 		/*
201 		 * If not in timewait, there are two possible paths.  First,
202 		 * the TCP connection is either embryonic or done, in which
203 		 * case we tear down all state.  Second, it may still be
204 		 * active, in which case we acquire a reference to the socket
205 		 * and will free it later when TCP is done.
206 		 */
207 		tp = intotcpcb(inp);
208 		if (inp->inp_vflag & INP_DROPPED ||
209 		    tp->t_state < TCPS_SYN_SENT) {
210 			tcp_discardcb(tp);
211 #ifdef INET6
212 			if (isipv6) {
213 				in_pcbdetach(inp);
214 				in_pcbfree(inp);
215 			} else {
216 #endif
217 				in_pcbdetach(inp);
218 				in_pcbfree(inp);
219 #ifdef INET6
220 			}
221 #endif
222 		} else {
223 			SOCK_LOCK(so);
224 			so->so_state |= SS_PROTOREF;
225 			SOCK_UNLOCK(so);
226 			inp->inp_vflag |= INP_SOCKREF;
227 			INP_UNLOCK(inp);
228 		}
229 	}
230 }
231 
232 /*
233  * pru_detach() detaches the TCP protocol from the socket.
234  * If the protocol state is non-embryonic, then can't
235  * do this directly: have to initiate a pru_disconnect(),
236  * which may finish later; embryonic TCB's can just
237  * be discarded here.
238  */
239 static void
240 tcp_usr_detach(struct socket *so)
241 {
242 	struct inpcb *inp;
243 	struct tcpcb *tp;
244 	TCPDEBUG0;
245 
246 	inp = sotoinpcb(so);
247 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
248 	INP_INFO_WLOCK(&tcbinfo);
249 	INP_LOCK(inp);
250 	KASSERT(inp->inp_socket != NULL,
251 	    ("tcp_usr_detach: inp_socket == NULL"));
252 	TCPDEBUG1();
253 
254 	/*
255 	 * First, if we still have full TCP state, and we're not dropped,
256 	 * initiate a disconnect.
257 	 */
258 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
259 	    !(inp->inp_vflag & INP_DROPPED)) {
260 		tp = intotcpcb(inp);
261 		tcp_disconnect(tp);
262 	}
263 	tcp_detach(so, inp);
264 	tp = NULL;
265 	TCPDEBUG2(PRU_DETACH);
266 	INP_INFO_WUNLOCK(&tcbinfo);
267 }
268 
269 /*
270  * Give the socket an address.
271  */
272 static int
273 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
274 {
275 	int error = 0;
276 	struct inpcb *inp;
277 	struct tcpcb *tp = NULL;
278 	struct sockaddr_in *sinp;
279 
280 	sinp = (struct sockaddr_in *)nam;
281 	if (nam->sa_len != sizeof (*sinp))
282 		return (EINVAL);
283 	/*
284 	 * Must check for multicast addresses and disallow binding
285 	 * to them.
286 	 */
287 	if (sinp->sin_family == AF_INET &&
288 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
289 		return (EAFNOSUPPORT);
290 
291 	TCPDEBUG0;
292 	INP_INFO_WLOCK(&tcbinfo);
293 	inp = sotoinpcb(so);
294 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
295 	INP_LOCK(inp);
296 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
297 		error = EINVAL;
298 		goto out;
299 	}
300 	tp = intotcpcb(inp);
301 	TCPDEBUG1();
302 	error = in_pcbbind(inp, nam, td->td_ucred);
303 out:
304 	TCPDEBUG2(PRU_BIND);
305 	INP_UNLOCK(inp);
306 	INP_INFO_WUNLOCK(&tcbinfo);
307 
308 	return (error);
309 }
310 
311 #ifdef INET6
312 static int
313 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
314 {
315 	int error = 0;
316 	struct inpcb *inp;
317 	struct tcpcb *tp = NULL;
318 	struct sockaddr_in6 *sin6p;
319 
320 	sin6p = (struct sockaddr_in6 *)nam;
321 	if (nam->sa_len != sizeof (*sin6p))
322 		return (EINVAL);
323 	/*
324 	 * Must check for multicast addresses and disallow binding
325 	 * to them.
326 	 */
327 	if (sin6p->sin6_family == AF_INET6 &&
328 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
329 		return (EAFNOSUPPORT);
330 
331 	TCPDEBUG0;
332 	INP_INFO_WLOCK(&tcbinfo);
333 	inp = sotoinpcb(so);
334 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
335 	INP_LOCK(inp);
336 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
337 		error = EINVAL;
338 		goto out;
339 	}
340 	tp = intotcpcb(inp);
341 	TCPDEBUG1();
342 	inp->inp_vflag &= ~INP_IPV4;
343 	inp->inp_vflag |= INP_IPV6;
344 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
345 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
346 			inp->inp_vflag |= INP_IPV4;
347 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
348 			struct sockaddr_in sin;
349 
350 			in6_sin6_2_sin(&sin, sin6p);
351 			inp->inp_vflag |= INP_IPV4;
352 			inp->inp_vflag &= ~INP_IPV6;
353 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
354 			    td->td_ucred);
355 			goto out;
356 		}
357 	}
358 	error = in6_pcbbind(inp, nam, td->td_ucred);
359 out:
360 	TCPDEBUG2(PRU_BIND);
361 	INP_UNLOCK(inp);
362 	INP_INFO_WUNLOCK(&tcbinfo);
363 	return (error);
364 }
365 #endif /* INET6 */
366 
367 /*
368  * Prepare to accept connections.
369  */
370 static int
371 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
372 {
373 	int error = 0;
374 	struct inpcb *inp;
375 	struct tcpcb *tp = NULL;
376 
377 	TCPDEBUG0;
378 	INP_INFO_WLOCK(&tcbinfo);
379 	inp = sotoinpcb(so);
380 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
381 	INP_LOCK(inp);
382 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
383 		error = EINVAL;
384 		goto out;
385 	}
386 	tp = intotcpcb(inp);
387 	TCPDEBUG1();
388 	SOCK_LOCK(so);
389 	error = solisten_proto_check(so);
390 	if (error == 0 && inp->inp_lport == 0)
391 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
392 	if (error == 0) {
393 		tp->t_state = TCPS_LISTEN;
394 		solisten_proto(so, backlog);
395 	}
396 	SOCK_UNLOCK(so);
397 
398 out:
399 	TCPDEBUG2(PRU_LISTEN);
400 	INP_UNLOCK(inp);
401 	INP_INFO_WUNLOCK(&tcbinfo);
402 	return (error);
403 }
404 
405 #ifdef INET6
406 static int
407 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
408 {
409 	int error = 0;
410 	struct inpcb *inp;
411 	struct tcpcb *tp = NULL;
412 
413 	TCPDEBUG0;
414 	INP_INFO_WLOCK(&tcbinfo);
415 	inp = sotoinpcb(so);
416 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
417 	INP_LOCK(inp);
418 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
419 		error = EINVAL;
420 		goto out;
421 	}
422 	tp = intotcpcb(inp);
423 	TCPDEBUG1();
424 	SOCK_LOCK(so);
425 	error = solisten_proto_check(so);
426 	if (error == 0 && inp->inp_lport == 0) {
427 		inp->inp_vflag &= ~INP_IPV4;
428 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
429 			inp->inp_vflag |= INP_IPV4;
430 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
431 	}
432 	if (error == 0) {
433 		tp->t_state = TCPS_LISTEN;
434 		solisten_proto(so, backlog);
435 	}
436 	SOCK_UNLOCK(so);
437 
438 out:
439 	TCPDEBUG2(PRU_LISTEN);
440 	INP_UNLOCK(inp);
441 	INP_INFO_WUNLOCK(&tcbinfo);
442 	return (error);
443 }
444 #endif /* INET6 */
445 
446 /*
447  * Initiate connection to peer.
448  * Create a template for use in transmissions on this connection.
449  * Enter SYN_SENT state, and mark socket as connecting.
450  * Start keep-alive timer, and seed output sequence space.
451  * Send initial segment on connection.
452  */
453 static int
454 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
455 {
456 	int error = 0;
457 	struct inpcb *inp;
458 	struct tcpcb *tp = NULL;
459 	struct sockaddr_in *sinp;
460 
461 	sinp = (struct sockaddr_in *)nam;
462 	if (nam->sa_len != sizeof (*sinp))
463 		return (EINVAL);
464 	/*
465 	 * Must disallow TCP ``connections'' to multicast addresses.
466 	 */
467 	if (sinp->sin_family == AF_INET
468 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
469 		return (EAFNOSUPPORT);
470 	if (jailed(td->td_ucred))
471 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
472 
473 	TCPDEBUG0;
474 	INP_INFO_WLOCK(&tcbinfo);
475 	inp = sotoinpcb(so);
476 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
477 	INP_LOCK(inp);
478 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
479 		error = EINVAL;
480 		goto out;
481 	}
482 	tp = intotcpcb(inp);
483 	TCPDEBUG1();
484 	if ((error = tcp_connect(tp, nam, td)) != 0)
485 		goto out;
486 	error = tcp_output(tp);
487 out:
488 	TCPDEBUG2(PRU_CONNECT);
489 	INP_UNLOCK(inp);
490 	INP_INFO_WUNLOCK(&tcbinfo);
491 	return (error);
492 }
493 
494 #ifdef INET6
495 static int
496 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
497 {
498 	int error = 0;
499 	struct inpcb *inp;
500 	struct tcpcb *tp = NULL;
501 	struct sockaddr_in6 *sin6p;
502 
503 	TCPDEBUG0;
504 
505 	sin6p = (struct sockaddr_in6 *)nam;
506 	if (nam->sa_len != sizeof (*sin6p))
507 		return (EINVAL);
508 	/*
509 	 * Must disallow TCP ``connections'' to multicast addresses.
510 	 */
511 	if (sin6p->sin6_family == AF_INET6
512 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
513 		return (EAFNOSUPPORT);
514 
515 	INP_INFO_WLOCK(&tcbinfo);
516 	inp = sotoinpcb(so);
517 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
518 	INP_LOCK(inp);
519 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
520 		error = EINVAL;
521 		goto out;
522 	}
523 	tp = intotcpcb(inp);
524 	TCPDEBUG1();
525 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
526 		struct sockaddr_in sin;
527 
528 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
529 			error = EINVAL;
530 			goto out;
531 		}
532 
533 		in6_sin6_2_sin(&sin, sin6p);
534 		inp->inp_vflag |= INP_IPV4;
535 		inp->inp_vflag &= ~INP_IPV6;
536 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
537 			goto out;
538 		error = tcp_output(tp);
539 		goto out;
540 	}
541 	inp->inp_vflag &= ~INP_IPV4;
542 	inp->inp_vflag |= INP_IPV6;
543 	inp->inp_inc.inc_isipv6 = 1;
544 	if ((error = tcp6_connect(tp, nam, td)) != 0)
545 		goto out;
546 	error = tcp_output(tp);
547 
548 out:
549 	TCPDEBUG2(PRU_CONNECT);
550 	INP_UNLOCK(inp);
551 	INP_INFO_WUNLOCK(&tcbinfo);
552 	return (error);
553 }
554 #endif /* INET6 */
555 
556 /*
557  * Initiate disconnect from peer.
558  * If connection never passed embryonic stage, just drop;
559  * else if don't need to let data drain, then can just drop anyways,
560  * else have to begin TCP shutdown process: mark socket disconnecting,
561  * drain unread data, state switch to reflect user close, and
562  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
563  * when peer sends FIN and acks ours.
564  *
565  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
566  */
567 static int
568 tcp_usr_disconnect(struct socket *so)
569 {
570 	struct inpcb *inp;
571 	struct tcpcb *tp = NULL;
572 	int error = 0;
573 
574 	TCPDEBUG0;
575 	INP_INFO_WLOCK(&tcbinfo);
576 	inp = sotoinpcb(so);
577 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
578 	INP_LOCK(inp);
579 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
580 		error = EINVAL;
581 		goto out;
582 	}
583 	tp = intotcpcb(inp);
584 	TCPDEBUG1();
585 	tcp_disconnect(tp);
586 out:
587 	TCPDEBUG2(PRU_DISCONNECT);
588 	INP_UNLOCK(inp);
589 	INP_INFO_WUNLOCK(&tcbinfo);
590 	return (error);
591 }
592 
593 /*
594  * Accept a connection.  Essentially all the work is
595  * done at higher levels; just return the address
596  * of the peer, storing through addr.
597  */
598 static int
599 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
600 {
601 	int error = 0;
602 	struct inpcb *inp = NULL;
603 	struct tcpcb *tp = NULL;
604 	struct in_addr addr;
605 	in_port_t port = 0;
606 	TCPDEBUG0;
607 
608 	if (so->so_state & SS_ISDISCONNECTED)
609 		return (ECONNABORTED);
610 
611 	inp = sotoinpcb(so);
612 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
613 	INP_LOCK(inp);
614 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
615 		error = ECONNABORTED;
616 		goto out;
617 	}
618 	tp = intotcpcb(inp);
619 	TCPDEBUG1();
620 
621 	/*
622 	 * We inline in_setpeeraddr and COMMON_END here, so that we can
623 	 * copy the data of interest and defer the malloc until after we
624 	 * release the lock.
625 	 */
626 	port = inp->inp_fport;
627 	addr = inp->inp_faddr;
628 
629 out:
630 	TCPDEBUG2(PRU_ACCEPT);
631 	INP_UNLOCK(inp);
632 	if (error == 0)
633 		*nam = in_sockaddr(port, &addr);
634 	return error;
635 }
636 
637 #ifdef INET6
638 static int
639 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
640 {
641 	struct inpcb *inp = NULL;
642 	int error = 0;
643 	struct tcpcb *tp = NULL;
644 	struct in_addr addr;
645 	struct in6_addr addr6;
646 	in_port_t port = 0;
647 	int v4 = 0;
648 	TCPDEBUG0;
649 
650 	if (so->so_state & SS_ISDISCONNECTED)
651 		return (ECONNABORTED);
652 
653 	inp = sotoinpcb(so);
654 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
655 	INP_LOCK(inp);
656 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
657 		error = EINVAL;
658 		goto out;
659 	}
660 	tp = intotcpcb(inp);
661 	TCPDEBUG1();
662 
663 	/*
664 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
665 	 * copy the data of interest and defer the malloc until after we
666 	 * release the lock.
667 	 */
668 	if (inp->inp_vflag & INP_IPV4) {
669 		v4 = 1;
670 		port = inp->inp_fport;
671 		addr = inp->inp_faddr;
672 	} else {
673 		port = inp->inp_fport;
674 		addr6 = inp->in6p_faddr;
675 	}
676 
677 out:
678 	TCPDEBUG2(PRU_ACCEPT);
679 	INP_UNLOCK(inp);
680 	if (error == 0) {
681 		if (v4)
682 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
683 		else
684 			*nam = in6_sockaddr(port, &addr6);
685 	}
686 	return error;
687 }
688 #endif /* INET6 */
689 
690 /*
691  * This is the wrapper function for in_setsockaddr. We just pass down
692  * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
693  * here because in_setsockaddr will call malloc and can block.
694  */
695 static int
696 tcp_sockaddr(struct socket *so, struct sockaddr **nam)
697 {
698 	return (in_setsockaddr(so, nam, &tcbinfo));
699 }
700 
701 /*
702  * This is the wrapper function for in_setpeeraddr. We just pass down
703  * the pcbinfo for in_setpeeraddr to lock.
704  */
705 static int
706 tcp_peeraddr(struct socket *so, struct sockaddr **nam)
707 {
708 	return (in_setpeeraddr(so, nam, &tcbinfo));
709 }
710 
711 /*
712  * Mark the connection as being incapable of further output.
713  */
714 static int
715 tcp_usr_shutdown(struct socket *so)
716 {
717 	int error = 0;
718 	struct inpcb *inp;
719 	struct tcpcb *tp = NULL;
720 
721 	TCPDEBUG0;
722 	INP_INFO_WLOCK(&tcbinfo);
723 	inp = sotoinpcb(so);
724 	KASSERT(inp != NULL, ("inp == NULL"));
725 	INP_LOCK(inp);
726 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
727 		error = EINVAL;
728 		goto out;
729 	}
730 	tp = intotcpcb(inp);
731 	TCPDEBUG1();
732 	socantsendmore(so);
733 	tcp_usrclosed(tp);
734 	error = tcp_output(tp);
735 
736 out:
737 	TCPDEBUG2(PRU_SHUTDOWN);
738 	INP_UNLOCK(inp);
739 	INP_INFO_WUNLOCK(&tcbinfo);
740 
741 	return (error);
742 }
743 
744 /*
745  * After a receive, possibly send window update to peer.
746  */
747 static int
748 tcp_usr_rcvd(struct socket *so, int flags)
749 {
750 	struct inpcb *inp;
751 	struct tcpcb *tp = NULL;
752 	int error = 0;
753 
754 	TCPDEBUG0;
755 	inp = sotoinpcb(so);
756 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
757 	INP_LOCK(inp);
758 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
759 		error = EINVAL;
760 		goto out;
761 	}
762 	tp = intotcpcb(inp);
763 	TCPDEBUG1();
764 	tcp_output(tp);
765 
766 out:
767 	TCPDEBUG2(PRU_RCVD);
768 	INP_UNLOCK(inp);
769 	return (error);
770 }
771 
772 /*
773  * Do a send by putting data in output queue and updating urgent
774  * marker if URG set.  Possibly send more data.  Unlike the other
775  * pru_*() routines, the mbuf chains are our responsibility.  We
776  * must either enqueue them or free them.  The other pru_* routines
777  * generally are caller-frees.
778  */
779 static int
780 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
781 	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
782 {
783 	int error = 0;
784 	struct inpcb *inp;
785 	struct tcpcb *tp = NULL;
786 	int headlocked = 0;
787 #ifdef INET6
788 	int isipv6;
789 #endif
790 	TCPDEBUG0;
791 
792 	/*
793 	 * We require the pcbinfo lock in two cases:
794 	 *
795 	 * (1) An implied connect is taking place, which can result in
796 	 *     binding IPs and ports and hence modification of the pcb hash
797 	 *     chains.
798 	 *
799 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
800 	 */
801 	if ((nam != NULL) || (flags & PRUS_EOF)) {
802 		INP_INFO_WLOCK(&tcbinfo);
803 		headlocked = 1;
804 	}
805 	inp = sotoinpcb(so);
806 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
807 	INP_LOCK(inp);
808 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
809 		error = EINVAL;
810 		goto out;
811 	}
812 #ifdef INET6
813 	isipv6 = nam && nam->sa_family == AF_INET6;
814 #endif /* INET6 */
815 	tp = intotcpcb(inp);
816 	TCPDEBUG1();
817 	if (control) {
818 		/* TCP doesn't do control messages (rights, creds, etc) */
819 		if (control->m_len) {
820 			m_freem(control);
821 			if (m)
822 				m_freem(m);
823 			error = EINVAL;
824 			goto out;
825 		}
826 		m_freem(control);	/* empty control, just free it */
827 	}
828 	if (!(flags & PRUS_OOB)) {
829 		sbappendstream(&so->so_snd, m);
830 		if (nam && tp->t_state < TCPS_SYN_SENT) {
831 			/*
832 			 * Do implied connect if not yet connected,
833 			 * initialize window to default value, and
834 			 * initialize maxseg/maxopd using peer's cached
835 			 * MSS.
836 			 */
837 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
838 #ifdef INET6
839 			if (isipv6)
840 				error = tcp6_connect(tp, nam, td);
841 			else
842 #endif /* INET6 */
843 			error = tcp_connect(tp, nam, td);
844 			if (error)
845 				goto out;
846 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
847 			tcp_mss(tp, -1);
848 		}
849 		if (flags & PRUS_EOF) {
850 			/*
851 			 * Close the send side of the connection after
852 			 * the data is sent.
853 			 */
854 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
855 			socantsendmore(so);
856 			tcp_usrclosed(tp);
857 		}
858 		if (headlocked) {
859 			INP_INFO_WUNLOCK(&tcbinfo);
860 			headlocked = 0;
861 		}
862 		if (tp != NULL) {
863 			if (flags & PRUS_MORETOCOME)
864 				tp->t_flags |= TF_MORETOCOME;
865 			error = tcp_output(tp);
866 			if (flags & PRUS_MORETOCOME)
867 				tp->t_flags &= ~TF_MORETOCOME;
868 		}
869 	} else {
870 		/*
871 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
872 		 */
873 		SOCKBUF_LOCK(&so->so_snd);
874 		if (sbspace(&so->so_snd) < -512) {
875 			SOCKBUF_UNLOCK(&so->so_snd);
876 			m_freem(m);
877 			error = ENOBUFS;
878 			goto out;
879 		}
880 		/*
881 		 * According to RFC961 (Assigned Protocols),
882 		 * the urgent pointer points to the last octet
883 		 * of urgent data.  We continue, however,
884 		 * to consider it to indicate the first octet
885 		 * of data past the urgent section.
886 		 * Otherwise, snd_up should be one lower.
887 		 */
888 		sbappendstream_locked(&so->so_snd, m);
889 		SOCKBUF_UNLOCK(&so->so_snd);
890 		if (nam && tp->t_state < TCPS_SYN_SENT) {
891 			/*
892 			 * Do implied connect if not yet connected,
893 			 * initialize window to default value, and
894 			 * initialize maxseg/maxopd using peer's cached
895 			 * MSS.
896 			 */
897 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
898 #ifdef INET6
899 			if (isipv6)
900 				error = tcp6_connect(tp, nam, td);
901 			else
902 #endif /* INET6 */
903 			error = tcp_connect(tp, nam, td);
904 			if (error)
905 				goto out;
906 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
907 			tcp_mss(tp, -1);
908 			INP_INFO_WUNLOCK(&tcbinfo);
909 			headlocked = 0;
910 		} else if (nam) {
911 			INP_INFO_WUNLOCK(&tcbinfo);
912 			headlocked = 0;
913 		}
914 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
915 		tp->t_flags |= TF_FORCEDATA;
916 		error = tcp_output(tp);
917 		tp->t_flags &= ~TF_FORCEDATA;
918 	}
919 out:
920 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
921 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
922 	INP_UNLOCK(inp);
923 	if (headlocked)
924 		INP_INFO_WUNLOCK(&tcbinfo);
925 	return (error);
926 }
927 
928 /*
929  * Abort the TCP.
930  *
931  * First, drop the connection.  Then collect state if possible.
932  */
933 static void
934 tcp_usr_abort(struct socket *so)
935 {
936 	struct inpcb *inp;
937 	struct tcpcb *tp;
938 	TCPDEBUG0;
939 
940 	inp = sotoinpcb(so);
941 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
942 
943 	INP_INFO_WLOCK(&tcbinfo);
944 	INP_LOCK(inp);
945 	KASSERT(inp->inp_socket != NULL,
946 	    ("tcp_usr_abort: inp_socket == NULL"));
947 	TCPDEBUG1();
948 
949 	/*
950 	 * First, if we still have full TCP state, and we're not dropped,
951 	 * drop.
952 	 */
953 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
954 	    !(inp->inp_vflag & INP_DROPPED)) {
955 		tp = intotcpcb(inp);
956 		tcp_drop(tp, ECONNABORTED);
957 	}
958 	tcp_detach(so, inp);
959 	tp = NULL;
960 	TCPDEBUG2(PRU_DETACH);
961 	INP_INFO_WUNLOCK(&tcbinfo);
962 }
963 
964 /*
965  * Receive out-of-band data.
966  */
967 static int
968 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
969 {
970 	int error = 0;
971 	struct inpcb *inp;
972 	struct tcpcb *tp = NULL;
973 
974 	TCPDEBUG0;
975 	inp = sotoinpcb(so);
976 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
977 	INP_LOCK(inp);
978 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
979 		error = EINVAL;
980 		goto out;
981 	}
982 	tp = intotcpcb(inp);
983 	TCPDEBUG1();
984 	if ((so->so_oobmark == 0 &&
985 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
986 	    so->so_options & SO_OOBINLINE ||
987 	    tp->t_oobflags & TCPOOB_HADDATA) {
988 		error = EINVAL;
989 		goto out;
990 	}
991 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
992 		error = EWOULDBLOCK;
993 		goto out;
994 	}
995 	m->m_len = 1;
996 	*mtod(m, caddr_t) = tp->t_iobc;
997 	if ((flags & MSG_PEEK) == 0)
998 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
999 
1000 out:
1001 	TCPDEBUG2(PRU_RCVOOB);
1002 	INP_UNLOCK(inp);
1003 	return (error);
1004 }
1005 
1006 struct pr_usrreqs tcp_usrreqs = {
1007 	.pru_abort =		tcp_usr_abort,
1008 	.pru_accept =		tcp_usr_accept,
1009 	.pru_attach =		tcp_usr_attach,
1010 	.pru_bind =		tcp_usr_bind,
1011 	.pru_connect =		tcp_usr_connect,
1012 	.pru_control =		in_control,
1013 	.pru_detach =		tcp_usr_detach,
1014 	.pru_disconnect =	tcp_usr_disconnect,
1015 	.pru_listen =		tcp_usr_listen,
1016 	.pru_peeraddr =		tcp_peeraddr,
1017 	.pru_rcvd =		tcp_usr_rcvd,
1018 	.pru_rcvoob =		tcp_usr_rcvoob,
1019 	.pru_send =		tcp_usr_send,
1020 	.pru_shutdown =		tcp_usr_shutdown,
1021 	.pru_sockaddr =		tcp_sockaddr,
1022 	.pru_sosetlabel =	in_pcbsosetlabel
1023 };
1024 
1025 #ifdef INET6
1026 struct pr_usrreqs tcp6_usrreqs = {
1027 	.pru_abort =		tcp_usr_abort,
1028 	.pru_accept =		tcp6_usr_accept,
1029 	.pru_attach =		tcp_usr_attach,
1030 	.pru_bind =		tcp6_usr_bind,
1031 	.pru_connect =		tcp6_usr_connect,
1032 	.pru_control =		in6_control,
1033 	.pru_detach =		tcp_usr_detach,
1034 	.pru_disconnect =	tcp_usr_disconnect,
1035 	.pru_listen =		tcp6_usr_listen,
1036 	.pru_peeraddr =		in6_mapped_peeraddr,
1037 	.pru_rcvd =		tcp_usr_rcvd,
1038 	.pru_rcvoob =		tcp_usr_rcvoob,
1039 	.pru_send =		tcp_usr_send,
1040 	.pru_shutdown =		tcp_usr_shutdown,
1041 	.pru_sockaddr =		in6_mapped_sockaddr,
1042  	.pru_sosetlabel =	in_pcbsosetlabel
1043 };
1044 #endif /* INET6 */
1045 
1046 /*
1047  * Common subroutine to open a TCP connection to remote host specified
1048  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1049  * port number if needed.  Call in_pcbconnect_setup to do the routing and
1050  * to choose a local host address (interface).  If there is an existing
1051  * incarnation of the same connection in TIME-WAIT state and if the remote
1052  * host was sending CC options and if the connection duration was < MSL, then
1053  * truncate the previous TIME-WAIT state and proceed.
1054  * Initialize connection parameters and enter SYN-SENT state.
1055  */
1056 static int
1057 tcp_connect(tp, nam, td)
1058 	register struct tcpcb *tp;
1059 	struct sockaddr *nam;
1060 	struct thread *td;
1061 {
1062 	struct inpcb *inp = tp->t_inpcb, *oinp;
1063 	struct socket *so = inp->inp_socket;
1064 	struct in_addr laddr;
1065 	u_short lport;
1066 	int error;
1067 
1068 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1069 	INP_LOCK_ASSERT(inp);
1070 
1071 	if (inp->inp_lport == 0) {
1072 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1073 		if (error)
1074 			return error;
1075 	}
1076 
1077 	/*
1078 	 * Cannot simply call in_pcbconnect, because there might be an
1079 	 * earlier incarnation of this same connection still in
1080 	 * TIME_WAIT state, creating an ADDRINUSE error.
1081 	 */
1082 	laddr = inp->inp_laddr;
1083 	lport = inp->inp_lport;
1084 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1085 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1086 	if (error && oinp == NULL)
1087 		return error;
1088 	if (oinp)
1089 		return EADDRINUSE;
1090 	inp->inp_laddr = laddr;
1091 	in_pcbrehash(inp);
1092 
1093 	/* Compute window scaling to request.  */
1094 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1095 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1096 		tp->request_r_scale++;
1097 
1098 	soisconnecting(so);
1099 	tcpstat.tcps_connattempt++;
1100 	tp->t_state = TCPS_SYN_SENT;
1101 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1102 	tp->iss = tcp_new_isn(tp);
1103 	tp->t_bw_rtseq = tp->iss;
1104 	tcp_sendseqinit(tp);
1105 
1106 	return 0;
1107 }
1108 
1109 #ifdef INET6
1110 static int
1111 tcp6_connect(tp, nam, td)
1112 	register struct tcpcb *tp;
1113 	struct sockaddr *nam;
1114 	struct thread *td;
1115 {
1116 	struct inpcb *inp = tp->t_inpcb, *oinp;
1117 	struct socket *so = inp->inp_socket;
1118 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1119 	struct in6_addr *addr6;
1120 	int error;
1121 
1122 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1123 	INP_LOCK_ASSERT(inp);
1124 
1125 	if (inp->inp_lport == 0) {
1126 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1127 		if (error)
1128 			return error;
1129 	}
1130 
1131 	/*
1132 	 * Cannot simply call in_pcbconnect, because there might be an
1133 	 * earlier incarnation of this same connection still in
1134 	 * TIME_WAIT state, creating an ADDRINUSE error.
1135 	 * in6_pcbladdr() also handles scope zone IDs.
1136 	 */
1137 	error = in6_pcbladdr(inp, nam, &addr6);
1138 	if (error)
1139 		return error;
1140 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1141 				  &sin6->sin6_addr, sin6->sin6_port,
1142 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1143 				  ? addr6
1144 				  : &inp->in6p_laddr,
1145 				  inp->inp_lport,  0, NULL);
1146 	if (oinp)
1147 		return EADDRINUSE;
1148 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1149 		inp->in6p_laddr = *addr6;
1150 	inp->in6p_faddr = sin6->sin6_addr;
1151 	inp->inp_fport = sin6->sin6_port;
1152 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1153 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
1154 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
1155 		inp->in6p_flowinfo |=
1156 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1157 	in_pcbrehash(inp);
1158 
1159 	/* Compute window scaling to request.  */
1160 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1161 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1162 		tp->request_r_scale++;
1163 
1164 	soisconnecting(so);
1165 	tcpstat.tcps_connattempt++;
1166 	tp->t_state = TCPS_SYN_SENT;
1167 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1168 	tp->iss = tcp_new_isn(tp);
1169 	tp->t_bw_rtseq = tp->iss;
1170 	tcp_sendseqinit(tp);
1171 
1172 	return 0;
1173 }
1174 #endif /* INET6 */
1175 
1176 /*
1177  * Export TCP internal state information via a struct tcp_info, based on the
1178  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1179  * (TCP state machine, etc).  We export all information using FreeBSD-native
1180  * constants -- for example, the numeric values for tcpi_state will differ
1181  * from Linux.
1182  */
1183 static void
1184 tcp_fill_info(tp, ti)
1185 	struct tcpcb *tp;
1186 	struct tcp_info *ti;
1187 {
1188 
1189 	INP_LOCK_ASSERT(tp->t_inpcb);
1190 	bzero(ti, sizeof(*ti));
1191 
1192 	ti->tcpi_state = tp->t_state;
1193 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1194 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1195 	if (tp->sack_enable)
1196 		ti->tcpi_options |= TCPI_OPT_SACK;
1197 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1198 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1199 		ti->tcpi_snd_wscale = tp->snd_scale;
1200 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1201 	}
1202 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1203 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1204 
1205 	/*
1206 	 * FreeBSD-specific extension fields for tcp_info.
1207 	 */
1208 	ti->tcpi_rcv_space = tp->rcv_wnd;
1209 	ti->tcpi_snd_wnd = tp->snd_wnd;
1210 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1211 }
1212 
1213 /*
1214  * The new sockopt interface makes it possible for us to block in the
1215  * copyin/out step (if we take a page fault).  Taking a page fault at
1216  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1217  * use TSM, there probably isn't any need for this function to run at
1218  * splnet() any more.  This needs more examination.)
1219  *
1220  * XXXRW: The locking here is wrong; we may take a page fault while holding
1221  * the inpcb lock.
1222  */
1223 int
1224 tcp_ctloutput(so, sopt)
1225 	struct socket *so;
1226 	struct sockopt *sopt;
1227 {
1228 	int	error, opt, optval;
1229 	struct	inpcb *inp;
1230 	struct	tcpcb *tp;
1231 	struct	tcp_info ti;
1232 
1233 	error = 0;
1234 	inp = sotoinpcb(so);
1235 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1236 	INP_LOCK(inp);
1237 	if (sopt->sopt_level != IPPROTO_TCP) {
1238 		INP_UNLOCK(inp);
1239 #ifdef INET6
1240 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1241 			error = ip6_ctloutput(so, sopt);
1242 		else
1243 #endif /* INET6 */
1244 		error = ip_ctloutput(so, sopt);
1245 		return (error);
1246 	}
1247 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1248 		error = ECONNRESET;
1249 		goto out;
1250 	}
1251 	tp = intotcpcb(inp);
1252 
1253 	switch (sopt->sopt_dir) {
1254 	case SOPT_SET:
1255 		switch (sopt->sopt_name) {
1256 #ifdef TCP_SIGNATURE
1257 		case TCP_MD5SIG:
1258 			error = sooptcopyin(sopt, &optval, sizeof optval,
1259 					    sizeof optval);
1260 			if (error)
1261 				break;
1262 
1263 			if (optval > 0)
1264 				tp->t_flags |= TF_SIGNATURE;
1265 			else
1266 				tp->t_flags &= ~TF_SIGNATURE;
1267 			break;
1268 #endif /* TCP_SIGNATURE */
1269 		case TCP_NODELAY:
1270 		case TCP_NOOPT:
1271 			error = sooptcopyin(sopt, &optval, sizeof optval,
1272 					    sizeof optval);
1273 			if (error)
1274 				break;
1275 
1276 			switch (sopt->sopt_name) {
1277 			case TCP_NODELAY:
1278 				opt = TF_NODELAY;
1279 				break;
1280 			case TCP_NOOPT:
1281 				opt = TF_NOOPT;
1282 				break;
1283 			default:
1284 				opt = 0; /* dead code to fool gcc */
1285 				break;
1286 			}
1287 
1288 			if (optval)
1289 				tp->t_flags |= opt;
1290 			else
1291 				tp->t_flags &= ~opt;
1292 			break;
1293 
1294 		case TCP_NOPUSH:
1295 			error = sooptcopyin(sopt, &optval, sizeof optval,
1296 					    sizeof optval);
1297 			if (error)
1298 				break;
1299 
1300 			if (optval)
1301 				tp->t_flags |= TF_NOPUSH;
1302 			else {
1303 				tp->t_flags &= ~TF_NOPUSH;
1304 				error = tcp_output(tp);
1305 			}
1306 			break;
1307 
1308 		case TCP_MAXSEG:
1309 			error = sooptcopyin(sopt, &optval, sizeof optval,
1310 					    sizeof optval);
1311 			if (error)
1312 				break;
1313 
1314 			if (optval > 0 && optval <= tp->t_maxseg &&
1315 			    optval + 40 >= tcp_minmss)
1316 				tp->t_maxseg = optval;
1317 			else
1318 				error = EINVAL;
1319 			break;
1320 
1321 		case TCP_INFO:
1322 			error = EINVAL;
1323 			break;
1324 
1325 		default:
1326 			error = ENOPROTOOPT;
1327 			break;
1328 		}
1329 		break;
1330 
1331 	case SOPT_GET:
1332 		switch (sopt->sopt_name) {
1333 #ifdef TCP_SIGNATURE
1334 		case TCP_MD5SIG:
1335 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1336 			error = sooptcopyout(sopt, &optval, sizeof optval);
1337 			break;
1338 #endif
1339 		case TCP_NODELAY:
1340 			optval = tp->t_flags & TF_NODELAY;
1341 			error = sooptcopyout(sopt, &optval, sizeof optval);
1342 			break;
1343 		case TCP_MAXSEG:
1344 			optval = tp->t_maxseg;
1345 			error = sooptcopyout(sopt, &optval, sizeof optval);
1346 			break;
1347 		case TCP_NOOPT:
1348 			optval = tp->t_flags & TF_NOOPT;
1349 			error = sooptcopyout(sopt, &optval, sizeof optval);
1350 			break;
1351 		case TCP_NOPUSH:
1352 			optval = tp->t_flags & TF_NOPUSH;
1353 			error = sooptcopyout(sopt, &optval, sizeof optval);
1354 			break;
1355 		case TCP_INFO:
1356 			tcp_fill_info(tp, &ti);
1357 			error = sooptcopyout(sopt, &ti, sizeof ti);
1358 			break;
1359 		default:
1360 			error = ENOPROTOOPT;
1361 			break;
1362 		}
1363 		break;
1364 	}
1365 out:
1366 	INP_UNLOCK(inp);
1367 	return (error);
1368 }
1369 
1370 /*
1371  * tcp_sendspace and tcp_recvspace are the default send and receive window
1372  * sizes, respectively.  These are obsolescent (this information should
1373  * be set by the route).
1374  */
1375 u_long	tcp_sendspace = 1024*32;
1376 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1377     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1378 u_long	tcp_recvspace = 1024*64;
1379 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1380     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1381 
1382 /*
1383  * Attach TCP protocol to socket, allocating
1384  * internet protocol control block, tcp control block,
1385  * bufer space, and entering LISTEN state if to accept connections.
1386  */
1387 static int
1388 tcp_attach(so)
1389 	struct socket *so;
1390 {
1391 	register struct tcpcb *tp;
1392 	struct inpcb *inp;
1393 	int error;
1394 #ifdef INET6
1395 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1396 #endif
1397 
1398 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1399 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1400 		if (error)
1401 			return (error);
1402 	}
1403 	INP_INFO_WLOCK(&tcbinfo);
1404 	error = in_pcballoc(so, &tcbinfo, "tcpinp");
1405 	if (error) {
1406 		INP_INFO_WUNLOCK(&tcbinfo);
1407 		return (error);
1408 	}
1409 	inp = sotoinpcb(so);
1410 	INP_LOCK(inp);
1411 #ifdef INET6
1412 	if (isipv6) {
1413 		inp->inp_vflag |= INP_IPV6;
1414 		inp->in6p_hops = -1;	/* use kernel default */
1415 	}
1416 	else
1417 #endif
1418 	inp->inp_vflag |= INP_IPV4;
1419 	tp = tcp_newtcpcb(inp);
1420 	if (tp == NULL) {
1421 #ifdef INET6
1422 		if (isipv6) {
1423 			in6_pcbdetach(inp);
1424 			in6_pcbfree(inp);
1425 		} else {
1426 #endif
1427 			in_pcbdetach(inp);
1428 			in_pcbfree(inp);
1429 #ifdef INET6
1430 		}
1431 #endif
1432 		INP_INFO_WUNLOCK(&tcbinfo);
1433 		return (ENOBUFS);
1434 	}
1435 	tp->t_state = TCPS_CLOSED;
1436 	INP_UNLOCK(inp);
1437 	INP_INFO_WUNLOCK(&tcbinfo);
1438 	return (0);
1439 }
1440 
1441 /*
1442  * Initiate (or continue) disconnect.
1443  * If embryonic state, just send reset (once).
1444  * If in ``let data drain'' option and linger null, just drop.
1445  * Otherwise (hard), mark socket disconnecting and drop
1446  * current input data; switch states based on user close, and
1447  * send segment to peer (with FIN).
1448  */
1449 static void
1450 tcp_disconnect(tp)
1451 	register struct tcpcb *tp;
1452 {
1453 	struct inpcb *inp = tp->t_inpcb;
1454 	struct socket *so = inp->inp_socket;
1455 
1456 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1457 	INP_LOCK_ASSERT(inp);
1458 
1459 	/*
1460 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1461 	 * socket is still open.
1462 	 */
1463 	if (tp->t_state < TCPS_ESTABLISHED) {
1464 		tp = tcp_close(tp);
1465 		KASSERT(tp != NULL,
1466 		    ("tcp_disconnect: tcp_close() returned NULL"));
1467 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1468 		tp = tcp_drop(tp, 0);
1469 		KASSERT(tp != NULL,
1470 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1471 	} else {
1472 		soisdisconnecting(so);
1473 		sbflush(&so->so_rcv);
1474 		tcp_usrclosed(tp);
1475 		if (!(inp->inp_vflag & INP_DROPPED))
1476 			tcp_output(tp);
1477 	}
1478 }
1479 
1480 /*
1481  * User issued close, and wish to trail through shutdown states:
1482  * if never received SYN, just forget it.  If got a SYN from peer,
1483  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1484  * If already got a FIN from peer, then almost done; go to LAST_ACK
1485  * state.  In all other cases, have already sent FIN to peer (e.g.
1486  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1487  * for peer to send FIN or not respond to keep-alives, etc.
1488  * We can let the user exit from the close as soon as the FIN is acked.
1489  */
1490 static void
1491 tcp_usrclosed(tp)
1492 	register struct tcpcb *tp;
1493 {
1494 
1495 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1496 	INP_LOCK_ASSERT(tp->t_inpcb);
1497 
1498 	switch (tp->t_state) {
1499 
1500 	case TCPS_CLOSED:
1501 	case TCPS_LISTEN:
1502 		tp->t_state = TCPS_CLOSED;
1503 		tp = tcp_close(tp);
1504 		/*
1505 		 * tcp_close() should never return NULL here as the socket is
1506 		 * still open.
1507 		 */
1508 		KASSERT(tp != NULL,
1509 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1510 		break;
1511 
1512 	case TCPS_SYN_SENT:
1513 	case TCPS_SYN_RECEIVED:
1514 		tp->t_flags |= TF_NEEDFIN;
1515 		break;
1516 
1517 	case TCPS_ESTABLISHED:
1518 		tp->t_state = TCPS_FIN_WAIT_1;
1519 		break;
1520 
1521 	case TCPS_CLOSE_WAIT:
1522 		tp->t_state = TCPS_LAST_ACK;
1523 		break;
1524 	}
1525 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1526 		soisdisconnected(tp->t_inpcb->inp_socket);
1527 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1528 		if (tp->t_state == TCPS_FIN_WAIT_2)
1529 			callout_reset(tp->tt_2msl, tcp_maxidle,
1530 				      tcp_timer_2msl, tp);
1531 	}
1532 }
1533