raw_diag.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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(sizeof(struct inet_diag_msg) +
  88. sizeof(struct inet_diag_meminfo) + 64,
  89. GFP_KERNEL);
  90. if (!rep) {
  91. sock_put(sk);
  92. return -ENOMEM;
  93. }
  94. err = inet_sk_diag_fill(sk, NULL, rep, r,
  95. sk_user_ns(NETLINK_CB(in_skb).sk),
  96. NETLINK_CB(in_skb).portid,
  97. nlh->nlmsg_seq, 0, nlh,
  98. netlink_net_capable(in_skb, CAP_NET_ADMIN));
  99. sock_put(sk);
  100. if (err < 0) {
  101. kfree_skb(rep);
  102. return err;
  103. }
  104. err = netlink_unicast(net->diag_nlsk, rep,
  105. NETLINK_CB(in_skb).portid,
  106. MSG_DONTWAIT);
  107. if (err > 0)
  108. err = 0;
  109. return err;
  110. }
  111. static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
  112. struct netlink_callback *cb,
  113. const struct inet_diag_req_v2 *r,
  114. struct nlattr *bc, bool net_admin)
  115. {
  116. if (!inet_diag_bc_sk(bc, sk))
  117. return 0;
  118. return inet_sk_diag_fill(sk, NULL, skb, r,
  119. sk_user_ns(NETLINK_CB(cb->skb).sk),
  120. NETLINK_CB(cb->skb).portid,
  121. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  122. cb->nlh, net_admin);
  123. }
  124. static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  125. const struct inet_diag_req_v2 *r, struct nlattr *bc)
  126. {
  127. bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
  128. struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
  129. struct net *net = sock_net(skb->sk);
  130. int num, s_num, slot, s_slot;
  131. struct sock *sk = NULL;
  132. if (IS_ERR(hashinfo))
  133. return;
  134. s_slot = cb->args[0];
  135. num = s_num = cb->args[1];
  136. read_lock(&hashinfo->lock);
  137. for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) {
  138. num = 0;
  139. sk_for_each(sk, &hashinfo->ht[slot]) {
  140. struct inet_sock *inet = inet_sk(sk);
  141. if (!net_eq(sock_net(sk), net))
  142. continue;
  143. if (num < s_num)
  144. goto next;
  145. if (sk->sk_family != r->sdiag_family)
  146. goto next;
  147. if (r->id.idiag_sport != inet->inet_sport &&
  148. r->id.idiag_sport)
  149. goto next;
  150. if (r->id.idiag_dport != inet->inet_dport &&
  151. r->id.idiag_dport)
  152. goto next;
  153. if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0)
  154. goto out_unlock;
  155. next:
  156. num++;
  157. }
  158. }
  159. out_unlock:
  160. read_unlock(&hashinfo->lock);
  161. cb->args[0] = slot;
  162. cb->args[1] = num;
  163. }
  164. static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
  165. void *info)
  166. {
  167. r->idiag_rqueue = sk_rmem_alloc_get(sk);
  168. r->idiag_wqueue = sk_wmem_alloc_get(sk);
  169. }
  170. #ifdef CONFIG_INET_DIAG_DESTROY
  171. static int raw_diag_destroy(struct sk_buff *in_skb,
  172. const struct inet_diag_req_v2 *r)
  173. {
  174. struct net *net = sock_net(in_skb->sk);
  175. struct sock *sk;
  176. int err;
  177. sk = raw_sock_get(net, r);
  178. if (IS_ERR(sk))
  179. return PTR_ERR(sk);
  180. err = sock_diag_destroy(sk, ECONNABORTED);
  181. sock_put(sk);
  182. return err;
  183. }
  184. #endif
  185. static const struct inet_diag_handler raw_diag_handler = {
  186. .dump = raw_diag_dump,
  187. .dump_one = raw_diag_dump_one,
  188. .idiag_get_info = raw_diag_get_info,
  189. .idiag_type = IPPROTO_RAW,
  190. .idiag_info_size = 0,
  191. #ifdef CONFIG_INET_DIAG_DESTROY
  192. .destroy = raw_diag_destroy,
  193. #endif
  194. };
  195. static void __always_unused __check_inet_diag_req_raw(void)
  196. {
  197. /*
  198. * Make sure the two structures are identical,
  199. * except the @pad field.
  200. */
  201. #define __offset_mismatch(m1, m2) \
  202. (offsetof(struct inet_diag_req_v2, m1) != \
  203. offsetof(struct inet_diag_req_raw, m2))
  204. BUILD_BUG_ON(sizeof(struct inet_diag_req_v2) !=
  205. sizeof(struct inet_diag_req_raw));
  206. BUILD_BUG_ON(__offset_mismatch(sdiag_family, sdiag_family));
  207. BUILD_BUG_ON(__offset_mismatch(sdiag_protocol, sdiag_protocol));
  208. BUILD_BUG_ON(__offset_mismatch(idiag_ext, idiag_ext));
  209. BUILD_BUG_ON(__offset_mismatch(pad, sdiag_raw_protocol));
  210. BUILD_BUG_ON(__offset_mismatch(idiag_states, idiag_states));
  211. BUILD_BUG_ON(__offset_mismatch(id, id));
  212. #undef __offset_mismatch
  213. }
  214. static int __init raw_diag_init(void)
  215. {
  216. return inet_diag_register(&raw_diag_handler);
  217. }
  218. static void __exit raw_diag_exit(void)
  219. {
  220. inet_diag_unregister(&raw_diag_handler);
  221. }
  222. module_init(raw_diag_init);
  223. module_exit(raw_diag_exit);
  224. MODULE_LICENSE("GPL");
  225. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */);
  226. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */);