act_skbmod.c 7.8 KB

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