act_skbmod.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. struct tcf_skbmod *d;
  79. bool exists = false;
  80. u8 *daddr = NULL;
  81. u8 *saddr = NULL;
  82. u16 eth_type = 0;
  83. u32 lflags = 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. if (parm->flags & SKBMOD_F_SWAPMAC)
  106. lflags = SKBMOD_F_SWAPMAC;
  107. err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
  108. if (err < 0)
  109. return err;
  110. exists = err;
  111. if (exists && bind)
  112. return 0;
  113. if (!lflags) {
  114. if (exists)
  115. tcf_idr_release(*a, bind);
  116. else
  117. tcf_idr_cleanup(tn, parm->index);
  118. return -EINVAL;
  119. }
  120. if (!exists) {
  121. ret = tcf_idr_create(tn, parm->index, est, a,
  122. &act_skbmod_ops, bind, true);
  123. if (ret) {
  124. tcf_idr_cleanup(tn, parm->index);
  125. return ret;
  126. }
  127. ret = ACT_P_CREATED;
  128. } else if (!ovr) {
  129. tcf_idr_release(*a, bind);
  130. return -EEXIST;
  131. }
  132. d = to_skbmod(*a);
  133. p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
  134. if (unlikely(!p)) {
  135. tcf_idr_release(*a, bind);
  136. return -ENOMEM;
  137. }
  138. p->flags = lflags;
  139. d->tcf_action = parm->action;
  140. if (ovr)
  141. spin_lock_bh(&d->tcf_lock);
  142. /* Protected by tcf_lock if overwriting existing action. */
  143. p_old = rcu_dereference_protected(d->skbmod_p, 1);
  144. if (lflags & SKBMOD_F_DMAC)
  145. ether_addr_copy(p->eth_dst, daddr);
  146. if (lflags & SKBMOD_F_SMAC)
  147. ether_addr_copy(p->eth_src, saddr);
  148. if (lflags & SKBMOD_F_ETYPE)
  149. p->eth_type = htons(eth_type);
  150. rcu_assign_pointer(d->skbmod_p, p);
  151. if (ovr)
  152. spin_unlock_bh(&d->tcf_lock);
  153. if (p_old)
  154. kfree_rcu(p_old, rcu);
  155. if (ret == ACT_P_CREATED)
  156. tcf_idr_insert(tn, *a);
  157. return ret;
  158. }
  159. static void tcf_skbmod_cleanup(struct tc_action *a)
  160. {
  161. struct tcf_skbmod *d = to_skbmod(a);
  162. struct tcf_skbmod_params *p;
  163. p = rcu_dereference_protected(d->skbmod_p, 1);
  164. if (p)
  165. kfree_rcu(p, rcu);
  166. }
  167. static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
  168. int bind, int ref)
  169. {
  170. struct tcf_skbmod *d = to_skbmod(a);
  171. unsigned char *b = skb_tail_pointer(skb);
  172. struct tcf_skbmod_params *p;
  173. struct tc_skbmod opt = {
  174. .index = d->tcf_index,
  175. .refcnt = refcount_read(&d->tcf_refcnt) - ref,
  176. .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
  177. };
  178. struct tcf_t t;
  179. spin_lock_bh(&d->tcf_lock);
  180. opt.action = d->tcf_action;
  181. p = rcu_dereference_protected(d->skbmod_p,
  182. lockdep_is_held(&d->tcf_lock));
  183. opt.flags = p->flags;
  184. if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
  185. goto nla_put_failure;
  186. if ((p->flags & SKBMOD_F_DMAC) &&
  187. nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
  188. goto nla_put_failure;
  189. if ((p->flags & SKBMOD_F_SMAC) &&
  190. nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
  191. goto nla_put_failure;
  192. if ((p->flags & SKBMOD_F_ETYPE) &&
  193. nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
  194. goto nla_put_failure;
  195. tcf_tm_dump(&t, &d->tcf_tm);
  196. if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
  197. goto nla_put_failure;
  198. spin_unlock_bh(&d->tcf_lock);
  199. return skb->len;
  200. nla_put_failure:
  201. spin_unlock_bh(&d->tcf_lock);
  202. nlmsg_trim(skb, b);
  203. return -1;
  204. }
  205. static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
  206. struct netlink_callback *cb, int type,
  207. const struct tc_action_ops *ops,
  208. struct netlink_ext_ack *extack)
  209. {
  210. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  211. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  212. }
  213. static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index,
  214. struct netlink_ext_ack *extack)
  215. {
  216. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  217. return tcf_idr_search(tn, a, index);
  218. }
  219. static struct tc_action_ops act_skbmod_ops = {
  220. .kind = "skbmod",
  221. .type = TCA_ACT_SKBMOD,
  222. .owner = THIS_MODULE,
  223. .act = tcf_skbmod_act,
  224. .dump = tcf_skbmod_dump,
  225. .init = tcf_skbmod_init,
  226. .cleanup = tcf_skbmod_cleanup,
  227. .walk = tcf_skbmod_walker,
  228. .lookup = tcf_skbmod_search,
  229. .size = sizeof(struct tcf_skbmod),
  230. };
  231. static __net_init int skbmod_init_net(struct net *net)
  232. {
  233. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  234. return tc_action_net_init(tn, &act_skbmod_ops);
  235. }
  236. static void __net_exit skbmod_exit_net(struct list_head *net_list)
  237. {
  238. tc_action_net_exit(net_list, skbmod_net_id);
  239. }
  240. static struct pernet_operations skbmod_net_ops = {
  241. .init = skbmod_init_net,
  242. .exit_batch = skbmod_exit_net,
  243. .id = &skbmod_net_id,
  244. .size = sizeof(struct tc_action_net),
  245. };
  246. MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
  247. MODULE_DESCRIPTION("SKB data mod-ing");
  248. MODULE_LICENSE("GPL");
  249. static int __init skbmod_init_module(void)
  250. {
  251. return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
  252. }
  253. static void __exit skbmod_cleanup_module(void)
  254. {
  255. tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
  256. }
  257. module_init(skbmod_init_module);
  258. module_exit(skbmod_cleanup_module);