sock_diag.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
  24. void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);
  25. int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attr);
  26. int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
  27. struct sk_buff *skb, int attrtype);
  28. static inline
  29. enum sknetlink_groups sock_diag_destroy_group(const struct sock *sk)
  30. {
  31. switch (sk->sk_family) {
  32. case AF_INET:
  33. if (sk->sk_type == SOCK_RAW)
  34. return SKNLGRP_NONE;
  35. switch (sk->sk_protocol) {
  36. case IPPROTO_TCP:
  37. return SKNLGRP_INET_TCP_DESTROY;
  38. case IPPROTO_UDP:
  39. return SKNLGRP_INET_UDP_DESTROY;
  40. default:
  41. return SKNLGRP_NONE;
  42. }
  43. case AF_INET6:
  44. if (sk->sk_type == SOCK_RAW)
  45. return SKNLGRP_NONE;
  46. switch (sk->sk_protocol) {
  47. case IPPROTO_TCP:
  48. return SKNLGRP_INET6_TCP_DESTROY;
  49. case IPPROTO_UDP:
  50. return SKNLGRP_INET6_UDP_DESTROY;
  51. default:
  52. return SKNLGRP_NONE;
  53. }
  54. default:
  55. return SKNLGRP_NONE;
  56. }
  57. }
  58. static inline
  59. bool sock_diag_has_destroy_listeners(const struct sock *sk)
  60. {
  61. const struct net *n = sock_net(sk);
  62. const enum sknetlink_groups group = sock_diag_destroy_group(sk);
  63. return group != SKNLGRP_NONE && n->diag_nlsk &&
  64. netlink_has_listeners(n->diag_nlsk, group);
  65. }
  66. void sock_diag_broadcast_destroy(struct sock *sk);
  67. int sock_diag_destroy(struct sock *sk, int err);
  68. #endif