Bluetooth: ISO: use hci_sync for setting CIG parameters
[platform/kernel/linux-starfive.git] / net / 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             ntohs(key->tp_range.tp_max.dst) <=
798             ntohs(key->tp_range.tp_min.dst)) {
799                 NL_SET_ERR_MSG_ATTR(extack,
800                                     tb[TCA_FLOWER_KEY_PORT_DST_MIN],
801                                     "Invalid destination port range (min must be strictly smaller than max)");
802                 return -EINVAL;
803         }
804         if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
805             ntohs(key->tp_range.tp_max.src) <=
806             ntohs(key->tp_range.tp_min.src)) {
807                 NL_SET_ERR_MSG_ATTR(extack,
808                                     tb[TCA_FLOWER_KEY_PORT_SRC_MIN],
809                                     "Invalid source port range (min must be strictly smaller than max)");
810                 return -EINVAL;
811         }
812
813         return 0;
814 }
815
816 static int fl_set_key_mpls_lse(const struct nlattr *nla_lse,
817                                struct flow_dissector_key_mpls *key_val,
818                                struct flow_dissector_key_mpls *key_mask,
819                                struct netlink_ext_ack *extack)
820 {
821         struct nlattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1];
822         struct flow_dissector_mpls_lse *lse_mask;
823         struct flow_dissector_mpls_lse *lse_val;
824         u8 lse_index;
825         u8 depth;
826         int err;
827
828         err = nla_parse_nested(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, nla_lse,
829                                mpls_stack_entry_policy, extack);
830         if (err < 0)
831                 return err;
832
833         if (!tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]) {
834                 NL_SET_ERR_MSG(extack, "Missing MPLS option \"depth\"");
835                 return -EINVAL;
836         }
837
838         depth = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]);
839
840         /* LSE depth starts at 1, for consistency with terminology used by
841          * RFC 3031 (section 3.9), where depth 0 refers to unlabeled packets.
842          */
843         if (depth < 1 || depth > FLOW_DIS_MPLS_MAX) {
844                 NL_SET_ERR_MSG_ATTR(extack,
845                                     tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH],
846                                     "Invalid MPLS depth");
847                 return -EINVAL;
848         }
849         lse_index = depth - 1;
850
851         dissector_set_mpls_lse(key_val, lse_index);
852         dissector_set_mpls_lse(key_mask, lse_index);
853
854         lse_val = &key_val->ls[lse_index];
855         lse_mask = &key_mask->ls[lse_index];
856
857         if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]) {
858                 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]);
859                 lse_mask->mpls_ttl = MPLS_TTL_MASK;
860         }
861         if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]) {
862                 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]);
863
864                 if (bos & ~MPLS_BOS_MASK) {
865                         NL_SET_ERR_MSG_ATTR(extack,
866                                             tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS],
867                                             "Bottom Of Stack (BOS) must be 0 or 1");
868                         return -EINVAL;
869                 }
870                 lse_val->mpls_bos = bos;
871                 lse_mask->mpls_bos = MPLS_BOS_MASK;
872         }
873         if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]) {
874                 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]);
875
876                 if (tc & ~MPLS_TC_MASK) {
877                         NL_SET_ERR_MSG_ATTR(extack,
878                                             tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC],
879                                             "Traffic Class (TC) must be between 0 and 7");
880                         return -EINVAL;
881                 }
882                 lse_val->mpls_tc = tc;
883                 lse_mask->mpls_tc = MPLS_TC_MASK;
884         }
885         if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]) {
886                 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]);
887
888                 if (label & ~MPLS_LABEL_MASK) {
889                         NL_SET_ERR_MSG_ATTR(extack,
890                                             tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL],
891                                             "Label must be between 0 and 1048575");
892                         return -EINVAL;
893                 }
894                 lse_val->mpls_label = label;
895                 lse_mask->mpls_label = MPLS_LABEL_MASK;
896         }
897
898         return 0;
899 }
900
901 static int fl_set_key_mpls_opts(const struct nlattr *nla_mpls_opts,
902                                 struct flow_dissector_key_mpls *key_val,
903                                 struct flow_dissector_key_mpls *key_mask,
904                                 struct netlink_ext_ack *extack)
905 {
906         struct nlattr *nla_lse;
907         int rem;
908         int err;
909
910         if (!(nla_mpls_opts->nla_type & NLA_F_NESTED)) {
911                 NL_SET_ERR_MSG_ATTR(extack, nla_mpls_opts,
912                                     "NLA_F_NESTED is missing");
913                 return -EINVAL;
914         }
915
916         nla_for_each_nested(nla_lse, nla_mpls_opts, rem) {
917                 if (nla_type(nla_lse) != TCA_FLOWER_KEY_MPLS_OPTS_LSE) {
918                         NL_SET_ERR_MSG_ATTR(extack, nla_lse,
919                                             "Invalid MPLS option type");
920                         return -EINVAL;
921                 }
922
923                 err = fl_set_key_mpls_lse(nla_lse, key_val, key_mask, extack);
924                 if (err < 0)
925                         return err;
926         }
927         if (rem) {
928                 NL_SET_ERR_MSG(extack,
929                                "Bytes leftover after parsing MPLS options");
930                 return -EINVAL;
931         }
932
933         return 0;
934 }
935
936 static int fl_set_key_mpls(struct nlattr **tb,
937                            struct flow_dissector_key_mpls *key_val,
938                            struct flow_dissector_key_mpls *key_mask,
939                            struct netlink_ext_ack *extack)
940 {
941         struct flow_dissector_mpls_lse *lse_mask;
942         struct flow_dissector_mpls_lse *lse_val;
943
944         if (tb[TCA_FLOWER_KEY_MPLS_OPTS]) {
945                 if (tb[TCA_FLOWER_KEY_MPLS_TTL] ||
946                     tb[TCA_FLOWER_KEY_MPLS_BOS] ||
947                     tb[TCA_FLOWER_KEY_MPLS_TC] ||
948                     tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
949                         NL_SET_ERR_MSG_ATTR(extack,
950                                             tb[TCA_FLOWER_KEY_MPLS_OPTS],
951                                             "MPLS label, Traffic Class, Bottom Of Stack and Time To Live must be encapsulated in the MPLS options attribute");
952                         return -EBADMSG;
953                 }
954
955                 return fl_set_key_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS],
956                                             key_val, key_mask, extack);
957         }
958
959         lse_val = &key_val->ls[0];
960         lse_mask = &key_mask->ls[0];
961
962         if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
963                 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
964                 lse_mask->mpls_ttl = MPLS_TTL_MASK;
965                 dissector_set_mpls_lse(key_val, 0);
966                 dissector_set_mpls_lse(key_mask, 0);
967         }
968         if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
969                 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
970
971                 if (bos & ~MPLS_BOS_MASK) {
972                         NL_SET_ERR_MSG_ATTR(extack,
973                                             tb[TCA_FLOWER_KEY_MPLS_BOS],
974                                             "Bottom Of Stack (BOS) must be 0 or 1");
975                         return -EINVAL;
976                 }
977                 lse_val->mpls_bos = bos;
978                 lse_mask->mpls_bos = MPLS_BOS_MASK;
979                 dissector_set_mpls_lse(key_val, 0);
980                 dissector_set_mpls_lse(key_mask, 0);
981         }
982         if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
983                 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
984
985                 if (tc & ~MPLS_TC_MASK) {
986                         NL_SET_ERR_MSG_ATTR(extack,
987                                             tb[TCA_FLOWER_KEY_MPLS_TC],
988                                             "Traffic Class (TC) must be between 0 and 7");
989                         return -EINVAL;
990                 }
991                 lse_val->mpls_tc = tc;
992                 lse_mask->mpls_tc = MPLS_TC_MASK;
993                 dissector_set_mpls_lse(key_val, 0);
994                 dissector_set_mpls_lse(key_mask, 0);
995         }
996         if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
997                 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
998
999                 if (label & ~MPLS_LABEL_MASK) {
1000                         NL_SET_ERR_MSG_ATTR(extack,
1001                                             tb[TCA_FLOWER_KEY_MPLS_LABEL],
1002                                             "Label must be between 0 and 1048575");
1003                         return -EINVAL;
1004                 }
1005                 lse_val->mpls_label = label;
1006                 lse_mask->mpls_label = MPLS_LABEL_MASK;
1007                 dissector_set_mpls_lse(key_val, 0);
1008                 dissector_set_mpls_lse(key_mask, 0);
1009         }
1010         return 0;
1011 }
1012
1013 static void fl_set_key_vlan(struct nlattr **tb,
1014                             __be16 ethertype,
1015                             int vlan_id_key, int vlan_prio_key,
1016                             int vlan_next_eth_type_key,
1017                             struct flow_dissector_key_vlan *key_val,
1018                             struct flow_dissector_key_vlan *key_mask)
1019 {
1020 #define VLAN_PRIORITY_MASK      0x7
1021
1022         if (tb[vlan_id_key]) {
1023                 key_val->vlan_id =
1024                         nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
1025                 key_mask->vlan_id = VLAN_VID_MASK;
1026         }
1027         if (tb[vlan_prio_key]) {
1028                 key_val->vlan_priority =
1029                         nla_get_u8(tb[vlan_prio_key]) &
1030                         VLAN_PRIORITY_MASK;
1031                 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
1032         }
1033         if (ethertype) {
1034                 key_val->vlan_tpid = ethertype;
1035                 key_mask->vlan_tpid = cpu_to_be16(~0);
1036         }
1037         if (tb[vlan_next_eth_type_key]) {
1038                 key_val->vlan_eth_type =
1039                         nla_get_be16(tb[vlan_next_eth_type_key]);
1040                 key_mask->vlan_eth_type = cpu_to_be16(~0);
1041         }
1042 }
1043
1044 static void fl_set_key_pppoe(struct nlattr **tb,
1045                              struct flow_dissector_key_pppoe *key_val,
1046                              struct flow_dissector_key_pppoe *key_mask,
1047                              struct fl_flow_key *key,
1048                              struct fl_flow_key *mask)
1049 {
1050         /* key_val::type must be set to ETH_P_PPP_SES
1051          * because ETH_P_PPP_SES was stored in basic.n_proto
1052          * which might get overwritten by ppp_proto
1053          * or might be set to 0, the role of key_val::type
1054          * is simmilar to vlan_key::tpid
1055          */
1056         key_val->type = htons(ETH_P_PPP_SES);
1057         key_mask->type = cpu_to_be16(~0);
1058
1059         if (tb[TCA_FLOWER_KEY_PPPOE_SID]) {
1060                 key_val->session_id =
1061                         nla_get_be16(tb[TCA_FLOWER_KEY_PPPOE_SID]);
1062                 key_mask->session_id = cpu_to_be16(~0);
1063         }
1064         if (tb[TCA_FLOWER_KEY_PPP_PROTO]) {
1065                 key_val->ppp_proto =
1066                         nla_get_be16(tb[TCA_FLOWER_KEY_PPP_PROTO]);
1067                 key_mask->ppp_proto = cpu_to_be16(~0);
1068
1069                 if (key_val->ppp_proto == htons(PPP_IP)) {
1070                         key->basic.n_proto = htons(ETH_P_IP);
1071                         mask->basic.n_proto = cpu_to_be16(~0);
1072                 } else if (key_val->ppp_proto == htons(PPP_IPV6)) {
1073                         key->basic.n_proto = htons(ETH_P_IPV6);
1074                         mask->basic.n_proto = cpu_to_be16(~0);
1075                 } else if (key_val->ppp_proto == htons(PPP_MPLS_UC)) {
1076                         key->basic.n_proto = htons(ETH_P_MPLS_UC);
1077                         mask->basic.n_proto = cpu_to_be16(~0);
1078                 } else if (key_val->ppp_proto == htons(PPP_MPLS_MC)) {
1079                         key->basic.n_proto = htons(ETH_P_MPLS_MC);
1080                         mask->basic.n_proto = cpu_to_be16(~0);
1081                 }
1082         } else {
1083                 key->basic.n_proto = 0;
1084                 mask->basic.n_proto = cpu_to_be16(0);
1085         }
1086 }
1087
1088 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
1089                             u32 *dissector_key, u32 *dissector_mask,
1090                             u32 flower_flag_bit, u32 dissector_flag_bit)
1091 {
1092         if (flower_mask & flower_flag_bit) {
1093                 *dissector_mask |= dissector_flag_bit;
1094                 if (flower_key & flower_flag_bit)
1095                         *dissector_key |= dissector_flag_bit;
1096         }
1097 }
1098
1099 static int fl_set_key_flags(struct nlattr **tb, u32 *flags_key,
1100                             u32 *flags_mask, struct netlink_ext_ack *extack)
1101 {
1102         u32 key, mask;
1103
1104         /* mask is mandatory for flags */
1105         if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) {
1106                 NL_SET_ERR_MSG(extack, "Missing flags mask");
1107                 return -EINVAL;
1108         }
1109
1110         key = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS]));
1111         mask = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
1112
1113         *flags_key  = 0;
1114         *flags_mask = 0;
1115
1116         fl_set_key_flag(key, mask, flags_key, flags_mask,
1117                         TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1118         fl_set_key_flag(key, mask, flags_key, flags_mask,
1119                         TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1120                         FLOW_DIS_FIRST_FRAG);
1121
1122         return 0;
1123 }
1124
1125 static void fl_set_key_ip(struct nlattr **tb, bool encap,
1126                           struct flow_dissector_key_ip *key,
1127                           struct flow_dissector_key_ip *mask)
1128 {
1129         int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1130         int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1131         int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1132         int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1133
1134         fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
1135         fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
1136 }
1137
1138 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
1139                              int depth, int option_len,
1140                              struct netlink_ext_ack *extack)
1141 {
1142         struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
1143         struct nlattr *class = NULL, *type = NULL, *data = NULL;
1144         struct geneve_opt *opt;
1145         int err, data_len = 0;
1146
1147         if (option_len > sizeof(struct geneve_opt))
1148                 data_len = option_len - sizeof(struct geneve_opt);
1149
1150         if (key->enc_opts.len > FLOW_DIS_TUN_OPTS_MAX - 4)
1151                 return -ERANGE;
1152
1153         opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
1154         memset(opt, 0xff, option_len);
1155         opt->length = data_len / 4;
1156         opt->r1 = 0;
1157         opt->r2 = 0;
1158         opt->r3 = 0;
1159
1160         /* If no mask has been prodived we assume an exact match. */
1161         if (!depth)
1162                 return sizeof(struct geneve_opt) + data_len;
1163
1164         if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
1165                 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
1166                 return -EINVAL;
1167         }
1168
1169         err = nla_parse_nested_deprecated(tb,
1170                                           TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
1171                                           nla, geneve_opt_policy, extack);
1172         if (err < 0)
1173                 return err;
1174
1175         /* We are not allowed to omit any of CLASS, TYPE or DATA
1176          * fields from the key.
1177          */
1178         if (!option_len &&
1179             (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
1180              !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
1181              !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
1182                 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
1183                 return -EINVAL;
1184         }
1185
1186         /* Omitting any of CLASS, TYPE or DATA fields is allowed
1187          * for the mask.
1188          */
1189         if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
1190                 int new_len = key->enc_opts.len;
1191
1192                 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
1193                 data_len = nla_len(data);
1194                 if (data_len < 4) {
1195                         NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
1196                         return -ERANGE;
1197                 }
1198                 if (data_len % 4) {
1199                         NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
1200                         return -ERANGE;
1201                 }
1202
1203                 new_len += sizeof(struct geneve_opt) + data_len;
1204                 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
1205                 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
1206                         NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
1207                         return -ERANGE;
1208                 }
1209                 opt->length = data_len / 4;
1210                 memcpy(opt->opt_data, nla_data(data), data_len);
1211         }
1212
1213         if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
1214                 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
1215                 opt->opt_class = nla_get_be16(class);
1216         }
1217
1218         if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
1219                 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
1220                 opt->type = nla_get_u8(type);
1221         }
1222
1223         return sizeof(struct geneve_opt) + data_len;
1224 }
1225
1226 static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1227                             int depth, int option_len,
1228                             struct netlink_ext_ack *extack)
1229 {
1230         struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1];
1231         struct vxlan_metadata *md;
1232         int err;
1233
1234         md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1235         memset(md, 0xff, sizeof(*md));
1236
1237         if (!depth)
1238                 return sizeof(*md);
1239
1240         if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) {
1241                 NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask");
1242                 return -EINVAL;
1243         }
1244
1245         err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla,
1246                                vxlan_opt_policy, extack);
1247         if (err < 0)
1248                 return err;
1249
1250         if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
1251                 NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
1252                 return -EINVAL;
1253         }
1254
1255         if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
1256                 md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]);
1257                 md->gbp &= VXLAN_GBP_MASK;
1258         }
1259
1260         return sizeof(*md);
1261 }
1262
1263 static int fl_set_erspan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1264                              int depth, int option_len,
1265                              struct netlink_ext_ack *extack)
1266 {
1267         struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1];
1268         struct erspan_metadata *md;
1269         int err;
1270
1271         md = (struct erspan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1272         memset(md, 0xff, sizeof(*md));
1273         md->version = 1;
1274
1275         if (!depth)
1276                 return sizeof(*md);
1277
1278         if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_ERSPAN) {
1279                 NL_SET_ERR_MSG(extack, "Non-erspan option type for mask");
1280                 return -EINVAL;
1281         }
1282
1283         err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, nla,
1284                                erspan_opt_policy, extack);
1285         if (err < 0)
1286                 return err;
1287
1288         if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) {
1289                 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver");
1290                 return -EINVAL;
1291         }
1292
1293         if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER])
1294                 md->version = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]);
1295
1296         if (md->version == 1) {
1297                 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1298                         NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index");
1299                         return -EINVAL;
1300                 }
1301                 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1302                         nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX];
1303                         memset(&md->u, 0x00, sizeof(md->u));
1304                         md->u.index = nla_get_be32(nla);
1305                 }
1306         } else if (md->version == 2) {
1307                 if (!option_len && (!tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] ||
1308                                     !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID])) {
1309                         NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid");
1310                         return -EINVAL;
1311                 }
1312                 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]) {
1313                         nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR];
1314                         md->u.md2.dir = nla_get_u8(nla);
1315                 }
1316                 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]) {
1317                         nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID];
1318                         set_hwid(&md->u.md2, nla_get_u8(nla));
1319                 }
1320         } else {
1321                 NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect");
1322                 return -EINVAL;
1323         }
1324
1325         return sizeof(*md);
1326 }
1327
1328 static int fl_set_gtp_opt(const struct nlattr *nla, struct fl_flow_key *key,
1329                           int depth, int option_len,
1330                           struct netlink_ext_ack *extack)
1331 {
1332         struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1];
1333         struct gtp_pdu_session_info *sinfo;
1334         u8 len = key->enc_opts.len;
1335         int err;
1336
1337         sinfo = (struct gtp_pdu_session_info *)&key->enc_opts.data[len];
1338         memset(sinfo, 0xff, option_len);
1339
1340         if (!depth)
1341                 return sizeof(*sinfo);
1342
1343         if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GTP) {
1344                 NL_SET_ERR_MSG_MOD(extack, "Non-gtp option type for mask");
1345                 return -EINVAL;
1346         }
1347
1348         err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GTP_MAX, nla,
1349                                gtp_opt_policy, extack);
1350         if (err < 0)
1351                 return err;
1352
1353         if (!option_len &&
1354             (!tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] ||
1355              !tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])) {
1356                 NL_SET_ERR_MSG_MOD(extack,
1357                                    "Missing tunnel key gtp option pdu type or qfi");
1358                 return -EINVAL;
1359         }
1360
1361         if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE])
1362                 sinfo->pdu_type =
1363                         nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE]);
1364
1365         if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])
1366                 sinfo->qfi = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI]);
1367
1368         return sizeof(*sinfo);
1369 }
1370
1371 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
1372                           struct fl_flow_key *mask,
1373                           struct netlink_ext_ack *extack)
1374 {
1375         const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
1376         int err, option_len, key_depth, msk_depth = 0;
1377
1378         err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
1379                                              TCA_FLOWER_KEY_ENC_OPTS_MAX,
1380                                              enc_opts_policy, extack);
1381         if (err)
1382                 return err;
1383
1384         nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
1385
1386         if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
1387                 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
1388                                                      TCA_FLOWER_KEY_ENC_OPTS_MAX,
1389                                                      enc_opts_policy, extack);
1390                 if (err)
1391                         return err;
1392
1393                 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
1394                 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
1395                 if (!nla_ok(nla_opt_msk, msk_depth)) {
1396                         NL_SET_ERR_MSG(extack, "Invalid nested attribute for masks");
1397                         return -EINVAL;
1398                 }
1399         }
1400
1401         nla_for_each_attr(nla_opt_key, nla_enc_key,
1402                           nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
1403                 switch (nla_type(nla_opt_key)) {
1404                 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
1405                         if (key->enc_opts.dst_opt_type &&
1406                             key->enc_opts.dst_opt_type != TUNNEL_GENEVE_OPT) {
1407                                 NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
1408                                 return -EINVAL;
1409                         }
1410                         option_len = 0;
1411                         key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
1412                         option_len = fl_set_geneve_opt(nla_opt_key, key,
1413                                                        key_depth, option_len,
1414                                                        extack);
1415                         if (option_len < 0)
1416                                 return option_len;
1417
1418                         key->enc_opts.len += option_len;
1419                         /* At the same time we need to parse through the mask
1420                          * in order to verify exact and mask attribute lengths.
1421                          */
1422                         mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
1423                         option_len = fl_set_geneve_opt(nla_opt_msk, mask,
1424                                                        msk_depth, option_len,
1425                                                        extack);
1426                         if (option_len < 0)
1427                                 return option_len;
1428
1429                         mask->enc_opts.len += option_len;
1430                         if (key->enc_opts.len != mask->enc_opts.len) {
1431                                 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1432                                 return -EINVAL;
1433                         }
1434                         break;
1435                 case TCA_FLOWER_KEY_ENC_OPTS_VXLAN:
1436                         if (key->enc_opts.dst_opt_type) {
1437                                 NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
1438                                 return -EINVAL;
1439                         }
1440                         option_len = 0;
1441                         key->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
1442                         option_len = fl_set_vxlan_opt(nla_opt_key, key,
1443                                                       key_depth, option_len,
1444                                                       extack);
1445                         if (option_len < 0)
1446                                 return option_len;
1447
1448                         key->enc_opts.len += option_len;
1449                         /* At the same time we need to parse through the mask
1450                          * in order to verify exact and mask attribute lengths.
1451                          */
1452                         mask->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
1453                         option_len = fl_set_vxlan_opt(nla_opt_msk, mask,
1454                                                       msk_depth, option_len,
1455                                                       extack);
1456                         if (option_len < 0)
1457                                 return option_len;
1458
1459                         mask->enc_opts.len += option_len;
1460                         if (key->enc_opts.len != mask->enc_opts.len) {
1461                                 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1462                                 return -EINVAL;
1463                         }
1464                         break;
1465                 case TCA_FLOWER_KEY_ENC_OPTS_ERSPAN:
1466                         if (key->enc_opts.dst_opt_type) {
1467                                 NL_SET_ERR_MSG(extack, "Duplicate type for erspan options");
1468                                 return -EINVAL;
1469                         }
1470                         option_len = 0;
1471                         key->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT;
1472                         option_len = fl_set_erspan_opt(nla_opt_key, key,
1473                                                        key_depth, option_len,
1474                                                        extack);
1475                         if (option_len < 0)
1476                                 return option_len;
1477
1478                         key->enc_opts.len += option_len;
1479                         /* At the same time we need to parse through the mask
1480                          * in order to verify exact and mask attribute lengths.
1481                          */
1482                         mask->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT;
1483                         option_len = fl_set_erspan_opt(nla_opt_msk, mask,
1484                                                        msk_depth, option_len,
1485                                                        extack);
1486                         if (option_len < 0)
1487                                 return option_len;
1488
1489                         mask->enc_opts.len += option_len;
1490                         if (key->enc_opts.len != mask->enc_opts.len) {
1491                                 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1492                                 return -EINVAL;
1493                         }
1494                         break;
1495                 case TCA_FLOWER_KEY_ENC_OPTS_GTP:
1496                         if (key->enc_opts.dst_opt_type) {
1497                                 NL_SET_ERR_MSG_MOD(extack,
1498                                                    "Duplicate type for gtp options");
1499                                 return -EINVAL;
1500                         }
1501                         option_len = 0;
1502                         key->enc_opts.dst_opt_type = TUNNEL_GTP_OPT;
1503                         option_len = fl_set_gtp_opt(nla_opt_key, key,
1504                                                     key_depth, option_len,
1505                                                     extack);
1506                         if (option_len < 0)
1507                                 return option_len;
1508
1509                         key->enc_opts.len += option_len;
1510                         /* At the same time we need to parse through the mask
1511                          * in order to verify exact and mask attribute lengths.
1512                          */
1513                         mask->enc_opts.dst_opt_type = TUNNEL_GTP_OPT;
1514                         option_len = fl_set_gtp_opt(nla_opt_msk, mask,
1515                                                     msk_depth, option_len,
1516                                                     extack);
1517                         if (option_len < 0)
1518                                 return option_len;
1519
1520                         mask->enc_opts.len += option_len;
1521                         if (key->enc_opts.len != mask->enc_opts.len) {
1522                                 NL_SET_ERR_MSG_MOD(extack,
1523                                                    "Key and mask miss aligned");
1524                                 return -EINVAL;
1525                         }
1526                         break;
1527                 default:
1528                         NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
1529                         return -EINVAL;
1530                 }
1531
1532                 if (!msk_depth)
1533                         continue;
1534
1535                 if (!nla_ok(nla_opt_msk, msk_depth)) {
1536                         NL_SET_ERR_MSG(extack, "A mask attribute is invalid");
1537                         return -EINVAL;
1538                 }
1539                 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
1540         }
1541
1542         return 0;
1543 }
1544
1545 static int fl_validate_ct_state(u16 state, struct nlattr *tb,
1546                                 struct netlink_ext_ack *extack)
1547 {
1548         if (state && !(state & TCA_FLOWER_KEY_CT_FLAGS_TRACKED)) {
1549                 NL_SET_ERR_MSG_ATTR(extack, tb,
1550                                     "no trk, so no other flag can be set");
1551                 return -EINVAL;
1552         }
1553
1554         if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
1555             state & TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED) {
1556                 NL_SET_ERR_MSG_ATTR(extack, tb,
1557                                     "new and est are mutually exclusive");
1558                 return -EINVAL;
1559         }
1560
1561         if (state & TCA_FLOWER_KEY_CT_FLAGS_INVALID &&
1562             state & ~(TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
1563                       TCA_FLOWER_KEY_CT_FLAGS_INVALID)) {
1564                 NL_SET_ERR_MSG_ATTR(extack, tb,
1565                                     "when inv is set, only trk may be set");
1566                 return -EINVAL;
1567         }
1568
1569         if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
1570             state & TCA_FLOWER_KEY_CT_FLAGS_REPLY) {
1571                 NL_SET_ERR_MSG_ATTR(extack, tb,
1572                                     "new and rpl are mutually exclusive");
1573                 return -EINVAL;
1574         }
1575
1576         return 0;
1577 }
1578
1579 static int fl_set_key_ct(struct nlattr **tb,
1580                          struct flow_dissector_key_ct *key,
1581                          struct flow_dissector_key_ct *mask,
1582                          struct netlink_ext_ack *extack)
1583 {
1584         if (tb[TCA_FLOWER_KEY_CT_STATE]) {
1585                 int err;
1586
1587                 if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) {
1588                         NL_SET_ERR_MSG(extack, "Conntrack isn't enabled");
1589                         return -EOPNOTSUPP;
1590                 }
1591                 fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
1592                                &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
1593                                sizeof(key->ct_state));
1594
1595                 err = fl_validate_ct_state(key->ct_state & mask->ct_state,
1596                                            tb[TCA_FLOWER_KEY_CT_STATE_MASK],
1597                                            extack);
1598                 if (err)
1599                         return err;
1600
1601         }
1602         if (tb[TCA_FLOWER_KEY_CT_ZONE]) {
1603                 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1604                         NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled");
1605                         return -EOPNOTSUPP;
1606                 }
1607                 fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
1608                                &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
1609                                sizeof(key->ct_zone));
1610         }
1611         if (tb[TCA_FLOWER_KEY_CT_MARK]) {
1612                 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1613                         NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled");
1614                         return -EOPNOTSUPP;
1615                 }
1616                 fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
1617                                &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
1618                                sizeof(key->ct_mark));
1619         }
1620         if (tb[TCA_FLOWER_KEY_CT_LABELS]) {
1621                 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1622                         NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled");
1623                         return -EOPNOTSUPP;
1624                 }
1625                 fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
1626                                mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
1627                                sizeof(key->ct_labels));
1628         }
1629
1630         return 0;
1631 }
1632
1633 static bool is_vlan_key(struct nlattr *tb, __be16 *ethertype,
1634                         struct fl_flow_key *key, struct fl_flow_key *mask,
1635                         int vthresh)
1636 {
1637         const bool good_num_of_vlans = key->num_of_vlans.num_of_vlans > vthresh;
1638
1639         if (!tb) {
1640                 *ethertype = 0;
1641                 return good_num_of_vlans;
1642         }
1643
1644         *ethertype = nla_get_be16(tb);
1645         if (good_num_of_vlans || eth_type_vlan(*ethertype))
1646                 return true;
1647
1648         key->basic.n_proto = *ethertype;
1649         mask->basic.n_proto = cpu_to_be16(~0);
1650         return false;
1651 }
1652
1653 static int fl_set_key(struct net *net, struct nlattr **tb,
1654                       struct fl_flow_key *key, struct fl_flow_key *mask,
1655                       struct netlink_ext_ack *extack)
1656 {
1657         __be16 ethertype;
1658         int ret = 0;
1659
1660         if (tb[TCA_FLOWER_INDEV]) {
1661                 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
1662                 if (err < 0)
1663                         return err;
1664                 key->meta.ingress_ifindex = err;
1665                 mask->meta.ingress_ifindex = 0xffffffff;
1666         }
1667
1668         fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1669                        mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1670                        sizeof(key->eth.dst));
1671         fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1672                        mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1673                        sizeof(key->eth.src));
1674         fl_set_key_val(tb, &key->num_of_vlans,
1675                        TCA_FLOWER_KEY_NUM_OF_VLANS,
1676                        &mask->num_of_vlans,
1677                        TCA_FLOWER_UNSPEC,
1678                        sizeof(key->num_of_vlans));
1679
1680         if (is_vlan_key(tb[TCA_FLOWER_KEY_ETH_TYPE], &ethertype, key, mask, 0)) {
1681                 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
1682                                 TCA_FLOWER_KEY_VLAN_PRIO,
1683                                 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1684                                 &key->vlan, &mask->vlan);
1685
1686                 if (is_vlan_key(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE],
1687                                 &ethertype, key, mask, 1)) {
1688                         fl_set_key_vlan(tb, ethertype,
1689                                         TCA_FLOWER_KEY_CVLAN_ID,
1690                                         TCA_FLOWER_KEY_CVLAN_PRIO,
1691                                         TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1692                                         &key->cvlan, &mask->cvlan);
1693                         fl_set_key_val(tb, &key->basic.n_proto,
1694                                        TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1695                                        &mask->basic.n_proto,
1696                                        TCA_FLOWER_UNSPEC,
1697                                        sizeof(key->basic.n_proto));
1698                 }
1699         }
1700
1701         if (key->basic.n_proto == htons(ETH_P_PPP_SES))
1702                 fl_set_key_pppoe(tb, &key->pppoe, &mask->pppoe, key, mask);
1703
1704         if (key->basic.n_proto == htons(ETH_P_IP) ||
1705             key->basic.n_proto == htons(ETH_P_IPV6)) {
1706                 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1707                                &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1708                                sizeof(key->basic.ip_proto));
1709                 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
1710         }
1711
1712         if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1713                 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1714                 mask->control.addr_type = ~0;
1715                 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1716                                &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1717                                sizeof(key->ipv4.src));
1718                 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1719                                &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1720                                sizeof(key->ipv4.dst));
1721         } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1722                 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1723                 mask->control.addr_type = ~0;
1724                 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1725                                &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1726                                sizeof(key->ipv6.src));
1727                 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1728                                &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1729                                sizeof(key->ipv6.dst));
1730         }
1731
1732         if (key->basic.ip_proto == IPPROTO_TCP) {
1733                 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1734                                &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1735                                sizeof(key->tp.src));
1736                 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1737                                &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1738                                sizeof(key->tp.dst));
1739                 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1740                                &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1741                                sizeof(key->tcp.flags));
1742         } else if (key->basic.ip_proto == IPPROTO_UDP) {
1743                 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1744                                &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1745                                sizeof(key->tp.src));
1746                 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1747                                &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1748                                sizeof(key->tp.dst));
1749         } else if (key->basic.ip_proto == IPPROTO_SCTP) {
1750                 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1751                                &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1752                                sizeof(key->tp.src));
1753                 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1754                                &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1755                                sizeof(key->tp.dst));
1756         } else if (key->basic.n_proto == htons(ETH_P_IP) &&
1757                    key->basic.ip_proto == IPPROTO_ICMP) {
1758                 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1759                                &mask->icmp.type,
1760                                TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1761                                sizeof(key->icmp.type));
1762                 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1763                                &mask->icmp.code,
1764                                TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1765                                sizeof(key->icmp.code));
1766         } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1767                    key->basic.ip_proto == IPPROTO_ICMPV6) {
1768                 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1769                                &mask->icmp.type,
1770                                TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1771                                sizeof(key->icmp.type));
1772                 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
1773                                &mask->icmp.code,
1774                                TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1775                                sizeof(key->icmp.code));
1776         } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1777                    key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
1778                 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack);
1779                 if (ret)
1780                         return ret;
1781         } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
1782                    key->basic.n_proto == htons(ETH_P_RARP)) {
1783                 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
1784                                &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
1785                                sizeof(key->arp.sip));
1786                 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
1787                                &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
1788                                sizeof(key->arp.tip));
1789                 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
1790                                &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
1791                                sizeof(key->arp.op));
1792                 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1793                                mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1794                                sizeof(key->arp.sha));
1795                 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1796                                mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1797                                sizeof(key->arp.tha));
1798         } else if (key->basic.ip_proto == IPPROTO_L2TP) {
1799                 fl_set_key_val(tb, &key->l2tpv3.session_id,
1800                                TCA_FLOWER_KEY_L2TPV3_SID,
1801                                &mask->l2tpv3.session_id, TCA_FLOWER_UNSPEC,
1802                                sizeof(key->l2tpv3.session_id));
1803         }
1804
1805         if (key->basic.ip_proto == IPPROTO_TCP ||
1806             key->basic.ip_proto == IPPROTO_UDP ||
1807             key->basic.ip_proto == IPPROTO_SCTP) {
1808                 ret = fl_set_key_port_range(tb, key, mask, extack);
1809                 if (ret)
1810                         return ret;
1811         }
1812
1813         if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
1814             tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
1815                 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1816                 mask->enc_control.addr_type = ~0;
1817                 fl_set_key_val(tb, &key->enc_ipv4.src,
1818                                TCA_FLOWER_KEY_ENC_IPV4_SRC,
1819                                &mask->enc_ipv4.src,
1820                                TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1821                                sizeof(key->enc_ipv4.src));
1822                 fl_set_key_val(tb, &key->enc_ipv4.dst,
1823                                TCA_FLOWER_KEY_ENC_IPV4_DST,
1824                                &mask->enc_ipv4.dst,
1825                                TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1826                                sizeof(key->enc_ipv4.dst));
1827         }
1828
1829         if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
1830             tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
1831                 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1832                 mask->enc_control.addr_type = ~0;
1833                 fl_set_key_val(tb, &key->enc_ipv6.src,
1834                                TCA_FLOWER_KEY_ENC_IPV6_SRC,
1835                                &mask->enc_ipv6.src,
1836                                TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1837                                sizeof(key->enc_ipv6.src));
1838                 fl_set_key_val(tb, &key->enc_ipv6.dst,
1839                                TCA_FLOWER_KEY_ENC_IPV6_DST,
1840                                &mask->enc_ipv6.dst,
1841                                TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1842                                sizeof(key->enc_ipv6.dst));
1843         }
1844
1845         fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
1846                        &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
1847                        sizeof(key->enc_key_id.keyid));
1848
1849         fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1850                        &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1851                        sizeof(key->enc_tp.src));
1852
1853         fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1854                        &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1855                        sizeof(key->enc_tp.dst));
1856
1857         fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
1858
1859         fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
1860                        &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
1861                        sizeof(key->hash.hash));
1862
1863         if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
1864                 ret = fl_set_enc_opt(tb, key, mask, extack);
1865                 if (ret)
1866                         return ret;
1867         }
1868
1869         ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack);
1870         if (ret)
1871                 return ret;
1872
1873         if (tb[TCA_FLOWER_KEY_FLAGS])
1874                 ret = fl_set_key_flags(tb, &key->control.flags,
1875                                        &mask->control.flags, extack);
1876
1877         return ret;
1878 }
1879
1880 static void fl_mask_copy(struct fl_flow_mask *dst,
1881                          struct fl_flow_mask *src)
1882 {
1883         const void *psrc = fl_key_get_start(&src->key, src);
1884         void *pdst = fl_key_get_start(&dst->key, src);
1885
1886         memcpy(pdst, psrc, fl_mask_range(src));
1887         dst->range = src->range;
1888 }
1889
1890 static const struct rhashtable_params fl_ht_params = {
1891         .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
1892         .head_offset = offsetof(struct cls_fl_filter, ht_node),
1893         .automatic_shrinking = true,
1894 };
1895
1896 static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
1897 {
1898         mask->filter_ht_params = fl_ht_params;
1899         mask->filter_ht_params.key_len = fl_mask_range(mask);
1900         mask->filter_ht_params.key_offset += mask->range.start;
1901
1902         return rhashtable_init(&mask->ht, &mask->filter_ht_params);
1903 }
1904
1905 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
1906 #define FL_KEY_MEMBER_SIZE(member) sizeof_field(struct fl_flow_key, member)
1907
1908 #define FL_KEY_IS_MASKED(mask, member)                                          \
1909         memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member),               \
1910                    0, FL_KEY_MEMBER_SIZE(member))                               \
1911
1912 #define FL_KEY_SET(keys, cnt, id, member)                                       \
1913         do {                                                                    \
1914                 keys[cnt].key_id = id;                                          \
1915                 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member);                \
1916                 cnt++;                                                          \
1917         } while(0);
1918
1919 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member)                       \
1920         do {                                                                    \
1921                 if (FL_KEY_IS_MASKED(mask, member))                             \
1922                         FL_KEY_SET(keys, cnt, id, member);                      \
1923         } while(0);
1924
1925 static void fl_init_dissector(struct flow_dissector *dissector,
1926                               struct fl_flow_key *mask)
1927 {
1928         struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1929         size_t cnt = 0;
1930
1931         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1932                              FLOW_DISSECTOR_KEY_META, meta);
1933         FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
1934         FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
1935         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1936                              FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
1937         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1938                              FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
1939         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1940                              FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
1941         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1942                              FLOW_DISSECTOR_KEY_PORTS, tp);
1943         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1944                              FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range);
1945         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1946                              FLOW_DISSECTOR_KEY_IP, ip);
1947         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1948                              FLOW_DISSECTOR_KEY_TCP, tcp);
1949         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1950                              FLOW_DISSECTOR_KEY_ICMP, icmp);
1951         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1952                              FLOW_DISSECTOR_KEY_ARP, arp);
1953         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1954                              FLOW_DISSECTOR_KEY_MPLS, mpls);
1955         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1956                              FLOW_DISSECTOR_KEY_VLAN, vlan);
1957         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1958                              FLOW_DISSECTOR_KEY_CVLAN, cvlan);
1959         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1960                              FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
1961         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1962                              FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
1963         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1964                              FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
1965         if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1966             FL_KEY_IS_MASKED(mask, enc_ipv6))
1967                 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1968                            enc_control);
1969         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1970                              FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
1971         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1972                              FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
1973         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1974                              FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
1975         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1976                              FLOW_DISSECTOR_KEY_CT, ct);
1977         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1978                              FLOW_DISSECTOR_KEY_HASH, hash);
1979         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1980                              FLOW_DISSECTOR_KEY_NUM_OF_VLANS, num_of_vlans);
1981         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1982                              FLOW_DISSECTOR_KEY_PPPOE, pppoe);
1983         FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1984                              FLOW_DISSECTOR_KEY_L2TPV3, l2tpv3);
1985
1986         skb_flow_dissector_init(dissector, keys, cnt);
1987 }
1988
1989 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1990                                                struct fl_flow_mask *mask)
1991 {
1992         struct fl_flow_mask *newmask;
1993         int err;
1994
1995         newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1996         if (!newmask)
1997                 return ERR_PTR(-ENOMEM);
1998
1999         fl_mask_copy(newmask, mask);
2000
2001         if ((newmask->key.tp_range.tp_min.dst &&
2002              newmask->key.tp_range.tp_max.dst) ||
2003             (newmask->key.tp_range.tp_min.src &&
2004              newmask->key.tp_range.tp_max.src))
2005                 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
2006
2007         err = fl_init_mask_hashtable(newmask);
2008         if (err)
2009                 goto errout_free;
2010
2011         fl_init_dissector(&newmask->dissector, &newmask->key);
2012
2013         INIT_LIST_HEAD_RCU(&newmask->filters);
2014
2015         refcount_set(&newmask->refcnt, 1);
2016         err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
2017                                       &newmask->ht_node, mask_ht_params);
2018         if (err)
2019                 goto errout_destroy;
2020
2021         spin_lock(&head->masks_lock);
2022         list_add_tail_rcu(&newmask->list, &head->masks);
2023         spin_unlock(&head->masks_lock);
2024
2025         return newmask;
2026
2027 errout_destroy:
2028         rhashtable_destroy(&newmask->ht);
2029 errout_free:
2030         kfree(newmask);
2031
2032         return ERR_PTR(err);
2033 }
2034
2035 static int fl_check_assign_mask(struct cls_fl_head *head,
2036                                 struct cls_fl_filter *fnew,
2037                                 struct cls_fl_filter *fold,
2038                                 struct fl_flow_mask *mask)
2039 {
2040         struct fl_flow_mask *newmask;
2041         int ret = 0;
2042
2043         rcu_read_lock();
2044
2045         /* Insert mask as temporary node to prevent concurrent creation of mask
2046          * with same key. Any concurrent lookups with same key will return
2047          * -EAGAIN because mask's refcnt is zero.
2048          */
2049         fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
2050                                                        &mask->ht_node,
2051                                                        mask_ht_params);
2052         if (!fnew->mask) {
2053                 rcu_read_unlock();
2054
2055                 if (fold) {
2056                         ret = -EINVAL;
2057                         goto errout_cleanup;
2058                 }
2059
2060                 newmask = fl_create_new_mask(head, mask);
2061                 if (IS_ERR(newmask)) {
2062                         ret = PTR_ERR(newmask);
2063                         goto errout_cleanup;
2064                 }
2065
2066                 fnew->mask = newmask;
2067                 return 0;
2068         } else if (IS_ERR(fnew->mask)) {
2069                 ret = PTR_ERR(fnew->mask);
2070         } else if (fold && fold->mask != fnew->mask) {
2071                 ret = -EINVAL;
2072         } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
2073                 /* Mask was deleted concurrently, try again */
2074                 ret = -EAGAIN;
2075         }
2076         rcu_read_unlock();
2077         return ret;
2078
2079 errout_cleanup:
2080         rhashtable_remove_fast(&head->ht, &mask->ht_node,
2081                                mask_ht_params);
2082         return ret;
2083 }
2084
2085 static int fl_set_parms(struct net *net, struct tcf_proto *tp,
2086                         struct cls_fl_filter *f, struct fl_flow_mask *mask,
2087                         unsigned long base, struct nlattr **tb,
2088                         struct nlattr *est,
2089                         struct fl_flow_tmplt *tmplt,
2090                         u32 flags, u32 fl_flags,
2091                         struct netlink_ext_ack *extack)
2092 {
2093         int err;
2094
2095         err = tcf_exts_validate_ex(net, tp, tb, est, &f->exts, flags,
2096                                    fl_flags, extack);
2097         if (err < 0)
2098                 return err;
2099
2100         if (tb[TCA_FLOWER_CLASSID]) {
2101                 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
2102                 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2103                         rtnl_lock();
2104                 tcf_bind_filter(tp, &f->res, base);
2105                 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2106                         rtnl_unlock();
2107         }
2108
2109         err = fl_set_key(net, tb, &f->key, &mask->key, extack);
2110         if (err)
2111                 return err;
2112
2113         fl_mask_update_range(mask);
2114         fl_set_masked_key(&f->mkey, &f->key, mask);
2115
2116         if (!fl_mask_fits_tmplt(tmplt, mask)) {
2117                 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
2118                 return -EINVAL;
2119         }
2120
2121         return 0;
2122 }
2123
2124 static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
2125                                struct cls_fl_filter *fold,
2126                                bool *in_ht)
2127 {
2128         struct fl_flow_mask *mask = fnew->mask;
2129         int err;
2130
2131         err = rhashtable_lookup_insert_fast(&mask->ht,
2132                                             &fnew->ht_node,
2133                                             mask->filter_ht_params);
2134         if (err) {
2135                 *in_ht = false;
2136                 /* It is okay if filter with same key exists when
2137                  * overwriting.
2138                  */
2139                 return fold && err == -EEXIST ? 0 : err;
2140         }
2141
2142         *in_ht = true;
2143         return 0;
2144 }
2145
2146 static int fl_change(struct net *net, struct sk_buff *in_skb,
2147                      struct tcf_proto *tp, unsigned long base,
2148                      u32 handle, struct nlattr **tca,
2149                      void **arg, u32 flags,
2150                      struct netlink_ext_ack *extack)
2151 {
2152         struct cls_fl_head *head = fl_head_dereference(tp);
2153         bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL);
2154         struct cls_fl_filter *fold = *arg;
2155         struct cls_fl_filter *fnew;
2156         struct fl_flow_mask *mask;
2157         struct nlattr **tb;
2158         bool in_ht;
2159         int err;
2160
2161         if (!tca[TCA_OPTIONS]) {
2162                 err = -EINVAL;
2163                 goto errout_fold;
2164         }
2165
2166         mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
2167         if (!mask) {
2168                 err = -ENOBUFS;
2169                 goto errout_fold;
2170         }
2171
2172         tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2173         if (!tb) {
2174                 err = -ENOBUFS;
2175                 goto errout_mask_alloc;
2176         }
2177
2178         err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
2179                                           tca[TCA_OPTIONS], fl_policy, NULL);
2180         if (err < 0)
2181                 goto errout_tb;
2182
2183         if (fold && handle && fold->handle != handle) {
2184                 err = -EINVAL;
2185                 goto errout_tb;
2186         }
2187
2188         fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
2189         if (!fnew) {
2190                 err = -ENOBUFS;
2191                 goto errout_tb;
2192         }
2193         INIT_LIST_HEAD(&fnew->hw_list);
2194         refcount_set(&fnew->refcnt, 1);
2195
2196         err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0);
2197         if (err < 0)
2198                 goto errout;
2199
2200         if (tb[TCA_FLOWER_FLAGS]) {
2201                 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
2202
2203                 if (!tc_flags_valid(fnew->flags)) {
2204                         err = -EINVAL;
2205                         goto errout;
2206                 }
2207         }
2208
2209         err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE],
2210                            tp->chain->tmplt_priv, flags, fnew->flags,
2211                            extack);
2212         if (err)
2213                 goto errout;
2214
2215         err = fl_check_assign_mask(head, fnew, fold, mask);
2216         if (err)
2217                 goto errout;
2218
2219         err = fl_ht_insert_unique(fnew, fold, &in_ht);
2220         if (err)
2221                 goto errout_mask;
2222
2223         if (!tc_skip_hw(fnew->flags)) {
2224                 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
2225                 if (err)
2226                         goto errout_ht;
2227         }
2228
2229         if (!tc_in_hw(fnew->flags))
2230                 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
2231
2232         spin_lock(&tp->lock);
2233
2234         /* tp was deleted concurrently. -EAGAIN will cause caller to lookup
2235          * proto again or create new one, if necessary.
2236          */
2237         if (tp->deleting) {
2238                 err = -EAGAIN;
2239                 goto errout_hw;
2240         }
2241
2242         if (fold) {
2243                 /* Fold filter was deleted concurrently. Retry lookup. */
2244                 if (fold->deleted) {
2245                         err = -EAGAIN;
2246                         goto errout_hw;
2247                 }
2248
2249                 fnew->handle = handle;
2250
2251                 if (!in_ht) {
2252                         struct rhashtable_params params =
2253                                 fnew->mask->filter_ht_params;
2254
2255                         err = rhashtable_insert_fast(&fnew->mask->ht,
2256                                                      &fnew->ht_node,
2257                                                      params);
2258                         if (err)
2259                                 goto errout_hw;
2260                         in_ht = true;
2261                 }
2262
2263                 refcount_inc(&fnew->refcnt);
2264                 rhashtable_remove_fast(&fold->mask->ht,
2265                                        &fold->ht_node,
2266                                        fold->mask->filter_ht_params);
2267                 idr_replace(&head->handle_idr, fnew, fnew->handle);
2268                 list_replace_rcu(&fold->list, &fnew->list);
2269                 fold->deleted = true;
2270
2271                 spin_unlock(&tp->lock);
2272
2273                 fl_mask_put(head, fold->mask);
2274                 if (!tc_skip_hw(fold->flags))
2275                         fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
2276                 tcf_unbind_filter(tp, &fold->res);
2277                 /* Caller holds reference to fold, so refcnt is always > 0
2278                  * after this.
2279                  */
2280                 refcount_dec(&fold->refcnt);
2281                 __fl_put(fold);
2282         } else {
2283                 if (handle) {
2284                         /* user specifies a handle and it doesn't exist */
2285                         err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
2286                                             handle, GFP_ATOMIC);
2287
2288                         /* Filter with specified handle was concurrently
2289                          * inserted after initial check in cls_api. This is not
2290                          * necessarily an error if NLM_F_EXCL is not set in
2291                          * message flags. Returning EAGAIN will cause cls_api to
2292                          * try to update concurrently inserted rule.
2293                          */
2294                         if (err == -ENOSPC)
2295                                 err = -EAGAIN;
2296                 } else {
2297                         handle = 1;
2298                         err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
2299                                             INT_MAX, GFP_ATOMIC);
2300                 }
2301                 if (err)
2302                         goto errout_hw;
2303
2304                 refcount_inc(&fnew->refcnt);
2305                 fnew->handle = handle;
2306                 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
2307                 spin_unlock(&tp->lock);
2308         }
2309
2310         *arg = fnew;
2311
2312         kfree(tb);
2313         tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
2314         return 0;
2315
2316 errout_ht:
2317         spin_lock(&tp->lock);
2318 errout_hw:
2319         fnew->deleted = true;
2320         spin_unlock(&tp->lock);
2321         if (!tc_skip_hw(fnew->flags))
2322                 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
2323         if (in_ht)
2324                 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
2325                                        fnew->mask->filter_ht_params);
2326 errout_mask:
2327         fl_mask_put(head, fnew->mask);
2328 errout:
2329         __fl_put(fnew);
2330 errout_tb:
2331         kfree(tb);
2332 errout_mask_alloc:
2333         tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
2334 errout_fold:
2335         if (fold)
2336                 __fl_put(fold);
2337         return err;
2338 }
2339
2340 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
2341                      bool rtnl_held, struct netlink_ext_ack *extack)
2342 {
2343         struct cls_fl_head *head = fl_head_dereference(tp);
2344         struct cls_fl_filter *f = arg;
2345         bool last_on_mask;
2346         int err = 0;
2347
2348         err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
2349         *last = list_empty(&head->masks);
2350         __fl_put(f);
2351
2352         return err;
2353 }
2354
2355 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
2356                     bool rtnl_held)
2357 {
2358         struct cls_fl_head *head = fl_head_dereference(tp);
2359         unsigned long id = arg->cookie, tmp;
2360         struct cls_fl_filter *f;
2361
2362         arg->count = arg->skip;
2363
2364         rcu_read_lock();
2365         idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) {
2366                 /* don't return filters that are being deleted */
2367                 if (!refcount_inc_not_zero(&f->refcnt))
2368                         continue;
2369                 rcu_read_unlock();
2370
2371                 if (arg->fn(tp, f, arg) < 0) {
2372                         __fl_put(f);
2373                         arg->stop = 1;
2374                         rcu_read_lock();
2375                         break;
2376                 }
2377                 __fl_put(f);
2378                 arg->count++;
2379                 rcu_read_lock();
2380         }
2381         rcu_read_unlock();
2382         arg->cookie = id;
2383 }
2384
2385 static struct cls_fl_filter *
2386 fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
2387 {
2388         struct cls_fl_head *head = fl_head_dereference(tp);
2389
2390         spin_lock(&tp->lock);
2391         if (list_empty(&head->hw_filters)) {
2392                 spin_unlock(&tp->lock);
2393                 return NULL;
2394         }
2395
2396         if (!f)
2397                 f = list_entry(&head->hw_filters, struct cls_fl_filter,
2398                                hw_list);
2399         list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
2400                 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
2401                         spin_unlock(&tp->lock);
2402                         return f;
2403                 }
2404         }
2405
2406         spin_unlock(&tp->lock);
2407         return NULL;
2408 }
2409
2410 static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
2411                         void *cb_priv, struct netlink_ext_ack *extack)
2412 {
2413         struct tcf_block *block = tp->chain->block;
2414         struct flow_cls_offload cls_flower = {};
2415         struct cls_fl_filter *f = NULL;
2416         int err;
2417
2418         /* hw_filters list can only be changed by hw offload functions after
2419          * obtaining rtnl lock. Make sure it is not changed while reoffload is
2420          * iterating it.
2421          */
2422         ASSERT_RTNL();
2423
2424         while ((f = fl_get_next_hw_filter(tp, f, add))) {
2425                 cls_flower.rule =
2426                         flow_rule_alloc(tcf_exts_num_actions(&f->exts));
2427                 if (!cls_flower.rule) {
2428                         __fl_put(f);
2429                         return -ENOMEM;
2430                 }
2431
2432                 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
2433                                            extack);
2434                 cls_flower.command = add ?
2435                         FLOW_CLS_REPLACE : FLOW_CLS_DESTROY;
2436                 cls_flower.cookie = (unsigned long)f;
2437                 cls_flower.rule->match.dissector = &f->mask->dissector;
2438                 cls_flower.rule->match.mask = &f->mask->key;
2439                 cls_flower.rule->match.key = &f->mkey;
2440
2441                 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts,
2442                                               cls_flower.common.extack);
2443                 if (err) {
2444                         kfree(cls_flower.rule);
2445                         if (tc_skip_sw(f->flags)) {
2446                                 __fl_put(f);
2447                                 return err;
2448                         }
2449                         goto next_flow;
2450                 }
2451
2452                 cls_flower.classid = f->res.classid;
2453
2454                 err = tc_setup_cb_reoffload(block, tp, add, cb,
2455                                             TC_SETUP_CLSFLOWER, &cls_flower,
2456                                             cb_priv, &f->flags,
2457                                             &f->in_hw_count);
2458                 tc_cleanup_offload_action(&cls_flower.rule->action);
2459                 kfree(cls_flower.rule);
2460
2461                 if (err) {
2462                         __fl_put(f);
2463                         return err;
2464                 }
2465 next_flow:
2466                 __fl_put(f);
2467         }
2468
2469         return 0;
2470 }
2471
2472 static void fl_hw_add(struct tcf_proto *tp, void *type_data)
2473 {
2474         struct flow_cls_offload *cls_flower = type_data;
2475         struct cls_fl_filter *f =
2476                 (struct cls_fl_filter *) cls_flower->cookie;
2477         struct cls_fl_head *head = fl_head_dereference(tp);
2478
2479         spin_lock(&tp->lock);
2480         list_add(&f->hw_list, &head->hw_filters);
2481         spin_unlock(&tp->lock);
2482 }
2483
2484 static void fl_hw_del(struct tcf_proto *tp, void *type_data)
2485 {
2486         struct flow_cls_offload *cls_flower = type_data;
2487         struct cls_fl_filter *f =
2488                 (struct cls_fl_filter *) cls_flower->cookie;
2489
2490         spin_lock(&tp->lock);
2491         if (!list_empty(&f->hw_list))
2492                 list_del_init(&f->hw_list);
2493         spin_unlock(&tp->lock);
2494 }
2495
2496 static int fl_hw_create_tmplt(struct tcf_chain *chain,
2497                               struct fl_flow_tmplt *tmplt)
2498 {
2499         struct flow_cls_offload cls_flower = {};
2500         struct tcf_block *block = chain->block;
2501
2502         cls_flower.rule = flow_rule_alloc(0);
2503         if (!cls_flower.rule)
2504                 return -ENOMEM;
2505
2506         cls_flower.common.chain_index = chain->index;
2507         cls_flower.command = FLOW_CLS_TMPLT_CREATE;
2508         cls_flower.cookie = (unsigned long) tmplt;
2509         cls_flower.rule->match.dissector = &tmplt->dissector;
2510         cls_flower.rule->match.mask = &tmplt->mask;
2511         cls_flower.rule->match.key = &tmplt->dummy_key;
2512
2513         /* We don't care if driver (any of them) fails to handle this
2514          * call. It serves just as a hint for it.
2515          */
2516         tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
2517         kfree(cls_flower.rule);
2518
2519         return 0;
2520 }
2521
2522 static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
2523                                 struct fl_flow_tmplt *tmplt)
2524 {
2525         struct flow_cls_offload cls_flower = {};
2526         struct tcf_block *block = chain->block;
2527
2528         cls_flower.common.chain_index = chain->index;
2529         cls_flower.command = FLOW_CLS_TMPLT_DESTROY;
2530         cls_flower.cookie = (unsigned long) tmplt;
2531
2532         tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
2533 }
2534
2535 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
2536                              struct nlattr **tca,
2537                              struct netlink_ext_ack *extack)
2538 {
2539         struct fl_flow_tmplt *tmplt;
2540         struct nlattr **tb;
2541         int err;
2542
2543         if (!tca[TCA_OPTIONS])
2544                 return ERR_PTR(-EINVAL);
2545
2546         tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2547         if (!tb)
2548                 return ERR_PTR(-ENOBUFS);
2549         err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
2550                                           tca[TCA_OPTIONS], fl_policy, NULL);
2551         if (err)
2552                 goto errout_tb;
2553
2554         tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
2555         if (!tmplt) {
2556                 err = -ENOMEM;
2557                 goto errout_tb;
2558         }
2559         tmplt->chain = chain;
2560         err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
2561         if (err)
2562                 goto errout_tmplt;
2563
2564         fl_init_dissector(&tmplt->dissector, &tmplt->mask);
2565
2566         err = fl_hw_create_tmplt(chain, tmplt);
2567         if (err)
2568                 goto errout_tmplt;
2569
2570         kfree(tb);
2571         return tmplt;
2572
2573 errout_tmplt:
2574         kfree(tmplt);
2575 errout_tb:
2576         kfree(tb);
2577         return ERR_PTR(err);
2578 }
2579
2580 static void fl_tmplt_destroy(void *tmplt_priv)
2581 {
2582         struct fl_flow_tmplt *tmplt = tmplt_priv;
2583
2584         fl_hw_destroy_tmplt(tmplt->chain, tmplt);
2585         kfree(tmplt);
2586 }
2587
2588 static int fl_dump_key_val(struct sk_buff *skb,
2589                            void *val, int val_type,
2590                            void *mask, int mask_type, int len)
2591 {
2592         int err;
2593
2594         if (!memchr_inv(mask, 0, len))
2595                 return 0;
2596         err = nla_put(skb, val_type, len, val);
2597         if (err)
2598                 return err;
2599         if (mask_type != TCA_FLOWER_UNSPEC) {
2600                 err = nla_put(skb, mask_type, len, mask);
2601                 if (err)
2602                         return err;
2603         }
2604         return 0;
2605 }
2606
2607 static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
2608                                   struct fl_flow_key *mask)
2609 {
2610         if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst,
2611                             TCA_FLOWER_KEY_PORT_DST_MIN,
2612                             &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC,
2613                             sizeof(key->tp_range.tp_min.dst)) ||
2614             fl_dump_key_val(skb, &key->tp_range.tp_max.dst,
2615                             TCA_FLOWER_KEY_PORT_DST_MAX,
2616                             &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC,
2617                             sizeof(key->tp_range.tp_max.dst)) ||
2618             fl_dump_key_val(skb, &key->tp_range.tp_min.src,
2619                             TCA_FLOWER_KEY_PORT_SRC_MIN,
2620                             &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC,
2621                             sizeof(key->tp_range.tp_min.src)) ||
2622             fl_dump_key_val(skb, &key->tp_range.tp_max.src,
2623                             TCA_FLOWER_KEY_PORT_SRC_MAX,
2624                             &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC,
2625                             sizeof(key->tp_range.tp_max.src)))
2626                 return -1;
2627
2628         return 0;
2629 }
2630
2631 static int fl_dump_key_mpls_opt_lse(struct sk_buff *skb,
2632                                     struct flow_dissector_key_mpls *mpls_key,
2633                                     struct flow_dissector_key_mpls *mpls_mask,
2634                                     u8 lse_index)
2635 {
2636         struct flow_dissector_mpls_lse *lse_mask = &mpls_mask->ls[lse_index];
2637         struct flow_dissector_mpls_lse *lse_key = &mpls_key->ls[lse_index];
2638         int err;
2639
2640         err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH,
2641                          lse_index + 1);
2642         if (err)
2643                 return err;
2644
2645         if (lse_mask->mpls_ttl) {
2646                 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL,
2647                                  lse_key->mpls_ttl);
2648                 if (err)
2649                         return err;
2650         }
2651         if (lse_mask->mpls_bos) {
2652                 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS,
2653                                  lse_key->mpls_bos);
2654                 if (err)
2655                         return err;
2656         }
2657         if (lse_mask->mpls_tc) {
2658                 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC,
2659                                  lse_key->mpls_tc);
2660                 if (err)
2661                         return err;
2662         }
2663         if (lse_mask->mpls_label) {
2664                 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL,
2665                                   lse_key->mpls_label);
2666                 if (err)
2667                         return err;
2668         }
2669
2670         return 0;
2671 }
2672
2673 static int fl_dump_key_mpls_opts(struct sk_buff *skb,
2674                                  struct flow_dissector_key_mpls *mpls_key,
2675                                  struct flow_dissector_key_mpls *mpls_mask)
2676 {
2677         struct nlattr *opts;
2678         struct nlattr *lse;
2679         u8 lse_index;
2680         int err;
2681
2682         opts = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS);
2683         if (!opts)
2684                 return -EMSGSIZE;
2685
2686         for (lse_index = 0; lse_index < FLOW_DIS_MPLS_MAX; lse_index++) {
2687                 if (!(mpls_mask->used_lses & 1 << lse_index))
2688                         continue;
2689
2690                 lse = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS_LSE);
2691                 if (!lse) {
2692                         err = -EMSGSIZE;
2693                         goto err_opts;
2694                 }
2695
2696                 err = fl_dump_key_mpls_opt_lse(skb, mpls_key, mpls_mask,
2697                                                lse_index);
2698                 if (err)
2699                         goto err_opts_lse;
2700                 nla_nest_end(skb, lse);
2701         }
2702         nla_nest_end(skb, opts);
2703
2704         return 0;
2705
2706 err_opts_lse:
2707         nla_nest_cancel(skb, lse);
2708 err_opts:
2709         nla_nest_cancel(skb, opts);
2710
2711         return err;
2712 }
2713
2714 static int fl_dump_key_mpls(struct sk_buff *skb,
2715                             struct flow_dissector_key_mpls *mpls_key,
2716                             struct flow_dissector_key_mpls *mpls_mask)
2717 {
2718         struct flow_dissector_mpls_lse *lse_mask;
2719         struct flow_dissector_mpls_lse *lse_key;
2720         int err;
2721
2722         if (!mpls_mask->used_lses)
2723                 return 0;
2724
2725         lse_mask = &mpls_mask->ls[0];
2726         lse_key = &mpls_key->ls[0];
2727
2728         /* For backward compatibility, don't use the MPLS nested attributes if
2729          * the rule can be expressed using the old attributes.
2730          */
2731         if (mpls_mask->used_lses & ~1 ||
2732             (!lse_mask->mpls_ttl && !lse_mask->mpls_bos &&
2733              !lse_mask->mpls_tc && !lse_mask->mpls_label))
2734                 return fl_dump_key_mpls_opts(skb, mpls_key, mpls_mask);
2735
2736         if (lse_mask->mpls_ttl) {
2737                 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
2738                                  lse_key->mpls_ttl);
2739                 if (err)
2740                         return err;
2741         }
2742         if (lse_mask->mpls_tc) {
2743                 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
2744                                  lse_key->mpls_tc);
2745                 if (err)
2746                         return err;
2747         }
2748         if (lse_mask->mpls_label) {
2749                 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
2750                                   lse_key->mpls_label);
2751                 if (err)
2752                         return err;
2753         }
2754         if (lse_mask->mpls_bos) {
2755                 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
2756                                  lse_key->mpls_bos);
2757                 if (err)
2758                         return err;
2759         }
2760         return 0;
2761 }
2762
2763 static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
2764                           struct flow_dissector_key_ip *key,
2765                           struct flow_dissector_key_ip *mask)
2766 {
2767         int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
2768         int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
2769         int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
2770         int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
2771
2772         if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
2773             fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
2774                 return -1;
2775
2776         return 0;
2777 }
2778
2779 static int fl_dump_key_vlan(struct sk_buff *skb,
2780                             int vlan_id_key, int vlan_prio_key,
2781                             struct flow_dissector_key_vlan *vlan_key,
2782                             struct flow_dissector_key_vlan *vlan_mask)
2783 {
2784         int err;
2785
2786         if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
2787                 return 0;
2788         if (vlan_mask->vlan_id) {
2789                 err = nla_put_u16(skb, vlan_id_key,
2790                                   vlan_key->vlan_id);
2791                 if (err)
2792                         return err;
2793         }
2794         if (vlan_mask->vlan_priority) {
2795                 err = nla_put_u8(skb, vlan_prio_key,
2796                                  vlan_key->vlan_priority);
2797                 if (err)
2798                         return err;
2799         }
2800         return 0;
2801 }
2802
2803 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
2804                             u32 *flower_key, u32 *flower_mask,
2805                             u32 flower_flag_bit, u32 dissector_flag_bit)
2806 {
2807         if (dissector_mask & dissector_flag_bit) {
2808                 *flower_mask |= flower_flag_bit;
2809                 if (dissector_key & dissector_flag_bit)
2810                         *flower_key |= flower_flag_bit;
2811         }
2812 }
2813
2814 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
2815 {
2816         u32 key, mask;
2817         __be32 _key, _mask;
2818         int err;
2819
2820         if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
2821                 return 0;
2822
2823         key = 0;
2824         mask = 0;
2825
2826         fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2827                         TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
2828         fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2829                         TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
2830                         FLOW_DIS_FIRST_FRAG);
2831
2832         _key = cpu_to_be32(key);
2833         _mask = cpu_to_be32(mask);
2834
2835         err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
2836         if (err)
2837                 return err;
2838
2839         return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
2840 }
2841
2842 static int fl_dump_key_geneve_opt(struct sk_buff *skb,
2843                                   struct flow_dissector_key_enc_opts *enc_opts)
2844 {
2845         struct geneve_opt *opt;
2846         struct nlattr *nest;
2847         int opt_off = 0;
2848
2849         nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
2850         if (!nest)
2851                 goto nla_put_failure;
2852
2853         while (enc_opts->len > opt_off) {
2854                 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
2855
2856                 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
2857                                  opt->opt_class))
2858                         goto nla_put_failure;
2859                 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
2860                                opt->type))
2861                         goto nla_put_failure;
2862                 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
2863                             opt->length * 4, opt->opt_data))
2864                         goto nla_put_failure;
2865
2866                 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
2867         }
2868         nla_nest_end(skb, nest);
2869         return 0;
2870
2871 nla_put_failure:
2872         nla_nest_cancel(skb, nest);
2873         return -EMSGSIZE;
2874 }
2875
2876 static int fl_dump_key_vxlan_opt(struct sk_buff *skb,
2877                                  struct flow_dissector_key_enc_opts *enc_opts)
2878 {
2879         struct vxlan_metadata *md;
2880         struct nlattr *nest;
2881
2882         nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN);
2883         if (!nest)
2884                 goto nla_put_failure;
2885
2886         md = (struct vxlan_metadata *)&enc_opts->data[0];
2887         if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp))
2888                 goto nla_put_failure;
2889
2890         nla_nest_end(skb, nest);
2891         return 0;
2892
2893 nla_put_failure:
2894         nla_nest_cancel(skb, nest);
2895         return -EMSGSIZE;
2896 }
2897
2898 static int fl_dump_key_erspan_opt(struct sk_buff *skb,
2899                                   struct flow_dissector_key_enc_opts *enc_opts)
2900 {
2901         struct erspan_metadata *md;
2902         struct nlattr *nest;
2903
2904         nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_ERSPAN);
2905         if (!nest)
2906                 goto nla_put_failure;
2907
2908         md = (struct erspan_metadata *)&enc_opts->data[0];
2909         if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER, md->version))
2910                 goto nla_put_failure;
2911
2912         if (md->version == 1 &&
2913             nla_put_be32(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index))
2914                 goto nla_put_failure;
2915
2916         if (md->version == 2 &&
2917             (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR,
2918                         md->u.md2.dir) ||
2919              nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID,
2920                         get_hwid(&md->u.md2))))
2921                 goto nla_put_failure;
2922
2923         nla_nest_end(skb, nest);
2924         return 0;
2925
2926 nla_put_failure:
2927         nla_nest_cancel(skb, nest);
2928         return -EMSGSIZE;
2929 }
2930
2931 static int fl_dump_key_gtp_opt(struct sk_buff *skb,
2932                                struct flow_dissector_key_enc_opts *enc_opts)
2933
2934 {
2935         struct gtp_pdu_session_info *session_info;
2936         struct nlattr *nest;
2937
2938         nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GTP);
2939         if (!nest)
2940                 goto nla_put_failure;
2941
2942         session_info = (struct gtp_pdu_session_info *)&enc_opts->data[0];
2943
2944         if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE,
2945                        session_info->pdu_type))
2946                 goto nla_put_failure;
2947
2948         if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_QFI, session_info->qfi))
2949                 goto nla_put_failure;
2950
2951         nla_nest_end(skb, nest);
2952         return 0;
2953
2954 nla_put_failure:
2955         nla_nest_cancel(skb, nest);
2956         return -EMSGSIZE;
2957 }
2958
2959 static int fl_dump_key_ct(struct sk_buff *skb,
2960                           struct flow_dissector_key_ct *key,
2961                           struct flow_dissector_key_ct *mask)
2962 {
2963         if (IS_ENABLED(CONFIG_NF_CONNTRACK) &&
2964             fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
2965                             &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
2966                             sizeof(key->ct_state)))
2967                 goto nla_put_failure;
2968
2969         if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
2970             fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
2971                             &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
2972                             sizeof(key->ct_zone)))
2973                 goto nla_put_failure;
2974
2975         if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
2976             fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
2977                             &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
2978                             sizeof(key->ct_mark)))
2979                 goto nla_put_failure;
2980
2981         if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
2982             fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
2983                             &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
2984                             sizeof(key->ct_labels)))
2985                 goto nla_put_failure;
2986
2987         return 0;
2988
2989 nla_put_failure:
2990         return -EMSGSIZE;
2991 }
2992
2993 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
2994                                struct flow_dissector_key_enc_opts *enc_opts)
2995 {
2996         struct nlattr *nest;
2997         int err;
2998
2999         if (!enc_opts->len)
3000                 return 0;
3001
3002         nest = nla_nest_start_noflag(skb, enc_opt_type);
3003         if (!nest)
3004                 goto nla_put_failure;
3005
3006         switch (enc_opts->dst_opt_type) {
3007         case TUNNEL_GENEVE_OPT:
3008                 err = fl_dump_key_geneve_opt(skb, enc_opts);
3009                 if (err)
3010                         goto nla_put_failure;
3011                 break;
3012         case TUNNEL_VXLAN_OPT:
3013                 err = fl_dump_key_vxlan_opt(skb, enc_opts);
3014                 if (err)
3015                         goto nla_put_failure;
3016                 break;
3017         case TUNNEL_ERSPAN_OPT:
3018                 err = fl_dump_key_erspan_opt(skb, enc_opts);
3019                 if (err)
3020                         goto nla_put_failure;
3021                 break;
3022         case TUNNEL_GTP_OPT:
3023                 err = fl_dump_key_gtp_opt(skb, enc_opts);
3024                 if (err)
3025                         goto nla_put_failure;
3026                 break;
3027         default:
3028                 goto nla_put_failure;
3029         }
3030         nla_nest_end(skb, nest);
3031         return 0;
3032
3033 nla_put_failure:
3034         nla_nest_cancel(skb, nest);
3035         return -EMSGSIZE;
3036 }
3037
3038 static int fl_dump_key_enc_opt(struct sk_buff *skb,
3039                                struct flow_dissector_key_enc_opts *key_opts,
3040                                struct flow_dissector_key_enc_opts *msk_opts)
3041 {
3042         int err;
3043
3044         err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
3045         if (err)
3046                 return err;
3047
3048         return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
3049 }
3050
3051 static int fl_dump_key(struct sk_buff *skb, struct net *net,
3052                        struct fl_flow_key *key, struct fl_flow_key *mask)
3053 {
3054         if (mask->meta.ingress_ifindex) {
3055                 struct net_device *dev;
3056
3057                 dev = __dev_get_by_index(net, key->meta.ingress_ifindex);
3058                 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
3059                         goto nla_put_failure;
3060         }
3061
3062         if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
3063                             mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
3064                             sizeof(key->eth.dst)) ||
3065             fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
3066                             mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
3067                             sizeof(key->eth.src)) ||
3068             fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
3069                             &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
3070                             sizeof(key->basic.n_proto)))
3071                 goto nla_put_failure;
3072
3073         if (mask->num_of_vlans.num_of_vlans) {
3074                 if (nla_put_u8(skb, TCA_FLOWER_KEY_NUM_OF_VLANS, key->num_of_vlans.num_of_vlans))
3075                         goto nla_put_failure;
3076         }
3077
3078         if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
3079                 goto nla_put_failure;
3080
3081         if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
3082                              TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
3083                 goto nla_put_failure;
3084
3085         if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
3086                              TCA_FLOWER_KEY_CVLAN_PRIO,
3087                              &key->cvlan, &mask->cvlan) ||
3088             (mask->cvlan.vlan_tpid &&
3089              nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3090                           key->cvlan.vlan_tpid)))
3091                 goto nla_put_failure;
3092
3093         if (mask->basic.n_proto) {
3094                 if (mask->cvlan.vlan_eth_type) {
3095                         if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
3096                                          key->basic.n_proto))
3097                                 goto nla_put_failure;
3098                 } else if (mask->vlan.vlan_eth_type) {
3099                         if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3100                                          key->vlan.vlan_eth_type))
3101                                 goto nla_put_failure;
3102                 }
3103         }
3104
3105         if ((key->basic.n_proto == htons(ETH_P_IP) ||
3106              key->basic.n_proto == htons(ETH_P_IPV6)) &&
3107             (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
3108                             &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
3109                             sizeof(key->basic.ip_proto)) ||
3110             fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
3111                 goto nla_put_failure;
3112
3113         if (mask->pppoe.session_id) {
3114                 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPPOE_SID,
3115                                  key->pppoe.session_id))
3116                         goto nla_put_failure;
3117         }
3118         if (mask->basic.n_proto && mask->pppoe.ppp_proto) {
3119                 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPP_PROTO,
3120                                  key->pppoe.ppp_proto))
3121                         goto nla_put_failure;
3122         }
3123
3124         if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
3125             (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
3126                              &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
3127                              sizeof(key->ipv4.src)) ||
3128              fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
3129                              &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
3130                              sizeof(key->ipv4.dst))))
3131                 goto nla_put_failure;
3132         else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
3133                  (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
3134                                   &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
3135                                   sizeof(key->ipv6.src)) ||
3136                   fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
3137                                   &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
3138                                   sizeof(key->ipv6.dst))))
3139                 goto nla_put_failure;
3140
3141         if (key->basic.ip_proto == IPPROTO_TCP &&
3142             (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
3143                              &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
3144                              sizeof(key->tp.src)) ||
3145              fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
3146                              &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
3147                              sizeof(key->tp.dst)) ||
3148              fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
3149                              &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
3150                              sizeof(key->tcp.flags))))
3151                 goto nla_put_failure;
3152         else if (key->basic.ip_proto == IPPROTO_UDP &&
3153                  (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
3154                                   &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
3155                                   sizeof(key->tp.src)) ||
3156                   fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
3157                                   &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
3158                                   sizeof(key->tp.dst))))
3159                 goto nla_put_failure;
3160         else if (key->basic.ip_proto == IPPROTO_SCTP &&
3161                  (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
3162                                   &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
3163                                   sizeof(key->tp.src)) ||
3164                   fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
3165                                   &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
3166                                   sizeof(key->tp.dst))))
3167                 goto nla_put_failure;
3168         else if (key->basic.n_proto == htons(ETH_P_IP) &&
3169                  key->basic.ip_proto == IPPROTO_ICMP &&
3170                  (fl_dump_key_val(skb, &key->icmp.type,
3171                                   TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
3172                                   TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
3173                                   sizeof(key->icmp.type)) ||
3174                   fl_dump_key_val(skb, &key->icmp.code,
3175                                   TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
3176                                   TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
3177                                   sizeof(key->icmp.code))))
3178                 goto nla_put_failure;
3179         else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
3180                  key->basic.ip_proto == IPPROTO_ICMPV6 &&
3181                  (fl_dump_key_val(skb, &key->icmp.type,
3182                                   TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
3183                                   TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
3184                                   sizeof(key->icmp.type)) ||
3185                   fl_dump_key_val(skb, &key->icmp.code,
3186                                   TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
3187                                   TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
3188                                   sizeof(key->icmp.code))))
3189                 goto nla_put_failure;
3190         else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
3191                   key->basic.n_proto == htons(ETH_P_RARP)) &&
3192                  (fl_dump_key_val(skb, &key->arp.sip,
3193                                   TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
3194                                   TCA_FLOWER_KEY_ARP_SIP_MASK,
3195                                   sizeof(key->arp.sip)) ||
3196                   fl_dump_key_val(skb, &key->arp.tip,
3197                                   TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
3198                                   TCA_FLOWER_KEY_ARP_TIP_MASK,
3199                                   sizeof(key->arp.tip)) ||
3200                   fl_dump_key_val(skb, &key->arp.op,
3201                                   TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
3202                                   TCA_FLOWER_KEY_ARP_OP_MASK,
3203                                   sizeof(key->arp.op)) ||
3204                   fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
3205                                   mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
3206                                   sizeof(key->arp.sha)) ||
3207                   fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
3208                                   mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
3209                                   sizeof(key->arp.tha))))
3210                 goto nla_put_failure;
3211         else if (key->basic.ip_proto == IPPROTO_L2TP &&
3212                  fl_dump_key_val(skb, &key->l2tpv3.session_id,
3213                                  TCA_FLOWER_KEY_L2TPV3_SID,
3214                                  &mask->l2tpv3.session_id,
3215                                  TCA_FLOWER_UNSPEC,
3216                                  sizeof(key->l2tpv3.session_id)))
3217                 goto nla_put_failure;
3218
3219         if ((key->basic.ip_proto == IPPROTO_TCP ||
3220              key->basic.ip_proto == IPPROTO_UDP ||
3221              key->basic.ip_proto == IPPROTO_SCTP) &&
3222              fl_dump_key_port_range(skb, key, mask))
3223                 goto nla_put_failure;
3224
3225         if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
3226             (fl_dump_key_val(skb, &key->enc_ipv4.src,
3227                             TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
3228                             TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
3229                             sizeof(key->enc_ipv4.src)) ||
3230              fl_dump_key_val(skb, &key->enc_ipv4.dst,
3231                              TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
3232                              TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
3233                              sizeof(key->enc_ipv4.dst))))
3234                 goto nla_put_failure;
3235         else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
3236                  (fl_dump_key_val(skb, &key->enc_ipv6.src,
3237                             TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
3238                             TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
3239                             sizeof(key->enc_ipv6.src)) ||
3240                  fl_dump_key_val(skb, &key->enc_ipv6.dst,
3241                                  TCA_FLOWER_KEY_ENC_IPV6_DST,
3242                                  &mask->enc_ipv6.dst,
3243                                  TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
3244                             sizeof(key->enc_ipv6.dst))))
3245                 goto nla_put_failure;
3246
3247         if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
3248                             &mask->enc_key_id, TCA_FLOWER_UNSPEC,
3249                             sizeof(key->enc_key_id)) ||
3250             fl_dump_key_val(skb, &key->enc_tp.src,
3251                             TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
3252                             &mask->enc_tp.src,
3253                             TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
3254                             sizeof(key->enc_tp.src)) ||
3255             fl_dump_key_val(skb, &key->enc_tp.dst,
3256                             TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
3257                             &mask->enc_tp.dst,
3258                             TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
3259                             sizeof(key->enc_tp.dst)) ||
3260             fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
3261             fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
3262                 goto nla_put_failure;
3263
3264         if (fl_dump_key_ct(skb, &key->ct, &mask->ct))
3265                 goto nla_put_failure;
3266
3267         if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
3268                 goto nla_put_failure;
3269
3270         if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
3271                              &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
3272                              sizeof(key->hash.hash)))
3273                 goto nla_put_failure;
3274
3275         return 0;
3276
3277 nla_put_failure:
3278         return -EMSGSIZE;
3279 }
3280
3281 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
3282                    struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3283 {
3284         struct cls_fl_filter *f = fh;
3285         struct nlattr *nest;
3286         struct fl_flow_key *key, *mask;
3287         bool skip_hw;
3288
3289         if (!f)
3290                 return skb->len;
3291
3292         t->tcm_handle = f->handle;
3293
3294         nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3295         if (!nest)
3296                 goto nla_put_failure;
3297
3298         spin_lock(&tp->lock);
3299
3300         if (f->res.classid &&
3301             nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
3302                 goto nla_put_failure_locked;
3303
3304         key = &f->key;
3305         mask = &f->mask->key;
3306         skip_hw = tc_skip_hw(f->flags);
3307
3308         if (fl_dump_key(skb, net, key, mask))
3309                 goto nla_put_failure_locked;
3310
3311         if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3312                 goto nla_put_failure_locked;
3313
3314         spin_unlock(&tp->lock);
3315
3316         if (!skip_hw)
3317                 fl_hw_update_stats(tp, f, rtnl_held);
3318
3319         if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
3320                 goto nla_put_failure;
3321
3322         if (tcf_exts_dump(skb, &f->exts))
3323                 goto nla_put_failure;
3324
3325         nla_nest_end(skb, nest);
3326
3327         if (tcf_exts_dump_stats(skb, &f->exts) < 0)
3328                 goto nla_put_failure;
3329
3330         return skb->len;
3331
3332 nla_put_failure_locked:
3333         spin_unlock(&tp->lock);
3334 nla_put_failure:
3335         nla_nest_cancel(skb, nest);
3336         return -1;
3337 }
3338
3339 static int fl_terse_dump(struct net *net, struct tcf_proto *tp, void *fh,
3340                          struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3341 {
3342         struct cls_fl_filter *f = fh;
3343         struct nlattr *nest;
3344         bool skip_hw;
3345
3346         if (!f)
3347                 return skb->len;
3348
3349         t->tcm_handle = f->handle;
3350
3351         nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3352         if (!nest)
3353                 goto nla_put_failure;
3354
3355         spin_lock(&tp->lock);
3356
3357         skip_hw = tc_skip_hw(f->flags);
3358
3359         if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3360                 goto nla_put_failure_locked;
3361
3362         spin_unlock(&tp->lock);
3363
3364         if (!skip_hw)
3365                 fl_hw_update_stats(tp, f, rtnl_held);
3366
3367         if (tcf_exts_terse_dump(skb, &f->exts))
3368                 goto nla_put_failure;
3369
3370         nla_nest_end(skb, nest);
3371
3372         return skb->len;
3373
3374 nla_put_failure_locked:
3375         spin_unlock(&tp->lock);
3376 nla_put_failure:
3377         nla_nest_cancel(skb, nest);
3378         return -1;
3379 }
3380
3381 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
3382 {
3383         struct fl_flow_tmplt *tmplt = tmplt_priv;
3384         struct fl_flow_key *key, *mask;
3385         struct nlattr *nest;
3386
3387         nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3388         if (!nest)
3389                 goto nla_put_failure;
3390
3391         key = &tmplt->dummy_key;
3392         mask = &tmplt->mask;
3393
3394         if (fl_dump_key(skb, net, key, mask))
3395                 goto nla_put_failure;
3396
3397         nla_nest_end(skb, nest);
3398
3399         return skb->len;
3400
3401 nla_put_failure:
3402         nla_nest_cancel(skb, nest);
3403         return -EMSGSIZE;
3404 }
3405
3406 static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
3407                           unsigned long base)
3408 {
3409         struct cls_fl_filter *f = fh;
3410
3411         tc_cls_bind_class(classid, cl, q, &f->res, base);
3412 }
3413
3414 static bool fl_delete_empty(struct tcf_proto *tp)
3415 {
3416         struct cls_fl_head *head = fl_head_dereference(tp);
3417
3418         spin_lock(&tp->lock);
3419         tp->deleting = idr_is_empty(&head->handle_idr);
3420         spin_unlock(&tp->lock);
3421
3422         return tp->deleting;
3423 }
3424
3425 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
3426         .kind           = "flower",
3427         .classify       = fl_classify,
3428         .init           = fl_init,
3429         .destroy        = fl_destroy,
3430         .get            = fl_get,
3431         .put            = fl_put,
3432         .change         = fl_change,
3433         .delete         = fl_delete,
3434         .delete_empty   = fl_delete_empty,
3435         .walk           = fl_walk,
3436         .reoffload      = fl_reoffload,
3437         .hw_add         = fl_hw_add,
3438         .hw_del         = fl_hw_del,
3439         .dump           = fl_dump,
3440         .terse_dump     = fl_terse_dump,
3441         .bind_class     = fl_bind_class,
3442         .tmplt_create   = fl_tmplt_create,
3443         .tmplt_destroy  = fl_tmplt_destroy,
3444         .tmplt_dump     = fl_tmplt_dump,
3445         .owner          = THIS_MODULE,
3446         .flags          = TCF_PROTO_OPS_DOIT_UNLOCKED,
3447 };
3448
3449 static int __init cls_fl_init(void)
3450 {
3451         return register_tcf_proto_ops(&cls_fl_ops);
3452 }
3453
3454 static void __exit cls_fl_exit(void)
3455 {
3456         unregister_tcf_proto_ops(&cls_fl_ops);
3457 }
3458
3459 module_init(cls_fl_init);
3460 module_exit(cls_fl_exit);
3461
3462 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3463 MODULE_DESCRIPTION("Flower classifier");
3464 MODULE_LICENSE("GPL v2");