#define        TFO_SERVER_ENABLE       2
 #define        TFO_CLIENT_NO_COOKIE    4       /* Data in SYN w/o cookie option */
 
-/* Process SYN data but skip cookie validation */
-#define        TFO_SERVER_COOKIE_NOT_CHKED     0x100
 /* Accept SYN data w/o any cookie option */
 #define        TFO_SERVER_COOKIE_NOT_REQD      0x200
 
  */
 #define        TFO_SERVER_WO_SOCKOPT1  0x400
 #define        TFO_SERVER_WO_SOCKOPT2  0x800
-/* Always create TFO child sockets on a TFO listener even when
- * cookie/data not present. (For testing purpose!)
- */
-#define        TFO_SERVER_ALWAYS       0x1000
 
 extern struct inet_timewait_death_row tcp_death_row;
 
                              struct request_sock *req);
 bool tcp_fastopen_check(struct sock *sk, struct sk_buff *skb,
                        struct request_sock *req,
-                       struct tcp_fastopen_cookie *foc,
-                       struct tcp_fastopen_cookie *valid_foc);
+                       struct tcp_fastopen_cookie *foc);
 void tcp_fastopen_init_key_once(bool publish);
 #define TCP_FASTOPEN_KEY_LENGTH 16
 
 
        return true;
 }
 
+/* Returns true if we should perform Fast Open on the SYN. The cookie (foc)
+ * may be updated and return the client in the SYN-ACK later. E.g., Fast Open
+ * cookie request (foc->len == 0).
+ */
 bool tcp_fastopen_check(struct sock *sk, struct sk_buff *skb,
                        struct request_sock *req,
-                       struct tcp_fastopen_cookie *foc,
-                       struct tcp_fastopen_cookie *valid_foc)
+                       struct tcp_fastopen_cookie *foc)
 {
-       bool skip_cookie = false;
-
-       if (likely(!fastopen_cookie_present(foc))) {
-               /* See include/net/tcp.h for the meaning of these knobs */
-               if ((sysctl_tcp_fastopen & TFO_SERVER_ALWAYS) ||
-                   ((sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_REQD) &&
-                   (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1)))
-                       skip_cookie = true; /* no cookie to validate */
-               else
-                       return false;
-       }
-       /* A FO option is present; bump the counter. */
-       NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVE);
+       struct tcp_fastopen_cookie valid_foc = { .len = -1 };
+       bool syn_data = TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1;
 
-       if ((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) == 0 ||
-           !tcp_fastopen_queue_check(sk))
+       if (!((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) &&
+             (syn_data || foc->len >= 0) &&
+             tcp_fastopen_queue_check(sk))) {
+               foc->len = -1;
                return false;
-
-       if (skip_cookie) {
-               tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
-               return true;
        }
 
-       if (foc->len == TCP_FASTOPEN_COOKIE_SIZE) {
-               if ((sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_CHKED) == 0) {
-                       tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr,
-                                               ip_hdr(skb)->daddr, valid_foc);
-                       if ((valid_foc->len != TCP_FASTOPEN_COOKIE_SIZE) ||
-                           memcmp(&foc->val[0], &valid_foc->val[0],
-                           TCP_FASTOPEN_COOKIE_SIZE) != 0)
-                               return false;
-                       valid_foc->len = -1;
-               }
-               /* Acknowledge the data received from the peer. */
+       if (syn_data && (sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_REQD))
+               goto fastopen;
+
+       tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr,
+                               ip_hdr(skb)->daddr, &valid_foc);
+
+       if (foc->len == TCP_FASTOPEN_COOKIE_SIZE &&
+           foc->len == valid_foc.len &&
+           !memcmp(foc->val, valid_foc.val, foc->len)) {
+fastopen:
                tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
+               foc->len = -1;
+               NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVE);
                return true;
-       } else if (foc->len == 0) { /* Client requesting a cookie */
-               tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr,
-                                       ip_hdr(skb)->daddr, valid_foc);
-               NET_INC_STATS_BH(sock_net(sk),
-                   LINUX_MIB_TCPFASTOPENCOOKIEREQD);
-       } else {
-               /* Client sent a cookie with wrong size. Treat it
-                * the same as invalid and return a valid one.
-                */
-               tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr,
-                                       ip_hdr(skb)->daddr, valid_foc);
        }
+
+       NET_INC_STATS_BH(sock_net(sk), foc->len ?
+                        LINUX_MIB_TCPFASTOPENPASSIVEFAIL :
+                        LINUX_MIB_TCPFASTOPENCOOKIEREQD);
+       *foc = valid_foc;
        return false;
 }
 EXPORT_SYMBOL(tcp_fastopen_check);
 
        bool want_cookie = false;
        struct flowi4 fl4;
        struct tcp_fastopen_cookie foc = { .len = -1 };
-       struct tcp_fastopen_cookie valid_foc = { .len = -1 };
        struct sk_buff *skb_synack;
        int do_fastopen;
 
                if (dst == NULL)
                        goto drop_and_free;
        }
-       do_fastopen = tcp_fastopen_check(sk, skb, req, &foc, &valid_foc);
+       do_fastopen = !want_cookie &&
+                     tcp_fastopen_check(sk, skb, req, &foc);
 
        /* We don't call tcp_v4_send_synack() directly because we need
         * to make sure a child socket can be created successfully before
         * latter to remove its dependency on the current implementation
         * of tcp_v4_send_synack()->tcp_select_initial_window().
         */
-       skb_synack = tcp_make_synack(sk, dst, req,
-           fastopen_cookie_present(&valid_foc) ? &valid_foc : NULL);
+       skb_synack = tcp_make_synack(sk, dst, req, &foc);
 
        if (skb_synack) {
                __tcp_v4_send_check(skb_synack, ireq->ir_loc_addr, ireq->ir_rmt_addr);
                tcp_rsk(req)->listener = NULL;
                /* Add the request_sock to the SYN table */
                inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
-               if (fastopen_cookie_present(&foc) && foc.len != 0)
-                       NET_INC_STATS_BH(sock_net(sk),
-                           LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
        } else if (tcp_fastopen_create_child(sk, skb, skb_synack, req))
                goto drop_and_release;