raw_diag.c 6.3 KB

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