act_skbmod.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/inet_ecn.h>
  14. #include <net/netlink.h>
  15. #include <net/pkt_sched.h>
  16. #include <net/pkt_cls.h>
  17. #include <net/tc_wrapper.h>
  18. #include <linux/tc_act/tc_skbmod.h>
  19. #include <net/tc_act/tc_skbmod.h>
  20. static struct tc_action_ops act_skbmod_ops;
  21. TC_INDIRECT_SCOPE int tcf_skbmod_act(struct sk_buff *skb,
  22. const struct tc_action *a,
  23. struct tcf_result *res)
  24. {
  25. struct tcf_skbmod *d = to_skbmod(a);
  26. int action, max_edit_len, err;
  27. struct tcf_skbmod_params *p;
  28. u64 flags;
  29. tcf_lastuse_update(&d->tcf_tm);
  30. bstats_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. max_edit_len = skb_mac_header_len(skb);
  35. p = rcu_dereference_bh(d->skbmod_p);
  36. flags = p->flags;
  37. /* tcf_skbmod_init() guarantees "flags" to be one of the following:
  38. * 1. a combination of SKBMOD_F_{DMAC,SMAC,ETYPE}
  39. * 2. SKBMOD_F_SWAPMAC
  40. * 3. SKBMOD_F_ECN
  41. * SKBMOD_F_ECN only works with IP packets; all other flags only work with Ethernet
  42. * packets.
  43. */
  44. if (flags == SKBMOD_F_ECN) {
  45. switch (skb_protocol(skb, true)) {
  46. case cpu_to_be16(ETH_P_IP):
  47. case cpu_to_be16(ETH_P_IPV6):
  48. max_edit_len += skb_network_header_len(skb);
  49. break;
  50. default:
  51. goto out;
  52. }
  53. } else if (!skb->dev || skb->dev->type != ARPHRD_ETHER) {
  54. goto out;
  55. }
  56. err = skb_ensure_writable(skb, max_edit_len);
  57. if (unlikely(err)) /* best policy is to drop on the floor */
  58. goto drop;
  59. if (flags & SKBMOD_F_DMAC)
  60. ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
  61. if (flags & SKBMOD_F_SMAC)
  62. ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
  63. if (flags & SKBMOD_F_ETYPE)
  64. eth_hdr(skb)->h_proto = p->eth_type;
  65. if (flags & SKBMOD_F_SWAPMAC) {
  66. u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
  67. /*XXX: I am sure we can come up with more efficient swapping*/
  68. ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
  69. ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
  70. ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
  71. }
  72. if (flags & SKBMOD_F_ECN)
  73. INET_ECN_set_ce(skb);
  74. out:
  75. return action;
  76. drop:
  77. qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
  78. return TC_ACT_SHOT;
  79. }
  80. static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
  81. [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) },
  82. [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN },
  83. [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN },
  84. [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 },
  85. };
  86. static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
  87. struct nlattr *est, struct tc_action **a,
  88. struct tcf_proto *tp, u32 flags,
  89. struct netlink_ext_ack *extack)
  90. {
  91. struct tc_action_net *tn = net_generic(net, act_skbmod_ops.net_id);
  92. bool ovr = flags & TCA_ACT_FLAGS_REPLACE;
  93. bool bind = flags & TCA_ACT_FLAGS_BIND;
  94. struct nlattr *tb[TCA_SKBMOD_MAX + 1];
  95. struct tcf_skbmod_params *p, *p_old;
  96. struct tcf_chain *goto_ch = NULL;
  97. struct tc_skbmod *parm;
  98. u32 lflags = 0, index;
  99. struct tcf_skbmod *d;
  100. bool exists = false;
  101. u8 *daddr = NULL;
  102. u8 *saddr = NULL;
  103. u16 eth_type = 0;
  104. int ret = 0, err;
  105. if (!nla)
  106. return -EINVAL;
  107. err = nla_parse_nested_deprecated(tb, TCA_SKBMOD_MAX, nla,
  108. skbmod_policy, NULL);
  109. if (err < 0)
  110. return err;
  111. if (!tb[TCA_SKBMOD_PARMS])
  112. return -EINVAL;
  113. if (tb[TCA_SKBMOD_DMAC]) {
  114. daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
  115. lflags |= SKBMOD_F_DMAC;
  116. }
  117. if (tb[TCA_SKBMOD_SMAC]) {
  118. saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
  119. lflags |= SKBMOD_F_SMAC;
  120. }
  121. if (tb[TCA_SKBMOD_ETYPE]) {
  122. eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
  123. lflags |= SKBMOD_F_ETYPE;
  124. }
  125. parm = nla_data(tb[TCA_SKBMOD_PARMS]);
  126. index = parm->index;
  127. if (parm->flags & SKBMOD_F_SWAPMAC)
  128. lflags = SKBMOD_F_SWAPMAC;
  129. if (parm->flags & SKBMOD_F_ECN)
  130. lflags = SKBMOD_F_ECN;
  131. err = tcf_idr_check_alloc(tn, &index, a, bind);
  132. if (err < 0)
  133. return err;
  134. exists = err;
  135. if (exists && bind)
  136. return ACT_P_BOUND;
  137. if (!lflags) {
  138. if (exists)
  139. tcf_idr_release(*a, bind);
  140. else
  141. tcf_idr_cleanup(tn, index);
  142. return -EINVAL;
  143. }
  144. if (!exists) {
  145. ret = tcf_idr_create(tn, index, est, a,
  146. &act_skbmod_ops, bind, true, flags);
  147. if (ret) {
  148. tcf_idr_cleanup(tn, index);
  149. return ret;
  150. }
  151. ret = ACT_P_CREATED;
  152. } else if (!ovr) {
  153. tcf_idr_release(*a, bind);
  154. return -EEXIST;
  155. }
  156. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  157. if (err < 0)
  158. goto release_idr;
  159. d = to_skbmod(*a);
  160. p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
  161. if (unlikely(!p)) {
  162. err = -ENOMEM;
  163. goto put_chain;
  164. }
  165. p->flags = lflags;
  166. if (ovr)
  167. spin_lock_bh(&d->tcf_lock);
  168. /* Protected by tcf_lock if overwriting existing action. */
  169. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  170. p_old = rcu_dereference_protected(d->skbmod_p, 1);
  171. if (lflags & SKBMOD_F_DMAC)
  172. ether_addr_copy(p->eth_dst, daddr);
  173. if (lflags & SKBMOD_F_SMAC)
  174. ether_addr_copy(p->eth_src, saddr);
  175. if (lflags & SKBMOD_F_ETYPE)
  176. p->eth_type = htons(eth_type);
  177. rcu_assign_pointer(d->skbmod_p, p);
  178. if (ovr)
  179. spin_unlock_bh(&d->tcf_lock);
  180. if (p_old)
  181. kfree_rcu(p_old, rcu);
  182. if (goto_ch)
  183. tcf_chain_put_by_act(goto_ch);
  184. return ret;
  185. put_chain:
  186. if (goto_ch)
  187. tcf_chain_put_by_act(goto_ch);
  188. release_idr:
  189. tcf_idr_release(*a, bind);
  190. return err;
  191. }
  192. static void tcf_skbmod_cleanup(struct tc_action *a)
  193. {
  194. struct tcf_skbmod *d = to_skbmod(a);
  195. struct tcf_skbmod_params *p;
  196. p = rcu_dereference_protected(d->skbmod_p, 1);
  197. if (p)
  198. kfree_rcu(p, rcu);
  199. }
  200. static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
  201. int bind, int ref)
  202. {
  203. struct tcf_skbmod *d = to_skbmod(a);
  204. unsigned char *b = skb_tail_pointer(skb);
  205. struct tcf_skbmod_params *p;
  206. struct tc_skbmod opt;
  207. struct tcf_t t;
  208. memset(&opt, 0, sizeof(opt));
  209. opt.index = d->tcf_index;
  210. opt.refcnt = refcount_read(&d->tcf_refcnt) - ref;
  211. opt.bindcnt = atomic_read(&d->tcf_bindcnt) - bind;
  212. spin_lock_bh(&d->tcf_lock);
  213. opt.action = d->tcf_action;
  214. p = rcu_dereference_protected(d->skbmod_p,
  215. lockdep_is_held(&d->tcf_lock));
  216. opt.flags = p->flags;
  217. if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
  218. goto nla_put_failure;
  219. if ((p->flags & SKBMOD_F_DMAC) &&
  220. nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
  221. goto nla_put_failure;
  222. if ((p->flags & SKBMOD_F_SMAC) &&
  223. nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
  224. goto nla_put_failure;
  225. if ((p->flags & SKBMOD_F_ETYPE) &&
  226. nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
  227. goto nla_put_failure;
  228. tcf_tm_dump(&t, &d->tcf_tm);
  229. if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
  230. goto nla_put_failure;
  231. spin_unlock_bh(&d->tcf_lock);
  232. return skb->len;
  233. nla_put_failure:
  234. spin_unlock_bh(&d->tcf_lock);
  235. nlmsg_trim(skb, b);
  236. return -1;
  237. }
  238. static struct tc_action_ops act_skbmod_ops = {
  239. .kind = "skbmod",
  240. .id = TCA_ACT_SKBMOD,
  241. .owner = THIS_MODULE,
  242. .act = tcf_skbmod_act,
  243. .dump = tcf_skbmod_dump,
  244. .init = tcf_skbmod_init,
  245. .cleanup = tcf_skbmod_cleanup,
  246. .size = sizeof(struct tcf_skbmod),
  247. };
  248. MODULE_ALIAS_NET_ACT("skbmod");
  249. static __net_init int skbmod_init_net(struct net *net)
  250. {
  251. struct tc_action_net *tn = net_generic(net, act_skbmod_ops.net_id);
  252. return tc_action_net_init(net, tn, &act_skbmod_ops);
  253. }
  254. static void __net_exit skbmod_exit_net(struct list_head *net_list)
  255. {
  256. tc_action_net_exit(net_list, act_skbmod_ops.net_id);
  257. }
  258. static struct pernet_operations skbmod_net_ops = {
  259. .init = skbmod_init_net,
  260. .exit_batch = skbmod_exit_net,
  261. .id = &act_skbmod_ops.net_id,
  262. .size = sizeof(struct tc_action_net),
  263. };
  264. MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
  265. MODULE_DESCRIPTION("SKB data mod-ing");
  266. MODULE_LICENSE("GPL");
  267. static int __init skbmod_init_module(void)
  268. {
  269. return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
  270. }
  271. static void __exit skbmod_cleanup_module(void)
  272. {
  273. tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
  274. }
  275. module_init(skbmod_init_module);
  276. module_exit(skbmod_cleanup_module);