xref: /freebsd/sys/netinet/tcp_usrreq.c (revision bf8e3513bbd411c51e6aa59995b0ef1e45bf55bb)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1993
5  *	The Regents of the University of California.
6  * Copyright (c) 2006-2007 Robert N. M. Watson
7  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8  * All rights reserved.
9  *
10  * Portions of this software were developed by Robert N. M. Watson under
11  * contract to Juniper Networks, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
38  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include "opt_ddb.h"
44 #include "opt_inet.h"
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
47 #include "opt_tcpdebug.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/limits.h>
52 #include <sys/malloc.h>
53 #include <sys/refcount.h>
54 #include <sys/kernel.h>
55 #include <sys/sysctl.h>
56 #include <sys/mbuf.h>
57 #ifdef INET6
58 #include <sys/domain.h>
59 #endif /* INET6 */
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/protosw.h>
63 #include <sys/proc.h>
64 #include <sys/jail.h>
65 #include <sys/syslog.h>
66 
67 #ifdef DDB
68 #include <ddb/ddb.h>
69 #endif
70 
71 #include <net/if.h>
72 #include <net/if_var.h>
73 #include <net/route.h>
74 #include <net/vnet.h>
75 
76 #include <netinet/in.h>
77 #include <netinet/in_kdtrace.h>
78 #include <netinet/in_pcb.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip_var.h>
82 #ifdef INET6
83 #include <netinet/ip6.h>
84 #include <netinet6/in6_pcb.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/scope6_var.h>
87 #endif
88 #include <netinet/tcp.h>
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcpip.h>
94 #include <netinet/cc/cc.h>
95 #include <netinet/tcp_fastopen.h>
96 #ifdef TCPPCAP
97 #include <netinet/tcp_pcap.h>
98 #endif
99 #ifdef TCPDEBUG
100 #include <netinet/tcp_debug.h>
101 #endif
102 #ifdef TCP_OFFLOAD
103 #include <netinet/tcp_offload.h>
104 #endif
105 #include <netipsec/ipsec_support.h>
106 
107 /*
108  * TCP protocol interface to socket abstraction.
109  */
110 static int	tcp_attach(struct socket *);
111 #ifdef INET
112 static int	tcp_connect(struct tcpcb *, struct sockaddr *,
113 		    struct thread *td);
114 #endif /* INET */
115 #ifdef INET6
116 static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
117 		    struct thread *td);
118 #endif /* INET6 */
119 static void	tcp_disconnect(struct tcpcb *);
120 static void	tcp_usrclosed(struct tcpcb *);
121 static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
122 
123 #ifdef TCPDEBUG
124 #define	TCPDEBUG0	int ostate = 0
125 #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
126 #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
127 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
128 #else
129 #define	TCPDEBUG0
130 #define	TCPDEBUG1()
131 #define	TCPDEBUG2(req)
132 #endif
133 
134 /*
135  * TCP attaches to socket via pru_attach(), reserving space,
136  * and an internet control block.
137  */
138 static int
139 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
140 {
141 	struct inpcb *inp;
142 	struct tcpcb *tp = NULL;
143 	int error;
144 	TCPDEBUG0;
145 
146 	inp = sotoinpcb(so);
147 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
148 	TCPDEBUG1();
149 
150 	error = tcp_attach(so);
151 	if (error)
152 		goto out;
153 
154 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
155 		so->so_linger = TCP_LINGERTIME;
156 
157 	inp = sotoinpcb(so);
158 	tp = intotcpcb(inp);
159 out:
160 	TCPDEBUG2(PRU_ATTACH);
161 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
162 	return error;
163 }
164 
165 /*
166  * tcp_detach is called when the socket layer loses its final reference
167  * to the socket, be it a file descriptor reference, a reference from TCP,
168  * etc.  At this point, there is only one case in which we will keep around
169  * inpcb state: time wait.
170  *
171  * This function can probably be re-absorbed back into tcp_usr_detach() now
172  * that there is a single detach path.
173  */
174 static void
175 tcp_detach(struct socket *so, struct inpcb *inp)
176 {
177 	struct tcpcb *tp;
178 
179 	INP_INFO_LOCK_ASSERT(&V_tcbinfo);
180 	INP_WLOCK_ASSERT(inp);
181 
182 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
183 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
184 
185 	tp = intotcpcb(inp);
186 
187 	if (inp->inp_flags & INP_TIMEWAIT) {
188 		/*
189 		 * There are two cases to handle: one in which the time wait
190 		 * state is being discarded (INP_DROPPED), and one in which
191 		 * this connection will remain in timewait.  In the former,
192 		 * it is time to discard all state (except tcptw, which has
193 		 * already been discarded by the timewait close code, which
194 		 * should be further up the call stack somewhere).  In the
195 		 * latter case, we detach from the socket, but leave the pcb
196 		 * present until timewait ends.
197 		 *
198 		 * XXXRW: Would it be cleaner to free the tcptw here?
199 		 *
200 		 * Astute question indeed, from twtcp perspective there are
201 		 * four cases to consider:
202 		 *
203 		 * #1 tcp_detach is called at tcptw creation time by
204 		 *  tcp_twstart, then do not discard the newly created tcptw
205 		 *  and leave inpcb present until timewait ends
206 		 * #2 tcp_detach is called at tcptw creation time by
207 		 *  tcp_twstart, but connection is local and tw will be
208 		 *  discarded immediately
209 		 * #3 tcp_detach is called at timewait end (or reuse) by
210 		 *  tcp_twclose, then the tcptw has already been discarded
211 		 *  (or reused) and inpcb is freed here
212 		 * #4 tcp_detach is called() after timewait ends (or reuse)
213 		 *  (e.g. by soclose), then tcptw has already been discarded
214 		 *  (or reused) and inpcb is freed here
215 		 *
216 		 *  In all three cases the tcptw should not be freed here.
217 		 */
218 		if (inp->inp_flags & INP_DROPPED) {
219 			in_pcbdetach(inp);
220 			if (__predict_true(tp == NULL)) {
221 				in_pcbfree(inp);
222 			} else {
223 				/*
224 				 * This case should not happen as in TIMEWAIT
225 				 * state the inp should not be destroyed before
226 				 * its tcptw.  If INVARIANTS is defined, panic.
227 				 */
228 #ifdef INVARIANTS
229 				panic("%s: Panic before an inp double-free: "
230 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
231 				    , __func__);
232 #else
233 				log(LOG_ERR, "%s: Avoid an inp double-free: "
234 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
235 				    , __func__);
236 #endif
237 				INP_WUNLOCK(inp);
238 			}
239 		} else {
240 			in_pcbdetach(inp);
241 			INP_WUNLOCK(inp);
242 		}
243 	} else {
244 		/*
245 		 * If the connection is not in timewait, we consider two
246 		 * two conditions: one in which no further processing is
247 		 * necessary (dropped || embryonic), and one in which TCP is
248 		 * not yet done, but no longer requires the socket, so the
249 		 * pcb will persist for the time being.
250 		 *
251 		 * XXXRW: Does the second case still occur?
252 		 */
253 		if (inp->inp_flags & INP_DROPPED ||
254 		    tp->t_state < TCPS_SYN_SENT) {
255 			tcp_discardcb(tp);
256 			in_pcbdetach(inp);
257 			in_pcbfree(inp);
258 		} else {
259 			in_pcbdetach(inp);
260 			INP_WUNLOCK(inp);
261 		}
262 	}
263 }
264 
265 /*
266  * pru_detach() detaches the TCP protocol from the socket.
267  * If the protocol state is non-embryonic, then can't
268  * do this directly: have to initiate a pru_disconnect(),
269  * which may finish later; embryonic TCB's can just
270  * be discarded here.
271  */
272 static void
273 tcp_usr_detach(struct socket *so)
274 {
275 	struct inpcb *inp;
276 	int rlock = 0;
277 
278 	inp = sotoinpcb(so);
279 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
280 	if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
281 		INP_INFO_RLOCK(&V_tcbinfo);
282 		rlock = 1;
283 	}
284 	INP_WLOCK(inp);
285 	KASSERT(inp->inp_socket != NULL,
286 	    ("tcp_usr_detach: inp_socket == NULL"));
287 	tcp_detach(so, inp);
288 	if (rlock)
289 		INP_INFO_RUNLOCK(&V_tcbinfo);
290 }
291 
292 #ifdef INET
293 /*
294  * Give the socket an address.
295  */
296 static int
297 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
298 {
299 	int error = 0;
300 	struct inpcb *inp;
301 	struct tcpcb *tp = NULL;
302 	struct sockaddr_in *sinp;
303 
304 	sinp = (struct sockaddr_in *)nam;
305 	if (nam->sa_len != sizeof (*sinp))
306 		return (EINVAL);
307 	/*
308 	 * Must check for multicast addresses and disallow binding
309 	 * to them.
310 	 */
311 	if (sinp->sin_family == AF_INET &&
312 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
313 		return (EAFNOSUPPORT);
314 
315 	TCPDEBUG0;
316 	inp = sotoinpcb(so);
317 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
318 	INP_WLOCK(inp);
319 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
320 		error = EINVAL;
321 		goto out;
322 	}
323 	tp = intotcpcb(inp);
324 	TCPDEBUG1();
325 	INP_HASH_WLOCK(&V_tcbinfo);
326 	error = in_pcbbind(inp, nam, td->td_ucred);
327 	INP_HASH_WUNLOCK(&V_tcbinfo);
328 out:
329 	TCPDEBUG2(PRU_BIND);
330 	TCP_PROBE2(debug__user, tp, PRU_BIND);
331 	INP_WUNLOCK(inp);
332 
333 	return (error);
334 }
335 #endif /* INET */
336 
337 #ifdef INET6
338 static int
339 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
340 {
341 	int error = 0;
342 	struct inpcb *inp;
343 	struct tcpcb *tp = NULL;
344 	struct sockaddr_in6 *sin6p;
345 
346 	sin6p = (struct sockaddr_in6 *)nam;
347 	if (nam->sa_len != sizeof (*sin6p))
348 		return (EINVAL);
349 	/*
350 	 * Must check for multicast addresses and disallow binding
351 	 * to them.
352 	 */
353 	if (sin6p->sin6_family == AF_INET6 &&
354 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
355 		return (EAFNOSUPPORT);
356 
357 	TCPDEBUG0;
358 	inp = sotoinpcb(so);
359 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
360 	INP_WLOCK(inp);
361 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
362 		error = EINVAL;
363 		goto out;
364 	}
365 	tp = intotcpcb(inp);
366 	TCPDEBUG1();
367 	INP_HASH_WLOCK(&V_tcbinfo);
368 	inp->inp_vflag &= ~INP_IPV4;
369 	inp->inp_vflag |= INP_IPV6;
370 #ifdef INET
371 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
372 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
373 			inp->inp_vflag |= INP_IPV4;
374 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
375 			struct sockaddr_in sin;
376 
377 			in6_sin6_2_sin(&sin, sin6p);
378 			inp->inp_vflag |= INP_IPV4;
379 			inp->inp_vflag &= ~INP_IPV6;
380 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
381 			    td->td_ucred);
382 			INP_HASH_WUNLOCK(&V_tcbinfo);
383 			goto out;
384 		}
385 	}
386 #endif
387 	error = in6_pcbbind(inp, nam, td->td_ucred);
388 	INP_HASH_WUNLOCK(&V_tcbinfo);
389 out:
390 	TCPDEBUG2(PRU_BIND);
391 	TCP_PROBE2(debug__user, tp, PRU_BIND);
392 	INP_WUNLOCK(inp);
393 	return (error);
394 }
395 #endif /* INET6 */
396 
397 #ifdef INET
398 /*
399  * Prepare to accept connections.
400  */
401 static int
402 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
403 {
404 	int error = 0;
405 	struct inpcb *inp;
406 	struct tcpcb *tp = NULL;
407 
408 	TCPDEBUG0;
409 	inp = sotoinpcb(so);
410 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
411 	INP_WLOCK(inp);
412 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
413 		error = EINVAL;
414 		goto out;
415 	}
416 	tp = intotcpcb(inp);
417 	TCPDEBUG1();
418 	SOCK_LOCK(so);
419 	error = solisten_proto_check(so);
420 	INP_HASH_WLOCK(&V_tcbinfo);
421 	if (error == 0 && inp->inp_lport == 0)
422 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
423 	INP_HASH_WUNLOCK(&V_tcbinfo);
424 	if (error == 0) {
425 		tcp_state_change(tp, TCPS_LISTEN);
426 		solisten_proto(so, backlog);
427 #ifdef TCP_OFFLOAD
428 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
429 			tcp_offload_listen_start(tp);
430 #endif
431 	}
432 	SOCK_UNLOCK(so);
433 
434 	if (IS_FASTOPEN(tp->t_flags))
435 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
436 
437 out:
438 	TCPDEBUG2(PRU_LISTEN);
439 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
440 	INP_WUNLOCK(inp);
441 	return (error);
442 }
443 #endif /* INET */
444 
445 #ifdef INET6
446 static int
447 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
448 {
449 	int error = 0;
450 	struct inpcb *inp;
451 	struct tcpcb *tp = NULL;
452 
453 	TCPDEBUG0;
454 	inp = sotoinpcb(so);
455 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
456 	INP_WLOCK(inp);
457 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
458 		error = EINVAL;
459 		goto out;
460 	}
461 	tp = intotcpcb(inp);
462 	TCPDEBUG1();
463 	SOCK_LOCK(so);
464 	error = solisten_proto_check(so);
465 	INP_HASH_WLOCK(&V_tcbinfo);
466 	if (error == 0 && inp->inp_lport == 0) {
467 		inp->inp_vflag &= ~INP_IPV4;
468 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
469 			inp->inp_vflag |= INP_IPV4;
470 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
471 	}
472 	INP_HASH_WUNLOCK(&V_tcbinfo);
473 	if (error == 0) {
474 		tcp_state_change(tp, TCPS_LISTEN);
475 		solisten_proto(so, backlog);
476 #ifdef TCP_OFFLOAD
477 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
478 			tcp_offload_listen_start(tp);
479 #endif
480 	}
481 	SOCK_UNLOCK(so);
482 
483 	if (IS_FASTOPEN(tp->t_flags))
484 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
485 
486 out:
487 	TCPDEBUG2(PRU_LISTEN);
488 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
489 	INP_WUNLOCK(inp);
490 	return (error);
491 }
492 #endif /* INET6 */
493 
494 #ifdef INET
495 /*
496  * Initiate connection to peer.
497  * Create a template for use in transmissions on this connection.
498  * Enter SYN_SENT state, and mark socket as connecting.
499  * Start keep-alive timer, and seed output sequence space.
500  * Send initial segment on connection.
501  */
502 static int
503 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
504 {
505 	int error = 0;
506 	struct inpcb *inp;
507 	struct tcpcb *tp = NULL;
508 	struct sockaddr_in *sinp;
509 
510 	sinp = (struct sockaddr_in *)nam;
511 	if (nam->sa_len != sizeof (*sinp))
512 		return (EINVAL);
513 	/*
514 	 * Must disallow TCP ``connections'' to multicast addresses.
515 	 */
516 	if (sinp->sin_family == AF_INET
517 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
518 		return (EAFNOSUPPORT);
519 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
520 		return (error);
521 
522 	TCPDEBUG0;
523 	inp = sotoinpcb(so);
524 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
525 	INP_WLOCK(inp);
526 	if (inp->inp_flags & INP_TIMEWAIT) {
527 		error = EADDRINUSE;
528 		goto out;
529 	}
530 	if (inp->inp_flags & INP_DROPPED) {
531 		error = ECONNREFUSED;
532 		goto out;
533 	}
534 	tp = intotcpcb(inp);
535 	TCPDEBUG1();
536 	if ((error = tcp_connect(tp, nam, td)) != 0)
537 		goto out;
538 #ifdef TCP_OFFLOAD
539 	if (registered_toedevs > 0 &&
540 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
541 	    (error = tcp_offload_connect(so, nam)) == 0)
542 		goto out;
543 #endif
544 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
545 	error = tp->t_fb->tfb_tcp_output(tp);
546 out:
547 	TCPDEBUG2(PRU_CONNECT);
548 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
549 	INP_WUNLOCK(inp);
550 	return (error);
551 }
552 #endif /* INET */
553 
554 #ifdef INET6
555 static int
556 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
557 {
558 	int error = 0;
559 	struct inpcb *inp;
560 	struct tcpcb *tp = NULL;
561 	struct sockaddr_in6 *sin6p;
562 
563 	TCPDEBUG0;
564 
565 	sin6p = (struct sockaddr_in6 *)nam;
566 	if (nam->sa_len != sizeof (*sin6p))
567 		return (EINVAL);
568 	/*
569 	 * Must disallow TCP ``connections'' to multicast addresses.
570 	 */
571 	if (sin6p->sin6_family == AF_INET6
572 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
573 		return (EAFNOSUPPORT);
574 
575 	inp = sotoinpcb(so);
576 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
577 	INP_WLOCK(inp);
578 	if (inp->inp_flags & INP_TIMEWAIT) {
579 		error = EADDRINUSE;
580 		goto out;
581 	}
582 	if (inp->inp_flags & INP_DROPPED) {
583 		error = ECONNREFUSED;
584 		goto out;
585 	}
586 	tp = intotcpcb(inp);
587 	TCPDEBUG1();
588 #ifdef INET
589 	/*
590 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
591 	 * therefore probably require the hash lock, which isn't held here.
592 	 * Is this a significant problem?
593 	 */
594 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
595 		struct sockaddr_in sin;
596 
597 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
598 			error = EINVAL;
599 			goto out;
600 		}
601 		if ((inp->inp_vflag & INP_IPV4) == 0) {
602 			error = EAFNOSUPPORT;
603 			goto out;
604 		}
605 
606 		in6_sin6_2_sin(&sin, sin6p);
607 		inp->inp_vflag |= INP_IPV4;
608 		inp->inp_vflag &= ~INP_IPV6;
609 		if ((error = prison_remote_ip4(td->td_ucred,
610 		    &sin.sin_addr)) != 0)
611 			goto out;
612 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
613 			goto out;
614 #ifdef TCP_OFFLOAD
615 		if (registered_toedevs > 0 &&
616 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
617 		    (error = tcp_offload_connect(so, nam)) == 0)
618 			goto out;
619 #endif
620 		error = tp->t_fb->tfb_tcp_output(tp);
621 		goto out;
622 	} else {
623 		if ((inp->inp_vflag & INP_IPV6) == 0) {
624 			error = EAFNOSUPPORT;
625 			goto out;
626 		}
627 	}
628 #endif
629 	inp->inp_vflag &= ~INP_IPV4;
630 	inp->inp_vflag |= INP_IPV6;
631 	inp->inp_inc.inc_flags |= INC_ISIPV6;
632 	if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
633 		goto out;
634 	if ((error = tcp6_connect(tp, nam, td)) != 0)
635 		goto out;
636 #ifdef TCP_OFFLOAD
637 	if (registered_toedevs > 0 &&
638 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
639 	    (error = tcp_offload_connect(so, nam)) == 0)
640 		goto out;
641 #endif
642 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
643 	error = tp->t_fb->tfb_tcp_output(tp);
644 
645 out:
646 	TCPDEBUG2(PRU_CONNECT);
647 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
648 	INP_WUNLOCK(inp);
649 	return (error);
650 }
651 #endif /* INET6 */
652 
653 /*
654  * Initiate disconnect from peer.
655  * If connection never passed embryonic stage, just drop;
656  * else if don't need to let data drain, then can just drop anyways,
657  * else have to begin TCP shutdown process: mark socket disconnecting,
658  * drain unread data, state switch to reflect user close, and
659  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
660  * when peer sends FIN and acks ours.
661  *
662  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
663  */
664 static int
665 tcp_usr_disconnect(struct socket *so)
666 {
667 	struct inpcb *inp;
668 	struct tcpcb *tp = NULL;
669 	int error = 0;
670 
671 	TCPDEBUG0;
672 	INP_INFO_RLOCK(&V_tcbinfo);
673 	inp = sotoinpcb(so);
674 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
675 	INP_WLOCK(inp);
676 	if (inp->inp_flags & INP_TIMEWAIT)
677 		goto out;
678 	if (inp->inp_flags & INP_DROPPED) {
679 		error = ECONNRESET;
680 		goto out;
681 	}
682 	tp = intotcpcb(inp);
683 	TCPDEBUG1();
684 	tcp_disconnect(tp);
685 out:
686 	TCPDEBUG2(PRU_DISCONNECT);
687 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
688 	INP_WUNLOCK(inp);
689 	INP_INFO_RUNLOCK(&V_tcbinfo);
690 	return (error);
691 }
692 
693 #ifdef INET
694 /*
695  * Accept a connection.  Essentially all the work is done at higher levels;
696  * just return the address of the peer, storing through addr.
697  */
698 static int
699 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
700 {
701 	int error = 0;
702 	struct inpcb *inp = NULL;
703 	struct tcpcb *tp = NULL;
704 	struct in_addr addr;
705 	in_port_t port = 0;
706 	TCPDEBUG0;
707 
708 	if (so->so_state & SS_ISDISCONNECTED)
709 		return (ECONNABORTED);
710 
711 	inp = sotoinpcb(so);
712 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
713 	INP_WLOCK(inp);
714 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
715 		error = ECONNABORTED;
716 		goto out;
717 	}
718 	tp = intotcpcb(inp);
719 	TCPDEBUG1();
720 
721 	/*
722 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
723 	 * copy the data of interest and defer the malloc until after we
724 	 * release the lock.
725 	 */
726 	port = inp->inp_fport;
727 	addr = inp->inp_faddr;
728 
729 out:
730 	TCPDEBUG2(PRU_ACCEPT);
731 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
732 	INP_WUNLOCK(inp);
733 	if (error == 0)
734 		*nam = in_sockaddr(port, &addr);
735 	return error;
736 }
737 #endif /* INET */
738 
739 #ifdef INET6
740 static int
741 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
742 {
743 	struct inpcb *inp = NULL;
744 	int error = 0;
745 	struct tcpcb *tp = NULL;
746 	struct in_addr addr;
747 	struct in6_addr addr6;
748 	in_port_t port = 0;
749 	int v4 = 0;
750 	TCPDEBUG0;
751 
752 	if (so->so_state & SS_ISDISCONNECTED)
753 		return (ECONNABORTED);
754 
755 	inp = sotoinpcb(so);
756 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
757 	INP_INFO_RLOCK(&V_tcbinfo);
758 	INP_WLOCK(inp);
759 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
760 		error = ECONNABORTED;
761 		goto out;
762 	}
763 	tp = intotcpcb(inp);
764 	TCPDEBUG1();
765 
766 	/*
767 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
768 	 * copy the data of interest and defer the malloc until after we
769 	 * release the lock.
770 	 */
771 	if (inp->inp_vflag & INP_IPV4) {
772 		v4 = 1;
773 		port = inp->inp_fport;
774 		addr = inp->inp_faddr;
775 	} else {
776 		port = inp->inp_fport;
777 		addr6 = inp->in6p_faddr;
778 	}
779 
780 out:
781 	TCPDEBUG2(PRU_ACCEPT);
782 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
783 	INP_WUNLOCK(inp);
784 	INP_INFO_RUNLOCK(&V_tcbinfo);
785 	if (error == 0) {
786 		if (v4)
787 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
788 		else
789 			*nam = in6_sockaddr(port, &addr6);
790 	}
791 	return error;
792 }
793 #endif /* INET6 */
794 
795 /*
796  * Mark the connection as being incapable of further output.
797  */
798 static int
799 tcp_usr_shutdown(struct socket *so)
800 {
801 	int error = 0;
802 	struct inpcb *inp;
803 	struct tcpcb *tp = NULL;
804 
805 	TCPDEBUG0;
806 	INP_INFO_RLOCK(&V_tcbinfo);
807 	inp = sotoinpcb(so);
808 	KASSERT(inp != NULL, ("inp == NULL"));
809 	INP_WLOCK(inp);
810 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
811 		error = ECONNRESET;
812 		goto out;
813 	}
814 	tp = intotcpcb(inp);
815 	TCPDEBUG1();
816 	socantsendmore(so);
817 	tcp_usrclosed(tp);
818 	if (!(inp->inp_flags & INP_DROPPED))
819 		error = tp->t_fb->tfb_tcp_output(tp);
820 
821 out:
822 	TCPDEBUG2(PRU_SHUTDOWN);
823 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
824 	INP_WUNLOCK(inp);
825 	INP_INFO_RUNLOCK(&V_tcbinfo);
826 
827 	return (error);
828 }
829 
830 /*
831  * After a receive, possibly send window update to peer.
832  */
833 static int
834 tcp_usr_rcvd(struct socket *so, int flags)
835 {
836 	struct inpcb *inp;
837 	struct tcpcb *tp = NULL;
838 	int error = 0;
839 
840 	TCPDEBUG0;
841 	inp = sotoinpcb(so);
842 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
843 	INP_WLOCK(inp);
844 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
845 		error = ECONNRESET;
846 		goto out;
847 	}
848 	tp = intotcpcb(inp);
849 	TCPDEBUG1();
850 	/*
851 	 * For passively-created TFO connections, don't attempt a window
852 	 * update while still in SYN_RECEIVED as this may trigger an early
853 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
854 	 * application response data, or failing that, when the DELACK timer
855 	 * expires.
856 	 */
857 	if (IS_FASTOPEN(tp->t_flags) &&
858 	    (tp->t_state == TCPS_SYN_RECEIVED))
859 		goto out;
860 #ifdef TCP_OFFLOAD
861 	if (tp->t_flags & TF_TOE)
862 		tcp_offload_rcvd(tp);
863 	else
864 #endif
865 	tp->t_fb->tfb_tcp_output(tp);
866 
867 out:
868 	TCPDEBUG2(PRU_RCVD);
869 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
870 	INP_WUNLOCK(inp);
871 	return (error);
872 }
873 
874 /*
875  * Do a send by putting data in output queue and updating urgent
876  * marker if URG set.  Possibly send more data.  Unlike the other
877  * pru_*() routines, the mbuf chains are our responsibility.  We
878  * must either enqueue them or free them.  The other pru_* routines
879  * generally are caller-frees.
880  */
881 static int
882 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
883     struct sockaddr *nam, struct mbuf *control, struct thread *td)
884 {
885 	int error = 0;
886 	struct inpcb *inp;
887 	struct tcpcb *tp = NULL;
888 #ifdef INET6
889 	int isipv6;
890 #endif
891 	TCPDEBUG0;
892 
893 	/*
894 	 * We require the pcbinfo lock if we will close the socket as part of
895 	 * this call.
896 	 */
897 	if (flags & PRUS_EOF)
898 		INP_INFO_RLOCK(&V_tcbinfo);
899 	inp = sotoinpcb(so);
900 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
901 	INP_WLOCK(inp);
902 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
903 		if (control)
904 			m_freem(control);
905 		/*
906 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
907 		 * for freeing memory.
908 		 */
909 		if (m && (flags & PRUS_NOTREADY) == 0)
910 			m_freem(m);
911 		error = ECONNRESET;
912 		goto out;
913 	}
914 #ifdef INET6
915 	isipv6 = nam && nam->sa_family == AF_INET6;
916 #endif /* INET6 */
917 	tp = intotcpcb(inp);
918 	TCPDEBUG1();
919 	if (control) {
920 		/* TCP doesn't do control messages (rights, creds, etc) */
921 		if (control->m_len) {
922 			m_freem(control);
923 			if (m)
924 				m_freem(m);
925 			error = EINVAL;
926 			goto out;
927 		}
928 		m_freem(control);	/* empty control, just free it */
929 	}
930 	if (!(flags & PRUS_OOB)) {
931 		sbappendstream(&so->so_snd, m, flags);
932 		if (nam && tp->t_state < TCPS_SYN_SENT) {
933 			/*
934 			 * Do implied connect if not yet connected,
935 			 * initialize window to default value, and
936 			 * initialize maxseg using peer's cached MSS.
937 			 */
938 #ifdef INET6
939 			if (isipv6)
940 				error = tcp6_connect(tp, nam, td);
941 #endif /* INET6 */
942 #if defined(INET6) && defined(INET)
943 			else
944 #endif
945 #ifdef INET
946 				error = tcp_connect(tp, nam, td);
947 #endif
948 			if (error)
949 				goto out;
950 			if (IS_FASTOPEN(tp->t_flags))
951 				tcp_fastopen_connect(tp);
952 			else {
953 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
954 				tcp_mss(tp, -1);
955 			}
956 		}
957 		if (flags & PRUS_EOF) {
958 			/*
959 			 * Close the send side of the connection after
960 			 * the data is sent.
961 			 */
962 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
963 			socantsendmore(so);
964 			tcp_usrclosed(tp);
965 		}
966 		if (!(inp->inp_flags & INP_DROPPED) &&
967 		    !(flags & PRUS_NOTREADY)) {
968 			if (flags & PRUS_MORETOCOME)
969 				tp->t_flags |= TF_MORETOCOME;
970 			error = tp->t_fb->tfb_tcp_output(tp);
971 			if (flags & PRUS_MORETOCOME)
972 				tp->t_flags &= ~TF_MORETOCOME;
973 		}
974 	} else {
975 		/*
976 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
977 		 */
978 		SOCKBUF_LOCK(&so->so_snd);
979 		if (sbspace(&so->so_snd) < -512) {
980 			SOCKBUF_UNLOCK(&so->so_snd);
981 			m_freem(m);
982 			error = ENOBUFS;
983 			goto out;
984 		}
985 		/*
986 		 * According to RFC961 (Assigned Protocols),
987 		 * the urgent pointer points to the last octet
988 		 * of urgent data.  We continue, however,
989 		 * to consider it to indicate the first octet
990 		 * of data past the urgent section.
991 		 * Otherwise, snd_up should be one lower.
992 		 */
993 		sbappendstream_locked(&so->so_snd, m, flags);
994 		SOCKBUF_UNLOCK(&so->so_snd);
995 		if (nam && tp->t_state < TCPS_SYN_SENT) {
996 			/*
997 			 * Do implied connect if not yet connected,
998 			 * initialize window to default value, and
999 			 * initialize maxseg using peer's cached MSS.
1000 			 */
1001 
1002 			/*
1003 			 * Not going to contemplate SYN|URG
1004 			 */
1005 			if (IS_FASTOPEN(tp->t_flags))
1006 				tp->t_flags &= ~TF_FASTOPEN;
1007 #ifdef INET6
1008 			if (isipv6)
1009 				error = tcp6_connect(tp, nam, td);
1010 #endif /* INET6 */
1011 #if defined(INET6) && defined(INET)
1012 			else
1013 #endif
1014 #ifdef INET
1015 				error = tcp_connect(tp, nam, td);
1016 #endif
1017 			if (error)
1018 				goto out;
1019 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1020 			tcp_mss(tp, -1);
1021 		}
1022 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1023 		if (!(flags & PRUS_NOTREADY)) {
1024 			tp->t_flags |= TF_FORCEDATA;
1025 			error = tp->t_fb->tfb_tcp_output(tp);
1026 			tp->t_flags &= ~TF_FORCEDATA;
1027 		}
1028 	}
1029 out:
1030 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1031 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1032 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1033 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1034 	INP_WUNLOCK(inp);
1035 	if (flags & PRUS_EOF)
1036 		INP_INFO_RUNLOCK(&V_tcbinfo);
1037 	return (error);
1038 }
1039 
1040 static int
1041 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1042 {
1043 	struct inpcb *inp;
1044 	struct tcpcb *tp;
1045 	int error;
1046 
1047 	inp = sotoinpcb(so);
1048 	INP_WLOCK(inp);
1049 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1050 		INP_WUNLOCK(inp);
1051 		for (int i = 0; i < count; i++)
1052 			m = m_free(m);
1053 		return (ECONNRESET);
1054 	}
1055 	tp = intotcpcb(inp);
1056 
1057 	SOCKBUF_LOCK(&so->so_snd);
1058 	error = sbready(&so->so_snd, m, count);
1059 	SOCKBUF_UNLOCK(&so->so_snd);
1060 	if (error == 0)
1061 		error = tp->t_fb->tfb_tcp_output(tp);
1062 	INP_WUNLOCK(inp);
1063 
1064 	return (error);
1065 }
1066 
1067 /*
1068  * Abort the TCP.  Drop the connection abruptly.
1069  */
1070 static void
1071 tcp_usr_abort(struct socket *so)
1072 {
1073 	struct inpcb *inp;
1074 	struct tcpcb *tp = NULL;
1075 	TCPDEBUG0;
1076 
1077 	inp = sotoinpcb(so);
1078 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1079 
1080 	INP_INFO_RLOCK(&V_tcbinfo);
1081 	INP_WLOCK(inp);
1082 	KASSERT(inp->inp_socket != NULL,
1083 	    ("tcp_usr_abort: inp_socket == NULL"));
1084 
1085 	/*
1086 	 * If we still have full TCP state, and we're not dropped, drop.
1087 	 */
1088 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1089 	    !(inp->inp_flags & INP_DROPPED)) {
1090 		tp = intotcpcb(inp);
1091 		TCPDEBUG1();
1092 		tcp_drop(tp, ECONNABORTED);
1093 		TCPDEBUG2(PRU_ABORT);
1094 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1095 	}
1096 	if (!(inp->inp_flags & INP_DROPPED)) {
1097 		SOCK_LOCK(so);
1098 		so->so_state |= SS_PROTOREF;
1099 		SOCK_UNLOCK(so);
1100 		inp->inp_flags |= INP_SOCKREF;
1101 	}
1102 	INP_WUNLOCK(inp);
1103 	INP_INFO_RUNLOCK(&V_tcbinfo);
1104 }
1105 
1106 /*
1107  * TCP socket is closed.  Start friendly disconnect.
1108  */
1109 static void
1110 tcp_usr_close(struct socket *so)
1111 {
1112 	struct inpcb *inp;
1113 	struct tcpcb *tp = NULL;
1114 	TCPDEBUG0;
1115 
1116 	inp = sotoinpcb(so);
1117 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1118 
1119 	INP_INFO_RLOCK(&V_tcbinfo);
1120 	INP_WLOCK(inp);
1121 	KASSERT(inp->inp_socket != NULL,
1122 	    ("tcp_usr_close: inp_socket == NULL"));
1123 
1124 	/*
1125 	 * If we still have full TCP state, and we're not dropped, initiate
1126 	 * a disconnect.
1127 	 */
1128 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1129 	    !(inp->inp_flags & INP_DROPPED)) {
1130 		tp = intotcpcb(inp);
1131 		TCPDEBUG1();
1132 		tcp_disconnect(tp);
1133 		TCPDEBUG2(PRU_CLOSE);
1134 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1135 	}
1136 	if (!(inp->inp_flags & INP_DROPPED)) {
1137 		SOCK_LOCK(so);
1138 		so->so_state |= SS_PROTOREF;
1139 		SOCK_UNLOCK(so);
1140 		inp->inp_flags |= INP_SOCKREF;
1141 	}
1142 	INP_WUNLOCK(inp);
1143 	INP_INFO_RUNLOCK(&V_tcbinfo);
1144 }
1145 
1146 /*
1147  * Receive out-of-band data.
1148  */
1149 static int
1150 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1151 {
1152 	int error = 0;
1153 	struct inpcb *inp;
1154 	struct tcpcb *tp = NULL;
1155 
1156 	TCPDEBUG0;
1157 	inp = sotoinpcb(so);
1158 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1159 	INP_WLOCK(inp);
1160 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1161 		error = ECONNRESET;
1162 		goto out;
1163 	}
1164 	tp = intotcpcb(inp);
1165 	TCPDEBUG1();
1166 	if ((so->so_oobmark == 0 &&
1167 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1168 	    so->so_options & SO_OOBINLINE ||
1169 	    tp->t_oobflags & TCPOOB_HADDATA) {
1170 		error = EINVAL;
1171 		goto out;
1172 	}
1173 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1174 		error = EWOULDBLOCK;
1175 		goto out;
1176 	}
1177 	m->m_len = 1;
1178 	*mtod(m, caddr_t) = tp->t_iobc;
1179 	if ((flags & MSG_PEEK) == 0)
1180 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1181 
1182 out:
1183 	TCPDEBUG2(PRU_RCVOOB);
1184 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1185 	INP_WUNLOCK(inp);
1186 	return (error);
1187 }
1188 
1189 #ifdef INET
1190 struct pr_usrreqs tcp_usrreqs = {
1191 	.pru_abort =		tcp_usr_abort,
1192 	.pru_accept =		tcp_usr_accept,
1193 	.pru_attach =		tcp_usr_attach,
1194 	.pru_bind =		tcp_usr_bind,
1195 	.pru_connect =		tcp_usr_connect,
1196 	.pru_control =		in_control,
1197 	.pru_detach =		tcp_usr_detach,
1198 	.pru_disconnect =	tcp_usr_disconnect,
1199 	.pru_listen =		tcp_usr_listen,
1200 	.pru_peeraddr =		in_getpeeraddr,
1201 	.pru_rcvd =		tcp_usr_rcvd,
1202 	.pru_rcvoob =		tcp_usr_rcvoob,
1203 	.pru_send =		tcp_usr_send,
1204 	.pru_ready =		tcp_usr_ready,
1205 	.pru_shutdown =		tcp_usr_shutdown,
1206 	.pru_sockaddr =		in_getsockaddr,
1207 	.pru_sosetlabel =	in_pcbsosetlabel,
1208 	.pru_close =		tcp_usr_close,
1209 };
1210 #endif /* INET */
1211 
1212 #ifdef INET6
1213 struct pr_usrreqs tcp6_usrreqs = {
1214 	.pru_abort =		tcp_usr_abort,
1215 	.pru_accept =		tcp6_usr_accept,
1216 	.pru_attach =		tcp_usr_attach,
1217 	.pru_bind =		tcp6_usr_bind,
1218 	.pru_connect =		tcp6_usr_connect,
1219 	.pru_control =		in6_control,
1220 	.pru_detach =		tcp_usr_detach,
1221 	.pru_disconnect =	tcp_usr_disconnect,
1222 	.pru_listen =		tcp6_usr_listen,
1223 	.pru_peeraddr =		in6_mapped_peeraddr,
1224 	.pru_rcvd =		tcp_usr_rcvd,
1225 	.pru_rcvoob =		tcp_usr_rcvoob,
1226 	.pru_send =		tcp_usr_send,
1227 	.pru_ready =		tcp_usr_ready,
1228 	.pru_shutdown =		tcp_usr_shutdown,
1229 	.pru_sockaddr =		in6_mapped_sockaddr,
1230 	.pru_sosetlabel =	in_pcbsosetlabel,
1231 	.pru_close =		tcp_usr_close,
1232 };
1233 #endif /* INET6 */
1234 
1235 #ifdef INET
1236 /*
1237  * Common subroutine to open a TCP connection to remote host specified
1238  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1239  * port number if needed.  Call in_pcbconnect_setup to do the routing and
1240  * to choose a local host address (interface).  If there is an existing
1241  * incarnation of the same connection in TIME-WAIT state and if the remote
1242  * host was sending CC options and if the connection duration was < MSL, then
1243  * truncate the previous TIME-WAIT state and proceed.
1244  * Initialize connection parameters and enter SYN-SENT state.
1245  */
1246 static int
1247 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1248 {
1249 	struct inpcb *inp = tp->t_inpcb, *oinp;
1250 	struct socket *so = inp->inp_socket;
1251 	struct in_addr laddr;
1252 	u_short lport;
1253 	int error;
1254 
1255 	INP_WLOCK_ASSERT(inp);
1256 	INP_HASH_WLOCK(&V_tcbinfo);
1257 
1258 	if (inp->inp_lport == 0) {
1259 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1260 		if (error)
1261 			goto out;
1262 	}
1263 
1264 	/*
1265 	 * Cannot simply call in_pcbconnect, because there might be an
1266 	 * earlier incarnation of this same connection still in
1267 	 * TIME_WAIT state, creating an ADDRINUSE error.
1268 	 */
1269 	laddr = inp->inp_laddr;
1270 	lport = inp->inp_lport;
1271 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1272 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1273 	if (error && oinp == NULL)
1274 		goto out;
1275 	if (oinp) {
1276 		error = EADDRINUSE;
1277 		goto out;
1278 	}
1279 	inp->inp_laddr = laddr;
1280 	in_pcbrehash(inp);
1281 	INP_HASH_WUNLOCK(&V_tcbinfo);
1282 
1283 	/*
1284 	 * Compute window scaling to request:
1285 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1286 	 * XXX: This should move to tcp_output().
1287 	 */
1288 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1289 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1290 		tp->request_r_scale++;
1291 
1292 	soisconnecting(so);
1293 	TCPSTAT_INC(tcps_connattempt);
1294 	tcp_state_change(tp, TCPS_SYN_SENT);
1295 	tp->iss = tcp_new_isn(tp);
1296 	tcp_sendseqinit(tp);
1297 
1298 	return 0;
1299 
1300 out:
1301 	INP_HASH_WUNLOCK(&V_tcbinfo);
1302 	return (error);
1303 }
1304 #endif /* INET */
1305 
1306 #ifdef INET6
1307 static int
1308 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1309 {
1310 	struct inpcb *inp = tp->t_inpcb;
1311 	int error;
1312 
1313 	INP_WLOCK_ASSERT(inp);
1314 	INP_HASH_WLOCK(&V_tcbinfo);
1315 
1316 	if (inp->inp_lport == 0) {
1317 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1318 		if (error)
1319 			goto out;
1320 	}
1321 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1322 	if (error != 0)
1323 		goto out;
1324 	INP_HASH_WUNLOCK(&V_tcbinfo);
1325 
1326 	/* Compute window scaling to request.  */
1327 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1328 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1329 		tp->request_r_scale++;
1330 
1331 	soisconnecting(inp->inp_socket);
1332 	TCPSTAT_INC(tcps_connattempt);
1333 	tcp_state_change(tp, TCPS_SYN_SENT);
1334 	tp->iss = tcp_new_isn(tp);
1335 	tcp_sendseqinit(tp);
1336 
1337 	return 0;
1338 
1339 out:
1340 	INP_HASH_WUNLOCK(&V_tcbinfo);
1341 	return error;
1342 }
1343 #endif /* INET6 */
1344 
1345 /*
1346  * Export TCP internal state information via a struct tcp_info, based on the
1347  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1348  * (TCP state machine, etc).  We export all information using FreeBSD-native
1349  * constants -- for example, the numeric values for tcpi_state will differ
1350  * from Linux.
1351  */
1352 static void
1353 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1354 {
1355 
1356 	INP_WLOCK_ASSERT(tp->t_inpcb);
1357 	bzero(ti, sizeof(*ti));
1358 
1359 	ti->tcpi_state = tp->t_state;
1360 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1361 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1362 	if (tp->t_flags & TF_SACK_PERMIT)
1363 		ti->tcpi_options |= TCPI_OPT_SACK;
1364 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1365 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1366 		ti->tcpi_snd_wscale = tp->snd_scale;
1367 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1368 	}
1369 	if (tp->t_flags & TF_ECN_PERMIT)
1370 		ti->tcpi_options |= TCPI_OPT_ECN;
1371 
1372 	ti->tcpi_rto = tp->t_rxtcur * tick;
1373 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1374 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1375 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1376 
1377 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1378 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1379 
1380 	/*
1381 	 * FreeBSD-specific extension fields for tcp_info.
1382 	 */
1383 	ti->tcpi_rcv_space = tp->rcv_wnd;
1384 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1385 	ti->tcpi_snd_wnd = tp->snd_wnd;
1386 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1387 	ti->tcpi_snd_nxt = tp->snd_nxt;
1388 	ti->tcpi_snd_mss = tp->t_maxseg;
1389 	ti->tcpi_rcv_mss = tp->t_maxseg;
1390 	if (tp->t_flags & TF_TOE)
1391 		ti->tcpi_options |= TCPI_OPT_TOE;
1392 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1393 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1394 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1395 }
1396 
1397 /*
1398  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1399  * socket option arguments.  When it re-acquires the lock after the copy, it
1400  * has to revalidate that the connection is still valid for the socket
1401  * option.
1402  */
1403 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
1404 	INP_WLOCK(inp);							\
1405 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
1406 		INP_WUNLOCK(inp);					\
1407 		cleanup;						\
1408 		return (ECONNRESET);					\
1409 	}								\
1410 	tp = intotcpcb(inp);						\
1411 } while(0)
1412 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1413 
1414 int
1415 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1416 {
1417 	int	error;
1418 	struct	inpcb *inp;
1419 	struct	tcpcb *tp;
1420 	struct tcp_function_block *blk;
1421 	struct tcp_function_set fsn;
1422 
1423 	error = 0;
1424 	inp = sotoinpcb(so);
1425 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1426 	INP_WLOCK(inp);
1427 	if (sopt->sopt_level != IPPROTO_TCP) {
1428 #ifdef INET6
1429 		if (inp->inp_vflag & INP_IPV6PROTO) {
1430 			INP_WUNLOCK(inp);
1431 			error = ip6_ctloutput(so, sopt);
1432 		}
1433 #endif /* INET6 */
1434 #if defined(INET6) && defined(INET)
1435 		else
1436 #endif
1437 #ifdef INET
1438 		{
1439 			INP_WUNLOCK(inp);
1440 			error = ip_ctloutput(so, sopt);
1441 		}
1442 #endif
1443 		return (error);
1444 	}
1445 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1446 		INP_WUNLOCK(inp);
1447 		return (ECONNRESET);
1448 	}
1449 	tp = intotcpcb(inp);
1450 	/*
1451 	 * Protect the TCP option TCP_FUNCTION_BLK so
1452 	 * that a sub-function can *never* overwrite this.
1453 	 */
1454 	if ((sopt->sopt_dir == SOPT_SET) &&
1455 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1456 		INP_WUNLOCK(inp);
1457 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
1458 		    sizeof fsn);
1459 		if (error)
1460 			return (error);
1461 		INP_WLOCK_RECHECK(inp);
1462 		blk = find_and_ref_tcp_functions(&fsn);
1463 		if (blk == NULL) {
1464 			INP_WUNLOCK(inp);
1465 			return (ENOENT);
1466 		}
1467 		if (tp->t_fb == blk) {
1468 			/* You already have this */
1469 			refcount_release(&blk->tfb_refcnt);
1470 			INP_WUNLOCK(inp);
1471 			return (0);
1472 		}
1473 		if (tp->t_state != TCPS_CLOSED) {
1474 			int error=EINVAL;
1475 			/*
1476 			 * The user has advanced the state
1477 			 * past the initial point, we may not
1478 			 * be able to switch.
1479 			 */
1480 			if (blk->tfb_tcp_handoff_ok != NULL) {
1481 				/*
1482 				 * Does the stack provide a
1483 				 * query mechanism, if so it may
1484 				 * still be possible?
1485 				 */
1486 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1487 			}
1488 			if (error) {
1489 				refcount_release(&blk->tfb_refcnt);
1490 				INP_WUNLOCK(inp);
1491 				return(error);
1492 			}
1493 		}
1494 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1495 			refcount_release(&blk->tfb_refcnt);
1496 			INP_WUNLOCK(inp);
1497 			return (ENOENT);
1498 		}
1499 		/*
1500 		 * Release the old refcnt, the
1501 		 * lookup acquired a ref on the
1502 		 * new one already.
1503 		 */
1504 		if (tp->t_fb->tfb_tcp_fb_fini) {
1505 			/*
1506 			 * Tell the stack to cleanup with 0 i.e.
1507 			 * the tcb is not going away.
1508 			 */
1509 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1510 		}
1511 		refcount_release(&tp->t_fb->tfb_refcnt);
1512 		tp->t_fb = blk;
1513 		if (tp->t_fb->tfb_tcp_fb_init) {
1514 			(*tp->t_fb->tfb_tcp_fb_init)(tp);
1515 		}
1516 #ifdef TCP_OFFLOAD
1517 		if (tp->t_flags & TF_TOE) {
1518 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
1519 			     sopt->sopt_name);
1520 		}
1521 #endif
1522 		INP_WUNLOCK(inp);
1523 		return (error);
1524 	} else if ((sopt->sopt_dir == SOPT_GET) &&
1525 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1526 		strcpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name);
1527 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1528 		INP_WUNLOCK(inp);
1529 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
1530 		return (error);
1531 	}
1532 	/* Pass in the INP locked, called must unlock it */
1533 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1534 }
1535 
1536 int
1537 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1538 {
1539 	int	error, opt, optval;
1540 	u_int	ui;
1541 	struct	tcp_info ti;
1542 	struct cc_algo *algo;
1543 	char	*pbuf, buf[TCP_CA_NAME_MAX];
1544 	size_t	len;
1545 
1546 	/*
1547 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1548 	 * SOPT_SET and SOPT_GET.
1549 	 */
1550 	switch (sopt->sopt_name) {
1551 	case TCP_CCALGOOPT:
1552 		INP_WUNLOCK(inp);
1553 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1554 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1555 		    sopt->sopt_valsize);
1556 		if (error) {
1557 			free(pbuf, M_TEMP);
1558 			return (error);
1559 		}
1560 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1561 		if (CC_ALGO(tp)->ctl_output != NULL)
1562 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1563 		else
1564 			error = ENOENT;
1565 		INP_WUNLOCK(inp);
1566 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1567 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1568 		free(pbuf, M_TEMP);
1569 		return (error);
1570 	}
1571 
1572 	switch (sopt->sopt_dir) {
1573 	case SOPT_SET:
1574 		switch (sopt->sopt_name) {
1575 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1576 		case TCP_MD5SIG:
1577 			if (!TCPMD5_ENABLED()) {
1578 				INP_WUNLOCK(inp);
1579 				return (ENOPROTOOPT);
1580 			}
1581 			error = TCPMD5_PCBCTL(inp, sopt);
1582 			if (error)
1583 				return (error);
1584 			goto unlock_and_done;
1585 #endif /* IPSEC */
1586 
1587 		case TCP_NODELAY:
1588 		case TCP_NOOPT:
1589 			INP_WUNLOCK(inp);
1590 			error = sooptcopyin(sopt, &optval, sizeof optval,
1591 			    sizeof optval);
1592 			if (error)
1593 				return (error);
1594 
1595 			INP_WLOCK_RECHECK(inp);
1596 			switch (sopt->sopt_name) {
1597 			case TCP_NODELAY:
1598 				opt = TF_NODELAY;
1599 				break;
1600 			case TCP_NOOPT:
1601 				opt = TF_NOOPT;
1602 				break;
1603 			default:
1604 				opt = 0; /* dead code to fool gcc */
1605 				break;
1606 			}
1607 
1608 			if (optval)
1609 				tp->t_flags |= opt;
1610 			else
1611 				tp->t_flags &= ~opt;
1612 unlock_and_done:
1613 #ifdef TCP_OFFLOAD
1614 			if (tp->t_flags & TF_TOE) {
1615 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
1616 				    sopt->sopt_name);
1617 			}
1618 #endif
1619 			INP_WUNLOCK(inp);
1620 			break;
1621 
1622 		case TCP_NOPUSH:
1623 			INP_WUNLOCK(inp);
1624 			error = sooptcopyin(sopt, &optval, sizeof optval,
1625 			    sizeof optval);
1626 			if (error)
1627 				return (error);
1628 
1629 			INP_WLOCK_RECHECK(inp);
1630 			if (optval)
1631 				tp->t_flags |= TF_NOPUSH;
1632 			else if (tp->t_flags & TF_NOPUSH) {
1633 				tp->t_flags &= ~TF_NOPUSH;
1634 				if (TCPS_HAVEESTABLISHED(tp->t_state))
1635 					error = tp->t_fb->tfb_tcp_output(tp);
1636 			}
1637 			goto unlock_and_done;
1638 
1639 		case TCP_MAXSEG:
1640 			INP_WUNLOCK(inp);
1641 			error = sooptcopyin(sopt, &optval, sizeof optval,
1642 			    sizeof optval);
1643 			if (error)
1644 				return (error);
1645 
1646 			INP_WLOCK_RECHECK(inp);
1647 			if (optval > 0 && optval <= tp->t_maxseg &&
1648 			    optval + 40 >= V_tcp_minmss)
1649 				tp->t_maxseg = optval;
1650 			else
1651 				error = EINVAL;
1652 			goto unlock_and_done;
1653 
1654 		case TCP_INFO:
1655 			INP_WUNLOCK(inp);
1656 			error = EINVAL;
1657 			break;
1658 
1659 		case TCP_CONGESTION:
1660 			INP_WUNLOCK(inp);
1661 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1662 			if (error)
1663 				break;
1664 			buf[sopt->sopt_valsize] = '\0';
1665 			INP_WLOCK_RECHECK(inp);
1666 			CC_LIST_RLOCK();
1667 			STAILQ_FOREACH(algo, &cc_list, entries)
1668 				if (strncmp(buf, algo->name,
1669 				    TCP_CA_NAME_MAX) == 0)
1670 					break;
1671 			CC_LIST_RUNLOCK();
1672 			if (algo == NULL) {
1673 				INP_WUNLOCK(inp);
1674 				error = EINVAL;
1675 				break;
1676 			}
1677 			/*
1678 			 * We hold a write lock over the tcb so it's safe to
1679 			 * do these things without ordering concerns.
1680 			 */
1681 			if (CC_ALGO(tp)->cb_destroy != NULL)
1682 				CC_ALGO(tp)->cb_destroy(tp->ccv);
1683 			CC_ALGO(tp) = algo;
1684 			/*
1685 			 * If something goes pear shaped initialising the new
1686 			 * algo, fall back to newreno (which does not
1687 			 * require initialisation).
1688 			 */
1689 			if (algo->cb_init != NULL &&
1690 			    algo->cb_init(tp->ccv) != 0) {
1691 				CC_ALGO(tp) = &newreno_cc_algo;
1692 				/*
1693 				 * The only reason init should fail is
1694 				 * because of malloc.
1695 				 */
1696 				error = ENOMEM;
1697 			}
1698 			INP_WUNLOCK(inp);
1699 			break;
1700 
1701 		case TCP_KEEPIDLE:
1702 		case TCP_KEEPINTVL:
1703 		case TCP_KEEPINIT:
1704 			INP_WUNLOCK(inp);
1705 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1706 			if (error)
1707 				return (error);
1708 
1709 			if (ui > (UINT_MAX / hz)) {
1710 				error = EINVAL;
1711 				break;
1712 			}
1713 			ui *= hz;
1714 
1715 			INP_WLOCK_RECHECK(inp);
1716 			switch (sopt->sopt_name) {
1717 			case TCP_KEEPIDLE:
1718 				tp->t_keepidle = ui;
1719 				/*
1720 				 * XXX: better check current remaining
1721 				 * timeout and "merge" it with new value.
1722 				 */
1723 				if ((tp->t_state > TCPS_LISTEN) &&
1724 				    (tp->t_state <= TCPS_CLOSING))
1725 					tcp_timer_activate(tp, TT_KEEP,
1726 					    TP_KEEPIDLE(tp));
1727 				break;
1728 			case TCP_KEEPINTVL:
1729 				tp->t_keepintvl = ui;
1730 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1731 				    (TP_MAXIDLE(tp) > 0))
1732 					tcp_timer_activate(tp, TT_2MSL,
1733 					    TP_MAXIDLE(tp));
1734 				break;
1735 			case TCP_KEEPINIT:
1736 				tp->t_keepinit = ui;
1737 				if (tp->t_state == TCPS_SYN_RECEIVED ||
1738 				    tp->t_state == TCPS_SYN_SENT)
1739 					tcp_timer_activate(tp, TT_KEEP,
1740 					    TP_KEEPINIT(tp));
1741 				break;
1742 			}
1743 			goto unlock_and_done;
1744 
1745 		case TCP_KEEPCNT:
1746 			INP_WUNLOCK(inp);
1747 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1748 			if (error)
1749 				return (error);
1750 
1751 			INP_WLOCK_RECHECK(inp);
1752 			tp->t_keepcnt = ui;
1753 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1754 			    (TP_MAXIDLE(tp) > 0))
1755 				tcp_timer_activate(tp, TT_2MSL,
1756 				    TP_MAXIDLE(tp));
1757 			goto unlock_and_done;
1758 
1759 #ifdef TCPPCAP
1760 		case TCP_PCAP_OUT:
1761 		case TCP_PCAP_IN:
1762 			INP_WUNLOCK(inp);
1763 			error = sooptcopyin(sopt, &optval, sizeof optval,
1764 			    sizeof optval);
1765 			if (error)
1766 				return (error);
1767 
1768 			INP_WLOCK_RECHECK(inp);
1769 			if (optval >= 0)
1770 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1771 					&(tp->t_outpkts) : &(tp->t_inpkts),
1772 					optval);
1773 			else
1774 				error = EINVAL;
1775 			goto unlock_and_done;
1776 #endif
1777 
1778 		case TCP_FASTOPEN: {
1779 			struct tcp_fastopen tfo_optval;
1780 
1781 			INP_WUNLOCK(inp);
1782 			if (!V_tcp_fastopen_client_enable &&
1783 			    !V_tcp_fastopen_server_enable)
1784 				return (EPERM);
1785 
1786 			error = sooptcopyin(sopt, &tfo_optval,
1787 				    sizeof(tfo_optval), sizeof(int));
1788 			if (error)
1789 				return (error);
1790 
1791 			INP_WLOCK_RECHECK(inp);
1792 			if (tfo_optval.enable) {
1793 				if (tp->t_state == TCPS_LISTEN) {
1794 					if (!V_tcp_fastopen_server_enable) {
1795 						error = EPERM;
1796 						goto unlock_and_done;
1797 					}
1798 
1799 					tp->t_flags |= TF_FASTOPEN;
1800 					if (tp->t_tfo_pending == NULL)
1801 						tp->t_tfo_pending =
1802 						    tcp_fastopen_alloc_counter();
1803 				} else {
1804 					/*
1805 					 * If a pre-shared key was provided,
1806 					 * stash it in the client cookie
1807 					 * field of the tcpcb for use during
1808 					 * connect.
1809 					 */
1810 					if (sopt->sopt_valsize ==
1811 					    sizeof(tfo_optval)) {
1812 						memcpy(tp->t_tfo_cookie.client,
1813 						       tfo_optval.psk,
1814 						       TCP_FASTOPEN_PSK_LEN);
1815 						tp->t_tfo_client_cookie_len =
1816 						    TCP_FASTOPEN_PSK_LEN;
1817 					}
1818 					tp->t_flags |= TF_FASTOPEN;
1819 				}
1820 			} else
1821 				tp->t_flags &= ~TF_FASTOPEN;
1822 			goto unlock_and_done;
1823 		}
1824 
1825 		default:
1826 			INP_WUNLOCK(inp);
1827 			error = ENOPROTOOPT;
1828 			break;
1829 		}
1830 		break;
1831 
1832 	case SOPT_GET:
1833 		tp = intotcpcb(inp);
1834 		switch (sopt->sopt_name) {
1835 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1836 		case TCP_MD5SIG:
1837 			if (!TCPMD5_ENABLED()) {
1838 				INP_WUNLOCK(inp);
1839 				return (ENOPROTOOPT);
1840 			}
1841 			error = TCPMD5_PCBCTL(inp, sopt);
1842 			break;
1843 #endif
1844 
1845 		case TCP_NODELAY:
1846 			optval = tp->t_flags & TF_NODELAY;
1847 			INP_WUNLOCK(inp);
1848 			error = sooptcopyout(sopt, &optval, sizeof optval);
1849 			break;
1850 		case TCP_MAXSEG:
1851 			optval = tp->t_maxseg;
1852 			INP_WUNLOCK(inp);
1853 			error = sooptcopyout(sopt, &optval, sizeof optval);
1854 			break;
1855 		case TCP_NOOPT:
1856 			optval = tp->t_flags & TF_NOOPT;
1857 			INP_WUNLOCK(inp);
1858 			error = sooptcopyout(sopt, &optval, sizeof optval);
1859 			break;
1860 		case TCP_NOPUSH:
1861 			optval = tp->t_flags & TF_NOPUSH;
1862 			INP_WUNLOCK(inp);
1863 			error = sooptcopyout(sopt, &optval, sizeof optval);
1864 			break;
1865 		case TCP_INFO:
1866 			tcp_fill_info(tp, &ti);
1867 			INP_WUNLOCK(inp);
1868 			error = sooptcopyout(sopt, &ti, sizeof ti);
1869 			break;
1870 		case TCP_CONGESTION:
1871 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1872 			INP_WUNLOCK(inp);
1873 			error = sooptcopyout(sopt, buf, len + 1);
1874 			break;
1875 		case TCP_KEEPIDLE:
1876 		case TCP_KEEPINTVL:
1877 		case TCP_KEEPINIT:
1878 		case TCP_KEEPCNT:
1879 			switch (sopt->sopt_name) {
1880 			case TCP_KEEPIDLE:
1881 				ui = TP_KEEPIDLE(tp) / hz;
1882 				break;
1883 			case TCP_KEEPINTVL:
1884 				ui = TP_KEEPINTVL(tp) / hz;
1885 				break;
1886 			case TCP_KEEPINIT:
1887 				ui = TP_KEEPINIT(tp) / hz;
1888 				break;
1889 			case TCP_KEEPCNT:
1890 				ui = TP_KEEPCNT(tp);
1891 				break;
1892 			}
1893 			INP_WUNLOCK(inp);
1894 			error = sooptcopyout(sopt, &ui, sizeof(ui));
1895 			break;
1896 #ifdef TCPPCAP
1897 		case TCP_PCAP_OUT:
1898 		case TCP_PCAP_IN:
1899 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1900 					&(tp->t_outpkts) : &(tp->t_inpkts));
1901 			INP_WUNLOCK(inp);
1902 			error = sooptcopyout(sopt, &optval, sizeof optval);
1903 			break;
1904 #endif
1905 		case TCP_FASTOPEN:
1906 			optval = tp->t_flags & TF_FASTOPEN;
1907 			INP_WUNLOCK(inp);
1908 			error = sooptcopyout(sopt, &optval, sizeof optval);
1909 			break;
1910 		default:
1911 			INP_WUNLOCK(inp);
1912 			error = ENOPROTOOPT;
1913 			break;
1914 		}
1915 		break;
1916 	}
1917 	return (error);
1918 }
1919 #undef INP_WLOCK_RECHECK
1920 #undef INP_WLOCK_RECHECK_CLEANUP
1921 
1922 /*
1923  * Attach TCP protocol to socket, allocating
1924  * internet protocol control block, tcp control block,
1925  * bufer space, and entering LISTEN state if to accept connections.
1926  */
1927 static int
1928 tcp_attach(struct socket *so)
1929 {
1930 	struct tcpcb *tp;
1931 	struct inpcb *inp;
1932 	int error;
1933 
1934 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1935 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1936 		if (error)
1937 			return (error);
1938 	}
1939 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1940 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1941 	INP_INFO_RLOCK(&V_tcbinfo);
1942 	error = in_pcballoc(so, &V_tcbinfo);
1943 	if (error) {
1944 		INP_INFO_RUNLOCK(&V_tcbinfo);
1945 		return (error);
1946 	}
1947 	inp = sotoinpcb(so);
1948 #ifdef INET6
1949 	if (inp->inp_vflag & INP_IPV6PROTO) {
1950 		inp->inp_vflag |= INP_IPV6;
1951 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1952 			inp->inp_vflag |= INP_IPV4;
1953 		inp->in6p_hops = -1;	/* use kernel default */
1954 	}
1955 	else
1956 #endif
1957 	inp->inp_vflag |= INP_IPV4;
1958 	tp = tcp_newtcpcb(inp);
1959 	if (tp == NULL) {
1960 		in_pcbdetach(inp);
1961 		in_pcbfree(inp);
1962 		INP_INFO_RUNLOCK(&V_tcbinfo);
1963 		return (ENOBUFS);
1964 	}
1965 	tp->t_state = TCPS_CLOSED;
1966 	INP_WUNLOCK(inp);
1967 	INP_INFO_RUNLOCK(&V_tcbinfo);
1968 	TCPSTATES_INC(TCPS_CLOSED);
1969 	return (0);
1970 }
1971 
1972 /*
1973  * Initiate (or continue) disconnect.
1974  * If embryonic state, just send reset (once).
1975  * If in ``let data drain'' option and linger null, just drop.
1976  * Otherwise (hard), mark socket disconnecting and drop
1977  * current input data; switch states based on user close, and
1978  * send segment to peer (with FIN).
1979  */
1980 static void
1981 tcp_disconnect(struct tcpcb *tp)
1982 {
1983 	struct inpcb *inp = tp->t_inpcb;
1984 	struct socket *so = inp->inp_socket;
1985 
1986 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1987 	INP_WLOCK_ASSERT(inp);
1988 
1989 	/*
1990 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1991 	 * socket is still open.
1992 	 */
1993 	if (tp->t_state < TCPS_ESTABLISHED) {
1994 		tp = tcp_close(tp);
1995 		KASSERT(tp != NULL,
1996 		    ("tcp_disconnect: tcp_close() returned NULL"));
1997 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1998 		tp = tcp_drop(tp, 0);
1999 		KASSERT(tp != NULL,
2000 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2001 	} else {
2002 		soisdisconnecting(so);
2003 		sbflush(&so->so_rcv);
2004 		tcp_usrclosed(tp);
2005 		if (!(inp->inp_flags & INP_DROPPED))
2006 			tp->t_fb->tfb_tcp_output(tp);
2007 	}
2008 }
2009 
2010 /*
2011  * User issued close, and wish to trail through shutdown states:
2012  * if never received SYN, just forget it.  If got a SYN from peer,
2013  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2014  * If already got a FIN from peer, then almost done; go to LAST_ACK
2015  * state.  In all other cases, have already sent FIN to peer (e.g.
2016  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2017  * for peer to send FIN or not respond to keep-alives, etc.
2018  * We can let the user exit from the close as soon as the FIN is acked.
2019  */
2020 static void
2021 tcp_usrclosed(struct tcpcb *tp)
2022 {
2023 
2024 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2025 	INP_WLOCK_ASSERT(tp->t_inpcb);
2026 
2027 	switch (tp->t_state) {
2028 	case TCPS_LISTEN:
2029 #ifdef TCP_OFFLOAD
2030 		tcp_offload_listen_stop(tp);
2031 #endif
2032 		tcp_state_change(tp, TCPS_CLOSED);
2033 		/* FALLTHROUGH */
2034 	case TCPS_CLOSED:
2035 		tp = tcp_close(tp);
2036 		/*
2037 		 * tcp_close() should never return NULL here as the socket is
2038 		 * still open.
2039 		 */
2040 		KASSERT(tp != NULL,
2041 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2042 		break;
2043 
2044 	case TCPS_SYN_SENT:
2045 	case TCPS_SYN_RECEIVED:
2046 		tp->t_flags |= TF_NEEDFIN;
2047 		break;
2048 
2049 	case TCPS_ESTABLISHED:
2050 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2051 		break;
2052 
2053 	case TCPS_CLOSE_WAIT:
2054 		tcp_state_change(tp, TCPS_LAST_ACK);
2055 		break;
2056 	}
2057 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2058 		soisdisconnected(tp->t_inpcb->inp_socket);
2059 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
2060 		if (tp->t_state == TCPS_FIN_WAIT_2) {
2061 			int timeout;
2062 
2063 			timeout = (tcp_fast_finwait2_recycle) ?
2064 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2065 			tcp_timer_activate(tp, TT_2MSL, timeout);
2066 		}
2067 	}
2068 }
2069 
2070 #ifdef DDB
2071 static void
2072 db_print_indent(int indent)
2073 {
2074 	int i;
2075 
2076 	for (i = 0; i < indent; i++)
2077 		db_printf(" ");
2078 }
2079 
2080 static void
2081 db_print_tstate(int t_state)
2082 {
2083 
2084 	switch (t_state) {
2085 	case TCPS_CLOSED:
2086 		db_printf("TCPS_CLOSED");
2087 		return;
2088 
2089 	case TCPS_LISTEN:
2090 		db_printf("TCPS_LISTEN");
2091 		return;
2092 
2093 	case TCPS_SYN_SENT:
2094 		db_printf("TCPS_SYN_SENT");
2095 		return;
2096 
2097 	case TCPS_SYN_RECEIVED:
2098 		db_printf("TCPS_SYN_RECEIVED");
2099 		return;
2100 
2101 	case TCPS_ESTABLISHED:
2102 		db_printf("TCPS_ESTABLISHED");
2103 		return;
2104 
2105 	case TCPS_CLOSE_WAIT:
2106 		db_printf("TCPS_CLOSE_WAIT");
2107 		return;
2108 
2109 	case TCPS_FIN_WAIT_1:
2110 		db_printf("TCPS_FIN_WAIT_1");
2111 		return;
2112 
2113 	case TCPS_CLOSING:
2114 		db_printf("TCPS_CLOSING");
2115 		return;
2116 
2117 	case TCPS_LAST_ACK:
2118 		db_printf("TCPS_LAST_ACK");
2119 		return;
2120 
2121 	case TCPS_FIN_WAIT_2:
2122 		db_printf("TCPS_FIN_WAIT_2");
2123 		return;
2124 
2125 	case TCPS_TIME_WAIT:
2126 		db_printf("TCPS_TIME_WAIT");
2127 		return;
2128 
2129 	default:
2130 		db_printf("unknown");
2131 		return;
2132 	}
2133 }
2134 
2135 static void
2136 db_print_tflags(u_int t_flags)
2137 {
2138 	int comma;
2139 
2140 	comma = 0;
2141 	if (t_flags & TF_ACKNOW) {
2142 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2143 		comma = 1;
2144 	}
2145 	if (t_flags & TF_DELACK) {
2146 		db_printf("%sTF_DELACK", comma ? ", " : "");
2147 		comma = 1;
2148 	}
2149 	if (t_flags & TF_NODELAY) {
2150 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2151 		comma = 1;
2152 	}
2153 	if (t_flags & TF_NOOPT) {
2154 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2155 		comma = 1;
2156 	}
2157 	if (t_flags & TF_SENTFIN) {
2158 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2159 		comma = 1;
2160 	}
2161 	if (t_flags & TF_REQ_SCALE) {
2162 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2163 		comma = 1;
2164 	}
2165 	if (t_flags & TF_RCVD_SCALE) {
2166 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2167 		comma = 1;
2168 	}
2169 	if (t_flags & TF_REQ_TSTMP) {
2170 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2171 		comma = 1;
2172 	}
2173 	if (t_flags & TF_RCVD_TSTMP) {
2174 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2175 		comma = 1;
2176 	}
2177 	if (t_flags & TF_SACK_PERMIT) {
2178 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2179 		comma = 1;
2180 	}
2181 	if (t_flags & TF_NEEDSYN) {
2182 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2183 		comma = 1;
2184 	}
2185 	if (t_flags & TF_NEEDFIN) {
2186 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2187 		comma = 1;
2188 	}
2189 	if (t_flags & TF_NOPUSH) {
2190 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2191 		comma = 1;
2192 	}
2193 	if (t_flags & TF_MORETOCOME) {
2194 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2195 		comma = 1;
2196 	}
2197 	if (t_flags & TF_LQ_OVERFLOW) {
2198 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2199 		comma = 1;
2200 	}
2201 	if (t_flags & TF_LASTIDLE) {
2202 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2203 		comma = 1;
2204 	}
2205 	if (t_flags & TF_RXWIN0SENT) {
2206 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2207 		comma = 1;
2208 	}
2209 	if (t_flags & TF_FASTRECOVERY) {
2210 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2211 		comma = 1;
2212 	}
2213 	if (t_flags & TF_CONGRECOVERY) {
2214 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2215 		comma = 1;
2216 	}
2217 	if (t_flags & TF_WASFRECOVERY) {
2218 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2219 		comma = 1;
2220 	}
2221 	if (t_flags & TF_SIGNATURE) {
2222 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2223 		comma = 1;
2224 	}
2225 	if (t_flags & TF_FORCEDATA) {
2226 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2227 		comma = 1;
2228 	}
2229 	if (t_flags & TF_TSO) {
2230 		db_printf("%sTF_TSO", comma ? ", " : "");
2231 		comma = 1;
2232 	}
2233 	if (t_flags & TF_ECN_PERMIT) {
2234 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2235 		comma = 1;
2236 	}
2237 	if (t_flags & TF_FASTOPEN) {
2238 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2239 		comma = 1;
2240 	}
2241 }
2242 
2243 static void
2244 db_print_toobflags(char t_oobflags)
2245 {
2246 	int comma;
2247 
2248 	comma = 0;
2249 	if (t_oobflags & TCPOOB_HAVEDATA) {
2250 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2251 		comma = 1;
2252 	}
2253 	if (t_oobflags & TCPOOB_HADDATA) {
2254 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2255 		comma = 1;
2256 	}
2257 }
2258 
2259 static void
2260 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2261 {
2262 
2263 	db_print_indent(indent);
2264 	db_printf("%s at %p\n", name, tp);
2265 
2266 	indent += 2;
2267 
2268 	db_print_indent(indent);
2269 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2270 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2271 
2272 	db_print_indent(indent);
2273 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2274 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2275 
2276 	db_print_indent(indent);
2277 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2278 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2279 
2280 	db_print_indent(indent);
2281 	db_printf("t_state: %d (", tp->t_state);
2282 	db_print_tstate(tp->t_state);
2283 	db_printf(")\n");
2284 
2285 	db_print_indent(indent);
2286 	db_printf("t_flags: 0x%x (", tp->t_flags);
2287 	db_print_tflags(tp->t_flags);
2288 	db_printf(")\n");
2289 
2290 	db_print_indent(indent);
2291 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2292 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2293 
2294 	db_print_indent(indent);
2295 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2296 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2297 
2298 	db_print_indent(indent);
2299 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2300 	    tp->iss, tp->irs, tp->rcv_nxt);
2301 
2302 	db_print_indent(indent);
2303 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2304 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2305 
2306 	db_print_indent(indent);
2307 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
2308 	   tp->snd_wnd, tp->snd_cwnd);
2309 
2310 	db_print_indent(indent);
2311 	db_printf("snd_ssthresh: %u   snd_recover: "
2312 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2313 
2314 	db_print_indent(indent);
2315 	db_printf("t_rcvtime: %u   t_startime: %u\n",
2316 	    tp->t_rcvtime, tp->t_starttime);
2317 
2318 	db_print_indent(indent);
2319 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
2320 	    tp->t_rtttime, tp->t_rtseq);
2321 
2322 	db_print_indent(indent);
2323 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
2324 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2325 
2326 	db_print_indent(indent);
2327 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2328 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2329 	    tp->t_rttbest);
2330 
2331 	db_print_indent(indent);
2332 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2333 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2334 
2335 	db_print_indent(indent);
2336 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2337 	db_print_toobflags(tp->t_oobflags);
2338 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2339 
2340 	db_print_indent(indent);
2341 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2342 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2343 
2344 	db_print_indent(indent);
2345 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
2346 	    tp->ts_recent, tp->ts_recent_age);
2347 
2348 	db_print_indent(indent);
2349 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
2350 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2351 
2352 	db_print_indent(indent);
2353 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
2354 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2355 	    tp->snd_recover_prev, tp->t_badrxtwin);
2356 
2357 	db_print_indent(indent);
2358 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
2359 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2360 
2361 	db_print_indent(indent);
2362 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2363 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2364 
2365 	/* Skip sackblks, sackhint. */
2366 
2367 	db_print_indent(indent);
2368 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2369 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2370 }
2371 
2372 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2373 {
2374 	struct tcpcb *tp;
2375 
2376 	if (!have_addr) {
2377 		db_printf("usage: show tcpcb <addr>\n");
2378 		return;
2379 	}
2380 	tp = (struct tcpcb *)addr;
2381 
2382 	db_print_tcpcb(tp, "tcpcb", 0);
2383 }
2384 #endif
2385