act_skbmod.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/act_skbmod.c skb data modifier
  4. *
  5. * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/if_arp.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/rtnetlink.h>
  13. #include <net/netlink.h>
  14. #include <net/pkt_sched.h>
  15. #include <net/pkt_cls.h>
  16. #include <linux/tc_act/tc_skbmod.h>
  17. #include <net/tc_act/tc_skbmod.h>
  18. static unsigned int skbmod_net_id;
  19. static struct tc_action_ops act_skbmod_ops;
  20. #define MAX_EDIT_LEN ETH_HLEN
  21. static int tcf_skbmod_act(struct sk_buff *skb, const struct tc_action *a,
  22. struct tcf_result *res)
  23. {
  24. struct tcf_skbmod *d = to_skbmod(a);
  25. int action;
  26. struct tcf_skbmod_params *p;
  27. u64 flags;
  28. int err;
  29. tcf_lastuse_update(&d->tcf_tm);
  30. bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
  31. action = READ_ONCE(d->tcf_action);
  32. if (unlikely(action == TC_ACT_SHOT))
  33. goto drop;
  34. if (!skb->dev || skb->dev->type != ARPHRD_ETHER)
  35. return action;
  36. /* XXX: if you are going to edit more fields beyond ethernet header
  37. * (example when you add IP header replacement or vlan swap)
  38. * then MAX_EDIT_LEN needs to change appropriately
  39. */
  40. err = skb_ensure_writable(skb, MAX_EDIT_LEN);
  41. if (unlikely(err)) /* best policy is to drop on the floor */
  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 tcf_proto *tp, u32 flags,
  73. struct netlink_ext_ack *extack)
  74. {
  75. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  76. struct nlattr *tb[TCA_SKBMOD_MAX + 1];
  77. struct tcf_skbmod_params *p, *p_old;
  78. struct tcf_chain *goto_ch = NULL;
  79. struct tc_skbmod *parm;
  80. u32 lflags = 0, index;
  81. struct tcf_skbmod *d;
  82. bool exists = false;
  83. u8 *daddr = NULL;
  84. u8 *saddr = NULL;
  85. u16 eth_type = 0;
  86. int ret = 0, err;
  87. if (!nla)
  88. return -EINVAL;
  89. err = nla_parse_nested_deprecated(tb, TCA_SKBMOD_MAX, nla,
  90. 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. index = parm->index;
  109. if (parm->flags & SKBMOD_F_SWAPMAC)
  110. lflags = SKBMOD_F_SWAPMAC;
  111. err = tcf_idr_check_alloc(tn, &index, a, bind);
  112. if (err < 0)
  113. return err;
  114. exists = err;
  115. if (exists && bind)
  116. return 0;
  117. if (!lflags) {
  118. if (exists)
  119. tcf_idr_release(*a, bind);
  120. else
  121. tcf_idr_cleanup(tn, index);
  122. return -EINVAL;
  123. }
  124. if (!exists) {
  125. ret = tcf_idr_create(tn, index, est, a,
  126. &act_skbmod_ops, bind, true, 0);
  127. if (ret) {
  128. tcf_idr_cleanup(tn, index);
  129. return ret;
  130. }
  131. ret = ACT_P_CREATED;
  132. } else if (!ovr) {
  133. tcf_idr_release(*a, bind);
  134. return -EEXIST;
  135. }
  136. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  137. if (err < 0)
  138. goto release_idr;
  139. d = to_skbmod(*a);
  140. p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
  141. if (unlikely(!p)) {
  142. err = -ENOMEM;
  143. goto put_chain;
  144. }
  145. p->flags = lflags;
  146. if (ovr)
  147. spin_lock_bh(&d->tcf_lock);
  148. /* Protected by tcf_lock if overwriting existing action. */
  149. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  150. p_old = rcu_dereference_protected(d->skbmod_p, 1);
  151. if (lflags & SKBMOD_F_DMAC)
  152. ether_addr_copy(p->eth_dst, daddr);
  153. if (lflags & SKBMOD_F_SMAC)
  154. ether_addr_copy(p->eth_src, saddr);
  155. if (lflags & SKBMOD_F_ETYPE)
  156. p->eth_type = htons(eth_type);
  157. rcu_assign_pointer(d->skbmod_p, p);
  158. if (ovr)
  159. spin_unlock_bh(&d->tcf_lock);
  160. if (p_old)
  161. kfree_rcu(p_old, rcu);
  162. if (goto_ch)
  163. tcf_chain_put_by_act(goto_ch);
  164. return ret;
  165. put_chain:
  166. if (goto_ch)
  167. tcf_chain_put_by_act(goto_ch);
  168. release_idr:
  169. tcf_idr_release(*a, bind);
  170. return err;
  171. }
  172. static void tcf_skbmod_cleanup(struct tc_action *a)
  173. {
  174. struct tcf_skbmod *d = to_skbmod(a);
  175. struct tcf_skbmod_params *p;
  176. p = rcu_dereference_protected(d->skbmod_p, 1);
  177. if (p)
  178. kfree_rcu(p, rcu);
  179. }
  180. static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
  181. int bind, int ref)
  182. {
  183. struct tcf_skbmod *d = to_skbmod(a);
  184. unsigned char *b = skb_tail_pointer(skb);
  185. struct tcf_skbmod_params *p;
  186. struct tc_skbmod opt = {
  187. .index = d->tcf_index,
  188. .refcnt = refcount_read(&d->tcf_refcnt) - ref,
  189. .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
  190. };
  191. struct tcf_t t;
  192. spin_lock_bh(&d->tcf_lock);
  193. opt.action = d->tcf_action;
  194. p = rcu_dereference_protected(d->skbmod_p,
  195. lockdep_is_held(&d->tcf_lock));
  196. opt.flags = p->flags;
  197. if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
  198. goto nla_put_failure;
  199. if ((p->flags & SKBMOD_F_DMAC) &&
  200. nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
  201. goto nla_put_failure;
  202. if ((p->flags & SKBMOD_F_SMAC) &&
  203. nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
  204. goto nla_put_failure;
  205. if ((p->flags & SKBMOD_F_ETYPE) &&
  206. nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
  207. goto nla_put_failure;
  208. tcf_tm_dump(&t, &d->tcf_tm);
  209. if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
  210. goto nla_put_failure;
  211. spin_unlock_bh(&d->tcf_lock);
  212. return skb->len;
  213. nla_put_failure:
  214. spin_unlock_bh(&d->tcf_lock);
  215. nlmsg_trim(skb, b);
  216. return -1;
  217. }
  218. static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
  219. struct netlink_callback *cb, int type,
  220. const struct tc_action_ops *ops,
  221. struct netlink_ext_ack *extack)
  222. {
  223. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  224. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  225. }
  226. static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index)
  227. {
  228. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  229. return tcf_idr_search(tn, a, index);
  230. }
  231. static struct tc_action_ops act_skbmod_ops = {
  232. .kind = "skbmod",
  233. .id = TCA_ACT_SKBMOD,
  234. .owner = THIS_MODULE,
  235. .act = tcf_skbmod_act,
  236. .dump = tcf_skbmod_dump,
  237. .init = tcf_skbmod_init,
  238. .cleanup = tcf_skbmod_cleanup,
  239. .walk = tcf_skbmod_walker,
  240. .lookup = tcf_skbmod_search,
  241. .size = sizeof(struct tcf_skbmod),
  242. };
  243. static __net_init int skbmod_init_net(struct net *net)
  244. {
  245. struct tc_action_net *tn = net_generic(net, skbmod_net_id);
  246. return tc_action_net_init(net, tn, &act_skbmod_ops);
  247. }
  248. static void __net_exit skbmod_exit_net(struct list_head *net_list)
  249. {
  250. tc_action_net_exit(net_list, skbmod_net_id);
  251. }
  252. static struct pernet_operations skbmod_net_ops = {
  253. .init = skbmod_init_net,
  254. .exit_batch = skbmod_exit_net,
  255. .id = &skbmod_net_id,
  256. .size = sizeof(struct tc_action_net),
  257. };
  258. MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
  259. MODULE_DESCRIPTION("SKB data mod-ing");
  260. MODULE_LICENSE("GPL");
  261. static int __init skbmod_init_module(void)
  262. {
  263. return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
  264. }
  265. static void __exit skbmod_cleanup_module(void)
  266. {
  267. tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
  268. }
  269. module_init(skbmod_init_module);
  270. module_exit(skbmod_cleanup_module);