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>
24 #include <net/pkt_cls.h>
26 #include <net/route.h>
27 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
28 #include <net/netfilter/nf_conntrack.h>
32 struct list_head filters
;
36 struct list_head list
;
38 struct tcf_ematch_tree ematches
;
52 static u32 flow_hashrnd __read_mostly
;
53 static int flow_hashrnd_initted __read_mostly
;
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 __constant_htons(ETH_P_IP
):
71 return ntohl(ip_hdr(skb
)->saddr
);
72 case __constant_htons(ETH_P_IPV6
):
73 return ntohl(ipv6_hdr(skb
)->saddr
.s6_addr32
[3]);
75 return addr_fold(skb
->sk
);
79 static u32
flow_get_dst(const struct sk_buff
*skb
)
81 switch (skb
->protocol
) {
82 case __constant_htons(ETH_P_IP
):
83 return ntohl(ip_hdr(skb
)->daddr
);
84 case __constant_htons(ETH_P_IPV6
):
85 return ntohl(ipv6_hdr(skb
)->daddr
.s6_addr32
[3]);
87 return addr_fold(skb
->dst
) ^ (__force u16
)skb
->protocol
;
91 static u32
flow_get_proto(const struct sk_buff
*skb
)
93 switch (skb
->protocol
) {
94 case __constant_htons(ETH_P_IP
):
95 return ip_hdr(skb
)->protocol
;
96 case __constant_htons(ETH_P_IPV6
):
97 return ipv6_hdr(skb
)->nexthdr
;
103 static int has_ports(u8 protocol
)
108 case IPPROTO_UDPLITE
:
118 static u32
flow_get_proto_src(const struct sk_buff
*skb
)
122 switch (skb
->protocol
) {
123 case __constant_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));
131 case __constant_htons(ETH_P_IPV6
): {
132 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
134 if (has_ports(iph
->nexthdr
))
135 res
= ntohs(*(__be16
*)&iph
[1]);
139 res
= addr_fold(skb
->sk
);
145 static u32
flow_get_proto_dst(const struct sk_buff
*skb
)
149 switch (skb
->protocol
) {
150 case __constant_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));
158 case __constant_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));
166 res
= addr_fold(skb
->dst
) ^ (__force u16
)skb
->protocol
;
172 static u32
flow_get_iif(const struct sk_buff
*skb
)
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
)
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
);
196 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
197 #define CTTUPLE(skb, member) \
199 enum ip_conntrack_info ctinfo; \
200 struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \
203 ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member; \
206 #define CTTUPLE(skb, member) \
213 static u32
flow_get_nfct_src(const struct sk_buff
*skb
)
215 switch (skb
->protocol
) {
216 case __constant_htons(ETH_P_IP
):
217 return ntohl(CTTUPLE(skb
, src
.u3
.ip
));
218 case __constant_htons(ETH_P_IPV6
):
219 return ntohl(CTTUPLE(skb
, src
.u3
.ip6
[3]));
222 return flow_get_src(skb
);
225 static u32
flow_get_nfct_dst(const struct sk_buff
*skb
)
227 switch (skb
->protocol
) {
228 case __constant_htons(ETH_P_IP
):
229 return ntohl(CTTUPLE(skb
, dst
.u3
.ip
));
230 case __constant_htons(ETH_P_IPV6
):
231 return ntohl(CTTUPLE(skb
, dst
.u3
.ip6
[3]));
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
));
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
));
248 return flow_get_proto_dst(skb
);
251 static u32
flow_get_rtclassid(const struct sk_buff
*skb
)
253 #ifdef CONFIG_NET_CLS_ROUTE
255 return skb
->dst
->tclassid
;
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_uid
;
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_gid
;
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)
280 return tag
& VLAN_VID_MASK
;
283 static u32
flow_key_get(const struct sk_buff
*skb
, int key
)
287 return flow_get_src(skb
);
289 return flow_get_dst(skb
);
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
);
297 return flow_get_iif(skb
);
298 case FLOW_KEY_PRIORITY
:
299 return flow_get_priority(skb
);
301 return flow_get_mark(skb
);
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
);
315 return flow_get_skuid(skb
);
317 return flow_get_skgid(skb
);
318 case FLOW_KEY_VLAN_TAG
:
319 return flow_get_vlan_tag(skb
);
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
;
336 list_for_each_entry(f
, &head
->filters
, list
) {
339 if (!tcf_em_tree_match(skb
, &f
->ematches
, NULL
))
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
, flow_hashrnd
);
354 classid
= (classid
& f
->mask
) ^ f
->xor;
355 classid
= (classid
>> f
->rshift
) + f
->addend
;
359 classid
%= f
->divisor
;
362 res
->classid
= TC_H_MAKE(f
->baseclass
, f
->baseclass
+ classid
);
364 r
= tcf_exts_exec(skb
, &f
->exts
, res
);
372 static const struct nla_policy flow_policy
[TCA_FLOW_MAX
+ 1] = {
373 [TCA_FLOW_KEYS
] = { .type
= NLA_U32
},
374 [TCA_FLOW_MODE
] = { .type
= NLA_U32
},
375 [TCA_FLOW_BASECLASS
] = { .type
= NLA_U32
},
376 [TCA_FLOW_RSHIFT
] = { .type
= NLA_U32
},
377 [TCA_FLOW_ADDEND
] = { .type
= NLA_U32
},
378 [TCA_FLOW_MASK
] = { .type
= NLA_U32
},
379 [TCA_FLOW_XOR
] = { .type
= NLA_U32
},
380 [TCA_FLOW_DIVISOR
] = { .type
= NLA_U32
},
381 [TCA_FLOW_ACT
] = { .type
= NLA_NESTED
},
382 [TCA_FLOW_POLICE
] = { .type
= NLA_NESTED
},
383 [TCA_FLOW_EMATCHES
] = { .type
= NLA_NESTED
},
386 static int flow_change(struct tcf_proto
*tp
, unsigned long base
,
387 u32 handle
, struct nlattr
**tca
,
390 struct flow_head
*head
= tp
->root
;
391 struct flow_filter
*f
;
392 struct nlattr
*opt
= tca
[TCA_OPTIONS
];
393 struct nlattr
*tb
[TCA_FLOW_MAX
+ 1];
395 struct tcf_ematch_tree t
;
396 unsigned int nkeys
= 0;
405 err
= nla_parse_nested(tb
, TCA_FLOW_MAX
, opt
, flow_policy
);
409 if (tb
[TCA_FLOW_BASECLASS
]) {
410 baseclass
= nla_get_u32(tb
[TCA_FLOW_BASECLASS
]);
411 if (TC_H_MIN(baseclass
) == 0)
415 if (tb
[TCA_FLOW_KEYS
]) {
416 keymask
= nla_get_u32(tb
[TCA_FLOW_KEYS
]);
418 nkeys
= hweight32(keymask
);
422 if (fls(keymask
) - 1 > FLOW_KEY_MAX
)
426 err
= tcf_exts_validate(tp
, tb
, tca
[TCA_RATE
], &e
, &flow_ext_map
);
430 err
= tcf_em_tree_validate(tp
, tb
[TCA_FLOW_EMATCHES
], &t
);
434 f
= (struct flow_filter
*)*arg
;
437 if (f
->handle
!= handle
&& handle
)
441 if (tb
[TCA_FLOW_MODE
])
442 mode
= nla_get_u32(tb
[TCA_FLOW_MODE
]);
443 if (mode
!= FLOW_MODE_HASH
&& nkeys
> 1)
449 if (!tb
[TCA_FLOW_KEYS
])
452 mode
= FLOW_MODE_MAP
;
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 (TC_H_MAJ(baseclass
) == 0)
459 baseclass
= TC_H_MAKE(tp
->q
->handle
, baseclass
);
460 if (TC_H_MIN(baseclass
) == 0)
461 baseclass
= TC_H_MAKE(baseclass
, 1);
464 f
= kzalloc(sizeof(*f
), GFP_KERNEL
);
472 tcf_exts_change(tp
, &f
->exts
, &e
);
473 tcf_em_tree_change(tp
, &f
->ematches
, &t
);
477 if (tb
[TCA_FLOW_KEYS
]) {
478 f
->keymask
= keymask
;
484 if (tb
[TCA_FLOW_MASK
])
485 f
->mask
= nla_get_u32(tb
[TCA_FLOW_MASK
]);
486 if (tb
[TCA_FLOW_XOR
])
487 f
->xor = nla_get_u32(tb
[TCA_FLOW_XOR
]);
488 if (tb
[TCA_FLOW_RSHIFT
])
489 f
->rshift
= nla_get_u32(tb
[TCA_FLOW_RSHIFT
]);
490 if (tb
[TCA_FLOW_ADDEND
])
491 f
->addend
= nla_get_u32(tb
[TCA_FLOW_ADDEND
]);
493 if (tb
[TCA_FLOW_DIVISOR
])
494 f
->divisor
= nla_get_u32(tb
[TCA_FLOW_DIVISOR
]);
496 f
->baseclass
= baseclass
;
499 list_add_tail(&f
->list
, &head
->filters
);
503 *arg
= (unsigned long)f
;
507 tcf_em_tree_destroy(tp
, &t
);
509 tcf_exts_destroy(tp
, &e
);
513 static void flow_destroy_filter(struct tcf_proto
*tp
, struct flow_filter
*f
)
515 tcf_exts_destroy(tp
, &f
->exts
);
516 tcf_em_tree_destroy(tp
, &f
->ematches
);
520 static int flow_delete(struct tcf_proto
*tp
, unsigned long arg
)
522 struct flow_filter
*f
= (struct flow_filter
*)arg
;
527 flow_destroy_filter(tp
, f
);
531 static int flow_init(struct tcf_proto
*tp
)
533 struct flow_head
*head
;
535 if (!flow_hashrnd_initted
) {
536 get_random_bytes(&flow_hashrnd
, 4);
537 flow_hashrnd_initted
= 1;
540 head
= kzalloc(sizeof(*head
), GFP_KERNEL
);
543 INIT_LIST_HEAD(&head
->filters
);
548 static void flow_destroy(struct tcf_proto
*tp
)
550 struct flow_head
*head
= tp
->root
;
551 struct flow_filter
*f
, *next
;
553 list_for_each_entry_safe(f
, next
, &head
->filters
, list
) {
555 flow_destroy_filter(tp
, f
);
560 static unsigned long flow_get(struct tcf_proto
*tp
, u32 handle
)
562 struct flow_head
*head
= tp
->root
;
563 struct flow_filter
*f
;
565 list_for_each_entry(f
, &head
->filters
, list
)
566 if (f
->handle
== handle
)
567 return (unsigned long)f
;
571 static void flow_put(struct tcf_proto
*tp
, unsigned long f
)
576 static int flow_dump(struct tcf_proto
*tp
, unsigned long fh
,
577 struct sk_buff
*skb
, struct tcmsg
*t
)
579 struct flow_filter
*f
= (struct flow_filter
*)fh
;
585 t
->tcm_handle
= f
->handle
;
587 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
589 goto nla_put_failure
;
591 NLA_PUT_U32(skb
, TCA_FLOW_KEYS
, f
->keymask
);
592 NLA_PUT_U32(skb
, TCA_FLOW_MODE
, f
->mode
);
594 if (f
->mask
!= ~0 || f
->xor != 0) {
595 NLA_PUT_U32(skb
, TCA_FLOW_MASK
, f
->mask
);
596 NLA_PUT_U32(skb
, TCA_FLOW_XOR
, f
->xor);
599 NLA_PUT_U32(skb
, TCA_FLOW_RSHIFT
, f
->rshift
);
601 NLA_PUT_U32(skb
, TCA_FLOW_ADDEND
, f
->addend
);
604 NLA_PUT_U32(skb
, TCA_FLOW_DIVISOR
, f
->divisor
);
606 NLA_PUT_U32(skb
, TCA_FLOW_BASECLASS
, f
->baseclass
);
608 if (tcf_exts_dump(skb
, &f
->exts
, &flow_ext_map
) < 0)
609 goto nla_put_failure
;
610 #ifdef CONFIG_NET_EMATCH
611 if (f
->ematches
.hdr
.nmatches
&&
612 tcf_em_tree_dump(skb
, &f
->ematches
, TCA_FLOW_EMATCHES
) < 0)
613 goto nla_put_failure
;
615 nla_nest_end(skb
, nest
);
617 if (tcf_exts_dump_stats(skb
, &f
->exts
, &flow_ext_map
) < 0)
618 goto nla_put_failure
;
623 nlmsg_trim(skb
, nest
);
627 static void flow_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
629 struct flow_head
*head
= tp
->root
;
630 struct flow_filter
*f
;
632 list_for_each_entry(f
, &head
->filters
, list
) {
633 if (arg
->count
< arg
->skip
)
635 if (arg
->fn(tp
, (unsigned long)f
, arg
) < 0) {
644 static struct tcf_proto_ops cls_flow_ops __read_mostly
= {
646 .classify
= flow_classify
,
648 .destroy
= flow_destroy
,
649 .change
= flow_change
,
650 .delete = flow_delete
,
655 .owner
= THIS_MODULE
,
658 static int __init
cls_flow_init(void)
660 return register_tcf_proto_ops(&cls_flow_ops
);
663 static void __exit
cls_flow_exit(void)
665 unregister_tcf_proto_ops(&cls_flow_ops
);
668 module_init(cls_flow_init
);
669 module_exit(cls_flow_exit
);
671 MODULE_LICENSE("GPL");
672 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
673 MODULE_DESCRIPTION("TC flow classifier");