1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/cls_flower.c Flower classifier
5 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
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 #include <linux/bitfield.h>
16 #include <linux/if_ether.h>
17 #include <linux/in6.h>
19 #include <linux/mpls.h>
20 #include <linux/ppp_defs.h>
22 #include <net/sch_generic.h>
23 #include <net/pkt_cls.h>
24 #include <net/pkt_sched.h>
26 #include <net/flow_dissector.h>
27 #include <net/geneve.h>
28 #include <net/vxlan.h>
29 #include <net/erspan.h>
31 #include <net/tc_wrapper.h>
34 #include <net/dst_metadata.h>
36 #include <uapi/linux/netfilter/nf_conntrack_common.h>
38 #define TCA_FLOWER_KEY_CT_FLAGS_MAX \
39 ((__TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) << 1)
40 #define TCA_FLOWER_KEY_CT_FLAGS_MASK \
41 (TCA_FLOWER_KEY_CT_FLAGS_MAX - 1)
44 struct flow_dissector_key_meta meta;
45 struct flow_dissector_key_control control;
46 struct flow_dissector_key_control enc_control;
47 struct flow_dissector_key_basic basic;
48 struct flow_dissector_key_eth_addrs eth;
49 struct flow_dissector_key_vlan vlan;
50 struct flow_dissector_key_vlan cvlan;
52 struct flow_dissector_key_ipv4_addrs ipv4;
53 struct flow_dissector_key_ipv6_addrs ipv6;
55 struct flow_dissector_key_ports tp;
56 struct flow_dissector_key_icmp icmp;
57 struct flow_dissector_key_arp arp;
58 struct flow_dissector_key_keyid enc_key_id;
60 struct flow_dissector_key_ipv4_addrs enc_ipv4;
61 struct flow_dissector_key_ipv6_addrs enc_ipv6;
63 struct flow_dissector_key_ports enc_tp;
64 struct flow_dissector_key_mpls mpls;
65 struct flow_dissector_key_tcp tcp;
66 struct flow_dissector_key_ip ip;
67 struct flow_dissector_key_ip enc_ip;
68 struct flow_dissector_key_enc_opts enc_opts;
69 struct flow_dissector_key_ports_range tp_range;
70 struct flow_dissector_key_ct ct;
71 struct flow_dissector_key_hash hash;
72 struct flow_dissector_key_num_of_vlans num_of_vlans;
73 struct flow_dissector_key_pppoe pppoe;
74 struct flow_dissector_key_l2tpv3 l2tpv3;
75 struct flow_dissector_key_ipsec ipsec;
76 struct flow_dissector_key_cfm cfm;
77 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
79 struct fl_flow_mask_range {
80 unsigned short int start;
81 unsigned short int end;
85 struct fl_flow_key key;
86 struct fl_flow_mask_range range;
88 struct rhash_head ht_node;
90 struct rhashtable_params filter_ht_params;
91 struct flow_dissector dissector;
92 struct list_head filters;
93 struct rcu_work rwork;
94 struct list_head list;
98 struct fl_flow_tmplt {
99 struct fl_flow_key dummy_key;
100 struct fl_flow_key mask;
101 struct flow_dissector dissector;
102 struct tcf_chain *chain;
106 struct rhashtable ht;
107 spinlock_t masks_lock; /* Protect masks list */
108 struct list_head masks;
109 struct list_head hw_filters;
110 struct rcu_work rwork;
111 struct idr handle_idr;
114 struct cls_fl_filter {
115 struct fl_flow_mask *mask;
116 struct rhash_head ht_node;
117 struct fl_flow_key mkey;
118 struct tcf_exts exts;
119 struct tcf_result res;
120 struct fl_flow_key key;
121 struct list_head list;
122 struct list_head hw_list;
126 u8 needs_tc_skb_ext:1;
127 struct rcu_work rwork;
128 struct net_device *hw_dev;
129 /* Flower classifier is unlocked, which means that its reference counter
130 * can be changed concurrently without any kind of external
131 * synchronization. Use atomic reference counter to be concurrency-safe.
137 static const struct rhashtable_params mask_ht_params = {
138 .key_offset = offsetof(struct fl_flow_mask, key),
139 .key_len = sizeof(struct fl_flow_key),
140 .head_offset = offsetof(struct fl_flow_mask, ht_node),
141 .automatic_shrinking = true,
144 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
146 return mask->range.end - mask->range.start;
149 static void fl_mask_update_range(struct fl_flow_mask *mask)
151 const u8 *bytes = (const u8 *) &mask->key;
152 size_t size = sizeof(mask->key);
153 size_t i, first = 0, last;
155 for (i = 0; i < size; i++) {
162 for (i = size - 1; i != first; i--) {
168 mask->range.start = rounddown(first, sizeof(long));
169 mask->range.end = roundup(last + 1, sizeof(long));
172 static void *fl_key_get_start(struct fl_flow_key *key,
173 const struct fl_flow_mask *mask)
175 return (u8 *) key + mask->range.start;
178 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
179 struct fl_flow_mask *mask)
181 const long *lkey = fl_key_get_start(key, mask);
182 const long *lmask = fl_key_get_start(&mask->key, mask);
183 long *lmkey = fl_key_get_start(mkey, mask);
186 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
187 *lmkey++ = *lkey++ & *lmask++;
190 static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
191 struct fl_flow_mask *mask)
193 const long *lmask = fl_key_get_start(&mask->key, mask);
199 ltmplt = fl_key_get_start(&tmplt->mask, mask);
200 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
201 if (~*ltmplt++ & *lmask++)
207 static void fl_clear_masked_range(struct fl_flow_key *key,
208 struct fl_flow_mask *mask)
210 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
213 static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
214 struct fl_flow_key *key,
215 struct fl_flow_key *mkey)
217 u16 min_mask, max_mask, min_val, max_val;
219 min_mask = ntohs(filter->mask->key.tp_range.tp_min.dst);
220 max_mask = ntohs(filter->mask->key.tp_range.tp_max.dst);
221 min_val = ntohs(filter->key.tp_range.tp_min.dst);
222 max_val = ntohs(filter->key.tp_range.tp_max.dst);
224 if (min_mask && max_mask) {
225 if (ntohs(key->tp_range.tp.dst) < min_val ||
226 ntohs(key->tp_range.tp.dst) > max_val)
229 /* skb does not have min and max values */
230 mkey->tp_range.tp_min.dst = filter->mkey.tp_range.tp_min.dst;
231 mkey->tp_range.tp_max.dst = filter->mkey.tp_range.tp_max.dst;
236 static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
237 struct fl_flow_key *key,
238 struct fl_flow_key *mkey)
240 u16 min_mask, max_mask, min_val, max_val;
242 min_mask = ntohs(filter->mask->key.tp_range.tp_min.src);
243 max_mask = ntohs(filter->mask->key.tp_range.tp_max.src);
244 min_val = ntohs(filter->key.tp_range.tp_min.src);
245 max_val = ntohs(filter->key.tp_range.tp_max.src);
247 if (min_mask && max_mask) {
248 if (ntohs(key->tp_range.tp.src) < min_val ||
249 ntohs(key->tp_range.tp.src) > max_val)
252 /* skb does not have min and max values */
253 mkey->tp_range.tp_min.src = filter->mkey.tp_range.tp_min.src;
254 mkey->tp_range.tp_max.src = filter->mkey.tp_range.tp_max.src;
259 static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
260 struct fl_flow_key *mkey)
262 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
263 mask->filter_ht_params);
266 static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
267 struct fl_flow_key *mkey,
268 struct fl_flow_key *key)
270 struct cls_fl_filter *filter, *f;
272 list_for_each_entry_rcu(filter, &mask->filters, list) {
273 if (!fl_range_port_dst_cmp(filter, key, mkey))
276 if (!fl_range_port_src_cmp(filter, key, mkey))
279 f = __fl_lookup(mask, mkey);
286 static noinline_for_stack
287 struct cls_fl_filter *fl_mask_lookup(struct fl_flow_mask *mask, struct fl_flow_key *key)
289 struct fl_flow_key mkey;
291 fl_set_masked_key(&mkey, key, mask);
292 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
293 return fl_lookup_range(mask, &mkey, key);
295 return __fl_lookup(mask, &mkey);
298 static u16 fl_ct_info_to_flower_map[] = {
299 [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
300 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
301 [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
302 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
303 [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
304 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED |
305 TCA_FLOWER_KEY_CT_FLAGS_REPLY,
306 [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
307 TCA_FLOWER_KEY_CT_FLAGS_RELATED |
308 TCA_FLOWER_KEY_CT_FLAGS_REPLY,
309 [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
310 TCA_FLOWER_KEY_CT_FLAGS_NEW,
313 TC_INDIRECT_SCOPE int fl_classify(struct sk_buff *skb,
314 const struct tcf_proto *tp,
315 struct tcf_result *res)
317 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
318 bool post_ct = tc_skb_cb(skb)->post_ct;
319 u16 zone = tc_skb_cb(skb)->zone;
320 struct fl_flow_key skb_key;
321 struct fl_flow_mask *mask;
322 struct cls_fl_filter *f;
324 list_for_each_entry_rcu(mask, &head->masks, list) {
325 flow_dissector_init_keys(&skb_key.control, &skb_key.basic);
326 fl_clear_masked_range(&skb_key, mask);
328 skb_flow_dissect_meta(skb, &mask->dissector, &skb_key);
329 /* skb_flow_dissect() does not set n_proto in case an unknown
330 * protocol, so do it rather here.
332 skb_key.basic.n_proto = skb_protocol(skb, false);
333 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
334 skb_flow_dissect_ct(skb, &mask->dissector, &skb_key,
335 fl_ct_info_to_flower_map,
336 ARRAY_SIZE(fl_ct_info_to_flower_map),
338 skb_flow_dissect_hash(skb, &mask->dissector, &skb_key);
339 skb_flow_dissect(skb, &mask->dissector, &skb_key,
340 FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP);
342 f = fl_mask_lookup(mask, &skb_key);
343 if (f && !tc_skip_sw(f->flags)) {
345 return tcf_exts_exec(skb, &f->exts, res);
351 static int fl_init(struct tcf_proto *tp)
353 struct cls_fl_head *head;
355 head = kzalloc(sizeof(*head), GFP_KERNEL);
359 spin_lock_init(&head->masks_lock);
360 INIT_LIST_HEAD_RCU(&head->masks);
361 INIT_LIST_HEAD(&head->hw_filters);
362 rcu_assign_pointer(tp->root, head);
363 idr_init(&head->handle_idr);
365 return rhashtable_init(&head->ht, &mask_ht_params);
368 static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done)
370 /* temporary masks don't have their filters list and ht initialized */
371 if (mask_init_done) {
372 WARN_ON(!list_empty(&mask->filters));
373 rhashtable_destroy(&mask->ht);
378 static void fl_mask_free_work(struct work_struct *work)
380 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
381 struct fl_flow_mask, rwork);
383 fl_mask_free(mask, true);
386 static void fl_uninit_mask_free_work(struct work_struct *work)
388 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
389 struct fl_flow_mask, rwork);
391 fl_mask_free(mask, false);
394 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask)
396 if (!refcount_dec_and_test(&mask->refcnt))
399 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
401 spin_lock(&head->masks_lock);
402 list_del_rcu(&mask->list);
403 spin_unlock(&head->masks_lock);
405 tcf_queue_work(&mask->rwork, fl_mask_free_work);
410 static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
412 /* Flower classifier only changes root pointer during init and destroy.
413 * Users must obtain reference to tcf_proto instance before calling its
414 * API, so tp->root pointer is protected from concurrent call to
415 * fl_destroy() by reference counting.
417 return rcu_dereference_raw(tp->root);
420 static void __fl_destroy_filter(struct cls_fl_filter *f)
422 if (f->needs_tc_skb_ext)
423 tc_skb_ext_tc_disable();
424 tcf_exts_destroy(&f->exts);
425 tcf_exts_put_net(&f->exts);
429 static void fl_destroy_filter_work(struct work_struct *work)
431 struct cls_fl_filter *f = container_of(to_rcu_work(work),
432 struct cls_fl_filter, rwork);
434 __fl_destroy_filter(f);
437 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
438 bool rtnl_held, struct netlink_ext_ack *extack)
440 struct tcf_block *block = tp->chain->block;
441 struct flow_cls_offload cls_flower = {};
443 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
444 cls_flower.command = FLOW_CLS_DESTROY;
445 cls_flower.cookie = (unsigned long) f;
447 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false,
448 &f->flags, &f->in_hw_count, rtnl_held);
452 static int fl_hw_replace_filter(struct tcf_proto *tp,
453 struct cls_fl_filter *f, bool rtnl_held,
454 struct netlink_ext_ack *extack)
456 struct tcf_block *block = tp->chain->block;
457 struct flow_cls_offload cls_flower = {};
458 bool skip_sw = tc_skip_sw(f->flags);
461 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
462 if (!cls_flower.rule)
465 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
466 cls_flower.command = FLOW_CLS_REPLACE;
467 cls_flower.cookie = (unsigned long) f;
468 cls_flower.rule->match.dissector = &f->mask->dissector;
469 cls_flower.rule->match.mask = &f->mask->key;
470 cls_flower.rule->match.key = &f->mkey;
471 cls_flower.classid = f->res.classid;
473 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts,
474 cls_flower.common.extack);
476 kfree(cls_flower.rule);
478 return skip_sw ? err : 0;
481 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower,
482 skip_sw, &f->flags, &f->in_hw_count, rtnl_held);
483 tc_cleanup_offload_action(&cls_flower.rule->action);
484 kfree(cls_flower.rule);
487 fl_hw_destroy_filter(tp, f, rtnl_held, NULL);
491 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
497 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f,
500 struct tcf_block *block = tp->chain->block;
501 struct flow_cls_offload cls_flower = {};
503 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
504 cls_flower.command = FLOW_CLS_STATS;
505 cls_flower.cookie = (unsigned long) f;
506 cls_flower.classid = f->res.classid;
508 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false,
511 tcf_exts_hw_stats_update(&f->exts, &cls_flower.stats, cls_flower.use_act_stats);
514 static void __fl_put(struct cls_fl_filter *f)
516 if (!refcount_dec_and_test(&f->refcnt))
519 if (tcf_exts_get_net(&f->exts))
520 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
522 __fl_destroy_filter(f);
525 static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
527 struct cls_fl_filter *f;
530 f = idr_find(&head->handle_idr, handle);
531 if (f && !refcount_inc_not_zero(&f->refcnt))
538 static struct tcf_exts *fl_get_exts(const struct tcf_proto *tp, u32 handle)
540 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
541 struct cls_fl_filter *f;
543 f = idr_find(&head->handle_idr, handle);
544 return f ? &f->exts : NULL;
547 static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
548 bool *last, bool rtnl_held,
549 struct netlink_ext_ack *extack)
551 struct cls_fl_head *head = fl_head_dereference(tp);
555 spin_lock(&tp->lock);
557 spin_unlock(&tp->lock);
562 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
563 f->mask->filter_ht_params);
564 idr_remove(&head->handle_idr, f->handle);
565 list_del_rcu(&f->list);
566 spin_unlock(&tp->lock);
568 *last = fl_mask_put(head, f->mask);
569 if (!tc_skip_hw(f->flags))
570 fl_hw_destroy_filter(tp, f, rtnl_held, extack);
571 tcf_unbind_filter(tp, &f->res);
577 static void fl_destroy_sleepable(struct work_struct *work)
579 struct cls_fl_head *head = container_of(to_rcu_work(work),
583 rhashtable_destroy(&head->ht);
585 module_put(THIS_MODULE);
588 static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
589 struct netlink_ext_ack *extack)
591 struct cls_fl_head *head = fl_head_dereference(tp);
592 struct fl_flow_mask *mask, *next_mask;
593 struct cls_fl_filter *f, *next;
596 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
597 list_for_each_entry_safe(f, next, &mask->filters, list) {
598 __fl_delete(tp, f, &last, rtnl_held, extack);
603 idr_destroy(&head->handle_idr);
605 __module_get(THIS_MODULE);
606 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
609 static void fl_put(struct tcf_proto *tp, void *arg)
611 struct cls_fl_filter *f = arg;
616 static void *fl_get(struct tcf_proto *tp, u32 handle)
618 struct cls_fl_head *head = fl_head_dereference(tp);
620 return __fl_get(head, handle);
623 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
624 [TCA_FLOWER_UNSPEC] = { .strict_start_type =
625 TCA_FLOWER_L2_MISS },
626 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
627 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
629 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
630 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
631 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
632 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
633 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
634 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
635 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
636 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
637 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
638 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
639 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
640 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
641 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
642 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
643 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
644 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
645 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
646 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
647 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
648 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
649 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
650 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
651 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
652 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
653 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
654 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
655 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
656 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
657 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
658 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
659 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
660 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
661 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
662 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
663 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
664 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
665 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
666 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
667 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
668 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
669 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
670 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
671 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
672 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
673 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
674 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
675 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
676 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
677 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
678 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
679 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
680 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
681 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
682 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
683 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
684 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
685 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
686 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
687 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
688 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
689 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
690 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
691 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
692 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
693 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
694 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
695 [TCA_FLOWER_KEY_MPLS_OPTS] = { .type = NLA_NESTED },
696 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
697 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
698 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
699 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
700 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
701 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
702 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
703 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
704 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
705 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
706 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
707 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
708 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
709 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
710 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
711 [TCA_FLOWER_KEY_CT_STATE] =
712 NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK),
713 [TCA_FLOWER_KEY_CT_STATE_MASK] =
714 NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK),
715 [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 },
716 [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 },
717 [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 },
718 [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 },
719 [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY,
720 .len = 128 / BITS_PER_BYTE },
721 [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY,
722 .len = 128 / BITS_PER_BYTE },
723 [TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
724 [TCA_FLOWER_KEY_HASH] = { .type = NLA_U32 },
725 [TCA_FLOWER_KEY_HASH_MASK] = { .type = NLA_U32 },
726 [TCA_FLOWER_KEY_NUM_OF_VLANS] = { .type = NLA_U8 },
727 [TCA_FLOWER_KEY_PPPOE_SID] = { .type = NLA_U16 },
728 [TCA_FLOWER_KEY_PPP_PROTO] = { .type = NLA_U16 },
729 [TCA_FLOWER_KEY_L2TPV3_SID] = { .type = NLA_U32 },
730 [TCA_FLOWER_KEY_SPI] = { .type = NLA_U32 },
731 [TCA_FLOWER_KEY_SPI_MASK] = { .type = NLA_U32 },
732 [TCA_FLOWER_L2_MISS] = NLA_POLICY_MAX(NLA_U8, 1),
733 [TCA_FLOWER_KEY_CFM] = { .type = NLA_NESTED },
736 static const struct nla_policy
737 enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
738 [TCA_FLOWER_KEY_ENC_OPTS_UNSPEC] = {
739 .strict_start_type = TCA_FLOWER_KEY_ENC_OPTS_VXLAN },
740 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
741 [TCA_FLOWER_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED },
742 [TCA_FLOWER_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED },
743 [TCA_FLOWER_KEY_ENC_OPTS_GTP] = { .type = NLA_NESTED },
746 static const struct nla_policy
747 geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
748 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
749 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
750 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
754 static const struct nla_policy
755 vxlan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1] = {
756 [TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 },
759 static const struct nla_policy
760 erspan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1] = {
761 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 },
762 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
763 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
764 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
767 static const struct nla_policy
768 gtp_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1] = {
769 [TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] = { .type = NLA_U8 },
770 [TCA_FLOWER_KEY_ENC_OPT_GTP_QFI] = { .type = NLA_U8 },
773 static const struct nla_policy
774 mpls_stack_entry_policy[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1] = {
775 [TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH] = { .type = NLA_U8 },
776 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL] = { .type = NLA_U8 },
777 [TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS] = { .type = NLA_U8 },
778 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TC] = { .type = NLA_U8 },
779 [TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL] = { .type = NLA_U32 },
782 static const struct nla_policy
783 cfm_opt_policy[TCA_FLOWER_KEY_CFM_OPT_MAX + 1] = {
784 [TCA_FLOWER_KEY_CFM_MD_LEVEL] = NLA_POLICY_MAX(NLA_U8,
785 FLOW_DIS_CFM_MDL_MAX),
786 [TCA_FLOWER_KEY_CFM_OPCODE] = { .type = NLA_U8 },
789 static void fl_set_key_val(struct nlattr **tb,
790 void *val, int val_type,
791 void *mask, int mask_type, int len)
795 nla_memcpy(val, tb[val_type], len);
796 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
797 memset(mask, 0xff, len);
799 nla_memcpy(mask, tb[mask_type], len);
802 static int fl_set_key_spi(struct nlattr **tb, struct fl_flow_key *key,
803 struct fl_flow_key *mask,
804 struct netlink_ext_ack *extack)
806 if (key->basic.ip_proto != IPPROTO_ESP &&
807 key->basic.ip_proto != IPPROTO_AH) {
808 NL_SET_ERR_MSG(extack,
809 "Protocol must be either ESP or AH");
813 fl_set_key_val(tb, &key->ipsec.spi,
815 &mask->ipsec.spi, TCA_FLOWER_KEY_SPI_MASK,
816 sizeof(key->ipsec.spi));
820 static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
821 struct fl_flow_key *mask,
822 struct netlink_ext_ack *extack)
824 fl_set_key_val(tb, &key->tp_range.tp_min.dst,
825 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst,
826 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.dst));
827 fl_set_key_val(tb, &key->tp_range.tp_max.dst,
828 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_range.tp_max.dst,
829 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.dst));
830 fl_set_key_val(tb, &key->tp_range.tp_min.src,
831 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_range.tp_min.src,
832 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.src));
833 fl_set_key_val(tb, &key->tp_range.tp_max.src,
834 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src,
835 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src));
837 if (mask->tp_range.tp_min.dst != mask->tp_range.tp_max.dst) {
838 NL_SET_ERR_MSG(extack,
839 "Both min and max destination ports must be specified");
842 if (mask->tp_range.tp_min.src != mask->tp_range.tp_max.src) {
843 NL_SET_ERR_MSG(extack,
844 "Both min and max source ports must be specified");
847 if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
848 ntohs(key->tp_range.tp_max.dst) <=
849 ntohs(key->tp_range.tp_min.dst)) {
850 NL_SET_ERR_MSG_ATTR(extack,
851 tb[TCA_FLOWER_KEY_PORT_DST_MIN],
852 "Invalid destination port range (min must be strictly smaller than max)");
855 if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
856 ntohs(key->tp_range.tp_max.src) <=
857 ntohs(key->tp_range.tp_min.src)) {
858 NL_SET_ERR_MSG_ATTR(extack,
859 tb[TCA_FLOWER_KEY_PORT_SRC_MIN],
860 "Invalid source port range (min must be strictly smaller than max)");
867 static int fl_set_key_mpls_lse(const struct nlattr *nla_lse,
868 struct flow_dissector_key_mpls *key_val,
869 struct flow_dissector_key_mpls *key_mask,
870 struct netlink_ext_ack *extack)
872 struct nlattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1];
873 struct flow_dissector_mpls_lse *lse_mask;
874 struct flow_dissector_mpls_lse *lse_val;
879 err = nla_parse_nested(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, nla_lse,
880 mpls_stack_entry_policy, extack);
884 if (!tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]) {
885 NL_SET_ERR_MSG(extack, "Missing MPLS option \"depth\"");
889 depth = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]);
891 /* LSE depth starts at 1, for consistency with terminology used by
892 * RFC 3031 (section 3.9), where depth 0 refers to unlabeled packets.
894 if (depth < 1 || depth > FLOW_DIS_MPLS_MAX) {
895 NL_SET_ERR_MSG_ATTR(extack,
896 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH],
897 "Invalid MPLS depth");
900 lse_index = depth - 1;
902 dissector_set_mpls_lse(key_val, lse_index);
903 dissector_set_mpls_lse(key_mask, lse_index);
905 lse_val = &key_val->ls[lse_index];
906 lse_mask = &key_mask->ls[lse_index];
908 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]) {
909 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]);
910 lse_mask->mpls_ttl = MPLS_TTL_MASK;
912 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]) {
913 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]);
915 if (bos & ~MPLS_BOS_MASK) {
916 NL_SET_ERR_MSG_ATTR(extack,
917 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS],
918 "Bottom Of Stack (BOS) must be 0 or 1");
921 lse_val->mpls_bos = bos;
922 lse_mask->mpls_bos = MPLS_BOS_MASK;
924 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]) {
925 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]);
927 if (tc & ~MPLS_TC_MASK) {
928 NL_SET_ERR_MSG_ATTR(extack,
929 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC],
930 "Traffic Class (TC) must be between 0 and 7");
933 lse_val->mpls_tc = tc;
934 lse_mask->mpls_tc = MPLS_TC_MASK;
936 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]) {
937 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]);
939 if (label & ~MPLS_LABEL_MASK) {
940 NL_SET_ERR_MSG_ATTR(extack,
941 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL],
942 "Label must be between 0 and 1048575");
945 lse_val->mpls_label = label;
946 lse_mask->mpls_label = MPLS_LABEL_MASK;
952 static int fl_set_key_mpls_opts(const struct nlattr *nla_mpls_opts,
953 struct flow_dissector_key_mpls *key_val,
954 struct flow_dissector_key_mpls *key_mask,
955 struct netlink_ext_ack *extack)
957 struct nlattr *nla_lse;
961 if (!(nla_mpls_opts->nla_type & NLA_F_NESTED)) {
962 NL_SET_ERR_MSG_ATTR(extack, nla_mpls_opts,
963 "NLA_F_NESTED is missing");
967 nla_for_each_nested(nla_lse, nla_mpls_opts, rem) {
968 if (nla_type(nla_lse) != TCA_FLOWER_KEY_MPLS_OPTS_LSE) {
969 NL_SET_ERR_MSG_ATTR(extack, nla_lse,
970 "Invalid MPLS option type");
974 err = fl_set_key_mpls_lse(nla_lse, key_val, key_mask, extack);
979 NL_SET_ERR_MSG(extack,
980 "Bytes leftover after parsing MPLS options");
987 static int fl_set_key_mpls(struct nlattr **tb,
988 struct flow_dissector_key_mpls *key_val,
989 struct flow_dissector_key_mpls *key_mask,
990 struct netlink_ext_ack *extack)
992 struct flow_dissector_mpls_lse *lse_mask;
993 struct flow_dissector_mpls_lse *lse_val;
995 if (tb[TCA_FLOWER_KEY_MPLS_OPTS]) {
996 if (tb[TCA_FLOWER_KEY_MPLS_TTL] ||
997 tb[TCA_FLOWER_KEY_MPLS_BOS] ||
998 tb[TCA_FLOWER_KEY_MPLS_TC] ||
999 tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
1000 NL_SET_ERR_MSG_ATTR(extack,
1001 tb[TCA_FLOWER_KEY_MPLS_OPTS],
1002 "MPLS label, Traffic Class, Bottom Of Stack and Time To Live must be encapsulated in the MPLS options attribute");
1006 return fl_set_key_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS],
1007 key_val, key_mask, extack);
1010 lse_val = &key_val->ls[0];
1011 lse_mask = &key_mask->ls[0];
1013 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
1014 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
1015 lse_mask->mpls_ttl = MPLS_TTL_MASK;
1016 dissector_set_mpls_lse(key_val, 0);
1017 dissector_set_mpls_lse(key_mask, 0);
1019 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
1020 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
1022 if (bos & ~MPLS_BOS_MASK) {
1023 NL_SET_ERR_MSG_ATTR(extack,
1024 tb[TCA_FLOWER_KEY_MPLS_BOS],
1025 "Bottom Of Stack (BOS) must be 0 or 1");
1028 lse_val->mpls_bos = bos;
1029 lse_mask->mpls_bos = MPLS_BOS_MASK;
1030 dissector_set_mpls_lse(key_val, 0);
1031 dissector_set_mpls_lse(key_mask, 0);
1033 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
1034 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
1036 if (tc & ~MPLS_TC_MASK) {
1037 NL_SET_ERR_MSG_ATTR(extack,
1038 tb[TCA_FLOWER_KEY_MPLS_TC],
1039 "Traffic Class (TC) must be between 0 and 7");
1042 lse_val->mpls_tc = tc;
1043 lse_mask->mpls_tc = MPLS_TC_MASK;
1044 dissector_set_mpls_lse(key_val, 0);
1045 dissector_set_mpls_lse(key_mask, 0);
1047 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
1048 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
1050 if (label & ~MPLS_LABEL_MASK) {
1051 NL_SET_ERR_MSG_ATTR(extack,
1052 tb[TCA_FLOWER_KEY_MPLS_LABEL],
1053 "Label must be between 0 and 1048575");
1056 lse_val->mpls_label = label;
1057 lse_mask->mpls_label = MPLS_LABEL_MASK;
1058 dissector_set_mpls_lse(key_val, 0);
1059 dissector_set_mpls_lse(key_mask, 0);
1064 static void fl_set_key_vlan(struct nlattr **tb,
1066 int vlan_id_key, int vlan_prio_key,
1067 int vlan_next_eth_type_key,
1068 struct flow_dissector_key_vlan *key_val,
1069 struct flow_dissector_key_vlan *key_mask)
1071 #define VLAN_PRIORITY_MASK 0x7
1073 if (tb[vlan_id_key]) {
1075 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
1076 key_mask->vlan_id = VLAN_VID_MASK;
1078 if (tb[vlan_prio_key]) {
1079 key_val->vlan_priority =
1080 nla_get_u8(tb[vlan_prio_key]) &
1082 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
1085 key_val->vlan_tpid = ethertype;
1086 key_mask->vlan_tpid = cpu_to_be16(~0);
1088 if (tb[vlan_next_eth_type_key]) {
1089 key_val->vlan_eth_type =
1090 nla_get_be16(tb[vlan_next_eth_type_key]);
1091 key_mask->vlan_eth_type = cpu_to_be16(~0);
1095 static void fl_set_key_pppoe(struct nlattr **tb,
1096 struct flow_dissector_key_pppoe *key_val,
1097 struct flow_dissector_key_pppoe *key_mask,
1098 struct fl_flow_key *key,
1099 struct fl_flow_key *mask)
1101 /* key_val::type must be set to ETH_P_PPP_SES
1102 * because ETH_P_PPP_SES was stored in basic.n_proto
1103 * which might get overwritten by ppp_proto
1104 * or might be set to 0, the role of key_val::type
1105 * is similar to vlan_key::tpid
1107 key_val->type = htons(ETH_P_PPP_SES);
1108 key_mask->type = cpu_to_be16(~0);
1110 if (tb[TCA_FLOWER_KEY_PPPOE_SID]) {
1111 key_val->session_id =
1112 nla_get_be16(tb[TCA_FLOWER_KEY_PPPOE_SID]);
1113 key_mask->session_id = cpu_to_be16(~0);
1115 if (tb[TCA_FLOWER_KEY_PPP_PROTO]) {
1116 key_val->ppp_proto =
1117 nla_get_be16(tb[TCA_FLOWER_KEY_PPP_PROTO]);
1118 key_mask->ppp_proto = cpu_to_be16(~0);
1120 if (key_val->ppp_proto == htons(PPP_IP)) {
1121 key->basic.n_proto = htons(ETH_P_IP);
1122 mask->basic.n_proto = cpu_to_be16(~0);
1123 } else if (key_val->ppp_proto == htons(PPP_IPV6)) {
1124 key->basic.n_proto = htons(ETH_P_IPV6);
1125 mask->basic.n_proto = cpu_to_be16(~0);
1126 } else if (key_val->ppp_proto == htons(PPP_MPLS_UC)) {
1127 key->basic.n_proto = htons(ETH_P_MPLS_UC);
1128 mask->basic.n_proto = cpu_to_be16(~0);
1129 } else if (key_val->ppp_proto == htons(PPP_MPLS_MC)) {
1130 key->basic.n_proto = htons(ETH_P_MPLS_MC);
1131 mask->basic.n_proto = cpu_to_be16(~0);
1134 key->basic.n_proto = 0;
1135 mask->basic.n_proto = cpu_to_be16(0);
1139 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
1140 u32 *dissector_key, u32 *dissector_mask,
1141 u32 flower_flag_bit, u32 dissector_flag_bit)
1143 if (flower_mask & flower_flag_bit) {
1144 *dissector_mask |= dissector_flag_bit;
1145 if (flower_key & flower_flag_bit)
1146 *dissector_key |= dissector_flag_bit;
1150 static int fl_set_key_flags(struct nlattr **tb, u32 *flags_key,
1151 u32 *flags_mask, struct netlink_ext_ack *extack)
1155 /* mask is mandatory for flags */
1156 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) {
1157 NL_SET_ERR_MSG(extack, "Missing flags mask");
1161 key = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS]));
1162 mask = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
1167 fl_set_key_flag(key, mask, flags_key, flags_mask,
1168 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1169 fl_set_key_flag(key, mask, flags_key, flags_mask,
1170 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1171 FLOW_DIS_FIRST_FRAG);
1176 static void fl_set_key_ip(struct nlattr **tb, bool encap,
1177 struct flow_dissector_key_ip *key,
1178 struct flow_dissector_key_ip *mask)
1180 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1181 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1182 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1183 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1185 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
1186 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
1189 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
1190 int depth, int option_len,
1191 struct netlink_ext_ack *extack)
1193 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
1194 struct nlattr *class = NULL, *type = NULL, *data = NULL;
1195 struct geneve_opt *opt;
1196 int err, data_len = 0;
1198 if (option_len > sizeof(struct geneve_opt))
1199 data_len = option_len - sizeof(struct geneve_opt);
1201 if (key->enc_opts.len > FLOW_DIS_TUN_OPTS_MAX - 4)
1204 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
1205 memset(opt, 0xff, option_len);
1206 opt->length = data_len / 4;
1211 /* If no mask has been prodived we assume an exact match. */
1213 return sizeof(struct geneve_opt) + data_len;
1215 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
1216 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
1220 err = nla_parse_nested_deprecated(tb,
1221 TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
1222 nla, geneve_opt_policy, extack);
1226 /* We are not allowed to omit any of CLASS, TYPE or DATA
1227 * fields from the key.
1230 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
1231 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
1232 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
1233 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
1237 /* Omitting any of CLASS, TYPE or DATA fields is allowed
1240 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
1241 int new_len = key->enc_opts.len;
1243 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
1244 data_len = nla_len(data);
1246 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
1250 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
1254 new_len += sizeof(struct geneve_opt) + data_len;
1255 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
1256 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
1257 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
1260 opt->length = data_len / 4;
1261 memcpy(opt->opt_data, nla_data(data), data_len);
1264 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
1265 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
1266 opt->opt_class = nla_get_be16(class);
1269 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
1270 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
1271 opt->type = nla_get_u8(type);
1274 return sizeof(struct geneve_opt) + data_len;
1277 static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1278 int depth, int option_len,
1279 struct netlink_ext_ack *extack)
1281 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1];
1282 struct vxlan_metadata *md;
1285 md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1286 memset(md, 0xff, sizeof(*md));
1291 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) {
1292 NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask");
1296 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla,
1297 vxlan_opt_policy, extack);
1301 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
1302 NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
1306 if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
1307 md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]);
1308 md->gbp &= VXLAN_GBP_MASK;
1314 static int fl_set_erspan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1315 int depth, int option_len,
1316 struct netlink_ext_ack *extack)
1318 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1];
1319 struct erspan_metadata *md;
1322 md = (struct erspan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1323 memset(md, 0xff, sizeof(*md));
1329 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_ERSPAN) {
1330 NL_SET_ERR_MSG(extack, "Non-erspan option type for mask");
1334 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, nla,
1335 erspan_opt_policy, extack);
1339 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) {
1340 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver");
1344 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER])
1345 md->version = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]);
1347 if (md->version == 1) {
1348 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1349 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index");
1352 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1353 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX];
1354 memset(&md->u, 0x00, sizeof(md->u));
1355 md->u.index = nla_get_be32(nla);
1357 } else if (md->version == 2) {
1358 if (!option_len && (!tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] ||
1359 !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID])) {
1360 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid");
1363 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]) {
1364 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR];
1365 md->u.md2.dir = nla_get_u8(nla);
1367 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]) {
1368 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID];
1369 set_hwid(&md->u.md2, nla_get_u8(nla));
1372 NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect");
1379 static int fl_set_gtp_opt(const struct nlattr *nla, struct fl_flow_key *key,
1380 int depth, int option_len,
1381 struct netlink_ext_ack *extack)
1383 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1];
1384 struct gtp_pdu_session_info *sinfo;
1385 u8 len = key->enc_opts.len;
1388 sinfo = (struct gtp_pdu_session_info *)&key->enc_opts.data[len];
1389 memset(sinfo, 0xff, option_len);
1392 return sizeof(*sinfo);
1394 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GTP) {
1395 NL_SET_ERR_MSG_MOD(extack, "Non-gtp option type for mask");
1399 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GTP_MAX, nla,
1400 gtp_opt_policy, extack);
1405 (!tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] ||
1406 !tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])) {
1407 NL_SET_ERR_MSG_MOD(extack,
1408 "Missing tunnel key gtp option pdu type or qfi");
1412 if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE])
1414 nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE]);
1416 if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])
1417 sinfo->qfi = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI]);
1419 return sizeof(*sinfo);
1422 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
1423 struct fl_flow_key *mask,
1424 struct netlink_ext_ack *extack)
1426 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
1427 int err, option_len, key_depth, msk_depth = 0;
1429 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
1430 TCA_FLOWER_KEY_ENC_OPTS_MAX,
1431 enc_opts_policy, extack);
1435 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
1437 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
1438 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
1439 TCA_FLOWER_KEY_ENC_OPTS_MAX,
1440 enc_opts_policy, extack);
1444 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
1445 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
1446 if (!nla_ok(nla_opt_msk, msk_depth)) {
1447 NL_SET_ERR_MSG(extack, "Invalid nested attribute for masks");
1452 nla_for_each_attr(nla_opt_key, nla_enc_key,
1453 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
1454 switch (nla_type(nla_opt_key)) {
1455 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
1456 if (key->enc_opts.dst_opt_type &&
1457 key->enc_opts.dst_opt_type != TUNNEL_GENEVE_OPT) {
1458 NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
1462 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
1463 option_len = fl_set_geneve_opt(nla_opt_key, key,
1464 key_depth, option_len,
1469 key->enc_opts.len += option_len;
1470 /* At the same time we need to parse through the mask
1471 * in order to verify exact and mask attribute lengths.
1473 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
1474 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
1475 msk_depth, option_len,
1480 mask->enc_opts.len += option_len;
1481 if (key->enc_opts.len != mask->enc_opts.len) {
1482 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1486 case TCA_FLOWER_KEY_ENC_OPTS_VXLAN:
1487 if (key->enc_opts.dst_opt_type) {
1488 NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
1492 key->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
1493 option_len = fl_set_vxlan_opt(nla_opt_key, key,
1494 key_depth, option_len,
1499 key->enc_opts.len += option_len;
1500 /* At the same time we need to parse through the mask
1501 * in order to verify exact and mask attribute lengths.
1503 mask->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
1504 option_len = fl_set_vxlan_opt(nla_opt_msk, mask,
1505 msk_depth, option_len,
1510 mask->enc_opts.len += option_len;
1511 if (key->enc_opts.len != mask->enc_opts.len) {
1512 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1516 case TCA_FLOWER_KEY_ENC_OPTS_ERSPAN:
1517 if (key->enc_opts.dst_opt_type) {
1518 NL_SET_ERR_MSG(extack, "Duplicate type for erspan options");
1522 key->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT;
1523 option_len = fl_set_erspan_opt(nla_opt_key, key,
1524 key_depth, option_len,
1529 key->enc_opts.len += option_len;
1530 /* At the same time we need to parse through the mask
1531 * in order to verify exact and mask attribute lengths.
1533 mask->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT;
1534 option_len = fl_set_erspan_opt(nla_opt_msk, mask,
1535 msk_depth, option_len,
1540 mask->enc_opts.len += option_len;
1541 if (key->enc_opts.len != mask->enc_opts.len) {
1542 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1546 case TCA_FLOWER_KEY_ENC_OPTS_GTP:
1547 if (key->enc_opts.dst_opt_type) {
1548 NL_SET_ERR_MSG_MOD(extack,
1549 "Duplicate type for gtp options");
1553 key->enc_opts.dst_opt_type = TUNNEL_GTP_OPT;
1554 option_len = fl_set_gtp_opt(nla_opt_key, key,
1555 key_depth, option_len,
1560 key->enc_opts.len += option_len;
1561 /* At the same time we need to parse through the mask
1562 * in order to verify exact and mask attribute lengths.
1564 mask->enc_opts.dst_opt_type = TUNNEL_GTP_OPT;
1565 option_len = fl_set_gtp_opt(nla_opt_msk, mask,
1566 msk_depth, option_len,
1571 mask->enc_opts.len += option_len;
1572 if (key->enc_opts.len != mask->enc_opts.len) {
1573 NL_SET_ERR_MSG_MOD(extack,
1574 "Key and mask miss aligned");
1579 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
1586 if (!nla_ok(nla_opt_msk, msk_depth)) {
1587 NL_SET_ERR_MSG(extack, "A mask attribute is invalid");
1590 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
1596 static int fl_validate_ct_state(u16 state, struct nlattr *tb,
1597 struct netlink_ext_ack *extack)
1599 if (state && !(state & TCA_FLOWER_KEY_CT_FLAGS_TRACKED)) {
1600 NL_SET_ERR_MSG_ATTR(extack, tb,
1601 "no trk, so no other flag can be set");
1605 if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
1606 state & TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED) {
1607 NL_SET_ERR_MSG_ATTR(extack, tb,
1608 "new and est are mutually exclusive");
1612 if (state & TCA_FLOWER_KEY_CT_FLAGS_INVALID &&
1613 state & ~(TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
1614 TCA_FLOWER_KEY_CT_FLAGS_INVALID)) {
1615 NL_SET_ERR_MSG_ATTR(extack, tb,
1616 "when inv is set, only trk may be set");
1620 if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
1621 state & TCA_FLOWER_KEY_CT_FLAGS_REPLY) {
1622 NL_SET_ERR_MSG_ATTR(extack, tb,
1623 "new and rpl are mutually exclusive");
1630 static int fl_set_key_ct(struct nlattr **tb,
1631 struct flow_dissector_key_ct *key,
1632 struct flow_dissector_key_ct *mask,
1633 struct netlink_ext_ack *extack)
1635 if (tb[TCA_FLOWER_KEY_CT_STATE]) {
1638 if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) {
1639 NL_SET_ERR_MSG(extack, "Conntrack isn't enabled");
1642 fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
1643 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
1644 sizeof(key->ct_state));
1646 err = fl_validate_ct_state(key->ct_state & mask->ct_state,
1647 tb[TCA_FLOWER_KEY_CT_STATE_MASK],
1653 if (tb[TCA_FLOWER_KEY_CT_ZONE]) {
1654 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1655 NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled");
1658 fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
1659 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
1660 sizeof(key->ct_zone));
1662 if (tb[TCA_FLOWER_KEY_CT_MARK]) {
1663 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1664 NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled");
1667 fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
1668 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
1669 sizeof(key->ct_mark));
1671 if (tb[TCA_FLOWER_KEY_CT_LABELS]) {
1672 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1673 NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled");
1676 fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
1677 mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
1678 sizeof(key->ct_labels));
1684 static bool is_vlan_key(struct nlattr *tb, __be16 *ethertype,
1685 struct fl_flow_key *key, struct fl_flow_key *mask,
1688 const bool good_num_of_vlans = key->num_of_vlans.num_of_vlans > vthresh;
1692 return good_num_of_vlans;
1695 *ethertype = nla_get_be16(tb);
1696 if (good_num_of_vlans || eth_type_vlan(*ethertype))
1699 key->basic.n_proto = *ethertype;
1700 mask->basic.n_proto = cpu_to_be16(~0);
1704 static void fl_set_key_cfm_md_level(struct nlattr **tb,
1705 struct fl_flow_key *key,
1706 struct fl_flow_key *mask,
1707 struct netlink_ext_ack *extack)
1711 if (!tb[TCA_FLOWER_KEY_CFM_MD_LEVEL])
1714 level = nla_get_u8(tb[TCA_FLOWER_KEY_CFM_MD_LEVEL]);
1715 key->cfm.mdl_ver = FIELD_PREP(FLOW_DIS_CFM_MDL_MASK, level);
1716 mask->cfm.mdl_ver = FLOW_DIS_CFM_MDL_MASK;
1719 static void fl_set_key_cfm_opcode(struct nlattr **tb,
1720 struct fl_flow_key *key,
1721 struct fl_flow_key *mask,
1722 struct netlink_ext_ack *extack)
1724 fl_set_key_val(tb, &key->cfm.opcode, TCA_FLOWER_KEY_CFM_OPCODE,
1725 &mask->cfm.opcode, TCA_FLOWER_UNSPEC,
1726 sizeof(key->cfm.opcode));
1729 static int fl_set_key_cfm(struct nlattr **tb,
1730 struct fl_flow_key *key,
1731 struct fl_flow_key *mask,
1732 struct netlink_ext_ack *extack)
1734 struct nlattr *nla_cfm_opt[TCA_FLOWER_KEY_CFM_OPT_MAX + 1];
1737 if (!tb[TCA_FLOWER_KEY_CFM])
1740 err = nla_parse_nested(nla_cfm_opt, TCA_FLOWER_KEY_CFM_OPT_MAX,
1741 tb[TCA_FLOWER_KEY_CFM], cfm_opt_policy, extack);
1745 fl_set_key_cfm_opcode(nla_cfm_opt, key, mask, extack);
1746 fl_set_key_cfm_md_level(nla_cfm_opt, key, mask, extack);
1751 static int fl_set_key(struct net *net, struct nlattr **tb,
1752 struct fl_flow_key *key, struct fl_flow_key *mask,
1753 struct netlink_ext_ack *extack)
1758 if (tb[TCA_FLOWER_INDEV]) {
1759 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
1762 key->meta.ingress_ifindex = err;
1763 mask->meta.ingress_ifindex = 0xffffffff;
1766 fl_set_key_val(tb, &key->meta.l2_miss, TCA_FLOWER_L2_MISS,
1767 &mask->meta.l2_miss, TCA_FLOWER_UNSPEC,
1768 sizeof(key->meta.l2_miss));
1770 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1771 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1772 sizeof(key->eth.dst));
1773 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1774 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1775 sizeof(key->eth.src));
1776 fl_set_key_val(tb, &key->num_of_vlans,
1777 TCA_FLOWER_KEY_NUM_OF_VLANS,
1778 &mask->num_of_vlans,
1780 sizeof(key->num_of_vlans));
1782 if (is_vlan_key(tb[TCA_FLOWER_KEY_ETH_TYPE], ðertype, key, mask, 0)) {
1783 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
1784 TCA_FLOWER_KEY_VLAN_PRIO,
1785 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1786 &key->vlan, &mask->vlan);
1788 if (is_vlan_key(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE],
1789 ðertype, key, mask, 1)) {
1790 fl_set_key_vlan(tb, ethertype,
1791 TCA_FLOWER_KEY_CVLAN_ID,
1792 TCA_FLOWER_KEY_CVLAN_PRIO,
1793 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1794 &key->cvlan, &mask->cvlan);
1795 fl_set_key_val(tb, &key->basic.n_proto,
1796 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1797 &mask->basic.n_proto,
1799 sizeof(key->basic.n_proto));
1803 if (key->basic.n_proto == htons(ETH_P_PPP_SES))
1804 fl_set_key_pppoe(tb, &key->pppoe, &mask->pppoe, key, mask);
1806 if (key->basic.n_proto == htons(ETH_P_IP) ||
1807 key->basic.n_proto == htons(ETH_P_IPV6)) {
1808 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1809 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1810 sizeof(key->basic.ip_proto));
1811 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
1814 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1815 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1816 mask->control.addr_type = ~0;
1817 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1818 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1819 sizeof(key->ipv4.src));
1820 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1821 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1822 sizeof(key->ipv4.dst));
1823 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1824 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1825 mask->control.addr_type = ~0;
1826 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1827 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1828 sizeof(key->ipv6.src));
1829 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1830 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1831 sizeof(key->ipv6.dst));
1834 if (key->basic.ip_proto == IPPROTO_TCP) {
1835 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1836 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1837 sizeof(key->tp.src));
1838 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1839 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1840 sizeof(key->tp.dst));
1841 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1842 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1843 sizeof(key->tcp.flags));
1844 } else if (key->basic.ip_proto == IPPROTO_UDP) {
1845 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1846 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1847 sizeof(key->tp.src));
1848 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1849 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1850 sizeof(key->tp.dst));
1851 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
1852 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1853 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1854 sizeof(key->tp.src));
1855 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1856 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1857 sizeof(key->tp.dst));
1858 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
1859 key->basic.ip_proto == IPPROTO_ICMP) {
1860 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1862 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1863 sizeof(key->icmp.type));
1864 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1866 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1867 sizeof(key->icmp.code));
1868 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1869 key->basic.ip_proto == IPPROTO_ICMPV6) {
1870 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1872 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1873 sizeof(key->icmp.type));
1874 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
1876 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1877 sizeof(key->icmp.code));
1878 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1879 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
1880 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack);
1883 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
1884 key->basic.n_proto == htons(ETH_P_RARP)) {
1885 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
1886 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
1887 sizeof(key->arp.sip));
1888 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
1889 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
1890 sizeof(key->arp.tip));
1891 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
1892 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
1893 sizeof(key->arp.op));
1894 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1895 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1896 sizeof(key->arp.sha));
1897 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1898 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1899 sizeof(key->arp.tha));
1900 } else if (key->basic.ip_proto == IPPROTO_L2TP) {
1901 fl_set_key_val(tb, &key->l2tpv3.session_id,
1902 TCA_FLOWER_KEY_L2TPV3_SID,
1903 &mask->l2tpv3.session_id, TCA_FLOWER_UNSPEC,
1904 sizeof(key->l2tpv3.session_id));
1905 } else if (key->basic.n_proto == htons(ETH_P_CFM)) {
1906 ret = fl_set_key_cfm(tb, key, mask, extack);
1911 if (key->basic.ip_proto == IPPROTO_TCP ||
1912 key->basic.ip_proto == IPPROTO_UDP ||
1913 key->basic.ip_proto == IPPROTO_SCTP) {
1914 ret = fl_set_key_port_range(tb, key, mask, extack);
1919 if (tb[TCA_FLOWER_KEY_SPI]) {
1920 ret = fl_set_key_spi(tb, key, mask, extack);
1925 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
1926 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
1927 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1928 mask->enc_control.addr_type = ~0;
1929 fl_set_key_val(tb, &key->enc_ipv4.src,
1930 TCA_FLOWER_KEY_ENC_IPV4_SRC,
1931 &mask->enc_ipv4.src,
1932 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1933 sizeof(key->enc_ipv4.src));
1934 fl_set_key_val(tb, &key->enc_ipv4.dst,
1935 TCA_FLOWER_KEY_ENC_IPV4_DST,
1936 &mask->enc_ipv4.dst,
1937 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1938 sizeof(key->enc_ipv4.dst));
1941 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
1942 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
1943 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1944 mask->enc_control.addr_type = ~0;
1945 fl_set_key_val(tb, &key->enc_ipv6.src,
1946 TCA_FLOWER_KEY_ENC_IPV6_SRC,
1947 &mask->enc_ipv6.src,
1948 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1949 sizeof(key->enc_ipv6.src));
1950 fl_set_key_val(tb, &key->enc_ipv6.dst,
1951 TCA_FLOWER_KEY_ENC_IPV6_DST,
1952 &mask->enc_ipv6.dst,
1953 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1954 sizeof(key->enc_ipv6.dst));
1957 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
1958 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
1959 sizeof(key->enc_key_id.keyid));
1961 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1962 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1963 sizeof(key->enc_tp.src));
1965 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1966 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1967 sizeof(key->enc_tp.dst));
1969 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
1971 fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
1972 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
1973 sizeof(key->hash.hash));
1975 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
1976 ret = fl_set_enc_opt(tb, key, mask, extack);
1981 ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack);
1985 if (tb[TCA_FLOWER_KEY_FLAGS])
1986 ret = fl_set_key_flags(tb, &key->control.flags,
1987 &mask->control.flags, extack);
1992 static void fl_mask_copy(struct fl_flow_mask *dst,
1993 struct fl_flow_mask *src)
1995 const void *psrc = fl_key_get_start(&src->key, src);
1996 void *pdst = fl_key_get_start(&dst->key, src);
1998 memcpy(pdst, psrc, fl_mask_range(src));
1999 dst->range = src->range;
2002 static const struct rhashtable_params fl_ht_params = {
2003 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
2004 .head_offset = offsetof(struct cls_fl_filter, ht_node),
2005 .automatic_shrinking = true,
2008 static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
2010 mask->filter_ht_params = fl_ht_params;
2011 mask->filter_ht_params.key_len = fl_mask_range(mask);
2012 mask->filter_ht_params.key_offset += mask->range.start;
2014 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
2017 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
2018 #define FL_KEY_MEMBER_SIZE(member) sizeof_field(struct fl_flow_key, member)
2020 #define FL_KEY_IS_MASKED(mask, member) \
2021 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
2022 0, FL_KEY_MEMBER_SIZE(member)) \
2024 #define FL_KEY_SET(keys, cnt, id, member) \
2026 keys[cnt].key_id = id; \
2027 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
2031 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
2033 if (FL_KEY_IS_MASKED(mask, member)) \
2034 FL_KEY_SET(keys, cnt, id, member); \
2037 static void fl_init_dissector(struct flow_dissector *dissector,
2038 struct fl_flow_key *mask)
2040 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
2043 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2044 FLOW_DISSECTOR_KEY_META, meta);
2045 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
2046 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
2047 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2048 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
2049 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2050 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
2051 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2052 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
2053 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2054 FLOW_DISSECTOR_KEY_PORTS, tp);
2055 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2056 FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range);
2057 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2058 FLOW_DISSECTOR_KEY_IP, ip);
2059 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2060 FLOW_DISSECTOR_KEY_TCP, tcp);
2061 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2062 FLOW_DISSECTOR_KEY_ICMP, icmp);
2063 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2064 FLOW_DISSECTOR_KEY_ARP, arp);
2065 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2066 FLOW_DISSECTOR_KEY_MPLS, mpls);
2067 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2068 FLOW_DISSECTOR_KEY_VLAN, vlan);
2069 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2070 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
2071 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2072 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
2073 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2074 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
2075 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2076 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
2077 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
2078 FL_KEY_IS_MASKED(mask, enc_ipv6))
2079 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
2081 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2082 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
2083 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2084 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
2085 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2086 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
2087 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2088 FLOW_DISSECTOR_KEY_CT, ct);
2089 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2090 FLOW_DISSECTOR_KEY_HASH, hash);
2091 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2092 FLOW_DISSECTOR_KEY_NUM_OF_VLANS, num_of_vlans);
2093 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2094 FLOW_DISSECTOR_KEY_PPPOE, pppoe);
2095 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2096 FLOW_DISSECTOR_KEY_L2TPV3, l2tpv3);
2097 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2098 FLOW_DISSECTOR_KEY_IPSEC, ipsec);
2099 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2100 FLOW_DISSECTOR_KEY_CFM, cfm);
2102 skb_flow_dissector_init(dissector, keys, cnt);
2105 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
2106 struct fl_flow_mask *mask)
2108 struct fl_flow_mask *newmask;
2111 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
2113 return ERR_PTR(-ENOMEM);
2115 fl_mask_copy(newmask, mask);
2117 if ((newmask->key.tp_range.tp_min.dst &&
2118 newmask->key.tp_range.tp_max.dst) ||
2119 (newmask->key.tp_range.tp_min.src &&
2120 newmask->key.tp_range.tp_max.src))
2121 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
2123 err = fl_init_mask_hashtable(newmask);
2127 fl_init_dissector(&newmask->dissector, &newmask->key);
2129 INIT_LIST_HEAD_RCU(&newmask->filters);
2131 refcount_set(&newmask->refcnt, 1);
2132 err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
2133 &newmask->ht_node, mask_ht_params);
2135 goto errout_destroy;
2137 spin_lock(&head->masks_lock);
2138 list_add_tail_rcu(&newmask->list, &head->masks);
2139 spin_unlock(&head->masks_lock);
2144 rhashtable_destroy(&newmask->ht);
2148 return ERR_PTR(err);
2151 static int fl_check_assign_mask(struct cls_fl_head *head,
2152 struct cls_fl_filter *fnew,
2153 struct cls_fl_filter *fold,
2154 struct fl_flow_mask *mask)
2156 struct fl_flow_mask *newmask;
2161 /* Insert mask as temporary node to prevent concurrent creation of mask
2162 * with same key. Any concurrent lookups with same key will return
2163 * -EAGAIN because mask's refcnt is zero.
2165 fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
2173 goto errout_cleanup;
2176 newmask = fl_create_new_mask(head, mask);
2177 if (IS_ERR(newmask)) {
2178 ret = PTR_ERR(newmask);
2179 goto errout_cleanup;
2182 fnew->mask = newmask;
2184 } else if (IS_ERR(fnew->mask)) {
2185 ret = PTR_ERR(fnew->mask);
2186 } else if (fold && fold->mask != fnew->mask) {
2188 } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
2189 /* Mask was deleted concurrently, try again */
2196 rhashtable_remove_fast(&head->ht, &mask->ht_node,
2201 static bool fl_needs_tc_skb_ext(const struct fl_flow_key *mask)
2203 return mask->meta.l2_miss;
2206 static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
2207 struct cls_fl_filter *fold,
2210 struct fl_flow_mask *mask = fnew->mask;
2213 err = rhashtable_lookup_insert_fast(&mask->ht,
2215 mask->filter_ht_params);
2218 /* It is okay if filter with same key exists when
2221 return fold && err == -EEXIST ? 0 : err;
2228 static int fl_change(struct net *net, struct sk_buff *in_skb,
2229 struct tcf_proto *tp, unsigned long base,
2230 u32 handle, struct nlattr **tca,
2231 void **arg, u32 flags,
2232 struct netlink_ext_ack *extack)
2234 struct cls_fl_head *head = fl_head_dereference(tp);
2235 bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL);
2236 struct cls_fl_filter *fold = *arg;
2237 bool bound_to_filter = false;
2238 struct cls_fl_filter *fnew;
2239 struct fl_flow_mask *mask;
2244 if (!tca[TCA_OPTIONS]) {
2249 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
2255 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2258 goto errout_mask_alloc;
2261 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
2262 tca[TCA_OPTIONS], fl_policy, NULL);
2266 if (fold && handle && fold->handle != handle) {
2271 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
2276 INIT_LIST_HEAD(&fnew->hw_list);
2277 refcount_set(&fnew->refcnt, 1);
2279 if (tb[TCA_FLOWER_FLAGS]) {
2280 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
2282 if (!tc_flags_valid(fnew->flags)) {
2290 spin_lock(&tp->lock);
2293 err = idr_alloc_u32(&head->handle_idr, NULL, &handle,
2294 INT_MAX, GFP_ATOMIC);
2296 err = idr_alloc_u32(&head->handle_idr, NULL, &handle,
2297 handle, GFP_ATOMIC);
2299 /* Filter with specified handle was concurrently
2300 * inserted after initial check in cls_api. This is not
2301 * necessarily an error if NLM_F_EXCL is not set in
2302 * message flags. Returning EAGAIN will cause cls_api to
2303 * try to update concurrently inserted rule.
2308 spin_unlock(&tp->lock);
2315 fnew->handle = handle;
2317 err = tcf_exts_init_ex(&fnew->exts, net, TCA_FLOWER_ACT, 0, tp, handle,
2318 !tc_skip_hw(fnew->flags));
2322 err = tcf_exts_validate_ex(net, tp, tb, tca[TCA_RATE],
2323 &fnew->exts, flags, fnew->flags,
2328 if (tb[TCA_FLOWER_CLASSID]) {
2329 fnew->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
2330 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2332 tcf_bind_filter(tp, &fnew->res, base);
2333 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2335 bound_to_filter = true;
2338 err = fl_set_key(net, tb, &fnew->key, &mask->key, extack);
2342 fl_mask_update_range(mask);
2343 fl_set_masked_key(&fnew->mkey, &fnew->key, mask);
2345 if (!fl_mask_fits_tmplt(tp->chain->tmplt_priv, mask)) {
2346 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
2351 /* Enable tc skb extension if filter matches on data extracted from
2354 if (fl_needs_tc_skb_ext(&mask->key)) {
2355 fnew->needs_tc_skb_ext = 1;
2356 tc_skb_ext_tc_enable();
2359 err = fl_check_assign_mask(head, fnew, fold, mask);
2363 err = fl_ht_insert_unique(fnew, fold, &in_ht);
2367 if (!tc_skip_hw(fnew->flags)) {
2368 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
2373 if (!tc_in_hw(fnew->flags))
2374 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
2376 spin_lock(&tp->lock);
2378 /* tp was deleted concurrently. -EAGAIN will cause caller to lookup
2379 * proto again or create new one, if necessary.
2387 /* Fold filter was deleted concurrently. Retry lookup. */
2388 if (fold->deleted) {
2393 fnew->handle = handle;
2396 struct rhashtable_params params =
2397 fnew->mask->filter_ht_params;
2399 err = rhashtable_insert_fast(&fnew->mask->ht,
2407 refcount_inc(&fnew->refcnt);
2408 rhashtable_remove_fast(&fold->mask->ht,
2410 fold->mask->filter_ht_params);
2411 idr_replace(&head->handle_idr, fnew, fnew->handle);
2412 list_replace_rcu(&fold->list, &fnew->list);
2413 fold->deleted = true;
2415 spin_unlock(&tp->lock);
2417 fl_mask_put(head, fold->mask);
2418 if (!tc_skip_hw(fold->flags))
2419 fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
2420 tcf_unbind_filter(tp, &fold->res);
2421 /* Caller holds reference to fold, so refcnt is always > 0
2424 refcount_dec(&fold->refcnt);
2427 idr_replace(&head->handle_idr, fnew, fnew->handle);
2429 refcount_inc(&fnew->refcnt);
2430 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
2431 spin_unlock(&tp->lock);
2437 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
2441 spin_lock(&tp->lock);
2443 fnew->deleted = true;
2444 spin_unlock(&tp->lock);
2445 if (!tc_skip_hw(fnew->flags))
2446 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
2448 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
2449 fnew->mask->filter_ht_params);
2451 fl_mask_put(head, fnew->mask);
2454 if (bound_to_filter) {
2455 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2457 tcf_unbind_filter(tp, &fnew->res);
2458 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2464 idr_remove(&head->handle_idr, fnew->handle);
2469 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
2476 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
2477 bool rtnl_held, struct netlink_ext_ack *extack)
2479 struct cls_fl_head *head = fl_head_dereference(tp);
2480 struct cls_fl_filter *f = arg;
2484 err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
2485 *last = list_empty(&head->masks);
2491 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
2494 struct cls_fl_head *head = fl_head_dereference(tp);
2495 unsigned long id = arg->cookie, tmp;
2496 struct cls_fl_filter *f;
2498 arg->count = arg->skip;
2501 idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) {
2502 /* don't return filters that are being deleted */
2503 if (!f || !refcount_inc_not_zero(&f->refcnt))
2507 if (arg->fn(tp, f, arg) < 0) {
2521 static struct cls_fl_filter *
2522 fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
2524 struct cls_fl_head *head = fl_head_dereference(tp);
2526 spin_lock(&tp->lock);
2527 if (list_empty(&head->hw_filters)) {
2528 spin_unlock(&tp->lock);
2533 f = list_entry(&head->hw_filters, struct cls_fl_filter,
2535 list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
2536 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
2537 spin_unlock(&tp->lock);
2542 spin_unlock(&tp->lock);
2546 static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
2547 void *cb_priv, struct netlink_ext_ack *extack)
2549 struct tcf_block *block = tp->chain->block;
2550 struct flow_cls_offload cls_flower = {};
2551 struct cls_fl_filter *f = NULL;
2554 /* hw_filters list can only be changed by hw offload functions after
2555 * obtaining rtnl lock. Make sure it is not changed while reoffload is
2560 while ((f = fl_get_next_hw_filter(tp, f, add))) {
2562 flow_rule_alloc(tcf_exts_num_actions(&f->exts));
2563 if (!cls_flower.rule) {
2568 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
2570 cls_flower.command = add ?
2571 FLOW_CLS_REPLACE : FLOW_CLS_DESTROY;
2572 cls_flower.cookie = (unsigned long)f;
2573 cls_flower.rule->match.dissector = &f->mask->dissector;
2574 cls_flower.rule->match.mask = &f->mask->key;
2575 cls_flower.rule->match.key = &f->mkey;
2577 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts,
2578 cls_flower.common.extack);
2580 kfree(cls_flower.rule);
2581 if (tc_skip_sw(f->flags)) {
2588 cls_flower.classid = f->res.classid;
2590 err = tc_setup_cb_reoffload(block, tp, add, cb,
2591 TC_SETUP_CLSFLOWER, &cls_flower,
2594 tc_cleanup_offload_action(&cls_flower.rule->action);
2595 kfree(cls_flower.rule);
2608 static void fl_hw_add(struct tcf_proto *tp, void *type_data)
2610 struct flow_cls_offload *cls_flower = type_data;
2611 struct cls_fl_filter *f =
2612 (struct cls_fl_filter *) cls_flower->cookie;
2613 struct cls_fl_head *head = fl_head_dereference(tp);
2615 spin_lock(&tp->lock);
2616 list_add(&f->hw_list, &head->hw_filters);
2617 spin_unlock(&tp->lock);
2620 static void fl_hw_del(struct tcf_proto *tp, void *type_data)
2622 struct flow_cls_offload *cls_flower = type_data;
2623 struct cls_fl_filter *f =
2624 (struct cls_fl_filter *) cls_flower->cookie;
2626 spin_lock(&tp->lock);
2627 if (!list_empty(&f->hw_list))
2628 list_del_init(&f->hw_list);
2629 spin_unlock(&tp->lock);
2632 static int fl_hw_create_tmplt(struct tcf_chain *chain,
2633 struct fl_flow_tmplt *tmplt)
2635 struct flow_cls_offload cls_flower = {};
2636 struct tcf_block *block = chain->block;
2638 cls_flower.rule = flow_rule_alloc(0);
2639 if (!cls_flower.rule)
2642 cls_flower.common.chain_index = chain->index;
2643 cls_flower.command = FLOW_CLS_TMPLT_CREATE;
2644 cls_flower.cookie = (unsigned long) tmplt;
2645 cls_flower.rule->match.dissector = &tmplt->dissector;
2646 cls_flower.rule->match.mask = &tmplt->mask;
2647 cls_flower.rule->match.key = &tmplt->dummy_key;
2649 /* We don't care if driver (any of them) fails to handle this
2650 * call. It serves just as a hint for it.
2652 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
2653 kfree(cls_flower.rule);
2658 static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
2659 struct fl_flow_tmplt *tmplt)
2661 struct flow_cls_offload cls_flower = {};
2662 struct tcf_block *block = chain->block;
2664 cls_flower.common.chain_index = chain->index;
2665 cls_flower.command = FLOW_CLS_TMPLT_DESTROY;
2666 cls_flower.cookie = (unsigned long) tmplt;
2668 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
2671 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
2672 struct nlattr **tca,
2673 struct netlink_ext_ack *extack)
2675 struct fl_flow_tmplt *tmplt;
2679 if (!tca[TCA_OPTIONS])
2680 return ERR_PTR(-EINVAL);
2682 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2684 return ERR_PTR(-ENOBUFS);
2685 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
2686 tca[TCA_OPTIONS], fl_policy, NULL);
2690 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
2695 tmplt->chain = chain;
2696 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
2700 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
2702 err = fl_hw_create_tmplt(chain, tmplt);
2713 return ERR_PTR(err);
2716 static void fl_tmplt_destroy(void *tmplt_priv)
2718 struct fl_flow_tmplt *tmplt = tmplt_priv;
2720 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
2724 static int fl_dump_key_val(struct sk_buff *skb,
2725 void *val, int val_type,
2726 void *mask, int mask_type, int len)
2730 if (!memchr_inv(mask, 0, len))
2732 err = nla_put(skb, val_type, len, val);
2735 if (mask_type != TCA_FLOWER_UNSPEC) {
2736 err = nla_put(skb, mask_type, len, mask);
2743 static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
2744 struct fl_flow_key *mask)
2746 if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst,
2747 TCA_FLOWER_KEY_PORT_DST_MIN,
2748 &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC,
2749 sizeof(key->tp_range.tp_min.dst)) ||
2750 fl_dump_key_val(skb, &key->tp_range.tp_max.dst,
2751 TCA_FLOWER_KEY_PORT_DST_MAX,
2752 &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC,
2753 sizeof(key->tp_range.tp_max.dst)) ||
2754 fl_dump_key_val(skb, &key->tp_range.tp_min.src,
2755 TCA_FLOWER_KEY_PORT_SRC_MIN,
2756 &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC,
2757 sizeof(key->tp_range.tp_min.src)) ||
2758 fl_dump_key_val(skb, &key->tp_range.tp_max.src,
2759 TCA_FLOWER_KEY_PORT_SRC_MAX,
2760 &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC,
2761 sizeof(key->tp_range.tp_max.src)))
2767 static int fl_dump_key_mpls_opt_lse(struct sk_buff *skb,
2768 struct flow_dissector_key_mpls *mpls_key,
2769 struct flow_dissector_key_mpls *mpls_mask,
2772 struct flow_dissector_mpls_lse *lse_mask = &mpls_mask->ls[lse_index];
2773 struct flow_dissector_mpls_lse *lse_key = &mpls_key->ls[lse_index];
2776 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH,
2781 if (lse_mask->mpls_ttl) {
2782 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL,
2787 if (lse_mask->mpls_bos) {
2788 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS,
2793 if (lse_mask->mpls_tc) {
2794 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC,
2799 if (lse_mask->mpls_label) {
2800 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL,
2801 lse_key->mpls_label);
2809 static int fl_dump_key_mpls_opts(struct sk_buff *skb,
2810 struct flow_dissector_key_mpls *mpls_key,
2811 struct flow_dissector_key_mpls *mpls_mask)
2813 struct nlattr *opts;
2818 opts = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS);
2822 for (lse_index = 0; lse_index < FLOW_DIS_MPLS_MAX; lse_index++) {
2823 if (!(mpls_mask->used_lses & 1 << lse_index))
2826 lse = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS_LSE);
2832 err = fl_dump_key_mpls_opt_lse(skb, mpls_key, mpls_mask,
2836 nla_nest_end(skb, lse);
2838 nla_nest_end(skb, opts);
2843 nla_nest_cancel(skb, lse);
2845 nla_nest_cancel(skb, opts);
2850 static int fl_dump_key_mpls(struct sk_buff *skb,
2851 struct flow_dissector_key_mpls *mpls_key,
2852 struct flow_dissector_key_mpls *mpls_mask)
2854 struct flow_dissector_mpls_lse *lse_mask;
2855 struct flow_dissector_mpls_lse *lse_key;
2858 if (!mpls_mask->used_lses)
2861 lse_mask = &mpls_mask->ls[0];
2862 lse_key = &mpls_key->ls[0];
2864 /* For backward compatibility, don't use the MPLS nested attributes if
2865 * the rule can be expressed using the old attributes.
2867 if (mpls_mask->used_lses & ~1 ||
2868 (!lse_mask->mpls_ttl && !lse_mask->mpls_bos &&
2869 !lse_mask->mpls_tc && !lse_mask->mpls_label))
2870 return fl_dump_key_mpls_opts(skb, mpls_key, mpls_mask);
2872 if (lse_mask->mpls_ttl) {
2873 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
2878 if (lse_mask->mpls_tc) {
2879 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
2884 if (lse_mask->mpls_label) {
2885 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
2886 lse_key->mpls_label);
2890 if (lse_mask->mpls_bos) {
2891 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
2899 static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
2900 struct flow_dissector_key_ip *key,
2901 struct flow_dissector_key_ip *mask)
2903 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
2904 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
2905 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
2906 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
2908 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
2909 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
2915 static int fl_dump_key_vlan(struct sk_buff *skb,
2916 int vlan_id_key, int vlan_prio_key,
2917 struct flow_dissector_key_vlan *vlan_key,
2918 struct flow_dissector_key_vlan *vlan_mask)
2922 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
2924 if (vlan_mask->vlan_id) {
2925 err = nla_put_u16(skb, vlan_id_key,
2930 if (vlan_mask->vlan_priority) {
2931 err = nla_put_u8(skb, vlan_prio_key,
2932 vlan_key->vlan_priority);
2939 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
2940 u32 *flower_key, u32 *flower_mask,
2941 u32 flower_flag_bit, u32 dissector_flag_bit)
2943 if (dissector_mask & dissector_flag_bit) {
2944 *flower_mask |= flower_flag_bit;
2945 if (dissector_key & dissector_flag_bit)
2946 *flower_key |= flower_flag_bit;
2950 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
2956 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
2962 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2963 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
2964 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2965 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
2966 FLOW_DIS_FIRST_FRAG);
2968 _key = cpu_to_be32(key);
2969 _mask = cpu_to_be32(mask);
2971 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
2975 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
2978 static int fl_dump_key_geneve_opt(struct sk_buff *skb,
2979 struct flow_dissector_key_enc_opts *enc_opts)
2981 struct geneve_opt *opt;
2982 struct nlattr *nest;
2985 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
2987 goto nla_put_failure;
2989 while (enc_opts->len > opt_off) {
2990 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
2992 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
2994 goto nla_put_failure;
2995 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
2997 goto nla_put_failure;
2998 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
2999 opt->length * 4, opt->opt_data))
3000 goto nla_put_failure;
3002 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
3004 nla_nest_end(skb, nest);
3008 nla_nest_cancel(skb, nest);
3012 static int fl_dump_key_vxlan_opt(struct sk_buff *skb,
3013 struct flow_dissector_key_enc_opts *enc_opts)
3015 struct vxlan_metadata *md;
3016 struct nlattr *nest;
3018 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN);
3020 goto nla_put_failure;
3022 md = (struct vxlan_metadata *)&enc_opts->data[0];
3023 if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp))
3024 goto nla_put_failure;
3026 nla_nest_end(skb, nest);
3030 nla_nest_cancel(skb, nest);
3034 static int fl_dump_key_erspan_opt(struct sk_buff *skb,
3035 struct flow_dissector_key_enc_opts *enc_opts)
3037 struct erspan_metadata *md;
3038 struct nlattr *nest;
3040 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_ERSPAN);
3042 goto nla_put_failure;
3044 md = (struct erspan_metadata *)&enc_opts->data[0];
3045 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER, md->version))
3046 goto nla_put_failure;
3048 if (md->version == 1 &&
3049 nla_put_be32(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index))
3050 goto nla_put_failure;
3052 if (md->version == 2 &&
3053 (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR,
3055 nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID,
3056 get_hwid(&md->u.md2))))
3057 goto nla_put_failure;
3059 nla_nest_end(skb, nest);
3063 nla_nest_cancel(skb, nest);
3067 static int fl_dump_key_gtp_opt(struct sk_buff *skb,
3068 struct flow_dissector_key_enc_opts *enc_opts)
3071 struct gtp_pdu_session_info *session_info;
3072 struct nlattr *nest;
3074 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GTP);
3076 goto nla_put_failure;
3078 session_info = (struct gtp_pdu_session_info *)&enc_opts->data[0];
3080 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE,
3081 session_info->pdu_type))
3082 goto nla_put_failure;
3084 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_QFI, session_info->qfi))
3085 goto nla_put_failure;
3087 nla_nest_end(skb, nest);
3091 nla_nest_cancel(skb, nest);
3095 static int fl_dump_key_ct(struct sk_buff *skb,
3096 struct flow_dissector_key_ct *key,
3097 struct flow_dissector_key_ct *mask)
3099 if (IS_ENABLED(CONFIG_NF_CONNTRACK) &&
3100 fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
3101 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
3102 sizeof(key->ct_state)))
3103 goto nla_put_failure;
3105 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
3106 fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
3107 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
3108 sizeof(key->ct_zone)))
3109 goto nla_put_failure;
3111 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
3112 fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
3113 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
3114 sizeof(key->ct_mark)))
3115 goto nla_put_failure;
3117 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
3118 fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
3119 &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
3120 sizeof(key->ct_labels)))
3121 goto nla_put_failure;
3129 static int fl_dump_key_cfm(struct sk_buff *skb,
3130 struct flow_dissector_key_cfm *key,
3131 struct flow_dissector_key_cfm *mask)
3133 struct nlattr *opts;
3137 if (!memchr_inv(mask, 0, sizeof(*mask)))
3140 opts = nla_nest_start(skb, TCA_FLOWER_KEY_CFM);
3144 if (FIELD_GET(FLOW_DIS_CFM_MDL_MASK, mask->mdl_ver)) {
3145 mdl = FIELD_GET(FLOW_DIS_CFM_MDL_MASK, key->mdl_ver);
3146 err = nla_put_u8(skb, TCA_FLOWER_KEY_CFM_MD_LEVEL, mdl);
3152 err = nla_put_u8(skb, TCA_FLOWER_KEY_CFM_OPCODE, key->opcode);
3157 nla_nest_end(skb, opts);
3162 nla_nest_cancel(skb, opts);
3166 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
3167 struct flow_dissector_key_enc_opts *enc_opts)
3169 struct nlattr *nest;
3175 nest = nla_nest_start_noflag(skb, enc_opt_type);
3177 goto nla_put_failure;
3179 switch (enc_opts->dst_opt_type) {
3180 case TUNNEL_GENEVE_OPT:
3181 err = fl_dump_key_geneve_opt(skb, enc_opts);
3183 goto nla_put_failure;
3185 case TUNNEL_VXLAN_OPT:
3186 err = fl_dump_key_vxlan_opt(skb, enc_opts);
3188 goto nla_put_failure;
3190 case TUNNEL_ERSPAN_OPT:
3191 err = fl_dump_key_erspan_opt(skb, enc_opts);
3193 goto nla_put_failure;
3195 case TUNNEL_GTP_OPT:
3196 err = fl_dump_key_gtp_opt(skb, enc_opts);
3198 goto nla_put_failure;
3201 goto nla_put_failure;
3203 nla_nest_end(skb, nest);
3207 nla_nest_cancel(skb, nest);
3211 static int fl_dump_key_enc_opt(struct sk_buff *skb,
3212 struct flow_dissector_key_enc_opts *key_opts,
3213 struct flow_dissector_key_enc_opts *msk_opts)
3217 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
3221 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
3224 static int fl_dump_key(struct sk_buff *skb, struct net *net,
3225 struct fl_flow_key *key, struct fl_flow_key *mask)
3227 if (mask->meta.ingress_ifindex) {
3228 struct net_device *dev;
3230 dev = __dev_get_by_index(net, key->meta.ingress_ifindex);
3231 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
3232 goto nla_put_failure;
3235 if (fl_dump_key_val(skb, &key->meta.l2_miss,
3236 TCA_FLOWER_L2_MISS, &mask->meta.l2_miss,
3237 TCA_FLOWER_UNSPEC, sizeof(key->meta.l2_miss)))
3238 goto nla_put_failure;
3240 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
3241 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
3242 sizeof(key->eth.dst)) ||
3243 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
3244 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
3245 sizeof(key->eth.src)) ||
3246 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
3247 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
3248 sizeof(key->basic.n_proto)))
3249 goto nla_put_failure;
3251 if (mask->num_of_vlans.num_of_vlans) {
3252 if (nla_put_u8(skb, TCA_FLOWER_KEY_NUM_OF_VLANS, key->num_of_vlans.num_of_vlans))
3253 goto nla_put_failure;
3256 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
3257 goto nla_put_failure;
3259 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
3260 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
3261 goto nla_put_failure;
3263 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
3264 TCA_FLOWER_KEY_CVLAN_PRIO,
3265 &key->cvlan, &mask->cvlan) ||
3266 (mask->cvlan.vlan_tpid &&
3267 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3268 key->cvlan.vlan_tpid)))
3269 goto nla_put_failure;
3271 if (mask->basic.n_proto) {
3272 if (mask->cvlan.vlan_eth_type) {
3273 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
3274 key->basic.n_proto))
3275 goto nla_put_failure;
3276 } else if (mask->vlan.vlan_eth_type) {
3277 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3278 key->vlan.vlan_eth_type))
3279 goto nla_put_failure;
3283 if ((key->basic.n_proto == htons(ETH_P_IP) ||
3284 key->basic.n_proto == htons(ETH_P_IPV6)) &&
3285 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
3286 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
3287 sizeof(key->basic.ip_proto)) ||
3288 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
3289 goto nla_put_failure;
3291 if (mask->pppoe.session_id) {
3292 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPPOE_SID,
3293 key->pppoe.session_id))
3294 goto nla_put_failure;
3296 if (mask->basic.n_proto && mask->pppoe.ppp_proto) {
3297 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPP_PROTO,
3298 key->pppoe.ppp_proto))
3299 goto nla_put_failure;
3302 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
3303 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
3304 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
3305 sizeof(key->ipv4.src)) ||
3306 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
3307 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
3308 sizeof(key->ipv4.dst))))
3309 goto nla_put_failure;
3310 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
3311 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
3312 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
3313 sizeof(key->ipv6.src)) ||
3314 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
3315 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
3316 sizeof(key->ipv6.dst))))
3317 goto nla_put_failure;
3319 if (key->basic.ip_proto == IPPROTO_TCP &&
3320 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
3321 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
3322 sizeof(key->tp.src)) ||
3323 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
3324 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
3325 sizeof(key->tp.dst)) ||
3326 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
3327 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
3328 sizeof(key->tcp.flags))))
3329 goto nla_put_failure;
3330 else if (key->basic.ip_proto == IPPROTO_UDP &&
3331 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
3332 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
3333 sizeof(key->tp.src)) ||
3334 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
3335 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
3336 sizeof(key->tp.dst))))
3337 goto nla_put_failure;
3338 else if (key->basic.ip_proto == IPPROTO_SCTP &&
3339 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
3340 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
3341 sizeof(key->tp.src)) ||
3342 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
3343 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
3344 sizeof(key->tp.dst))))
3345 goto nla_put_failure;
3346 else if (key->basic.n_proto == htons(ETH_P_IP) &&
3347 key->basic.ip_proto == IPPROTO_ICMP &&
3348 (fl_dump_key_val(skb, &key->icmp.type,
3349 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
3350 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
3351 sizeof(key->icmp.type)) ||
3352 fl_dump_key_val(skb, &key->icmp.code,
3353 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
3354 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
3355 sizeof(key->icmp.code))))
3356 goto nla_put_failure;
3357 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
3358 key->basic.ip_proto == IPPROTO_ICMPV6 &&
3359 (fl_dump_key_val(skb, &key->icmp.type,
3360 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
3361 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
3362 sizeof(key->icmp.type)) ||
3363 fl_dump_key_val(skb, &key->icmp.code,
3364 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
3365 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
3366 sizeof(key->icmp.code))))
3367 goto nla_put_failure;
3368 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
3369 key->basic.n_proto == htons(ETH_P_RARP)) &&
3370 (fl_dump_key_val(skb, &key->arp.sip,
3371 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
3372 TCA_FLOWER_KEY_ARP_SIP_MASK,
3373 sizeof(key->arp.sip)) ||
3374 fl_dump_key_val(skb, &key->arp.tip,
3375 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
3376 TCA_FLOWER_KEY_ARP_TIP_MASK,
3377 sizeof(key->arp.tip)) ||
3378 fl_dump_key_val(skb, &key->arp.op,
3379 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
3380 TCA_FLOWER_KEY_ARP_OP_MASK,
3381 sizeof(key->arp.op)) ||
3382 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
3383 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
3384 sizeof(key->arp.sha)) ||
3385 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
3386 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
3387 sizeof(key->arp.tha))))
3388 goto nla_put_failure;
3389 else if (key->basic.ip_proto == IPPROTO_L2TP &&
3390 fl_dump_key_val(skb, &key->l2tpv3.session_id,
3391 TCA_FLOWER_KEY_L2TPV3_SID,
3392 &mask->l2tpv3.session_id,
3394 sizeof(key->l2tpv3.session_id)))
3395 goto nla_put_failure;
3397 if (key->ipsec.spi &&
3398 fl_dump_key_val(skb, &key->ipsec.spi, TCA_FLOWER_KEY_SPI,
3399 &mask->ipsec.spi, TCA_FLOWER_KEY_SPI_MASK,
3400 sizeof(key->ipsec.spi)))
3401 goto nla_put_failure;
3403 if ((key->basic.ip_proto == IPPROTO_TCP ||
3404 key->basic.ip_proto == IPPROTO_UDP ||
3405 key->basic.ip_proto == IPPROTO_SCTP) &&
3406 fl_dump_key_port_range(skb, key, mask))
3407 goto nla_put_failure;
3409 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
3410 (fl_dump_key_val(skb, &key->enc_ipv4.src,
3411 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
3412 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
3413 sizeof(key->enc_ipv4.src)) ||
3414 fl_dump_key_val(skb, &key->enc_ipv4.dst,
3415 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
3416 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
3417 sizeof(key->enc_ipv4.dst))))
3418 goto nla_put_failure;
3419 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
3420 (fl_dump_key_val(skb, &key->enc_ipv6.src,
3421 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
3422 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
3423 sizeof(key->enc_ipv6.src)) ||
3424 fl_dump_key_val(skb, &key->enc_ipv6.dst,
3425 TCA_FLOWER_KEY_ENC_IPV6_DST,
3426 &mask->enc_ipv6.dst,
3427 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
3428 sizeof(key->enc_ipv6.dst))))
3429 goto nla_put_failure;
3431 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
3432 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
3433 sizeof(key->enc_key_id)) ||
3434 fl_dump_key_val(skb, &key->enc_tp.src,
3435 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
3437 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
3438 sizeof(key->enc_tp.src)) ||
3439 fl_dump_key_val(skb, &key->enc_tp.dst,
3440 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
3442 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
3443 sizeof(key->enc_tp.dst)) ||
3444 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
3445 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
3446 goto nla_put_failure;
3448 if (fl_dump_key_ct(skb, &key->ct, &mask->ct))
3449 goto nla_put_failure;
3451 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
3452 goto nla_put_failure;
3454 if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
3455 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
3456 sizeof(key->hash.hash)))
3457 goto nla_put_failure;
3459 if (fl_dump_key_cfm(skb, &key->cfm, &mask->cfm))
3460 goto nla_put_failure;
3468 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
3469 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3471 struct cls_fl_filter *f = fh;
3472 struct nlattr *nest;
3473 struct fl_flow_key *key, *mask;
3479 t->tcm_handle = f->handle;
3481 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3483 goto nla_put_failure;
3485 spin_lock(&tp->lock);
3487 if (f->res.classid &&
3488 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
3489 goto nla_put_failure_locked;
3492 mask = &f->mask->key;
3493 skip_hw = tc_skip_hw(f->flags);
3495 if (fl_dump_key(skb, net, key, mask))
3496 goto nla_put_failure_locked;
3498 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3499 goto nla_put_failure_locked;
3501 spin_unlock(&tp->lock);
3504 fl_hw_update_stats(tp, f, rtnl_held);
3506 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
3507 goto nla_put_failure;
3509 if (tcf_exts_dump(skb, &f->exts))
3510 goto nla_put_failure;
3512 nla_nest_end(skb, nest);
3514 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
3515 goto nla_put_failure;
3519 nla_put_failure_locked:
3520 spin_unlock(&tp->lock);
3522 nla_nest_cancel(skb, nest);
3526 static int fl_terse_dump(struct net *net, struct tcf_proto *tp, void *fh,
3527 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3529 struct cls_fl_filter *f = fh;
3530 struct nlattr *nest;
3536 t->tcm_handle = f->handle;
3538 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3540 goto nla_put_failure;
3542 spin_lock(&tp->lock);
3544 skip_hw = tc_skip_hw(f->flags);
3546 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3547 goto nla_put_failure_locked;
3549 spin_unlock(&tp->lock);
3552 fl_hw_update_stats(tp, f, rtnl_held);
3554 if (tcf_exts_terse_dump(skb, &f->exts))
3555 goto nla_put_failure;
3557 nla_nest_end(skb, nest);
3561 nla_put_failure_locked:
3562 spin_unlock(&tp->lock);
3564 nla_nest_cancel(skb, nest);
3568 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
3570 struct fl_flow_tmplt *tmplt = tmplt_priv;
3571 struct fl_flow_key *key, *mask;
3572 struct nlattr *nest;
3574 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3576 goto nla_put_failure;
3578 key = &tmplt->dummy_key;
3579 mask = &tmplt->mask;
3581 if (fl_dump_key(skb, net, key, mask))
3582 goto nla_put_failure;
3584 nla_nest_end(skb, nest);
3589 nla_nest_cancel(skb, nest);
3593 static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
3596 struct cls_fl_filter *f = fh;
3598 tc_cls_bind_class(classid, cl, q, &f->res, base);
3601 static bool fl_delete_empty(struct tcf_proto *tp)
3603 struct cls_fl_head *head = fl_head_dereference(tp);
3605 spin_lock(&tp->lock);
3606 tp->deleting = idr_is_empty(&head->handle_idr);
3607 spin_unlock(&tp->lock);
3609 return tp->deleting;
3612 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
3614 .classify = fl_classify,
3616 .destroy = fl_destroy,
3619 .change = fl_change,
3620 .delete = fl_delete,
3621 .delete_empty = fl_delete_empty,
3623 .reoffload = fl_reoffload,
3624 .hw_add = fl_hw_add,
3625 .hw_del = fl_hw_del,
3627 .terse_dump = fl_terse_dump,
3628 .bind_class = fl_bind_class,
3629 .tmplt_create = fl_tmplt_create,
3630 .tmplt_destroy = fl_tmplt_destroy,
3631 .tmplt_dump = fl_tmplt_dump,
3632 .get_exts = fl_get_exts,
3633 .owner = THIS_MODULE,
3634 .flags = TCF_PROTO_OPS_DOIT_UNLOCKED,
3637 static int __init cls_fl_init(void)
3639 return register_tcf_proto_ops(&cls_fl_ops);
3642 static void __exit cls_fl_exit(void)
3644 unregister_tcf_proto_ops(&cls_fl_ops);
3647 module_init(cls_fl_init);
3648 module_exit(cls_fl_exit);
3650 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3651 MODULE_DESCRIPTION("Flower classifier");
3652 MODULE_LICENSE("GPL v2");