sock_diag.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <linux/mutex.h>
  2. #include <linux/socket.h>
  3. #include <linux/skbuff.h>
  4. #include <net/netlink.h>
  5. #include <net/net_namespace.h>
  6. #include <linux/module.h>
  7. #include <linux/inet_diag.h>
  8. #include <linux/sock_diag.h>
  9. static struct sock_diag_handler *sock_diag_handlers[AF_MAX];
  10. static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
  11. static DEFINE_MUTEX(sock_diag_table_mutex);
  12. void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
  13. {
  14. mutex_lock(&sock_diag_table_mutex);
  15. inet_rcv_compat = fn;
  16. mutex_unlock(&sock_diag_table_mutex);
  17. }
  18. EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
  19. void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
  20. {
  21. mutex_lock(&sock_diag_table_mutex);
  22. inet_rcv_compat = NULL;
  23. mutex_unlock(&sock_diag_table_mutex);
  24. }
  25. EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
  26. int sock_diag_register(struct sock_diag_handler *hndl)
  27. {
  28. int err = 0;
  29. if (hndl->family >= AF_MAX)
  30. return -EINVAL;
  31. mutex_lock(&sock_diag_table_mutex);
  32. if (sock_diag_handlers[hndl->family])
  33. err = -EBUSY;
  34. else
  35. sock_diag_handlers[hndl->family] = hndl;
  36. mutex_unlock(&sock_diag_table_mutex);
  37. return err;
  38. }
  39. EXPORT_SYMBOL_GPL(sock_diag_register);
  40. void sock_diag_unregister(struct sock_diag_handler *hnld)
  41. {
  42. int family = hnld->family;
  43. if (family >= AF_MAX)
  44. return;
  45. mutex_lock(&sock_diag_table_mutex);
  46. BUG_ON(sock_diag_handlers[family] != hnld);
  47. sock_diag_handlers[family] = NULL;
  48. mutex_unlock(&sock_diag_table_mutex);
  49. }
  50. EXPORT_SYMBOL_GPL(sock_diag_unregister);
  51. static inline struct sock_diag_handler *sock_diag_lock_handler(int family)
  52. {
  53. if (sock_diag_handlers[family] == NULL)
  54. request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
  55. NETLINK_SOCK_DIAG, IPPROTO_IP);
  56. mutex_lock(&sock_diag_table_mutex);
  57. return sock_diag_handlers[family];
  58. }
  59. static inline void sock_diag_unlock_handler(struct sock_diag_handler *h)
  60. {
  61. mutex_unlock(&sock_diag_table_mutex);
  62. }
  63. static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  64. {
  65. int err;
  66. struct sock_diag_req *req = NLMSG_DATA(nlh);
  67. struct sock_diag_handler *hndl;
  68. if (nlmsg_len(nlh) < sizeof(*req))
  69. return -EINVAL;
  70. hndl = sock_diag_lock_handler(req->sdiag_family);
  71. if (hndl == NULL)
  72. err = -ENOENT;
  73. else
  74. err = hndl->dump(skb, nlh);
  75. sock_diag_unlock_handler(hndl);
  76. return err;
  77. }
  78. static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  79. {
  80. int ret;
  81. switch (nlh->nlmsg_type) {
  82. case TCPDIAG_GETSOCK:
  83. case DCCPDIAG_GETSOCK:
  84. if (inet_rcv_compat == NULL)
  85. request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
  86. NETLINK_SOCK_DIAG, IPPROTO_IP);
  87. mutex_lock(&sock_diag_table_mutex);
  88. if (inet_rcv_compat != NULL)
  89. ret = inet_rcv_compat(skb, nlh);
  90. else
  91. ret = -EOPNOTSUPP;
  92. mutex_unlock(&sock_diag_table_mutex);
  93. return ret;
  94. case SOCK_DIAG_BY_FAMILY:
  95. return __sock_diag_rcv_msg(skb, nlh);
  96. default:
  97. return -EINVAL;
  98. }
  99. }
  100. static DEFINE_MUTEX(sock_diag_mutex);
  101. static void sock_diag_rcv(struct sk_buff *skb)
  102. {
  103. mutex_lock(&sock_diag_mutex);
  104. netlink_rcv_skb(skb, &sock_diag_rcv_msg);
  105. mutex_unlock(&sock_diag_mutex);
  106. }
  107. struct sock *sock_diag_nlsk;
  108. EXPORT_SYMBOL_GPL(sock_diag_nlsk);
  109. static int __init sock_diag_init(void)
  110. {
  111. sock_diag_nlsk = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0,
  112. sock_diag_rcv, NULL, THIS_MODULE);
  113. return sock_diag_nlsk == NULL ? -ENOMEM : 0;
  114. }
  115. static void __exit sock_diag_exit(void)
  116. {
  117. netlink_kernel_release(sock_diag_nlsk);
  118. }
  119. module_init(sock_diag_init);
  120. module_exit(sock_diag_exit);
  121. MODULE_LICENSE("GPL");
  122. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);