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>
17 #include <linux/if_ether.h>
18 #include <linux/in6.h>
21 #include <net/sch_generic.h>
22 #include <net/pkt_cls.h>
24 #include <net/flow_dissector.h>
28 struct flow_dissector_key_control control
;
29 struct flow_dissector_key_basic basic
;
30 struct flow_dissector_key_eth_addrs eth
;
31 struct flow_dissector_key_addrs ipaddrs
;
33 struct flow_dissector_key_ipv4_addrs ipv4
;
34 struct flow_dissector_key_ipv6_addrs ipv6
;
36 struct flow_dissector_key_ports tp
;
37 } __aligned(BITS_PER_LONG
/ 8); /* Ensure that we can do comparisons as longs. */
39 struct fl_flow_mask_range
{
40 unsigned short int start
;
41 unsigned short int end
;
45 struct fl_flow_key key
;
46 struct fl_flow_mask_range range
;
52 struct fl_flow_mask mask
;
53 struct flow_dissector dissector
;
56 struct list_head filters
;
57 struct rhashtable_params ht_params
;
61 struct cls_fl_filter
{
62 struct rhash_head ht_node
;
63 struct fl_flow_key mkey
;
65 struct tcf_result res
;
66 struct fl_flow_key key
;
67 struct list_head list
;
72 static unsigned short int fl_mask_range(const struct fl_flow_mask
*mask
)
74 return mask
->range
.end
- mask
->range
.start
;
77 static void fl_mask_update_range(struct fl_flow_mask
*mask
)
79 const u8
*bytes
= (const u8
*) &mask
->key
;
80 size_t size
= sizeof(mask
->key
);
81 size_t i
, first
= 0, last
= size
- 1;
83 for (i
= 0; i
< sizeof(mask
->key
); i
++) {
90 mask
->range
.start
= rounddown(first
, sizeof(long));
91 mask
->range
.end
= roundup(last
+ 1, sizeof(long));
94 static void *fl_key_get_start(struct fl_flow_key
*key
,
95 const struct fl_flow_mask
*mask
)
97 return (u8
*) key
+ mask
->range
.start
;
100 static void fl_set_masked_key(struct fl_flow_key
*mkey
, struct fl_flow_key
*key
,
101 struct fl_flow_mask
*mask
)
103 const long *lkey
= fl_key_get_start(key
, mask
);
104 const long *lmask
= fl_key_get_start(&mask
->key
, mask
);
105 long *lmkey
= fl_key_get_start(mkey
, mask
);
108 for (i
= 0; i
< fl_mask_range(mask
); i
+= sizeof(long))
109 *lmkey
++ = *lkey
++ & *lmask
++;
112 static void fl_clear_masked_range(struct fl_flow_key
*key
,
113 struct fl_flow_mask
*mask
)
115 memset(fl_key_get_start(key
, mask
), 0, fl_mask_range(mask
));
118 static int fl_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
119 struct tcf_result
*res
)
121 struct cls_fl_head
*head
= rcu_dereference_bh(tp
->root
);
122 struct cls_fl_filter
*f
;
123 struct fl_flow_key skb_key
;
124 struct fl_flow_key skb_mkey
;
126 fl_clear_masked_range(&skb_key
, &head
->mask
);
127 skb_key
.indev_ifindex
= skb
->skb_iif
;
128 /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
129 * so do it rather here.
131 skb_key
.basic
.n_proto
= skb
->protocol
;
132 skb_flow_dissect(skb
, &head
->dissector
, &skb_key
, 0);
134 fl_set_masked_key(&skb_mkey
, &skb_key
, &head
->mask
);
136 f
= rhashtable_lookup_fast(&head
->ht
,
137 fl_key_get_start(&skb_mkey
, &head
->mask
),
141 return tcf_exts_exec(skb
, &f
->exts
, res
);
146 static int fl_init(struct tcf_proto
*tp
)
148 struct cls_fl_head
*head
;
150 head
= kzalloc(sizeof(*head
), GFP_KERNEL
);
154 INIT_LIST_HEAD_RCU(&head
->filters
);
155 rcu_assign_pointer(tp
->root
, head
);
160 static void fl_destroy_filter(struct rcu_head
*head
)
162 struct cls_fl_filter
*f
= container_of(head
, struct cls_fl_filter
, rcu
);
164 tcf_exts_destroy(&f
->exts
);
168 static void fl_hw_destroy_filter(struct tcf_proto
*tp
, unsigned long cookie
)
170 struct net_device
*dev
= tp
->q
->dev_queue
->dev
;
171 struct tc_cls_flower_offload offload
= {0};
172 struct tc_to_netdev tc
;
174 if (!tc_should_offload(dev
, tp
, 0))
177 offload
.command
= TC_CLSFLOWER_DESTROY
;
178 offload
.cookie
= cookie
;
180 tc
.type
= TC_SETUP_CLSFLOWER
;
181 tc
.cls_flower
= &offload
;
183 dev
->netdev_ops
->ndo_setup_tc(dev
, tp
->q
->handle
, tp
->protocol
, &tc
);
186 static void fl_hw_replace_filter(struct tcf_proto
*tp
,
187 struct flow_dissector
*dissector
,
188 struct fl_flow_key
*mask
,
189 struct fl_flow_key
*key
,
190 struct tcf_exts
*actions
,
191 unsigned long cookie
, u32 flags
)
193 struct net_device
*dev
= tp
->q
->dev_queue
->dev
;
194 struct tc_cls_flower_offload offload
= {0};
195 struct tc_to_netdev tc
;
197 if (!tc_should_offload(dev
, tp
, flags
))
200 offload
.command
= TC_CLSFLOWER_REPLACE
;
201 offload
.cookie
= cookie
;
202 offload
.dissector
= dissector
;
205 offload
.exts
= actions
;
207 tc
.type
= TC_SETUP_CLSFLOWER
;
208 tc
.cls_flower
= &offload
;
210 dev
->netdev_ops
->ndo_setup_tc(dev
, tp
->q
->handle
, tp
->protocol
, &tc
);
213 static void fl_hw_update_stats(struct tcf_proto
*tp
, struct cls_fl_filter
*f
)
215 struct net_device
*dev
= tp
->q
->dev_queue
->dev
;
216 struct tc_cls_flower_offload offload
= {0};
217 struct tc_to_netdev tc
;
219 if (!tc_should_offload(dev
, tp
, 0))
222 offload
.command
= TC_CLSFLOWER_STATS
;
223 offload
.cookie
= (unsigned long)f
;
224 offload
.exts
= &f
->exts
;
226 tc
.type
= TC_SETUP_CLSFLOWER
;
227 tc
.cls_flower
= &offload
;
229 dev
->netdev_ops
->ndo_setup_tc(dev
, tp
->q
->handle
, tp
->protocol
, &tc
);
232 static bool fl_destroy(struct tcf_proto
*tp
, bool force
)
234 struct cls_fl_head
*head
= rtnl_dereference(tp
->root
);
235 struct cls_fl_filter
*f
, *next
;
237 if (!force
&& !list_empty(&head
->filters
))
240 list_for_each_entry_safe(f
, next
, &head
->filters
, list
) {
241 fl_hw_destroy_filter(tp
, (unsigned long)f
);
242 list_del_rcu(&f
->list
);
243 call_rcu(&f
->rcu
, fl_destroy_filter
);
245 RCU_INIT_POINTER(tp
->root
, NULL
);
246 if (head
->mask_assigned
)
247 rhashtable_destroy(&head
->ht
);
248 kfree_rcu(head
, rcu
);
252 static unsigned long fl_get(struct tcf_proto
*tp
, u32 handle
)
254 struct cls_fl_head
*head
= rtnl_dereference(tp
->root
);
255 struct cls_fl_filter
*f
;
257 list_for_each_entry(f
, &head
->filters
, list
)
258 if (f
->handle
== handle
)
259 return (unsigned long) f
;
263 static const struct nla_policy fl_policy
[TCA_FLOWER_MAX
+ 1] = {
264 [TCA_FLOWER_UNSPEC
] = { .type
= NLA_UNSPEC
},
265 [TCA_FLOWER_CLASSID
] = { .type
= NLA_U32
},
266 [TCA_FLOWER_INDEV
] = { .type
= NLA_STRING
,
268 [TCA_FLOWER_KEY_ETH_DST
] = { .len
= ETH_ALEN
},
269 [TCA_FLOWER_KEY_ETH_DST_MASK
] = { .len
= ETH_ALEN
},
270 [TCA_FLOWER_KEY_ETH_SRC
] = { .len
= ETH_ALEN
},
271 [TCA_FLOWER_KEY_ETH_SRC_MASK
] = { .len
= ETH_ALEN
},
272 [TCA_FLOWER_KEY_ETH_TYPE
] = { .type
= NLA_U16
},
273 [TCA_FLOWER_KEY_IP_PROTO
] = { .type
= NLA_U8
},
274 [TCA_FLOWER_KEY_IPV4_SRC
] = { .type
= NLA_U32
},
275 [TCA_FLOWER_KEY_IPV4_SRC_MASK
] = { .type
= NLA_U32
},
276 [TCA_FLOWER_KEY_IPV4_DST
] = { .type
= NLA_U32
},
277 [TCA_FLOWER_KEY_IPV4_DST_MASK
] = { .type
= NLA_U32
},
278 [TCA_FLOWER_KEY_IPV6_SRC
] = { .len
= sizeof(struct in6_addr
) },
279 [TCA_FLOWER_KEY_IPV6_SRC_MASK
] = { .len
= sizeof(struct in6_addr
) },
280 [TCA_FLOWER_KEY_IPV6_DST
] = { .len
= sizeof(struct in6_addr
) },
281 [TCA_FLOWER_KEY_IPV6_DST_MASK
] = { .len
= sizeof(struct in6_addr
) },
282 [TCA_FLOWER_KEY_TCP_SRC
] = { .type
= NLA_U16
},
283 [TCA_FLOWER_KEY_TCP_DST
] = { .type
= NLA_U16
},
284 [TCA_FLOWER_KEY_UDP_SRC
] = { .type
= NLA_U16
},
285 [TCA_FLOWER_KEY_UDP_DST
] = { .type
= NLA_U16
},
288 static void fl_set_key_val(struct nlattr
**tb
,
289 void *val
, int val_type
,
290 void *mask
, int mask_type
, int len
)
294 memcpy(val
, nla_data(tb
[val_type
]), len
);
295 if (mask_type
== TCA_FLOWER_UNSPEC
|| !tb
[mask_type
])
296 memset(mask
, 0xff, len
);
298 memcpy(mask
, nla_data(tb
[mask_type
]), len
);
301 static int fl_set_key(struct net
*net
, struct nlattr
**tb
,
302 struct fl_flow_key
*key
, struct fl_flow_key
*mask
)
304 #ifdef CONFIG_NET_CLS_IND
305 if (tb
[TCA_FLOWER_INDEV
]) {
306 int err
= tcf_change_indev(net
, tb
[TCA_FLOWER_INDEV
]);
309 key
->indev_ifindex
= err
;
310 mask
->indev_ifindex
= 0xffffffff;
314 fl_set_key_val(tb
, key
->eth
.dst
, TCA_FLOWER_KEY_ETH_DST
,
315 mask
->eth
.dst
, TCA_FLOWER_KEY_ETH_DST_MASK
,
316 sizeof(key
->eth
.dst
));
317 fl_set_key_val(tb
, key
->eth
.src
, TCA_FLOWER_KEY_ETH_SRC
,
318 mask
->eth
.src
, TCA_FLOWER_KEY_ETH_SRC_MASK
,
319 sizeof(key
->eth
.src
));
321 fl_set_key_val(tb
, &key
->basic
.n_proto
, TCA_FLOWER_KEY_ETH_TYPE
,
322 &mask
->basic
.n_proto
, TCA_FLOWER_UNSPEC
,
323 sizeof(key
->basic
.n_proto
));
325 if (key
->basic
.n_proto
== htons(ETH_P_IP
) ||
326 key
->basic
.n_proto
== htons(ETH_P_IPV6
)) {
327 fl_set_key_val(tb
, &key
->basic
.ip_proto
, TCA_FLOWER_KEY_IP_PROTO
,
328 &mask
->basic
.ip_proto
, TCA_FLOWER_UNSPEC
,
329 sizeof(key
->basic
.ip_proto
));
332 if (tb
[TCA_FLOWER_KEY_IPV4_SRC
] || tb
[TCA_FLOWER_KEY_IPV4_DST
]) {
333 key
->control
.addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
334 fl_set_key_val(tb
, &key
->ipv4
.src
, TCA_FLOWER_KEY_IPV4_SRC
,
335 &mask
->ipv4
.src
, TCA_FLOWER_KEY_IPV4_SRC_MASK
,
336 sizeof(key
->ipv4
.src
));
337 fl_set_key_val(tb
, &key
->ipv4
.dst
, TCA_FLOWER_KEY_IPV4_DST
,
338 &mask
->ipv4
.dst
, TCA_FLOWER_KEY_IPV4_DST_MASK
,
339 sizeof(key
->ipv4
.dst
));
340 } else if (tb
[TCA_FLOWER_KEY_IPV6_SRC
] || tb
[TCA_FLOWER_KEY_IPV6_DST
]) {
341 key
->control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
342 fl_set_key_val(tb
, &key
->ipv6
.src
, TCA_FLOWER_KEY_IPV6_SRC
,
343 &mask
->ipv6
.src
, TCA_FLOWER_KEY_IPV6_SRC_MASK
,
344 sizeof(key
->ipv6
.src
));
345 fl_set_key_val(tb
, &key
->ipv6
.dst
, TCA_FLOWER_KEY_IPV6_DST
,
346 &mask
->ipv6
.dst
, TCA_FLOWER_KEY_IPV6_DST_MASK
,
347 sizeof(key
->ipv6
.dst
));
350 if (key
->basic
.ip_proto
== IPPROTO_TCP
) {
351 fl_set_key_val(tb
, &key
->tp
.src
, TCA_FLOWER_KEY_TCP_SRC
,
352 &mask
->tp
.src
, TCA_FLOWER_UNSPEC
,
353 sizeof(key
->tp
.src
));
354 fl_set_key_val(tb
, &key
->tp
.dst
, TCA_FLOWER_KEY_TCP_DST
,
355 &mask
->tp
.dst
, TCA_FLOWER_UNSPEC
,
356 sizeof(key
->tp
.dst
));
357 } else if (key
->basic
.ip_proto
== IPPROTO_UDP
) {
358 fl_set_key_val(tb
, &key
->tp
.src
, TCA_FLOWER_KEY_UDP_SRC
,
359 &mask
->tp
.src
, TCA_FLOWER_UNSPEC
,
360 sizeof(key
->tp
.src
));
361 fl_set_key_val(tb
, &key
->tp
.dst
, TCA_FLOWER_KEY_UDP_DST
,
362 &mask
->tp
.dst
, TCA_FLOWER_UNSPEC
,
363 sizeof(key
->tp
.dst
));
369 static bool fl_mask_eq(struct fl_flow_mask
*mask1
,
370 struct fl_flow_mask
*mask2
)
372 const long *lmask1
= fl_key_get_start(&mask1
->key
, mask1
);
373 const long *lmask2
= fl_key_get_start(&mask2
->key
, mask2
);
375 return !memcmp(&mask1
->range
, &mask2
->range
, sizeof(mask1
->range
)) &&
376 !memcmp(lmask1
, lmask2
, fl_mask_range(mask1
));
379 static const struct rhashtable_params fl_ht_params
= {
380 .key_offset
= offsetof(struct cls_fl_filter
, mkey
), /* base offset */
381 .head_offset
= offsetof(struct cls_fl_filter
, ht_node
),
382 .automatic_shrinking
= true,
385 static int fl_init_hashtable(struct cls_fl_head
*head
,
386 struct fl_flow_mask
*mask
)
388 head
->ht_params
= fl_ht_params
;
389 head
->ht_params
.key_len
= fl_mask_range(mask
);
390 head
->ht_params
.key_offset
+= mask
->range
.start
;
392 return rhashtable_init(&head
->ht
, &head
->ht_params
);
395 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
396 #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
397 #define FL_KEY_MEMBER_END_OFFSET(member) \
398 (FL_KEY_MEMBER_OFFSET(member) + FL_KEY_MEMBER_SIZE(member))
400 #define FL_KEY_IN_RANGE(mask, member) \
401 (FL_KEY_MEMBER_OFFSET(member) <= (mask)->range.end && \
402 FL_KEY_MEMBER_END_OFFSET(member) >= (mask)->range.start)
404 #define FL_KEY_SET(keys, cnt, id, member) \
406 keys[cnt].key_id = id; \
407 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
411 #define FL_KEY_SET_IF_IN_RANGE(mask, keys, cnt, id, member) \
413 if (FL_KEY_IN_RANGE(mask, member)) \
414 FL_KEY_SET(keys, cnt, id, member); \
417 static void fl_init_dissector(struct cls_fl_head
*head
,
418 struct fl_flow_mask
*mask
)
420 struct flow_dissector_key keys
[FLOW_DISSECTOR_KEY_MAX
];
423 FL_KEY_SET(keys
, cnt
, FLOW_DISSECTOR_KEY_CONTROL
, control
);
424 FL_KEY_SET(keys
, cnt
, FLOW_DISSECTOR_KEY_BASIC
, basic
);
425 FL_KEY_SET_IF_IN_RANGE(mask
, keys
, cnt
,
426 FLOW_DISSECTOR_KEY_ETH_ADDRS
, eth
);
427 FL_KEY_SET_IF_IN_RANGE(mask
, keys
, cnt
,
428 FLOW_DISSECTOR_KEY_IPV4_ADDRS
, ipv4
);
429 FL_KEY_SET_IF_IN_RANGE(mask
, keys
, cnt
,
430 FLOW_DISSECTOR_KEY_IPV6_ADDRS
, ipv6
);
431 FL_KEY_SET_IF_IN_RANGE(mask
, keys
, cnt
,
432 FLOW_DISSECTOR_KEY_PORTS
, tp
);
434 skb_flow_dissector_init(&head
->dissector
, keys
, cnt
);
437 static int fl_check_assign_mask(struct cls_fl_head
*head
,
438 struct fl_flow_mask
*mask
)
442 if (head
->mask_assigned
) {
443 if (!fl_mask_eq(&head
->mask
, mask
))
449 /* Mask is not assigned yet. So assign it and init hashtable
452 err
= fl_init_hashtable(head
, mask
);
455 memcpy(&head
->mask
, mask
, sizeof(head
->mask
));
456 head
->mask_assigned
= true;
458 fl_init_dissector(head
, mask
);
463 static int fl_set_parms(struct net
*net
, struct tcf_proto
*tp
,
464 struct cls_fl_filter
*f
, struct fl_flow_mask
*mask
,
465 unsigned long base
, struct nlattr
**tb
,
466 struct nlattr
*est
, bool ovr
)
471 tcf_exts_init(&e
, TCA_FLOWER_ACT
, 0);
472 err
= tcf_exts_validate(net
, tp
, tb
, est
, &e
, ovr
);
476 if (tb
[TCA_FLOWER_CLASSID
]) {
477 f
->res
.classid
= nla_get_u32(tb
[TCA_FLOWER_CLASSID
]);
478 tcf_bind_filter(tp
, &f
->res
, base
);
481 err
= fl_set_key(net
, tb
, &f
->key
, &mask
->key
);
485 fl_mask_update_range(mask
);
486 fl_set_masked_key(&f
->mkey
, &f
->key
, mask
);
488 tcf_exts_change(tp
, &f
->exts
, &e
);
492 tcf_exts_destroy(&e
);
496 static u32
fl_grab_new_handle(struct tcf_proto
*tp
,
497 struct cls_fl_head
*head
)
499 unsigned int i
= 0x80000000;
503 if (++head
->hgen
== 0x7FFFFFFF)
505 } while (--i
> 0 && fl_get(tp
, head
->hgen
));
507 if (unlikely(i
== 0)) {
508 pr_err("Insufficient number of handles\n");
517 static int fl_change(struct net
*net
, struct sk_buff
*in_skb
,
518 struct tcf_proto
*tp
, unsigned long base
,
519 u32 handle
, struct nlattr
**tca
,
520 unsigned long *arg
, bool ovr
)
522 struct cls_fl_head
*head
= rtnl_dereference(tp
->root
);
523 struct cls_fl_filter
*fold
= (struct cls_fl_filter
*) *arg
;
524 struct cls_fl_filter
*fnew
;
525 struct nlattr
*tb
[TCA_FLOWER_MAX
+ 1];
526 struct fl_flow_mask mask
= {};
530 if (!tca
[TCA_OPTIONS
])
533 err
= nla_parse_nested(tb
, TCA_FLOWER_MAX
, tca
[TCA_OPTIONS
], fl_policy
);
537 if (fold
&& handle
&& fold
->handle
!= handle
)
540 fnew
= kzalloc(sizeof(*fnew
), GFP_KERNEL
);
544 tcf_exts_init(&fnew
->exts
, TCA_FLOWER_ACT
, 0);
547 handle
= fl_grab_new_handle(tp
, head
);
553 fnew
->handle
= handle
;
555 if (tb
[TCA_FLOWER_FLAGS
])
556 flags
= nla_get_u32(tb
[TCA_FLOWER_FLAGS
]);
558 err
= fl_set_parms(net
, tp
, fnew
, &mask
, base
, tb
, tca
[TCA_RATE
], ovr
);
562 err
= fl_check_assign_mask(head
, &mask
);
566 err
= rhashtable_insert_fast(&head
->ht
, &fnew
->ht_node
,
571 fl_hw_replace_filter(tp
,
580 rhashtable_remove_fast(&head
->ht
, &fold
->ht_node
,
582 fl_hw_destroy_filter(tp
, (unsigned long)fold
);
585 *arg
= (unsigned long) fnew
;
588 list_replace_rcu(&fold
->list
, &fnew
->list
);
589 tcf_unbind_filter(tp
, &fold
->res
);
590 call_rcu(&fold
->rcu
, fl_destroy_filter
);
592 list_add_tail_rcu(&fnew
->list
, &head
->filters
);
602 static int fl_delete(struct tcf_proto
*tp
, unsigned long arg
)
604 struct cls_fl_head
*head
= rtnl_dereference(tp
->root
);
605 struct cls_fl_filter
*f
= (struct cls_fl_filter
*) arg
;
607 rhashtable_remove_fast(&head
->ht
, &f
->ht_node
,
609 list_del_rcu(&f
->list
);
610 fl_hw_destroy_filter(tp
, (unsigned long)f
);
611 tcf_unbind_filter(tp
, &f
->res
);
612 call_rcu(&f
->rcu
, fl_destroy_filter
);
616 static void fl_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
618 struct cls_fl_head
*head
= rtnl_dereference(tp
->root
);
619 struct cls_fl_filter
*f
;
621 list_for_each_entry_rcu(f
, &head
->filters
, list
) {
622 if (arg
->count
< arg
->skip
)
624 if (arg
->fn(tp
, (unsigned long) f
, arg
) < 0) {
633 static int fl_dump_key_val(struct sk_buff
*skb
,
634 void *val
, int val_type
,
635 void *mask
, int mask_type
, int len
)
639 if (!memchr_inv(mask
, 0, len
))
641 err
= nla_put(skb
, val_type
, len
, val
);
644 if (mask_type
!= TCA_FLOWER_UNSPEC
) {
645 err
= nla_put(skb
, mask_type
, len
, mask
);
652 static int fl_dump(struct net
*net
, struct tcf_proto
*tp
, unsigned long fh
,
653 struct sk_buff
*skb
, struct tcmsg
*t
)
655 struct cls_fl_head
*head
= rtnl_dereference(tp
->root
);
656 struct cls_fl_filter
*f
= (struct cls_fl_filter
*) fh
;
658 struct fl_flow_key
*key
, *mask
;
663 t
->tcm_handle
= f
->handle
;
665 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
667 goto nla_put_failure
;
669 if (f
->res
.classid
&&
670 nla_put_u32(skb
, TCA_FLOWER_CLASSID
, f
->res
.classid
))
671 goto nla_put_failure
;
674 mask
= &head
->mask
.key
;
676 if (mask
->indev_ifindex
) {
677 struct net_device
*dev
;
679 dev
= __dev_get_by_index(net
, key
->indev_ifindex
);
680 if (dev
&& nla_put_string(skb
, TCA_FLOWER_INDEV
, dev
->name
))
681 goto nla_put_failure
;
684 fl_hw_update_stats(tp
, f
);
686 if (fl_dump_key_val(skb
, key
->eth
.dst
, TCA_FLOWER_KEY_ETH_DST
,
687 mask
->eth
.dst
, TCA_FLOWER_KEY_ETH_DST_MASK
,
688 sizeof(key
->eth
.dst
)) ||
689 fl_dump_key_val(skb
, key
->eth
.src
, TCA_FLOWER_KEY_ETH_SRC
,
690 mask
->eth
.src
, TCA_FLOWER_KEY_ETH_SRC_MASK
,
691 sizeof(key
->eth
.src
)) ||
692 fl_dump_key_val(skb
, &key
->basic
.n_proto
, TCA_FLOWER_KEY_ETH_TYPE
,
693 &mask
->basic
.n_proto
, TCA_FLOWER_UNSPEC
,
694 sizeof(key
->basic
.n_proto
)))
695 goto nla_put_failure
;
696 if ((key
->basic
.n_proto
== htons(ETH_P_IP
) ||
697 key
->basic
.n_proto
== htons(ETH_P_IPV6
)) &&
698 fl_dump_key_val(skb
, &key
->basic
.ip_proto
, TCA_FLOWER_KEY_IP_PROTO
,
699 &mask
->basic
.ip_proto
, TCA_FLOWER_UNSPEC
,
700 sizeof(key
->basic
.ip_proto
)))
701 goto nla_put_failure
;
703 if (key
->control
.addr_type
== FLOW_DISSECTOR_KEY_IPV4_ADDRS
&&
704 (fl_dump_key_val(skb
, &key
->ipv4
.src
, TCA_FLOWER_KEY_IPV4_SRC
,
705 &mask
->ipv4
.src
, TCA_FLOWER_KEY_IPV4_SRC_MASK
,
706 sizeof(key
->ipv4
.src
)) ||
707 fl_dump_key_val(skb
, &key
->ipv4
.dst
, TCA_FLOWER_KEY_IPV4_DST
,
708 &mask
->ipv4
.dst
, TCA_FLOWER_KEY_IPV4_DST_MASK
,
709 sizeof(key
->ipv4
.dst
))))
710 goto nla_put_failure
;
711 else if (key
->control
.addr_type
== FLOW_DISSECTOR_KEY_IPV6_ADDRS
&&
712 (fl_dump_key_val(skb
, &key
->ipv6
.src
, TCA_FLOWER_KEY_IPV6_SRC
,
713 &mask
->ipv6
.src
, TCA_FLOWER_KEY_IPV6_SRC_MASK
,
714 sizeof(key
->ipv6
.src
)) ||
715 fl_dump_key_val(skb
, &key
->ipv6
.dst
, TCA_FLOWER_KEY_IPV6_DST
,
716 &mask
->ipv6
.dst
, TCA_FLOWER_KEY_IPV6_DST_MASK
,
717 sizeof(key
->ipv6
.dst
))))
718 goto nla_put_failure
;
720 if (key
->basic
.ip_proto
== IPPROTO_TCP
&&
721 (fl_dump_key_val(skb
, &key
->tp
.src
, TCA_FLOWER_KEY_TCP_SRC
,
722 &mask
->tp
.src
, TCA_FLOWER_UNSPEC
,
723 sizeof(key
->tp
.src
)) ||
724 fl_dump_key_val(skb
, &key
->tp
.dst
, TCA_FLOWER_KEY_TCP_DST
,
725 &mask
->tp
.dst
, TCA_FLOWER_UNSPEC
,
726 sizeof(key
->tp
.dst
))))
727 goto nla_put_failure
;
728 else if (key
->basic
.ip_proto
== IPPROTO_UDP
&&
729 (fl_dump_key_val(skb
, &key
->tp
.src
, TCA_FLOWER_KEY_UDP_SRC
,
730 &mask
->tp
.src
, TCA_FLOWER_UNSPEC
,
731 sizeof(key
->tp
.src
)) ||
732 fl_dump_key_val(skb
, &key
->tp
.dst
, TCA_FLOWER_KEY_UDP_DST
,
733 &mask
->tp
.dst
, TCA_FLOWER_UNSPEC
,
734 sizeof(key
->tp
.dst
))))
735 goto nla_put_failure
;
737 if (tcf_exts_dump(skb
, &f
->exts
))
738 goto nla_put_failure
;
740 nla_nest_end(skb
, nest
);
742 if (tcf_exts_dump_stats(skb
, &f
->exts
) < 0)
743 goto nla_put_failure
;
748 nla_nest_cancel(skb
, nest
);
752 static struct tcf_proto_ops cls_fl_ops __read_mostly
= {
754 .classify
= fl_classify
,
756 .destroy
= fl_destroy
,
762 .owner
= THIS_MODULE
,
765 static int __init
cls_fl_init(void)
767 return register_tcf_proto_ops(&cls_fl_ops
);
770 static void __exit
cls_fl_exit(void)
772 unregister_tcf_proto_ops(&cls_fl_ops
);
775 module_init(cls_fl_init
);
776 module_exit(cls_fl_exit
);
778 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
779 MODULE_DESCRIPTION("Flower classifier");
780 MODULE_LICENSE("GPL v2");