fed up with those stupid warnings
[mmotm.git] / net / sched / cls_flow.c
blob9402a7fd3785bd0153f80ccb627a7a8c75950952
1 /*
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>
19 #include <linux/in.h>
20 #include <linux/ip.h>
21 #include <linux/ipv6.h>
22 #include <linux/if_vlan.h>
24 #include <net/pkt_cls.h>
25 #include <net/ip.h>
26 #include <net/route.h>
27 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
28 #include <net/netfilter/nf_conntrack.h>
29 #endif
31 struct flow_head {
32 struct list_head filters;
35 struct flow_filter {
36 struct list_head list;
37 struct tcf_exts exts;
38 struct tcf_ematch_tree ematches;
39 struct timer_list perturb_timer;
40 u32 perturb_period;
41 u32 handle;
43 u32 nkeys;
44 u32 keymask;
45 u32 mode;
46 u32 mask;
47 u32 xor;
48 u32 rshift;
49 u32 addend;
50 u32 divisor;
51 u32 baseclass;
52 u32 hashrnd;
55 static const struct tcf_ext_map flow_ext_map = {
56 .action = TCA_FLOW_ACT,
57 .police = TCA_FLOW_POLICE,
60 static inline u32 addr_fold(void *addr)
62 unsigned long a = (unsigned long)addr;
64 return (a & 0xFFFFFFFF) ^ (BITS_PER_LONG > 32 ? a >> 32 : 0);
67 static u32 flow_get_src(const struct sk_buff *skb)
69 switch (skb->protocol) {
70 case htons(ETH_P_IP):
71 return ntohl(ip_hdr(skb)->saddr);
72 case htons(ETH_P_IPV6):
73 return ntohl(ipv6_hdr(skb)->saddr.s6_addr32[3]);
74 default:
75 return addr_fold(skb->sk);
79 static u32 flow_get_dst(const struct sk_buff *skb)
81 switch (skb->protocol) {
82 case htons(ETH_P_IP):
83 return ntohl(ip_hdr(skb)->daddr);
84 case htons(ETH_P_IPV6):
85 return ntohl(ipv6_hdr(skb)->daddr.s6_addr32[3]);
86 default:
87 return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
91 static u32 flow_get_proto(const struct sk_buff *skb)
93 switch (skb->protocol) {
94 case htons(ETH_P_IP):
95 return ip_hdr(skb)->protocol;
96 case htons(ETH_P_IPV6):
97 return ipv6_hdr(skb)->nexthdr;
98 default:
99 return 0;
103 static int has_ports(u8 protocol)
105 switch (protocol) {
106 case IPPROTO_TCP:
107 case IPPROTO_UDP:
108 case IPPROTO_UDPLITE:
109 case IPPROTO_SCTP:
110 case IPPROTO_DCCP:
111 case IPPROTO_ESP:
112 return 1;
113 default:
114 return 0;
118 static u32 flow_get_proto_src(const struct sk_buff *skb)
120 u32 res = 0;
122 switch (skb->protocol) {
123 case htons(ETH_P_IP): {
124 struct iphdr *iph = ip_hdr(skb);
126 if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
127 has_ports(iph->protocol))
128 res = ntohs(*(__be16 *)((void *)iph + iph->ihl * 4));
129 break;
131 case htons(ETH_P_IPV6): {
132 struct ipv6hdr *iph = ipv6_hdr(skb);
134 if (has_ports(iph->nexthdr))
135 res = ntohs(*(__be16 *)&iph[1]);
136 break;
138 default:
139 res = addr_fold(skb->sk);
142 return res;
145 static u32 flow_get_proto_dst(const struct sk_buff *skb)
147 u32 res = 0;
149 switch (skb->protocol) {
150 case htons(ETH_P_IP): {
151 struct iphdr *iph = ip_hdr(skb);
153 if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
154 has_ports(iph->protocol))
155 res = ntohs(*(__be16 *)((void *)iph + iph->ihl * 4 + 2));
156 break;
158 case htons(ETH_P_IPV6): {
159 struct ipv6hdr *iph = ipv6_hdr(skb);
161 if (has_ports(iph->nexthdr))
162 res = ntohs(*(__be16 *)((void *)&iph[1] + 2));
163 break;
165 default:
166 res = addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
169 return res;
172 static u32 flow_get_iif(const struct sk_buff *skb)
174 return skb->iif;
177 static u32 flow_get_priority(const struct sk_buff *skb)
179 return skb->priority;
182 static u32 flow_get_mark(const struct sk_buff *skb)
184 return skb->mark;
187 static u32 flow_get_nfct(const struct sk_buff *skb)
189 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
190 return addr_fold(skb->nfct);
191 #else
192 return 0;
193 #endif
196 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
197 #define CTTUPLE(skb, member) \
198 ({ \
199 enum ip_conntrack_info ctinfo; \
200 struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \
201 if (ct == NULL) \
202 goto fallback; \
203 ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member; \
205 #else
206 #define CTTUPLE(skb, member) \
207 ({ \
208 goto fallback; \
209 0; \
211 #endif
213 static u32 flow_get_nfct_src(const struct sk_buff *skb)
215 switch (skb->protocol) {
216 case htons(ETH_P_IP):
217 return ntohl(CTTUPLE(skb, src.u3.ip));
218 case htons(ETH_P_IPV6):
219 return ntohl(CTTUPLE(skb, src.u3.ip6[3]));
221 fallback:
222 return flow_get_src(skb);
225 static u32 flow_get_nfct_dst(const struct sk_buff *skb)
227 switch (skb->protocol) {
228 case htons(ETH_P_IP):
229 return ntohl(CTTUPLE(skb, dst.u3.ip));
230 case htons(ETH_P_IPV6):
231 return ntohl(CTTUPLE(skb, dst.u3.ip6[3]));
233 fallback:
234 return flow_get_dst(skb);
237 static u32 flow_get_nfct_proto_src(const struct sk_buff *skb)
239 return ntohs(CTTUPLE(skb, src.u.all));
240 fallback:
241 return flow_get_proto_src(skb);
244 static u32 flow_get_nfct_proto_dst(const struct sk_buff *skb)
246 return ntohs(CTTUPLE(skb, dst.u.all));
247 fallback:
248 return flow_get_proto_dst(skb);
251 static u32 flow_get_rtclassid(const struct sk_buff *skb)
253 #ifdef CONFIG_NET_CLS_ROUTE
254 if (skb_dst(skb))
255 return skb_dst(skb)->tclassid;
256 #endif
257 return 0;
260 static u32 flow_get_skuid(const struct sk_buff *skb)
262 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file)
263 return skb->sk->sk_socket->file->f_cred->fsuid;
264 return 0;
267 static u32 flow_get_skgid(const struct sk_buff *skb)
269 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file)
270 return skb->sk->sk_socket->file->f_cred->fsgid;
271 return 0;
274 static u32 flow_get_vlan_tag(const struct sk_buff *skb)
276 u16 uninitialized_var(tag);
278 if (vlan_get_tag(skb, &tag) < 0)
279 return 0;
280 return tag & VLAN_VID_MASK;
283 static u32 flow_key_get(const struct sk_buff *skb, int key)
285 switch (key) {
286 case FLOW_KEY_SRC:
287 return flow_get_src(skb);
288 case FLOW_KEY_DST:
289 return flow_get_dst(skb);
290 case FLOW_KEY_PROTO:
291 return flow_get_proto(skb);
292 case FLOW_KEY_PROTO_SRC:
293 return flow_get_proto_src(skb);
294 case FLOW_KEY_PROTO_DST:
295 return flow_get_proto_dst(skb);
296 case FLOW_KEY_IIF:
297 return flow_get_iif(skb);
298 case FLOW_KEY_PRIORITY:
299 return flow_get_priority(skb);
300 case FLOW_KEY_MARK:
301 return flow_get_mark(skb);
302 case FLOW_KEY_NFCT:
303 return flow_get_nfct(skb);
304 case FLOW_KEY_NFCT_SRC:
305 return flow_get_nfct_src(skb);
306 case FLOW_KEY_NFCT_DST:
307 return flow_get_nfct_dst(skb);
308 case FLOW_KEY_NFCT_PROTO_SRC:
309 return flow_get_nfct_proto_src(skb);
310 case FLOW_KEY_NFCT_PROTO_DST:
311 return flow_get_nfct_proto_dst(skb);
312 case FLOW_KEY_RTCLASSID:
313 return flow_get_rtclassid(skb);
314 case FLOW_KEY_SKUID:
315 return flow_get_skuid(skb);
316 case FLOW_KEY_SKGID:
317 return flow_get_skgid(skb);
318 case FLOW_KEY_VLAN_TAG:
319 return flow_get_vlan_tag(skb);
320 default:
321 WARN_ON(1);
322 return 0;
326 static int flow_classify(struct sk_buff *skb, struct tcf_proto *tp,
327 struct tcf_result *res)
329 struct flow_head *head = tp->root;
330 struct flow_filter *f;
331 u32 keymask;
332 u32 classid;
333 unsigned int n, key;
334 int r;
336 list_for_each_entry(f, &head->filters, list) {
337 u32 keys[f->nkeys];
339 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
340 continue;
342 keymask = f->keymask;
344 for (n = 0; n < f->nkeys; n++) {
345 key = ffs(keymask) - 1;
346 keymask &= ~(1 << key);
347 keys[n] = flow_key_get(skb, key);
350 if (f->mode == FLOW_MODE_HASH)
351 classid = jhash2(keys, f->nkeys, f->hashrnd);
352 else {
353 classid = keys[0];
354 classid = (classid & f->mask) ^ f->xor;
355 classid = (classid >> f->rshift) + f->addend;
358 if (f->divisor)
359 classid %= f->divisor;
361 res->class = 0;
362 res->classid = TC_H_MAKE(f->baseclass, f->baseclass + classid);
364 r = tcf_exts_exec(skb, &f->exts, res);
365 if (r < 0)
366 continue;
367 return r;
369 return -1;
372 static void flow_perturbation(unsigned long arg)
374 struct flow_filter *f = (struct flow_filter *)arg;
376 get_random_bytes(&f->hashrnd, 4);
377 if (f->perturb_period)
378 mod_timer(&f->perturb_timer, jiffies + f->perturb_period);
381 static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
382 [TCA_FLOW_KEYS] = { .type = NLA_U32 },
383 [TCA_FLOW_MODE] = { .type = NLA_U32 },
384 [TCA_FLOW_BASECLASS] = { .type = NLA_U32 },
385 [TCA_FLOW_RSHIFT] = { .type = NLA_U32 },
386 [TCA_FLOW_ADDEND] = { .type = NLA_U32 },
387 [TCA_FLOW_MASK] = { .type = NLA_U32 },
388 [TCA_FLOW_XOR] = { .type = NLA_U32 },
389 [TCA_FLOW_DIVISOR] = { .type = NLA_U32 },
390 [TCA_FLOW_ACT] = { .type = NLA_NESTED },
391 [TCA_FLOW_POLICE] = { .type = NLA_NESTED },
392 [TCA_FLOW_EMATCHES] = { .type = NLA_NESTED },
393 [TCA_FLOW_PERTURB] = { .type = NLA_U32 },
396 static int flow_change(struct tcf_proto *tp, unsigned long base,
397 u32 handle, struct nlattr **tca,
398 unsigned long *arg)
400 struct flow_head *head = tp->root;
401 struct flow_filter *f;
402 struct nlattr *opt = tca[TCA_OPTIONS];
403 struct nlattr *tb[TCA_FLOW_MAX + 1];
404 struct tcf_exts e;
405 struct tcf_ematch_tree t;
406 unsigned int nkeys = 0;
407 unsigned int perturb_period = 0;
408 u32 baseclass = 0;
409 u32 keymask = 0;
410 u32 mode;
411 int err;
413 if (opt == NULL)
414 return -EINVAL;
416 err = nla_parse_nested(tb, TCA_FLOW_MAX, opt, flow_policy);
417 if (err < 0)
418 return err;
420 if (tb[TCA_FLOW_BASECLASS]) {
421 baseclass = nla_get_u32(tb[TCA_FLOW_BASECLASS]);
422 if (TC_H_MIN(baseclass) == 0)
423 return -EINVAL;
426 if (tb[TCA_FLOW_KEYS]) {
427 keymask = nla_get_u32(tb[TCA_FLOW_KEYS]);
429 nkeys = hweight32(keymask);
430 if (nkeys == 0)
431 return -EINVAL;
433 if (fls(keymask) - 1 > FLOW_KEY_MAX)
434 return -EOPNOTSUPP;
437 err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &flow_ext_map);
438 if (err < 0)
439 return err;
441 err = tcf_em_tree_validate(tp, tb[TCA_FLOW_EMATCHES], &t);
442 if (err < 0)
443 goto err1;
445 f = (struct flow_filter *)*arg;
446 if (f != NULL) {
447 err = -EINVAL;
448 if (f->handle != handle && handle)
449 goto err2;
451 mode = f->mode;
452 if (tb[TCA_FLOW_MODE])
453 mode = nla_get_u32(tb[TCA_FLOW_MODE]);
454 if (mode != FLOW_MODE_HASH && nkeys > 1)
455 goto err2;
457 if (mode == FLOW_MODE_HASH)
458 perturb_period = f->perturb_period;
459 if (tb[TCA_FLOW_PERTURB]) {
460 if (mode != FLOW_MODE_HASH)
461 goto err2;
462 perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
464 } else {
465 err = -EINVAL;
466 if (!handle)
467 goto err2;
468 if (!tb[TCA_FLOW_KEYS])
469 goto err2;
471 mode = FLOW_MODE_MAP;
472 if (tb[TCA_FLOW_MODE])
473 mode = nla_get_u32(tb[TCA_FLOW_MODE]);
474 if (mode != FLOW_MODE_HASH && nkeys > 1)
475 goto err2;
477 if (tb[TCA_FLOW_PERTURB]) {
478 if (mode != FLOW_MODE_HASH)
479 goto err2;
480 perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
483 if (TC_H_MAJ(baseclass) == 0)
484 baseclass = TC_H_MAKE(tp->q->handle, baseclass);
485 if (TC_H_MIN(baseclass) == 0)
486 baseclass = TC_H_MAKE(baseclass, 1);
488 err = -ENOBUFS;
489 f = kzalloc(sizeof(*f), GFP_KERNEL);
490 if (f == NULL)
491 goto err2;
493 f->handle = handle;
494 f->mask = ~0U;
496 get_random_bytes(&f->hashrnd, 4);
497 f->perturb_timer.function = flow_perturbation;
498 f->perturb_timer.data = (unsigned long)f;
499 init_timer_deferrable(&f->perturb_timer);
502 tcf_exts_change(tp, &f->exts, &e);
503 tcf_em_tree_change(tp, &f->ematches, &t);
505 tcf_tree_lock(tp);
507 if (tb[TCA_FLOW_KEYS]) {
508 f->keymask = keymask;
509 f->nkeys = nkeys;
512 f->mode = mode;
514 if (tb[TCA_FLOW_MASK])
515 f->mask = nla_get_u32(tb[TCA_FLOW_MASK]);
516 if (tb[TCA_FLOW_XOR])
517 f->xor = nla_get_u32(tb[TCA_FLOW_XOR]);
518 if (tb[TCA_FLOW_RSHIFT])
519 f->rshift = nla_get_u32(tb[TCA_FLOW_RSHIFT]);
520 if (tb[TCA_FLOW_ADDEND])
521 f->addend = nla_get_u32(tb[TCA_FLOW_ADDEND]);
523 if (tb[TCA_FLOW_DIVISOR])
524 f->divisor = nla_get_u32(tb[TCA_FLOW_DIVISOR]);
525 if (baseclass)
526 f->baseclass = baseclass;
528 f->perturb_period = perturb_period;
529 del_timer(&f->perturb_timer);
530 if (perturb_period)
531 mod_timer(&f->perturb_timer, jiffies + perturb_period);
533 if (*arg == 0)
534 list_add_tail(&f->list, &head->filters);
536 tcf_tree_unlock(tp);
538 *arg = (unsigned long)f;
539 return 0;
541 err2:
542 tcf_em_tree_destroy(tp, &t);
543 err1:
544 tcf_exts_destroy(tp, &e);
545 return err;
548 static void flow_destroy_filter(struct tcf_proto *tp, struct flow_filter *f)
550 del_timer_sync(&f->perturb_timer);
551 tcf_exts_destroy(tp, &f->exts);
552 tcf_em_tree_destroy(tp, &f->ematches);
553 kfree(f);
556 static int flow_delete(struct tcf_proto *tp, unsigned long arg)
558 struct flow_filter *f = (struct flow_filter *)arg;
560 tcf_tree_lock(tp);
561 list_del(&f->list);
562 tcf_tree_unlock(tp);
563 flow_destroy_filter(tp, f);
564 return 0;
567 static int flow_init(struct tcf_proto *tp)
569 struct flow_head *head;
571 head = kzalloc(sizeof(*head), GFP_KERNEL);
572 if (head == NULL)
573 return -ENOBUFS;
574 INIT_LIST_HEAD(&head->filters);
575 tp->root = head;
576 return 0;
579 static void flow_destroy(struct tcf_proto *tp)
581 struct flow_head *head = tp->root;
582 struct flow_filter *f, *next;
584 list_for_each_entry_safe(f, next, &head->filters, list) {
585 list_del(&f->list);
586 flow_destroy_filter(tp, f);
588 kfree(head);
591 static unsigned long flow_get(struct tcf_proto *tp, u32 handle)
593 struct flow_head *head = tp->root;
594 struct flow_filter *f;
596 list_for_each_entry(f, &head->filters, list)
597 if (f->handle == handle)
598 return (unsigned long)f;
599 return 0;
602 static void flow_put(struct tcf_proto *tp, unsigned long f)
604 return;
607 static int flow_dump(struct tcf_proto *tp, unsigned long fh,
608 struct sk_buff *skb, struct tcmsg *t)
610 struct flow_filter *f = (struct flow_filter *)fh;
611 struct nlattr *nest;
613 if (f == NULL)
614 return skb->len;
616 t->tcm_handle = f->handle;
618 nest = nla_nest_start(skb, TCA_OPTIONS);
619 if (nest == NULL)
620 goto nla_put_failure;
622 NLA_PUT_U32(skb, TCA_FLOW_KEYS, f->keymask);
623 NLA_PUT_U32(skb, TCA_FLOW_MODE, f->mode);
625 if (f->mask != ~0 || f->xor != 0) {
626 NLA_PUT_U32(skb, TCA_FLOW_MASK, f->mask);
627 NLA_PUT_U32(skb, TCA_FLOW_XOR, f->xor);
629 if (f->rshift)
630 NLA_PUT_U32(skb, TCA_FLOW_RSHIFT, f->rshift);
631 if (f->addend)
632 NLA_PUT_U32(skb, TCA_FLOW_ADDEND, f->addend);
634 if (f->divisor)
635 NLA_PUT_U32(skb, TCA_FLOW_DIVISOR, f->divisor);
636 if (f->baseclass)
637 NLA_PUT_U32(skb, TCA_FLOW_BASECLASS, f->baseclass);
639 if (f->perturb_period)
640 NLA_PUT_U32(skb, TCA_FLOW_PERTURB, f->perturb_period / HZ);
642 if (tcf_exts_dump(skb, &f->exts, &flow_ext_map) < 0)
643 goto nla_put_failure;
644 #ifdef CONFIG_NET_EMATCH
645 if (f->ematches.hdr.nmatches &&
646 tcf_em_tree_dump(skb, &f->ematches, TCA_FLOW_EMATCHES) < 0)
647 goto nla_put_failure;
648 #endif
649 nla_nest_end(skb, nest);
651 if (tcf_exts_dump_stats(skb, &f->exts, &flow_ext_map) < 0)
652 goto nla_put_failure;
654 return skb->len;
656 nla_put_failure:
657 nlmsg_trim(skb, nest);
658 return -1;
661 static void flow_walk(struct tcf_proto *tp, struct tcf_walker *arg)
663 struct flow_head *head = tp->root;
664 struct flow_filter *f;
666 list_for_each_entry(f, &head->filters, list) {
667 if (arg->count < arg->skip)
668 goto skip;
669 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
670 arg->stop = 1;
671 break;
673 skip:
674 arg->count++;
678 static struct tcf_proto_ops cls_flow_ops __read_mostly = {
679 .kind = "flow",
680 .classify = flow_classify,
681 .init = flow_init,
682 .destroy = flow_destroy,
683 .change = flow_change,
684 .delete = flow_delete,
685 .get = flow_get,
686 .put = flow_put,
687 .dump = flow_dump,
688 .walk = flow_walk,
689 .owner = THIS_MODULE,
692 static int __init cls_flow_init(void)
694 return register_tcf_proto_ops(&cls_flow_ops);
697 static void __exit cls_flow_exit(void)
699 unregister_tcf_proto_ops(&cls_flow_ops);
702 module_init(cls_flow_init);
703 module_exit(cls_flow_exit);
705 MODULE_LICENSE("GPL");
706 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
707 MODULE_DESCRIPTION("TC flow classifier");