udp_diag.c 7.5 KB

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