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 struct can_filter *canprio_rules;
45 int canprio_rules_count = 0;
46 //--------------------------------------
48 /* Definition of Netlink messages */
53 TCA_CANPRIO_INV_EN, /* enable inverse rules */
56 #define TCA_CANPRIO_MAX (__TCA_CANPRIO_MAX - 1)
58 static const struct nla_policy canprio_policy[TCA_CANPRIO_MAX + 1] = {
59 [TCA_CANPRIO_CLASSID] = { .type = NLA_U32 },
60 //FIXME Be aware of possible problems with 64bit kernel and 32bit userspace etc.
61 [TCA_CANPRIO_RULES] = { .len = (sizeof(struct can_filter)) }, //FIXME
62 [TCA_CANPRIO_INV_EN] = { .type = NLA_U32 },
65 struct canprio_eff_item {
66 struct hlist_node list;
71 static DEFINE_SPINLOCK(canprio_match_eff_lock);
75 struct list_head flist;
78 struct canprio_filter {
80 // For each SFF Can ID (11 bit) there is one record in this bitfield
81 DECLARE_BITMAP(match_sff, CAN_SFF_MASK + 1);
82 int inv_match_en; // Inverted match flag
83 struct hlist_head match_eff; // List of EFF frames to match
84 struct tcf_result res;
85 struct list_head link;
89 * ----------------------------------------------------------------------------
92 static void canprio_sff_match_add(struct canprio_filter *f, u32 canid, u32 mask)
96 printk("%s() invoked\n", __FUNCTION__);
100 if (mask == CAN_SFF_MASK) {
101 set_bit(canid, f->match_sff);
105 for (i = 0; i <= CAN_SFF_MASK; i++) {
106 if ((i & mask) == canid)
107 set_bit(i, f->match_sff);
111 static int canprio_eff_match_add(struct canprio_filter *f, u32 canid, u32 mask)
113 struct canprio_eff_item *eff;
116 printk("%s() invoked\n", __FUNCTION__);
117 mask &= CAN_EFF_MASK;
118 canid = canid & mask;
120 eff = kmalloc(sizeof(struct canprio_eff_item), GFP_KERNEL);
124 spin_lock(&canprio_match_eff_lock);
129 hlist_add_head_rcu(&eff->list, &f->match_eff);
131 spin_unlock(&canprio_match_eff_lock);
137 * Extracts Can ID ot ouf the sk_buff structure.
139 static u32 canprio_get_id(struct sk_buff *skb)
141 /* Can ID is inside of data field */
142 struct can_frame *cf = (struct can_frame *)skb->data;
143 //canid_t canid = cf->canid;
145 return (u32)cf->can_id;
149 * Performs the classification. Iterates over all instances of filter
150 * checking for Can ID match.
152 * @skb: Socket buffer
154 * @res: Is used for setting Class ID as a result of classification
156 * Returns value relevant for policing. Used return values:
157 * TC_POLICE_OK if succesfully classified (without regard to policing rules)
158 * TC_POLICE_UNSPEC if no matching filter was found
160 static int canprio_classify(struct sk_buff *skb, struct tcf_proto *tp,
161 struct tcf_result *res)
163 struct canprio_head *head = (struct canprio_head *)tp->root;
164 struct canprio_filter *f;
167 printk(" canprio_classify() invoked\n");
168 canid = canprio_get_id(skb);
170 list_for_each_entry(f, &head->flist, link) {
172 printk(" canprio_classify() can ID of received frame = 0x%x\n", canid);
174 if (canid & CAN_EFF_FLAG) {
175 struct canprio_eff_item *effi;
176 struct hlist_node *next;
180 hlist_for_each_entry_rcu(effi, next, &f->match_eff, list) {
181 if ((effi->canid ^ canid) & effi->mask) {
189 match = test_bit(canid, f->match_sff);
198 printk( " canprio_classify() match ok: ID 0x%x\n", canid);
202 return TC_POLICE_UNSPEC;
208 static int canprio_set_parms(struct tcf_proto *tp, struct canprio_filter *f,
209 unsigned long base, struct nlattr **tb,
213 printk(" canprio_set_parms invoked\n");
215 if (tb[TCA_CANPRIO_CLASSID]) {
216 f->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
217 bitmap_zero(f->match_sff, CAN_SFF_MASK + 1);
218 INIT_HLIST_HEAD(&f->match_eff);
220 tcf_bind_filter(tp, &f->res, base);
227 * Looks up a filter element by its handle and returns the internal
228 * filter ID (i.e. pointer)
230 static unsigned long canprio_get(struct tcf_proto *tp, u32 handle)
232 struct canprio_head *head = (struct canprio_head *)tp->root;
233 struct canprio_filter *f;
235 //printk("canprio_get(%d) invoked\n", handle);
239 //printk("[running for_each_entry]\n");
240 list_for_each_entry(f, &head->flist, link) {
241 //printk("[f->handle = %d]\n", f->handle);
242 if (f->handle == handle) {
243 //printk("found something\n");
244 return (unsigned long) f;
252 * Is invoked when a filter element previously referenced
253 * with get() is no longer used
255 static void canprio_put(struct tcf_proto *tp, unsigned long f)
260 static unsigned int canprio_gen_handle(struct tcf_proto *tp)
262 struct canprio_head *head = (struct canprio_head *)tp->root;
269 if ((head->hgenerator += 0x10000) == 0)
270 head->hgenerator = 0x10000;
272 h = head->hgenerator;
273 //if (canprio_get(tp, h) == 0)
275 tmp = canprio_get(tp, h);
276 //printk("___tried %d result %lu\n", h, tmp);
285 * Called for changing properties of an existing filter or after addition
286 * of a new filter to a class (by calling bind_tcf which binds an instance
287 * of a filter to the class).
289 * @tp: Structure representing instance of a filter.
290 * Part of a linked list of all filters.
293 * @tca: Messages passed through the Netlink from userspace.
296 static int canprio_change(struct tcf_proto *tp, unsigned long base, u32 handle,
297 struct nlattr **tca, unsigned long *arg)
300 struct canprio_head *head = (struct canprio_head *)tp->root;
301 struct canprio_filter *f = (struct canprio_filter *)*arg;
302 struct nlattr *tb[TCA_CANPRIO_MAX + 1];
305 printk(" canprio_change invoked\n");
307 if (tca[TCA_OPTIONS] == NULL)
310 /*Parses a stream of attributes and stores a pointer to each attribute in
311 the tb array accessible via the attribute type. Policy may be set to NULL
312 if no validation is required.*/
313 err = nla_parse_nested(tb, TCA_CANPRIO_MAX, tca[TCA_OPTIONS], canprio_policy);
317 //Change existing filter
319 if (handle && f->handle != handle)
321 return canprio_set_parms(tp, f, base, tb, tca[TCA_RATE]);
326 f = kzalloc(sizeof(*f), GFP_KERNEL);
337 unsigned int i = 0x80000000;
339 if (++head->hgenerator == 0x7FFFFFFF)
340 head->hgenerator = 1;
341 } while (--i > 0 && canprio_get(tp, head->hgenerator));
344 pr_err("Insufficient number of handles\n");
348 handle = canprio_gen_handle(tp);
349 //printk("__new handle %d\n", handle);
351 //FIXME where is hgenerator initialized
355 //Configure newly created filter
356 err = canprio_set_parms(tp, f, base, tb, tca[TCA_RATE]);
362 if (tb[TCA_CANPRIO_RULES] == NULL)
365 canprio_rules = nla_data(tb[TCA_CANPRIO_RULES]);
366 canprio_rules_count = (nla_len(tb[TCA_CANPRIO_RULES]) / sizeof(struct can_filter));
367 printk(" rules_count = %u\n", canprio_rules_count);
369 for (i = 0; i < canprio_rules_count; i++) {
370 /* FIXME: shouldn't use here the same logic as in
371 * can_rcv_filter() to filter for various combination
372 * of flags (EFF, RTR) */
373 if (canprio_rules[i].can_id & CAN_EFF_FLAG) {
374 err = canprio_eff_match_add(f, canprio_rules[i].can_id, canprio_rules[i].can_mask);
378 canprio_sff_match_add(f, canprio_rules[i].can_id, canprio_rules[i].can_mask);
381 printk(" can ID to match = 0x%x with mask 0x%x\n",
382 canprio_rules[i].can_id, canprio_rules[i].can_mask);
386 if (tb[TCA_CANPRIO_INV_EN] != NULL)
387 f->inv_match_en = nla_get_u32(tb[TCA_CANPRIO_INV_EN]);
389 //Add newly created filter to list of all filters
391 list_add(&f->link, &head->flist);
393 *arg = (unsigned long) f;
398 if (*arg == 0UL && f)
405 static void canprio_delete_filter(struct tcf_proto *tp, struct canprio_filter *f)
407 struct canprio_eff_item *effi;
408 struct hlist_node *p, *n;
412 tcf_unbind_filter(tp, &f->res);
413 hlist_for_each_entry_safe(effi, p, n, &f->match_eff, list) {
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("tp = %p\n", tp);
466 printk(" canprio_init invoked\n");
468 head = kzalloc(sizeof(*head), GFP_KERNEL);
472 INIT_LIST_HEAD(&head->flist);
474 tp->protocol = htons(ETH_P_CAN); /* Work only on AF_CAN packets - not tested! */
480 * Iterates over all elements of a filter and invokes a callback function
481 * for each of them. This is used to obtain diagnostic data
483 static void canprio_walk(struct tcf_proto *tp, struct tcf_walker *arg)
485 struct canprio_head *head = (struct canprio_head *) tp->root;
486 struct canprio_filter *f;
488 printk("%s() invoked\n", __FUNCTION__);
490 list_for_each_entry(f, &head->flist, link) {
491 if (arg->count < arg->skip)
494 printk("calling canprio_dump()\n");
495 if (arg->fn(tp, (unsigned long) f, arg) < 0) {
505 * Returns diagnostic data for a filter or one of its elements.
507 static int canprio_dump(struct tcf_proto *tp, unsigned long fh,
508 struct sk_buff *skb, struct tcmsg *t)
510 struct canprio_filter *f = (struct canprio_filter *) fh;
513 printk("%s() invoked\n", __FUNCTION__);
518 t->tcm_handle = f->handle;
520 nest = nla_nest_start(skb, TCA_OPTIONS);
522 goto nla_put_failure;
525 NLA_PUT_U32(skb, TCA_CANPRIO_CLASSID, f->res.classid);
528 //NLA_PUT(skb, TCA_CANPRIO_RULES, canprio_rules_count * sizeof(struct canprio_rule),
534 nla_nest_end(skb, nest);
539 nla_nest_cancel(skb, nest);
544 static struct tcf_proto_ops cls_canprio_ops __read_mostly = {
546 .classify = canprio_classify,
547 .init = canprio_init,
548 .destroy = canprio_destroy,
551 .change = canprio_change,
552 .delete = canprio_delete,
553 .walk = canprio_walk,
554 .dump = canprio_dump,
555 .owner = THIS_MODULE,
558 static int __init init_canprio(void)
560 printk("Canprio loaded\n");
561 return register_tcf_proto_ops(&cls_canprio_ops);
564 static void __exit exit_canprio(void)
566 printk("Canprio removed\n");
567 unregister_tcf_proto_ops(&cls_canprio_ops);
570 module_init(init_canprio);
571 module_exit(exit_canprio);
572 MODULE_LICENSE("GPL");
573 MODULE_AUTHOR(""); // FIXME
574 MODULE_DESCRIPTION("Controller Area Network can_id classifier");