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