2 * cls_canprio.c -- Canprio classifier.
3 * Makes decisions accoring to controller area network (CAN) identifiers.
5 * This program is free software; you can distribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; version 2 of
10 * Idea: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
11 * Copyright: (c) 2011 Czech Technical University in Prague
12 * (c) 2011 Volkswagen Group Research
13 * Authors: Michal Sojka <sojkam1@fel.cvut.cz>
14 * Pavel Pisa <pisa@cmp.felk.cvut.cz>
15 * Rostislav Lisovy <lisovy@kormus.cz>
16 * Founded by: Volkswagen Group Research
18 * Some function descriptions are heavily inspired by article "Linux Network
19 * Traffic Control -- Implementation Overview" by Werner Almesberger
22 * Implementation notes;
23 * parameter of functions named "base" is pointer to some parent element
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/string.h>
31 #include <linux/errno.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/skbuff.h>
34 #include <net/netlink.h>
35 #include <net/act_api.h>
36 #include <net/pkt_cls.h>
37 #include <linux/can.h>
38 #include <linux/bitmap.h>
39 #include <linux/spinlock.h>
40 #include <linux/rcupdate.h>
41 #include <linux/can.h>
44 /* Definition of Netlink messages */
48 TCA_CANPRIO_RULES, /* Array of can_filter structs; We are able
49 to determine the length after receiving */
50 TCA_CANPRIO_INV_EN, /* enable inverse rules */
53 #define TCA_CANPRIO_MAX (__TCA_CANPRIO_MAX - 1)
55 static const struct nla_policy canprio_policy[TCA_CANPRIO_MAX + 1] = {
56 [TCA_CANPRIO_CLASSID] = { .type = NLA_U32 },
57 //FIXME Be aware of possible problems with 64bit kernel and 32bit userspace etc.
58 [TCA_CANPRIO_RULES] = { /*.len = (sizeof(struct can_filter))*/ }, //FIXME
59 [TCA_CANPRIO_INV_EN] = { .type = NLA_U32 },
62 struct canprio_rules {
63 struct can_filter *rules_raw; /* Raw rules copied from netlink message;
64 Used for sending information to userspace
65 (when 'tc filter show' is invoked) AND
66 when matching EFF frames*/
67 DECLARE_BITMAP(match_sff, CAN_SFF_MASK + 1); /* For each SFF Can ID (11 bit)
68 there is one record in this bitfield */
69 int inv_match_en; /* Inverted match flag */
77 struct list_head flist;
80 struct canprio_filter {
82 struct canprio_rules *rules; /* All rules necessary for classification */
83 struct tcf_result res; /* Class ID (flow id) the instance
84 of a filter is bound to */
85 struct list_head link;
89 * ----------------------------------------------------------------------------
92 static void canprio_sff_match_add(struct canprio_rules *rls, u32 canid, u32 mask)
96 printk("%s() invoked\n", __FUNCTION__);
100 if (mask == CAN_SFF_MASK) {
101 set_bit(canid, rls->match_sff);
104 /* Add record (set bit to 1) for each ID that conforms particular rule */
105 for (i = 0; i <= CAN_SFF_MASK; i++) {
106 if ((i & mask) == canid)
107 set_bit(i, rls->match_sff);
112 * Extracts Can ID ot ouf the sk_buff structure.
114 static canid_t canprio_get_id(struct sk_buff *skb)
116 /* Can ID is inside of data field */
117 struct can_frame *cf = (struct can_frame *)skb->data;
123 * Performs the classification. Iterates over all instances of filter
124 * checking for Can ID match.
126 * @skb: Socket buffer
128 * @res: Is used for setting Class ID as a result of classification
130 * Returns value relevant for policing. Used return values:
131 * TC_POLICE_OK if succesfully classified (without regard to policing rules)
132 * TC_POLICE_UNSPEC if no matching filter was found
134 static int canprio_classify(struct sk_buff *skb, struct tcf_proto *tp,
135 struct tcf_result *res)
137 struct canprio_head *head = (struct canprio_head *)tp->root;
138 struct canprio_filter *f;
142 printk(" canprio_classify() invoked\n");
143 canid = (u32)canprio_get_id(skb);
145 list_for_each_entry(f, &head->flist, link) {
148 if (canid & CAN_EFF_FLAG) {
149 canid &= CAN_EFF_MASK;
151 for (i = 0; i < f->rules->eff_rules_count; i++) {
152 if ((f->rules->rules_raw[i].can_id & f->rules->rules_raw[i].can_mask & CAN_EFF_MASK) ==
153 (canid & f->rules->rules_raw[i].can_mask & CAN_EFF_MASK)) {
160 match = test_bit(canid, f->rules->match_sff);
163 //if (f->rules->inv_match_en)
168 printk( " canprio_classify() match ok: ID 0x%x\n", canid);
173 return TC_POLICE_UNSPEC;
177 * Looks up a filter element by its handle and returns the internal
178 * filter ID (i.e. pointer)
180 static unsigned long canprio_get(struct tcf_proto *tp, u32 handle)
182 struct canprio_head *head = (struct canprio_head *)tp->root;
183 struct canprio_filter *f;
185 //printk("canprio_get(%d) invoked\n", handle);
189 //printk("[running for_each_entry]\n");
190 list_for_each_entry(f, &head->flist, link) {
191 //printk("[f->handle = %d]\n", f->handle);
192 if (f->handle == handle) {
193 //printk("found something\n");
194 return (unsigned long) f;
202 * Is invoked when a filter element previously referenced
203 * with get() is no longer used
205 static void canprio_put(struct tcf_proto *tp, unsigned long f)
210 static unsigned int canprio_gen_handle(struct tcf_proto *tp)
212 struct canprio_head *head = (struct canprio_head *)tp->root;
219 if ((head->hgenerator += 0x10000) == 0)
220 head->hgenerator = 0x10000;
222 h = head->hgenerator;
223 //if (canprio_get(tp, h) == 0)
225 tmp = canprio_get(tp, h);
226 //printk("___tried %d result %lu\n", h, tmp);
234 static int canprio_set_parms(struct tcf_proto *tp, struct canprio_filter *f,
235 unsigned long base, struct nlattr **tb,
238 struct can_filter *canprio_nl_rules;
239 struct canprio_rules *rules_tmp;
242 printk("%s() invoked\n", __FUNCTION__);
244 rules_tmp = kzalloc(sizeof(*rules_tmp), GFP_KERNEL);
245 if (rules_tmp == NULL)
248 bitmap_zero(rules_tmp->match_sff, CAN_SFF_MASK + 1);
249 rules_tmp->inv_match_en = 0;
252 if (tb[TCA_CANPRIO_CLASSID] == NULL) //FIXME is enough?
255 if (tb[TCA_CANPRIO_RULES]) {
256 canprio_nl_rules = nla_data(tb[TCA_CANPRIO_RULES]);
257 rules_tmp->sff_rules_count = 0;
258 rules_tmp->eff_rules_count = 0;
259 rules_tmp->rules_count = (nla_len(tb[TCA_CANPRIO_RULES]) / sizeof(struct can_filter));
260 printk(" rules_count = %u\n", rules_tmp->rules_count);
262 rules_tmp->rules_raw = kzalloc(sizeof(struct can_filter) * rules_tmp->rules_count, GFP_KERNEL);
263 if (rules_tmp->rules_raw == NULL)
266 /* Process EFF frames */
267 for (i = 0; i < rules_tmp->rules_count; i++) {
268 if ((canprio_nl_rules[i].can_id & CAN_EFF_FLAG) &&
269 (canprio_nl_rules[i].can_mask & CAN_EFF_FLAG)) {
271 memcpy(rules_tmp->rules_raw + rules_tmp->eff_rules_count,
272 &canprio_nl_rules[i], sizeof(struct can_filter));
273 rules_tmp->eff_rules_count ++;
275 printk(" can ID to match = 0x%x with mask 0x%x\n",
276 canprio_nl_rules[i].can_id, canprio_nl_rules[i].can_mask);
282 /* Process SFF frames
283 We need two for() loops for copying rules into two contiguous areas in rules_raw */
284 for (i = 0; i < rules_tmp->rules_count; i++) {
285 if ((canprio_nl_rules[i].can_id & CAN_EFF_FLAG) &&
286 (canprio_nl_rules[i].can_mask & CAN_EFF_FLAG)) {
290 memcpy(rules_tmp->rules_raw + rules_tmp->eff_rules_count + rules_tmp->sff_rules_count,
291 &canprio_nl_rules[i], sizeof(struct can_filter));
292 rules_tmp->sff_rules_count ++;
294 canprio_sff_match_add(rules_tmp, canprio_nl_rules[i].can_id,
295 canprio_nl_rules[i].can_mask);
297 printk(" can ID to match = 0x%x with mask 0x%x\n",
298 canprio_nl_rules[i].can_id, canprio_nl_rules[i].can_mask);
304 if (tb[TCA_CANPRIO_INV_EN] != NULL)
305 rules_tmp->inv_match_en = nla_get_u32(tb[TCA_CANPRIO_INV_EN]);
307 if (f->rules == NULL) { // Setting parameters for newly created filter
308 f->rules = rules_tmp;
309 } else { // Changing existing filter
310 struct canprio_rules *rules_old;
312 rules_old = f->rules;
313 f->rules = rules_tmp;
320 if (rules_tmp->rules_raw != NULL) //FIXME is ok?
321 kfree(rules_tmp->rules_raw);
327 * Called for changing properties of an existing filter or after addition
328 * of a new filter to a class (by calling bind_tcf which binds an instance
329 * of a filter to the class).
331 * @tp: Structure representing instance of a filter.
332 * Part of a linked list of all filters.
335 * @tca: Messages passed through the Netlink from userspace.
338 static int canprio_change(struct tcf_proto *tp, unsigned long base, u32 handle,
339 struct nlattr **tca, unsigned long *arg)
341 struct canprio_head *head = (struct canprio_head *)tp->root;
342 struct canprio_filter *f = (struct canprio_filter *)*arg;
343 struct nlattr *tb[TCA_CANPRIO_MAX + 1];
346 printk("%s() invoked\n", __FUNCTION__);
348 if (tca[TCA_OPTIONS] == NULL)
351 /* Parses a stream of attributes and stores a pointer to each attribute in
352 the tb array accessible via the attribute type. Policy may be set to NULL
353 if no validation is required.*/
354 err = nla_parse_nested(tb, TCA_CANPRIO_MAX, tca[TCA_OPTIONS], canprio_policy);
357 /* Change existing filter (remove all settings and add
358 them thereafter as if filter was newly created) */
360 if (handle && f->handle != handle)
363 printk("[change existing filter]\n");
364 return canprio_set_parms(tp, f, base, tb, tca[TCA_RATE]);
367 /* Create new filter */
369 f = kzalloc(sizeof(*f), GFP_KERNEL);
374 if (tb[TCA_CANPRIO_CLASSID]) {
375 f->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
376 tcf_bind_filter(tp, &f->res, base);
381 if (handle) /* handle passed from userspace */
385 //handle = canprio_gen_handle(tp);
386 //f->handle = handle;
391 err = canprio_set_parms(tp, f, base, tb, tca[TCA_RATE]);
395 //Add newly created filter to list of all filters
397 list_add(&f->link, &head->flist);
399 *arg = (unsigned long) f;
405 if (*arg == 0UL && f)
412 static void canprio_delete_filter(struct tcf_proto *tp, struct canprio_filter *f)
414 tcf_unbind_filter(tp, &f->res);
415 kfree(f->rules->rules_raw);
421 * Remove whole filter.
423 static void canprio_destroy(struct tcf_proto *tp)
425 struct canprio_head *head = tp->root;
426 struct canprio_filter *f, *n;
428 list_for_each_entry_safe(f, n, &head->flist, link) {
430 canprio_delete_filter(tp, f);
436 * Delete one instance of a filter.
438 static int canprio_delete(struct tcf_proto *tp, unsigned long arg)
440 struct canprio_head *head = (struct canprio_head *)tp->root;
441 struct canprio_filter *t;
442 struct canprio_filter *f = (struct canprio_filter *)arg;
444 rcu_barrier(); /* Wait for completion of call_rcu()'s */
446 list_for_each_entry(t, &head->flist, link)
451 canprio_delete_filter(tp, t);
462 static int canprio_init(struct tcf_proto *tp)
464 struct canprio_head *head;
465 printk(" canprio_init invoked\n");
467 head = kzalloc(sizeof(*head), GFP_KERNEL);
471 INIT_LIST_HEAD(&head->flist);
473 tp->protocol = htons(ETH_P_CAN); /* Work only on AF_CAN packets - not tested! */
479 * Iterates over all elements of a filter and invokes a callback function
480 * for each of them. This is used to obtain diagnostic data
482 static void canprio_walk(struct tcf_proto *tp, struct tcf_walker *arg)
484 struct canprio_head *head = (struct canprio_head *) tp->root;
485 struct canprio_filter *f;
487 printk("%s() invoked\n", __FUNCTION__);
489 list_for_each_entry(f, &head->flist, link) {
490 if (arg->count < arg->skip)
493 if (arg->fn(tp, (unsigned long) f, arg) < 0) {
503 * Returns diagnostic data for a filter or one of its elements.
505 static int canprio_dump(struct tcf_proto *tp, unsigned long fh,
506 struct sk_buff *skb, struct tcmsg *t)
508 struct canprio_filter *f = (struct canprio_filter *) fh;
510 printk("%s() invoked\n", __FUNCTION__);
515 t->tcm_handle = f->handle;
517 nest = nla_nest_start(skb, TCA_OPTIONS);
519 goto nla_put_failure;
522 NLA_PUT_U32(skb, TCA_CANPRIO_CLASSID, f->res.classid);
524 NLA_PUT(skb, TCA_CANPRIO_RULES, f->rules->rules_count *
525 sizeof(struct can_filter), f->rules->rules_raw);
528 nla_nest_end(skb, nest);
533 nla_nest_cancel(skb, nest);
538 static struct tcf_proto_ops cls_canprio_ops __read_mostly = {
540 .classify = canprio_classify,
541 .init = canprio_init,
542 .destroy = canprio_destroy,
545 .change = canprio_change,
546 .delete = canprio_delete,
547 .walk = canprio_walk,
548 .dump = canprio_dump,
549 .owner = THIS_MODULE,
552 static int __init init_canprio(void)
554 printk("Canprio loaded\n");
555 return register_tcf_proto_ops(&cls_canprio_ops);
558 static void __exit exit_canprio(void)
560 printk("Canprio removed\n");
561 unregister_tcf_proto_ops(&cls_canprio_ops);
564 module_init(init_canprio);
565 module_exit(exit_canprio);
566 MODULE_LICENSE("GPL");
567 MODULE_AUTHOR(""); // FIXME
568 MODULE_DESCRIPTION("Controller Area Network can_id classifier");