act_skbmod.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * net/sched/act_skbmod.c skb data modifier
  3. *
  4. * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/rtnetlink.h>
  16. #include <net/netlink.h>
  17. #include <net/pkt_sched.h>
  18. #include <linux/tc_act/tc_skbmod.h>
  19. #include <net/tc_act/tc_skbmod.h>
  20. #define SKBMOD_TAB_MASK 15
  21. static int skbmod_net_id;
  22. static struct tc_action_ops act_skbmod_ops;
  23. #define MAX_EDIT_LEN ETH_HLEN
  24. static int tcf_skbmod_run(struct sk_buff *skb, const struct tc_action *a,
  25. struct tcf_result *res)
  26. {
  27. struct tcf_skbmod *d = to_skbmod(a);
  28. int action;
  29. struct tcf_skbmod_params *p;
  30. u64 flags;
  31. int err;
  32. tcf_lastuse_update(&d->tcf_tm);
  33. bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
  34. /* XXX: if you are going to edit more fields beyond ethernet header
  35. * (example when you add IP header replacement or vlan swap)
  36. * then MAX_EDIT_LEN needs to change appropriately
  37. */
  38. err = skb_ensure_writable(skb, MAX_EDIT_LEN);
  39. if (unlikely(err)) { /* best policy is to drop on the floor */
  40. qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
  41. return TC_ACT_SHOT;
  42. }
  43. rcu_read_lock();
  44. action = READ_ONCE(d->tcf_action);
  45. if (unlikely(action == TC_ACT_SHOT)) {
  46. qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
  47. rcu_read_unlock();
  48. return action;
  49. }
  50. p = rcu_dereference(d->skbmod_p);
  51. flags = p->flags;
  52. if (flags & SKBMOD_F_DMAC)
  53. ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
  54. if (flags & SKBMOD_F_SMAC)
  55. ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
  56. if (flags & SKBMOD_F_ETYPE)
  57. eth_hdr(skb)->h_proto = p->eth_type;
  58. rcu_read_unlock();
  59. if (flags & SKBMOD_F_SWAPMAC) {
  60. u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
  61. /*XXX: I am sure we can come up with more efficient swapping*/
  62. ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
  63. ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
  64. ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
  65. }
  66. return action;
  67. }
  68. static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
  69. [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) },
  70. [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN },
  71. [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN },
  72. [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 },
  73. };
  74. static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
  75. struct nlattr *est, struct tc_action **a,
  76. int ovr, int bind)
  77. {
  78. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  79. struct nlattr *tb[TCA_SKBMOD_MAX + 1];
  80. struct tcf_skbmod_params *p, *p_old;
  81. struct tc_skbmod *parm;
  82. struct tcf_skbmod *d;
  83. bool exists = false;
  84. u8 *daddr = NULL;
  85. u8 *saddr = NULL;
  86. u16 eth_type = 0;
  87. u32 lflags = 0;
  88. int ret = 0, err;
  89. if (!nla)
  90. return -EINVAL;
  91. err = nla_parse_nested(tb, TCA_SKBMOD_MAX, nla, skbmod_policy);
  92. if (err < 0)
  93. return err;
  94. if (!tb[TCA_SKBMOD_PARMS])
  95. return -EINVAL;
  96. if (tb[TCA_SKBMOD_DMAC]) {
  97. daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
  98. lflags |= SKBMOD_F_DMAC;
  99. }
  100. if (tb[TCA_SKBMOD_SMAC]) {
  101. saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
  102. lflags |= SKBMOD_F_SMAC;
  103. }
  104. if (tb[TCA_SKBMOD_ETYPE]) {
  105. eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
  106. lflags |= SKBMOD_F_ETYPE;
  107. }
  108. parm = nla_data(tb[TCA_SKBMOD_PARMS]);
  109. if (parm->flags & SKBMOD_F_SWAPMAC)
  110. lflags = SKBMOD_F_SWAPMAC;
  111. exists = tcf_hash_check(tn, parm->index, a, bind);
  112. if (exists && bind)
  113. return 0;
  114. if (!lflags)
  115. return -EINVAL;
  116. if (!exists) {
  117. ret = tcf_hash_create(tn, parm->index, est, a,
  118. &act_skbmod_ops, bind, true);
  119. if (ret)
  120. return ret;
  121. ret = ACT_P_CREATED;
  122. } else {
  123. tcf_hash_release(*a, bind);
  124. if (!ovr)
  125. return -EEXIST;
  126. }
  127. d = to_skbmod(*a);
  128. ASSERT_RTNL();
  129. p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
  130. if (unlikely(!p)) {
  131. if (ovr)
  132. tcf_hash_release(*a, bind);
  133. return -ENOMEM;
  134. }
  135. p->flags = lflags;
  136. d->tcf_action = parm->action;
  137. p_old = rtnl_dereference(d->skbmod_p);
  138. if (ovr)
  139. spin_lock_bh(&d->tcf_lock);
  140. if (lflags & SKBMOD_F_DMAC)
  141. ether_addr_copy(p->eth_dst, daddr);
  142. if (lflags & SKBMOD_F_SMAC)
  143. ether_addr_copy(p->eth_src, saddr);
  144. if (lflags & SKBMOD_F_ETYPE)
  145. p->eth_type = htons(eth_type);
  146. rcu_assign_pointer(d->skbmod_p, p);
  147. if (ovr)
  148. spin_unlock_bh(&d->tcf_lock);
  149. if (p_old)
  150. kfree_rcu(p_old, rcu);
  151. if (ret == ACT_P_CREATED)
  152. tcf_hash_insert(tn, *a);
  153. return ret;
  154. }
  155. static void tcf_skbmod_cleanup(struct tc_action *a, int bind)
  156. {
  157. struct tcf_skbmod *d = to_skbmod(a);
  158. struct tcf_skbmod_params *p;
  159. p = rcu_dereference_protected(d->skbmod_p, 1);
  160. if (p)
  161. kfree_rcu(p, rcu);
  162. }
  163. static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
  164. int bind, int ref)
  165. {
  166. struct tcf_skbmod *d = to_skbmod(a);
  167. unsigned char *b = skb_tail_pointer(skb);
  168. struct tcf_skbmod_params *p = rtnl_dereference(d->skbmod_p);
  169. struct tc_skbmod opt = {
  170. .index = d->tcf_index,
  171. .refcnt = d->tcf_refcnt - ref,
  172. .bindcnt = d->tcf_bindcnt - bind,
  173. .action = d->tcf_action,
  174. };
  175. struct tcf_t t;
  176. opt.flags = p->flags;
  177. if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
  178. goto nla_put_failure;
  179. if ((p->flags & SKBMOD_F_DMAC) &&
  180. nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
  181. goto nla_put_failure;
  182. if ((p->flags & SKBMOD_F_SMAC) &&
  183. nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
  184. goto nla_put_failure;
  185. if ((p->flags & SKBMOD_F_ETYPE) &&
  186. nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
  187. goto nla_put_failure;
  188. tcf_tm_dump(&t, &d->tcf_tm);
  189. if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
  190. goto nla_put_failure;
  191. return skb->len;
  192. nla_put_failure:
  193. nlmsg_trim(skb, b);
  194. return -1;
  195. }
  196. static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
  197. struct netlink_callback *cb, int type,
  198. const struct tc_action_ops *ops)
  199. {
  200. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  201. return tcf_generic_walker(tn, skb, cb, type, ops);
  202. }
  203. static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index)
  204. {
  205. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  206. return tcf_hash_search(tn, a, index);
  207. }
  208. static struct tc_action_ops act_skbmod_ops = {
  209. .kind = "skbmod",
  210. .type = TCA_ACT_SKBMOD,
  211. .owner = THIS_MODULE,
  212. .act = tcf_skbmod_run,
  213. .dump = tcf_skbmod_dump,
  214. .init = tcf_skbmod_init,
  215. .cleanup = tcf_skbmod_cleanup,
  216. .walk = tcf_skbmod_walker,
  217. .lookup = tcf_skbmod_search,
  218. .size = sizeof(struct tcf_skbmod),
  219. };
  220. static __net_init int skbmod_init_net(struct net *net)
  221. {
  222. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  223. return tc_action_net_init(tn, &act_skbmod_ops, SKBMOD_TAB_MASK);
  224. }
  225. static void __net_exit skbmod_exit_net(struct net *net)
  226. {
  227. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  228. tc_action_net_exit(tn);
  229. }
  230. static struct pernet_operations skbmod_net_ops = {
  231. .init = skbmod_init_net,
  232. .exit = skbmod_exit_net,
  233. .id = &skbmod_net_id,
  234. .size = sizeof(struct tc_action_net),
  235. };
  236. MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
  237. MODULE_DESCRIPTION("SKB data mod-ing");
  238. MODULE_LICENSE("GPL");
  239. static int __init skbmod_init_module(void)
  240. {
  241. return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
  242. }
  243. static void __exit skbmod_cleanup_module(void)
  244. {
  245. tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
  246. }
  247. module_init(skbmod_init_module);
  248. module_exit(skbmod_cleanup_module);