1 /* cls_canprio.c -- Canprio classifier.
2 * Makes decisions accoring to Can IDs.
4 * Implementation notes;
5 * parameter of functions named "base" is pointer to some parent element
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/errno.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/skbuff.h>
17 #include <net/netlink.h>
18 #include <net/act_api.h>
19 #include <net/pkt_cls.h>
20 #include <linux/can.h>
21 #include <linux/bitmap.h>
22 #include <linux/spinlock.h>
23 #include <linux/rcupdate.h>
25 //--------------------------------------
26 //FIXME put in some *.h file shared with userspace "tc"?
32 struct canprio_rule *canprio_rules;
33 int canprio_rules_count = 0;
34 //--------------------------------------
36 /* Definition of Netlink messages */
41 TCA_CANPRIO_INV_EN, /* enable inverse rules */
44 #define TCA_CANPRIO_MAX (__TCA_CANPRIO_MAX - 1)
46 static const struct nla_policy canprio_policy[TCA_CANPRIO_MAX + 1] = {
47 [TCA_CANPRIO_CLASSID] = { .type = NLA_U32 },
48 //FIXME Be aware of possible problems with 64bit kernel and 32bit userspace etc.
49 [TCA_CANPRIO_RULES] = { .len = sizeof(canprio_rules) },
50 [TCA_CANPRIO_INV_EN] = { .type = NLA_U32 },
53 struct canprio_eff_item {
54 struct hlist_node list;
59 static DEFINE_SPINLOCK(canprio_match_eff_lock);
63 struct list_head flist;
66 struct canprio_filter {
68 // For each SFF Can ID (11 bit) there is one record in this bitfield
69 DECLARE_BITMAP(match_sff, CAN_SFF_MASK + 1);
71 struct hlist_head match_eff;
72 struct tcf_result res;
73 struct list_head link;
77 * ----------------------------------------------------------------------------
80 static void sff_match_add(struct canprio_filter *f, u32 canid, u32 mask)
87 if (mask == CAN_SFF_MASK) {
88 set_bit(canid, f->match_sff);
92 for (i = 0; i <= CAN_SFF_MASK; i++) {
93 if ((i & mask) == canid)
94 set_bit(i, f->match_sff);
98 static int eff_match_add(struct canprio_filter *f, u32 canid, u32 mask)
100 struct canprio_eff_item *eff;
103 mask &= CAN_EFF_MASK;
104 canid = canid & mask;
106 eff = kmalloc(sizeof(struct canprio_eff_item), GFP_KERNEL);
110 spin_lock(&canprio_match_eff_lock);
115 hlist_add_head_rcu(&eff->list, &f->match_eff);
117 spin_unlock(&canprio_match_eff_lock);
123 * Extracts Can ID ot ouf the sk_buff structure.
125 static u32 canprio_get_id(struct sk_buff *skb)
127 /* Can ID is inside of data field */
128 struct can_frame *cf = (struct can_frame *)skb->data;
129 //canid_t canid = cf->canid;
131 return (u32)cf->can_id;
135 * Performs the classification. Iterates over all instances of filter
136 * checking for Can ID match.
138 * @skb: Socket buffer
140 * @res: Is used for setting Class ID as a result of classification
142 * Returns value relevant for policing. Used return values:
143 * TC_POLICE_OK if succesfully classified (without regard to policing rules)
144 * TC_POLICE_UNSPEC if no matching filter was found
146 static int canprio_classify(struct sk_buff *skb, struct tcf_proto *tp,
147 struct tcf_result *res)
149 struct canprio_head *head = (struct canprio_head *)tp->root;
150 struct canprio_filter *f;
153 printk(" canprio_classify() invoked\n");
154 canid = canprio_get_id(skb);
156 list_for_each_entry(f, &head->flist, link) {
158 printk(" canprio_classify() can ID of received frame = 0x%x\n", canid);
160 if (canid & CAN_EFF_FLAG) {
161 struct canprio_eff_item *effi;
162 struct hlist_node *next;
166 hlist_for_each_entry_rcu(effi, next, &f->match_eff, list) {
167 if((effi->canid ^ canid) & effi->mask) {
175 if(test_bit(canid, f->match_sff)) {
186 printk( " canprio_classify() match ok: ID 0x%x\n", canid);
190 return TC_POLICE_UNSPEC;
196 static int canprio_set_parms(struct tcf_proto *tp, struct canprio_filter *f,
197 unsigned long base, struct nlattr **tb,
201 printk(" canprio_set_parms invoked\n");
203 if (tb[TCA_CANPRIO_CLASSID]) {
204 f->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
205 bitmap_zero(f->match_sff, CAN_SFF_MASK + 1);
206 INIT_HLIST_HEAD(&f->match_eff);
208 tcf_bind_filter(tp, &f->res, base);
215 * Looks up a filter element by its handle and returns the internal filter ID
217 static unsigned long canprio_get(struct tcf_proto *tp, u32 handle)
219 unsigned long l = 0UL;
220 struct canprio_head *head = (struct canprio_head *) tp->root;
221 struct canprio_filter *f;
226 list_for_each_entry(f, &head->flist, link)
227 if (f->handle == handle)
228 l = (unsigned long) f;
234 * Is invoked when a filter element previously referenced
235 * with get() is no longer used
237 static void canprio_put(struct tcf_proto *tp, unsigned long f)
243 * Called for changing properties of an existing filter or after addition
244 * of a new filter to a class (by calling bind_tcf which binds an instance
245 * of a filter to the class).
247 * @tp: Structure representing instance of a filter.
248 * Part of a linked list of all filters.
251 * @tca: Messages passed through the Netlink from userspace.
254 static int canprio_change(struct tcf_proto *tp, unsigned long base, u32 handle,
255 struct nlattr **tca, unsigned long *arg)
258 struct canprio_head *head = (struct canprio_head *)tp->root;
259 struct canprio_filter *f = (struct canprio_filter *)*arg;
260 struct nlattr *tb[TCA_CANPRIO_MAX + 1];
263 printk(" canprio_change invoked\n");
265 if (tca[TCA_OPTIONS] == NULL)
268 //Parses a stream of attributes and stores a pointer to each attribute in
269 //the tb array accessible via the attribute type. Policy may be set to NULL
270 //if no validation is required.
271 err = nla_parse_nested(tb, TCA_CANPRIO_MAX, tca[TCA_OPTIONS], canprio_policy);
275 //Change existing filter (?)
277 if (handle && f->handle != handle)
279 return canprio_set_parms(tp, f, base, tb, tca[TCA_RATE]);
282 //Create new filter (?)
284 f = kzalloc(sizeof(*f), GFP_KERNEL);
295 unsigned int i = 0x80000000;
297 if (++head->hgenerator == 0x7FFFFFFF)
298 head->hgenerator = 1;
299 } while (--i > 0 && canprio_get(tp, head->hgenerator));
302 pr_err("Insufficient number of handles\n");
306 f->handle = head->hgenerator;
310 //Configure newly created filter
311 err = canprio_set_parms(tp, f, base, tb, tca[TCA_RATE]);
317 if (tb[TCA_CANPRIO_RULES] == NULL)
320 canprio_rules = nla_data(tb[TCA_CANPRIO_RULES]);
321 canprio_rules_count = (nla_len(tb[TCA_CANPRIO_RULES]) / sizeof(struct canprio_rule));
322 printk(" rules_count = %u\n", canprio_rules_count);
324 for (i = 0; i < canprio_rules_count; i++) {
325 if (canprio_rules[i].canid & CAN_EFF_FLAG) {
326 err = eff_match_add(f, canprio_rules[i].canid, canprio_rules[i].canid_mask);
330 sff_match_add(f, canprio_rules[i].canid, canprio_rules[i].canid_mask);
333 printk(" can ID to match = 0x%x with mask 0x%x\n",
334 canprio_rules[i].canid, canprio_rules[i].canid_mask);
338 if (tb[TCA_CANPRIO_INV_EN] != NULL)
339 f->inv_match_en = nla_get_u32(tb[TCA_CANPRIO_INV_EN]);
341 //Add newly created filter to list of all filters
343 list_add(&f->link, &head->flist);
345 *arg = (unsigned long) f;
350 if (*arg == 0UL && f) //FIXME why 0UL?
357 static void canprio_delete_filter(struct tcf_proto *tp, struct canprio_filter *f)
359 struct canprio_eff_item *effi;
360 struct hlist_node *p, *n;
364 tcf_unbind_filter(tp, &f->res);
365 hlist_for_each_entry_safe(effi, p, n, &f->match_eff, list) {
373 * Remove whole filter.
375 static void canprio_destroy(struct tcf_proto *tp)
377 struct canprio_head *head = tp->root;
378 struct canprio_filter *f, *n;
380 list_for_each_entry_safe(f, n, &head->flist, link) {
382 canprio_delete_filter(tp, f);
388 * Delete one instance of a filter.
390 static int canprio_delete(struct tcf_proto *tp, unsigned long arg)
392 struct canprio_head *head = (struct canprio_head *)tp->root;
393 struct canprio_filter *t;
394 struct canprio_filter *f = (struct canprio_filter *)arg;
396 rcu_barrier(); /* Wait for completion of call_rcu()'s */
398 list_for_each_entry(t, &head->flist, link)
403 canprio_delete_filter(tp, t);
410 //FIXME copied from cls_basic, not sure if ok;
411 static int canprio_init(struct tcf_proto *tp)
413 struct canprio_head *head;
415 printk(" canprio_init invoked\n");
417 head = kzalloc(sizeof(*head), GFP_KERNEL);
420 INIT_LIST_HEAD(&head->flist);
427 static struct tcf_proto_ops cls_canprio_ops __read_mostly = {
429 .classify = canprio_classify,
430 .init = canprio_init,
431 .destroy = canprio_destroy,
434 .change = canprio_change,
435 .delete = canprio_delete,
436 .owner = THIS_MODULE,
439 static int __init init_canprio(void)
441 printk("Canprio loaded\n");
442 return register_tcf_proto_ops(&cls_canprio_ops);
445 static void __exit exit_canprio(void)
447 printk("Canprio removed\n");
448 unregister_tcf_proto_ops(&cls_canprio_ops);
451 module_init(init_canprio);
452 module_exit(exit_canprio);
453 MODULE_LICENSE("GPL");