udp_diag.c 7.6 KB

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