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>
18 #include <linux/if_ether.h>
19 #include <linux/in6.h>
21 #include <linux/mpls.h>
23 #include <net/sch_generic.h>
24 #include <net/pkt_cls.h>
26 #include <net/flow_dissector.h>
27 #include <net/geneve.h>
30 #include <net/dst_metadata.h>
34 struct flow_dissector_key_control control;
35 struct flow_dissector_key_control enc_control;
36 struct flow_dissector_key_basic basic;
37 struct flow_dissector_key_eth_addrs eth;
38 struct flow_dissector_key_vlan vlan;
39 struct flow_dissector_key_vlan cvlan;
41 struct flow_dissector_key_ipv4_addrs ipv4;
42 struct flow_dissector_key_ipv6_addrs ipv6;
44 struct flow_dissector_key_ports tp;
45 struct flow_dissector_key_icmp icmp;
46 struct flow_dissector_key_arp arp;
47 struct flow_dissector_key_keyid enc_key_id;
49 struct flow_dissector_key_ipv4_addrs enc_ipv4;
50 struct flow_dissector_key_ipv6_addrs enc_ipv6;
52 struct flow_dissector_key_ports enc_tp;
53 struct flow_dissector_key_mpls mpls;
54 struct flow_dissector_key_tcp tcp;
55 struct flow_dissector_key_ip ip;
56 struct flow_dissector_key_ip enc_ip;
57 struct flow_dissector_key_enc_opts enc_opts;
58 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
60 struct fl_flow_mask_range {
61 unsigned short int start;
62 unsigned short int end;
66 struct fl_flow_key key;
67 struct fl_flow_mask_range range;
68 struct rhash_head ht_node;
70 struct rhashtable_params filter_ht_params;
71 struct flow_dissector dissector;
72 struct list_head filters;
73 struct rcu_work rwork;
74 struct list_head list;
77 struct fl_flow_tmplt {
78 struct fl_flow_key dummy_key;
79 struct fl_flow_key mask;
80 struct flow_dissector dissector;
81 struct tcf_chain *chain;
86 struct list_head masks;
87 struct rcu_work rwork;
88 struct idr handle_idr;
91 struct cls_fl_filter {
92 struct fl_flow_mask *mask;
93 struct rhash_head ht_node;
94 struct fl_flow_key mkey;
96 struct tcf_result res;
97 struct fl_flow_key key;
98 struct list_head list;
101 unsigned int in_hw_count;
102 struct rcu_work rwork;
103 struct net_device *hw_dev;
106 static const struct rhashtable_params mask_ht_params = {
107 .key_offset = offsetof(struct fl_flow_mask, key),
108 .key_len = sizeof(struct fl_flow_key),
109 .head_offset = offsetof(struct fl_flow_mask, ht_node),
110 .automatic_shrinking = true,
113 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
115 return mask->range.end - mask->range.start;
118 static void fl_mask_update_range(struct fl_flow_mask *mask)
120 const u8 *bytes = (const u8 *) &mask->key;
121 size_t size = sizeof(mask->key);
122 size_t i, first = 0, last;
124 for (i = 0; i < size; i++) {
131 for (i = size - 1; i != first; i--) {
137 mask->range.start = rounddown(first, sizeof(long));
138 mask->range.end = roundup(last + 1, sizeof(long));
141 static void *fl_key_get_start(struct fl_flow_key *key,
142 const struct fl_flow_mask *mask)
144 return (u8 *) key + mask->range.start;
147 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
148 struct fl_flow_mask *mask)
150 const long *lkey = fl_key_get_start(key, mask);
151 const long *lmask = fl_key_get_start(&mask->key, mask);
152 long *lmkey = fl_key_get_start(mkey, mask);
155 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
156 *lmkey++ = *lkey++ & *lmask++;
159 static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
160 struct fl_flow_mask *mask)
162 const long *lmask = fl_key_get_start(&mask->key, mask);
168 ltmplt = fl_key_get_start(&tmplt->mask, mask);
169 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
170 if (~*ltmplt++ & *lmask++)
176 static void fl_clear_masked_range(struct fl_flow_key *key,
177 struct fl_flow_mask *mask)
179 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
182 static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
183 struct fl_flow_key *mkey)
185 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
186 mask->filter_ht_params);
189 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
190 struct tcf_result *res)
192 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
193 struct cls_fl_filter *f;
194 struct fl_flow_mask *mask;
195 struct fl_flow_key skb_key;
196 struct fl_flow_key skb_mkey;
198 list_for_each_entry_rcu(mask, &head->masks, list) {
199 flow_dissector_init_keys(&skb_key.control, &skb_key.basic);
200 fl_clear_masked_range(&skb_key, mask);
202 skb_key.indev_ifindex = skb->skb_iif;
203 /* skb_flow_dissect() does not set n_proto in case an unknown
204 * protocol, so do it rather here.
206 skb_key.basic.n_proto = skb_protocol(skb, false);
207 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
208 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
210 fl_set_masked_key(&skb_mkey, &skb_key, mask);
212 f = fl_lookup(mask, &skb_mkey);
213 if (f && !tc_skip_sw(f->flags)) {
215 return tcf_exts_exec(skb, &f->exts, res);
221 static int fl_init(struct tcf_proto *tp)
223 struct cls_fl_head *head;
225 head = kzalloc(sizeof(*head), GFP_KERNEL);
229 INIT_LIST_HEAD_RCU(&head->masks);
230 rcu_assign_pointer(tp->root, head);
231 idr_init(&head->handle_idr);
233 return rhashtable_init(&head->ht, &mask_ht_params);
236 static void fl_mask_free(struct fl_flow_mask *mask)
238 rhashtable_destroy(&mask->ht);
242 static void fl_mask_free_work(struct work_struct *work)
244 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
245 struct fl_flow_mask, rwork);
250 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask,
253 if (!list_empty(&mask->filters))
256 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
257 list_del_rcu(&mask->list);
259 tcf_queue_work(&mask->rwork, fl_mask_free_work);
266 static void __fl_destroy_filter(struct cls_fl_filter *f)
268 tcf_exts_destroy(&f->exts);
269 tcf_exts_put_net(&f->exts);
273 static void fl_destroy_filter_work(struct work_struct *work)
275 struct cls_fl_filter *f = container_of(to_rcu_work(work),
276 struct cls_fl_filter, rwork);
279 __fl_destroy_filter(f);
283 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
284 struct netlink_ext_ack *extack)
286 struct tc_cls_flower_offload cls_flower = {};
287 struct tcf_block *block = tp->chain->block;
289 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
290 cls_flower.command = TC_CLSFLOWER_DESTROY;
291 cls_flower.cookie = (unsigned long) f;
293 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
295 tcf_block_offload_dec(block, &f->flags);
298 static int fl_hw_replace_filter(struct tcf_proto *tp,
299 struct cls_fl_filter *f,
300 struct netlink_ext_ack *extack)
302 struct tc_cls_flower_offload cls_flower = {};
303 struct tcf_block *block = tp->chain->block;
304 bool skip_sw = tc_skip_sw(f->flags);
307 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
308 cls_flower.command = TC_CLSFLOWER_REPLACE;
309 cls_flower.cookie = (unsigned long) f;
310 cls_flower.dissector = &f->mask->dissector;
311 cls_flower.mask = &f->mask->key;
312 cls_flower.key = &f->mkey;
313 cls_flower.exts = &f->exts;
314 cls_flower.classid = f->res.classid;
316 err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
317 &cls_flower, skip_sw);
319 fl_hw_destroy_filter(tp, f, NULL);
321 } else if (err > 0) {
322 f->in_hw_count = err;
323 tcf_block_offload_inc(block, &f->flags);
326 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
332 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
334 struct tc_cls_flower_offload cls_flower = {};
335 struct tcf_block *block = tp->chain->block;
337 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
338 cls_flower.command = TC_CLSFLOWER_STATS;
339 cls_flower.cookie = (unsigned long) f;
340 cls_flower.exts = &f->exts;
341 cls_flower.classid = f->res.classid;
343 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
347 static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
348 struct netlink_ext_ack *extack)
350 struct cls_fl_head *head = rtnl_dereference(tp->root);
351 bool async = tcf_exts_get_net(&f->exts);
354 idr_remove(&head->handle_idr, f->handle);
355 list_del_rcu(&f->list);
356 last = fl_mask_put(head, f->mask, async);
357 if (!tc_skip_hw(f->flags))
358 fl_hw_destroy_filter(tp, f, extack);
359 tcf_unbind_filter(tp, &f->res);
361 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
363 __fl_destroy_filter(f);
368 static void fl_destroy_sleepable(struct work_struct *work)
370 struct cls_fl_head *head = container_of(to_rcu_work(work),
374 rhashtable_destroy(&head->ht);
376 module_put(THIS_MODULE);
379 static void fl_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
381 struct cls_fl_head *head = rtnl_dereference(tp->root);
382 struct fl_flow_mask *mask, *next_mask;
383 struct cls_fl_filter *f, *next;
385 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
386 list_for_each_entry_safe(f, next, &mask->filters, list) {
387 if (__fl_delete(tp, f, extack))
391 idr_destroy(&head->handle_idr);
393 __module_get(THIS_MODULE);
394 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
397 static void *fl_get(struct tcf_proto *tp, u32 handle)
399 struct cls_fl_head *head = rtnl_dereference(tp->root);
401 return idr_find(&head->handle_idr, handle);
404 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
405 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
406 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
407 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
409 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
410 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
411 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
412 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
413 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
414 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
415 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
416 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
417 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
418 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
419 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
420 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
421 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
422 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
423 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
424 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
425 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
426 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
427 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
428 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
429 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
430 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
431 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
432 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
433 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
434 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
435 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
436 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
437 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
438 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
439 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
440 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
441 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
442 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
443 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
444 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
445 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
446 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
447 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
448 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
449 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
450 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
451 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
452 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
453 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
454 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
455 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
456 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
457 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
458 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
459 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
460 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
461 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
462 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
463 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
464 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
465 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
466 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
467 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
468 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
469 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
470 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
471 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
472 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
473 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
474 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
475 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
476 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
477 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
478 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
479 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
480 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
481 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
482 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
483 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
484 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
485 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
486 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
487 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
488 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
489 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
490 [TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
493 static const struct nla_policy
494 enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
495 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
498 static const struct nla_policy
499 geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
500 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
501 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
502 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
506 static void fl_set_key_val(struct nlattr **tb,
507 void *val, int val_type,
508 void *mask, int mask_type, int len)
512 memcpy(val, nla_data(tb[val_type]), len);
513 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
514 memset(mask, 0xff, len);
516 memcpy(mask, nla_data(tb[mask_type]), len);
519 static int fl_set_key_mpls(struct nlattr **tb,
520 struct flow_dissector_key_mpls *key_val,
521 struct flow_dissector_key_mpls *key_mask)
523 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
524 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
525 key_mask->mpls_ttl = MPLS_TTL_MASK;
527 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
528 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
530 if (bos & ~MPLS_BOS_MASK)
532 key_val->mpls_bos = bos;
533 key_mask->mpls_bos = MPLS_BOS_MASK;
535 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
536 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
538 if (tc & ~MPLS_TC_MASK)
540 key_val->mpls_tc = tc;
541 key_mask->mpls_tc = MPLS_TC_MASK;
543 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
544 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
546 if (label & ~MPLS_LABEL_MASK)
548 key_val->mpls_label = label;
549 key_mask->mpls_label = MPLS_LABEL_MASK;
554 static void fl_set_key_vlan(struct nlattr **tb,
556 int vlan_id_key, int vlan_prio_key,
557 struct flow_dissector_key_vlan *key_val,
558 struct flow_dissector_key_vlan *key_mask)
560 #define VLAN_PRIORITY_MASK 0x7
562 if (tb[vlan_id_key]) {
564 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
565 key_mask->vlan_id = VLAN_VID_MASK;
567 if (tb[vlan_prio_key]) {
568 key_val->vlan_priority =
569 nla_get_u8(tb[vlan_prio_key]) &
571 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
573 key_val->vlan_tpid = ethertype;
574 key_mask->vlan_tpid = cpu_to_be16(~0);
577 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
578 u32 *dissector_key, u32 *dissector_mask,
579 u32 flower_flag_bit, u32 dissector_flag_bit)
581 if (flower_mask & flower_flag_bit) {
582 *dissector_mask |= dissector_flag_bit;
583 if (flower_key & flower_flag_bit)
584 *dissector_key |= dissector_flag_bit;
588 static int fl_set_key_flags(struct nlattr **tb,
589 u32 *flags_key, u32 *flags_mask)
593 /* mask is mandatory for flags */
594 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
597 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
598 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
603 fl_set_key_flag(key, mask, flags_key, flags_mask,
604 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
605 fl_set_key_flag(key, mask, flags_key, flags_mask,
606 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
607 FLOW_DIS_FIRST_FRAG);
612 static void fl_set_key_ip(struct nlattr **tb, bool encap,
613 struct flow_dissector_key_ip *key,
614 struct flow_dissector_key_ip *mask)
616 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
617 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
618 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
619 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
621 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
622 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
625 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
626 int depth, int option_len,
627 struct netlink_ext_ack *extack)
629 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
630 struct nlattr *class = NULL, *type = NULL, *data = NULL;
631 struct geneve_opt *opt;
632 int err, data_len = 0;
634 if (option_len > sizeof(struct geneve_opt))
635 data_len = option_len - sizeof(struct geneve_opt);
637 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
638 memset(opt, 0xff, option_len);
639 opt->length = data_len / 4;
644 /* If no mask has been prodived we assume an exact match. */
646 return sizeof(struct geneve_opt) + data_len;
648 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
649 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
653 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
654 nla, geneve_opt_policy, extack);
658 /* We are not allowed to omit any of CLASS, TYPE or DATA
659 * fields from the key.
662 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
663 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
664 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
665 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
669 /* Omitting any of CLASS, TYPE or DATA fields is allowed
672 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
673 int new_len = key->enc_opts.len;
675 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
676 data_len = nla_len(data);
678 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
682 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
686 new_len += sizeof(struct geneve_opt) + data_len;
687 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
688 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
689 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
692 opt->length = data_len / 4;
693 memcpy(opt->opt_data, nla_data(data), data_len);
696 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
697 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
698 opt->opt_class = nla_get_be16(class);
701 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
702 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
703 opt->type = nla_get_u8(type);
706 return sizeof(struct geneve_opt) + data_len;
709 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
710 struct fl_flow_key *mask,
711 struct netlink_ext_ack *extack)
713 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
714 int err, option_len, key_depth, msk_depth = 0;
716 err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS],
717 TCA_FLOWER_KEY_ENC_OPTS_MAX,
718 enc_opts_policy, extack);
722 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
724 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
725 err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
726 TCA_FLOWER_KEY_ENC_OPTS_MAX,
727 enc_opts_policy, extack);
731 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
732 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
735 nla_for_each_attr(nla_opt_key, nla_enc_key,
736 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
737 switch (nla_type(nla_opt_key)) {
738 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
740 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
741 option_len = fl_set_geneve_opt(nla_opt_key, key,
742 key_depth, option_len,
747 key->enc_opts.len += option_len;
748 /* At the same time we need to parse through the mask
749 * in order to verify exact and mask attribute lengths.
751 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
752 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
753 msk_depth, option_len,
758 mask->enc_opts.len += option_len;
759 if (key->enc_opts.len != mask->enc_opts.len) {
760 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
765 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
768 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
776 static int fl_set_key(struct net *net, struct nlattr **tb,
777 struct fl_flow_key *key, struct fl_flow_key *mask,
778 struct netlink_ext_ack *extack)
782 #ifdef CONFIG_NET_CLS_IND
783 if (tb[TCA_FLOWER_INDEV]) {
784 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
787 key->indev_ifindex = err;
788 mask->indev_ifindex = 0xffffffff;
792 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
793 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
794 sizeof(key->eth.dst));
795 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
796 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
797 sizeof(key->eth.src));
799 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
800 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
802 if (eth_type_vlan(ethertype)) {
803 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
804 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
807 if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
808 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
809 if (eth_type_vlan(ethertype)) {
810 fl_set_key_vlan(tb, ethertype,
811 TCA_FLOWER_KEY_CVLAN_ID,
812 TCA_FLOWER_KEY_CVLAN_PRIO,
813 &key->cvlan, &mask->cvlan);
814 fl_set_key_val(tb, &key->basic.n_proto,
815 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
816 &mask->basic.n_proto,
818 sizeof(key->basic.n_proto));
820 key->basic.n_proto = ethertype;
821 mask->basic.n_proto = cpu_to_be16(~0);
825 key->basic.n_proto = ethertype;
826 mask->basic.n_proto = cpu_to_be16(~0);
830 if (key->basic.n_proto == htons(ETH_P_IP) ||
831 key->basic.n_proto == htons(ETH_P_IPV6)) {
832 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
833 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
834 sizeof(key->basic.ip_proto));
835 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
838 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
839 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
840 mask->control.addr_type = ~0;
841 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
842 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
843 sizeof(key->ipv4.src));
844 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
845 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
846 sizeof(key->ipv4.dst));
847 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
848 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
849 mask->control.addr_type = ~0;
850 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
851 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
852 sizeof(key->ipv6.src));
853 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
854 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
855 sizeof(key->ipv6.dst));
858 if (key->basic.ip_proto == IPPROTO_TCP) {
859 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
860 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
861 sizeof(key->tp.src));
862 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
863 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
864 sizeof(key->tp.dst));
865 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
866 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
867 sizeof(key->tcp.flags));
868 } else if (key->basic.ip_proto == IPPROTO_UDP) {
869 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
870 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
871 sizeof(key->tp.src));
872 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
873 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
874 sizeof(key->tp.dst));
875 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
876 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
877 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
878 sizeof(key->tp.src));
879 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
880 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
881 sizeof(key->tp.dst));
882 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
883 key->basic.ip_proto == IPPROTO_ICMP) {
884 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
886 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
887 sizeof(key->icmp.type));
888 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
890 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
891 sizeof(key->icmp.code));
892 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
893 key->basic.ip_proto == IPPROTO_ICMPV6) {
894 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
896 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
897 sizeof(key->icmp.type));
898 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
900 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
901 sizeof(key->icmp.code));
902 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
903 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
904 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
907 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
908 key->basic.n_proto == htons(ETH_P_RARP)) {
909 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
910 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
911 sizeof(key->arp.sip));
912 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
913 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
914 sizeof(key->arp.tip));
915 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
916 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
917 sizeof(key->arp.op));
918 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
919 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
920 sizeof(key->arp.sha));
921 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
922 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
923 sizeof(key->arp.tha));
926 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
927 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
928 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
929 mask->enc_control.addr_type = ~0;
930 fl_set_key_val(tb, &key->enc_ipv4.src,
931 TCA_FLOWER_KEY_ENC_IPV4_SRC,
933 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
934 sizeof(key->enc_ipv4.src));
935 fl_set_key_val(tb, &key->enc_ipv4.dst,
936 TCA_FLOWER_KEY_ENC_IPV4_DST,
938 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
939 sizeof(key->enc_ipv4.dst));
942 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
943 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
944 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
945 mask->enc_control.addr_type = ~0;
946 fl_set_key_val(tb, &key->enc_ipv6.src,
947 TCA_FLOWER_KEY_ENC_IPV6_SRC,
949 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
950 sizeof(key->enc_ipv6.src));
951 fl_set_key_val(tb, &key->enc_ipv6.dst,
952 TCA_FLOWER_KEY_ENC_IPV6_DST,
954 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
955 sizeof(key->enc_ipv6.dst));
958 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
959 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
960 sizeof(key->enc_key_id.keyid));
962 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
963 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
964 sizeof(key->enc_tp.src));
966 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
967 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
968 sizeof(key->enc_tp.dst));
970 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
972 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
973 ret = fl_set_enc_opt(tb, key, mask, extack);
978 if (tb[TCA_FLOWER_KEY_FLAGS])
979 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
984 static void fl_mask_copy(struct fl_flow_mask *dst,
985 struct fl_flow_mask *src)
987 const void *psrc = fl_key_get_start(&src->key, src);
988 void *pdst = fl_key_get_start(&dst->key, src);
990 memcpy(pdst, psrc, fl_mask_range(src));
991 dst->range = src->range;
994 static const struct rhashtable_params fl_ht_params = {
995 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
996 .head_offset = offsetof(struct cls_fl_filter, ht_node),
997 .automatic_shrinking = true,
1000 static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
1002 mask->filter_ht_params = fl_ht_params;
1003 mask->filter_ht_params.key_len = fl_mask_range(mask);
1004 mask->filter_ht_params.key_offset += mask->range.start;
1006 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
1009 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
1010 #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
1012 #define FL_KEY_IS_MASKED(mask, member) \
1013 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
1014 0, FL_KEY_MEMBER_SIZE(member)) \
1016 #define FL_KEY_SET(keys, cnt, id, member) \
1018 keys[cnt].key_id = id; \
1019 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
1023 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
1025 if (FL_KEY_IS_MASKED(mask, member)) \
1026 FL_KEY_SET(keys, cnt, id, member); \
1029 static void fl_init_dissector(struct flow_dissector *dissector,
1030 struct fl_flow_key *mask)
1032 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1035 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
1036 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
1037 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1038 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
1039 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1040 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
1041 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1042 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
1043 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1044 FLOW_DISSECTOR_KEY_PORTS, tp);
1045 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1046 FLOW_DISSECTOR_KEY_IP, ip);
1047 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1048 FLOW_DISSECTOR_KEY_TCP, tcp);
1049 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1050 FLOW_DISSECTOR_KEY_ICMP, icmp);
1051 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1052 FLOW_DISSECTOR_KEY_ARP, arp);
1053 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1054 FLOW_DISSECTOR_KEY_MPLS, mpls);
1055 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1056 FLOW_DISSECTOR_KEY_VLAN, vlan);
1057 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1058 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
1059 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1060 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
1061 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1062 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
1063 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1064 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
1065 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1066 FL_KEY_IS_MASKED(mask, enc_ipv6))
1067 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1069 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1070 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
1071 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1072 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
1073 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1074 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
1076 skb_flow_dissector_init(dissector, keys, cnt);
1079 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1080 struct fl_flow_mask *mask)
1082 struct fl_flow_mask *newmask;
1085 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1087 return ERR_PTR(-ENOMEM);
1089 fl_mask_copy(newmask, mask);
1091 err = fl_init_mask_hashtable(newmask);
1095 fl_init_dissector(&newmask->dissector, &newmask->key);
1097 INIT_LIST_HEAD_RCU(&newmask->filters);
1099 err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
1102 goto errout_destroy;
1104 list_add_tail_rcu(&newmask->list, &head->masks);
1109 rhashtable_destroy(&newmask->ht);
1113 return ERR_PTR(err);
1116 static int fl_check_assign_mask(struct cls_fl_head *head,
1117 struct cls_fl_filter *fnew,
1118 struct cls_fl_filter *fold,
1119 struct fl_flow_mask *mask)
1121 struct fl_flow_mask *newmask;
1123 fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
1128 newmask = fl_create_new_mask(head, mask);
1129 if (IS_ERR(newmask))
1130 return PTR_ERR(newmask);
1132 fnew->mask = newmask;
1133 } else if (fold && fold->mask != fnew->mask) {
1140 static int fl_set_parms(struct net *net, struct tcf_proto *tp,
1141 struct cls_fl_filter *f, struct fl_flow_mask *mask,
1142 unsigned long base, struct nlattr **tb,
1143 struct nlattr *est, bool ovr,
1144 struct fl_flow_tmplt *tmplt,
1145 struct netlink_ext_ack *extack)
1149 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
1153 if (tb[TCA_FLOWER_CLASSID]) {
1154 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
1155 tcf_bind_filter(tp, &f->res, base);
1158 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
1162 fl_mask_update_range(mask);
1163 fl_set_masked_key(&f->mkey, &f->key, mask);
1165 if (!fl_mask_fits_tmplt(tmplt, mask)) {
1166 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
1173 static int fl_change(struct net *net, struct sk_buff *in_skb,
1174 struct tcf_proto *tp, unsigned long base,
1175 u32 handle, struct nlattr **tca,
1176 void **arg, bool ovr, struct netlink_ext_ack *extack)
1178 struct cls_fl_head *head = rtnl_dereference(tp->root);
1179 struct cls_fl_filter *fold = *arg;
1180 struct cls_fl_filter *fnew;
1181 struct fl_flow_mask *mask;
1185 if (!tca[TCA_OPTIONS])
1188 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
1192 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1195 goto errout_mask_alloc;
1198 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
1203 if (fold && handle && fold->handle != handle) {
1208 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
1214 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
1218 if (tb[TCA_FLOWER_FLAGS]) {
1219 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1221 if (!tc_flags_valid(fnew->flags)) {
1227 err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr,
1228 tp->chain->tmplt_priv, extack);
1232 err = fl_check_assign_mask(head, fnew, fold, mask);
1238 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1239 INT_MAX, GFP_KERNEL);
1241 /* user specifies a handle and it doesn't exist */
1242 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1243 handle, GFP_KERNEL);
1247 fnew->handle = handle;
1249 if (!tc_skip_sw(fnew->flags)) {
1250 if (!fold && fl_lookup(fnew->mask, &fnew->mkey)) {
1255 err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1256 fnew->mask->filter_ht_params);
1261 if (!tc_skip_hw(fnew->flags)) {
1262 err = fl_hw_replace_filter(tp, fnew, extack);
1267 if (!tc_in_hw(fnew->flags))
1268 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1271 if (!tc_skip_sw(fold->flags))
1272 rhashtable_remove_fast(&fold->mask->ht,
1274 fold->mask->filter_ht_params);
1275 if (!tc_skip_hw(fold->flags))
1276 fl_hw_destroy_filter(tp, fold, NULL);
1282 idr_replace(&head->handle_idr, fnew, fnew->handle);
1283 list_replace_rcu(&fold->list, &fnew->list);
1284 tcf_unbind_filter(tp, &fold->res);
1285 tcf_exts_get_net(&fold->exts);
1286 tcf_queue_work(&fold->rwork, fl_destroy_filter_work);
1288 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
1297 idr_remove(&head->handle_idr, fnew->handle);
1300 fl_mask_put(head, fnew->mask, false);
1303 tcf_exts_destroy(&fnew->exts);
1312 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1313 struct netlink_ext_ack *extack)
1315 struct cls_fl_head *head = rtnl_dereference(tp->root);
1316 struct cls_fl_filter *f = arg;
1318 if (!tc_skip_sw(f->flags))
1319 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
1320 f->mask->filter_ht_params);
1321 __fl_delete(tp, f, extack);
1322 *last = list_empty(&head->masks);
1326 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1328 struct cls_fl_head *head = rtnl_dereference(tp->root);
1329 struct cls_fl_filter *f;
1331 arg->count = arg->skip;
1333 while ((f = idr_get_next_ul(&head->handle_idr,
1334 &arg->cookie)) != NULL) {
1335 if (arg->fn(tp, f, arg) < 0) {
1339 arg->cookie = f->handle + 1;
1344 static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1345 void *cb_priv, struct netlink_ext_ack *extack)
1347 struct cls_fl_head *head = rtnl_dereference(tp->root);
1348 struct tc_cls_flower_offload cls_flower = {};
1349 struct tcf_block *block = tp->chain->block;
1350 struct fl_flow_mask *mask;
1351 struct cls_fl_filter *f;
1354 list_for_each_entry(mask, &head->masks, list) {
1355 list_for_each_entry(f, &mask->filters, list) {
1356 if (tc_skip_hw(f->flags))
1359 tc_cls_common_offload_init(&cls_flower.common, tp,
1361 cls_flower.command = add ?
1362 TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
1363 cls_flower.cookie = (unsigned long)f;
1364 cls_flower.dissector = &mask->dissector;
1365 cls_flower.mask = &mask->key;
1366 cls_flower.key = &f->mkey;
1367 cls_flower.exts = &f->exts;
1368 cls_flower.classid = f->res.classid;
1370 err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1372 if (add && tc_skip_sw(f->flags))
1377 tc_cls_offload_cnt_update(block, &f->in_hw_count,
1385 static void fl_hw_create_tmplt(struct tcf_chain *chain,
1386 struct fl_flow_tmplt *tmplt)
1388 struct tc_cls_flower_offload cls_flower = {};
1389 struct tcf_block *block = chain->block;
1390 struct tcf_exts dummy_exts = { 0, };
1392 cls_flower.common.chain_index = chain->index;
1393 cls_flower.command = TC_CLSFLOWER_TMPLT_CREATE;
1394 cls_flower.cookie = (unsigned long) tmplt;
1395 cls_flower.dissector = &tmplt->dissector;
1396 cls_flower.mask = &tmplt->mask;
1397 cls_flower.key = &tmplt->dummy_key;
1398 cls_flower.exts = &dummy_exts;
1400 /* We don't care if driver (any of them) fails to handle this
1401 * call. It serves just as a hint for it.
1403 tc_setup_cb_call(block, NULL, TC_SETUP_CLSFLOWER,
1404 &cls_flower, false);
1407 static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
1408 struct fl_flow_tmplt *tmplt)
1410 struct tc_cls_flower_offload cls_flower = {};
1411 struct tcf_block *block = chain->block;
1413 cls_flower.common.chain_index = chain->index;
1414 cls_flower.command = TC_CLSFLOWER_TMPLT_DESTROY;
1415 cls_flower.cookie = (unsigned long) tmplt;
1417 tc_setup_cb_call(block, NULL, TC_SETUP_CLSFLOWER,
1418 &cls_flower, false);
1421 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
1422 struct nlattr **tca,
1423 struct netlink_ext_ack *extack)
1425 struct fl_flow_tmplt *tmplt;
1429 if (!tca[TCA_OPTIONS])
1430 return ERR_PTR(-EINVAL);
1432 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1434 return ERR_PTR(-ENOBUFS);
1435 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
1440 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
1445 tmplt->chain = chain;
1446 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
1451 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
1453 fl_hw_create_tmplt(chain, tmplt);
1461 return ERR_PTR(err);
1464 static void fl_tmplt_destroy(void *tmplt_priv)
1466 struct fl_flow_tmplt *tmplt = tmplt_priv;
1468 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
1472 static int fl_dump_key_val(struct sk_buff *skb,
1473 void *val, int val_type,
1474 void *mask, int mask_type, int len)
1478 if (!memchr_inv(mask, 0, len))
1480 err = nla_put(skb, val_type, len, val);
1483 if (mask_type != TCA_FLOWER_UNSPEC) {
1484 err = nla_put(skb, mask_type, len, mask);
1491 static int fl_dump_key_mpls(struct sk_buff *skb,
1492 struct flow_dissector_key_mpls *mpls_key,
1493 struct flow_dissector_key_mpls *mpls_mask)
1497 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1499 if (mpls_mask->mpls_ttl) {
1500 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1501 mpls_key->mpls_ttl);
1505 if (mpls_mask->mpls_tc) {
1506 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1511 if (mpls_mask->mpls_label) {
1512 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1513 mpls_key->mpls_label);
1517 if (mpls_mask->mpls_bos) {
1518 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1519 mpls_key->mpls_bos);
1526 static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
1527 struct flow_dissector_key_ip *key,
1528 struct flow_dissector_key_ip *mask)
1530 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1531 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1532 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1533 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1535 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
1536 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
1542 static int fl_dump_key_vlan(struct sk_buff *skb,
1543 int vlan_id_key, int vlan_prio_key,
1544 struct flow_dissector_key_vlan *vlan_key,
1545 struct flow_dissector_key_vlan *vlan_mask)
1549 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1551 if (vlan_mask->vlan_id) {
1552 err = nla_put_u16(skb, vlan_id_key,
1557 if (vlan_mask->vlan_priority) {
1558 err = nla_put_u8(skb, vlan_prio_key,
1559 vlan_key->vlan_priority);
1566 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1567 u32 *flower_key, u32 *flower_mask,
1568 u32 flower_flag_bit, u32 dissector_flag_bit)
1570 if (dissector_mask & dissector_flag_bit) {
1571 *flower_mask |= flower_flag_bit;
1572 if (dissector_key & dissector_flag_bit)
1573 *flower_key |= flower_flag_bit;
1577 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1583 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1589 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1590 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1591 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1592 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1593 FLOW_DIS_FIRST_FRAG);
1595 _key = cpu_to_be32(key);
1596 _mask = cpu_to_be32(mask);
1598 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1602 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1605 static int fl_dump_key_geneve_opt(struct sk_buff *skb,
1606 struct flow_dissector_key_enc_opts *enc_opts)
1608 struct geneve_opt *opt;
1609 struct nlattr *nest;
1612 nest = nla_nest_start(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
1614 goto nla_put_failure;
1616 while (enc_opts->len > opt_off) {
1617 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
1619 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
1621 goto nla_put_failure;
1622 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
1624 goto nla_put_failure;
1625 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
1626 opt->length * 4, opt->opt_data))
1627 goto nla_put_failure;
1629 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
1631 nla_nest_end(skb, nest);
1635 nla_nest_cancel(skb, nest);
1639 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
1640 struct flow_dissector_key_enc_opts *enc_opts)
1642 struct nlattr *nest;
1648 nest = nla_nest_start(skb, enc_opt_type);
1650 goto nla_put_failure;
1652 switch (enc_opts->dst_opt_type) {
1653 case TUNNEL_GENEVE_OPT:
1654 err = fl_dump_key_geneve_opt(skb, enc_opts);
1656 goto nla_put_failure;
1659 goto nla_put_failure;
1661 nla_nest_end(skb, nest);
1665 nla_nest_cancel(skb, nest);
1669 static int fl_dump_key_enc_opt(struct sk_buff *skb,
1670 struct flow_dissector_key_enc_opts *key_opts,
1671 struct flow_dissector_key_enc_opts *msk_opts)
1675 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
1679 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
1682 static int fl_dump_key(struct sk_buff *skb, struct net *net,
1683 struct fl_flow_key *key, struct fl_flow_key *mask)
1685 if (mask->indev_ifindex) {
1686 struct net_device *dev;
1688 dev = __dev_get_by_index(net, key->indev_ifindex);
1689 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1690 goto nla_put_failure;
1693 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1694 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1695 sizeof(key->eth.dst)) ||
1696 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1697 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1698 sizeof(key->eth.src)) ||
1699 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1700 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1701 sizeof(key->basic.n_proto)))
1702 goto nla_put_failure;
1704 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1705 goto nla_put_failure;
1707 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
1708 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
1709 goto nla_put_failure;
1711 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
1712 TCA_FLOWER_KEY_CVLAN_PRIO,
1713 &key->cvlan, &mask->cvlan) ||
1714 (mask->cvlan.vlan_tpid &&
1715 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1716 key->cvlan.vlan_tpid)))
1717 goto nla_put_failure;
1719 if (mask->basic.n_proto) {
1720 if (mask->cvlan.vlan_tpid) {
1721 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1722 key->basic.n_proto))
1723 goto nla_put_failure;
1724 } else if (mask->vlan.vlan_tpid) {
1725 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1726 key->basic.n_proto))
1727 goto nla_put_failure;
1731 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1732 key->basic.n_proto == htons(ETH_P_IPV6)) &&
1733 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1734 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1735 sizeof(key->basic.ip_proto)) ||
1736 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
1737 goto nla_put_failure;
1739 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1740 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1741 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1742 sizeof(key->ipv4.src)) ||
1743 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1744 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1745 sizeof(key->ipv4.dst))))
1746 goto nla_put_failure;
1747 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1748 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1749 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1750 sizeof(key->ipv6.src)) ||
1751 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1752 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1753 sizeof(key->ipv6.dst))))
1754 goto nla_put_failure;
1756 if (key->basic.ip_proto == IPPROTO_TCP &&
1757 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1758 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1759 sizeof(key->tp.src)) ||
1760 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1761 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1762 sizeof(key->tp.dst)) ||
1763 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1764 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1765 sizeof(key->tcp.flags))))
1766 goto nla_put_failure;
1767 else if (key->basic.ip_proto == IPPROTO_UDP &&
1768 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1769 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1770 sizeof(key->tp.src)) ||
1771 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1772 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1773 sizeof(key->tp.dst))))
1774 goto nla_put_failure;
1775 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1776 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1777 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1778 sizeof(key->tp.src)) ||
1779 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1780 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1781 sizeof(key->tp.dst))))
1782 goto nla_put_failure;
1783 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1784 key->basic.ip_proto == IPPROTO_ICMP &&
1785 (fl_dump_key_val(skb, &key->icmp.type,
1786 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1787 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1788 sizeof(key->icmp.type)) ||
1789 fl_dump_key_val(skb, &key->icmp.code,
1790 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1791 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1792 sizeof(key->icmp.code))))
1793 goto nla_put_failure;
1794 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1795 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1796 (fl_dump_key_val(skb, &key->icmp.type,
1797 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1798 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1799 sizeof(key->icmp.type)) ||
1800 fl_dump_key_val(skb, &key->icmp.code,
1801 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1802 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1803 sizeof(key->icmp.code))))
1804 goto nla_put_failure;
1805 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1806 key->basic.n_proto == htons(ETH_P_RARP)) &&
1807 (fl_dump_key_val(skb, &key->arp.sip,
1808 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1809 TCA_FLOWER_KEY_ARP_SIP_MASK,
1810 sizeof(key->arp.sip)) ||
1811 fl_dump_key_val(skb, &key->arp.tip,
1812 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1813 TCA_FLOWER_KEY_ARP_TIP_MASK,
1814 sizeof(key->arp.tip)) ||
1815 fl_dump_key_val(skb, &key->arp.op,
1816 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1817 TCA_FLOWER_KEY_ARP_OP_MASK,
1818 sizeof(key->arp.op)) ||
1819 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1820 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1821 sizeof(key->arp.sha)) ||
1822 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1823 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1824 sizeof(key->arp.tha))))
1825 goto nla_put_failure;
1827 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1828 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1829 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1830 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1831 sizeof(key->enc_ipv4.src)) ||
1832 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1833 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1834 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1835 sizeof(key->enc_ipv4.dst))))
1836 goto nla_put_failure;
1837 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1838 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1839 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1840 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1841 sizeof(key->enc_ipv6.src)) ||
1842 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1843 TCA_FLOWER_KEY_ENC_IPV6_DST,
1844 &mask->enc_ipv6.dst,
1845 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1846 sizeof(key->enc_ipv6.dst))))
1847 goto nla_put_failure;
1849 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
1850 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
1851 sizeof(key->enc_key_id)) ||
1852 fl_dump_key_val(skb, &key->enc_tp.src,
1853 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1855 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1856 sizeof(key->enc_tp.src)) ||
1857 fl_dump_key_val(skb, &key->enc_tp.dst,
1858 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1860 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1861 sizeof(key->enc_tp.dst)) ||
1862 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
1863 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
1864 goto nla_put_failure;
1866 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1867 goto nla_put_failure;
1875 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
1876 struct sk_buff *skb, struct tcmsg *t)
1878 struct cls_fl_filter *f = fh;
1879 struct nlattr *nest;
1880 struct fl_flow_key *key, *mask;
1885 t->tcm_handle = f->handle;
1887 nest = nla_nest_start(skb, TCA_OPTIONS);
1889 goto nla_put_failure;
1891 if (f->res.classid &&
1892 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1893 goto nla_put_failure;
1896 mask = &f->mask->key;
1898 if (fl_dump_key(skb, net, key, mask))
1899 goto nla_put_failure;
1901 if (!tc_skip_hw(f->flags))
1902 fl_hw_update_stats(tp, f);
1904 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1905 goto nla_put_failure;
1907 if (tcf_exts_dump(skb, &f->exts))
1908 goto nla_put_failure;
1910 nla_nest_end(skb, nest);
1912 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1913 goto nla_put_failure;
1918 nla_nest_cancel(skb, nest);
1922 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
1924 struct fl_flow_tmplt *tmplt = tmplt_priv;
1925 struct fl_flow_key *key, *mask;
1926 struct nlattr *nest;
1928 nest = nla_nest_start(skb, TCA_OPTIONS);
1930 goto nla_put_failure;
1932 key = &tmplt->dummy_key;
1933 mask = &tmplt->mask;
1935 if (fl_dump_key(skb, net, key, mask))
1936 goto nla_put_failure;
1938 nla_nest_end(skb, nest);
1943 nla_nest_cancel(skb, nest);
1947 static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
1950 struct cls_fl_filter *f = fh;
1952 if (f && f->res.classid == classid) {
1954 __tcf_bind_filter(q, &f->res, base);
1956 __tcf_unbind_filter(q, &f->res);
1960 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1962 .classify = fl_classify,
1964 .destroy = fl_destroy,
1966 .change = fl_change,
1967 .delete = fl_delete,
1969 .reoffload = fl_reoffload,
1971 .bind_class = fl_bind_class,
1972 .tmplt_create = fl_tmplt_create,
1973 .tmplt_destroy = fl_tmplt_destroy,
1974 .tmplt_dump = fl_tmplt_dump,
1975 .owner = THIS_MODULE,
1978 static int __init cls_fl_init(void)
1980 return register_tcf_proto_ops(&cls_fl_ops);
1983 static void __exit cls_fl_exit(void)
1985 unregister_tcf_proto_ops(&cls_fl_ops);
1988 module_init(cls_fl_init);
1989 module_exit(cls_fl_exit);
1991 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1992 MODULE_DESCRIPTION("Flower classifier");
1993 MODULE_LICENSE("GPL v2");