udp_diag.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #include <net/ipv6.h>
  18. static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
  19. struct netlink_callback *cb, 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, 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_v2 *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. #if IS_ENABLED(CONFIG_IPV6)
  39. else if (req->sdiag_family == AF_INET6)
  40. sk = __udp6_lib_lookup(&init_net,
  41. (struct in6_addr *)req->id.idiag_src,
  42. req->id.idiag_sport,
  43. (struct in6_addr *)req->id.idiag_dst,
  44. req->id.idiag_dport,
  45. req->id.idiag_if, tbl);
  46. #endif
  47. else
  48. goto out_nosk;
  49. err = -ENOENT;
  50. if (sk == NULL)
  51. goto out_nosk;
  52. err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
  53. if (err)
  54. goto out;
  55. err = -ENOMEM;
  56. rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
  57. sizeof(struct inet_diag_meminfo) +
  58. 64)), GFP_KERNEL);
  59. if (!rep)
  60. goto out;
  61. err = inet_sk_diag_fill(sk, NULL, rep, req,
  62. NETLINK_CB(in_skb).pid,
  63. nlh->nlmsg_seq, 0, nlh);
  64. if (err < 0) {
  65. WARN_ON(err == -EMSGSIZE);
  66. kfree_skb(rep);
  67. goto out;
  68. }
  69. err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
  70. MSG_DONTWAIT);
  71. if (err > 0)
  72. err = 0;
  73. out:
  74. if (sk)
  75. sock_put(sk);
  76. out_nosk:
  77. return err;
  78. }
  79. static void udp_dump(struct udp_table *table, struct sk_buff *skb, struct netlink_callback *cb,
  80. struct inet_diag_req_v2 *r, struct nlattr *bc)
  81. {
  82. int num, s_num, slot, s_slot;
  83. s_slot = cb->args[0];
  84. num = s_num = cb->args[1];
  85. for (slot = s_slot; slot <= table->mask; num = s_num = 0, slot++) {
  86. struct sock *sk;
  87. struct hlist_nulls_node *node;
  88. struct udp_hslot *hslot = &table->hash[slot];
  89. if (hlist_nulls_empty(&hslot->head))
  90. continue;
  91. spin_lock_bh(&hslot->lock);
  92. sk_nulls_for_each(sk, node, &hslot->head) {
  93. struct inet_sock *inet = inet_sk(sk);
  94. if (num < s_num)
  95. goto next;
  96. if (!(r->idiag_states & (1 << sk->sk_state)))
  97. goto next;
  98. if (r->sdiag_family != AF_UNSPEC &&
  99. sk->sk_family != r->sdiag_family)
  100. goto next;
  101. if (r->id.idiag_sport != inet->inet_sport &&
  102. r->id.idiag_sport)
  103. goto next;
  104. if (r->id.idiag_dport != inet->inet_dport &&
  105. r->id.idiag_dport)
  106. goto next;
  107. if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
  108. spin_unlock_bh(&hslot->lock);
  109. goto done;
  110. }
  111. next:
  112. num++;
  113. }
  114. spin_unlock_bh(&hslot->lock);
  115. }
  116. done:
  117. cb->args[0] = slot;
  118. cb->args[1] = num;
  119. }
  120. static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  121. struct inet_diag_req_v2 *r, struct nlattr *bc)
  122. {
  123. udp_dump(&udp_table, skb, cb, r, bc);
  124. }
  125. static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
  126. struct inet_diag_req_v2 *req)
  127. {
  128. return udp_dump_one(&udp_table, in_skb, nlh, req);
  129. }
  130. static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
  131. void *info)
  132. {
  133. r->idiag_rqueue = sk_rmem_alloc_get(sk);
  134. r->idiag_wqueue = sk_wmem_alloc_get(sk);
  135. }
  136. #ifdef CONFIG_INET_DIAG_DESTROY
  137. static int __udp_diag_destroy(struct sk_buff *in_skb,
  138. const struct inet_diag_req_v2 *req,
  139. struct udp_table *tbl)
  140. {
  141. struct net *net = sock_net(in_skb->sk);
  142. struct sock *sk;
  143. int err;
  144. rcu_read_lock();
  145. if (req->sdiag_family == AF_INET)
  146. sk = __udp4_lib_lookup(net,
  147. req->id.idiag_dst[0], req->id.idiag_dport,
  148. req->id.idiag_src[0], req->id.idiag_sport,
  149. req->id.idiag_if, tbl);
  150. #if IS_ENABLED(CONFIG_IPV6)
  151. else if (req->sdiag_family == AF_INET6) {
  152. if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
  153. ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
  154. sk = __udp4_lib_lookup(net,
  155. req->id.idiag_dst[3], req->id.idiag_dport,
  156. req->id.idiag_src[3], req->id.idiag_sport,
  157. req->id.idiag_if, tbl);
  158. else
  159. sk = __udp6_lib_lookup(net,
  160. (struct in6_addr *)req->id.idiag_dst,
  161. req->id.idiag_dport,
  162. (struct in6_addr *)req->id.idiag_src,
  163. req->id.idiag_sport,
  164. req->id.idiag_if, tbl);
  165. }
  166. #endif
  167. else {
  168. rcu_read_unlock();
  169. return -EINVAL;
  170. }
  171. if (sk && !atomic_inc_not_zero(&sk->sk_refcnt))
  172. sk = NULL;
  173. rcu_read_unlock();
  174. if (!sk)
  175. return -ENOENT;
  176. if (sock_diag_check_cookie(sk, (__u32 *) req->id.idiag_cookie)) {
  177. sock_put(sk);
  178. return -ENOENT;
  179. }
  180. err = sock_diag_destroy(sk, ECONNABORTED);
  181. sock_put(sk);
  182. return err;
  183. }
  184. static int udp_diag_destroy(struct sk_buff *in_skb,
  185. struct inet_diag_req_v2 *req)
  186. {
  187. return __udp_diag_destroy(in_skb, req, &udp_table);
  188. }
  189. static int udplite_diag_destroy(struct sk_buff *in_skb,
  190. struct inet_diag_req_v2 *req)
  191. {
  192. return __udp_diag_destroy(in_skb, req, &udplite_table);
  193. }
  194. #endif
  195. static const struct inet_diag_handler udp_diag_handler = {
  196. .dump = udp_diag_dump,
  197. .dump_one = udp_diag_dump_one,
  198. .idiag_get_info = udp_diag_get_info,
  199. .idiag_type = IPPROTO_UDP,
  200. #ifdef CONFIG_INET_DIAG_DESTROY
  201. .destroy = udp_diag_destroy,
  202. #endif
  203. };
  204. static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  205. struct inet_diag_req_v2 *r, struct nlattr *bc)
  206. {
  207. udp_dump(&udplite_table, skb, cb, r, bc);
  208. }
  209. static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
  210. struct inet_diag_req_v2 *req)
  211. {
  212. return udp_dump_one(&udplite_table, in_skb, nlh, req);
  213. }
  214. static const struct inet_diag_handler udplite_diag_handler = {
  215. .dump = udplite_diag_dump,
  216. .dump_one = udplite_diag_dump_one,
  217. .idiag_get_info = udp_diag_get_info,
  218. .idiag_type = IPPROTO_UDPLITE,
  219. #ifdef CONFIG_INET_DIAG_DESTROY
  220. .destroy = udplite_diag_destroy,
  221. #endif
  222. };
  223. static int __init udp_diag_init(void)
  224. {
  225. int err;
  226. err = inet_diag_register(&udp_diag_handler);
  227. if (err)
  228. goto out;
  229. err = inet_diag_register(&udplite_diag_handler);
  230. if (err)
  231. goto out_lite;
  232. out:
  233. return err;
  234. out_lite:
  235. inet_diag_unregister(&udp_diag_handler);
  236. goto out;
  237. }
  238. static void __exit udp_diag_exit(void)
  239. {
  240. inet_diag_unregister(&udplite_diag_handler);
  241. inet_diag_unregister(&udp_diag_handler);
  242. }
  243. module_init(udp_diag_init);
  244. module_exit(udp_diag_exit);
  245. MODULE_LICENSE("GPL");
  246. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */);
  247. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */);