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 #include <net/flow_keys.h>
31 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
32 #include <net/netfilter/nf_conntrack.h>
36 struct list_head filters
;
41 struct list_head list
;
43 struct tcf_ematch_tree ematches
;
45 struct timer_list perturb_timer
;
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
, const struct flow_keys
*flow
)
72 return ntohl(flow
->src
);
73 return addr_fold(skb
->sk
);
76 static u32
flow_get_dst(const struct sk_buff
*skb
, const struct flow_keys
*flow
)
79 return ntohl(flow
->dst
);
80 return addr_fold(skb_dst(skb
)) ^ (__force u16
) tc_skb_protocol(skb
);
83 static u32
flow_get_proto(const struct sk_buff
*skb
, const struct flow_keys
*flow
)
85 return flow
->ip_proto
;
88 static u32
flow_get_proto_src(const struct sk_buff
*skb
, const struct flow_keys
*flow
)
91 return ntohs(flow
->port16
[0]);
93 return addr_fold(skb
->sk
);
96 static u32
flow_get_proto_dst(const struct sk_buff
*skb
, const struct flow_keys
*flow
)
99 return ntohs(flow
->port16
[1]);
101 return addr_fold(skb_dst(skb
)) ^ (__force u16
) tc_skb_protocol(skb
);
104 static u32
flow_get_iif(const struct sk_buff
*skb
)
109 static u32
flow_get_priority(const struct sk_buff
*skb
)
111 return skb
->priority
;
114 static u32
flow_get_mark(const struct sk_buff
*skb
)
119 static u32
flow_get_nfct(const struct sk_buff
*skb
)
121 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
122 return addr_fold(skb
->nfct
);
128 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
129 #define CTTUPLE(skb, member) \
131 enum ip_conntrack_info ctinfo; \
132 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \
135 ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member; \
138 #define CTTUPLE(skb, member) \
145 static u32
flow_get_nfct_src(const struct sk_buff
*skb
, const struct flow_keys
*flow
)
147 switch (tc_skb_protocol(skb
)) {
148 case htons(ETH_P_IP
):
149 return ntohl(CTTUPLE(skb
, src
.u3
.ip
));
150 case htons(ETH_P_IPV6
):
151 return ntohl(CTTUPLE(skb
, src
.u3
.ip6
[3]));
154 return flow_get_src(skb
, flow
);
157 static u32
flow_get_nfct_dst(const struct sk_buff
*skb
, const struct flow_keys
*flow
)
159 switch (tc_skb_protocol(skb
)) {
160 case htons(ETH_P_IP
):
161 return ntohl(CTTUPLE(skb
, dst
.u3
.ip
));
162 case htons(ETH_P_IPV6
):
163 return ntohl(CTTUPLE(skb
, dst
.u3
.ip6
[3]));
166 return flow_get_dst(skb
, flow
);
169 static u32
flow_get_nfct_proto_src(const struct sk_buff
*skb
, const struct flow_keys
*flow
)
171 return ntohs(CTTUPLE(skb
, src
.u
.all
));
173 return flow_get_proto_src(skb
, flow
);
176 static u32
flow_get_nfct_proto_dst(const struct sk_buff
*skb
, const struct flow_keys
*flow
)
178 return ntohs(CTTUPLE(skb
, dst
.u
.all
));
180 return flow_get_proto_dst(skb
, flow
);
183 static u32
flow_get_rtclassid(const struct sk_buff
*skb
)
185 #ifdef CONFIG_IP_ROUTE_CLASSID
187 return skb_dst(skb
)->tclassid
;
192 static u32
flow_get_skuid(const struct sk_buff
*skb
)
194 if (skb
->sk
&& skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
) {
195 kuid_t skuid
= skb
->sk
->sk_socket
->file
->f_cred
->fsuid
;
196 return from_kuid(&init_user_ns
, skuid
);
201 static u32
flow_get_skgid(const struct sk_buff
*skb
)
203 if (skb
->sk
&& skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
) {
204 kgid_t skgid
= skb
->sk
->sk_socket
->file
->f_cred
->fsgid
;
205 return from_kgid(&init_user_ns
, skgid
);
210 static u32
flow_get_vlan_tag(const struct sk_buff
*skb
)
212 u16
uninitialized_var(tag
);
214 if (vlan_get_tag(skb
, &tag
) < 0)
216 return tag
& VLAN_VID_MASK
;
219 static u32
flow_get_rxhash(struct sk_buff
*skb
)
221 return skb_get_hash(skb
);
224 static u32
flow_key_get(struct sk_buff
*skb
, int key
, struct flow_keys
*flow
)
228 return flow_get_src(skb
, flow
);
230 return flow_get_dst(skb
, flow
);
232 return flow_get_proto(skb
, flow
);
233 case FLOW_KEY_PROTO_SRC
:
234 return flow_get_proto_src(skb
, flow
);
235 case FLOW_KEY_PROTO_DST
:
236 return flow_get_proto_dst(skb
, flow
);
238 return flow_get_iif(skb
);
239 case FLOW_KEY_PRIORITY
:
240 return flow_get_priority(skb
);
242 return flow_get_mark(skb
);
244 return flow_get_nfct(skb
);
245 case FLOW_KEY_NFCT_SRC
:
246 return flow_get_nfct_src(skb
, flow
);
247 case FLOW_KEY_NFCT_DST
:
248 return flow_get_nfct_dst(skb
, flow
);
249 case FLOW_KEY_NFCT_PROTO_SRC
:
250 return flow_get_nfct_proto_src(skb
, flow
);
251 case FLOW_KEY_NFCT_PROTO_DST
:
252 return flow_get_nfct_proto_dst(skb
, flow
);
253 case FLOW_KEY_RTCLASSID
:
254 return flow_get_rtclassid(skb
);
256 return flow_get_skuid(skb
);
258 return flow_get_skgid(skb
);
259 case FLOW_KEY_VLAN_TAG
:
260 return flow_get_vlan_tag(skb
);
261 case FLOW_KEY_RXHASH
:
262 return flow_get_rxhash(skb
);
269 #define FLOW_KEYS_NEEDED ((1 << FLOW_KEY_SRC) | \
270 (1 << FLOW_KEY_DST) | \
271 (1 << FLOW_KEY_PROTO) | \
272 (1 << FLOW_KEY_PROTO_SRC) | \
273 (1 << FLOW_KEY_PROTO_DST) | \
274 (1 << FLOW_KEY_NFCT_SRC) | \
275 (1 << FLOW_KEY_NFCT_DST) | \
276 (1 << FLOW_KEY_NFCT_PROTO_SRC) | \
277 (1 << FLOW_KEY_NFCT_PROTO_DST))
279 static int flow_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
280 struct tcf_result
*res
)
282 struct flow_head
*head
= rcu_dereference_bh(tp
->root
);
283 struct flow_filter
*f
;
289 list_for_each_entry_rcu(f
, &head
->filters
, list
) {
290 u32 keys
[FLOW_KEY_MAX
+ 1];
291 struct flow_keys flow_keys
;
293 if (!tcf_em_tree_match(skb
, &f
->ematches
, NULL
))
296 keymask
= f
->keymask
;
297 if (keymask
& FLOW_KEYS_NEEDED
)
298 skb_flow_dissect(skb
, &flow_keys
);
300 for (n
= 0; n
< f
->nkeys
; n
++) {
301 key
= ffs(keymask
) - 1;
302 keymask
&= ~(1 << key
);
303 keys
[n
] = flow_key_get(skb
, key
, &flow_keys
);
306 if (f
->mode
== FLOW_MODE_HASH
)
307 classid
= jhash2(keys
, f
->nkeys
, f
->hashrnd
);
310 classid
= (classid
& f
->mask
) ^ f
->xor;
311 classid
= (classid
>> f
->rshift
) + f
->addend
;
315 classid
%= f
->divisor
;
318 res
->classid
= TC_H_MAKE(f
->baseclass
, f
->baseclass
+ classid
);
320 r
= tcf_exts_exec(skb
, &f
->exts
, res
);
328 static void flow_perturbation(unsigned long arg
)
330 struct flow_filter
*f
= (struct flow_filter
*)arg
;
332 get_random_bytes(&f
->hashrnd
, 4);
333 if (f
->perturb_period
)
334 mod_timer(&f
->perturb_timer
, jiffies
+ f
->perturb_period
);
337 static const struct nla_policy flow_policy
[TCA_FLOW_MAX
+ 1] = {
338 [TCA_FLOW_KEYS
] = { .type
= NLA_U32
},
339 [TCA_FLOW_MODE
] = { .type
= NLA_U32
},
340 [TCA_FLOW_BASECLASS
] = { .type
= NLA_U32
},
341 [TCA_FLOW_RSHIFT
] = { .type
= NLA_U32
},
342 [TCA_FLOW_ADDEND
] = { .type
= NLA_U32
},
343 [TCA_FLOW_MASK
] = { .type
= NLA_U32
},
344 [TCA_FLOW_XOR
] = { .type
= NLA_U32
},
345 [TCA_FLOW_DIVISOR
] = { .type
= NLA_U32
},
346 [TCA_FLOW_ACT
] = { .type
= NLA_NESTED
},
347 [TCA_FLOW_POLICE
] = { .type
= NLA_NESTED
},
348 [TCA_FLOW_EMATCHES
] = { .type
= NLA_NESTED
},
349 [TCA_FLOW_PERTURB
] = { .type
= NLA_U32
},
352 static void flow_destroy_filter(struct rcu_head
*head
)
354 struct flow_filter
*f
= container_of(head
, struct flow_filter
, rcu
);
356 del_timer_sync(&f
->perturb_timer
);
357 tcf_exts_destroy(&f
->exts
);
358 tcf_em_tree_destroy(&f
->ematches
);
362 static int flow_change(struct net
*net
, struct sk_buff
*in_skb
,
363 struct tcf_proto
*tp
, unsigned long base
,
364 u32 handle
, struct nlattr
**tca
,
365 unsigned long *arg
, bool ovr
)
367 struct flow_head
*head
= rtnl_dereference(tp
->root
);
368 struct flow_filter
*fold
, *fnew
;
369 struct nlattr
*opt
= tca
[TCA_OPTIONS
];
370 struct nlattr
*tb
[TCA_FLOW_MAX
+ 1];
372 struct tcf_ematch_tree t
;
373 unsigned int nkeys
= 0;
374 unsigned int perturb_period
= 0;
383 err
= nla_parse_nested(tb
, TCA_FLOW_MAX
, opt
, flow_policy
);
387 if (tb
[TCA_FLOW_BASECLASS
]) {
388 baseclass
= nla_get_u32(tb
[TCA_FLOW_BASECLASS
]);
389 if (TC_H_MIN(baseclass
) == 0)
393 if (tb
[TCA_FLOW_KEYS
]) {
394 keymask
= nla_get_u32(tb
[TCA_FLOW_KEYS
]);
396 nkeys
= hweight32(keymask
);
400 if (fls(keymask
) - 1 > FLOW_KEY_MAX
)
403 if ((keymask
& (FLOW_KEY_SKUID
|FLOW_KEY_SKGID
)) &&
404 sk_user_ns(NETLINK_CB(in_skb
).sk
) != &init_user_ns
)
408 tcf_exts_init(&e
, TCA_FLOW_ACT
, TCA_FLOW_POLICE
);
409 err
= tcf_exts_validate(net
, tp
, tb
, tca
[TCA_RATE
], &e
, ovr
);
413 err
= tcf_em_tree_validate(tp
, tb
[TCA_FLOW_EMATCHES
], &t
);
418 fnew
= kzalloc(sizeof(*fnew
), GFP_KERNEL
);
422 tcf_exts_init(&fnew
->exts
, TCA_FLOW_ACT
, TCA_FLOW_POLICE
);
424 fold
= (struct flow_filter
*)*arg
;
427 if (fold
->handle
!= handle
&& handle
)
430 /* Copy fold into fnew */
432 fnew
->handle
= fold
->handle
;
433 fnew
->nkeys
= fold
->nkeys
;
434 fnew
->keymask
= fold
->keymask
;
435 fnew
->mode
= fold
->mode
;
436 fnew
->mask
= fold
->mask
;
437 fnew
->xor = fold
->xor;
438 fnew
->rshift
= fold
->rshift
;
439 fnew
->addend
= fold
->addend
;
440 fnew
->divisor
= fold
->divisor
;
441 fnew
->baseclass
= fold
->baseclass
;
442 fnew
->hashrnd
= fold
->hashrnd
;
445 if (tb
[TCA_FLOW_MODE
])
446 mode
= nla_get_u32(tb
[TCA_FLOW_MODE
]);
447 if (mode
!= FLOW_MODE_HASH
&& nkeys
> 1)
450 if (mode
== FLOW_MODE_HASH
)
451 perturb_period
= fold
->perturb_period
;
452 if (tb
[TCA_FLOW_PERTURB
]) {
453 if (mode
!= FLOW_MODE_HASH
)
455 perturb_period
= nla_get_u32(tb
[TCA_FLOW_PERTURB
]) * HZ
;
461 if (!tb
[TCA_FLOW_KEYS
])
464 mode
= FLOW_MODE_MAP
;
465 if (tb
[TCA_FLOW_MODE
])
466 mode
= nla_get_u32(tb
[TCA_FLOW_MODE
]);
467 if (mode
!= FLOW_MODE_HASH
&& nkeys
> 1)
470 if (tb
[TCA_FLOW_PERTURB
]) {
471 if (mode
!= FLOW_MODE_HASH
)
473 perturb_period
= nla_get_u32(tb
[TCA_FLOW_PERTURB
]) * HZ
;
476 if (TC_H_MAJ(baseclass
) == 0)
477 baseclass
= TC_H_MAKE(tp
->q
->handle
, baseclass
);
478 if (TC_H_MIN(baseclass
) == 0)
479 baseclass
= TC_H_MAKE(baseclass
, 1);
481 fnew
->handle
= handle
;
484 get_random_bytes(&fnew
->hashrnd
, 4);
487 fnew
->perturb_timer
.function
= flow_perturbation
;
488 fnew
->perturb_timer
.data
= (unsigned long)fnew
;
489 init_timer_deferrable(&fnew
->perturb_timer
);
491 tcf_exts_change(tp
, &fnew
->exts
, &e
);
492 tcf_em_tree_change(tp
, &fnew
->ematches
, &t
);
494 netif_keep_dst(qdisc_dev(tp
->q
));
496 if (tb
[TCA_FLOW_KEYS
]) {
497 fnew
->keymask
= keymask
;
503 if (tb
[TCA_FLOW_MASK
])
504 fnew
->mask
= nla_get_u32(tb
[TCA_FLOW_MASK
]);
505 if (tb
[TCA_FLOW_XOR
])
506 fnew
->xor = nla_get_u32(tb
[TCA_FLOW_XOR
]);
507 if (tb
[TCA_FLOW_RSHIFT
])
508 fnew
->rshift
= nla_get_u32(tb
[TCA_FLOW_RSHIFT
]);
509 if (tb
[TCA_FLOW_ADDEND
])
510 fnew
->addend
= nla_get_u32(tb
[TCA_FLOW_ADDEND
]);
512 if (tb
[TCA_FLOW_DIVISOR
])
513 fnew
->divisor
= nla_get_u32(tb
[TCA_FLOW_DIVISOR
]);
515 fnew
->baseclass
= baseclass
;
517 fnew
->perturb_period
= perturb_period
;
519 mod_timer(&fnew
->perturb_timer
, jiffies
+ perturb_period
);
522 list_add_tail_rcu(&fnew
->list
, &head
->filters
);
524 list_replace_rcu(&fold
->list
, &fnew
->list
);
526 *arg
= (unsigned long)fnew
;
529 call_rcu(&fold
->rcu
, flow_destroy_filter
);
533 tcf_em_tree_destroy(&t
);
536 tcf_exts_destroy(&e
);
540 static int flow_delete(struct tcf_proto
*tp
, unsigned long arg
)
542 struct flow_filter
*f
= (struct flow_filter
*)arg
;
544 list_del_rcu(&f
->list
);
545 call_rcu(&f
->rcu
, flow_destroy_filter
);
549 static int flow_init(struct tcf_proto
*tp
)
551 struct flow_head
*head
;
553 head
= kzalloc(sizeof(*head
), GFP_KERNEL
);
556 INIT_LIST_HEAD(&head
->filters
);
557 rcu_assign_pointer(tp
->root
, head
);
561 static bool flow_destroy(struct tcf_proto
*tp
, bool force
)
563 struct flow_head
*head
= rtnl_dereference(tp
->root
);
564 struct flow_filter
*f
, *next
;
566 if (!force
&& !list_empty(&head
->filters
))
569 list_for_each_entry_safe(f
, next
, &head
->filters
, list
) {
570 list_del_rcu(&f
->list
);
571 call_rcu(&f
->rcu
, flow_destroy_filter
);
573 RCU_INIT_POINTER(tp
->root
, NULL
);
574 kfree_rcu(head
, rcu
);
578 static unsigned long flow_get(struct tcf_proto
*tp
, u32 handle
)
580 struct flow_head
*head
= rtnl_dereference(tp
->root
);
581 struct flow_filter
*f
;
583 list_for_each_entry(f
, &head
->filters
, list
)
584 if (f
->handle
== handle
)
585 return (unsigned long)f
;
589 static int flow_dump(struct net
*net
, struct tcf_proto
*tp
, unsigned long fh
,
590 struct sk_buff
*skb
, struct tcmsg
*t
)
592 struct flow_filter
*f
= (struct flow_filter
*)fh
;
598 t
->tcm_handle
= f
->handle
;
600 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
602 goto nla_put_failure
;
604 if (nla_put_u32(skb
, TCA_FLOW_KEYS
, f
->keymask
) ||
605 nla_put_u32(skb
, TCA_FLOW_MODE
, f
->mode
))
606 goto nla_put_failure
;
608 if (f
->mask
!= ~0 || f
->xor != 0) {
609 if (nla_put_u32(skb
, TCA_FLOW_MASK
, f
->mask
) ||
610 nla_put_u32(skb
, TCA_FLOW_XOR
, f
->xor))
611 goto nla_put_failure
;
614 nla_put_u32(skb
, TCA_FLOW_RSHIFT
, f
->rshift
))
615 goto nla_put_failure
;
617 nla_put_u32(skb
, TCA_FLOW_ADDEND
, f
->addend
))
618 goto nla_put_failure
;
621 nla_put_u32(skb
, TCA_FLOW_DIVISOR
, f
->divisor
))
622 goto nla_put_failure
;
624 nla_put_u32(skb
, TCA_FLOW_BASECLASS
, f
->baseclass
))
625 goto nla_put_failure
;
627 if (f
->perturb_period
&&
628 nla_put_u32(skb
, TCA_FLOW_PERTURB
, f
->perturb_period
/ HZ
))
629 goto nla_put_failure
;
631 if (tcf_exts_dump(skb
, &f
->exts
) < 0)
632 goto nla_put_failure
;
633 #ifdef CONFIG_NET_EMATCH
634 if (f
->ematches
.hdr
.nmatches
&&
635 tcf_em_tree_dump(skb
, &f
->ematches
, TCA_FLOW_EMATCHES
) < 0)
636 goto nla_put_failure
;
638 nla_nest_end(skb
, nest
);
640 if (tcf_exts_dump_stats(skb
, &f
->exts
) < 0)
641 goto nla_put_failure
;
646 nla_nest_cancel(skb
, nest
);
650 static void flow_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
652 struct flow_head
*head
= rtnl_dereference(tp
->root
);
653 struct flow_filter
*f
;
655 list_for_each_entry(f
, &head
->filters
, list
) {
656 if (arg
->count
< arg
->skip
)
658 if (arg
->fn(tp
, (unsigned long)f
, arg
) < 0) {
667 static struct tcf_proto_ops cls_flow_ops __read_mostly
= {
669 .classify
= flow_classify
,
671 .destroy
= flow_destroy
,
672 .change
= flow_change
,
673 .delete = flow_delete
,
677 .owner
= THIS_MODULE
,
680 static int __init
cls_flow_init(void)
682 return register_tcf_proto_ops(&cls_flow_ops
);
685 static void __exit
cls_flow_exit(void)
687 unregister_tcf_proto_ops(&cls_flow_ops
);
690 module_init(cls_flow_init
);
691 module_exit(cls_flow_exit
);
693 MODULE_LICENSE("GPL");
694 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
695 MODULE_DESCRIPTION("TC flow classifier");