2 * net/sched/cls_flower.c Flower classifier
4 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/rhashtable.h>
16 #include <linux/workqueue.h>
17 #include <linux/refcount.h>
19 #include <linux/if_ether.h>
20 #include <linux/in6.h>
22 #include <linux/mpls.h>
24 #include <net/sch_generic.h>
25 #include <net/pkt_cls.h>
27 #include <net/flow_dissector.h>
28 #include <net/geneve.h>
31 #include <net/dst_metadata.h>
35 struct flow_dissector_key_control control;
36 struct flow_dissector_key_control enc_control;
37 struct flow_dissector_key_basic basic;
38 struct flow_dissector_key_eth_addrs eth;
39 struct flow_dissector_key_vlan vlan;
40 struct flow_dissector_key_vlan cvlan;
42 struct flow_dissector_key_ipv4_addrs ipv4;
43 struct flow_dissector_key_ipv6_addrs ipv6;
45 struct flow_dissector_key_ports tp;
46 struct flow_dissector_key_icmp icmp;
47 struct flow_dissector_key_arp arp;
48 struct flow_dissector_key_keyid enc_key_id;
50 struct flow_dissector_key_ipv4_addrs enc_ipv4;
51 struct flow_dissector_key_ipv6_addrs enc_ipv6;
53 struct flow_dissector_key_ports enc_tp;
54 struct flow_dissector_key_mpls mpls;
55 struct flow_dissector_key_tcp tcp;
56 struct flow_dissector_key_ip ip;
57 struct flow_dissector_key_ip enc_ip;
58 struct flow_dissector_key_enc_opts enc_opts;
59 struct flow_dissector_key_ports tp_min;
60 struct flow_dissector_key_ports tp_max;
61 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
63 struct fl_flow_mask_range {
64 unsigned short int start;
65 unsigned short int end;
69 struct fl_flow_key key;
70 struct fl_flow_mask_range range;
72 struct rhash_head ht_node;
74 struct rhashtable_params filter_ht_params;
75 struct flow_dissector dissector;
76 struct list_head filters;
77 struct rcu_work rwork;
78 struct list_head list;
81 struct fl_flow_tmplt {
82 struct fl_flow_key dummy_key;
83 struct fl_flow_key mask;
84 struct flow_dissector dissector;
85 struct tcf_chain *chain;
90 struct list_head masks;
91 struct rcu_work rwork;
92 struct idr handle_idr;
95 struct cls_fl_filter {
96 struct fl_flow_mask *mask;
97 struct rhash_head ht_node;
98 struct fl_flow_key mkey;
100 struct tcf_result res;
101 struct fl_flow_key key;
102 struct list_head list;
106 struct rcu_work rwork;
107 struct net_device *hw_dev;
108 /* Flower classifier is unlocked, which means that its reference counter
109 * can be changed concurrently without any kind of external
110 * synchronization. Use atomic reference counter to be concurrency-safe.
115 static const struct rhashtable_params mask_ht_params = {
116 .key_offset = offsetof(struct fl_flow_mask, key),
117 .key_len = sizeof(struct fl_flow_key),
118 .head_offset = offsetof(struct fl_flow_mask, ht_node),
119 .automatic_shrinking = true,
122 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
124 return mask->range.end - mask->range.start;
127 static void fl_mask_update_range(struct fl_flow_mask *mask)
129 const u8 *bytes = (const u8 *) &mask->key;
130 size_t size = sizeof(mask->key);
131 size_t i, first = 0, last;
133 for (i = 0; i < size; i++) {
140 for (i = size - 1; i != first; i--) {
146 mask->range.start = rounddown(first, sizeof(long));
147 mask->range.end = roundup(last + 1, sizeof(long));
150 static void *fl_key_get_start(struct fl_flow_key *key,
151 const struct fl_flow_mask *mask)
153 return (u8 *) key + mask->range.start;
156 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
157 struct fl_flow_mask *mask)
159 const long *lkey = fl_key_get_start(key, mask);
160 const long *lmask = fl_key_get_start(&mask->key, mask);
161 long *lmkey = fl_key_get_start(mkey, mask);
164 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
165 *lmkey++ = *lkey++ & *lmask++;
168 static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
169 struct fl_flow_mask *mask)
171 const long *lmask = fl_key_get_start(&mask->key, mask);
177 ltmplt = fl_key_get_start(&tmplt->mask, mask);
178 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
179 if (~*ltmplt++ & *lmask++)
185 static void fl_clear_masked_range(struct fl_flow_key *key,
186 struct fl_flow_mask *mask)
188 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
191 static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
192 struct fl_flow_key *key,
193 struct fl_flow_key *mkey)
195 __be16 min_mask, max_mask, min_val, max_val;
197 min_mask = htons(filter->mask->key.tp_min.dst);
198 max_mask = htons(filter->mask->key.tp_max.dst);
199 min_val = htons(filter->key.tp_min.dst);
200 max_val = htons(filter->key.tp_max.dst);
202 if (min_mask && max_mask) {
203 if (htons(key->tp.dst) < min_val ||
204 htons(key->tp.dst) > max_val)
207 /* skb does not have min and max values */
208 mkey->tp_min.dst = filter->mkey.tp_min.dst;
209 mkey->tp_max.dst = filter->mkey.tp_max.dst;
214 static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
215 struct fl_flow_key *key,
216 struct fl_flow_key *mkey)
218 __be16 min_mask, max_mask, min_val, max_val;
220 min_mask = htons(filter->mask->key.tp_min.src);
221 max_mask = htons(filter->mask->key.tp_max.src);
222 min_val = htons(filter->key.tp_min.src);
223 max_val = htons(filter->key.tp_max.src);
225 if (min_mask && max_mask) {
226 if (htons(key->tp.src) < min_val ||
227 htons(key->tp.src) > max_val)
230 /* skb does not have min and max values */
231 mkey->tp_min.src = filter->mkey.tp_min.src;
232 mkey->tp_max.src = filter->mkey.tp_max.src;
237 static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
238 struct fl_flow_key *mkey)
240 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
241 mask->filter_ht_params);
244 static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
245 struct fl_flow_key *mkey,
246 struct fl_flow_key *key)
248 struct cls_fl_filter *filter, *f;
250 list_for_each_entry_rcu(filter, &mask->filters, list) {
251 if (!fl_range_port_dst_cmp(filter, key, mkey))
254 if (!fl_range_port_src_cmp(filter, key, mkey))
257 f = __fl_lookup(mask, mkey);
264 static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
265 struct fl_flow_key *mkey,
266 struct fl_flow_key *key)
268 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
269 return fl_lookup_range(mask, mkey, key);
271 return __fl_lookup(mask, mkey);
274 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
275 struct tcf_result *res)
277 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
278 struct cls_fl_filter *f;
279 struct fl_flow_mask *mask;
280 struct fl_flow_key skb_key;
281 struct fl_flow_key skb_mkey;
283 list_for_each_entry_rcu(mask, &head->masks, list) {
284 fl_clear_masked_range(&skb_key, mask);
286 skb_key.indev_ifindex = skb->skb_iif;
287 /* skb_flow_dissect() does not set n_proto in case an unknown
288 * protocol, so do it rather here.
290 skb_key.basic.n_proto = skb->protocol;
291 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
292 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
294 fl_set_masked_key(&skb_mkey, &skb_key, mask);
296 f = fl_lookup(mask, &skb_mkey, &skb_key);
297 if (f && !tc_skip_sw(f->flags)) {
299 return tcf_exts_exec(skb, &f->exts, res);
305 static int fl_init(struct tcf_proto *tp)
307 struct cls_fl_head *head;
309 head = kzalloc(sizeof(*head), GFP_KERNEL);
313 INIT_LIST_HEAD_RCU(&head->masks);
314 rcu_assign_pointer(tp->root, head);
315 idr_init(&head->handle_idr);
317 return rhashtable_init(&head->ht, &mask_ht_params);
320 static void fl_mask_free(struct fl_flow_mask *mask)
322 rhashtable_destroy(&mask->ht);
326 static void fl_mask_free_work(struct work_struct *work)
328 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
329 struct fl_flow_mask, rwork);
334 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask,
337 if (!list_empty(&mask->filters))
340 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
341 list_del_rcu(&mask->list);
343 tcf_queue_work(&mask->rwork, fl_mask_free_work);
350 static void __fl_destroy_filter(struct cls_fl_filter *f)
352 tcf_exts_destroy(&f->exts);
353 tcf_exts_put_net(&f->exts);
357 static void fl_destroy_filter_work(struct work_struct *work)
359 struct cls_fl_filter *f = container_of(to_rcu_work(work),
360 struct cls_fl_filter, rwork);
363 __fl_destroy_filter(f);
367 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
368 struct netlink_ext_ack *extack)
370 struct tc_cls_flower_offload cls_flower = {};
371 struct tcf_block *block = tp->chain->block;
373 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
374 cls_flower.command = TC_CLSFLOWER_DESTROY;
375 cls_flower.cookie = (unsigned long) f;
377 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
378 tcf_block_offload_dec(block, &f->flags);
381 static int fl_hw_replace_filter(struct tcf_proto *tp,
382 struct cls_fl_filter *f,
383 struct netlink_ext_ack *extack)
385 struct tc_cls_flower_offload cls_flower = {};
386 struct tcf_block *block = tp->chain->block;
387 bool skip_sw = tc_skip_sw(f->flags);
390 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
391 if (!cls_flower.rule)
394 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
395 cls_flower.command = TC_CLSFLOWER_REPLACE;
396 cls_flower.cookie = (unsigned long) f;
397 cls_flower.rule->match.dissector = &f->mask->dissector;
398 cls_flower.rule->match.mask = &f->mask->key;
399 cls_flower.rule->match.key = &f->mkey;
400 cls_flower.classid = f->res.classid;
402 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
404 kfree(cls_flower.rule);
406 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
412 err = tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, skip_sw);
413 kfree(cls_flower.rule);
416 fl_hw_destroy_filter(tp, f, NULL);
418 } else if (err > 0) {
419 f->in_hw_count = err;
420 tcf_block_offload_inc(block, &f->flags);
423 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
429 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
431 struct tc_cls_flower_offload cls_flower = {};
432 struct tcf_block *block = tp->chain->block;
434 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
435 cls_flower.command = TC_CLSFLOWER_STATS;
436 cls_flower.cookie = (unsigned long) f;
437 cls_flower.classid = f->res.classid;
439 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
441 tcf_exts_stats_update(&f->exts, cls_flower.stats.bytes,
442 cls_flower.stats.pkts,
443 cls_flower.stats.lastused);
446 static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
448 /* Flower classifier only changes root pointer during init and destroy.
449 * Users must obtain reference to tcf_proto instance before calling its
450 * API, so tp->root pointer is protected from concurrent call to
451 * fl_destroy() by reference counting.
453 return rcu_dereference_raw(tp->root);
456 static void __fl_put(struct cls_fl_filter *f)
458 if (!refcount_dec_and_test(&f->refcnt))
461 if (tcf_exts_get_net(&f->exts))
462 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
464 __fl_destroy_filter(f);
467 static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
469 struct cls_fl_filter *f;
472 f = idr_find(&head->handle_idr, handle);
473 if (f && !refcount_inc_not_zero(&f->refcnt))
480 static struct cls_fl_filter *fl_get_next_filter(struct tcf_proto *tp,
481 unsigned long *handle)
483 struct cls_fl_head *head = fl_head_dereference(tp);
484 struct cls_fl_filter *f;
487 while ((f = idr_get_next_ul(&head->handle_idr, handle))) {
488 /* don't return filters that are being deleted */
489 if (refcount_inc_not_zero(&f->refcnt))
498 static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
499 struct netlink_ext_ack *extack)
501 struct cls_fl_head *head = fl_head_dereference(tp);
502 bool async = tcf_exts_get_net(&f->exts);
505 idr_remove(&head->handle_idr, f->handle);
506 list_del_rcu(&f->list);
507 last = fl_mask_put(head, f->mask, async);
508 if (!tc_skip_hw(f->flags))
509 fl_hw_destroy_filter(tp, f, extack);
510 tcf_unbind_filter(tp, &f->res);
516 static void fl_destroy_sleepable(struct work_struct *work)
518 struct cls_fl_head *head = container_of(to_rcu_work(work),
522 rhashtable_destroy(&head->ht);
524 module_put(THIS_MODULE);
527 static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
528 struct netlink_ext_ack *extack)
530 struct cls_fl_head *head = fl_head_dereference(tp);
531 struct fl_flow_mask *mask, *next_mask;
532 struct cls_fl_filter *f, *next;
534 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
535 list_for_each_entry_safe(f, next, &mask->filters, list) {
536 if (__fl_delete(tp, f, extack))
540 idr_destroy(&head->handle_idr);
542 __module_get(THIS_MODULE);
543 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
546 static void fl_put(struct tcf_proto *tp, void *arg)
548 struct cls_fl_filter *f = arg;
553 static void *fl_get(struct tcf_proto *tp, u32 handle)
555 struct cls_fl_head *head = fl_head_dereference(tp);
557 return __fl_get(head, handle);
560 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
561 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
562 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
563 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
565 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
566 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
567 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
568 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
569 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
570 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
571 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
572 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
573 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
574 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
575 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
576 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
577 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
578 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
579 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
580 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
581 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
582 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
583 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
584 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
585 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
586 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
587 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
588 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
589 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
590 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
591 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
592 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
593 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
594 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
595 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
596 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
597 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
598 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
599 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
600 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
601 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
602 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
603 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
604 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
605 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
606 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
607 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
608 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
609 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
610 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
611 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
612 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
613 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
614 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
615 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
616 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
617 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
618 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
619 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
620 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
621 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
622 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
623 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
624 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
625 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
626 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
627 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
628 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
629 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
630 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
631 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
632 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
633 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
634 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
635 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
636 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
637 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
638 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
639 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
640 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
641 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
642 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
643 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
644 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
645 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
648 static const struct nla_policy
649 enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
650 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
653 static const struct nla_policy
654 geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
655 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
656 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
657 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
661 static void fl_set_key_val(struct nlattr **tb,
662 void *val, int val_type,
663 void *mask, int mask_type, int len)
667 memcpy(val, nla_data(tb[val_type]), len);
668 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
669 memset(mask, 0xff, len);
671 memcpy(mask, nla_data(tb[mask_type]), len);
674 static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
675 struct fl_flow_key *mask)
677 fl_set_key_val(tb, &key->tp_min.dst,
678 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_min.dst,
679 TCA_FLOWER_UNSPEC, sizeof(key->tp_min.dst));
680 fl_set_key_val(tb, &key->tp_max.dst,
681 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_max.dst,
682 TCA_FLOWER_UNSPEC, sizeof(key->tp_max.dst));
683 fl_set_key_val(tb, &key->tp_min.src,
684 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_min.src,
685 TCA_FLOWER_UNSPEC, sizeof(key->tp_min.src));
686 fl_set_key_val(tb, &key->tp_max.src,
687 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_max.src,
688 TCA_FLOWER_UNSPEC, sizeof(key->tp_max.src));
690 if ((mask->tp_min.dst && mask->tp_max.dst &&
691 htons(key->tp_max.dst) <= htons(key->tp_min.dst)) ||
692 (mask->tp_min.src && mask->tp_max.src &&
693 htons(key->tp_max.src) <= htons(key->tp_min.src)))
699 static int fl_set_key_mpls(struct nlattr **tb,
700 struct flow_dissector_key_mpls *key_val,
701 struct flow_dissector_key_mpls *key_mask)
703 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
704 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
705 key_mask->mpls_ttl = MPLS_TTL_MASK;
707 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
708 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
710 if (bos & ~MPLS_BOS_MASK)
712 key_val->mpls_bos = bos;
713 key_mask->mpls_bos = MPLS_BOS_MASK;
715 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
716 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
718 if (tc & ~MPLS_TC_MASK)
720 key_val->mpls_tc = tc;
721 key_mask->mpls_tc = MPLS_TC_MASK;
723 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
724 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
726 if (label & ~MPLS_LABEL_MASK)
728 key_val->mpls_label = label;
729 key_mask->mpls_label = MPLS_LABEL_MASK;
734 static void fl_set_key_vlan(struct nlattr **tb,
736 int vlan_id_key, int vlan_prio_key,
737 struct flow_dissector_key_vlan *key_val,
738 struct flow_dissector_key_vlan *key_mask)
740 #define VLAN_PRIORITY_MASK 0x7
742 if (tb[vlan_id_key]) {
744 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
745 key_mask->vlan_id = VLAN_VID_MASK;
747 if (tb[vlan_prio_key]) {
748 key_val->vlan_priority =
749 nla_get_u8(tb[vlan_prio_key]) &
751 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
753 key_val->vlan_tpid = ethertype;
754 key_mask->vlan_tpid = cpu_to_be16(~0);
757 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
758 u32 *dissector_key, u32 *dissector_mask,
759 u32 flower_flag_bit, u32 dissector_flag_bit)
761 if (flower_mask & flower_flag_bit) {
762 *dissector_mask |= dissector_flag_bit;
763 if (flower_key & flower_flag_bit)
764 *dissector_key |= dissector_flag_bit;
768 static int fl_set_key_flags(struct nlattr **tb,
769 u32 *flags_key, u32 *flags_mask)
773 /* mask is mandatory for flags */
774 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
777 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
778 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
783 fl_set_key_flag(key, mask, flags_key, flags_mask,
784 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
785 fl_set_key_flag(key, mask, flags_key, flags_mask,
786 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
787 FLOW_DIS_FIRST_FRAG);
792 static void fl_set_key_ip(struct nlattr **tb, bool encap,
793 struct flow_dissector_key_ip *key,
794 struct flow_dissector_key_ip *mask)
796 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
797 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
798 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
799 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
801 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
802 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
805 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
806 int depth, int option_len,
807 struct netlink_ext_ack *extack)
809 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
810 struct nlattr *class = NULL, *type = NULL, *data = NULL;
811 struct geneve_opt *opt;
812 int err, data_len = 0;
814 if (option_len > sizeof(struct geneve_opt))
815 data_len = option_len - sizeof(struct geneve_opt);
817 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
818 memset(opt, 0xff, option_len);
819 opt->length = data_len / 4;
824 /* If no mask has been prodived we assume an exact match. */
826 return sizeof(struct geneve_opt) + data_len;
828 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
829 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
833 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
834 nla, geneve_opt_policy, extack);
838 /* We are not allowed to omit any of CLASS, TYPE or DATA
839 * fields from the key.
842 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
843 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
844 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
845 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
849 /* Omitting any of CLASS, TYPE or DATA fields is allowed
852 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
853 int new_len = key->enc_opts.len;
855 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
856 data_len = nla_len(data);
858 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
862 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
866 new_len += sizeof(struct geneve_opt) + data_len;
867 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
868 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
869 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
872 opt->length = data_len / 4;
873 memcpy(opt->opt_data, nla_data(data), data_len);
876 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
877 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
878 opt->opt_class = nla_get_be16(class);
881 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
882 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
883 opt->type = nla_get_u8(type);
886 return sizeof(struct geneve_opt) + data_len;
889 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
890 struct fl_flow_key *mask,
891 struct netlink_ext_ack *extack)
893 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
894 int err, option_len, key_depth, msk_depth = 0;
896 err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS],
897 TCA_FLOWER_KEY_ENC_OPTS_MAX,
898 enc_opts_policy, extack);
902 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
904 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
905 err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
906 TCA_FLOWER_KEY_ENC_OPTS_MAX,
907 enc_opts_policy, extack);
911 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
912 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
915 nla_for_each_attr(nla_opt_key, nla_enc_key,
916 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
917 switch (nla_type(nla_opt_key)) {
918 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
920 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
921 option_len = fl_set_geneve_opt(nla_opt_key, key,
922 key_depth, option_len,
927 key->enc_opts.len += option_len;
928 /* At the same time we need to parse through the mask
929 * in order to verify exact and mask attribute lengths.
931 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
932 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
933 msk_depth, option_len,
938 mask->enc_opts.len += option_len;
939 if (key->enc_opts.len != mask->enc_opts.len) {
940 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
945 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
948 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
956 static int fl_set_key(struct net *net, struct nlattr **tb,
957 struct fl_flow_key *key, struct fl_flow_key *mask,
958 struct netlink_ext_ack *extack)
962 #ifdef CONFIG_NET_CLS_IND
963 if (tb[TCA_FLOWER_INDEV]) {
964 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
967 key->indev_ifindex = err;
968 mask->indev_ifindex = 0xffffffff;
972 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
973 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
974 sizeof(key->eth.dst));
975 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
976 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
977 sizeof(key->eth.src));
979 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
980 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
982 if (eth_type_vlan(ethertype)) {
983 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
984 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
987 if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
988 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
989 if (eth_type_vlan(ethertype)) {
990 fl_set_key_vlan(tb, ethertype,
991 TCA_FLOWER_KEY_CVLAN_ID,
992 TCA_FLOWER_KEY_CVLAN_PRIO,
993 &key->cvlan, &mask->cvlan);
994 fl_set_key_val(tb, &key->basic.n_proto,
995 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
996 &mask->basic.n_proto,
998 sizeof(key->basic.n_proto));
1000 key->basic.n_proto = ethertype;
1001 mask->basic.n_proto = cpu_to_be16(~0);
1005 key->basic.n_proto = ethertype;
1006 mask->basic.n_proto = cpu_to_be16(~0);
1010 if (key->basic.n_proto == htons(ETH_P_IP) ||
1011 key->basic.n_proto == htons(ETH_P_IPV6)) {
1012 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1013 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1014 sizeof(key->basic.ip_proto));
1015 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
1018 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1019 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1020 mask->control.addr_type = ~0;
1021 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1022 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1023 sizeof(key->ipv4.src));
1024 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1025 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1026 sizeof(key->ipv4.dst));
1027 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1028 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1029 mask->control.addr_type = ~0;
1030 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1031 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1032 sizeof(key->ipv6.src));
1033 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1034 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1035 sizeof(key->ipv6.dst));
1038 if (key->basic.ip_proto == IPPROTO_TCP) {
1039 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1040 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1041 sizeof(key->tp.src));
1042 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1043 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1044 sizeof(key->tp.dst));
1045 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1046 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1047 sizeof(key->tcp.flags));
1048 } else if (key->basic.ip_proto == IPPROTO_UDP) {
1049 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1050 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1051 sizeof(key->tp.src));
1052 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1053 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1054 sizeof(key->tp.dst));
1055 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
1056 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1057 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1058 sizeof(key->tp.src));
1059 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1060 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1061 sizeof(key->tp.dst));
1062 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
1063 key->basic.ip_proto == IPPROTO_ICMP) {
1064 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1066 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1067 sizeof(key->icmp.type));
1068 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1070 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1071 sizeof(key->icmp.code));
1072 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1073 key->basic.ip_proto == IPPROTO_ICMPV6) {
1074 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1076 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1077 sizeof(key->icmp.type));
1078 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
1080 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1081 sizeof(key->icmp.code));
1082 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1083 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
1084 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
1087 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
1088 key->basic.n_proto == htons(ETH_P_RARP)) {
1089 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
1090 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
1091 sizeof(key->arp.sip));
1092 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
1093 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
1094 sizeof(key->arp.tip));
1095 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
1096 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
1097 sizeof(key->arp.op));
1098 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1099 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1100 sizeof(key->arp.sha));
1101 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1102 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1103 sizeof(key->arp.tha));
1106 if (key->basic.ip_proto == IPPROTO_TCP ||
1107 key->basic.ip_proto == IPPROTO_UDP ||
1108 key->basic.ip_proto == IPPROTO_SCTP) {
1109 ret = fl_set_key_port_range(tb, key, mask);
1114 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
1115 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
1116 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1117 mask->enc_control.addr_type = ~0;
1118 fl_set_key_val(tb, &key->enc_ipv4.src,
1119 TCA_FLOWER_KEY_ENC_IPV4_SRC,
1120 &mask->enc_ipv4.src,
1121 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1122 sizeof(key->enc_ipv4.src));
1123 fl_set_key_val(tb, &key->enc_ipv4.dst,
1124 TCA_FLOWER_KEY_ENC_IPV4_DST,
1125 &mask->enc_ipv4.dst,
1126 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1127 sizeof(key->enc_ipv4.dst));
1130 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
1131 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
1132 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1133 mask->enc_control.addr_type = ~0;
1134 fl_set_key_val(tb, &key->enc_ipv6.src,
1135 TCA_FLOWER_KEY_ENC_IPV6_SRC,
1136 &mask->enc_ipv6.src,
1137 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1138 sizeof(key->enc_ipv6.src));
1139 fl_set_key_val(tb, &key->enc_ipv6.dst,
1140 TCA_FLOWER_KEY_ENC_IPV6_DST,
1141 &mask->enc_ipv6.dst,
1142 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1143 sizeof(key->enc_ipv6.dst));
1146 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
1147 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
1148 sizeof(key->enc_key_id.keyid));
1150 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1151 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1152 sizeof(key->enc_tp.src));
1154 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1155 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1156 sizeof(key->enc_tp.dst));
1158 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
1160 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
1161 ret = fl_set_enc_opt(tb, key, mask, extack);
1166 if (tb[TCA_FLOWER_KEY_FLAGS])
1167 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
1172 static void fl_mask_copy(struct fl_flow_mask *dst,
1173 struct fl_flow_mask *src)
1175 const void *psrc = fl_key_get_start(&src->key, src);
1176 void *pdst = fl_key_get_start(&dst->key, src);
1178 memcpy(pdst, psrc, fl_mask_range(src));
1179 dst->range = src->range;
1182 static const struct rhashtable_params fl_ht_params = {
1183 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
1184 .head_offset = offsetof(struct cls_fl_filter, ht_node),
1185 .automatic_shrinking = true,
1188 static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
1190 mask->filter_ht_params = fl_ht_params;
1191 mask->filter_ht_params.key_len = fl_mask_range(mask);
1192 mask->filter_ht_params.key_offset += mask->range.start;
1194 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
1197 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
1198 #define FL_KEY_MEMBER_SIZE(member) FIELD_SIZEOF(struct fl_flow_key, member)
1200 #define FL_KEY_IS_MASKED(mask, member) \
1201 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
1202 0, FL_KEY_MEMBER_SIZE(member)) \
1204 #define FL_KEY_SET(keys, cnt, id, member) \
1206 keys[cnt].key_id = id; \
1207 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
1211 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
1213 if (FL_KEY_IS_MASKED(mask, member)) \
1214 FL_KEY_SET(keys, cnt, id, member); \
1217 static void fl_init_dissector(struct flow_dissector *dissector,
1218 struct fl_flow_key *mask)
1220 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1223 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
1224 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
1225 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1226 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
1227 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1228 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
1229 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1230 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
1231 if (FL_KEY_IS_MASKED(mask, tp) ||
1232 FL_KEY_IS_MASKED(mask, tp_min) || FL_KEY_IS_MASKED(mask, tp_max))
1233 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_PORTS, tp);
1234 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1235 FLOW_DISSECTOR_KEY_IP, ip);
1236 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1237 FLOW_DISSECTOR_KEY_TCP, tcp);
1238 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1239 FLOW_DISSECTOR_KEY_ICMP, icmp);
1240 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1241 FLOW_DISSECTOR_KEY_ARP, arp);
1242 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1243 FLOW_DISSECTOR_KEY_MPLS, mpls);
1244 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1245 FLOW_DISSECTOR_KEY_VLAN, vlan);
1246 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1247 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
1248 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1249 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
1250 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1251 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
1252 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1253 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
1254 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1255 FL_KEY_IS_MASKED(mask, enc_ipv6))
1256 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1258 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1259 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
1260 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1261 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
1262 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1263 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
1265 skb_flow_dissector_init(dissector, keys, cnt);
1268 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1269 struct fl_flow_mask *mask)
1271 struct fl_flow_mask *newmask;
1274 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1276 return ERR_PTR(-ENOMEM);
1278 fl_mask_copy(newmask, mask);
1280 if ((newmask->key.tp_min.dst && newmask->key.tp_max.dst) ||
1281 (newmask->key.tp_min.src && newmask->key.tp_max.src))
1282 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
1284 err = fl_init_mask_hashtable(newmask);
1288 fl_init_dissector(&newmask->dissector, &newmask->key);
1290 INIT_LIST_HEAD_RCU(&newmask->filters);
1292 err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
1295 goto errout_destroy;
1297 list_add_tail_rcu(&newmask->list, &head->masks);
1302 rhashtable_destroy(&newmask->ht);
1306 return ERR_PTR(err);
1309 static int fl_check_assign_mask(struct cls_fl_head *head,
1310 struct cls_fl_filter *fnew,
1311 struct cls_fl_filter *fold,
1312 struct fl_flow_mask *mask)
1314 struct fl_flow_mask *newmask;
1316 fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
1321 newmask = fl_create_new_mask(head, mask);
1322 if (IS_ERR(newmask))
1323 return PTR_ERR(newmask);
1325 fnew->mask = newmask;
1326 } else if (fold && fold->mask != fnew->mask) {
1333 static int fl_set_parms(struct net *net, struct tcf_proto *tp,
1334 struct cls_fl_filter *f, struct fl_flow_mask *mask,
1335 unsigned long base, struct nlattr **tb,
1336 struct nlattr *est, bool ovr,
1337 struct fl_flow_tmplt *tmplt,
1338 struct netlink_ext_ack *extack)
1342 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, true,
1347 if (tb[TCA_FLOWER_CLASSID]) {
1348 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
1349 tcf_bind_filter(tp, &f->res, base);
1352 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
1356 fl_mask_update_range(mask);
1357 fl_set_masked_key(&f->mkey, &f->key, mask);
1359 if (!fl_mask_fits_tmplt(tmplt, mask)) {
1360 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
1367 static int fl_change(struct net *net, struct sk_buff *in_skb,
1368 struct tcf_proto *tp, unsigned long base,
1369 u32 handle, struct nlattr **tca,
1370 void **arg, bool ovr, bool rtnl_held,
1371 struct netlink_ext_ack *extack)
1373 struct cls_fl_head *head = fl_head_dereference(tp);
1374 struct cls_fl_filter *fold = *arg;
1375 struct cls_fl_filter *fnew;
1376 struct fl_flow_mask *mask;
1380 if (!tca[TCA_OPTIONS]) {
1385 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
1391 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1394 goto errout_mask_alloc;
1397 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
1402 if (fold && handle && fold->handle != handle) {
1407 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
1412 refcount_set(&fnew->refcnt, 1);
1414 err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0);
1418 if (tb[TCA_FLOWER_FLAGS]) {
1419 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1421 if (!tc_flags_valid(fnew->flags)) {
1427 err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr,
1428 tp->chain->tmplt_priv, extack);
1432 err = fl_check_assign_mask(head, fnew, fold, mask);
1436 if (!tc_skip_hw(fnew->flags)) {
1437 err = fl_hw_replace_filter(tp, fnew, extack);
1442 if (!tc_in_hw(fnew->flags))
1443 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1445 refcount_inc(&fnew->refcnt);
1447 fnew->handle = handle;
1449 err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1450 fnew->mask->filter_ht_params);
1454 rhashtable_remove_fast(&fold->mask->ht,
1456 fold->mask->filter_ht_params);
1457 idr_replace(&head->handle_idr, fnew, fnew->handle);
1458 list_replace_rcu(&fold->list, &fnew->list);
1460 if (!tc_skip_hw(fold->flags))
1461 fl_hw_destroy_filter(tp, fold, NULL);
1462 tcf_unbind_filter(tp, &fold->res);
1463 tcf_exts_get_net(&fold->exts);
1464 /* Caller holds reference to fold, so refcnt is always > 0
1467 refcount_dec(&fold->refcnt);
1470 if (__fl_lookup(fnew->mask, &fnew->mkey)) {
1476 /* user specifies a handle and it doesn't exist */
1477 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1478 handle, GFP_ATOMIC);
1481 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1482 INT_MAX, GFP_ATOMIC);
1487 fnew->handle = handle;
1489 err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1490 fnew->mask->filter_ht_params);
1494 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
1504 idr_remove(&head->handle_idr, fnew->handle);
1506 if (!tc_skip_hw(fnew->flags))
1507 fl_hw_destroy_filter(tp, fnew, NULL);
1509 fl_mask_put(head, fnew->mask, false);
1511 tcf_exts_destroy(&fnew->exts);
1523 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1524 bool rtnl_held, struct netlink_ext_ack *extack)
1526 struct cls_fl_head *head = fl_head_dereference(tp);
1527 struct cls_fl_filter *f = arg;
1529 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
1530 f->mask->filter_ht_params);
1531 __fl_delete(tp, f, extack);
1532 *last = list_empty(&head->masks);
1538 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
1541 struct cls_fl_filter *f;
1543 arg->count = arg->skip;
1545 while ((f = fl_get_next_filter(tp, &arg->cookie)) != NULL) {
1546 if (arg->fn(tp, f, arg) < 0) {
1557 static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1558 void *cb_priv, struct netlink_ext_ack *extack)
1560 struct cls_fl_head *head = fl_head_dereference(tp);
1561 struct tc_cls_flower_offload cls_flower = {};
1562 struct tcf_block *block = tp->chain->block;
1563 struct fl_flow_mask *mask;
1564 struct cls_fl_filter *f;
1567 list_for_each_entry(mask, &head->masks, list) {
1568 list_for_each_entry(f, &mask->filters, list) {
1569 if (tc_skip_hw(f->flags))
1573 flow_rule_alloc(tcf_exts_num_actions(&f->exts));
1574 if (!cls_flower.rule)
1577 tc_cls_common_offload_init(&cls_flower.common, tp,
1579 cls_flower.command = add ?
1580 TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
1581 cls_flower.cookie = (unsigned long)f;
1582 cls_flower.rule->match.dissector = &mask->dissector;
1583 cls_flower.rule->match.mask = &mask->key;
1584 cls_flower.rule->match.key = &f->mkey;
1586 err = tc_setup_flow_action(&cls_flower.rule->action,
1589 kfree(cls_flower.rule);
1590 if (tc_skip_sw(f->flags)) {
1591 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
1597 cls_flower.classid = f->res.classid;
1599 err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1600 kfree(cls_flower.rule);
1603 if (add && tc_skip_sw(f->flags))
1608 tc_cls_offload_cnt_update(block, &f->in_hw_count,
1616 static int fl_hw_create_tmplt(struct tcf_chain *chain,
1617 struct fl_flow_tmplt *tmplt)
1619 struct tc_cls_flower_offload cls_flower = {};
1620 struct tcf_block *block = chain->block;
1622 cls_flower.rule = flow_rule_alloc(0);
1623 if (!cls_flower.rule)
1626 cls_flower.common.chain_index = chain->index;
1627 cls_flower.command = TC_CLSFLOWER_TMPLT_CREATE;
1628 cls_flower.cookie = (unsigned long) tmplt;
1629 cls_flower.rule->match.dissector = &tmplt->dissector;
1630 cls_flower.rule->match.mask = &tmplt->mask;
1631 cls_flower.rule->match.key = &tmplt->dummy_key;
1633 /* We don't care if driver (any of them) fails to handle this
1634 * call. It serves just as a hint for it.
1636 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
1637 kfree(cls_flower.rule);
1642 static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
1643 struct fl_flow_tmplt *tmplt)
1645 struct tc_cls_flower_offload cls_flower = {};
1646 struct tcf_block *block = chain->block;
1648 cls_flower.common.chain_index = chain->index;
1649 cls_flower.command = TC_CLSFLOWER_TMPLT_DESTROY;
1650 cls_flower.cookie = (unsigned long) tmplt;
1652 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
1655 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
1656 struct nlattr **tca,
1657 struct netlink_ext_ack *extack)
1659 struct fl_flow_tmplt *tmplt;
1663 if (!tca[TCA_OPTIONS])
1664 return ERR_PTR(-EINVAL);
1666 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1668 return ERR_PTR(-ENOBUFS);
1669 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
1674 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
1679 tmplt->chain = chain;
1680 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
1684 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
1686 err = fl_hw_create_tmplt(chain, tmplt);
1697 return ERR_PTR(err);
1700 static void fl_tmplt_destroy(void *tmplt_priv)
1702 struct fl_flow_tmplt *tmplt = tmplt_priv;
1704 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
1708 static int fl_dump_key_val(struct sk_buff *skb,
1709 void *val, int val_type,
1710 void *mask, int mask_type, int len)
1714 if (!memchr_inv(mask, 0, len))
1716 err = nla_put(skb, val_type, len, val);
1719 if (mask_type != TCA_FLOWER_UNSPEC) {
1720 err = nla_put(skb, mask_type, len, mask);
1727 static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
1728 struct fl_flow_key *mask)
1730 if (fl_dump_key_val(skb, &key->tp_min.dst, TCA_FLOWER_KEY_PORT_DST_MIN,
1731 &mask->tp_min.dst, TCA_FLOWER_UNSPEC,
1732 sizeof(key->tp_min.dst)) ||
1733 fl_dump_key_val(skb, &key->tp_max.dst, TCA_FLOWER_KEY_PORT_DST_MAX,
1734 &mask->tp_max.dst, TCA_FLOWER_UNSPEC,
1735 sizeof(key->tp_max.dst)) ||
1736 fl_dump_key_val(skb, &key->tp_min.src, TCA_FLOWER_KEY_PORT_SRC_MIN,
1737 &mask->tp_min.src, TCA_FLOWER_UNSPEC,
1738 sizeof(key->tp_min.src)) ||
1739 fl_dump_key_val(skb, &key->tp_max.src, TCA_FLOWER_KEY_PORT_SRC_MAX,
1740 &mask->tp_max.src, TCA_FLOWER_UNSPEC,
1741 sizeof(key->tp_max.src)))
1747 static int fl_dump_key_mpls(struct sk_buff *skb,
1748 struct flow_dissector_key_mpls *mpls_key,
1749 struct flow_dissector_key_mpls *mpls_mask)
1753 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1755 if (mpls_mask->mpls_ttl) {
1756 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1757 mpls_key->mpls_ttl);
1761 if (mpls_mask->mpls_tc) {
1762 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1767 if (mpls_mask->mpls_label) {
1768 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1769 mpls_key->mpls_label);
1773 if (mpls_mask->mpls_bos) {
1774 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1775 mpls_key->mpls_bos);
1782 static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
1783 struct flow_dissector_key_ip *key,
1784 struct flow_dissector_key_ip *mask)
1786 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1787 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1788 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1789 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1791 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
1792 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
1798 static int fl_dump_key_vlan(struct sk_buff *skb,
1799 int vlan_id_key, int vlan_prio_key,
1800 struct flow_dissector_key_vlan *vlan_key,
1801 struct flow_dissector_key_vlan *vlan_mask)
1805 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1807 if (vlan_mask->vlan_id) {
1808 err = nla_put_u16(skb, vlan_id_key,
1813 if (vlan_mask->vlan_priority) {
1814 err = nla_put_u8(skb, vlan_prio_key,
1815 vlan_key->vlan_priority);
1822 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1823 u32 *flower_key, u32 *flower_mask,
1824 u32 flower_flag_bit, u32 dissector_flag_bit)
1826 if (dissector_mask & dissector_flag_bit) {
1827 *flower_mask |= flower_flag_bit;
1828 if (dissector_key & dissector_flag_bit)
1829 *flower_key |= flower_flag_bit;
1833 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1839 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1845 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1846 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1847 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1848 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1849 FLOW_DIS_FIRST_FRAG);
1851 _key = cpu_to_be32(key);
1852 _mask = cpu_to_be32(mask);
1854 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1858 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1861 static int fl_dump_key_geneve_opt(struct sk_buff *skb,
1862 struct flow_dissector_key_enc_opts *enc_opts)
1864 struct geneve_opt *opt;
1865 struct nlattr *nest;
1868 nest = nla_nest_start(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
1870 goto nla_put_failure;
1872 while (enc_opts->len > opt_off) {
1873 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
1875 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
1877 goto nla_put_failure;
1878 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
1880 goto nla_put_failure;
1881 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
1882 opt->length * 4, opt->opt_data))
1883 goto nla_put_failure;
1885 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
1887 nla_nest_end(skb, nest);
1891 nla_nest_cancel(skb, nest);
1895 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
1896 struct flow_dissector_key_enc_opts *enc_opts)
1898 struct nlattr *nest;
1904 nest = nla_nest_start(skb, enc_opt_type);
1906 goto nla_put_failure;
1908 switch (enc_opts->dst_opt_type) {
1909 case TUNNEL_GENEVE_OPT:
1910 err = fl_dump_key_geneve_opt(skb, enc_opts);
1912 goto nla_put_failure;
1915 goto nla_put_failure;
1917 nla_nest_end(skb, nest);
1921 nla_nest_cancel(skb, nest);
1925 static int fl_dump_key_enc_opt(struct sk_buff *skb,
1926 struct flow_dissector_key_enc_opts *key_opts,
1927 struct flow_dissector_key_enc_opts *msk_opts)
1931 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
1935 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
1938 static int fl_dump_key(struct sk_buff *skb, struct net *net,
1939 struct fl_flow_key *key, struct fl_flow_key *mask)
1941 if (mask->indev_ifindex) {
1942 struct net_device *dev;
1944 dev = __dev_get_by_index(net, key->indev_ifindex);
1945 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1946 goto nla_put_failure;
1949 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1950 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1951 sizeof(key->eth.dst)) ||
1952 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1953 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1954 sizeof(key->eth.src)) ||
1955 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1956 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1957 sizeof(key->basic.n_proto)))
1958 goto nla_put_failure;
1960 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1961 goto nla_put_failure;
1963 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
1964 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
1965 goto nla_put_failure;
1967 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
1968 TCA_FLOWER_KEY_CVLAN_PRIO,
1969 &key->cvlan, &mask->cvlan) ||
1970 (mask->cvlan.vlan_tpid &&
1971 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1972 key->cvlan.vlan_tpid)))
1973 goto nla_put_failure;
1975 if (mask->basic.n_proto) {
1976 if (mask->cvlan.vlan_tpid) {
1977 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1978 key->basic.n_proto))
1979 goto nla_put_failure;
1980 } else if (mask->vlan.vlan_tpid) {
1981 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1982 key->basic.n_proto))
1983 goto nla_put_failure;
1987 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1988 key->basic.n_proto == htons(ETH_P_IPV6)) &&
1989 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1990 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1991 sizeof(key->basic.ip_proto)) ||
1992 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
1993 goto nla_put_failure;
1995 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1996 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1997 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1998 sizeof(key->ipv4.src)) ||
1999 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
2000 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
2001 sizeof(key->ipv4.dst))))
2002 goto nla_put_failure;
2003 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
2004 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
2005 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
2006 sizeof(key->ipv6.src)) ||
2007 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
2008 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
2009 sizeof(key->ipv6.dst))))
2010 goto nla_put_failure;
2012 if (key->basic.ip_proto == IPPROTO_TCP &&
2013 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
2014 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
2015 sizeof(key->tp.src)) ||
2016 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
2017 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
2018 sizeof(key->tp.dst)) ||
2019 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
2020 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
2021 sizeof(key->tcp.flags))))
2022 goto nla_put_failure;
2023 else if (key->basic.ip_proto == IPPROTO_UDP &&
2024 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
2025 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
2026 sizeof(key->tp.src)) ||
2027 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
2028 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
2029 sizeof(key->tp.dst))))
2030 goto nla_put_failure;
2031 else if (key->basic.ip_proto == IPPROTO_SCTP &&
2032 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
2033 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
2034 sizeof(key->tp.src)) ||
2035 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
2036 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
2037 sizeof(key->tp.dst))))
2038 goto nla_put_failure;
2039 else if (key->basic.n_proto == htons(ETH_P_IP) &&
2040 key->basic.ip_proto == IPPROTO_ICMP &&
2041 (fl_dump_key_val(skb, &key->icmp.type,
2042 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
2043 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
2044 sizeof(key->icmp.type)) ||
2045 fl_dump_key_val(skb, &key->icmp.code,
2046 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
2047 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
2048 sizeof(key->icmp.code))))
2049 goto nla_put_failure;
2050 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
2051 key->basic.ip_proto == IPPROTO_ICMPV6 &&
2052 (fl_dump_key_val(skb, &key->icmp.type,
2053 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
2054 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
2055 sizeof(key->icmp.type)) ||
2056 fl_dump_key_val(skb, &key->icmp.code,
2057 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
2058 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
2059 sizeof(key->icmp.code))))
2060 goto nla_put_failure;
2061 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
2062 key->basic.n_proto == htons(ETH_P_RARP)) &&
2063 (fl_dump_key_val(skb, &key->arp.sip,
2064 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
2065 TCA_FLOWER_KEY_ARP_SIP_MASK,
2066 sizeof(key->arp.sip)) ||
2067 fl_dump_key_val(skb, &key->arp.tip,
2068 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
2069 TCA_FLOWER_KEY_ARP_TIP_MASK,
2070 sizeof(key->arp.tip)) ||
2071 fl_dump_key_val(skb, &key->arp.op,
2072 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
2073 TCA_FLOWER_KEY_ARP_OP_MASK,
2074 sizeof(key->arp.op)) ||
2075 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
2076 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
2077 sizeof(key->arp.sha)) ||
2078 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
2079 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
2080 sizeof(key->arp.tha))))
2081 goto nla_put_failure;
2083 if ((key->basic.ip_proto == IPPROTO_TCP ||
2084 key->basic.ip_proto == IPPROTO_UDP ||
2085 key->basic.ip_proto == IPPROTO_SCTP) &&
2086 fl_dump_key_port_range(skb, key, mask))
2087 goto nla_put_failure;
2089 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
2090 (fl_dump_key_val(skb, &key->enc_ipv4.src,
2091 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
2092 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
2093 sizeof(key->enc_ipv4.src)) ||
2094 fl_dump_key_val(skb, &key->enc_ipv4.dst,
2095 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
2096 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
2097 sizeof(key->enc_ipv4.dst))))
2098 goto nla_put_failure;
2099 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
2100 (fl_dump_key_val(skb, &key->enc_ipv6.src,
2101 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
2102 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
2103 sizeof(key->enc_ipv6.src)) ||
2104 fl_dump_key_val(skb, &key->enc_ipv6.dst,
2105 TCA_FLOWER_KEY_ENC_IPV6_DST,
2106 &mask->enc_ipv6.dst,
2107 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
2108 sizeof(key->enc_ipv6.dst))))
2109 goto nla_put_failure;
2111 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
2112 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
2113 sizeof(key->enc_key_id)) ||
2114 fl_dump_key_val(skb, &key->enc_tp.src,
2115 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
2117 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
2118 sizeof(key->enc_tp.src)) ||
2119 fl_dump_key_val(skb, &key->enc_tp.dst,
2120 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
2122 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
2123 sizeof(key->enc_tp.dst)) ||
2124 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
2125 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
2126 goto nla_put_failure;
2128 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
2129 goto nla_put_failure;
2137 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
2138 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
2140 struct cls_fl_filter *f = fh;
2141 struct nlattr *nest;
2142 struct fl_flow_key *key, *mask;
2147 t->tcm_handle = f->handle;
2149 nest = nla_nest_start(skb, TCA_OPTIONS);
2151 goto nla_put_failure;
2153 if (f->res.classid &&
2154 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
2155 goto nla_put_failure;
2158 mask = &f->mask->key;
2160 if (fl_dump_key(skb, net, key, mask))
2161 goto nla_put_failure;
2163 if (!tc_skip_hw(f->flags))
2164 fl_hw_update_stats(tp, f);
2166 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
2167 goto nla_put_failure;
2169 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
2170 goto nla_put_failure;
2172 if (tcf_exts_dump(skb, &f->exts))
2173 goto nla_put_failure;
2175 nla_nest_end(skb, nest);
2177 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
2178 goto nla_put_failure;
2183 nla_nest_cancel(skb, nest);
2187 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
2189 struct fl_flow_tmplt *tmplt = tmplt_priv;
2190 struct fl_flow_key *key, *mask;
2191 struct nlattr *nest;
2193 nest = nla_nest_start(skb, TCA_OPTIONS);
2195 goto nla_put_failure;
2197 key = &tmplt->dummy_key;
2198 mask = &tmplt->mask;
2200 if (fl_dump_key(skb, net, key, mask))
2201 goto nla_put_failure;
2203 nla_nest_end(skb, nest);
2208 nla_nest_cancel(skb, nest);
2212 static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
2214 struct cls_fl_filter *f = fh;
2216 if (f && f->res.classid == classid)
2220 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
2222 .classify = fl_classify,
2224 .destroy = fl_destroy,
2227 .change = fl_change,
2228 .delete = fl_delete,
2230 .reoffload = fl_reoffload,
2232 .bind_class = fl_bind_class,
2233 .tmplt_create = fl_tmplt_create,
2234 .tmplt_destroy = fl_tmplt_destroy,
2235 .tmplt_dump = fl_tmplt_dump,
2236 .owner = THIS_MODULE,
2239 static int __init cls_fl_init(void)
2241 return register_tcf_proto_ops(&cls_fl_ops);
2244 static void __exit cls_fl_exit(void)
2246 unregister_tcf_proto_ops(&cls_fl_ops);
2249 module_init(cls_fl_init);
2250 module_exit(cls_fl_exit);
2252 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
2253 MODULE_DESCRIPTION("Flower classifier");
2254 MODULE_LICENSE("GPL v2");