act_skbmod.c 7.5 KB

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