Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 2 | #ifndef __SOCK_DIAG_H__ |
| 3 | #define __SOCK_DIAG_H__ |
Pavel Emelyanov | e7c466e | 2011-12-15 02:42:42 +0000 | [diff] [blame] | 4 | |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 5 | #include <linux/netlink.h> |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 6 | #include <linux/user_namespace.h> |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 7 | #include <net/net_namespace.h> |
| 8 | #include <net/sock.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 9 | #include <uapi/linux/sock_diag.h> |
Pavel Emelyanov | e7c466e | 2011-12-15 02:42:42 +0000 | [diff] [blame] | 10 | |
Pavel Emelyanov | e6fe237 | 2011-12-30 00:52:21 +0000 | [diff] [blame] | 11 | struct sk_buff; |
| 12 | struct nlmsghdr; |
Pavel Emelyanov | 5d2e5f27 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 13 | struct sock; |
Pavel Emelyanov | e6fe237 | 2011-12-30 00:52:21 +0000 | [diff] [blame] | 14 | |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 15 | struct sock_diag_handler { |
| 16 | __u8 family; |
| 17 | int (*dump)(struct sk_buff *skb, struct nlmsghdr *nlh); |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 18 | int (*get_info)(struct sk_buff *skb, struct sock *sk); |
Lorenzo Colitti | 64be0ae | 2015-12-16 12:30:03 +0900 | [diff] [blame] | 19 | int (*destroy)(struct sk_buff *skb, struct nlmsghdr *nlh); |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 20 | }; |
| 21 | |
Shan Wei | 8dcf01f | 2012-04-24 18:21:07 +0000 | [diff] [blame] | 22 | int sock_diag_register(const struct sock_diag_handler *h); |
| 23 | void sock_diag_unregister(const struct sock_diag_handler *h); |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 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 | |
Daniel Borkmann | 92acdc5 | 2020-09-30 17:18:16 +0200 | [diff] [blame] | 28 | u64 __sock_gen_cookie(struct sock *sk); |
| 29 | |
| 30 | static 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 Dumazet | 33cf7c9 | 2015-03-11 18:53:14 -0700 | [diff] [blame] | 41 | int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie); |
| 42 | void sock_diag_save_cookie(struct sock *sk, __u32 *cookie); |
Pavel Emelyanov | f65c1b5 | 2011-12-15 02:43:44 +0000 | [diff] [blame] | 43 | |
Pavel Emelyanov | 5d2e5f27 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 44 | int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr); |
Eric W. Biederman | a53b72c | 2014-04-23 14:26:25 -0700 | [diff] [blame] | 45 | int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk, |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 46 | struct sk_buff *skb, int attrtype); |
Pavel Emelyanov | 5d2e5f27 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 47 | |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 48 | static inline |
| 49 | enum sknetlink_groups sock_diag_destroy_group(const struct sock *sk) |
| 50 | { |
| 51 | switch (sk->sk_family) { |
| 52 | case AF_INET: |
Willem de Bruijn | 9a0fee2 | 2016-06-24 16:02:35 -0400 | [diff] [blame] | 53 | if (sk->sk_type == SOCK_RAW) |
| 54 | return SKNLGRP_NONE; |
| 55 | |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 56 | 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 Bruijn | 9a0fee2 | 2016-06-24 16:02:35 -0400 | [diff] [blame] | 65 | if (sk->sk_type == SOCK_RAW) |
| 66 | return SKNLGRP_NONE; |
| 67 | |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 68 | 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 | |
| 81 | static inline |
| 82 | bool 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 | } |
| 90 | void sock_diag_broadcast_destroy(struct sock *sk); |
| 91 | |
Lorenzo Colitti | 64be0ae | 2015-12-16 12:30:03 +0900 | [diff] [blame] | 92 | int sock_diag_destroy(struct sock *sk, int err); |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 93 | #endif |