kernel/extp{read,write}v: Change ioccnt from u_int to int.
[dragonfly.git] / sys / netinet / tcp_usrreq.c
blobb0e3327cc6a27809fecd74330624288846151b9c
1 /*
2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
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.
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.
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
62 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
63 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.17 2002/10/11 11:46:44 ume Exp $
66 #include "opt_ipsec.h"
67 #include "opt_inet.h"
68 #include "opt_inet6.h"
69 #include "opt_tcpdebug.h"
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/malloc.h>
75 #include <sys/sysctl.h>
76 #include <sys/globaldata.h>
77 #include <sys/thread.h>
79 #include <sys/mbuf.h>
80 #ifdef INET6
81 #include <sys/domain.h>
82 #endif /* INET6 */
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/socketops.h>
86 #include <sys/protosw.h>
88 #include <sys/thread2.h>
89 #include <sys/msgport2.h>
90 #include <sys/socketvar2.h>
92 #include <net/if.h>
93 #include <net/netisr.h>
94 #include <net/route.h>
96 #include <net/netmsg2.h>
97 #include <net/netisr2.h>
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #ifdef INET6
102 #include <netinet/ip6.h>
103 #endif
104 #include <netinet/in_pcb.h>
105 #ifdef INET6
106 #include <netinet6/in6_pcb.h>
107 #endif
108 #include <netinet/in_var.h>
109 #include <netinet/ip_var.h>
110 #ifdef INET6
111 #include <netinet6/ip6_var.h>
112 #include <netinet6/tcp6_var.h>
113 #endif
114 #include <netinet/tcp.h>
115 #include <netinet/tcp_fsm.h>
116 #include <netinet/tcp_seq.h>
117 #include <netinet/tcp_timer.h>
118 #include <netinet/tcp_timer2.h>
119 #include <netinet/tcp_var.h>
120 #include <netinet/tcpip.h>
121 #ifdef TCPDEBUG
122 #include <netinet/tcp_debug.h>
123 #endif
125 #ifdef IPSEC
126 #include <netinet6/ipsec.h>
127 #endif /*IPSEC*/
130 * TCP protocol interface to socket abstraction.
132 extern char *tcpstates[]; /* XXX ??? */
134 static int tcp_attach (struct socket *, struct pru_attach_info *);
135 static void tcp_connect (netmsg_t msg);
136 #ifdef INET6
137 static void tcp6_connect (netmsg_t msg);
138 static int tcp6_connect_oncpu(struct tcpcb *tp, int flags,
139 struct mbuf **mp,
140 struct sockaddr_in6 *sin6,
141 struct in6_addr *addr6);
142 #endif /* INET6 */
143 static struct tcpcb *
144 tcp_disconnect (struct tcpcb *);
145 static struct tcpcb *
146 tcp_usrclosed (struct tcpcb *);
148 #ifdef TCPDEBUG
149 #define TCPDEBUG0 int ostate = 0
150 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
151 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
152 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
153 #else
154 #define TCPDEBUG0
155 #define TCPDEBUG1()
156 #define TCPDEBUG2(req)
157 #endif
160 * For some ill optimized programs, which try to use TCP_NOPUSH
161 * to improve performance, will have small amount of data sits
162 * in the sending buffer. These small amount of data will _not_
163 * be pushed into the network until more data are written into
164 * the socket or the socket write side is shutdown.
166 static int tcp_disable_nopush = 1;
167 SYSCTL_INT(_net_inet_tcp, OID_AUTO, disable_nopush, CTLFLAG_RW,
168 &tcp_disable_nopush, 0, "TCP_NOPUSH socket option will have no effect");
171 * Allocate socket buffer space.
173 static int
174 tcp_usr_preattach(struct socket *so, int proto __unused,
175 struct pru_attach_info *ai)
177 int error;
179 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
180 error = soreserve(so, tcp_sendspace, tcp_recvspace,
181 ai->sb_rlimit);
182 if (error)
183 return (error);
185 atomic_set_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE);
186 atomic_set_int(&so->so_snd.ssb_flags, SSB_AUTOSIZE | SSB_PREALLOC);
188 return 0;
192 * TCP attaches to socket via pru_attach(), reserving space,
193 * and an internet control block. This socket may move to
194 * other CPU later when we bind/connect.
196 static void
197 tcp_usr_attach(netmsg_t msg)
199 struct socket *so = msg->base.nm_so;
200 struct pru_attach_info *ai = msg->attach.nm_ai;
201 int error;
202 struct inpcb *inp;
203 struct tcpcb *tp = NULL;
204 TCPDEBUG0;
206 inp = so->so_pcb;
207 KASSERT(inp == NULL, ("tcp socket attached"));
208 TCPDEBUG1();
210 error = tcp_attach(so, ai);
211 if (error)
212 goto out;
214 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
215 so->so_linger = TCP_LINGERTIME;
216 tp = sototcpcb(so);
217 out:
218 TCPDEBUG2(PRU_ATTACH);
219 lwkt_replymsg(&msg->lmsg, error);
223 * pru_detach() detaches the TCP protocol from the socket.
224 * If the protocol state is non-embryonic, then can't
225 * do this directly: have to initiate a pru_disconnect(),
226 * which may finish later; embryonic TCB's can just
227 * be discarded here.
229 static void
230 tcp_usr_detach(netmsg_t msg)
232 struct socket *so = msg->base.nm_so;
233 int error = 0;
234 struct inpcb *inp;
235 struct tcpcb *tp;
236 TCPDEBUG0;
238 inp = so->so_pcb;
241 * If the inp is already detached or never attached, it may have
242 * been due to an async close or async attach failure. Just return
243 * as if no error occured.
245 if (inp) {
246 tp = intotcpcb(inp);
247 KASSERT(tp != NULL, ("tcp_usr_detach: tp is NULL"));
248 TCPDEBUG1();
249 tp = tcp_disconnect(tp);
250 TCPDEBUG2(PRU_DETACH);
252 lwkt_replymsg(&msg->lmsg, error);
256 * NOTE: ignore_error is non-zero for certain disconnection races
257 * which we want to silently allow, otherwise close() may return
258 * an unexpected error.
260 * NOTE: The variables (msg) and (tp) are assumed.
262 #define COMMON_START(so, inp, ignore_error) \
263 TCPDEBUG0; \
265 inp = so->so_pcb; \
266 do { \
267 if (inp == NULL) { \
268 error = ignore_error ? 0 : EINVAL; \
269 tp = NULL; \
270 goto out; \
272 tp = intotcpcb(inp); \
273 TCPDEBUG1(); \
274 } while(0)
276 #define COMMON_END1(req, noreply) \
277 out: do { \
278 TCPDEBUG2(req); \
279 if (!(noreply)) \
280 lwkt_replymsg(&msg->lmsg, error); \
281 return; \
282 } while(0)
284 #define COMMON_END(req) COMMON_END1((req), 0)
286 static void
287 tcp_sosetport(struct lwkt_msg *msg, lwkt_port_t port)
289 sosetport(((struct netmsg_base *)msg)->nm_so, port);
293 * Give the socket an address.
295 static void
296 tcp_usr_bind(netmsg_t msg)
298 struct socket *so = msg->bind.base.nm_so;
299 struct sockaddr *nam = msg->bind.nm_nam;
300 struct thread *td = msg->bind.nm_td;
301 int error = 0;
302 struct inpcb *inp;
303 struct tcpcb *tp;
304 struct sockaddr_in *sinp;
305 lwkt_port_t port0 = netisr_cpuport(0);
307 COMMON_START(so, inp, 0);
310 * Must check for multicast addresses and disallow binding
311 * to them.
313 sinp = (struct sockaddr_in *)nam;
314 if (sinp->sin_family == AF_INET &&
315 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
316 error = EAFNOSUPPORT;
317 goto out;
321 * Check "already bound" here (in_pcbbind() does the same check
322 * though), so we don't forward a connected socket to netisr0,
323 * which would panic in the following in_pcbunlink().
325 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) {
326 error = EINVAL; /* already bound */
327 goto out;
331 * Use netisr0 to serialize in_pcbbind(), so that pru_detach and
332 * pru_bind for different sockets on the same local port could be
333 * properly ordered. The original race is illustrated here for
334 * reference.
336 * s1 = socket();
337 * bind(s1, *.PORT);
338 * close(s1); <----- asynchronous
339 * s2 = socket();
340 * bind(s2, *.PORT);
342 * All will expect bind(s2, *.PORT) to succeed. However, it will
343 * fail, if following sequence happens due to random socket initial
344 * msgport and asynchronous close(2):
346 * netisrN netisrM
347 * : :
348 * : pru_bind(s2) [*.PORT is used by s1]
349 * pru_detach(s1) :
351 if (&curthread->td_msgport != port0) {
352 lwkt_msg_t lmsg = &msg->bind.base.lmsg;
354 KASSERT((msg->bind.nm_flags & PRUB_RELINK) == 0,
355 ("already asked to relink"));
357 in_pcbunlink(so->so_pcb, &tcbinfo[mycpuid]);
358 msg->bind.nm_flags |= PRUB_RELINK;
360 TCP_STATE_MIGRATE_START(tp);
362 /* See the related comment in tcp_connect() */
363 lwkt_setmsg_receipt(lmsg, tcp_sosetport);
364 lwkt_forwardmsg(port0, lmsg);
365 /* msg invalid now */
366 return;
368 KASSERT(so->so_port == port0, ("so_port is not netisr0"));
370 if (msg->bind.nm_flags & PRUB_RELINK) {
371 msg->bind.nm_flags &= ~PRUB_RELINK;
372 TCP_STATE_MIGRATE_END(tp);
373 in_pcblink(so->so_pcb, &tcbinfo[mycpuid]);
375 KASSERT(inp->inp_pcbinfo == &tcbinfo[0], ("pcbinfo is not tcbinfo0"));
377 error = in_pcbbind(inp, nam, td);
378 if (error)
379 goto out;
381 COMMON_END(PRU_BIND);
384 #ifdef INET6
386 static void
387 tcp6_usr_bind(netmsg_t msg)
389 struct socket *so = msg->bind.base.nm_so;
390 struct sockaddr *nam = msg->bind.nm_nam;
391 struct thread *td = msg->bind.nm_td;
392 int error = 0;
393 struct inpcb *inp;
394 struct tcpcb *tp;
395 struct sockaddr_in6 *sin6p;
397 COMMON_START(so, inp, 0);
400 * Must check for multicast addresses and disallow binding
401 * to them.
403 sin6p = (struct sockaddr_in6 *)nam;
404 if (sin6p->sin6_family == AF_INET6 &&
405 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
406 error = EAFNOSUPPORT;
407 goto out;
409 error = in6_pcbbind(inp, nam, td);
410 if (error)
411 goto out;
412 COMMON_END(PRU_BIND);
414 #endif /* INET6 */
416 struct netmsg_inswildcard {
417 struct netmsg_base base;
418 struct inpcb *nm_inp;
421 static void
422 in_pcbinswildcardhash_handler(netmsg_t msg)
424 struct netmsg_inswildcard *nm = (struct netmsg_inswildcard *)msg;
425 int cpu = mycpuid, nextcpu;
427 in_pcbinswildcardhash_oncpu(nm->nm_inp, &tcbinfo[cpu]);
429 nextcpu = cpu + 1;
430 if (nextcpu < netisr_ncpus)
431 lwkt_forwardmsg(netisr_cpuport(nextcpu), &nm->base.lmsg);
432 else
433 lwkt_replymsg(&nm->base.lmsg, 0);
437 * Prepare to accept connections.
439 static void
440 tcp_usr_listen(netmsg_t msg)
442 struct socket *so = msg->listen.base.nm_so;
443 struct thread *td = msg->listen.nm_td;
444 int error = 0;
445 struct inpcb *inp;
446 struct tcpcb *tp;
447 struct netmsg_inswildcard nm;
448 lwkt_port_t port0 = netisr_cpuport(0);
450 COMMON_START(so, inp, 0);
452 if (&curthread->td_msgport != port0) {
453 lwkt_msg_t lmsg = &msg->listen.base.lmsg;
455 KASSERT((msg->listen.nm_flags & PRUL_RELINK) == 0,
456 ("already asked to relink"));
458 in_pcbunlink(so->so_pcb, &tcbinfo[mycpuid]);
459 msg->listen.nm_flags |= PRUL_RELINK;
461 TCP_STATE_MIGRATE_START(tp);
463 /* See the related comment in tcp_connect() */
464 lwkt_setmsg_receipt(lmsg, tcp_sosetport);
465 lwkt_forwardmsg(port0, lmsg);
466 /* msg invalid now */
467 return;
469 KASSERT(so->so_port == port0, ("so_port is not netisr0"));
471 if (msg->listen.nm_flags & PRUL_RELINK) {
472 msg->listen.nm_flags &= ~PRUL_RELINK;
473 TCP_STATE_MIGRATE_END(tp);
474 in_pcblink(so->so_pcb, &tcbinfo[mycpuid]);
476 KASSERT(inp->inp_pcbinfo == &tcbinfo[0], ("pcbinfo is not tcbinfo0"));
478 if (tp->t_flags & TF_LISTEN)
479 goto out;
481 if (inp->inp_lport == 0) {
482 error = in_pcbbind(inp, NULL, td);
483 if (error)
484 goto out;
487 TCP_STATE_CHANGE(tp, TCPS_LISTEN);
488 tp->t_flags |= TF_LISTEN;
489 tp->tt_msg = NULL; /* Catch any invalid timer usage */
492 * Create tcpcb per-cpu port cache
494 * NOTE:
495 * This _must_ be done before installing this inpcb into
496 * wildcard hash.
498 tcp_pcbport_create(tp);
500 if (netisr_ncpus > 1) {
502 * Put this inpcb into wildcard hash on other cpus.
504 ASSERT_INP_NOTINHASH(inp);
505 netmsg_init(&nm.base, NULL, &curthread->td_msgport,
506 MSGF_PRIORITY, in_pcbinswildcardhash_handler);
507 nm.nm_inp = inp;
508 lwkt_domsg(netisr_cpuport(1), &nm.base.lmsg, 0);
510 in_pcbinswildcardhash(inp);
511 COMMON_END(PRU_LISTEN);
514 #ifdef INET6
516 static void
517 tcp6_usr_listen(netmsg_t msg)
519 struct socket *so = msg->listen.base.nm_so;
520 struct thread *td = msg->listen.nm_td;
521 int error = 0;
522 struct inpcb *inp;
523 struct tcpcb *tp;
524 struct netmsg_inswildcard nm;
526 COMMON_START(so, inp, 0);
528 if (tp->t_flags & TF_LISTEN)
529 goto out;
531 if (inp->inp_lport == 0) {
532 error = in6_pcbbind(inp, NULL, td);
533 if (error)
534 goto out;
537 TCP_STATE_CHANGE(tp, TCPS_LISTEN);
538 tp->t_flags |= TF_LISTEN;
539 tp->tt_msg = NULL; /* Catch any invalid timer usage */
542 * Create tcpcb per-cpu port cache
544 * NOTE:
545 * This _must_ be done before installing this inpcb into
546 * wildcard hash.
548 tcp_pcbport_create(tp);
550 if (netisr_ncpus > 1) {
552 * Put this inpcb into wildcard hash on other cpus.
554 KKASSERT(so->so_port == netisr_cpuport(0));
555 ASSERT_NETISR0;
556 KKASSERT(inp->inp_pcbinfo == &tcbinfo[0]);
557 ASSERT_INP_NOTINHASH(inp);
559 netmsg_init(&nm.base, NULL, &curthread->td_msgport,
560 MSGF_PRIORITY, in_pcbinswildcardhash_handler);
561 nm.nm_inp = inp;
562 lwkt_domsg(netisr_cpuport(1), &nm.base.lmsg, 0);
564 in_pcbinswildcardhash(inp);
565 COMMON_END(PRU_LISTEN);
567 #endif /* INET6 */
570 * Initiate connection to peer.
571 * Create a template for use in transmissions on this connection.
572 * Enter SYN_SENT state, and mark socket as connecting.
573 * Start keep-alive timer, and seed output sequence space.
574 * Send initial segment on connection.
576 static void
577 tcp_usr_connect(netmsg_t msg)
579 struct socket *so = msg->connect.base.nm_so;
580 struct sockaddr *nam = msg->connect.nm_nam;
581 struct thread *td = msg->connect.nm_td;
582 int error = 0;
583 struct inpcb *inp;
584 struct tcpcb *tp;
585 struct sockaddr_in *sinp;
587 ASSERT_NETISR_NCPUS(mycpuid);
589 COMMON_START(so, inp, 0);
592 * Must disallow TCP ``connections'' to multicast addresses.
594 sinp = (struct sockaddr_in *)nam;
595 if (sinp->sin_family == AF_INET
596 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
597 error = EAFNOSUPPORT;
598 goto out;
601 if (!prison_remote_ip(td, (struct sockaddr*)sinp)) {
602 error = EAFNOSUPPORT; /* IPv6 only jail */
603 goto out;
606 tcp_connect(msg);
607 /* msg is invalid now */
608 return;
609 out:
610 if (msg->connect.nm_m) {
611 m_freem(msg->connect.nm_m);
612 msg->connect.nm_m = NULL;
614 if (msg->connect.nm_flags & PRUC_HELDTD)
615 lwkt_rele(td);
616 if (error && (msg->connect.nm_flags & PRUC_ASYNC)) {
617 so->so_error = error;
618 soisdisconnected(so);
620 lwkt_replymsg(&msg->lmsg, error);
623 #ifdef INET6
625 static void
626 tcp6_usr_connect(netmsg_t msg)
628 struct socket *so = msg->connect.base.nm_so;
629 struct sockaddr *nam = msg->connect.nm_nam;
630 struct thread *td = msg->connect.nm_td;
631 int error = 0;
632 struct inpcb *inp;
633 struct tcpcb *tp;
634 struct sockaddr_in6 *sin6p;
636 ASSERT_NETISR_NCPUS(mycpuid);
638 COMMON_START(so, inp, 0);
641 * Must disallow TCP ``connections'' to multicast addresses.
643 sin6p = (struct sockaddr_in6 *)nam;
644 if (sin6p->sin6_family == AF_INET6
645 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
646 error = EAFNOSUPPORT;
647 goto out;
650 if (!prison_remote_ip(td, nam)) {
651 error = EAFNOSUPPORT; /* IPv4 only jail */
652 goto out;
655 /* Reject v4-mapped address */
656 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
657 error = EADDRNOTAVAIL;
658 goto out;
661 inp->inp_inc.inc_isipv6 = 1;
662 tcp6_connect(msg);
663 /* msg is invalid now */
664 return;
665 out:
666 if (msg->connect.nm_m) {
667 m_freem(msg->connect.nm_m);
668 msg->connect.nm_m = NULL;
670 lwkt_replymsg(&msg->lmsg, error);
673 #endif /* INET6 */
676 * Initiate disconnect from peer.
677 * If connection never passed embryonic stage, just drop;
678 * else if don't need to let data drain, then can just drop anyways,
679 * else have to begin TCP shutdown process: mark socket disconnecting,
680 * drain unread data, state switch to reflect user close, and
681 * send segment (e.g. FIN) to peer. Socket will be really disconnected
682 * when peer sends FIN and acks ours.
684 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
686 static void
687 tcp_usr_disconnect(netmsg_t msg)
689 struct socket *so = msg->disconnect.base.nm_so;
690 int error = 0;
691 struct inpcb *inp;
692 struct tcpcb *tp;
694 COMMON_START(so, inp, 1);
695 tp = tcp_disconnect(tp);
696 COMMON_END(PRU_DISCONNECT);
700 * Accept a connection. Essentially all the work is
701 * done at higher levels; just return the address
702 * of the peer, storing through addr.
704 static void
705 tcp_usr_accept(netmsg_t msg)
707 struct socket *so = msg->accept.base.nm_so;
708 struct sockaddr **nam = msg->accept.nm_nam;
709 int error = 0;
710 struct inpcb *inp;
711 struct tcpcb *tp = NULL;
712 TCPDEBUG0;
714 inp = so->so_pcb;
715 if (so->so_state & SS_ISDISCONNECTED) {
716 error = ECONNABORTED;
717 goto out;
719 if (inp == NULL) {
720 error = EINVAL;
721 goto out;
724 tp = intotcpcb(inp);
725 TCPDEBUG1();
726 in_setpeeraddr(so, nam);
727 COMMON_END(PRU_ACCEPT);
730 #ifdef INET6
731 static void
732 tcp6_usr_accept(netmsg_t msg)
734 struct socket *so = msg->accept.base.nm_so;
735 struct sockaddr **nam = msg->accept.nm_nam;
736 int error = 0;
737 struct inpcb *inp;
738 struct tcpcb *tp = NULL;
739 TCPDEBUG0;
741 inp = so->so_pcb;
743 if (so->so_state & SS_ISDISCONNECTED) {
744 error = ECONNABORTED;
745 goto out;
747 if (inp == NULL) {
748 error = EINVAL;
749 goto out;
751 tp = intotcpcb(inp);
752 TCPDEBUG1();
753 in6_setpeeraddr(so, nam);
754 COMMON_END(PRU_ACCEPT);
756 #endif /* INET6 */
759 * Mark the connection as being incapable of further output.
761 static void
762 tcp_usr_shutdown(netmsg_t msg)
764 struct socket *so = msg->shutdown.base.nm_so;
765 int error = 0;
766 struct inpcb *inp;
767 struct tcpcb *tp;
769 COMMON_START(so, inp, 0);
770 socantsendmore(so);
771 tp = tcp_usrclosed(tp);
772 if (tp)
773 error = tcp_output(tp);
774 COMMON_END(PRU_SHUTDOWN);
778 * After a receive, possibly send window update to peer.
780 static void
781 tcp_usr_rcvd(netmsg_t msg)
783 struct socket *so = msg->rcvd.base.nm_so;
784 int error = 0, noreply = 0;
785 struct inpcb *inp;
786 struct tcpcb *tp;
788 COMMON_START(so, inp, 0);
790 if (msg->rcvd.nm_pru_flags & PRUR_ASYNC) {
791 noreply = 1;
792 so_async_rcvd_reply(so);
794 tcp_output(tp);
796 COMMON_END1(PRU_RCVD, noreply);
800 * Do a send by putting data in output queue and updating urgent
801 * marker if URG set. Possibly send more data. Unlike the other
802 * pru_*() routines, the mbuf chains are our responsibility. We
803 * must either enqueue them or free them. The other pru_* routines
804 * generally are caller-frees.
806 static void
807 tcp_usr_send(netmsg_t msg)
809 struct socket *so = msg->send.base.nm_so;
810 int flags = msg->send.nm_flags;
811 struct mbuf *m = msg->send.nm_m;
812 int error = 0;
813 struct inpcb *inp;
814 struct tcpcb *tp;
815 TCPDEBUG0;
817 KKASSERT(msg->send.nm_control == NULL);
818 KKASSERT(msg->send.nm_addr == NULL);
819 KKASSERT((flags & PRUS_FREEADDR) == 0);
821 inp = so->so_pcb;
823 if (inp == NULL) {
825 * OOPS! we lost a race, the TCP session got reset after
826 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
827 * network interrupt in the non-critical section of sosend().
829 m_freem(m);
830 error = ECONNRESET; /* XXX EPIPE? */
831 tp = NULL;
832 TCPDEBUG1();
833 goto out;
835 tp = intotcpcb(inp);
836 TCPDEBUG1();
838 #ifdef foo
840 * This is no longer necessary, since:
841 * - sosendtcp() has already checked it for us
842 * - It does not work with asynchronized send
846 * Don't let too much OOB data build up
848 if (flags & PRUS_OOB) {
849 if (ssb_space(&so->so_snd) < -512) {
850 m_freem(m);
851 error = ENOBUFS;
852 goto out;
855 #endif
858 * Pump the data into the socket.
860 if (m) {
861 ssb_appendstream(&so->so_snd, m);
862 sowwakeup(so);
864 if (flags & PRUS_OOB) {
866 * According to RFC961 (Assigned Protocols),
867 * the urgent pointer points to the last octet
868 * of urgent data. We continue, however,
869 * to consider it to indicate the first octet
870 * of data past the urgent section.
871 * Otherwise, snd_up should be one lower.
873 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
874 tp->t_flags |= TF_FORCE;
875 error = tcp_output(tp);
876 tp->t_flags &= ~TF_FORCE;
877 } else {
878 if (flags & PRUS_EOF) {
880 * Close the send side of the connection after
881 * the data is sent.
883 socantsendmore(so);
884 tp = tcp_usrclosed(tp);
886 if (tp != NULL && !tcp_output_pending(tp)) {
887 if (flags & PRUS_MORETOCOME)
888 tp->t_flags |= TF_MORETOCOME;
889 error = tcp_output_fair(tp);
890 if (flags & PRUS_MORETOCOME)
891 tp->t_flags &= ~TF_MORETOCOME;
894 COMMON_END1((flags & PRUS_OOB) ? PRU_SENDOOB :
895 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND),
896 (flags & PRUS_NOREPLY));
900 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
901 * will sofree() it when we return.
903 static void
904 tcp_usr_abort(netmsg_t msg)
906 struct socket *so = msg->abort.base.nm_so;
907 int error = 0;
908 struct inpcb *inp;
909 struct tcpcb *tp;
911 COMMON_START(so, inp, 1);
912 tp = tcp_drop(tp, ECONNABORTED);
913 COMMON_END(PRU_ABORT);
917 * Receive out-of-band data.
919 static void
920 tcp_usr_rcvoob(netmsg_t msg)
922 struct socket *so = msg->rcvoob.base.nm_so;
923 struct mbuf *m = msg->rcvoob.nm_m;
924 int flags = msg->rcvoob.nm_flags;
925 int error = 0;
926 struct inpcb *inp;
927 struct tcpcb *tp;
929 COMMON_START(so, inp, 0);
930 if ((so->so_oobmark == 0 &&
931 (so->so_state & SS_RCVATMARK) == 0) ||
932 so->so_options & SO_OOBINLINE ||
933 tp->t_oobflags & TCPOOB_HADDATA) {
934 error = EINVAL;
935 goto out;
937 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
938 error = EWOULDBLOCK;
939 goto out;
941 m->m_len = 1;
942 *mtod(m, caddr_t) = tp->t_iobc;
943 if ((flags & MSG_PEEK) == 0)
944 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
945 COMMON_END(PRU_RCVOOB);
948 static void
949 tcp_usr_savefaddr(struct socket *so, const struct sockaddr *faddr)
951 in_savefaddr(so, faddr);
954 #ifdef INET6
955 static void
956 tcp6_usr_savefaddr(struct socket *so, const struct sockaddr *faddr)
958 in6_savefaddr(so, faddr);
960 #endif
962 static int
963 tcp_usr_preconnect(struct socket *so, const struct sockaddr *nam,
964 struct thread *td __unused)
966 const struct sockaddr_in *sinp;
968 sinp = (const struct sockaddr_in *)nam;
969 if (sinp->sin_family == AF_INET &&
970 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
971 return EAFNOSUPPORT;
973 soisconnecting(so);
974 return 0;
977 /* xxx - should be const */
978 struct pr_usrreqs tcp_usrreqs = {
979 .pru_abort = tcp_usr_abort,
980 .pru_accept = tcp_usr_accept,
981 .pru_attach = tcp_usr_attach,
982 .pru_bind = tcp_usr_bind,
983 .pru_connect = tcp_usr_connect,
984 .pru_connect2 = pr_generic_notsupp,
985 .pru_control = in_control_dispatch,
986 .pru_detach = tcp_usr_detach,
987 .pru_disconnect = tcp_usr_disconnect,
988 .pru_listen = tcp_usr_listen,
989 .pru_peeraddr = in_setpeeraddr_dispatch,
990 .pru_rcvd = tcp_usr_rcvd,
991 .pru_rcvoob = tcp_usr_rcvoob,
992 .pru_send = tcp_usr_send,
993 .pru_sense = pru_sense_null,
994 .pru_shutdown = tcp_usr_shutdown,
995 .pru_sockaddr = in_setsockaddr_dispatch,
996 .pru_sosend = sosendtcp,
997 .pru_soreceive = sorecvtcp,
998 .pru_savefaddr = tcp_usr_savefaddr,
999 .pru_preconnect = tcp_usr_preconnect,
1000 .pru_preattach = tcp_usr_preattach
1003 #ifdef INET6
1004 struct pr_usrreqs tcp6_usrreqs = {
1005 .pru_abort = tcp_usr_abort,
1006 .pru_accept = tcp6_usr_accept,
1007 .pru_attach = tcp_usr_attach,
1008 .pru_bind = tcp6_usr_bind,
1009 .pru_connect = tcp6_usr_connect,
1010 .pru_connect2 = pr_generic_notsupp,
1011 .pru_control = in6_control_dispatch,
1012 .pru_detach = tcp_usr_detach,
1013 .pru_disconnect = tcp_usr_disconnect,
1014 .pru_listen = tcp6_usr_listen,
1015 .pru_peeraddr = in6_setpeeraddr_dispatch,
1016 .pru_rcvd = tcp_usr_rcvd,
1017 .pru_rcvoob = tcp_usr_rcvoob,
1018 .pru_send = tcp_usr_send,
1019 .pru_sense = pru_sense_null,
1020 .pru_shutdown = tcp_usr_shutdown,
1021 .pru_sockaddr = in6_setsockaddr_dispatch,
1022 .pru_sosend = sosendtcp,
1023 .pru_soreceive = sorecvtcp,
1024 .pru_savefaddr = tcp6_usr_savefaddr
1026 #endif /* INET6 */
1028 static int
1029 tcp_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf *m,
1030 const struct sockaddr_in *sin, struct sockaddr_in *if_sin,
1031 uint16_t hash)
1033 struct inpcb *inp = tp->t_inpcb, *oinp;
1034 struct socket *so = inp->inp_socket;
1035 struct route *ro = &inp->inp_route;
1037 KASSERT(inp->inp_pcbinfo == &tcbinfo[mycpu->gd_cpuid],
1038 ("pcbinfo mismatch"));
1040 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
1041 sin->sin_addr, sin->sin_port,
1042 (inp->inp_laddr.s_addr != INADDR_ANY ?
1043 inp->inp_laddr : if_sin->sin_addr),
1044 inp->inp_lport, 0, NULL);
1045 if (oinp != NULL) {
1046 m_freem(m);
1047 return (EADDRINUSE);
1049 if (inp->inp_laddr.s_addr == INADDR_ANY)
1050 inp->inp_laddr = if_sin->sin_addr;
1051 KASSERT(inp->inp_faddr.s_addr == sin->sin_addr.s_addr,
1052 ("faddr mismatch for reconnect"));
1053 KASSERT(inp->inp_fport == sin->sin_port,
1054 ("fport mismatch for reconnect"));
1055 in_pcbinsconnhash(inp);
1057 inp->inp_flags |= INP_HASH;
1058 inp->inp_hashval = hash;
1061 * We are now on the inpcb's owner CPU, if the cached route was
1062 * freed because the rtentry's owner CPU is not the current CPU
1063 * (e.g. in tcp_connect()), then we try to reallocate it here with
1064 * the hope that a rtentry may be cloned from a RTF_PRCLONING
1065 * rtentry.
1067 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
1068 ro->ro_rt == NULL) {
1069 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
1070 ro->ro_dst.sa_family = AF_INET;
1071 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
1072 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr =
1073 sin->sin_addr;
1074 rtalloc(ro);
1078 * Now that no more errors can occur, change the protocol processing
1079 * port to the current thread (which is the correct thread).
1081 * Create TCP timer message now; we are on the tcpcb's owner
1082 * CPU/thread.
1084 tcp_create_timermsg(tp, &curthread->td_msgport);
1087 * Compute window scaling to request. Use a larger scaling then
1088 * needed for the initial receive buffer in case the receive buffer
1089 * gets expanded.
1091 if (tp->request_r_scale < TCP_MIN_WINSHIFT)
1092 tp->request_r_scale = TCP_MIN_WINSHIFT;
1093 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1094 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat
1096 tp->request_r_scale++;
1099 soisconnecting(so);
1100 tcpstat.tcps_connattempt++;
1101 TCP_STATE_CHANGE(tp, TCPS_SYN_SENT);
1102 tcp_callout_reset(tp, tp->tt_keep, tp->t_keepinit, tcp_timer_keep);
1103 tp->iss = tcp_new_isn(tp);
1104 tcp_sendseqinit(tp);
1105 if (m) {
1106 ssb_appendstream(&so->so_snd, m);
1107 m = NULL;
1108 if (flags & PRUS_OOB)
1109 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
1113 * Close the send side of the connection after
1114 * the data is sent if flagged.
1116 if ((flags & (PRUS_OOB|PRUS_EOF)) == PRUS_EOF) {
1117 socantsendmore(so);
1118 tp = tcp_usrclosed(tp);
1120 return (tcp_output(tp));
1124 * Common subroutine to open a TCP connection to remote host specified
1125 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1126 * port number if needed. Call in_pcbladdr to do the routing and to choose
1127 * a local host address (interface).
1128 * Initialize connection parameters and enter SYN-SENT state.
1130 static void
1131 tcp_connect(netmsg_t msg)
1133 struct socket *so = msg->connect.base.nm_so;
1134 struct sockaddr *nam = msg->connect.nm_nam;
1135 struct thread *td = msg->connect.nm_td;
1136 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1137 struct sockaddr_in *if_sin = NULL;
1138 struct inpcb *inp;
1139 struct tcpcb *tp;
1140 int error;
1141 uint16_t hash;
1142 lwkt_port_t port;
1144 COMMON_START(so, inp, 0);
1147 * Reconnect our pcb if we have to
1149 if (msg->connect.nm_flags & PRUC_RECONNECT) {
1150 msg->connect.nm_flags &= ~PRUC_RECONNECT;
1151 TCP_STATE_MIGRATE_END(tp);
1152 in_pcblink(so->so_pcb, &tcbinfo[mycpu->gd_cpuid]);
1153 } else {
1154 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1155 kprintf("inpcb %p, double-connect race\n", inp);
1156 error = EISCONN;
1157 if (so->so_state & SS_ISCONNECTING)
1158 error = EALREADY;
1159 goto out;
1161 KASSERT(inp->inp_fport == 0, ("invalid fport"));
1165 * Select local port, if it is not yet selected.
1167 if (inp->inp_lport == 0) {
1168 KKASSERT(inp->inp_laddr.s_addr == INADDR_ANY);
1170 error = in_pcbladdr(inp, nam, &if_sin, td);
1171 if (error)
1172 goto out;
1173 inp->inp_laddr.s_addr = if_sin->sin_addr.s_addr;
1174 msg->connect.nm_flags |= PRUC_HASLADDR;
1177 * Install faddr/fport earlier, so that when this
1178 * inpcb is installed on to the lport hash, the
1179 * 4-tuple contains correct value.
1181 * NOTE: The faddr/fport will have to be installed
1182 * after the in_pcbladdr(), which may change them.
1184 inp->inp_faddr = sin->sin_addr;
1185 inp->inp_fport = sin->sin_port;
1187 error = in_pcbbind_remote(inp, nam, td);
1188 if (error)
1189 goto out;
1192 if ((msg->connect.nm_flags & PRUC_HASLADDR) == 0) {
1194 * Rarely used path:
1195 * This inpcb was bound before this connect.
1197 error = in_pcbladdr(inp, nam, &if_sin, td);
1198 if (error)
1199 goto out;
1202 * Save or refresh the faddr/fport, since they may
1203 * be changed by in_pcbladdr().
1205 inp->inp_faddr = sin->sin_addr;
1206 inp->inp_fport = sin->sin_port;
1208 #ifdef INVARIANTS
1209 else {
1210 KASSERT(inp->inp_faddr.s_addr == sin->sin_addr.s_addr,
1211 ("faddr mismatch for reconnect"));
1212 KASSERT(inp->inp_fport == sin->sin_port,
1213 ("fport mismatch for reconnect"));
1215 #endif
1216 KKASSERT(inp->inp_socket == so);
1218 hash = tcp_addrhash(sin->sin_addr.s_addr, sin->sin_port,
1219 (inp->inp_laddr.s_addr != INADDR_ANY ?
1220 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr),
1221 inp->inp_lport);
1222 port = netisr_hashport(hash);
1224 if (port != &curthread->td_msgport) {
1225 lwkt_msg_t lmsg = &msg->connect.base.lmsg;
1228 * in_pcbladdr() may have allocated a route entry for us
1229 * on the current CPU, but we need a route entry on the
1230 * inpcb's owner CPU, so free it here.
1232 in_pcbresetroute(inp);
1235 * We are moving the protocol processing port the socket
1236 * is on, we have to unlink here and re-link on the
1237 * target cpu.
1239 in_pcbunlink(so->so_pcb, &tcbinfo[mycpu->gd_cpuid]);
1240 msg->connect.nm_flags |= PRUC_RECONNECT;
1241 msg->connect.base.nm_dispatch = tcp_connect;
1243 TCP_STATE_MIGRATE_START(tp);
1246 * Use message put done receipt to change this socket's
1247 * so_port, i.e. _after_ this message was put onto the
1248 * target netisr's msgport but _before_ the message could
1249 * be pulled from the target netisr's msgport, so that:
1250 * - The upper half (socket code) will not see the new
1251 * msgport before this message reaches the new msgport
1252 * and messages for this socket will be ordered.
1253 * - This message will see the new msgport, when its
1254 * handler is called in the target netisr.
1256 * NOTE:
1257 * We MUST use messege put done receipt to change this
1258 * socket's so_port:
1259 * If we changed the so_port in this netisr after the
1260 * lwkt_forwardmsg (so messages for this socket will be
1261 * ordered) and changed the so_port in the target netisr
1262 * at the very beginning of this message's handler, we
1263 * would suffer so_port overwritten race, given this
1264 * message might be forwarded again.
1266 * NOTE:
1267 * This mechanism depends on that the netisr's msgport
1268 * is spin msgport (currently it is :).
1270 * If the upper half saw the new msgport before this
1271 * message reached the target netisr's msgport, the
1272 * messages sent from the upper half could reach the new
1273 * msgport before this message, thus there would be
1274 * message reordering. The worst case could be soclose()
1275 * saw the new msgport and the detach message could reach
1276 * the new msgport before this message, i.e. the inpcb
1277 * could have been destroyed when this message was still
1278 * pending on or on its way to the new msgport. Other
1279 * weird cases could also happen, e.g. inpcb->inp_pcbinfo,
1280 * since we have unlinked this inpcb from the current
1281 * pcbinfo first.
1283 lwkt_setmsg_receipt(lmsg, tcp_sosetport);
1284 lwkt_forwardmsg(port, lmsg);
1285 /* msg invalid now */
1286 return;
1287 } else if (msg->connect.nm_flags & PRUC_HELDTD) {
1289 * The original thread is no longer needed; release it.
1291 lwkt_rele(td);
1292 msg->connect.nm_flags &= ~PRUC_HELDTD;
1294 error = tcp_connect_oncpu(tp, msg->connect.nm_sndflags,
1295 msg->connect.nm_m, sin, if_sin, hash);
1296 msg->connect.nm_m = NULL;
1297 out:
1298 if (msg->connect.nm_m) {
1299 m_freem(msg->connect.nm_m);
1300 msg->connect.nm_m = NULL;
1302 if (msg->connect.nm_flags & PRUC_HELDTD)
1303 lwkt_rele(td);
1304 if (error && (msg->connect.nm_flags & PRUC_ASYNC)) {
1305 so->so_error = error;
1306 soisdisconnected(so);
1308 lwkt_replymsg(&msg->connect.base.lmsg, error);
1309 /* msg invalid now */
1312 #ifdef INET6
1314 static void
1315 tcp6_connect(netmsg_t msg)
1317 struct tcpcb *tp;
1318 struct socket *so = msg->connect.base.nm_so;
1319 struct sockaddr *nam = msg->connect.nm_nam;
1320 struct thread *td = msg->connect.nm_td;
1321 struct inpcb *inp;
1322 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1323 struct in6_addr *addr6;
1324 lwkt_port_t port;
1325 int error;
1327 COMMON_START(so, inp, 0);
1330 * Reconnect our pcb if we have to
1332 if (msg->connect.nm_flags & PRUC_RECONNECT) {
1333 msg->connect.nm_flags &= ~PRUC_RECONNECT;
1334 TCP_STATE_MIGRATE_END(tp);
1335 in_pcblink(so->so_pcb, &tcbinfo[mycpu->gd_cpuid]);
1339 * Bind if we have to
1341 if (inp->inp_lport == 0) {
1342 error = in6_pcbbind(inp, NULL, td);
1343 if (error)
1344 goto out;
1348 * Cannot simply call in_pcbconnect, because there might be an
1349 * earlier incarnation of this same connection still in
1350 * TIME_WAIT state, creating an ADDRINUSE error.
1352 error = in6_pcbladdr(inp, nam, &addr6, td);
1353 if (error)
1354 goto out;
1356 port = tcp6_addrport(); /* XXX hack for now, always cpu0 */
1358 if (port != &curthread->td_msgport) {
1359 lwkt_msg_t lmsg = &msg->connect.base.lmsg;
1362 * in_pcbladdr() may have allocated a route entry for us
1363 * on the current CPU, but we need a route entry on the
1364 * inpcb's owner CPU, so free it here.
1366 in_pcbresetroute(inp);
1368 in_pcbunlink(so->so_pcb, &tcbinfo[mycpu->gd_cpuid]);
1369 msg->connect.nm_flags |= PRUC_RECONNECT;
1370 msg->connect.base.nm_dispatch = tcp6_connect;
1372 TCP_STATE_MIGRATE_START(tp);
1374 /* See the related comment in tcp_connect() */
1375 lwkt_setmsg_receipt(lmsg, tcp_sosetport);
1376 lwkt_forwardmsg(port, lmsg);
1377 /* msg invalid now */
1378 return;
1380 error = tcp6_connect_oncpu(tp, msg->connect.nm_sndflags,
1381 &msg->connect.nm_m, sin6, addr6);
1382 /* nm_m may still be intact */
1383 out:
1384 if (msg->connect.nm_m) {
1385 m_freem(msg->connect.nm_m);
1386 msg->connect.nm_m = NULL;
1388 lwkt_replymsg(&msg->connect.base.lmsg, error);
1389 /* msg invalid now */
1392 static int
1393 tcp6_connect_oncpu(struct tcpcb *tp, int flags, struct mbuf **mp,
1394 struct sockaddr_in6 *sin6, struct in6_addr *addr6)
1396 struct mbuf *m = *mp;
1397 struct inpcb *inp = tp->t_inpcb;
1398 struct socket *so = inp->inp_socket;
1399 struct inpcb *oinp;
1402 * Cannot simply call in_pcbconnect, because there might be an
1403 * earlier incarnation of this same connection still in
1404 * TIME_WAIT state, creating an ADDRINUSE error.
1406 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1407 &sin6->sin6_addr, sin6->sin6_port,
1408 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1409 addr6 : &inp->in6p_laddr),
1410 inp->inp_lport, 0, NULL);
1411 if (oinp)
1412 return (EADDRINUSE);
1414 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1415 inp->in6p_laddr = *addr6;
1416 inp->in6p_faddr = sin6->sin6_addr;
1417 inp->inp_fport = sin6->sin6_port;
1418 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
1419 inp->in6p_flowinfo = sin6->sin6_flowinfo;
1420 in_pcbinsconnhash(inp);
1423 * Now that no more errors can occur, change the protocol processing
1424 * port to the current thread (which is the correct thread).
1426 * Create TCP timer message now; we are on the tcpcb's owner
1427 * CPU/thread.
1429 tcp_create_timermsg(tp, &curthread->td_msgport);
1431 /* Compute window scaling to request. */
1432 if (tp->request_r_scale < TCP_MIN_WINSHIFT)
1433 tp->request_r_scale = TCP_MIN_WINSHIFT;
1434 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1435 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.ssb_hiwat) {
1436 tp->request_r_scale++;
1439 soisconnecting(so);
1440 tcpstat.tcps_connattempt++;
1441 TCP_STATE_CHANGE(tp, TCPS_SYN_SENT);
1442 tcp_callout_reset(tp, tp->tt_keep, tp->t_keepinit, tcp_timer_keep);
1443 tp->iss = tcp_new_isn(tp);
1444 tcp_sendseqinit(tp);
1445 if (m) {
1446 ssb_appendstream(&so->so_snd, m);
1447 *mp = NULL;
1448 if (flags & PRUS_OOB)
1449 tp->snd_up = tp->snd_una + so->so_snd.ssb_cc;
1453 * Close the send side of the connection after
1454 * the data is sent if flagged.
1456 if ((flags & (PRUS_OOB|PRUS_EOF)) == PRUS_EOF) {
1457 socantsendmore(so);
1458 tp = tcp_usrclosed(tp);
1460 return (tcp_output(tp));
1463 #endif /* INET6 */
1466 * The new sockopt interface makes it possible for us to block in the
1467 * copyin/out step (if we take a page fault). Taking a page fault while
1468 * in a critical section is probably a Bad Thing. (Since sockets and pcbs
1469 * both now use TSM, there probably isn't any need for this function to
1470 * run in a critical section any more. This needs more examination.)
1472 void
1473 tcp_ctloutput(netmsg_t msg)
1475 struct socket *so = msg->base.nm_so;
1476 struct sockopt *sopt = msg->ctloutput.nm_sopt;
1477 struct thread *td = NULL;
1478 int error, opt, optval, opthz;
1479 struct inpcb *inp;
1480 struct tcpcb *tp;
1482 if (msg->ctloutput.nm_flags & PRCO_HELDTD)
1483 td = sopt->sopt_td;
1485 error = 0;
1486 inp = so->so_pcb;
1487 if (inp == NULL) {
1488 error = ECONNRESET;
1489 goto done;
1491 tp = intotcpcb(inp);
1493 /* Get socket's owner cpuid hint */
1494 if (sopt->sopt_level == SOL_SOCKET &&
1495 sopt->sopt_dir == SOPT_GET &&
1496 sopt->sopt_name == SO_CPUHINT) {
1497 if (tp->t_flags & TF_LISTEN) {
1499 * Listen sockets owner cpuid is always 0,
1500 * which does not make sense if SO_REUSEPORT
1501 * is not set.
1503 * NOTE: inp_lgrpindex is _not_ assigned in jail.
1505 if ((so->so_options & SO_REUSEPORT) &&
1506 inp->inp_lgrpindex >= 0)
1507 optval = inp->inp_lgrpindex % netisr_ncpus;
1508 else
1509 optval = -1; /* no hint */
1510 } else {
1511 optval = mycpuid;
1513 soopt_from_kbuf(sopt, &optval, sizeof(optval));
1514 goto done;
1517 if (sopt->sopt_level != IPPROTO_TCP) {
1518 if (sopt->sopt_level == IPPROTO_IP) {
1519 switch (sopt->sopt_name) {
1520 case IP_MULTICAST_IF:
1521 case IP_MULTICAST_VIF:
1522 case IP_MULTICAST_TTL:
1523 case IP_MULTICAST_LOOP:
1524 case IP_ADD_MEMBERSHIP:
1525 case IP_DROP_MEMBERSHIP:
1527 * Multicast does not make sense on
1528 * TCP sockets.
1530 error = EOPNOTSUPP;
1531 goto done;
1534 #ifdef INET6
1535 if (INP_CHECK_SOCKAF(so, AF_INET6))
1536 ip6_ctloutput_dispatch(msg);
1537 else
1538 #endif /* INET6 */
1539 ip_ctloutput(msg);
1540 /* msg invalid now */
1541 if (td != NULL)
1542 lwkt_rele(td);
1543 return;
1546 switch (sopt->sopt_dir) {
1547 case SOPT_SET:
1548 error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1549 sizeof optval);
1550 if (error)
1551 break;
1552 switch (sopt->sopt_name) {
1553 case TCP_FASTKEEP:
1554 if (optval > 0)
1555 tp->t_keepidle = tp->t_keepintvl;
1556 else
1557 tp->t_keepidle = tcp_keepidle;
1558 tcp_timer_keep_activity(tp, 0);
1559 break;
1560 #ifdef TCP_SIGNATURE
1561 case TCP_SIGNATURE_ENABLE:
1562 if (tp->t_state == TCPS_CLOSED) {
1564 * This is the only safe state that this
1565 * option could be changed. Some segments
1566 * could already have been sent in other
1567 * states.
1569 if (optval > 0)
1570 tp->t_flags |= TF_SIGNATURE;
1571 else
1572 tp->t_flags &= ~TF_SIGNATURE;
1573 } else {
1574 error = EOPNOTSUPP;
1576 break;
1577 #endif /* TCP_SIGNATURE */
1578 case TCP_NODELAY:
1579 case TCP_NOOPT:
1580 switch (sopt->sopt_name) {
1581 case TCP_NODELAY:
1582 opt = TF_NODELAY;
1583 break;
1584 case TCP_NOOPT:
1585 opt = TF_NOOPT;
1586 break;
1587 default:
1588 opt = 0; /* dead code to fool gcc */
1589 break;
1592 if (optval)
1593 tp->t_flags |= opt;
1594 else
1595 tp->t_flags &= ~opt;
1596 break;
1598 case TCP_NOPUSH:
1599 if (tcp_disable_nopush)
1600 break;
1601 if (optval)
1602 tp->t_flags |= TF_NOPUSH;
1603 else {
1604 tp->t_flags &= ~TF_NOPUSH;
1605 error = tcp_output(tp);
1607 break;
1609 case TCP_MAXSEG:
1611 * Must be between 0 and maxseg. If the requested
1612 * maxseg is too small to satisfy the desired minmss,
1613 * pump it up (silently so sysctl modifications of
1614 * minmss do not create unexpected program failures).
1615 * Handle degenerate cases.
1617 if (optval > 0 && optval <= tp->t_maxseg) {
1618 if (optval + 40 < tcp_minmss) {
1619 optval = tcp_minmss - 40;
1620 if (optval < 0)
1621 optval = 1;
1623 tp->t_maxseg = optval;
1624 } else {
1625 error = EINVAL;
1627 break;
1629 case TCP_KEEPINIT:
1630 opthz = ((int64_t)optval * hz) / 1000;
1631 if (opthz >= 1)
1632 tp->t_keepinit = opthz;
1633 else
1634 error = EINVAL;
1635 break;
1637 case TCP_KEEPIDLE:
1638 opthz = ((int64_t)optval * hz) / 1000;
1639 if (opthz >= 1) {
1640 tp->t_keepidle = opthz;
1641 tcp_timer_keep_activity(tp, 0);
1642 } else {
1643 error = EINVAL;
1645 break;
1647 case TCP_KEEPINTVL:
1648 opthz = ((int64_t)optval * hz) / 1000;
1649 if (opthz >= 1) {
1650 tp->t_keepintvl = opthz;
1651 tp->t_maxidle = tp->t_keepintvl * tp->t_keepcnt;
1652 } else {
1653 error = EINVAL;
1655 break;
1657 case TCP_KEEPCNT:
1658 if (optval > 0) {
1659 tp->t_keepcnt = optval;
1660 tp->t_maxidle = tp->t_keepintvl * tp->t_keepcnt;
1661 } else {
1662 error = EINVAL;
1664 break;
1666 default:
1667 error = ENOPROTOOPT;
1668 break;
1670 break;
1672 case SOPT_GET:
1673 switch (sopt->sopt_name) {
1674 #ifdef TCP_SIGNATURE
1675 case TCP_SIGNATURE_ENABLE:
1676 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1677 break;
1678 #endif /* TCP_SIGNATURE */
1679 case TCP_NODELAY:
1680 optval = tp->t_flags & TF_NODELAY;
1681 break;
1682 case TCP_MAXSEG:
1683 optval = tp->t_maxseg;
1684 break;
1685 case TCP_NOOPT:
1686 optval = tp->t_flags & TF_NOOPT;
1687 break;
1688 case TCP_NOPUSH:
1689 optval = tp->t_flags & TF_NOPUSH;
1690 break;
1691 case TCP_KEEPINIT:
1692 optval = ((int64_t)tp->t_keepinit * 1000) / hz;
1693 break;
1694 case TCP_KEEPIDLE:
1695 optval = ((int64_t)tp->t_keepidle * 1000) / hz;
1696 break;
1697 case TCP_KEEPINTVL:
1698 optval = ((int64_t)tp->t_keepintvl * 1000) / hz;
1699 break;
1700 case TCP_KEEPCNT:
1701 optval = tp->t_keepcnt;
1702 break;
1703 default:
1704 error = ENOPROTOOPT;
1705 break;
1707 if (error == 0)
1708 soopt_from_kbuf(sopt, &optval, sizeof optval);
1709 break;
1711 done:
1712 if (td != NULL)
1713 lwkt_rele(td);
1714 lwkt_replymsg(&msg->lmsg, error);
1717 struct netmsg_tcp_ctloutput {
1718 struct netmsg_pr_ctloutput ctloutput;
1719 struct sockopt sopt;
1720 int sopt_val;
1724 * Allocate netmsg_pr_ctloutput for asynchronous tcp_ctloutput.
1726 struct netmsg_pr_ctloutput *
1727 tcp_ctloutmsg(struct sockopt *sopt)
1729 struct netmsg_tcp_ctloutput *msg;
1730 int flags = 0, error;
1732 KASSERT(sopt->sopt_dir == SOPT_SET, ("not from ctloutput"));
1734 /* Only small set of options allows asynchronous setting. */
1735 if (sopt->sopt_level != IPPROTO_TCP)
1736 return NULL;
1737 switch (sopt->sopt_name) {
1738 case TCP_NODELAY:
1739 case TCP_NOOPT:
1740 case TCP_NOPUSH:
1741 case TCP_FASTKEEP:
1742 break;
1743 default:
1744 return NULL;
1747 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK | M_NULLOK);
1748 if (msg == NULL) {
1749 /* Fallback to synchronous tcp_ctloutput */
1750 return NULL;
1753 /* Save the sockopt */
1754 msg->sopt = *sopt;
1756 /* Fixup the sopt.sopt_val ptr */
1757 error = sooptcopyin(sopt, &msg->sopt_val,
1758 sizeof(msg->sopt_val), sizeof(msg->sopt_val));
1759 if (error) {
1760 kfree(msg, M_LWKTMSG);
1761 return NULL;
1763 msg->sopt.sopt_val = &msg->sopt_val;
1765 /* Hold the current thread */
1766 if (msg->sopt.sopt_td != NULL) {
1767 flags |= PRCO_HELDTD;
1768 lwkt_hold(msg->sopt.sopt_td);
1771 msg->ctloutput.nm_flags = flags;
1772 msg->ctloutput.nm_sopt = &msg->sopt;
1774 return &msg->ctloutput;
1778 * tcp_sendspace and tcp_recvspace are the default send and receive window
1779 * sizes, respectively. These are obsolescent (this information should
1780 * be set by the route).
1782 * Use a default that does not require tcp window scaling to be turned
1783 * on. Individual programs or the administrator can increase the default.
1785 u_long tcp_sendspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1786 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1787 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1788 u_long tcp_recvspace = 57344; /* largest multiple of PAGE_SIZE < 64k */
1789 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1790 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1793 * Attach TCP protocol to socket, allocating internet protocol control
1794 * block, tcp control block, buffer space, and entering CLOSED state.
1796 static int
1797 tcp_attach(struct socket *so, struct pru_attach_info *ai)
1799 struct inpcb *inp;
1800 int error;
1801 int cpu;
1802 #ifdef INET6
1803 boolean_t isipv6 = INP_CHECK_SOCKAF(so, AF_INET6);
1804 #endif
1806 if (ai != NULL) {
1807 error = tcp_usr_preattach(so, 0 /* don't care */, ai);
1808 if (error)
1809 return (error);
1810 } else {
1811 /* Post attach; do nothing */
1814 cpu = mycpu->gd_cpuid;
1817 * Set the default pcbinfo. This will likely change when we
1818 * bind/connect.
1820 error = in_pcballoc(so, &tcbinfo[cpu]);
1821 if (error)
1822 return (error);
1823 inp = so->so_pcb;
1824 #ifdef INET6
1825 if (isipv6)
1826 inp->in6p_hops = -1; /* use kernel default */
1827 #endif
1828 tcp_newtcpcb(inp);
1829 /* Keep a reference for asynchronized pru_rcvd */
1830 soreference(so);
1831 return (0);
1835 * Initiate (or continue) disconnect.
1836 * If embryonic state, just send reset (once).
1837 * If in ``let data drain'' option and linger null, just drop.
1838 * Otherwise (hard), mark socket disconnecting and drop
1839 * current input data; switch states based on user close, and
1840 * send segment to peer (with FIN).
1842 static struct tcpcb *
1843 tcp_disconnect(struct tcpcb *tp)
1845 struct socket *so = tp->t_inpcb->inp_socket;
1847 if (tp->t_state < TCPS_ESTABLISHED) {
1848 tp = tcp_close(tp);
1849 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1850 tp = tcp_drop(tp, 0);
1851 } else {
1852 lwkt_gettoken(&so->so_rcv.ssb_token);
1853 soisdisconnecting(so);
1854 sbflush(&so->so_rcv.sb);
1855 tp = tcp_usrclosed(tp);
1856 if (tp)
1857 tcp_output(tp);
1858 lwkt_reltoken(&so->so_rcv.ssb_token);
1860 return (tp);
1864 * User issued close, and wish to trail through shutdown states:
1865 * if never received SYN, just forget it. If got a SYN from peer,
1866 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1867 * If already got a FIN from peer, then almost done; go to LAST_ACK
1868 * state. In all other cases, have already sent FIN to peer (e.g.
1869 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1870 * for peer to send FIN or not respond to keep-alives, etc.
1871 * We can let the user exit from the close as soon as the FIN is acked.
1873 static struct tcpcb *
1874 tcp_usrclosed(struct tcpcb *tp)
1877 switch (tp->t_state) {
1879 case TCPS_CLOSED:
1880 case TCPS_LISTEN:
1881 TCP_STATE_CHANGE(tp, TCPS_CLOSED);
1882 tp = tcp_close(tp);
1883 break;
1885 case TCPS_SYN_SENT:
1886 case TCPS_SYN_RECEIVED:
1887 tp->t_flags |= TF_NEEDFIN;
1888 break;
1890 case TCPS_ESTABLISHED:
1891 TCP_STATE_CHANGE(tp, TCPS_FIN_WAIT_1);
1892 break;
1894 case TCPS_CLOSE_WAIT:
1895 TCP_STATE_CHANGE(tp, TCPS_LAST_ACK);
1896 break;
1898 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1899 soisdisconnected(tp->t_inpcb->inp_socket);
1900 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1901 if (tp->t_state == TCPS_FIN_WAIT_2) {
1902 tcp_callout_reset(tp, tp->tt_2msl, tp->t_maxidle,
1903 tcp_timer_2msl);
1906 return (tp);