act_skbedit.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2008, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Author: Alexander Duyck <alexander.h.duyck@intel.com>
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/rtnetlink.h>
  23. #include <net/netlink.h>
  24. #include <net/pkt_sched.h>
  25. #include <net/ip.h>
  26. #include <net/ipv6.h>
  27. #include <net/dsfield.h>
  28. #include <linux/tc_act/tc_skbedit.h>
  29. #include <net/tc_act/tc_skbedit.h>
  30. static unsigned int skbedit_net_id;
  31. static struct tc_action_ops act_skbedit_ops;
  32. static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a,
  33. struct tcf_result *res)
  34. {
  35. struct tcf_skbedit *d = to_skbedit(a);
  36. struct tcf_skbedit_params *params;
  37. int action;
  38. tcf_lastuse_update(&d->tcf_tm);
  39. bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
  40. params = rcu_dereference_bh(d->params);
  41. action = READ_ONCE(d->tcf_action);
  42. if (params->flags & SKBEDIT_F_PRIORITY)
  43. skb->priority = params->priority;
  44. if (params->flags & SKBEDIT_F_INHERITDSFIELD) {
  45. int wlen = skb_network_offset(skb);
  46. switch (tc_skb_protocol(skb)) {
  47. case htons(ETH_P_IP):
  48. wlen += sizeof(struct iphdr);
  49. if (!pskb_may_pull(skb, wlen))
  50. goto err;
  51. skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
  52. break;
  53. case htons(ETH_P_IPV6):
  54. wlen += sizeof(struct ipv6hdr);
  55. if (!pskb_may_pull(skb, wlen))
  56. goto err;
  57. skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
  58. break;
  59. }
  60. }
  61. if (params->flags & SKBEDIT_F_QUEUE_MAPPING &&
  62. skb->dev->real_num_tx_queues > params->queue_mapping)
  63. skb_set_queue_mapping(skb, params->queue_mapping);
  64. if (params->flags & SKBEDIT_F_MARK) {
  65. skb->mark &= ~params->mask;
  66. skb->mark |= params->mark & params->mask;
  67. }
  68. if (params->flags & SKBEDIT_F_PTYPE)
  69. skb->pkt_type = params->ptype;
  70. return action;
  71. err:
  72. qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats));
  73. return TC_ACT_SHOT;
  74. }
  75. static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
  76. [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
  77. [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
  78. [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
  79. [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
  80. [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) },
  81. [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) },
  82. [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) },
  83. };
  84. static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
  85. struct nlattr *est, struct tc_action **a,
  86. int ovr, int bind, bool rtnl_held,
  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_old, *params_new;
  91. struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
  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(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL);
  102. if (err < 0)
  103. return err;
  104. if (tb[TCA_SKBEDIT_PARMS] == NULL)
  105. return -EINVAL;
  106. if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
  107. flags |= SKBEDIT_F_PRIORITY;
  108. priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
  109. }
  110. if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
  111. flags |= SKBEDIT_F_QUEUE_MAPPING;
  112. queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
  113. }
  114. if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
  115. ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
  116. if (!skb_pkt_type_ok(*ptype))
  117. return -EINVAL;
  118. flags |= SKBEDIT_F_PTYPE;
  119. }
  120. if (tb[TCA_SKBEDIT_MARK] != NULL) {
  121. flags |= SKBEDIT_F_MARK;
  122. mark = nla_data(tb[TCA_SKBEDIT_MARK]);
  123. }
  124. if (tb[TCA_SKBEDIT_MASK] != NULL) {
  125. flags |= SKBEDIT_F_MASK;
  126. mask = nla_data(tb[TCA_SKBEDIT_MASK]);
  127. }
  128. if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
  129. u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]);
  130. if (*pure_flags & SKBEDIT_F_INHERITDSFIELD)
  131. flags |= SKBEDIT_F_INHERITDSFIELD;
  132. }
  133. parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
  134. index = parm->index;
  135. err = tcf_idr_check_alloc(tn, &index, a, bind);
  136. if (err < 0)
  137. return err;
  138. exists = err;
  139. if (exists && bind)
  140. return 0;
  141. if (!flags) {
  142. if (exists)
  143. tcf_idr_release(*a, bind);
  144. else
  145. tcf_idr_cleanup(tn, index);
  146. return -EINVAL;
  147. }
  148. if (!exists) {
  149. ret = tcf_idr_create(tn, index, est, a,
  150. &act_skbedit_ops, bind, true);
  151. if (ret) {
  152. tcf_idr_cleanup(tn, index);
  153. return ret;
  154. }
  155. d = to_skbedit(*a);
  156. ret = ACT_P_CREATED;
  157. } else {
  158. d = to_skbedit(*a);
  159. if (!ovr) {
  160. tcf_idr_release(*a, bind);
  161. return -EEXIST;
  162. }
  163. }
  164. ASSERT_RTNL();
  165. params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
  166. if (unlikely(!params_new)) {
  167. tcf_idr_release(*a, bind);
  168. return -ENOMEM;
  169. }
  170. params_new->flags = flags;
  171. if (flags & SKBEDIT_F_PRIORITY)
  172. params_new->priority = *priority;
  173. if (flags & SKBEDIT_F_QUEUE_MAPPING)
  174. params_new->queue_mapping = *queue_mapping;
  175. if (flags & SKBEDIT_F_MARK)
  176. params_new->mark = *mark;
  177. if (flags & SKBEDIT_F_PTYPE)
  178. params_new->ptype = *ptype;
  179. /* default behaviour is to use all the bits */
  180. params_new->mask = 0xffffffff;
  181. if (flags & SKBEDIT_F_MASK)
  182. params_new->mask = *mask;
  183. d->tcf_action = parm->action;
  184. params_old = rtnl_dereference(d->params);
  185. rcu_assign_pointer(d->params, params_new);
  186. if (params_old)
  187. kfree_rcu(params_old, rcu);
  188. if (ret == ACT_P_CREATED)
  189. tcf_idr_insert(tn, *a);
  190. return ret;
  191. }
  192. static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
  193. int bind, int ref)
  194. {
  195. unsigned char *b = skb_tail_pointer(skb);
  196. struct tcf_skbedit *d = to_skbedit(a);
  197. struct tcf_skbedit_params *params;
  198. struct tc_skbedit opt = {
  199. .index = d->tcf_index,
  200. .refcnt = refcount_read(&d->tcf_refcnt) - ref,
  201. .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
  202. .action = d->tcf_action,
  203. };
  204. u64 pure_flags = 0;
  205. struct tcf_t t;
  206. params = rtnl_dereference(d->params);
  207. if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
  208. goto nla_put_failure;
  209. if ((params->flags & SKBEDIT_F_PRIORITY) &&
  210. nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority))
  211. goto nla_put_failure;
  212. if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) &&
  213. nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping))
  214. goto nla_put_failure;
  215. if ((params->flags & SKBEDIT_F_MARK) &&
  216. nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark))
  217. goto nla_put_failure;
  218. if ((params->flags & SKBEDIT_F_PTYPE) &&
  219. nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype))
  220. goto nla_put_failure;
  221. if ((params->flags & SKBEDIT_F_MASK) &&
  222. nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask))
  223. goto nla_put_failure;
  224. if (params->flags & SKBEDIT_F_INHERITDSFIELD)
  225. pure_flags |= SKBEDIT_F_INHERITDSFIELD;
  226. if (pure_flags != 0 &&
  227. nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags))
  228. goto nla_put_failure;
  229. tcf_tm_dump(&t, &d->tcf_tm);
  230. if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
  231. goto nla_put_failure;
  232. return skb->len;
  233. nla_put_failure:
  234. nlmsg_trim(skb, b);
  235. return -1;
  236. }
  237. static void tcf_skbedit_cleanup(struct tc_action *a)
  238. {
  239. struct tcf_skbedit *d = to_skbedit(a);
  240. struct tcf_skbedit_params *params;
  241. params = rcu_dereference_protected(d->params, 1);
  242. if (params)
  243. kfree_rcu(params, rcu);
  244. }
  245. static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
  246. struct netlink_callback *cb, int type,
  247. const struct tc_action_ops *ops,
  248. struct netlink_ext_ack *extack)
  249. {
  250. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  251. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  252. }
  253. static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index,
  254. struct netlink_ext_ack *extack)
  255. {
  256. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  257. return tcf_idr_search(tn, a, index);
  258. }
  259. static struct tc_action_ops act_skbedit_ops = {
  260. .kind = "skbedit",
  261. .type = TCA_ACT_SKBEDIT,
  262. .owner = THIS_MODULE,
  263. .act = tcf_skbedit_act,
  264. .dump = tcf_skbedit_dump,
  265. .init = tcf_skbedit_init,
  266. .cleanup = tcf_skbedit_cleanup,
  267. .walk = tcf_skbedit_walker,
  268. .lookup = tcf_skbedit_search,
  269. .size = sizeof(struct tcf_skbedit),
  270. };
  271. static __net_init int skbedit_init_net(struct net *net)
  272. {
  273. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  274. return tc_action_net_init(net, tn, &act_skbedit_ops);
  275. }
  276. static void __net_exit skbedit_exit_net(struct list_head *net_list)
  277. {
  278. tc_action_net_exit(net_list, skbedit_net_id);
  279. }
  280. static struct pernet_operations skbedit_net_ops = {
  281. .init = skbedit_init_net,
  282. .exit_batch = skbedit_exit_net,
  283. .id = &skbedit_net_id,
  284. .size = sizeof(struct tc_action_net),
  285. };
  286. MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
  287. MODULE_DESCRIPTION("SKB Editing");
  288. MODULE_LICENSE("GPL");
  289. static int __init skbedit_init_module(void)
  290. {
  291. return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
  292. }
  293. static void __exit skbedit_cleanup_module(void)
  294. {
  295. tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
  296. }
  297. module_init(skbedit_init_module);
  298. module_exit(skbedit_cleanup_module);