+void
+inp_fc_unthrottle_tcp(struct inpcb *inp)
+{
+ struct tcpcb *tp = inp->inp_ppcb;
+ /*
+ * Back off the slow-start threshold and enter
+ * congestion avoidance phase
+ */
+ if (CC_ALGO(tp)->pre_fr != NULL)
+ CC_ALGO(tp)->pre_fr(tp);
+
+ tp->snd_cwnd = tp->snd_ssthresh;
+
+ /*
+ * Restart counting for ABC as we changed the
+ * congestion window just now.
+ */
+ tp->t_bytes_acked = 0;
+
+ /* Reset retransmit shift as we know that the reason
+ * for delay in sending a packet is due to flow
+ * control on the outgoing interface. There is no need
+ * to backoff retransmit timer.
+ */
+ tp->t_rxtshift = 0;
+
+ /*
+ * Start the output stream again. Since we are
+ * not retransmitting data, do not reset the
+ * retransmit timer or rtt calculation.
+ */
+ tcp_output(tp);
+}
+
+static int
+tcp_getstat SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg1, arg2)
+
+ int error;
+
+ proc_t caller = PROC_NULL;
+ proc_t caller_parent = PROC_NULL;
+ char command_name[MAXCOMLEN + 1] = "";
+ char parent_name[MAXCOMLEN + 1] = "";
+
+ if ((caller = proc_self()) != PROC_NULL) {
+ /* get process name */
+ strlcpy(command_name, caller->p_comm, sizeof(command_name));
+
+ /* get parent process name if possible */
+ if ((caller_parent = proc_find(caller->p_ppid)) != PROC_NULL) {
+ strlcpy(parent_name, caller_parent->p_comm,
+ sizeof(parent_name));
+ proc_rele(caller_parent);
+ }
+
+ if ((escape_str(command_name, strlen(command_name),
+ sizeof(command_name)) == 0) &&
+ (escape_str(parent_name, strlen(parent_name),
+ sizeof(parent_name)) == 0)) {
+ kern_asl_msg(LOG_DEBUG, "messagetracer",
+ 5,
+ "com.apple.message.domain",
+ "com.apple.kernel.tcpstat", /* 1 */
+ "com.apple.message.signature",
+ "tcpstat", /* 2 */
+ "com.apple.message.signature2", command_name, /* 3 */
+ "com.apple.message.signature3", parent_name, /* 4 */
+ "com.apple.message.summarize", "YES", /* 5 */
+ NULL);
+ }
+ }
+ if (caller != PROC_NULL)
+ proc_rele(caller);
+
+ if (req->oldptr == 0) {
+ req->oldlen= (size_t)sizeof(struct tcpstat);
+ }
+
+ error = SYSCTL_OUT(req, &tcpstat, MIN(sizeof (tcpstat), req->oldlen));
+
+ return (error);
+
+}
+
+/*
+ * Checksum extended TCP header and data.
+ */
+int
+tcp_input_checksum(int af, struct mbuf *m, struct tcphdr *th, int off, int tlen)
+{
+ struct ifnet *ifp = m->m_pkthdr.rcvif;
+
+ switch (af) {
+ case AF_INET: {
+ struct ip *ip = mtod(m, struct ip *);
+ struct ipovly *ipov = (struct ipovly *)ip;
+
+ if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_DID_CSUM)
+ return (0);
+
+ if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
+ (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
+ (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
+ if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
+ th->th_sum = m->m_pkthdr.csum_rx_val;
+ } else {
+ uint16_t sum = m->m_pkthdr.csum_rx_val;
+ uint16_t start = m->m_pkthdr.csum_rx_start;
+
+ /*
+ * Perform 1's complement adjustment of octets
+ * that got included/excluded in the hardware-
+ * calculated checksum value. Ignore cases
+ * where the value includes or excludes the IP
+ * header span, as the sum for those octets
+ * would already be 0xffff and thus no-op.
+ */
+ if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
+ start != 0 && (off - start) != off) {
+#if BYTE_ORDER != BIG_ENDIAN
+ if (start < off) {
+ HTONS(ip->ip_len);
+ HTONS(ip->ip_off);
+ }
+#endif
+ /* callee folds in sum */
+ sum = m_adj_sum16(m, start, off, sum);
+#if BYTE_ORDER != BIG_ENDIAN
+ if (start < off) {
+ NTOHS(ip->ip_off);
+ NTOHS(ip->ip_len);
+ }
+#endif
+ }
+
+ /* callee folds in sum */
+ th->th_sum = in_pseudo(ip->ip_src.s_addr,
+ ip->ip_dst.s_addr,
+ sum + htonl(tlen + IPPROTO_TCP));
+ }
+ th->th_sum ^= 0xffff;
+ } else {
+ uint16_t ip_sum;
+ int len;
+ char b[9];
+
+ bcopy(ipov->ih_x1, b, sizeof (ipov->ih_x1));
+ bzero(ipov->ih_x1, sizeof (ipov->ih_x1));
+ ip_sum = ipov->ih_len;
+ ipov->ih_len = (u_short)tlen;
+#if BYTE_ORDER != BIG_ENDIAN
+ HTONS(ipov->ih_len);
+#endif
+ len = sizeof (struct ip) + tlen;
+ th->th_sum = in_cksum(m, len);
+ bcopy(b, ipov->ih_x1, sizeof (ipov->ih_x1));
+ ipov->ih_len = ip_sum;
+
+ tcp_in_cksum_stats(len);
+ }
+ break;
+ }
+#if INET6
+ case AF_INET6: {
+ struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
+
+ if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_DID_CSUM)
+ return (0);
+
+ if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
+ (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
+ (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
+ if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
+ th->th_sum = m->m_pkthdr.csum_rx_val;
+ } else {
+ uint16_t sum = m->m_pkthdr.csum_rx_val;
+ uint16_t start = m->m_pkthdr.csum_rx_start;
+
+ /*
+ * Perform 1's complement adjustment of octets
+ * that got included/excluded in the hardware-
+ * calculated checksum value.
+ */
+ if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
+ start != off) {
+ uint16_t s, d;
+
+ if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
+ s = ip6->ip6_src.s6_addr16[1];
+ ip6->ip6_src.s6_addr16[1] = 0 ;
+ }
+ if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
+ d = ip6->ip6_dst.s6_addr16[1];
+ ip6->ip6_dst.s6_addr16[1] = 0;
+ }
+
+ /* callee folds in sum */
+ sum = m_adj_sum16(m, start, off, sum);
+
+ if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
+ ip6->ip6_src.s6_addr16[1] = s;
+ if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
+ ip6->ip6_dst.s6_addr16[1] = d;
+ }
+
+ th->th_sum = in6_pseudo(
+ &ip6->ip6_src, &ip6->ip6_dst,
+ sum + htonl(tlen + IPPROTO_TCP));
+ }
+ th->th_sum ^= 0xffff;
+ } else {
+ tcp_in6_cksum_stats(tlen);
+ th->th_sum = in6_cksum(m, IPPROTO_TCP, off, tlen);
+ }
+ break;
+ }
+#endif /* INET6 */
+ default:
+ VERIFY(0);
+ /* NOTREACHED */
+ }
+
+ if (th->th_sum != 0) {
+ tcpstat.tcps_rcvbadsum++;
+ IF_TCP_STATINC(ifp, badformat);
+ return (-1);
+ }
+
+ return (0);
+}
+
+SYSCTL_PROC(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
+ tcp_getstat, "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
+
+static int
+sysctl_rexmtthresh SYSCTL_HANDLER_ARGS
+{
+#pragma unused(arg1, arg2)
+
+ int error, val = tcprexmtthresh;
+
+ error = sysctl_handle_int(oidp, &val, 0, req);
+ if (error || !req->newptr)
+ return (error);
+
+ /*
+ * Constrain the number of duplicate ACKs
+ * to consider for TCP fast retransmit
+ * to either 2 or 3
+ */
+
+ if (val < 2 || val > 3)
+ return (EINVAL);
+
+ tcprexmtthresh = val;
+
+ return (0);
+}