act_skbmod.c 7.9 KB

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