blob: a2f8109bb215751427e99b3c026badfe7113b875 [file] [log] [blame]
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001#ifndef __SOCK_DIAG_H__
2#define __SOCK_DIAG_H__
Pavel Emelyanove7c466e2011-12-15 02:42:42 +00003
Craig Gallekeb4cb002015-06-15 11:26:18 -04004#include <linux/netlink.h>
Nicolas Dichtele8d96122013-04-25 06:53:54 +00005#include <linux/user_namespace.h>
Craig Gallekeb4cb002015-06-15 11:26:18 -04006#include <net/net_namespace.h>
7#include <net/sock.h>
David Howells607ca462012-10-13 10:46:48 +01008#include <uapi/linux/sock_diag.h>
Pavel Emelyanove7c466e2011-12-15 02:42:42 +00009
Pavel Emelyanove6fe2372011-12-30 00:52:21 +000010struct sk_buff;
11struct nlmsghdr;
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000012struct sock;
Pavel Emelyanove6fe2372011-12-30 00:52:21 +000013
Pavel Emelyanovd3664772011-12-06 07:58:03 +000014struct sock_diag_handler {
15 __u8 family;
16 int (*dump)(struct sk_buff *skb, struct nlmsghdr *nlh);
Craig Gallekeb4cb002015-06-15 11:26:18 -040017 int (*get_info)(struct sk_buff *skb, struct sock *sk);
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +090018 int (*destroy)(struct sk_buff *skb, struct nlmsghdr *nlh);
Pavel Emelyanovd3664772011-12-06 07:58:03 +000019};
20
Shan Wei8dcf01f2012-04-24 18:21:07 +000021int sock_diag_register(const struct sock_diag_handler *h);
22void sock_diag_unregister(const struct sock_diag_handler *h);
Pavel Emelyanovd3664772011-12-06 07:58:03 +000023
24void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
25void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
26
Chenbo Feng09313662017-03-22 17:27:34 -070027u64 sock_gen_cookie(struct sock *sk);
Eric Dumazet33cf7c92015-03-11 18:53:14 -070028int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
29void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000030
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000031int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr);
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070032int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
Nicolas Dichtele8d96122013-04-25 06:53:54 +000033 struct sk_buff *skb, int attrtype);
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000034
Craig Gallekeb4cb002015-06-15 11:26:18 -040035static inline
36enum sknetlink_groups sock_diag_destroy_group(const struct sock *sk)
37{
38 switch (sk->sk_family) {
39 case AF_INET:
Willem de Bruijn9a0fee22016-06-24 16:02:35 -040040 if (sk->sk_type == SOCK_RAW)
41 return SKNLGRP_NONE;
42
Craig Gallekeb4cb002015-06-15 11:26:18 -040043 switch (sk->sk_protocol) {
44 case IPPROTO_TCP:
45 return SKNLGRP_INET_TCP_DESTROY;
46 case IPPROTO_UDP:
47 return SKNLGRP_INET_UDP_DESTROY;
48 default:
49 return SKNLGRP_NONE;
50 }
51 case AF_INET6:
Willem de Bruijn9a0fee22016-06-24 16:02:35 -040052 if (sk->sk_type == SOCK_RAW)
53 return SKNLGRP_NONE;
54
Craig Gallekeb4cb002015-06-15 11:26:18 -040055 switch (sk->sk_protocol) {
56 case IPPROTO_TCP:
57 return SKNLGRP_INET6_TCP_DESTROY;
58 case IPPROTO_UDP:
59 return SKNLGRP_INET6_UDP_DESTROY;
60 default:
61 return SKNLGRP_NONE;
62 }
63 default:
64 return SKNLGRP_NONE;
65 }
66}
67
68static inline
69bool sock_diag_has_destroy_listeners(const struct sock *sk)
70{
71 const struct net *n = sock_net(sk);
72 const enum sknetlink_groups group = sock_diag_destroy_group(sk);
73
74 return group != SKNLGRP_NONE && n->diag_nlsk &&
75 netlink_has_listeners(n->diag_nlsk, group);
76}
77void sock_diag_broadcast_destroy(struct sock *sk);
78
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +090079int sock_diag_destroy(struct sock *sk, int err);
Pavel Emelyanovd3664772011-12-06 07:58:03 +000080#endif