sock_diag.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* License: GPL */
  2. #include <linux/mutex.h>
  3. #include <linux/socket.h>
  4. #include <linux/skbuff.h>
  5. #include <net/netlink.h>
  6. #include <net/net_namespace.h>
  7. #include <linux/module.h>
  8. #include <net/sock.h>
  9. #include <linux/kernel.h>
  10. #include <linux/tcp.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/inet_diag.h>
  13. #include <linux/sock_diag.h>
  14. static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
  15. static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
  16. static DEFINE_MUTEX(sock_diag_table_mutex);
  17. static struct workqueue_struct *broadcast_wq;
  18. static u64 sock_gen_cookie(struct sock *sk)
  19. {
  20. while (1) {
  21. u64 res = atomic64_read(&sk->sk_cookie);
  22. if (res)
  23. return res;
  24. res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
  25. atomic64_cmpxchg(&sk->sk_cookie, 0, res);
  26. }
  27. }
  28. int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
  29. {
  30. u64 res;
  31. if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE)
  32. return 0;
  33. res = sock_gen_cookie(sk);
  34. if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1])
  35. return -ESTALE;
  36. return 0;
  37. }
  38. EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
  39. void sock_diag_save_cookie(struct sock *sk, __u32 *cookie)
  40. {
  41. u64 res = sock_gen_cookie(sk);
  42. cookie[0] = (u32)res;
  43. cookie[1] = (u32)(res >> 32);
  44. }
  45. EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
  46. int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
  47. {
  48. u32 mem[SK_MEMINFO_VARS];
  49. mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
  50. mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
  51. mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
  52. mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
  53. mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
  54. mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
  55. mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
  56. mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
  57. return nla_put(skb, attrtype, sizeof(mem), &mem);
  58. }
  59. EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
  60. int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
  61. struct sk_buff *skb, int attrtype)
  62. {
  63. struct sock_fprog_kern *fprog;
  64. struct sk_filter *filter;
  65. struct nlattr *attr;
  66. unsigned int flen;
  67. int err = 0;
  68. if (!may_report_filterinfo) {
  69. nla_reserve(skb, attrtype, 0);
  70. return 0;
  71. }
  72. rcu_read_lock();
  73. filter = rcu_dereference(sk->sk_filter);
  74. if (!filter)
  75. goto out;
  76. fprog = filter->prog->orig_prog;
  77. if (!fprog)
  78. goto out;
  79. flen = bpf_classic_proglen(fprog);
  80. attr = nla_reserve(skb, attrtype, flen);
  81. if (attr == NULL) {
  82. err = -EMSGSIZE;
  83. goto out;
  84. }
  85. memcpy(nla_data(attr), fprog->filter, flen);
  86. out:
  87. rcu_read_unlock();
  88. return err;
  89. }
  90. EXPORT_SYMBOL(sock_diag_put_filterinfo);
  91. struct broadcast_sk {
  92. struct sock *sk;
  93. struct work_struct work;
  94. };
  95. static size_t sock_diag_nlmsg_size(void)
  96. {
  97. return NLMSG_ALIGN(sizeof(struct inet_diag_msg)
  98. + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */
  99. + nla_total_size(sizeof(struct tcp_info))); /* INET_DIAG_INFO */
  100. }
  101. static void sock_diag_broadcast_destroy_work(struct work_struct *work)
  102. {
  103. struct broadcast_sk *bsk =
  104. container_of(work, struct broadcast_sk, work);
  105. struct sock *sk = bsk->sk;
  106. const struct sock_diag_handler *hndl;
  107. struct sk_buff *skb;
  108. const enum sknetlink_groups group = sock_diag_destroy_group(sk);
  109. int err = -1;
  110. WARN_ON(group == SKNLGRP_NONE);
  111. skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL);
  112. if (!skb)
  113. goto out;
  114. mutex_lock(&sock_diag_table_mutex);
  115. hndl = sock_diag_handlers[sk->sk_family];
  116. if (hndl && hndl->get_info)
  117. err = hndl->get_info(skb, sk);
  118. mutex_unlock(&sock_diag_table_mutex);
  119. if (!err)
  120. nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group,
  121. GFP_KERNEL);
  122. else
  123. kfree_skb(skb);
  124. out:
  125. sk_destruct(sk);
  126. kfree(bsk);
  127. }
  128. void sock_diag_broadcast_destroy(struct sock *sk)
  129. {
  130. /* Note, this function is often called from an interrupt context. */
  131. struct broadcast_sk *bsk =
  132. kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC);
  133. if (!bsk)
  134. return sk_destruct(sk);
  135. bsk->sk = sk;
  136. INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work);
  137. queue_work(broadcast_wq, &bsk->work);
  138. }
  139. void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
  140. {
  141. mutex_lock(&sock_diag_table_mutex);
  142. inet_rcv_compat = fn;
  143. mutex_unlock(&sock_diag_table_mutex);
  144. }
  145. EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
  146. void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
  147. {
  148. mutex_lock(&sock_diag_table_mutex);
  149. inet_rcv_compat = NULL;
  150. mutex_unlock(&sock_diag_table_mutex);
  151. }
  152. EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
  153. int sock_diag_register(const struct sock_diag_handler *hndl)
  154. {
  155. int err = 0;
  156. if (hndl->family >= AF_MAX)
  157. return -EINVAL;
  158. mutex_lock(&sock_diag_table_mutex);
  159. if (sock_diag_handlers[hndl->family])
  160. err = -EBUSY;
  161. else
  162. sock_diag_handlers[hndl->family] = hndl;
  163. mutex_unlock(&sock_diag_table_mutex);
  164. return err;
  165. }
  166. EXPORT_SYMBOL_GPL(sock_diag_register);
  167. void sock_diag_unregister(const struct sock_diag_handler *hnld)
  168. {
  169. int family = hnld->family;
  170. if (family >= AF_MAX)
  171. return;
  172. mutex_lock(&sock_diag_table_mutex);
  173. BUG_ON(sock_diag_handlers[family] != hnld);
  174. sock_diag_handlers[family] = NULL;
  175. mutex_unlock(&sock_diag_table_mutex);
  176. }
  177. EXPORT_SYMBOL_GPL(sock_diag_unregister);
  178. static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  179. {
  180. int err;
  181. struct sock_diag_req *req = nlmsg_data(nlh);
  182. const struct sock_diag_handler *hndl;
  183. if (nlmsg_len(nlh) < sizeof(*req))
  184. return -EINVAL;
  185. if (req->sdiag_family >= AF_MAX)
  186. return -EINVAL;
  187. if (sock_diag_handlers[req->sdiag_family] == NULL)
  188. request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
  189. NETLINK_SOCK_DIAG, req->sdiag_family);
  190. mutex_lock(&sock_diag_table_mutex);
  191. hndl = sock_diag_handlers[req->sdiag_family];
  192. if (hndl == NULL)
  193. err = -ENOENT;
  194. else
  195. err = hndl->dump(skb, nlh);
  196. mutex_unlock(&sock_diag_table_mutex);
  197. return err;
  198. }
  199. static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  200. {
  201. int ret;
  202. switch (nlh->nlmsg_type) {
  203. case TCPDIAG_GETSOCK:
  204. case DCCPDIAG_GETSOCK:
  205. if (inet_rcv_compat == NULL)
  206. request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
  207. NETLINK_SOCK_DIAG, AF_INET);
  208. mutex_lock(&sock_diag_table_mutex);
  209. if (inet_rcv_compat != NULL)
  210. ret = inet_rcv_compat(skb, nlh);
  211. else
  212. ret = -EOPNOTSUPP;
  213. mutex_unlock(&sock_diag_table_mutex);
  214. return ret;
  215. case SOCK_DIAG_BY_FAMILY:
  216. return __sock_diag_rcv_msg(skb, nlh);
  217. default:
  218. return -EINVAL;
  219. }
  220. }
  221. static DEFINE_MUTEX(sock_diag_mutex);
  222. static void sock_diag_rcv(struct sk_buff *skb)
  223. {
  224. mutex_lock(&sock_diag_mutex);
  225. netlink_rcv_skb(skb, &sock_diag_rcv_msg);
  226. mutex_unlock(&sock_diag_mutex);
  227. }
  228. static int sock_diag_bind(struct net *net, int group)
  229. {
  230. switch (group) {
  231. case SKNLGRP_INET_TCP_DESTROY:
  232. case SKNLGRP_INET_UDP_DESTROY:
  233. if (!sock_diag_handlers[AF_INET])
  234. request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
  235. NETLINK_SOCK_DIAG, AF_INET);
  236. break;
  237. case SKNLGRP_INET6_TCP_DESTROY:
  238. case SKNLGRP_INET6_UDP_DESTROY:
  239. if (!sock_diag_handlers[AF_INET6])
  240. request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
  241. NETLINK_SOCK_DIAG, AF_INET6);
  242. break;
  243. }
  244. return 0;
  245. }
  246. static int __net_init diag_net_init(struct net *net)
  247. {
  248. struct netlink_kernel_cfg cfg = {
  249. .groups = SKNLGRP_MAX,
  250. .input = sock_diag_rcv,
  251. .bind = sock_diag_bind,
  252. .flags = NL_CFG_F_NONROOT_RECV,
  253. };
  254. net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
  255. return net->diag_nlsk == NULL ? -ENOMEM : 0;
  256. }
  257. static void __net_exit diag_net_exit(struct net *net)
  258. {
  259. netlink_kernel_release(net->diag_nlsk);
  260. net->diag_nlsk = NULL;
  261. }
  262. static struct pernet_operations diag_net_ops = {
  263. .init = diag_net_init,
  264. .exit = diag_net_exit,
  265. };
  266. static int __init sock_diag_init(void)
  267. {
  268. broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0);
  269. BUG_ON(!broadcast_wq);
  270. return register_pernet_subsys(&diag_net_ops);
  271. }
  272. device_initcall(sock_diag_init);