Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / net / sched / cls_flower.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
77b9900e
JP
2/*
3 * net/sched/cls_flower.c Flower classifier
4 *
5 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
77b9900e
JP
6 */
7
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/rhashtable.h>
d9363774 12#include <linux/workqueue.h>
06177558 13#include <linux/refcount.h>
7cfffd5f 14#include <linux/bitfield.h>
77b9900e
JP
15
16#include <linux/if_ether.h>
17#include <linux/in6.h>
18#include <linux/ip.h>
a577d8f7 19#include <linux/mpls.h>
5008750e 20#include <linux/ppp_defs.h>
77b9900e
JP
21
22#include <net/sch_generic.h>
23#include <net/pkt_cls.h>
ec624fe7 24#include <net/pkt_sched.h>
77b9900e
JP
25#include <net/ip.h>
26#include <net/flow_dissector.h>
0a6e7778 27#include <net/geneve.h>
d8f9dfae 28#include <net/vxlan.h>
79b1011c 29#include <net/erspan.h>
e3acda7a 30#include <net/gtp.h>
6dd514f4 31#include <net/pfcp.h>
9f3101dc 32#include <net/tc_wrapper.h>
77b9900e 33
bc3103f1
AV
34#include <net/dst.h>
35#include <net/dst_metadata.h>
36
e0ace68a
PB
37#include <uapi/linux/netfilter/nf_conntrack_common.h>
38
1bcc51ac 39#define TCA_FLOWER_KEY_CT_FLAGS_MAX \
40 ((__TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) << 1)
41#define TCA_FLOWER_KEY_CT_FLAGS_MASK \
42 (TCA_FLOWER_KEY_CT_FLAGS_MAX - 1)
43
0e83a787
AST
44#define TCA_FLOWER_KEY_FLAGS_POLICY_MASK \
45 (TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT | \
46 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST)
47
11036bd7
AST
48#define TCA_FLOWER_KEY_ENC_FLAGS_POLICY_MASK \
49 (TCA_FLOWER_KEY_FLAGS_TUNNEL_CSUM | \
50 TCA_FLOWER_KEY_FLAGS_TUNNEL_DONT_FRAGMENT | \
51 TCA_FLOWER_KEY_FLAGS_TUNNEL_OAM | \
52 TCA_FLOWER_KEY_FLAGS_TUNNEL_CRIT_OPT)
1d17568e 53
77b9900e 54struct fl_flow_key {
8212ed77 55 struct flow_dissector_key_meta meta;
42aecaa9 56 struct flow_dissector_key_control control;
bc3103f1 57 struct flow_dissector_key_control enc_control;
77b9900e
JP
58 struct flow_dissector_key_basic basic;
59 struct flow_dissector_key_eth_addrs eth;
9399ae9a 60 struct flow_dissector_key_vlan vlan;
d64efd09 61 struct flow_dissector_key_vlan cvlan;
77b9900e 62 union {
c3f83241 63 struct flow_dissector_key_ipv4_addrs ipv4;
77b9900e
JP
64 struct flow_dissector_key_ipv6_addrs ipv6;
65 };
66 struct flow_dissector_key_ports tp;
7b684884 67 struct flow_dissector_key_icmp icmp;
99d31326 68 struct flow_dissector_key_arp arp;
bc3103f1
AV
69 struct flow_dissector_key_keyid enc_key_id;
70 union {
71 struct flow_dissector_key_ipv4_addrs enc_ipv4;
72 struct flow_dissector_key_ipv6_addrs enc_ipv6;
73 };
f4d997fd 74 struct flow_dissector_key_ports enc_tp;
a577d8f7 75 struct flow_dissector_key_mpls mpls;
fdfc7dd6 76 struct flow_dissector_key_tcp tcp;
4d80cc0a 77 struct flow_dissector_key_ip ip;
0e2c17b6 78 struct flow_dissector_key_ip enc_ip;
0a6e7778 79 struct flow_dissector_key_enc_opts enc_opts;
83d85bb0 80 struct flow_dissector_key_ports_range tp_range;
e0ace68a 81 struct flow_dissector_key_ct ct;
5923b8f7 82 struct flow_dissector_key_hash hash;
b4000312 83 struct flow_dissector_key_num_of_vlans num_of_vlans;
5008750e 84 struct flow_dissector_key_pppoe pppoe;
8b189ea0 85 struct flow_dissector_key_l2tpv3 l2tpv3;
4c13eda7 86 struct flow_dissector_key_ipsec ipsec;
7cfffd5f 87 struct flow_dissector_key_cfm cfm;
77b9900e
JP
88} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
89
90struct fl_flow_mask_range {
91 unsigned short int start;
92 unsigned short int end;
93};
94
95struct fl_flow_mask {
96 struct fl_flow_key key;
97 struct fl_flow_mask_range range;
5c72299f 98 u32 flags;
05cd271f
PB
99 struct rhash_head ht_node;
100 struct rhashtable ht;
101 struct rhashtable_params filter_ht_params;
102 struct flow_dissector dissector;
103 struct list_head filters;
44a5cd43 104 struct rcu_work rwork;
05cd271f 105 struct list_head list;
f48ef4d5 106 refcount_t refcnt;
77b9900e
JP
107};
108
b95ec7eb
JP
109struct fl_flow_tmplt {
110 struct fl_flow_key dummy_key;
111 struct fl_flow_key mask;
112 struct flow_dissector dissector;
113 struct tcf_chain *chain;
114};
115
77b9900e
JP
116struct cls_fl_head {
117 struct rhashtable ht;
259e60f9 118 spinlock_t masks_lock; /* Protect masks list */
05cd271f 119 struct list_head masks;
c049d56e 120 struct list_head hw_filters;
aaa908ff 121 struct rcu_work rwork;
c15ab236 122 struct idr handle_idr;
77b9900e
JP
123};
124
125struct cls_fl_filter {
05cd271f 126 struct fl_flow_mask *mask;
77b9900e
JP
127 struct rhash_head ht_node;
128 struct fl_flow_key mkey;
129 struct tcf_exts exts;
130 struct tcf_result res;
131 struct fl_flow_key key;
132 struct list_head list;
c049d56e 133 struct list_head hw_list;
77b9900e 134 u32 handle;
e69985c6 135 u32 flags;
86c55361 136 u32 in_hw_count;
1a432018 137 u8 needs_tc_skb_ext:1;
aaa908ff 138 struct rcu_work rwork;
7091d8c7 139 struct net_device *hw_dev;
06177558
VB
140 /* Flower classifier is unlocked, which means that its reference counter
141 * can be changed concurrently without any kind of external
142 * synchronization. Use atomic reference counter to be concurrency-safe.
143 */
144 refcount_t refcnt;
b2552b8c 145 bool deleted;
77b9900e
JP
146};
147
05cd271f
PB
148static const struct rhashtable_params mask_ht_params = {
149 .key_offset = offsetof(struct fl_flow_mask, key),
150 .key_len = sizeof(struct fl_flow_key),
151 .head_offset = offsetof(struct fl_flow_mask, ht_node),
152 .automatic_shrinking = true,
153};
154
77b9900e
JP
155static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
156{
157 return mask->range.end - mask->range.start;
158}
159
160static void fl_mask_update_range(struct fl_flow_mask *mask)
161{
162 const u8 *bytes = (const u8 *) &mask->key;
163 size_t size = sizeof(mask->key);
05cd271f 164 size_t i, first = 0, last;
77b9900e 165
05cd271f
PB
166 for (i = 0; i < size; i++) {
167 if (bytes[i]) {
168 first = i;
169 break;
170 }
171 }
172 last = first;
173 for (i = size - 1; i != first; i--) {
77b9900e 174 if (bytes[i]) {
77b9900e 175 last = i;
05cd271f 176 break;
77b9900e
JP
177 }
178 }
179 mask->range.start = rounddown(first, sizeof(long));
180 mask->range.end = roundup(last + 1, sizeof(long));
181}
182
183static void *fl_key_get_start(struct fl_flow_key *key,
184 const struct fl_flow_mask *mask)
185{
186 return (u8 *) key + mask->range.start;
187}
188
189static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
190 struct fl_flow_mask *mask)
191{
192 const long *lkey = fl_key_get_start(key, mask);
193 const long *lmask = fl_key_get_start(&mask->key, mask);
194 long *lmkey = fl_key_get_start(mkey, mask);
195 int i;
196
197 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
198 *lmkey++ = *lkey++ & *lmask++;
199}
200
b95ec7eb
JP
201static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
202 struct fl_flow_mask *mask)
203{
204 const long *lmask = fl_key_get_start(&mask->key, mask);
205 const long *ltmplt;
206 int i;
207
208 if (!tmplt)
209 return true;
210 ltmplt = fl_key_get_start(&tmplt->mask, mask);
211 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
212 if (~*ltmplt++ & *lmask++)
213 return false;
214 }
215 return true;
216}
217
77b9900e
JP
218static void fl_clear_masked_range(struct fl_flow_key *key,
219 struct fl_flow_mask *mask)
220{
221 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
222}
223
5c72299f
AN
224static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
225 struct fl_flow_key *key,
226 struct fl_flow_key *mkey)
227{
6215afcb 228 u16 min_mask, max_mask, min_val, max_val;
5c72299f 229
6215afcb
VO
230 min_mask = ntohs(filter->mask->key.tp_range.tp_min.dst);
231 max_mask = ntohs(filter->mask->key.tp_range.tp_max.dst);
232 min_val = ntohs(filter->key.tp_range.tp_min.dst);
233 max_val = ntohs(filter->key.tp_range.tp_max.dst);
5c72299f
AN
234
235 if (min_mask && max_mask) {
6215afcb
VO
236 if (ntohs(key->tp_range.tp.dst) < min_val ||
237 ntohs(key->tp_range.tp.dst) > max_val)
5c72299f
AN
238 return false;
239
240 /* skb does not have min and max values */
8ffb055b
YK
241 mkey->tp_range.tp_min.dst = filter->mkey.tp_range.tp_min.dst;
242 mkey->tp_range.tp_max.dst = filter->mkey.tp_range.tp_max.dst;
5c72299f
AN
243 }
244 return true;
245}
246
247static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
248 struct fl_flow_key *key,
249 struct fl_flow_key *mkey)
250{
6215afcb 251 u16 min_mask, max_mask, min_val, max_val;
5c72299f 252
6215afcb
VO
253 min_mask = ntohs(filter->mask->key.tp_range.tp_min.src);
254 max_mask = ntohs(filter->mask->key.tp_range.tp_max.src);
255 min_val = ntohs(filter->key.tp_range.tp_min.src);
256 max_val = ntohs(filter->key.tp_range.tp_max.src);
5c72299f
AN
257
258 if (min_mask && max_mask) {
6215afcb
VO
259 if (ntohs(key->tp_range.tp.src) < min_val ||
260 ntohs(key->tp_range.tp.src) > max_val)
5c72299f
AN
261 return false;
262
263 /* skb does not have min and max values */
8ffb055b
YK
264 mkey->tp_range.tp_min.src = filter->mkey.tp_range.tp_min.src;
265 mkey->tp_range.tp_max.src = filter->mkey.tp_range.tp_max.src;
5c72299f
AN
266 }
267 return true;
268}
269
270static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
271 struct fl_flow_key *mkey)
a3308d8f 272{
05cd271f
PB
273 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
274 mask->filter_ht_params);
a3308d8f
PB
275}
276
5c72299f
AN
277static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
278 struct fl_flow_key *mkey,
279 struct fl_flow_key *key)
280{
281 struct cls_fl_filter *filter, *f;
282
283 list_for_each_entry_rcu(filter, &mask->filters, list) {
284 if (!fl_range_port_dst_cmp(filter, key, mkey))
285 continue;
286
287 if (!fl_range_port_src_cmp(filter, key, mkey))
288 continue;
289
290 f = __fl_lookup(mask, mkey);
291 if (f)
292 return f;
293 }
294 return NULL;
295}
296
0af413bd
AB
297static noinline_for_stack
298struct cls_fl_filter *fl_mask_lookup(struct fl_flow_mask *mask, struct fl_flow_key *key)
5c72299f 299{
0af413bd
AB
300 struct fl_flow_key mkey;
301
302 fl_set_masked_key(&mkey, key, mask);
5c72299f 303 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
0af413bd 304 return fl_lookup_range(mask, &mkey, key);
5c72299f 305
0af413bd 306 return __fl_lookup(mask, &mkey);
5c72299f
AN
307}
308
e0ace68a
PB
309static u16 fl_ct_info_to_flower_map[] = {
310 [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
311 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
312 [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
313 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
314 [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
8c85d18c
PB
315 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED |
316 TCA_FLOWER_KEY_CT_FLAGS_REPLY,
e0ace68a 317 [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
8c85d18c
PB
318 TCA_FLOWER_KEY_CT_FLAGS_RELATED |
319 TCA_FLOWER_KEY_CT_FLAGS_REPLY,
e0ace68a
PB
320 [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
321 TCA_FLOWER_KEY_CT_FLAGS_NEW,
322};
323
9f3101dc
PT
324TC_INDIRECT_SCOPE int fl_classify(struct sk_buff *skb,
325 const struct tcf_proto *tp,
326 struct tcf_result *res)
77b9900e
JP
327{
328 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
ec624fe7 329 bool post_ct = tc_skb_cb(skb)->post_ct;
38495958 330 u16 zone = tc_skb_cb(skb)->zone;
e0ace68a
PB
331 struct fl_flow_key skb_key;
332 struct fl_flow_mask *mask;
333 struct cls_fl_filter *f;
77b9900e 334
05cd271f 335 list_for_each_entry_rcu(mask, &head->masks, list) {
8a9093c7 336 flow_dissector_init_keys(&skb_key.control, &skb_key.basic);
05cd271f 337 fl_clear_masked_range(&skb_key, mask);
bc3103f1 338
8212ed77 339 skb_flow_dissect_meta(skb, &mask->dissector, &skb_key);
05cd271f
PB
340 /* skb_flow_dissect() does not set n_proto in case an unknown
341 * protocol, so do it rather here.
342 */
d7bf2ebe 343 skb_key.basic.n_proto = skb_protocol(skb, false);
05cd271f 344 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
e0ace68a
PB
345 skb_flow_dissect_ct(skb, &mask->dissector, &skb_key,
346 fl_ct_info_to_flower_map,
7baf2429 347 ARRAY_SIZE(fl_ct_info_to_flower_map),
38495958 348 post_ct, zone);
5923b8f7 349 skb_flow_dissect_hash(skb, &mask->dissector, &skb_key);
6de6e46d
YK
350 skb_flow_dissect(skb, &mask->dissector, &skb_key,
351 FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP);
77b9900e 352
0af413bd 353 f = fl_mask_lookup(mask, &skb_key);
05cd271f
PB
354 if (f && !tc_skip_sw(f->flags)) {
355 *res = f->res;
356 return tcf_exts_exec(skb, &f->exts, res);
357 }
77b9900e
JP
358 }
359 return -1;
360}
361
362static int fl_init(struct tcf_proto *tp)
363{
364 struct cls_fl_head *head;
365
366 head = kzalloc(sizeof(*head), GFP_KERNEL);
367 if (!head)
368 return -ENOBUFS;
369
259e60f9 370 spin_lock_init(&head->masks_lock);
05cd271f 371 INIT_LIST_HEAD_RCU(&head->masks);
c049d56e 372 INIT_LIST_HEAD(&head->hw_filters);
77b9900e 373 rcu_assign_pointer(tp->root, head);
c15ab236 374 idr_init(&head->handle_idr);
77b9900e 375
05cd271f
PB
376 return rhashtable_init(&head->ht, &mask_ht_params);
377}
378
99815f50 379static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done)
44a5cd43 380{
99815f50
VB
381 /* temporary masks don't have their filters list and ht initialized */
382 if (mask_init_done) {
383 WARN_ON(!list_empty(&mask->filters));
384 rhashtable_destroy(&mask->ht);
385 }
44a5cd43
PA
386 kfree(mask);
387}
388
389static void fl_mask_free_work(struct work_struct *work)
390{
391 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
392 struct fl_flow_mask, rwork);
393
99815f50
VB
394 fl_mask_free(mask, true);
395}
396
397static void fl_uninit_mask_free_work(struct work_struct *work)
398{
399 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
400 struct fl_flow_mask, rwork);
401
402 fl_mask_free(mask, false);
44a5cd43
PA
403}
404
9994677c 405static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask)
05cd271f 406{
f48ef4d5 407 if (!refcount_dec_and_test(&mask->refcnt))
05cd271f
PB
408 return false;
409
410 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
259e60f9
VB
411
412 spin_lock(&head->masks_lock);
05cd271f 413 list_del_rcu(&mask->list);
259e60f9
VB
414 spin_unlock(&head->masks_lock);
415
9994677c 416 tcf_queue_work(&mask->rwork, fl_mask_free_work);
05cd271f
PB
417
418 return true;
77b9900e
JP
419}
420
c049d56e
VB
421static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
422{
423 /* Flower classifier only changes root pointer during init and destroy.
424 * Users must obtain reference to tcf_proto instance before calling its
425 * API, so tp->root pointer is protected from concurrent call to
426 * fl_destroy() by reference counting.
427 */
428 return rcu_dereference_raw(tp->root);
429}
430
0dadc117
CW
431static void __fl_destroy_filter(struct cls_fl_filter *f)
432{
1a432018
IS
433 if (f->needs_tc_skb_ext)
434 tc_skb_ext_tc_disable();
0dadc117
CW
435 tcf_exts_destroy(&f->exts);
436 tcf_exts_put_net(&f->exts);
437 kfree(f);
438}
439
0552c8af 440static void fl_destroy_filter_work(struct work_struct *work)
77b9900e 441{
aaa908ff
CW
442 struct cls_fl_filter *f = container_of(to_rcu_work(work),
443 struct cls_fl_filter, rwork);
77b9900e 444
0dadc117 445 __fl_destroy_filter(f);
0552c8af
CW
446}
447
1b0f8037 448static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
c24e43d8 449 bool rtnl_held, struct netlink_ext_ack *extack)
5b33f488 450{
208c0f4b 451 struct tcf_block *block = tp->chain->block;
f9e30088 452 struct flow_cls_offload cls_flower = {};
5b33f488 453
d6787147 454 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
f9e30088 455 cls_flower.command = FLOW_CLS_DESTROY;
de4784ca 456 cls_flower.cookie = (unsigned long) f;
5b33f488 457
40119211 458 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false,
918190f5 459 &f->flags, &f->in_hw_count, rtnl_held);
c24e43d8 460
5b33f488
AV
461}
462
e8eb36cd 463static int fl_hw_replace_filter(struct tcf_proto *tp,
c24e43d8 464 struct cls_fl_filter *f, bool rtnl_held,
41002038 465 struct netlink_ext_ack *extack)
5b33f488 466{
208c0f4b 467 struct tcf_block *block = tp->chain->block;
f9e30088 468 struct flow_cls_offload cls_flower = {};
717503b9 469 bool skip_sw = tc_skip_sw(f->flags);
c24e43d8
VB
470 int err = 0;
471
e3ab786b 472 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
918190f5
VB
473 if (!cls_flower.rule)
474 return -ENOMEM;
8f256622 475
d6787147 476 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
f9e30088 477 cls_flower.command = FLOW_CLS_REPLACE;
de4784ca 478 cls_flower.cookie = (unsigned long) f;
8f256622
PNA
479 cls_flower.rule->match.dissector = &f->mask->dissector;
480 cls_flower.rule->match.mask = &f->mask->key;
481 cls_flower.rule->match.key = &f->mkey;
384c181e 482 cls_flower.classid = f->res.classid;
5b33f488 483
c2ccf84e
IS
484 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts,
485 cls_flower.common.extack);
3a7b6861
PNA
486 if (err) {
487 kfree(cls_flower.rule);
11c95317
IS
488
489 return skip_sw ? err : 0;
3a7b6861
PNA
490 }
491
40119211 492 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower,
918190f5 493 skip_sw, &f->flags, &f->in_hw_count, rtnl_held);
9c1c0e12 494 tc_cleanup_offload_action(&cls_flower.rule->action);
8f256622
PNA
495 kfree(cls_flower.rule);
496
40119211 497 if (err) {
918190f5
VB
498 fl_hw_destroy_filter(tp, f, rtnl_held, NULL);
499 return err;
717503b9
JP
500 }
501
918190f5
VB
502 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
503 return -EINVAL;
c24e43d8 504
918190f5 505 return 0;
5b33f488
AV
506}
507
c24e43d8
VB
508static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f,
509 bool rtnl_held)
10cbc684 510{
208c0f4b 511 struct tcf_block *block = tp->chain->block;
f9e30088 512 struct flow_cls_offload cls_flower = {};
10cbc684 513
d6787147 514 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
f9e30088 515 cls_flower.command = FLOW_CLS_STATS;
de4784ca 516 cls_flower.cookie = (unsigned long) f;
384c181e 517 cls_flower.classid = f->res.classid;
10cbc684 518
918190f5
VB
519 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false,
520 rtnl_held);
3b1903ef 521
5246c896 522 tcf_exts_hw_stats_update(&f->exts, &cls_flower.stats, cls_flower.use_act_stats);
10cbc684
AV
523}
524
06177558
VB
525static void __fl_put(struct cls_fl_filter *f)
526{
527 if (!refcount_dec_and_test(&f->refcnt))
528 return;
529
530 if (tcf_exts_get_net(&f->exts))
531 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
532 else
533 __fl_destroy_filter(f);
534}
535
536static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
537{
538 struct cls_fl_filter *f;
539
540 rcu_read_lock();
541 f = idr_find(&head->handle_idr, handle);
542 if (f && !refcount_inc_not_zero(&f->refcnt))
543 f = NULL;
544 rcu_read_unlock();
545
546 return f;
547}
548
606c7c43
PB
549static struct tcf_exts *fl_get_exts(const struct tcf_proto *tp, u32 handle)
550{
551 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
552 struct cls_fl_filter *f;
553
554 f = idr_find(&head->handle_idr, handle);
555 return f ? &f->exts : NULL;
556}
557
b2552b8c 558static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
c24e43d8
VB
559 bool *last, bool rtnl_held,
560 struct netlink_ext_ack *extack)
13fa876e 561{
e474619a 562 struct cls_fl_head *head = fl_head_dereference(tp);
c15ab236 563
b2552b8c
VB
564 *last = false;
565
3d81e711
VB
566 spin_lock(&tp->lock);
567 if (f->deleted) {
568 spin_unlock(&tp->lock);
b2552b8c 569 return -ENOENT;
3d81e711 570 }
b2552b8c
VB
571
572 f->deleted = true;
573 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
574 f->mask->filter_ht_params);
9c160941 575 idr_remove(&head->handle_idr, f->handle);
13fa876e 576 list_del_rcu(&f->list);
3d81e711
VB
577 spin_unlock(&tp->lock);
578
9994677c 579 *last = fl_mask_put(head, f->mask);
79685219 580 if (!tc_skip_hw(f->flags))
c24e43d8 581 fl_hw_destroy_filter(tp, f, rtnl_held, extack);
13fa876e 582 tcf_unbind_filter(tp, &f->res);
06177558 583 __fl_put(f);
05cd271f 584
b2552b8c 585 return 0;
13fa876e
RD
586}
587
d9363774
DB
588static void fl_destroy_sleepable(struct work_struct *work)
589{
aaa908ff
CW
590 struct cls_fl_head *head = container_of(to_rcu_work(work),
591 struct cls_fl_head,
592 rwork);
de9dc650
PB
593
594 rhashtable_destroy(&head->ht);
d9363774
DB
595 kfree(head);
596 module_put(THIS_MODULE);
597}
598
12db03b6
VB
599static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
600 struct netlink_ext_ack *extack)
77b9900e 601{
e474619a 602 struct cls_fl_head *head = fl_head_dereference(tp);
05cd271f 603 struct fl_flow_mask *mask, *next_mask;
77b9900e 604 struct cls_fl_filter *f, *next;
b2552b8c 605 bool last;
77b9900e 606
05cd271f
PB
607 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
608 list_for_each_entry_safe(f, next, &mask->filters, list) {
c24e43d8 609 __fl_delete(tp, f, &last, rtnl_held, extack);
b2552b8c 610 if (last)
05cd271f
PB
611 break;
612 }
613 }
c15ab236 614 idr_destroy(&head->handle_idr);
d9363774
DB
615
616 __module_get(THIS_MODULE);
aaa908ff 617 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
77b9900e
JP
618}
619
06177558
VB
620static void fl_put(struct tcf_proto *tp, void *arg)
621{
622 struct cls_fl_filter *f = arg;
623
624 __fl_put(f);
625}
626
8113c095 627static void *fl_get(struct tcf_proto *tp, u32 handle)
77b9900e 628{
e474619a 629 struct cls_fl_head *head = fl_head_dereference(tp);
77b9900e 630
06177558 631 return __fl_get(head, handle);
77b9900e
JP
632}
633
634static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
1a432018
IS
635 [TCA_FLOWER_UNSPEC] = { .strict_start_type =
636 TCA_FLOWER_L2_MISS },
77b9900e
JP
637 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
638 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
639 .len = IFNAMSIZ },
640 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
641 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
642 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
643 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
644 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
645 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
646 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
647 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
648 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
649 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
650 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
651 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
652 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
653 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
654 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
655 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
b175c3a4
JHS
656 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
657 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
9399ae9a
HHZ
658 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
659 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
660 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
bc3103f1
AV
661 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
662 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
663 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
664 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
665 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
666 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
667 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
668 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
669 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
aa72d708
OG
670 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
671 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
672 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
673 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
5976c5f4
SH
674 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
675 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
676 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
677 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
f4d997fd
HHZ
678 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
679 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
680 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
681 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
0e83a787
AST
682 [TCA_FLOWER_KEY_FLAGS] = NLA_POLICY_MASK(NLA_BE32,
683 TCA_FLOWER_KEY_FLAGS_POLICY_MASK),
684 [TCA_FLOWER_KEY_FLAGS_MASK] = NLA_POLICY_MASK(NLA_BE32,
685 TCA_FLOWER_KEY_FLAGS_POLICY_MASK),
7b684884
SH
686 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
687 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
688 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
689 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
690 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
691 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
692 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
693 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
99d31326
SH
694 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
695 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
696 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
697 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
698 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
699 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
700 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
701 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
702 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
703 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
a577d8f7
BL
704 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
705 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
706 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
707 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
61aec25a 708 [TCA_FLOWER_KEY_MPLS_OPTS] = { .type = NLA_NESTED },
fdfc7dd6
JP
709 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
710 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
4d80cc0a
OG
711 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
712 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
713 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
714 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
d64efd09
JL
715 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
716 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
717 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
0e2c17b6
OG
718 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
719 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
720 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
721 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
0a6e7778
PJV
722 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
723 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
1bcc51ac 724 [TCA_FLOWER_KEY_CT_STATE] =
725 NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK),
726 [TCA_FLOWER_KEY_CT_STATE_MASK] =
727 NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK),
e0ace68a
PB
728 [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 },
729 [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 },
730 [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 },
731 [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 },
732 [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY,
733 .len = 128 / BITS_PER_BYTE },
734 [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY,
735 .len = 128 / BITS_PER_BYTE },
e2debf08 736 [TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
5923b8f7
AL
737 [TCA_FLOWER_KEY_HASH] = { .type = NLA_U32 },
738 [TCA_FLOWER_KEY_HASH_MASK] = { .type = NLA_U32 },
b4000312 739 [TCA_FLOWER_KEY_NUM_OF_VLANS] = { .type = NLA_U8 },
5008750e
WD
740 [TCA_FLOWER_KEY_PPPOE_SID] = { .type = NLA_U16 },
741 [TCA_FLOWER_KEY_PPP_PROTO] = { .type = NLA_U16 },
8b189ea0 742 [TCA_FLOWER_KEY_L2TPV3_SID] = { .type = NLA_U32 },
4c13eda7
RK
743 [TCA_FLOWER_KEY_SPI] = { .type = NLA_U32 },
744 [TCA_FLOWER_KEY_SPI_MASK] = { .type = NLA_U32 },
1a432018 745 [TCA_FLOWER_L2_MISS] = NLA_POLICY_MAX(NLA_U8, 1),
7cfffd5f 746 [TCA_FLOWER_KEY_CFM] = { .type = NLA_NESTED },
11036bd7
AST
747 [TCA_FLOWER_KEY_ENC_FLAGS] = NLA_POLICY_MASK(NLA_BE32,
748 TCA_FLOWER_KEY_ENC_FLAGS_POLICY_MASK),
749 [TCA_FLOWER_KEY_ENC_FLAGS_MASK] = NLA_POLICY_MASK(NLA_BE32,
750 TCA_FLOWER_KEY_ENC_FLAGS_POLICY_MASK),
0a6e7778
PJV
751};
752
753static const struct nla_policy
754enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
d8f9dfae
XL
755 [TCA_FLOWER_KEY_ENC_OPTS_UNSPEC] = {
756 .strict_start_type = TCA_FLOWER_KEY_ENC_OPTS_VXLAN },
0a6e7778 757 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
d8f9dfae 758 [TCA_FLOWER_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED },
79b1011c 759 [TCA_FLOWER_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED },
e3acda7a 760 [TCA_FLOWER_KEY_ENC_OPTS_GTP] = { .type = NLA_NESTED },
6dd514f4 761 [TCA_FLOWER_KEY_ENC_OPTS_PFCP] = { .type = NLA_NESTED },
0a6e7778
PJV
762};
763
764static const struct nla_policy
765geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
766 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
767 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
768 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
b27055a0 769 .len = 127 },
77b9900e
JP
770};
771
d8f9dfae
XL
772static const struct nla_policy
773vxlan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1] = {
774 [TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 },
775};
776
79b1011c
XL
777static const struct nla_policy
778erspan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1] = {
779 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 },
780 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
781 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
782 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
783};
784
e3acda7a
WD
785static const struct nla_policy
786gtp_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1] = {
787 [TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] = { .type = NLA_U8 },
788 [TCA_FLOWER_KEY_ENC_OPT_GTP_QFI] = { .type = NLA_U8 },
789};
790
6dd514f4
MS
791static const struct nla_policy
792pfcp_opt_policy[TCA_FLOWER_KEY_ENC_OPT_PFCP_MAX + 1] = {
793 [TCA_FLOWER_KEY_ENC_OPT_PFCP_TYPE] = { .type = NLA_U8 },
794 [TCA_FLOWER_KEY_ENC_OPT_PFCP_SEID] = { .type = NLA_U64 },
795};
796
61aec25a
GN
797static const struct nla_policy
798mpls_stack_entry_policy[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1] = {
799 [TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH] = { .type = NLA_U8 },
800 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL] = { .type = NLA_U8 },
801 [TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS] = { .type = NLA_U8 },
802 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TC] = { .type = NLA_U8 },
803 [TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL] = { .type = NLA_U32 },
804};
805
4d50e500
ED
806static const struct nla_policy
807cfm_opt_policy[TCA_FLOWER_KEY_CFM_OPT_MAX + 1] = {
7cfffd5f
ZD
808 [TCA_FLOWER_KEY_CFM_MD_LEVEL] = NLA_POLICY_MAX(NLA_U8,
809 FLOW_DIS_CFM_MDL_MAX),
810 [TCA_FLOWER_KEY_CFM_OPCODE] = { .type = NLA_U8 },
811};
812
77b9900e
JP
813static void fl_set_key_val(struct nlattr **tb,
814 void *val, int val_type,
815 void *mask, int mask_type, int len)
816{
817 if (!tb[val_type])
818 return;
e0ace68a 819 nla_memcpy(val, tb[val_type], len);
77b9900e
JP
820 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
821 memset(mask, 0xff, len);
822 else
e0ace68a 823 nla_memcpy(mask, tb[mask_type], len);
77b9900e
JP
824}
825
4c13eda7
RK
826static int fl_set_key_spi(struct nlattr **tb, struct fl_flow_key *key,
827 struct fl_flow_key *mask,
828 struct netlink_ext_ack *extack)
829{
830 if (key->basic.ip_proto != IPPROTO_ESP &&
831 key->basic.ip_proto != IPPROTO_AH) {
832 NL_SET_ERR_MSG(extack,
833 "Protocol must be either ESP or AH");
834 return -EINVAL;
835 }
836
837 fl_set_key_val(tb, &key->ipsec.spi,
838 TCA_FLOWER_KEY_SPI,
839 &mask->ipsec.spi, TCA_FLOWER_KEY_SPI_MASK,
840 sizeof(key->ipsec.spi));
841 return 0;
842}
843
5c72299f 844static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
bd7d4c12
GN
845 struct fl_flow_key *mask,
846 struct netlink_ext_ack *extack)
5c72299f 847{
8ffb055b
YK
848 fl_set_key_val(tb, &key->tp_range.tp_min.dst,
849 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst,
850 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.dst));
851 fl_set_key_val(tb, &key->tp_range.tp_max.dst,
852 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_range.tp_max.dst,
853 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.dst));
854 fl_set_key_val(tb, &key->tp_range.tp_min.src,
855 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_range.tp_min.src,
856 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.src));
857 fl_set_key_val(tb, &key->tp_range.tp_max.src,
858 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src,
859 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src));
860
d3f87278
IS
861 if (mask->tp_range.tp_min.dst != mask->tp_range.tp_max.dst) {
862 NL_SET_ERR_MSG(extack,
863 "Both min and max destination ports must be specified");
864 return -EINVAL;
865 }
866 if (mask->tp_range.tp_min.src != mask->tp_range.tp_max.src) {
867 NL_SET_ERR_MSG(extack,
868 "Both min and max source ports must be specified");
869 return -EINVAL;
870 }
bd7d4c12 871 if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
6215afcb
VO
872 ntohs(key->tp_range.tp_max.dst) <=
873 ntohs(key->tp_range.tp_min.dst)) {
bd7d4c12
GN
874 NL_SET_ERR_MSG_ATTR(extack,
875 tb[TCA_FLOWER_KEY_PORT_DST_MIN],
876 "Invalid destination port range (min must be strictly smaller than max)");
5c72299f 877 return -EINVAL;
bd7d4c12
GN
878 }
879 if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
6215afcb
VO
880 ntohs(key->tp_range.tp_max.src) <=
881 ntohs(key->tp_range.tp_min.src)) {
bd7d4c12
GN
882 NL_SET_ERR_MSG_ATTR(extack,
883 tb[TCA_FLOWER_KEY_PORT_SRC_MIN],
884 "Invalid source port range (min must be strictly smaller than max)");
885 return -EINVAL;
886 }
5c72299f
AN
887
888 return 0;
889}
890
61aec25a
GN
891static int fl_set_key_mpls_lse(const struct nlattr *nla_lse,
892 struct flow_dissector_key_mpls *key_val,
893 struct flow_dissector_key_mpls *key_mask,
894 struct netlink_ext_ack *extack)
895{
896 struct nlattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1];
897 struct flow_dissector_mpls_lse *lse_mask;
898 struct flow_dissector_mpls_lse *lse_val;
899 u8 lse_index;
900 u8 depth;
901 int err;
902
903 err = nla_parse_nested(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, nla_lse,
904 mpls_stack_entry_policy, extack);
905 if (err < 0)
906 return err;
907
908 if (!tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]) {
909 NL_SET_ERR_MSG(extack, "Missing MPLS option \"depth\"");
910 return -EINVAL;
911 }
912
913 depth = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]);
914
915 /* LSE depth starts at 1, for consistency with terminology used by
916 * RFC 3031 (section 3.9), where depth 0 refers to unlabeled packets.
917 */
918 if (depth < 1 || depth > FLOW_DIS_MPLS_MAX) {
919 NL_SET_ERR_MSG_ATTR(extack,
920 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH],
921 "Invalid MPLS depth");
922 return -EINVAL;
923 }
924 lse_index = depth - 1;
925
926 dissector_set_mpls_lse(key_val, lse_index);
927 dissector_set_mpls_lse(key_mask, lse_index);
928
929 lse_val = &key_val->ls[lse_index];
930 lse_mask = &key_mask->ls[lse_index];
931
932 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]) {
933 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]);
934 lse_mask->mpls_ttl = MPLS_TTL_MASK;
935 }
936 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]) {
937 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]);
938
939 if (bos & ~MPLS_BOS_MASK) {
940 NL_SET_ERR_MSG_ATTR(extack,
941 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS],
942 "Bottom Of Stack (BOS) must be 0 or 1");
943 return -EINVAL;
944 }
945 lse_val->mpls_bos = bos;
946 lse_mask->mpls_bos = MPLS_BOS_MASK;
947 }
948 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]) {
949 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]);
950
951 if (tc & ~MPLS_TC_MASK) {
952 NL_SET_ERR_MSG_ATTR(extack,
953 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC],
954 "Traffic Class (TC) must be between 0 and 7");
955 return -EINVAL;
956 }
957 lse_val->mpls_tc = tc;
958 lse_mask->mpls_tc = MPLS_TC_MASK;
959 }
960 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]) {
961 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]);
962
963 if (label & ~MPLS_LABEL_MASK) {
964 NL_SET_ERR_MSG_ATTR(extack,
965 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL],
966 "Label must be between 0 and 1048575");
967 return -EINVAL;
968 }
969 lse_val->mpls_label = label;
970 lse_mask->mpls_label = MPLS_LABEL_MASK;
971 }
972
973 return 0;
974}
975
976static int fl_set_key_mpls_opts(const struct nlattr *nla_mpls_opts,
977 struct flow_dissector_key_mpls *key_val,
978 struct flow_dissector_key_mpls *key_mask,
979 struct netlink_ext_ack *extack)
980{
981 struct nlattr *nla_lse;
982 int rem;
983 int err;
984
985 if (!(nla_mpls_opts->nla_type & NLA_F_NESTED)) {
986 NL_SET_ERR_MSG_ATTR(extack, nla_mpls_opts,
987 "NLA_F_NESTED is missing");
988 return -EINVAL;
989 }
990
991 nla_for_each_nested(nla_lse, nla_mpls_opts, rem) {
992 if (nla_type(nla_lse) != TCA_FLOWER_KEY_MPLS_OPTS_LSE) {
993 NL_SET_ERR_MSG_ATTR(extack, nla_lse,
994 "Invalid MPLS option type");
995 return -EINVAL;
996 }
997
998 err = fl_set_key_mpls_lse(nla_lse, key_val, key_mask, extack);
999 if (err < 0)
1000 return err;
1001 }
1002 if (rem) {
1003 NL_SET_ERR_MSG(extack,
1004 "Bytes leftover after parsing MPLS options");
1005 return -EINVAL;
1006 }
1007
1008 return 0;
1009}
1010
1a7fca63
BL
1011static int fl_set_key_mpls(struct nlattr **tb,
1012 struct flow_dissector_key_mpls *key_val,
442f730e
GN
1013 struct flow_dissector_key_mpls *key_mask,
1014 struct netlink_ext_ack *extack)
a577d8f7 1015{
58cff782
GN
1016 struct flow_dissector_mpls_lse *lse_mask;
1017 struct flow_dissector_mpls_lse *lse_val;
1018
61aec25a
GN
1019 if (tb[TCA_FLOWER_KEY_MPLS_OPTS]) {
1020 if (tb[TCA_FLOWER_KEY_MPLS_TTL] ||
1021 tb[TCA_FLOWER_KEY_MPLS_BOS] ||
1022 tb[TCA_FLOWER_KEY_MPLS_TC] ||
1023 tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
1024 NL_SET_ERR_MSG_ATTR(extack,
1025 tb[TCA_FLOWER_KEY_MPLS_OPTS],
1026 "MPLS label, Traffic Class, Bottom Of Stack and Time To Live must be encapsulated in the MPLS options attribute");
1027 return -EBADMSG;
1028 }
1029
1030 return fl_set_key_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS],
1031 key_val, key_mask, extack);
1032 }
1033
58cff782
GN
1034 lse_val = &key_val->ls[0];
1035 lse_mask = &key_mask->ls[0];
1036
a577d8f7 1037 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
58cff782
GN
1038 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
1039 lse_mask->mpls_ttl = MPLS_TTL_MASK;
1040 dissector_set_mpls_lse(key_val, 0);
1041 dissector_set_mpls_lse(key_mask, 0);
a577d8f7
BL
1042 }
1043 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
1a7fca63
BL
1044 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
1045
442f730e
GN
1046 if (bos & ~MPLS_BOS_MASK) {
1047 NL_SET_ERR_MSG_ATTR(extack,
1048 tb[TCA_FLOWER_KEY_MPLS_BOS],
1049 "Bottom Of Stack (BOS) must be 0 or 1");
1a7fca63 1050 return -EINVAL;
442f730e 1051 }
58cff782
GN
1052 lse_val->mpls_bos = bos;
1053 lse_mask->mpls_bos = MPLS_BOS_MASK;
1054 dissector_set_mpls_lse(key_val, 0);
1055 dissector_set_mpls_lse(key_mask, 0);
a577d8f7
BL
1056 }
1057 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
1a7fca63
BL
1058 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
1059
442f730e
GN
1060 if (tc & ~MPLS_TC_MASK) {
1061 NL_SET_ERR_MSG_ATTR(extack,
1062 tb[TCA_FLOWER_KEY_MPLS_TC],
1063 "Traffic Class (TC) must be between 0 and 7");
1a7fca63 1064 return -EINVAL;
442f730e 1065 }
58cff782
GN
1066 lse_val->mpls_tc = tc;
1067 lse_mask->mpls_tc = MPLS_TC_MASK;
1068 dissector_set_mpls_lse(key_val, 0);
1069 dissector_set_mpls_lse(key_mask, 0);
a577d8f7
BL
1070 }
1071 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
1a7fca63
BL
1072 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
1073
442f730e
GN
1074 if (label & ~MPLS_LABEL_MASK) {
1075 NL_SET_ERR_MSG_ATTR(extack,
1076 tb[TCA_FLOWER_KEY_MPLS_LABEL],
1077 "Label must be between 0 and 1048575");
1a7fca63 1078 return -EINVAL;
442f730e 1079 }
58cff782
GN
1080 lse_val->mpls_label = label;
1081 lse_mask->mpls_label = MPLS_LABEL_MASK;
1082 dissector_set_mpls_lse(key_val, 0);
1083 dissector_set_mpls_lse(key_mask, 0);
a577d8f7 1084 }
1a7fca63 1085 return 0;
a577d8f7
BL
1086}
1087
9399ae9a 1088static void fl_set_key_vlan(struct nlattr **tb,
aaab0834 1089 __be16 ethertype,
d64efd09 1090 int vlan_id_key, int vlan_prio_key,
2105f700 1091 int vlan_next_eth_type_key,
9399ae9a
HHZ
1092 struct flow_dissector_key_vlan *key_val,
1093 struct flow_dissector_key_vlan *key_mask)
1094{
1095#define VLAN_PRIORITY_MASK 0x7
1096
d64efd09 1097 if (tb[vlan_id_key]) {
9399ae9a 1098 key_val->vlan_id =
d64efd09 1099 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
9399ae9a
HHZ
1100 key_mask->vlan_id = VLAN_VID_MASK;
1101 }
d64efd09 1102 if (tb[vlan_prio_key]) {
9399ae9a 1103 key_val->vlan_priority =
d64efd09 1104 nla_get_u8(tb[vlan_prio_key]) &
9399ae9a
HHZ
1105 VLAN_PRIORITY_MASK;
1106 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
1107 }
99fdb22b
BS
1108 if (ethertype) {
1109 key_val->vlan_tpid = ethertype;
1110 key_mask->vlan_tpid = cpu_to_be16(~0);
1111 }
2105f700
VB
1112 if (tb[vlan_next_eth_type_key]) {
1113 key_val->vlan_eth_type =
1114 nla_get_be16(tb[vlan_next_eth_type_key]);
1115 key_mask->vlan_eth_type = cpu_to_be16(~0);
1116 }
9399ae9a
HHZ
1117}
1118
5008750e
WD
1119static void fl_set_key_pppoe(struct nlattr **tb,
1120 struct flow_dissector_key_pppoe *key_val,
1121 struct flow_dissector_key_pppoe *key_mask,
1122 struct fl_flow_key *key,
1123 struct fl_flow_key *mask)
1124{
1125 /* key_val::type must be set to ETH_P_PPP_SES
1126 * because ETH_P_PPP_SES was stored in basic.n_proto
1127 * which might get overwritten by ppp_proto
1128 * or might be set to 0, the role of key_val::type
4170f0ef 1129 * is similar to vlan_key::tpid
5008750e
WD
1130 */
1131 key_val->type = htons(ETH_P_PPP_SES);
1132 key_mask->type = cpu_to_be16(~0);
1133
1134 if (tb[TCA_FLOWER_KEY_PPPOE_SID]) {
1135 key_val->session_id =
1136 nla_get_be16(tb[TCA_FLOWER_KEY_PPPOE_SID]);
1137 key_mask->session_id = cpu_to_be16(~0);
1138 }
1139 if (tb[TCA_FLOWER_KEY_PPP_PROTO]) {
1140 key_val->ppp_proto =
1141 nla_get_be16(tb[TCA_FLOWER_KEY_PPP_PROTO]);
1142 key_mask->ppp_proto = cpu_to_be16(~0);
1143
1144 if (key_val->ppp_proto == htons(PPP_IP)) {
1145 key->basic.n_proto = htons(ETH_P_IP);
1146 mask->basic.n_proto = cpu_to_be16(~0);
1147 } else if (key_val->ppp_proto == htons(PPP_IPV6)) {
1148 key->basic.n_proto = htons(ETH_P_IPV6);
1149 mask->basic.n_proto = cpu_to_be16(~0);
1150 } else if (key_val->ppp_proto == htons(PPP_MPLS_UC)) {
1151 key->basic.n_proto = htons(ETH_P_MPLS_UC);
1152 mask->basic.n_proto = cpu_to_be16(~0);
1153 } else if (key_val->ppp_proto == htons(PPP_MPLS_MC)) {
1154 key->basic.n_proto = htons(ETH_P_MPLS_MC);
1155 mask->basic.n_proto = cpu_to_be16(~0);
1156 }
1157 } else {
1158 key->basic.n_proto = 0;
1159 mask->basic.n_proto = cpu_to_be16(0);
1160 }
1161}
1162
faa3ffce
OG
1163static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
1164 u32 *dissector_key, u32 *dissector_mask,
1165 u32 flower_flag_bit, u32 dissector_flag_bit)
1166{
1167 if (flower_mask & flower_flag_bit) {
1168 *dissector_mask |= dissector_flag_bit;
1169 if (flower_key & flower_flag_bit)
1170 *dissector_key |= dissector_flag_bit;
1171 }
1172}
1173
536b97ac
AST
1174static int fl_set_key_flags(struct nlattr *tca_opts, struct nlattr **tb,
1175 bool encap, u32 *flags_key, u32 *flags_mask,
1176 struct netlink_ext_ack *extack)
faa3ffce 1177{
fcb4bb07 1178 int fl_key, fl_mask;
faa3ffce
OG
1179 u32 key, mask;
1180
fcb4bb07
AST
1181 if (encap) {
1182 fl_key = TCA_FLOWER_KEY_ENC_FLAGS;
1183 fl_mask = TCA_FLOWER_KEY_ENC_FLAGS_MASK;
1184 } else {
1185 fl_key = TCA_FLOWER_KEY_FLAGS;
1186 fl_mask = TCA_FLOWER_KEY_FLAGS_MASK;
1187 }
1188
d9724772 1189 /* mask is mandatory for flags */
536b97ac 1190 if (NL_REQ_ATTR_CHECK(extack, tca_opts, tb, fl_mask)) {
e304e21a 1191 NL_SET_ERR_MSG(extack, "Missing flags mask");
d9724772 1192 return -EINVAL;
e304e21a 1193 }
faa3ffce 1194
fcb4bb07
AST
1195 key = be32_to_cpu(nla_get_be32(tb[fl_key]));
1196 mask = be32_to_cpu(nla_get_be32(tb[fl_mask]));
faa3ffce
OG
1197
1198 *flags_key = 0;
1199 *flags_mask = 0;
1200
1201 fl_set_key_flag(key, mask, flags_key, flags_mask,
1202 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
459d153d
PJV
1203 fl_set_key_flag(key, mask, flags_key, flags_mask,
1204 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1205 FLOW_DIS_FIRST_FRAG);
d9724772 1206
988f8723
AST
1207 fl_set_key_flag(key, mask, flags_key, flags_mask,
1208 TCA_FLOWER_KEY_FLAGS_TUNNEL_CSUM,
1209 FLOW_DIS_F_TUNNEL_CSUM);
1210
1211 fl_set_key_flag(key, mask, flags_key, flags_mask,
1212 TCA_FLOWER_KEY_FLAGS_TUNNEL_DONT_FRAGMENT,
1213 FLOW_DIS_F_TUNNEL_DONT_FRAGMENT);
1214
1215 fl_set_key_flag(key, mask, flags_key, flags_mask,
1216 TCA_FLOWER_KEY_FLAGS_TUNNEL_OAM, FLOW_DIS_F_TUNNEL_OAM);
1217
1218 fl_set_key_flag(key, mask, flags_key, flags_mask,
1219 TCA_FLOWER_KEY_FLAGS_TUNNEL_CRIT_OPT,
1220 FLOW_DIS_F_TUNNEL_CRIT_OPT);
1221
d9724772 1222 return 0;
faa3ffce
OG
1223}
1224
0e2c17b6 1225static void fl_set_key_ip(struct nlattr **tb, bool encap,
4d80cc0a
OG
1226 struct flow_dissector_key_ip *key,
1227 struct flow_dissector_key_ip *mask)
1228{
0e2c17b6
OG
1229 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1230 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1231 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1232 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
4d80cc0a 1233
0e2c17b6
OG
1234 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
1235 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
4d80cc0a
OG
1236}
1237
0a6e7778
PJV
1238static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
1239 int depth, int option_len,
1240 struct netlink_ext_ack *extack)
1241{
1242 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
1243 struct nlattr *class = NULL, *type = NULL, *data = NULL;
1244 struct geneve_opt *opt;
1245 int err, data_len = 0;
1246
1247 if (option_len > sizeof(struct geneve_opt))
1248 data_len = option_len - sizeof(struct geneve_opt);
1249
4d56304e
HH
1250 if (key->enc_opts.len > FLOW_DIS_TUN_OPTS_MAX - 4)
1251 return -ERANGE;
1252
0a6e7778
PJV
1253 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
1254 memset(opt, 0xff, option_len);
1255 opt->length = data_len / 4;
1256 opt->r1 = 0;
1257 opt->r2 = 0;
1258 opt->r3 = 0;
1259
1260 /* If no mask has been prodived we assume an exact match. */
1261 if (!depth)
1262 return sizeof(struct geneve_opt) + data_len;
1263
1264 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
1265 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
1266 return -EINVAL;
1267 }
1268
8cb08174
JB
1269 err = nla_parse_nested_deprecated(tb,
1270 TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
1271 nla, geneve_opt_policy, extack);
0a6e7778
PJV
1272 if (err < 0)
1273 return err;
1274
1275 /* We are not allowed to omit any of CLASS, TYPE or DATA
1276 * fields from the key.
1277 */
1278 if (!option_len &&
1279 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
1280 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
1281 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
1282 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
1283 return -EINVAL;
1284 }
1285
1286 /* Omitting any of CLASS, TYPE or DATA fields is allowed
1287 * for the mask.
1288 */
1289 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
1290 int new_len = key->enc_opts.len;
1291
1292 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
1293 data_len = nla_len(data);
1294 if (data_len < 4) {
1295 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
1296 return -ERANGE;
1297 }
1298 if (data_len % 4) {
1299 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
1300 return -ERANGE;
1301 }
1302
1303 new_len += sizeof(struct geneve_opt) + data_len;
1304 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
1305 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
1306 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
1307 return -ERANGE;
1308 }
1309 opt->length = data_len / 4;
1310 memcpy(opt->opt_data, nla_data(data), data_len);
1311 }
1312
1313 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
1314 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
1315 opt->opt_class = nla_get_be16(class);
1316 }
1317
1318 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
1319 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
1320 opt->type = nla_get_u8(type);
1321 }
1322
1323 return sizeof(struct geneve_opt) + data_len;
1324}
1325
d8f9dfae
XL
1326static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1327 int depth, int option_len,
1328 struct netlink_ext_ack *extack)
1329{
1330 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1];
1331 struct vxlan_metadata *md;
1332 int err;
1333
1334 md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1335 memset(md, 0xff, sizeof(*md));
1336
1337 if (!depth)
1338 return sizeof(*md);
1339
1340 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) {
1341 NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask");
1342 return -EINVAL;
1343 }
1344
1345 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla,
1346 vxlan_opt_policy, extack);
1347 if (err < 0)
1348 return err;
1349
1350 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
1351 NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
1352 return -EINVAL;
1353 }
1354
13e6ce98 1355 if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
d8f9dfae 1356 md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]);
13e6ce98
XL
1357 md->gbp &= VXLAN_GBP_MASK;
1358 }
d8f9dfae
XL
1359
1360 return sizeof(*md);
1361}
1362
79b1011c
XL
1363static int fl_set_erspan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1364 int depth, int option_len,
1365 struct netlink_ext_ack *extack)
1366{
1367 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1];
1368 struct erspan_metadata *md;
1369 int err;
1370
1371 md = (struct erspan_metadata *)&key->enc_opts.data[key->enc_opts.len];
79b1011c
XL
1372 md->version = 1;
1373
1374 if (!depth)
1375 return sizeof(*md);
1376
1377 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_ERSPAN) {
1378 NL_SET_ERR_MSG(extack, "Non-erspan option type for mask");
1379 return -EINVAL;
1380 }
1381
1382 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, nla,
1383 erspan_opt_policy, extack);
1384 if (err < 0)
1385 return err;
1386
1387 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) {
1388 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver");
1389 return -EINVAL;
1390 }
1391
1392 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER])
1393 md->version = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]);
1394
1395 if (md->version == 1) {
1396 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1397 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index");
1398 return -EINVAL;
1399 }
29220780 1400 memset(&md->u.index, 0xff, sizeof(md->u.index));
79b1011c
XL
1401 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1402 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX];
1403 md->u.index = nla_get_be32(nla);
1404 }
1405 } else if (md->version == 2) {
1406 if (!option_len && (!tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] ||
1407 !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID])) {
1408 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid");
1409 return -EINVAL;
1410 }
29220780 1411 md->u.md2.dir = 1;
79b1011c
XL
1412 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]) {
1413 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR];
1414 md->u.md2.dir = nla_get_u8(nla);
1415 }
29220780 1416 set_hwid(&md->u.md2, 0xff);
79b1011c
XL
1417 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]) {
1418 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID];
1419 set_hwid(&md->u.md2, nla_get_u8(nla));
1420 }
1421 } else {
1422 NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect");
1423 return -EINVAL;
1424 }
1425
1426 return sizeof(*md);
1427}
1428
e3acda7a
WD
1429static int fl_set_gtp_opt(const struct nlattr *nla, struct fl_flow_key *key,
1430 int depth, int option_len,
1431 struct netlink_ext_ack *extack)
1432{
1433 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1];
1434 struct gtp_pdu_session_info *sinfo;
1435 u8 len = key->enc_opts.len;
1436 int err;
1437
1438 sinfo = (struct gtp_pdu_session_info *)&key->enc_opts.data[len];
1439 memset(sinfo, 0xff, option_len);
1440
1441 if (!depth)
1442 return sizeof(*sinfo);
1443
1444 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GTP) {
1445 NL_SET_ERR_MSG_MOD(extack, "Non-gtp option type for mask");
1446 return -EINVAL;
1447 }
1448
1449 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GTP_MAX, nla,
1450 gtp_opt_policy, extack);
1451 if (err < 0)
1452 return err;
1453
1454 if (!option_len &&
1455 (!tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] ||
1456 !tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])) {
1457 NL_SET_ERR_MSG_MOD(extack,
1458 "Missing tunnel key gtp option pdu type or qfi");
1459 return -EINVAL;
1460 }
1461
1462 if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE])
1463 sinfo->pdu_type =
1464 nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE]);
1465
1466 if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])
1467 sinfo->qfi = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI]);
1468
1469 return sizeof(*sinfo);
1470}
1471
6dd514f4
MS
1472static int fl_set_pfcp_opt(const struct nlattr *nla, struct fl_flow_key *key,
1473 int depth, int option_len,
1474 struct netlink_ext_ack *extack)
1475{
1476 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_MAX + 1];
1477 struct pfcp_metadata *md;
1478 int err;
1479
1480 md = (struct pfcp_metadata *)&key->enc_opts.data[key->enc_opts.len];
1481 memset(md, 0xff, sizeof(*md));
1482
1483 if (!depth)
1484 return sizeof(*md);
1485
1486 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_PFCP) {
1487 NL_SET_ERR_MSG_MOD(extack, "Non-pfcp option type for mask");
1488 return -EINVAL;
1489 }
1490
1491 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_PFCP_MAX, nla,
1492 pfcp_opt_policy, extack);
1493 if (err < 0)
1494 return err;
1495
1496 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_TYPE]) {
1497 NL_SET_ERR_MSG_MOD(extack, "Missing tunnel key pfcp option type");
1498 return -EINVAL;
1499 }
1500
1501 if (tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_TYPE])
1502 md->type = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_TYPE]);
1503
1504 if (tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_SEID])
1505 md->seid = nla_get_be64(tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_SEID]);
1506
1507 return sizeof(*md);
1508}
1509
0a6e7778
PJV
1510static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
1511 struct fl_flow_key *mask,
1512 struct netlink_ext_ack *extack)
1513{
1514 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
63c82997
JK
1515 int err, option_len, key_depth, msk_depth = 0;
1516
8cb08174
JB
1517 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
1518 TCA_FLOWER_KEY_ENC_OPTS_MAX,
1519 enc_opts_policy, extack);
63c82997
JK
1520 if (err)
1521 return err;
0a6e7778
PJV
1522
1523 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
1524
1525 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
8cb08174
JB
1526 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
1527 TCA_FLOWER_KEY_ENC_OPTS_MAX,
1528 enc_opts_policy, extack);
63c82997
JK
1529 if (err)
1530 return err;
1531
0a6e7778
PJV
1532 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
1533 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
c96adff9
CW
1534 if (!nla_ok(nla_opt_msk, msk_depth)) {
1535 NL_SET_ERR_MSG(extack, "Invalid nested attribute for masks");
1536 return -EINVAL;
1537 }
0a6e7778
PJV
1538 }
1539
1540 nla_for_each_attr(nla_opt_key, nla_enc_key,
1541 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
1542 switch (nla_type(nla_opt_key)) {
1543 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
d8f9dfae 1544 if (key->enc_opts.dst_opt_type &&
5832c4a7
AL
1545 key->enc_opts.dst_opt_type !=
1546 IP_TUNNEL_GENEVE_OPT_BIT) {
d8f9dfae
XL
1547 NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
1548 return -EINVAL;
1549 }
0a6e7778 1550 option_len = 0;
5832c4a7 1551 key->enc_opts.dst_opt_type = IP_TUNNEL_GENEVE_OPT_BIT;
0a6e7778
PJV
1552 option_len = fl_set_geneve_opt(nla_opt_key, key,
1553 key_depth, option_len,
1554 extack);
1555 if (option_len < 0)
1556 return option_len;
1557
1558 key->enc_opts.len += option_len;
1559 /* At the same time we need to parse through the mask
1560 * in order to verify exact and mask attribute lengths.
1561 */
5832c4a7 1562 mask->enc_opts.dst_opt_type = IP_TUNNEL_GENEVE_OPT_BIT;
0a6e7778
PJV
1563 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
1564 msk_depth, option_len,
1565 extack);
1566 if (option_len < 0)
1567 return option_len;
1568
1569 mask->enc_opts.len += option_len;
1570 if (key->enc_opts.len != mask->enc_opts.len) {
1571 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1572 return -EINVAL;
1573 }
d8f9dfae
XL
1574 break;
1575 case TCA_FLOWER_KEY_ENC_OPTS_VXLAN:
1576 if (key->enc_opts.dst_opt_type) {
1577 NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
1578 return -EINVAL;
1579 }
1580 option_len = 0;
5832c4a7 1581 key->enc_opts.dst_opt_type = IP_TUNNEL_VXLAN_OPT_BIT;
d8f9dfae
XL
1582 option_len = fl_set_vxlan_opt(nla_opt_key, key,
1583 key_depth, option_len,
1584 extack);
1585 if (option_len < 0)
1586 return option_len;
1587
1588 key->enc_opts.len += option_len;
1589 /* At the same time we need to parse through the mask
1590 * in order to verify exact and mask attribute lengths.
1591 */
5832c4a7 1592 mask->enc_opts.dst_opt_type = IP_TUNNEL_VXLAN_OPT_BIT;
d8f9dfae
XL
1593 option_len = fl_set_vxlan_opt(nla_opt_msk, mask,
1594 msk_depth, option_len,
1595 extack);
1596 if (option_len < 0)
1597 return option_len;
1598
1599 mask->enc_opts.len += option_len;
1600 if (key->enc_opts.len != mask->enc_opts.len) {
1601 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1602 return -EINVAL;
1603 }
79b1011c
XL
1604 break;
1605 case TCA_FLOWER_KEY_ENC_OPTS_ERSPAN:
1606 if (key->enc_opts.dst_opt_type) {
1607 NL_SET_ERR_MSG(extack, "Duplicate type for erspan options");
1608 return -EINVAL;
1609 }
1610 option_len = 0;
5832c4a7 1611 key->enc_opts.dst_opt_type = IP_TUNNEL_ERSPAN_OPT_BIT;
79b1011c
XL
1612 option_len = fl_set_erspan_opt(nla_opt_key, key,
1613 key_depth, option_len,
1614 extack);
1615 if (option_len < 0)
1616 return option_len;
1617
1618 key->enc_opts.len += option_len;
1619 /* At the same time we need to parse through the mask
1620 * in order to verify exact and mask attribute lengths.
1621 */
5832c4a7 1622 mask->enc_opts.dst_opt_type = IP_TUNNEL_ERSPAN_OPT_BIT;
79b1011c
XL
1623 option_len = fl_set_erspan_opt(nla_opt_msk, mask,
1624 msk_depth, option_len,
1625 extack);
1626 if (option_len < 0)
1627 return option_len;
1628
1629 mask->enc_opts.len += option_len;
1630 if (key->enc_opts.len != mask->enc_opts.len) {
1631 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1632 return -EINVAL;
1633 }
0a6e7778 1634 break;
e3acda7a
WD
1635 case TCA_FLOWER_KEY_ENC_OPTS_GTP:
1636 if (key->enc_opts.dst_opt_type) {
1637 NL_SET_ERR_MSG_MOD(extack,
1638 "Duplicate type for gtp options");
1639 return -EINVAL;
1640 }
1641 option_len = 0;
5832c4a7 1642 key->enc_opts.dst_opt_type = IP_TUNNEL_GTP_OPT_BIT;
e3acda7a
WD
1643 option_len = fl_set_gtp_opt(nla_opt_key, key,
1644 key_depth, option_len,
1645 extack);
1646 if (option_len < 0)
1647 return option_len;
1648
1649 key->enc_opts.len += option_len;
1650 /* At the same time we need to parse through the mask
1651 * in order to verify exact and mask attribute lengths.
1652 */
5832c4a7 1653 mask->enc_opts.dst_opt_type = IP_TUNNEL_GTP_OPT_BIT;
e3acda7a
WD
1654 option_len = fl_set_gtp_opt(nla_opt_msk, mask,
1655 msk_depth, option_len,
1656 extack);
1657 if (option_len < 0)
1658 return option_len;
1659
1660 mask->enc_opts.len += option_len;
1661 if (key->enc_opts.len != mask->enc_opts.len) {
1662 NL_SET_ERR_MSG_MOD(extack,
1663 "Key and mask miss aligned");
1664 return -EINVAL;
1665 }
1666 break;
6dd514f4
MS
1667 case TCA_FLOWER_KEY_ENC_OPTS_PFCP:
1668 if (key->enc_opts.dst_opt_type) {
1669 NL_SET_ERR_MSG_MOD(extack, "Duplicate type for pfcp options");
1670 return -EINVAL;
1671 }
1672 option_len = 0;
1673 key->enc_opts.dst_opt_type = IP_TUNNEL_PFCP_OPT_BIT;
1674 option_len = fl_set_pfcp_opt(nla_opt_key, key,
1675 key_depth, option_len,
1676 extack);
1677 if (option_len < 0)
1678 return option_len;
1679
1680 key->enc_opts.len += option_len;
1681 /* At the same time we need to parse through the mask
1682 * in order to verify exact and mask attribute lengths.
1683 */
1684 mask->enc_opts.dst_opt_type = IP_TUNNEL_PFCP_OPT_BIT;
1685 option_len = fl_set_pfcp_opt(nla_opt_msk, mask,
1686 msk_depth, option_len,
1687 extack);
1688 if (option_len < 0)
1689 return option_len;
1690
1691 mask->enc_opts.len += option_len;
1692 if (key->enc_opts.len != mask->enc_opts.len) {
1693 NL_SET_ERR_MSG_MOD(extack, "Key and mask miss aligned");
1694 return -EINVAL;
1695 }
1696 break;
0a6e7778
PJV
1697 default:
1698 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
1699 return -EINVAL;
1700 }
c96adff9
CW
1701
1702 if (!msk_depth)
1703 continue;
1704
1705 if (!nla_ok(nla_opt_msk, msk_depth)) {
1706 NL_SET_ERR_MSG(extack, "A mask attribute is invalid");
1707 return -EINVAL;
1708 }
1709 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
0a6e7778
PJV
1710 }
1711
1712 return 0;
1713}
1714
1bcc51ac 1715static int fl_validate_ct_state(u16 state, struct nlattr *tb,
1716 struct netlink_ext_ack *extack)
1717{
1718 if (state && !(state & TCA_FLOWER_KEY_CT_FLAGS_TRACKED)) {
1719 NL_SET_ERR_MSG_ATTR(extack, tb,
1720 "no trk, so no other flag can be set");
1721 return -EINVAL;
1722 }
1723
1724 if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
1725 state & TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED) {
1726 NL_SET_ERR_MSG_ATTR(extack, tb,
1727 "new and est are mutually exclusive");
1728 return -EINVAL;
1729 }
1730
3aed8b63 1731 if (state & TCA_FLOWER_KEY_CT_FLAGS_INVALID &&
1732 state & ~(TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
1733 TCA_FLOWER_KEY_CT_FLAGS_INVALID)) {
1734 NL_SET_ERR_MSG_ATTR(extack, tb,
1735 "when inv is set, only trk may be set");
1736 return -EINVAL;
1737 }
1738
1739 if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
1740 state & TCA_FLOWER_KEY_CT_FLAGS_REPLY) {
1741 NL_SET_ERR_MSG_ATTR(extack, tb,
1742 "new and rpl are mutually exclusive");
1743 return -EINVAL;
1744 }
1745
1bcc51ac 1746 return 0;
1747}
1748
e0ace68a
PB
1749static int fl_set_key_ct(struct nlattr **tb,
1750 struct flow_dissector_key_ct *key,
1751 struct flow_dissector_key_ct *mask,
1752 struct netlink_ext_ack *extack)
1753{
1754 if (tb[TCA_FLOWER_KEY_CT_STATE]) {
1bcc51ac 1755 int err;
1756
e0ace68a
PB
1757 if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) {
1758 NL_SET_ERR_MSG(extack, "Conntrack isn't enabled");
1759 return -EOPNOTSUPP;
1760 }
1761 fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
1762 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
1763 sizeof(key->ct_state));
1bcc51ac 1764
afa536d8 1765 err = fl_validate_ct_state(key->ct_state & mask->ct_state,
1bcc51ac 1766 tb[TCA_FLOWER_KEY_CT_STATE_MASK],
1767 extack);
1768 if (err)
1769 return err;
1770
e0ace68a
PB
1771 }
1772 if (tb[TCA_FLOWER_KEY_CT_ZONE]) {
1773 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1774 NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled");
1775 return -EOPNOTSUPP;
1776 }
1777 fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
1778 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
1779 sizeof(key->ct_zone));
1780 }
1781 if (tb[TCA_FLOWER_KEY_CT_MARK]) {
1782 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1783 NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled");
1784 return -EOPNOTSUPP;
1785 }
1786 fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
1787 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
1788 sizeof(key->ct_mark));
1789 }
1790 if (tb[TCA_FLOWER_KEY_CT_LABELS]) {
1791 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1792 NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled");
1793 return -EOPNOTSUPP;
1794 }
1795 fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
1796 mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
1797 sizeof(key->ct_labels));
1798 }
1799
1800 return 0;
1801}
1802
285ba06b 1803static bool is_vlan_key(struct nlattr *tb, __be16 *ethertype,
99fdb22b
BS
1804 struct fl_flow_key *key, struct fl_flow_key *mask,
1805 int vthresh)
285ba06b 1806{
99fdb22b
BS
1807 const bool good_num_of_vlans = key->num_of_vlans.num_of_vlans > vthresh;
1808
1809 if (!tb) {
1810 *ethertype = 0;
1811 return good_num_of_vlans;
1812 }
285ba06b
BS
1813
1814 *ethertype = nla_get_be16(tb);
99fdb22b 1815 if (good_num_of_vlans || eth_type_vlan(*ethertype))
285ba06b
BS
1816 return true;
1817
1818 key->basic.n_proto = *ethertype;
1819 mask->basic.n_proto = cpu_to_be16(~0);
1820 return false;
1821}
1822
7cfffd5f
ZD
1823static void fl_set_key_cfm_md_level(struct nlattr **tb,
1824 struct fl_flow_key *key,
1825 struct fl_flow_key *mask,
1826 struct netlink_ext_ack *extack)
1827{
1828 u8 level;
1829
1830 if (!tb[TCA_FLOWER_KEY_CFM_MD_LEVEL])
1831 return;
1832
1833 level = nla_get_u8(tb[TCA_FLOWER_KEY_CFM_MD_LEVEL]);
1834 key->cfm.mdl_ver = FIELD_PREP(FLOW_DIS_CFM_MDL_MASK, level);
1835 mask->cfm.mdl_ver = FLOW_DIS_CFM_MDL_MASK;
1836}
1837
1838static void fl_set_key_cfm_opcode(struct nlattr **tb,
1839 struct fl_flow_key *key,
1840 struct fl_flow_key *mask,
1841 struct netlink_ext_ack *extack)
1842{
1843 fl_set_key_val(tb, &key->cfm.opcode, TCA_FLOWER_KEY_CFM_OPCODE,
1844 &mask->cfm.opcode, TCA_FLOWER_UNSPEC,
1845 sizeof(key->cfm.opcode));
1846}
1847
1848static int fl_set_key_cfm(struct nlattr **tb,
1849 struct fl_flow_key *key,
1850 struct fl_flow_key *mask,
1851 struct netlink_ext_ack *extack)
1852{
4d50e500 1853 struct nlattr *nla_cfm_opt[TCA_FLOWER_KEY_CFM_OPT_MAX + 1];
7cfffd5f
ZD
1854 int err;
1855
1856 if (!tb[TCA_FLOWER_KEY_CFM])
1857 return 0;
1858
1859 err = nla_parse_nested(nla_cfm_opt, TCA_FLOWER_KEY_CFM_OPT_MAX,
1860 tb[TCA_FLOWER_KEY_CFM], cfm_opt_policy, extack);
1861 if (err < 0)
1862 return err;
1863
1864 fl_set_key_cfm_opcode(nla_cfm_opt, key, mask, extack);
1865 fl_set_key_cfm_md_level(nla_cfm_opt, key, mask, extack);
1866
1867 return 0;
1868}
1869
536b97ac
AST
1870static int fl_set_key(struct net *net, struct nlattr *tca_opts,
1871 struct nlattr **tb, struct fl_flow_key *key,
1872 struct fl_flow_key *mask, struct netlink_ext_ack *extack)
77b9900e 1873{
9399ae9a 1874 __be16 ethertype;
d9724772 1875 int ret = 0;
a5148626 1876
77b9900e 1877 if (tb[TCA_FLOWER_INDEV]) {
1057c55f 1878 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
77b9900e
JP
1879 if (err < 0)
1880 return err;
8212ed77
JP
1881 key->meta.ingress_ifindex = err;
1882 mask->meta.ingress_ifindex = 0xffffffff;
77b9900e
JP
1883 }
1884
1a432018
IS
1885 fl_set_key_val(tb, &key->meta.l2_miss, TCA_FLOWER_L2_MISS,
1886 &mask->meta.l2_miss, TCA_FLOWER_UNSPEC,
1887 sizeof(key->meta.l2_miss));
1888
77b9900e
JP
1889 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1890 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1891 sizeof(key->eth.dst));
1892 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1893 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1894 sizeof(key->eth.src));
b4000312
BS
1895 fl_set_key_val(tb, &key->num_of_vlans,
1896 TCA_FLOWER_KEY_NUM_OF_VLANS,
1897 &mask->num_of_vlans,
1898 TCA_FLOWER_UNSPEC,
1899 sizeof(key->num_of_vlans));
66530bdf 1900
99fdb22b 1901 if (is_vlan_key(tb[TCA_FLOWER_KEY_ETH_TYPE], &ethertype, key, mask, 0)) {
6ee59e55
BS
1902 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
1903 TCA_FLOWER_KEY_VLAN_PRIO,
1904 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1905 &key->vlan, &mask->vlan);
1906
99fdb22b
BS
1907 if (is_vlan_key(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE],
1908 &ethertype, key, mask, 1)) {
6ee59e55
BS
1909 fl_set_key_vlan(tb, ethertype,
1910 TCA_FLOWER_KEY_CVLAN_ID,
1911 TCA_FLOWER_KEY_CVLAN_PRIO,
1912 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1913 &key->cvlan, &mask->cvlan);
1914 fl_set_key_val(tb, &key->basic.n_proto,
1915 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1916 &mask->basic.n_proto,
1917 TCA_FLOWER_UNSPEC,
1918 sizeof(key->basic.n_proto));
1919 }
9399ae9a 1920 }
66530bdf 1921
5008750e
WD
1922 if (key->basic.n_proto == htons(ETH_P_PPP_SES))
1923 fl_set_key_pppoe(tb, &key->pppoe, &mask->pppoe, key, mask);
1924
77b9900e
JP
1925 if (key->basic.n_proto == htons(ETH_P_IP) ||
1926 key->basic.n_proto == htons(ETH_P_IPV6)) {
1927 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1928 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1929 sizeof(key->basic.ip_proto));
0e2c17b6 1930 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
77b9900e 1931 }
66530bdf
JHS
1932
1933 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1934 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
970bfcd0 1935 mask->control.addr_type = ~0;
77b9900e
JP
1936 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1937 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1938 sizeof(key->ipv4.src));
1939 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1940 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1941 sizeof(key->ipv4.dst));
66530bdf
JHS
1942 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1943 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
970bfcd0 1944 mask->control.addr_type = ~0;
77b9900e
JP
1945 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1946 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1947 sizeof(key->ipv6.src));
1948 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1949 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1950 sizeof(key->ipv6.dst));
1951 }
66530bdf 1952
77b9900e
JP
1953 if (key->basic.ip_proto == IPPROTO_TCP) {
1954 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
aa72d708 1955 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
77b9900e
JP
1956 sizeof(key->tp.src));
1957 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
aa72d708 1958 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
77b9900e 1959 sizeof(key->tp.dst));
fdfc7dd6
JP
1960 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1961 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1962 sizeof(key->tcp.flags));
77b9900e
JP
1963 } else if (key->basic.ip_proto == IPPROTO_UDP) {
1964 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
aa72d708 1965 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
77b9900e
JP
1966 sizeof(key->tp.src));
1967 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
aa72d708 1968 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
77b9900e 1969 sizeof(key->tp.dst));
5976c5f4
SH
1970 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
1971 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1972 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1973 sizeof(key->tp.src));
1974 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1975 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1976 sizeof(key->tp.dst));
7b684884
SH
1977 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
1978 key->basic.ip_proto == IPPROTO_ICMP) {
1979 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1980 &mask->icmp.type,
1981 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1982 sizeof(key->icmp.type));
1983 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1984 &mask->icmp.code,
1985 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1986 sizeof(key->icmp.code));
1987 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1988 key->basic.ip_proto == IPPROTO_ICMPV6) {
1989 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1990 &mask->icmp.type,
1991 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1992 sizeof(key->icmp.type));
040587af 1993 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
7b684884 1994 &mask->icmp.code,
040587af 1995 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
7b684884 1996 sizeof(key->icmp.code));
a577d8f7
BL
1997 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1998 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
442f730e 1999 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack);
1a7fca63
BL
2000 if (ret)
2001 return ret;
99d31326
SH
2002 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
2003 key->basic.n_proto == htons(ETH_P_RARP)) {
2004 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
2005 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
2006 sizeof(key->arp.sip));
2007 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
2008 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
2009 sizeof(key->arp.tip));
2010 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
2011 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
2012 sizeof(key->arp.op));
2013 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
2014 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
2015 sizeof(key->arp.sha));
2016 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
2017 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
2018 sizeof(key->arp.tha));
8b189ea0
WD
2019 } else if (key->basic.ip_proto == IPPROTO_L2TP) {
2020 fl_set_key_val(tb, &key->l2tpv3.session_id,
2021 TCA_FLOWER_KEY_L2TPV3_SID,
2022 &mask->l2tpv3.session_id, TCA_FLOWER_UNSPEC,
2023 sizeof(key->l2tpv3.session_id));
7cfffd5f
ZD
2024 } else if (key->basic.n_proto == htons(ETH_P_CFM)) {
2025 ret = fl_set_key_cfm(tb, key, mask, extack);
2026 if (ret)
2027 return ret;
77b9900e
JP
2028 }
2029
5c72299f
AN
2030 if (key->basic.ip_proto == IPPROTO_TCP ||
2031 key->basic.ip_proto == IPPROTO_UDP ||
2032 key->basic.ip_proto == IPPROTO_SCTP) {
bd7d4c12 2033 ret = fl_set_key_port_range(tb, key, mask, extack);
5c72299f
AN
2034 if (ret)
2035 return ret;
2036 }
2037
4c13eda7
RK
2038 if (tb[TCA_FLOWER_KEY_SPI]) {
2039 ret = fl_set_key_spi(tb, key, mask, extack);
2040 if (ret)
2041 return ret;
2042 }
2043
bc3103f1
AV
2044 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
2045 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
2046 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
970bfcd0 2047 mask->enc_control.addr_type = ~0;
bc3103f1
AV
2048 fl_set_key_val(tb, &key->enc_ipv4.src,
2049 TCA_FLOWER_KEY_ENC_IPV4_SRC,
2050 &mask->enc_ipv4.src,
2051 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
2052 sizeof(key->enc_ipv4.src));
2053 fl_set_key_val(tb, &key->enc_ipv4.dst,
2054 TCA_FLOWER_KEY_ENC_IPV4_DST,
2055 &mask->enc_ipv4.dst,
2056 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
2057 sizeof(key->enc_ipv4.dst));
2058 }
2059
2060 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
2061 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
2062 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
970bfcd0 2063 mask->enc_control.addr_type = ~0;
bc3103f1
AV
2064 fl_set_key_val(tb, &key->enc_ipv6.src,
2065 TCA_FLOWER_KEY_ENC_IPV6_SRC,
2066 &mask->enc_ipv6.src,
2067 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
2068 sizeof(key->enc_ipv6.src));
2069 fl_set_key_val(tb, &key->enc_ipv6.dst,
2070 TCA_FLOWER_KEY_ENC_IPV6_DST,
2071 &mask->enc_ipv6.dst,
2072 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
2073 sizeof(key->enc_ipv6.dst));
2074 }
2075
2076 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
eb523f42 2077 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
bc3103f1
AV
2078 sizeof(key->enc_key_id.keyid));
2079
f4d997fd
HHZ
2080 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
2081 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
2082 sizeof(key->enc_tp.src));
2083
2084 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
2085 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
2086 sizeof(key->enc_tp.dst));
2087
0e2c17b6
OG
2088 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
2089
5923b8f7
AL
2090 fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
2091 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
2092 sizeof(key->hash.hash));
2093
0a6e7778
PJV
2094 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
2095 ret = fl_set_enc_opt(tb, key, mask, extack);
2096 if (ret)
2097 return ret;
2098 }
2099
e0ace68a
PB
2100 ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack);
2101 if (ret)
2102 return ret;
2103
1d17568e 2104 if (tb[TCA_FLOWER_KEY_FLAGS]) {
536b97ac
AST
2105 ret = fl_set_key_flags(tca_opts, tb, false,
2106 &key->control.flags,
e304e21a 2107 &mask->control.flags, extack);
1d17568e
DC
2108 if (ret)
2109 return ret;
2110 }
2111
2112 if (tb[TCA_FLOWER_KEY_ENC_FLAGS])
536b97ac
AST
2113 ret = fl_set_key_flags(tca_opts, tb, true,
2114 &key->enc_control.flags,
11036bd7 2115 &mask->enc_control.flags, extack);
faa3ffce 2116
d9724772 2117 return ret;
77b9900e
JP
2118}
2119
05cd271f
PB
2120static void fl_mask_copy(struct fl_flow_mask *dst,
2121 struct fl_flow_mask *src)
77b9900e 2122{
05cd271f
PB
2123 const void *psrc = fl_key_get_start(&src->key, src);
2124 void *pdst = fl_key_get_start(&dst->key, src);
77b9900e 2125
05cd271f
PB
2126 memcpy(pdst, psrc, fl_mask_range(src));
2127 dst->range = src->range;
77b9900e
JP
2128}
2129
2130static const struct rhashtable_params fl_ht_params = {
2131 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
2132 .head_offset = offsetof(struct cls_fl_filter, ht_node),
2133 .automatic_shrinking = true,
2134};
2135
05cd271f 2136static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
77b9900e 2137{
05cd271f
PB
2138 mask->filter_ht_params = fl_ht_params;
2139 mask->filter_ht_params.key_len = fl_mask_range(mask);
2140 mask->filter_ht_params.key_offset += mask->range.start;
77b9900e 2141
05cd271f 2142 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
77b9900e
JP
2143}
2144
2145#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
c593642c 2146#define FL_KEY_MEMBER_SIZE(member) sizeof_field(struct fl_flow_key, member)
77b9900e 2147
339ba878
HHZ
2148#define FL_KEY_IS_MASKED(mask, member) \
2149 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
2150 0, FL_KEY_MEMBER_SIZE(member)) \
77b9900e
JP
2151
2152#define FL_KEY_SET(keys, cnt, id, member) \
2153 do { \
2154 keys[cnt].key_id = id; \
2155 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
2156 cnt++; \
2157 } while(0);
2158
339ba878 2159#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
77b9900e 2160 do { \
339ba878 2161 if (FL_KEY_IS_MASKED(mask, member)) \
77b9900e
JP
2162 FL_KEY_SET(keys, cnt, id, member); \
2163 } while(0);
2164
33fb5cba
JP
2165static void fl_init_dissector(struct flow_dissector *dissector,
2166 struct fl_flow_key *mask)
77b9900e
JP
2167{
2168 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
2169 size_t cnt = 0;
2170
8212ed77
JP
2171 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2172 FLOW_DISSECTOR_KEY_META, meta);
42aecaa9 2173 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
77b9900e 2174 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
33fb5cba 2175 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
339ba878 2176 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
33fb5cba 2177 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
339ba878 2178 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
33fb5cba 2179 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
339ba878 2180 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
8ffb055b
YK
2181 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2182 FLOW_DISSECTOR_KEY_PORTS, tp);
2183 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2184 FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range);
33fb5cba 2185 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
4d80cc0a 2186 FLOW_DISSECTOR_KEY_IP, ip);
33fb5cba 2187 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
fdfc7dd6 2188 FLOW_DISSECTOR_KEY_TCP, tcp);
33fb5cba 2189 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
7b684884 2190 FLOW_DISSECTOR_KEY_ICMP, icmp);
33fb5cba 2191 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
99d31326 2192 FLOW_DISSECTOR_KEY_ARP, arp);
33fb5cba 2193 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
a577d8f7 2194 FLOW_DISSECTOR_KEY_MPLS, mpls);
33fb5cba 2195 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
9399ae9a 2196 FLOW_DISSECTOR_KEY_VLAN, vlan);
33fb5cba 2197 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
d64efd09 2198 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
33fb5cba 2199 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
519d1052 2200 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
33fb5cba 2201 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
519d1052 2202 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
33fb5cba 2203 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
519d1052 2204 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
33fb5cba 2205 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
706bf4f4
AST
2206 FL_KEY_IS_MASKED(mask, enc_ipv6) ||
2207 FL_KEY_IS_MASKED(mask, enc_control))
519d1052
HHZ
2208 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
2209 enc_control);
33fb5cba 2210 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
f4d997fd 2211 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
33fb5cba 2212 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
0e2c17b6 2213 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
0a6e7778
PJV
2214 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2215 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
e0ace68a
PB
2216 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2217 FLOW_DISSECTOR_KEY_CT, ct);
5923b8f7
AL
2218 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2219 FLOW_DISSECTOR_KEY_HASH, hash);
b4000312
BS
2220 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2221 FLOW_DISSECTOR_KEY_NUM_OF_VLANS, num_of_vlans);
5008750e
WD
2222 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2223 FLOW_DISSECTOR_KEY_PPPOE, pppoe);
8b189ea0
WD
2224 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2225 FLOW_DISSECTOR_KEY_L2TPV3, l2tpv3);
4c13eda7
RK
2226 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2227 FLOW_DISSECTOR_KEY_IPSEC, ipsec);
7cfffd5f
ZD
2228 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2229 FLOW_DISSECTOR_KEY_CFM, cfm);
77b9900e 2230
33fb5cba 2231 skb_flow_dissector_init(dissector, keys, cnt);
05cd271f
PB
2232}
2233
2234static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
2235 struct fl_flow_mask *mask)
2236{
2237 struct fl_flow_mask *newmask;
2238 int err;
2239
2240 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
2241 if (!newmask)
2242 return ERR_PTR(-ENOMEM);
2243
2244 fl_mask_copy(newmask, mask);
2245
8ffb055b
YK
2246 if ((newmask->key.tp_range.tp_min.dst &&
2247 newmask->key.tp_range.tp_max.dst) ||
2248 (newmask->key.tp_range.tp_min.src &&
2249 newmask->key.tp_range.tp_max.src))
5c72299f
AN
2250 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
2251
05cd271f
PB
2252 err = fl_init_mask_hashtable(newmask);
2253 if (err)
2254 goto errout_free;
2255
33fb5cba 2256 fl_init_dissector(&newmask->dissector, &newmask->key);
05cd271f
PB
2257
2258 INIT_LIST_HEAD_RCU(&newmask->filters);
2259
f48ef4d5 2260 refcount_set(&newmask->refcnt, 1);
195c234d
VB
2261 err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
2262 &newmask->ht_node, mask_ht_params);
05cd271f
PB
2263 if (err)
2264 goto errout_destroy;
2265
259e60f9 2266 spin_lock(&head->masks_lock);
05cd271f 2267 list_add_tail_rcu(&newmask->list, &head->masks);
259e60f9 2268 spin_unlock(&head->masks_lock);
05cd271f
PB
2269
2270 return newmask;
2271
2272errout_destroy:
2273 rhashtable_destroy(&newmask->ht);
2274errout_free:
2275 kfree(newmask);
2276
2277 return ERR_PTR(err);
77b9900e
JP
2278}
2279
2280static int fl_check_assign_mask(struct cls_fl_head *head,
05cd271f
PB
2281 struct cls_fl_filter *fnew,
2282 struct cls_fl_filter *fold,
77b9900e
JP
2283 struct fl_flow_mask *mask)
2284{
05cd271f 2285 struct fl_flow_mask *newmask;
f48ef4d5 2286 int ret = 0;
77b9900e 2287
f48ef4d5 2288 rcu_read_lock();
195c234d
VB
2289
2290 /* Insert mask as temporary node to prevent concurrent creation of mask
2291 * with same key. Any concurrent lookups with same key will return
99815f50 2292 * -EAGAIN because mask's refcnt is zero.
195c234d
VB
2293 */
2294 fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
2295 &mask->ht_node,
2296 mask_ht_params);
05cd271f 2297 if (!fnew->mask) {
f48ef4d5
VB
2298 rcu_read_unlock();
2299
195c234d
VB
2300 if (fold) {
2301 ret = -EINVAL;
2302 goto errout_cleanup;
2303 }
77b9900e 2304
05cd271f 2305 newmask = fl_create_new_mask(head, mask);
195c234d
VB
2306 if (IS_ERR(newmask)) {
2307 ret = PTR_ERR(newmask);
2308 goto errout_cleanup;
2309 }
77b9900e 2310
05cd271f 2311 fnew->mask = newmask;
f48ef4d5 2312 return 0;
195c234d
VB
2313 } else if (IS_ERR(fnew->mask)) {
2314 ret = PTR_ERR(fnew->mask);
f6521c58 2315 } else if (fold && fold->mask != fnew->mask) {
f48ef4d5
VB
2316 ret = -EINVAL;
2317 } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
2318 /* Mask was deleted concurrently, try again */
2319 ret = -EAGAIN;
05cd271f 2320 }
f48ef4d5
VB
2321 rcu_read_unlock();
2322 return ret;
195c234d
VB
2323
2324errout_cleanup:
2325 rhashtable_remove_fast(&head->ht, &mask->ht_node,
2326 mask_ht_params);
195c234d 2327 return ret;
77b9900e
JP
2328}
2329
1a432018
IS
2330static bool fl_needs_tc_skb_ext(const struct fl_flow_key *mask)
2331{
2332 return mask->meta.l2_miss;
2333}
2334
1f17f774
VB
2335static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
2336 struct cls_fl_filter *fold,
2337 bool *in_ht)
2338{
2339 struct fl_flow_mask *mask = fnew->mask;
2340 int err;
2341
9e35552a
VB
2342 err = rhashtable_lookup_insert_fast(&mask->ht,
2343 &fnew->ht_node,
2344 mask->filter_ht_params);
1f17f774
VB
2345 if (err) {
2346 *in_ht = false;
2347 /* It is okay if filter with same key exists when
2348 * overwriting.
2349 */
2350 return fold && err == -EEXIST ? 0 : err;
2351 }
2352
2353 *in_ht = true;
2354 return 0;
2355}
2356
77b9900e
JP
2357static int fl_change(struct net *net, struct sk_buff *in_skb,
2358 struct tcf_proto *tp, unsigned long base,
2359 u32 handle, struct nlattr **tca,
695176bf 2360 void **arg, u32 flags,
12db03b6 2361 struct netlink_ext_ack *extack)
77b9900e 2362{
e474619a 2363 struct cls_fl_head *head = fl_head_dereference(tp);
695176bf 2364 bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL);
536b97ac 2365 struct nlattr *tca_opts = tca[TCA_OPTIONS];
8113c095 2366 struct cls_fl_filter *fold = *arg;
ac177a33 2367 bool bound_to_filter = false;
77b9900e 2368 struct cls_fl_filter *fnew;
2cddd201 2369 struct fl_flow_mask *mask;
39b7b6a6 2370 struct nlattr **tb;
1f17f774 2371 bool in_ht;
77b9900e
JP
2372 int err;
2373
536b97ac 2374 if (!tca_opts) {
06177558
VB
2375 err = -EINVAL;
2376 goto errout_fold;
2377 }
77b9900e 2378
2cddd201 2379 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
06177558
VB
2380 if (!mask) {
2381 err = -ENOBUFS;
2382 goto errout_fold;
2383 }
39b7b6a6 2384
2cddd201
IV
2385 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2386 if (!tb) {
2387 err = -ENOBUFS;
2388 goto errout_mask_alloc;
2389 }
2390
8cb08174 2391 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
536b97ac 2392 tca_opts, fl_policy, NULL);
77b9900e 2393 if (err < 0)
39b7b6a6 2394 goto errout_tb;
77b9900e 2395
39b7b6a6
AB
2396 if (fold && handle && fold->handle != handle) {
2397 err = -EINVAL;
2398 goto errout_tb;
2399 }
77b9900e
JP
2400
2401 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
39b7b6a6
AB
2402 if (!fnew) {
2403 err = -ENOBUFS;
2404 goto errout_tb;
2405 }
c049d56e 2406 INIT_LIST_HEAD(&fnew->hw_list);
06177558 2407 refcount_set(&fnew->refcnt, 1);
77b9900e 2408
e69985c6
AV
2409 if (tb[TCA_FLOWER_FLAGS]) {
2410 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
2411
2412 if (!tc_flags_valid(fnew->flags)) {
dfd2f0eb 2413 kfree(fnew);
e69985c6 2414 err = -EINVAL;
dfd2f0eb 2415 goto errout_tb;
e69985c6
AV
2416 }
2417 }
5b33f488 2418
08a0063d
PB
2419 if (!fold) {
2420 spin_lock(&tp->lock);
2421 if (!handle) {
2422 handle = 1;
dd4f6bbf 2423 err = idr_alloc_u32(&head->handle_idr, NULL, &handle,
08a0063d
PB
2424 INT_MAX, GFP_ATOMIC);
2425 } else {
dd4f6bbf 2426 err = idr_alloc_u32(&head->handle_idr, NULL, &handle,
08a0063d
PB
2427 handle, GFP_ATOMIC);
2428
2429 /* Filter with specified handle was concurrently
2430 * inserted after initial check in cls_api. This is not
2431 * necessarily an error if NLM_F_EXCL is not set in
2432 * message flags. Returning EAGAIN will cause cls_api to
2433 * try to update concurrently inserted rule.
2434 */
2435 if (err == -ENOSPC)
2436 err = -EAGAIN;
2437 }
2438 spin_unlock(&tp->lock);
2439
dfd2f0eb
ED
2440 if (err) {
2441 kfree(fnew);
2442 goto errout_tb;
2443 }
08a0063d 2444 }
5110f3ff 2445 fnew->handle = handle;
08a0063d 2446
606c7c43
PB
2447 err = tcf_exts_init_ex(&fnew->exts, net, TCA_FLOWER_ACT, 0, tp, handle,
2448 !tc_skip_hw(fnew->flags));
08a0063d
PB
2449 if (err < 0)
2450 goto errout_idr;
2451
ac177a33
VN
2452 err = tcf_exts_validate_ex(net, tp, tb, tca[TCA_RATE],
2453 &fnew->exts, flags, fnew->flags,
2454 extack);
2455 if (err < 0)
08a0063d 2456 goto errout_idr;
77b9900e 2457
ac177a33
VN
2458 if (tb[TCA_FLOWER_CLASSID]) {
2459 fnew->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
2460 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2461 rtnl_lock();
2462 tcf_bind_filter(tp, &fnew->res, base);
2463 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2464 rtnl_unlock();
2465 bound_to_filter = true;
2466 }
2467
536b97ac 2468 err = fl_set_key(net, tca_opts, tb, &fnew->key, &mask->key, extack);
ac177a33
VN
2469 if (err)
2470 goto unbind_filter;
2471
2472 fl_mask_update_range(mask);
2473 fl_set_masked_key(&fnew->mkey, &fnew->key, mask);
2474
2475 if (!fl_mask_fits_tmplt(tp->chain->tmplt_priv, mask)) {
2476 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
2477 err = -EINVAL;
2478 goto unbind_filter;
2479 }
2480
2481 /* Enable tc skb extension if filter matches on data extracted from
2482 * this extension.
2483 */
2484 if (fl_needs_tc_skb_ext(&mask->key)) {
2485 fnew->needs_tc_skb_ext = 1;
2486 tc_skb_ext_tc_enable();
2487 }
2488
2cddd201 2489 err = fl_check_assign_mask(head, fnew, fold, mask);
77b9900e 2490 if (err)
ac177a33 2491 goto unbind_filter;
ecb3dea4 2492
1f17f774
VB
2493 err = fl_ht_insert_unique(fnew, fold, &in_ht);
2494 if (err)
2495 goto errout_mask;
2496
79685219 2497 if (!tc_skip_hw(fnew->flags)) {
c24e43d8 2498 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
79685219 2499 if (err)
1f17f774 2500 goto errout_ht;
79685219 2501 }
5b33f488 2502
55593960
OG
2503 if (!tc_in_hw(fnew->flags))
2504 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
2505
a12c76a0
XL
2506 tcf_proto_update_usesw(tp, fnew->flags);
2507
3d81e711
VB
2508 spin_lock(&tp->lock);
2509
272ffaad
VB
2510 /* tp was deleted concurrently. -EAGAIN will cause caller to lookup
2511 * proto again or create new one, if necessary.
2512 */
2513 if (tp->deleting) {
2514 err = -EAGAIN;
2515 goto errout_hw;
2516 }
2517
5b33f488 2518 if (fold) {
b2552b8c
VB
2519 /* Fold filter was deleted concurrently. Retry lookup. */
2520 if (fold->deleted) {
2521 err = -EAGAIN;
2522 goto errout_hw;
2523 }
2524
620da486
VB
2525 fnew->handle = handle;
2526
1f17f774
VB
2527 if (!in_ht) {
2528 struct rhashtable_params params =
2529 fnew->mask->filter_ht_params;
2530
2531 err = rhashtable_insert_fast(&fnew->mask->ht,
2532 &fnew->ht_node,
2533 params);
2534 if (err)
2535 goto errout_hw;
2536 in_ht = true;
2537 }
620da486 2538
c049d56e 2539 refcount_inc(&fnew->refcnt);
599d2570
RD
2540 rhashtable_remove_fast(&fold->mask->ht,
2541 &fold->ht_node,
2542 fold->mask->filter_ht_params);
234a4624 2543 idr_replace(&head->handle_idr, fnew, fnew->handle);
ff3532f2 2544 list_replace_rcu(&fold->list, &fnew->list);
b2552b8c 2545 fold->deleted = true;
620da486 2546
3d81e711
VB
2547 spin_unlock(&tp->lock);
2548
9994677c 2549 fl_mask_put(head, fold->mask);
620da486 2550 if (!tc_skip_hw(fold->flags))
c24e43d8 2551 fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
77b9900e 2552 tcf_unbind_filter(tp, &fold->res);
06177558
VB
2553 /* Caller holds reference to fold, so refcnt is always > 0
2554 * after this.
2555 */
2556 refcount_dec(&fold->refcnt);
2557 __fl_put(fold);
77b9900e 2558 } else {
08a0063d 2559 idr_replace(&head->handle_idr, fnew, fnew->handle);
620da486 2560
c049d56e 2561 refcount_inc(&fnew->refcnt);
05cd271f 2562 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
3d81e711 2563 spin_unlock(&tp->lock);
77b9900e
JP
2564 }
2565
620da486
VB
2566 *arg = fnew;
2567
39b7b6a6 2568 kfree(tb);
99815f50 2569 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
77b9900e
JP
2570 return 0;
2571
c049d56e
VB
2572errout_ht:
2573 spin_lock(&tp->lock);
620da486 2574errout_hw:
c049d56e 2575 fnew->deleted = true;
3d81e711 2576 spin_unlock(&tp->lock);
620da486 2577 if (!tc_skip_hw(fnew->flags))
c24e43d8 2578 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
1f17f774
VB
2579 if (in_ht)
2580 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
2581 fnew->mask->filter_ht_params);
ecb3dea4 2582errout_mask:
9994677c 2583 fl_mask_put(head, fnew->mask);
ac177a33
VN
2584
2585unbind_filter:
2586 if (bound_to_filter) {
2587 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2588 rtnl_lock();
2589 tcf_unbind_filter(tp, &fnew->res);
2590 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2591 rtnl_unlock();
2592 }
2593
08a0063d 2594errout_idr:
1fde0ca3
JL
2595 if (!fold) {
2596 spin_lock(&tp->lock);
fd741f0d 2597 idr_remove(&head->handle_idr, fnew->handle);
1fde0ca3
JL
2598 spin_unlock(&tp->lock);
2599 }
c049d56e 2600 __fl_put(fnew);
39b7b6a6
AB
2601errout_tb:
2602 kfree(tb);
2cddd201 2603errout_mask_alloc:
99815f50 2604 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
06177558
VB
2605errout_fold:
2606 if (fold)
2607 __fl_put(fold);
77b9900e
JP
2608 return err;
2609}
2610
571acf21 2611static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
12db03b6 2612 bool rtnl_held, struct netlink_ext_ack *extack)
77b9900e 2613{
e474619a 2614 struct cls_fl_head *head = fl_head_dereference(tp);
8113c095 2615 struct cls_fl_filter *f = arg;
b2552b8c
VB
2616 bool last_on_mask;
2617 int err = 0;
77b9900e 2618
c24e43d8 2619 err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
05cd271f 2620 *last = list_empty(&head->masks);
06177558
VB
2621 __fl_put(f);
2622
b2552b8c 2623 return err;
77b9900e
JP
2624}
2625
12db03b6
VB
2626static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
2627 bool rtnl_held)
77b9900e 2628{
d39d7149
CW
2629 struct cls_fl_head *head = fl_head_dereference(tp);
2630 unsigned long id = arg->cookie, tmp;
77b9900e 2631 struct cls_fl_filter *f;
05cd271f 2632
01683a14
VB
2633 arg->count = arg->skip;
2634
d5ef1906 2635 rcu_read_lock();
d39d7149
CW
2636 idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) {
2637 /* don't return filters that are being deleted */
dd4f6bbf 2638 if (!f || !refcount_inc_not_zero(&f->refcnt))
d39d7149 2639 continue;
d5ef1906
VB
2640 rcu_read_unlock();
2641
01683a14 2642 if (arg->fn(tp, f, arg) < 0) {
06177558 2643 __fl_put(f);
01683a14 2644 arg->stop = 1;
d5ef1906 2645 rcu_read_lock();
01683a14 2646 break;
05cd271f 2647 }
06177558 2648 __fl_put(f);
01683a14 2649 arg->count++;
d5ef1906 2650 rcu_read_lock();
77b9900e 2651 }
d5ef1906 2652 rcu_read_unlock();
d39d7149 2653 arg->cookie = id;
77b9900e
JP
2654}
2655
c049d56e
VB
2656static struct cls_fl_filter *
2657fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
2658{
2659 struct cls_fl_head *head = fl_head_dereference(tp);
2660
2661 spin_lock(&tp->lock);
2662 if (list_empty(&head->hw_filters)) {
2663 spin_unlock(&tp->lock);
2664 return NULL;
2665 }
2666
2667 if (!f)
2668 f = list_entry(&head->hw_filters, struct cls_fl_filter,
2669 hw_list);
2670 list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
2671 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
2672 spin_unlock(&tp->lock);
2673 return f;
2674 }
2675 }
2676
2677 spin_unlock(&tp->lock);
2678 return NULL;
2679}
2680
a7323311 2681static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
31533cba
JH
2682 void *cb_priv, struct netlink_ext_ack *extack)
2683{
31533cba 2684 struct tcf_block *block = tp->chain->block;
f9e30088 2685 struct flow_cls_offload cls_flower = {};
c049d56e 2686 struct cls_fl_filter *f = NULL;
31533cba
JH
2687 int err;
2688
c049d56e
VB
2689 /* hw_filters list can only be changed by hw offload functions after
2690 * obtaining rtnl lock. Make sure it is not changed while reoffload is
2691 * iterating it.
2692 */
2693 ASSERT_RTNL();
3a7b6861 2694
c049d56e 2695 while ((f = fl_get_next_hw_filter(tp, f, add))) {
95e27a4d
JH
2696 cls_flower.rule =
2697 flow_rule_alloc(tcf_exts_num_actions(&f->exts));
2698 if (!cls_flower.rule) {
2699 __fl_put(f);
2700 return -ENOMEM;
2701 }
31533cba 2702
95e27a4d 2703 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
d6787147 2704 extack);
95e27a4d 2705 cls_flower.command = add ?
f9e30088 2706 FLOW_CLS_REPLACE : FLOW_CLS_DESTROY;
95e27a4d
JH
2707 cls_flower.cookie = (unsigned long)f;
2708 cls_flower.rule->match.dissector = &f->mask->dissector;
2709 cls_flower.rule->match.mask = &f->mask->key;
2710 cls_flower.rule->match.key = &f->mkey;
2711
c2ccf84e
IS
2712 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts,
2713 cls_flower.common.extack);
95e27a4d 2714 if (err) {
8f256622 2715 kfree(cls_flower.rule);
95e27a4d 2716 if (tc_skip_sw(f->flags)) {
95e27a4d
JH
2717 __fl_put(f);
2718 return err;
31533cba 2719 }
95e27a4d
JH
2720 goto next_flow;
2721 }
31533cba 2722
95e27a4d
JH
2723 cls_flower.classid = f->res.classid;
2724
40119211
VB
2725 err = tc_setup_cb_reoffload(block, tp, add, cb,
2726 TC_SETUP_CLSFLOWER, &cls_flower,
2727 cb_priv, &f->flags,
2728 &f->in_hw_count);
9c1c0e12 2729 tc_cleanup_offload_action(&cls_flower.rule->action);
95e27a4d
JH
2730 kfree(cls_flower.rule);
2731
2732 if (err) {
40119211
VB
2733 __fl_put(f);
2734 return err;
31533cba 2735 }
95e27a4d 2736next_flow:
95e27a4d 2737 __fl_put(f);
31533cba
JH
2738 }
2739
2740 return 0;
2741}
2742
a449a3e7
VB
2743static void fl_hw_add(struct tcf_proto *tp, void *type_data)
2744{
2745 struct flow_cls_offload *cls_flower = type_data;
2746 struct cls_fl_filter *f =
2747 (struct cls_fl_filter *) cls_flower->cookie;
2748 struct cls_fl_head *head = fl_head_dereference(tp);
2749
2750 spin_lock(&tp->lock);
2751 list_add(&f->hw_list, &head->hw_filters);
2752 spin_unlock(&tp->lock);
2753}
2754
2755static void fl_hw_del(struct tcf_proto *tp, void *type_data)
2756{
2757 struct flow_cls_offload *cls_flower = type_data;
2758 struct cls_fl_filter *f =
2759 (struct cls_fl_filter *) cls_flower->cookie;
2760
2761 spin_lock(&tp->lock);
2762 if (!list_empty(&f->hw_list))
2763 list_del_init(&f->hw_list);
2764 spin_unlock(&tp->lock);
2765}
2766
8f256622
PNA
2767static int fl_hw_create_tmplt(struct tcf_chain *chain,
2768 struct fl_flow_tmplt *tmplt)
34738452 2769{
f9e30088 2770 struct flow_cls_offload cls_flower = {};
34738452 2771 struct tcf_block *block = chain->block;
34738452 2772
e3ab786b 2773 cls_flower.rule = flow_rule_alloc(0);
8f256622
PNA
2774 if (!cls_flower.rule)
2775 return -ENOMEM;
2776
34738452 2777 cls_flower.common.chain_index = chain->index;
f9e30088 2778 cls_flower.command = FLOW_CLS_TMPLT_CREATE;
34738452 2779 cls_flower.cookie = (unsigned long) tmplt;
8f256622
PNA
2780 cls_flower.rule->match.dissector = &tmplt->dissector;
2781 cls_flower.rule->match.mask = &tmplt->mask;
2782 cls_flower.rule->match.key = &tmplt->dummy_key;
34738452
JP
2783
2784 /* We don't care if driver (any of them) fails to handle this
2785 * call. It serves just as a hint for it.
2786 */
40119211 2787 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
8f256622
PNA
2788 kfree(cls_flower.rule);
2789
2790 return 0;
34738452
JP
2791}
2792
2793static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
2794 struct fl_flow_tmplt *tmplt)
2795{
f9e30088 2796 struct flow_cls_offload cls_flower = {};
34738452
JP
2797 struct tcf_block *block = chain->block;
2798
2799 cls_flower.common.chain_index = chain->index;
f9e30088 2800 cls_flower.command = FLOW_CLS_TMPLT_DESTROY;
34738452
JP
2801 cls_flower.cookie = (unsigned long) tmplt;
2802
40119211 2803 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
34738452
JP
2804}
2805
b95ec7eb
JP
2806static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
2807 struct nlattr **tca,
2808 struct netlink_ext_ack *extack)
2809{
536b97ac 2810 struct nlattr *tca_opts = tca[TCA_OPTIONS];
b95ec7eb
JP
2811 struct fl_flow_tmplt *tmplt;
2812 struct nlattr **tb;
2813 int err;
2814
536b97ac 2815 if (!tca_opts)
b95ec7eb
JP
2816 return ERR_PTR(-EINVAL);
2817
2818 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2819 if (!tb)
2820 return ERR_PTR(-ENOBUFS);
8cb08174 2821 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
536b97ac 2822 tca_opts, fl_policy, NULL);
b95ec7eb
JP
2823 if (err)
2824 goto errout_tb;
2825
2826 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
1cbc36a5
DC
2827 if (!tmplt) {
2828 err = -ENOMEM;
b95ec7eb 2829 goto errout_tb;
1cbc36a5 2830 }
b95ec7eb 2831 tmplt->chain = chain;
536b97ac
AST
2832 err = fl_set_key(net, tca_opts, tb, &tmplt->dummy_key,
2833 &tmplt->mask, extack);
b95ec7eb
JP
2834 if (err)
2835 goto errout_tmplt;
b95ec7eb
JP
2836
2837 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
2838
8f256622
PNA
2839 err = fl_hw_create_tmplt(chain, tmplt);
2840 if (err)
2841 goto errout_tmplt;
34738452 2842
8f256622 2843 kfree(tb);
b95ec7eb
JP
2844 return tmplt;
2845
2846errout_tmplt:
2847 kfree(tmplt);
2848errout_tb:
2849 kfree(tb);
2850 return ERR_PTR(err);
2851}
2852
ec3ed293
VB
2853static void fl_tmplt_destroy(void *tmplt_priv)
2854{
2855 struct fl_flow_tmplt *tmplt = tmplt_priv;
2856
95278dda
CW
2857 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
2858 kfree(tmplt);
ec3ed293
VB
2859}
2860
32f2a0af
IS
2861static void fl_tmplt_reoffload(struct tcf_chain *chain, bool add,
2862 flow_setup_cb_t *cb, void *cb_priv)
2863{
2864 struct fl_flow_tmplt *tmplt = chain->tmplt_priv;
2865 struct flow_cls_offload cls_flower = {};
2866
2867 cls_flower.rule = flow_rule_alloc(0);
2868 if (!cls_flower.rule)
2869 return;
2870
2871 cls_flower.common.chain_index = chain->index;
2872 cls_flower.command = add ? FLOW_CLS_TMPLT_CREATE :
2873 FLOW_CLS_TMPLT_DESTROY;
2874 cls_flower.cookie = (unsigned long) tmplt;
2875 cls_flower.rule->match.dissector = &tmplt->dissector;
2876 cls_flower.rule->match.mask = &tmplt->mask;
2877 cls_flower.rule->match.key = &tmplt->dummy_key;
2878
2879 cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
2880 kfree(cls_flower.rule);
2881}
2882
77b9900e
JP
2883static int fl_dump_key_val(struct sk_buff *skb,
2884 void *val, int val_type,
2885 void *mask, int mask_type, int len)
2886{
2887 int err;
2888
2889 if (!memchr_inv(mask, 0, len))
2890 return 0;
2891 err = nla_put(skb, val_type, len, val);
2892 if (err)
2893 return err;
2894 if (mask_type != TCA_FLOWER_UNSPEC) {
2895 err = nla_put(skb, mask_type, len, mask);
2896 if (err)
2897 return err;
2898 }
2899 return 0;
2900}
2901
5c72299f
AN
2902static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
2903 struct fl_flow_key *mask)
2904{
8ffb055b
YK
2905 if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst,
2906 TCA_FLOWER_KEY_PORT_DST_MIN,
2907 &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC,
2908 sizeof(key->tp_range.tp_min.dst)) ||
2909 fl_dump_key_val(skb, &key->tp_range.tp_max.dst,
2910 TCA_FLOWER_KEY_PORT_DST_MAX,
2911 &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC,
2912 sizeof(key->tp_range.tp_max.dst)) ||
2913 fl_dump_key_val(skb, &key->tp_range.tp_min.src,
2914 TCA_FLOWER_KEY_PORT_SRC_MIN,
2915 &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC,
2916 sizeof(key->tp_range.tp_min.src)) ||
2917 fl_dump_key_val(skb, &key->tp_range.tp_max.src,
2918 TCA_FLOWER_KEY_PORT_SRC_MAX,
2919 &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC,
2920 sizeof(key->tp_range.tp_max.src)))
5c72299f
AN
2921 return -1;
2922
2923 return 0;
2924}
2925
61aec25a
GN
2926static int fl_dump_key_mpls_opt_lse(struct sk_buff *skb,
2927 struct flow_dissector_key_mpls *mpls_key,
2928 struct flow_dissector_key_mpls *mpls_mask,
2929 u8 lse_index)
2930{
2931 struct flow_dissector_mpls_lse *lse_mask = &mpls_mask->ls[lse_index];
2932 struct flow_dissector_mpls_lse *lse_key = &mpls_key->ls[lse_index];
2933 int err;
2934
2935 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH,
2936 lse_index + 1);
2937 if (err)
2938 return err;
2939
2940 if (lse_mask->mpls_ttl) {
2941 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL,
2942 lse_key->mpls_ttl);
2943 if (err)
2944 return err;
2945 }
2946 if (lse_mask->mpls_bos) {
2947 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS,
2948 lse_key->mpls_bos);
2949 if (err)
2950 return err;
2951 }
2952 if (lse_mask->mpls_tc) {
2953 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC,
2954 lse_key->mpls_tc);
2955 if (err)
2956 return err;
2957 }
2958 if (lse_mask->mpls_label) {
7fdd375e
GN
2959 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL,
2960 lse_key->mpls_label);
61aec25a
GN
2961 if (err)
2962 return err;
2963 }
2964
2965 return 0;
2966}
2967
2968static int fl_dump_key_mpls_opts(struct sk_buff *skb,
2969 struct flow_dissector_key_mpls *mpls_key,
2970 struct flow_dissector_key_mpls *mpls_mask)
2971{
2972 struct nlattr *opts;
2973 struct nlattr *lse;
2974 u8 lse_index;
2975 int err;
2976
2977 opts = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS);
2978 if (!opts)
2979 return -EMSGSIZE;
2980
2981 for (lse_index = 0; lse_index < FLOW_DIS_MPLS_MAX; lse_index++) {
2982 if (!(mpls_mask->used_lses & 1 << lse_index))
2983 continue;
2984
2985 lse = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS_LSE);
2986 if (!lse) {
2987 err = -EMSGSIZE;
2988 goto err_opts;
2989 }
2990
2991 err = fl_dump_key_mpls_opt_lse(skb, mpls_key, mpls_mask,
2992 lse_index);
2993 if (err)
2994 goto err_opts_lse;
2995 nla_nest_end(skb, lse);
2996 }
2997 nla_nest_end(skb, opts);
2998
2999 return 0;
3000
3001err_opts_lse:
3002 nla_nest_cancel(skb, lse);
3003err_opts:
3004 nla_nest_cancel(skb, opts);
3005
3006 return err;
3007}
3008
a577d8f7
BL
3009static int fl_dump_key_mpls(struct sk_buff *skb,
3010 struct flow_dissector_key_mpls *mpls_key,
3011 struct flow_dissector_key_mpls *mpls_mask)
3012{
58cff782
GN
3013 struct flow_dissector_mpls_lse *lse_mask;
3014 struct flow_dissector_mpls_lse *lse_key;
a577d8f7
BL
3015 int err;
3016
61aec25a 3017 if (!mpls_mask->used_lses)
a577d8f7 3018 return 0;
58cff782
GN
3019
3020 lse_mask = &mpls_mask->ls[0];
3021 lse_key = &mpls_key->ls[0];
3022
61aec25a
GN
3023 /* For backward compatibility, don't use the MPLS nested attributes if
3024 * the rule can be expressed using the old attributes.
3025 */
3026 if (mpls_mask->used_lses & ~1 ||
3027 (!lse_mask->mpls_ttl && !lse_mask->mpls_bos &&
3028 !lse_mask->mpls_tc && !lse_mask->mpls_label))
3029 return fl_dump_key_mpls_opts(skb, mpls_key, mpls_mask);
3030
58cff782 3031 if (lse_mask->mpls_ttl) {
a577d8f7 3032 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
58cff782 3033 lse_key->mpls_ttl);
a577d8f7
BL
3034 if (err)
3035 return err;
3036 }
58cff782 3037 if (lse_mask->mpls_tc) {
a577d8f7 3038 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
58cff782 3039 lse_key->mpls_tc);
a577d8f7
BL
3040 if (err)
3041 return err;
3042 }
58cff782 3043 if (lse_mask->mpls_label) {
a577d8f7 3044 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
58cff782 3045 lse_key->mpls_label);
a577d8f7
BL
3046 if (err)
3047 return err;
3048 }
58cff782 3049 if (lse_mask->mpls_bos) {
a577d8f7 3050 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
58cff782 3051 lse_key->mpls_bos);
a577d8f7
BL
3052 if (err)
3053 return err;
3054 }
3055 return 0;
3056}
3057
0e2c17b6 3058static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
4d80cc0a
OG
3059 struct flow_dissector_key_ip *key,
3060 struct flow_dissector_key_ip *mask)
3061{
0e2c17b6
OG
3062 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
3063 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
3064 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
3065 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
3066
3067 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
3068 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
4d80cc0a
OG
3069 return -1;
3070
3071 return 0;
3072}
3073
9399ae9a 3074static int fl_dump_key_vlan(struct sk_buff *skb,
d64efd09 3075 int vlan_id_key, int vlan_prio_key,
9399ae9a
HHZ
3076 struct flow_dissector_key_vlan *vlan_key,
3077 struct flow_dissector_key_vlan *vlan_mask)
3078{
3079 int err;
3080
3081 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
3082 return 0;
3083 if (vlan_mask->vlan_id) {
d64efd09 3084 err = nla_put_u16(skb, vlan_id_key,
9399ae9a
HHZ
3085 vlan_key->vlan_id);
3086 if (err)
3087 return err;
3088 }
3089 if (vlan_mask->vlan_priority) {
d64efd09 3090 err = nla_put_u8(skb, vlan_prio_key,
9399ae9a
HHZ
3091 vlan_key->vlan_priority);
3092 if (err)
3093 return err;
3094 }
3095 return 0;
3096}
3097
faa3ffce
OG
3098static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
3099 u32 *flower_key, u32 *flower_mask,
3100 u32 flower_flag_bit, u32 dissector_flag_bit)
3101{
3102 if (dissector_mask & dissector_flag_bit) {
3103 *flower_mask |= flower_flag_bit;
3104 if (dissector_key & dissector_flag_bit)
3105 *flower_key |= flower_flag_bit;
3106 }
3107}
3108
fcb4bb07
AST
3109static int fl_dump_key_flags(struct sk_buff *skb, bool encap,
3110 u32 flags_key, u32 flags_mask)
faa3ffce 3111{
fcb4bb07 3112 int fl_key, fl_mask;
faa3ffce 3113 __be32 _key, _mask;
fcb4bb07 3114 u32 key, mask;
faa3ffce
OG
3115 int err;
3116
fcb4bb07
AST
3117 if (encap) {
3118 fl_key = TCA_FLOWER_KEY_ENC_FLAGS;
3119 fl_mask = TCA_FLOWER_KEY_ENC_FLAGS_MASK;
3120 } else {
3121 fl_key = TCA_FLOWER_KEY_FLAGS;
3122 fl_mask = TCA_FLOWER_KEY_FLAGS_MASK;
3123 }
3124
faa3ffce
OG
3125 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
3126 return 0;
3127
3128 key = 0;
3129 mask = 0;
3130
3131 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3132 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
459d153d
PJV
3133 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3134 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
3135 FLOW_DIS_FIRST_FRAG);
faa3ffce 3136
988f8723
AST
3137 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3138 TCA_FLOWER_KEY_FLAGS_TUNNEL_CSUM,
3139 FLOW_DIS_F_TUNNEL_CSUM);
3140
3141 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3142 TCA_FLOWER_KEY_FLAGS_TUNNEL_DONT_FRAGMENT,
3143 FLOW_DIS_F_TUNNEL_DONT_FRAGMENT);
3144
3145 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3146 TCA_FLOWER_KEY_FLAGS_TUNNEL_OAM, FLOW_DIS_F_TUNNEL_OAM);
3147
3148 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3149 TCA_FLOWER_KEY_FLAGS_TUNNEL_CRIT_OPT,
3150 FLOW_DIS_F_TUNNEL_CRIT_OPT);
3151
faa3ffce
OG
3152 _key = cpu_to_be32(key);
3153 _mask = cpu_to_be32(mask);
3154
fcb4bb07 3155 err = nla_put(skb, fl_key, 4, &_key);
faa3ffce
OG
3156 if (err)
3157 return err;
3158
fcb4bb07 3159 return nla_put(skb, fl_mask, 4, &_mask);
faa3ffce
OG
3160}
3161
0a6e7778
PJV
3162static int fl_dump_key_geneve_opt(struct sk_buff *skb,
3163 struct flow_dissector_key_enc_opts *enc_opts)
3164{
3165 struct geneve_opt *opt;
3166 struct nlattr *nest;
3167 int opt_off = 0;
3168
ae0be8de 3169 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
0a6e7778
PJV
3170 if (!nest)
3171 goto nla_put_failure;
3172
3173 while (enc_opts->len > opt_off) {
3174 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
3175
3176 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
3177 opt->opt_class))
3178 goto nla_put_failure;
3179 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
3180 opt->type))
3181 goto nla_put_failure;
3182 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
3183 opt->length * 4, opt->opt_data))
3184 goto nla_put_failure;
3185
3186 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
3187 }
3188 nla_nest_end(skb, nest);
3189 return 0;
3190
3191nla_put_failure:
3192 nla_nest_cancel(skb, nest);
3193 return -EMSGSIZE;
3194}
3195
d8f9dfae
XL
3196static int fl_dump_key_vxlan_opt(struct sk_buff *skb,
3197 struct flow_dissector_key_enc_opts *enc_opts)
3198{
3199 struct vxlan_metadata *md;
3200 struct nlattr *nest;
3201
3202 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN);
3203 if (!nest)
3204 goto nla_put_failure;
3205
3206 md = (struct vxlan_metadata *)&enc_opts->data[0];
3207 if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp))
3208 goto nla_put_failure;
3209
3210 nla_nest_end(skb, nest);
3211 return 0;
3212
3213nla_put_failure:
3214 nla_nest_cancel(skb, nest);
3215 return -EMSGSIZE;
3216}
3217
79b1011c
XL
3218static int fl_dump_key_erspan_opt(struct sk_buff *skb,
3219 struct flow_dissector_key_enc_opts *enc_opts)
3220{
3221 struct erspan_metadata *md;
3222 struct nlattr *nest;
3223
3224 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_ERSPAN);
3225 if (!nest)
3226 goto nla_put_failure;
3227
3228 md = (struct erspan_metadata *)&enc_opts->data[0];
3229 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER, md->version))
3230 goto nla_put_failure;
3231
3232 if (md->version == 1 &&
3233 nla_put_be32(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index))
3234 goto nla_put_failure;
3235
3236 if (md->version == 2 &&
3237 (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR,
3238 md->u.md2.dir) ||
3239 nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID,
3240 get_hwid(&md->u.md2))))
3241 goto nla_put_failure;
3242
3243 nla_nest_end(skb, nest);
3244 return 0;
3245
3246nla_put_failure:
3247 nla_nest_cancel(skb, nest);
3248 return -EMSGSIZE;
3249}
3250
e3acda7a
WD
3251static int fl_dump_key_gtp_opt(struct sk_buff *skb,
3252 struct flow_dissector_key_enc_opts *enc_opts)
3253
3254{
3255 struct gtp_pdu_session_info *session_info;
3256 struct nlattr *nest;
3257
3258 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GTP);
3259 if (!nest)
3260 goto nla_put_failure;
3261
3262 session_info = (struct gtp_pdu_session_info *)&enc_opts->data[0];
3263
3264 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE,
3265 session_info->pdu_type))
3266 goto nla_put_failure;
3267
3268 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_QFI, session_info->qfi))
3269 goto nla_put_failure;
3270
3271 nla_nest_end(skb, nest);
3272 return 0;
3273
3274nla_put_failure:
3275 nla_nest_cancel(skb, nest);
3276 return -EMSGSIZE;
3277}
3278
6dd514f4
MS
3279static int fl_dump_key_pfcp_opt(struct sk_buff *skb,
3280 struct flow_dissector_key_enc_opts *enc_opts)
3281{
3282 struct pfcp_metadata *md;
3283 struct nlattr *nest;
3284
3285 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_PFCP);
3286 if (!nest)
3287 goto nla_put_failure;
3288
3289 md = (struct pfcp_metadata *)&enc_opts->data[0];
3290 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_PFCP_TYPE, md->type))
3291 goto nla_put_failure;
3292
3293 if (nla_put_be64(skb, TCA_FLOWER_KEY_ENC_OPT_PFCP_SEID,
3294 md->seid, 0))
3295 goto nla_put_failure;
3296
3297 nla_nest_end(skb, nest);
3298 return 0;
3299
3300nla_put_failure:
3301 nla_nest_cancel(skb, nest);
3302 return -EMSGSIZE;
3303}
3304
e0ace68a
PB
3305static int fl_dump_key_ct(struct sk_buff *skb,
3306 struct flow_dissector_key_ct *key,
3307 struct flow_dissector_key_ct *mask)
3308{
3309 if (IS_ENABLED(CONFIG_NF_CONNTRACK) &&
3310 fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
3311 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
3312 sizeof(key->ct_state)))
3313 goto nla_put_failure;
3314
3315 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
3316 fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
3317 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
3318 sizeof(key->ct_zone)))
3319 goto nla_put_failure;
3320
3321 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
3322 fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
3323 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
3324 sizeof(key->ct_mark)))
3325 goto nla_put_failure;
3326
3327 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
3328 fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
3329 &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
3330 sizeof(key->ct_labels)))
3331 goto nla_put_failure;
3332
3333 return 0;
3334
3335nla_put_failure:
3336 return -EMSGSIZE;
3337}
3338
7cfffd5f
ZD
3339static int fl_dump_key_cfm(struct sk_buff *skb,
3340 struct flow_dissector_key_cfm *key,
3341 struct flow_dissector_key_cfm *mask)
3342{
3343 struct nlattr *opts;
3344 int err;
3345 u8 mdl;
3346
3347 if (!memchr_inv(mask, 0, sizeof(*mask)))
3348 return 0;
3349
3350 opts = nla_nest_start(skb, TCA_FLOWER_KEY_CFM);
3351 if (!opts)
3352 return -EMSGSIZE;
3353
3354 if (FIELD_GET(FLOW_DIS_CFM_MDL_MASK, mask->mdl_ver)) {
3355 mdl = FIELD_GET(FLOW_DIS_CFM_MDL_MASK, key->mdl_ver);
3356 err = nla_put_u8(skb, TCA_FLOWER_KEY_CFM_MD_LEVEL, mdl);
3357 if (err)
3358 goto err_cfm_opts;
3359 }
3360
3361 if (mask->opcode) {
3362 err = nla_put_u8(skb, TCA_FLOWER_KEY_CFM_OPCODE, key->opcode);
3363 if (err)
3364 goto err_cfm_opts;
3365 }
3366
3367 nla_nest_end(skb, opts);
3368
3369 return 0;
3370
3371err_cfm_opts:
3372 nla_nest_cancel(skb, opts);
3373 return err;
3374}
3375
0a6e7778
PJV
3376static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
3377 struct flow_dissector_key_enc_opts *enc_opts)
3378{
3379 struct nlattr *nest;
3380 int err;
3381
3382 if (!enc_opts->len)
3383 return 0;
3384
ae0be8de 3385 nest = nla_nest_start_noflag(skb, enc_opt_type);
0a6e7778
PJV
3386 if (!nest)
3387 goto nla_put_failure;
3388
3389 switch (enc_opts->dst_opt_type) {
5832c4a7 3390 case IP_TUNNEL_GENEVE_OPT_BIT:
0a6e7778
PJV
3391 err = fl_dump_key_geneve_opt(skb, enc_opts);
3392 if (err)
3393 goto nla_put_failure;
3394 break;
5832c4a7 3395 case IP_TUNNEL_VXLAN_OPT_BIT:
d8f9dfae
XL
3396 err = fl_dump_key_vxlan_opt(skb, enc_opts);
3397 if (err)
3398 goto nla_put_failure;
3399 break;
5832c4a7 3400 case IP_TUNNEL_ERSPAN_OPT_BIT:
79b1011c
XL
3401 err = fl_dump_key_erspan_opt(skb, enc_opts);
3402 if (err)
3403 goto nla_put_failure;
3404 break;
5832c4a7 3405 case IP_TUNNEL_GTP_OPT_BIT:
e3acda7a
WD
3406 err = fl_dump_key_gtp_opt(skb, enc_opts);
3407 if (err)
3408 goto nla_put_failure;
3409 break;
6dd514f4
MS
3410 case IP_TUNNEL_PFCP_OPT_BIT:
3411 err = fl_dump_key_pfcp_opt(skb, enc_opts);
3412 if (err)
3413 goto nla_put_failure;
3414 break;
0a6e7778
PJV
3415 default:
3416 goto nla_put_failure;
3417 }
3418 nla_nest_end(skb, nest);
3419 return 0;
3420
3421nla_put_failure:
3422 nla_nest_cancel(skb, nest);
3423 return -EMSGSIZE;
3424}
3425
3426static int fl_dump_key_enc_opt(struct sk_buff *skb,
3427 struct flow_dissector_key_enc_opts *key_opts,
3428 struct flow_dissector_key_enc_opts *msk_opts)
3429{
3430 int err;
3431
3432 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
3433 if (err)
3434 return err;
3435
3436 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
3437}
3438
f5749081
JP
3439static int fl_dump_key(struct sk_buff *skb, struct net *net,
3440 struct fl_flow_key *key, struct fl_flow_key *mask)
77b9900e 3441{
8212ed77 3442 if (mask->meta.ingress_ifindex) {
77b9900e
JP
3443 struct net_device *dev;
3444
8212ed77 3445 dev = __dev_get_by_index(net, key->meta.ingress_ifindex);
77b9900e
JP
3446 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
3447 goto nla_put_failure;
3448 }
3449
1a432018
IS
3450 if (fl_dump_key_val(skb, &key->meta.l2_miss,
3451 TCA_FLOWER_L2_MISS, &mask->meta.l2_miss,
3452 TCA_FLOWER_UNSPEC, sizeof(key->meta.l2_miss)))
3453 goto nla_put_failure;
3454
77b9900e
JP
3455 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
3456 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
3457 sizeof(key->eth.dst)) ||
3458 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
3459 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
3460 sizeof(key->eth.src)) ||
3461 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
3462 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
3463 sizeof(key->basic.n_proto)))
3464 goto nla_put_failure;
9399ae9a 3465
b4000312
BS
3466 if (mask->num_of_vlans.num_of_vlans) {
3467 if (nla_put_u8(skb, TCA_FLOWER_KEY_NUM_OF_VLANS, key->num_of_vlans.num_of_vlans))
3468 goto nla_put_failure;
3469 }
3470
a577d8f7
BL
3471 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
3472 goto nla_put_failure;
3473
d64efd09
JL
3474 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
3475 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
9399ae9a
HHZ
3476 goto nla_put_failure;
3477
d64efd09
JL
3478 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
3479 TCA_FLOWER_KEY_CVLAN_PRIO,
3480 &key->cvlan, &mask->cvlan) ||
3481 (mask->cvlan.vlan_tpid &&
158abbf1
JL
3482 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3483 key->cvlan.vlan_tpid)))
d3069512
JL
3484 goto nla_put_failure;
3485
5e9a0fe4 3486 if (mask->basic.n_proto) {
2105f700 3487 if (mask->cvlan.vlan_eth_type) {
5e9a0fe4
JL
3488 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
3489 key->basic.n_proto))
3490 goto nla_put_failure;
2105f700 3491 } else if (mask->vlan.vlan_eth_type) {
5e9a0fe4 3492 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2105f700 3493 key->vlan.vlan_eth_type))
5e9a0fe4
JL
3494 goto nla_put_failure;
3495 }
d64efd09
JL
3496 }
3497
77b9900e
JP
3498 if ((key->basic.n_proto == htons(ETH_P_IP) ||
3499 key->basic.n_proto == htons(ETH_P_IPV6)) &&
4d80cc0a 3500 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
77b9900e 3501 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
4d80cc0a 3502 sizeof(key->basic.ip_proto)) ||
0e2c17b6 3503 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
77b9900e
JP
3504 goto nla_put_failure;
3505
5008750e
WD
3506 if (mask->pppoe.session_id) {
3507 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPPOE_SID,
3508 key->pppoe.session_id))
3509 goto nla_put_failure;
3510 }
3511 if (mask->basic.n_proto && mask->pppoe.ppp_proto) {
3512 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPP_PROTO,
3513 key->pppoe.ppp_proto))
3514 goto nla_put_failure;
3515 }
3516
c3f83241 3517 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
77b9900e
JP
3518 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
3519 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
3520 sizeof(key->ipv4.src)) ||
3521 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
3522 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
3523 sizeof(key->ipv4.dst))))
3524 goto nla_put_failure;
c3f83241 3525 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
77b9900e
JP
3526 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
3527 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
3528 sizeof(key->ipv6.src)) ||
3529 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
3530 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
3531 sizeof(key->ipv6.dst))))
3532 goto nla_put_failure;
3533
3534 if (key->basic.ip_proto == IPPROTO_TCP &&
3535 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
aa72d708 3536 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
77b9900e
JP
3537 sizeof(key->tp.src)) ||
3538 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
aa72d708 3539 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
fdfc7dd6
JP
3540 sizeof(key->tp.dst)) ||
3541 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
3542 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
3543 sizeof(key->tcp.flags))))
77b9900e
JP
3544 goto nla_put_failure;
3545 else if (key->basic.ip_proto == IPPROTO_UDP &&
3546 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
aa72d708 3547 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
77b9900e
JP
3548 sizeof(key->tp.src)) ||
3549 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
aa72d708 3550 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
5976c5f4
SH
3551 sizeof(key->tp.dst))))
3552 goto nla_put_failure;
3553 else if (key->basic.ip_proto == IPPROTO_SCTP &&
3554 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
3555 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
3556 sizeof(key->tp.src)) ||
3557 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
3558 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
77b9900e
JP
3559 sizeof(key->tp.dst))))
3560 goto nla_put_failure;
7b684884
SH
3561 else if (key->basic.n_proto == htons(ETH_P_IP) &&
3562 key->basic.ip_proto == IPPROTO_ICMP &&
3563 (fl_dump_key_val(skb, &key->icmp.type,
3564 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
3565 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
3566 sizeof(key->icmp.type)) ||
3567 fl_dump_key_val(skb, &key->icmp.code,
3568 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
3569 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
3570 sizeof(key->icmp.code))))
3571 goto nla_put_failure;
3572 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
3573 key->basic.ip_proto == IPPROTO_ICMPV6 &&
3574 (fl_dump_key_val(skb, &key->icmp.type,
3575 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
3576 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
3577 sizeof(key->icmp.type)) ||
3578 fl_dump_key_val(skb, &key->icmp.code,
3579 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
3580 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
3581 sizeof(key->icmp.code))))
3582 goto nla_put_failure;
99d31326
SH
3583 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
3584 key->basic.n_proto == htons(ETH_P_RARP)) &&
3585 (fl_dump_key_val(skb, &key->arp.sip,
3586 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
3587 TCA_FLOWER_KEY_ARP_SIP_MASK,
3588 sizeof(key->arp.sip)) ||
3589 fl_dump_key_val(skb, &key->arp.tip,
3590 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
3591 TCA_FLOWER_KEY_ARP_TIP_MASK,
3592 sizeof(key->arp.tip)) ||
3593 fl_dump_key_val(skb, &key->arp.op,
3594 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
3595 TCA_FLOWER_KEY_ARP_OP_MASK,
3596 sizeof(key->arp.op)) ||
3597 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
3598 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
3599 sizeof(key->arp.sha)) ||
3600 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
3601 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
3602 sizeof(key->arp.tha))))
3603 goto nla_put_failure;
8b189ea0
WD
3604 else if (key->basic.ip_proto == IPPROTO_L2TP &&
3605 fl_dump_key_val(skb, &key->l2tpv3.session_id,
3606 TCA_FLOWER_KEY_L2TPV3_SID,
3607 &mask->l2tpv3.session_id,
3608 TCA_FLOWER_UNSPEC,
3609 sizeof(key->l2tpv3.session_id)))
3610 goto nla_put_failure;
77b9900e 3611
4c13eda7
RK
3612 if (key->ipsec.spi &&
3613 fl_dump_key_val(skb, &key->ipsec.spi, TCA_FLOWER_KEY_SPI,
3614 &mask->ipsec.spi, TCA_FLOWER_KEY_SPI_MASK,
3615 sizeof(key->ipsec.spi)))
3616 goto nla_put_failure;
3617
5c72299f
AN
3618 if ((key->basic.ip_proto == IPPROTO_TCP ||
3619 key->basic.ip_proto == IPPROTO_UDP ||
3620 key->basic.ip_proto == IPPROTO_SCTP) &&
3621 fl_dump_key_port_range(skb, key, mask))
3622 goto nla_put_failure;
3623
bc3103f1
AV
3624 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
3625 (fl_dump_key_val(skb, &key->enc_ipv4.src,
3626 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
3627 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
3628 sizeof(key->enc_ipv4.src)) ||
3629 fl_dump_key_val(skb, &key->enc_ipv4.dst,
3630 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
3631 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
3632 sizeof(key->enc_ipv4.dst))))
3633 goto nla_put_failure;
3634 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
3635 (fl_dump_key_val(skb, &key->enc_ipv6.src,
3636 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
3637 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
3638 sizeof(key->enc_ipv6.src)) ||
3639 fl_dump_key_val(skb, &key->enc_ipv6.dst,
3640 TCA_FLOWER_KEY_ENC_IPV6_DST,
3641 &mask->enc_ipv6.dst,
3642 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
3643 sizeof(key->enc_ipv6.dst))))
3644 goto nla_put_failure;
3645
3646 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
eb523f42 3647 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
f4d997fd
HHZ
3648 sizeof(key->enc_key_id)) ||
3649 fl_dump_key_val(skb, &key->enc_tp.src,
3650 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
3651 &mask->enc_tp.src,
3652 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
3653 sizeof(key->enc_tp.src)) ||
3654 fl_dump_key_val(skb, &key->enc_tp.dst,
3655 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
3656 &mask->enc_tp.dst,
3657 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
0e2c17b6 3658 sizeof(key->enc_tp.dst)) ||
0a6e7778
PJV
3659 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
3660 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
bc3103f1
AV
3661 goto nla_put_failure;
3662
e0ace68a
PB
3663 if (fl_dump_key_ct(skb, &key->ct, &mask->ct))
3664 goto nla_put_failure;
3665
fcb4bb07
AST
3666 if (fl_dump_key_flags(skb, false, key->control.flags,
3667 mask->control.flags))
faa3ffce
OG
3668 goto nla_put_failure;
3669
5923b8f7
AL
3670 if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
3671 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
3672 sizeof(key->hash.hash)))
3673 goto nla_put_failure;
3674
7cfffd5f
ZD
3675 if (fl_dump_key_cfm(skb, &key->cfm, &mask->cfm))
3676 goto nla_put_failure;
3677
11036bd7
AST
3678 if (fl_dump_key_flags(skb, true, key->enc_control.flags,
3679 mask->enc_control.flags))
1d17568e
DC
3680 goto nla_put_failure;
3681
f5749081
JP
3682 return 0;
3683
3684nla_put_failure:
3685 return -EMSGSIZE;
3686}
3687
3688static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
12db03b6 3689 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
f5749081
JP
3690{
3691 struct cls_fl_filter *f = fh;
3692 struct nlattr *nest;
3693 struct fl_flow_key *key, *mask;
3d81e711 3694 bool skip_hw;
f5749081
JP
3695
3696 if (!f)
3697 return skb->len;
3698
3699 t->tcm_handle = f->handle;
3700
ae0be8de 3701 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
f5749081
JP
3702 if (!nest)
3703 goto nla_put_failure;
3704
3d81e711
VB
3705 spin_lock(&tp->lock);
3706
f5749081
JP
3707 if (f->res.classid &&
3708 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
3d81e711 3709 goto nla_put_failure_locked;
f5749081
JP
3710
3711 key = &f->key;
3712 mask = &f->mask->key;
3d81e711 3713 skip_hw = tc_skip_hw(f->flags);
f5749081
JP
3714
3715 if (fl_dump_key(skb, net, key, mask))
3d81e711 3716 goto nla_put_failure_locked;
f5749081 3717
749e6720 3718 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3d81e711
VB
3719 goto nla_put_failure_locked;
3720
3721 spin_unlock(&tp->lock);
3722
3723 if (!skip_hw)
c24e43d8 3724 fl_hw_update_stats(tp, f, rtnl_held);
e69985c6 3725
86c55361
VB
3726 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
3727 goto nla_put_failure;
3728
77b9900e
JP
3729 if (tcf_exts_dump(skb, &f->exts))
3730 goto nla_put_failure;
3731
3732 nla_nest_end(skb, nest);
3733
3734 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
3735 goto nla_put_failure;
3736
3737 return skb->len;
3738
3d81e711
VB
3739nla_put_failure_locked:
3740 spin_unlock(&tp->lock);
77b9900e
JP
3741nla_put_failure:
3742 nla_nest_cancel(skb, nest);
3743 return -1;
3744}
3745
0348451d
VB
3746static int fl_terse_dump(struct net *net, struct tcf_proto *tp, void *fh,
3747 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3748{
3749 struct cls_fl_filter *f = fh;
3750 struct nlattr *nest;
3751 bool skip_hw;
3752
3753 if (!f)
3754 return skb->len;
3755
3756 t->tcm_handle = f->handle;
3757
3758 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3759 if (!nest)
3760 goto nla_put_failure;
3761
3762 spin_lock(&tp->lock);
3763
3764 skip_hw = tc_skip_hw(f->flags);
3765
3766 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3767 goto nla_put_failure_locked;
3768
3769 spin_unlock(&tp->lock);
3770
3771 if (!skip_hw)
3772 fl_hw_update_stats(tp, f, rtnl_held);
3773
3774 if (tcf_exts_terse_dump(skb, &f->exts))
3775 goto nla_put_failure;
3776
3777 nla_nest_end(skb, nest);
3778
3779 return skb->len;
3780
3781nla_put_failure_locked:
3782 spin_unlock(&tp->lock);
3783nla_put_failure:
3784 nla_nest_cancel(skb, nest);
3785 return -1;
3786}
3787
b95ec7eb
JP
3788static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
3789{
3790 struct fl_flow_tmplt *tmplt = tmplt_priv;
3791 struct fl_flow_key *key, *mask;
3792 struct nlattr *nest;
3793
ae0be8de 3794 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
b95ec7eb
JP
3795 if (!nest)
3796 goto nla_put_failure;
3797
3798 key = &tmplt->dummy_key;
3799 mask = &tmplt->mask;
3800
3801 if (fl_dump_key(skb, net, key, mask))
3802 goto nla_put_failure;
3803
3804 nla_nest_end(skb, nest);
3805
3806 return skb->len;
3807
3808nla_put_failure:
3809 nla_nest_cancel(skb, nest);
3810 return -EMSGSIZE;
3811}
3812
2e24cd75
CW
3813static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
3814 unsigned long base)
07d79fc7
CW
3815{
3816 struct cls_fl_filter *f = fh;
3817
cc9039a1 3818 tc_cls_bind_class(classid, cl, q, &f->res, base);
07d79fc7
CW
3819}
3820
a5b72a08
DC
3821static bool fl_delete_empty(struct tcf_proto *tp)
3822{
3823 struct cls_fl_head *head = fl_head_dereference(tp);
3824
3825 spin_lock(&tp->lock);
3826 tp->deleting = idr_is_empty(&head->handle_idr);
3827 spin_unlock(&tp->lock);
3828
3829 return tp->deleting;
3830}
3831
77b9900e
JP
3832static struct tcf_proto_ops cls_fl_ops __read_mostly = {
3833 .kind = "flower",
3834 .classify = fl_classify,
3835 .init = fl_init,
3836 .destroy = fl_destroy,
3837 .get = fl_get,
06177558 3838 .put = fl_put,
77b9900e
JP
3839 .change = fl_change,
3840 .delete = fl_delete,
a5b72a08 3841 .delete_empty = fl_delete_empty,
77b9900e 3842 .walk = fl_walk,
31533cba 3843 .reoffload = fl_reoffload,
a449a3e7
VB
3844 .hw_add = fl_hw_add,
3845 .hw_del = fl_hw_del,
77b9900e 3846 .dump = fl_dump,
0348451d 3847 .terse_dump = fl_terse_dump,
07d79fc7 3848 .bind_class = fl_bind_class,
b95ec7eb
JP
3849 .tmplt_create = fl_tmplt_create,
3850 .tmplt_destroy = fl_tmplt_destroy,
32f2a0af 3851 .tmplt_reoffload = fl_tmplt_reoffload,
b95ec7eb 3852 .tmplt_dump = fl_tmplt_dump,
606c7c43 3853 .get_exts = fl_get_exts,
77b9900e 3854 .owner = THIS_MODULE,
92149190 3855 .flags = TCF_PROTO_OPS_DOIT_UNLOCKED,
77b9900e 3856};
241a94ab 3857MODULE_ALIAS_NET_CLS("flower");
77b9900e
JP
3858
3859static int __init cls_fl_init(void)
3860{
3861 return register_tcf_proto_ops(&cls_fl_ops);
3862}
3863
3864static void __exit cls_fl_exit(void)
3865{
3866 unregister_tcf_proto_ops(&cls_fl_ops);
3867}
3868
3869module_init(cls_fl_init);
3870module_exit(cls_fl_exit);
3871
3872MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3873MODULE_DESCRIPTION("Flower classifier");
3874MODULE_LICENSE("GPL v2");