2 * net/sched/cls_flow.c Generic flow classifier
4 * Copyright (c) 2007, 2008 Patrick McHardy <kaber@trash.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/list.h>
15 #include <linux/jhash.h>
16 #include <linux/random.h>
17 #include <linux/pkt_cls.h>
18 #include <linux/skbuff.h>
21 #include <linux/ipv6.h>
22 #include <linux/if_vlan.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
26 #include <net/pkt_cls.h>
28 #include <net/route.h>
29 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
30 #include <net/netfilter/nf_conntrack.h>
34 struct list_head filters;
38 struct list_head list;
40 struct tcf_ematch_tree ematches;
41 struct timer_list perturb_timer;
57 static const struct tcf_ext_map flow_ext_map = {
58 .action = TCA_FLOW_ACT,
59 .police = TCA_FLOW_POLICE,
62 static inline u32 addr_fold(void *addr)
64 unsigned long a = (unsigned long)addr;
66 return (a & 0xFFFFFFFF) ^ (BITS_PER_LONG > 32 ? a >> 32 : 0);
69 static u32 flow_get_src(const struct sk_buff *skb, int nhoff)
71 __be32 *data = NULL, hdata;
73 switch (skb->protocol) {
75 data = skb_header_pointer(skb,
76 nhoff + offsetof(struct iphdr,
80 case htons(ETH_P_IPV6):
81 data = skb_header_pointer(skb,
82 nhoff + offsetof(struct ipv6hdr,
90 return addr_fold(skb->sk);
93 static u32 flow_get_dst(const struct sk_buff *skb, int nhoff)
95 __be32 *data = NULL, hdata;
97 switch (skb->protocol) {
99 data = skb_header_pointer(skb,
100 nhoff + offsetof(struct iphdr,
104 case htons(ETH_P_IPV6):
105 data = skb_header_pointer(skb,
106 nhoff + offsetof(struct ipv6hdr,
114 return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
117 static u32 flow_get_proto(const struct sk_buff *skb, int nhoff)
119 __u8 *data = NULL, hdata;
121 switch (skb->protocol) {
122 case htons(ETH_P_IP):
123 data = skb_header_pointer(skb,
124 nhoff + offsetof(struct iphdr,
128 case htons(ETH_P_IPV6):
129 data = skb_header_pointer(skb,
130 nhoff + offsetof(struct ipv6hdr,
140 /* helper function to get either src or dst port */
141 static __be16 *flow_get_proto_common(const struct sk_buff *skb, int nhoff,
142 __be16 *_port, int dst)
147 switch (skb->protocol) {
148 case htons(ETH_P_IP): {
149 struct iphdr *iph, _iph;
151 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
154 if (ip_is_fragment(iph))
156 poff = proto_ports_offset(iph->protocol);
158 port = skb_header_pointer(skb,
159 nhoff + iph->ihl * 4 + poff + dst,
160 sizeof(*_port), _port);
163 case htons(ETH_P_IPV6): {
164 struct ipv6hdr *iph, _iph;
166 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
169 poff = proto_ports_offset(iph->nexthdr);
171 port = skb_header_pointer(skb,
172 nhoff + sizeof(*iph) + poff + dst,
173 sizeof(*_port), _port);
181 static u32 flow_get_proto_src(const struct sk_buff *skb, int nhoff)
183 __be16 _port, *port = flow_get_proto_common(skb, nhoff, &_port, 0);
188 return addr_fold(skb->sk);
191 static u32 flow_get_proto_dst(const struct sk_buff *skb, int nhoff)
193 __be16 _port, *port = flow_get_proto_common(skb, nhoff, &_port, 2);
198 return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
201 static u32 flow_get_iif(const struct sk_buff *skb)
206 static u32 flow_get_priority(const struct sk_buff *skb)
208 return skb->priority;
211 static u32 flow_get_mark(const struct sk_buff *skb)
216 static u32 flow_get_nfct(const struct sk_buff *skb)
218 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
219 return addr_fold(skb->nfct);
225 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
226 #define CTTUPLE(skb, member) \
228 enum ip_conntrack_info ctinfo; \
229 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \
232 ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member; \
235 #define CTTUPLE(skb, member) \
242 static u32 flow_get_nfct_src(const struct sk_buff *skb, int nhoff)
244 switch (skb->protocol) {
245 case htons(ETH_P_IP):
246 return ntohl(CTTUPLE(skb, src.u3.ip));
247 case htons(ETH_P_IPV6):
248 return ntohl(CTTUPLE(skb, src.u3.ip6[3]));
251 return flow_get_src(skb, nhoff);
254 static u32 flow_get_nfct_dst(const struct sk_buff *skb, int nhoff)
256 switch (skb->protocol) {
257 case htons(ETH_P_IP):
258 return ntohl(CTTUPLE(skb, dst.u3.ip));
259 case htons(ETH_P_IPV6):
260 return ntohl(CTTUPLE(skb, dst.u3.ip6[3]));
263 return flow_get_dst(skb, nhoff);
266 static u32 flow_get_nfct_proto_src(const struct sk_buff *skb, int nhoff)
268 return ntohs(CTTUPLE(skb, src.u.all));
270 return flow_get_proto_src(skb, nhoff);
273 static u32 flow_get_nfct_proto_dst(const struct sk_buff *skb, int nhoff)
275 return ntohs(CTTUPLE(skb, dst.u.all));
277 return flow_get_proto_dst(skb, nhoff);
280 static u32 flow_get_rtclassid(const struct sk_buff *skb)
282 #ifdef CONFIG_IP_ROUTE_CLASSID
284 return skb_dst(skb)->tclassid;
289 static u32 flow_get_skuid(const struct sk_buff *skb)
291 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file)
292 return skb->sk->sk_socket->file->f_cred->fsuid;
296 static u32 flow_get_skgid(const struct sk_buff *skb)
298 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file)
299 return skb->sk->sk_socket->file->f_cred->fsgid;
303 static u32 flow_get_vlan_tag(const struct sk_buff *skb)
305 u16 uninitialized_var(tag);
307 if (vlan_get_tag(skb, &tag) < 0)
309 return tag & VLAN_VID_MASK;
312 static u32 flow_get_rxhash(struct sk_buff *skb)
314 return skb_get_rxhash(skb);
317 static u32 flow_key_get(struct sk_buff *skb, int key)
319 int nhoff = skb_network_offset(skb);
323 return flow_get_src(skb, nhoff);
325 return flow_get_dst(skb, nhoff);
327 return flow_get_proto(skb, nhoff);
328 case FLOW_KEY_PROTO_SRC:
329 return flow_get_proto_src(skb, nhoff);
330 case FLOW_KEY_PROTO_DST:
331 return flow_get_proto_dst(skb, nhoff);
333 return flow_get_iif(skb);
334 case FLOW_KEY_PRIORITY:
335 return flow_get_priority(skb);
337 return flow_get_mark(skb);
339 return flow_get_nfct(skb);
340 case FLOW_KEY_NFCT_SRC:
341 return flow_get_nfct_src(skb, nhoff);
342 case FLOW_KEY_NFCT_DST:
343 return flow_get_nfct_dst(skb, nhoff);
344 case FLOW_KEY_NFCT_PROTO_SRC:
345 return flow_get_nfct_proto_src(skb, nhoff);
346 case FLOW_KEY_NFCT_PROTO_DST:
347 return flow_get_nfct_proto_dst(skb, nhoff);
348 case FLOW_KEY_RTCLASSID:
349 return flow_get_rtclassid(skb);
351 return flow_get_skuid(skb);
353 return flow_get_skgid(skb);
354 case FLOW_KEY_VLAN_TAG:
355 return flow_get_vlan_tag(skb);
356 case FLOW_KEY_RXHASH:
357 return flow_get_rxhash(skb);
364 static int flow_classify(struct sk_buff *skb, const struct tcf_proto *tp,
365 struct tcf_result *res)
367 struct flow_head *head = tp->root;
368 struct flow_filter *f;
374 list_for_each_entry(f, &head->filters, list) {
377 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
380 keymask = f->keymask;
382 for (n = 0; n < f->nkeys; n++) {
383 key = ffs(keymask) - 1;
384 keymask &= ~(1 << key);
385 keys[n] = flow_key_get(skb, key);
388 if (f->mode == FLOW_MODE_HASH)
389 classid = jhash2(keys, f->nkeys, f->hashrnd);
392 classid = (classid & f->mask) ^ f->xor;
393 classid = (classid >> f->rshift) + f->addend;
397 classid %= f->divisor;
400 res->classid = TC_H_MAKE(f->baseclass, f->baseclass + classid);
402 r = tcf_exts_exec(skb, &f->exts, res);
410 static void flow_perturbation(unsigned long arg)
412 struct flow_filter *f = (struct flow_filter *)arg;
414 get_random_bytes(&f->hashrnd, 4);
415 if (f->perturb_period)
416 mod_timer(&f->perturb_timer, jiffies + f->perturb_period);
419 static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
420 [TCA_FLOW_KEYS] = { .type = NLA_U32 },
421 [TCA_FLOW_MODE] = { .type = NLA_U32 },
422 [TCA_FLOW_BASECLASS] = { .type = NLA_U32 },
423 [TCA_FLOW_RSHIFT] = { .type = NLA_U32 },
424 [TCA_FLOW_ADDEND] = { .type = NLA_U32 },
425 [TCA_FLOW_MASK] = { .type = NLA_U32 },
426 [TCA_FLOW_XOR] = { .type = NLA_U32 },
427 [TCA_FLOW_DIVISOR] = { .type = NLA_U32 },
428 [TCA_FLOW_ACT] = { .type = NLA_NESTED },
429 [TCA_FLOW_POLICE] = { .type = NLA_NESTED },
430 [TCA_FLOW_EMATCHES] = { .type = NLA_NESTED },
431 [TCA_FLOW_PERTURB] = { .type = NLA_U32 },
434 static int flow_change(struct tcf_proto *tp, unsigned long base,
435 u32 handle, struct nlattr **tca,
438 struct flow_head *head = tp->root;
439 struct flow_filter *f;
440 struct nlattr *opt = tca[TCA_OPTIONS];
441 struct nlattr *tb[TCA_FLOW_MAX + 1];
443 struct tcf_ematch_tree t;
444 unsigned int nkeys = 0;
445 unsigned int perturb_period = 0;
454 err = nla_parse_nested(tb, TCA_FLOW_MAX, opt, flow_policy);
458 if (tb[TCA_FLOW_BASECLASS]) {
459 baseclass = nla_get_u32(tb[TCA_FLOW_BASECLASS]);
460 if (TC_H_MIN(baseclass) == 0)
464 if (tb[TCA_FLOW_KEYS]) {
465 keymask = nla_get_u32(tb[TCA_FLOW_KEYS]);
467 nkeys = hweight32(keymask);
471 if (fls(keymask) - 1 > FLOW_KEY_MAX)
475 err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &flow_ext_map);
479 err = tcf_em_tree_validate(tp, tb[TCA_FLOW_EMATCHES], &t);
483 f = (struct flow_filter *)*arg;
486 if (f->handle != handle && handle)
490 if (tb[TCA_FLOW_MODE])
491 mode = nla_get_u32(tb[TCA_FLOW_MODE]);
492 if (mode != FLOW_MODE_HASH && nkeys > 1)
495 if (mode == FLOW_MODE_HASH)
496 perturb_period = f->perturb_period;
497 if (tb[TCA_FLOW_PERTURB]) {
498 if (mode != FLOW_MODE_HASH)
500 perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
506 if (!tb[TCA_FLOW_KEYS])
509 mode = FLOW_MODE_MAP;
510 if (tb[TCA_FLOW_MODE])
511 mode = nla_get_u32(tb[TCA_FLOW_MODE]);
512 if (mode != FLOW_MODE_HASH && nkeys > 1)
515 if (tb[TCA_FLOW_PERTURB]) {
516 if (mode != FLOW_MODE_HASH)
518 perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
521 if (TC_H_MAJ(baseclass) == 0)
522 baseclass = TC_H_MAKE(tp->q->handle, baseclass);
523 if (TC_H_MIN(baseclass) == 0)
524 baseclass = TC_H_MAKE(baseclass, 1);
527 f = kzalloc(sizeof(*f), GFP_KERNEL);
534 get_random_bytes(&f->hashrnd, 4);
535 f->perturb_timer.function = flow_perturbation;
536 f->perturb_timer.data = (unsigned long)f;
537 init_timer_deferrable(&f->perturb_timer);
540 tcf_exts_change(tp, &f->exts, &e);
541 tcf_em_tree_change(tp, &f->ematches, &t);
545 if (tb[TCA_FLOW_KEYS]) {
546 f->keymask = keymask;
552 if (tb[TCA_FLOW_MASK])
553 f->mask = nla_get_u32(tb[TCA_FLOW_MASK]);
554 if (tb[TCA_FLOW_XOR])
555 f->xor = nla_get_u32(tb[TCA_FLOW_XOR]);
556 if (tb[TCA_FLOW_RSHIFT])
557 f->rshift = nla_get_u32(tb[TCA_FLOW_RSHIFT]);
558 if (tb[TCA_FLOW_ADDEND])
559 f->addend = nla_get_u32(tb[TCA_FLOW_ADDEND]);
561 if (tb[TCA_FLOW_DIVISOR])
562 f->divisor = nla_get_u32(tb[TCA_FLOW_DIVISOR]);
564 f->baseclass = baseclass;
566 f->perturb_period = perturb_period;
567 del_timer(&f->perturb_timer);
569 mod_timer(&f->perturb_timer, jiffies + perturb_period);
572 list_add_tail(&f->list, &head->filters);
576 *arg = (unsigned long)f;
580 tcf_em_tree_destroy(tp, &t);
582 tcf_exts_destroy(tp, &e);
586 static void flow_destroy_filter(struct tcf_proto *tp, struct flow_filter *f)
588 del_timer_sync(&f->perturb_timer);
589 tcf_exts_destroy(tp, &f->exts);
590 tcf_em_tree_destroy(tp, &f->ematches);
594 static int flow_delete(struct tcf_proto *tp, unsigned long arg)
596 struct flow_filter *f = (struct flow_filter *)arg;
601 flow_destroy_filter(tp, f);
605 static int flow_init(struct tcf_proto *tp)
607 struct flow_head *head;
609 head = kzalloc(sizeof(*head), GFP_KERNEL);
612 INIT_LIST_HEAD(&head->filters);
617 static void flow_destroy(struct tcf_proto *tp)
619 struct flow_head *head = tp->root;
620 struct flow_filter *f, *next;
622 list_for_each_entry_safe(f, next, &head->filters, list) {
624 flow_destroy_filter(tp, f);
629 static unsigned long flow_get(struct tcf_proto *tp, u32 handle)
631 struct flow_head *head = tp->root;
632 struct flow_filter *f;
634 list_for_each_entry(f, &head->filters, list)
635 if (f->handle == handle)
636 return (unsigned long)f;
640 static void flow_put(struct tcf_proto *tp, unsigned long f)
644 static int flow_dump(struct tcf_proto *tp, unsigned long fh,
645 struct sk_buff *skb, struct tcmsg *t)
647 struct flow_filter *f = (struct flow_filter *)fh;
653 t->tcm_handle = f->handle;
655 nest = nla_nest_start(skb, TCA_OPTIONS);
657 goto nla_put_failure;
659 NLA_PUT_U32(skb, TCA_FLOW_KEYS, f->keymask);
660 NLA_PUT_U32(skb, TCA_FLOW_MODE, f->mode);
662 if (f->mask != ~0 || f->xor != 0) {
663 NLA_PUT_U32(skb, TCA_FLOW_MASK, f->mask);
664 NLA_PUT_U32(skb, TCA_FLOW_XOR, f->xor);
667 NLA_PUT_U32(skb, TCA_FLOW_RSHIFT, f->rshift);
669 NLA_PUT_U32(skb, TCA_FLOW_ADDEND, f->addend);
672 NLA_PUT_U32(skb, TCA_FLOW_DIVISOR, f->divisor);
674 NLA_PUT_U32(skb, TCA_FLOW_BASECLASS, f->baseclass);
676 if (f->perturb_period)
677 NLA_PUT_U32(skb, TCA_FLOW_PERTURB, f->perturb_period / HZ);
679 if (tcf_exts_dump(skb, &f->exts, &flow_ext_map) < 0)
680 goto nla_put_failure;
681 #ifdef CONFIG_NET_EMATCH
682 if (f->ematches.hdr.nmatches &&
683 tcf_em_tree_dump(skb, &f->ematches, TCA_FLOW_EMATCHES) < 0)
684 goto nla_put_failure;
686 nla_nest_end(skb, nest);
688 if (tcf_exts_dump_stats(skb, &f->exts, &flow_ext_map) < 0)
689 goto nla_put_failure;
694 nlmsg_trim(skb, nest);
698 static void flow_walk(struct tcf_proto *tp, struct tcf_walker *arg)
700 struct flow_head *head = tp->root;
701 struct flow_filter *f;
703 list_for_each_entry(f, &head->filters, list) {
704 if (arg->count < arg->skip)
706 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
715 static struct tcf_proto_ops cls_flow_ops __read_mostly = {
717 .classify = flow_classify,
719 .destroy = flow_destroy,
720 .change = flow_change,
721 .delete = flow_delete,
726 .owner = THIS_MODULE,
729 static int __init cls_flow_init(void)
731 return register_tcf_proto_ops(&cls_flow_ops);
734 static void __exit cls_flow_exit(void)
736 unregister_tcf_proto_ops(&cls_flow_ops);
739 module_init(cls_flow_init);
740 module_exit(cls_flow_exit);
742 MODULE_LICENSE("GPL");
743 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
744 MODULE_DESCRIPTION("TC flow classifier");