X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/4bd07ac2140668789aa3ee8ec4dde4a3e0a3bba5..9d749ea394c01276fa19e397e70f46858e849c76:/bsd/netinet/tcp_usrreq.c diff --git a/bsd/netinet/tcp_usrreq.c b/bsd/netinet/tcp_usrreq.c index bfc86e994..1af338ade 100644 --- a/bsd/netinet/tcp_usrreq.c +++ b/bsd/netinet/tcp_usrreq.c @@ -1,8 +1,8 @@ /* - * Copyright (c) 2000-2014 Apple Inc. All rights reserved. + * Copyright (c) 2000-2017 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ - * + * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in @@ -11,10 +11,10 @@ * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. - * + * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. - * + * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, @@ -22,7 +22,7 @@ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. - * + * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* @@ -70,7 +70,9 @@ #if INET6 #include #endif /* INET6 */ +#if !CONFIG_EMBEDDED #include +#endif #include #include #include @@ -101,6 +103,7 @@ #include #include #include +#include #include #if TCPDEBUG #include @@ -117,7 +120,6 @@ #include #endif /* FLOW_DIVERT */ -void tcp_fill_info(struct tcpcb *, struct tcp_info *); errno_t tcp_fill_info_for_info_tuple(struct info_tuple *, struct tcp_info *); int tcp_sysctl_info(struct sysctl_oid *, void *, int , struct sysctl_req *); @@ -136,13 +138,8 @@ static int tcp6_connect(struct tcpcb *, struct sockaddr *, struct proc *); static int tcp6_usr_connect(struct socket *, struct sockaddr *, struct proc *); #endif /* INET6 */ -static struct tcpcb * - tcp_disconnect(struct tcpcb *); -static struct tcpcb * - tcp_usrclosed(struct tcpcb *); - -extern uint32_t tcp_autorcvbuf_max; - +static struct tcpcb *tcp_disconnect(struct tcpcb *); +static struct tcpcb *tcp_usrclosed(struct tcpcb *); extern void tcp_sbrcv_trim(struct tcpcb *tp, struct sockbuf *sb); #if TCPDEBUG @@ -183,7 +180,7 @@ tcp_usr_attach(struct socket *so, __unused int proto, struct proc *p) error = EISCONN; goto out; } - + error = tcp_attach(so, p); if (error) goto out; @@ -214,7 +211,7 @@ tcp_usr_detach(struct socket *so) if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) { return EINVAL; /* XXX */ } - lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED); + socket_lock_assert_owned(so); tp = intotcpcb(inp); /* In case we got disconnected from the peer */ if (tp == NULL) @@ -296,6 +293,17 @@ tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p) error = in_pcbbind(inp, nam, p); if (error) goto out; + +#if NECP + /* Update NECP client with bind result if not in middle of connect */ + if ((inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS) && + !uuid_is_null(inp->necp_client_uuid)) { + socket_unlock(so, 0); + necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp); + socket_lock(so, 0); + } +#endif /* NECP */ + COMMON_END(PRU_BIND); } @@ -401,6 +409,7 @@ static int tcp_connect_complete(struct socket *so) { struct tcpcb *tp = sototcpcb(so); + struct inpcb *inp = sotoinpcb(so); int error = 0; /* TFO delays the tcp_output until later, when the app calls write() */ @@ -415,6 +424,15 @@ tcp_connect_complete(struct socket *so) error = tcp_output(tp); } +#if NECP + /* Update NECP client with connected five-tuple */ + if (error == 0 && !uuid_is_null(inp->necp_client_uuid)) { + socket_unlock(so, 0); + necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp); + socket_lock(so, 0); + } +#endif /* NECP */ + return (error); } @@ -457,8 +475,6 @@ tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) error = ENETDOWN; } - /* Disable PRECONNECT_DATA, as we don't need to send a SYN anymore. */ - so->so_flags1 &= ~SOF1_PRECONNECT_DATA; return error; } #endif /* FLOW_DIVERT */ @@ -497,95 +513,53 @@ tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) static int tcp_usr_connectx_common(struct socket *so, int af, - struct sockaddr_list **src_sl, struct sockaddr_list **dst_sl, + struct sockaddr *src, struct sockaddr *dst, struct proc *p, uint32_t ifscope, sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg, uint32_t arglen, struct uio *auio, user_ssize_t *bytes_written) { -#pragma unused(aid) -#if !MPTCP -#pragma unused(flags, arg, arglen) -#endif /* !MPTCP */ - struct sockaddr_entry *src_se = NULL, *dst_se = NULL; +#pragma unused(aid, flags, arg, arglen) struct inpcb *inp = sotoinpcb(so); - int error; + int error = 0; user_ssize_t datalen = 0; if (inp == NULL) return (EINVAL); - VERIFY(dst_sl != NULL); - - /* select source (if specified) and destination addresses */ - error = in_selectaddrs(af, src_sl, &src_se, dst_sl, &dst_se); - if (error != 0) - return (error); + VERIFY(dst != NULL); - VERIFY(*dst_sl != NULL && dst_se != NULL); - VERIFY(src_se == NULL || *src_sl != NULL); - VERIFY(dst_se->se_addr->sa_family == af); - VERIFY(src_se == NULL || src_se->se_addr->sa_family == af); + ASSERT(!(inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS)); + inp->inp_flags2 |= INP2_CONNECT_IN_PROGRESS; #if NECP - inp_update_necp_policy(inp, src_se ? src_se->se_addr : NULL, dst_se ? dst_se->se_addr : NULL, ifscope); + inp_update_necp_policy(inp, src, dst, ifscope); #endif /* NECP */ if ((so->so_flags1 & SOF1_DATA_IDEMPOTENT) && (tcp_fastopen & TCP_FASTOPEN_CLIENT)) sototcpcb(so)->t_flagsext |= TF_FASTOPEN; - /* - * We get here for 2 cases: - * - * a. From MPTCP, to connect a subflow. There is no need to - * bind the socket to the source address and/or interface, - * since everything has been taken care of by MPTCP. We - * simply check whether or not this is for the initial - * MPTCP connection attempt, or to join an existing one. - * - * b. From the socket layer, to connect a TCP. Perform the - * bind to source address and/or interface as necessary. - */ -#if MPTCP - if (flags & CONNREQF_MPTCP) { - struct mptsub_connreq *mpcr = arg; - - /* Check to make sure this came down from MPTCP */ - if (arg == NULL || arglen != sizeof (*mpcr)) - return (EOPNOTSUPP); - - switch (mpcr->mpcr_type) { - case MPTSUB_CONNREQ_MP_ENABLE: - break; - case MPTSUB_CONNREQ_MP_ADD: - break; - default: - return (EOPNOTSUPP); - } - } else -#endif /* MPTCP */ - { - /* bind socket to the specified interface, if requested */ - if (ifscope != IFSCOPE_NONE && - (error = inp_bindif(inp, ifscope, NULL)) != 0) - return (error); + /* bind socket to the specified interface, if requested */ + if (ifscope != IFSCOPE_NONE && + (error = inp_bindif(inp, ifscope, NULL)) != 0) { + goto done; + } - /* if source address and/or port is specified, bind to it */ - if (src_se != NULL) { - struct sockaddr *sa = src_se->se_addr; - error = sobindlock(so, sa, 0); /* already locked */ - if (error != 0) - return (error); + /* if source address and/or port is specified, bind to it */ + if (src != NULL) { + error = sobindlock(so, src, 0); /* already locked */ + if (error != 0) { + goto done; } } switch (af) { case AF_INET: - error = tcp_usr_connect(so, dst_se->se_addr, p); + error = tcp_usr_connect(so, dst, p); break; #if INET6 case AF_INET6: - error = tcp6_usr_connect(so, dst_se->se_addr, p); + error = tcp6_usr_connect(so, dst, p); break; #endif /* INET6 */ default: @@ -593,8 +567,9 @@ tcp_usr_connectx_common(struct socket *so, int af, /* NOTREACHED */ } - if (error != 0) - return (error); + if (error != 0) { + goto done; + } /* if there is data, copy it */ if (auio != NULL) { @@ -625,18 +600,22 @@ tcp_usr_connectx_common(struct socket *so, int af, if (error == 0 && pcid != NULL) *pcid = 1; /* there is only one connection in regular TCP */ +done: + if (error && error != EINPROGRESS) + so->so_flags1 &= ~SOF1_PRECONNECT_DATA; + + inp->inp_flags2 &= ~INP2_CONNECT_IN_PROGRESS; return (error); } static int -tcp_usr_connectx(struct socket *so, struct sockaddr_list **src_sl, - struct sockaddr_list **dst_sl, struct proc *p, uint32_t ifscope, +tcp_usr_connectx(struct socket *so, struct sockaddr *src, + struct sockaddr *dst, struct proc *p, uint32_t ifscope, sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg, uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written) { - return (tcp_usr_connectx_common(so, AF_INET, src_sl, dst_sl, - p, ifscope, aid, pcid, flags, arg, arglen, uio, - bytes_written)); + return (tcp_usr_connectx_common(so, AF_INET, src, dst, p, ifscope, aid, + pcid, flags, arg, arglen, uio, bytes_written)); } #if INET6 @@ -672,8 +651,6 @@ tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) error = ENETDOWN; } - /* Disable PRECONNECT_DATA, as we don't need to send a SYN anymore. */ - so->so_flags1 &= ~SOF1_PRECONNECT_DATA; return error; } #endif /* FLOW_DIVERT */ @@ -729,14 +706,13 @@ tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p) } static int -tcp6_usr_connectx(struct socket *so, struct sockaddr_list **src_sl, - struct sockaddr_list **dst_sl, struct proc *p, uint32_t ifscope, +tcp6_usr_connectx(struct socket *so, struct sockaddr*src, + struct sockaddr *dst, struct proc *p, uint32_t ifscope, sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg, uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written) { - return (tcp_usr_connectx_common(so, AF_INET6, src_sl, dst_sl, - p, ifscope, aid, pcid, flags, arg, arglen, uio, - bytes_written)); + return (tcp_usr_connectx_common(so, AF_INET6, src, dst, p, ifscope, aid, + pcid, flags, arg, arglen, uio, bytes_written)); } #endif /* INET6 */ @@ -758,8 +734,7 @@ tcp_usr_disconnect(struct socket *so) struct inpcb *inp = sotoinpcb(so); struct tcpcb *tp; - lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx, - LCK_MTX_ASSERT_OWNED); + socket_lock_assert_owned(so); COMMON_START(); /* In case we got disconnected from the peer */ if (tp == NULL) @@ -1046,8 +1021,8 @@ tcp_usr_send(struct socket *so, int flags, struct mbuf *m, m_freem(control); control = NULL; } else if (control->m_len) { - /* - * if not unordered, TCP should not have + /* + * if not unordered, TCP should not have * control mbufs */ m_freem(control); @@ -1112,8 +1087,8 @@ tcp_usr_send(struct socket *so, int flags, struct mbuf *m, tp->t_flags &= ~TF_MORETOCOME; } } else { - if (sbspace(&so->so_snd) == 0) { - /* if no space is left in sockbuf, + if (sbspace(&so->so_snd) == 0) { + /* if no space is left in sockbuf, * do not try to squeeze in OOB traffic */ m_freem(m); error = ENOBUFS; @@ -1164,7 +1139,7 @@ tcp_usr_send(struct socket *so, int flags, struct mbuf *m, error = sbwait(&so->so_snd); } - COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : + COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); } @@ -1183,6 +1158,7 @@ tcp_usr_abort(struct socket *so) if (tp == NULL) goto out; tp = tcp_drop(tp, ECONNABORTED); + VERIFY(so->so_usecount > 0); so->so_usecount--; COMMON_END(PRU_ABORT); } @@ -1216,6 +1192,7 @@ tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) } m->m_len = 1; *mtod(m, caddr_t) = tp->t_iobc; + so->so_state &= ~SS_RCVATMARK; if ((flags & MSG_PEEK) == 0) tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); COMMON_END(PRU_RCVOOB); @@ -1224,10 +1201,22 @@ tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) static int tcp_usr_preconnect(struct socket *so) { - int error = tcp_output(sototcpcb(so)); + struct inpcb *inp = sotoinpcb(so); + int error = 0; + +#if NECP + if (necp_socket_should_use_flow_divert(inp)) { + /* May happen, if in tcp_usr_connect we did not had a chance + * to set the usrreqs (due to some error). So, let's get out + * of here. + */ + goto out; + } +#endif /* NECP */ + + error = tcp_output(sototcpcb(so)); - /* One read has been done. This was enough. Get back to "normal" behavior. */ - so->so_flags1 &= ~SOF1_PRECONNECT_DATA; + soclearfastopen(so); COMMON_END(PRU_PRECONNECT); } @@ -1306,18 +1295,13 @@ struct pr_usrreqs tcp6_usrreqs = { * in_pcbladdr:EADDRNOTAVAIL Address not available */ static int -tcp_connect(tp, nam, p) - register struct tcpcb *tp; - struct sockaddr *nam; - struct proc *p; +tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct proc *p) { struct inpcb *inp = tp->t_inpcb, *oinp; struct socket *so = inp->inp_socket; struct tcpcb *otp; struct sockaddr_in *sin = (struct sockaddr_in *)(void *)nam; struct in_addr laddr; - struct rmxp_tao *taop; - struct rmxp_tao tao_noncached; int error = 0; struct ifnet *outif = NULL; @@ -1332,23 +1316,23 @@ tcp_connect(tp, nam, p) * earlier incarnation of this same connection still in * TIME_WAIT state, creating an ADDRINUSE error. */ - error = in_pcbladdr(inp, nam, &laddr, IFSCOPE_NONE, &outif); + error = in_pcbladdr(inp, nam, &laddr, IFSCOPE_NONE, &outif, 0); if (error) goto done; - tcp_unlock(inp->inp_socket, 0, 0); + socket_unlock(inp->inp_socket, 0); oinp = in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port, inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr : laddr, inp->inp_lport, 0, NULL); - tcp_lock(inp->inp_socket, 0, 0); + socket_lock(inp->inp_socket, 0); if (oinp) { if (oinp != inp) /* 4143933: avoid deadlock if inp == oinp */ - tcp_lock(oinp->inp_socket, 1, 0); + socket_lock(oinp->inp_socket, 1); if (in_pcb_checkstate(oinp, WNT_RELEASE, 1) == WNT_STOPUSING) { if (oinp != inp) - tcp_unlock(oinp->inp_socket, 1, 0); + socket_unlock(oinp->inp_socket, 1); goto skip_oinp; } @@ -1361,12 +1345,12 @@ tcp_connect(tp, nam, p) printf("tcp_connect: inp=0x%llx err=EADDRINUSE\n", (uint64_t)VM_KERNEL_ADDRPERM(inp)); if (oinp != inp) - tcp_unlock(oinp->inp_socket, 1, 0); + socket_unlock(oinp->inp_socket, 1); error = EADDRINUSE; goto done; } if (oinp != inp) - tcp_unlock(oinp->inp_socket, 1, 0); + socket_unlock(oinp->inp_socket, 1); } skip_oinp: if ((inp->inp_laddr.s_addr == INADDR_ANY ? laddr.s_addr : @@ -1385,6 +1369,7 @@ skip_oinp: inp->inp_laddr = laddr; /* no reference needed */ inp->inp_last_outifp = outif; + inp->inp_flags |= INP_INADDR_ANY; } inp->inp_faddr = sin->sin_addr; @@ -1395,7 +1380,7 @@ skip_oinp: if (inp->inp_flowhash == 0) inp->inp_flowhash = inp_calc_flowhash(inp); - tcp_set_max_rwinscale(tp, so); + tcp_set_max_rwinscale(tp, so, TCP_AUTORCVBUF_MAX(outif)); soisconnecting(so); tcpstat.tcps_connattempt++; @@ -1406,24 +1391,6 @@ skip_oinp: if (nstat_collect) nstat_route_connect_attempt(inp->inp_route.ro_rt); - /* - * Generate a CC value for this connection and - * check whether CC or CCnew should be used. - */ - if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) { - taop = &tao_noncached; - bzero(taop, sizeof(*taop)); - } - - tp->cc_send = CC_INC(tcp_ccgen); - if (taop->tao_ccsent != 0 && - CC_GEQ(tp->cc_send, taop->tao_ccsent)) { - taop->tao_ccsent = tp->cc_send; - } else { - taop->tao_ccsent = 0; - tp->t_flags |= TF_SENDCCNEW; - } - done: if (outif != NULL) ifnet_release(outif); @@ -1433,18 +1400,13 @@ done: #if INET6 static int -tcp6_connect(tp, nam, p) - register struct tcpcb *tp; - struct sockaddr *nam; - struct proc *p; +tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct proc *p) { struct inpcb *inp = tp->t_inpcb, *oinp; struct socket *so = inp->inp_socket; struct tcpcb *otp; struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(void *)nam; struct in6_addr addr6; - struct rmxp_tao *taop; - struct rmxp_tao tao_noncached; int error = 0; struct ifnet *outif = NULL; @@ -1466,14 +1428,14 @@ tcp6_connect(tp, nam, p) error = in6_pcbladdr(inp, nam, &addr6, &outif); if (error) goto done; - tcp_unlock(inp->inp_socket, 0, 0); + socket_unlock(inp->inp_socket, 0); oinp = in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, sin6->sin6_port, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ? &addr6 : &inp->in6p_laddr, inp->inp_lport, 0, NULL); - tcp_lock(inp->inp_socket, 0, 0); + socket_lock(inp->inp_socket, 0); if (oinp) { if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && otp->t_state == TCPS_TIME_WAIT && @@ -1512,36 +1474,18 @@ tcp6_connect(tp, nam, p) (htonl(inp->inp_flowhash) & IPV6_FLOWLABEL_MASK); } - tcp_set_max_rwinscale(tp, so); + tcp_set_max_rwinscale(tp, so, TCP_AUTORCVBUF_MAX(outif)); soisconnecting(so); tcpstat.tcps_connattempt++; tp->t_state = TCPS_SYN_SENT; - tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, + tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, TCP_CONN_KEEPINIT(tp)); tp->iss = tcp_new_isn(tp); tcp_sendseqinit(tp); if (nstat_collect) nstat_route_connect_attempt(inp->inp_route.ro_rt); - /* - * Generate a CC value for this connection and - * check whether CC or CCnew should be used. - */ - if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) { - taop = &tao_noncached; - bzero(taop, sizeof(*taop)); - } - - tp->cc_send = CC_INC(tcp_ccgen); - if (taop->tao_ccsent != 0 && - CC_GEQ(tp->cc_send, taop->tao_ccsent)) { - taop->tao_ccsent = tp->cc_send; - } else { - taop->tao_ccsent = 0; - tp->t_flags |= TF_SENDCCNEW; - } - done: if (outif != NULL) ifnet_release(outif); @@ -1553,11 +1497,11 @@ done: /* * Export TCP internal state information via a struct tcp_info */ -__private_extern__ void +void tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) { struct inpcb *inp = tp->t_inpcb; - + bzero(ti, sizeof(*ti)); ti->tcpi_state = tp->t_state; @@ -1580,6 +1524,9 @@ tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) if (IN_FASTRECOVERY(tp) || tp->t_rxtshift > 0) ti->tcpi_flags |= TCPI_FLAG_LOSSRECOVERY; + if (tp->t_flags & TF_STREAMING_ON) + ti->tcpi_flags |= TCPI_FLAG_STREAMING_ON; + ti->tcpi_rto = tp->t_timer[TCPT_REXMT] ? tp->t_rxtcur : 0; ti->tcpi_snd_mss = tp->t_maxseg; ti->tcpi_rcv_mss = tp->t_maxseg; @@ -1592,7 +1539,7 @@ tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) ti->tcpi_snd_ssthresh = tp->snd_ssthresh; ti->tcpi_snd_cwnd = tp->snd_cwnd; ti->tcpi_snd_sbbytes = inp->inp_socket->so_snd.sb_cc; - + ti->tcpi_rcv_space = tp->rcv_wnd; ti->tcpi_snd_wnd = tp->snd_wnd; @@ -1604,7 +1551,7 @@ tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) tp->t_bwmeas != NULL) { ti->tcpi_snd_bw = (tp->t_bwmeas->bw_sndbw * 8000); } - + ti->tcpi_last_outif = (tp->t_inpcb->inp_last_outifp == NULL) ? 0 : tp->t_inpcb->inp_last_outifp->if_index; @@ -1612,8 +1559,9 @@ tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) ti->tcpi_txpackets = inp->inp_stat->txpackets; ti->tcpi_txbytes = inp->inp_stat->txbytes; ti->tcpi_txretransmitbytes = tp->t_stat.txretransmitbytes; + ti->tcpi_txretransmitpackets = tp->t_stat.rxmitpkts; ti->tcpi_txunacked = tp->snd_max - tp->snd_una; - + //atomic_get_64(ti->tcpi_rxbytes, &inp->inp_stat->rxbytes); ti->tcpi_rxpackets = inp->inp_stat->rxpackets; ti->tcpi_rxbytes = inp->inp_stat->rxbytes; @@ -1649,6 +1597,12 @@ tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) ti->tcpi_tfo_syn_data_sent = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_SENT); ti->tcpi_tfo_syn_data_acked = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_ACKED); ti->tcpi_tfo_syn_loss = !!(tp->t_tfo_stats & TFO_S_SYN_LOSS); + ti->tcpi_tfo_cookie_wrong = !!(tp->t_tfo_stats & TFO_S_COOKIE_WRONG); + ti->tcpi_tfo_no_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_NO_COOKIE_RCV); + ti->tcpi_tfo_heuristics_disable = !!(tp->t_tfo_stats & TFO_S_HEURISTICS_DISABLE); + ti->tcpi_tfo_send_blackhole = !!(tp->t_tfo_stats & TFO_S_SEND_BLACKHOLE); + ti->tcpi_tfo_recv_blackhole = !!(tp->t_tfo_stats & TFO_S_RECV_BLACKHOLE); + ti->tcpi_tfo_onebyte_proxy = !!(tp->t_tfo_stats & TFO_S_ONE_BYTE_PROXY); ti->tcpi_ecn_client_setup = !!(tp->ecn_flags & TE_SETUPSENT); ti->tcpi_ecn_server_setup = !!(tp->ecn_flags & TE_SETUPRECEIVED); @@ -1661,9 +1615,20 @@ tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) if (tp->t_inpcb->inp_last_outifp != NULL) { if (IFNET_IS_CELLULAR(tp->t_inpcb->inp_last_outifp)) ti->tcpi_if_cell = 1; - else if (IFNET_IS_WIFI(tp->t_inpcb->inp_last_outifp)) + if (IFNET_IS_WIFI(tp->t_inpcb->inp_last_outifp)) ti->tcpi_if_wifi = 1; + if (IFNET_IS_WIRED(tp->t_inpcb->inp_last_outifp)) + ti->tcpi_if_wired = 1; + if (IFNET_IS_WIFI_INFRA(tp->t_inpcb->inp_last_outifp)) + ti->tcpi_if_wifi_infra = 1; + if (tp->t_inpcb->inp_last_outifp->if_eflags & IFEF_AWDL) + ti->tcpi_if_wifi_awdl = 1; } + if (tp->tcp_cc_index == TCP_CC_ALGO_BACKGROUND_INDEX) + ti->tcpi_snd_background = 1; + if (tcp_recv_bg == 1 || + IS_TCP_RECV_BG(tp->t_inpcb->inp_socket)) + ti->tcpi_rcv_background = 1; ti->tcpi_ecn_recv_ce = tp->t_ecn_recv_ce; ti->tcpi_ecn_recv_cwr = tp->t_ecn_recv_cwr; @@ -1684,15 +1649,15 @@ tcp_fill_info_for_info_tuple(struct info_tuple *itpl, struct tcp_info *ti) struct inpcb *inp = NULL; struct socket *so; struct tcpcb *tp; - + if (itpl->itpl_proto == IPPROTO_TCP) pcbinfo = &tcbinfo; else return EINVAL; - + if (itpl->itpl_local_sa.sa_family == AF_INET && itpl->itpl_remote_sa.sa_family == AF_INET) { - inp = in_pcblookup_hash(pcbinfo, + inp = in_pcblookup_hash(pcbinfo, itpl->itpl_remote_sin.sin_addr, itpl->itpl_remote_sin.sin_port, itpl->itpl_local_sin.sin_addr, @@ -1702,18 +1667,18 @@ tcp_fill_info_for_info_tuple(struct info_tuple *itpl, struct tcp_info *ti) itpl->itpl_remote_sa.sa_family == AF_INET6) { struct in6_addr ina6_local; struct in6_addr ina6_remote; - + ina6_local = itpl->itpl_local_sin6.sin6_addr; - if (IN6_IS_SCOPE_LINKLOCAL(&ina6_local) && + if (IN6_IS_SCOPE_LINKLOCAL(&ina6_local) && itpl->itpl_local_sin6.sin6_scope_id) ina6_local.s6_addr16[1] = htons(itpl->itpl_local_sin6.sin6_scope_id); ina6_remote = itpl->itpl_remote_sin6.sin6_addr; - if (IN6_IS_SCOPE_LINKLOCAL(&ina6_remote) && + if (IN6_IS_SCOPE_LINKLOCAL(&ina6_remote) && itpl->itpl_remote_sin6.sin6_scope_id) ina6_remote.s6_addr16[1] = htons(itpl->itpl_remote_sin6.sin6_scope_id); - - inp = in6_pcblookup_hash(pcbinfo, + + inp = in6_pcblookup_hash(pcbinfo, &ina6_remote, itpl->itpl_remote_sin6.sin6_port, &ina6_local, @@ -1775,6 +1740,7 @@ tcp_connection_fill_info(struct tcpcb *tp, struct tcp_connection_info *tci) tci->tcpi_txpackets = inp->inp_stat->txpackets; tci->tcpi_txbytes = inp->inp_stat->txbytes; tci->tcpi_txretransmitbytes = tp->t_stat.txretransmitbytes; + tci->tcpi_txretransmitpackets = tp->t_stat.rxmitpkts; tci->tcpi_rxpackets = inp->inp_stat->rxpackets; tci->tcpi_rxbytes = inp->inp_stat->rxbytes; tci->tcpi_rxoutoforderbytes = tp->t_stat.rxoutoforderbytes; @@ -1788,16 +1754,23 @@ tcp_connection_fill_info(struct tcpcb *tp, struct tcp_connection_info *tci) tci->tcpi_tfo_syn_data_sent = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_SENT); tci->tcpi_tfo_syn_data_acked = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_ACKED); tci->tcpi_tfo_syn_loss = !!(tp->t_tfo_stats & TFO_S_SYN_LOSS); + tci->tcpi_tfo_cookie_wrong = !!(tp->t_tfo_stats & TFO_S_COOKIE_WRONG); + tci->tcpi_tfo_no_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_NO_COOKIE_RCV); + tci->tcpi_tfo_heuristics_disable = !!(tp->t_tfo_stats & TFO_S_HEURISTICS_DISABLE); + tci->tcpi_tfo_send_blackhole = !!(tp->t_tfo_stats & TFO_S_SEND_BLACKHOLE); + tci->tcpi_tfo_recv_blackhole = !!(tp->t_tfo_stats & TFO_S_RECV_BLACKHOLE); + tci->tcpi_tfo_onebyte_proxy = !!(tp->t_tfo_stats & TFO_S_ONE_BYTE_PROXY); } } -__private_extern__ int +__private_extern__ int tcp_sysctl_info(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req) { int error; - struct tcp_info ti; + struct tcp_info ti = {}; struct info_tuple itpl; +#if !CONFIG_EMBEDDED proc_t caller = PROC_NULL; proc_t caller_parent = PROC_NULL; char command_name[MAXCOMLEN + 1] = ""; @@ -1814,9 +1787,9 @@ tcp_sysctl_info(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused proc_rele(caller_parent); } - if ((escape_str(command_name, strlen(command_name), + if ((escape_str(command_name, strlen(command_name) + 1, sizeof(command_name)) == 0) && - (escape_str(parent_name, strlen(parent_name), + (escape_str(parent_name, strlen(parent_name) + 1, sizeof(parent_name)) == 0)) { kern_asl_msg(LOG_DEBUG, "messagetracer", 5, @@ -1833,6 +1806,7 @@ tcp_sysctl_info(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused if (caller != PROC_NULL) proc_rele(caller); +#endif /* !CONFIG_EMBEDDED */ if (req->newptr == USER_ADDR_NULL) { return EINVAL; @@ -1852,7 +1826,7 @@ tcp_sysctl_info(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused if (error != 0) { return error; } - + return 0; } @@ -1862,32 +1836,36 @@ tcp_lookup_peer_pid_locked(struct socket *so, pid_t *out_pid) int error = EHOSTUNREACH; *out_pid = -1; if ((so->so_state & SS_ISCONNECTED) == 0) return ENOTCONN; - + struct inpcb *inp = (struct inpcb*)so->so_pcb; uint16_t lport = inp->inp_lport; uint16_t fport = inp->inp_fport; struct inpcb *finp = NULL; - + struct in6_addr laddr6, faddr6; + struct in_addr laddr4, faddr4; + + if (inp->inp_vflag & INP_IPV6) { + laddr6 = inp->in6p_laddr; + faddr6 = inp->in6p_faddr; + } else if (inp->inp_vflag & INP_IPV4) { + laddr4 = inp->inp_laddr; + faddr4 = inp->inp_faddr; + } + + socket_unlock(so, 0); if (inp->inp_vflag & INP_IPV6) { - struct in6_addr laddr6 = inp->in6p_laddr; - struct in6_addr faddr6 = inp->in6p_faddr; - socket_unlock(so, 0); finp = in6_pcblookup_hash(&tcbinfo, &laddr6, lport, &faddr6, fport, 0, NULL); - socket_lock(so, 0); } else if (inp->inp_vflag & INP_IPV4) { - struct in_addr laddr4 = inp->inp_laddr; - struct in_addr faddr4 = inp->inp_faddr; - socket_unlock(so, 0); finp = in_pcblookup_hash(&tcbinfo, laddr4, lport, faddr4, fport, 0, NULL); - socket_lock(so, 0); } - + if (finp) { *out_pid = finp->inp_socket->last_pid; error = 0; in_pcb_checkstate(finp, WNT_RELEASE, 0); } - + socket_lock(so, 0); + return error; } @@ -1906,15 +1884,12 @@ tcp_getconninfo(struct socket *so, struct conninfo_tcp *tcp_ci) * splnet() any more. This needs more examination.) */ int -tcp_ctloutput(so, sopt) - struct socket *so; - struct sockopt *sopt; +tcp_ctloutput(struct socket *so, struct sockopt *sopt) { - int error, opt, optval; + int error = 0, opt = 0, optval = 0; struct inpcb *inp; struct tcpcb *tp; - error = 0; inp = sotoinpcb(so); if (inp == NULL) { return (ECONNRESET); @@ -2029,7 +2004,7 @@ tcp_ctloutput(so, sopt) error = EINVAL; break; } - minpkts = (in.min_burst_size != 0) ? in.min_burst_size : + minpkts = (in.min_burst_size != 0) ? in.min_burst_size : tp->t_bwmeas->bw_minsizepkts; maxpkts = (in.max_burst_size != 0) ? in.max_burst_size : tp->t_bwmeas->bw_maxsizepkts; @@ -2066,7 +2041,7 @@ tcp_ctloutput(so, sopt) } else { tp->t_keepidle = optval * TCP_RETRANSHZ; /* reset the timer to new value */ - tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, + tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, TCP_CONN_KEEPIDLE(tp)); tcp_check_timer_state(tp); } @@ -2126,6 +2101,21 @@ tcp_ctloutput(so, sopt) } break; + case TCP_KEEPALIVE_OFFLOAD: + error = sooptcopyin(sopt, &optval, sizeof(optval), + sizeof(optval)); + if (error) + break; + if (optval < 0 || optval > INT32_MAX) { + error = EINVAL; + break; + } + if (optval != 0) + inp->inp_flags2 |= INP2_KEEPALIVE_OFFLOAD; + else + inp->inp_flags2 &= ~INP2_KEEPALIVE_OFFLOAD; + break; + case PERSIST_TIMEOUT: error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval); @@ -2133,7 +2123,7 @@ tcp_ctloutput(so, sopt) break; if (optval < 0) error = EINVAL; - else + else tp->t_persist_timeout = optval * TCP_RETRANSHZ; break; case TCP_RXT_CONNDROPTIME: @@ -2158,7 +2148,7 @@ tcp_ctloutput(so, sopt) if (optval == 0) { so->so_flags &= ~(SOF_NOTSENT_LOWAT); tp->t_notsent_lowat = 0; - } else { + } else { so->so_flags |= SOF_NOTSENT_LOWAT; tp->t_notsent_lowat = optval; } @@ -2169,13 +2159,16 @@ tcp_ctloutput(so, sopt) sizeof(optval)); if (error) break; - if (optval < 0 || + if (optval < 0 || optval > TCP_ADAPTIVE_TIMEOUT_MAX) { error = EINVAL; break; } else if (optval == 0) { tp->t_adaptive_rtimo = 0; tcp_keepalive_reset(tp); + + if (tp->t_mpsub) + mptcp_reset_keepalive(tp); } else { tp->t_adaptive_rtimo = optval; } @@ -2185,7 +2178,7 @@ tcp_ctloutput(so, sopt) sizeof (optval)); if (error) break; - if (optval < 0 || + if (optval < 0 || optval > TCP_ADAPTIVE_TIMEOUT_MAX) { error = EINVAL; break; @@ -2211,13 +2204,13 @@ tcp_ctloutput(so, sopt) } /* - * allocate memory for storing message + * allocate memory for storing message * related state */ VERIFY(so->so_msg_state == NULL); - MALLOC(so->so_msg_state, + MALLOC(so->so_msg_state, struct msg_state *, - sizeof(struct msg_state), + sizeof(struct msg_state), M_TEMP, M_WAITOK | M_ZERO); if (so->so_msg_state == NULL) { error = ENOMEM; @@ -2227,9 +2220,9 @@ tcp_ctloutput(so, sopt) /* Enable message delivery */ so->so_flags |= SOF_ENABLE_MSGS; } else { - /* - * Can't disable message delivery on socket - * because of restrictions imposed by + /* + * Can't disable message delivery on socket + * because of restrictions imposed by * encoding/decoding */ error = EINVAL; @@ -2287,6 +2280,27 @@ tcp_ctloutput(so, sopt) else tcp_disable_tfo(tp); break; + case TCP_FASTOPEN_FORCE_HEURISTICS: + error = sooptcopyin(sopt, &optval, sizeof(optval), + sizeof(optval)); + + if (error) + break; + if (optval < 0 || optval > 1) { + error = EINVAL; + break; + } + + if (tp->t_state != TCPS_CLOSED) { + error = EINVAL; + break; + } + if (optval) + tp->t_flagsext |= TF_FASTOPEN_HEUR; + else + tp->t_flagsext &= ~TF_FASTOPEN_HEUR; + + break; case TCP_ENABLE_ECN: error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval); @@ -2297,6 +2311,7 @@ tcp_ctloutput(so, sopt) tp->ecn_flags &= ~TE_ECN_MODE_DISABLE; } else { tp->ecn_flags &= ~TE_ECN_MODE_ENABLE; + tp->ecn_flags |= TE_ECN_MODE_DISABLE; } break; case TCP_ECN_MODE: @@ -2317,6 +2332,30 @@ tcp_ctloutput(so, sopt) error = EINVAL; } break; + case TCP_NOTIFY_ACKNOWLEDGEMENT: + error = sooptcopyin(sopt, &optval, + sizeof(optval), sizeof(optval)); + if (error) + break; + if (optval <= 0) { + error = EINVAL; + break; + } + if (tp->t_notify_ack_count >= TCP_MAX_NOTIFY_ACK) { + error = ETOOMANYREFS; + break; + } + + /* + * validate that the given marker id is not + * a duplicate to avoid ambiguity + */ + if ((error = tcp_notify_ack_id_valid(tp, so, + optval)) != 0) { + break; + } + error = tcp_add_notify_ack_marker(tp, optval); + break; case SO_FLUSH: if ((error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval))) != 0) @@ -2338,7 +2377,24 @@ tcp_ctloutput(so, sopt) TRAFFIC_MGT_SO_BACKGROUND); } break; - + case TCP_RXT_MINIMUM_TIMEOUT: + error = sooptcopyin(sopt, &optval, sizeof(optval), + sizeof(optval)); + if (error) + break; + if (optval < 0) { + error = EINVAL; + break; + } + if (optval == 0) { + tp->t_rxt_minimum_timeout = 0; + } else { + tp->t_rxt_minimum_timeout = min(optval, + TCP_RXT_MINIMUM_TIMEOUT_LIMIT); + /* convert to milliseconds */ + tp->t_rxt_minimum_timeout *= TCP_RETRANSHZ; + } + break; default: error = ENOPROTOOPT; break; @@ -2354,13 +2410,25 @@ tcp_ctloutput(so, sopt) optval = tp->t_maxseg; break; case TCP_KEEPALIVE: - optval = tp->t_keepidle / TCP_RETRANSHZ; + if (tp->t_keepidle > 0) + optval = tp->t_keepidle / TCP_RETRANSHZ; + else + optval = tcp_keepidle / TCP_RETRANSHZ; break; case TCP_KEEPINTVL: - optval = tp->t_keepintvl / TCP_RETRANSHZ; + if (tp->t_keepintvl > 0) + optval = tp->t_keepintvl / TCP_RETRANSHZ; + else + optval = tcp_keepintvl / TCP_RETRANSHZ; break; case TCP_KEEPCNT: - optval = tp->t_keepcnt; + if (tp->t_keepcnt > 0) + optval = tp->t_keepcnt; + else + optval = tcp_keepcnt; + break; + case TCP_KEEPALIVE_OFFLOAD: + optval = !!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD); break; case TCP_NOOPT: optval = tp->t_flags & TF_NOOPT; @@ -2390,7 +2458,7 @@ tcp_ctloutput(so, sopt) break; case TCP_RXT_FINDROP: optval = tp->t_flagsext & TF_RXTFINDROP; - break; + break; case TCP_NOTIMEWAIT: optval = (tp->t_flagsext & TF_NOTIMEWAIT) ? 1 : 0; break; @@ -2402,6 +2470,9 @@ tcp_ctloutput(so, sopt) } optval = tfo_enabled(tp); break; + case TCP_FASTOPEN_FORCE_HEURISTICS: + optval = (tp->t_flagsext & TF_FASTOPEN_HEUR) ? 1 : 0; + break; case TCP_MEASURE_SND_BW: optval = tp->t_flagsext & TF_MEASURESNDBW; break; @@ -2421,7 +2492,7 @@ tcp_ctloutput(so, sopt) goto done; } case TCP_MEASURE_BW_BURST: { - struct tcp_measure_bw_burst out; + struct tcp_measure_bw_burst out = {}; if ((tp->t_flagsext & TF_MEASURESNDBW) == 0 || tp->t_bwmeas == NULL) { error = EINVAL; @@ -2473,8 +2544,26 @@ tcp_ctloutput(so, sopt) optval = tp->t_adaptive_wtimo; break; case SO_TRAFFIC_MGT_BACKGROUND: - optval = (so->so_traffic_mgt_flags & - TRAFFIC_MGT_SO_BACKGROUND) ? 1 : 0; + optval = (so->so_flags1 & + SOF1_TRAFFIC_MGT_SO_BACKGROUND) ? 1 : 0; + break; + case TCP_NOTIFY_ACKNOWLEDGEMENT: { + struct tcp_notify_ack_complete retid; + + if (sopt->sopt_valsize != sizeof (retid)) { + error = EINVAL; + break; + } + bzero(&retid, sizeof (retid)); + tcp_get_notify_ack_count(tp, &retid); + if (retid.notify_complete_count > 0) + tcp_get_notify_ack_ids(tp, &retid); + + error = sooptcopyout(sopt, &retid, sizeof (retid)); + goto done; + } + case TCP_RXT_MINIMUM_TIMEOUT: + optval = tp->t_rxt_minimum_timeout / TCP_RETRANSHZ; break; default: error = ENOPROTOOPT; @@ -2500,10 +2589,12 @@ u_int32_t tcp_recvspace = 1448*384; * sb_max in sbreserve. Disallow setting the tcp send and recv space * to be more than sb_max because that will cause tcp_attach to fail * (see radar 5713060) - */ + */ static int sysctl_tcp_sospace(struct sysctl_oid *oidp, __unused void *arg1, - __unused int arg2, struct sysctl_req *req) { + int arg2, struct sysctl_req *req) +{ +#pragma unused(arg2) u_int32_t new_value = 0, *space_p = NULL; int changed = 0, error = 0; u_quad_t sb_effective_max = (sb_max / (MSIZE+MCLBYTES)) * MCLBYTES; @@ -2523,6 +2614,7 @@ sysctl_tcp_sospace(struct sysctl_oid *oidp, __unused void *arg1, if (changed) { if (new_value > 0 && new_value <= sb_effective_max) { *space_p = new_value; + SYSCTL_SKMEM_UPDATE_AT_OFFSET(arg2, new_value); } else { error = ERANGE; } @@ -2530,19 +2622,21 @@ sysctl_tcp_sospace(struct sysctl_oid *oidp, __unused void *arg1, return error; } +#if SYSCTL_SKMEM +SYSCTL_PROC(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, + CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_sendspace, + offsetof(skmem_sysctl, tcp.sendspace), sysctl_tcp_sospace, + "IU", "Maximum outgoing TCP datagram size"); +SYSCTL_PROC(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, + CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_recvspace, + offsetof(skmem_sysctl, tcp.recvspace), sysctl_tcp_sospace, + "IU", "Maximum incoming TCP datagram size"); +#else /* SYSCTL_SKMEM */ SYSCTL_PROC(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_sendspace , 0, &sysctl_tcp_sospace, "IU", "Maximum outgoing TCP datagram size"); SYSCTL_PROC(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_recvspace , 0, &sysctl_tcp_sospace, "IU", "Maximum incoming TCP datagram size"); - -/* Sysctl for testing and tuning the connectx with data api */ -#define TCP_PRECONNECT_SBSZ_MAX 1460 -#define TCP_PRECONNECT_SBSZ_MIN (TCP_MSS) -#define TCP_PRECONNECT_SBSZ_DEF (TCP6_MSS) -static int tcp_preconnect_sbspace = TCP_PRECONNECT_SBSZ_DEF; -SYSCTL_INT(_net_inet_tcp, OID_AUTO, preconn_sbsz, CTLFLAG_RW | CTLFLAG_LOCKED, - &tcp_preconnect_sbspace, 0, "Maximum preconnect space"); - +#endif /* SYSCTL_SKMEM */ /* * Attach TCP protocol to socket, allocating @@ -2556,11 +2650,9 @@ SYSCTL_INT(_net_inet_tcp, OID_AUTO, preconn_sbsz, CTLFLAG_RW | CTLFLAG_LOCKED, * soreserve:ENOBUFS */ static int -tcp_attach(so, p) - struct socket *so; - struct proc *p; +tcp_attach(struct socket *so, struct proc *p) { - register struct tcpcb *tp; + struct tcpcb *tp; struct inpcb *inp; int error; #if INET6 @@ -2580,8 +2672,7 @@ tcp_attach(so, p) } if (so->so_snd.sb_preconn_hiwat == 0) { - soreserve_preconnect(so, imin(TCP_PRECONNECT_SBSZ_MAX, - imax(tcp_preconnect_sbspace, TCP_PRECONNECT_SBSZ_MIN))); + soreserve_preconnect(so, 2048); } if ((so->so_rcv.sb_flags & SB_USRSIZE) == 0) @@ -2626,11 +2717,13 @@ tcp_attach(so, p) * send segment to peer (with FIN). */ static struct tcpcb * -tcp_disconnect(tp) - register struct tcpcb *tp; +tcp_disconnect(struct tcpcb *tp) { struct socket *so = tp->t_inpcb->inp_socket; + if (so->so_rcv.sb_cc != 0 || tp->t_reassqlen != 0) + return tcp_drop(tp, 0); + if (tp->t_state < TCPS_ESTABLISHED) tp = tcp_close(tp); else if ((so->so_options & SO_LINGER) && so->so_linger == 0) @@ -2662,34 +2755,32 @@ tcp_disconnect(tp) * We can let the user exit from the close as soon as the FIN is acked. */ static struct tcpcb * -tcp_usrclosed(tp) - register struct tcpcb *tp; +tcp_usrclosed(struct tcpcb *tp) { - switch (tp->t_state) { case TCPS_CLOSED: case TCPS_LISTEN: + case TCPS_SYN_SENT: tp = tcp_close(tp); break; - case TCPS_SYN_SENT: case TCPS_SYN_RECEIVED: tp->t_flags |= TF_NEEDFIN; break; case TCPS_ESTABLISHED: - DTRACE_TCP4(state__change, void, NULL, + DTRACE_TCP4(state__change, void, NULL, struct inpcb *, tp->t_inpcb, - struct tcpcb *, tp, + struct tcpcb *, tp, int32_t, TCPS_FIN_WAIT_1); tp->t_state = TCPS_FIN_WAIT_1; break; case TCPS_CLOSE_WAIT: - DTRACE_TCP4(state__change, void, NULL, + DTRACE_TCP4(state__change, void, NULL, struct inpcb *, tp->t_inpcb, - struct tcpcb *, tp, + struct tcpcb *, tp, int32_t, TCPS_LAST_ACK); tp->t_state = TCPS_LAST_ACK; break; @@ -2698,7 +2789,7 @@ tcp_usrclosed(tp) soisdisconnected(tp->t_inpcb->inp_socket); /* To prevent the connection hanging in FIN_WAIT_2 forever. */ if (tp->t_state == TCPS_FIN_WAIT_2) - tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp, + tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp, TCP_CONN_MAXIDLE(tp)); } return (tp); @@ -2733,12 +2824,12 @@ tcp_out6_cksum_stats(u_int32_t len) tcpstat.tcps_snd6_swcsum_bytes += len; } -/* +/* * When messages are enabled on a TCP socket, the message priority * is sent as a control message. This function will extract it. */ int -tcp_get_msg_priority(struct mbuf *control, uint32_t *msgpri) +tcp_get_msg_priority(struct mbuf *control, uint32_t *msgpri) { struct cmsghdr *cm; if (control == NULL) @@ -2757,7 +2848,7 @@ tcp_get_msg_priority(struct mbuf *control, uint32_t *msgpri) } } - VERIFY(*msgpri >= MSG_PRI_MIN && *msgpri <= MSG_PRI_MAX); + VERIFY(*msgpri >= MSG_PRI_MIN && *msgpri <= MSG_PRI_MAX); return (0); } #endif /* INET6 */