act_skbmod.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. struct tcf_skbmod_params *p;
  27. int max_edit_len, err;
  28. u64 flags;
  29. tcf_lastuse_update(&d->tcf_tm);
  30. bstats_update(this_cpu_ptr(d->common.cpu_bstats), skb);
  31. p = rcu_dereference_bh(d->skbmod_p);
  32. if (unlikely(p->action == TC_ACT_SHOT))
  33. goto drop;
  34. max_edit_len = skb_mac_header_len(skb);
  35. flags = p->flags;
  36. /* tcf_skbmod_init() guarantees "flags" to be one of the following:
  37. * 1. a combination of SKBMOD_F_{DMAC,SMAC,ETYPE}
  38. * 2. SKBMOD_F_SWAPMAC
  39. * 3. SKBMOD_F_ECN
  40. * SKBMOD_F_ECN only works with IP packets; all other flags only work with Ethernet
  41. * packets.
  42. */
  43. if (flags == SKBMOD_F_ECN) {
  44. switch (skb_protocol(skb, true)) {
  45. case cpu_to_be16(ETH_P_IP):
  46. case cpu_to_be16(ETH_P_IPV6):
  47. max_edit_len += skb_network_header_len(skb);
  48. break;
  49. default:
  50. goto out;
  51. }
  52. } else if (!skb->dev || skb->dev->type != ARPHRD_ETHER) {
  53. goto out;
  54. }
  55. err = skb_ensure_writable(skb, max_edit_len);
  56. if (unlikely(err)) /* best policy is to drop on the floor */
  57. goto drop;
  58. if (flags & SKBMOD_F_DMAC)
  59. ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
  60. if (flags & SKBMOD_F_SMAC)
  61. ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
  62. if (flags & SKBMOD_F_ETYPE)
  63. eth_hdr(skb)->h_proto = p->eth_type;
  64. if (flags & SKBMOD_F_SWAPMAC) {
  65. u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
  66. /*XXX: I am sure we can come up with more efficient swapping*/
  67. ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
  68. ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
  69. ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
  70. }
  71. if (flags & SKBMOD_F_ECN)
  72. INET_ECN_set_ce(skb);
  73. out:
  74. return p->action;
  75. drop:
  76. qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
  77. return TC_ACT_SHOT;
  78. }
  79. static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
  80. [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) },
  81. [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN },
  82. [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN },
  83. [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 },
  84. };
  85. static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
  86. struct nlattr *est, struct tc_action **a,
  87. struct tcf_proto *tp, u32 flags,
  88. struct netlink_ext_ack *extack)
  89. {
  90. struct tc_action_net *tn = net_generic(net, act_skbmod_ops.net_id);
  91. bool ovr = flags & TCA_ACT_FLAGS_REPLACE;
  92. bool bind = flags & TCA_ACT_FLAGS_BIND;
  93. struct nlattr *tb[TCA_SKBMOD_MAX + 1];
  94. struct tcf_skbmod_params *p, *p_old;
  95. struct tcf_chain *goto_ch = NULL;
  96. struct tc_skbmod *parm;
  97. u32 lflags = 0, index;
  98. struct tcf_skbmod *d;
  99. bool exists = false;
  100. u8 *daddr = NULL;
  101. u8 *saddr = NULL;
  102. u16 eth_type = 0;
  103. int ret = 0, err;
  104. if (!nla)
  105. return -EINVAL;
  106. err = nla_parse_nested_deprecated(tb, TCA_SKBMOD_MAX, nla,
  107. skbmod_policy, NULL);
  108. if (err < 0)
  109. return err;
  110. if (!tb[TCA_SKBMOD_PARMS])
  111. return -EINVAL;
  112. if (tb[TCA_SKBMOD_DMAC]) {
  113. daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
  114. lflags |= SKBMOD_F_DMAC;
  115. }
  116. if (tb[TCA_SKBMOD_SMAC]) {
  117. saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
  118. lflags |= SKBMOD_F_SMAC;
  119. }
  120. if (tb[TCA_SKBMOD_ETYPE]) {
  121. eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
  122. lflags |= SKBMOD_F_ETYPE;
  123. }
  124. parm = nla_data(tb[TCA_SKBMOD_PARMS]);
  125. index = parm->index;
  126. if (parm->flags & SKBMOD_F_SWAPMAC)
  127. lflags = SKBMOD_F_SWAPMAC;
  128. if (parm->flags & SKBMOD_F_ECN)
  129. lflags = SKBMOD_F_ECN;
  130. err = tcf_idr_check_alloc(tn, &index, a, bind);
  131. if (err < 0)
  132. return err;
  133. exists = err;
  134. if (exists && bind)
  135. return ACT_P_BOUND;
  136. if (!lflags) {
  137. if (exists)
  138. tcf_idr_release(*a, bind);
  139. else
  140. tcf_idr_cleanup(tn, index);
  141. return -EINVAL;
  142. }
  143. if (!exists) {
  144. ret = tcf_idr_create(tn, index, est, a,
  145. &act_skbmod_ops, bind, true, flags);
  146. if (ret) {
  147. tcf_idr_cleanup(tn, index);
  148. return ret;
  149. }
  150. ret = ACT_P_CREATED;
  151. } else if (!ovr) {
  152. tcf_idr_release(*a, bind);
  153. return -EEXIST;
  154. }
  155. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  156. if (err < 0)
  157. goto release_idr;
  158. d = to_skbmod(*a);
  159. p = kzalloc_obj(struct tcf_skbmod_params);
  160. if (unlikely(!p)) {
  161. err = -ENOMEM;
  162. goto put_chain;
  163. }
  164. p->flags = lflags;
  165. p->action = parm->action;
  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. rcu_read_lock();
  213. p = rcu_dereference(d->skbmod_p);
  214. opt.action = p->action;
  215. opt.flags = p->flags;
  216. if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
  217. goto nla_put_failure;
  218. if ((p->flags & SKBMOD_F_DMAC) &&
  219. nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
  220. goto nla_put_failure;
  221. if ((p->flags & SKBMOD_F_SMAC) &&
  222. nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
  223. goto nla_put_failure;
  224. if ((p->flags & SKBMOD_F_ETYPE) &&
  225. nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
  226. goto nla_put_failure;
  227. tcf_tm_dump(&t, &d->tcf_tm);
  228. if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
  229. goto nla_put_failure;
  230. rcu_read_unlock();
  231. return skb->len;
  232. nla_put_failure:
  233. rcu_read_unlock();
  234. nlmsg_trim(skb, b);
  235. return -1;
  236. }
  237. static struct tc_action_ops act_skbmod_ops = {
  238. .kind = "skbmod",
  239. .id = TCA_ACT_SKBMOD,
  240. .owner = THIS_MODULE,
  241. .act = tcf_skbmod_act,
  242. .dump = tcf_skbmod_dump,
  243. .init = tcf_skbmod_init,
  244. .cleanup = tcf_skbmod_cleanup,
  245. .size = sizeof(struct tcf_skbmod),
  246. };
  247. MODULE_ALIAS_NET_ACT("skbmod");
  248. static __net_init int skbmod_init_net(struct net *net)
  249. {
  250. struct tc_action_net *tn = net_generic(net, act_skbmod_ops.net_id);
  251. return tc_action_net_init(net, tn, &act_skbmod_ops);
  252. }
  253. static void __net_exit skbmod_exit_net(struct list_head *net_list)
  254. {
  255. tc_action_net_exit(net_list, act_skbmod_ops.net_id);
  256. }
  257. static struct pernet_operations skbmod_net_ops = {
  258. .init = skbmod_init_net,
  259. .exit_batch = skbmod_exit_net,
  260. .id = &act_skbmod_ops.net_id,
  261. .size = sizeof(struct tc_action_net),
  262. };
  263. MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
  264. MODULE_DESCRIPTION("SKB data mod-ing");
  265. MODULE_LICENSE("GPL");
  266. static int __init skbmod_init_module(void)
  267. {
  268. return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
  269. }
  270. static void __exit skbmod_cleanup_module(void)
  271. {
  272. tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
  273. }
  274. module_init(skbmod_init_module);
  275. module_exit(skbmod_cleanup_module);