blob: dffa990a9629f0ca3d04c4667534908968f7a8d2 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -04002/*
3 * net/sched/act_skbmod.c skb data modifier
4 *
5 * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com>
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -04006*/
7
8#include <linux/module.h>
Peilin Ye727d6a82021-07-19 16:41:24 -07009#include <linux/if_arp.h>
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040010#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/skbuff.h>
13#include <linux/rtnetlink.h>
Peilin Ye56af5e72021-07-27 18:33:15 -070014#include <net/inet_ecn.h>
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040015#include <net/netlink.h>
16#include <net/pkt_sched.h>
Davide Caratti7c3d8252019-03-20 15:00:12 +010017#include <net/pkt_cls.h>
Pedro Tammela871cf382022-12-06 10:55:12 -030018#include <net/tc_wrapper.h>
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040019
20#include <linux/tc_act/tc_skbmod.h>
21#include <net/tc_act/tc_skbmod.h>
22
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040023static struct tc_action_ops act_skbmod_ops;
24
Pedro Tammela871cf382022-12-06 10:55:12 -030025TC_INDIRECT_SCOPE int tcf_skbmod_act(struct sk_buff *skb,
26 const struct tc_action *a,
27 struct tcf_result *res)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040028{
29 struct tcf_skbmod *d = to_skbmod(a);
Peilin Ye56af5e72021-07-27 18:33:15 -070030 int action, max_edit_len, err;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040031 struct tcf_skbmod_params *p;
32 u64 flags;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040033
34 tcf_lastuse_update(&d->tcf_tm);
Ahmed S. Darwish50dc9a82021-10-16 10:49:09 +020035 bstats_update(this_cpu_ptr(d->common.cpu_bstats), skb);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040036
Peilin Ye727d6a82021-07-19 16:41:24 -070037 action = READ_ONCE(d->tcf_action);
38 if (unlikely(action == TC_ACT_SHOT))
39 goto drop;
40
Peilin Ye56af5e72021-07-27 18:33:15 -070041 max_edit_len = skb_mac_header_len(skb);
42 p = rcu_dereference_bh(d->skbmod_p);
43 flags = p->flags;
Peilin Ye727d6a82021-07-19 16:41:24 -070044
Peilin Ye56af5e72021-07-27 18:33:15 -070045 /* tcf_skbmod_init() guarantees "flags" to be one of the following:
46 * 1. a combination of SKBMOD_F_{DMAC,SMAC,ETYPE}
47 * 2. SKBMOD_F_SWAPMAC
48 * 3. SKBMOD_F_ECN
49 * SKBMOD_F_ECN only works with IP packets; all other flags only work with Ethernet
50 * packets.
51 */
52 if (flags == SKBMOD_F_ECN) {
53 switch (skb_protocol(skb, true)) {
54 case cpu_to_be16(ETH_P_IP):
55 case cpu_to_be16(ETH_P_IPV6):
56 max_edit_len += skb_network_header_len(skb);
57 break;
58 default:
59 goto out;
60 }
61 } else if (!skb->dev || skb->dev->type != ARPHRD_ETHER) {
62 goto out;
63 }
64
65 err = skb_ensure_writable(skb, max_edit_len);
Paolo Abeni7fd4b282018-07-30 14:30:43 +020066 if (unlikely(err)) /* best policy is to drop on the floor */
67 goto drop;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040068
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040069 if (flags & SKBMOD_F_DMAC)
70 ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
71 if (flags & SKBMOD_F_SMAC)
72 ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
73 if (flags & SKBMOD_F_ETYPE)
74 eth_hdr(skb)->h_proto = p->eth_type;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040075
76 if (flags & SKBMOD_F_SWAPMAC) {
77 u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
78 /*XXX: I am sure we can come up with more efficient swapping*/
79 ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
80 ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
81 ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
82 }
83
Peilin Ye56af5e72021-07-27 18:33:15 -070084 if (flags & SKBMOD_F_ECN)
85 INET_ECN_set_ce(skb);
86
87out:
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040088 return action;
Paolo Abeni7fd4b282018-07-30 14:30:43 +020089
90drop:
91 qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
92 return TC_ACT_SHOT;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040093}
94
95static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
96 [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) },
97 [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN },
98 [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN },
99 [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 },
100};
101
102static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
103 struct nlattr *est, struct tc_action **a,
Vlad Buslovabbb0d32019-10-30 16:09:05 +0200104 struct tcf_proto *tp, u32 flags,
Vlad Buslov789871b2018-07-05 17:24:25 +0300105 struct netlink_ext_ack *extack)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400106{
Zhengchao Shaoacd0a7a2022-09-08 12:14:33 +0800107 struct tc_action_net *tn = net_generic(net, act_skbmod_ops.net_id);
Cong Wang695176b2021-07-29 16:12:14 -0700108 bool ovr = flags & TCA_ACT_FLAGS_REPLACE;
109 bool bind = flags & TCA_ACT_FLAGS_BIND;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400110 struct nlattr *tb[TCA_SKBMOD_MAX + 1];
111 struct tcf_skbmod_params *p, *p_old;
Davide Caratti7c3d8252019-03-20 15:00:12 +0100112 struct tcf_chain *goto_ch = NULL;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400113 struct tc_skbmod *parm;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000114 u32 lflags = 0, index;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400115 struct tcf_skbmod *d;
116 bool exists = false;
117 u8 *daddr = NULL;
118 u8 *saddr = NULL;
119 u16 eth_type = 0;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400120 int ret = 0, err;
121
122 if (!nla)
123 return -EINVAL;
124
Johannes Berg8cb08172019-04-26 14:07:28 +0200125 err = nla_parse_nested_deprecated(tb, TCA_SKBMOD_MAX, nla,
126 skbmod_policy, NULL);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400127 if (err < 0)
128 return err;
129
130 if (!tb[TCA_SKBMOD_PARMS])
131 return -EINVAL;
132
133 if (tb[TCA_SKBMOD_DMAC]) {
134 daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
135 lflags |= SKBMOD_F_DMAC;
136 }
137
138 if (tb[TCA_SKBMOD_SMAC]) {
139 saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
140 lflags |= SKBMOD_F_SMAC;
141 }
142
143 if (tb[TCA_SKBMOD_ETYPE]) {
144 eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
145 lflags |= SKBMOD_F_ETYPE;
146 }
147
148 parm = nla_data(tb[TCA_SKBMOD_PARMS]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000149 index = parm->index;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400150 if (parm->flags & SKBMOD_F_SWAPMAC)
151 lflags = SKBMOD_F_SWAPMAC;
Peilin Ye56af5e72021-07-27 18:33:15 -0700152 if (parm->flags & SKBMOD_F_ECN)
153 lflags = SKBMOD_F_ECN;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400154
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000155 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300156 if (err < 0)
157 return err;
158 exists = err;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400159 if (exists && bind)
160 return 0;
161
Roman Mashaka52956d2018-05-11 14:35:33 -0400162 if (!lflags) {
163 if (exists)
164 tcf_idr_release(*a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300165 else
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000166 tcf_idr_cleanup(tn, index);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400167 return -EINVAL;
Roman Mashaka52956d2018-05-11 14:35:33 -0400168 }
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400169
170 if (!exists) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000171 ret = tcf_idr_create(tn, index, est, a,
Baowen Zheng40bd0942021-12-17 19:16:17 +0100172 &act_skbmod_ops, bind, true, flags);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300173 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000174 tcf_idr_cleanup(tn, index);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400175 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300176 }
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400177
178 ret = ACT_P_CREATED;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300179 } else if (!ovr) {
Chris Mi65a206c2017-08-30 02:31:59 -0400180 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300181 return -EEXIST;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400182 }
Davide Caratti7c3d8252019-03-20 15:00:12 +0100183 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
184 if (err < 0)
185 goto release_idr;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400186
187 d = to_skbmod(*a);
188
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400189 p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
190 if (unlikely(!p)) {
Davide Caratti7c3d8252019-03-20 15:00:12 +0100191 err = -ENOMEM;
192 goto put_chain;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400193 }
194
195 p->flags = lflags;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400196
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400197 if (ovr)
198 spin_lock_bh(&d->tcf_lock);
Vlad Buslovc8814552018-08-10 20:51:49 +0300199 /* Protected by tcf_lock if overwriting existing action. */
Davide Caratti7c3d8252019-03-20 15:00:12 +0100200 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Vlad Buslovc8814552018-08-10 20:51:49 +0300201 p_old = rcu_dereference_protected(d->skbmod_p, 1);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400202
203 if (lflags & SKBMOD_F_DMAC)
204 ether_addr_copy(p->eth_dst, daddr);
205 if (lflags & SKBMOD_F_SMAC)
206 ether_addr_copy(p->eth_src, saddr);
207 if (lflags & SKBMOD_F_ETYPE)
208 p->eth_type = htons(eth_type);
209
210 rcu_assign_pointer(d->skbmod_p, p);
211 if (ovr)
212 spin_unlock_bh(&d->tcf_lock);
213
214 if (p_old)
215 kfree_rcu(p_old, rcu);
Davide Caratti7c3d8252019-03-20 15:00:12 +0100216 if (goto_ch)
217 tcf_chain_put_by_act(goto_ch);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400218
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400219 return ret;
Davide Caratti7c3d8252019-03-20 15:00:12 +0100220put_chain:
221 if (goto_ch)
222 tcf_chain_put_by_act(goto_ch);
223release_idr:
224 tcf_idr_release(*a, bind);
225 return err;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400226}
227
Cong Wang9a63b252017-12-05 12:53:07 -0800228static void tcf_skbmod_cleanup(struct tc_action *a)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400229{
230 struct tcf_skbmod *d = to_skbmod(a);
231 struct tcf_skbmod_params *p;
232
233 p = rcu_dereference_protected(d->skbmod_p, 1);
Davide Caratti2d433612018-03-16 00:00:57 +0100234 if (p)
235 kfree_rcu(p, rcu);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400236}
237
238static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
239 int bind, int ref)
240{
241 struct tcf_skbmod *d = to_skbmod(a);
242 unsigned char *b = skb_tail_pointer(skb);
Vlad Buslovc8814552018-08-10 20:51:49 +0300243 struct tcf_skbmod_params *p;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400244 struct tc_skbmod opt = {
245 .index = d->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300246 .refcnt = refcount_read(&d->tcf_refcnt) - ref,
247 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400248 };
249 struct tcf_t t;
250
Vlad Buslovc8814552018-08-10 20:51:49 +0300251 spin_lock_bh(&d->tcf_lock);
252 opt.action = d->tcf_action;
253 p = rcu_dereference_protected(d->skbmod_p,
254 lockdep_is_held(&d->tcf_lock));
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400255 opt.flags = p->flags;
256 if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
257 goto nla_put_failure;
258 if ((p->flags & SKBMOD_F_DMAC) &&
259 nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
260 goto nla_put_failure;
261 if ((p->flags & SKBMOD_F_SMAC) &&
262 nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
263 goto nla_put_failure;
264 if ((p->flags & SKBMOD_F_ETYPE) &&
265 nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
266 goto nla_put_failure;
267
268 tcf_tm_dump(&t, &d->tcf_tm);
269 if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
270 goto nla_put_failure;
271
Vlad Buslovc8814552018-08-10 20:51:49 +0300272 spin_unlock_bh(&d->tcf_lock);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400273 return skb->len;
274nla_put_failure:
Vlad Buslovc8814552018-08-10 20:51:49 +0300275 spin_unlock_bh(&d->tcf_lock);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400276 nlmsg_trim(skb, b);
277 return -1;
278}
279
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400280static struct tc_action_ops act_skbmod_ops = {
281 .kind = "skbmod",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200282 .id = TCA_ACT_SKBMOD,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400283 .owner = THIS_MODULE,
Jamal Hadi Salim353d2c22018-08-12 09:34:59 -0400284 .act = tcf_skbmod_act,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400285 .dump = tcf_skbmod_dump,
286 .init = tcf_skbmod_init,
287 .cleanup = tcf_skbmod_cleanup,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400288 .size = sizeof(struct tcf_skbmod),
289};
290
291static __net_init int skbmod_init_net(struct net *net)
292{
Zhengchao Shaoacd0a7a2022-09-08 12:14:33 +0800293 struct tc_action_net *tn = net_generic(net, act_skbmod_ops.net_id);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400294
Cong Wang981471bd2019-08-25 10:01:32 -0700295 return tc_action_net_init(net, tn, &act_skbmod_ops);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400296}
297
Cong Wang039af9c2017-12-11 15:35:03 -0800298static void __net_exit skbmod_exit_net(struct list_head *net_list)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400299{
Zhengchao Shaoacd0a7a2022-09-08 12:14:33 +0800300 tc_action_net_exit(net_list, act_skbmod_ops.net_id);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400301}
302
303static struct pernet_operations skbmod_net_ops = {
304 .init = skbmod_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800305 .exit_batch = skbmod_exit_net,
Zhengchao Shaoacd0a7a2022-09-08 12:14:33 +0800306 .id = &act_skbmod_ops.net_id,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400307 .size = sizeof(struct tc_action_net),
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400308};
309
310MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
311MODULE_DESCRIPTION("SKB data mod-ing");
312MODULE_LICENSE("GPL");
313
314static int __init skbmod_init_module(void)
315{
316 return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
317}
318
319static void __exit skbmod_cleanup_module(void)
320{
321 tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
322}
323
324module_init(skbmod_init_module);
325module_exit(skbmod_cleanup_module);