psample.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * net/psample/psample.c - Netlink channel for packet sampling
  4. * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
  5. */
  6. #include <linux/types.h>
  7. #include <linux/kernel.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/module.h>
  10. #include <linux/timekeeping.h>
  11. #include <net/net_namespace.h>
  12. #include <net/sock.h>
  13. #include <net/netlink.h>
  14. #include <net/genetlink.h>
  15. #include <net/psample.h>
  16. #include <linux/spinlock.h>
  17. #include <net/ip_tunnels.h>
  18. #include <net/dst_metadata.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. .flags = GENL_MCAST_CAP_NET_ADMIN, },
  31. };
  32. static struct genl_family psample_nl_family __ro_after_init;
  33. static int psample_group_nl_fill(struct sk_buff *msg,
  34. struct psample_group *group,
  35. enum psample_command cmd, u32 portid, u32 seq,
  36. int flags)
  37. {
  38. void *hdr;
  39. int ret;
  40. hdr = genlmsg_put(msg, portid, seq, &psample_nl_family, flags, cmd);
  41. if (!hdr)
  42. return -EMSGSIZE;
  43. ret = nla_put_u32(msg, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
  44. if (ret < 0)
  45. goto error;
  46. ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_REFCOUNT, group->refcount);
  47. if (ret < 0)
  48. goto error;
  49. ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_SEQ, group->seq);
  50. if (ret < 0)
  51. goto error;
  52. genlmsg_end(msg, hdr);
  53. return 0;
  54. error:
  55. genlmsg_cancel(msg, hdr);
  56. return -EMSGSIZE;
  57. }
  58. static int psample_nl_cmd_get_group_dumpit(struct sk_buff *msg,
  59. struct netlink_callback *cb)
  60. {
  61. struct psample_group *group;
  62. int start = cb->args[0];
  63. int idx = 0;
  64. int err;
  65. spin_lock_bh(&psample_groups_lock);
  66. list_for_each_entry(group, &psample_groups_list, list) {
  67. if (!net_eq(group->net, sock_net(msg->sk)))
  68. continue;
  69. if (idx < start) {
  70. idx++;
  71. continue;
  72. }
  73. err = psample_group_nl_fill(msg, group, PSAMPLE_CMD_NEW_GROUP,
  74. NETLINK_CB(cb->skb).portid,
  75. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  76. if (err)
  77. break;
  78. idx++;
  79. }
  80. spin_unlock_bh(&psample_groups_lock);
  81. cb->args[0] = idx;
  82. return msg->len;
  83. }
  84. static const struct genl_small_ops psample_nl_ops[] = {
  85. {
  86. .cmd = PSAMPLE_CMD_GET_GROUP,
  87. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  88. .dumpit = psample_nl_cmd_get_group_dumpit,
  89. /* can be retrieved by unprivileged users */
  90. }
  91. };
  92. static struct genl_family psample_nl_family __ro_after_init = {
  93. .name = PSAMPLE_GENL_NAME,
  94. .version = PSAMPLE_GENL_VERSION,
  95. .maxattr = PSAMPLE_ATTR_MAX,
  96. .netnsok = true,
  97. .module = THIS_MODULE,
  98. .mcgrps = psample_nl_mcgrps,
  99. .small_ops = psample_nl_ops,
  100. .n_small_ops = ARRAY_SIZE(psample_nl_ops),
  101. .resv_start_op = PSAMPLE_CMD_GET_GROUP + 1,
  102. .n_mcgrps = ARRAY_SIZE(psample_nl_mcgrps),
  103. };
  104. static void psample_group_notify(struct psample_group *group,
  105. enum psample_command cmd)
  106. {
  107. struct sk_buff *msg;
  108. int err;
  109. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  110. if (!msg)
  111. return;
  112. err = psample_group_nl_fill(msg, group, cmd, 0, 0, NLM_F_MULTI);
  113. if (!err)
  114. genlmsg_multicast_netns(&psample_nl_family, group->net, msg, 0,
  115. PSAMPLE_NL_MCGRP_CONFIG, GFP_ATOMIC);
  116. else
  117. nlmsg_free(msg);
  118. }
  119. static struct psample_group *psample_group_create(struct net *net,
  120. u32 group_num)
  121. {
  122. struct psample_group *group;
  123. group = kzalloc(sizeof(*group), GFP_ATOMIC);
  124. if (!group)
  125. return NULL;
  126. group->net = net;
  127. group->group_num = group_num;
  128. list_add_tail(&group->list, &psample_groups_list);
  129. psample_group_notify(group, PSAMPLE_CMD_NEW_GROUP);
  130. return group;
  131. }
  132. static void psample_group_destroy(struct psample_group *group)
  133. {
  134. psample_group_notify(group, PSAMPLE_CMD_DEL_GROUP);
  135. list_del(&group->list);
  136. kfree_rcu(group, rcu);
  137. }
  138. static struct psample_group *
  139. psample_group_lookup(struct net *net, u32 group_num)
  140. {
  141. struct psample_group *group;
  142. list_for_each_entry(group, &psample_groups_list, list)
  143. if ((group->group_num == group_num) && (group->net == net))
  144. return group;
  145. return NULL;
  146. }
  147. struct psample_group *psample_group_get(struct net *net, u32 group_num)
  148. {
  149. struct psample_group *group;
  150. spin_lock_bh(&psample_groups_lock);
  151. group = psample_group_lookup(net, group_num);
  152. if (!group) {
  153. group = psample_group_create(net, group_num);
  154. if (!group)
  155. goto out;
  156. }
  157. group->refcount++;
  158. out:
  159. spin_unlock_bh(&psample_groups_lock);
  160. return group;
  161. }
  162. EXPORT_SYMBOL_GPL(psample_group_get);
  163. void psample_group_take(struct psample_group *group)
  164. {
  165. spin_lock_bh(&psample_groups_lock);
  166. group->refcount++;
  167. spin_unlock_bh(&psample_groups_lock);
  168. }
  169. EXPORT_SYMBOL_GPL(psample_group_take);
  170. void psample_group_put(struct psample_group *group)
  171. {
  172. spin_lock_bh(&psample_groups_lock);
  173. if (--group->refcount == 0)
  174. psample_group_destroy(group);
  175. spin_unlock_bh(&psample_groups_lock);
  176. }
  177. EXPORT_SYMBOL_GPL(psample_group_put);
  178. #ifdef CONFIG_INET
  179. static int __psample_ip_tun_to_nlattr(struct sk_buff *skb,
  180. struct ip_tunnel_info *tun_info)
  181. {
  182. unsigned short tun_proto = ip_tunnel_info_af(tun_info);
  183. const void *tun_opts = ip_tunnel_info_opts(tun_info);
  184. const struct ip_tunnel_key *tun_key = &tun_info->key;
  185. int tun_opts_len = tun_info->options_len;
  186. if (test_bit(IP_TUNNEL_KEY_BIT, tun_key->tun_flags) &&
  187. nla_put_be64(skb, PSAMPLE_TUNNEL_KEY_ATTR_ID, tun_key->tun_id,
  188. PSAMPLE_TUNNEL_KEY_ATTR_PAD))
  189. return -EMSGSIZE;
  190. if (tun_info->mode & IP_TUNNEL_INFO_BRIDGE &&
  191. nla_put_flag(skb, PSAMPLE_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE))
  192. return -EMSGSIZE;
  193. switch (tun_proto) {
  194. case AF_INET:
  195. if (tun_key->u.ipv4.src &&
  196. nla_put_in_addr(skb, PSAMPLE_TUNNEL_KEY_ATTR_IPV4_SRC,
  197. tun_key->u.ipv4.src))
  198. return -EMSGSIZE;
  199. if (tun_key->u.ipv4.dst &&
  200. nla_put_in_addr(skb, PSAMPLE_TUNNEL_KEY_ATTR_IPV4_DST,
  201. tun_key->u.ipv4.dst))
  202. return -EMSGSIZE;
  203. break;
  204. case AF_INET6:
  205. if (!ipv6_addr_any(&tun_key->u.ipv6.src) &&
  206. nla_put_in6_addr(skb, PSAMPLE_TUNNEL_KEY_ATTR_IPV6_SRC,
  207. &tun_key->u.ipv6.src))
  208. return -EMSGSIZE;
  209. if (!ipv6_addr_any(&tun_key->u.ipv6.dst) &&
  210. nla_put_in6_addr(skb, PSAMPLE_TUNNEL_KEY_ATTR_IPV6_DST,
  211. &tun_key->u.ipv6.dst))
  212. return -EMSGSIZE;
  213. break;
  214. }
  215. if (tun_key->tos &&
  216. nla_put_u8(skb, PSAMPLE_TUNNEL_KEY_ATTR_TOS, tun_key->tos))
  217. return -EMSGSIZE;
  218. if (nla_put_u8(skb, PSAMPLE_TUNNEL_KEY_ATTR_TTL, tun_key->ttl))
  219. return -EMSGSIZE;
  220. if (test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, tun_key->tun_flags) &&
  221. nla_put_flag(skb, PSAMPLE_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  222. return -EMSGSIZE;
  223. if (test_bit(IP_TUNNEL_CSUM_BIT, tun_key->tun_flags) &&
  224. nla_put_flag(skb, PSAMPLE_TUNNEL_KEY_ATTR_CSUM))
  225. return -EMSGSIZE;
  226. if (tun_key->tp_src &&
  227. nla_put_be16(skb, PSAMPLE_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src))
  228. return -EMSGSIZE;
  229. if (tun_key->tp_dst &&
  230. nla_put_be16(skb, PSAMPLE_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst))
  231. return -EMSGSIZE;
  232. if (test_bit(IP_TUNNEL_OAM_BIT, tun_key->tun_flags) &&
  233. nla_put_flag(skb, PSAMPLE_TUNNEL_KEY_ATTR_OAM))
  234. return -EMSGSIZE;
  235. if (tun_opts_len) {
  236. if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, tun_key->tun_flags) &&
  237. nla_put(skb, PSAMPLE_TUNNEL_KEY_ATTR_GENEVE_OPTS,
  238. tun_opts_len, tun_opts))
  239. return -EMSGSIZE;
  240. else if (test_bit(IP_TUNNEL_ERSPAN_OPT_BIT,
  241. tun_key->tun_flags) &&
  242. nla_put(skb, PSAMPLE_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
  243. tun_opts_len, tun_opts))
  244. return -EMSGSIZE;
  245. }
  246. return 0;
  247. }
  248. static int psample_ip_tun_to_nlattr(struct sk_buff *skb,
  249. struct ip_tunnel_info *tun_info)
  250. {
  251. struct nlattr *nla;
  252. int err;
  253. nla = nla_nest_start_noflag(skb, PSAMPLE_ATTR_TUNNEL);
  254. if (!nla)
  255. return -EMSGSIZE;
  256. err = __psample_ip_tun_to_nlattr(skb, tun_info);
  257. if (err) {
  258. nla_nest_cancel(skb, nla);
  259. return err;
  260. }
  261. nla_nest_end(skb, nla);
  262. return 0;
  263. }
  264. static int psample_tunnel_meta_len(struct ip_tunnel_info *tun_info)
  265. {
  266. unsigned short tun_proto = ip_tunnel_info_af(tun_info);
  267. const struct ip_tunnel_key *tun_key = &tun_info->key;
  268. int tun_opts_len = tun_info->options_len;
  269. int sum = nla_total_size(0); /* PSAMPLE_ATTR_TUNNEL */
  270. if (test_bit(IP_TUNNEL_KEY_BIT, tun_key->tun_flags))
  271. sum += nla_total_size_64bit(sizeof(u64));
  272. if (tun_info->mode & IP_TUNNEL_INFO_BRIDGE)
  273. sum += nla_total_size(0);
  274. switch (tun_proto) {
  275. case AF_INET:
  276. if (tun_key->u.ipv4.src)
  277. sum += nla_total_size(sizeof(u32));
  278. if (tun_key->u.ipv4.dst)
  279. sum += nla_total_size(sizeof(u32));
  280. break;
  281. case AF_INET6:
  282. if (!ipv6_addr_any(&tun_key->u.ipv6.src))
  283. sum += nla_total_size(sizeof(struct in6_addr));
  284. if (!ipv6_addr_any(&tun_key->u.ipv6.dst))
  285. sum += nla_total_size(sizeof(struct in6_addr));
  286. break;
  287. }
  288. if (tun_key->tos)
  289. sum += nla_total_size(sizeof(u8));
  290. sum += nla_total_size(sizeof(u8)); /* TTL */
  291. if (test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, tun_key->tun_flags))
  292. sum += nla_total_size(0);
  293. if (test_bit(IP_TUNNEL_CSUM_BIT, tun_key->tun_flags))
  294. sum += nla_total_size(0);
  295. if (tun_key->tp_src)
  296. sum += nla_total_size(sizeof(u16));
  297. if (tun_key->tp_dst)
  298. sum += nla_total_size(sizeof(u16));
  299. if (test_bit(IP_TUNNEL_OAM_BIT, tun_key->tun_flags))
  300. sum += nla_total_size(0);
  301. if (tun_opts_len) {
  302. if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, tun_key->tun_flags))
  303. sum += nla_total_size(tun_opts_len);
  304. else if (test_bit(IP_TUNNEL_ERSPAN_OPT_BIT,
  305. tun_key->tun_flags))
  306. sum += nla_total_size(tun_opts_len);
  307. }
  308. return sum;
  309. }
  310. #endif
  311. void psample_sample_packet(struct psample_group *group,
  312. const struct sk_buff *skb, u32 sample_rate,
  313. const struct psample_metadata *md)
  314. {
  315. ktime_t tstamp = ktime_get_real();
  316. int out_ifindex = md->out_ifindex;
  317. int in_ifindex = md->in_ifindex;
  318. u32 trunc_size = md->trunc_size;
  319. #ifdef CONFIG_INET
  320. struct ip_tunnel_info *tun_info;
  321. #endif
  322. struct sk_buff *nl_skb;
  323. int data_len;
  324. int meta_len;
  325. void *data;
  326. int ret;
  327. if (!genl_has_listeners(&psample_nl_family, group->net,
  328. PSAMPLE_NL_MCGRP_SAMPLE))
  329. return;
  330. meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) +
  331. (out_ifindex ? nla_total_size(sizeof(u16)) : 0) +
  332. (md->out_tc_valid ? nla_total_size(sizeof(u16)) : 0) +
  333. (md->out_tc_occ_valid ? nla_total_size_64bit(sizeof(u64)) : 0) +
  334. (md->latency_valid ? nla_total_size_64bit(sizeof(u64)) : 0) +
  335. nla_total_size(sizeof(u32)) + /* sample_rate */
  336. nla_total_size(sizeof(u32)) + /* orig_size */
  337. nla_total_size(sizeof(u32)) + /* group_num */
  338. nla_total_size(sizeof(u32)) + /* seq */
  339. nla_total_size_64bit(sizeof(u64)) + /* timestamp */
  340. nla_total_size(sizeof(u16)) + /* protocol */
  341. (md->user_cookie_len ?
  342. nla_total_size(md->user_cookie_len) : 0) + /* user cookie */
  343. (md->rate_as_probability ?
  344. nla_total_size(0) : 0); /* rate as probability */
  345. #ifdef CONFIG_INET
  346. tun_info = skb_tunnel_info(skb);
  347. if (tun_info)
  348. meta_len += psample_tunnel_meta_len(tun_info);
  349. #endif
  350. data_len = min(skb->len, trunc_size);
  351. if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
  352. data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
  353. - NLA_ALIGNTO;
  354. nl_skb = genlmsg_new(meta_len + nla_total_size(data_len), GFP_ATOMIC);
  355. if (unlikely(!nl_skb))
  356. return;
  357. data = genlmsg_put(nl_skb, 0, 0, &psample_nl_family, 0,
  358. PSAMPLE_CMD_SAMPLE);
  359. if (unlikely(!data))
  360. goto error;
  361. if (in_ifindex) {
  362. ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_IIFINDEX, in_ifindex);
  363. if (unlikely(ret < 0))
  364. goto error;
  365. }
  366. if (out_ifindex) {
  367. ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OIFINDEX, out_ifindex);
  368. if (unlikely(ret < 0))
  369. goto error;
  370. }
  371. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_RATE, sample_rate);
  372. if (unlikely(ret < 0))
  373. goto error;
  374. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_ORIGSIZE, skb->len);
  375. if (unlikely(ret < 0))
  376. goto error;
  377. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
  378. if (unlikely(ret < 0))
  379. goto error;
  380. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_GROUP_SEQ, group->seq++);
  381. if (unlikely(ret < 0))
  382. goto error;
  383. if (md->out_tc_valid) {
  384. ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OUT_TC, md->out_tc);
  385. if (unlikely(ret < 0))
  386. goto error;
  387. }
  388. if (md->out_tc_occ_valid) {
  389. ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_OUT_TC_OCC,
  390. md->out_tc_occ, PSAMPLE_ATTR_PAD);
  391. if (unlikely(ret < 0))
  392. goto error;
  393. }
  394. if (md->latency_valid) {
  395. ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_LATENCY,
  396. md->latency, PSAMPLE_ATTR_PAD);
  397. if (unlikely(ret < 0))
  398. goto error;
  399. }
  400. ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_TIMESTAMP,
  401. ktime_to_ns(tstamp), PSAMPLE_ATTR_PAD);
  402. if (unlikely(ret < 0))
  403. goto error;
  404. ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_PROTO,
  405. be16_to_cpu(skb->protocol));
  406. if (unlikely(ret < 0))
  407. goto error;
  408. if (data_len) {
  409. int nla_len = nla_total_size(data_len);
  410. struct nlattr *nla;
  411. nla = skb_put(nl_skb, nla_len);
  412. nla->nla_type = PSAMPLE_ATTR_DATA;
  413. nla->nla_len = nla_attr_size(data_len);
  414. if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
  415. goto error;
  416. }
  417. #ifdef CONFIG_INET
  418. if (tun_info) {
  419. ret = psample_ip_tun_to_nlattr(nl_skb, tun_info);
  420. if (unlikely(ret < 0))
  421. goto error;
  422. }
  423. #endif
  424. if (md->user_cookie && md->user_cookie_len &&
  425. nla_put(nl_skb, PSAMPLE_ATTR_USER_COOKIE, md->user_cookie_len,
  426. md->user_cookie))
  427. goto error;
  428. if (md->rate_as_probability &&
  429. nla_put_flag(nl_skb, PSAMPLE_ATTR_SAMPLE_PROBABILITY))
  430. goto error;
  431. genlmsg_end(nl_skb, data);
  432. genlmsg_multicast_netns(&psample_nl_family, group->net, nl_skb, 0,
  433. PSAMPLE_NL_MCGRP_SAMPLE, GFP_ATOMIC);
  434. return;
  435. error:
  436. pr_err_ratelimited("Could not create psample log message\n");
  437. nlmsg_free(nl_skb);
  438. }
  439. EXPORT_SYMBOL_GPL(psample_sample_packet);
  440. static int __init psample_module_init(void)
  441. {
  442. return genl_register_family(&psample_nl_family);
  443. }
  444. static void __exit psample_module_exit(void)
  445. {
  446. genl_unregister_family(&psample_nl_family);
  447. }
  448. module_init(psample_module_init);
  449. module_exit(psample_module_exit);
  450. MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>");
  451. MODULE_DESCRIPTION("netlink channel for packet sampling");
  452. MODULE_LICENSE("GPL v2");