act_skbedit.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 (skb_protocol(skb, true)) {
  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. u64 packets, u64 drops,
  67. u64 lastuse, bool hw)
  68. {
  69. struct tcf_skbedit *d = to_skbedit(a);
  70. struct tcf_t *tm = &d->tcf_tm;
  71. tcf_action_update_stats(a, bytes, packets, drops, hw);
  72. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  73. }
  74. static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
  75. [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
  76. [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
  77. [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
  78. [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
  79. [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) },
  80. [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) },
  81. [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) },
  82. };
  83. static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
  84. struct nlattr *est, struct tc_action **a,
  85. int ovr, int bind, bool rtnl_held,
  86. struct tcf_proto *tp, u32 act_flags,
  87. struct netlink_ext_ack *extack)
  88. {
  89. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  90. struct tcf_skbedit_params *params_new;
  91. struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
  92. struct tcf_chain *goto_ch = NULL;
  93. struct tc_skbedit *parm;
  94. struct tcf_skbedit *d;
  95. u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
  96. u16 *queue_mapping = NULL, *ptype = NULL;
  97. bool exists = false;
  98. int ret = 0, err;
  99. u32 index;
  100. if (nla == NULL)
  101. return -EINVAL;
  102. err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla,
  103. skbedit_policy, NULL);
  104. if (err < 0)
  105. return err;
  106. if (tb[TCA_SKBEDIT_PARMS] == NULL)
  107. return -EINVAL;
  108. if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
  109. flags |= SKBEDIT_F_PRIORITY;
  110. priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
  111. }
  112. if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
  113. flags |= SKBEDIT_F_QUEUE_MAPPING;
  114. queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
  115. }
  116. if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
  117. ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
  118. if (!skb_pkt_type_ok(*ptype))
  119. return -EINVAL;
  120. flags |= SKBEDIT_F_PTYPE;
  121. }
  122. if (tb[TCA_SKBEDIT_MARK] != NULL) {
  123. flags |= SKBEDIT_F_MARK;
  124. mark = nla_data(tb[TCA_SKBEDIT_MARK]);
  125. }
  126. if (tb[TCA_SKBEDIT_MASK] != NULL) {
  127. flags |= SKBEDIT_F_MASK;
  128. mask = nla_data(tb[TCA_SKBEDIT_MASK]);
  129. }
  130. if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
  131. u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]);
  132. if (*pure_flags & SKBEDIT_F_INHERITDSFIELD)
  133. flags |= SKBEDIT_F_INHERITDSFIELD;
  134. }
  135. parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
  136. index = parm->index;
  137. err = tcf_idr_check_alloc(tn, &index, a, bind);
  138. if (err < 0)
  139. return err;
  140. exists = err;
  141. if (exists && bind)
  142. return 0;
  143. if (!flags) {
  144. if (exists)
  145. tcf_idr_release(*a, bind);
  146. else
  147. tcf_idr_cleanup(tn, index);
  148. return -EINVAL;
  149. }
  150. if (!exists) {
  151. ret = tcf_idr_create(tn, index, est, a,
  152. &act_skbedit_ops, bind, true, 0);
  153. if (ret) {
  154. tcf_idr_cleanup(tn, index);
  155. return ret;
  156. }
  157. d = to_skbedit(*a);
  158. ret = ACT_P_CREATED;
  159. } else {
  160. d = to_skbedit(*a);
  161. if (!ovr) {
  162. tcf_idr_release(*a, bind);
  163. return -EEXIST;
  164. }
  165. }
  166. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  167. if (err < 0)
  168. goto release_idr;
  169. params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
  170. if (unlikely(!params_new)) {
  171. err = -ENOMEM;
  172. goto put_chain;
  173. }
  174. params_new->flags = flags;
  175. if (flags & SKBEDIT_F_PRIORITY)
  176. params_new->priority = *priority;
  177. if (flags & SKBEDIT_F_QUEUE_MAPPING)
  178. params_new->queue_mapping = *queue_mapping;
  179. if (flags & SKBEDIT_F_MARK)
  180. params_new->mark = *mark;
  181. if (flags & SKBEDIT_F_PTYPE)
  182. params_new->ptype = *ptype;
  183. /* default behaviour is to use all the bits */
  184. params_new->mask = 0xffffffff;
  185. if (flags & SKBEDIT_F_MASK)
  186. params_new->mask = *mask;
  187. spin_lock_bh(&d->tcf_lock);
  188. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  189. params_new = rcu_replace_pointer(d->params, params_new,
  190. lockdep_is_held(&d->tcf_lock));
  191. spin_unlock_bh(&d->tcf_lock);
  192. if (params_new)
  193. kfree_rcu(params_new, rcu);
  194. if (goto_ch)
  195. tcf_chain_put_by_act(goto_ch);
  196. return ret;
  197. put_chain:
  198. if (goto_ch)
  199. tcf_chain_put_by_act(goto_ch);
  200. release_idr:
  201. tcf_idr_release(*a, bind);
  202. return err;
  203. }
  204. static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
  205. int bind, int ref)
  206. {
  207. unsigned char *b = skb_tail_pointer(skb);
  208. struct tcf_skbedit *d = to_skbedit(a);
  209. struct tcf_skbedit_params *params;
  210. struct tc_skbedit opt = {
  211. .index = d->tcf_index,
  212. .refcnt = refcount_read(&d->tcf_refcnt) - ref,
  213. .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
  214. };
  215. u64 pure_flags = 0;
  216. struct tcf_t t;
  217. spin_lock_bh(&d->tcf_lock);
  218. params = rcu_dereference_protected(d->params,
  219. lockdep_is_held(&d->tcf_lock));
  220. opt.action = d->tcf_action;
  221. if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
  222. goto nla_put_failure;
  223. if ((params->flags & SKBEDIT_F_PRIORITY) &&
  224. nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority))
  225. goto nla_put_failure;
  226. if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) &&
  227. nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping))
  228. goto nla_put_failure;
  229. if ((params->flags & SKBEDIT_F_MARK) &&
  230. nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark))
  231. goto nla_put_failure;
  232. if ((params->flags & SKBEDIT_F_PTYPE) &&
  233. nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype))
  234. goto nla_put_failure;
  235. if ((params->flags & SKBEDIT_F_MASK) &&
  236. nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask))
  237. goto nla_put_failure;
  238. if (params->flags & SKBEDIT_F_INHERITDSFIELD)
  239. pure_flags |= SKBEDIT_F_INHERITDSFIELD;
  240. if (pure_flags != 0 &&
  241. nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags))
  242. goto nla_put_failure;
  243. tcf_tm_dump(&t, &d->tcf_tm);
  244. if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
  245. goto nla_put_failure;
  246. spin_unlock_bh(&d->tcf_lock);
  247. return skb->len;
  248. nla_put_failure:
  249. spin_unlock_bh(&d->tcf_lock);
  250. nlmsg_trim(skb, b);
  251. return -1;
  252. }
  253. static void tcf_skbedit_cleanup(struct tc_action *a)
  254. {
  255. struct tcf_skbedit *d = to_skbedit(a);
  256. struct tcf_skbedit_params *params;
  257. params = rcu_dereference_protected(d->params, 1);
  258. if (params)
  259. kfree_rcu(params, rcu);
  260. }
  261. static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
  262. struct netlink_callback *cb, int type,
  263. const struct tc_action_ops *ops,
  264. struct netlink_ext_ack *extack)
  265. {
  266. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  267. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  268. }
  269. static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index)
  270. {
  271. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  272. return tcf_idr_search(tn, a, index);
  273. }
  274. static size_t tcf_skbedit_get_fill_size(const struct tc_action *act)
  275. {
  276. return nla_total_size(sizeof(struct tc_skbedit))
  277. + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_PRIORITY */
  278. + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING */
  279. + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MARK */
  280. + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_PTYPE */
  281. + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MASK */
  282. + nla_total_size_64bit(sizeof(u64)); /* TCA_SKBEDIT_FLAGS */
  283. }
  284. static struct tc_action_ops act_skbedit_ops = {
  285. .kind = "skbedit",
  286. .id = TCA_ID_SKBEDIT,
  287. .owner = THIS_MODULE,
  288. .act = tcf_skbedit_act,
  289. .stats_update = tcf_skbedit_stats_update,
  290. .dump = tcf_skbedit_dump,
  291. .init = tcf_skbedit_init,
  292. .cleanup = tcf_skbedit_cleanup,
  293. .walk = tcf_skbedit_walker,
  294. .get_fill_size = tcf_skbedit_get_fill_size,
  295. .lookup = tcf_skbedit_search,
  296. .size = sizeof(struct tcf_skbedit),
  297. };
  298. static __net_init int skbedit_init_net(struct net *net)
  299. {
  300. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  301. return tc_action_net_init(net, tn, &act_skbedit_ops);
  302. }
  303. static void __net_exit skbedit_exit_net(struct list_head *net_list)
  304. {
  305. tc_action_net_exit(net_list, skbedit_net_id);
  306. }
  307. static struct pernet_operations skbedit_net_ops = {
  308. .init = skbedit_init_net,
  309. .exit_batch = skbedit_exit_net,
  310. .id = &skbedit_net_id,
  311. .size = sizeof(struct tc_action_net),
  312. };
  313. MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
  314. MODULE_DESCRIPTION("SKB Editing");
  315. MODULE_LICENSE("GPL");
  316. static int __init skbedit_init_module(void)
  317. {
  318. return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
  319. }
  320. static void __exit skbedit_cleanup_module(void)
  321. {
  322. tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
  323. }
  324. module_init(skbedit_init_module);
  325. module_exit(skbedit_cleanup_module);