udp_diag.c 5.4 KB

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