Merge tag 'mediatek-drm-fixes-5.3' of https://github.com/ckhu-mediatek/linux.git...
[linux-2.6-block.git] / net / sched / cls_flower.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
77b9900e
JP
2/*
3 * net/sched/cls_flower.c Flower classifier
4 *
5 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
77b9900e
JP
6 */
7
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/rhashtable.h>
d9363774 12#include <linux/workqueue.h>
06177558 13#include <linux/refcount.h>
77b9900e
JP
14
15#include <linux/if_ether.h>
16#include <linux/in6.h>
17#include <linux/ip.h>
a577d8f7 18#include <linux/mpls.h>
77b9900e
JP
19
20#include <net/sch_generic.h>
21#include <net/pkt_cls.h>
22#include <net/ip.h>
23#include <net/flow_dissector.h>
0a6e7778 24#include <net/geneve.h>
77b9900e 25
bc3103f1
AV
26#include <net/dst.h>
27#include <net/dst_metadata.h>
28
e0ace68a
PB
29#include <uapi/linux/netfilter/nf_conntrack_common.h>
30
77b9900e 31struct fl_flow_key {
8212ed77 32 struct flow_dissector_key_meta meta;
42aecaa9 33 struct flow_dissector_key_control control;
bc3103f1 34 struct flow_dissector_key_control enc_control;
77b9900e
JP
35 struct flow_dissector_key_basic basic;
36 struct flow_dissector_key_eth_addrs eth;
9399ae9a 37 struct flow_dissector_key_vlan vlan;
d64efd09 38 struct flow_dissector_key_vlan cvlan;
77b9900e 39 union {
c3f83241 40 struct flow_dissector_key_ipv4_addrs ipv4;
77b9900e
JP
41 struct flow_dissector_key_ipv6_addrs ipv6;
42 };
43 struct flow_dissector_key_ports tp;
7b684884 44 struct flow_dissector_key_icmp icmp;
99d31326 45 struct flow_dissector_key_arp arp;
bc3103f1
AV
46 struct flow_dissector_key_keyid enc_key_id;
47 union {
48 struct flow_dissector_key_ipv4_addrs enc_ipv4;
49 struct flow_dissector_key_ipv6_addrs enc_ipv6;
50 };
f4d997fd 51 struct flow_dissector_key_ports enc_tp;
a577d8f7 52 struct flow_dissector_key_mpls mpls;
fdfc7dd6 53 struct flow_dissector_key_tcp tcp;
4d80cc0a 54 struct flow_dissector_key_ip ip;
0e2c17b6 55 struct flow_dissector_key_ip enc_ip;
0a6e7778 56 struct flow_dissector_key_enc_opts enc_opts;
5c72299f
AN
57 struct flow_dissector_key_ports tp_min;
58 struct flow_dissector_key_ports tp_max;
e0ace68a 59 struct flow_dissector_key_ct ct;
77b9900e
JP
60} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
61
62struct fl_flow_mask_range {
63 unsigned short int start;
64 unsigned short int end;
65};
66
67struct fl_flow_mask {
68 struct fl_flow_key key;
69 struct fl_flow_mask_range range;
5c72299f 70 u32 flags;
05cd271f
PB
71 struct rhash_head ht_node;
72 struct rhashtable ht;
73 struct rhashtable_params filter_ht_params;
74 struct flow_dissector dissector;
75 struct list_head filters;
44a5cd43 76 struct rcu_work rwork;
05cd271f 77 struct list_head list;
f48ef4d5 78 refcount_t refcnt;
77b9900e
JP
79};
80
b95ec7eb
JP
81struct fl_flow_tmplt {
82 struct fl_flow_key dummy_key;
83 struct fl_flow_key mask;
84 struct flow_dissector dissector;
85 struct tcf_chain *chain;
86};
87
77b9900e
JP
88struct cls_fl_head {
89 struct rhashtable ht;
259e60f9 90 spinlock_t masks_lock; /* Protect masks list */
05cd271f 91 struct list_head masks;
c049d56e 92 struct list_head hw_filters;
aaa908ff 93 struct rcu_work rwork;
c15ab236 94 struct idr handle_idr;
77b9900e
JP
95};
96
97struct cls_fl_filter {
05cd271f 98 struct fl_flow_mask *mask;
77b9900e
JP
99 struct rhash_head ht_node;
100 struct fl_flow_key mkey;
101 struct tcf_exts exts;
102 struct tcf_result res;
103 struct fl_flow_key key;
104 struct list_head list;
c049d56e 105 struct list_head hw_list;
77b9900e 106 u32 handle;
e69985c6 107 u32 flags;
86c55361 108 u32 in_hw_count;
aaa908ff 109 struct rcu_work rwork;
7091d8c7 110 struct net_device *hw_dev;
06177558
VB
111 /* Flower classifier is unlocked, which means that its reference counter
112 * can be changed concurrently without any kind of external
113 * synchronization. Use atomic reference counter to be concurrency-safe.
114 */
115 refcount_t refcnt;
b2552b8c 116 bool deleted;
77b9900e
JP
117};
118
05cd271f
PB
119static const struct rhashtable_params mask_ht_params = {
120 .key_offset = offsetof(struct fl_flow_mask, key),
121 .key_len = sizeof(struct fl_flow_key),
122 .head_offset = offsetof(struct fl_flow_mask, ht_node),
123 .automatic_shrinking = true,
124};
125
77b9900e
JP
126static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
127{
128 return mask->range.end - mask->range.start;
129}
130
131static void fl_mask_update_range(struct fl_flow_mask *mask)
132{
133 const u8 *bytes = (const u8 *) &mask->key;
134 size_t size = sizeof(mask->key);
05cd271f 135 size_t i, first = 0, last;
77b9900e 136
05cd271f
PB
137 for (i = 0; i < size; i++) {
138 if (bytes[i]) {
139 first = i;
140 break;
141 }
142 }
143 last = first;
144 for (i = size - 1; i != first; i--) {
77b9900e 145 if (bytes[i]) {
77b9900e 146 last = i;
05cd271f 147 break;
77b9900e
JP
148 }
149 }
150 mask->range.start = rounddown(first, sizeof(long));
151 mask->range.end = roundup(last + 1, sizeof(long));
152}
153
154static void *fl_key_get_start(struct fl_flow_key *key,
155 const struct fl_flow_mask *mask)
156{
157 return (u8 *) key + mask->range.start;
158}
159
160static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
161 struct fl_flow_mask *mask)
162{
163 const long *lkey = fl_key_get_start(key, mask);
164 const long *lmask = fl_key_get_start(&mask->key, mask);
165 long *lmkey = fl_key_get_start(mkey, mask);
166 int i;
167
168 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
169 *lmkey++ = *lkey++ & *lmask++;
170}
171
b95ec7eb
JP
172static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
173 struct fl_flow_mask *mask)
174{
175 const long *lmask = fl_key_get_start(&mask->key, mask);
176 const long *ltmplt;
177 int i;
178
179 if (!tmplt)
180 return true;
181 ltmplt = fl_key_get_start(&tmplt->mask, mask);
182 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
183 if (~*ltmplt++ & *lmask++)
184 return false;
185 }
186 return true;
187}
188
77b9900e
JP
189static void fl_clear_masked_range(struct fl_flow_key *key,
190 struct fl_flow_mask *mask)
191{
192 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
193}
194
5c72299f
AN
195static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
196 struct fl_flow_key *key,
197 struct fl_flow_key *mkey)
198{
199 __be16 min_mask, max_mask, min_val, max_val;
200
201 min_mask = htons(filter->mask->key.tp_min.dst);
202 max_mask = htons(filter->mask->key.tp_max.dst);
203 min_val = htons(filter->key.tp_min.dst);
204 max_val = htons(filter->key.tp_max.dst);
205
206 if (min_mask && max_mask) {
207 if (htons(key->tp.dst) < min_val ||
208 htons(key->tp.dst) > max_val)
209 return false;
210
211 /* skb does not have min and max values */
212 mkey->tp_min.dst = filter->mkey.tp_min.dst;
213 mkey->tp_max.dst = filter->mkey.tp_max.dst;
214 }
215 return true;
216}
217
218static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
219 struct fl_flow_key *key,
220 struct fl_flow_key *mkey)
221{
222 __be16 min_mask, max_mask, min_val, max_val;
223
224 min_mask = htons(filter->mask->key.tp_min.src);
225 max_mask = htons(filter->mask->key.tp_max.src);
226 min_val = htons(filter->key.tp_min.src);
227 max_val = htons(filter->key.tp_max.src);
228
229 if (min_mask && max_mask) {
230 if (htons(key->tp.src) < min_val ||
231 htons(key->tp.src) > max_val)
232 return false;
233
234 /* skb does not have min and max values */
235 mkey->tp_min.src = filter->mkey.tp_min.src;
236 mkey->tp_max.src = filter->mkey.tp_max.src;
237 }
238 return true;
239}
240
241static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
242 struct fl_flow_key *mkey)
a3308d8f 243{
05cd271f
PB
244 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
245 mask->filter_ht_params);
a3308d8f
PB
246}
247
5c72299f
AN
248static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
249 struct fl_flow_key *mkey,
250 struct fl_flow_key *key)
251{
252 struct cls_fl_filter *filter, *f;
253
254 list_for_each_entry_rcu(filter, &mask->filters, list) {
255 if (!fl_range_port_dst_cmp(filter, key, mkey))
256 continue;
257
258 if (!fl_range_port_src_cmp(filter, key, mkey))
259 continue;
260
261 f = __fl_lookup(mask, mkey);
262 if (f)
263 return f;
264 }
265 return NULL;
266}
267
268static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
269 struct fl_flow_key *mkey,
270 struct fl_flow_key *key)
271{
272 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
273 return fl_lookup_range(mask, mkey, key);
274
275 return __fl_lookup(mask, mkey);
276}
277
e0ace68a
PB
278static u16 fl_ct_info_to_flower_map[] = {
279 [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
280 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
281 [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
282 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
283 [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
284 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
285 [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
286 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
287 [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
288 TCA_FLOWER_KEY_CT_FLAGS_NEW,
289};
290
77b9900e
JP
291static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
292 struct tcf_result *res)
293{
294 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
77b9900e 295 struct fl_flow_key skb_mkey;
e0ace68a
PB
296 struct fl_flow_key skb_key;
297 struct fl_flow_mask *mask;
298 struct cls_fl_filter *f;
77b9900e 299
05cd271f
PB
300 list_for_each_entry_rcu(mask, &head->masks, list) {
301 fl_clear_masked_range(&skb_key, mask);
bc3103f1 302
8212ed77 303 skb_flow_dissect_meta(skb, &mask->dissector, &skb_key);
05cd271f
PB
304 /* skb_flow_dissect() does not set n_proto in case an unknown
305 * protocol, so do it rather here.
306 */
307 skb_key.basic.n_proto = skb->protocol;
308 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
e0ace68a
PB
309 skb_flow_dissect_ct(skb, &mask->dissector, &skb_key,
310 fl_ct_info_to_flower_map,
311 ARRAY_SIZE(fl_ct_info_to_flower_map));
05cd271f 312 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
77b9900e 313
05cd271f 314 fl_set_masked_key(&skb_mkey, &skb_key, mask);
77b9900e 315
5c72299f 316 f = fl_lookup(mask, &skb_mkey, &skb_key);
05cd271f
PB
317 if (f && !tc_skip_sw(f->flags)) {
318 *res = f->res;
319 return tcf_exts_exec(skb, &f->exts, res);
320 }
77b9900e
JP
321 }
322 return -1;
323}
324
325static int fl_init(struct tcf_proto *tp)
326{
327 struct cls_fl_head *head;
328
329 head = kzalloc(sizeof(*head), GFP_KERNEL);
330 if (!head)
331 return -ENOBUFS;
332
259e60f9 333 spin_lock_init(&head->masks_lock);
05cd271f 334 INIT_LIST_HEAD_RCU(&head->masks);
c049d56e 335 INIT_LIST_HEAD(&head->hw_filters);
77b9900e 336 rcu_assign_pointer(tp->root, head);
c15ab236 337 idr_init(&head->handle_idr);
77b9900e 338
05cd271f
PB
339 return rhashtable_init(&head->ht, &mask_ht_params);
340}
341
99815f50 342static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done)
44a5cd43 343{
99815f50
VB
344 /* temporary masks don't have their filters list and ht initialized */
345 if (mask_init_done) {
346 WARN_ON(!list_empty(&mask->filters));
347 rhashtable_destroy(&mask->ht);
348 }
44a5cd43
PA
349 kfree(mask);
350}
351
352static void fl_mask_free_work(struct work_struct *work)
353{
354 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
355 struct fl_flow_mask, rwork);
356
99815f50
VB
357 fl_mask_free(mask, true);
358}
359
360static void fl_uninit_mask_free_work(struct work_struct *work)
361{
362 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
363 struct fl_flow_mask, rwork);
364
365 fl_mask_free(mask, false);
44a5cd43
PA
366}
367
9994677c 368static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask)
05cd271f 369{
f48ef4d5 370 if (!refcount_dec_and_test(&mask->refcnt))
05cd271f
PB
371 return false;
372
373 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
259e60f9
VB
374
375 spin_lock(&head->masks_lock);
05cd271f 376 list_del_rcu(&mask->list);
259e60f9
VB
377 spin_unlock(&head->masks_lock);
378
9994677c 379 tcf_queue_work(&mask->rwork, fl_mask_free_work);
05cd271f
PB
380
381 return true;
77b9900e
JP
382}
383
c049d56e
VB
384static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
385{
386 /* Flower classifier only changes root pointer during init and destroy.
387 * Users must obtain reference to tcf_proto instance before calling its
388 * API, so tp->root pointer is protected from concurrent call to
389 * fl_destroy() by reference counting.
390 */
391 return rcu_dereference_raw(tp->root);
392}
393
0dadc117
CW
394static void __fl_destroy_filter(struct cls_fl_filter *f)
395{
396 tcf_exts_destroy(&f->exts);
397 tcf_exts_put_net(&f->exts);
398 kfree(f);
399}
400
0552c8af 401static void fl_destroy_filter_work(struct work_struct *work)
77b9900e 402{
aaa908ff
CW
403 struct cls_fl_filter *f = container_of(to_rcu_work(work),
404 struct cls_fl_filter, rwork);
77b9900e 405
0dadc117 406 __fl_destroy_filter(f);
0552c8af
CW
407}
408
1b0f8037 409static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
c24e43d8 410 bool rtnl_held, struct netlink_ext_ack *extack)
5b33f488 411{
208c0f4b 412 struct tcf_block *block = tp->chain->block;
f9e30088 413 struct flow_cls_offload cls_flower = {};
5b33f488 414
c24e43d8
VB
415 if (!rtnl_held)
416 rtnl_lock();
417
d6787147 418 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
f9e30088 419 cls_flower.command = FLOW_CLS_DESTROY;
de4784ca 420 cls_flower.cookie = (unsigned long) f;
5b33f488 421
aeb3fecd 422 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
3d81e711 423 spin_lock(&tp->lock);
c049d56e 424 list_del_init(&f->hw_list);
caa72601 425 tcf_block_offload_dec(block, &f->flags);
3d81e711 426 spin_unlock(&tp->lock);
c24e43d8
VB
427
428 if (!rtnl_held)
429 rtnl_unlock();
5b33f488
AV
430}
431
e8eb36cd 432static int fl_hw_replace_filter(struct tcf_proto *tp,
c24e43d8 433 struct cls_fl_filter *f, bool rtnl_held,
41002038 434 struct netlink_ext_ack *extack)
5b33f488 435{
c049d56e 436 struct cls_fl_head *head = fl_head_dereference(tp);
208c0f4b 437 struct tcf_block *block = tp->chain->block;
f9e30088 438 struct flow_cls_offload cls_flower = {};
717503b9 439 bool skip_sw = tc_skip_sw(f->flags);
c24e43d8
VB
440 int err = 0;
441
442 if (!rtnl_held)
443 rtnl_lock();
5b33f488 444
e3ab786b 445 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
c24e43d8
VB
446 if (!cls_flower.rule) {
447 err = -ENOMEM;
448 goto errout;
449 }
8f256622 450
d6787147 451 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
f9e30088 452 cls_flower.command = FLOW_CLS_REPLACE;
de4784ca 453 cls_flower.cookie = (unsigned long) f;
8f256622
PNA
454 cls_flower.rule->match.dissector = &f->mask->dissector;
455 cls_flower.rule->match.mask = &f->mask->key;
456 cls_flower.rule->match.key = &f->mkey;
384c181e 457 cls_flower.classid = f->res.classid;
5b33f488 458
3a7b6861
PNA
459 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
460 if (err) {
461 kfree(cls_flower.rule);
c24e43d8 462 if (skip_sw)
1f15bb4f 463 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
c24e43d8
VB
464 else
465 err = 0;
466 goto errout;
3a7b6861
PNA
467 }
468
aeb3fecd 469 err = tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, skip_sw);
8f256622
PNA
470 kfree(cls_flower.rule);
471
717503b9 472 if (err < 0) {
c24e43d8
VB
473 fl_hw_destroy_filter(tp, f, true, NULL);
474 goto errout;
717503b9 475 } else if (err > 0) {
31533cba 476 f->in_hw_count = err;
c24e43d8 477 err = 0;
3d81e711 478 spin_lock(&tp->lock);
caa72601 479 tcf_block_offload_inc(block, &f->flags);
3d81e711 480 spin_unlock(&tp->lock);
717503b9
JP
481 }
482
c24e43d8
VB
483 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) {
484 err = -EINVAL;
485 goto errout;
486 }
717503b9 487
c049d56e
VB
488 spin_lock(&tp->lock);
489 list_add(&f->hw_list, &head->hw_filters);
490 spin_unlock(&tp->lock);
c24e43d8
VB
491errout:
492 if (!rtnl_held)
493 rtnl_unlock();
494
495 return err;
5b33f488
AV
496}
497
c24e43d8
VB
498static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f,
499 bool rtnl_held)
10cbc684 500{
208c0f4b 501 struct tcf_block *block = tp->chain->block;
f9e30088 502 struct flow_cls_offload cls_flower = {};
10cbc684 503
c24e43d8
VB
504 if (!rtnl_held)
505 rtnl_lock();
506
d6787147 507 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
f9e30088 508 cls_flower.command = FLOW_CLS_STATS;
de4784ca 509 cls_flower.cookie = (unsigned long) f;
384c181e 510 cls_flower.classid = f->res.classid;
10cbc684 511
aeb3fecd 512 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
3b1903ef
PNA
513
514 tcf_exts_stats_update(&f->exts, cls_flower.stats.bytes,
515 cls_flower.stats.pkts,
516 cls_flower.stats.lastused);
c24e43d8
VB
517
518 if (!rtnl_held)
519 rtnl_unlock();
10cbc684
AV
520}
521
06177558
VB
522static void __fl_put(struct cls_fl_filter *f)
523{
524 if (!refcount_dec_and_test(&f->refcnt))
525 return;
526
527 if (tcf_exts_get_net(&f->exts))
528 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
529 else
530 __fl_destroy_filter(f);
531}
532
533static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
534{
535 struct cls_fl_filter *f;
536
537 rcu_read_lock();
538 f = idr_find(&head->handle_idr, handle);
539 if (f && !refcount_inc_not_zero(&f->refcnt))
540 f = NULL;
541 rcu_read_unlock();
542
543 return f;
544}
545
b2552b8c 546static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
c24e43d8
VB
547 bool *last, bool rtnl_held,
548 struct netlink_ext_ack *extack)
13fa876e 549{
e474619a 550 struct cls_fl_head *head = fl_head_dereference(tp);
c15ab236 551
b2552b8c
VB
552 *last = false;
553
3d81e711
VB
554 spin_lock(&tp->lock);
555 if (f->deleted) {
556 spin_unlock(&tp->lock);
b2552b8c 557 return -ENOENT;
3d81e711 558 }
b2552b8c
VB
559
560 f->deleted = true;
561 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
562 f->mask->filter_ht_params);
9c160941 563 idr_remove(&head->handle_idr, f->handle);
13fa876e 564 list_del_rcu(&f->list);
3d81e711
VB
565 spin_unlock(&tp->lock);
566
9994677c 567 *last = fl_mask_put(head, f->mask);
79685219 568 if (!tc_skip_hw(f->flags))
c24e43d8 569 fl_hw_destroy_filter(tp, f, rtnl_held, extack);
13fa876e 570 tcf_unbind_filter(tp, &f->res);
06177558 571 __fl_put(f);
05cd271f 572
b2552b8c 573 return 0;
13fa876e
RD
574}
575
d9363774
DB
576static void fl_destroy_sleepable(struct work_struct *work)
577{
aaa908ff
CW
578 struct cls_fl_head *head = container_of(to_rcu_work(work),
579 struct cls_fl_head,
580 rwork);
de9dc650
PB
581
582 rhashtable_destroy(&head->ht);
d9363774
DB
583 kfree(head);
584 module_put(THIS_MODULE);
585}
586
12db03b6
VB
587static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
588 struct netlink_ext_ack *extack)
77b9900e 589{
e474619a 590 struct cls_fl_head *head = fl_head_dereference(tp);
05cd271f 591 struct fl_flow_mask *mask, *next_mask;
77b9900e 592 struct cls_fl_filter *f, *next;
b2552b8c 593 bool last;
77b9900e 594
05cd271f
PB
595 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
596 list_for_each_entry_safe(f, next, &mask->filters, list) {
c24e43d8 597 __fl_delete(tp, f, &last, rtnl_held, extack);
b2552b8c 598 if (last)
05cd271f
PB
599 break;
600 }
601 }
c15ab236 602 idr_destroy(&head->handle_idr);
d9363774
DB
603
604 __module_get(THIS_MODULE);
aaa908ff 605 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
77b9900e
JP
606}
607
06177558
VB
608static void fl_put(struct tcf_proto *tp, void *arg)
609{
610 struct cls_fl_filter *f = arg;
611
612 __fl_put(f);
613}
614
8113c095 615static void *fl_get(struct tcf_proto *tp, u32 handle)
77b9900e 616{
e474619a 617 struct cls_fl_head *head = fl_head_dereference(tp);
77b9900e 618
06177558 619 return __fl_get(head, handle);
77b9900e
JP
620}
621
622static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
623 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
624 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
625 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
626 .len = IFNAMSIZ },
627 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
628 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
629 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
630 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
631 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
632 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
633 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
634 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
635 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
636 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
637 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
638 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
639 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
640 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
641 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
642 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
b175c3a4
JHS
643 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
644 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
9399ae9a
HHZ
645 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
646 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
647 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
bc3103f1
AV
648 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
649 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
650 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
651 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
652 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
653 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
654 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
655 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
656 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
aa72d708
OG
657 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
658 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
659 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
660 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
5976c5f4
SH
661 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
662 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
663 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
664 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
f4d997fd
HHZ
665 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
666 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
667 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
668 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
faa3ffce
OG
669 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
670 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
7b684884
SH
671 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
672 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
673 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
674 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
675 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
676 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
677 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
678 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
99d31326
SH
679 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
680 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
681 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
682 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
683 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
684 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
685 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
686 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
687 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
688 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
a577d8f7
BL
689 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
690 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
691 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
692 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
fdfc7dd6
JP
693 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
694 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
4d80cc0a
OG
695 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
696 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
697 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
698 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
d64efd09
JL
699 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
700 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
701 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
0e2c17b6
OG
702 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
703 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
704 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
705 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
0a6e7778
PJV
706 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
707 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
e0ace68a
PB
708 [TCA_FLOWER_KEY_CT_STATE] = { .type = NLA_U16 },
709 [TCA_FLOWER_KEY_CT_STATE_MASK] = { .type = NLA_U16 },
710 [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 },
711 [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 },
712 [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 },
713 [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 },
714 [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY,
715 .len = 128 / BITS_PER_BYTE },
716 [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY,
717 .len = 128 / BITS_PER_BYTE },
0a6e7778
PJV
718};
719
720static const struct nla_policy
721enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
722 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
723};
724
725static const struct nla_policy
726geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
727 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
728 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
729 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
730 .len = 128 },
77b9900e
JP
731};
732
733static void fl_set_key_val(struct nlattr **tb,
734 void *val, int val_type,
735 void *mask, int mask_type, int len)
736{
737 if (!tb[val_type])
738 return;
e0ace68a 739 nla_memcpy(val, tb[val_type], len);
77b9900e
JP
740 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
741 memset(mask, 0xff, len);
742 else
e0ace68a 743 nla_memcpy(mask, tb[mask_type], len);
77b9900e
JP
744}
745
5c72299f
AN
746static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
747 struct fl_flow_key *mask)
748{
749 fl_set_key_val(tb, &key->tp_min.dst,
750 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_min.dst,
751 TCA_FLOWER_UNSPEC, sizeof(key->tp_min.dst));
752 fl_set_key_val(tb, &key->tp_max.dst,
753 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_max.dst,
754 TCA_FLOWER_UNSPEC, sizeof(key->tp_max.dst));
755 fl_set_key_val(tb, &key->tp_min.src,
756 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_min.src,
757 TCA_FLOWER_UNSPEC, sizeof(key->tp_min.src));
758 fl_set_key_val(tb, &key->tp_max.src,
759 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_max.src,
760 TCA_FLOWER_UNSPEC, sizeof(key->tp_max.src));
761
762 if ((mask->tp_min.dst && mask->tp_max.dst &&
763 htons(key->tp_max.dst) <= htons(key->tp_min.dst)) ||
764 (mask->tp_min.src && mask->tp_max.src &&
765 htons(key->tp_max.src) <= htons(key->tp_min.src)))
766 return -EINVAL;
767
768 return 0;
769}
770
1a7fca63
BL
771static int fl_set_key_mpls(struct nlattr **tb,
772 struct flow_dissector_key_mpls *key_val,
773 struct flow_dissector_key_mpls *key_mask)
a577d8f7
BL
774{
775 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
776 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
777 key_mask->mpls_ttl = MPLS_TTL_MASK;
778 }
779 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
1a7fca63
BL
780 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
781
782 if (bos & ~MPLS_BOS_MASK)
783 return -EINVAL;
784 key_val->mpls_bos = bos;
a577d8f7
BL
785 key_mask->mpls_bos = MPLS_BOS_MASK;
786 }
787 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
1a7fca63
BL
788 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
789
790 if (tc & ~MPLS_TC_MASK)
791 return -EINVAL;
792 key_val->mpls_tc = tc;
a577d8f7
BL
793 key_mask->mpls_tc = MPLS_TC_MASK;
794 }
795 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
1a7fca63
BL
796 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
797
798 if (label & ~MPLS_LABEL_MASK)
799 return -EINVAL;
800 key_val->mpls_label = label;
a577d8f7
BL
801 key_mask->mpls_label = MPLS_LABEL_MASK;
802 }
1a7fca63 803 return 0;
a577d8f7
BL
804}
805
9399ae9a 806static void fl_set_key_vlan(struct nlattr **tb,
aaab0834 807 __be16 ethertype,
d64efd09 808 int vlan_id_key, int vlan_prio_key,
9399ae9a
HHZ
809 struct flow_dissector_key_vlan *key_val,
810 struct flow_dissector_key_vlan *key_mask)
811{
812#define VLAN_PRIORITY_MASK 0x7
813
d64efd09 814 if (tb[vlan_id_key]) {
9399ae9a 815 key_val->vlan_id =
d64efd09 816 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
9399ae9a
HHZ
817 key_mask->vlan_id = VLAN_VID_MASK;
818 }
d64efd09 819 if (tb[vlan_prio_key]) {
9399ae9a 820 key_val->vlan_priority =
d64efd09 821 nla_get_u8(tb[vlan_prio_key]) &
9399ae9a
HHZ
822 VLAN_PRIORITY_MASK;
823 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
824 }
aaab0834
JL
825 key_val->vlan_tpid = ethertype;
826 key_mask->vlan_tpid = cpu_to_be16(~0);
9399ae9a
HHZ
827}
828
faa3ffce
OG
829static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
830 u32 *dissector_key, u32 *dissector_mask,
831 u32 flower_flag_bit, u32 dissector_flag_bit)
832{
833 if (flower_mask & flower_flag_bit) {
834 *dissector_mask |= dissector_flag_bit;
835 if (flower_key & flower_flag_bit)
836 *dissector_key |= dissector_flag_bit;
837 }
838}
839
d9724772
OG
840static int fl_set_key_flags(struct nlattr **tb,
841 u32 *flags_key, u32 *flags_mask)
faa3ffce
OG
842{
843 u32 key, mask;
844
d9724772
OG
845 /* mask is mandatory for flags */
846 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
847 return -EINVAL;
faa3ffce
OG
848
849 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
d9724772 850 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
faa3ffce
OG
851
852 *flags_key = 0;
853 *flags_mask = 0;
854
855 fl_set_key_flag(key, mask, flags_key, flags_mask,
856 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
459d153d
PJV
857 fl_set_key_flag(key, mask, flags_key, flags_mask,
858 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
859 FLOW_DIS_FIRST_FRAG);
d9724772
OG
860
861 return 0;
faa3ffce
OG
862}
863
0e2c17b6 864static void fl_set_key_ip(struct nlattr **tb, bool encap,
4d80cc0a
OG
865 struct flow_dissector_key_ip *key,
866 struct flow_dissector_key_ip *mask)
867{
0e2c17b6
OG
868 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
869 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
870 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
871 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
4d80cc0a 872
0e2c17b6
OG
873 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
874 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
4d80cc0a
OG
875}
876
0a6e7778
PJV
877static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
878 int depth, int option_len,
879 struct netlink_ext_ack *extack)
880{
881 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
882 struct nlattr *class = NULL, *type = NULL, *data = NULL;
883 struct geneve_opt *opt;
884 int err, data_len = 0;
885
886 if (option_len > sizeof(struct geneve_opt))
887 data_len = option_len - sizeof(struct geneve_opt);
888
889 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
890 memset(opt, 0xff, option_len);
891 opt->length = data_len / 4;
892 opt->r1 = 0;
893 opt->r2 = 0;
894 opt->r3 = 0;
895
896 /* If no mask has been prodived we assume an exact match. */
897 if (!depth)
898 return sizeof(struct geneve_opt) + data_len;
899
900 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
901 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
902 return -EINVAL;
903 }
904
8cb08174
JB
905 err = nla_parse_nested_deprecated(tb,
906 TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
907 nla, geneve_opt_policy, extack);
0a6e7778
PJV
908 if (err < 0)
909 return err;
910
911 /* We are not allowed to omit any of CLASS, TYPE or DATA
912 * fields from the key.
913 */
914 if (!option_len &&
915 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
916 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
917 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
918 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
919 return -EINVAL;
920 }
921
922 /* Omitting any of CLASS, TYPE or DATA fields is allowed
923 * for the mask.
924 */
925 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
926 int new_len = key->enc_opts.len;
927
928 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
929 data_len = nla_len(data);
930 if (data_len < 4) {
931 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
932 return -ERANGE;
933 }
934 if (data_len % 4) {
935 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
936 return -ERANGE;
937 }
938
939 new_len += sizeof(struct geneve_opt) + data_len;
940 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
941 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
942 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
943 return -ERANGE;
944 }
945 opt->length = data_len / 4;
946 memcpy(opt->opt_data, nla_data(data), data_len);
947 }
948
949 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
950 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
951 opt->opt_class = nla_get_be16(class);
952 }
953
954 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
955 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
956 opt->type = nla_get_u8(type);
957 }
958
959 return sizeof(struct geneve_opt) + data_len;
960}
961
962static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
963 struct fl_flow_key *mask,
964 struct netlink_ext_ack *extack)
965{
966 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
63c82997
JK
967 int err, option_len, key_depth, msk_depth = 0;
968
8cb08174
JB
969 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
970 TCA_FLOWER_KEY_ENC_OPTS_MAX,
971 enc_opts_policy, extack);
63c82997
JK
972 if (err)
973 return err;
0a6e7778
PJV
974
975 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
976
977 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
8cb08174
JB
978 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
979 TCA_FLOWER_KEY_ENC_OPTS_MAX,
980 enc_opts_policy, extack);
63c82997
JK
981 if (err)
982 return err;
983
0a6e7778
PJV
984 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
985 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
986 }
987
988 nla_for_each_attr(nla_opt_key, nla_enc_key,
989 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
990 switch (nla_type(nla_opt_key)) {
991 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
992 option_len = 0;
993 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
994 option_len = fl_set_geneve_opt(nla_opt_key, key,
995 key_depth, option_len,
996 extack);
997 if (option_len < 0)
998 return option_len;
999
1000 key->enc_opts.len += option_len;
1001 /* At the same time we need to parse through the mask
1002 * in order to verify exact and mask attribute lengths.
1003 */
1004 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
1005 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
1006 msk_depth, option_len,
1007 extack);
1008 if (option_len < 0)
1009 return option_len;
1010
1011 mask->enc_opts.len += option_len;
1012 if (key->enc_opts.len != mask->enc_opts.len) {
1013 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1014 return -EINVAL;
1015 }
1016
1017 if (msk_depth)
1018 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
1019 break;
1020 default:
1021 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
1022 return -EINVAL;
1023 }
1024 }
1025
1026 return 0;
1027}
1028
e0ace68a
PB
1029static int fl_set_key_ct(struct nlattr **tb,
1030 struct flow_dissector_key_ct *key,
1031 struct flow_dissector_key_ct *mask,
1032 struct netlink_ext_ack *extack)
1033{
1034 if (tb[TCA_FLOWER_KEY_CT_STATE]) {
1035 if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) {
1036 NL_SET_ERR_MSG(extack, "Conntrack isn't enabled");
1037 return -EOPNOTSUPP;
1038 }
1039 fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
1040 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
1041 sizeof(key->ct_state));
1042 }
1043 if (tb[TCA_FLOWER_KEY_CT_ZONE]) {
1044 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1045 NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled");
1046 return -EOPNOTSUPP;
1047 }
1048 fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
1049 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
1050 sizeof(key->ct_zone));
1051 }
1052 if (tb[TCA_FLOWER_KEY_CT_MARK]) {
1053 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1054 NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled");
1055 return -EOPNOTSUPP;
1056 }
1057 fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
1058 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
1059 sizeof(key->ct_mark));
1060 }
1061 if (tb[TCA_FLOWER_KEY_CT_LABELS]) {
1062 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1063 NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled");
1064 return -EOPNOTSUPP;
1065 }
1066 fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
1067 mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
1068 sizeof(key->ct_labels));
1069 }
1070
1071 return 0;
1072}
1073
77b9900e 1074static int fl_set_key(struct net *net, struct nlattr **tb,
1057c55f
AA
1075 struct fl_flow_key *key, struct fl_flow_key *mask,
1076 struct netlink_ext_ack *extack)
77b9900e 1077{
9399ae9a 1078 __be16 ethertype;
d9724772 1079 int ret = 0;
a5148626 1080
77b9900e 1081 if (tb[TCA_FLOWER_INDEV]) {
1057c55f 1082 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
77b9900e
JP
1083 if (err < 0)
1084 return err;
8212ed77
JP
1085 key->meta.ingress_ifindex = err;
1086 mask->meta.ingress_ifindex = 0xffffffff;
77b9900e
JP
1087 }
1088
1089 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1090 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1091 sizeof(key->eth.dst));
1092 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1093 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1094 sizeof(key->eth.src));
66530bdf 1095
0b498a52 1096 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
9399ae9a
HHZ
1097 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
1098
aaab0834 1099 if (eth_type_vlan(ethertype)) {
d64efd09
JL
1100 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
1101 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
1102 &mask->vlan);
1103
5e9a0fe4
JL
1104 if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
1105 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
1106 if (eth_type_vlan(ethertype)) {
1107 fl_set_key_vlan(tb, ethertype,
1108 TCA_FLOWER_KEY_CVLAN_ID,
1109 TCA_FLOWER_KEY_CVLAN_PRIO,
1110 &key->cvlan, &mask->cvlan);
1111 fl_set_key_val(tb, &key->basic.n_proto,
1112 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1113 &mask->basic.n_proto,
1114 TCA_FLOWER_UNSPEC,
1115 sizeof(key->basic.n_proto));
1116 } else {
1117 key->basic.n_proto = ethertype;
1118 mask->basic.n_proto = cpu_to_be16(~0);
1119 }
d64efd09 1120 }
0b498a52
AB
1121 } else {
1122 key->basic.n_proto = ethertype;
1123 mask->basic.n_proto = cpu_to_be16(~0);
1124 }
9399ae9a 1125 }
66530bdf 1126
77b9900e
JP
1127 if (key->basic.n_proto == htons(ETH_P_IP) ||
1128 key->basic.n_proto == htons(ETH_P_IPV6)) {
1129 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1130 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1131 sizeof(key->basic.ip_proto));
0e2c17b6 1132 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
77b9900e 1133 }
66530bdf
JHS
1134
1135 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1136 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
970bfcd0 1137 mask->control.addr_type = ~0;
77b9900e
JP
1138 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1139 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1140 sizeof(key->ipv4.src));
1141 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1142 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1143 sizeof(key->ipv4.dst));
66530bdf
JHS
1144 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1145 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
970bfcd0 1146 mask->control.addr_type = ~0;
77b9900e
JP
1147 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1148 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1149 sizeof(key->ipv6.src));
1150 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1151 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1152 sizeof(key->ipv6.dst));
1153 }
66530bdf 1154
77b9900e
JP
1155 if (key->basic.ip_proto == IPPROTO_TCP) {
1156 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
aa72d708 1157 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
77b9900e
JP
1158 sizeof(key->tp.src));
1159 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
aa72d708 1160 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
77b9900e 1161 sizeof(key->tp.dst));
fdfc7dd6
JP
1162 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1163 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1164 sizeof(key->tcp.flags));
77b9900e
JP
1165 } else if (key->basic.ip_proto == IPPROTO_UDP) {
1166 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
aa72d708 1167 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
77b9900e
JP
1168 sizeof(key->tp.src));
1169 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
aa72d708 1170 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
77b9900e 1171 sizeof(key->tp.dst));
5976c5f4
SH
1172 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
1173 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1174 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1175 sizeof(key->tp.src));
1176 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1177 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1178 sizeof(key->tp.dst));
7b684884
SH
1179 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
1180 key->basic.ip_proto == IPPROTO_ICMP) {
1181 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1182 &mask->icmp.type,
1183 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1184 sizeof(key->icmp.type));
1185 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1186 &mask->icmp.code,
1187 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1188 sizeof(key->icmp.code));
1189 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1190 key->basic.ip_proto == IPPROTO_ICMPV6) {
1191 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1192 &mask->icmp.type,
1193 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1194 sizeof(key->icmp.type));
040587af 1195 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
7b684884 1196 &mask->icmp.code,
040587af 1197 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
7b684884 1198 sizeof(key->icmp.code));
a577d8f7
BL
1199 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1200 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
1a7fca63
BL
1201 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
1202 if (ret)
1203 return ret;
99d31326
SH
1204 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
1205 key->basic.n_proto == htons(ETH_P_RARP)) {
1206 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
1207 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
1208 sizeof(key->arp.sip));
1209 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
1210 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
1211 sizeof(key->arp.tip));
1212 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
1213 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
1214 sizeof(key->arp.op));
1215 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1216 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1217 sizeof(key->arp.sha));
1218 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1219 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1220 sizeof(key->arp.tha));
77b9900e
JP
1221 }
1222
5c72299f
AN
1223 if (key->basic.ip_proto == IPPROTO_TCP ||
1224 key->basic.ip_proto == IPPROTO_UDP ||
1225 key->basic.ip_proto == IPPROTO_SCTP) {
1226 ret = fl_set_key_port_range(tb, key, mask);
1227 if (ret)
1228 return ret;
1229 }
1230
bc3103f1
AV
1231 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
1232 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
1233 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
970bfcd0 1234 mask->enc_control.addr_type = ~0;
bc3103f1
AV
1235 fl_set_key_val(tb, &key->enc_ipv4.src,
1236 TCA_FLOWER_KEY_ENC_IPV4_SRC,
1237 &mask->enc_ipv4.src,
1238 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1239 sizeof(key->enc_ipv4.src));
1240 fl_set_key_val(tb, &key->enc_ipv4.dst,
1241 TCA_FLOWER_KEY_ENC_IPV4_DST,
1242 &mask->enc_ipv4.dst,
1243 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1244 sizeof(key->enc_ipv4.dst));
1245 }
1246
1247 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
1248 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
1249 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
970bfcd0 1250 mask->enc_control.addr_type = ~0;
bc3103f1
AV
1251 fl_set_key_val(tb, &key->enc_ipv6.src,
1252 TCA_FLOWER_KEY_ENC_IPV6_SRC,
1253 &mask->enc_ipv6.src,
1254 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1255 sizeof(key->enc_ipv6.src));
1256 fl_set_key_val(tb, &key->enc_ipv6.dst,
1257 TCA_FLOWER_KEY_ENC_IPV6_DST,
1258 &mask->enc_ipv6.dst,
1259 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1260 sizeof(key->enc_ipv6.dst));
1261 }
1262
1263 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
eb523f42 1264 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
bc3103f1
AV
1265 sizeof(key->enc_key_id.keyid));
1266
f4d997fd
HHZ
1267 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1268 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1269 sizeof(key->enc_tp.src));
1270
1271 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1272 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1273 sizeof(key->enc_tp.dst));
1274
0e2c17b6
OG
1275 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
1276
0a6e7778
PJV
1277 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
1278 ret = fl_set_enc_opt(tb, key, mask, extack);
1279 if (ret)
1280 return ret;
1281 }
1282
e0ace68a
PB
1283 ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack);
1284 if (ret)
1285 return ret;
1286
d9724772
OG
1287 if (tb[TCA_FLOWER_KEY_FLAGS])
1288 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
faa3ffce 1289
d9724772 1290 return ret;
77b9900e
JP
1291}
1292
05cd271f
PB
1293static void fl_mask_copy(struct fl_flow_mask *dst,
1294 struct fl_flow_mask *src)
77b9900e 1295{
05cd271f
PB
1296 const void *psrc = fl_key_get_start(&src->key, src);
1297 void *pdst = fl_key_get_start(&dst->key, src);
77b9900e 1298
05cd271f
PB
1299 memcpy(pdst, psrc, fl_mask_range(src));
1300 dst->range = src->range;
77b9900e
JP
1301}
1302
1303static const struct rhashtable_params fl_ht_params = {
1304 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
1305 .head_offset = offsetof(struct cls_fl_filter, ht_node),
1306 .automatic_shrinking = true,
1307};
1308
05cd271f 1309static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
77b9900e 1310{
05cd271f
PB
1311 mask->filter_ht_params = fl_ht_params;
1312 mask->filter_ht_params.key_len = fl_mask_range(mask);
1313 mask->filter_ht_params.key_offset += mask->range.start;
77b9900e 1314
05cd271f 1315 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
77b9900e
JP
1316}
1317
1318#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
cb205a81 1319#define FL_KEY_MEMBER_SIZE(member) FIELD_SIZEOF(struct fl_flow_key, member)
77b9900e 1320
339ba878
HHZ
1321#define FL_KEY_IS_MASKED(mask, member) \
1322 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
1323 0, FL_KEY_MEMBER_SIZE(member)) \
77b9900e
JP
1324
1325#define FL_KEY_SET(keys, cnt, id, member) \
1326 do { \
1327 keys[cnt].key_id = id; \
1328 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
1329 cnt++; \
1330 } while(0);
1331
339ba878 1332#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
77b9900e 1333 do { \
339ba878 1334 if (FL_KEY_IS_MASKED(mask, member)) \
77b9900e
JP
1335 FL_KEY_SET(keys, cnt, id, member); \
1336 } while(0);
1337
33fb5cba
JP
1338static void fl_init_dissector(struct flow_dissector *dissector,
1339 struct fl_flow_key *mask)
77b9900e
JP
1340{
1341 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1342 size_t cnt = 0;
1343
8212ed77
JP
1344 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1345 FLOW_DISSECTOR_KEY_META, meta);
42aecaa9 1346 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
77b9900e 1347 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
33fb5cba 1348 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
339ba878 1349 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
33fb5cba 1350 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
339ba878 1351 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
33fb5cba 1352 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
339ba878 1353 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
5c72299f
AN
1354 if (FL_KEY_IS_MASKED(mask, tp) ||
1355 FL_KEY_IS_MASKED(mask, tp_min) || FL_KEY_IS_MASKED(mask, tp_max))
1356 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_PORTS, tp);
33fb5cba 1357 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
4d80cc0a 1358 FLOW_DISSECTOR_KEY_IP, ip);
33fb5cba 1359 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
fdfc7dd6 1360 FLOW_DISSECTOR_KEY_TCP, tcp);
33fb5cba 1361 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
7b684884 1362 FLOW_DISSECTOR_KEY_ICMP, icmp);
33fb5cba 1363 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
99d31326 1364 FLOW_DISSECTOR_KEY_ARP, arp);
33fb5cba 1365 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
a577d8f7 1366 FLOW_DISSECTOR_KEY_MPLS, mpls);
33fb5cba 1367 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
9399ae9a 1368 FLOW_DISSECTOR_KEY_VLAN, vlan);
33fb5cba 1369 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
d64efd09 1370 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
33fb5cba 1371 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
519d1052 1372 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
33fb5cba 1373 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
519d1052 1374 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
33fb5cba 1375 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
519d1052 1376 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
33fb5cba
JP
1377 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1378 FL_KEY_IS_MASKED(mask, enc_ipv6))
519d1052
HHZ
1379 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1380 enc_control);
33fb5cba 1381 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
f4d997fd 1382 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
33fb5cba 1383 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
0e2c17b6 1384 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
0a6e7778
PJV
1385 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1386 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
e0ace68a
PB
1387 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1388 FLOW_DISSECTOR_KEY_CT, ct);
77b9900e 1389
33fb5cba 1390 skb_flow_dissector_init(dissector, keys, cnt);
05cd271f
PB
1391}
1392
1393static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1394 struct fl_flow_mask *mask)
1395{
1396 struct fl_flow_mask *newmask;
1397 int err;
1398
1399 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1400 if (!newmask)
1401 return ERR_PTR(-ENOMEM);
1402
1403 fl_mask_copy(newmask, mask);
1404
5c72299f
AN
1405 if ((newmask->key.tp_min.dst && newmask->key.tp_max.dst) ||
1406 (newmask->key.tp_min.src && newmask->key.tp_max.src))
1407 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
1408
05cd271f
PB
1409 err = fl_init_mask_hashtable(newmask);
1410 if (err)
1411 goto errout_free;
1412
33fb5cba 1413 fl_init_dissector(&newmask->dissector, &newmask->key);
05cd271f
PB
1414
1415 INIT_LIST_HEAD_RCU(&newmask->filters);
1416
f48ef4d5 1417 refcount_set(&newmask->refcnt, 1);
195c234d
VB
1418 err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
1419 &newmask->ht_node, mask_ht_params);
05cd271f
PB
1420 if (err)
1421 goto errout_destroy;
1422
259e60f9 1423 spin_lock(&head->masks_lock);
05cd271f 1424 list_add_tail_rcu(&newmask->list, &head->masks);
259e60f9 1425 spin_unlock(&head->masks_lock);
05cd271f
PB
1426
1427 return newmask;
1428
1429errout_destroy:
1430 rhashtable_destroy(&newmask->ht);
1431errout_free:
1432 kfree(newmask);
1433
1434 return ERR_PTR(err);
77b9900e
JP
1435}
1436
1437static int fl_check_assign_mask(struct cls_fl_head *head,
05cd271f
PB
1438 struct cls_fl_filter *fnew,
1439 struct cls_fl_filter *fold,
77b9900e
JP
1440 struct fl_flow_mask *mask)
1441{
05cd271f 1442 struct fl_flow_mask *newmask;
f48ef4d5 1443 int ret = 0;
77b9900e 1444
f48ef4d5 1445 rcu_read_lock();
195c234d
VB
1446
1447 /* Insert mask as temporary node to prevent concurrent creation of mask
1448 * with same key. Any concurrent lookups with same key will return
99815f50 1449 * -EAGAIN because mask's refcnt is zero.
195c234d
VB
1450 */
1451 fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
1452 &mask->ht_node,
1453 mask_ht_params);
05cd271f 1454 if (!fnew->mask) {
f48ef4d5
VB
1455 rcu_read_unlock();
1456
195c234d
VB
1457 if (fold) {
1458 ret = -EINVAL;
1459 goto errout_cleanup;
1460 }
77b9900e 1461
05cd271f 1462 newmask = fl_create_new_mask(head, mask);
195c234d
VB
1463 if (IS_ERR(newmask)) {
1464 ret = PTR_ERR(newmask);
1465 goto errout_cleanup;
1466 }
77b9900e 1467
05cd271f 1468 fnew->mask = newmask;
f48ef4d5 1469 return 0;
195c234d
VB
1470 } else if (IS_ERR(fnew->mask)) {
1471 ret = PTR_ERR(fnew->mask);
f6521c58 1472 } else if (fold && fold->mask != fnew->mask) {
f48ef4d5
VB
1473 ret = -EINVAL;
1474 } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
1475 /* Mask was deleted concurrently, try again */
1476 ret = -EAGAIN;
05cd271f 1477 }
f48ef4d5
VB
1478 rcu_read_unlock();
1479 return ret;
195c234d
VB
1480
1481errout_cleanup:
1482 rhashtable_remove_fast(&head->ht, &mask->ht_node,
1483 mask_ht_params);
195c234d 1484 return ret;
77b9900e
JP
1485}
1486
1487static int fl_set_parms(struct net *net, struct tcf_proto *tp,
1488 struct cls_fl_filter *f, struct fl_flow_mask *mask,
1489 unsigned long base, struct nlattr **tb,
50a56190 1490 struct nlattr *est, bool ovr,
c24e43d8 1491 struct fl_flow_tmplt *tmplt, bool rtnl_held,
50a56190 1492 struct netlink_ext_ack *extack)
77b9900e 1493{
77b9900e
JP
1494 int err;
1495
c24e43d8 1496 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, rtnl_held,
ec6743a1 1497 extack);
77b9900e
JP
1498 if (err < 0)
1499 return err;
1500
1501 if (tb[TCA_FLOWER_CLASSID]) {
1502 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
c24e43d8
VB
1503 if (!rtnl_held)
1504 rtnl_lock();
77b9900e 1505 tcf_bind_filter(tp, &f->res, base);
c24e43d8
VB
1506 if (!rtnl_held)
1507 rtnl_unlock();
77b9900e
JP
1508 }
1509
1057c55f 1510 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
77b9900e 1511 if (err)
45507529 1512 return err;
77b9900e
JP
1513
1514 fl_mask_update_range(mask);
1515 fl_set_masked_key(&f->mkey, &f->key, mask);
1516
b95ec7eb
JP
1517 if (!fl_mask_fits_tmplt(tmplt, mask)) {
1518 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
1519 return -EINVAL;
1520 }
1521
77b9900e 1522 return 0;
77b9900e
JP
1523}
1524
1f17f774
VB
1525static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
1526 struct cls_fl_filter *fold,
1527 bool *in_ht)
1528{
1529 struct fl_flow_mask *mask = fnew->mask;
1530 int err;
1531
9e35552a
VB
1532 err = rhashtable_lookup_insert_fast(&mask->ht,
1533 &fnew->ht_node,
1534 mask->filter_ht_params);
1f17f774
VB
1535 if (err) {
1536 *in_ht = false;
1537 /* It is okay if filter with same key exists when
1538 * overwriting.
1539 */
1540 return fold && err == -EEXIST ? 0 : err;
1541 }
1542
1543 *in_ht = true;
1544 return 0;
1545}
1546
77b9900e
JP
1547static int fl_change(struct net *net, struct sk_buff *in_skb,
1548 struct tcf_proto *tp, unsigned long base,
1549 u32 handle, struct nlattr **tca,
12db03b6
VB
1550 void **arg, bool ovr, bool rtnl_held,
1551 struct netlink_ext_ack *extack)
77b9900e 1552{
e474619a 1553 struct cls_fl_head *head = fl_head_dereference(tp);
8113c095 1554 struct cls_fl_filter *fold = *arg;
77b9900e 1555 struct cls_fl_filter *fnew;
2cddd201 1556 struct fl_flow_mask *mask;
39b7b6a6 1557 struct nlattr **tb;
1f17f774 1558 bool in_ht;
77b9900e
JP
1559 int err;
1560
06177558
VB
1561 if (!tca[TCA_OPTIONS]) {
1562 err = -EINVAL;
1563 goto errout_fold;
1564 }
77b9900e 1565
2cddd201 1566 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
06177558
VB
1567 if (!mask) {
1568 err = -ENOBUFS;
1569 goto errout_fold;
1570 }
39b7b6a6 1571
2cddd201
IV
1572 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1573 if (!tb) {
1574 err = -ENOBUFS;
1575 goto errout_mask_alloc;
1576 }
1577
8cb08174
JB
1578 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
1579 tca[TCA_OPTIONS], fl_policy, NULL);
77b9900e 1580 if (err < 0)
39b7b6a6 1581 goto errout_tb;
77b9900e 1582
39b7b6a6
AB
1583 if (fold && handle && fold->handle != handle) {
1584 err = -EINVAL;
1585 goto errout_tb;
1586 }
77b9900e
JP
1587
1588 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
39b7b6a6
AB
1589 if (!fnew) {
1590 err = -ENOBUFS;
1591 goto errout_tb;
1592 }
c049d56e 1593 INIT_LIST_HEAD(&fnew->hw_list);
06177558 1594 refcount_set(&fnew->refcnt, 1);
77b9900e 1595
14215108 1596 err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0);
b9a24bb7
WC
1597 if (err < 0)
1598 goto errout;
77b9900e 1599
e69985c6
AV
1600 if (tb[TCA_FLOWER_FLAGS]) {
1601 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1602
1603 if (!tc_flags_valid(fnew->flags)) {
1604 err = -EINVAL;
ecb3dea4 1605 goto errout;
e69985c6
AV
1606 }
1607 }
5b33f488 1608
2cddd201 1609 err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr,
c24e43d8 1610 tp->chain->tmplt_priv, rtnl_held, extack);
77b9900e 1611 if (err)
ecb3dea4 1612 goto errout;
77b9900e 1613
2cddd201 1614 err = fl_check_assign_mask(head, fnew, fold, mask);
77b9900e 1615 if (err)
ecb3dea4
VB
1616 goto errout;
1617
1f17f774
VB
1618 err = fl_ht_insert_unique(fnew, fold, &in_ht);
1619 if (err)
1620 goto errout_mask;
1621
79685219 1622 if (!tc_skip_hw(fnew->flags)) {
c24e43d8 1623 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
79685219 1624 if (err)
1f17f774 1625 goto errout_ht;
79685219 1626 }
5b33f488 1627
55593960
OG
1628 if (!tc_in_hw(fnew->flags))
1629 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1630
3d81e711
VB
1631 spin_lock(&tp->lock);
1632
272ffaad
VB
1633 /* tp was deleted concurrently. -EAGAIN will cause caller to lookup
1634 * proto again or create new one, if necessary.
1635 */
1636 if (tp->deleting) {
1637 err = -EAGAIN;
1638 goto errout_hw;
1639 }
1640
5b33f488 1641 if (fold) {
b2552b8c
VB
1642 /* Fold filter was deleted concurrently. Retry lookup. */
1643 if (fold->deleted) {
1644 err = -EAGAIN;
1645 goto errout_hw;
1646 }
1647
620da486
VB
1648 fnew->handle = handle;
1649
1f17f774
VB
1650 if (!in_ht) {
1651 struct rhashtable_params params =
1652 fnew->mask->filter_ht_params;
1653
1654 err = rhashtable_insert_fast(&fnew->mask->ht,
1655 &fnew->ht_node,
1656 params);
1657 if (err)
1658 goto errout_hw;
1659 in_ht = true;
1660 }
620da486 1661
c049d56e 1662 refcount_inc(&fnew->refcnt);
599d2570
RD
1663 rhashtable_remove_fast(&fold->mask->ht,
1664 &fold->ht_node,
1665 fold->mask->filter_ht_params);
234a4624 1666 idr_replace(&head->handle_idr, fnew, fnew->handle);
ff3532f2 1667 list_replace_rcu(&fold->list, &fnew->list);
b2552b8c 1668 fold->deleted = true;
620da486 1669
3d81e711
VB
1670 spin_unlock(&tp->lock);
1671
9994677c 1672 fl_mask_put(head, fold->mask);
620da486 1673 if (!tc_skip_hw(fold->flags))
c24e43d8 1674 fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
77b9900e 1675 tcf_unbind_filter(tp, &fold->res);
06177558
VB
1676 /* Caller holds reference to fold, so refcnt is always > 0
1677 * after this.
1678 */
1679 refcount_dec(&fold->refcnt);
1680 __fl_put(fold);
77b9900e 1681 } else {
620da486
VB
1682 if (handle) {
1683 /* user specifies a handle and it doesn't exist */
1684 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1685 handle, GFP_ATOMIC);
9a2d9389
VB
1686
1687 /* Filter with specified handle was concurrently
1688 * inserted after initial check in cls_api. This is not
1689 * necessarily an error if NLM_F_EXCL is not set in
1690 * message flags. Returning EAGAIN will cause cls_api to
1691 * try to update concurrently inserted rule.
1692 */
1693 if (err == -ENOSPC)
1694 err = -EAGAIN;
620da486
VB
1695 } else {
1696 handle = 1;
1697 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1698 INT_MAX, GFP_ATOMIC);
1699 }
1700 if (err)
1701 goto errout_hw;
1702
c049d56e 1703 refcount_inc(&fnew->refcnt);
620da486 1704 fnew->handle = handle;
05cd271f 1705 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
3d81e711 1706 spin_unlock(&tp->lock);
77b9900e
JP
1707 }
1708
620da486
VB
1709 *arg = fnew;
1710
39b7b6a6 1711 kfree(tb);
99815f50 1712 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
77b9900e
JP
1713 return 0;
1714
c049d56e
VB
1715errout_ht:
1716 spin_lock(&tp->lock);
620da486 1717errout_hw:
c049d56e 1718 fnew->deleted = true;
3d81e711 1719 spin_unlock(&tp->lock);
620da486 1720 if (!tc_skip_hw(fnew->flags))
c24e43d8 1721 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
1f17f774
VB
1722 if (in_ht)
1723 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
1724 fnew->mask->filter_ht_params);
ecb3dea4 1725errout_mask:
9994677c 1726 fl_mask_put(head, fnew->mask);
77b9900e 1727errout:
c049d56e 1728 __fl_put(fnew);
39b7b6a6
AB
1729errout_tb:
1730 kfree(tb);
2cddd201 1731errout_mask_alloc:
99815f50 1732 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
06177558
VB
1733errout_fold:
1734 if (fold)
1735 __fl_put(fold);
77b9900e
JP
1736 return err;
1737}
1738
571acf21 1739static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
12db03b6 1740 bool rtnl_held, struct netlink_ext_ack *extack)
77b9900e 1741{
e474619a 1742 struct cls_fl_head *head = fl_head_dereference(tp);
8113c095 1743 struct cls_fl_filter *f = arg;
b2552b8c
VB
1744 bool last_on_mask;
1745 int err = 0;
77b9900e 1746
c24e43d8 1747 err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
05cd271f 1748 *last = list_empty(&head->masks);
06177558
VB
1749 __fl_put(f);
1750
b2552b8c 1751 return err;
77b9900e
JP
1752}
1753
12db03b6
VB
1754static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
1755 bool rtnl_held)
77b9900e 1756{
d39d7149
CW
1757 struct cls_fl_head *head = fl_head_dereference(tp);
1758 unsigned long id = arg->cookie, tmp;
77b9900e 1759 struct cls_fl_filter *f;
05cd271f 1760
01683a14
VB
1761 arg->count = arg->skip;
1762
d39d7149
CW
1763 idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) {
1764 /* don't return filters that are being deleted */
1765 if (!refcount_inc_not_zero(&f->refcnt))
1766 continue;
01683a14 1767 if (arg->fn(tp, f, arg) < 0) {
06177558 1768 __fl_put(f);
01683a14
VB
1769 arg->stop = 1;
1770 break;
05cd271f 1771 }
06177558 1772 __fl_put(f);
01683a14 1773 arg->count++;
77b9900e 1774 }
d39d7149 1775 arg->cookie = id;
77b9900e
JP
1776}
1777
c049d56e
VB
1778static struct cls_fl_filter *
1779fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
1780{
1781 struct cls_fl_head *head = fl_head_dereference(tp);
1782
1783 spin_lock(&tp->lock);
1784 if (list_empty(&head->hw_filters)) {
1785 spin_unlock(&tp->lock);
1786 return NULL;
1787 }
1788
1789 if (!f)
1790 f = list_entry(&head->hw_filters, struct cls_fl_filter,
1791 hw_list);
1792 list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
1793 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
1794 spin_unlock(&tp->lock);
1795 return f;
1796 }
1797 }
1798
1799 spin_unlock(&tp->lock);
1800 return NULL;
1801}
1802
a7323311 1803static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
31533cba
JH
1804 void *cb_priv, struct netlink_ext_ack *extack)
1805{
31533cba 1806 struct tcf_block *block = tp->chain->block;
f9e30088 1807 struct flow_cls_offload cls_flower = {};
c049d56e 1808 struct cls_fl_filter *f = NULL;
31533cba
JH
1809 int err;
1810
c049d56e
VB
1811 /* hw_filters list can only be changed by hw offload functions after
1812 * obtaining rtnl lock. Make sure it is not changed while reoffload is
1813 * iterating it.
1814 */
1815 ASSERT_RTNL();
3a7b6861 1816
c049d56e 1817 while ((f = fl_get_next_hw_filter(tp, f, add))) {
95e27a4d
JH
1818 cls_flower.rule =
1819 flow_rule_alloc(tcf_exts_num_actions(&f->exts));
1820 if (!cls_flower.rule) {
1821 __fl_put(f);
1822 return -ENOMEM;
1823 }
31533cba 1824
95e27a4d 1825 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
d6787147 1826 extack);
95e27a4d 1827 cls_flower.command = add ?
f9e30088 1828 FLOW_CLS_REPLACE : FLOW_CLS_DESTROY;
95e27a4d
JH
1829 cls_flower.cookie = (unsigned long)f;
1830 cls_flower.rule->match.dissector = &f->mask->dissector;
1831 cls_flower.rule->match.mask = &f->mask->key;
1832 cls_flower.rule->match.key = &f->mkey;
1833
1834 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
1835 if (err) {
8f256622 1836 kfree(cls_flower.rule);
95e27a4d
JH
1837 if (tc_skip_sw(f->flags)) {
1838 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
1839 __fl_put(f);
1840 return err;
31533cba 1841 }
95e27a4d
JH
1842 goto next_flow;
1843 }
31533cba 1844
95e27a4d
JH
1845 cls_flower.classid = f->res.classid;
1846
1847 err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1848 kfree(cls_flower.rule);
1849
1850 if (err) {
1851 if (add && tc_skip_sw(f->flags)) {
1852 __fl_put(f);
1853 return err;
1854 }
1855 goto next_flow;
31533cba 1856 }
95e27a4d
JH
1857
1858 spin_lock(&tp->lock);
1859 tc_cls_offload_cnt_update(block, &f->in_hw_count, &f->flags,
1860 add);
1861 spin_unlock(&tp->lock);
1862next_flow:
95e27a4d 1863 __fl_put(f);
31533cba
JH
1864 }
1865
1866 return 0;
1867}
1868
8f256622
PNA
1869static int fl_hw_create_tmplt(struct tcf_chain *chain,
1870 struct fl_flow_tmplt *tmplt)
34738452 1871{
f9e30088 1872 struct flow_cls_offload cls_flower = {};
34738452 1873 struct tcf_block *block = chain->block;
34738452 1874
e3ab786b 1875 cls_flower.rule = flow_rule_alloc(0);
8f256622
PNA
1876 if (!cls_flower.rule)
1877 return -ENOMEM;
1878
34738452 1879 cls_flower.common.chain_index = chain->index;
f9e30088 1880 cls_flower.command = FLOW_CLS_TMPLT_CREATE;
34738452 1881 cls_flower.cookie = (unsigned long) tmplt;
8f256622
PNA
1882 cls_flower.rule->match.dissector = &tmplt->dissector;
1883 cls_flower.rule->match.mask = &tmplt->mask;
1884 cls_flower.rule->match.key = &tmplt->dummy_key;
34738452
JP
1885
1886 /* We don't care if driver (any of them) fails to handle this
1887 * call. It serves just as a hint for it.
1888 */
aeb3fecd 1889 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
8f256622
PNA
1890 kfree(cls_flower.rule);
1891
1892 return 0;
34738452
JP
1893}
1894
1895static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
1896 struct fl_flow_tmplt *tmplt)
1897{
f9e30088 1898 struct flow_cls_offload cls_flower = {};
34738452
JP
1899 struct tcf_block *block = chain->block;
1900
1901 cls_flower.common.chain_index = chain->index;
f9e30088 1902 cls_flower.command = FLOW_CLS_TMPLT_DESTROY;
34738452
JP
1903 cls_flower.cookie = (unsigned long) tmplt;
1904
aeb3fecd 1905 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
34738452
JP
1906}
1907
b95ec7eb
JP
1908static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
1909 struct nlattr **tca,
1910 struct netlink_ext_ack *extack)
1911{
1912 struct fl_flow_tmplt *tmplt;
1913 struct nlattr **tb;
1914 int err;
1915
1916 if (!tca[TCA_OPTIONS])
1917 return ERR_PTR(-EINVAL);
1918
1919 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1920 if (!tb)
1921 return ERR_PTR(-ENOBUFS);
8cb08174
JB
1922 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
1923 tca[TCA_OPTIONS], fl_policy, NULL);
b95ec7eb
JP
1924 if (err)
1925 goto errout_tb;
1926
1927 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
1cbc36a5
DC
1928 if (!tmplt) {
1929 err = -ENOMEM;
b95ec7eb 1930 goto errout_tb;
1cbc36a5 1931 }
b95ec7eb
JP
1932 tmplt->chain = chain;
1933 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
1934 if (err)
1935 goto errout_tmplt;
b95ec7eb
JP
1936
1937 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
1938
8f256622
PNA
1939 err = fl_hw_create_tmplt(chain, tmplt);
1940 if (err)
1941 goto errout_tmplt;
34738452 1942
8f256622 1943 kfree(tb);
b95ec7eb
JP
1944 return tmplt;
1945
1946errout_tmplt:
1947 kfree(tmplt);
1948errout_tb:
1949 kfree(tb);
1950 return ERR_PTR(err);
1951}
1952
ec3ed293
VB
1953static void fl_tmplt_destroy(void *tmplt_priv)
1954{
1955 struct fl_flow_tmplt *tmplt = tmplt_priv;
1956
95278dda
CW
1957 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
1958 kfree(tmplt);
ec3ed293
VB
1959}
1960
77b9900e
JP
1961static int fl_dump_key_val(struct sk_buff *skb,
1962 void *val, int val_type,
1963 void *mask, int mask_type, int len)
1964{
1965 int err;
1966
1967 if (!memchr_inv(mask, 0, len))
1968 return 0;
1969 err = nla_put(skb, val_type, len, val);
1970 if (err)
1971 return err;
1972 if (mask_type != TCA_FLOWER_UNSPEC) {
1973 err = nla_put(skb, mask_type, len, mask);
1974 if (err)
1975 return err;
1976 }
1977 return 0;
1978}
1979
5c72299f
AN
1980static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
1981 struct fl_flow_key *mask)
1982{
1983 if (fl_dump_key_val(skb, &key->tp_min.dst, TCA_FLOWER_KEY_PORT_DST_MIN,
1984 &mask->tp_min.dst, TCA_FLOWER_UNSPEC,
1985 sizeof(key->tp_min.dst)) ||
1986 fl_dump_key_val(skb, &key->tp_max.dst, TCA_FLOWER_KEY_PORT_DST_MAX,
1987 &mask->tp_max.dst, TCA_FLOWER_UNSPEC,
1988 sizeof(key->tp_max.dst)) ||
1989 fl_dump_key_val(skb, &key->tp_min.src, TCA_FLOWER_KEY_PORT_SRC_MIN,
1990 &mask->tp_min.src, TCA_FLOWER_UNSPEC,
1991 sizeof(key->tp_min.src)) ||
1992 fl_dump_key_val(skb, &key->tp_max.src, TCA_FLOWER_KEY_PORT_SRC_MAX,
1993 &mask->tp_max.src, TCA_FLOWER_UNSPEC,
1994 sizeof(key->tp_max.src)))
1995 return -1;
1996
1997 return 0;
1998}
1999
a577d8f7
BL
2000static int fl_dump_key_mpls(struct sk_buff *skb,
2001 struct flow_dissector_key_mpls *mpls_key,
2002 struct flow_dissector_key_mpls *mpls_mask)
2003{
2004 int err;
2005
2006 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
2007 return 0;
2008 if (mpls_mask->mpls_ttl) {
2009 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
2010 mpls_key->mpls_ttl);
2011 if (err)
2012 return err;
2013 }
2014 if (mpls_mask->mpls_tc) {
2015 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
2016 mpls_key->mpls_tc);
2017 if (err)
2018 return err;
2019 }
2020 if (mpls_mask->mpls_label) {
2021 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
2022 mpls_key->mpls_label);
2023 if (err)
2024 return err;
2025 }
2026 if (mpls_mask->mpls_bos) {
2027 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
2028 mpls_key->mpls_bos);
2029 if (err)
2030 return err;
2031 }
2032 return 0;
2033}
2034
0e2c17b6 2035static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
4d80cc0a
OG
2036 struct flow_dissector_key_ip *key,
2037 struct flow_dissector_key_ip *mask)
2038{
0e2c17b6
OG
2039 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
2040 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
2041 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
2042 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
2043
2044 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
2045 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
4d80cc0a
OG
2046 return -1;
2047
2048 return 0;
2049}
2050
9399ae9a 2051static int fl_dump_key_vlan(struct sk_buff *skb,
d64efd09 2052 int vlan_id_key, int vlan_prio_key,
9399ae9a
HHZ
2053 struct flow_dissector_key_vlan *vlan_key,
2054 struct flow_dissector_key_vlan *vlan_mask)
2055{
2056 int err;
2057
2058 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
2059 return 0;
2060 if (vlan_mask->vlan_id) {
d64efd09 2061 err = nla_put_u16(skb, vlan_id_key,
9399ae9a
HHZ
2062 vlan_key->vlan_id);
2063 if (err)
2064 return err;
2065 }
2066 if (vlan_mask->vlan_priority) {
d64efd09 2067 err = nla_put_u8(skb, vlan_prio_key,
9399ae9a
HHZ
2068 vlan_key->vlan_priority);
2069 if (err)
2070 return err;
2071 }
2072 return 0;
2073}
2074
faa3ffce
OG
2075static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
2076 u32 *flower_key, u32 *flower_mask,
2077 u32 flower_flag_bit, u32 dissector_flag_bit)
2078{
2079 if (dissector_mask & dissector_flag_bit) {
2080 *flower_mask |= flower_flag_bit;
2081 if (dissector_key & dissector_flag_bit)
2082 *flower_key |= flower_flag_bit;
2083 }
2084}
2085
2086static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
2087{
2088 u32 key, mask;
2089 __be32 _key, _mask;
2090 int err;
2091
2092 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
2093 return 0;
2094
2095 key = 0;
2096 mask = 0;
2097
2098 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2099 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
459d153d
PJV
2100 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2101 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
2102 FLOW_DIS_FIRST_FRAG);
faa3ffce
OG
2103
2104 _key = cpu_to_be32(key);
2105 _mask = cpu_to_be32(mask);
2106
2107 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
2108 if (err)
2109 return err;
2110
2111 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
2112}
2113
0a6e7778
PJV
2114static int fl_dump_key_geneve_opt(struct sk_buff *skb,
2115 struct flow_dissector_key_enc_opts *enc_opts)
2116{
2117 struct geneve_opt *opt;
2118 struct nlattr *nest;
2119 int opt_off = 0;
2120
ae0be8de 2121 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
0a6e7778
PJV
2122 if (!nest)
2123 goto nla_put_failure;
2124
2125 while (enc_opts->len > opt_off) {
2126 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
2127
2128 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
2129 opt->opt_class))
2130 goto nla_put_failure;
2131 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
2132 opt->type))
2133 goto nla_put_failure;
2134 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
2135 opt->length * 4, opt->opt_data))
2136 goto nla_put_failure;
2137
2138 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
2139 }
2140 nla_nest_end(skb, nest);
2141 return 0;
2142
2143nla_put_failure:
2144 nla_nest_cancel(skb, nest);
2145 return -EMSGSIZE;
2146}
2147
e0ace68a
PB
2148static int fl_dump_key_ct(struct sk_buff *skb,
2149 struct flow_dissector_key_ct *key,
2150 struct flow_dissector_key_ct *mask)
2151{
2152 if (IS_ENABLED(CONFIG_NF_CONNTRACK) &&
2153 fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
2154 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
2155 sizeof(key->ct_state)))
2156 goto nla_put_failure;
2157
2158 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
2159 fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
2160 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
2161 sizeof(key->ct_zone)))
2162 goto nla_put_failure;
2163
2164 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
2165 fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
2166 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
2167 sizeof(key->ct_mark)))
2168 goto nla_put_failure;
2169
2170 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
2171 fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
2172 &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
2173 sizeof(key->ct_labels)))
2174 goto nla_put_failure;
2175
2176 return 0;
2177
2178nla_put_failure:
2179 return -EMSGSIZE;
2180}
2181
0a6e7778
PJV
2182static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
2183 struct flow_dissector_key_enc_opts *enc_opts)
2184{
2185 struct nlattr *nest;
2186 int err;
2187
2188 if (!enc_opts->len)
2189 return 0;
2190
ae0be8de 2191 nest = nla_nest_start_noflag(skb, enc_opt_type);
0a6e7778
PJV
2192 if (!nest)
2193 goto nla_put_failure;
2194
2195 switch (enc_opts->dst_opt_type) {
2196 case TUNNEL_GENEVE_OPT:
2197 err = fl_dump_key_geneve_opt(skb, enc_opts);
2198 if (err)
2199 goto nla_put_failure;
2200 break;
2201 default:
2202 goto nla_put_failure;
2203 }
2204 nla_nest_end(skb, nest);
2205 return 0;
2206
2207nla_put_failure:
2208 nla_nest_cancel(skb, nest);
2209 return -EMSGSIZE;
2210}
2211
2212static int fl_dump_key_enc_opt(struct sk_buff *skb,
2213 struct flow_dissector_key_enc_opts *key_opts,
2214 struct flow_dissector_key_enc_opts *msk_opts)
2215{
2216 int err;
2217
2218 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
2219 if (err)
2220 return err;
2221
2222 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
2223}
2224
f5749081
JP
2225static int fl_dump_key(struct sk_buff *skb, struct net *net,
2226 struct fl_flow_key *key, struct fl_flow_key *mask)
77b9900e 2227{
8212ed77 2228 if (mask->meta.ingress_ifindex) {
77b9900e
JP
2229 struct net_device *dev;
2230
8212ed77 2231 dev = __dev_get_by_index(net, key->meta.ingress_ifindex);
77b9900e
JP
2232 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
2233 goto nla_put_failure;
2234 }
2235
2236 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
2237 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
2238 sizeof(key->eth.dst)) ||
2239 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
2240 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
2241 sizeof(key->eth.src)) ||
2242 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
2243 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
2244 sizeof(key->basic.n_proto)))
2245 goto nla_put_failure;
9399ae9a 2246
a577d8f7
BL
2247 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
2248 goto nla_put_failure;
2249
d64efd09
JL
2250 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
2251 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
9399ae9a
HHZ
2252 goto nla_put_failure;
2253
d64efd09
JL
2254 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
2255 TCA_FLOWER_KEY_CVLAN_PRIO,
2256 &key->cvlan, &mask->cvlan) ||
2257 (mask->cvlan.vlan_tpid &&
158abbf1
JL
2258 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2259 key->cvlan.vlan_tpid)))
d3069512
JL
2260 goto nla_put_failure;
2261
5e9a0fe4
JL
2262 if (mask->basic.n_proto) {
2263 if (mask->cvlan.vlan_tpid) {
2264 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
2265 key->basic.n_proto))
2266 goto nla_put_failure;
2267 } else if (mask->vlan.vlan_tpid) {
2268 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2269 key->basic.n_proto))
2270 goto nla_put_failure;
2271 }
d64efd09
JL
2272 }
2273
77b9900e
JP
2274 if ((key->basic.n_proto == htons(ETH_P_IP) ||
2275 key->basic.n_proto == htons(ETH_P_IPV6)) &&
4d80cc0a 2276 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
77b9900e 2277 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
4d80cc0a 2278 sizeof(key->basic.ip_proto)) ||
0e2c17b6 2279 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
77b9900e
JP
2280 goto nla_put_failure;
2281
c3f83241 2282 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
77b9900e
JP
2283 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
2284 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
2285 sizeof(key->ipv4.src)) ||
2286 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
2287 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
2288 sizeof(key->ipv4.dst))))
2289 goto nla_put_failure;
c3f83241 2290 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
77b9900e
JP
2291 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
2292 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
2293 sizeof(key->ipv6.src)) ||
2294 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
2295 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
2296 sizeof(key->ipv6.dst))))
2297 goto nla_put_failure;
2298
2299 if (key->basic.ip_proto == IPPROTO_TCP &&
2300 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
aa72d708 2301 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
77b9900e
JP
2302 sizeof(key->tp.src)) ||
2303 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
aa72d708 2304 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
fdfc7dd6
JP
2305 sizeof(key->tp.dst)) ||
2306 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
2307 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
2308 sizeof(key->tcp.flags))))
77b9900e
JP
2309 goto nla_put_failure;
2310 else if (key->basic.ip_proto == IPPROTO_UDP &&
2311 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
aa72d708 2312 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
77b9900e
JP
2313 sizeof(key->tp.src)) ||
2314 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
aa72d708 2315 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
5976c5f4
SH
2316 sizeof(key->tp.dst))))
2317 goto nla_put_failure;
2318 else if (key->basic.ip_proto == IPPROTO_SCTP &&
2319 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
2320 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
2321 sizeof(key->tp.src)) ||
2322 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
2323 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
77b9900e
JP
2324 sizeof(key->tp.dst))))
2325 goto nla_put_failure;
7b684884
SH
2326 else if (key->basic.n_proto == htons(ETH_P_IP) &&
2327 key->basic.ip_proto == IPPROTO_ICMP &&
2328 (fl_dump_key_val(skb, &key->icmp.type,
2329 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
2330 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
2331 sizeof(key->icmp.type)) ||
2332 fl_dump_key_val(skb, &key->icmp.code,
2333 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
2334 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
2335 sizeof(key->icmp.code))))
2336 goto nla_put_failure;
2337 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
2338 key->basic.ip_proto == IPPROTO_ICMPV6 &&
2339 (fl_dump_key_val(skb, &key->icmp.type,
2340 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
2341 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
2342 sizeof(key->icmp.type)) ||
2343 fl_dump_key_val(skb, &key->icmp.code,
2344 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
2345 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
2346 sizeof(key->icmp.code))))
2347 goto nla_put_failure;
99d31326
SH
2348 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
2349 key->basic.n_proto == htons(ETH_P_RARP)) &&
2350 (fl_dump_key_val(skb, &key->arp.sip,
2351 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
2352 TCA_FLOWER_KEY_ARP_SIP_MASK,
2353 sizeof(key->arp.sip)) ||
2354 fl_dump_key_val(skb, &key->arp.tip,
2355 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
2356 TCA_FLOWER_KEY_ARP_TIP_MASK,
2357 sizeof(key->arp.tip)) ||
2358 fl_dump_key_val(skb, &key->arp.op,
2359 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
2360 TCA_FLOWER_KEY_ARP_OP_MASK,
2361 sizeof(key->arp.op)) ||
2362 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
2363 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
2364 sizeof(key->arp.sha)) ||
2365 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
2366 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
2367 sizeof(key->arp.tha))))
2368 goto nla_put_failure;
77b9900e 2369
5c72299f
AN
2370 if ((key->basic.ip_proto == IPPROTO_TCP ||
2371 key->basic.ip_proto == IPPROTO_UDP ||
2372 key->basic.ip_proto == IPPROTO_SCTP) &&
2373 fl_dump_key_port_range(skb, key, mask))
2374 goto nla_put_failure;
2375
bc3103f1
AV
2376 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
2377 (fl_dump_key_val(skb, &key->enc_ipv4.src,
2378 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
2379 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
2380 sizeof(key->enc_ipv4.src)) ||
2381 fl_dump_key_val(skb, &key->enc_ipv4.dst,
2382 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
2383 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
2384 sizeof(key->enc_ipv4.dst))))
2385 goto nla_put_failure;
2386 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
2387 (fl_dump_key_val(skb, &key->enc_ipv6.src,
2388 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
2389 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
2390 sizeof(key->enc_ipv6.src)) ||
2391 fl_dump_key_val(skb, &key->enc_ipv6.dst,
2392 TCA_FLOWER_KEY_ENC_IPV6_DST,
2393 &mask->enc_ipv6.dst,
2394 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
2395 sizeof(key->enc_ipv6.dst))))
2396 goto nla_put_failure;
2397
2398 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
eb523f42 2399 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
f4d997fd
HHZ
2400 sizeof(key->enc_key_id)) ||
2401 fl_dump_key_val(skb, &key->enc_tp.src,
2402 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
2403 &mask->enc_tp.src,
2404 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
2405 sizeof(key->enc_tp.src)) ||
2406 fl_dump_key_val(skb, &key->enc_tp.dst,
2407 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
2408 &mask->enc_tp.dst,
2409 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
0e2c17b6 2410 sizeof(key->enc_tp.dst)) ||
0a6e7778
PJV
2411 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
2412 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
bc3103f1
AV
2413 goto nla_put_failure;
2414
e0ace68a
PB
2415 if (fl_dump_key_ct(skb, &key->ct, &mask->ct))
2416 goto nla_put_failure;
2417
faa3ffce
OG
2418 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
2419 goto nla_put_failure;
2420
f5749081
JP
2421 return 0;
2422
2423nla_put_failure:
2424 return -EMSGSIZE;
2425}
2426
2427static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
12db03b6 2428 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
f5749081
JP
2429{
2430 struct cls_fl_filter *f = fh;
2431 struct nlattr *nest;
2432 struct fl_flow_key *key, *mask;
3d81e711 2433 bool skip_hw;
f5749081
JP
2434
2435 if (!f)
2436 return skb->len;
2437
2438 t->tcm_handle = f->handle;
2439
ae0be8de 2440 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
f5749081
JP
2441 if (!nest)
2442 goto nla_put_failure;
2443
3d81e711
VB
2444 spin_lock(&tp->lock);
2445
f5749081
JP
2446 if (f->res.classid &&
2447 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
3d81e711 2448 goto nla_put_failure_locked;
f5749081
JP
2449
2450 key = &f->key;
2451 mask = &f->mask->key;
3d81e711 2452 skip_hw = tc_skip_hw(f->flags);
f5749081
JP
2453
2454 if (fl_dump_key(skb, net, key, mask))
3d81e711 2455 goto nla_put_failure_locked;
f5749081 2456
749e6720 2457 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3d81e711
VB
2458 goto nla_put_failure_locked;
2459
2460 spin_unlock(&tp->lock);
2461
2462 if (!skip_hw)
c24e43d8 2463 fl_hw_update_stats(tp, f, rtnl_held);
e69985c6 2464
86c55361
VB
2465 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
2466 goto nla_put_failure;
2467
77b9900e
JP
2468 if (tcf_exts_dump(skb, &f->exts))
2469 goto nla_put_failure;
2470
2471 nla_nest_end(skb, nest);
2472
2473 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
2474 goto nla_put_failure;
2475
2476 return skb->len;
2477
3d81e711
VB
2478nla_put_failure_locked:
2479 spin_unlock(&tp->lock);
77b9900e
JP
2480nla_put_failure:
2481 nla_nest_cancel(skb, nest);
2482 return -1;
2483}
2484
b95ec7eb
JP
2485static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
2486{
2487 struct fl_flow_tmplt *tmplt = tmplt_priv;
2488 struct fl_flow_key *key, *mask;
2489 struct nlattr *nest;
2490
ae0be8de 2491 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
b95ec7eb
JP
2492 if (!nest)
2493 goto nla_put_failure;
2494
2495 key = &tmplt->dummy_key;
2496 mask = &tmplt->mask;
2497
2498 if (fl_dump_key(skb, net, key, mask))
2499 goto nla_put_failure;
2500
2501 nla_nest_end(skb, nest);
2502
2503 return skb->len;
2504
2505nla_put_failure:
2506 nla_nest_cancel(skb, nest);
2507 return -EMSGSIZE;
2508}
2509
07d79fc7
CW
2510static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
2511{
2512 struct cls_fl_filter *f = fh;
2513
2514 if (f && f->res.classid == classid)
2515 f->res.class = cl;
2516}
2517
77b9900e
JP
2518static struct tcf_proto_ops cls_fl_ops __read_mostly = {
2519 .kind = "flower",
2520 .classify = fl_classify,
2521 .init = fl_init,
2522 .destroy = fl_destroy,
2523 .get = fl_get,
06177558 2524 .put = fl_put,
77b9900e
JP
2525 .change = fl_change,
2526 .delete = fl_delete,
2527 .walk = fl_walk,
31533cba 2528 .reoffload = fl_reoffload,
77b9900e 2529 .dump = fl_dump,
07d79fc7 2530 .bind_class = fl_bind_class,
b95ec7eb
JP
2531 .tmplt_create = fl_tmplt_create,
2532 .tmplt_destroy = fl_tmplt_destroy,
2533 .tmplt_dump = fl_tmplt_dump,
77b9900e 2534 .owner = THIS_MODULE,
92149190 2535 .flags = TCF_PROTO_OPS_DOIT_UNLOCKED,
77b9900e
JP
2536};
2537
2538static int __init cls_fl_init(void)
2539{
2540 return register_tcf_proto_ops(&cls_fl_ops);
2541}
2542
2543static void __exit cls_fl_exit(void)
2544{
2545 unregister_tcf_proto_ops(&cls_fl_ops);
2546}
2547
2548module_init(cls_fl_init);
2549module_exit(cls_fl_exit);
2550
2551MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
2552MODULE_DESCRIPTION("Flower classifier");
2553MODULE_LICENSE("GPL v2");