diag.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * net/tipc/diag.c: TIPC socket diag
  3. *
  4. * Copyright (c) 2018, Ericsson AB
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright holders nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * Alternatively, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") version 2 as published by the Free
  21. * Software Foundation.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "ASIS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include "core.h"
  36. #include "socket.h"
  37. #include <linux/sock_diag.h>
  38. #include <linux/tipc_sockets_diag.h>
  39. static u64 __tipc_diag_gen_cookie(struct sock *sk)
  40. {
  41. u32 res[2];
  42. sock_diag_save_cookie(sk, res);
  43. return *((u64 *)res);
  44. }
  45. static int __tipc_add_sock_diag(struct sk_buff *skb,
  46. struct netlink_callback *cb,
  47. struct tipc_sock *tsk)
  48. {
  49. struct tipc_sock_diag_req *req = nlmsg_data(cb->nlh);
  50. struct nlmsghdr *nlh;
  51. int err;
  52. nlh = nlmsg_put_answer(skb, cb, SOCK_DIAG_BY_FAMILY, 0,
  53. NLM_F_MULTI);
  54. if (!nlh)
  55. return -EMSGSIZE;
  56. err = tipc_sk_fill_sock_diag(skb, cb, tsk, req->tidiag_states,
  57. __tipc_diag_gen_cookie);
  58. if (err)
  59. return err;
  60. nlmsg_end(skb, nlh);
  61. return 0;
  62. }
  63. static int tipc_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
  64. {
  65. return tipc_nl_sk_walk(skb, cb, __tipc_add_sock_diag);
  66. }
  67. static int tipc_sock_diag_handler_dump(struct sk_buff *skb,
  68. struct nlmsghdr *h)
  69. {
  70. int hdrlen = sizeof(struct tipc_sock_diag_req);
  71. struct net *net = sock_net(skb->sk);
  72. if (nlmsg_len(h) < hdrlen)
  73. return -EINVAL;
  74. if (h->nlmsg_flags & NLM_F_DUMP) {
  75. struct netlink_dump_control c = {
  76. .start = tipc_dump_start,
  77. .dump = tipc_diag_dump,
  78. .done = tipc_dump_done,
  79. };
  80. netlink_dump_start(net->diag_nlsk, skb, h, &c);
  81. return 0;
  82. }
  83. return -EOPNOTSUPP;
  84. }
  85. static const struct sock_diag_handler tipc_sock_diag_handler = {
  86. .family = AF_TIPC,
  87. .dump = tipc_sock_diag_handler_dump,
  88. };
  89. static int __init tipc_diag_init(void)
  90. {
  91. return sock_diag_register(&tipc_sock_diag_handler);
  92. }
  93. static void __exit tipc_diag_exit(void)
  94. {
  95. sock_diag_unregister(&tipc_sock_diag_handler);
  96. }
  97. module_init(tipc_diag_init);
  98. module_exit(tipc_diag_exit);
  99. MODULE_LICENSE("Dual BSD/GPL");
  100. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, AF_TIPC);