act_skbedit.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <linux/tc_act/tc_skbedit.h>
  26. #include <net/tc_act/tc_skbedit.h>
  27. static unsigned int skbedit_net_id;
  28. static struct tc_action_ops act_skbedit_ops;
  29. static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
  30. struct tcf_result *res)
  31. {
  32. struct tcf_skbedit *d = to_skbedit(a);
  33. spin_lock(&d->tcf_lock);
  34. tcf_lastuse_update(&d->tcf_tm);
  35. bstats_update(&d->tcf_bstats, skb);
  36. if (d->flags & SKBEDIT_F_PRIORITY)
  37. skb->priority = d->priority;
  38. if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
  39. skb->dev->real_num_tx_queues > d->queue_mapping)
  40. skb_set_queue_mapping(skb, d->queue_mapping);
  41. if (d->flags & SKBEDIT_F_MARK) {
  42. skb->mark &= ~d->mask;
  43. skb->mark |= d->mark & d->mask;
  44. }
  45. if (d->flags & SKBEDIT_F_PTYPE)
  46. skb->pkt_type = d->ptype;
  47. spin_unlock(&d->tcf_lock);
  48. return d->tcf_action;
  49. }
  50. static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
  51. [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
  52. [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
  53. [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
  54. [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
  55. [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) },
  56. [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) },
  57. };
  58. static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
  59. struct nlattr *est, struct tc_action **a,
  60. int ovr, int bind)
  61. {
  62. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  63. struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
  64. struct tc_skbedit *parm;
  65. struct tcf_skbedit *d;
  66. u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
  67. u16 *queue_mapping = NULL, *ptype = NULL;
  68. bool exists = false;
  69. int ret = 0, err;
  70. if (nla == NULL)
  71. return -EINVAL;
  72. err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL);
  73. if (err < 0)
  74. return err;
  75. if (tb[TCA_SKBEDIT_PARMS] == NULL)
  76. return -EINVAL;
  77. if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
  78. flags |= SKBEDIT_F_PRIORITY;
  79. priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
  80. }
  81. if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
  82. flags |= SKBEDIT_F_QUEUE_MAPPING;
  83. queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
  84. }
  85. if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
  86. ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
  87. if (!skb_pkt_type_ok(*ptype))
  88. return -EINVAL;
  89. flags |= SKBEDIT_F_PTYPE;
  90. }
  91. if (tb[TCA_SKBEDIT_MARK] != NULL) {
  92. flags |= SKBEDIT_F_MARK;
  93. mark = nla_data(tb[TCA_SKBEDIT_MARK]);
  94. }
  95. if (tb[TCA_SKBEDIT_MASK] != NULL) {
  96. flags |= SKBEDIT_F_MASK;
  97. mask = nla_data(tb[TCA_SKBEDIT_MASK]);
  98. }
  99. parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
  100. exists = tcf_idr_check(tn, parm->index, a, bind);
  101. if (exists && bind)
  102. return 0;
  103. if (!flags) {
  104. if (exists)
  105. tcf_idr_release(*a, bind);
  106. return -EINVAL;
  107. }
  108. if (!exists) {
  109. ret = tcf_idr_create(tn, parm->index, est, a,
  110. &act_skbedit_ops, bind, false);
  111. if (ret)
  112. return ret;
  113. d = to_skbedit(*a);
  114. ret = ACT_P_CREATED;
  115. } else {
  116. d = to_skbedit(*a);
  117. tcf_idr_release(*a, bind);
  118. if (!ovr)
  119. return -EEXIST;
  120. }
  121. spin_lock_bh(&d->tcf_lock);
  122. d->flags = flags;
  123. if (flags & SKBEDIT_F_PRIORITY)
  124. d->priority = *priority;
  125. if (flags & SKBEDIT_F_QUEUE_MAPPING)
  126. d->queue_mapping = *queue_mapping;
  127. if (flags & SKBEDIT_F_MARK)
  128. d->mark = *mark;
  129. if (flags & SKBEDIT_F_PTYPE)
  130. d->ptype = *ptype;
  131. /* default behaviour is to use all the bits */
  132. d->mask = 0xffffffff;
  133. if (flags & SKBEDIT_F_MASK)
  134. d->mask = *mask;
  135. d->tcf_action = parm->action;
  136. spin_unlock_bh(&d->tcf_lock);
  137. if (ret == ACT_P_CREATED)
  138. tcf_idr_insert(tn, *a);
  139. return ret;
  140. }
  141. static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
  142. int bind, int ref)
  143. {
  144. unsigned char *b = skb_tail_pointer(skb);
  145. struct tcf_skbedit *d = to_skbedit(a);
  146. struct tc_skbedit opt = {
  147. .index = d->tcf_index,
  148. .refcnt = d->tcf_refcnt - ref,
  149. .bindcnt = d->tcf_bindcnt - bind,
  150. .action = d->tcf_action,
  151. };
  152. struct tcf_t t;
  153. if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
  154. goto nla_put_failure;
  155. if ((d->flags & SKBEDIT_F_PRIORITY) &&
  156. nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority))
  157. goto nla_put_failure;
  158. if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
  159. nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping))
  160. goto nla_put_failure;
  161. if ((d->flags & SKBEDIT_F_MARK) &&
  162. nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark))
  163. goto nla_put_failure;
  164. if ((d->flags & SKBEDIT_F_PTYPE) &&
  165. nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype))
  166. goto nla_put_failure;
  167. if ((d->flags & SKBEDIT_F_MASK) &&
  168. nla_put_u32(skb, TCA_SKBEDIT_MASK, d->mask))
  169. goto nla_put_failure;
  170. tcf_tm_dump(&t, &d->tcf_tm);
  171. if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
  172. goto nla_put_failure;
  173. return skb->len;
  174. nla_put_failure:
  175. nlmsg_trim(skb, b);
  176. return -1;
  177. }
  178. static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
  179. struct netlink_callback *cb, int type,
  180. const struct tc_action_ops *ops)
  181. {
  182. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  183. return tcf_generic_walker(tn, skb, cb, type, ops);
  184. }
  185. static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index)
  186. {
  187. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  188. return tcf_idr_search(tn, a, index);
  189. }
  190. static struct tc_action_ops act_skbedit_ops = {
  191. .kind = "skbedit",
  192. .type = TCA_ACT_SKBEDIT,
  193. .owner = THIS_MODULE,
  194. .act = tcf_skbedit,
  195. .dump = tcf_skbedit_dump,
  196. .init = tcf_skbedit_init,
  197. .walk = tcf_skbedit_walker,
  198. .lookup = tcf_skbedit_search,
  199. .size = sizeof(struct tcf_skbedit),
  200. };
  201. static __net_init int skbedit_init_net(struct net *net)
  202. {
  203. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  204. return tc_action_net_init(net, tn, &act_skbedit_ops);
  205. }
  206. static void __net_exit skbedit_exit_net(struct net *net)
  207. {
  208. struct tc_action_net *tn = net_generic(net, skbedit_net_id);
  209. tc_action_net_exit(tn);
  210. }
  211. static struct pernet_operations skbedit_net_ops = {
  212. .init = skbedit_init_net,
  213. .exit = skbedit_exit_net,
  214. .id = &skbedit_net_id,
  215. .size = sizeof(struct tc_action_net),
  216. };
  217. MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
  218. MODULE_DESCRIPTION("SKB Editing");
  219. MODULE_LICENSE("GPL");
  220. static int __init skbedit_init_module(void)
  221. {
  222. return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
  223. }
  224. static void __exit skbedit_cleanup_module(void)
  225. {
  226. tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
  227. }
  228. module_init(skbedit_init_module);
  229. module_exit(skbedit_cleanup_module);