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>
29 #include <net/dst_metadata.h>
33 struct flow_dissector_key_control control;
34 struct flow_dissector_key_control enc_control;
35 struct flow_dissector_key_basic basic;
36 struct flow_dissector_key_eth_addrs eth;
37 struct flow_dissector_key_vlan vlan;
39 struct flow_dissector_key_ipv4_addrs ipv4;
40 struct flow_dissector_key_ipv6_addrs ipv6;
42 struct flow_dissector_key_ports tp;
43 struct flow_dissector_key_icmp icmp;
44 struct flow_dissector_key_arp arp;
45 struct flow_dissector_key_keyid enc_key_id;
47 struct flow_dissector_key_ipv4_addrs enc_ipv4;
48 struct flow_dissector_key_ipv6_addrs enc_ipv6;
50 struct flow_dissector_key_ports enc_tp;
51 struct flow_dissector_key_mpls mpls;
52 struct flow_dissector_key_tcp tcp;
53 struct flow_dissector_key_ip ip;
54 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
56 struct fl_flow_mask_range {
57 unsigned short int start;
58 unsigned short int end;
62 struct fl_flow_key key;
63 struct fl_flow_mask_range range;
69 struct fl_flow_mask mask;
70 struct flow_dissector dissector;
73 struct list_head filters;
74 struct rhashtable_params ht_params;
76 struct work_struct work;
81 struct cls_fl_filter {
82 struct rhash_head ht_node;
83 struct fl_flow_key mkey;
85 struct tcf_result res;
86 struct fl_flow_key key;
87 struct list_head list;
91 struct tc_to_netdev tc;
92 struct net_device *hw_dev;
95 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
97 return mask->range.end - mask->range.start;
100 static void fl_mask_update_range(struct fl_flow_mask *mask)
102 const u8 *bytes = (const u8 *) &mask->key;
103 size_t size = sizeof(mask->key);
104 size_t i, first = 0, last = size - 1;
106 for (i = 0; i < sizeof(mask->key); i++) {
113 mask->range.start = rounddown(first, sizeof(long));
114 mask->range.end = roundup(last + 1, sizeof(long));
117 static void *fl_key_get_start(struct fl_flow_key *key,
118 const struct fl_flow_mask *mask)
120 return (u8 *) key + mask->range.start;
123 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
124 struct fl_flow_mask *mask)
126 const long *lkey = fl_key_get_start(key, mask);
127 const long *lmask = fl_key_get_start(&mask->key, mask);
128 long *lmkey = fl_key_get_start(mkey, mask);
131 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
132 *lmkey++ = *lkey++ & *lmask++;
135 static void fl_clear_masked_range(struct fl_flow_key *key,
136 struct fl_flow_mask *mask)
138 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
141 static struct cls_fl_filter *fl_lookup(struct cls_fl_head *head,
142 struct fl_flow_key *mkey)
144 return rhashtable_lookup_fast(&head->ht,
145 fl_key_get_start(mkey, &head->mask),
149 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
150 struct tcf_result *res)
152 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
153 struct cls_fl_filter *f;
154 struct fl_flow_key skb_key;
155 struct fl_flow_key skb_mkey;
156 struct ip_tunnel_info *info;
158 if (!atomic_read(&head->ht.nelems))
161 fl_clear_masked_range(&skb_key, &head->mask);
163 info = skb_tunnel_info(skb);
165 struct ip_tunnel_key *key = &info->key;
167 switch (ip_tunnel_info_af(info)) {
169 skb_key.enc_control.addr_type =
170 FLOW_DISSECTOR_KEY_IPV4_ADDRS;
171 skb_key.enc_ipv4.src = key->u.ipv4.src;
172 skb_key.enc_ipv4.dst = key->u.ipv4.dst;
175 skb_key.enc_control.addr_type =
176 FLOW_DISSECTOR_KEY_IPV6_ADDRS;
177 skb_key.enc_ipv6.src = key->u.ipv6.src;
178 skb_key.enc_ipv6.dst = key->u.ipv6.dst;
182 skb_key.enc_key_id.keyid = tunnel_id_to_key32(key->tun_id);
183 skb_key.enc_tp.src = key->tp_src;
184 skb_key.enc_tp.dst = key->tp_dst;
187 skb_key.indev_ifindex = skb->skb_iif;
188 /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
189 * so do it rather here.
191 skb_key.basic.n_proto = skb->protocol;
192 skb_flow_dissect(skb, &head->dissector, &skb_key, 0);
194 fl_set_masked_key(&skb_mkey, &skb_key, &head->mask);
196 f = fl_lookup(head, &skb_mkey);
197 if (f && !tc_skip_sw(f->flags)) {
199 return tcf_exts_exec(skb, &f->exts, res);
204 static int fl_init(struct tcf_proto *tp)
206 struct cls_fl_head *head;
208 head = kzalloc(sizeof(*head), GFP_KERNEL);
212 INIT_LIST_HEAD_RCU(&head->filters);
213 rcu_assign_pointer(tp->root, head);
218 static void fl_destroy_filter(struct rcu_head *head)
220 struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
222 tcf_exts_destroy(&f->exts);
226 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f)
228 struct tc_cls_flower_offload offload = {0};
229 struct net_device *dev = f->hw_dev;
230 struct tc_to_netdev *tc = &f->tc;
232 if (!tc_can_offload(dev, tp))
235 offload.command = TC_CLSFLOWER_DESTROY;
236 offload.prio = tp->prio;
237 offload.cookie = (unsigned long)f;
239 tc->cls_flower = &offload;
241 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSFLOWER, tp->q->handle,
242 tp->chain->index, tp->protocol, tc);
245 static int fl_hw_replace_filter(struct tcf_proto *tp,
246 struct flow_dissector *dissector,
247 struct fl_flow_key *mask,
248 struct cls_fl_filter *f)
250 struct net_device *dev = tp->q->dev_queue->dev;
251 struct tc_cls_flower_offload offload = {0};
252 struct tc_to_netdev *tc = &f->tc;
255 if (!tc_can_offload(dev, tp)) {
256 if (tcf_exts_get_dev(dev, &f->exts, &f->hw_dev) ||
257 (f->hw_dev && !tc_can_offload(f->hw_dev, tp))) {
259 return tc_skip_sw(f->flags) ? -EINVAL : 0;
262 offload.egress_dev = true;
267 offload.command = TC_CLSFLOWER_REPLACE;
268 offload.prio = tp->prio;
269 offload.cookie = (unsigned long)f;
270 offload.dissector = dissector;
272 offload.key = &f->mkey;
273 offload.exts = &f->exts;
275 tc->cls_flower = &offload;
277 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSFLOWER,
278 tp->q->handle, tp->chain->index,
281 f->flags |= TCA_CLS_FLAGS_IN_HW;
283 if (tc_skip_sw(f->flags))
288 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
290 struct tc_cls_flower_offload offload = {0};
291 struct net_device *dev = f->hw_dev;
292 struct tc_to_netdev *tc = &f->tc;
294 if (!tc_can_offload(dev, tp))
297 offload.command = TC_CLSFLOWER_STATS;
298 offload.prio = tp->prio;
299 offload.cookie = (unsigned long)f;
300 offload.exts = &f->exts;
302 tc->cls_flower = &offload;
304 dev->netdev_ops->ndo_setup_tc(dev, TC_CLSFLOWER_STATS, tp->q->handle,
305 tp->chain->index, tp->protocol, tc);
308 static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f)
310 list_del_rcu(&f->list);
311 if (!tc_skip_hw(f->flags))
312 fl_hw_destroy_filter(tp, f);
313 tcf_unbind_filter(tp, &f->res);
314 call_rcu(&f->rcu, fl_destroy_filter);
317 static void fl_destroy_sleepable(struct work_struct *work)
319 struct cls_fl_head *head = container_of(work, struct cls_fl_head,
321 if (head->mask_assigned)
322 rhashtable_destroy(&head->ht);
324 module_put(THIS_MODULE);
327 static void fl_destroy_rcu(struct rcu_head *rcu)
329 struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
331 INIT_WORK(&head->work, fl_destroy_sleepable);
332 schedule_work(&head->work);
335 static void fl_destroy(struct tcf_proto *tp)
337 struct cls_fl_head *head = rtnl_dereference(tp->root);
338 struct cls_fl_filter *f, *next;
340 list_for_each_entry_safe(f, next, &head->filters, list)
343 __module_get(THIS_MODULE);
344 call_rcu(&head->rcu, fl_destroy_rcu);
347 static unsigned long fl_get(struct tcf_proto *tp, u32 handle)
349 struct cls_fl_head *head = rtnl_dereference(tp->root);
350 struct cls_fl_filter *f;
352 list_for_each_entry(f, &head->filters, list)
353 if (f->handle == handle)
354 return (unsigned long) f;
358 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
359 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
360 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
361 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
363 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
364 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
365 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
366 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
367 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
368 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
369 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
370 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
371 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
372 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
373 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
374 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
375 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
376 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
377 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
378 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
379 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
380 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
381 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
382 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
383 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
384 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
385 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
386 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
387 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
388 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
389 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
390 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
391 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
392 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
393 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
394 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
395 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
396 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
397 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
398 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
399 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
400 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
401 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
402 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
403 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
404 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
405 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
406 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
407 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
408 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
409 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
410 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
411 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
412 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
413 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
414 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
415 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
416 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
417 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
418 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
419 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
420 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
421 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
422 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
423 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
424 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
425 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
426 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
427 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
428 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
429 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
430 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
431 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
432 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
433 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
434 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
437 static void fl_set_key_val(struct nlattr **tb,
438 void *val, int val_type,
439 void *mask, int mask_type, int len)
443 memcpy(val, nla_data(tb[val_type]), len);
444 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
445 memset(mask, 0xff, len);
447 memcpy(mask, nla_data(tb[mask_type]), len);
450 static int fl_set_key_mpls(struct nlattr **tb,
451 struct flow_dissector_key_mpls *key_val,
452 struct flow_dissector_key_mpls *key_mask)
454 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
455 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
456 key_mask->mpls_ttl = MPLS_TTL_MASK;
458 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
459 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
461 if (bos & ~MPLS_BOS_MASK)
463 key_val->mpls_bos = bos;
464 key_mask->mpls_bos = MPLS_BOS_MASK;
466 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
467 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
469 if (tc & ~MPLS_TC_MASK)
471 key_val->mpls_tc = tc;
472 key_mask->mpls_tc = MPLS_TC_MASK;
474 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
475 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
477 if (label & ~MPLS_LABEL_MASK)
479 key_val->mpls_label = label;
480 key_mask->mpls_label = MPLS_LABEL_MASK;
485 static void fl_set_key_vlan(struct nlattr **tb,
486 struct flow_dissector_key_vlan *key_val,
487 struct flow_dissector_key_vlan *key_mask)
489 #define VLAN_PRIORITY_MASK 0x7
491 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
493 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
494 key_mask->vlan_id = VLAN_VID_MASK;
496 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
497 key_val->vlan_priority =
498 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
500 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
504 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
505 u32 *dissector_key, u32 *dissector_mask,
506 u32 flower_flag_bit, u32 dissector_flag_bit)
508 if (flower_mask & flower_flag_bit) {
509 *dissector_mask |= dissector_flag_bit;
510 if (flower_key & flower_flag_bit)
511 *dissector_key |= dissector_flag_bit;
515 static int fl_set_key_flags(struct nlattr **tb,
516 u32 *flags_key, u32 *flags_mask)
520 /* mask is mandatory for flags */
521 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
524 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
525 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
530 fl_set_key_flag(key, mask, flags_key, flags_mask,
531 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
536 static void fl_set_key_ip(struct nlattr **tb,
537 struct flow_dissector_key_ip *key,
538 struct flow_dissector_key_ip *mask)
540 fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS,
541 &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK,
544 fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL,
545 &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK,
549 static int fl_set_key(struct net *net, struct nlattr **tb,
550 struct fl_flow_key *key, struct fl_flow_key *mask)
554 #ifdef CONFIG_NET_CLS_IND
555 if (tb[TCA_FLOWER_INDEV]) {
556 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]);
559 key->indev_ifindex = err;
560 mask->indev_ifindex = 0xffffffff;
564 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
565 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
566 sizeof(key->eth.dst));
567 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
568 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
569 sizeof(key->eth.src));
571 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
572 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
574 if (ethertype == htons(ETH_P_8021Q)) {
575 fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
576 fl_set_key_val(tb, &key->basic.n_proto,
577 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
578 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
579 sizeof(key->basic.n_proto));
581 key->basic.n_proto = ethertype;
582 mask->basic.n_proto = cpu_to_be16(~0);
586 if (key->basic.n_proto == htons(ETH_P_IP) ||
587 key->basic.n_proto == htons(ETH_P_IPV6)) {
588 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
589 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
590 sizeof(key->basic.ip_proto));
591 fl_set_key_ip(tb, &key->ip, &mask->ip);
594 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
595 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
596 mask->control.addr_type = ~0;
597 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
598 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
599 sizeof(key->ipv4.src));
600 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
601 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
602 sizeof(key->ipv4.dst));
603 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
604 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
605 mask->control.addr_type = ~0;
606 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
607 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
608 sizeof(key->ipv6.src));
609 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
610 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
611 sizeof(key->ipv6.dst));
614 if (key->basic.ip_proto == IPPROTO_TCP) {
615 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
616 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
617 sizeof(key->tp.src));
618 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
619 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
620 sizeof(key->tp.dst));
621 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
622 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
623 sizeof(key->tcp.flags));
624 } else if (key->basic.ip_proto == IPPROTO_UDP) {
625 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
626 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
627 sizeof(key->tp.src));
628 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
629 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
630 sizeof(key->tp.dst));
631 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
632 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
633 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
634 sizeof(key->tp.src));
635 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
636 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
637 sizeof(key->tp.dst));
638 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
639 key->basic.ip_proto == IPPROTO_ICMP) {
640 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
642 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
643 sizeof(key->icmp.type));
644 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
646 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
647 sizeof(key->icmp.code));
648 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
649 key->basic.ip_proto == IPPROTO_ICMPV6) {
650 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
652 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
653 sizeof(key->icmp.type));
654 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
656 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
657 sizeof(key->icmp.code));
658 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
659 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
660 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
663 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
664 key->basic.n_proto == htons(ETH_P_RARP)) {
665 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
666 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
667 sizeof(key->arp.sip));
668 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
669 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
670 sizeof(key->arp.tip));
671 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
672 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
673 sizeof(key->arp.op));
674 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
675 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
676 sizeof(key->arp.sha));
677 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
678 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
679 sizeof(key->arp.tha));
682 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
683 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
684 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
685 mask->enc_control.addr_type = ~0;
686 fl_set_key_val(tb, &key->enc_ipv4.src,
687 TCA_FLOWER_KEY_ENC_IPV4_SRC,
689 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
690 sizeof(key->enc_ipv4.src));
691 fl_set_key_val(tb, &key->enc_ipv4.dst,
692 TCA_FLOWER_KEY_ENC_IPV4_DST,
694 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
695 sizeof(key->enc_ipv4.dst));
698 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
699 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
700 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
701 mask->enc_control.addr_type = ~0;
702 fl_set_key_val(tb, &key->enc_ipv6.src,
703 TCA_FLOWER_KEY_ENC_IPV6_SRC,
705 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
706 sizeof(key->enc_ipv6.src));
707 fl_set_key_val(tb, &key->enc_ipv6.dst,
708 TCA_FLOWER_KEY_ENC_IPV6_DST,
710 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
711 sizeof(key->enc_ipv6.dst));
714 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
715 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
716 sizeof(key->enc_key_id.keyid));
718 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
719 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
720 sizeof(key->enc_tp.src));
722 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
723 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
724 sizeof(key->enc_tp.dst));
726 if (tb[TCA_FLOWER_KEY_FLAGS])
727 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
732 static bool fl_mask_eq(struct fl_flow_mask *mask1,
733 struct fl_flow_mask *mask2)
735 const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
736 const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
738 return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
739 !memcmp(lmask1, lmask2, fl_mask_range(mask1));
742 static const struct rhashtable_params fl_ht_params = {
743 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
744 .head_offset = offsetof(struct cls_fl_filter, ht_node),
745 .automatic_shrinking = true,
748 static int fl_init_hashtable(struct cls_fl_head *head,
749 struct fl_flow_mask *mask)
751 head->ht_params = fl_ht_params;
752 head->ht_params.key_len = fl_mask_range(mask);
753 head->ht_params.key_offset += mask->range.start;
755 return rhashtable_init(&head->ht, &head->ht_params);
758 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
759 #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
761 #define FL_KEY_IS_MASKED(mask, member) \
762 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
763 0, FL_KEY_MEMBER_SIZE(member)) \
765 #define FL_KEY_SET(keys, cnt, id, member) \
767 keys[cnt].key_id = id; \
768 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
772 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
774 if (FL_KEY_IS_MASKED(mask, member)) \
775 FL_KEY_SET(keys, cnt, id, member); \
778 static void fl_init_dissector(struct cls_fl_head *head,
779 struct fl_flow_mask *mask)
781 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
784 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
785 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
786 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
787 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
788 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
789 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
790 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
791 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
792 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
793 FLOW_DISSECTOR_KEY_PORTS, tp);
794 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
795 FLOW_DISSECTOR_KEY_IP, ip);
796 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
797 FLOW_DISSECTOR_KEY_TCP, tcp);
798 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
799 FLOW_DISSECTOR_KEY_ICMP, icmp);
800 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
801 FLOW_DISSECTOR_KEY_ARP, arp);
802 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
803 FLOW_DISSECTOR_KEY_MPLS, mpls);
804 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
805 FLOW_DISSECTOR_KEY_VLAN, vlan);
806 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
807 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
808 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
809 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
810 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
811 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
812 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
813 FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
814 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
816 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
817 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
819 skb_flow_dissector_init(&head->dissector, keys, cnt);
822 static int fl_check_assign_mask(struct cls_fl_head *head,
823 struct fl_flow_mask *mask)
827 if (head->mask_assigned) {
828 if (!fl_mask_eq(&head->mask, mask))
834 /* Mask is not assigned yet. So assign it and init hashtable
837 err = fl_init_hashtable(head, mask);
840 memcpy(&head->mask, mask, sizeof(head->mask));
841 head->mask_assigned = true;
843 fl_init_dissector(head, mask);
848 static int fl_set_parms(struct net *net, struct tcf_proto *tp,
849 struct cls_fl_filter *f, struct fl_flow_mask *mask,
850 unsigned long base, struct nlattr **tb,
851 struct nlattr *est, bool ovr)
855 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
859 if (tb[TCA_FLOWER_CLASSID]) {
860 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
861 tcf_bind_filter(tp, &f->res, base);
864 err = fl_set_key(net, tb, &f->key, &mask->key);
868 fl_mask_update_range(mask);
869 fl_set_masked_key(&f->mkey, &f->key, mask);
874 static u32 fl_grab_new_handle(struct tcf_proto *tp,
875 struct cls_fl_head *head)
877 unsigned int i = 0x80000000;
881 if (++head->hgen == 0x7FFFFFFF)
883 } while (--i > 0 && fl_get(tp, head->hgen));
885 if (unlikely(i == 0)) {
886 pr_err("Insufficient number of handles\n");
895 static int fl_change(struct net *net, struct sk_buff *in_skb,
896 struct tcf_proto *tp, unsigned long base,
897 u32 handle, struct nlattr **tca,
898 unsigned long *arg, bool ovr)
900 struct cls_fl_head *head = rtnl_dereference(tp->root);
901 struct cls_fl_filter *fold = (struct cls_fl_filter *) *arg;
902 struct cls_fl_filter *fnew;
904 struct fl_flow_mask mask = {};
907 if (!tca[TCA_OPTIONS])
910 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
914 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
919 if (fold && handle && fold->handle != handle) {
924 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
930 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
935 handle = fl_grab_new_handle(tp, head);
941 fnew->handle = handle;
943 if (tb[TCA_FLOWER_FLAGS]) {
944 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
946 if (!tc_flags_valid(fnew->flags)) {
952 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr);
956 err = fl_check_assign_mask(head, &mask);
960 if (!tc_skip_sw(fnew->flags)) {
961 if (!fold && fl_lookup(head, &fnew->mkey)) {
966 err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
972 if (!tc_skip_hw(fnew->flags)) {
973 err = fl_hw_replace_filter(tp,
981 if (!tc_in_hw(fnew->flags))
982 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
985 if (!tc_skip_sw(fold->flags))
986 rhashtable_remove_fast(&head->ht, &fold->ht_node,
988 if (!tc_skip_hw(fold->flags))
989 fl_hw_destroy_filter(tp, fold);
992 *arg = (unsigned long) fnew;
995 list_replace_rcu(&fold->list, &fnew->list);
996 tcf_unbind_filter(tp, &fold->res);
997 call_rcu(&fold->rcu, fl_destroy_filter);
999 list_add_tail_rcu(&fnew->list, &head->filters);
1006 tcf_exts_destroy(&fnew->exts);
1013 static int fl_delete(struct tcf_proto *tp, unsigned long arg, bool *last)
1015 struct cls_fl_head *head = rtnl_dereference(tp->root);
1016 struct cls_fl_filter *f = (struct cls_fl_filter *) arg;
1018 if (!tc_skip_sw(f->flags))
1019 rhashtable_remove_fast(&head->ht, &f->ht_node,
1022 *last = list_empty(&head->filters);
1026 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1028 struct cls_fl_head *head = rtnl_dereference(tp->root);
1029 struct cls_fl_filter *f;
1031 list_for_each_entry_rcu(f, &head->filters, list) {
1032 if (arg->count < arg->skip)
1034 if (arg->fn(tp, (unsigned long) f, arg) < 0) {
1043 static int fl_dump_key_val(struct sk_buff *skb,
1044 void *val, int val_type,
1045 void *mask, int mask_type, int len)
1049 if (!memchr_inv(mask, 0, len))
1051 err = nla_put(skb, val_type, len, val);
1054 if (mask_type != TCA_FLOWER_UNSPEC) {
1055 err = nla_put(skb, mask_type, len, mask);
1062 static int fl_dump_key_mpls(struct sk_buff *skb,
1063 struct flow_dissector_key_mpls *mpls_key,
1064 struct flow_dissector_key_mpls *mpls_mask)
1068 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1070 if (mpls_mask->mpls_ttl) {
1071 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1072 mpls_key->mpls_ttl);
1076 if (mpls_mask->mpls_tc) {
1077 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1082 if (mpls_mask->mpls_label) {
1083 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1084 mpls_key->mpls_label);
1088 if (mpls_mask->mpls_bos) {
1089 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1090 mpls_key->mpls_bos);
1097 static int fl_dump_key_ip(struct sk_buff *skb,
1098 struct flow_dissector_key_ip *key,
1099 struct flow_dissector_key_ip *mask)
1101 if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos,
1102 TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) ||
1103 fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl,
1104 TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl)))
1110 static int fl_dump_key_vlan(struct sk_buff *skb,
1111 struct flow_dissector_key_vlan *vlan_key,
1112 struct flow_dissector_key_vlan *vlan_mask)
1116 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1118 if (vlan_mask->vlan_id) {
1119 err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
1124 if (vlan_mask->vlan_priority) {
1125 err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
1126 vlan_key->vlan_priority);
1133 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1134 u32 *flower_key, u32 *flower_mask,
1135 u32 flower_flag_bit, u32 dissector_flag_bit)
1137 if (dissector_mask & dissector_flag_bit) {
1138 *flower_mask |= flower_flag_bit;
1139 if (dissector_key & dissector_flag_bit)
1140 *flower_key |= flower_flag_bit;
1144 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1150 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1156 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1157 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1159 _key = cpu_to_be32(key);
1160 _mask = cpu_to_be32(mask);
1162 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1166 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1169 static int fl_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
1170 struct sk_buff *skb, struct tcmsg *t)
1172 struct cls_fl_head *head = rtnl_dereference(tp->root);
1173 struct cls_fl_filter *f = (struct cls_fl_filter *) fh;
1174 struct nlattr *nest;
1175 struct fl_flow_key *key, *mask;
1180 t->tcm_handle = f->handle;
1182 nest = nla_nest_start(skb, TCA_OPTIONS);
1184 goto nla_put_failure;
1186 if (f->res.classid &&
1187 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1188 goto nla_put_failure;
1191 mask = &head->mask.key;
1193 if (mask->indev_ifindex) {
1194 struct net_device *dev;
1196 dev = __dev_get_by_index(net, key->indev_ifindex);
1197 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1198 goto nla_put_failure;
1201 if (!tc_skip_hw(f->flags))
1202 fl_hw_update_stats(tp, f);
1204 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1205 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1206 sizeof(key->eth.dst)) ||
1207 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1208 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1209 sizeof(key->eth.src)) ||
1210 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1211 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1212 sizeof(key->basic.n_proto)))
1213 goto nla_put_failure;
1215 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1216 goto nla_put_failure;
1218 if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
1219 goto nla_put_failure;
1221 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1222 key->basic.n_proto == htons(ETH_P_IPV6)) &&
1223 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1224 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1225 sizeof(key->basic.ip_proto)) ||
1226 fl_dump_key_ip(skb, &key->ip, &mask->ip)))
1227 goto nla_put_failure;
1229 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1230 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1231 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1232 sizeof(key->ipv4.src)) ||
1233 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1234 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1235 sizeof(key->ipv4.dst))))
1236 goto nla_put_failure;
1237 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1238 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1239 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1240 sizeof(key->ipv6.src)) ||
1241 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1242 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1243 sizeof(key->ipv6.dst))))
1244 goto nla_put_failure;
1246 if (key->basic.ip_proto == IPPROTO_TCP &&
1247 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1248 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1249 sizeof(key->tp.src)) ||
1250 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1251 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1252 sizeof(key->tp.dst)) ||
1253 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1254 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1255 sizeof(key->tcp.flags))))
1256 goto nla_put_failure;
1257 else if (key->basic.ip_proto == IPPROTO_UDP &&
1258 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1259 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1260 sizeof(key->tp.src)) ||
1261 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1262 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1263 sizeof(key->tp.dst))))
1264 goto nla_put_failure;
1265 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1266 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1267 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1268 sizeof(key->tp.src)) ||
1269 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1270 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1271 sizeof(key->tp.dst))))
1272 goto nla_put_failure;
1273 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1274 key->basic.ip_proto == IPPROTO_ICMP &&
1275 (fl_dump_key_val(skb, &key->icmp.type,
1276 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1277 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1278 sizeof(key->icmp.type)) ||
1279 fl_dump_key_val(skb, &key->icmp.code,
1280 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1281 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1282 sizeof(key->icmp.code))))
1283 goto nla_put_failure;
1284 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1285 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1286 (fl_dump_key_val(skb, &key->icmp.type,
1287 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1288 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1289 sizeof(key->icmp.type)) ||
1290 fl_dump_key_val(skb, &key->icmp.code,
1291 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1292 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1293 sizeof(key->icmp.code))))
1294 goto nla_put_failure;
1295 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1296 key->basic.n_proto == htons(ETH_P_RARP)) &&
1297 (fl_dump_key_val(skb, &key->arp.sip,
1298 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1299 TCA_FLOWER_KEY_ARP_SIP_MASK,
1300 sizeof(key->arp.sip)) ||
1301 fl_dump_key_val(skb, &key->arp.tip,
1302 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1303 TCA_FLOWER_KEY_ARP_TIP_MASK,
1304 sizeof(key->arp.tip)) ||
1305 fl_dump_key_val(skb, &key->arp.op,
1306 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1307 TCA_FLOWER_KEY_ARP_OP_MASK,
1308 sizeof(key->arp.op)) ||
1309 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1310 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1311 sizeof(key->arp.sha)) ||
1312 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1313 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1314 sizeof(key->arp.tha))))
1315 goto nla_put_failure;
1317 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1318 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1319 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1320 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1321 sizeof(key->enc_ipv4.src)) ||
1322 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1323 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1324 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1325 sizeof(key->enc_ipv4.dst))))
1326 goto nla_put_failure;
1327 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1328 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1329 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1330 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1331 sizeof(key->enc_ipv6.src)) ||
1332 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1333 TCA_FLOWER_KEY_ENC_IPV6_DST,
1334 &mask->enc_ipv6.dst,
1335 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1336 sizeof(key->enc_ipv6.dst))))
1337 goto nla_put_failure;
1339 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
1340 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
1341 sizeof(key->enc_key_id)) ||
1342 fl_dump_key_val(skb, &key->enc_tp.src,
1343 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1345 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1346 sizeof(key->enc_tp.src)) ||
1347 fl_dump_key_val(skb, &key->enc_tp.dst,
1348 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1350 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1351 sizeof(key->enc_tp.dst)))
1352 goto nla_put_failure;
1354 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1355 goto nla_put_failure;
1357 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1358 goto nla_put_failure;
1360 if (tcf_exts_dump(skb, &f->exts))
1361 goto nla_put_failure;
1363 nla_nest_end(skb, nest);
1365 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1366 goto nla_put_failure;
1371 nla_nest_cancel(skb, nest);
1375 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1377 .classify = fl_classify,
1379 .destroy = fl_destroy,
1381 .change = fl_change,
1382 .delete = fl_delete,
1385 .owner = THIS_MODULE,
1388 static int __init cls_fl_init(void)
1390 return register_tcf_proto_ops(&cls_fl_ops);
1393 static void __exit cls_fl_exit(void)
1395 unregister_tcf_proto_ops(&cls_fl_ops);
1398 module_init(cls_fl_init);
1399 module_exit(cls_fl_exit);
1401 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1402 MODULE_DESCRIPTION("Flower classifier");
1403 MODULE_LICENSE("GPL v2");