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>
25 #include <net/pkt_cls.h>
27 #include <net/route.h>
28 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
29 #include <net/netfilter/nf_conntrack.h>
33 struct list_head filters;
37 struct list_head list;
39 struct tcf_ematch_tree ematches;
40 struct timer_list perturb_timer;
56 static const struct tcf_ext_map flow_ext_map = {
57 .action = TCA_FLOW_ACT,
58 .police = TCA_FLOW_POLICE,
61 static inline u32 addr_fold(void *addr)
63 unsigned long a = (unsigned long)addr;
65 return (a & 0xFFFFFFFF) ^ (BITS_PER_LONG > 32 ? a >> 32 : 0);
68 static u32 flow_get_src(const struct sk_buff *skb)
70 switch (skb->protocol) {
72 return ntohl(ip_hdr(skb)->saddr);
73 case htons(ETH_P_IPV6):
74 return ntohl(ipv6_hdr(skb)->saddr.s6_addr32[3]);
76 return addr_fold(skb->sk);
80 static u32 flow_get_dst(const struct sk_buff *skb)
82 switch (skb->protocol) {
84 return ntohl(ip_hdr(skb)->daddr);
85 case htons(ETH_P_IPV6):
86 return ntohl(ipv6_hdr(skb)->daddr.s6_addr32[3]);
88 return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
92 static u32 flow_get_proto(const struct sk_buff *skb)
94 switch (skb->protocol) {
96 return ip_hdr(skb)->protocol;
97 case htons(ETH_P_IPV6):
98 return ipv6_hdr(skb)->nexthdr;
104 static int has_ports(u8 protocol)
109 case IPPROTO_UDPLITE:
119 static u32 flow_get_proto_src(const struct sk_buff *skb)
123 switch (skb->protocol) {
124 case htons(ETH_P_IP): {
125 struct iphdr *iph = ip_hdr(skb);
127 if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
128 has_ports(iph->protocol))
129 res = ntohs(*(__be16 *)((void *)iph + iph->ihl * 4));
132 case htons(ETH_P_IPV6): {
133 struct ipv6hdr *iph = ipv6_hdr(skb);
135 if (has_ports(iph->nexthdr))
136 res = ntohs(*(__be16 *)&iph[1]);
140 res = addr_fold(skb->sk);
146 static u32 flow_get_proto_dst(const struct sk_buff *skb)
150 switch (skb->protocol) {
151 case htons(ETH_P_IP): {
152 struct iphdr *iph = ip_hdr(skb);
154 if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
155 has_ports(iph->protocol))
156 res = ntohs(*(__be16 *)((void *)iph + iph->ihl * 4 + 2));
159 case htons(ETH_P_IPV6): {
160 struct ipv6hdr *iph = ipv6_hdr(skb);
162 if (has_ports(iph->nexthdr))
163 res = ntohs(*(__be16 *)((void *)&iph[1] + 2));
167 res = addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
173 static u32 flow_get_iif(const struct sk_buff *skb)
178 static u32 flow_get_priority(const struct sk_buff *skb)
180 return skb->priority;
183 static u32 flow_get_mark(const struct sk_buff *skb)
188 static u32 flow_get_nfct(const struct sk_buff *skb)
190 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
191 return addr_fold(skb->nfct);
197 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
198 #define CTTUPLE(skb, member) \
200 enum ip_conntrack_info ctinfo; \
201 struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \
204 ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member; \
207 #define CTTUPLE(skb, member) \
214 static u32 flow_get_nfct_src(const struct sk_buff *skb)
216 switch (skb->protocol) {
217 case htons(ETH_P_IP):
218 return ntohl(CTTUPLE(skb, src.u3.ip));
219 case htons(ETH_P_IPV6):
220 return ntohl(CTTUPLE(skb, src.u3.ip6[3]));
223 return flow_get_src(skb);
226 static u32 flow_get_nfct_dst(const struct sk_buff *skb)
228 switch (skb->protocol) {
229 case htons(ETH_P_IP):
230 return ntohl(CTTUPLE(skb, dst.u3.ip));
231 case htons(ETH_P_IPV6):
232 return ntohl(CTTUPLE(skb, dst.u3.ip6[3]));
235 return flow_get_dst(skb);
238 static u32 flow_get_nfct_proto_src(const struct sk_buff *skb)
240 return ntohs(CTTUPLE(skb, src.u.all));
242 return flow_get_proto_src(skb);
245 static u32 flow_get_nfct_proto_dst(const struct sk_buff *skb)
247 return ntohs(CTTUPLE(skb, dst.u.all));
249 return flow_get_proto_dst(skb);
252 static u32 flow_get_rtclassid(const struct sk_buff *skb)
254 #ifdef CONFIG_NET_CLS_ROUTE
256 return skb_dst(skb)->tclassid;
261 static u32 flow_get_skuid(const struct sk_buff *skb)
263 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file)
264 return skb->sk->sk_socket->file->f_cred->fsuid;
268 static u32 flow_get_skgid(const struct sk_buff *skb)
270 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file)
271 return skb->sk->sk_socket->file->f_cred->fsgid;
275 static u32 flow_get_vlan_tag(const struct sk_buff *skb)
277 u16 uninitialized_var(tag);
279 if (vlan_get_tag(skb, &tag) < 0)
281 return tag & VLAN_VID_MASK;
284 static u32 flow_key_get(const struct sk_buff *skb, int key)
288 return flow_get_src(skb);
290 return flow_get_dst(skb);
292 return flow_get_proto(skb);
293 case FLOW_KEY_PROTO_SRC:
294 return flow_get_proto_src(skb);
295 case FLOW_KEY_PROTO_DST:
296 return flow_get_proto_dst(skb);
298 return flow_get_iif(skb);
299 case FLOW_KEY_PRIORITY:
300 return flow_get_priority(skb);
302 return flow_get_mark(skb);
304 return flow_get_nfct(skb);
305 case FLOW_KEY_NFCT_SRC:
306 return flow_get_nfct_src(skb);
307 case FLOW_KEY_NFCT_DST:
308 return flow_get_nfct_dst(skb);
309 case FLOW_KEY_NFCT_PROTO_SRC:
310 return flow_get_nfct_proto_src(skb);
311 case FLOW_KEY_NFCT_PROTO_DST:
312 return flow_get_nfct_proto_dst(skb);
313 case FLOW_KEY_RTCLASSID:
314 return flow_get_rtclassid(skb);
316 return flow_get_skuid(skb);
318 return flow_get_skgid(skb);
319 case FLOW_KEY_VLAN_TAG:
320 return flow_get_vlan_tag(skb);
327 static int flow_classify(struct sk_buff *skb, struct tcf_proto *tp,
328 struct tcf_result *res)
330 struct flow_head *head = tp->root;
331 struct flow_filter *f;
337 list_for_each_entry(f, &head->filters, list) {
340 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
343 keymask = f->keymask;
345 for (n = 0; n < f->nkeys; n++) {
346 key = ffs(keymask) - 1;
347 keymask &= ~(1 << key);
348 keys[n] = flow_key_get(skb, key);
351 if (f->mode == FLOW_MODE_HASH)
352 classid = jhash2(keys, f->nkeys, f->hashrnd);
355 classid = (classid & f->mask) ^ f->xor;
356 classid = (classid >> f->rshift) + f->addend;
360 classid %= f->divisor;
363 res->classid = TC_H_MAKE(f->baseclass, f->baseclass + classid);
365 r = tcf_exts_exec(skb, &f->exts, res);
373 static void flow_perturbation(unsigned long arg)
375 struct flow_filter *f = (struct flow_filter *)arg;
377 get_random_bytes(&f->hashrnd, 4);
378 if (f->perturb_period)
379 mod_timer(&f->perturb_timer, jiffies + f->perturb_period);
382 static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
383 [TCA_FLOW_KEYS] = { .type = NLA_U32 },
384 [TCA_FLOW_MODE] = { .type = NLA_U32 },
385 [TCA_FLOW_BASECLASS] = { .type = NLA_U32 },
386 [TCA_FLOW_RSHIFT] = { .type = NLA_U32 },
387 [TCA_FLOW_ADDEND] = { .type = NLA_U32 },
388 [TCA_FLOW_MASK] = { .type = NLA_U32 },
389 [TCA_FLOW_XOR] = { .type = NLA_U32 },
390 [TCA_FLOW_DIVISOR] = { .type = NLA_U32 },
391 [TCA_FLOW_ACT] = { .type = NLA_NESTED },
392 [TCA_FLOW_POLICE] = { .type = NLA_NESTED },
393 [TCA_FLOW_EMATCHES] = { .type = NLA_NESTED },
394 [TCA_FLOW_PERTURB] = { .type = NLA_U32 },
397 static int flow_change(struct tcf_proto *tp, unsigned long base,
398 u32 handle, struct nlattr **tca,
401 struct flow_head *head = tp->root;
402 struct flow_filter *f;
403 struct nlattr *opt = tca[TCA_OPTIONS];
404 struct nlattr *tb[TCA_FLOW_MAX + 1];
406 struct tcf_ematch_tree t;
407 unsigned int nkeys = 0;
408 unsigned int perturb_period = 0;
417 err = nla_parse_nested(tb, TCA_FLOW_MAX, opt, flow_policy);
421 if (tb[TCA_FLOW_BASECLASS]) {
422 baseclass = nla_get_u32(tb[TCA_FLOW_BASECLASS]);
423 if (TC_H_MIN(baseclass) == 0)
427 if (tb[TCA_FLOW_KEYS]) {
428 keymask = nla_get_u32(tb[TCA_FLOW_KEYS]);
430 nkeys = hweight32(keymask);
434 if (fls(keymask) - 1 > FLOW_KEY_MAX)
438 err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &flow_ext_map);
442 err = tcf_em_tree_validate(tp, tb[TCA_FLOW_EMATCHES], &t);
446 f = (struct flow_filter *)*arg;
449 if (f->handle != handle && handle)
453 if (tb[TCA_FLOW_MODE])
454 mode = nla_get_u32(tb[TCA_FLOW_MODE]);
455 if (mode != FLOW_MODE_HASH && nkeys > 1)
458 if (mode == FLOW_MODE_HASH)
459 perturb_period = f->perturb_period;
460 if (tb[TCA_FLOW_PERTURB]) {
461 if (mode != FLOW_MODE_HASH)
463 perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
469 if (!tb[TCA_FLOW_KEYS])
472 mode = FLOW_MODE_MAP;
473 if (tb[TCA_FLOW_MODE])
474 mode = nla_get_u32(tb[TCA_FLOW_MODE]);
475 if (mode != FLOW_MODE_HASH && nkeys > 1)
478 if (tb[TCA_FLOW_PERTURB]) {
479 if (mode != FLOW_MODE_HASH)
481 perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
484 if (TC_H_MAJ(baseclass) == 0)
485 baseclass = TC_H_MAKE(tp->q->handle, baseclass);
486 if (TC_H_MIN(baseclass) == 0)
487 baseclass = TC_H_MAKE(baseclass, 1);
490 f = kzalloc(sizeof(*f), GFP_KERNEL);
497 get_random_bytes(&f->hashrnd, 4);
498 f->perturb_timer.function = flow_perturbation;
499 f->perturb_timer.data = (unsigned long)f;
500 init_timer_deferrable(&f->perturb_timer);
503 tcf_exts_change(tp, &f->exts, &e);
504 tcf_em_tree_change(tp, &f->ematches, &t);
508 if (tb[TCA_FLOW_KEYS]) {
509 f->keymask = keymask;
515 if (tb[TCA_FLOW_MASK])
516 f->mask = nla_get_u32(tb[TCA_FLOW_MASK]);
517 if (tb[TCA_FLOW_XOR])
518 f->xor = nla_get_u32(tb[TCA_FLOW_XOR]);
519 if (tb[TCA_FLOW_RSHIFT])
520 f->rshift = nla_get_u32(tb[TCA_FLOW_RSHIFT]);
521 if (tb[TCA_FLOW_ADDEND])
522 f->addend = nla_get_u32(tb[TCA_FLOW_ADDEND]);
524 if (tb[TCA_FLOW_DIVISOR])
525 f->divisor = nla_get_u32(tb[TCA_FLOW_DIVISOR]);
527 f->baseclass = baseclass;
529 f->perturb_period = perturb_period;
530 del_timer(&f->perturb_timer);
532 mod_timer(&f->perturb_timer, jiffies + perturb_period);
535 list_add_tail(&f->list, &head->filters);
539 *arg = (unsigned long)f;
543 tcf_em_tree_destroy(tp, &t);
545 tcf_exts_destroy(tp, &e);
549 static void flow_destroy_filter(struct tcf_proto *tp, struct flow_filter *f)
551 del_timer_sync(&f->perturb_timer);
552 tcf_exts_destroy(tp, &f->exts);
553 tcf_em_tree_destroy(tp, &f->ematches);
557 static int flow_delete(struct tcf_proto *tp, unsigned long arg)
559 struct flow_filter *f = (struct flow_filter *)arg;
564 flow_destroy_filter(tp, f);
568 static int flow_init(struct tcf_proto *tp)
570 struct flow_head *head;
572 head = kzalloc(sizeof(*head), GFP_KERNEL);
575 INIT_LIST_HEAD(&head->filters);
580 static void flow_destroy(struct tcf_proto *tp)
582 struct flow_head *head = tp->root;
583 struct flow_filter *f, *next;
585 list_for_each_entry_safe(f, next, &head->filters, list) {
587 flow_destroy_filter(tp, f);
592 static unsigned long flow_get(struct tcf_proto *tp, u32 handle)
594 struct flow_head *head = tp->root;
595 struct flow_filter *f;
597 list_for_each_entry(f, &head->filters, list)
598 if (f->handle == handle)
599 return (unsigned long)f;
603 static void flow_put(struct tcf_proto *tp, unsigned long f)
608 static int flow_dump(struct tcf_proto *tp, unsigned long fh,
609 struct sk_buff *skb, struct tcmsg *t)
611 struct flow_filter *f = (struct flow_filter *)fh;
617 t->tcm_handle = f->handle;
619 nest = nla_nest_start(skb, TCA_OPTIONS);
621 goto nla_put_failure;
623 NLA_PUT_U32(skb, TCA_FLOW_KEYS, f->keymask);
624 NLA_PUT_U32(skb, TCA_FLOW_MODE, f->mode);
626 if (f->mask != ~0 || f->xor != 0) {
627 NLA_PUT_U32(skb, TCA_FLOW_MASK, f->mask);
628 NLA_PUT_U32(skb, TCA_FLOW_XOR, f->xor);
631 NLA_PUT_U32(skb, TCA_FLOW_RSHIFT, f->rshift);
633 NLA_PUT_U32(skb, TCA_FLOW_ADDEND, f->addend);
636 NLA_PUT_U32(skb, TCA_FLOW_DIVISOR, f->divisor);
638 NLA_PUT_U32(skb, TCA_FLOW_BASECLASS, f->baseclass);
640 if (f->perturb_period)
641 NLA_PUT_U32(skb, TCA_FLOW_PERTURB, f->perturb_period / HZ);
643 if (tcf_exts_dump(skb, &f->exts, &flow_ext_map) < 0)
644 goto nla_put_failure;
645 #ifdef CONFIG_NET_EMATCH
646 if (f->ematches.hdr.nmatches &&
647 tcf_em_tree_dump(skb, &f->ematches, TCA_FLOW_EMATCHES) < 0)
648 goto nla_put_failure;
650 nla_nest_end(skb, nest);
652 if (tcf_exts_dump_stats(skb, &f->exts, &flow_ext_map) < 0)
653 goto nla_put_failure;
658 nlmsg_trim(skb, nest);
662 static void flow_walk(struct tcf_proto *tp, struct tcf_walker *arg)
664 struct flow_head *head = tp->root;
665 struct flow_filter *f;
667 list_for_each_entry(f, &head->filters, list) {
668 if (arg->count < arg->skip)
670 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
679 static struct tcf_proto_ops cls_flow_ops __read_mostly = {
681 .classify = flow_classify,
683 .destroy = flow_destroy,
684 .change = flow_change,
685 .delete = flow_delete,
690 .owner = THIS_MODULE,
693 static int __init cls_flow_init(void)
695 return register_tcf_proto_ops(&cls_flow_ops);
698 static void __exit cls_flow_exit(void)
700 unregister_tcf_proto_ops(&cls_flow_ops);
703 module_init(cls_flow_init);
704 module_exit(cls_flow_exit);
706 MODULE_LICENSE("GPL");
707 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
708 MODULE_DESCRIPTION("TC flow classifier");