xref: /dflybsd-src/sys/netinet/tcp_usrreq.c (revision 30e3ae034c9501c319c415ada6d5e23372649c88)
1 /*
2  * Copyright (c) 2003, 2004 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 2003, 2004 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Jeffrey M. Hsu.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of The DragonFly Project nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific, prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1982, 1986, 1988, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
67  * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.17 2002/10/11 11:46:44 ume Exp $
68  * $DragonFly: src/sys/netinet/tcp_usrreq.c,v 1.51 2008/09/29 20:52:23 dillon Exp $
69  */
70 
71 #include "opt_ipsec.h"
72 #include "opt_inet.h"
73 #include "opt_inet6.h"
74 #include "opt_tcpdebug.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/malloc.h>
80 #include <sys/sysctl.h>
81 #include <sys/globaldata.h>
82 #include <sys/thread.h>
83 
84 #include <sys/mbuf.h>
85 #ifdef INET6
86 #include <sys/domain.h>
87 #endif /* INET6 */
88 #include <sys/socket.h>
89 #include <sys/socketvar.h>
90 #include <sys/protosw.h>
91 
92 #include <sys/thread2.h>
93 #include <sys/msgport2.h>
94 #include <sys/socketvar2.h>
95 
96 #include <net/if.h>
97 #include <net/netisr.h>
98 #include <net/route.h>
99 
100 #include <net/netmsg2.h>
101 
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
104 #ifdef INET6
105 #include <netinet/ip6.h>
106 #endif
107 #include <netinet/in_pcb.h>
108 #ifdef INET6
109 #include <netinet6/in6_pcb.h>
110 #endif
111 #include <netinet/in_var.h>
112 #include <netinet/ip_var.h>
113 #ifdef INET6
114 #include <netinet6/ip6_var.h>
115 #include <netinet6/tcp6_var.h>
116 #endif
117 #include <netinet/tcp.h>
118 #include <netinet/tcp_fsm.h>
119 #include <netinet/tcp_seq.h>
120 #include <netinet/tcp_timer.h>
121 #include <netinet/tcp_timer2.h>
122 #include <netinet/tcp_var.h>
123 #include <netinet/tcpip.h>
124 #ifdef TCPDEBUG
125 #include <netinet/tcp_debug.h>
126 #endif
127 
128 #ifdef IPSEC
129 #include <netinet6/ipsec.h>
130 #endif /*IPSEC*/
131 
132 /*
133  * TCP protocol interface to socket abstraction.
134  */
135 extern	char *tcpstates[];	/* XXX ??? */
136 
137 static int	tcp_attach (struct socket *, struct pru_attach_info *);
138 static int	tcp_connect (struct tcpcb *, int flags, struct mbuf *m,
139 				struct sockaddr *, struct thread *);
140 #ifdef INET6
141 static int	tcp6_connect (struct tcpcb *, int flags, struct mbuf *m,
142 				struct sockaddr *, struct thread *);
143 static int	tcp6_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf *m,
144 				struct sockaddr_in6 *sin6,
145 				struct in6_addr *addr6);
146 #endif /* INET6 */
147 static struct tcpcb *
148 		tcp_disconnect (struct tcpcb *);
149 static struct tcpcb *
150 		tcp_usrclosed (struct tcpcb *);
151 
152 #ifdef TCPDEBUG
153 #define	TCPDEBUG0	int ostate = 0
154 #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
155 #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
156 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
157 #else
158 #define	TCPDEBUG0
159 #define	TCPDEBUG1()
160 #define	TCPDEBUG2(req)
161 #endif
162 
163 /*
164  * TCP attaches to socket via pru_attach(), reserving space,
165  * and an internet control block.  This is likely occuring on
166  * cpu0 and may have to move later when we bind/connect.
167  */
168 static int
169 tcp_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
170 {
171 	int error;
172 	struct inpcb *inp;
173 	struct tcpcb *tp = 0;
174 	TCPDEBUG0;
175 
176 	soreference(so);
177 	inp = so->so_pcb;
178 	TCPDEBUG1();
179 	if (inp) {
180 		error = EISCONN;
181 		goto out;
182 	}
183 
184 	error = tcp_attach(so, ai);
185 	if (error)
186 		goto out;
187 
188 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
189 		so->so_linger = TCP_LINGERTIME;
190 	tp = sototcpcb(so);
191 out:
192 	sofree(so);		/* from ref above */
193 	TCPDEBUG2(PRU_ATTACH);
194 	return error;
195 }
196 
197 /*
198  * pru_detach() detaches the TCP protocol from the socket.
199  * If the protocol state is non-embryonic, then can't
200  * do this directly: have to initiate a pru_disconnect(),
201  * which may finish later; embryonic TCB's can just
202  * be discarded here.
203  */
204 static int
205 tcp_usr_detach(struct socket *so)
206 {
207 	int error = 0;
208 	struct inpcb *inp;
209 	struct tcpcb *tp;
210 	TCPDEBUG0;
211 
212 	inp = so->so_pcb;
213 
214 	/*
215 	 * If the inp is already detached it may have been due to an async
216 	 * close.  Just return as if no error occured.
217 	 */
218 	if (inp == NULL)
219 		return 0;
220 
221 	/*
222 	 * It's possible for the tcpcb (tp) to disconnect from the inp due
223 	 * to tcp_drop()->tcp_close() being called.  This may occur *after*
224 	 * the detach message has been queued so we may find a NULL tp here.
225 	 */
226 	if ((tp = intotcpcb(inp)) != NULL) {
227 		TCPDEBUG1();
228 		tp = tcp_disconnect(tp);
229 		TCPDEBUG2(PRU_DETACH);
230 	}
231 	return error;
232 }
233 
234 /*
235  * Note: ignore_error is non-zero for certain disconnection races
236  * which we want to silently allow, otherwise close() may return
237  * an unexpected error.
238  */
239 #define	COMMON_START(so, inp, ignore_error)			\
240 	TCPDEBUG0; 		\
241 				\
242 	inp = so->so_pcb; 	\
243 	do {			\
244 		 if (inp == NULL) {				\
245 			 return (ignore_error ? 0 : EINVAL);	\
246 		 }						\
247 		 tp = intotcpcb(inp);				\
248 		 TCPDEBUG1();					\
249 	} while(0)
250 
251 #define COMMON_END(req)	out: TCPDEBUG2(req); return error; goto out
252 
253 /*
254  * Give the socket an address.
255  */
256 static int
257 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
258 {
259 	int error = 0;
260 	struct inpcb *inp;
261 	struct tcpcb *tp;
262 	struct sockaddr_in *sinp;
263 
264 	COMMON_START(so, inp, 0);
265 
266 	/*
267 	 * Must check for multicast addresses and disallow binding
268 	 * to them.
269 	 */
270 	sinp = (struct sockaddr_in *)nam;
271 	if (sinp->sin_family == AF_INET &&
272 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
273 		error = EAFNOSUPPORT;
274 		goto out;
275 	}
276 	error = in_pcbbind(inp, nam, td);
277 	if (error)
278 		goto out;
279 	COMMON_END(PRU_BIND);
280 
281 }
282 
283 #ifdef INET6
284 static int
285 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
286 {
287 	int error = 0;
288 	struct inpcb *inp;
289 	struct tcpcb *tp;
290 	struct sockaddr_in6 *sin6p;
291 
292 	COMMON_START(so, inp, 0);
293 
294 	/*
295 	 * Must check for multicast addresses and disallow binding
296 	 * to them.
297 	 */
298 	sin6p = (struct sockaddr_in6 *)nam;
299 	if (sin6p->sin6_family == AF_INET6 &&
300 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
301 		error = EAFNOSUPPORT;
302 		goto out;
303 	}
304 	inp->inp_vflag &= ~INP_IPV4;
305 	inp->inp_vflag |= INP_IPV6;
306 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
307 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
308 			inp->inp_vflag |= INP_IPV4;
309 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
310 			struct sockaddr_in sin;
311 
312 			in6_sin6_2_sin(&sin, sin6p);
313 			inp->inp_vflag |= INP_IPV4;
314 			inp->inp_vflag &= ~INP_IPV6;
315 			error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
316 			goto out;
317 		}
318 	}
319 	error = in6_pcbbind(inp, nam, td);
320 	if (error)
321 		goto out;
322 	COMMON_END(PRU_BIND);
323 }
324 #endif /* INET6 */
325 
326 #ifdef SMP
327 struct netmsg_inswildcard {
328 	struct netmsg		nm_netmsg;
329 	struct inpcb		*nm_inp;
330 	struct inpcbinfo	*nm_pcbinfo;
331 };
332 
333 static void
334 in_pcbinswildcardhash_handler(struct netmsg *msg0)
335 {
336 	struct netmsg_inswildcard *msg = (struct netmsg_inswildcard *)msg0;
337 
338 	in_pcbinswildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo);
339 	lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, 0);
340 }
341 #endif
342 
343 /*
344  * Prepare to accept connections.
345  */
346 static int
347 tcp_usr_listen(struct socket *so, struct thread *td)
348 {
349 	int error = 0;
350 	struct inpcb *inp;
351 	struct tcpcb *tp;
352 #ifdef SMP
353 	int cpu;
354 #endif
355 
356 	COMMON_START(so, inp, 0);
357 	if (inp->inp_lport == 0) {
358 		error = in_pcbbind(inp, NULL, td);
359 		if (error != 0)
360 			goto out;
361 	}
362 
363 	tp->t_state = TCPS_LISTEN;
364 	tp->tt_msg = NULL; /* Catch any invalid timer usage */
365 #ifdef SMP
366 	/*
367 	 * We have to set the flag because we can't have other cpus
368 	 * messing with our inp's flags.
369 	 */
370 	inp->inp_flags |= INP_WILDCARD_MP;
371 	for (cpu = 0; cpu < ncpus2; cpu++) {
372 		struct netmsg_inswildcard *msg;
373 
374 		if (cpu == mycpu->gd_cpuid) {
375 			in_pcbinswildcardhash(inp);
376 			continue;
377 		}
378 
379 		msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
380 			      M_INTWAIT);
381 		netmsg_init(&msg->nm_netmsg, NULL, &netisr_afree_rport,
382 			    0, in_pcbinswildcardhash_handler);
383 		msg->nm_inp = inp;
384 		msg->nm_pcbinfo = &tcbinfo[cpu];
385 		lwkt_sendmsg(cpu_portfn(cpu), &msg->nm_netmsg.nm_lmsg);
386 	}
387 #else
388 	in_pcbinswildcardhash(inp);
389 #endif
390 	COMMON_END(PRU_LISTEN);
391 }
392 
393 #ifdef INET6
394 static int
395 tcp6_usr_listen(struct socket *so, struct thread *td)
396 {
397 	int error = 0;
398 	struct inpcb *inp;
399 	struct tcpcb *tp;
400 #ifdef SMP
401 	int cpu;
402 #endif
403 
404 	COMMON_START(so, inp, 0);
405 	if (inp->inp_lport == 0) {
406 		if (!(inp->inp_flags & IN6P_IPV6_V6ONLY))
407 			inp->inp_vflag |= INP_IPV4;
408 		else
409 			inp->inp_vflag &= ~INP_IPV4;
410 		error = in6_pcbbind(inp, NULL, td);
411 	}
412 	if (error == 0)
413 		tp->t_state = TCPS_LISTEN;
414 #ifdef SMP
415 	/*
416 	 * We have to set the flag because we can't have other cpus
417 	 * messing with our inp's flags.
418 	 */
419 	inp->inp_flags |= INP_WILDCARD_MP;
420 	for (cpu = 0; cpu < ncpus2; cpu++) {
421 		struct netmsg_inswildcard *msg;
422 
423 		if (cpu == mycpu->gd_cpuid) {
424 			in_pcbinswildcardhash(inp);
425 			continue;
426 		}
427 
428 		msg = kmalloc(sizeof(struct netmsg_inswildcard), M_LWKTMSG,
429 			      M_INTWAIT);
430 		netmsg_init(&msg->nm_netmsg, NULL, &netisr_afree_rport,
431 			    0, in_pcbinswildcardhash_handler);
432 		msg->nm_inp = inp;
433 		msg->nm_pcbinfo = &tcbinfo[cpu];
434 		lwkt_sendmsg(cpu_portfn(cpu), &msg->nm_netmsg.nm_lmsg);
435 	}
436 #else
437 	in_pcbinswildcardhash(inp);
438 #endif
439 	COMMON_END(PRU_LISTEN);
440 }
441 #endif /* INET6 */
442 
443 /*
444  * Initiate connection to peer.
445  * Create a template for use in transmissions on this connection.
446  * Enter SYN_SENT state, and mark socket as connecting.
447  * Start keep-alive timer, and seed output sequence space.
448  * Send initial segment on connection.
449  */
450 static int
451 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
452 {
453 	int error = 0;
454 	struct inpcb *inp;
455 	struct tcpcb *tp;
456 	struct sockaddr_in *sinp;
457 
458 	COMMON_START(so, inp, 0);
459 
460 	/*
461 	 * Must disallow TCP ``connections'' to multicast addresses.
462 	 */
463 	sinp = (struct sockaddr_in *)nam;
464 	if (sinp->sin_family == AF_INET
465 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
466 		error = EAFNOSUPPORT;
467 		goto out;
468 	}
469 
470 	if (!prison_remote_ip(td, (struct sockaddr*)sinp)) {
471 		error = EAFNOSUPPORT; /* IPv6 only jail */
472 		goto out;
473 	}
474 
475 	if ((error = tcp_connect(tp, 0, NULL, nam, td)) != 0)
476 		goto out;
477 	COMMON_END(PRU_CONNECT);
478 }
479 
480 #ifdef INET6
481 static int
482 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
483 {
484 	int error = 0;
485 	struct inpcb *inp;
486 	struct tcpcb *tp;
487 	struct sockaddr_in6 *sin6p;
488 
489 	COMMON_START(so, inp, 0);
490 
491 	/*
492 	 * Must disallow TCP ``connections'' to multicast addresses.
493 	 */
494 	sin6p = (struct sockaddr_in6 *)nam;
495 	if (sin6p->sin6_family == AF_INET6
496 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
497 		error = EAFNOSUPPORT;
498 		goto out;
499 	}
500 
501 	if (!prison_remote_ip(td, nam)) {
502 		error = EAFNOSUPPORT; /* IPv4 only jail */
503 		goto out;
504 	}
505 
506 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
507 		struct sockaddr_in sin;
508 
509 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
510 			error = EINVAL;
511 			goto out;
512 		}
513 
514 		in6_sin6_2_sin(&sin, sin6p);
515 		inp->inp_vflag |= INP_IPV4;
516 		inp->inp_vflag &= ~INP_IPV6;
517 		error = tcp_connect(tp, 0, NULL, (struct sockaddr *)&sin, td);
518 		if (error)
519 			goto out;
520 		goto out;
521 	}
522 	inp->inp_vflag &= ~INP_IPV4;
523 	inp->inp_vflag |= INP_IPV6;
524 	inp->inp_inc.inc_isipv6 = 1;
525 	if ((error = tcp6_connect(tp, 0, NULL, nam, td)) != 0)
526 		goto out;
527 	error = tcp_output(tp);
528 	COMMON_END(PRU_CONNECT);
529 }
530 #endif /* INET6 */
531 
532 /*
533  * Initiate disconnect from peer.
534  * If connection never passed embryonic stage, just drop;
535  * else if don't need to let data drain, then can just drop anyways,
536  * else have to begin TCP shutdown process: mark socket disconnecting,
537  * drain unread data, state switch to reflect user close, and
538  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
539  * when peer sends FIN and acks ours.
540  *
541  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
542  */
543 static int
544 tcp_usr_disconnect(struct socket *so)
545 {
546 	int error = 0;
547 	struct inpcb *inp;
548 	struct tcpcb *tp;
549 
550 	COMMON_START(so, inp, 1);
551 	tp = tcp_disconnect(tp);
552 	COMMON_END(PRU_DISCONNECT);
553 }
554 
555 /*
556  * Accept a connection.  Essentially all the work is
557  * done at higher levels; just return the address
558  * of the peer, storing through addr.
559  */
560 static int
561 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
562 {
563 	int error = 0;
564 	struct inpcb *inp;
565 	struct tcpcb *tp = NULL;
566 	TCPDEBUG0;
567 
568 	inp = so->so_pcb;
569 	if (so->so_state & SS_ISDISCONNECTED) {
570 		error = ECONNABORTED;
571 		goto out;
572 	}
573 	if (inp == 0)
574 		return (EINVAL);
575 
576 	tp = intotcpcb(inp);
577 	TCPDEBUG1();
578 	in_setpeeraddr(so, nam);
579 	COMMON_END(PRU_ACCEPT);
580 }
581 
582 #ifdef INET6
583 static int
584 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
585 {
586 	int error = 0;
587 	struct inpcb *inp;
588 	struct tcpcb *tp = NULL;
589 	TCPDEBUG0;
590 
591 	inp = so->so_pcb;
592 
593 	if (so->so_state & SS_ISDISCONNECTED) {
594 		error = ECONNABORTED;
595 		goto out;
596 	}
597 	if (inp == 0)
598 		return (EINVAL);
599 	tp = intotcpcb(inp);
600 	TCPDEBUG1();
601 	in6_mapped_peeraddr(so, nam);
602 	COMMON_END(PRU_ACCEPT);
603 }
604 #endif /* INET6 */
605 /*
606  * Mark the connection as being incapable of further output.
607  */
608 static int
609 tcp_usr_shutdown(struct socket *so)
610 {
611 	int error = 0;
612 	struct inpcb *inp;
613 	struct tcpcb *tp;
614 
615 	COMMON_START(so, inp, 0);
616 	socantsendmore(so);
617 	tp = tcp_usrclosed(tp);
618 	if (tp)
619 		error = tcp_output(tp);
620 	COMMON_END(PRU_SHUTDOWN);
621 }
622 
623 /*
624  * After a receive, possibly send window update to peer.
625  */
626 static int
627 tcp_usr_rcvd(struct socket *so, int flags)
628 {
629 	int error = 0;
630 	struct inpcb *inp;
631 	struct tcpcb *tp;
632 
633 	COMMON_START(so, inp, 0);
634 	tcp_output(tp);
635 	COMMON_END(PRU_RCVD);
636 }
637 
638 /*
639  * Do a send by putting data in output queue and updating urgent
640  * marker if URG set.  Possibly send more data.  Unlike the other
641  * pru_*() routines, the mbuf chains are our responsibility.  We
642  * must either enqueue them or free them.  The other pru_* routines
643  * generally are caller-frees.
644  */
645 static int
646 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
647 	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
648 {
649 	int error = 0;
650 	struct inpcb *inp;
651 	struct tcpcb *tp;
652 #ifdef INET6
653 	int isipv6;
654 #endif
655 	TCPDEBUG0;
656 
657 	inp = so->so_pcb;
658 
659 	if (inp == NULL) {
660 		/*
661 		 * OOPS! we lost a race, the TCP session got reset after
662 		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
663 		 * network interrupt in the non-critical section of sosend().
664 		 */
665 		m_freem(m);
666 		if (control)
667 			m_freem(control);
668 		error = ECONNRESET;	/* XXX EPIPE? */
669 		tp = NULL;
670 		TCPDEBUG1();
671 		goto out;
672 	}
673 #ifdef INET6
674 	isipv6 = nam && nam->sa_family == AF_INET6;
675 #endif /* INET6 */
676 	tp = intotcpcb(inp);
677 	TCPDEBUG1();
678 	if (control) {
679 		/* TCP doesn't do control messages (rights, creds, etc) */
680 		if (control->m_len) {
681 			m_freem(control);
682 			m_freem(m);
683 			error = EINVAL;
684 			goto out;
685 		}
686 		m_freem(control);	/* empty control, just free it */
687 	}
688 
689 	/*
690 	 * Don't let too much OOB data build up
691 	 */
692 	if (flags & PRUS_OOB) {
693 		if (ssb_space(&so->so_snd) < -512) {
694 			m_freem(m);
695 			error = ENOBUFS;
696 			goto out;
697 		}
698 	}
699 
700 	/*
701 	 * Do implied connect if not yet connected.  Any data sent
702 	 * with the connect is handled by tcp_connect() and friends.
703 	 *
704 	 * NOTE!  PROTOCOL THREAD MAY BE CHANGED BY THE CONNECT!
705 	 */
706 	if (nam && tp->t_state < TCPS_SYN_SENT) {
707 #ifdef INET6
708 		if (isipv6)
709 			error = tcp6_connect(tp, flags, m, nam, td);
710 		else
711 #endif /* INET6 */
712 		error = tcp_connect(tp, flags, m, nam, td);
713 #if 0
714 		/* WTF is this doing here? */
715 		tp->snd_wnd = TTCP_CLIENT_SND_WND;
716 		tcp_mss(tp, -1);
717 #endif
718 		goto out;
719 	}
720 
721 	/*
722 	 * Pump the data into the socket.
723 	 */
724 	if (m)
725 		ssb_appendstream(&so->so_snd, m);
726 	if (flags & PRUS_OOB) {
727 		/*
728 		 * According to RFC961 (Assigned Protocols),
729 		 * the urgent pointer points to the last octet
730 		 * of urgent data.  We continue, however,
731 		 * to consider it to indicate the first octet
732 		 * of data past the urgent section.
733 		 * Otherwise, snd_up should be one lower.
734 		 */
735 		tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
736 		tp->t_flags |= TF_FORCE;
737 		error = tcp_output(tp);
738 		tp->t_flags &= ~TF_FORCE;
739 	} else {
740 		if (flags & PRUS_EOF) {
741 			/*
742 			 * Close the send side of the connection after
743 			 * the data is sent.
744 			 */
745 			socantsendmore(so);
746 			tp = tcp_usrclosed(tp);
747 		}
748 		if (tp != NULL) {
749 			if (flags & PRUS_MORETOCOME)
750 				tp->t_flags |= TF_MORETOCOME;
751 			error = tcp_output(tp);
752 			if (flags & PRUS_MORETOCOME)
753 				tp->t_flags &= ~TF_MORETOCOME;
754 		}
755 	}
756 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
757 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
758 }
759 
760 /*
761  * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
762  *	 will sofree() it when we return.
763  */
764 static int
765 tcp_usr_abort(struct socket *so)
766 {
767 	int error = 0;
768 	struct inpcb *inp;
769 	struct tcpcb *tp;
770 
771 	COMMON_START(so, inp, 1);
772 	tp = tcp_drop(tp, ECONNABORTED);
773 	COMMON_END(PRU_ABORT);
774 }
775 
776 /*
777  * Receive out-of-band data.
778  */
779 static int
780 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
781 {
782 	int error = 0;
783 	struct inpcb *inp;
784 	struct tcpcb *tp;
785 
786 	COMMON_START(so, inp, 0);
787 	if ((so->so_oobmark == 0 &&
788 	     (so->so_state & SS_RCVATMARK) == 0) ||
789 	    so->so_options & SO_OOBINLINE ||
790 	    tp->t_oobflags & TCPOOB_HADDATA) {
791 		error = EINVAL;
792 		goto out;
793 	}
794 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
795 		error = EWOULDBLOCK;
796 		goto out;
797 	}
798 	m->m_len = 1;
799 	*mtod(m, caddr_t) = tp->t_iobc;
800 	if ((flags & MSG_PEEK) == 0)
801 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
802 	COMMON_END(PRU_RCVOOB);
803 }
804 
805 /* xxx - should be const */
806 struct pr_usrreqs tcp_usrreqs = {
807 	.pru_abort = tcp_usr_abort,
808 	.pru_accept = tcp_usr_accept,
809 	.pru_attach = tcp_usr_attach,
810 	.pru_bind = tcp_usr_bind,
811 	.pru_connect = tcp_usr_connect,
812 	.pru_connect2 = pru_connect2_notsupp,
813 	.pru_control = in_control,
814 	.pru_detach = tcp_usr_detach,
815 	.pru_disconnect = tcp_usr_disconnect,
816 	.pru_listen = tcp_usr_listen,
817 	.pru_peeraddr = in_setpeeraddr,
818 	.pru_rcvd = tcp_usr_rcvd,
819 	.pru_rcvoob = tcp_usr_rcvoob,
820 	.pru_send = tcp_usr_send,
821 	.pru_sense = pru_sense_null,
822 	.pru_shutdown = tcp_usr_shutdown,
823 	.pru_sockaddr = in_setsockaddr,
824 	.pru_sosend = sosend,
825 	.pru_soreceive = soreceive
826 };
827 
828 #ifdef INET6
829 struct pr_usrreqs tcp6_usrreqs = {
830 	.pru_abort = tcp_usr_abort,
831 	.pru_accept = tcp6_usr_accept,
832 	.pru_attach = tcp_usr_attach,
833 	.pru_bind = tcp6_usr_bind,
834 	.pru_connect = tcp6_usr_connect,
835 	.pru_connect2 = pru_connect2_notsupp,
836 	.pru_control = in6_control,
837 	.pru_detach = tcp_usr_detach,
838 	.pru_disconnect = tcp_usr_disconnect,
839 	.pru_listen = tcp6_usr_listen,
840 	.pru_peeraddr = in6_mapped_peeraddr,
841 	.pru_rcvd = tcp_usr_rcvd,
842 	.pru_rcvoob = tcp_usr_rcvoob,
843 	.pru_send = tcp_usr_send,
844 	.pru_sense = pru_sense_null,
845 	.pru_shutdown = tcp_usr_shutdown,
846 	.pru_sockaddr = in6_mapped_sockaddr,
847 	.pru_sosend = sosend,
848 	.pru_soreceive = soreceive
849 };
850 #endif /* INET6 */
851 
852 static int
853 tcp_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf *m,
854 		  struct sockaddr_in *sin, struct sockaddr_in *if_sin)
855 {
856 	struct inpcb *inp = tp->t_inpcb, *oinp;
857 	struct socket *so = inp->inp_socket;
858 	struct route *ro = &inp->inp_route;
859 
860 	oinp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
861 	    sin->sin_addr, sin->sin_port,
862 	    inp->inp_laddr.s_addr != INADDR_ANY ?
863 		inp->inp_laddr : if_sin->sin_addr,
864 	    inp->inp_lport, 0, NULL);
865 	if (oinp != NULL) {
866 		m_freem(m);
867 		return (EADDRINUSE);
868 	}
869 	if (inp->inp_laddr.s_addr == INADDR_ANY)
870 		inp->inp_laddr = if_sin->sin_addr;
871 	inp->inp_faddr = sin->sin_addr;
872 	inp->inp_fport = sin->sin_port;
873 	inp->inp_cpcbinfo = &tcbinfo[mycpu->gd_cpuid];
874 	in_pcbinsconnhash(inp);
875 
876 	/*
877 	 * We are now on the inpcb's owner CPU, if the cached route was
878 	 * freed because the rtentry's owner CPU is not the current CPU
879 	 * (e.g. in tcp_connect()), then we try to reallocate it here with
880 	 * the hope that a rtentry may be cloned from a RTF_PRCLONING
881 	 * rtentry.
882 	 */
883 	if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
884 	    ro->ro_rt == NULL) {
885 		bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
886 		ro->ro_dst.sa_family = AF_INET;
887 		ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
888 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr =
889 			sin->sin_addr;
890 		rtalloc(ro);
891 	}
892 
893 	/*
894 	 * Now that no more errors can occur, change the protocol processing
895 	 * port to the current thread (which is the correct thread).
896 	 *
897 	 * Create TCP timer message now; we are on the tcpcb's owner
898 	 * CPU/thread.
899 	 */
900 	tcp_create_timermsg(tp, &curthread->td_msgport);
901 
902 	/*
903 	 * Compute window scaling to request.  Use a larger scaling then
904 	 * needed for the initial receive buffer in case the receive buffer
905 	 * gets expanded.
906 	 */
907 	if (tp->request_r_scale < TCP_MIN_WINSHIFT)
908 		tp->request_r_scale = TCP_MIN_WINSHIFT;
909 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
910 	       (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat
911 	) {
912 		tp->request_r_scale++;
913 	}
914 
915 	soisconnecting(so);
916 	tcpstat.tcps_connattempt++;
917 	tp->t_state = TCPS_SYN_SENT;
918 	tcp_callout_reset(tp, tp->tt_keep, tcp_keepinit, tcp_timer_keep);
919 	tp->iss = tcp_new_isn(tp);
920 	tcp_sendseqinit(tp);
921 	if (m) {
922 		ssb_appendstream(&so->so_snd, m);
923 		m = NULL;
924 		if (flags & PRUS_OOB)
925 			tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
926 	}
927 
928 	/*
929 	 * Close the send side of the connection after
930 	 * the data is sent if flagged.
931 	 */
932 	if ((flags & (PRUS_OOB|PRUS_EOF)) == PRUS_EOF) {
933 		socantsendmore(so);
934 		tp = tcp_usrclosed(tp);
935 	}
936 	return (tcp_output(tp));
937 }
938 
939 #ifdef SMP
940 
941 struct netmsg_tcp_connect {
942 	struct netmsg		nm_netmsg;
943 	struct tcpcb		*nm_tp;
944 	struct sockaddr_in	*nm_sin;
945 	struct sockaddr_in	*nm_ifsin;
946 	int			nm_flags;
947 	struct mbuf		*nm_m;
948 };
949 
950 /*
951  * This is called in the target protocol processing thread.  We must
952  * re-link our pcb to the new tcpcb
953  */
954 static void
955 tcp_connect_handler(netmsg_t netmsg)
956 {
957 	struct netmsg_tcp_connect *msg = (void *)netmsg;
958 	struct socket *so = netmsg->nm_so;
959 	int error;
960 
961 	in_pcblink(so->so_pcb, &tcbinfo[mycpu->gd_cpuid]);
962 	error = tcp_connect_oncpu(msg->nm_tp, msg->nm_flags, msg->nm_m,
963 				  msg->nm_sin, msg->nm_ifsin);
964 	lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error);
965 }
966 
967 struct netmsg_tcp6_connect {
968 	struct netmsg		nm_netmsg;
969 	struct tcpcb		*nm_tp;
970 	struct sockaddr_in6	*nm_sin6;
971 	struct in6_addr		*nm_addr6;
972 	int			nm_flags;
973 	struct mbuf		*nm_m;
974 };
975 
976 #ifdef INET6
977 static void
978 tcp6_connect_handler(netmsg_t netmsg)
979 {
980 	struct netmsg_tcp6_connect *msg = (void *)netmsg;
981 	int error;
982 
983 	error = tcp6_connect_oncpu(msg->nm_tp, msg->nm_flags, msg->nm_m,
984 				   msg->nm_sin6, msg->nm_addr6);
985 	lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error);
986 }
987 #endif
988 
989 #endif /* SMP */
990 
991 /*
992  * Common subroutine to open a TCP connection to remote host specified
993  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
994  * port number if needed.  Call in_pcbladdr to do the routing and to choose
995  * a local host address (interface).
996  * Initialize connection parameters and enter SYN-SENT state.
997  */
998 static int
999 tcp_connect(struct tcpcb *tp, int flags, struct mbuf *m,
1000 	    struct sockaddr *nam, struct thread *td)
1001 {
1002 	struct inpcb *inp = tp->t_inpcb;
1003 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1004 	struct sockaddr_in *if_sin;
1005 	struct socket *so;
1006 	int error;
1007 #ifdef SMP
1008 	lwkt_port_t port;
1009 #endif
1010 
1011 	/*
1012 	 * Bind if we have to
1013 	 */
1014 	if (inp->inp_lport == 0) {
1015 		error = in_pcbbind(inp, NULL, td);
1016 		if (error) {
1017 			m_freem(m);
1018 			return (error);
1019 		}
1020 	}
1021 	so = inp->inp_socket;
1022 	KKASSERT(so);
1023 
1024 	/*
1025 	 * Calculate the correct protocol processing thread.  The connect
1026 	 * operation must run there.  Set the forwarding port before we
1027 	 * forward the message or it will get bounced right back to us.
1028 	 */
1029 	error = in_pcbladdr(inp, nam, &if_sin, td);
1030 	if (error) {
1031 		m_freem(m);
1032 		return (error);
1033 	}
1034 
1035 #ifdef SMP
1036 	port = tcp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1037 	    inp->inp_laddr.s_addr ?
1038 		inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr,
1039 	    inp->inp_lport);
1040 
1041 	if (port != &curthread->td_msgport) {
1042 		struct netmsg_tcp_connect msg;
1043 		struct route *ro = &inp->inp_route;
1044 
1045 		/*
1046 		 * in_pcbladdr() may have allocated a route entry for us
1047 		 * on the current CPU, but we need a route entry on the
1048 		 * inpcb's owner CPU, so free it here.
1049 		 */
1050 		if (ro->ro_rt != NULL)
1051 			RTFREE(ro->ro_rt);
1052 		bzero(ro, sizeof(*ro));
1053 
1054 		/*
1055 		 * We are moving the protocol processing port the socket
1056 		 * is on, we have to unlink here and re-link on the
1057 		 * target cpu.
1058 		 */
1059 		in_pcbunlink(so->so_pcb, &tcbinfo[mycpu->gd_cpuid]);
1060 		sosetport(so, port);
1061 
1062 		netmsg_init(&msg.nm_netmsg, so, &curthread->td_msgport,
1063 			    0, tcp_connect_handler);
1064 		msg.nm_tp = tp;
1065 		msg.nm_sin = sin;
1066 		msg.nm_ifsin = if_sin;
1067 		msg.nm_flags = flags;
1068 		msg.nm_m = m;
1069 		error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0);
1070 	} else {
1071 		error = tcp_connect_oncpu(tp, flags, m, sin, if_sin);
1072 	}
1073 #else
1074 	KKASSERT(so->so_port == &curthread->td_msgport);
1075 	error = tcp_connect_oncpu(tp, flags, m, sin, if_sin);
1076 #endif
1077 	return (error);
1078 }
1079 
1080 #ifdef INET6
1081 
1082 static int
1083 tcp6_connect(struct tcpcb *tp, int flags, struct mbuf *m,
1084 	     struct sockaddr *nam, struct thread *td)
1085 {
1086 	struct inpcb *inp = tp->t_inpcb;
1087 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1088 	struct in6_addr *addr6;
1089 #ifdef SMP
1090 	lwkt_port_t port;
1091 #endif
1092 	int error;
1093 
1094 	if (inp->inp_lport == 0) {
1095 		error = in6_pcbbind(inp, NULL, td);
1096 		if (error) {
1097 			m_freem(m);
1098 			return (error);
1099 		}
1100 	}
1101 
1102 	/*
1103 	 * Cannot simply call in_pcbconnect, because there might be an
1104 	 * earlier incarnation of this same connection still in
1105 	 * TIME_WAIT state, creating an ADDRINUSE error.
1106 	 */
1107 	error = in6_pcbladdr(inp, nam, &addr6, td);
1108 	if (error) {
1109 		m_freem(m);
1110 		return (error);
1111 	}
1112 
1113 #ifdef SMP
1114 	port = tcp6_addrport();	/* XXX hack for now, always cpu0 */
1115 
1116 	if (port != &curthread->td_msgport) {
1117 		struct netmsg_tcp6_connect msg;
1118 		struct route *ro = &inp->inp_route;
1119 
1120 		/*
1121 		 * in_pcbladdr() may have allocated a route entry for us
1122 		 * on the current CPU, but we need a route entry on the
1123 		 * inpcb's owner CPU, so free it here.
1124 		 */
1125 		if (ro->ro_rt != NULL)
1126 			RTFREE(ro->ro_rt);
1127 		bzero(ro, sizeof(*ro));
1128 
1129 		netmsg_init(&msg.nm_netmsg, NULL, &curthread->td_msgport,
1130 			    0, tcp6_connect_handler);
1131 		msg.nm_tp = tp;
1132 		msg.nm_sin6 = sin6;
1133 		msg.nm_addr6 = addr6;
1134 		msg.nm_flags = flags;
1135 		msg.nm_m = m;
1136 		error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0);
1137 	} else {
1138 		error = tcp6_connect_oncpu(tp, flags, m, sin6, addr6);
1139 	}
1140 #else
1141 	error = tcp6_connect_oncpu(tp, flags, m, sin6, addr6);
1142 #endif
1143 	return (error);
1144 }
1145 
1146 static int
1147 tcp6_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf *m,
1148 		   struct sockaddr_in6 *sin6, struct in6_addr *addr6)
1149 {
1150 	struct inpcb *inp = tp->t_inpcb;
1151 	struct socket *so = inp->inp_socket;
1152 	struct inpcb *oinp;
1153 
1154 	/*
1155 	 * Cannot simply call in_pcbconnect, because there might be an
1156 	 * earlier incarnation of this same connection still in
1157 	 * TIME_WAIT state, creating an ADDRINUSE error.
1158 	 */
1159 	oinp = in6_pcblookup_hash(inp->inp_cpcbinfo,
1160 				  &sin6->sin6_addr, sin6->sin6_port,
1161 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1162 				      addr6 : &inp->in6p_laddr,
1163 				  inp->inp_lport,  0, NULL);
1164 	if (oinp) {
1165 		m_freem(m);
1166 		return (EADDRINUSE);
1167 	}
1168 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1169 		inp->in6p_laddr = *addr6;
1170 	inp->in6p_faddr = sin6->sin6_addr;
1171 	inp->inp_fport = sin6->sin6_port;
1172 	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
1173 		inp->in6p_flowinfo = sin6->sin6_flowinfo;
1174 	in_pcbinsconnhash(inp);
1175 
1176 	/*
1177 	 * Now that no more errors can occur, change the protocol processing
1178 	 * port to the current thread (which is the correct thread).
1179 	 *
1180 	 * Create TCP timer message now; we are on the tcpcb's owner
1181 	 * CPU/thread.
1182 	 */
1183 	tcp_create_timermsg(tp, &curthread->td_msgport);
1184 
1185 	/* Compute window scaling to request.  */
1186 	if (tp->request_r_scale < TCP_MIN_WINSHIFT)
1187 		tp->request_r_scale = TCP_MIN_WINSHIFT;
1188 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1189 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat) {
1190 		tp->request_r_scale++;
1191 	}
1192 
1193 	soisconnecting(so);
1194 	tcpstat.tcps_connattempt++;
1195 	tp->t_state = TCPS_SYN_SENT;
1196 	tcp_callout_reset(tp, tp->tt_keep, tcp_keepinit, tcp_timer_keep);
1197 	tp->iss = tcp_new_isn(tp);
1198 	tcp_sendseqinit(tp);
1199 	if (m) {
1200 		ssb_appendstream(&so->so_snd, m);
1201 		m = NULL;
1202 		if (flags & PRUS_OOB)
1203 			tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
1204 	}
1205 
1206 	/*
1207 	 * Close the send side of the connection after
1208 	 * the data is sent if flagged.
1209 	 */
1210 	if ((flags & (PRUS_OOB|PRUS_EOF)) == PRUS_EOF) {
1211 		socantsendmore(so);
1212 		tp = tcp_usrclosed(tp);
1213 	}
1214 	return (tcp_output(tp));
1215 }
1216 
1217 #endif /* INET6 */
1218 
1219 /*
1220  * The new sockopt interface makes it possible for us to block in the
1221  * copyin/out step (if we take a page fault).  Taking a page fault while
1222  * in a critical section is probably a Bad Thing.  (Since sockets and pcbs
1223  * both now use TSM, there probably isn't any need for this function to
1224  * run in a critical section any more.  This needs more examination.)
1225  */
1226 int
1227 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1228 {
1229 	int	error, opt, optval;
1230 	struct	inpcb *inp;
1231 	struct	tcpcb *tp;
1232 
1233 	error = 0;
1234 	inp = so->so_pcb;
1235 	if (inp == NULL)
1236 		return (ECONNRESET);
1237 
1238 	if (sopt->sopt_level != IPPROTO_TCP) {
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 	tp = intotcpcb(inp);
1248 
1249 	switch (sopt->sopt_dir) {
1250 	case SOPT_SET:
1251 		error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1252 				      sizeof optval);
1253 		if (error)
1254 			break;
1255 		switch (sopt->sopt_name) {
1256 #ifdef TCP_SIGNATURE
1257 		case TCP_SIGNATURE_ENABLE:
1258 			if (optval > 0)
1259 				tp->t_flags |= TF_SIGNATURE;
1260 			else
1261 				tp->t_flags &= ~TF_SIGNATURE;
1262 			break;
1263 #endif /* TCP_SIGNATURE */
1264 		case TCP_NODELAY:
1265 		case TCP_NOOPT:
1266 			switch (sopt->sopt_name) {
1267 			case TCP_NODELAY:
1268 				opt = TF_NODELAY;
1269 				break;
1270 			case TCP_NOOPT:
1271 				opt = TF_NOOPT;
1272 				break;
1273 			default:
1274 				opt = 0; /* dead code to fool gcc */
1275 				break;
1276 			}
1277 
1278 			if (optval)
1279 				tp->t_flags |= opt;
1280 			else
1281 				tp->t_flags &= ~opt;
1282 			break;
1283 
1284 		case TCP_NOPUSH:
1285 			if (optval)
1286 				tp->t_flags |= TF_NOPUSH;
1287 			else {
1288 				tp->t_flags &= ~TF_NOPUSH;
1289 				error = tcp_output(tp);
1290 			}
1291 			break;
1292 
1293 		case TCP_MAXSEG:
1294 			/*
1295 			 * Must be between 0 and maxseg.  If the requested
1296 			 * maxseg is too small to satisfy the desired minmss,
1297 			 * pump it up (silently so sysctl modifications of
1298 			 * minmss do not create unexpected program failures).
1299 			 * Handle degenerate cases.
1300 			 */
1301 			if (optval > 0 && optval <= tp->t_maxseg) {
1302 				if (optval + 40 < tcp_minmss) {
1303 					optval = tcp_minmss - 40;
1304 					if (optval < 0)
1305 						optval = 1;
1306 				}
1307 				tp->t_maxseg = optval;
1308 			} else {
1309 				error = EINVAL;
1310 			}
1311 			break;
1312 
1313 		default:
1314 			error = ENOPROTOOPT;
1315 			break;
1316 		}
1317 		break;
1318 
1319 	case SOPT_GET:
1320 		switch (sopt->sopt_name) {
1321 #ifdef TCP_SIGNATURE
1322 		case TCP_SIGNATURE_ENABLE:
1323 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1324 			break;
1325 #endif /* TCP_SIGNATURE */
1326 		case TCP_NODELAY:
1327 			optval = tp->t_flags & TF_NODELAY;
1328 			break;
1329 		case TCP_MAXSEG:
1330 			optval = tp->t_maxseg;
1331 			break;
1332 		case TCP_NOOPT:
1333 			optval = tp->t_flags & TF_NOOPT;
1334 			break;
1335 		case TCP_NOPUSH:
1336 			optval = tp->t_flags & TF_NOPUSH;
1337 			break;
1338 		default:
1339 			error = ENOPROTOOPT;
1340 			break;
1341 		}
1342 		if (error == 0)
1343 			soopt_from_kbuf(sopt, &optval, sizeof optval);
1344 		break;
1345 	}
1346 	return (error);
1347 }
1348 
1349 /*
1350  * tcp_sendspace and tcp_recvspace are the default send and receive window
1351  * sizes, respectively.  These are obsolescent (this information should
1352  * be set by the route).
1353  *
1354  * Use a default that does not require tcp window scaling to be turned
1355  * on.  Individual programs or the administrator can increase the default.
1356  */
1357 u_long	tcp_sendspace = 57344;	/* largest multiple of PAGE_SIZE < 64k */
1358 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1359     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1360 u_long	tcp_recvspace = 57344;	/* largest multiple of PAGE_SIZE < 64k */
1361 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1362     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1363 
1364 /*
1365  * Attach TCP protocol to socket, allocating internet protocol control
1366  * block, tcp control block, bufer space, and entering LISTEN state
1367  * if to accept connections.
1368  */
1369 static int
1370 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1371 {
1372 	struct tcpcb *tp;
1373 	struct inpcb *inp;
1374 	int error;
1375 	int cpu;
1376 #ifdef INET6
1377 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1378 #endif
1379 
1380 	if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
1381 		lwkt_gettoken(&so->so_rcv.ssb_token);
1382 		error = soreserve(so, tcp_sendspace, tcp_recvspace,
1383 				  ai->sb_rlimit);
1384 		lwkt_reltoken(&so->so_rcv.ssb_token);
1385 		if (error)
1386 			return (error);
1387 	}
1388 	atomic_set_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE);
1389 	atomic_set_int(&so->so_snd.ssb_flags, SSB_AUTOSIZE);
1390 	cpu = mycpu->gd_cpuid;
1391 
1392 	/*
1393 	 * Set the default port for protocol processing. This will likely
1394 	 * change when we connect.
1395 	 */
1396 	error = in_pcballoc(so, &tcbinfo[cpu]);
1397 	if (error)
1398 		return (error);
1399 	inp = so->so_pcb;
1400 #ifdef INET6
1401 	if (isipv6) {
1402 		inp->inp_vflag |= INP_IPV6;
1403 		inp->in6p_hops = -1;	/* use kernel default */
1404 	}
1405 	else
1406 #endif
1407 	inp->inp_vflag |= INP_IPV4;
1408 	tp = tcp_newtcpcb(inp);
1409 	if (tp == NULL) {
1410 		/*
1411 		 * Make sure the socket is destroyed by the pcbdetach.
1412 		 */
1413 		soreference(so);
1414 #ifdef INET6
1415 		if (isipv6)
1416 			in6_pcbdetach(inp);
1417 		else
1418 #endif
1419 		in_pcbdetach(inp);
1420 		sofree(so);	/* from ref above */
1421 		return (ENOBUFS);
1422 	}
1423 	tp->t_state = TCPS_CLOSED;
1424 	return (0);
1425 }
1426 
1427 /*
1428  * Initiate (or continue) disconnect.
1429  * If embryonic state, just send reset (once).
1430  * If in ``let data drain'' option and linger null, just drop.
1431  * Otherwise (hard), mark socket disconnecting and drop
1432  * current input data; switch states based on user close, and
1433  * send segment to peer (with FIN).
1434  */
1435 static struct tcpcb *
1436 tcp_disconnect(struct tcpcb *tp)
1437 {
1438 	struct socket *so = tp->t_inpcb->inp_socket;
1439 
1440 	if (tp->t_state < TCPS_ESTABLISHED) {
1441 		tp = tcp_close(tp);
1442 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1443 		tp = tcp_drop(tp, 0);
1444 	} else {
1445 		lwkt_gettoken(&so->so_rcv.ssb_token);
1446 		soisdisconnecting(so);
1447 		sbflush(&so->so_rcv.sb);
1448 		tp = tcp_usrclosed(tp);
1449 		if (tp)
1450 			tcp_output(tp);
1451 		lwkt_reltoken(&so->so_rcv.ssb_token);
1452 	}
1453 	return (tp);
1454 }
1455 
1456 /*
1457  * User issued close, and wish to trail through shutdown states:
1458  * if never received SYN, just forget it.  If got a SYN from peer,
1459  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1460  * If already got a FIN from peer, then almost done; go to LAST_ACK
1461  * state.  In all other cases, have already sent FIN to peer (e.g.
1462  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1463  * for peer to send FIN or not respond to keep-alives, etc.
1464  * We can let the user exit from the close as soon as the FIN is acked.
1465  */
1466 static struct tcpcb *
1467 tcp_usrclosed(struct tcpcb *tp)
1468 {
1469 
1470 	switch (tp->t_state) {
1471 
1472 	case TCPS_CLOSED:
1473 	case TCPS_LISTEN:
1474 		tp->t_state = TCPS_CLOSED;
1475 		tp = tcp_close(tp);
1476 		break;
1477 
1478 	case TCPS_SYN_SENT:
1479 	case TCPS_SYN_RECEIVED:
1480 		tp->t_flags |= TF_NEEDFIN;
1481 		break;
1482 
1483 	case TCPS_ESTABLISHED:
1484 		tp->t_state = TCPS_FIN_WAIT_1;
1485 		break;
1486 
1487 	case TCPS_CLOSE_WAIT:
1488 		tp->t_state = TCPS_LAST_ACK;
1489 		break;
1490 	}
1491 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1492 		soisdisconnected(tp->t_inpcb->inp_socket);
1493 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1494 		if (tp->t_state == TCPS_FIN_WAIT_2) {
1495 			tcp_callout_reset(tp, tp->tt_2msl, tcp_maxidle,
1496 			    tcp_timer_2msl);
1497 		}
1498 	}
1499 	return (tp);
1500 }
1501