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