Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __SOCK_DIAG_H__
0003 #define __SOCK_DIAG_H__
0004 
0005 #include <linux/netlink.h>
0006 #include <linux/user_namespace.h>
0007 #include <net/net_namespace.h>
0008 #include <net/sock.h>
0009 #include <uapi/linux/sock_diag.h>
0010 
0011 struct sk_buff;
0012 struct nlmsghdr;
0013 struct sock;
0014 
0015 struct sock_diag_handler {
0016     __u8 family;
0017     int (*dump)(struct sk_buff *skb, struct nlmsghdr *nlh);
0018     int (*get_info)(struct sk_buff *skb, struct sock *sk);
0019     int (*destroy)(struct sk_buff *skb, struct nlmsghdr *nlh);
0020 };
0021 
0022 int sock_diag_register(const struct sock_diag_handler *h);
0023 void sock_diag_unregister(const struct sock_diag_handler *h);
0024 
0025 void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
0026 void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
0027 
0028 u64 __sock_gen_cookie(struct sock *sk);
0029 
0030 static inline u64 sock_gen_cookie(struct sock *sk)
0031 {
0032     u64 cookie;
0033 
0034     preempt_disable();
0035     cookie = __sock_gen_cookie(sk);
0036     preempt_enable();
0037 
0038     return cookie;
0039 }
0040 
0041 int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
0042 void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);
0043 
0044 int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr);
0045 int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
0046                  struct sk_buff *skb, int attrtype);
0047 
0048 static inline
0049 enum sknetlink_groups sock_diag_destroy_group(const struct sock *sk)
0050 {
0051     switch (sk->sk_family) {
0052     case AF_INET:
0053         if (sk->sk_type == SOCK_RAW)
0054             return SKNLGRP_NONE;
0055 
0056         switch (sk->sk_protocol) {
0057         case IPPROTO_TCP:
0058             return SKNLGRP_INET_TCP_DESTROY;
0059         case IPPROTO_UDP:
0060             return SKNLGRP_INET_UDP_DESTROY;
0061         default:
0062             return SKNLGRP_NONE;
0063         }
0064     case AF_INET6:
0065         if (sk->sk_type == SOCK_RAW)
0066             return SKNLGRP_NONE;
0067 
0068         switch (sk->sk_protocol) {
0069         case IPPROTO_TCP:
0070             return SKNLGRP_INET6_TCP_DESTROY;
0071         case IPPROTO_UDP:
0072             return SKNLGRP_INET6_UDP_DESTROY;
0073         default:
0074             return SKNLGRP_NONE;
0075         }
0076     default:
0077         return SKNLGRP_NONE;
0078     }
0079 }
0080 
0081 static inline
0082 bool sock_diag_has_destroy_listeners(const struct sock *sk)
0083 {
0084     const struct net *n = sock_net(sk);
0085     const enum sknetlink_groups group = sock_diag_destroy_group(sk);
0086 
0087     return group != SKNLGRP_NONE && n->diag_nlsk &&
0088         netlink_has_listeners(n->diag_nlsk, group);
0089 }
0090 void sock_diag_broadcast_destroy(struct sock *sk);
0091 
0092 int sock_diag_destroy(struct sock *sk, int err);
0093 #endif