]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
net: init sk_cookie for inet socket
authorYafang Shao <laoar.shao@gmail.com>
Sun, 22 Apr 2018 13:50:04 +0000 (21:50 +0800)
committerDavid S. Miller <davem@davemloft.net>
Mon, 23 Apr 2018 15:56:44 +0000 (11:56 -0400)
With sk_cookie we can identify a socket, that is very helpful for
traceing and statistic, i.e. tcp tracepiont and ebpf.
So we'd better init it by default for inet socket.
When using it, we just need call atomic64_read(&sk->sk_cookie).

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/sock_diag.h
net/ipv4/tcp_input.c

index 15fe980a27ead226cdf5cbd00df3c54f2dd944a6..5c916e6dff3627a8c830a1bcbf9624dd3312cc33 100644 (file)
@@ -25,6 +25,15 @@ void sock_diag_unregister(const struct sock_diag_handler *h);
 void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
 void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
 
+static inline
+void sock_init_cookie(struct sock *sk)
+{
+       u64 res;
+
+       res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
+       atomic64_set(&sk->sk_cookie, res);
+}
+
 u64 sock_gen_cookie(struct sock *sk);
 int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
 void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);
index 5a17cfc753260c44382af4003123f5aa3b981be8..17b78582ba631299f3a01e354b846386d6e82050 100644 (file)
@@ -78,6 +78,7 @@
 #include <linux/errqueue.h>
 #include <trace/events/tcp.h>
 #include <linux/static_key.h>
+#include <linux/sock_diag.h>
 
 int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
 
@@ -6190,10 +6191,15 @@ struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
 #if IS_ENABLED(CONFIG_IPV6)
                ireq->pktopts = NULL;
 #endif
-               atomic64_set(&ireq->ir_cookie, 0);
                ireq->ireq_state = TCP_NEW_SYN_RECV;
                write_pnet(&ireq->ireq_net, sock_net(sk_listener));
                ireq->ireq_family = sk_listener->sk_family;
+
+               BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
+                                       offsetof(struct sock, sk_cookie));
+               BUILD_BUG_ON(offsetof(struct inet_request_sock, ireq_net) !=
+                                       offsetof(struct sock, sk_net));
+               sock_init_cookie((struct sock *)ireq);
        }
 
        return req;