sock_diag.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __SOCK_DIAG_H__
  3. #define __SOCK_DIAG_H__
  4. #include <linux/netlink.h>
  5. #include <linux/user_namespace.h>
  6. #include <net/net_namespace.h>
  7. #include <net/sock.h>
  8. #include <uapi/linux/sock_diag.h>
  9. struct sk_buff;
  10. struct nlmsghdr;
  11. struct sock;
  12. struct sock_diag_handler {
  13. __u8 family;
  14. int (*dump)(struct sk_buff *skb, struct nlmsghdr *nlh);
  15. int (*get_info)(struct sk_buff *skb, struct sock *sk);
  16. int (*destroy)(struct sk_buff *skb, struct nlmsghdr *nlh);
  17. };
  18. int sock_diag_register(const struct sock_diag_handler *h);
  19. void sock_diag_unregister(const struct sock_diag_handler *h);
  20. void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
  21. void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
  22. u64 __sock_gen_cookie(struct sock *sk);
  23. static inline u64 sock_gen_cookie(struct sock *sk)
  24. {
  25. u64 cookie;
  26. preempt_disable();
  27. cookie = __sock_gen_cookie(sk);
  28. preempt_enable();
  29. return cookie;
  30. }
  31. int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
  32. void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);
  33. int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr);
  34. int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
  35. struct sk_buff *skb, int attrtype);
  36. static inline
  37. enum sknetlink_groups sock_diag_destroy_group(const struct sock *sk)
  38. {
  39. switch (sk->sk_family) {
  40. case AF_INET:
  41. if (sk->sk_type == SOCK_RAW)
  42. return SKNLGRP_NONE;
  43. switch (sk->sk_protocol) {
  44. case IPPROTO_TCP:
  45. return SKNLGRP_INET_TCP_DESTROY;
  46. case IPPROTO_UDP:
  47. return SKNLGRP_INET_UDP_DESTROY;
  48. default:
  49. return SKNLGRP_NONE;
  50. }
  51. case AF_INET6:
  52. if (sk->sk_type == SOCK_RAW)
  53. return SKNLGRP_NONE;
  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. static inline
  67. bool sock_diag_has_destroy_listeners(const struct sock *sk)
  68. {
  69. const struct net *n = sock_net(sk);
  70. const enum sknetlink_groups group = sock_diag_destroy_group(sk);
  71. return group != SKNLGRP_NONE && n->diag_nlsk &&
  72. netlink_has_listeners(n->diag_nlsk, group);
  73. }
  74. void sock_diag_broadcast_destroy(struct sock *sk);
  75. int sock_diag_destroy(struct sock *sk, int err);
  76. #endif