1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __SOCK_DIAG_H__ 3 #define __SOCK_DIAG_H__ 4 5 #include <linux/netlink.h> 6 #include <linux/user_namespace.h> 7 #include <net/net_namespace.h> 8 #include <net/sock.h> 9 #include <uapi/linux/sock_diag.h> 10 11 struct sk_buff; 12 struct nlmsghdr; 13 struct sock; 14 15 struct sock_diag_handler { 16 __u8 family; 17 int (*dump)(struct sk_buff *skb, struct nlmsghdr *nlh); 18 int (*get_info)(struct sk_buff *skb, struct sock *sk); 19 int (*destroy)(struct sk_buff *skb, struct nlmsghdr *nlh); 20 }; 21 22 int sock_diag_register(const struct sock_diag_handler *h); 23 void sock_diag_unregister(const struct sock_diag_handler *h); 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 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 41 int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie); 42 void sock_diag_save_cookie(struct sock *sk, __u32 *cookie); 43 44 int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr); 45 int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk, 46 struct sk_buff *skb, int attrtype); 47 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: 53 if (sk->sk_type == SOCK_RAW) 54 return SKNLGRP_NONE; 55 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: 65 if (sk->sk_type == SOCK_RAW) 66 return SKNLGRP_NONE; 67 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 92 int sock_diag_destroy(struct sock *sk, int err); 93 #endif 94