/net/sched/act_skbedit.c

http://github.com/mirrors/linux · C · 380 lines · 319 code · 54 blank · 7 comment · 59 complexity · 91c2f1de73ff796a20ba89159d698ae3 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2008, Intel Corporation.
  4. *
  5. * Author: Alexander Duyck <alexander.h.duyck@intel.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/rtnetlink.h>
  12. #include <net/netlink.h>
  13. #include <net/pkt_sched.h>
  14. #include <net/ip.h>
  15. #include <net/ipv6.h>
  16. #include <net/dsfield.h>
  17. #include <net/pkt_cls.h>
  18. #include <linux/tc_act/tc_skbedit.h>
  19. #include <net/tc_act/tc_skbedit.h>
  20. static unsigned int skbedit_net_id;
  21. static struct tc_action_ops act_skbedit_ops;
  22. static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a,
  23. struct tcf_result *res)
  24. {
  25. struct tcf_skbedit *d = to_skbedit(a);
  26. struct tcf_skbedit_params *params;
  27. int action;
  28. tcf_lastuse_update(&d->tcf_tm);
  29. bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
  30. params = rcu_dereference_bh(d->params);
  31. action = READ_ONCE(d->tcf_action);
  32. if (params->flags & SKBEDIT_F_PRIORITY)
  33. skb->priority = params->priority;
  34. if (params->flags & SKBEDIT_F_INHERITDSFIELD) {
  35. int wlen = skb_network_offset(skb);
  36. switch (tc_skb_protocol(skb)) {
  37. case htons(ETH_P_IP):
  38. wlen += sizeof(struct iphdr);
  39. if (!pskb_may_pull(skb, wlen))
  40. goto err;
  41. skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
  42. break;
  43. case htons(ETH_P_IPV6):
  44. wlen += sizeof(struct ipv6hdr);
  45. if (!pskb_may_pull(skb, wlen))
  46. goto err;
  47. skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
  48. break;
  49. }
  50. }
  51. if (params->flags & SKBEDIT_F_QUEUE_MAPPING &&
  52. skb->dev->real_num_tx_queues > params->queue_mapping)
  53. skb_set_queue_mapping(skb, params->queue_mapping);
  54. if (params->flags & SKBEDIT_F_MARK) {
  55. skb->mark &= ~params->mask;
  56. skb->mark |= params->mark & params->mask;
  57. }
  58. if (params->flags & SKBEDIT_F_PTYPE)
  59. skb->pkt_type = params->ptype;
  60. return action;
  61. err:
  62. qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats));
  63. return TC_ACT_SHOT;
  64. }
  65. static void tcf_skbedit_stats_update(struct tc_action *a, u64 bytes,
  66. u32 packets, u64 lastuse, bool hw)
  67. {
  68. struct tcf_skbedit *d = to_skbedit(a);
  69. struct tcf_t *tm = &d->tcf_tm;
  70. tcf_action_update_stats(a, bytes, packets, false, hw);
  71. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  72. }
  73. static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
  74. [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
  75. [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
  76. [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
  77. [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
  78. [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) },
  79. [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) },
  80. [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) },
  81. };
  82. static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
  83. struct nlattr *est, struct tc_action **a,
  84. int ovr, int bind, bool rtnl_held,
  85. struct tcf_proto *tp, u32 act_flags,
  86. struct netlink_ext_ack *extack)
  87. {
  88. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  89. struct tcf_skbedit_params *params_new;
  90. struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
  91. struct tcf_chain *goto_ch = NULL;
  92. struct tc_skbedit *parm;
  93. struct tcf_skbedit *d;
  94. u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
  95. u16 *queue_mapping = NULL, *ptype = NULL;
  96. bool exists = false;
  97. int ret = 0, err;
  98. u32 index;
  99. if (nla == NULL)
  100. return -EINVAL;
  101. err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla,
  102. skbedit_policy, NULL);
  103. if (err < 0)
  104. return err;
  105. if (tb[TCA_SKBEDIT_PARMS] == NULL)
  106. return -EINVAL;
  107. if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
  108. flags |= SKBEDIT_F_PRIORITY;
  109. priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
  110. }
  111. if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
  112. flags |= SKBEDIT_F_QUEUE_MAPPING;
  113. queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
  114. }
  115. if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
  116. ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
  117. if (!skb_pkt_type_ok(*ptype))
  118. return -EINVAL;
  119. flags |= SKBEDIT_F_PTYPE;
  120. }
  121. if (tb[TCA_SKBEDIT_MARK] != NULL) {
  122. flags |= SKBEDIT_F_MARK;
  123. mark = nla_data(tb[TCA_SKBEDIT_MARK]);
  124. }
  125. if (tb[TCA_SKBEDIT_MASK] != NULL) {
  126. flags |= SKBEDIT_F_MASK;
  127. mask = nla_data(tb[TCA_SKBEDIT_MASK]);
  128. }
  129. if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
  130. u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]);
  131. if (*pure_flags & SKBEDIT_F_INHERITDSFIELD)
  132. flags |= SKBEDIT_F_INHERITDSFIELD;
  133. }
  134. parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
  135. index = parm->index;
  136. err = tcf_idr_check_alloc(tn, &index, a, bind);
  137. if (err < 0)
  138. return err;
  139. exists = err;
  140. if (exists && bind)
  141. return 0;
  142. if (!flags) {
  143. if (exists)
  144. tcf_idr_release(*a, bind);
  145. else
  146. tcf_idr_cleanup(tn, index);
  147. return -EINVAL;
  148. }
  149. if (!exists) {
  150. ret = tcf_idr_create(tn, index, est, a,
  151. &act_skbedit_ops, bind, true, 0);
  152. if (ret) {
  153. tcf_idr_cleanup(tn, index);
  154. return ret;
  155. }
  156. d = to_skbedit(*a);
  157. ret = ACT_P_CREATED;
  158. } else {
  159. d = to_skbedit(*a);
  160. if (!ovr) {
  161. tcf_idr_release(*a, bind);
  162. return -EEXIST;
  163. }
  164. }
  165. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  166. if (err < 0)
  167. goto release_idr;
  168. params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
  169. if (unlikely(!params_new)) {
  170. err = -ENOMEM;
  171. goto put_chain;
  172. }
  173. params_new->flags = flags;
  174. if (flags & SKBEDIT_F_PRIORITY)
  175. params_new->priority = *priority;
  176. if (flags & SKBEDIT_F_QUEUE_MAPPING)
  177. params_new->queue_mapping = *queue_mapping;
  178. if (flags & SKBEDIT_F_MARK)
  179. params_new->mark = *mark;
  180. if (flags & SKBEDIT_F_PTYPE)
  181. params_new->ptype = *ptype;
  182. /* default behaviour is to use all the bits */
  183. params_new->mask = 0xffffffff;
  184. if (flags & SKBEDIT_F_MASK)
  185. params_new->mask = *mask;
  186. spin_lock_bh(&d->tcf_lock);
  187. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  188. params_new = rcu_replace_pointer(d->params, params_new,
  189. lockdep_is_held(&d->tcf_lock));
  190. spin_unlock_bh(&d->tcf_lock);
  191. if (params_new)
  192. kfree_rcu(params_new, rcu);
  193. if (goto_ch)
  194. tcf_chain_put_by_act(goto_ch);
  195. if (ret == ACT_P_CREATED)
  196. tcf_idr_insert(tn, *a);
  197. return ret;
  198. put_chain:
  199. if (goto_ch)
  200. tcf_chain_put_by_act(goto_ch);
  201. release_idr:
  202. tcf_idr_release(*a, bind);
  203. return err;
  204. }
  205. static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
  206. int bind, int ref)
  207. {
  208. unsigned char *b = skb_tail_pointer(skb);
  209. struct tcf_skbedit *d = to_skbedit(a);
  210. struct tcf_skbedit_params *params;
  211. struct tc_skbedit opt = {
  212. .index = d->tcf_index,
  213. .refcnt = refcount_read(&d->tcf_refcnt) - ref,
  214. .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
  215. };
  216. u64 pure_flags = 0;
  217. struct tcf_t t;
  218. spin_lock_bh(&d->tcf_lock);
  219. params = rcu_dereference_protected(d->params,
  220. lockdep_is_held(&d->tcf_lock));
  221. opt.action = d->tcf_action;
  222. if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
  223. goto nla_put_failure;
  224. if ((params->flags & SKBEDIT_F_PRIORITY) &&
  225. nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority))
  226. goto nla_put_failure;
  227. if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) &&
  228. nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping))
  229. goto nla_put_failure;
  230. if ((params->flags & SKBEDIT_F_MARK) &&
  231. nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark))
  232. goto nla_put_failure;
  233. if ((params->flags & SKBEDIT_F_PTYPE) &&
  234. nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype))
  235. goto nla_put_failure;
  236. if ((params->flags & SKBEDIT_F_MASK) &&
  237. nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask))
  238. goto nla_put_failure;
  239. if (params->flags & SKBEDIT_F_INHERITDSFIELD)
  240. pure_flags |= SKBEDIT_F_INHERITDSFIELD;
  241. if (pure_flags != 0 &&
  242. nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags))
  243. goto nla_put_failure;
  244. tcf_tm_dump(&t, &d->tcf_tm);
  245. if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
  246. goto nla_put_failure;
  247. spin_unlock_bh(&d->tcf_lock);
  248. return skb->len;
  249. nla_put_failure:
  250. spin_unlock_bh(&d->tcf_lock);
  251. nlmsg_trim(skb, b);
  252. return -1;
  253. }
  254. static void tcf_skbedit_cleanup(struct tc_action *a)
  255. {
  256. struct tcf_skbedit *d = to_skbedit(a);
  257. struct tcf_skbedit_params *params;
  258. params = rcu_dereference_protected(d->params, 1);
  259. if (params)
  260. kfree_rcu(params, rcu);
  261. }
  262. static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
  263. struct netlink_callback *cb, int type,
  264. const struct tc_action_ops *ops,
  265. struct netlink_ext_ack *extack)
  266. {
  267. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  268. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  269. }
  270. static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index)
  271. {
  272. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  273. return tcf_idr_search(tn, a, index);
  274. }
  275. static size_t tcf_skbedit_get_fill_size(const struct tc_action *act)
  276. {
  277. return nla_total_size(sizeof(struct tc_skbedit))
  278. + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_PRIORITY */
  279. + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING */
  280. + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MARK */
  281. + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_PTYPE */
  282. + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MASK */
  283. + nla_total_size_64bit(sizeof(u64)); /* TCA_SKBEDIT_FLAGS */
  284. }
  285. static struct tc_action_ops act_skbedit_ops = {
  286. .kind = "skbedit",
  287. .id = TCA_ID_SKBEDIT,
  288. .owner = THIS_MODULE,
  289. .act = tcf_skbedit_act,
  290. .stats_update = tcf_skbedit_stats_update,
  291. .dump = tcf_skbedit_dump,
  292. .init = tcf_skbedit_init,
  293. .cleanup = tcf_skbedit_cleanup,
  294. .walk = tcf_skbedit_walker,
  295. .get_fill_size = tcf_skbedit_get_fill_size,
  296. .lookup = tcf_skbedit_search,
  297. .size = sizeof(struct tcf_skbedit),
  298. };
  299. static __net_init int skbedit_init_net(struct net *net)
  300. {
  301. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  302. return tc_action_net_init(net, tn, &act_skbedit_ops);
  303. }
  304. static void __net_exit skbedit_exit_net(struct list_head *net_list)
  305. {
  306. tc_action_net_exit(net_list, skbedit_net_id);
  307. }
  308. static struct pernet_operations skbedit_net_ops = {
  309. .init = skbedit_init_net,
  310. .exit_batch = skbedit_exit_net,
  311. .id = &skbedit_net_id,
  312. .size = sizeof(struct tc_action_net),
  313. };
  314. MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
  315. MODULE_DESCRIPTION("SKB Editing");
  316. MODULE_LICENSE("GPL");
  317. static int __init skbedit_init_module(void)
  318. {
  319. return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
  320. }
  321. static void __exit skbedit_cleanup_module(void)
  322. {
  323. tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
  324. }
  325. module_init(skbedit_init_module);
  326. module_exit(skbedit_cleanup_module);