2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2006 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
36 #include "opt_inet6.h"
37 #include "opt_tcpdebug.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
46 #include <sys/domain.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
55 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
60 #include <netinet/ip6.h>
62 #include <netinet/in_pcb.h>
64 #include <netinet6/in6_pcb.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/scope6_var.h>
72 #include <netinet/tcp.h>
73 #include <netinet/tcp_fsm.h>
74 #include <netinet/tcp_seq.h>
75 #include <netinet/tcp_timer.h>
76 #include <netinet/tcp_var.h>
77 #include <netinet/tcpip.h>
79 #include <netinet/tcp_debug.h>
83 * TCP protocol interface to socket abstraction.
85 extern char *tcpstates[]; /* XXX ??? */
87 static int tcp_attach(struct socket *);
88 static int tcp_connect(struct tcpcb *, struct sockaddr *,
91 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
94 static void tcp_disconnect(struct tcpcb *);
95 static void tcp_usrclosed(struct tcpcb *);
96 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
99 #define TCPDEBUG0 int ostate = 0
100 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
101 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
102 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
106 #define TCPDEBUG2(req)
110 * TCP attaches to socket via pru_attach(), reserving space,
111 * and an internet control block.
114 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
117 struct tcpcb *tp = NULL;
122 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
125 error = tcp_attach(so);
129 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
130 so->so_linger = TCP_LINGERTIME;
135 TCPDEBUG2(PRU_ATTACH);
140 * tcp_detach() releases any protocol state that can be reasonably released
141 * when a socket shutdown is requested, and is a shared code path for
142 * tcp_usr_detach() and tcp_usr_abort(), the two socket close entry points.
144 * Accepts pcbinfo, inpcb locked, will unlock the inpcb (if needed) on
148 tcp_detach(struct socket *so, struct inpcb *inp)
152 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
155 INP_INFO_WLOCK_ASSERT(&tcbinfo);
156 INP_LOCK_ASSERT(inp);
158 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
159 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
161 if (inp->inp_vflag & INP_TIMEWAIT) {
162 if (inp->inp_vflag & INP_DROPPED) {
164 * Connection was in time wait and has been dropped;
165 * the calling path is either via tcp_twclose(), or
166 * as a result of an eventual soclose() after
167 * tcp_twclose() has been called. In either case,
168 * tcp_twclose() has detached the tcptw from the
169 * inpcb, so we just detach and free the inpcb.
171 * XXXRW: Would it be cleaner to free the tcptw
187 * Connection is in time wait and has not yet been
188 * dropped; allow the socket to be discarded, but
189 * need to keep inpcb until end of time wait.
201 * If not in timewait, there are two possible paths. First,
202 * the TCP connection is either embryonic or done, in which
203 * case we tear down all state. Second, it may still be
204 * active, in which case we acquire a reference to the socket
205 * and will free it later when TCP is done.
208 if (inp->inp_vflag & INP_DROPPED ||
209 tp->t_state < TCPS_SYN_SENT) {
224 so->so_state |= SS_PROTOREF;
226 inp->inp_vflag |= INP_SOCKREF;
233 * pru_detach() detaches the TCP protocol from the socket.
234 * If the protocol state is non-embryonic, then can't
235 * do this directly: have to initiate a pru_disconnect(),
236 * which may finish later; embryonic TCB's can just
240 tcp_usr_detach(struct socket *so)
247 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
248 INP_INFO_WLOCK(&tcbinfo);
250 KASSERT(inp->inp_socket != NULL,
251 ("tcp_usr_detach: inp_socket == NULL"));
255 * First, if we still have full TCP state, and we're not dropped,
256 * initiate a disconnect.
258 if (!(inp->inp_vflag & INP_TIMEWAIT) &&
259 !(inp->inp_vflag & INP_DROPPED)) {
265 TCPDEBUG2(PRU_DETACH);
266 INP_INFO_WUNLOCK(&tcbinfo);
270 * Give the socket an address.
273 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
277 struct tcpcb *tp = NULL;
278 struct sockaddr_in *sinp;
280 sinp = (struct sockaddr_in *)nam;
281 if (nam->sa_len != sizeof (*sinp))
284 * Must check for multicast addresses and disallow binding
287 if (sinp->sin_family == AF_INET &&
288 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
289 return (EAFNOSUPPORT);
292 INP_INFO_WLOCK(&tcbinfo);
294 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
296 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
302 error = in_pcbbind(inp, nam, td->td_ucred);
306 INP_INFO_WUNLOCK(&tcbinfo);
313 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
317 struct tcpcb *tp = NULL;
318 struct sockaddr_in6 *sin6p;
320 sin6p = (struct sockaddr_in6 *)nam;
321 if (nam->sa_len != sizeof (*sin6p))
324 * Must check for multicast addresses and disallow binding
327 if (sin6p->sin6_family == AF_INET6 &&
328 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
329 return (EAFNOSUPPORT);
332 INP_INFO_WLOCK(&tcbinfo);
334 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
336 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
342 inp->inp_vflag &= ~INP_IPV4;
343 inp->inp_vflag |= INP_IPV6;
344 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
345 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
346 inp->inp_vflag |= INP_IPV4;
347 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
348 struct sockaddr_in sin;
350 in6_sin6_2_sin(&sin, sin6p);
351 inp->inp_vflag |= INP_IPV4;
352 inp->inp_vflag &= ~INP_IPV6;
353 error = in_pcbbind(inp, (struct sockaddr *)&sin,
358 error = in6_pcbbind(inp, nam, td->td_ucred);
362 INP_INFO_WUNLOCK(&tcbinfo);
368 * Prepare to accept connections.
371 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
375 struct tcpcb *tp = NULL;
378 INP_INFO_WLOCK(&tcbinfo);
380 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
382 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
389 error = solisten_proto_check(so);
390 if (error == 0 && inp->inp_lport == 0)
391 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
393 tp->t_state = TCPS_LISTEN;
394 solisten_proto(so, backlog);
399 TCPDEBUG2(PRU_LISTEN);
401 INP_INFO_WUNLOCK(&tcbinfo);
407 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
411 struct tcpcb *tp = NULL;
414 INP_INFO_WLOCK(&tcbinfo);
416 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
418 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
425 error = solisten_proto_check(so);
426 if (error == 0 && inp->inp_lport == 0) {
427 inp->inp_vflag &= ~INP_IPV4;
428 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
429 inp->inp_vflag |= INP_IPV4;
430 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
433 tp->t_state = TCPS_LISTEN;
434 solisten_proto(so, backlog);
439 TCPDEBUG2(PRU_LISTEN);
441 INP_INFO_WUNLOCK(&tcbinfo);
447 * Initiate connection to peer.
448 * Create a template for use in transmissions on this connection.
449 * Enter SYN_SENT state, and mark socket as connecting.
450 * Start keep-alive timer, and seed output sequence space.
451 * Send initial segment on connection.
454 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
458 struct tcpcb *tp = NULL;
459 struct sockaddr_in *sinp;
461 sinp = (struct sockaddr_in *)nam;
462 if (nam->sa_len != sizeof (*sinp))
465 * Must disallow TCP ``connections'' to multicast addresses.
467 if (sinp->sin_family == AF_INET
468 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
469 return (EAFNOSUPPORT);
470 if (jailed(td->td_ucred))
471 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
474 INP_INFO_WLOCK(&tcbinfo);
476 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
478 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
484 if ((error = tcp_connect(tp, nam, td)) != 0)
486 error = tcp_output(tp);
488 TCPDEBUG2(PRU_CONNECT);
490 INP_INFO_WUNLOCK(&tcbinfo);
496 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
500 struct tcpcb *tp = NULL;
501 struct sockaddr_in6 *sin6p;
505 sin6p = (struct sockaddr_in6 *)nam;
506 if (nam->sa_len != sizeof (*sin6p))
509 * Must disallow TCP ``connections'' to multicast addresses.
511 if (sin6p->sin6_family == AF_INET6
512 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
513 return (EAFNOSUPPORT);
515 INP_INFO_WLOCK(&tcbinfo);
517 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
519 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
525 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
526 struct sockaddr_in sin;
528 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
533 in6_sin6_2_sin(&sin, sin6p);
534 inp->inp_vflag |= INP_IPV4;
535 inp->inp_vflag &= ~INP_IPV6;
536 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
538 error = tcp_output(tp);
541 inp->inp_vflag &= ~INP_IPV4;
542 inp->inp_vflag |= INP_IPV6;
543 inp->inp_inc.inc_isipv6 = 1;
544 if ((error = tcp6_connect(tp, nam, td)) != 0)
546 error = tcp_output(tp);
549 TCPDEBUG2(PRU_CONNECT);
551 INP_INFO_WUNLOCK(&tcbinfo);
557 * Initiate disconnect from peer.
558 * If connection never passed embryonic stage, just drop;
559 * else if don't need to let data drain, then can just drop anyways,
560 * else have to begin TCP shutdown process: mark socket disconnecting,
561 * drain unread data, state switch to reflect user close, and
562 * send segment (e.g. FIN) to peer. Socket will be really disconnected
563 * when peer sends FIN and acks ours.
565 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
568 tcp_usr_disconnect(struct socket *so)
571 struct tcpcb *tp = NULL;
575 INP_INFO_WLOCK(&tcbinfo);
577 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
579 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
587 TCPDEBUG2(PRU_DISCONNECT);
589 INP_INFO_WUNLOCK(&tcbinfo);
594 * Accept a connection. Essentially all the work is
595 * done at higher levels; just return the address
596 * of the peer, storing through addr.
599 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
602 struct inpcb *inp = NULL;
603 struct tcpcb *tp = NULL;
608 if (so->so_state & SS_ISDISCONNECTED)
609 return (ECONNABORTED);
612 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
614 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
615 error = ECONNABORTED;
622 * We inline in_setpeeraddr and COMMON_END here, so that we can
623 * copy the data of interest and defer the malloc until after we
626 port = inp->inp_fport;
627 addr = inp->inp_faddr;
630 TCPDEBUG2(PRU_ACCEPT);
633 *nam = in_sockaddr(port, &addr);
639 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
641 struct inpcb *inp = NULL;
643 struct tcpcb *tp = NULL;
645 struct in6_addr addr6;
650 if (so->so_state & SS_ISDISCONNECTED)
651 return (ECONNABORTED);
654 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
656 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
664 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
665 * copy the data of interest and defer the malloc until after we
668 if (inp->inp_vflag & INP_IPV4) {
670 port = inp->inp_fport;
671 addr = inp->inp_faddr;
673 port = inp->inp_fport;
674 addr6 = inp->in6p_faddr;
678 TCPDEBUG2(PRU_ACCEPT);
682 *nam = in6_v4mapsin6_sockaddr(port, &addr);
684 *nam = in6_sockaddr(port, &addr6);
691 * This is the wrapper function for in_setsockaddr. We just pass down
692 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
693 * here because in_setsockaddr will call malloc and can block.
696 tcp_sockaddr(struct socket *so, struct sockaddr **nam)
698 return (in_setsockaddr(so, nam, &tcbinfo));
702 * This is the wrapper function for in_setpeeraddr. We just pass down
703 * the pcbinfo for in_setpeeraddr to lock.
706 tcp_peeraddr(struct socket *so, struct sockaddr **nam)
708 return (in_setpeeraddr(so, nam, &tcbinfo));
712 * Mark the connection as being incapable of further output.
715 tcp_usr_shutdown(struct socket *so)
719 struct tcpcb *tp = NULL;
722 INP_INFO_WLOCK(&tcbinfo);
724 KASSERT(inp != NULL, ("inp == NULL"));
726 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
734 error = tcp_output(tp);
737 TCPDEBUG2(PRU_SHUTDOWN);
739 INP_INFO_WUNLOCK(&tcbinfo);
745 * After a receive, possibly send window update to peer.
748 tcp_usr_rcvd(struct socket *so, int flags)
751 struct tcpcb *tp = NULL;
756 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
758 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
773 * Do a send by putting data in output queue and updating urgent
774 * marker if URG set. Possibly send more data. Unlike the other
775 * pru_*() routines, the mbuf chains are our responsibility. We
776 * must either enqueue them or free them. The other pru_* routines
777 * generally are caller-frees.
780 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
781 struct sockaddr *nam, struct mbuf *control, struct thread *td)
785 struct tcpcb *tp = NULL;
793 * We require the pcbinfo lock in two cases:
795 * (1) An implied connect is taking place, which can result in
796 * binding IPs and ports and hence modification of the pcb hash
799 * (2) PRUS_EOF is set, resulting in explicit close on the send.
801 if ((nam != NULL) || (flags & PRUS_EOF)) {
802 INP_INFO_WLOCK(&tcbinfo);
806 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
808 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
813 isipv6 = nam && nam->sa_family == AF_INET6;
818 /* TCP doesn't do control messages (rights, creds, etc) */
819 if (control->m_len) {
826 m_freem(control); /* empty control, just free it */
828 if (!(flags & PRUS_OOB)) {
829 sbappendstream(&so->so_snd, m);
830 if (nam && tp->t_state < TCPS_SYN_SENT) {
832 * Do implied connect if not yet connected,
833 * initialize window to default value, and
834 * initialize maxseg/maxopd using peer's cached
837 INP_INFO_WLOCK_ASSERT(&tcbinfo);
840 error = tcp6_connect(tp, nam, td);
843 error = tcp_connect(tp, nam, td);
846 tp->snd_wnd = TTCP_CLIENT_SND_WND;
849 if (flags & PRUS_EOF) {
851 * Close the send side of the connection after
854 INP_INFO_WLOCK_ASSERT(&tcbinfo);
859 INP_INFO_WUNLOCK(&tcbinfo);
863 if (flags & PRUS_MORETOCOME)
864 tp->t_flags |= TF_MORETOCOME;
865 error = tcp_output(tp);
866 if (flags & PRUS_MORETOCOME)
867 tp->t_flags &= ~TF_MORETOCOME;
871 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
873 SOCKBUF_LOCK(&so->so_snd);
874 if (sbspace(&so->so_snd) < -512) {
875 SOCKBUF_UNLOCK(&so->so_snd);
881 * According to RFC961 (Assigned Protocols),
882 * the urgent pointer points to the last octet
883 * of urgent data. We continue, however,
884 * to consider it to indicate the first octet
885 * of data past the urgent section.
886 * Otherwise, snd_up should be one lower.
888 sbappendstream_locked(&so->so_snd, m);
889 SOCKBUF_UNLOCK(&so->so_snd);
890 if (nam && tp->t_state < TCPS_SYN_SENT) {
892 * Do implied connect if not yet connected,
893 * initialize window to default value, and
894 * initialize maxseg/maxopd using peer's cached
897 INP_INFO_WLOCK_ASSERT(&tcbinfo);
900 error = tcp6_connect(tp, nam, td);
903 error = tcp_connect(tp, nam, td);
906 tp->snd_wnd = TTCP_CLIENT_SND_WND;
908 INP_INFO_WUNLOCK(&tcbinfo);
911 INP_INFO_WUNLOCK(&tcbinfo);
914 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
915 tp->t_flags |= TF_FORCEDATA;
916 error = tcp_output(tp);
917 tp->t_flags &= ~TF_FORCEDATA;
920 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
921 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
924 INP_INFO_WUNLOCK(&tcbinfo);
931 * First, drop the connection. Then collect state if possible.
934 tcp_usr_abort(struct socket *so)
941 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
943 INP_INFO_WLOCK(&tcbinfo);
945 KASSERT(inp->inp_socket != NULL,
946 ("tcp_usr_abort: inp_socket == NULL"));
950 * First, if we still have full TCP state, and we're not dropped,
953 if (!(inp->inp_vflag & INP_TIMEWAIT) &&
954 !(inp->inp_vflag & INP_DROPPED)) {
956 tcp_drop(tp, ECONNABORTED);
960 TCPDEBUG2(PRU_DETACH);
961 INP_INFO_WUNLOCK(&tcbinfo);
965 * Receive out-of-band data.
968 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
972 struct tcpcb *tp = NULL;
976 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
978 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
984 if ((so->so_oobmark == 0 &&
985 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
986 so->so_options & SO_OOBINLINE ||
987 tp->t_oobflags & TCPOOB_HADDATA) {
991 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
996 *mtod(m, caddr_t) = tp->t_iobc;
997 if ((flags & MSG_PEEK) == 0)
998 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1001 TCPDEBUG2(PRU_RCVOOB);
1006 struct pr_usrreqs tcp_usrreqs = {
1007 .pru_abort = tcp_usr_abort,
1008 .pru_accept = tcp_usr_accept,
1009 .pru_attach = tcp_usr_attach,
1010 .pru_bind = tcp_usr_bind,
1011 .pru_connect = tcp_usr_connect,
1012 .pru_control = in_control,
1013 .pru_detach = tcp_usr_detach,
1014 .pru_disconnect = tcp_usr_disconnect,
1015 .pru_listen = tcp_usr_listen,
1016 .pru_peeraddr = tcp_peeraddr,
1017 .pru_rcvd = tcp_usr_rcvd,
1018 .pru_rcvoob = tcp_usr_rcvoob,
1019 .pru_send = tcp_usr_send,
1020 .pru_shutdown = tcp_usr_shutdown,
1021 .pru_sockaddr = tcp_sockaddr,
1022 .pru_sosetlabel = in_pcbsosetlabel
1026 struct pr_usrreqs tcp6_usrreqs = {
1027 .pru_abort = tcp_usr_abort,
1028 .pru_accept = tcp6_usr_accept,
1029 .pru_attach = tcp_usr_attach,
1030 .pru_bind = tcp6_usr_bind,
1031 .pru_connect = tcp6_usr_connect,
1032 .pru_control = in6_control,
1033 .pru_detach = tcp_usr_detach,
1034 .pru_disconnect = tcp_usr_disconnect,
1035 .pru_listen = tcp6_usr_listen,
1036 .pru_peeraddr = in6_mapped_peeraddr,
1037 .pru_rcvd = tcp_usr_rcvd,
1038 .pru_rcvoob = tcp_usr_rcvoob,
1039 .pru_send = tcp_usr_send,
1040 .pru_shutdown = tcp_usr_shutdown,
1041 .pru_sockaddr = in6_mapped_sockaddr,
1042 .pru_sosetlabel = in_pcbsosetlabel
1047 * Common subroutine to open a TCP connection to remote host specified
1048 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1049 * port number if needed. Call in_pcbconnect_setup to do the routing and
1050 * to choose a local host address (interface). If there is an existing
1051 * incarnation of the same connection in TIME-WAIT state and if the remote
1052 * host was sending CC options and if the connection duration was < MSL, then
1053 * truncate the previous TIME-WAIT state and proceed.
1054 * Initialize connection parameters and enter SYN-SENT state.
1057 tcp_connect(tp, nam, td)
1058 register struct tcpcb *tp;
1059 struct sockaddr *nam;
1062 struct inpcb *inp = tp->t_inpcb, *oinp;
1063 struct socket *so = inp->inp_socket;
1064 struct in_addr laddr;
1068 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1069 INP_LOCK_ASSERT(inp);
1071 if (inp->inp_lport == 0) {
1072 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1078 * Cannot simply call in_pcbconnect, because there might be an
1079 * earlier incarnation of this same connection still in
1080 * TIME_WAIT state, creating an ADDRINUSE error.
1082 laddr = inp->inp_laddr;
1083 lport = inp->inp_lport;
1084 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1085 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1086 if (error && oinp == NULL)
1090 inp->inp_laddr = laddr;
1093 /* Compute window scaling to request. */
1094 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1095 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1096 tp->request_r_scale++;
1099 tcpstat.tcps_connattempt++;
1100 tp->t_state = TCPS_SYN_SENT;
1101 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1102 tp->iss = tcp_new_isn(tp);
1103 tp->t_bw_rtseq = tp->iss;
1104 tcp_sendseqinit(tp);
1111 tcp6_connect(tp, nam, td)
1112 register struct tcpcb *tp;
1113 struct sockaddr *nam;
1116 struct inpcb *inp = tp->t_inpcb, *oinp;
1117 struct socket *so = inp->inp_socket;
1118 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1119 struct in6_addr *addr6;
1122 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1123 INP_LOCK_ASSERT(inp);
1125 if (inp->inp_lport == 0) {
1126 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1132 * Cannot simply call in_pcbconnect, because there might be an
1133 * earlier incarnation of this same connection still in
1134 * TIME_WAIT state, creating an ADDRINUSE error.
1135 * in6_pcbladdr() also handles scope zone IDs.
1137 error = in6_pcbladdr(inp, nam, &addr6);
1140 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1141 &sin6->sin6_addr, sin6->sin6_port,
1142 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1145 inp->inp_lport, 0, NULL);
1148 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1149 inp->in6p_laddr = *addr6;
1150 inp->in6p_faddr = sin6->sin6_addr;
1151 inp->inp_fport = sin6->sin6_port;
1152 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1153 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
1154 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
1155 inp->in6p_flowinfo |=
1156 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1159 /* Compute window scaling to request. */
1160 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1161 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1162 tp->request_r_scale++;
1165 tcpstat.tcps_connattempt++;
1166 tp->t_state = TCPS_SYN_SENT;
1167 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1168 tp->iss = tcp_new_isn(tp);
1169 tp->t_bw_rtseq = tp->iss;
1170 tcp_sendseqinit(tp);
1177 * Export TCP internal state information via a struct tcp_info, based on the
1178 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1179 * (TCP state machine, etc). We export all information using FreeBSD-native
1180 * constants -- for example, the numeric values for tcpi_state will differ
1184 tcp_fill_info(tp, ti)
1186 struct tcp_info *ti;
1189 INP_LOCK_ASSERT(tp->t_inpcb);
1190 bzero(ti, sizeof(*ti));
1192 ti->tcpi_state = tp->t_state;
1193 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1194 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1195 if (tp->sack_enable)
1196 ti->tcpi_options |= TCPI_OPT_SACK;
1197 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1198 ti->tcpi_options |= TCPI_OPT_WSCALE;
1199 ti->tcpi_snd_wscale = tp->snd_scale;
1200 ti->tcpi_rcv_wscale = tp->rcv_scale;
1202 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1203 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1206 * FreeBSD-specific extension fields for tcp_info.
1208 ti->tcpi_rcv_space = tp->rcv_wnd;
1209 ti->tcpi_snd_wnd = tp->snd_wnd;
1210 ti->tcpi_snd_bwnd = tp->snd_bwnd;
1214 * The new sockopt interface makes it possible for us to block in the
1215 * copyin/out step (if we take a page fault). Taking a page fault at
1216 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1217 * use TSM, there probably isn't any need for this function to run at
1218 * splnet() any more. This needs more examination.)
1220 * XXXRW: The locking here is wrong; we may take a page fault while holding
1224 tcp_ctloutput(so, sopt)
1226 struct sockopt *sopt;
1228 int error, opt, optval;
1234 inp = sotoinpcb(so);
1235 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1237 if (sopt->sopt_level != IPPROTO_TCP) {
1240 if (INP_CHECK_SOCKAF(so, AF_INET6))
1241 error = ip6_ctloutput(so, sopt);
1244 error = ip_ctloutput(so, sopt);
1247 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1251 tp = intotcpcb(inp);
1253 switch (sopt->sopt_dir) {
1255 switch (sopt->sopt_name) {
1256 #ifdef TCP_SIGNATURE
1258 error = sooptcopyin(sopt, &optval, sizeof optval,
1264 tp->t_flags |= TF_SIGNATURE;
1266 tp->t_flags &= ~TF_SIGNATURE;
1268 #endif /* TCP_SIGNATURE */
1271 error = sooptcopyin(sopt, &optval, sizeof optval,
1276 switch (sopt->sopt_name) {
1284 opt = 0; /* dead code to fool gcc */
1291 tp->t_flags &= ~opt;
1295 error = sooptcopyin(sopt, &optval, sizeof optval,
1301 tp->t_flags |= TF_NOPUSH;
1303 tp->t_flags &= ~TF_NOPUSH;
1304 error = tcp_output(tp);
1309 error = sooptcopyin(sopt, &optval, sizeof optval,
1314 if (optval > 0 && optval <= tp->t_maxseg &&
1315 optval + 40 >= tcp_minmss)
1316 tp->t_maxseg = optval;
1326 error = ENOPROTOOPT;
1332 switch (sopt->sopt_name) {
1333 #ifdef TCP_SIGNATURE
1335 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1336 error = sooptcopyout(sopt, &optval, sizeof optval);
1340 optval = tp->t_flags & TF_NODELAY;
1341 error = sooptcopyout(sopt, &optval, sizeof optval);
1344 optval = tp->t_maxseg;
1345 error = sooptcopyout(sopt, &optval, sizeof optval);
1348 optval = tp->t_flags & TF_NOOPT;
1349 error = sooptcopyout(sopt, &optval, sizeof optval);
1352 optval = tp->t_flags & TF_NOPUSH;
1353 error = sooptcopyout(sopt, &optval, sizeof optval);
1356 tcp_fill_info(tp, &ti);
1357 error = sooptcopyout(sopt, &ti, sizeof ti);
1360 error = ENOPROTOOPT;
1371 * tcp_sendspace and tcp_recvspace are the default send and receive window
1372 * sizes, respectively. These are obsolescent (this information should
1373 * be set by the route).
1375 u_long tcp_sendspace = 1024*32;
1376 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1377 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1378 u_long tcp_recvspace = 1024*64;
1379 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1380 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1383 * Attach TCP protocol to socket, allocating
1384 * internet protocol control block, tcp control block,
1385 * bufer space, and entering LISTEN state if to accept connections.
1391 register struct tcpcb *tp;
1395 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1398 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1399 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1403 INP_INFO_WLOCK(&tcbinfo);
1404 error = in_pcballoc(so, &tcbinfo);
1406 INP_INFO_WUNLOCK(&tcbinfo);
1409 inp = sotoinpcb(so);
1412 inp->inp_vflag |= INP_IPV6;
1413 inp->in6p_hops = -1; /* use kernel default */
1417 inp->inp_vflag |= INP_IPV4;
1418 tp = tcp_newtcpcb(inp);
1431 INP_INFO_WUNLOCK(&tcbinfo);
1434 tp->t_state = TCPS_CLOSED;
1436 INP_INFO_WUNLOCK(&tcbinfo);
1441 * Initiate (or continue) disconnect.
1442 * If embryonic state, just send reset (once).
1443 * If in ``let data drain'' option and linger null, just drop.
1444 * Otherwise (hard), mark socket disconnecting and drop
1445 * current input data; switch states based on user close, and
1446 * send segment to peer (with FIN).
1450 register struct tcpcb *tp;
1452 struct inpcb *inp = tp->t_inpcb;
1453 struct socket *so = inp->inp_socket;
1455 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1456 INP_LOCK_ASSERT(inp);
1459 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1460 * socket is still open.
1462 if (tp->t_state < TCPS_ESTABLISHED) {
1465 ("tcp_disconnect: tcp_close() returned NULL"));
1466 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1467 tp = tcp_drop(tp, 0);
1469 ("tcp_disconnect: tcp_drop() returned NULL"));
1471 soisdisconnecting(so);
1472 sbflush(&so->so_rcv);
1474 if (!(inp->inp_vflag & INP_DROPPED))
1480 * User issued close, and wish to trail through shutdown states:
1481 * if never received SYN, just forget it. If got a SYN from peer,
1482 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1483 * If already got a FIN from peer, then almost done; go to LAST_ACK
1484 * state. In all other cases, have already sent FIN to peer (e.g.
1485 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1486 * for peer to send FIN or not respond to keep-alives, etc.
1487 * We can let the user exit from the close as soon as the FIN is acked.
1491 register struct tcpcb *tp;
1494 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1495 INP_LOCK_ASSERT(tp->t_inpcb);
1497 switch (tp->t_state) {
1501 tp->t_state = TCPS_CLOSED;
1504 * tcp_close() should never return NULL here as the socket is
1508 ("tcp_usrclosed: tcp_close() returned NULL"));
1512 case TCPS_SYN_RECEIVED:
1513 tp->t_flags |= TF_NEEDFIN;
1516 case TCPS_ESTABLISHED:
1517 tp->t_state = TCPS_FIN_WAIT_1;
1520 case TCPS_CLOSE_WAIT:
1521 tp->t_state = TCPS_LAST_ACK;
1524 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1525 soisdisconnected(tp->t_inpcb->inp_socket);
1526 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1527 if (tp->t_state == TCPS_FIN_WAIT_2)
1528 callout_reset(tp->tt_2msl, tcp_maxidle,
1529 tcp_timer_2msl, tp);