blob: a0596ca0e80ac77aeb0afa29648532ef51a5deae [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
Eric Dumazet33cf7c92015-03-11 18:53:14 -070027int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
28void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000029
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000030int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr);
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070031int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
Nicolas Dichtele8d96122013-04-25 06:53:54 +000032 struct sk_buff *skb, int attrtype);
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000033
Craig Gallekeb4cb002015-06-15 11:26:18 -040034static inline
35enum sknetlink_groups sock_diag_destroy_group(const struct sock *sk)
36{
37 switch (sk->sk_family) {
38 case AF_INET:
Willem de Bruijn9a0fee22016-06-24 16:02:35 -040039 if (sk->sk_type == SOCK_RAW)
40 return SKNLGRP_NONE;
41
Craig Gallekeb4cb002015-06-15 11:26:18 -040042 switch (sk->sk_protocol) {
43 case IPPROTO_TCP:
44 return SKNLGRP_INET_TCP_DESTROY;
45 case IPPROTO_UDP:
46 return SKNLGRP_INET_UDP_DESTROY;
47 default:
48 return SKNLGRP_NONE;
49 }
50 case AF_INET6:
Willem de Bruijn9a0fee22016-06-24 16:02:35 -040051 if (sk->sk_type == SOCK_RAW)
52 return SKNLGRP_NONE;
53
Craig Gallekeb4cb002015-06-15 11:26:18 -040054 switch (sk->sk_protocol) {
55 case IPPROTO_TCP:
56 return SKNLGRP_INET6_TCP_DESTROY;
57 case IPPROTO_UDP:
58 return SKNLGRP_INET6_UDP_DESTROY;
59 default:
60 return SKNLGRP_NONE;
61 }
62 default:
63 return SKNLGRP_NONE;
64 }
65}
66
67static inline
68bool sock_diag_has_destroy_listeners(const struct sock *sk)
69{
70 const struct net *n = sock_net(sk);
71 const enum sknetlink_groups group = sock_diag_destroy_group(sk);
72
73 return group != SKNLGRP_NONE && n->diag_nlsk &&
74 netlink_has_listeners(n->diag_nlsk, group);
75}
76void sock_diag_broadcast_destroy(struct sock *sk);
77
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +090078int sock_diag_destroy(struct sock *sk, int err);
Pavel Emelyanovd3664772011-12-06 07:58:03 +000079#endif