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