]>
Commit | Line | Data |
---|---|---|
9952f691 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
ca9b0e27 AD |
2 | /* |
3 | * Copyright (c) 2008, Intel Corporation. | |
4 | * | |
ca9b0e27 AD |
5 | * Author: Alexander Duyck <alexander.h.duyck@intel.com> |
6 | */ | |
7 | ||
8 | #include <linux/module.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> | |
e7e3728b QF |
15 | #include <net/ip.h> |
16 | #include <net/ipv6.h> | |
17 | #include <net/dsfield.h> | |
ec7727bb | 18 | #include <net/pkt_cls.h> |
ca9b0e27 AD |
19 | |
20 | #include <linux/tc_act/tc_skbedit.h> | |
21 | #include <net/tc_act/tc_skbedit.h> | |
22 | ||
c7d03a00 | 23 | static unsigned int skbedit_net_id; |
a85a970a | 24 | static struct tc_action_ops act_skbedit_ops; |
ddf97ccd | 25 | |
45da1dac JHS |
26 | static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a, |
27 | struct tcf_result *res) | |
ca9b0e27 | 28 | { |
a85a970a | 29 | struct tcf_skbedit *d = to_skbedit(a); |
c749cdda DC |
30 | struct tcf_skbedit_params *params; |
31 | int action; | |
ca9b0e27 | 32 | |
9c4a4e48 | 33 | tcf_lastuse_update(&d->tcf_tm); |
6f3dfb0d | 34 | bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb); |
ca9b0e27 | 35 | |
7fd4b288 | 36 | params = rcu_dereference_bh(d->params); |
c749cdda DC |
37 | action = READ_ONCE(d->tcf_action); |
38 | ||
39 | if (params->flags & SKBEDIT_F_PRIORITY) | |
40 | skb->priority = params->priority; | |
41 | if (params->flags & SKBEDIT_F_INHERITDSFIELD) { | |
e7e3728b QF |
42 | int wlen = skb_network_offset(skb); |
43 | ||
44 | switch (tc_skb_protocol(skb)) { | |
45 | case htons(ETH_P_IP): | |
46 | wlen += sizeof(struct iphdr); | |
47 | if (!pskb_may_pull(skb, wlen)) | |
48 | goto err; | |
49 | skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2; | |
50 | break; | |
51 | ||
52 | case htons(ETH_P_IPV6): | |
53 | wlen += sizeof(struct ipv6hdr); | |
54 | if (!pskb_may_pull(skb, wlen)) | |
55 | goto err; | |
56 | skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2; | |
57 | break; | |
58 | } | |
59 | } | |
c749cdda DC |
60 | if (params->flags & SKBEDIT_F_QUEUE_MAPPING && |
61 | skb->dev->real_num_tx_queues > params->queue_mapping) | |
62 | skb_set_queue_mapping(skb, params->queue_mapping); | |
63 | if (params->flags & SKBEDIT_F_MARK) { | |
64 | skb->mark &= ~params->mask; | |
65 | skb->mark |= params->mark & params->mask; | |
4fe77d82 | 66 | } |
c749cdda DC |
67 | if (params->flags & SKBEDIT_F_PTYPE) |
68 | skb->pkt_type = params->ptype; | |
c749cdda | 69 | return action; |
7fd4b288 | 70 | |
e7e3728b | 71 | err: |
6f3dfb0d | 72 | qstats_drop_inc(this_cpu_ptr(d->common.cpu_qstats)); |
7fd4b288 | 73 | return TC_ACT_SHOT; |
ca9b0e27 AD |
74 | } |
75 | ||
76 | static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { | |
77 | [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, | |
78 | [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, | |
79 | [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, | |
1c55d62e | 80 | [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, |
ff202ee1 | 81 | [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) }, |
4fe77d82 | 82 | [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) }, |
e7e3728b | 83 | [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) }, |
ca9b0e27 AD |
84 | }; |
85 | ||
c1b52739 | 86 | static int tcf_skbedit_init(struct net *net, struct nlattr *nla, |
a85a970a | 87 | struct nlattr *est, struct tc_action **a, |
789871bb | 88 | int ovr, int bind, bool rtnl_held, |
85d0966f | 89 | struct tcf_proto *tp, |
789871bb | 90 | struct netlink_ext_ack *extack) |
ca9b0e27 | 91 | { |
ddf97ccd | 92 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
6d7a8df6 | 93 | struct tcf_skbedit_params *params_new; |
ca9b0e27 | 94 | struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; |
ec7727bb | 95 | struct tcf_chain *goto_ch = NULL; |
ca9b0e27 AD |
96 | struct tc_skbedit *parm; |
97 | struct tcf_skbedit *d; | |
4fe77d82 | 98 | u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL; |
ff202ee1 | 99 | u16 *queue_mapping = NULL, *ptype = NULL; |
b2313077 WC |
100 | bool exists = false; |
101 | int ret = 0, err; | |
7be8ef2c | 102 | u32 index; |
ca9b0e27 AD |
103 | |
104 | if (nla == NULL) | |
105 | return -EINVAL; | |
106 | ||
8cb08174 JB |
107 | err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla, |
108 | skbedit_policy, NULL); | |
ca9b0e27 AD |
109 | if (err < 0) |
110 | return err; | |
111 | ||
112 | if (tb[TCA_SKBEDIT_PARMS] == NULL) | |
113 | return -EINVAL; | |
114 | ||
115 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { | |
116 | flags |= SKBEDIT_F_PRIORITY; | |
117 | priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); | |
118 | } | |
119 | ||
120 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { | |
121 | flags |= SKBEDIT_F_QUEUE_MAPPING; | |
122 | queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); | |
123 | } | |
1c55d62e | 124 | |
ff202ee1 JHS |
125 | if (tb[TCA_SKBEDIT_PTYPE] != NULL) { |
126 | ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]); | |
127 | if (!skb_pkt_type_ok(*ptype)) | |
128 | return -EINVAL; | |
129 | flags |= SKBEDIT_F_PTYPE; | |
130 | } | |
131 | ||
1c55d62e | 132 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
133 | flags |= SKBEDIT_F_MARK; | |
134 | mark = nla_data(tb[TCA_SKBEDIT_MARK]); | |
135 | } | |
136 | ||
4fe77d82 AQ |
137 | if (tb[TCA_SKBEDIT_MASK] != NULL) { |
138 | flags |= SKBEDIT_F_MASK; | |
139 | mask = nla_data(tb[TCA_SKBEDIT_MASK]); | |
140 | } | |
141 | ||
e7e3728b QF |
142 | if (tb[TCA_SKBEDIT_FLAGS] != NULL) { |
143 | u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]); | |
144 | ||
145 | if (*pure_flags & SKBEDIT_F_INHERITDSFIELD) | |
146 | flags |= SKBEDIT_F_INHERITDSFIELD; | |
147 | } | |
148 | ||
ca9b0e27 | 149 | parm = nla_data(tb[TCA_SKBEDIT_PARMS]); |
7be8ef2c DL |
150 | index = parm->index; |
151 | err = tcf_idr_check_alloc(tn, &index, a, bind); | |
0190c1d4 VB |
152 | if (err < 0) |
153 | return err; | |
154 | exists = err; | |
5e1567ae JHS |
155 | if (exists && bind) |
156 | return 0; | |
157 | ||
158 | if (!flags) { | |
af5d0184 RM |
159 | if (exists) |
160 | tcf_idr_release(*a, bind); | |
0190c1d4 | 161 | else |
7be8ef2c | 162 | tcf_idr_cleanup(tn, index); |
5e1567ae JHS |
163 | return -EINVAL; |
164 | } | |
165 | ||
166 | if (!exists) { | |
7be8ef2c | 167 | ret = tcf_idr_create(tn, index, est, a, |
6f3dfb0d | 168 | &act_skbedit_ops, bind, true); |
0190c1d4 | 169 | if (ret) { |
7be8ef2c | 170 | tcf_idr_cleanup(tn, index); |
86062033 | 171 | return ret; |
0190c1d4 | 172 | } |
ca9b0e27 | 173 | |
a85a970a | 174 | d = to_skbedit(*a); |
ca9b0e27 AD |
175 | ret = ACT_P_CREATED; |
176 | } else { | |
a85a970a | 177 | d = to_skbedit(*a); |
4e8ddd7f VB |
178 | if (!ovr) { |
179 | tcf_idr_release(*a, bind); | |
ca9b0e27 | 180 | return -EEXIST; |
4e8ddd7f | 181 | } |
ca9b0e27 | 182 | } |
ec7727bb DC |
183 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
184 | if (err < 0) | |
185 | goto release_idr; | |
ca9b0e27 | 186 | |
c749cdda DC |
187 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); |
188 | if (unlikely(!params_new)) { | |
ec7727bb DC |
189 | err = -ENOMEM; |
190 | goto put_chain; | |
c749cdda DC |
191 | } |
192 | ||
193 | params_new->flags = flags; | |
ca9b0e27 | 194 | if (flags & SKBEDIT_F_PRIORITY) |
c749cdda | 195 | params_new->priority = *priority; |
ca9b0e27 | 196 | if (flags & SKBEDIT_F_QUEUE_MAPPING) |
c749cdda | 197 | params_new->queue_mapping = *queue_mapping; |
1c55d62e | 198 | if (flags & SKBEDIT_F_MARK) |
c749cdda | 199 | params_new->mark = *mark; |
ff202ee1 | 200 | if (flags & SKBEDIT_F_PTYPE) |
c749cdda | 201 | params_new->ptype = *ptype; |
4fe77d82 | 202 | /* default behaviour is to use all the bits */ |
c749cdda | 203 | params_new->mask = 0xffffffff; |
4fe77d82 | 204 | if (flags & SKBEDIT_F_MASK) |
c749cdda | 205 | params_new->mask = *mask; |
1c55d62e | 206 | |
6d7a8df6 | 207 | spin_lock_bh(&d->tcf_lock); |
ec7727bb | 208 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
6d7a8df6 VB |
209 | rcu_swap_protected(d->params, params_new, |
210 | lockdep_is_held(&d->tcf_lock)); | |
211 | spin_unlock_bh(&d->tcf_lock); | |
212 | if (params_new) | |
213 | kfree_rcu(params_new, rcu); | |
ec7727bb DC |
214 | if (goto_ch) |
215 | tcf_chain_put_by_act(goto_ch); | |
ca9b0e27 AD |
216 | |
217 | if (ret == ACT_P_CREATED) | |
65a206c0 | 218 | tcf_idr_insert(tn, *a); |
ca9b0e27 | 219 | return ret; |
ec7727bb DC |
220 | put_chain: |
221 | if (goto_ch) | |
222 | tcf_chain_put_by_act(goto_ch); | |
223 | release_idr: | |
224 | tcf_idr_release(*a, bind); | |
225 | return err; | |
ca9b0e27 AD |
226 | } |
227 | ||
cc7ec456 ED |
228 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
229 | int bind, int ref) | |
ca9b0e27 AD |
230 | { |
231 | unsigned char *b = skb_tail_pointer(skb); | |
a85a970a | 232 | struct tcf_skbedit *d = to_skbedit(a); |
c749cdda | 233 | struct tcf_skbedit_params *params; |
1c40be12 ED |
234 | struct tc_skbedit opt = { |
235 | .index = d->tcf_index, | |
036bb443 VB |
236 | .refcnt = refcount_read(&d->tcf_refcnt) - ref, |
237 | .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, | |
1c40be12 | 238 | }; |
e7e3728b | 239 | u64 pure_flags = 0; |
c749cdda DC |
240 | struct tcf_t t; |
241 | ||
6d7a8df6 VB |
242 | spin_lock_bh(&d->tcf_lock); |
243 | params = rcu_dereference_protected(d->params, | |
244 | lockdep_is_held(&d->tcf_lock)); | |
245 | opt.action = d->tcf_action; | |
ca9b0e27 | 246 | |
1b34ec43 DM |
247 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
248 | goto nla_put_failure; | |
c749cdda DC |
249 | if ((params->flags & SKBEDIT_F_PRIORITY) && |
250 | nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority)) | |
1b34ec43 | 251 | goto nla_put_failure; |
c749cdda DC |
252 | if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) && |
253 | nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping)) | |
1b34ec43 | 254 | goto nla_put_failure; |
c749cdda DC |
255 | if ((params->flags & SKBEDIT_F_MARK) && |
256 | nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark)) | |
1b34ec43 | 257 | goto nla_put_failure; |
c749cdda DC |
258 | if ((params->flags & SKBEDIT_F_PTYPE) && |
259 | nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype)) | |
ff202ee1 | 260 | goto nla_put_failure; |
c749cdda DC |
261 | if ((params->flags & SKBEDIT_F_MASK) && |
262 | nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask)) | |
4fe77d82 | 263 | goto nla_put_failure; |
c749cdda | 264 | if (params->flags & SKBEDIT_F_INHERITDSFIELD) |
e7e3728b QF |
265 | pure_flags |= SKBEDIT_F_INHERITDSFIELD; |
266 | if (pure_flags != 0 && | |
267 | nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags)) | |
268 | goto nla_put_failure; | |
48d8ee16 JHS |
269 | |
270 | tcf_tm_dump(&t, &d->tcf_tm); | |
9854518e | 271 | if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD)) |
1b34ec43 | 272 | goto nla_put_failure; |
6d7a8df6 VB |
273 | spin_unlock_bh(&d->tcf_lock); |
274 | ||
ca9b0e27 AD |
275 | return skb->len; |
276 | ||
277 | nla_put_failure: | |
6d7a8df6 | 278 | spin_unlock_bh(&d->tcf_lock); |
ca9b0e27 AD |
279 | nlmsg_trim(skb, b); |
280 | return -1; | |
281 | } | |
282 | ||
c749cdda DC |
283 | static void tcf_skbedit_cleanup(struct tc_action *a) |
284 | { | |
285 | struct tcf_skbedit *d = to_skbedit(a); | |
286 | struct tcf_skbedit_params *params; | |
287 | ||
288 | params = rcu_dereference_protected(d->params, 1); | |
289 | if (params) | |
290 | kfree_rcu(params, rcu); | |
291 | } | |
292 | ||
ddf97ccd WC |
293 | static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, |
294 | struct netlink_callback *cb, int type, | |
41780105 AA |
295 | const struct tc_action_ops *ops, |
296 | struct netlink_ext_ack *extack) | |
ddf97ccd WC |
297 | { |
298 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
299 | ||
b3620145 | 300 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
ddf97ccd WC |
301 | } |
302 | ||
f061b48c | 303 | static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccd WC |
304 | { |
305 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
306 | ||
65a206c0 | 307 | return tcf_idr_search(tn, a, index); |
ddf97ccd WC |
308 | } |
309 | ||
e1fea322 RM |
310 | static size_t tcf_skbedit_get_fill_size(const struct tc_action *act) |
311 | { | |
312 | return nla_total_size(sizeof(struct tc_skbedit)) | |
313 | + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_PRIORITY */ | |
314 | + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING */ | |
315 | + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MARK */ | |
316 | + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_PTYPE */ | |
317 | + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MASK */ | |
318 | + nla_total_size_64bit(sizeof(u64)); /* TCA_SKBEDIT_FLAGS */ | |
319 | } | |
320 | ||
ca9b0e27 AD |
321 | static struct tc_action_ops act_skbedit_ops = { |
322 | .kind = "skbedit", | |
eddd2cf1 | 323 | .id = TCA_ID_SKBEDIT, |
ca9b0e27 | 324 | .owner = THIS_MODULE, |
45da1dac | 325 | .act = tcf_skbedit_act, |
ca9b0e27 | 326 | .dump = tcf_skbedit_dump, |
ca9b0e27 | 327 | .init = tcf_skbedit_init, |
c749cdda | 328 | .cleanup = tcf_skbedit_cleanup, |
ddf97ccd | 329 | .walk = tcf_skbedit_walker, |
e1fea322 | 330 | .get_fill_size = tcf_skbedit_get_fill_size, |
ddf97ccd | 331 | .lookup = tcf_skbedit_search, |
a85a970a | 332 | .size = sizeof(struct tcf_skbedit), |
ddf97ccd WC |
333 | }; |
334 | ||
335 | static __net_init int skbedit_init_net(struct net *net) | |
336 | { | |
337 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
338 | ||
c7e460ce | 339 | return tc_action_net_init(tn, &act_skbedit_ops); |
ddf97ccd WC |
340 | } |
341 | ||
039af9c6 | 342 | static void __net_exit skbedit_exit_net(struct list_head *net_list) |
ddf97ccd | 343 | { |
039af9c6 | 344 | tc_action_net_exit(net_list, skbedit_net_id); |
ddf97ccd WC |
345 | } |
346 | ||
347 | static struct pernet_operations skbedit_net_ops = { | |
348 | .init = skbedit_init_net, | |
039af9c6 | 349 | .exit_batch = skbedit_exit_net, |
ddf97ccd WC |
350 | .id = &skbedit_net_id, |
351 | .size = sizeof(struct tc_action_net), | |
ca9b0e27 AD |
352 | }; |
353 | ||
354 | MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); | |
355 | MODULE_DESCRIPTION("SKB Editing"); | |
356 | MODULE_LICENSE("GPL"); | |
357 | ||
358 | static int __init skbedit_init_module(void) | |
359 | { | |
ddf97ccd | 360 | return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); |
ca9b0e27 AD |
361 | } |
362 | ||
363 | static void __exit skbedit_cleanup_module(void) | |
364 | { | |
ddf97ccd | 365 | tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); |
ca9b0e27 AD |
366 | } |
367 | ||
368 | module_init(skbedit_init_module); | |
369 | module_exit(skbedit_cleanup_module); |