psample.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 <[email protected]>
  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_UNS_ADMIN_PERM },
  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 (tun_key->tun_flags & TUNNEL_KEY &&
  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 ((tun_key->tun_flags & TUNNEL_DONT_FRAGMENT) &&
  221. nla_put_flag(skb, PSAMPLE_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  222. return -EMSGSIZE;
  223. if ((tun_key->tun_flags & TUNNEL_CSUM) &&
  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 ((tun_key->tun_flags & TUNNEL_OAM) &&
  233. nla_put_flag(skb, PSAMPLE_TUNNEL_KEY_ATTR_OAM))
  234. return -EMSGSIZE;
  235. if (tun_opts_len) {
  236. if (tun_key->tun_flags & TUNNEL_GENEVE_OPT &&
  237. nla_put(skb, PSAMPLE_TUNNEL_KEY_ATTR_GENEVE_OPTS,
  238. tun_opts_len, tun_opts))
  239. return -EMSGSIZE;
  240. else if (tun_key->tun_flags & TUNNEL_ERSPAN_OPT &&
  241. nla_put(skb, PSAMPLE_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
  242. tun_opts_len, tun_opts))
  243. return -EMSGSIZE;
  244. }
  245. return 0;
  246. }
  247. static int psample_ip_tun_to_nlattr(struct sk_buff *skb,
  248. struct ip_tunnel_info *tun_info)
  249. {
  250. struct nlattr *nla;
  251. int err;
  252. nla = nla_nest_start_noflag(skb, PSAMPLE_ATTR_TUNNEL);
  253. if (!nla)
  254. return -EMSGSIZE;
  255. err = __psample_ip_tun_to_nlattr(skb, tun_info);
  256. if (err) {
  257. nla_nest_cancel(skb, nla);
  258. return err;
  259. }
  260. nla_nest_end(skb, nla);
  261. return 0;
  262. }
  263. static int psample_tunnel_meta_len(struct ip_tunnel_info *tun_info)
  264. {
  265. unsigned short tun_proto = ip_tunnel_info_af(tun_info);
  266. const struct ip_tunnel_key *tun_key = &tun_info->key;
  267. int tun_opts_len = tun_info->options_len;
  268. int sum = nla_total_size(0); /* PSAMPLE_ATTR_TUNNEL */
  269. if (tun_key->tun_flags & TUNNEL_KEY)
  270. sum += nla_total_size_64bit(sizeof(u64));
  271. if (tun_info->mode & IP_TUNNEL_INFO_BRIDGE)
  272. sum += nla_total_size(0);
  273. switch (tun_proto) {
  274. case AF_INET:
  275. if (tun_key->u.ipv4.src)
  276. sum += nla_total_size(sizeof(u32));
  277. if (tun_key->u.ipv4.dst)
  278. sum += nla_total_size(sizeof(u32));
  279. break;
  280. case AF_INET6:
  281. if (!ipv6_addr_any(&tun_key->u.ipv6.src))
  282. sum += nla_total_size(sizeof(struct in6_addr));
  283. if (!ipv6_addr_any(&tun_key->u.ipv6.dst))
  284. sum += nla_total_size(sizeof(struct in6_addr));
  285. break;
  286. }
  287. if (tun_key->tos)
  288. sum += nla_total_size(sizeof(u8));
  289. sum += nla_total_size(sizeof(u8)); /* TTL */
  290. if (tun_key->tun_flags & TUNNEL_DONT_FRAGMENT)
  291. sum += nla_total_size(0);
  292. if (tun_key->tun_flags & TUNNEL_CSUM)
  293. sum += nla_total_size(0);
  294. if (tun_key->tp_src)
  295. sum += nla_total_size(sizeof(u16));
  296. if (tun_key->tp_dst)
  297. sum += nla_total_size(sizeof(u16));
  298. if (tun_key->tun_flags & TUNNEL_OAM)
  299. sum += nla_total_size(0);
  300. if (tun_opts_len) {
  301. if (tun_key->tun_flags & TUNNEL_GENEVE_OPT)
  302. sum += nla_total_size(tun_opts_len);
  303. else if (tun_key->tun_flags & TUNNEL_ERSPAN_OPT)
  304. sum += nla_total_size(tun_opts_len);
  305. }
  306. return sum;
  307. }
  308. #endif
  309. void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
  310. u32 sample_rate, const struct psample_metadata *md)
  311. {
  312. ktime_t tstamp = ktime_get_real();
  313. int out_ifindex = md->out_ifindex;
  314. int in_ifindex = md->in_ifindex;
  315. u32 trunc_size = md->trunc_size;
  316. #ifdef CONFIG_INET
  317. struct ip_tunnel_info *tun_info;
  318. #endif
  319. struct sk_buff *nl_skb;
  320. int data_len;
  321. int meta_len;
  322. void *data;
  323. int ret;
  324. meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) +
  325. (out_ifindex ? nla_total_size(sizeof(u16)) : 0) +
  326. (md->out_tc_valid ? nla_total_size(sizeof(u16)) : 0) +
  327. (md->out_tc_occ_valid ? nla_total_size_64bit(sizeof(u64)) : 0) +
  328. (md->latency_valid ? nla_total_size_64bit(sizeof(u64)) : 0) +
  329. nla_total_size(sizeof(u32)) + /* sample_rate */
  330. nla_total_size(sizeof(u32)) + /* orig_size */
  331. nla_total_size(sizeof(u32)) + /* group_num */
  332. nla_total_size(sizeof(u32)) + /* seq */
  333. nla_total_size_64bit(sizeof(u64)) + /* timestamp */
  334. nla_total_size(sizeof(u16)); /* protocol */
  335. #ifdef CONFIG_INET
  336. tun_info = skb_tunnel_info(skb);
  337. if (tun_info)
  338. meta_len += psample_tunnel_meta_len(tun_info);
  339. #endif
  340. data_len = min(skb->len, trunc_size);
  341. if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
  342. data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
  343. - NLA_ALIGNTO;
  344. nl_skb = genlmsg_new(meta_len + nla_total_size(data_len), GFP_ATOMIC);
  345. if (unlikely(!nl_skb))
  346. return;
  347. data = genlmsg_put(nl_skb, 0, 0, &psample_nl_family, 0,
  348. PSAMPLE_CMD_SAMPLE);
  349. if (unlikely(!data))
  350. goto error;
  351. if (in_ifindex) {
  352. ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_IIFINDEX, in_ifindex);
  353. if (unlikely(ret < 0))
  354. goto error;
  355. }
  356. if (out_ifindex) {
  357. ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OIFINDEX, out_ifindex);
  358. if (unlikely(ret < 0))
  359. goto error;
  360. }
  361. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_RATE, sample_rate);
  362. if (unlikely(ret < 0))
  363. goto error;
  364. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_ORIGSIZE, skb->len);
  365. if (unlikely(ret < 0))
  366. goto error;
  367. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
  368. if (unlikely(ret < 0))
  369. goto error;
  370. ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_GROUP_SEQ, group->seq++);
  371. if (unlikely(ret < 0))
  372. goto error;
  373. if (md->out_tc_valid) {
  374. ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OUT_TC, md->out_tc);
  375. if (unlikely(ret < 0))
  376. goto error;
  377. }
  378. if (md->out_tc_occ_valid) {
  379. ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_OUT_TC_OCC,
  380. md->out_tc_occ, PSAMPLE_ATTR_PAD);
  381. if (unlikely(ret < 0))
  382. goto error;
  383. }
  384. if (md->latency_valid) {
  385. ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_LATENCY,
  386. md->latency, PSAMPLE_ATTR_PAD);
  387. if (unlikely(ret < 0))
  388. goto error;
  389. }
  390. ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_TIMESTAMP,
  391. ktime_to_ns(tstamp), PSAMPLE_ATTR_PAD);
  392. if (unlikely(ret < 0))
  393. goto error;
  394. ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_PROTO,
  395. be16_to_cpu(skb->protocol));
  396. if (unlikely(ret < 0))
  397. goto error;
  398. if (data_len) {
  399. int nla_len = nla_total_size(data_len);
  400. struct nlattr *nla;
  401. nla = skb_put(nl_skb, nla_len);
  402. nla->nla_type = PSAMPLE_ATTR_DATA;
  403. nla->nla_len = nla_attr_size(data_len);
  404. if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
  405. goto error;
  406. }
  407. #ifdef CONFIG_INET
  408. if (tun_info) {
  409. ret = psample_ip_tun_to_nlattr(nl_skb, tun_info);
  410. if (unlikely(ret < 0))
  411. goto error;
  412. }
  413. #endif
  414. genlmsg_end(nl_skb, data);
  415. genlmsg_multicast_netns(&psample_nl_family, group->net, nl_skb, 0,
  416. PSAMPLE_NL_MCGRP_SAMPLE, GFP_ATOMIC);
  417. return;
  418. error:
  419. pr_err_ratelimited("Could not create psample log message\n");
  420. nlmsg_free(nl_skb);
  421. }
  422. EXPORT_SYMBOL_GPL(psample_sample_packet);
  423. static int __init psample_module_init(void)
  424. {
  425. return genl_register_family(&psample_nl_family);
  426. }
  427. static void __exit psample_module_exit(void)
  428. {
  429. genl_unregister_family(&psample_nl_family);
  430. }
  431. module_init(psample_module_init);
  432. module_exit(psample_module_exit);
  433. MODULE_AUTHOR("Yotam Gigi <[email protected]>");
  434. MODULE_DESCRIPTION("netlink channel for packet sampling");
  435. MODULE_LICENSE("GPL v2");