blob: 0b9ecd8cf9793bc26138a0a36474e78773fb4f31 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Pavel Emelyanovd3664772011-12-06 07:58:03 +00002#ifndef __SOCK_DIAG_H__
3#define __SOCK_DIAG_H__
Pavel Emelyanove7c466e2011-12-15 02:42:42 +00004
Craig Gallekeb4cb002015-06-15 11:26:18 -04005#include <linux/netlink.h>
Nicolas Dichtele8d96122013-04-25 06:53:54 +00006#include <linux/user_namespace.h>
Craig Gallekeb4cb002015-06-15 11:26:18 -04007#include <net/net_namespace.h>
8#include <net/sock.h>
David Howells607ca462012-10-13 10:46:48 +01009#include <uapi/linux/sock_diag.h>
Pavel Emelyanove7c466e2011-12-15 02:42:42 +000010
Pavel Emelyanove6fe2372011-12-30 00:52:21 +000011struct sk_buff;
12struct nlmsghdr;
Pavel Emelyanov5d2e5f272011-12-30 00:53:13 +000013struct sock;
Pavel Emelyanove6fe2372011-12-30 00:52:21 +000014
Pavel Emelyanovd3664772011-12-06 07:58:03 +000015struct sock_diag_handler {
16 __u8 family;
17 int (*dump)(struct sk_buff *skb, struct nlmsghdr *nlh);
Craig Gallekeb4cb002015-06-15 11:26:18 -040018 int (*get_info)(struct sk_buff *skb, struct sock *sk);
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +090019 int (*destroy)(struct sk_buff *skb, struct nlmsghdr *nlh);
Pavel Emelyanovd3664772011-12-06 07:58:03 +000020};
21
Shan Wei8dcf01f2012-04-24 18:21:07 +000022int sock_diag_register(const struct sock_diag_handler *h);
23void sock_diag_unregister(const struct sock_diag_handler *h);
Pavel Emelyanovd3664772011-12-06 07:58:03 +000024
25void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
26void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
27
Daniel Borkmann92acdc52020-09-30 17:18:16 +020028u64 __sock_gen_cookie(struct sock *sk);
29
30static inline u64 sock_gen_cookie(struct sock *sk)
31{
32 u64 cookie;
33
34 preempt_disable();
35 cookie = __sock_gen_cookie(sk);
36 preempt_enable();
37
38 return cookie;
39}
40
Eric Dumazet33cf7c92015-03-11 18:53:14 -070041int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
42void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000043
Pavel Emelyanov5d2e5f272011-12-30 00:53:13 +000044int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr);
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070045int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
Nicolas Dichtele8d96122013-04-25 06:53:54 +000046 struct sk_buff *skb, int attrtype);
Pavel Emelyanov5d2e5f272011-12-30 00:53:13 +000047
Craig Gallekeb4cb002015-06-15 11:26:18 -040048static inline
49enum sknetlink_groups sock_diag_destroy_group(const struct sock *sk)
50{
51 switch (sk->sk_family) {
52 case AF_INET:
Willem de Bruijn9a0fee22016-06-24 16:02:35 -040053 if (sk->sk_type == SOCK_RAW)
54 return SKNLGRP_NONE;
55
Craig Gallekeb4cb002015-06-15 11:26:18 -040056 switch (sk->sk_protocol) {
57 case IPPROTO_TCP:
58 return SKNLGRP_INET_TCP_DESTROY;
59 case IPPROTO_UDP:
60 return SKNLGRP_INET_UDP_DESTROY;
61 default:
62 return SKNLGRP_NONE;
63 }
64 case AF_INET6:
Willem de Bruijn9a0fee22016-06-24 16:02:35 -040065 if (sk->sk_type == SOCK_RAW)
66 return SKNLGRP_NONE;
67
Craig Gallekeb4cb002015-06-15 11:26:18 -040068 switch (sk->sk_protocol) {
69 case IPPROTO_TCP:
70 return SKNLGRP_INET6_TCP_DESTROY;
71 case IPPROTO_UDP:
72 return SKNLGRP_INET6_UDP_DESTROY;
73 default:
74 return SKNLGRP_NONE;
75 }
76 default:
77 return SKNLGRP_NONE;
78 }
79}
80
81static inline
82bool sock_diag_has_destroy_listeners(const struct sock *sk)
83{
84 const struct net *n = sock_net(sk);
85 const enum sknetlink_groups group = sock_diag_destroy_group(sk);
86
87 return group != SKNLGRP_NONE && n->diag_nlsk &&
88 netlink_has_listeners(n->diag_nlsk, group);
89}
90void sock_diag_broadcast_destroy(struct sock *sk);
91
Lorenzo Colitti64be0ae2015-12-16 12:30:03 +090092int sock_diag_destroy(struct sock *sk, int err);
Pavel Emelyanovd3664772011-12-06 07:58:03 +000093#endif