act_skbedit.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. if (nla == NULL)
  99. return -EINVAL;
  100. err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL);
  101. if (err < 0)
  102. return err;
  103. if (tb[TCA_SKBEDIT_PARMS] == NULL)
  104. return -EINVAL;
  105. if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
  106. flags |= SKBEDIT_F_PRIORITY;
  107. priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
  108. }
  109. if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
  110. flags |= SKBEDIT_F_QUEUE_MAPPING;
  111. queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
  112. }
  113. if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
  114. ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
  115. if (!skb_pkt_type_ok(*ptype))
  116. return -EINVAL;
  117. flags |= SKBEDIT_F_PTYPE;
  118. }
  119. if (tb[TCA_SKBEDIT_MARK] != NULL) {
  120. flags |= SKBEDIT_F_MARK;
  121. mark = nla_data(tb[TCA_SKBEDIT_MARK]);
  122. }
  123. if (tb[TCA_SKBEDIT_MASK] != NULL) {
  124. flags |= SKBEDIT_F_MASK;
  125. mask = nla_data(tb[TCA_SKBEDIT_MASK]);
  126. }
  127. if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
  128. u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]);
  129. if (*pure_flags & SKBEDIT_F_INHERITDSFIELD)
  130. flags |= SKBEDIT_F_INHERITDSFIELD;
  131. }
  132. parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
  133. err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
  134. if (err < 0)
  135. return err;
  136. exists = err;
  137. if (exists && bind)
  138. return 0;
  139. if (!flags) {
  140. if (exists)
  141. tcf_idr_release(*a, bind);
  142. else
  143. tcf_idr_cleanup(tn, parm->index);
  144. return -EINVAL;
  145. }
  146. if (!exists) {
  147. ret = tcf_idr_create(tn, parm->index, est, a,
  148. &act_skbedit_ops, bind, true);
  149. if (ret) {
  150. tcf_idr_cleanup(tn, parm->index);
  151. return ret;
  152. }
  153. d = to_skbedit(*a);
  154. ret = ACT_P_CREATED;
  155. } else {
  156. d = to_skbedit(*a);
  157. if (!ovr) {
  158. tcf_idr_release(*a, bind);
  159. return -EEXIST;
  160. }
  161. }
  162. ASSERT_RTNL();
  163. params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
  164. if (unlikely(!params_new)) {
  165. if (ret == ACT_P_CREATED)
  166. tcf_idr_release(*a, bind);
  167. return -ENOMEM;
  168. }
  169. params_new->flags = flags;
  170. if (flags & SKBEDIT_F_PRIORITY)
  171. params_new->priority = *priority;
  172. if (flags & SKBEDIT_F_QUEUE_MAPPING)
  173. params_new->queue_mapping = *queue_mapping;
  174. if (flags & SKBEDIT_F_MARK)
  175. params_new->mark = *mark;
  176. if (flags & SKBEDIT_F_PTYPE)
  177. params_new->ptype = *ptype;
  178. /* default behaviour is to use all the bits */
  179. params_new->mask = 0xffffffff;
  180. if (flags & SKBEDIT_F_MASK)
  181. params_new->mask = *mask;
  182. d->tcf_action = parm->action;
  183. params_old = rtnl_dereference(d->params);
  184. rcu_assign_pointer(d->params, params_new);
  185. if (params_old)
  186. kfree_rcu(params_old, rcu);
  187. if (ret == ACT_P_CREATED)
  188. tcf_idr_insert(tn, *a);
  189. return ret;
  190. }
  191. static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
  192. int bind, int ref)
  193. {
  194. unsigned char *b = skb_tail_pointer(skb);
  195. struct tcf_skbedit *d = to_skbedit(a);
  196. struct tcf_skbedit_params *params;
  197. struct tc_skbedit opt = {
  198. .index = d->tcf_index,
  199. .refcnt = refcount_read(&d->tcf_refcnt) - ref,
  200. .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
  201. .action = d->tcf_action,
  202. };
  203. u64 pure_flags = 0;
  204. struct tcf_t t;
  205. params = rtnl_dereference(d->params);
  206. if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
  207. goto nla_put_failure;
  208. if ((params->flags & SKBEDIT_F_PRIORITY) &&
  209. nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority))
  210. goto nla_put_failure;
  211. if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) &&
  212. nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping))
  213. goto nla_put_failure;
  214. if ((params->flags & SKBEDIT_F_MARK) &&
  215. nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark))
  216. goto nla_put_failure;
  217. if ((params->flags & SKBEDIT_F_PTYPE) &&
  218. nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype))
  219. goto nla_put_failure;
  220. if ((params->flags & SKBEDIT_F_MASK) &&
  221. nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask))
  222. goto nla_put_failure;
  223. if (params->flags & SKBEDIT_F_INHERITDSFIELD)
  224. pure_flags |= SKBEDIT_F_INHERITDSFIELD;
  225. if (pure_flags != 0 &&
  226. nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags))
  227. goto nla_put_failure;
  228. tcf_tm_dump(&t, &d->tcf_tm);
  229. if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
  230. goto nla_put_failure;
  231. return skb->len;
  232. nla_put_failure:
  233. nlmsg_trim(skb, b);
  234. return -1;
  235. }
  236. static void tcf_skbedit_cleanup(struct tc_action *a)
  237. {
  238. struct tcf_skbedit *d = to_skbedit(a);
  239. struct tcf_skbedit_params *params;
  240. params = rcu_dereference_protected(d->params, 1);
  241. if (params)
  242. kfree_rcu(params, rcu);
  243. }
  244. static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
  245. struct netlink_callback *cb, int type,
  246. const struct tc_action_ops *ops,
  247. struct netlink_ext_ack *extack)
  248. {
  249. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  250. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  251. }
  252. static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index,
  253. struct netlink_ext_ack *extack)
  254. {
  255. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  256. return tcf_idr_search(tn, a, index);
  257. }
  258. static struct tc_action_ops act_skbedit_ops = {
  259. .kind = "skbedit",
  260. .type = TCA_ACT_SKBEDIT,
  261. .owner = THIS_MODULE,
  262. .act = tcf_skbedit_act,
  263. .dump = tcf_skbedit_dump,
  264. .init = tcf_skbedit_init,
  265. .cleanup = tcf_skbedit_cleanup,
  266. .walk = tcf_skbedit_walker,
  267. .lookup = tcf_skbedit_search,
  268. .size = sizeof(struct tcf_skbedit),
  269. };
  270. static __net_init int skbedit_init_net(struct net *net)
  271. {
  272. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  273. return tc_action_net_init(tn, &act_skbedit_ops);
  274. }
  275. static void __net_exit skbedit_exit_net(struct list_head *net_list)
  276. {
  277. tc_action_net_exit(net_list, skbedit_net_id);
  278. }
  279. static struct pernet_operations skbedit_net_ops = {
  280. .init = skbedit_init_net,
  281. .exit_batch = skbedit_exit_net,
  282. .id = &skbedit_net_id,
  283. .size = sizeof(struct tc_action_net),
  284. };
  285. MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
  286. MODULE_DESCRIPTION("SKB Editing");
  287. MODULE_LICENSE("GPL");
  288. static int __init skbedit_init_module(void)
  289. {
  290. return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
  291. }
  292. static void __exit skbedit_cleanup_module(void)
  293. {
  294. tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
  295. }
  296. module_init(skbedit_init_module);
  297. module_exit(skbedit_cleanup_module);