Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 1 | #ifndef __SOCK_DIAG_H__ |
| 2 | #define __SOCK_DIAG_H__ |
Pavel Emelyanov | e7c466e | 2011-12-15 02:42:42 +0000 | [diff] [blame] | 3 | |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 4 | #include <linux/netlink.h> |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 5 | #include <linux/user_namespace.h> |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 6 | #include <net/net_namespace.h> |
| 7 | #include <net/sock.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 8 | #include <uapi/linux/sock_diag.h> |
Pavel Emelyanov | e7c466e | 2011-12-15 02:42:42 +0000 | [diff] [blame] | 9 | |
Pavel Emelyanov | e6fe237 | 2011-12-30 00:52:21 +0000 | [diff] [blame] | 10 | struct sk_buff; |
| 11 | struct nlmsghdr; |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 12 | struct sock; |
Pavel Emelyanov | e6fe237 | 2011-12-30 00:52:21 +0000 | [diff] [blame] | 13 | |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 14 | struct sock_diag_handler { |
| 15 | __u8 family; |
| 16 | int (*dump)(struct sk_buff *skb, struct nlmsghdr *nlh); |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 17 | int (*get_info)(struct sk_buff *skb, struct sock *sk); |
Lorenzo Colitti | 64be0ae | 2015-12-16 12:30:03 +0900 | [diff] [blame] | 18 | int (*destroy)(struct sk_buff *skb, struct nlmsghdr *nlh); |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 19 | }; |
| 20 | |
Shan Wei | 8dcf01f | 2012-04-24 18:21:07 +0000 | [diff] [blame] | 21 | int sock_diag_register(const struct sock_diag_handler *h); |
| 22 | void sock_diag_unregister(const struct sock_diag_handler *h); |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 23 | |
| 24 | void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)); |
| 25 | void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)); |
| 26 | |
Eric Dumazet | 33cf7c9 | 2015-03-11 18:53:14 -0700 | [diff] [blame] | 27 | int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie); |
| 28 | void sock_diag_save_cookie(struct sock *sk, __u32 *cookie); |
Pavel Emelyanov | f65c1b5 | 2011-12-15 02:43:44 +0000 | [diff] [blame] | 29 | |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 30 | 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] | 31 | int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk, |
Nicolas Dichtel | e8d9612 | 2013-04-25 06:53:54 +0000 | [diff] [blame] | 32 | struct sk_buff *skb, int attrtype); |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 33 | |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 34 | static inline |
| 35 | enum sknetlink_groups sock_diag_destroy_group(const struct sock *sk) |
| 36 | { |
| 37 | switch (sk->sk_family) { |
| 38 | case AF_INET: |
Willem de Bruijn | 9a0fee2 | 2016-06-24 16:02:35 -0400 | [diff] [blame] | 39 | if (sk->sk_type == SOCK_RAW) |
| 40 | return SKNLGRP_NONE; |
| 41 | |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 42 | 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 Bruijn | 9a0fee2 | 2016-06-24 16:02:35 -0400 | [diff] [blame] | 51 | if (sk->sk_type == SOCK_RAW) |
| 52 | return SKNLGRP_NONE; |
| 53 | |
Craig Gallek | eb4cb00 | 2015-06-15 11:26:18 -0400 | [diff] [blame] | 54 | 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 | |
| 67 | static inline |
| 68 | bool 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 | } |
| 76 | void sock_diag_broadcast_destroy(struct sock *sk); |
| 77 | |
Lorenzo Colitti | 64be0ae | 2015-12-16 12:30:03 +0900 | [diff] [blame] | 78 | int sock_diag_destroy(struct sock *sk, int err); |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 79 | #endif |