sock_diag.c 7.9 KB

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