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>
15 #include <linux/if_ether.h>
16 #include <linux/in6.h>
18 #include <linux/mpls.h>
19 #include <linux/ppp_defs.h>
21 #include <net/sch_generic.h>
22 #include <net/pkt_cls.h>
23 #include <net/pkt_sched.h>
25 #include <net/flow_dissector.h>
26 #include <net/geneve.h>
27 #include <net/vxlan.h>
28 #include <net/erspan.h>
32 #include <net/dst_metadata.h>
34 #include <uapi/linux/netfilter/nf_conntrack_common.h>
36 #define TCA_FLOWER_KEY_CT_FLAGS_MAX \
37 ((__TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) << 1)
38 #define TCA_FLOWER_KEY_CT_FLAGS_MASK \
39 (TCA_FLOWER_KEY_CT_FLAGS_MAX - 1)
42 struct flow_dissector_key_meta meta;
43 struct flow_dissector_key_control control;
44 struct flow_dissector_key_control enc_control;
45 struct flow_dissector_key_basic basic;
46 struct flow_dissector_key_eth_addrs eth;
47 struct flow_dissector_key_vlan vlan;
48 struct flow_dissector_key_vlan cvlan;
50 struct flow_dissector_key_ipv4_addrs ipv4;
51 struct flow_dissector_key_ipv6_addrs ipv6;
53 struct flow_dissector_key_ports tp;
54 struct flow_dissector_key_icmp icmp;
55 struct flow_dissector_key_arp arp;
56 struct flow_dissector_key_keyid enc_key_id;
58 struct flow_dissector_key_ipv4_addrs enc_ipv4;
59 struct flow_dissector_key_ipv6_addrs enc_ipv6;
61 struct flow_dissector_key_ports enc_tp;
62 struct flow_dissector_key_mpls mpls;
63 struct flow_dissector_key_tcp tcp;
64 struct flow_dissector_key_ip ip;
65 struct flow_dissector_key_ip enc_ip;
66 struct flow_dissector_key_enc_opts enc_opts;
67 struct flow_dissector_key_ports_range tp_range;
68 struct flow_dissector_key_ct ct;
69 struct flow_dissector_key_hash hash;
70 struct flow_dissector_key_num_of_vlans num_of_vlans;
71 struct flow_dissector_key_pppoe pppoe;
72 struct flow_dissector_key_l2tpv3 l2tpv3;
73 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
75 struct fl_flow_mask_range {
76 unsigned short int start;
77 unsigned short int end;
81 struct fl_flow_key key;
82 struct fl_flow_mask_range range;
84 struct rhash_head ht_node;
86 struct rhashtable_params filter_ht_params;
87 struct flow_dissector dissector;
88 struct list_head filters;
89 struct rcu_work rwork;
90 struct list_head list;
94 struct fl_flow_tmplt {
95 struct fl_flow_key dummy_key;
96 struct fl_flow_key mask;
97 struct flow_dissector dissector;
98 struct tcf_chain *chain;
102 struct rhashtable ht;
103 spinlock_t masks_lock; /* Protect masks list */
104 struct list_head masks;
105 struct list_head hw_filters;
106 struct rcu_work rwork;
107 struct idr handle_idr;
110 struct cls_fl_filter {
111 struct fl_flow_mask *mask;
112 struct rhash_head ht_node;
113 struct fl_flow_key mkey;
114 struct tcf_exts exts;
115 struct tcf_result res;
116 struct fl_flow_key key;
117 struct list_head list;
118 struct list_head hw_list;
122 struct rcu_work rwork;
123 struct net_device *hw_dev;
124 /* Flower classifier is unlocked, which means that its reference counter
125 * can be changed concurrently without any kind of external
126 * synchronization. Use atomic reference counter to be concurrency-safe.
132 static const struct rhashtable_params mask_ht_params = {
133 .key_offset = offsetof(struct fl_flow_mask, key),
134 .key_len = sizeof(struct fl_flow_key),
135 .head_offset = offsetof(struct fl_flow_mask, ht_node),
136 .automatic_shrinking = true,
139 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
141 return mask->range.end - mask->range.start;
144 static void fl_mask_update_range(struct fl_flow_mask *mask)
146 const u8 *bytes = (const u8 *) &mask->key;
147 size_t size = sizeof(mask->key);
148 size_t i, first = 0, last;
150 for (i = 0; i < size; i++) {
157 for (i = size - 1; i != first; i--) {
163 mask->range.start = rounddown(first, sizeof(long));
164 mask->range.end = roundup(last + 1, sizeof(long));
167 static void *fl_key_get_start(struct fl_flow_key *key,
168 const struct fl_flow_mask *mask)
170 return (u8 *) key + mask->range.start;
173 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
174 struct fl_flow_mask *mask)
176 const long *lkey = fl_key_get_start(key, mask);
177 const long *lmask = fl_key_get_start(&mask->key, mask);
178 long *lmkey = fl_key_get_start(mkey, mask);
181 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
182 *lmkey++ = *lkey++ & *lmask++;
185 static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
186 struct fl_flow_mask *mask)
188 const long *lmask = fl_key_get_start(&mask->key, mask);
194 ltmplt = fl_key_get_start(&tmplt->mask, mask);
195 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
196 if (~*ltmplt++ & *lmask++)
202 static void fl_clear_masked_range(struct fl_flow_key *key,
203 struct fl_flow_mask *mask)
205 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
208 static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
209 struct fl_flow_key *key,
210 struct fl_flow_key *mkey)
212 u16 min_mask, max_mask, min_val, max_val;
214 min_mask = ntohs(filter->mask->key.tp_range.tp_min.dst);
215 max_mask = ntohs(filter->mask->key.tp_range.tp_max.dst);
216 min_val = ntohs(filter->key.tp_range.tp_min.dst);
217 max_val = ntohs(filter->key.tp_range.tp_max.dst);
219 if (min_mask && max_mask) {
220 if (ntohs(key->tp_range.tp.dst) < min_val ||
221 ntohs(key->tp_range.tp.dst) > max_val)
224 /* skb does not have min and max values */
225 mkey->tp_range.tp_min.dst = filter->mkey.tp_range.tp_min.dst;
226 mkey->tp_range.tp_max.dst = filter->mkey.tp_range.tp_max.dst;
231 static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
232 struct fl_flow_key *key,
233 struct fl_flow_key *mkey)
235 u16 min_mask, max_mask, min_val, max_val;
237 min_mask = ntohs(filter->mask->key.tp_range.tp_min.src);
238 max_mask = ntohs(filter->mask->key.tp_range.tp_max.src);
239 min_val = ntohs(filter->key.tp_range.tp_min.src);
240 max_val = ntohs(filter->key.tp_range.tp_max.src);
242 if (min_mask && max_mask) {
243 if (ntohs(key->tp_range.tp.src) < min_val ||
244 ntohs(key->tp_range.tp.src) > max_val)
247 /* skb does not have min and max values */
248 mkey->tp_range.tp_min.src = filter->mkey.tp_range.tp_min.src;
249 mkey->tp_range.tp_max.src = filter->mkey.tp_range.tp_max.src;
254 static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
255 struct fl_flow_key *mkey)
257 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
258 mask->filter_ht_params);
261 static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
262 struct fl_flow_key *mkey,
263 struct fl_flow_key *key)
265 struct cls_fl_filter *filter, *f;
267 list_for_each_entry_rcu(filter, &mask->filters, list) {
268 if (!fl_range_port_dst_cmp(filter, key, mkey))
271 if (!fl_range_port_src_cmp(filter, key, mkey))
274 f = __fl_lookup(mask, mkey);
281 static noinline_for_stack
282 struct cls_fl_filter *fl_mask_lookup(struct fl_flow_mask *mask, struct fl_flow_key *key)
284 struct fl_flow_key mkey;
286 fl_set_masked_key(&mkey, key, mask);
287 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
288 return fl_lookup_range(mask, &mkey, key);
290 return __fl_lookup(mask, &mkey);
293 static u16 fl_ct_info_to_flower_map[] = {
294 [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
295 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
296 [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
297 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
298 [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
299 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED |
300 TCA_FLOWER_KEY_CT_FLAGS_REPLY,
301 [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
302 TCA_FLOWER_KEY_CT_FLAGS_RELATED |
303 TCA_FLOWER_KEY_CT_FLAGS_REPLY,
304 [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
305 TCA_FLOWER_KEY_CT_FLAGS_NEW,
308 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
309 struct tcf_result *res)
311 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
312 bool post_ct = tc_skb_cb(skb)->post_ct;
313 u16 zone = tc_skb_cb(skb)->zone;
314 struct fl_flow_key skb_key;
315 struct fl_flow_mask *mask;
316 struct cls_fl_filter *f;
318 list_for_each_entry_rcu(mask, &head->masks, list) {
319 flow_dissector_init_keys(&skb_key.control, &skb_key.basic);
320 fl_clear_masked_range(&skb_key, mask);
322 skb_flow_dissect_meta(skb, &mask->dissector, &skb_key);
323 /* skb_flow_dissect() does not set n_proto in case an unknown
324 * protocol, so do it rather here.
326 skb_key.basic.n_proto = skb_protocol(skb, false);
327 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
328 skb_flow_dissect_ct(skb, &mask->dissector, &skb_key,
329 fl_ct_info_to_flower_map,
330 ARRAY_SIZE(fl_ct_info_to_flower_map),
332 skb_flow_dissect_hash(skb, &mask->dissector, &skb_key);
333 skb_flow_dissect(skb, &mask->dissector, &skb_key,
334 FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP);
336 f = fl_mask_lookup(mask, &skb_key);
337 if (f && !tc_skip_sw(f->flags)) {
339 return tcf_exts_exec(skb, &f->exts, res);
345 static int fl_init(struct tcf_proto *tp)
347 struct cls_fl_head *head;
349 head = kzalloc(sizeof(*head), GFP_KERNEL);
353 spin_lock_init(&head->masks_lock);
354 INIT_LIST_HEAD_RCU(&head->masks);
355 INIT_LIST_HEAD(&head->hw_filters);
356 rcu_assign_pointer(tp->root, head);
357 idr_init(&head->handle_idr);
359 return rhashtable_init(&head->ht, &mask_ht_params);
362 static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done)
364 /* temporary masks don't have their filters list and ht initialized */
365 if (mask_init_done) {
366 WARN_ON(!list_empty(&mask->filters));
367 rhashtable_destroy(&mask->ht);
372 static void fl_mask_free_work(struct work_struct *work)
374 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
375 struct fl_flow_mask, rwork);
377 fl_mask_free(mask, true);
380 static void fl_uninit_mask_free_work(struct work_struct *work)
382 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
383 struct fl_flow_mask, rwork);
385 fl_mask_free(mask, false);
388 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask)
390 if (!refcount_dec_and_test(&mask->refcnt))
393 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
395 spin_lock(&head->masks_lock);
396 list_del_rcu(&mask->list);
397 spin_unlock(&head->masks_lock);
399 tcf_queue_work(&mask->rwork, fl_mask_free_work);
404 static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
406 /* Flower classifier only changes root pointer during init and destroy.
407 * Users must obtain reference to tcf_proto instance before calling its
408 * API, so tp->root pointer is protected from concurrent call to
409 * fl_destroy() by reference counting.
411 return rcu_dereference_raw(tp->root);
414 static void __fl_destroy_filter(struct cls_fl_filter *f)
416 tcf_exts_destroy(&f->exts);
417 tcf_exts_put_net(&f->exts);
421 static void fl_destroy_filter_work(struct work_struct *work)
423 struct cls_fl_filter *f = container_of(to_rcu_work(work),
424 struct cls_fl_filter, rwork);
426 __fl_destroy_filter(f);
429 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
430 bool rtnl_held, struct netlink_ext_ack *extack)
432 struct tcf_block *block = tp->chain->block;
433 struct flow_cls_offload cls_flower = {};
435 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
436 cls_flower.command = FLOW_CLS_DESTROY;
437 cls_flower.cookie = (unsigned long) f;
439 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false,
440 &f->flags, &f->in_hw_count, rtnl_held);
444 static int fl_hw_replace_filter(struct tcf_proto *tp,
445 struct cls_fl_filter *f, bool rtnl_held,
446 struct netlink_ext_ack *extack)
448 struct tcf_block *block = tp->chain->block;
449 struct flow_cls_offload cls_flower = {};
450 bool skip_sw = tc_skip_sw(f->flags);
453 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
454 if (!cls_flower.rule)
457 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
458 cls_flower.command = FLOW_CLS_REPLACE;
459 cls_flower.cookie = (unsigned long) f;
460 cls_flower.rule->match.dissector = &f->mask->dissector;
461 cls_flower.rule->match.mask = &f->mask->key;
462 cls_flower.rule->match.key = &f->mkey;
463 cls_flower.classid = f->res.classid;
465 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts,
466 cls_flower.common.extack);
468 kfree(cls_flower.rule);
470 return skip_sw ? err : 0;
473 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower,
474 skip_sw, &f->flags, &f->in_hw_count, rtnl_held);
475 tc_cleanup_offload_action(&cls_flower.rule->action);
476 kfree(cls_flower.rule);
479 fl_hw_destroy_filter(tp, f, rtnl_held, NULL);
483 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
489 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f,
492 struct tcf_block *block = tp->chain->block;
493 struct flow_cls_offload cls_flower = {};
495 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
496 cls_flower.command = FLOW_CLS_STATS;
497 cls_flower.cookie = (unsigned long) f;
498 cls_flower.classid = f->res.classid;
500 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false,
503 tcf_exts_hw_stats_update(&f->exts, cls_flower.stats.bytes,
504 cls_flower.stats.pkts,
505 cls_flower.stats.drops,
506 cls_flower.stats.lastused,
507 cls_flower.stats.used_hw_stats,
508 cls_flower.stats.used_hw_stats_valid);
511 static void __fl_put(struct cls_fl_filter *f)
513 if (!refcount_dec_and_test(&f->refcnt))
516 if (tcf_exts_get_net(&f->exts))
517 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
519 __fl_destroy_filter(f);
522 static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
524 struct cls_fl_filter *f;
527 f = idr_find(&head->handle_idr, handle);
528 if (f && !refcount_inc_not_zero(&f->refcnt))
535 static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
536 bool *last, bool rtnl_held,
537 struct netlink_ext_ack *extack)
539 struct cls_fl_head *head = fl_head_dereference(tp);
543 spin_lock(&tp->lock);
545 spin_unlock(&tp->lock);
550 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
551 f->mask->filter_ht_params);
552 idr_remove(&head->handle_idr, f->handle);
553 list_del_rcu(&f->list);
554 spin_unlock(&tp->lock);
556 *last = fl_mask_put(head, f->mask);
557 if (!tc_skip_hw(f->flags))
558 fl_hw_destroy_filter(tp, f, rtnl_held, extack);
559 tcf_unbind_filter(tp, &f->res);
565 static void fl_destroy_sleepable(struct work_struct *work)
567 struct cls_fl_head *head = container_of(to_rcu_work(work),
571 rhashtable_destroy(&head->ht);
573 module_put(THIS_MODULE);
576 static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
577 struct netlink_ext_ack *extack)
579 struct cls_fl_head *head = fl_head_dereference(tp);
580 struct fl_flow_mask *mask, *next_mask;
581 struct cls_fl_filter *f, *next;
584 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
585 list_for_each_entry_safe(f, next, &mask->filters, list) {
586 __fl_delete(tp, f, &last, rtnl_held, extack);
591 idr_destroy(&head->handle_idr);
593 __module_get(THIS_MODULE);
594 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
597 static void fl_put(struct tcf_proto *tp, void *arg)
599 struct cls_fl_filter *f = arg;
604 static void *fl_get(struct tcf_proto *tp, u32 handle)
606 struct cls_fl_head *head = fl_head_dereference(tp);
608 return __fl_get(head, handle);
611 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
612 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
613 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
614 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
616 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
617 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
618 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
619 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
620 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
621 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
622 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
623 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
624 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
625 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
626 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
627 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
628 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
629 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
630 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
631 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
632 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
633 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
634 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
635 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
636 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
637 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
638 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
639 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
640 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
641 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
642 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
643 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
644 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
645 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
646 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
647 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
648 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
649 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
650 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
651 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
652 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
653 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
654 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
655 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
656 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
657 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
658 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
659 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
660 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
661 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
662 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
663 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
664 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
665 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
666 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
667 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
668 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
669 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
670 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
671 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
672 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
673 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
674 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
675 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
676 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
677 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
678 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
679 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
680 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
681 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
682 [TCA_FLOWER_KEY_MPLS_OPTS] = { .type = NLA_NESTED },
683 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
684 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
685 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
686 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
687 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
688 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
689 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
690 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
691 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
692 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
693 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
694 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
695 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
696 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
697 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
698 [TCA_FLOWER_KEY_CT_STATE] =
699 NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK),
700 [TCA_FLOWER_KEY_CT_STATE_MASK] =
701 NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK),
702 [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 },
703 [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 },
704 [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 },
705 [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 },
706 [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY,
707 .len = 128 / BITS_PER_BYTE },
708 [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY,
709 .len = 128 / BITS_PER_BYTE },
710 [TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
711 [TCA_FLOWER_KEY_HASH] = { .type = NLA_U32 },
712 [TCA_FLOWER_KEY_HASH_MASK] = { .type = NLA_U32 },
713 [TCA_FLOWER_KEY_NUM_OF_VLANS] = { .type = NLA_U8 },
714 [TCA_FLOWER_KEY_PPPOE_SID] = { .type = NLA_U16 },
715 [TCA_FLOWER_KEY_PPP_PROTO] = { .type = NLA_U16 },
716 [TCA_FLOWER_KEY_L2TPV3_SID] = { .type = NLA_U32 },
720 static const struct nla_policy
721 enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
722 [TCA_FLOWER_KEY_ENC_OPTS_UNSPEC] = {
723 .strict_start_type = TCA_FLOWER_KEY_ENC_OPTS_VXLAN },
724 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
725 [TCA_FLOWER_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED },
726 [TCA_FLOWER_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED },
727 [TCA_FLOWER_KEY_ENC_OPTS_GTP] = { .type = NLA_NESTED },
730 static const struct nla_policy
731 geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
732 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
733 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
734 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
738 static const struct nla_policy
739 vxlan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1] = {
740 [TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 },
743 static const struct nla_policy
744 erspan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1] = {
745 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 },
746 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
747 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
748 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
751 static const struct nla_policy
752 gtp_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1] = {
753 [TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] = { .type = NLA_U8 },
754 [TCA_FLOWER_KEY_ENC_OPT_GTP_QFI] = { .type = NLA_U8 },
757 static const struct nla_policy
758 mpls_stack_entry_policy[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1] = {
759 [TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH] = { .type = NLA_U8 },
760 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL] = { .type = NLA_U8 },
761 [TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS] = { .type = NLA_U8 },
762 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TC] = { .type = NLA_U8 },
763 [TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL] = { .type = NLA_U32 },
766 static void fl_set_key_val(struct nlattr **tb,
767 void *val, int val_type,
768 void *mask, int mask_type, int len)
772 nla_memcpy(val, tb[val_type], len);
773 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
774 memset(mask, 0xff, len);
776 nla_memcpy(mask, tb[mask_type], len);
779 static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
780 struct fl_flow_key *mask,
781 struct netlink_ext_ack *extack)
783 fl_set_key_val(tb, &key->tp_range.tp_min.dst,
784 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst,
785 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.dst));
786 fl_set_key_val(tb, &key->tp_range.tp_max.dst,
787 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_range.tp_max.dst,
788 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.dst));
789 fl_set_key_val(tb, &key->tp_range.tp_min.src,
790 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_range.tp_min.src,
791 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.src));
792 fl_set_key_val(tb, &key->tp_range.tp_max.src,
793 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src,
794 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src));
796 if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
797 ntohs(key->tp_range.tp_max.dst) <=
798 ntohs(key->tp_range.tp_min.dst)) {
799 NL_SET_ERR_MSG_ATTR(extack,
800 tb[TCA_FLOWER_KEY_PORT_DST_MIN],
801 "Invalid destination port range (min must be strictly smaller than max)");
804 if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
805 ntohs(key->tp_range.tp_max.src) <=
806 ntohs(key->tp_range.tp_min.src)) {
807 NL_SET_ERR_MSG_ATTR(extack,
808 tb[TCA_FLOWER_KEY_PORT_SRC_MIN],
809 "Invalid source port range (min must be strictly smaller than max)");
816 static int fl_set_key_mpls_lse(const struct nlattr *nla_lse,
817 struct flow_dissector_key_mpls *key_val,
818 struct flow_dissector_key_mpls *key_mask,
819 struct netlink_ext_ack *extack)
821 struct nlattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1];
822 struct flow_dissector_mpls_lse *lse_mask;
823 struct flow_dissector_mpls_lse *lse_val;
828 err = nla_parse_nested(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, nla_lse,
829 mpls_stack_entry_policy, extack);
833 if (!tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]) {
834 NL_SET_ERR_MSG(extack, "Missing MPLS option \"depth\"");
838 depth = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]);
840 /* LSE depth starts at 1, for consistency with terminology used by
841 * RFC 3031 (section 3.9), where depth 0 refers to unlabeled packets.
843 if (depth < 1 || depth > FLOW_DIS_MPLS_MAX) {
844 NL_SET_ERR_MSG_ATTR(extack,
845 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH],
846 "Invalid MPLS depth");
849 lse_index = depth - 1;
851 dissector_set_mpls_lse(key_val, lse_index);
852 dissector_set_mpls_lse(key_mask, lse_index);
854 lse_val = &key_val->ls[lse_index];
855 lse_mask = &key_mask->ls[lse_index];
857 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]) {
858 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]);
859 lse_mask->mpls_ttl = MPLS_TTL_MASK;
861 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]) {
862 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]);
864 if (bos & ~MPLS_BOS_MASK) {
865 NL_SET_ERR_MSG_ATTR(extack,
866 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS],
867 "Bottom Of Stack (BOS) must be 0 or 1");
870 lse_val->mpls_bos = bos;
871 lse_mask->mpls_bos = MPLS_BOS_MASK;
873 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]) {
874 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]);
876 if (tc & ~MPLS_TC_MASK) {
877 NL_SET_ERR_MSG_ATTR(extack,
878 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC],
879 "Traffic Class (TC) must be between 0 and 7");
882 lse_val->mpls_tc = tc;
883 lse_mask->mpls_tc = MPLS_TC_MASK;
885 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]) {
886 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]);
888 if (label & ~MPLS_LABEL_MASK) {
889 NL_SET_ERR_MSG_ATTR(extack,
890 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL],
891 "Label must be between 0 and 1048575");
894 lse_val->mpls_label = label;
895 lse_mask->mpls_label = MPLS_LABEL_MASK;
901 static int fl_set_key_mpls_opts(const struct nlattr *nla_mpls_opts,
902 struct flow_dissector_key_mpls *key_val,
903 struct flow_dissector_key_mpls *key_mask,
904 struct netlink_ext_ack *extack)
906 struct nlattr *nla_lse;
910 if (!(nla_mpls_opts->nla_type & NLA_F_NESTED)) {
911 NL_SET_ERR_MSG_ATTR(extack, nla_mpls_opts,
912 "NLA_F_NESTED is missing");
916 nla_for_each_nested(nla_lse, nla_mpls_opts, rem) {
917 if (nla_type(nla_lse) != TCA_FLOWER_KEY_MPLS_OPTS_LSE) {
918 NL_SET_ERR_MSG_ATTR(extack, nla_lse,
919 "Invalid MPLS option type");
923 err = fl_set_key_mpls_lse(nla_lse, key_val, key_mask, extack);
928 NL_SET_ERR_MSG(extack,
929 "Bytes leftover after parsing MPLS options");
936 static int fl_set_key_mpls(struct nlattr **tb,
937 struct flow_dissector_key_mpls *key_val,
938 struct flow_dissector_key_mpls *key_mask,
939 struct netlink_ext_ack *extack)
941 struct flow_dissector_mpls_lse *lse_mask;
942 struct flow_dissector_mpls_lse *lse_val;
944 if (tb[TCA_FLOWER_KEY_MPLS_OPTS]) {
945 if (tb[TCA_FLOWER_KEY_MPLS_TTL] ||
946 tb[TCA_FLOWER_KEY_MPLS_BOS] ||
947 tb[TCA_FLOWER_KEY_MPLS_TC] ||
948 tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
949 NL_SET_ERR_MSG_ATTR(extack,
950 tb[TCA_FLOWER_KEY_MPLS_OPTS],
951 "MPLS label, Traffic Class, Bottom Of Stack and Time To Live must be encapsulated in the MPLS options attribute");
955 return fl_set_key_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS],
956 key_val, key_mask, extack);
959 lse_val = &key_val->ls[0];
960 lse_mask = &key_mask->ls[0];
962 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
963 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
964 lse_mask->mpls_ttl = MPLS_TTL_MASK;
965 dissector_set_mpls_lse(key_val, 0);
966 dissector_set_mpls_lse(key_mask, 0);
968 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
969 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
971 if (bos & ~MPLS_BOS_MASK) {
972 NL_SET_ERR_MSG_ATTR(extack,
973 tb[TCA_FLOWER_KEY_MPLS_BOS],
974 "Bottom Of Stack (BOS) must be 0 or 1");
977 lse_val->mpls_bos = bos;
978 lse_mask->mpls_bos = MPLS_BOS_MASK;
979 dissector_set_mpls_lse(key_val, 0);
980 dissector_set_mpls_lse(key_mask, 0);
982 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
983 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
985 if (tc & ~MPLS_TC_MASK) {
986 NL_SET_ERR_MSG_ATTR(extack,
987 tb[TCA_FLOWER_KEY_MPLS_TC],
988 "Traffic Class (TC) must be between 0 and 7");
991 lse_val->mpls_tc = tc;
992 lse_mask->mpls_tc = MPLS_TC_MASK;
993 dissector_set_mpls_lse(key_val, 0);
994 dissector_set_mpls_lse(key_mask, 0);
996 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
997 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
999 if (label & ~MPLS_LABEL_MASK) {
1000 NL_SET_ERR_MSG_ATTR(extack,
1001 tb[TCA_FLOWER_KEY_MPLS_LABEL],
1002 "Label must be between 0 and 1048575");
1005 lse_val->mpls_label = label;
1006 lse_mask->mpls_label = MPLS_LABEL_MASK;
1007 dissector_set_mpls_lse(key_val, 0);
1008 dissector_set_mpls_lse(key_mask, 0);
1013 static void fl_set_key_vlan(struct nlattr **tb,
1015 int vlan_id_key, int vlan_prio_key,
1016 int vlan_next_eth_type_key,
1017 struct flow_dissector_key_vlan *key_val,
1018 struct flow_dissector_key_vlan *key_mask)
1020 #define VLAN_PRIORITY_MASK 0x7
1022 if (tb[vlan_id_key]) {
1024 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
1025 key_mask->vlan_id = VLAN_VID_MASK;
1027 if (tb[vlan_prio_key]) {
1028 key_val->vlan_priority =
1029 nla_get_u8(tb[vlan_prio_key]) &
1031 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
1034 key_val->vlan_tpid = ethertype;
1035 key_mask->vlan_tpid = cpu_to_be16(~0);
1037 if (tb[vlan_next_eth_type_key]) {
1038 key_val->vlan_eth_type =
1039 nla_get_be16(tb[vlan_next_eth_type_key]);
1040 key_mask->vlan_eth_type = cpu_to_be16(~0);
1044 static void fl_set_key_pppoe(struct nlattr **tb,
1045 struct flow_dissector_key_pppoe *key_val,
1046 struct flow_dissector_key_pppoe *key_mask,
1047 struct fl_flow_key *key,
1048 struct fl_flow_key *mask)
1050 /* key_val::type must be set to ETH_P_PPP_SES
1051 * because ETH_P_PPP_SES was stored in basic.n_proto
1052 * which might get overwritten by ppp_proto
1053 * or might be set to 0, the role of key_val::type
1054 * is simmilar to vlan_key::tpid
1056 key_val->type = htons(ETH_P_PPP_SES);
1057 key_mask->type = cpu_to_be16(~0);
1059 if (tb[TCA_FLOWER_KEY_PPPOE_SID]) {
1060 key_val->session_id =
1061 nla_get_be16(tb[TCA_FLOWER_KEY_PPPOE_SID]);
1062 key_mask->session_id = cpu_to_be16(~0);
1064 if (tb[TCA_FLOWER_KEY_PPP_PROTO]) {
1065 key_val->ppp_proto =
1066 nla_get_be16(tb[TCA_FLOWER_KEY_PPP_PROTO]);
1067 key_mask->ppp_proto = cpu_to_be16(~0);
1069 if (key_val->ppp_proto == htons(PPP_IP)) {
1070 key->basic.n_proto = htons(ETH_P_IP);
1071 mask->basic.n_proto = cpu_to_be16(~0);
1072 } else if (key_val->ppp_proto == htons(PPP_IPV6)) {
1073 key->basic.n_proto = htons(ETH_P_IPV6);
1074 mask->basic.n_proto = cpu_to_be16(~0);
1075 } else if (key_val->ppp_proto == htons(PPP_MPLS_UC)) {
1076 key->basic.n_proto = htons(ETH_P_MPLS_UC);
1077 mask->basic.n_proto = cpu_to_be16(~0);
1078 } else if (key_val->ppp_proto == htons(PPP_MPLS_MC)) {
1079 key->basic.n_proto = htons(ETH_P_MPLS_MC);
1080 mask->basic.n_proto = cpu_to_be16(~0);
1083 key->basic.n_proto = 0;
1084 mask->basic.n_proto = cpu_to_be16(0);
1088 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
1089 u32 *dissector_key, u32 *dissector_mask,
1090 u32 flower_flag_bit, u32 dissector_flag_bit)
1092 if (flower_mask & flower_flag_bit) {
1093 *dissector_mask |= dissector_flag_bit;
1094 if (flower_key & flower_flag_bit)
1095 *dissector_key |= dissector_flag_bit;
1099 static int fl_set_key_flags(struct nlattr **tb, u32 *flags_key,
1100 u32 *flags_mask, struct netlink_ext_ack *extack)
1104 /* mask is mandatory for flags */
1105 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) {
1106 NL_SET_ERR_MSG(extack, "Missing flags mask");
1110 key = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS]));
1111 mask = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
1116 fl_set_key_flag(key, mask, flags_key, flags_mask,
1117 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1118 fl_set_key_flag(key, mask, flags_key, flags_mask,
1119 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1120 FLOW_DIS_FIRST_FRAG);
1125 static void fl_set_key_ip(struct nlattr **tb, bool encap,
1126 struct flow_dissector_key_ip *key,
1127 struct flow_dissector_key_ip *mask)
1129 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1130 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1131 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1132 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1134 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
1135 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
1138 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
1139 int depth, int option_len,
1140 struct netlink_ext_ack *extack)
1142 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
1143 struct nlattr *class = NULL, *type = NULL, *data = NULL;
1144 struct geneve_opt *opt;
1145 int err, data_len = 0;
1147 if (option_len > sizeof(struct geneve_opt))
1148 data_len = option_len - sizeof(struct geneve_opt);
1150 if (key->enc_opts.len > FLOW_DIS_TUN_OPTS_MAX - 4)
1153 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
1154 memset(opt, 0xff, option_len);
1155 opt->length = data_len / 4;
1160 /* If no mask has been prodived we assume an exact match. */
1162 return sizeof(struct geneve_opt) + data_len;
1164 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
1165 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
1169 err = nla_parse_nested_deprecated(tb,
1170 TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
1171 nla, geneve_opt_policy, extack);
1175 /* We are not allowed to omit any of CLASS, TYPE or DATA
1176 * fields from the key.
1179 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
1180 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
1181 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
1182 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
1186 /* Omitting any of CLASS, TYPE or DATA fields is allowed
1189 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
1190 int new_len = key->enc_opts.len;
1192 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
1193 data_len = nla_len(data);
1195 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
1199 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
1203 new_len += sizeof(struct geneve_opt) + data_len;
1204 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
1205 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
1206 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
1209 opt->length = data_len / 4;
1210 memcpy(opt->opt_data, nla_data(data), data_len);
1213 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
1214 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
1215 opt->opt_class = nla_get_be16(class);
1218 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
1219 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
1220 opt->type = nla_get_u8(type);
1223 return sizeof(struct geneve_opt) + data_len;
1226 static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1227 int depth, int option_len,
1228 struct netlink_ext_ack *extack)
1230 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1];
1231 struct vxlan_metadata *md;
1234 md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1235 memset(md, 0xff, sizeof(*md));
1240 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) {
1241 NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask");
1245 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla,
1246 vxlan_opt_policy, extack);
1250 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
1251 NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
1255 if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
1256 md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]);
1257 md->gbp &= VXLAN_GBP_MASK;
1263 static int fl_set_erspan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1264 int depth, int option_len,
1265 struct netlink_ext_ack *extack)
1267 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1];
1268 struct erspan_metadata *md;
1271 md = (struct erspan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1272 memset(md, 0xff, sizeof(*md));
1278 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_ERSPAN) {
1279 NL_SET_ERR_MSG(extack, "Non-erspan option type for mask");
1283 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, nla,
1284 erspan_opt_policy, extack);
1288 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) {
1289 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver");
1293 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER])
1294 md->version = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]);
1296 if (md->version == 1) {
1297 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1298 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index");
1301 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1302 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX];
1303 memset(&md->u, 0x00, sizeof(md->u));
1304 md->u.index = nla_get_be32(nla);
1306 } else if (md->version == 2) {
1307 if (!option_len && (!tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] ||
1308 !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID])) {
1309 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid");
1312 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]) {
1313 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR];
1314 md->u.md2.dir = nla_get_u8(nla);
1316 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]) {
1317 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID];
1318 set_hwid(&md->u.md2, nla_get_u8(nla));
1321 NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect");
1328 static int fl_set_gtp_opt(const struct nlattr *nla, struct fl_flow_key *key,
1329 int depth, int option_len,
1330 struct netlink_ext_ack *extack)
1332 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1];
1333 struct gtp_pdu_session_info *sinfo;
1334 u8 len = key->enc_opts.len;
1337 sinfo = (struct gtp_pdu_session_info *)&key->enc_opts.data[len];
1338 memset(sinfo, 0xff, option_len);
1341 return sizeof(*sinfo);
1343 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GTP) {
1344 NL_SET_ERR_MSG_MOD(extack, "Non-gtp option type for mask");
1348 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GTP_MAX, nla,
1349 gtp_opt_policy, extack);
1354 (!tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] ||
1355 !tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])) {
1356 NL_SET_ERR_MSG_MOD(extack,
1357 "Missing tunnel key gtp option pdu type or qfi");
1361 if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE])
1363 nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE]);
1365 if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])
1366 sinfo->qfi = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI]);
1368 return sizeof(*sinfo);
1371 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
1372 struct fl_flow_key *mask,
1373 struct netlink_ext_ack *extack)
1375 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
1376 int err, option_len, key_depth, msk_depth = 0;
1378 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
1379 TCA_FLOWER_KEY_ENC_OPTS_MAX,
1380 enc_opts_policy, extack);
1384 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
1386 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
1387 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
1388 TCA_FLOWER_KEY_ENC_OPTS_MAX,
1389 enc_opts_policy, extack);
1393 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
1394 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
1395 if (!nla_ok(nla_opt_msk, msk_depth)) {
1396 NL_SET_ERR_MSG(extack, "Invalid nested attribute for masks");
1401 nla_for_each_attr(nla_opt_key, nla_enc_key,
1402 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
1403 switch (nla_type(nla_opt_key)) {
1404 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
1405 if (key->enc_opts.dst_opt_type &&
1406 key->enc_opts.dst_opt_type != TUNNEL_GENEVE_OPT) {
1407 NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
1411 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
1412 option_len = fl_set_geneve_opt(nla_opt_key, key,
1413 key_depth, option_len,
1418 key->enc_opts.len += option_len;
1419 /* At the same time we need to parse through the mask
1420 * in order to verify exact and mask attribute lengths.
1422 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
1423 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
1424 msk_depth, option_len,
1429 mask->enc_opts.len += option_len;
1430 if (key->enc_opts.len != mask->enc_opts.len) {
1431 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1435 case TCA_FLOWER_KEY_ENC_OPTS_VXLAN:
1436 if (key->enc_opts.dst_opt_type) {
1437 NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
1441 key->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
1442 option_len = fl_set_vxlan_opt(nla_opt_key, key,
1443 key_depth, option_len,
1448 key->enc_opts.len += option_len;
1449 /* At the same time we need to parse through the mask
1450 * in order to verify exact and mask attribute lengths.
1452 mask->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
1453 option_len = fl_set_vxlan_opt(nla_opt_msk, mask,
1454 msk_depth, option_len,
1459 mask->enc_opts.len += option_len;
1460 if (key->enc_opts.len != mask->enc_opts.len) {
1461 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1465 case TCA_FLOWER_KEY_ENC_OPTS_ERSPAN:
1466 if (key->enc_opts.dst_opt_type) {
1467 NL_SET_ERR_MSG(extack, "Duplicate type for erspan options");
1471 key->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT;
1472 option_len = fl_set_erspan_opt(nla_opt_key, key,
1473 key_depth, option_len,
1478 key->enc_opts.len += option_len;
1479 /* At the same time we need to parse through the mask
1480 * in order to verify exact and mask attribute lengths.
1482 mask->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT;
1483 option_len = fl_set_erspan_opt(nla_opt_msk, mask,
1484 msk_depth, option_len,
1489 mask->enc_opts.len += option_len;
1490 if (key->enc_opts.len != mask->enc_opts.len) {
1491 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1495 case TCA_FLOWER_KEY_ENC_OPTS_GTP:
1496 if (key->enc_opts.dst_opt_type) {
1497 NL_SET_ERR_MSG_MOD(extack,
1498 "Duplicate type for gtp options");
1502 key->enc_opts.dst_opt_type = TUNNEL_GTP_OPT;
1503 option_len = fl_set_gtp_opt(nla_opt_key, key,
1504 key_depth, option_len,
1509 key->enc_opts.len += option_len;
1510 /* At the same time we need to parse through the mask
1511 * in order to verify exact and mask attribute lengths.
1513 mask->enc_opts.dst_opt_type = TUNNEL_GTP_OPT;
1514 option_len = fl_set_gtp_opt(nla_opt_msk, mask,
1515 msk_depth, option_len,
1520 mask->enc_opts.len += option_len;
1521 if (key->enc_opts.len != mask->enc_opts.len) {
1522 NL_SET_ERR_MSG_MOD(extack,
1523 "Key and mask miss aligned");
1528 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
1535 if (!nla_ok(nla_opt_msk, msk_depth)) {
1536 NL_SET_ERR_MSG(extack, "A mask attribute is invalid");
1539 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
1545 static int fl_validate_ct_state(u16 state, struct nlattr *tb,
1546 struct netlink_ext_ack *extack)
1548 if (state && !(state & TCA_FLOWER_KEY_CT_FLAGS_TRACKED)) {
1549 NL_SET_ERR_MSG_ATTR(extack, tb,
1550 "no trk, so no other flag can be set");
1554 if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
1555 state & TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED) {
1556 NL_SET_ERR_MSG_ATTR(extack, tb,
1557 "new and est are mutually exclusive");
1561 if (state & TCA_FLOWER_KEY_CT_FLAGS_INVALID &&
1562 state & ~(TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
1563 TCA_FLOWER_KEY_CT_FLAGS_INVALID)) {
1564 NL_SET_ERR_MSG_ATTR(extack, tb,
1565 "when inv is set, only trk may be set");
1569 if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
1570 state & TCA_FLOWER_KEY_CT_FLAGS_REPLY) {
1571 NL_SET_ERR_MSG_ATTR(extack, tb,
1572 "new and rpl are mutually exclusive");
1579 static int fl_set_key_ct(struct nlattr **tb,
1580 struct flow_dissector_key_ct *key,
1581 struct flow_dissector_key_ct *mask,
1582 struct netlink_ext_ack *extack)
1584 if (tb[TCA_FLOWER_KEY_CT_STATE]) {
1587 if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) {
1588 NL_SET_ERR_MSG(extack, "Conntrack isn't enabled");
1591 fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
1592 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
1593 sizeof(key->ct_state));
1595 err = fl_validate_ct_state(key->ct_state & mask->ct_state,
1596 tb[TCA_FLOWER_KEY_CT_STATE_MASK],
1602 if (tb[TCA_FLOWER_KEY_CT_ZONE]) {
1603 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1604 NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled");
1607 fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
1608 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
1609 sizeof(key->ct_zone));
1611 if (tb[TCA_FLOWER_KEY_CT_MARK]) {
1612 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1613 NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled");
1616 fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
1617 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
1618 sizeof(key->ct_mark));
1620 if (tb[TCA_FLOWER_KEY_CT_LABELS]) {
1621 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1622 NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled");
1625 fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
1626 mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
1627 sizeof(key->ct_labels));
1633 static bool is_vlan_key(struct nlattr *tb, __be16 *ethertype,
1634 struct fl_flow_key *key, struct fl_flow_key *mask,
1637 const bool good_num_of_vlans = key->num_of_vlans.num_of_vlans > vthresh;
1641 return good_num_of_vlans;
1644 *ethertype = nla_get_be16(tb);
1645 if (good_num_of_vlans || eth_type_vlan(*ethertype))
1648 key->basic.n_proto = *ethertype;
1649 mask->basic.n_proto = cpu_to_be16(~0);
1653 static int fl_set_key(struct net *net, struct nlattr **tb,
1654 struct fl_flow_key *key, struct fl_flow_key *mask,
1655 struct netlink_ext_ack *extack)
1660 if (tb[TCA_FLOWER_INDEV]) {
1661 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
1664 key->meta.ingress_ifindex = err;
1665 mask->meta.ingress_ifindex = 0xffffffff;
1668 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1669 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1670 sizeof(key->eth.dst));
1671 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1672 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1673 sizeof(key->eth.src));
1674 fl_set_key_val(tb, &key->num_of_vlans,
1675 TCA_FLOWER_KEY_NUM_OF_VLANS,
1676 &mask->num_of_vlans,
1678 sizeof(key->num_of_vlans));
1680 if (is_vlan_key(tb[TCA_FLOWER_KEY_ETH_TYPE], ðertype, key, mask, 0)) {
1681 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
1682 TCA_FLOWER_KEY_VLAN_PRIO,
1683 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1684 &key->vlan, &mask->vlan);
1686 if (is_vlan_key(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE],
1687 ðertype, key, mask, 1)) {
1688 fl_set_key_vlan(tb, ethertype,
1689 TCA_FLOWER_KEY_CVLAN_ID,
1690 TCA_FLOWER_KEY_CVLAN_PRIO,
1691 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1692 &key->cvlan, &mask->cvlan);
1693 fl_set_key_val(tb, &key->basic.n_proto,
1694 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1695 &mask->basic.n_proto,
1697 sizeof(key->basic.n_proto));
1701 if (key->basic.n_proto == htons(ETH_P_PPP_SES))
1702 fl_set_key_pppoe(tb, &key->pppoe, &mask->pppoe, key, mask);
1704 if (key->basic.n_proto == htons(ETH_P_IP) ||
1705 key->basic.n_proto == htons(ETH_P_IPV6)) {
1706 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1707 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1708 sizeof(key->basic.ip_proto));
1709 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
1712 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1713 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1714 mask->control.addr_type = ~0;
1715 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1716 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1717 sizeof(key->ipv4.src));
1718 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1719 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1720 sizeof(key->ipv4.dst));
1721 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1722 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1723 mask->control.addr_type = ~0;
1724 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1725 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1726 sizeof(key->ipv6.src));
1727 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1728 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1729 sizeof(key->ipv6.dst));
1732 if (key->basic.ip_proto == IPPROTO_TCP) {
1733 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1734 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1735 sizeof(key->tp.src));
1736 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1737 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1738 sizeof(key->tp.dst));
1739 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1740 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1741 sizeof(key->tcp.flags));
1742 } else if (key->basic.ip_proto == IPPROTO_UDP) {
1743 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1744 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1745 sizeof(key->tp.src));
1746 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1747 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1748 sizeof(key->tp.dst));
1749 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
1750 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1751 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1752 sizeof(key->tp.src));
1753 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1754 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1755 sizeof(key->tp.dst));
1756 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
1757 key->basic.ip_proto == IPPROTO_ICMP) {
1758 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1760 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1761 sizeof(key->icmp.type));
1762 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1764 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1765 sizeof(key->icmp.code));
1766 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1767 key->basic.ip_proto == IPPROTO_ICMPV6) {
1768 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1770 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1771 sizeof(key->icmp.type));
1772 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
1774 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1775 sizeof(key->icmp.code));
1776 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1777 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
1778 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack);
1781 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
1782 key->basic.n_proto == htons(ETH_P_RARP)) {
1783 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
1784 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
1785 sizeof(key->arp.sip));
1786 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
1787 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
1788 sizeof(key->arp.tip));
1789 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
1790 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
1791 sizeof(key->arp.op));
1792 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1793 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1794 sizeof(key->arp.sha));
1795 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1796 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1797 sizeof(key->arp.tha));
1798 } else if (key->basic.ip_proto == IPPROTO_L2TP) {
1799 fl_set_key_val(tb, &key->l2tpv3.session_id,
1800 TCA_FLOWER_KEY_L2TPV3_SID,
1801 &mask->l2tpv3.session_id, TCA_FLOWER_UNSPEC,
1802 sizeof(key->l2tpv3.session_id));
1805 if (key->basic.ip_proto == IPPROTO_TCP ||
1806 key->basic.ip_proto == IPPROTO_UDP ||
1807 key->basic.ip_proto == IPPROTO_SCTP) {
1808 ret = fl_set_key_port_range(tb, key, mask, extack);
1813 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
1814 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
1815 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1816 mask->enc_control.addr_type = ~0;
1817 fl_set_key_val(tb, &key->enc_ipv4.src,
1818 TCA_FLOWER_KEY_ENC_IPV4_SRC,
1819 &mask->enc_ipv4.src,
1820 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1821 sizeof(key->enc_ipv4.src));
1822 fl_set_key_val(tb, &key->enc_ipv4.dst,
1823 TCA_FLOWER_KEY_ENC_IPV4_DST,
1824 &mask->enc_ipv4.dst,
1825 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1826 sizeof(key->enc_ipv4.dst));
1829 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
1830 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
1831 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1832 mask->enc_control.addr_type = ~0;
1833 fl_set_key_val(tb, &key->enc_ipv6.src,
1834 TCA_FLOWER_KEY_ENC_IPV6_SRC,
1835 &mask->enc_ipv6.src,
1836 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1837 sizeof(key->enc_ipv6.src));
1838 fl_set_key_val(tb, &key->enc_ipv6.dst,
1839 TCA_FLOWER_KEY_ENC_IPV6_DST,
1840 &mask->enc_ipv6.dst,
1841 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1842 sizeof(key->enc_ipv6.dst));
1845 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
1846 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
1847 sizeof(key->enc_key_id.keyid));
1849 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1850 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1851 sizeof(key->enc_tp.src));
1853 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1854 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1855 sizeof(key->enc_tp.dst));
1857 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
1859 fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
1860 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
1861 sizeof(key->hash.hash));
1863 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
1864 ret = fl_set_enc_opt(tb, key, mask, extack);
1869 ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack);
1873 if (tb[TCA_FLOWER_KEY_FLAGS])
1874 ret = fl_set_key_flags(tb, &key->control.flags,
1875 &mask->control.flags, extack);
1880 static void fl_mask_copy(struct fl_flow_mask *dst,
1881 struct fl_flow_mask *src)
1883 const void *psrc = fl_key_get_start(&src->key, src);
1884 void *pdst = fl_key_get_start(&dst->key, src);
1886 memcpy(pdst, psrc, fl_mask_range(src));
1887 dst->range = src->range;
1890 static const struct rhashtable_params fl_ht_params = {
1891 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
1892 .head_offset = offsetof(struct cls_fl_filter, ht_node),
1893 .automatic_shrinking = true,
1896 static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
1898 mask->filter_ht_params = fl_ht_params;
1899 mask->filter_ht_params.key_len = fl_mask_range(mask);
1900 mask->filter_ht_params.key_offset += mask->range.start;
1902 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
1905 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
1906 #define FL_KEY_MEMBER_SIZE(member) sizeof_field(struct fl_flow_key, member)
1908 #define FL_KEY_IS_MASKED(mask, member) \
1909 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
1910 0, FL_KEY_MEMBER_SIZE(member)) \
1912 #define FL_KEY_SET(keys, cnt, id, member) \
1914 keys[cnt].key_id = id; \
1915 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
1919 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
1921 if (FL_KEY_IS_MASKED(mask, member)) \
1922 FL_KEY_SET(keys, cnt, id, member); \
1925 static void fl_init_dissector(struct flow_dissector *dissector,
1926 struct fl_flow_key *mask)
1928 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1931 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1932 FLOW_DISSECTOR_KEY_META, meta);
1933 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
1934 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
1935 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1936 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
1937 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1938 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
1939 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1940 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
1941 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1942 FLOW_DISSECTOR_KEY_PORTS, tp);
1943 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1944 FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range);
1945 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1946 FLOW_DISSECTOR_KEY_IP, ip);
1947 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1948 FLOW_DISSECTOR_KEY_TCP, tcp);
1949 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1950 FLOW_DISSECTOR_KEY_ICMP, icmp);
1951 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1952 FLOW_DISSECTOR_KEY_ARP, arp);
1953 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1954 FLOW_DISSECTOR_KEY_MPLS, mpls);
1955 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1956 FLOW_DISSECTOR_KEY_VLAN, vlan);
1957 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1958 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
1959 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1960 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
1961 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1962 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
1963 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1964 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
1965 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1966 FL_KEY_IS_MASKED(mask, enc_ipv6))
1967 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1969 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1970 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
1971 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1972 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
1973 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1974 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
1975 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1976 FLOW_DISSECTOR_KEY_CT, ct);
1977 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1978 FLOW_DISSECTOR_KEY_HASH, hash);
1979 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1980 FLOW_DISSECTOR_KEY_NUM_OF_VLANS, num_of_vlans);
1981 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1982 FLOW_DISSECTOR_KEY_PPPOE, pppoe);
1983 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1984 FLOW_DISSECTOR_KEY_L2TPV3, l2tpv3);
1986 skb_flow_dissector_init(dissector, keys, cnt);
1989 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1990 struct fl_flow_mask *mask)
1992 struct fl_flow_mask *newmask;
1995 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1997 return ERR_PTR(-ENOMEM);
1999 fl_mask_copy(newmask, mask);
2001 if ((newmask->key.tp_range.tp_min.dst &&
2002 newmask->key.tp_range.tp_max.dst) ||
2003 (newmask->key.tp_range.tp_min.src &&
2004 newmask->key.tp_range.tp_max.src))
2005 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
2007 err = fl_init_mask_hashtable(newmask);
2011 fl_init_dissector(&newmask->dissector, &newmask->key);
2013 INIT_LIST_HEAD_RCU(&newmask->filters);
2015 refcount_set(&newmask->refcnt, 1);
2016 err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
2017 &newmask->ht_node, mask_ht_params);
2019 goto errout_destroy;
2021 spin_lock(&head->masks_lock);
2022 list_add_tail_rcu(&newmask->list, &head->masks);
2023 spin_unlock(&head->masks_lock);
2028 rhashtable_destroy(&newmask->ht);
2032 return ERR_PTR(err);
2035 static int fl_check_assign_mask(struct cls_fl_head *head,
2036 struct cls_fl_filter *fnew,
2037 struct cls_fl_filter *fold,
2038 struct fl_flow_mask *mask)
2040 struct fl_flow_mask *newmask;
2045 /* Insert mask as temporary node to prevent concurrent creation of mask
2046 * with same key. Any concurrent lookups with same key will return
2047 * -EAGAIN because mask's refcnt is zero.
2049 fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
2057 goto errout_cleanup;
2060 newmask = fl_create_new_mask(head, mask);
2061 if (IS_ERR(newmask)) {
2062 ret = PTR_ERR(newmask);
2063 goto errout_cleanup;
2066 fnew->mask = newmask;
2068 } else if (IS_ERR(fnew->mask)) {
2069 ret = PTR_ERR(fnew->mask);
2070 } else if (fold && fold->mask != fnew->mask) {
2072 } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
2073 /* Mask was deleted concurrently, try again */
2080 rhashtable_remove_fast(&head->ht, &mask->ht_node,
2085 static int fl_set_parms(struct net *net, struct tcf_proto *tp,
2086 struct cls_fl_filter *f, struct fl_flow_mask *mask,
2087 unsigned long base, struct nlattr **tb,
2089 struct fl_flow_tmplt *tmplt,
2090 u32 flags, u32 fl_flags,
2091 struct netlink_ext_ack *extack)
2095 err = tcf_exts_validate_ex(net, tp, tb, est, &f->exts, flags,
2100 if (tb[TCA_FLOWER_CLASSID]) {
2101 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
2102 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2104 tcf_bind_filter(tp, &f->res, base);
2105 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2109 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
2113 fl_mask_update_range(mask);
2114 fl_set_masked_key(&f->mkey, &f->key, mask);
2116 if (!fl_mask_fits_tmplt(tmplt, mask)) {
2117 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
2124 static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
2125 struct cls_fl_filter *fold,
2128 struct fl_flow_mask *mask = fnew->mask;
2131 err = rhashtable_lookup_insert_fast(&mask->ht,
2133 mask->filter_ht_params);
2136 /* It is okay if filter with same key exists when
2139 return fold && err == -EEXIST ? 0 : err;
2146 static int fl_change(struct net *net, struct sk_buff *in_skb,
2147 struct tcf_proto *tp, unsigned long base,
2148 u32 handle, struct nlattr **tca,
2149 void **arg, u32 flags,
2150 struct netlink_ext_ack *extack)
2152 struct cls_fl_head *head = fl_head_dereference(tp);
2153 bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL);
2154 struct cls_fl_filter *fold = *arg;
2155 struct cls_fl_filter *fnew;
2156 struct fl_flow_mask *mask;
2161 if (!tca[TCA_OPTIONS]) {
2166 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
2172 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2175 goto errout_mask_alloc;
2178 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
2179 tca[TCA_OPTIONS], fl_policy, NULL);
2183 if (fold && handle && fold->handle != handle) {
2188 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
2193 INIT_LIST_HEAD(&fnew->hw_list);
2194 refcount_set(&fnew->refcnt, 1);
2196 err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0);
2200 if (tb[TCA_FLOWER_FLAGS]) {
2201 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
2203 if (!tc_flags_valid(fnew->flags)) {
2209 err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE],
2210 tp->chain->tmplt_priv, flags, fnew->flags,
2215 err = fl_check_assign_mask(head, fnew, fold, mask);
2219 err = fl_ht_insert_unique(fnew, fold, &in_ht);
2223 if (!tc_skip_hw(fnew->flags)) {
2224 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
2229 if (!tc_in_hw(fnew->flags))
2230 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
2232 spin_lock(&tp->lock);
2234 /* tp was deleted concurrently. -EAGAIN will cause caller to lookup
2235 * proto again or create new one, if necessary.
2243 /* Fold filter was deleted concurrently. Retry lookup. */
2244 if (fold->deleted) {
2249 fnew->handle = handle;
2252 struct rhashtable_params params =
2253 fnew->mask->filter_ht_params;
2255 err = rhashtable_insert_fast(&fnew->mask->ht,
2263 refcount_inc(&fnew->refcnt);
2264 rhashtable_remove_fast(&fold->mask->ht,
2266 fold->mask->filter_ht_params);
2267 idr_replace(&head->handle_idr, fnew, fnew->handle);
2268 list_replace_rcu(&fold->list, &fnew->list);
2269 fold->deleted = true;
2271 spin_unlock(&tp->lock);
2273 fl_mask_put(head, fold->mask);
2274 if (!tc_skip_hw(fold->flags))
2275 fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
2276 tcf_unbind_filter(tp, &fold->res);
2277 /* Caller holds reference to fold, so refcnt is always > 0
2280 refcount_dec(&fold->refcnt);
2284 /* user specifies a handle and it doesn't exist */
2285 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
2286 handle, GFP_ATOMIC);
2288 /* Filter with specified handle was concurrently
2289 * inserted after initial check in cls_api. This is not
2290 * necessarily an error if NLM_F_EXCL is not set in
2291 * message flags. Returning EAGAIN will cause cls_api to
2292 * try to update concurrently inserted rule.
2298 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
2299 INT_MAX, GFP_ATOMIC);
2304 refcount_inc(&fnew->refcnt);
2305 fnew->handle = handle;
2306 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
2307 spin_unlock(&tp->lock);
2313 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
2317 spin_lock(&tp->lock);
2319 fnew->deleted = true;
2320 spin_unlock(&tp->lock);
2321 if (!tc_skip_hw(fnew->flags))
2322 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
2324 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
2325 fnew->mask->filter_ht_params);
2327 fl_mask_put(head, fnew->mask);
2333 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
2340 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
2341 bool rtnl_held, struct netlink_ext_ack *extack)
2343 struct cls_fl_head *head = fl_head_dereference(tp);
2344 struct cls_fl_filter *f = arg;
2348 err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
2349 *last = list_empty(&head->masks);
2355 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
2358 struct cls_fl_head *head = fl_head_dereference(tp);
2359 unsigned long id = arg->cookie, tmp;
2360 struct cls_fl_filter *f;
2362 arg->count = arg->skip;
2365 idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) {
2366 /* don't return filters that are being deleted */
2367 if (!refcount_inc_not_zero(&f->refcnt))
2371 if (arg->fn(tp, f, arg) < 0) {
2385 static struct cls_fl_filter *
2386 fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
2388 struct cls_fl_head *head = fl_head_dereference(tp);
2390 spin_lock(&tp->lock);
2391 if (list_empty(&head->hw_filters)) {
2392 spin_unlock(&tp->lock);
2397 f = list_entry(&head->hw_filters, struct cls_fl_filter,
2399 list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
2400 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
2401 spin_unlock(&tp->lock);
2406 spin_unlock(&tp->lock);
2410 static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
2411 void *cb_priv, struct netlink_ext_ack *extack)
2413 struct tcf_block *block = tp->chain->block;
2414 struct flow_cls_offload cls_flower = {};
2415 struct cls_fl_filter *f = NULL;
2418 /* hw_filters list can only be changed by hw offload functions after
2419 * obtaining rtnl lock. Make sure it is not changed while reoffload is
2424 while ((f = fl_get_next_hw_filter(tp, f, add))) {
2426 flow_rule_alloc(tcf_exts_num_actions(&f->exts));
2427 if (!cls_flower.rule) {
2432 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
2434 cls_flower.command = add ?
2435 FLOW_CLS_REPLACE : FLOW_CLS_DESTROY;
2436 cls_flower.cookie = (unsigned long)f;
2437 cls_flower.rule->match.dissector = &f->mask->dissector;
2438 cls_flower.rule->match.mask = &f->mask->key;
2439 cls_flower.rule->match.key = &f->mkey;
2441 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts,
2442 cls_flower.common.extack);
2444 kfree(cls_flower.rule);
2445 if (tc_skip_sw(f->flags)) {
2452 cls_flower.classid = f->res.classid;
2454 err = tc_setup_cb_reoffload(block, tp, add, cb,
2455 TC_SETUP_CLSFLOWER, &cls_flower,
2458 tc_cleanup_offload_action(&cls_flower.rule->action);
2459 kfree(cls_flower.rule);
2472 static void fl_hw_add(struct tcf_proto *tp, void *type_data)
2474 struct flow_cls_offload *cls_flower = type_data;
2475 struct cls_fl_filter *f =
2476 (struct cls_fl_filter *) cls_flower->cookie;
2477 struct cls_fl_head *head = fl_head_dereference(tp);
2479 spin_lock(&tp->lock);
2480 list_add(&f->hw_list, &head->hw_filters);
2481 spin_unlock(&tp->lock);
2484 static void fl_hw_del(struct tcf_proto *tp, void *type_data)
2486 struct flow_cls_offload *cls_flower = type_data;
2487 struct cls_fl_filter *f =
2488 (struct cls_fl_filter *) cls_flower->cookie;
2490 spin_lock(&tp->lock);
2491 if (!list_empty(&f->hw_list))
2492 list_del_init(&f->hw_list);
2493 spin_unlock(&tp->lock);
2496 static int fl_hw_create_tmplt(struct tcf_chain *chain,
2497 struct fl_flow_tmplt *tmplt)
2499 struct flow_cls_offload cls_flower = {};
2500 struct tcf_block *block = chain->block;
2502 cls_flower.rule = flow_rule_alloc(0);
2503 if (!cls_flower.rule)
2506 cls_flower.common.chain_index = chain->index;
2507 cls_flower.command = FLOW_CLS_TMPLT_CREATE;
2508 cls_flower.cookie = (unsigned long) tmplt;
2509 cls_flower.rule->match.dissector = &tmplt->dissector;
2510 cls_flower.rule->match.mask = &tmplt->mask;
2511 cls_flower.rule->match.key = &tmplt->dummy_key;
2513 /* We don't care if driver (any of them) fails to handle this
2514 * call. It serves just as a hint for it.
2516 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
2517 kfree(cls_flower.rule);
2522 static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
2523 struct fl_flow_tmplt *tmplt)
2525 struct flow_cls_offload cls_flower = {};
2526 struct tcf_block *block = chain->block;
2528 cls_flower.common.chain_index = chain->index;
2529 cls_flower.command = FLOW_CLS_TMPLT_DESTROY;
2530 cls_flower.cookie = (unsigned long) tmplt;
2532 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
2535 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
2536 struct nlattr **tca,
2537 struct netlink_ext_ack *extack)
2539 struct fl_flow_tmplt *tmplt;
2543 if (!tca[TCA_OPTIONS])
2544 return ERR_PTR(-EINVAL);
2546 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2548 return ERR_PTR(-ENOBUFS);
2549 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
2550 tca[TCA_OPTIONS], fl_policy, NULL);
2554 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
2559 tmplt->chain = chain;
2560 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
2564 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
2566 err = fl_hw_create_tmplt(chain, tmplt);
2577 return ERR_PTR(err);
2580 static void fl_tmplt_destroy(void *tmplt_priv)
2582 struct fl_flow_tmplt *tmplt = tmplt_priv;
2584 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
2588 static int fl_dump_key_val(struct sk_buff *skb,
2589 void *val, int val_type,
2590 void *mask, int mask_type, int len)
2594 if (!memchr_inv(mask, 0, len))
2596 err = nla_put(skb, val_type, len, val);
2599 if (mask_type != TCA_FLOWER_UNSPEC) {
2600 err = nla_put(skb, mask_type, len, mask);
2607 static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
2608 struct fl_flow_key *mask)
2610 if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst,
2611 TCA_FLOWER_KEY_PORT_DST_MIN,
2612 &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC,
2613 sizeof(key->tp_range.tp_min.dst)) ||
2614 fl_dump_key_val(skb, &key->tp_range.tp_max.dst,
2615 TCA_FLOWER_KEY_PORT_DST_MAX,
2616 &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC,
2617 sizeof(key->tp_range.tp_max.dst)) ||
2618 fl_dump_key_val(skb, &key->tp_range.tp_min.src,
2619 TCA_FLOWER_KEY_PORT_SRC_MIN,
2620 &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC,
2621 sizeof(key->tp_range.tp_min.src)) ||
2622 fl_dump_key_val(skb, &key->tp_range.tp_max.src,
2623 TCA_FLOWER_KEY_PORT_SRC_MAX,
2624 &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC,
2625 sizeof(key->tp_range.tp_max.src)))
2631 static int fl_dump_key_mpls_opt_lse(struct sk_buff *skb,
2632 struct flow_dissector_key_mpls *mpls_key,
2633 struct flow_dissector_key_mpls *mpls_mask,
2636 struct flow_dissector_mpls_lse *lse_mask = &mpls_mask->ls[lse_index];
2637 struct flow_dissector_mpls_lse *lse_key = &mpls_key->ls[lse_index];
2640 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH,
2645 if (lse_mask->mpls_ttl) {
2646 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL,
2651 if (lse_mask->mpls_bos) {
2652 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS,
2657 if (lse_mask->mpls_tc) {
2658 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC,
2663 if (lse_mask->mpls_label) {
2664 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL,
2665 lse_key->mpls_label);
2673 static int fl_dump_key_mpls_opts(struct sk_buff *skb,
2674 struct flow_dissector_key_mpls *mpls_key,
2675 struct flow_dissector_key_mpls *mpls_mask)
2677 struct nlattr *opts;
2682 opts = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS);
2686 for (lse_index = 0; lse_index < FLOW_DIS_MPLS_MAX; lse_index++) {
2687 if (!(mpls_mask->used_lses & 1 << lse_index))
2690 lse = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS_LSE);
2696 err = fl_dump_key_mpls_opt_lse(skb, mpls_key, mpls_mask,
2700 nla_nest_end(skb, lse);
2702 nla_nest_end(skb, opts);
2707 nla_nest_cancel(skb, lse);
2709 nla_nest_cancel(skb, opts);
2714 static int fl_dump_key_mpls(struct sk_buff *skb,
2715 struct flow_dissector_key_mpls *mpls_key,
2716 struct flow_dissector_key_mpls *mpls_mask)
2718 struct flow_dissector_mpls_lse *lse_mask;
2719 struct flow_dissector_mpls_lse *lse_key;
2722 if (!mpls_mask->used_lses)
2725 lse_mask = &mpls_mask->ls[0];
2726 lse_key = &mpls_key->ls[0];
2728 /* For backward compatibility, don't use the MPLS nested attributes if
2729 * the rule can be expressed using the old attributes.
2731 if (mpls_mask->used_lses & ~1 ||
2732 (!lse_mask->mpls_ttl && !lse_mask->mpls_bos &&
2733 !lse_mask->mpls_tc && !lse_mask->mpls_label))
2734 return fl_dump_key_mpls_opts(skb, mpls_key, mpls_mask);
2736 if (lse_mask->mpls_ttl) {
2737 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
2742 if (lse_mask->mpls_tc) {
2743 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
2748 if (lse_mask->mpls_label) {
2749 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
2750 lse_key->mpls_label);
2754 if (lse_mask->mpls_bos) {
2755 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
2763 static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
2764 struct flow_dissector_key_ip *key,
2765 struct flow_dissector_key_ip *mask)
2767 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
2768 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
2769 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
2770 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
2772 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
2773 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
2779 static int fl_dump_key_vlan(struct sk_buff *skb,
2780 int vlan_id_key, int vlan_prio_key,
2781 struct flow_dissector_key_vlan *vlan_key,
2782 struct flow_dissector_key_vlan *vlan_mask)
2786 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
2788 if (vlan_mask->vlan_id) {
2789 err = nla_put_u16(skb, vlan_id_key,
2794 if (vlan_mask->vlan_priority) {
2795 err = nla_put_u8(skb, vlan_prio_key,
2796 vlan_key->vlan_priority);
2803 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
2804 u32 *flower_key, u32 *flower_mask,
2805 u32 flower_flag_bit, u32 dissector_flag_bit)
2807 if (dissector_mask & dissector_flag_bit) {
2808 *flower_mask |= flower_flag_bit;
2809 if (dissector_key & dissector_flag_bit)
2810 *flower_key |= flower_flag_bit;
2814 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
2820 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
2826 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2827 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
2828 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2829 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
2830 FLOW_DIS_FIRST_FRAG);
2832 _key = cpu_to_be32(key);
2833 _mask = cpu_to_be32(mask);
2835 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
2839 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
2842 static int fl_dump_key_geneve_opt(struct sk_buff *skb,
2843 struct flow_dissector_key_enc_opts *enc_opts)
2845 struct geneve_opt *opt;
2846 struct nlattr *nest;
2849 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
2851 goto nla_put_failure;
2853 while (enc_opts->len > opt_off) {
2854 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
2856 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
2858 goto nla_put_failure;
2859 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
2861 goto nla_put_failure;
2862 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
2863 opt->length * 4, opt->opt_data))
2864 goto nla_put_failure;
2866 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
2868 nla_nest_end(skb, nest);
2872 nla_nest_cancel(skb, nest);
2876 static int fl_dump_key_vxlan_opt(struct sk_buff *skb,
2877 struct flow_dissector_key_enc_opts *enc_opts)
2879 struct vxlan_metadata *md;
2880 struct nlattr *nest;
2882 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN);
2884 goto nla_put_failure;
2886 md = (struct vxlan_metadata *)&enc_opts->data[0];
2887 if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp))
2888 goto nla_put_failure;
2890 nla_nest_end(skb, nest);
2894 nla_nest_cancel(skb, nest);
2898 static int fl_dump_key_erspan_opt(struct sk_buff *skb,
2899 struct flow_dissector_key_enc_opts *enc_opts)
2901 struct erspan_metadata *md;
2902 struct nlattr *nest;
2904 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_ERSPAN);
2906 goto nla_put_failure;
2908 md = (struct erspan_metadata *)&enc_opts->data[0];
2909 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER, md->version))
2910 goto nla_put_failure;
2912 if (md->version == 1 &&
2913 nla_put_be32(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index))
2914 goto nla_put_failure;
2916 if (md->version == 2 &&
2917 (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR,
2919 nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID,
2920 get_hwid(&md->u.md2))))
2921 goto nla_put_failure;
2923 nla_nest_end(skb, nest);
2927 nla_nest_cancel(skb, nest);
2931 static int fl_dump_key_gtp_opt(struct sk_buff *skb,
2932 struct flow_dissector_key_enc_opts *enc_opts)
2935 struct gtp_pdu_session_info *session_info;
2936 struct nlattr *nest;
2938 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GTP);
2940 goto nla_put_failure;
2942 session_info = (struct gtp_pdu_session_info *)&enc_opts->data[0];
2944 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE,
2945 session_info->pdu_type))
2946 goto nla_put_failure;
2948 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_QFI, session_info->qfi))
2949 goto nla_put_failure;
2951 nla_nest_end(skb, nest);
2955 nla_nest_cancel(skb, nest);
2959 static int fl_dump_key_ct(struct sk_buff *skb,
2960 struct flow_dissector_key_ct *key,
2961 struct flow_dissector_key_ct *mask)
2963 if (IS_ENABLED(CONFIG_NF_CONNTRACK) &&
2964 fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
2965 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
2966 sizeof(key->ct_state)))
2967 goto nla_put_failure;
2969 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
2970 fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
2971 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
2972 sizeof(key->ct_zone)))
2973 goto nla_put_failure;
2975 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
2976 fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
2977 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
2978 sizeof(key->ct_mark)))
2979 goto nla_put_failure;
2981 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
2982 fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
2983 &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
2984 sizeof(key->ct_labels)))
2985 goto nla_put_failure;
2993 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
2994 struct flow_dissector_key_enc_opts *enc_opts)
2996 struct nlattr *nest;
3002 nest = nla_nest_start_noflag(skb, enc_opt_type);
3004 goto nla_put_failure;
3006 switch (enc_opts->dst_opt_type) {
3007 case TUNNEL_GENEVE_OPT:
3008 err = fl_dump_key_geneve_opt(skb, enc_opts);
3010 goto nla_put_failure;
3012 case TUNNEL_VXLAN_OPT:
3013 err = fl_dump_key_vxlan_opt(skb, enc_opts);
3015 goto nla_put_failure;
3017 case TUNNEL_ERSPAN_OPT:
3018 err = fl_dump_key_erspan_opt(skb, enc_opts);
3020 goto nla_put_failure;
3022 case TUNNEL_GTP_OPT:
3023 err = fl_dump_key_gtp_opt(skb, enc_opts);
3025 goto nla_put_failure;
3028 goto nla_put_failure;
3030 nla_nest_end(skb, nest);
3034 nla_nest_cancel(skb, nest);
3038 static int fl_dump_key_enc_opt(struct sk_buff *skb,
3039 struct flow_dissector_key_enc_opts *key_opts,
3040 struct flow_dissector_key_enc_opts *msk_opts)
3044 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
3048 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
3051 static int fl_dump_key(struct sk_buff *skb, struct net *net,
3052 struct fl_flow_key *key, struct fl_flow_key *mask)
3054 if (mask->meta.ingress_ifindex) {
3055 struct net_device *dev;
3057 dev = __dev_get_by_index(net, key->meta.ingress_ifindex);
3058 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
3059 goto nla_put_failure;
3062 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
3063 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
3064 sizeof(key->eth.dst)) ||
3065 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
3066 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
3067 sizeof(key->eth.src)) ||
3068 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
3069 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
3070 sizeof(key->basic.n_proto)))
3071 goto nla_put_failure;
3073 if (mask->num_of_vlans.num_of_vlans) {
3074 if (nla_put_u8(skb, TCA_FLOWER_KEY_NUM_OF_VLANS, key->num_of_vlans.num_of_vlans))
3075 goto nla_put_failure;
3078 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
3079 goto nla_put_failure;
3081 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
3082 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
3083 goto nla_put_failure;
3085 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
3086 TCA_FLOWER_KEY_CVLAN_PRIO,
3087 &key->cvlan, &mask->cvlan) ||
3088 (mask->cvlan.vlan_tpid &&
3089 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3090 key->cvlan.vlan_tpid)))
3091 goto nla_put_failure;
3093 if (mask->basic.n_proto) {
3094 if (mask->cvlan.vlan_eth_type) {
3095 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
3096 key->basic.n_proto))
3097 goto nla_put_failure;
3098 } else if (mask->vlan.vlan_eth_type) {
3099 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3100 key->vlan.vlan_eth_type))
3101 goto nla_put_failure;
3105 if ((key->basic.n_proto == htons(ETH_P_IP) ||
3106 key->basic.n_proto == htons(ETH_P_IPV6)) &&
3107 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
3108 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
3109 sizeof(key->basic.ip_proto)) ||
3110 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
3111 goto nla_put_failure;
3113 if (mask->pppoe.session_id) {
3114 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPPOE_SID,
3115 key->pppoe.session_id))
3116 goto nla_put_failure;
3118 if (mask->basic.n_proto && mask->pppoe.ppp_proto) {
3119 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPP_PROTO,
3120 key->pppoe.ppp_proto))
3121 goto nla_put_failure;
3124 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
3125 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
3126 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
3127 sizeof(key->ipv4.src)) ||
3128 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
3129 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
3130 sizeof(key->ipv4.dst))))
3131 goto nla_put_failure;
3132 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
3133 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
3134 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
3135 sizeof(key->ipv6.src)) ||
3136 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
3137 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
3138 sizeof(key->ipv6.dst))))
3139 goto nla_put_failure;
3141 if (key->basic.ip_proto == IPPROTO_TCP &&
3142 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
3143 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
3144 sizeof(key->tp.src)) ||
3145 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
3146 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
3147 sizeof(key->tp.dst)) ||
3148 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
3149 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
3150 sizeof(key->tcp.flags))))
3151 goto nla_put_failure;
3152 else if (key->basic.ip_proto == IPPROTO_UDP &&
3153 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
3154 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
3155 sizeof(key->tp.src)) ||
3156 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
3157 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
3158 sizeof(key->tp.dst))))
3159 goto nla_put_failure;
3160 else if (key->basic.ip_proto == IPPROTO_SCTP &&
3161 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
3162 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
3163 sizeof(key->tp.src)) ||
3164 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
3165 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
3166 sizeof(key->tp.dst))))
3167 goto nla_put_failure;
3168 else if (key->basic.n_proto == htons(ETH_P_IP) &&
3169 key->basic.ip_proto == IPPROTO_ICMP &&
3170 (fl_dump_key_val(skb, &key->icmp.type,
3171 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
3172 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
3173 sizeof(key->icmp.type)) ||
3174 fl_dump_key_val(skb, &key->icmp.code,
3175 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
3176 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
3177 sizeof(key->icmp.code))))
3178 goto nla_put_failure;
3179 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
3180 key->basic.ip_proto == IPPROTO_ICMPV6 &&
3181 (fl_dump_key_val(skb, &key->icmp.type,
3182 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
3183 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
3184 sizeof(key->icmp.type)) ||
3185 fl_dump_key_val(skb, &key->icmp.code,
3186 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
3187 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
3188 sizeof(key->icmp.code))))
3189 goto nla_put_failure;
3190 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
3191 key->basic.n_proto == htons(ETH_P_RARP)) &&
3192 (fl_dump_key_val(skb, &key->arp.sip,
3193 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
3194 TCA_FLOWER_KEY_ARP_SIP_MASK,
3195 sizeof(key->arp.sip)) ||
3196 fl_dump_key_val(skb, &key->arp.tip,
3197 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
3198 TCA_FLOWER_KEY_ARP_TIP_MASK,
3199 sizeof(key->arp.tip)) ||
3200 fl_dump_key_val(skb, &key->arp.op,
3201 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
3202 TCA_FLOWER_KEY_ARP_OP_MASK,
3203 sizeof(key->arp.op)) ||
3204 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
3205 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
3206 sizeof(key->arp.sha)) ||
3207 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
3208 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
3209 sizeof(key->arp.tha))))
3210 goto nla_put_failure;
3211 else if (key->basic.ip_proto == IPPROTO_L2TP &&
3212 fl_dump_key_val(skb, &key->l2tpv3.session_id,
3213 TCA_FLOWER_KEY_L2TPV3_SID,
3214 &mask->l2tpv3.session_id,
3216 sizeof(key->l2tpv3.session_id)))
3217 goto nla_put_failure;
3219 if ((key->basic.ip_proto == IPPROTO_TCP ||
3220 key->basic.ip_proto == IPPROTO_UDP ||
3221 key->basic.ip_proto == IPPROTO_SCTP) &&
3222 fl_dump_key_port_range(skb, key, mask))
3223 goto nla_put_failure;
3225 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
3226 (fl_dump_key_val(skb, &key->enc_ipv4.src,
3227 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
3228 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
3229 sizeof(key->enc_ipv4.src)) ||
3230 fl_dump_key_val(skb, &key->enc_ipv4.dst,
3231 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
3232 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
3233 sizeof(key->enc_ipv4.dst))))
3234 goto nla_put_failure;
3235 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
3236 (fl_dump_key_val(skb, &key->enc_ipv6.src,
3237 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
3238 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
3239 sizeof(key->enc_ipv6.src)) ||
3240 fl_dump_key_val(skb, &key->enc_ipv6.dst,
3241 TCA_FLOWER_KEY_ENC_IPV6_DST,
3242 &mask->enc_ipv6.dst,
3243 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
3244 sizeof(key->enc_ipv6.dst))))
3245 goto nla_put_failure;
3247 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
3248 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
3249 sizeof(key->enc_key_id)) ||
3250 fl_dump_key_val(skb, &key->enc_tp.src,
3251 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
3253 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
3254 sizeof(key->enc_tp.src)) ||
3255 fl_dump_key_val(skb, &key->enc_tp.dst,
3256 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
3258 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
3259 sizeof(key->enc_tp.dst)) ||
3260 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
3261 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
3262 goto nla_put_failure;
3264 if (fl_dump_key_ct(skb, &key->ct, &mask->ct))
3265 goto nla_put_failure;
3267 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
3268 goto nla_put_failure;
3270 if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
3271 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
3272 sizeof(key->hash.hash)))
3273 goto nla_put_failure;
3281 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
3282 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3284 struct cls_fl_filter *f = fh;
3285 struct nlattr *nest;
3286 struct fl_flow_key *key, *mask;
3292 t->tcm_handle = f->handle;
3294 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3296 goto nla_put_failure;
3298 spin_lock(&tp->lock);
3300 if (f->res.classid &&
3301 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
3302 goto nla_put_failure_locked;
3305 mask = &f->mask->key;
3306 skip_hw = tc_skip_hw(f->flags);
3308 if (fl_dump_key(skb, net, key, mask))
3309 goto nla_put_failure_locked;
3311 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3312 goto nla_put_failure_locked;
3314 spin_unlock(&tp->lock);
3317 fl_hw_update_stats(tp, f, rtnl_held);
3319 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
3320 goto nla_put_failure;
3322 if (tcf_exts_dump(skb, &f->exts))
3323 goto nla_put_failure;
3325 nla_nest_end(skb, nest);
3327 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
3328 goto nla_put_failure;
3332 nla_put_failure_locked:
3333 spin_unlock(&tp->lock);
3335 nla_nest_cancel(skb, nest);
3339 static int fl_terse_dump(struct net *net, struct tcf_proto *tp, void *fh,
3340 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3342 struct cls_fl_filter *f = fh;
3343 struct nlattr *nest;
3349 t->tcm_handle = f->handle;
3351 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3353 goto nla_put_failure;
3355 spin_lock(&tp->lock);
3357 skip_hw = tc_skip_hw(f->flags);
3359 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3360 goto nla_put_failure_locked;
3362 spin_unlock(&tp->lock);
3365 fl_hw_update_stats(tp, f, rtnl_held);
3367 if (tcf_exts_terse_dump(skb, &f->exts))
3368 goto nla_put_failure;
3370 nla_nest_end(skb, nest);
3374 nla_put_failure_locked:
3375 spin_unlock(&tp->lock);
3377 nla_nest_cancel(skb, nest);
3381 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
3383 struct fl_flow_tmplt *tmplt = tmplt_priv;
3384 struct fl_flow_key *key, *mask;
3385 struct nlattr *nest;
3387 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3389 goto nla_put_failure;
3391 key = &tmplt->dummy_key;
3392 mask = &tmplt->mask;
3394 if (fl_dump_key(skb, net, key, mask))
3395 goto nla_put_failure;
3397 nla_nest_end(skb, nest);
3402 nla_nest_cancel(skb, nest);
3406 static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
3409 struct cls_fl_filter *f = fh;
3411 tc_cls_bind_class(classid, cl, q, &f->res, base);
3414 static bool fl_delete_empty(struct tcf_proto *tp)
3416 struct cls_fl_head *head = fl_head_dereference(tp);
3418 spin_lock(&tp->lock);
3419 tp->deleting = idr_is_empty(&head->handle_idr);
3420 spin_unlock(&tp->lock);
3422 return tp->deleting;
3425 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
3427 .classify = fl_classify,
3429 .destroy = fl_destroy,
3432 .change = fl_change,
3433 .delete = fl_delete,
3434 .delete_empty = fl_delete_empty,
3436 .reoffload = fl_reoffload,
3437 .hw_add = fl_hw_add,
3438 .hw_del = fl_hw_del,
3440 .terse_dump = fl_terse_dump,
3441 .bind_class = fl_bind_class,
3442 .tmplt_create = fl_tmplt_create,
3443 .tmplt_destroy = fl_tmplt_destroy,
3444 .tmplt_dump = fl_tmplt_dump,
3445 .owner = THIS_MODULE,
3446 .flags = TCF_PROTO_OPS_DOIT_UNLOCKED,
3449 static int __init cls_fl_init(void)
3451 return register_tcf_proto_ops(&cls_fl_ops);
3454 static void __exit cls_fl_exit(void)
3456 unregister_tcf_proto_ops(&cls_fl_ops);
3459 module_init(cls_fl_init);
3460 module_exit(cls_fl_exit);
3462 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3463 MODULE_DESCRIPTION("Flower classifier");
3464 MODULE_LICENSE("GPL v2");