+
+ /* There is not enough space for the cookie, so we cannot do TFO */
+ if (MAX_TCPOPTLEN - optlen < cookie_len)
+ goto fallback;
+
+ /* Do not send SYN+data if there is more in the queue than MSS */
+ if (so->so_snd.sb_cc > (tp->t_maxopd - MAX_TCPOPTLEN))
+ goto fallback;
+
+ /* Ok, everything looks good. We can go on and do TFO */
+ return (len);
+
+fallback:
+ tp->t_flagsext &= ~TF_FASTOPEN;
+ return (0);
+}
+
+/* Returns the number of bytes written to the TCP option-space */
+static unsigned
+tcp_tfo_write_cookie_rep(struct tcpcb *tp, unsigned optlen, u_char *opt)
+{
+ u_char out[CCAES_BLOCK_SIZE];
+ unsigned ret = 0;
+ u_char *bp;
+
+ if ((MAX_TCPOPTLEN - optlen) <
+ (TCPOLEN_FASTOPEN_REQ + TFO_COOKIE_LEN_DEFAULT))
+ return (ret);
+
+ tcp_tfo_gen_cookie(tp->t_inpcb, out, sizeof(out));
+
+ bp = opt + optlen;
+
+ *bp++ = TCPOPT_FASTOPEN;
+ *bp++ = 2 + TFO_COOKIE_LEN_DEFAULT;
+ memcpy(bp, out, TFO_COOKIE_LEN_DEFAULT);
+ ret += 2 + TFO_COOKIE_LEN_DEFAULT;
+
+ tp->t_tfo_stats |= TFO_S_COOKIE_SENT;
+ tcpstat.tcps_tfo_cookie_sent++;
+
+ return (ret);
+}
+
+static unsigned
+tcp_tfo_write_cookie(struct tcpcb *tp, unsigned optlen, int32_t len,
+ u_char *opt)
+{
+ u_int8_t tfo_len = MAX_TCPOPTLEN - optlen - TCPOLEN_FASTOPEN_REQ;
+ struct socket *so = tp->t_inpcb->inp_socket;
+ unsigned ret = 0;
+ int res;
+ u_char *bp;
+
+ if (so->so_flags1 & SOF1_DATA_AUTHENTICATED) {
+ /* If there is some data, let's track it */
+ if (len > 0) {
+ tp->t_tfo_stats |= TFO_S_SYN_DATA_SENT;
+ tcpstat.tcps_tfo_syn_data_sent++;
+ }
+
+ return (0);
+ }
+
+ bp = opt + optlen;
+
+ /*
+ * The cookie will be copied in the appropriate place within the
+ * TCP-option space. That way we avoid the need for an intermediate
+ * variable.
+ */
+ res = tcp_cache_get_cookie(tp, bp + TCPOLEN_FASTOPEN_REQ, &tfo_len);
+ if (res == 0) {
+ *bp++ = TCPOPT_FASTOPEN;
+ *bp++ = TCPOLEN_FASTOPEN_REQ;
+ ret += TCPOLEN_FASTOPEN_REQ;
+
+ tp->t_tfo_flags |= TFO_F_COOKIE_REQ;
+
+ tp->t_tfo_stats |= TFO_S_COOKIE_REQ;
+ tcpstat.tcps_tfo_cookie_req++;
+ } else {
+ *bp++ = TCPOPT_FASTOPEN;
+ *bp++ = TCPOLEN_FASTOPEN_REQ + tfo_len;
+
+ ret += TCPOLEN_FASTOPEN_REQ + tfo_len;
+
+ tp->t_tfo_flags |= TFO_F_COOKIE_SENT;
+
+ /* If there is some data, let's track it */
+ if (len > 0) {
+ tp->t_tfo_stats |= TFO_S_SYN_DATA_SENT;
+ tcpstat.tcps_tfo_syn_data_sent++;
+ }
+ }
+
+ return (ret);
+}
+
+static inline bool
+tcp_send_ecn_flags_on_syn(struct tcpcb *tp, struct socket *so)
+{
+ return(!((tp->ecn_flags & TE_SETUPSENT) ||
+ (so->so_flags & SOF_MP_SUBFLOW) ||
+ (tp->t_flagsext & TF_FASTOPEN)));
+}
+
+void
+tcp_set_ecn(struct tcpcb *tp, struct ifnet *ifp)
+{
+ boolean_t inbound;
+
+ /*
+ * Socket option has precedence
+ */
+ if (tp->ecn_flags & TE_ECN_MODE_ENABLE) {
+ tp->ecn_flags |= TE_ENABLE_ECN;
+ goto check_heuristic;
+ }
+
+ if (tp->ecn_flags & TE_ECN_MODE_DISABLE) {
+ tp->ecn_flags &= ~TE_ENABLE_ECN;
+ return;
+ }
+ /*
+ * Per interface setting comes next
+ */
+ if (ifp != NULL) {
+ if (ifp->if_eflags & IFEF_ECN_ENABLE) {
+ tp->ecn_flags |= TE_ENABLE_ECN;
+ goto check_heuristic;
+ }
+
+ if (ifp->if_eflags & IFEF_ECN_DISABLE) {
+ tp->ecn_flags &= ~TE_ENABLE_ECN;
+ return;
+ }