raw_diag.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/inet_diag.h>
  4. #include <linux/sock_diag.h>
  5. #include <net/inet_sock.h>
  6. #include <net/raw.h>
  7. #include <net/rawv6.h>
  8. #ifdef pr_fmt
  9. # undef pr_fmt
  10. #endif
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. static struct raw_hashinfo *
  13. raw_get_hashinfo(const struct inet_diag_req_v2 *r)
  14. {
  15. if (r->sdiag_family == AF_INET) {
  16. return &raw_v4_hashinfo;
  17. #if IS_ENABLED(CONFIG_IPV6)
  18. } else if (r->sdiag_family == AF_INET6) {
  19. return &raw_v6_hashinfo;
  20. #endif
  21. } else {
  22. return ERR_PTR(-EINVAL);
  23. }
  24. }
  25. /*
  26. * Due to requirement of not breaking user API we can't simply
  27. * rename @pad field in inet_diag_req_v2 structure, instead
  28. * use helper to figure it out.
  29. */
  30. static bool raw_lookup(struct net *net, struct sock *sk,
  31. const struct inet_diag_req_v2 *req)
  32. {
  33. struct inet_diag_req_raw *r = (void *)req;
  34. if (r->sdiag_family == AF_INET)
  35. return raw_v4_match(net, sk, r->sdiag_raw_protocol,
  36. r->id.idiag_dst[0],
  37. r->id.idiag_src[0],
  38. r->id.idiag_if, 0);
  39. #if IS_ENABLED(CONFIG_IPV6)
  40. else
  41. return raw_v6_match(net, sk, r->sdiag_raw_protocol,
  42. (const struct in6_addr *)r->id.idiag_src,
  43. (const struct in6_addr *)r->id.idiag_dst,
  44. r->id.idiag_if, 0);
  45. #endif
  46. return false;
  47. }
  48. static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r)
  49. {
  50. struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
  51. struct hlist_head *hlist;
  52. struct sock *sk;
  53. int slot;
  54. if (IS_ERR(hashinfo))
  55. return ERR_CAST(hashinfo);
  56. rcu_read_lock();
  57. for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) {
  58. hlist = &hashinfo->ht[slot];
  59. sk_for_each_rcu(sk, hlist) {
  60. if (raw_lookup(net, sk, r)) {
  61. /*
  62. * Grab it and keep until we fill
  63. * diag message to be reported, so
  64. * caller should call sock_put then.
  65. */
  66. if (refcount_inc_not_zero(&sk->sk_refcnt))
  67. goto out_unlock;
  68. }
  69. }
  70. }
  71. sk = ERR_PTR(-ENOENT);
  72. out_unlock:
  73. rcu_read_unlock();
  74. return sk;
  75. }
  76. static int raw_diag_dump_one(struct netlink_callback *cb,
  77. const struct inet_diag_req_v2 *r)
  78. {
  79. struct sk_buff *in_skb = cb->skb;
  80. struct sk_buff *rep;
  81. struct sock *sk;
  82. struct net *net;
  83. int err;
  84. net = sock_net(in_skb->sk);
  85. sk = raw_sock_get(net, r);
  86. if (IS_ERR(sk))
  87. return PTR_ERR(sk);
  88. rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) +
  89. inet_diag_msg_attrs_size() +
  90. nla_total_size(sizeof(struct inet_diag_meminfo)) + 64,
  91. GFP_KERNEL);
  92. if (!rep) {
  93. sock_put(sk);
  94. return -ENOMEM;
  95. }
  96. err = inet_sk_diag_fill(sk, NULL, rep, cb, r, 0,
  97. netlink_net_capable(in_skb, CAP_NET_ADMIN));
  98. sock_put(sk);
  99. if (err < 0) {
  100. kfree_skb(rep);
  101. return err;
  102. }
  103. err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
  104. return err;
  105. }
  106. static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
  107. struct netlink_callback *cb,
  108. const struct inet_diag_req_v2 *r,
  109. struct nlattr *bc, bool net_admin)
  110. {
  111. if (!inet_diag_bc_sk(bc, sk))
  112. return 0;
  113. return inet_sk_diag_fill(sk, NULL, skb, cb, r, NLM_F_MULTI, net_admin);
  114. }
  115. static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  116. const struct inet_diag_req_v2 *r)
  117. {
  118. bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
  119. struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
  120. struct net *net = sock_net(skb->sk);
  121. struct inet_diag_dump_data *cb_data;
  122. int num, s_num, slot, s_slot;
  123. struct hlist_head *hlist;
  124. struct sock *sk = NULL;
  125. struct nlattr *bc;
  126. if (IS_ERR(hashinfo))
  127. return;
  128. cb_data = cb->data;
  129. bc = cb_data->inet_diag_nla_bc;
  130. s_slot = cb->args[0];
  131. num = s_num = cb->args[1];
  132. rcu_read_lock();
  133. for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) {
  134. num = 0;
  135. hlist = &hashinfo->ht[slot];
  136. sk_for_each_rcu(sk, hlist) {
  137. struct inet_sock *inet = inet_sk(sk);
  138. if (!net_eq(sock_net(sk), net))
  139. continue;
  140. if (num < s_num)
  141. goto next;
  142. if (sk->sk_family != r->sdiag_family)
  143. goto next;
  144. if (r->id.idiag_sport != inet->inet_sport &&
  145. r->id.idiag_sport)
  146. goto next;
  147. if (r->id.idiag_dport != inet->inet_dport &&
  148. r->id.idiag_dport)
  149. goto next;
  150. if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0)
  151. goto out_unlock;
  152. next:
  153. num++;
  154. }
  155. }
  156. out_unlock:
  157. rcu_read_unlock();
  158. cb->args[0] = slot;
  159. cb->args[1] = num;
  160. }
  161. static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
  162. void *info)
  163. {
  164. r->idiag_rqueue = sk_rmem_alloc_get(sk);
  165. r->idiag_wqueue = sk_wmem_alloc_get(sk);
  166. }
  167. #ifdef CONFIG_INET_DIAG_DESTROY
  168. static int raw_diag_destroy(struct sk_buff *in_skb,
  169. const struct inet_diag_req_v2 *r)
  170. {
  171. struct net *net = sock_net(in_skb->sk);
  172. struct sock *sk;
  173. int err;
  174. sk = raw_sock_get(net, r);
  175. if (IS_ERR(sk))
  176. return PTR_ERR(sk);
  177. err = sock_diag_destroy(sk, ECONNABORTED);
  178. sock_put(sk);
  179. return err;
  180. }
  181. #endif
  182. static const struct inet_diag_handler raw_diag_handler = {
  183. .dump = raw_diag_dump,
  184. .dump_one = raw_diag_dump_one,
  185. .idiag_get_info = raw_diag_get_info,
  186. .idiag_type = IPPROTO_RAW,
  187. .idiag_info_size = 0,
  188. #ifdef CONFIG_INET_DIAG_DESTROY
  189. .destroy = raw_diag_destroy,
  190. #endif
  191. };
  192. static void __always_unused __check_inet_diag_req_raw(void)
  193. {
  194. /*
  195. * Make sure the two structures are identical,
  196. * except the @pad field.
  197. */
  198. #define __offset_mismatch(m1, m2) \
  199. (offsetof(struct inet_diag_req_v2, m1) != \
  200. offsetof(struct inet_diag_req_raw, m2))
  201. BUILD_BUG_ON(sizeof(struct inet_diag_req_v2) !=
  202. sizeof(struct inet_diag_req_raw));
  203. BUILD_BUG_ON(__offset_mismatch(sdiag_family, sdiag_family));
  204. BUILD_BUG_ON(__offset_mismatch(sdiag_protocol, sdiag_protocol));
  205. BUILD_BUG_ON(__offset_mismatch(idiag_ext, idiag_ext));
  206. BUILD_BUG_ON(__offset_mismatch(pad, sdiag_raw_protocol));
  207. BUILD_BUG_ON(__offset_mismatch(idiag_states, idiag_states));
  208. BUILD_BUG_ON(__offset_mismatch(id, id));
  209. #undef __offset_mismatch
  210. }
  211. static int __init raw_diag_init(void)
  212. {
  213. return inet_diag_register(&raw_diag_handler);
  214. }
  215. static void __exit raw_diag_exit(void)
  216. {
  217. inet_diag_unregister(&raw_diag_handler);
  218. }
  219. module_init(raw_diag_init);
  220. module_exit(raw_diag_exit);
  221. MODULE_LICENSE("GPL");
  222. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */);
  223. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */);