]> asedeno.scripts.mit.edu Git - linux.git/blob - include/linux/sock_diag.h
net: init sk_cookie for inet socket
[linux.git] / include / linux / sock_diag.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __SOCK_DIAG_H__
3 #define __SOCK_DIAG_H__
4
5 #include <linux/netlink.h>
6 #include <linux/user_namespace.h>
7 #include <net/net_namespace.h>
8 #include <net/sock.h>
9 #include <uapi/linux/sock_diag.h>
10
11 struct sk_buff;
12 struct nlmsghdr;
13 struct sock;
14
15 struct sock_diag_handler {
16         __u8 family;
17         int (*dump)(struct sk_buff *skb, struct nlmsghdr *nlh);
18         int (*get_info)(struct sk_buff *skb, struct sock *sk);
19         int (*destroy)(struct sk_buff *skb, struct nlmsghdr *nlh);
20 };
21
22 int sock_diag_register(const struct sock_diag_handler *h);
23 void sock_diag_unregister(const struct sock_diag_handler *h);
24
25 void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
26 void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
27
28 static inline
29 void sock_init_cookie(struct sock *sk)
30 {
31         u64 res;
32
33         res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
34         atomic64_set(&sk->sk_cookie, res);
35 }
36
37 u64 sock_gen_cookie(struct sock *sk);
38 int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
39 void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);
40
41 int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr);
42 int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
43                              struct sk_buff *skb, int attrtype);
44
45 static inline
46 enum sknetlink_groups sock_diag_destroy_group(const struct sock *sk)
47 {
48         switch (sk->sk_family) {
49         case AF_INET:
50                 if (sk->sk_type == SOCK_RAW)
51                         return SKNLGRP_NONE;
52
53                 switch (sk->sk_protocol) {
54                 case IPPROTO_TCP:
55                         return SKNLGRP_INET_TCP_DESTROY;
56                 case IPPROTO_UDP:
57                         return SKNLGRP_INET_UDP_DESTROY;
58                 default:
59                         return SKNLGRP_NONE;
60                 }
61         case AF_INET6:
62                 if (sk->sk_type == SOCK_RAW)
63                         return SKNLGRP_NONE;
64
65                 switch (sk->sk_protocol) {
66                 case IPPROTO_TCP:
67                         return SKNLGRP_INET6_TCP_DESTROY;
68                 case IPPROTO_UDP:
69                         return SKNLGRP_INET6_UDP_DESTROY;
70                 default:
71                         return SKNLGRP_NONE;
72                 }
73         default:
74                 return SKNLGRP_NONE;
75         }
76 }
77
78 static inline
79 bool sock_diag_has_destroy_listeners(const struct sock *sk)
80 {
81         const struct net *n = sock_net(sk);
82         const enum sknetlink_groups group = sock_diag_destroy_group(sk);
83
84         return group != SKNLGRP_NONE && n->diag_nlsk &&
85                 netlink_has_listeners(n->diag_nlsk, group);
86 }
87 void sock_diag_broadcast_destroy(struct sock *sk);
88
89 int sock_diag_destroy(struct sock *sk, int err);
90 #endif