]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
tcp: provide SYN headers for passive connections
authorEric Dumazet <edumazet@google.com>
Mon, 4 May 2015 04:34:46 +0000 (21:34 -0700)
committerDavid S. Miller <davem@davemloft.net>
Tue, 5 May 2015 20:02:34 +0000 (16:02 -0400)
This patch allows a server application to get the TCP SYN headers for
its passive connections.  This is useful if the server is doing
fingerprinting of clients based on SYN packet contents.

Two socket options are added: TCP_SAVE_SYN and TCP_SAVED_SYN.

The first is used on a socket to enable saving the SYN headers
for child connections. This can be set before or after the listen()
call.

The latter is used to retrieve the SYN headers for passive connections,
if the parent listener has enabled TCP_SAVE_SYN.

TCP_SAVED_SYN is read once, it frees the saved SYN headers.

The data returned in TCP_SAVED_SYN are network (IPv4/IPv6) and TCP
headers.

Original patch was written by Tom Herbert, I changed it to not hold
a full skb (and associated dst and conntracking reference).

We have used such patch for about 3 years at Google.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Acked-by: Neal Cardwell <ncardwell@google.com>
Tested-by: Neal Cardwell <ncardwell@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/tcp.h
include/net/request_sock.h
include/uapi/linux/tcp.h
net/ipv4/tcp.c
net/ipv4/tcp_input.c
net/ipv4/tcp_ipv4.c
net/ipv4/tcp_minisocks.c

index 3b2911502a8cf4a6e3eaf721e0e0105937ad456e..e6fb5df22db1fb3a2a902581d958e6d4881b399b 100644 (file)
@@ -199,6 +199,7 @@ struct tcp_sock {
                syn_fastopen:1, /* SYN includes Fast Open option */
                syn_fastopen_exp:1,/* SYN includes Fast Open exp. option */
                syn_data_acked:1,/* data in SYN is acked by SYN-ACK */
+               save_syn:1,     /* Save headers of SYN packet */
                is_cwnd_limited:1;/* forward progress limited by snd_cwnd? */
        u32     tlp_high_seq;   /* snd_nxt at the time of TLP retransmit. */
 
@@ -326,6 +327,7 @@ struct tcp_sock {
         * socket. Used to retransmit SYNACKs etc.
         */
        struct request_sock *fastopen_rsk;
+       u32     *saved_syn;
 };
 
 enum tsq_flags {
@@ -393,4 +395,10 @@ static inline int fastopen_init_queue(struct sock *sk, int backlog)
        return 0;
 }
 
+static inline void tcp_saved_syn_free(struct tcp_sock *tp)
+{
+       kfree(tp->saved_syn);
+       tp->saved_syn = NULL;
+}
+
 #endif /* _LINUX_TCP_H */
index 9f4265ce88927b0fe1d7418dbeff5f306855ae15..87935cad2f7b37eadd01d30a3d39a9f73d0145b9 100644 (file)
@@ -64,6 +64,7 @@ struct request_sock {
        struct timer_list               rsk_timer;
        const struct request_sock_ops   *rsk_ops;
        struct sock                     *sk;
+       u32                             *saved_syn;
        u32                             secid;
        u32                             peer_secid;
 };
@@ -77,7 +78,7 @@ reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener)
                req->rsk_ops = ops;
                sock_hold(sk_listener);
                req->rsk_listener = sk_listener;
-
+               req->saved_syn = NULL;
                /* Following is temporary. It is coupled with debugging
                 * helpers in reqsk_put() & reqsk_free()
                 */
@@ -104,6 +105,7 @@ static inline void reqsk_free(struct request_sock *req)
        req->rsk_ops->destructor(req);
        if (req->rsk_listener)
                sock_put(req->rsk_listener);
+       kfree(req->saved_syn);
        kmem_cache_free(req->rsk_ops->slab, req);
 }
 
index faa72f4fa547bcfe643b9cfd32b83b62caf6b973..51ebedba577f36e75b9aefd1cdf9e191f47f734f 100644 (file)
@@ -113,6 +113,8 @@ enum {
 #define TCP_TIMESTAMP          24
 #define TCP_NOTSENT_LOWAT      25      /* limit number of unsent bytes in write queue */
 #define TCP_CC_INFO            26      /* Get Congestion Control (optional) info */
+#define TCP_SAVE_SYN           27      /* Record SYN headers for new connections */
+#define TCP_SAVED_SYN          28      /* Get SYN headers recorded for connection */
 
 struct tcp_repair_opt {
        __u32   opt_code;
index 46efa03d2b11352913654fc336a2ad5e6b957e01..ecccfdc50d76d7b834c1ba54c5763bad437542f0 100644 (file)
@@ -2482,6 +2482,13 @@ static int do_tcp_setsockopt(struct sock *sk, int level,
                        icsk->icsk_syn_retries = val;
                break;
 
+       case TCP_SAVE_SYN:
+               if (val < 0 || val > 1)
+                       err = -EINVAL;
+               else
+                       tp->save_syn = val;
+               break;
+
        case TCP_LINGER2:
                if (val < 0)
                        tp->linger2 = -1;
@@ -2818,6 +2825,34 @@ static int do_tcp_getsockopt(struct sock *sk, int level,
        case TCP_NOTSENT_LOWAT:
                val = tp->notsent_lowat;
                break;
+       case TCP_SAVE_SYN:
+               val = tp->save_syn;
+               break;
+       case TCP_SAVED_SYN: {
+               if (get_user(len, optlen))
+                       return -EFAULT;
+
+               lock_sock(sk);
+               if (tp->saved_syn) {
+                       len = min_t(unsigned int, tp->saved_syn[0], len);
+                       if (put_user(len, optlen)) {
+                               release_sock(sk);
+                               return -EFAULT;
+                       }
+                       if (copy_to_user(optval, tp->saved_syn + 1, len)) {
+                               release_sock(sk);
+                               return -EFAULT;
+                       }
+                       tcp_saved_syn_free(tp);
+                       release_sock(sk);
+               } else {
+                       release_sock(sk);
+                       len = 0;
+                       if (put_user(len, optlen))
+                               return -EFAULT;
+               }
+               return 0;
+       }
        default:
                return -ENOPROTOOPT;
        }
index 09bdc4abfcbb6a55445ca99e601a30ec87f042d7..df2ca615cd0c2221b439a921aeb28bdf2317b85c 100644 (file)
@@ -6060,6 +6060,23 @@ static bool tcp_syn_flood_action(struct sock *sk,
        return want_cookie;
 }
 
+static void tcp_reqsk_record_syn(const struct sock *sk,
+                                struct request_sock *req,
+                                const struct sk_buff *skb)
+{
+       if (tcp_sk(sk)->save_syn) {
+               u32 len = skb_network_header_len(skb) + tcp_hdrlen(skb);
+               u32 *copy;
+
+               copy = kmalloc(len + sizeof(u32), GFP_ATOMIC);
+               if (copy) {
+                       copy[0] = len;
+                       memcpy(&copy[1], skb_network_header(skb), len);
+                       req->saved_syn = copy;
+               }
+       }
+}
+
 int tcp_conn_request(struct request_sock_ops *rsk_ops,
                     const struct tcp_request_sock_ops *af_ops,
                     struct sock *sk, struct sk_buff *skb)
@@ -6192,6 +6209,7 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
                tcp_rsk(req)->tfo_listener = false;
                af_ops->queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
        }
+       tcp_reqsk_record_syn(sk, req, skb);
 
        return 0;
 
index fc1c658ec6c18cb1daa1cc06039a9f19df67bb5e..91cb4768a860345d3ec40c716ec2a10a813e9969 100644 (file)
@@ -1802,6 +1802,7 @@ void tcp_v4_destroy_sock(struct sock *sk)
 
        /* If socket is aborted during connect operation */
        tcp_free_fastopen_req(tp);
+       tcp_saved_syn_free(tp);
 
        sk_sockets_allocated_dec(sk);
        sock_release_memcg(sk);
index e5d7649136fcb31ca70b097dcbd9873df07e7417..ebe2ab2596ed3c2fbd27ecb3bf6f6b66c9e06e08 100644 (file)
@@ -536,6 +536,9 @@ struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req,
                newtp->fastopen_rsk = NULL;
                newtp->syn_data_acked = 0;
 
+               newtp->saved_syn = req->saved_syn;
+               req->saved_syn = NULL;
+
                TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_PASSIVEOPENS);
        }
        return newsk;