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>
20 #include <net/sch_generic.h>
21 #include <net/pkt_cls.h>
23 #include <net/flow_dissector.h>
24 #include <net/geneve.h>
27 #include <net/dst_metadata.h>
29 #include <uapi/linux/netfilter/nf_conntrack_common.h>
32 struct flow_dissector_key_meta meta;
33 struct flow_dissector_key_control control;
34 struct flow_dissector_key_control enc_control;
35 struct flow_dissector_key_basic basic;
36 struct flow_dissector_key_eth_addrs eth;
37 struct flow_dissector_key_vlan vlan;
38 struct flow_dissector_key_vlan cvlan;
40 struct flow_dissector_key_ipv4_addrs ipv4;
41 struct flow_dissector_key_ipv6_addrs ipv6;
43 struct flow_dissector_key_ports tp;
44 struct flow_dissector_key_icmp icmp;
45 struct flow_dissector_key_arp arp;
46 struct flow_dissector_key_keyid enc_key_id;
48 struct flow_dissector_key_ipv4_addrs enc_ipv4;
49 struct flow_dissector_key_ipv6_addrs enc_ipv6;
51 struct flow_dissector_key_ports enc_tp;
52 struct flow_dissector_key_mpls mpls;
53 struct flow_dissector_key_tcp tcp;
54 struct flow_dissector_key_ip ip;
55 struct flow_dissector_key_ip enc_ip;
56 struct flow_dissector_key_enc_opts enc_opts;
58 struct flow_dissector_key_ports tp;
60 struct flow_dissector_key_ports tp_min;
61 struct flow_dissector_key_ports tp_max;
64 struct flow_dissector_key_ct ct;
65 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
67 struct fl_flow_mask_range {
68 unsigned short int start;
69 unsigned short int end;
73 struct fl_flow_key key;
74 struct fl_flow_mask_range range;
76 struct rhash_head ht_node;
78 struct rhashtable_params filter_ht_params;
79 struct flow_dissector dissector;
80 struct list_head filters;
81 struct rcu_work rwork;
82 struct list_head list;
86 struct fl_flow_tmplt {
87 struct fl_flow_key dummy_key;
88 struct fl_flow_key mask;
89 struct flow_dissector dissector;
90 struct tcf_chain *chain;
95 spinlock_t masks_lock; /* Protect masks list */
96 struct list_head masks;
97 struct list_head hw_filters;
98 struct rcu_work rwork;
99 struct idr handle_idr;
102 struct cls_fl_filter {
103 struct fl_flow_mask *mask;
104 struct rhash_head ht_node;
105 struct fl_flow_key mkey;
106 struct tcf_exts exts;
107 struct tcf_result res;
108 struct fl_flow_key key;
109 struct list_head list;
110 struct list_head hw_list;
114 struct rcu_work rwork;
115 struct net_device *hw_dev;
116 /* Flower classifier is unlocked, which means that its reference counter
117 * can be changed concurrently without any kind of external
118 * synchronization. Use atomic reference counter to be concurrency-safe.
124 static const struct rhashtable_params mask_ht_params = {
125 .key_offset = offsetof(struct fl_flow_mask, key),
126 .key_len = sizeof(struct fl_flow_key),
127 .head_offset = offsetof(struct fl_flow_mask, ht_node),
128 .automatic_shrinking = true,
131 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
133 return mask->range.end - mask->range.start;
136 static void fl_mask_update_range(struct fl_flow_mask *mask)
138 const u8 *bytes = (const u8 *) &mask->key;
139 size_t size = sizeof(mask->key);
140 size_t i, first = 0, last;
142 for (i = 0; i < size; i++) {
149 for (i = size - 1; i != first; i--) {
155 mask->range.start = rounddown(first, sizeof(long));
156 mask->range.end = roundup(last + 1, sizeof(long));
159 static void *fl_key_get_start(struct fl_flow_key *key,
160 const struct fl_flow_mask *mask)
162 return (u8 *) key + mask->range.start;
165 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
166 struct fl_flow_mask *mask)
168 const long *lkey = fl_key_get_start(key, mask);
169 const long *lmask = fl_key_get_start(&mask->key, mask);
170 long *lmkey = fl_key_get_start(mkey, mask);
173 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
174 *lmkey++ = *lkey++ & *lmask++;
177 static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
178 struct fl_flow_mask *mask)
180 const long *lmask = fl_key_get_start(&mask->key, mask);
186 ltmplt = fl_key_get_start(&tmplt->mask, mask);
187 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
188 if (~*ltmplt++ & *lmask++)
194 static void fl_clear_masked_range(struct fl_flow_key *key,
195 struct fl_flow_mask *mask)
197 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
200 static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
201 struct fl_flow_key *key,
202 struct fl_flow_key *mkey)
204 __be16 min_mask, max_mask, min_val, max_val;
206 min_mask = htons(filter->mask->key.tp_range.tp_min.dst);
207 max_mask = htons(filter->mask->key.tp_range.tp_max.dst);
208 min_val = htons(filter->key.tp_range.tp_min.dst);
209 max_val = htons(filter->key.tp_range.tp_max.dst);
211 if (min_mask && max_mask) {
212 if (htons(key->tp_range.tp.dst) < min_val ||
213 htons(key->tp_range.tp.dst) > max_val)
216 /* skb does not have min and max values */
217 mkey->tp_range.tp_min.dst = filter->mkey.tp_range.tp_min.dst;
218 mkey->tp_range.tp_max.dst = filter->mkey.tp_range.tp_max.dst;
223 static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
224 struct fl_flow_key *key,
225 struct fl_flow_key *mkey)
227 __be16 min_mask, max_mask, min_val, max_val;
229 min_mask = htons(filter->mask->key.tp_range.tp_min.src);
230 max_mask = htons(filter->mask->key.tp_range.tp_max.src);
231 min_val = htons(filter->key.tp_range.tp_min.src);
232 max_val = htons(filter->key.tp_range.tp_max.src);
234 if (min_mask && max_mask) {
235 if (htons(key->tp_range.tp.src) < min_val ||
236 htons(key->tp_range.tp.src) > max_val)
239 /* skb does not have min and max values */
240 mkey->tp_range.tp_min.src = filter->mkey.tp_range.tp_min.src;
241 mkey->tp_range.tp_max.src = filter->mkey.tp_range.tp_max.src;
246 static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
247 struct fl_flow_key *mkey)
249 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
250 mask->filter_ht_params);
253 static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
254 struct fl_flow_key *mkey,
255 struct fl_flow_key *key)
257 struct cls_fl_filter *filter, *f;
259 list_for_each_entry_rcu(filter, &mask->filters, list) {
260 if (!fl_range_port_dst_cmp(filter, key, mkey))
263 if (!fl_range_port_src_cmp(filter, key, mkey))
266 f = __fl_lookup(mask, mkey);
273 static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
274 struct fl_flow_key *mkey,
275 struct fl_flow_key *key)
277 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
278 return fl_lookup_range(mask, mkey, key);
280 return __fl_lookup(mask, mkey);
283 static u16 fl_ct_info_to_flower_map[] = {
284 [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
285 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
286 [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
287 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
288 [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
289 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
290 [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
291 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
292 [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
293 TCA_FLOWER_KEY_CT_FLAGS_NEW,
296 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
297 struct tcf_result *res)
299 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
300 struct fl_flow_key skb_mkey;
301 struct fl_flow_key skb_key;
302 struct fl_flow_mask *mask;
303 struct cls_fl_filter *f;
305 list_for_each_entry_rcu(mask, &head->masks, list) {
306 flow_dissector_init_keys(&skb_key.control, &skb_key.basic);
307 fl_clear_masked_range(&skb_key, mask);
309 skb_flow_dissect_meta(skb, &mask->dissector, &skb_key);
310 /* skb_flow_dissect() does not set n_proto in case an unknown
311 * protocol, so do it rather here.
313 skb_key.basic.n_proto = skb_protocol(skb, false);
314 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
315 skb_flow_dissect_ct(skb, &mask->dissector, &skb_key,
316 fl_ct_info_to_flower_map,
317 ARRAY_SIZE(fl_ct_info_to_flower_map));
318 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
320 fl_set_masked_key(&skb_mkey, &skb_key, mask);
322 f = fl_lookup(mask, &skb_mkey, &skb_key);
323 if (f && !tc_skip_sw(f->flags)) {
325 return tcf_exts_exec(skb, &f->exts, res);
331 static int fl_init(struct tcf_proto *tp)
333 struct cls_fl_head *head;
335 head = kzalloc(sizeof(*head), GFP_KERNEL);
339 spin_lock_init(&head->masks_lock);
340 INIT_LIST_HEAD_RCU(&head->masks);
341 INIT_LIST_HEAD(&head->hw_filters);
342 rcu_assign_pointer(tp->root, head);
343 idr_init(&head->handle_idr);
345 return rhashtable_init(&head->ht, &mask_ht_params);
348 static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done)
350 /* temporary masks don't have their filters list and ht initialized */
351 if (mask_init_done) {
352 WARN_ON(!list_empty(&mask->filters));
353 rhashtable_destroy(&mask->ht);
358 static void fl_mask_free_work(struct work_struct *work)
360 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
361 struct fl_flow_mask, rwork);
363 fl_mask_free(mask, true);
366 static void fl_uninit_mask_free_work(struct work_struct *work)
368 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
369 struct fl_flow_mask, rwork);
371 fl_mask_free(mask, false);
374 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask)
376 if (!refcount_dec_and_test(&mask->refcnt))
379 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
381 spin_lock(&head->masks_lock);
382 list_del_rcu(&mask->list);
383 spin_unlock(&head->masks_lock);
385 tcf_queue_work(&mask->rwork, fl_mask_free_work);
390 static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
392 /* Flower classifier only changes root pointer during init and destroy.
393 * Users must obtain reference to tcf_proto instance before calling its
394 * API, so tp->root pointer is protected from concurrent call to
395 * fl_destroy() by reference counting.
397 return rcu_dereference_raw(tp->root);
400 static void __fl_destroy_filter(struct cls_fl_filter *f)
402 tcf_exts_destroy(&f->exts);
403 tcf_exts_put_net(&f->exts);
407 static void fl_destroy_filter_work(struct work_struct *work)
409 struct cls_fl_filter *f = container_of(to_rcu_work(work),
410 struct cls_fl_filter, rwork);
412 __fl_destroy_filter(f);
415 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
416 bool rtnl_held, struct netlink_ext_ack *extack)
418 struct tcf_block *block = tp->chain->block;
419 struct flow_cls_offload cls_flower = {};
421 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
422 cls_flower.command = FLOW_CLS_DESTROY;
423 cls_flower.cookie = (unsigned long) f;
425 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false,
426 &f->flags, &f->in_hw_count, rtnl_held);
430 static int fl_hw_replace_filter(struct tcf_proto *tp,
431 struct cls_fl_filter *f, bool rtnl_held,
432 struct netlink_ext_ack *extack)
434 struct tcf_block *block = tp->chain->block;
435 struct flow_cls_offload cls_flower = {};
436 bool skip_sw = tc_skip_sw(f->flags);
439 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
440 if (!cls_flower.rule)
443 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
444 cls_flower.command = FLOW_CLS_REPLACE;
445 cls_flower.cookie = (unsigned long) f;
446 cls_flower.rule->match.dissector = &f->mask->dissector;
447 cls_flower.rule->match.mask = &f->mask->key;
448 cls_flower.rule->match.key = &f->mkey;
449 cls_flower.classid = f->res.classid;
451 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts,
454 kfree(cls_flower.rule);
456 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
462 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower,
463 skip_sw, &f->flags, &f->in_hw_count, rtnl_held);
464 tc_cleanup_flow_action(&cls_flower.rule->action);
465 kfree(cls_flower.rule);
468 fl_hw_destroy_filter(tp, f, rtnl_held, NULL);
472 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
478 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f,
481 struct tcf_block *block = tp->chain->block;
482 struct flow_cls_offload cls_flower = {};
484 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
485 cls_flower.command = FLOW_CLS_STATS;
486 cls_flower.cookie = (unsigned long) f;
487 cls_flower.classid = f->res.classid;
489 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false,
492 tcf_exts_stats_update(&f->exts, cls_flower.stats.bytes,
493 cls_flower.stats.pkts,
494 cls_flower.stats.lastused);
497 static void __fl_put(struct cls_fl_filter *f)
499 if (!refcount_dec_and_test(&f->refcnt))
502 if (tcf_exts_get_net(&f->exts))
503 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
505 __fl_destroy_filter(f);
508 static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
510 struct cls_fl_filter *f;
513 f = idr_find(&head->handle_idr, handle);
514 if (f && !refcount_inc_not_zero(&f->refcnt))
521 static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
522 bool *last, bool rtnl_held,
523 struct netlink_ext_ack *extack)
525 struct cls_fl_head *head = fl_head_dereference(tp);
529 spin_lock(&tp->lock);
531 spin_unlock(&tp->lock);
536 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
537 f->mask->filter_ht_params);
538 idr_remove(&head->handle_idr, f->handle);
539 list_del_rcu(&f->list);
540 spin_unlock(&tp->lock);
542 *last = fl_mask_put(head, f->mask);
543 if (!tc_skip_hw(f->flags))
544 fl_hw_destroy_filter(tp, f, rtnl_held, extack);
545 tcf_unbind_filter(tp, &f->res);
551 static void fl_destroy_sleepable(struct work_struct *work)
553 struct cls_fl_head *head = container_of(to_rcu_work(work),
557 rhashtable_destroy(&head->ht);
559 module_put(THIS_MODULE);
562 static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
563 struct netlink_ext_ack *extack)
565 struct cls_fl_head *head = fl_head_dereference(tp);
566 struct fl_flow_mask *mask, *next_mask;
567 struct cls_fl_filter *f, *next;
570 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
571 list_for_each_entry_safe(f, next, &mask->filters, list) {
572 __fl_delete(tp, f, &last, rtnl_held, extack);
577 idr_destroy(&head->handle_idr);
579 __module_get(THIS_MODULE);
580 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
583 static void fl_put(struct tcf_proto *tp, void *arg)
585 struct cls_fl_filter *f = arg;
590 static void *fl_get(struct tcf_proto *tp, u32 handle)
592 struct cls_fl_head *head = fl_head_dereference(tp);
594 return __fl_get(head, handle);
597 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
598 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
599 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
600 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
602 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
603 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
604 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
605 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
606 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
607 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
608 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
609 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
610 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
611 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
612 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
613 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
614 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
615 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
616 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
617 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
618 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
619 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
620 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
621 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
622 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
623 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
624 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
625 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
626 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
627 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
628 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
629 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
630 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
631 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
632 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
633 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
634 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
635 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
636 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
637 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
638 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
639 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
640 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
641 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
642 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
643 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
644 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
645 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
646 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
647 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
648 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
649 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
650 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
651 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
652 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
653 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
654 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
655 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
656 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
657 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
658 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
659 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
660 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
661 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
662 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
663 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
664 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
665 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
666 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
667 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
668 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
669 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
670 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
671 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
672 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
673 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
674 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
675 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
676 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
677 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
678 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
679 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
680 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
681 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
682 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
683 [TCA_FLOWER_KEY_CT_STATE] = { .type = NLA_U16 },
684 [TCA_FLOWER_KEY_CT_STATE_MASK] = { .type = NLA_U16 },
685 [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 },
686 [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 },
687 [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 },
688 [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 },
689 [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY,
690 .len = 128 / BITS_PER_BYTE },
691 [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY,
692 .len = 128 / BITS_PER_BYTE },
693 [TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
696 static const struct nla_policy
697 enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
698 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
701 static const struct nla_policy
702 geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
703 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
704 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
705 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
709 static void fl_set_key_val(struct nlattr **tb,
710 void *val, int val_type,
711 void *mask, int mask_type, int len)
715 nla_memcpy(val, tb[val_type], len);
716 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
717 memset(mask, 0xff, len);
719 nla_memcpy(mask, tb[mask_type], len);
722 static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
723 struct fl_flow_key *mask)
725 fl_set_key_val(tb, &key->tp_range.tp_min.dst,
726 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst,
727 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.dst));
728 fl_set_key_val(tb, &key->tp_range.tp_max.dst,
729 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_range.tp_max.dst,
730 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.dst));
731 fl_set_key_val(tb, &key->tp_range.tp_min.src,
732 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_range.tp_min.src,
733 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.src));
734 fl_set_key_val(tb, &key->tp_range.tp_max.src,
735 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src,
736 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src));
738 if ((mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
739 htons(key->tp_range.tp_max.dst) <=
740 htons(key->tp_range.tp_min.dst)) ||
741 (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
742 htons(key->tp_range.tp_max.src) <=
743 htons(key->tp_range.tp_min.src)))
749 static int fl_set_key_mpls(struct nlattr **tb,
750 struct flow_dissector_key_mpls *key_val,
751 struct flow_dissector_key_mpls *key_mask)
753 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
754 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
755 key_mask->mpls_ttl = MPLS_TTL_MASK;
757 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
758 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
760 if (bos & ~MPLS_BOS_MASK)
762 key_val->mpls_bos = bos;
763 key_mask->mpls_bos = MPLS_BOS_MASK;
765 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
766 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
768 if (tc & ~MPLS_TC_MASK)
770 key_val->mpls_tc = tc;
771 key_mask->mpls_tc = MPLS_TC_MASK;
773 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
774 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
776 if (label & ~MPLS_LABEL_MASK)
778 key_val->mpls_label = label;
779 key_mask->mpls_label = MPLS_LABEL_MASK;
784 static void fl_set_key_vlan(struct nlattr **tb,
786 int vlan_id_key, int vlan_prio_key,
787 int vlan_next_eth_type_key,
788 struct flow_dissector_key_vlan *key_val,
789 struct flow_dissector_key_vlan *key_mask)
791 #define VLAN_PRIORITY_MASK 0x7
793 if (tb[vlan_id_key]) {
795 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
796 key_mask->vlan_id = VLAN_VID_MASK;
798 if (tb[vlan_prio_key]) {
799 key_val->vlan_priority =
800 nla_get_u8(tb[vlan_prio_key]) &
802 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
804 key_val->vlan_tpid = ethertype;
805 key_mask->vlan_tpid = cpu_to_be16(~0);
806 if (tb[vlan_next_eth_type_key]) {
807 key_val->vlan_eth_type =
808 nla_get_be16(tb[vlan_next_eth_type_key]);
809 key_mask->vlan_eth_type = cpu_to_be16(~0);
813 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
814 u32 *dissector_key, u32 *dissector_mask,
815 u32 flower_flag_bit, u32 dissector_flag_bit)
817 if (flower_mask & flower_flag_bit) {
818 *dissector_mask |= dissector_flag_bit;
819 if (flower_key & flower_flag_bit)
820 *dissector_key |= dissector_flag_bit;
824 static int fl_set_key_flags(struct nlattr **tb,
825 u32 *flags_key, u32 *flags_mask)
829 /* mask is mandatory for flags */
830 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
833 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
834 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
839 fl_set_key_flag(key, mask, flags_key, flags_mask,
840 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
841 fl_set_key_flag(key, mask, flags_key, flags_mask,
842 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
843 FLOW_DIS_FIRST_FRAG);
848 static void fl_set_key_ip(struct nlattr **tb, bool encap,
849 struct flow_dissector_key_ip *key,
850 struct flow_dissector_key_ip *mask)
852 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
853 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
854 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
855 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
857 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
858 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
861 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
862 int depth, int option_len,
863 struct netlink_ext_ack *extack)
865 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
866 struct nlattr *class = NULL, *type = NULL, *data = NULL;
867 struct geneve_opt *opt;
868 int err, data_len = 0;
870 if (option_len > sizeof(struct geneve_opt))
871 data_len = option_len - sizeof(struct geneve_opt);
873 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
874 memset(opt, 0xff, option_len);
875 opt->length = data_len / 4;
880 /* If no mask has been prodived we assume an exact match. */
882 return sizeof(struct geneve_opt) + data_len;
884 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
885 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
889 err = nla_parse_nested_deprecated(tb,
890 TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
891 nla, geneve_opt_policy, extack);
895 /* We are not allowed to omit any of CLASS, TYPE or DATA
896 * fields from the key.
899 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
900 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
901 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
902 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
906 /* Omitting any of CLASS, TYPE or DATA fields is allowed
909 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
910 int new_len = key->enc_opts.len;
912 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
913 data_len = nla_len(data);
915 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
919 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
923 new_len += sizeof(struct geneve_opt) + data_len;
924 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
925 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
926 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
929 opt->length = data_len / 4;
930 memcpy(opt->opt_data, nla_data(data), data_len);
933 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
934 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
935 opt->opt_class = nla_get_be16(class);
938 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
939 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
940 opt->type = nla_get_u8(type);
943 return sizeof(struct geneve_opt) + data_len;
946 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
947 struct fl_flow_key *mask,
948 struct netlink_ext_ack *extack)
950 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
951 int err, option_len, key_depth, msk_depth = 0;
953 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
954 TCA_FLOWER_KEY_ENC_OPTS_MAX,
955 enc_opts_policy, extack);
959 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
961 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
962 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
963 TCA_FLOWER_KEY_ENC_OPTS_MAX,
964 enc_opts_policy, extack);
968 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
969 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
972 nla_for_each_attr(nla_opt_key, nla_enc_key,
973 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
974 switch (nla_type(nla_opt_key)) {
975 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
977 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
978 option_len = fl_set_geneve_opt(nla_opt_key, key,
979 key_depth, option_len,
984 key->enc_opts.len += option_len;
985 /* At the same time we need to parse through the mask
986 * in order to verify exact and mask attribute lengths.
988 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
989 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
990 msk_depth, option_len,
995 mask->enc_opts.len += option_len;
996 if (key->enc_opts.len != mask->enc_opts.len) {
997 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1002 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
1005 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
1013 static int fl_set_key_ct(struct nlattr **tb,
1014 struct flow_dissector_key_ct *key,
1015 struct flow_dissector_key_ct *mask,
1016 struct netlink_ext_ack *extack)
1018 if (tb[TCA_FLOWER_KEY_CT_STATE]) {
1019 if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) {
1020 NL_SET_ERR_MSG(extack, "Conntrack isn't enabled");
1023 fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
1024 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
1025 sizeof(key->ct_state));
1027 if (tb[TCA_FLOWER_KEY_CT_ZONE]) {
1028 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1029 NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled");
1032 fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
1033 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
1034 sizeof(key->ct_zone));
1036 if (tb[TCA_FLOWER_KEY_CT_MARK]) {
1037 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1038 NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled");
1041 fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
1042 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
1043 sizeof(key->ct_mark));
1045 if (tb[TCA_FLOWER_KEY_CT_LABELS]) {
1046 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1047 NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled");
1050 fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
1051 mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
1052 sizeof(key->ct_labels));
1058 static int fl_set_key(struct net *net, struct nlattr **tb,
1059 struct fl_flow_key *key, struct fl_flow_key *mask,
1060 struct netlink_ext_ack *extack)
1065 if (tb[TCA_FLOWER_INDEV]) {
1066 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
1069 key->meta.ingress_ifindex = err;
1070 mask->meta.ingress_ifindex = 0xffffffff;
1073 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1074 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1075 sizeof(key->eth.dst));
1076 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1077 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1078 sizeof(key->eth.src));
1080 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
1081 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
1083 if (eth_type_vlan(ethertype)) {
1084 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
1085 TCA_FLOWER_KEY_VLAN_PRIO,
1086 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1087 &key->vlan, &mask->vlan);
1089 if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
1090 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
1091 if (eth_type_vlan(ethertype)) {
1092 fl_set_key_vlan(tb, ethertype,
1093 TCA_FLOWER_KEY_CVLAN_ID,
1094 TCA_FLOWER_KEY_CVLAN_PRIO,
1095 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1096 &key->cvlan, &mask->cvlan);
1097 fl_set_key_val(tb, &key->basic.n_proto,
1098 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1099 &mask->basic.n_proto,
1101 sizeof(key->basic.n_proto));
1103 key->basic.n_proto = ethertype;
1104 mask->basic.n_proto = cpu_to_be16(~0);
1108 key->basic.n_proto = ethertype;
1109 mask->basic.n_proto = cpu_to_be16(~0);
1113 if (key->basic.n_proto == htons(ETH_P_IP) ||
1114 key->basic.n_proto == htons(ETH_P_IPV6)) {
1115 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1116 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1117 sizeof(key->basic.ip_proto));
1118 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
1121 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1122 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1123 mask->control.addr_type = ~0;
1124 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1125 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1126 sizeof(key->ipv4.src));
1127 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1128 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1129 sizeof(key->ipv4.dst));
1130 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1131 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1132 mask->control.addr_type = ~0;
1133 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1134 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1135 sizeof(key->ipv6.src));
1136 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1137 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1138 sizeof(key->ipv6.dst));
1141 if (key->basic.ip_proto == IPPROTO_TCP) {
1142 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1143 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1144 sizeof(key->tp.src));
1145 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1146 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1147 sizeof(key->tp.dst));
1148 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1149 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1150 sizeof(key->tcp.flags));
1151 } else if (key->basic.ip_proto == IPPROTO_UDP) {
1152 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1153 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1154 sizeof(key->tp.src));
1155 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1156 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1157 sizeof(key->tp.dst));
1158 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
1159 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1160 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1161 sizeof(key->tp.src));
1162 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1163 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1164 sizeof(key->tp.dst));
1165 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
1166 key->basic.ip_proto == IPPROTO_ICMP) {
1167 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1169 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1170 sizeof(key->icmp.type));
1171 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1173 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1174 sizeof(key->icmp.code));
1175 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1176 key->basic.ip_proto == IPPROTO_ICMPV6) {
1177 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1179 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1180 sizeof(key->icmp.type));
1181 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
1183 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1184 sizeof(key->icmp.code));
1185 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1186 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
1187 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
1190 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
1191 key->basic.n_proto == htons(ETH_P_RARP)) {
1192 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
1193 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
1194 sizeof(key->arp.sip));
1195 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
1196 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
1197 sizeof(key->arp.tip));
1198 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
1199 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
1200 sizeof(key->arp.op));
1201 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1202 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1203 sizeof(key->arp.sha));
1204 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1205 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1206 sizeof(key->arp.tha));
1209 if (key->basic.ip_proto == IPPROTO_TCP ||
1210 key->basic.ip_proto == IPPROTO_UDP ||
1211 key->basic.ip_proto == IPPROTO_SCTP) {
1212 ret = fl_set_key_port_range(tb, key, mask);
1217 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
1218 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
1219 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1220 mask->enc_control.addr_type = ~0;
1221 fl_set_key_val(tb, &key->enc_ipv4.src,
1222 TCA_FLOWER_KEY_ENC_IPV4_SRC,
1223 &mask->enc_ipv4.src,
1224 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1225 sizeof(key->enc_ipv4.src));
1226 fl_set_key_val(tb, &key->enc_ipv4.dst,
1227 TCA_FLOWER_KEY_ENC_IPV4_DST,
1228 &mask->enc_ipv4.dst,
1229 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1230 sizeof(key->enc_ipv4.dst));
1233 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
1234 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
1235 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1236 mask->enc_control.addr_type = ~0;
1237 fl_set_key_val(tb, &key->enc_ipv6.src,
1238 TCA_FLOWER_KEY_ENC_IPV6_SRC,
1239 &mask->enc_ipv6.src,
1240 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1241 sizeof(key->enc_ipv6.src));
1242 fl_set_key_val(tb, &key->enc_ipv6.dst,
1243 TCA_FLOWER_KEY_ENC_IPV6_DST,
1244 &mask->enc_ipv6.dst,
1245 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1246 sizeof(key->enc_ipv6.dst));
1249 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
1250 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
1251 sizeof(key->enc_key_id.keyid));
1253 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1254 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1255 sizeof(key->enc_tp.src));
1257 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1258 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1259 sizeof(key->enc_tp.dst));
1261 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
1263 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
1264 ret = fl_set_enc_opt(tb, key, mask, extack);
1269 ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack);
1273 if (tb[TCA_FLOWER_KEY_FLAGS])
1274 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
1279 static void fl_mask_copy(struct fl_flow_mask *dst,
1280 struct fl_flow_mask *src)
1282 const void *psrc = fl_key_get_start(&src->key, src);
1283 void *pdst = fl_key_get_start(&dst->key, src);
1285 memcpy(pdst, psrc, fl_mask_range(src));
1286 dst->range = src->range;
1289 static const struct rhashtable_params fl_ht_params = {
1290 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
1291 .head_offset = offsetof(struct cls_fl_filter, ht_node),
1292 .automatic_shrinking = true,
1295 static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
1297 mask->filter_ht_params = fl_ht_params;
1298 mask->filter_ht_params.key_len = fl_mask_range(mask);
1299 mask->filter_ht_params.key_offset += mask->range.start;
1301 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
1304 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
1305 #define FL_KEY_MEMBER_SIZE(member) FIELD_SIZEOF(struct fl_flow_key, member)
1307 #define FL_KEY_IS_MASKED(mask, member) \
1308 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
1309 0, FL_KEY_MEMBER_SIZE(member)) \
1311 #define FL_KEY_SET(keys, cnt, id, member) \
1313 keys[cnt].key_id = id; \
1314 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
1318 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
1320 if (FL_KEY_IS_MASKED(mask, member)) \
1321 FL_KEY_SET(keys, cnt, id, member); \
1324 static void fl_init_dissector(struct flow_dissector *dissector,
1325 struct fl_flow_key *mask)
1327 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1330 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1331 FLOW_DISSECTOR_KEY_META, meta);
1332 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
1333 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
1334 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1335 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
1336 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1337 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
1338 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1339 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
1340 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1341 FLOW_DISSECTOR_KEY_PORTS, tp);
1342 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1343 FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range);
1344 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1345 FLOW_DISSECTOR_KEY_IP, ip);
1346 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1347 FLOW_DISSECTOR_KEY_TCP, tcp);
1348 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1349 FLOW_DISSECTOR_KEY_ICMP, icmp);
1350 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1351 FLOW_DISSECTOR_KEY_ARP, arp);
1352 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1353 FLOW_DISSECTOR_KEY_MPLS, mpls);
1354 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1355 FLOW_DISSECTOR_KEY_VLAN, vlan);
1356 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1357 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
1358 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1359 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
1360 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1361 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
1362 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1363 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
1364 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1365 FL_KEY_IS_MASKED(mask, enc_ipv6))
1366 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1368 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1369 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
1370 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1371 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
1372 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1373 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
1374 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1375 FLOW_DISSECTOR_KEY_CT, ct);
1377 skb_flow_dissector_init(dissector, keys, cnt);
1380 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1381 struct fl_flow_mask *mask)
1383 struct fl_flow_mask *newmask;
1386 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1388 return ERR_PTR(-ENOMEM);
1390 fl_mask_copy(newmask, mask);
1392 if ((newmask->key.tp_range.tp_min.dst &&
1393 newmask->key.tp_range.tp_max.dst) ||
1394 (newmask->key.tp_range.tp_min.src &&
1395 newmask->key.tp_range.tp_max.src))
1396 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
1398 err = fl_init_mask_hashtable(newmask);
1402 fl_init_dissector(&newmask->dissector, &newmask->key);
1404 INIT_LIST_HEAD_RCU(&newmask->filters);
1406 refcount_set(&newmask->refcnt, 1);
1407 err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
1408 &newmask->ht_node, mask_ht_params);
1410 goto errout_destroy;
1412 spin_lock(&head->masks_lock);
1413 list_add_tail_rcu(&newmask->list, &head->masks);
1414 spin_unlock(&head->masks_lock);
1419 rhashtable_destroy(&newmask->ht);
1423 return ERR_PTR(err);
1426 static int fl_check_assign_mask(struct cls_fl_head *head,
1427 struct cls_fl_filter *fnew,
1428 struct cls_fl_filter *fold,
1429 struct fl_flow_mask *mask)
1431 struct fl_flow_mask *newmask;
1436 /* Insert mask as temporary node to prevent concurrent creation of mask
1437 * with same key. Any concurrent lookups with same key will return
1438 * -EAGAIN because mask's refcnt is zero.
1440 fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
1448 goto errout_cleanup;
1451 newmask = fl_create_new_mask(head, mask);
1452 if (IS_ERR(newmask)) {
1453 ret = PTR_ERR(newmask);
1454 goto errout_cleanup;
1457 fnew->mask = newmask;
1459 } else if (IS_ERR(fnew->mask)) {
1460 ret = PTR_ERR(fnew->mask);
1461 } else if (fold && fold->mask != fnew->mask) {
1463 } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
1464 /* Mask was deleted concurrently, try again */
1471 rhashtable_remove_fast(&head->ht, &mask->ht_node,
1476 static int fl_set_parms(struct net *net, struct tcf_proto *tp,
1477 struct cls_fl_filter *f, struct fl_flow_mask *mask,
1478 unsigned long base, struct nlattr **tb,
1479 struct nlattr *est, bool ovr,
1480 struct fl_flow_tmplt *tmplt, bool rtnl_held,
1481 struct netlink_ext_ack *extack)
1485 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, rtnl_held,
1490 if (tb[TCA_FLOWER_CLASSID]) {
1491 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
1494 tcf_bind_filter(tp, &f->res, base);
1499 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
1503 fl_mask_update_range(mask);
1504 fl_set_masked_key(&f->mkey, &f->key, mask);
1506 if (!fl_mask_fits_tmplt(tmplt, mask)) {
1507 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
1514 static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
1515 struct cls_fl_filter *fold,
1518 struct fl_flow_mask *mask = fnew->mask;
1521 err = rhashtable_lookup_insert_fast(&mask->ht,
1523 mask->filter_ht_params);
1526 /* It is okay if filter with same key exists when
1529 return fold && err == -EEXIST ? 0 : err;
1536 static int fl_change(struct net *net, struct sk_buff *in_skb,
1537 struct tcf_proto *tp, unsigned long base,
1538 u32 handle, struct nlattr **tca,
1539 void **arg, bool ovr, bool rtnl_held,
1540 struct netlink_ext_ack *extack)
1542 struct cls_fl_head *head = fl_head_dereference(tp);
1543 struct cls_fl_filter *fold = *arg;
1544 struct cls_fl_filter *fnew;
1545 struct fl_flow_mask *mask;
1550 if (!tca[TCA_OPTIONS]) {
1555 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
1561 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1564 goto errout_mask_alloc;
1567 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
1568 tca[TCA_OPTIONS], fl_policy, NULL);
1572 if (fold && handle && fold->handle != handle) {
1577 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
1582 INIT_LIST_HEAD(&fnew->hw_list);
1583 refcount_set(&fnew->refcnt, 1);
1585 err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0);
1589 if (tb[TCA_FLOWER_FLAGS]) {
1590 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1592 if (!tc_flags_valid(fnew->flags)) {
1598 err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr,
1599 tp->chain->tmplt_priv, rtnl_held, extack);
1603 err = fl_check_assign_mask(head, fnew, fold, mask);
1607 err = fl_ht_insert_unique(fnew, fold, &in_ht);
1611 if (!tc_skip_hw(fnew->flags)) {
1612 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
1617 if (!tc_in_hw(fnew->flags))
1618 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1620 spin_lock(&tp->lock);
1622 /* tp was deleted concurrently. -EAGAIN will cause caller to lookup
1623 * proto again or create new one, if necessary.
1631 /* Fold filter was deleted concurrently. Retry lookup. */
1632 if (fold->deleted) {
1637 fnew->handle = handle;
1640 struct rhashtable_params params =
1641 fnew->mask->filter_ht_params;
1643 err = rhashtable_insert_fast(&fnew->mask->ht,
1651 refcount_inc(&fnew->refcnt);
1652 rhashtable_remove_fast(&fold->mask->ht,
1654 fold->mask->filter_ht_params);
1655 idr_replace(&head->handle_idr, fnew, fnew->handle);
1656 list_replace_rcu(&fold->list, &fnew->list);
1657 fold->deleted = true;
1659 spin_unlock(&tp->lock);
1661 fl_mask_put(head, fold->mask);
1662 if (!tc_skip_hw(fold->flags))
1663 fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
1664 tcf_unbind_filter(tp, &fold->res);
1665 /* Caller holds reference to fold, so refcnt is always > 0
1668 refcount_dec(&fold->refcnt);
1672 /* user specifies a handle and it doesn't exist */
1673 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1674 handle, GFP_ATOMIC);
1676 /* Filter with specified handle was concurrently
1677 * inserted after initial check in cls_api. This is not
1678 * necessarily an error if NLM_F_EXCL is not set in
1679 * message flags. Returning EAGAIN will cause cls_api to
1680 * try to update concurrently inserted rule.
1686 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1687 INT_MAX, GFP_ATOMIC);
1692 refcount_inc(&fnew->refcnt);
1693 fnew->handle = handle;
1694 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
1695 spin_unlock(&tp->lock);
1701 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
1705 spin_lock(&tp->lock);
1707 fnew->deleted = true;
1708 spin_unlock(&tp->lock);
1709 if (!tc_skip_hw(fnew->flags))
1710 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
1712 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
1713 fnew->mask->filter_ht_params);
1715 fl_mask_put(head, fnew->mask);
1721 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
1728 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1729 bool rtnl_held, struct netlink_ext_ack *extack)
1731 struct cls_fl_head *head = fl_head_dereference(tp);
1732 struct cls_fl_filter *f = arg;
1736 err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
1737 *last = list_empty(&head->masks);
1743 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
1746 struct cls_fl_head *head = fl_head_dereference(tp);
1747 unsigned long id = arg->cookie, tmp;
1748 struct cls_fl_filter *f;
1750 arg->count = arg->skip;
1753 idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) {
1754 /* don't return filters that are being deleted */
1755 if (!refcount_inc_not_zero(&f->refcnt))
1759 if (arg->fn(tp, f, arg) < 0) {
1773 static struct cls_fl_filter *
1774 fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
1776 struct cls_fl_head *head = fl_head_dereference(tp);
1778 spin_lock(&tp->lock);
1779 if (list_empty(&head->hw_filters)) {
1780 spin_unlock(&tp->lock);
1785 f = list_entry(&head->hw_filters, struct cls_fl_filter,
1787 list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
1788 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
1789 spin_unlock(&tp->lock);
1794 spin_unlock(&tp->lock);
1798 static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
1799 void *cb_priv, struct netlink_ext_ack *extack)
1801 struct tcf_block *block = tp->chain->block;
1802 struct flow_cls_offload cls_flower = {};
1803 struct cls_fl_filter *f = NULL;
1806 /* hw_filters list can only be changed by hw offload functions after
1807 * obtaining rtnl lock. Make sure it is not changed while reoffload is
1812 while ((f = fl_get_next_hw_filter(tp, f, add))) {
1814 flow_rule_alloc(tcf_exts_num_actions(&f->exts));
1815 if (!cls_flower.rule) {
1820 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
1822 cls_flower.command = add ?
1823 FLOW_CLS_REPLACE : FLOW_CLS_DESTROY;
1824 cls_flower.cookie = (unsigned long)f;
1825 cls_flower.rule->match.dissector = &f->mask->dissector;
1826 cls_flower.rule->match.mask = &f->mask->key;
1827 cls_flower.rule->match.key = &f->mkey;
1829 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts,
1832 kfree(cls_flower.rule);
1833 if (tc_skip_sw(f->flags)) {
1834 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
1841 cls_flower.classid = f->res.classid;
1843 err = tc_setup_cb_reoffload(block, tp, add, cb,
1844 TC_SETUP_CLSFLOWER, &cls_flower,
1847 tc_cleanup_flow_action(&cls_flower.rule->action);
1848 kfree(cls_flower.rule);
1861 static void fl_hw_add(struct tcf_proto *tp, void *type_data)
1863 struct flow_cls_offload *cls_flower = type_data;
1864 struct cls_fl_filter *f =
1865 (struct cls_fl_filter *) cls_flower->cookie;
1866 struct cls_fl_head *head = fl_head_dereference(tp);
1868 spin_lock(&tp->lock);
1869 list_add(&f->hw_list, &head->hw_filters);
1870 spin_unlock(&tp->lock);
1873 static void fl_hw_del(struct tcf_proto *tp, void *type_data)
1875 struct flow_cls_offload *cls_flower = type_data;
1876 struct cls_fl_filter *f =
1877 (struct cls_fl_filter *) cls_flower->cookie;
1879 spin_lock(&tp->lock);
1880 if (!list_empty(&f->hw_list))
1881 list_del_init(&f->hw_list);
1882 spin_unlock(&tp->lock);
1885 static int fl_hw_create_tmplt(struct tcf_chain *chain,
1886 struct fl_flow_tmplt *tmplt)
1888 struct flow_cls_offload cls_flower = {};
1889 struct tcf_block *block = chain->block;
1891 cls_flower.rule = flow_rule_alloc(0);
1892 if (!cls_flower.rule)
1895 cls_flower.common.chain_index = chain->index;
1896 cls_flower.command = FLOW_CLS_TMPLT_CREATE;
1897 cls_flower.cookie = (unsigned long) tmplt;
1898 cls_flower.rule->match.dissector = &tmplt->dissector;
1899 cls_flower.rule->match.mask = &tmplt->mask;
1900 cls_flower.rule->match.key = &tmplt->dummy_key;
1902 /* We don't care if driver (any of them) fails to handle this
1903 * call. It serves just as a hint for it.
1905 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
1906 kfree(cls_flower.rule);
1911 static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
1912 struct fl_flow_tmplt *tmplt)
1914 struct flow_cls_offload cls_flower = {};
1915 struct tcf_block *block = chain->block;
1917 cls_flower.common.chain_index = chain->index;
1918 cls_flower.command = FLOW_CLS_TMPLT_DESTROY;
1919 cls_flower.cookie = (unsigned long) tmplt;
1921 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
1924 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
1925 struct nlattr **tca,
1926 struct netlink_ext_ack *extack)
1928 struct fl_flow_tmplt *tmplt;
1932 if (!tca[TCA_OPTIONS])
1933 return ERR_PTR(-EINVAL);
1935 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1937 return ERR_PTR(-ENOBUFS);
1938 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
1939 tca[TCA_OPTIONS], fl_policy, NULL);
1943 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
1948 tmplt->chain = chain;
1949 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
1953 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
1955 err = fl_hw_create_tmplt(chain, tmplt);
1966 return ERR_PTR(err);
1969 static void fl_tmplt_destroy(void *tmplt_priv)
1971 struct fl_flow_tmplt *tmplt = tmplt_priv;
1973 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
1977 static int fl_dump_key_val(struct sk_buff *skb,
1978 void *val, int val_type,
1979 void *mask, int mask_type, int len)
1983 if (!memchr_inv(mask, 0, len))
1985 err = nla_put(skb, val_type, len, val);
1988 if (mask_type != TCA_FLOWER_UNSPEC) {
1989 err = nla_put(skb, mask_type, len, mask);
1996 static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
1997 struct fl_flow_key *mask)
1999 if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst,
2000 TCA_FLOWER_KEY_PORT_DST_MIN,
2001 &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC,
2002 sizeof(key->tp_range.tp_min.dst)) ||
2003 fl_dump_key_val(skb, &key->tp_range.tp_max.dst,
2004 TCA_FLOWER_KEY_PORT_DST_MAX,
2005 &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC,
2006 sizeof(key->tp_range.tp_max.dst)) ||
2007 fl_dump_key_val(skb, &key->tp_range.tp_min.src,
2008 TCA_FLOWER_KEY_PORT_SRC_MIN,
2009 &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC,
2010 sizeof(key->tp_range.tp_min.src)) ||
2011 fl_dump_key_val(skb, &key->tp_range.tp_max.src,
2012 TCA_FLOWER_KEY_PORT_SRC_MAX,
2013 &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC,
2014 sizeof(key->tp_range.tp_max.src)))
2020 static int fl_dump_key_mpls(struct sk_buff *skb,
2021 struct flow_dissector_key_mpls *mpls_key,
2022 struct flow_dissector_key_mpls *mpls_mask)
2026 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
2028 if (mpls_mask->mpls_ttl) {
2029 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
2030 mpls_key->mpls_ttl);
2034 if (mpls_mask->mpls_tc) {
2035 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
2040 if (mpls_mask->mpls_label) {
2041 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
2042 mpls_key->mpls_label);
2046 if (mpls_mask->mpls_bos) {
2047 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
2048 mpls_key->mpls_bos);
2055 static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
2056 struct flow_dissector_key_ip *key,
2057 struct flow_dissector_key_ip *mask)
2059 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
2060 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
2061 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
2062 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
2064 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
2065 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
2071 static int fl_dump_key_vlan(struct sk_buff *skb,
2072 int vlan_id_key, int vlan_prio_key,
2073 struct flow_dissector_key_vlan *vlan_key,
2074 struct flow_dissector_key_vlan *vlan_mask)
2078 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
2080 if (vlan_mask->vlan_id) {
2081 err = nla_put_u16(skb, vlan_id_key,
2086 if (vlan_mask->vlan_priority) {
2087 err = nla_put_u8(skb, vlan_prio_key,
2088 vlan_key->vlan_priority);
2095 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
2096 u32 *flower_key, u32 *flower_mask,
2097 u32 flower_flag_bit, u32 dissector_flag_bit)
2099 if (dissector_mask & dissector_flag_bit) {
2100 *flower_mask |= flower_flag_bit;
2101 if (dissector_key & dissector_flag_bit)
2102 *flower_key |= flower_flag_bit;
2106 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
2112 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
2118 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2119 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
2120 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2121 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
2122 FLOW_DIS_FIRST_FRAG);
2124 _key = cpu_to_be32(key);
2125 _mask = cpu_to_be32(mask);
2127 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
2131 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
2134 static int fl_dump_key_geneve_opt(struct sk_buff *skb,
2135 struct flow_dissector_key_enc_opts *enc_opts)
2137 struct geneve_opt *opt;
2138 struct nlattr *nest;
2141 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
2143 goto nla_put_failure;
2145 while (enc_opts->len > opt_off) {
2146 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
2148 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
2150 goto nla_put_failure;
2151 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
2153 goto nla_put_failure;
2154 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
2155 opt->length * 4, opt->opt_data))
2156 goto nla_put_failure;
2158 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
2160 nla_nest_end(skb, nest);
2164 nla_nest_cancel(skb, nest);
2168 static int fl_dump_key_ct(struct sk_buff *skb,
2169 struct flow_dissector_key_ct *key,
2170 struct flow_dissector_key_ct *mask)
2172 if (IS_ENABLED(CONFIG_NF_CONNTRACK) &&
2173 fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
2174 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
2175 sizeof(key->ct_state)))
2176 goto nla_put_failure;
2178 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
2179 fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
2180 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
2181 sizeof(key->ct_zone)))
2182 goto nla_put_failure;
2184 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
2185 fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
2186 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
2187 sizeof(key->ct_mark)))
2188 goto nla_put_failure;
2190 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
2191 fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
2192 &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
2193 sizeof(key->ct_labels)))
2194 goto nla_put_failure;
2202 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
2203 struct flow_dissector_key_enc_opts *enc_opts)
2205 struct nlattr *nest;
2211 nest = nla_nest_start_noflag(skb, enc_opt_type);
2213 goto nla_put_failure;
2215 switch (enc_opts->dst_opt_type) {
2216 case TUNNEL_GENEVE_OPT:
2217 err = fl_dump_key_geneve_opt(skb, enc_opts);
2219 goto nla_put_failure;
2222 goto nla_put_failure;
2224 nla_nest_end(skb, nest);
2228 nla_nest_cancel(skb, nest);
2232 static int fl_dump_key_enc_opt(struct sk_buff *skb,
2233 struct flow_dissector_key_enc_opts *key_opts,
2234 struct flow_dissector_key_enc_opts *msk_opts)
2238 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
2242 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
2245 static int fl_dump_key(struct sk_buff *skb, struct net *net,
2246 struct fl_flow_key *key, struct fl_flow_key *mask)
2248 if (mask->meta.ingress_ifindex) {
2249 struct net_device *dev;
2251 dev = __dev_get_by_index(net, key->meta.ingress_ifindex);
2252 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
2253 goto nla_put_failure;
2256 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
2257 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
2258 sizeof(key->eth.dst)) ||
2259 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
2260 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
2261 sizeof(key->eth.src)) ||
2262 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
2263 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
2264 sizeof(key->basic.n_proto)))
2265 goto nla_put_failure;
2267 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
2268 goto nla_put_failure;
2270 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
2271 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
2272 goto nla_put_failure;
2274 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
2275 TCA_FLOWER_KEY_CVLAN_PRIO,
2276 &key->cvlan, &mask->cvlan) ||
2277 (mask->cvlan.vlan_tpid &&
2278 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2279 key->cvlan.vlan_tpid)))
2280 goto nla_put_failure;
2282 if (mask->basic.n_proto) {
2283 if (mask->cvlan.vlan_eth_type) {
2284 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
2285 key->basic.n_proto))
2286 goto nla_put_failure;
2287 } else if (mask->vlan.vlan_eth_type) {
2288 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2289 key->vlan.vlan_eth_type))
2290 goto nla_put_failure;
2294 if ((key->basic.n_proto == htons(ETH_P_IP) ||
2295 key->basic.n_proto == htons(ETH_P_IPV6)) &&
2296 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
2297 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
2298 sizeof(key->basic.ip_proto)) ||
2299 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
2300 goto nla_put_failure;
2302 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
2303 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
2304 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
2305 sizeof(key->ipv4.src)) ||
2306 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
2307 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
2308 sizeof(key->ipv4.dst))))
2309 goto nla_put_failure;
2310 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
2311 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
2312 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
2313 sizeof(key->ipv6.src)) ||
2314 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
2315 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
2316 sizeof(key->ipv6.dst))))
2317 goto nla_put_failure;
2319 if (key->basic.ip_proto == IPPROTO_TCP &&
2320 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
2321 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
2322 sizeof(key->tp.src)) ||
2323 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
2324 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
2325 sizeof(key->tp.dst)) ||
2326 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
2327 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
2328 sizeof(key->tcp.flags))))
2329 goto nla_put_failure;
2330 else if (key->basic.ip_proto == IPPROTO_UDP &&
2331 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
2332 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
2333 sizeof(key->tp.src)) ||
2334 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
2335 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
2336 sizeof(key->tp.dst))))
2337 goto nla_put_failure;
2338 else if (key->basic.ip_proto == IPPROTO_SCTP &&
2339 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
2340 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
2341 sizeof(key->tp.src)) ||
2342 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
2343 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
2344 sizeof(key->tp.dst))))
2345 goto nla_put_failure;
2346 else if (key->basic.n_proto == htons(ETH_P_IP) &&
2347 key->basic.ip_proto == IPPROTO_ICMP &&
2348 (fl_dump_key_val(skb, &key->icmp.type,
2349 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
2350 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
2351 sizeof(key->icmp.type)) ||
2352 fl_dump_key_val(skb, &key->icmp.code,
2353 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
2354 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
2355 sizeof(key->icmp.code))))
2356 goto nla_put_failure;
2357 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
2358 key->basic.ip_proto == IPPROTO_ICMPV6 &&
2359 (fl_dump_key_val(skb, &key->icmp.type,
2360 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
2361 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
2362 sizeof(key->icmp.type)) ||
2363 fl_dump_key_val(skb, &key->icmp.code,
2364 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
2365 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
2366 sizeof(key->icmp.code))))
2367 goto nla_put_failure;
2368 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
2369 key->basic.n_proto == htons(ETH_P_RARP)) &&
2370 (fl_dump_key_val(skb, &key->arp.sip,
2371 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
2372 TCA_FLOWER_KEY_ARP_SIP_MASK,
2373 sizeof(key->arp.sip)) ||
2374 fl_dump_key_val(skb, &key->arp.tip,
2375 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
2376 TCA_FLOWER_KEY_ARP_TIP_MASK,
2377 sizeof(key->arp.tip)) ||
2378 fl_dump_key_val(skb, &key->arp.op,
2379 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
2380 TCA_FLOWER_KEY_ARP_OP_MASK,
2381 sizeof(key->arp.op)) ||
2382 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
2383 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
2384 sizeof(key->arp.sha)) ||
2385 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
2386 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
2387 sizeof(key->arp.tha))))
2388 goto nla_put_failure;
2390 if ((key->basic.ip_proto == IPPROTO_TCP ||
2391 key->basic.ip_proto == IPPROTO_UDP ||
2392 key->basic.ip_proto == IPPROTO_SCTP) &&
2393 fl_dump_key_port_range(skb, key, mask))
2394 goto nla_put_failure;
2396 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
2397 (fl_dump_key_val(skb, &key->enc_ipv4.src,
2398 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
2399 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
2400 sizeof(key->enc_ipv4.src)) ||
2401 fl_dump_key_val(skb, &key->enc_ipv4.dst,
2402 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
2403 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
2404 sizeof(key->enc_ipv4.dst))))
2405 goto nla_put_failure;
2406 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
2407 (fl_dump_key_val(skb, &key->enc_ipv6.src,
2408 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
2409 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
2410 sizeof(key->enc_ipv6.src)) ||
2411 fl_dump_key_val(skb, &key->enc_ipv6.dst,
2412 TCA_FLOWER_KEY_ENC_IPV6_DST,
2413 &mask->enc_ipv6.dst,
2414 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
2415 sizeof(key->enc_ipv6.dst))))
2416 goto nla_put_failure;
2418 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
2419 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
2420 sizeof(key->enc_key_id)) ||
2421 fl_dump_key_val(skb, &key->enc_tp.src,
2422 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
2424 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
2425 sizeof(key->enc_tp.src)) ||
2426 fl_dump_key_val(skb, &key->enc_tp.dst,
2427 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
2429 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
2430 sizeof(key->enc_tp.dst)) ||
2431 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
2432 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
2433 goto nla_put_failure;
2435 if (fl_dump_key_ct(skb, &key->ct, &mask->ct))
2436 goto nla_put_failure;
2438 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
2439 goto nla_put_failure;
2447 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
2448 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
2450 struct cls_fl_filter *f = fh;
2451 struct nlattr *nest;
2452 struct fl_flow_key *key, *mask;
2458 t->tcm_handle = f->handle;
2460 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
2462 goto nla_put_failure;
2464 spin_lock(&tp->lock);
2466 if (f->res.classid &&
2467 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
2468 goto nla_put_failure_locked;
2471 mask = &f->mask->key;
2472 skip_hw = tc_skip_hw(f->flags);
2474 if (fl_dump_key(skb, net, key, mask))
2475 goto nla_put_failure_locked;
2477 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
2478 goto nla_put_failure_locked;
2480 spin_unlock(&tp->lock);
2483 fl_hw_update_stats(tp, f, rtnl_held);
2485 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
2486 goto nla_put_failure;
2488 if (tcf_exts_dump(skb, &f->exts))
2489 goto nla_put_failure;
2491 nla_nest_end(skb, nest);
2493 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
2494 goto nla_put_failure;
2498 nla_put_failure_locked:
2499 spin_unlock(&tp->lock);
2501 nla_nest_cancel(skb, nest);
2505 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
2507 struct fl_flow_tmplt *tmplt = tmplt_priv;
2508 struct fl_flow_key *key, *mask;
2509 struct nlattr *nest;
2511 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
2513 goto nla_put_failure;
2515 key = &tmplt->dummy_key;
2516 mask = &tmplt->mask;
2518 if (fl_dump_key(skb, net, key, mask))
2519 goto nla_put_failure;
2521 nla_nest_end(skb, nest);
2526 nla_nest_cancel(skb, nest);
2530 static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
2533 struct cls_fl_filter *f = fh;
2535 if (f && f->res.classid == classid) {
2537 __tcf_bind_filter(q, &f->res, base);
2539 __tcf_unbind_filter(q, &f->res);
2543 static bool fl_delete_empty(struct tcf_proto *tp)
2545 struct cls_fl_head *head = fl_head_dereference(tp);
2547 spin_lock(&tp->lock);
2548 tp->deleting = idr_is_empty(&head->handle_idr);
2549 spin_unlock(&tp->lock);
2551 return tp->deleting;
2554 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
2556 .classify = fl_classify,
2558 .destroy = fl_destroy,
2561 .change = fl_change,
2562 .delete = fl_delete,
2563 .delete_empty = fl_delete_empty,
2565 .reoffload = fl_reoffload,
2566 .hw_add = fl_hw_add,
2567 .hw_del = fl_hw_del,
2569 .bind_class = fl_bind_class,
2570 .tmplt_create = fl_tmplt_create,
2571 .tmplt_destroy = fl_tmplt_destroy,
2572 .tmplt_dump = fl_tmplt_dump,
2573 .owner = THIS_MODULE,
2574 .flags = TCF_PROTO_OPS_DOIT_UNLOCKED,
2577 static int __init cls_fl_init(void)
2579 return register_tcf_proto_ops(&cls_fl_ops);
2582 static void __exit cls_fl_exit(void)
2584 unregister_tcf_proto_ops(&cls_fl_ops);
2587 module_init(cls_fl_init);
2588 module_exit(cls_fl_exit);
2590 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
2591 MODULE_DESCRIPTION("Flower classifier");
2592 MODULE_LICENSE("GPL v2");