psample.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * net/psample/psample.c - Netlink channel for packet sampling
  3. * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/module.h>
  13. #include <net/net_namespace.h>
  14. #include <net/sock.h>
  15. #include <net/netlink.h>
  16. #include <net/genetlink.h>
  17. #include <net/psample.h>
  18. #include <linux/spinlock.h>
  19. #define PSAMPLE_MAX_PACKET_SIZE 0xffff
  20. static LIST_HEAD(psample_groups_list);
  21. static DEFINE_SPINLOCK(psample_groups_lock);
  22. /* multicast groups */
  23. enum psample_nl_multicast_groups {
  24. PSAMPLE_NL_MCGRP_CONFIG,
  25. PSAMPLE_NL_MCGRP_SAMPLE,
  26. };
  27. static const struct genl_multicast_group psample_nl_mcgrps[] = {
  28. [PSAMPLE_NL_MCGRP_CONFIG] = { .name = PSAMPLE_NL_MCGRP_CONFIG_NAME },
  29. [PSAMPLE_NL_MCGRP_SAMPLE] = { .name = PSAMPLE_NL_MCGRP_SAMPLE_NAME },
  30. };
  31. static struct genl_family psample_nl_family __ro_after_init;
  32. static int psample_group_nl_fill(struct sk_buff *msg,
  33. struct psample_group *group,
  34. enum psample_command cmd, u32 portid, u32 seq,
  35. int flags)
  36. {
  37. void *hdr;
  38. int ret;
  39. hdr = genlmsg_put(msg, portid, seq, &psample_nl_family, flags, cmd);
  40. if (!hdr)
  41. return -EMSGSIZE;
  42. ret = nla_put_u32(msg, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
  43. if (ret < 0)
  44. goto error;
  45. ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_REFCOUNT, group->refcount);
  46. if (ret < 0)
  47. goto error;
  48. ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_SEQ, group->seq);
  49. if (ret < 0)
  50. goto error;
  51. genlmsg_end(msg, hdr);
  52. return 0;
  53. error:
  54. genlmsg_cancel(msg, hdr);
  55. return -EMSGSIZE;
  56. }
  57. static int psample_nl_cmd_get_group_dumpit(struct sk_buff *msg,
  58. struct netlink_callback *cb)
  59. {
  60. struct psample_group *group;
  61. int start = cb->args[0];
  62. int idx = 0;
  63. int err;
  64. spin_lock(&psample_groups_lock);
  65. list_for_each_entry(group, &psample_groups_list, list) {
  66. if (!net_eq(group->net, sock_net(msg->sk)))
  67. continue;
  68. if (idx < start) {
  69. idx++;
  70. continue;
  71. }
  72. err = psample_group_nl_fill(msg, group, PSAMPLE_CMD_NEW_GROUP,
  73. NETLINK_CB(cb->skb).portid,
  74. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  75. if (err)
  76. break;
  77. idx++;
  78. }
  79. spin_unlock(&psample_groups_lock);
  80. cb->args[0] = idx;
  81. return msg->len;
  82. }
  83. static const struct genl_ops psample_nl_ops[] = {
  84. {
  85. .cmd = PSAMPLE_CMD_GET_GROUP,
  86. .dumpit = psample_nl_cmd_get_group_dumpit,
  87. /* can be retrieved by unprivileged users */
  88. }
  89. };
  90. static struct genl_family psample_nl_family __ro_after_init = {
  91. .name = PSAMPLE_GENL_NAME,
  92. .version = PSAMPLE_GENL_VERSION,
  93. .maxattr = PSAMPLE_ATTR_MAX,
  94. .netnsok = true,
  95. .module = THIS_MODULE,
  96. .mcgrps = psample_nl_mcgrps,
  97. .ops = psample_nl_ops,
  98. .n_ops = ARRAY_SIZE(psample_nl_ops),
  99. .n_mcgrps = ARRAY_SIZE(psample_nl_mcgrps),
  100. };
  101. static void psample_group_notify(struct psample_group *group,
  102. enum psample_command cmd)
  103. {
  104. struct sk_buff *msg;
  105. int err;
  106. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  107. if (!msg)
  108. return;
  109. err = psample_group_nl_fill(msg, group, cmd, 0, 0, NLM_F_MULTI);
  110. if (!err)
  111. genlmsg_multicast_netns(&psample_nl_family, group->net, msg, 0,
  112. PSAMPLE_NL_MCGRP_CONFIG, GFP_ATOMIC);
  113. else
  114. nlmsg_free(msg);
  115. }
  116. static struct psample_group *psample_group_create(struct net *net,
  117. u32 group_num)
  118. {
  119. struct psample_group *group;
  120. group = kzalloc(sizeof(*group), GFP_ATOMIC);
  121. if (!group)
  122. return NULL;
  123. group->net = net;
  124. group->group_num = group_num;
  125. list_add_tail(&group->list, &psample_groups_list);
  126. psample_group_notify(group, PSAMPLE_CMD_NEW_GROUP);
  127. return group;
  128. }
  129. static void psample_group_destroy(struct psample_group *group)
  130. {
  131. psample_group_notify(group, PSAMPLE_CMD_DEL_GROUP);
  132. list_del(&group->list);
  133. kfree_rcu(group, rcu);
  134. }
  135. static struct psample_group *
  136. psample_group_lookup(struct net *net, u32 group_num)
  137. {
  138. struct psample_group *group;
  139. list_for_each_entry(group, &psample_groups_list, list)
  140. if ((group->group_num == group_num) && (group->net == net))
  141. return group;
  142. return NULL;
  143. }
  144. struct psample_group *psample_group_get(struct net *net, u32 group_num)
  145. {
  146. struct psample_group *group;
  147. spin_lock(&psample_groups_lock);
  148. group = psample_group_lookup(net, group_num);
  149. if (!group) {
  150. group = psample_group_create(net, group_num);
  151. if (!group)
  152. goto out;
  153. }
  154. group->refcount++;
  155. out:
  156. spin_unlock(&psample_groups_lock);
  157. return group;
  158. }
  159. EXPORT_SYMBOL_GPL(psample_group_get);
  160. void psample_group_put(struct psample_group *group)
  161. {
  162. spin_lock(&psample_groups_lock);
  163. if (--group->refcount == 0)
  164. psample_group_destroy(group);
  165. spin_unlock(&psample_groups_lock);
  166. }
  167. EXPORT_SYMBOL_GPL(psample_group_put);
  168. void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
  169. u32 trunc_size, int in_ifindex, int out_ifindex,
  170. u32 sample_rate)
  171. {
  172. struct sk_buff *nl_skb;
  173. int data_len;
  174. int meta_len;
  175. void *data;
  176. int ret;
  177. meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) +
  178. (out_ifindex ? nla_total_size(sizeof(u16)) : 0) +
  179. nla_total_size(sizeof(u32)) + /* sample_rate */
  180. nla_total_size(sizeof(u32)) + /* orig_size */
  181. nla_total_size(sizeof(u32)) + /* group_num */
  182. nla_total_size(sizeof(u32)); /* seq */
  183. data_len = min(skb->len, trunc_size);
  184. if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
  185. data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
  186. - NLA_ALIGNTO;
  187. nl_skb = genlmsg_new(meta_len + nla_total_size(data_len), GFP_ATOMIC);
  188. if (unlikely(!nl_skb))
  189. return;
  190. data = genlmsg_put(nl_skb, 0, 0, &psample_nl_family, 0,
  191. PSAMPLE_CMD_SAMPLE);
  192. if (unlikely(!data))
  193. goto error;
  194. if (in_ifindex) {
  195. ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_IIFINDEX, in_ifindex);
  196. if (unlikely(ret < 0))
  197. goto error;
  198. }
  199. if (out_ifindex) {
  200. ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OIFINDEX, out_ifindex);
  201. if (unlikely(ret < 0))
  202. goto error;
  203. }
  204. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_RATE, sample_rate);
  205. if (unlikely(ret < 0))
  206. goto error;
  207. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_ORIGSIZE, skb->len);
  208. if (unlikely(ret < 0))
  209. goto error;
  210. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
  211. if (unlikely(ret < 0))
  212. goto error;
  213. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_GROUP_SEQ, group->seq++);
  214. if (unlikely(ret < 0))
  215. goto error;
  216. if (data_len) {
  217. int nla_len = nla_total_size(data_len);
  218. struct nlattr *nla;
  219. nla = skb_put(nl_skb, nla_len);
  220. nla->nla_type = PSAMPLE_ATTR_DATA;
  221. nla->nla_len = nla_attr_size(data_len);
  222. if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
  223. goto error;
  224. }
  225. genlmsg_end(nl_skb, data);
  226. genlmsg_multicast_netns(&psample_nl_family, group->net, nl_skb, 0,
  227. PSAMPLE_NL_MCGRP_SAMPLE, GFP_ATOMIC);
  228. return;
  229. error:
  230. pr_err_ratelimited("Could not create psample log message\n");
  231. nlmsg_free(nl_skb);
  232. }
  233. EXPORT_SYMBOL_GPL(psample_sample_packet);
  234. static int __init psample_module_init(void)
  235. {
  236. return genl_register_family(&psample_nl_family);
  237. }
  238. static void __exit psample_module_exit(void)
  239. {
  240. genl_unregister_family(&psample_nl_family);
  241. }
  242. module_init(psample_module_init);
  243. module_exit(psample_module_exit);
  244. MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>");
  245. MODULE_DESCRIPTION("netlink channel for packet sampling");
  246. MODULE_LICENSE("GPL v2");