]>
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 | ||
d7bf2ebe | 44 | switch (skb_protocol(skb, true)) { |
e7e3728b QF |
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 | ||
837cb17d | 76 | static void tcf_skbedit_stats_update(struct tc_action *a, u64 bytes, |
4b61d3e8 PL |
77 | u64 packets, u64 drops, |
78 | u64 lastuse, bool hw) | |
837cb17d PM |
79 | { |
80 | struct tcf_skbedit *d = to_skbedit(a); | |
81 | struct tcf_t *tm = &d->tcf_tm; | |
82 | ||
4b61d3e8 | 83 | tcf_action_update_stats(a, bytes, packets, drops, hw); |
837cb17d PM |
84 | tm->lastuse = max_t(u64, tm->lastuse, lastuse); |
85 | } | |
86 | ||
ca9b0e27 AD |
87 | static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = { |
88 | [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) }, | |
89 | [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) }, | |
90 | [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) }, | |
1c55d62e | 91 | [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) }, |
ff202ee1 | 92 | [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) }, |
4fe77d82 | 93 | [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) }, |
e7e3728b | 94 | [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) }, |
ca9b0e27 AD |
95 | }; |
96 | ||
c1b52739 | 97 | static int tcf_skbedit_init(struct net *net, struct nlattr *nla, |
a85a970a | 98 | struct nlattr *est, struct tc_action **a, |
789871bb | 99 | int ovr, int bind, bool rtnl_held, |
abbb0d33 | 100 | struct tcf_proto *tp, u32 act_flags, |
789871bb | 101 | struct netlink_ext_ack *extack) |
ca9b0e27 | 102 | { |
ddf97ccd | 103 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); |
6d7a8df6 | 104 | struct tcf_skbedit_params *params_new; |
ca9b0e27 | 105 | struct nlattr *tb[TCA_SKBEDIT_MAX + 1]; |
ec7727bb | 106 | struct tcf_chain *goto_ch = NULL; |
ca9b0e27 AD |
107 | struct tc_skbedit *parm; |
108 | struct tcf_skbedit *d; | |
4fe77d82 | 109 | u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL; |
ff202ee1 | 110 | u16 *queue_mapping = NULL, *ptype = NULL; |
b2313077 WC |
111 | bool exists = false; |
112 | int ret = 0, err; | |
7be8ef2c | 113 | u32 index; |
ca9b0e27 AD |
114 | |
115 | if (nla == NULL) | |
116 | return -EINVAL; | |
117 | ||
8cb08174 JB |
118 | err = nla_parse_nested_deprecated(tb, TCA_SKBEDIT_MAX, nla, |
119 | skbedit_policy, NULL); | |
ca9b0e27 AD |
120 | if (err < 0) |
121 | return err; | |
122 | ||
123 | if (tb[TCA_SKBEDIT_PARMS] == NULL) | |
124 | return -EINVAL; | |
125 | ||
126 | if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { | |
127 | flags |= SKBEDIT_F_PRIORITY; | |
128 | priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]); | |
129 | } | |
130 | ||
131 | if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { | |
132 | flags |= SKBEDIT_F_QUEUE_MAPPING; | |
133 | queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]); | |
134 | } | |
1c55d62e | 135 | |
ff202ee1 JHS |
136 | if (tb[TCA_SKBEDIT_PTYPE] != NULL) { |
137 | ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]); | |
138 | if (!skb_pkt_type_ok(*ptype)) | |
139 | return -EINVAL; | |
140 | flags |= SKBEDIT_F_PTYPE; | |
141 | } | |
142 | ||
1c55d62e | 143 | if (tb[TCA_SKBEDIT_MARK] != NULL) { |
144 | flags |= SKBEDIT_F_MARK; | |
145 | mark = nla_data(tb[TCA_SKBEDIT_MARK]); | |
146 | } | |
147 | ||
4fe77d82 AQ |
148 | if (tb[TCA_SKBEDIT_MASK] != NULL) { |
149 | flags |= SKBEDIT_F_MASK; | |
150 | mask = nla_data(tb[TCA_SKBEDIT_MASK]); | |
151 | } | |
152 | ||
e7e3728b QF |
153 | if (tb[TCA_SKBEDIT_FLAGS] != NULL) { |
154 | u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]); | |
155 | ||
156 | if (*pure_flags & SKBEDIT_F_INHERITDSFIELD) | |
157 | flags |= SKBEDIT_F_INHERITDSFIELD; | |
158 | } | |
159 | ||
ca9b0e27 | 160 | parm = nla_data(tb[TCA_SKBEDIT_PARMS]); |
7be8ef2c DL |
161 | index = parm->index; |
162 | err = tcf_idr_check_alloc(tn, &index, a, bind); | |
0190c1d4 VB |
163 | if (err < 0) |
164 | return err; | |
165 | exists = err; | |
5e1567ae JHS |
166 | if (exists && bind) |
167 | return 0; | |
168 | ||
169 | if (!flags) { | |
af5d0184 RM |
170 | if (exists) |
171 | tcf_idr_release(*a, bind); | |
0190c1d4 | 172 | else |
7be8ef2c | 173 | tcf_idr_cleanup(tn, index); |
5e1567ae JHS |
174 | return -EINVAL; |
175 | } | |
176 | ||
177 | if (!exists) { | |
7be8ef2c | 178 | ret = tcf_idr_create(tn, index, est, a, |
e3822678 | 179 | &act_skbedit_ops, bind, true, 0); |
0190c1d4 | 180 | if (ret) { |
7be8ef2c | 181 | tcf_idr_cleanup(tn, index); |
86062033 | 182 | return ret; |
0190c1d4 | 183 | } |
ca9b0e27 | 184 | |
a85a970a | 185 | d = to_skbedit(*a); |
ca9b0e27 AD |
186 | ret = ACT_P_CREATED; |
187 | } else { | |
a85a970a | 188 | d = to_skbedit(*a); |
4e8ddd7f VB |
189 | if (!ovr) { |
190 | tcf_idr_release(*a, bind); | |
ca9b0e27 | 191 | return -EEXIST; |
4e8ddd7f | 192 | } |
ca9b0e27 | 193 | } |
ec7727bb DC |
194 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
195 | if (err < 0) | |
196 | goto release_idr; | |
ca9b0e27 | 197 | |
c749cdda DC |
198 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); |
199 | if (unlikely(!params_new)) { | |
ec7727bb DC |
200 | err = -ENOMEM; |
201 | goto put_chain; | |
c749cdda DC |
202 | } |
203 | ||
204 | params_new->flags = flags; | |
ca9b0e27 | 205 | if (flags & SKBEDIT_F_PRIORITY) |
c749cdda | 206 | params_new->priority = *priority; |
ca9b0e27 | 207 | if (flags & SKBEDIT_F_QUEUE_MAPPING) |
c749cdda | 208 | params_new->queue_mapping = *queue_mapping; |
1c55d62e | 209 | if (flags & SKBEDIT_F_MARK) |
c749cdda | 210 | params_new->mark = *mark; |
ff202ee1 | 211 | if (flags & SKBEDIT_F_PTYPE) |
c749cdda | 212 | params_new->ptype = *ptype; |
4fe77d82 | 213 | /* default behaviour is to use all the bits */ |
c749cdda | 214 | params_new->mask = 0xffffffff; |
4fe77d82 | 215 | if (flags & SKBEDIT_F_MASK) |
c749cdda | 216 | params_new->mask = *mask; |
1c55d62e | 217 | |
6d7a8df6 | 218 | spin_lock_bh(&d->tcf_lock); |
ec7727bb | 219 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
445d3749 PM |
220 | params_new = rcu_replace_pointer(d->params, params_new, |
221 | lockdep_is_held(&d->tcf_lock)); | |
6d7a8df6 VB |
222 | spin_unlock_bh(&d->tcf_lock); |
223 | if (params_new) | |
224 | kfree_rcu(params_new, rcu); | |
ec7727bb DC |
225 | if (goto_ch) |
226 | tcf_chain_put_by_act(goto_ch); | |
ca9b0e27 AD |
227 | |
228 | if (ret == ACT_P_CREATED) | |
65a206c0 | 229 | tcf_idr_insert(tn, *a); |
ca9b0e27 | 230 | return ret; |
ec7727bb DC |
231 | put_chain: |
232 | if (goto_ch) | |
233 | tcf_chain_put_by_act(goto_ch); | |
234 | release_idr: | |
235 | tcf_idr_release(*a, bind); | |
236 | return err; | |
ca9b0e27 AD |
237 | } |
238 | ||
cc7ec456 ED |
239 | static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a, |
240 | int bind, int ref) | |
ca9b0e27 AD |
241 | { |
242 | unsigned char *b = skb_tail_pointer(skb); | |
a85a970a | 243 | struct tcf_skbedit *d = to_skbedit(a); |
c749cdda | 244 | struct tcf_skbedit_params *params; |
1c40be12 ED |
245 | struct tc_skbedit opt = { |
246 | .index = d->tcf_index, | |
036bb443 VB |
247 | .refcnt = refcount_read(&d->tcf_refcnt) - ref, |
248 | .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, | |
1c40be12 | 249 | }; |
e7e3728b | 250 | u64 pure_flags = 0; |
c749cdda DC |
251 | struct tcf_t t; |
252 | ||
6d7a8df6 VB |
253 | spin_lock_bh(&d->tcf_lock); |
254 | params = rcu_dereference_protected(d->params, | |
255 | lockdep_is_held(&d->tcf_lock)); | |
256 | opt.action = d->tcf_action; | |
ca9b0e27 | 257 | |
1b34ec43 DM |
258 | if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt)) |
259 | goto nla_put_failure; | |
c749cdda DC |
260 | if ((params->flags & SKBEDIT_F_PRIORITY) && |
261 | nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, params->priority)) | |
1b34ec43 | 262 | goto nla_put_failure; |
c749cdda DC |
263 | if ((params->flags & SKBEDIT_F_QUEUE_MAPPING) && |
264 | nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, params->queue_mapping)) | |
1b34ec43 | 265 | goto nla_put_failure; |
c749cdda DC |
266 | if ((params->flags & SKBEDIT_F_MARK) && |
267 | nla_put_u32(skb, TCA_SKBEDIT_MARK, params->mark)) | |
1b34ec43 | 268 | goto nla_put_failure; |
c749cdda DC |
269 | if ((params->flags & SKBEDIT_F_PTYPE) && |
270 | nla_put_u16(skb, TCA_SKBEDIT_PTYPE, params->ptype)) | |
ff202ee1 | 271 | goto nla_put_failure; |
c749cdda DC |
272 | if ((params->flags & SKBEDIT_F_MASK) && |
273 | nla_put_u32(skb, TCA_SKBEDIT_MASK, params->mask)) | |
4fe77d82 | 274 | goto nla_put_failure; |
c749cdda | 275 | if (params->flags & SKBEDIT_F_INHERITDSFIELD) |
e7e3728b QF |
276 | pure_flags |= SKBEDIT_F_INHERITDSFIELD; |
277 | if (pure_flags != 0 && | |
278 | nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags)) | |
279 | goto nla_put_failure; | |
48d8ee16 JHS |
280 | |
281 | tcf_tm_dump(&t, &d->tcf_tm); | |
9854518e | 282 | if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD)) |
1b34ec43 | 283 | goto nla_put_failure; |
6d7a8df6 VB |
284 | spin_unlock_bh(&d->tcf_lock); |
285 | ||
ca9b0e27 AD |
286 | return skb->len; |
287 | ||
288 | nla_put_failure: | |
6d7a8df6 | 289 | spin_unlock_bh(&d->tcf_lock); |
ca9b0e27 AD |
290 | nlmsg_trim(skb, b); |
291 | return -1; | |
292 | } | |
293 | ||
c749cdda DC |
294 | static void tcf_skbedit_cleanup(struct tc_action *a) |
295 | { | |
296 | struct tcf_skbedit *d = to_skbedit(a); | |
297 | struct tcf_skbedit_params *params; | |
298 | ||
299 | params = rcu_dereference_protected(d->params, 1); | |
300 | if (params) | |
301 | kfree_rcu(params, rcu); | |
302 | } | |
303 | ||
ddf97ccd WC |
304 | static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb, |
305 | struct netlink_callback *cb, int type, | |
41780105 AA |
306 | const struct tc_action_ops *ops, |
307 | struct netlink_ext_ack *extack) | |
ddf97ccd WC |
308 | { |
309 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
310 | ||
b3620145 | 311 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
ddf97ccd WC |
312 | } |
313 | ||
f061b48c | 314 | static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccd WC |
315 | { |
316 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
317 | ||
65a206c0 | 318 | return tcf_idr_search(tn, a, index); |
ddf97ccd WC |
319 | } |
320 | ||
e1fea322 RM |
321 | static size_t tcf_skbedit_get_fill_size(const struct tc_action *act) |
322 | { | |
323 | return nla_total_size(sizeof(struct tc_skbedit)) | |
324 | + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_PRIORITY */ | |
325 | + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_QUEUE_MAPPING */ | |
326 | + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MARK */ | |
327 | + nla_total_size(sizeof(u16)) /* TCA_SKBEDIT_PTYPE */ | |
328 | + nla_total_size(sizeof(u32)) /* TCA_SKBEDIT_MASK */ | |
329 | + nla_total_size_64bit(sizeof(u64)); /* TCA_SKBEDIT_FLAGS */ | |
330 | } | |
331 | ||
ca9b0e27 AD |
332 | static struct tc_action_ops act_skbedit_ops = { |
333 | .kind = "skbedit", | |
eddd2cf1 | 334 | .id = TCA_ID_SKBEDIT, |
ca9b0e27 | 335 | .owner = THIS_MODULE, |
45da1dac | 336 | .act = tcf_skbedit_act, |
837cb17d | 337 | .stats_update = tcf_skbedit_stats_update, |
ca9b0e27 | 338 | .dump = tcf_skbedit_dump, |
ca9b0e27 | 339 | .init = tcf_skbedit_init, |
c749cdda | 340 | .cleanup = tcf_skbedit_cleanup, |
ddf97ccd | 341 | .walk = tcf_skbedit_walker, |
e1fea322 | 342 | .get_fill_size = tcf_skbedit_get_fill_size, |
ddf97ccd | 343 | .lookup = tcf_skbedit_search, |
a85a970a | 344 | .size = sizeof(struct tcf_skbedit), |
ddf97ccd WC |
345 | }; |
346 | ||
347 | static __net_init int skbedit_init_net(struct net *net) | |
348 | { | |
349 | struct tc_action_net *tn = net_generic(net, skbedit_net_id); | |
350 | ||
981471bd | 351 | return tc_action_net_init(net, tn, &act_skbedit_ops); |
ddf97ccd WC |
352 | } |
353 | ||
039af9c6 | 354 | static void __net_exit skbedit_exit_net(struct list_head *net_list) |
ddf97ccd | 355 | { |
039af9c6 | 356 | tc_action_net_exit(net_list, skbedit_net_id); |
ddf97ccd WC |
357 | } |
358 | ||
359 | static struct pernet_operations skbedit_net_ops = { | |
360 | .init = skbedit_init_net, | |
039af9c6 | 361 | .exit_batch = skbedit_exit_net, |
ddf97ccd WC |
362 | .id = &skbedit_net_id, |
363 | .size = sizeof(struct tc_action_net), | |
ca9b0e27 AD |
364 | }; |
365 | ||
366 | MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); | |
367 | MODULE_DESCRIPTION("SKB Editing"); | |
368 | MODULE_LICENSE("GPL"); | |
369 | ||
370 | static int __init skbedit_init_module(void) | |
371 | { | |
ddf97ccd | 372 | return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops); |
ca9b0e27 AD |
373 | } |
374 | ||
375 | static void __exit skbedit_cleanup_module(void) | |
376 | { | |
ddf97ccd | 377 | tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops); |
ca9b0e27 AD |
378 | } |
379 | ||
380 | module_init(skbedit_init_module); | |
381 | module_exit(skbedit_cleanup_module); |