2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
34 #include "opt_inet6.h"
35 #include "opt_tcpdebug.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
44 #include <sys/domain.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/protosw.h>
53 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
58 #include <netinet/ip6.h>
60 #include <netinet/in_pcb.h>
62 #include <netinet6/in6_pcb.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip_var.h>
67 #include <netinet6/ip6_var.h>
68 #include <netinet6/scope6_var.h>
70 #include <netinet/tcp.h>
71 #include <netinet/tcp_fsm.h>
72 #include <netinet/tcp_seq.h>
73 #include <netinet/tcp_timer.h>
74 #include <netinet/tcp_var.h>
75 #include <netinet/tcpip.h>
77 #include <netinet/tcp_debug.h>
81 * TCP protocol interface to socket abstraction.
83 extern char *tcpstates[]; /* XXX ??? */
85 static int tcp_attach(struct socket *);
86 static int tcp_connect(struct tcpcb *, struct sockaddr *,
89 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
93 tcp_disconnect(struct tcpcb *);
95 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)
118 struct tcpcb *tp = 0;
121 INP_INFO_WLOCK(&tcbinfo);
129 error = tcp_attach(so);
133 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
134 so->so_linger = TCP_LINGERTIME;
139 TCPDEBUG2(PRU_ATTACH);
140 INP_INFO_WUNLOCK(&tcbinfo);
145 * pru_detach() detaches the TCP protocol from the socket.
146 * If the protocol state is non-embryonic, then can't
147 * do this directly: have to initiate a pru_disconnect(),
148 * which may finish later; embryonic TCB's can just
152 tcp_usr_detach(struct socket *so)
159 INP_INFO_WLOCK(&tcbinfo);
162 INP_INFO_WUNLOCK(&tcbinfo);
168 tp = tcp_disconnect(tp);
170 TCPDEBUG2(PRU_DETACH);
173 INP_INFO_WUNLOCK(&tcbinfo);
181 #define COMMON_START() \
184 if (inirw == INI_READ) \
185 INP_INFO_RLOCK(&tcbinfo); \
186 else if (inirw == INI_WRITE) \
187 INP_INFO_WLOCK(&tcbinfo); \
188 inp = sotoinpcb(so); \
190 if (inirw == INI_READ) \
191 INP_INFO_RUNLOCK(&tcbinfo); \
192 else if (inirw == INI_WRITE) \
193 INP_INFO_WUNLOCK(&tcbinfo); \
197 if (inirw == INI_READ) \
198 INP_INFO_RUNLOCK(&tcbinfo); \
199 tp = intotcpcb(inp); \
203 #define COMMON_END(req) \
204 out: TCPDEBUG2(req); \
208 if (inirw == INI_WRITE) \
209 INP_INFO_WUNLOCK(&tcbinfo); \
215 * Give the socket an address.
218 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
223 struct sockaddr_in *sinp;
224 const int inirw = INI_WRITE;
226 sinp = (struct sockaddr_in *)nam;
227 if (nam->sa_len != sizeof (*sinp))
230 * Must check for multicast addresses and disallow binding
233 if (sinp->sin_family == AF_INET &&
234 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
235 return (EAFNOSUPPORT);
238 error = in_pcbbind(inp, nam, td->td_ucred);
241 COMMON_END(PRU_BIND);
246 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
251 struct sockaddr_in6 *sin6p;
252 const int inirw = INI_WRITE;
254 sin6p = (struct sockaddr_in6 *)nam;
255 if (nam->sa_len != sizeof (*sin6p))
258 * Must check for multicast addresses and disallow binding
261 if (sin6p->sin6_family == AF_INET6 &&
262 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
263 return (EAFNOSUPPORT);
266 inp->inp_vflag &= ~INP_IPV4;
267 inp->inp_vflag |= INP_IPV6;
268 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
269 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
270 inp->inp_vflag |= INP_IPV4;
271 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
272 struct sockaddr_in sin;
274 in6_sin6_2_sin(&sin, sin6p);
275 inp->inp_vflag |= INP_IPV4;
276 inp->inp_vflag &= ~INP_IPV6;
277 error = in_pcbbind(inp, (struct sockaddr *)&sin,
282 error = in6_pcbbind(inp, nam, td->td_ucred);
285 COMMON_END(PRU_BIND);
290 * Prepare to accept connections.
293 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
298 const int inirw = INI_WRITE;
302 error = solisten_proto_check(so);
303 if (error == 0 && inp->inp_lport == 0)
304 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
306 tp->t_state = TCPS_LISTEN;
307 solisten_proto(so, backlog);
310 COMMON_END(PRU_LISTEN);
315 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
320 const int inirw = INI_WRITE;
324 error = solisten_proto_check(so);
325 if (error == 0 && inp->inp_lport == 0) {
326 inp->inp_vflag &= ~INP_IPV4;
327 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
328 inp->inp_vflag |= INP_IPV4;
329 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
332 tp->t_state = TCPS_LISTEN;
333 solisten_proto(so, backlog);
336 COMMON_END(PRU_LISTEN);
341 * Initiate connection to peer.
342 * Create a template for use in transmissions on this connection.
343 * Enter SYN_SENT state, and mark socket as connecting.
344 * Start keep-alive timer, and seed output sequence space.
345 * Send initial segment on connection.
348 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
353 struct sockaddr_in *sinp;
354 const int inirw = INI_WRITE;
356 sinp = (struct sockaddr_in *)nam;
357 if (nam->sa_len != sizeof (*sinp))
360 * Must disallow TCP ``connections'' to multicast addresses.
362 if (sinp->sin_family == AF_INET
363 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
364 return (EAFNOSUPPORT);
365 if (jailed(td->td_ucred))
366 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
369 if ((error = tcp_connect(tp, nam, td)) != 0)
371 error = tcp_output(tp);
372 COMMON_END(PRU_CONNECT);
377 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
382 struct sockaddr_in6 *sin6p;
383 const int inirw = INI_WRITE;
385 sin6p = (struct sockaddr_in6 *)nam;
386 if (nam->sa_len != sizeof (*sin6p))
389 * Must disallow TCP ``connections'' to multicast addresses.
391 if (sin6p->sin6_family == AF_INET6
392 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
393 return (EAFNOSUPPORT);
396 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
397 struct sockaddr_in sin;
399 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
404 in6_sin6_2_sin(&sin, sin6p);
405 inp->inp_vflag |= INP_IPV4;
406 inp->inp_vflag &= ~INP_IPV6;
407 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
409 error = tcp_output(tp);
412 inp->inp_vflag &= ~INP_IPV4;
413 inp->inp_vflag |= INP_IPV6;
414 inp->inp_inc.inc_isipv6 = 1;
415 if ((error = tcp6_connect(tp, nam, td)) != 0)
417 error = tcp_output(tp);
418 COMMON_END(PRU_CONNECT);
423 * Initiate disconnect from peer.
424 * If connection never passed embryonic stage, just drop;
425 * else if don't need to let data drain, then can just drop anyways,
426 * else have to begin TCP shutdown process: mark socket disconnecting,
427 * drain unread data, state switch to reflect user close, and
428 * send segment (e.g. FIN) to peer. Socket will be really disconnected
429 * when peer sends FIN and acks ours.
431 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
434 tcp_usr_disconnect(struct socket *so)
439 const int inirw = INI_WRITE;
442 tp = tcp_disconnect(tp);
443 COMMON_END(PRU_DISCONNECT);
447 * Accept a connection. Essentially all the work is
448 * done at higher levels; just return the address
449 * of the peer, storing through addr.
452 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
455 struct inpcb *inp = NULL;
456 struct tcpcb *tp = NULL;
461 if (so->so_state & SS_ISDISCONNECTED) {
462 error = ECONNABORTED;
466 INP_INFO_RLOCK(&tcbinfo);
469 INP_INFO_RUNLOCK(&tcbinfo);
473 INP_INFO_RUNLOCK(&tcbinfo);
478 * We inline in_setpeeraddr and COMMON_END here, so that we can
479 * copy the data of interest and defer the malloc until after we
482 port = inp->inp_fport;
483 addr = inp->inp_faddr;
485 out: TCPDEBUG2(PRU_ACCEPT);
489 *nam = in_sockaddr(port, &addr);
495 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
497 struct inpcb *inp = NULL;
499 struct tcpcb *tp = NULL;
501 struct in6_addr addr6;
506 if (so->so_state & SS_ISDISCONNECTED) {
507 error = ECONNABORTED;
511 INP_INFO_RLOCK(&tcbinfo);
514 INP_INFO_RUNLOCK(&tcbinfo);
518 INP_INFO_RUNLOCK(&tcbinfo);
522 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
523 * copy the data of interest and defer the malloc until after we
526 if (inp->inp_vflag & INP_IPV4) {
528 port = inp->inp_fport;
529 addr = inp->inp_faddr;
531 port = inp->inp_fport;
532 addr6 = inp->in6p_faddr;
535 out: TCPDEBUG2(PRU_ACCEPT);
540 *nam = in6_v4mapsin6_sockaddr(port, &addr);
542 *nam = in6_sockaddr(port, &addr6);
549 * This is the wrapper function for in_setsockaddr. We just pass down
550 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
551 * here because in_setsockaddr will call malloc and can block.
554 tcp_sockaddr(struct socket *so, struct sockaddr **nam)
556 return (in_setsockaddr(so, nam, &tcbinfo));
560 * This is the wrapper function for in_setpeeraddr. We just pass down
561 * the pcbinfo for in_setpeeraddr to lock.
564 tcp_peeraddr(struct socket *so, struct sockaddr **nam)
566 return (in_setpeeraddr(so, nam, &tcbinfo));
570 * Mark the connection as being incapable of further output.
573 tcp_usr_shutdown(struct socket *so)
578 const int inirw = INI_WRITE;
582 tp = tcp_usrclosed(tp);
584 error = tcp_output(tp);
585 COMMON_END(PRU_SHUTDOWN);
589 * After a receive, possibly send window update to peer.
592 tcp_usr_rcvd(struct socket *so, int flags)
597 const int inirw = INI_READ;
601 COMMON_END(PRU_RCVD);
605 * Do a send by putting data in output queue and updating urgent
606 * marker if URG set. Possibly send more data. Unlike the other
607 * pru_*() routines, the mbuf chains are our responsibility. We
608 * must either enqueue them or free them. The other pru_* routines
609 * generally are caller-frees.
612 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
613 struct sockaddr *nam, struct mbuf *control, struct thread *td)
625 * Need write lock here because this function might call
626 * tcp_connect or tcp_usrclosed.
627 * We really want to have to this function upgrade from read lock
630 INP_INFO_WLOCK(&tcbinfo);
634 * OOPS! we lost a race, the TCP session got reset after
635 * we checked SBS_CANTSENDMORE, eg: while doing uiomove or a
636 * network interrupt in the non-splnet() section of sosend().
642 error = ECONNRESET; /* XXX EPIPE? */
649 isipv6 = nam && nam->sa_family == AF_INET6;
654 /* TCP doesn't do control messages (rights, creds, etc) */
655 if (control->m_len) {
662 m_freem(control); /* empty control, just free it */
664 if (!(flags & PRUS_OOB)) {
665 sbappendstream(&so->so_snd, m);
666 if (nam && tp->t_state < TCPS_SYN_SENT) {
668 * Do implied connect if not yet connected,
669 * initialize window to default value, and
670 * initialize maxseg/maxopd using peer's cached
675 error = tcp6_connect(tp, nam, td);
678 error = tcp_connect(tp, nam, td);
681 tp->snd_wnd = TTCP_CLIENT_SND_WND;
685 if (flags & PRUS_EOF) {
687 * Close the send side of the connection after
691 tp = tcp_usrclosed(tp);
693 INP_INFO_WUNLOCK(&tcbinfo);
696 if (flags & PRUS_MORETOCOME)
697 tp->t_flags |= TF_MORETOCOME;
698 error = tcp_output(tp);
699 if (flags & PRUS_MORETOCOME)
700 tp->t_flags &= ~TF_MORETOCOME;
703 SOCKBUF_LOCK(&so->so_snd);
704 if (sbspace(&so->so_snd) < -512) {
705 SOCKBUF_UNLOCK(&so->so_snd);
711 * According to RFC961 (Assigned Protocols),
712 * the urgent pointer points to the last octet
713 * of urgent data. We continue, however,
714 * to consider it to indicate the first octet
715 * of data past the urgent section.
716 * Otherwise, snd_up should be one lower.
718 sbappendstream_locked(&so->so_snd, m);
719 SOCKBUF_UNLOCK(&so->so_snd);
720 if (nam && tp->t_state < TCPS_SYN_SENT) {
722 * Do implied connect if not yet connected,
723 * initialize window to default value, and
724 * initialize maxseg/maxopd using peer's cached
729 error = tcp6_connect(tp, nam, td);
732 error = tcp_connect(tp, nam, td);
735 tp->snd_wnd = TTCP_CLIENT_SND_WND;
738 INP_INFO_WUNLOCK(&tcbinfo);
740 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
741 tp->t_flags |= TF_FORCEDATA;
742 error = tcp_output(tp);
743 tp->t_flags &= ~TF_FORCEDATA;
746 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
747 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
751 INP_INFO_WUNLOCK(&tcbinfo);
759 tcp_usr_abort(struct socket *so)
764 const int inirw = INI_WRITE;
767 tp = tcp_drop(tp, ECONNABORTED);
768 COMMON_END(PRU_ABORT);
772 * Receive out-of-band data.
775 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
780 const int inirw = INI_READ;
783 if ((so->so_oobmark == 0 &&
784 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
785 so->so_options & SO_OOBINLINE ||
786 tp->t_oobflags & TCPOOB_HADDATA) {
790 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
795 *mtod(m, caddr_t) = tp->t_iobc;
796 if ((flags & MSG_PEEK) == 0)
797 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
798 COMMON_END(PRU_RCVOOB);
801 struct pr_usrreqs tcp_usrreqs = {
802 .pru_abort = tcp_usr_abort,
803 .pru_accept = tcp_usr_accept,
804 .pru_attach = tcp_usr_attach,
805 .pru_bind = tcp_usr_bind,
806 .pru_connect = tcp_usr_connect,
807 .pru_control = in_control,
808 .pru_detach = tcp_usr_detach,
809 .pru_disconnect = tcp_usr_disconnect,
810 .pru_listen = tcp_usr_listen,
811 .pru_peeraddr = tcp_peeraddr,
812 .pru_rcvd = tcp_usr_rcvd,
813 .pru_rcvoob = tcp_usr_rcvoob,
814 .pru_send = tcp_usr_send,
815 .pru_shutdown = tcp_usr_shutdown,
816 .pru_sockaddr = tcp_sockaddr,
817 .pru_sosetlabel = in_pcbsosetlabel
821 struct pr_usrreqs tcp6_usrreqs = {
822 .pru_abort = tcp_usr_abort,
823 .pru_accept = tcp6_usr_accept,
824 .pru_attach = tcp_usr_attach,
825 .pru_bind = tcp6_usr_bind,
826 .pru_connect = tcp6_usr_connect,
827 .pru_control = in6_control,
828 .pru_detach = tcp_usr_detach,
829 .pru_disconnect = tcp_usr_disconnect,
830 .pru_listen = tcp6_usr_listen,
831 .pru_peeraddr = in6_mapped_peeraddr,
832 .pru_rcvd = tcp_usr_rcvd,
833 .pru_rcvoob = tcp_usr_rcvoob,
834 .pru_send = tcp_usr_send,
835 .pru_shutdown = tcp_usr_shutdown,
836 .pru_sockaddr = in6_mapped_sockaddr,
837 .pru_sosetlabel = in_pcbsosetlabel
842 * Common subroutine to open a TCP connection to remote host specified
843 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
844 * port number if needed. Call in_pcbconnect_setup to do the routing and
845 * to choose a local host address (interface). If there is an existing
846 * incarnation of the same connection in TIME-WAIT state and if the remote
847 * host was sending CC options and if the connection duration was < MSL, then
848 * truncate the previous TIME-WAIT state and proceed.
849 * Initialize connection parameters and enter SYN-SENT state.
852 tcp_connect(tp, nam, td)
853 register struct tcpcb *tp;
854 struct sockaddr *nam;
857 struct inpcb *inp = tp->t_inpcb, *oinp;
858 struct socket *so = inp->inp_socket;
859 struct in_addr laddr;
863 if (inp->inp_lport == 0) {
864 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
870 * Cannot simply call in_pcbconnect, because there might be an
871 * earlier incarnation of this same connection still in
872 * TIME_WAIT state, creating an ADDRINUSE error.
874 laddr = inp->inp_laddr;
875 lport = inp->inp_lport;
876 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
877 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
878 if (error && oinp == NULL)
882 inp->inp_laddr = laddr;
885 /* Compute window scaling to request. */
886 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
887 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
888 tp->request_r_scale++;
891 tcpstat.tcps_connattempt++;
892 tp->t_state = TCPS_SYN_SENT;
893 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
894 tp->iss = tcp_new_isn(tp);
895 tp->t_bw_rtseq = tp->iss;
903 tcp6_connect(tp, nam, td)
904 register struct tcpcb *tp;
905 struct sockaddr *nam;
908 struct inpcb *inp = tp->t_inpcb, *oinp;
909 struct socket *so = inp->inp_socket;
910 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
911 struct in6_addr *addr6;
914 if (inp->inp_lport == 0) {
915 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
921 * Cannot simply call in_pcbconnect, because there might be an
922 * earlier incarnation of this same connection still in
923 * TIME_WAIT state, creating an ADDRINUSE error.
924 * in6_pcbladdr() also handles scope zone IDs.
926 error = in6_pcbladdr(inp, nam, &addr6);
929 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
930 &sin6->sin6_addr, sin6->sin6_port,
931 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
934 inp->inp_lport, 0, NULL);
937 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
938 inp->in6p_laddr = *addr6;
939 inp->in6p_faddr = sin6->sin6_addr;
940 inp->inp_fport = sin6->sin6_port;
941 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
942 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
943 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
944 inp->in6p_flowinfo |=
945 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
948 /* Compute window scaling to request. */
949 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
950 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
951 tp->request_r_scale++;
954 tcpstat.tcps_connattempt++;
955 tp->t_state = TCPS_SYN_SENT;
956 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
957 tp->iss = tcp_new_isn(tp);
958 tp->t_bw_rtseq = tp->iss;
966 * Export TCP internal state information via a struct tcp_info, based on the
967 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
968 * (TCP state machine, etc). We export all information using FreeBSD-native
969 * constants -- for example, the numeric values for tcpi_state will differ
973 tcp_fill_info(tp, ti)
978 INP_LOCK_ASSERT(tp->t_inpcb);
979 bzero(ti, sizeof(*ti));
981 ti->tcpi_state = tp->t_state;
982 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
983 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
985 ti->tcpi_options |= TCPI_OPT_SACK;
986 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
987 ti->tcpi_options |= TCPI_OPT_WSCALE;
988 ti->tcpi_snd_wscale = tp->snd_scale;
989 ti->tcpi_rcv_wscale = tp->rcv_scale;
991 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
992 ti->tcpi_snd_cwnd = tp->snd_cwnd;
995 * FreeBSD-specific extension fields for tcp_info.
997 ti->tcpi_rcv_space = tp->rcv_wnd;
998 ti->tcpi_snd_wnd = tp->snd_wnd;
999 ti->tcpi_snd_bwnd = tp->snd_bwnd;
1003 * The new sockopt interface makes it possible for us to block in the
1004 * copyin/out step (if we take a page fault). Taking a page fault at
1005 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1006 * use TSM, there probably isn't any need for this function to run at
1007 * splnet() any more. This needs more examination.)
1009 * XXXRW: The locking here is wrong; we may take a page fault while holding
1013 tcp_ctloutput(so, sopt)
1015 struct sockopt *sopt;
1017 int error, opt, optval;
1023 INP_INFO_RLOCK(&tcbinfo);
1024 inp = sotoinpcb(so);
1026 INP_INFO_RUNLOCK(&tcbinfo);
1027 return (ECONNRESET);
1030 INP_INFO_RUNLOCK(&tcbinfo);
1031 if (sopt->sopt_level != IPPROTO_TCP) {
1034 if (INP_CHECK_SOCKAF(so, AF_INET6))
1035 error = ip6_ctloutput(so, sopt);
1038 error = ip_ctloutput(so, sopt);
1041 tp = intotcpcb(inp);
1043 switch (sopt->sopt_dir) {
1045 switch (sopt->sopt_name) {
1046 #ifdef TCP_SIGNATURE
1048 error = sooptcopyin(sopt, &optval, sizeof optval,
1054 tp->t_flags |= TF_SIGNATURE;
1056 tp->t_flags &= ~TF_SIGNATURE;
1058 #endif /* TCP_SIGNATURE */
1061 error = sooptcopyin(sopt, &optval, sizeof optval,
1066 switch (sopt->sopt_name) {
1074 opt = 0; /* dead code to fool gcc */
1081 tp->t_flags &= ~opt;
1085 error = sooptcopyin(sopt, &optval, sizeof optval,
1091 tp->t_flags |= TF_NOPUSH;
1093 tp->t_flags &= ~TF_NOPUSH;
1094 error = tcp_output(tp);
1099 error = sooptcopyin(sopt, &optval, sizeof optval,
1104 if (optval > 0 && optval <= tp->t_maxseg &&
1105 optval + 40 >= tcp_minmss)
1106 tp->t_maxseg = optval;
1116 error = ENOPROTOOPT;
1122 switch (sopt->sopt_name) {
1123 #ifdef TCP_SIGNATURE
1125 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1126 error = sooptcopyout(sopt, &optval, sizeof optval);
1130 optval = tp->t_flags & TF_NODELAY;
1131 error = sooptcopyout(sopt, &optval, sizeof optval);
1134 optval = tp->t_maxseg;
1135 error = sooptcopyout(sopt, &optval, sizeof optval);
1138 optval = tp->t_flags & TF_NOOPT;
1139 error = sooptcopyout(sopt, &optval, sizeof optval);
1142 optval = tp->t_flags & TF_NOPUSH;
1143 error = sooptcopyout(sopt, &optval, sizeof optval);
1146 tcp_fill_info(tp, &ti);
1147 error = sooptcopyout(sopt, &ti, sizeof ti);
1150 error = ENOPROTOOPT;
1160 * tcp_sendspace and tcp_recvspace are the default send and receive window
1161 * sizes, respectively. These are obsolescent (this information should
1162 * be set by the route).
1164 u_long tcp_sendspace = 1024*32;
1165 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1166 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1167 u_long tcp_recvspace = 1024*64;
1168 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1169 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1172 * Attach TCP protocol to socket, allocating
1173 * internet protocol control block, tcp control block,
1174 * bufer space, and entering LISTEN state if to accept connections.
1180 register struct tcpcb *tp;
1184 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1187 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1189 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1190 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1194 error = in_pcballoc(so, &tcbinfo, "tcpinp");
1197 inp = sotoinpcb(so);
1200 inp->inp_vflag |= INP_IPV6;
1201 inp->in6p_hops = -1; /* use kernel default */
1205 inp->inp_vflag |= INP_IPV4;
1206 tp = tcp_newtcpcb(inp);
1208 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1210 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1219 so->so_state |= nofd;
1222 tp->t_state = TCPS_CLOSED;
1227 * Initiate (or continue) disconnect.
1228 * If embryonic state, just send reset (once).
1229 * If in ``let data drain'' option and linger null, just drop.
1230 * Otherwise (hard), mark socket disconnecting and drop
1231 * current input data; switch states based on user close, and
1232 * send segment to peer (with FIN).
1234 static struct tcpcb *
1236 register struct tcpcb *tp;
1238 struct inpcb *inp = tp->t_inpcb;
1239 struct socket *so = inp->inp_socket;
1241 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1242 INP_LOCK_ASSERT(inp);
1244 if (tp->t_state < TCPS_ESTABLISHED)
1246 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1247 tp = tcp_drop(tp, 0);
1249 soisdisconnecting(so);
1250 sbflush(&so->so_rcv);
1251 tp = tcp_usrclosed(tp);
1253 (void) tcp_output(tp);
1259 * User issued close, and wish to trail through shutdown states:
1260 * if never received SYN, just forget it. If got a SYN from peer,
1261 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1262 * If already got a FIN from peer, then almost done; go to LAST_ACK
1263 * state. In all other cases, have already sent FIN to peer (e.g.
1264 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1265 * for peer to send FIN or not respond to keep-alives, etc.
1266 * We can let the user exit from the close as soon as the FIN is acked.
1268 static struct tcpcb *
1270 register struct tcpcb *tp;
1273 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1274 INP_LOCK_ASSERT(tp->t_inpcb);
1276 switch (tp->t_state) {
1280 tp->t_state = TCPS_CLOSED;
1285 case TCPS_SYN_RECEIVED:
1286 tp->t_flags |= TF_NEEDFIN;
1289 case TCPS_ESTABLISHED:
1290 tp->t_state = TCPS_FIN_WAIT_1;
1293 case TCPS_CLOSE_WAIT:
1294 tp->t_state = TCPS_LAST_ACK;
1297 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1298 soisdisconnected(tp->t_inpcb->inp_socket);
1299 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1300 if (tp->t_state == TCPS_FIN_WAIT_2)
1301 callout_reset(tp->tt_2msl, tcp_maxidle,
1302 tcp_timer_2msl, tp);