udp_diag.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * udp_diag.c Module for monitoring UDP transport protocols sockets.
  3. *
  4. * Authors: Pavel Emelyanov, <xemul@parallels.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/inet_diag.h>
  13. #include <linux/udp.h>
  14. #include <net/udp.h>
  15. #include <net/udplite.h>
  16. #include <linux/inet_diag.h>
  17. #include <linux/sock_diag.h>
  18. static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
  19. struct netlink_callback *cb, struct inet_diag_req *req,
  20. struct nlattr *bc)
  21. {
  22. if (!inet_diag_bc_sk(bc, sk))
  23. return 0;
  24. return inet_sk_diag_fill(sk, NULL, skb, req, NETLINK_CB(cb->skb).pid,
  25. cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
  26. }
  27. static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
  28. const struct nlmsghdr *nlh, struct inet_diag_req *req)
  29. {
  30. int err = -EINVAL;
  31. struct sock *sk;
  32. struct sk_buff *rep;
  33. if (req->sdiag_family == AF_INET)
  34. sk = __udp4_lib_lookup(&init_net,
  35. req->id.idiag_src[0], req->id.idiag_sport,
  36. req->id.idiag_dst[0], req->id.idiag_dport,
  37. req->id.idiag_if, tbl);
  38. else if (req->sdiag_family == AF_INET6)
  39. sk = __udp6_lib_lookup(&init_net,
  40. (struct in6_addr *)req->id.idiag_src,
  41. req->id.idiag_sport,
  42. (struct in6_addr *)req->id.idiag_dst,
  43. req->id.idiag_dport,
  44. req->id.idiag_if, tbl);
  45. else
  46. goto out_nosk;
  47. err = -ENOENT;
  48. if (sk == NULL)
  49. goto out_nosk;
  50. err = inet_diag_check_cookie(sk, req);
  51. if (err)
  52. goto out;
  53. err = -ENOMEM;
  54. rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
  55. sizeof(struct inet_diag_meminfo) +
  56. 64)), GFP_KERNEL);
  57. if (!rep)
  58. goto out;
  59. err = inet_sk_diag_fill(sk, NULL, rep, req,
  60. NETLINK_CB(in_skb).pid,
  61. nlh->nlmsg_seq, 0, nlh);
  62. if (err < 0) {
  63. WARN_ON(err == -EMSGSIZE);
  64. kfree_skb(rep);
  65. goto out;
  66. }
  67. err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
  68. MSG_DONTWAIT);
  69. if (err > 0)
  70. err = 0;
  71. out:
  72. if (sk)
  73. sock_put(sk);
  74. out_nosk:
  75. return err;
  76. }
  77. static void udp_dump(struct udp_table *table, struct sk_buff *skb, struct netlink_callback *cb,
  78. struct inet_diag_req *r, struct nlattr *bc)
  79. {
  80. int num, s_num, slot, s_slot;
  81. s_slot = cb->args[0];
  82. num = s_num = cb->args[1];
  83. for (slot = s_slot; slot <= table->mask; num = s_num = 0, slot++) {
  84. struct sock *sk;
  85. struct hlist_nulls_node *node;
  86. struct udp_hslot *hslot = &table->hash[slot];
  87. if (hlist_nulls_empty(&hslot->head))
  88. continue;
  89. spin_lock_bh(&hslot->lock);
  90. sk_nulls_for_each(sk, node, &hslot->head) {
  91. struct inet_sock *inet = inet_sk(sk);
  92. if (num < s_num)
  93. goto next;
  94. if (!(r->idiag_states & (1 << sk->sk_state)))
  95. goto next;
  96. if (r->sdiag_family != AF_UNSPEC &&
  97. sk->sk_family != r->sdiag_family)
  98. goto next;
  99. if (r->id.idiag_sport != inet->inet_sport &&
  100. r->id.idiag_sport)
  101. goto next;
  102. if (r->id.idiag_dport != inet->inet_dport &&
  103. r->id.idiag_dport)
  104. goto next;
  105. if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
  106. spin_unlock_bh(&hslot->lock);
  107. goto done;
  108. }
  109. next:
  110. num++;
  111. }
  112. spin_unlock_bh(&hslot->lock);
  113. }
  114. done:
  115. cb->args[0] = slot;
  116. cb->args[1] = num;
  117. }
  118. static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  119. struct inet_diag_req *r, struct nlattr *bc)
  120. {
  121. udp_dump(&udp_table, skb, cb, r, bc);
  122. }
  123. static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
  124. struct inet_diag_req *req)
  125. {
  126. return udp_dump_one(&udp_table, in_skb, nlh, req);
  127. }
  128. static const struct inet_diag_handler udp_diag_handler = {
  129. .dump = udp_diag_dump,
  130. .dump_one = udp_diag_dump_one,
  131. .idiag_type = IPPROTO_UDP,
  132. };
  133. static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  134. struct inet_diag_req *r, struct nlattr *bc)
  135. {
  136. udp_dump(&udplite_table, skb, cb, r, bc);
  137. }
  138. static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
  139. struct inet_diag_req *req)
  140. {
  141. return udp_dump_one(&udplite_table, in_skb, nlh, req);
  142. }
  143. static const struct inet_diag_handler udplite_diag_handler = {
  144. .dump = udplite_diag_dump,
  145. .dump_one = udplite_diag_dump_one,
  146. .idiag_type = IPPROTO_UDPLITE,
  147. };
  148. static int __init udp_diag_init(void)
  149. {
  150. int err;
  151. err = inet_diag_register(&udp_diag_handler);
  152. if (err)
  153. goto out;
  154. err = inet_diag_register(&udplite_diag_handler);
  155. if (err)
  156. goto out_lite;
  157. out:
  158. return err;
  159. out_lite:
  160. inet_diag_unregister(&udp_diag_handler);
  161. goto out;
  162. }
  163. static void __exit udp_diag_exit(void)
  164. {
  165. inet_diag_unregister(&udplite_diag_handler);
  166. inet_diag_unregister(&udp_diag_handler);
  167. }
  168. module_init(udp_diag_init);
  169. module_exit(udp_diag_exit);
  170. MODULE_LICENSE("GPL");
  171. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 17);
  172. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 136);