udp_diag.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/sock_diag.h>
  17. static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
  18. struct netlink_callback *cb,
  19. const struct inet_diag_req_v2 *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,
  25. sk_user_ns(NETLINK_CB(cb->skb).sk),
  26. NETLINK_CB(cb->skb).portid,
  27. cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
  28. }
  29. static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
  30. const struct nlmsghdr *nlh,
  31. const struct inet_diag_req_v2 *req)
  32. {
  33. int err = -EINVAL;
  34. struct sock *sk;
  35. struct sk_buff *rep;
  36. struct net *net = sock_net(in_skb->sk);
  37. if (req->sdiag_family == AF_INET)
  38. sk = __udp4_lib_lookup(net,
  39. req->id.idiag_src[0], req->id.idiag_sport,
  40. req->id.idiag_dst[0], req->id.idiag_dport,
  41. req->id.idiag_if, tbl);
  42. #if IS_ENABLED(CONFIG_IPV6)
  43. else if (req->sdiag_family == AF_INET6)
  44. sk = __udp6_lib_lookup(net,
  45. (struct in6_addr *)req->id.idiag_src,
  46. req->id.idiag_sport,
  47. (struct in6_addr *)req->id.idiag_dst,
  48. req->id.idiag_dport,
  49. req->id.idiag_if, tbl);
  50. #endif
  51. else
  52. goto out_nosk;
  53. err = -ENOENT;
  54. if (!sk)
  55. goto out_nosk;
  56. err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
  57. if (err)
  58. goto out;
  59. err = -ENOMEM;
  60. rep = nlmsg_new(sizeof(struct inet_diag_msg) +
  61. sizeof(struct inet_diag_meminfo) + 64,
  62. GFP_KERNEL);
  63. if (!rep)
  64. goto out;
  65. err = inet_sk_diag_fill(sk, NULL, rep, req,
  66. sk_user_ns(NETLINK_CB(in_skb).sk),
  67. NETLINK_CB(in_skb).portid,
  68. nlh->nlmsg_seq, 0, nlh);
  69. if (err < 0) {
  70. WARN_ON(err == -EMSGSIZE);
  71. kfree_skb(rep);
  72. goto out;
  73. }
  74. err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
  75. MSG_DONTWAIT);
  76. if (err > 0)
  77. err = 0;
  78. out:
  79. if (sk)
  80. sock_put(sk);
  81. out_nosk:
  82. return err;
  83. }
  84. static void udp_dump(struct udp_table *table, struct sk_buff *skb,
  85. struct netlink_callback *cb,
  86. const struct inet_diag_req_v2 *r, struct nlattr *bc)
  87. {
  88. int num, s_num, slot, s_slot;
  89. struct net *net = sock_net(skb->sk);
  90. s_slot = cb->args[0];
  91. num = s_num = cb->args[1];
  92. for (slot = s_slot; slot <= table->mask; s_num = 0, slot++) {
  93. struct sock *sk;
  94. struct hlist_nulls_node *node;
  95. struct udp_hslot *hslot = &table->hash[slot];
  96. num = 0;
  97. if (hlist_nulls_empty(&hslot->head))
  98. continue;
  99. spin_lock_bh(&hslot->lock);
  100. sk_nulls_for_each(sk, node, &hslot->head) {
  101. struct inet_sock *inet = inet_sk(sk);
  102. if (!net_eq(sock_net(sk), net))
  103. continue;
  104. if (num < s_num)
  105. goto next;
  106. if (!(r->idiag_states & (1 << sk->sk_state)))
  107. goto next;
  108. if (r->sdiag_family != AF_UNSPEC &&
  109. sk->sk_family != r->sdiag_family)
  110. goto next;
  111. if (r->id.idiag_sport != inet->inet_sport &&
  112. r->id.idiag_sport)
  113. goto next;
  114. if (r->id.idiag_dport != inet->inet_dport &&
  115. r->id.idiag_dport)
  116. goto next;
  117. if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
  118. spin_unlock_bh(&hslot->lock);
  119. goto done;
  120. }
  121. next:
  122. num++;
  123. }
  124. spin_unlock_bh(&hslot->lock);
  125. }
  126. done:
  127. cb->args[0] = slot;
  128. cb->args[1] = num;
  129. }
  130. static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  131. const struct inet_diag_req_v2 *r, struct nlattr *bc)
  132. {
  133. udp_dump(&udp_table, skb, cb, r, bc);
  134. }
  135. static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
  136. const struct inet_diag_req_v2 *req)
  137. {
  138. return udp_dump_one(&udp_table, in_skb, nlh, req);
  139. }
  140. static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
  141. void *info)
  142. {
  143. r->idiag_rqueue = sk_rmem_alloc_get(sk);
  144. r->idiag_wqueue = sk_wmem_alloc_get(sk);
  145. }
  146. static const struct inet_diag_handler udp_diag_handler = {
  147. .dump = udp_diag_dump,
  148. .dump_one = udp_diag_dump_one,
  149. .idiag_get_info = udp_diag_get_info,
  150. .idiag_type = IPPROTO_UDP,
  151. .idiag_info_size = 0,
  152. };
  153. static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  154. const struct inet_diag_req_v2 *r,
  155. struct nlattr *bc)
  156. {
  157. udp_dump(&udplite_table, skb, cb, r, bc);
  158. }
  159. static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
  160. const struct inet_diag_req_v2 *req)
  161. {
  162. return udp_dump_one(&udplite_table, in_skb, nlh, req);
  163. }
  164. static const struct inet_diag_handler udplite_diag_handler = {
  165. .dump = udplite_diag_dump,
  166. .dump_one = udplite_diag_dump_one,
  167. .idiag_get_info = udp_diag_get_info,
  168. .idiag_type = IPPROTO_UDPLITE,
  169. .idiag_info_size = 0,
  170. };
  171. static int __init udp_diag_init(void)
  172. {
  173. int err;
  174. err = inet_diag_register(&udp_diag_handler);
  175. if (err)
  176. goto out;
  177. err = inet_diag_register(&udplite_diag_handler);
  178. if (err)
  179. goto out_lite;
  180. out:
  181. return err;
  182. out_lite:
  183. inet_diag_unregister(&udp_diag_handler);
  184. goto out;
  185. }
  186. static void __exit udp_diag_exit(void)
  187. {
  188. inet_diag_unregister(&udplite_diag_handler);
  189. inet_diag_unregister(&udp_diag_handler);
  190. }
  191. module_init(udp_diag_init);
  192. module_exit(udp_diag_exit);
  193. MODULE_LICENSE("GPL");
  194. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */);
  195. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */);