2 * cls_can.c -- Controller Area Network classifier.
3 * Makes decisions according to Controller Area Network identifiers (can_id).
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@gmail.cz>
16 * Funded by: Volkswagen Group Research
18 * Some function descriptions are heavily inspired by article "Linux Network
19 * Traffic Control -- Implementation Overview" by Werner Almesberger
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/errno.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/skbuff.h>
30 #include <net/netlink.h>
31 #include <net/act_api.h>
32 #include <net/pkt_cls.h>
33 #include <linux/bitmap.h>
34 #include <linux/spinlock.h>
35 #include <linux/rcupdate.h>
36 #include <linux/can.h>
38 /* Definition of Netlink message parts */
42 TCA_CANFLTR_RULES, /* Array of can_filter structs; We are able
43 to determine the length after receiving */
46 #define TCA_CANFLTR_MAX (__TCA_CANFLTR_MAX - 1)
48 static const struct nla_policy canfltr_policy[TCA_CANFLTR_MAX + 1] = {
49 [TCA_CANFLTR_CLASSID] = { .type = NLA_U32 }, /* Be aware of possible
50 problems with 64bit kernel and
51 32bit userspace etc. */
52 [TCA_CANFLTR_RULES] = { .type = NLA_NESTED }
55 struct canfltr_rules {
56 struct can_filter *rules_raw; /* Raw rules copied from netlink
57 message; Used for sending information
58 to userspace (when 'tc filter show' is
59 invoked) AND when matching EFF frames*/
60 DECLARE_BITMAP(match_sff, (1 << CAN_SFF_ID_BITS)); /* For each SFF CAN
61 ID (11 bit) there is one record in this
72 struct list_head flist;
75 struct canfltr_state {
77 struct canfltr_rules *rules; /* All rules necessary for
79 struct tcf_result res; /* Class ID (flow id) the instance
80 of a filter is bound to */
81 struct list_head link;
85 * ----------------------------------------------------------------------------
88 static void canfltr_sff_match_add(struct canfltr_rules *rls,
89 u32 can_id, u32 can_mask)
93 /* Limit can_mask and can_id to SFF range to
94 protect against write after end of array */
95 can_mask &= CAN_SFF_MASK;
99 if (can_mask == CAN_SFF_MASK) {
100 set_bit(can_id, rls->match_sff);
106 bitmap_fill(rls->match_sff, (1 << CAN_SFF_ID_BITS));
110 /* individual frame filter */
111 /* Add record (set bit to 1) for each ID that
112 conforms particular rule */
113 for (i = 0; i < (1 << CAN_SFF_ID_BITS); i++) {
114 if ((i & can_mask) == can_id)
115 set_bit(i, rls->match_sff);
120 * canfltr_get_id() - Extracts Can ID out of the sk_buff structure.
122 static canid_t canfltr_get_id(struct sk_buff *skb)
124 /* Can ID is inside of data field */
125 struct can_frame *cf = (struct can_frame *)skb->data;
131 * canfltr_classify() - Performs the classification.
133 * @skb: Socket buffer
135 * @res: Is used for setting Class ID as a result of classification
137 * Iterates over all instances of filter, checking for CAN ID match.
139 * Returns value relevant for policing. Used return values:
140 * TC_POLICE_OK if succesfully classified (without regard to policing rules)
141 * TC_POLICE_UNSPEC if no matching rule was found
143 static int canfltr_classify(struct sk_buff *skb, const struct tcf_proto *tp,
144 struct tcf_result *res)
146 struct canfltr_head *head = (struct canfltr_head *)tp->root;
147 struct canfltr_state *f;
148 struct canfltr_rules *r;
152 can_id = canfltr_get_id(skb);
155 list_for_each_entry(f, &head->flist, link) {
157 r = rcu_dereference(f->rules);
160 if (can_id & CAN_EFF_FLAG) {
161 can_id &= CAN_EFF_MASK;
163 for (i = 0; i < r->eff_rules_count; i++) {
164 if (!(((r->rules_raw[i].can_id ^ can_id) &
165 r->rules_raw[i].can_mask) & CAN_EFF_MASK)) {
171 can_id &= CAN_SFF_MASK;
172 match = test_bit(can_id, r->match_sff);
183 return TC_POLICE_UNSPEC;
187 * canfltr_get() - Looks up a filter element by its handle and returns the
188 * internal filter ID (i.e. pointer)
190 static unsigned long canfltr_get(struct tcf_proto *tp, u32 handle)
192 struct canfltr_head *head = (struct canfltr_head *)tp->root;
193 struct canfltr_state *f;
198 list_for_each_entry(f, &head->flist, link) {
199 if (f->handle == handle)
200 return (unsigned long) f;
207 * canfltr_put() - Is invoked when a filter element previously referenced
208 * with get() is no longer used
210 static void canfltr_put(struct tcf_proto *tp, unsigned long f)
214 static unsigned int canfltr_gen_handle(struct tcf_proto *tp)
216 struct canfltr_head *head = (struct canfltr_head *)tp->root;
222 head->hgenerator += 0x10000;
223 if (head->hgenerator == 0)
224 head->hgenerator = 0x10000;
226 h = head->hgenerator;
227 if (canfltr_get(tp, h) == 0)
233 static void canfltr_rules_free_rcu(struct rcu_head *rcu)
235 kfree(container_of(rcu, struct canfltr_rules, rcu));
238 static int canfltr_set_parms(struct tcf_proto *tp, struct canfltr_state *f,
239 unsigned long base, struct nlattr **tb,
242 struct can_filter *canfltr_nl_rules;
243 struct canfltr_rules *rules_tmp;
247 rules_tmp = kzalloc(sizeof(*rules_tmp), GFP_KERNEL);
252 if (tb[TCA_CANFLTR_CLASSID] == NULL)
255 if (tb[TCA_CANFLTR_RULES]) {
256 canfltr_nl_rules = nla_data(tb[TCA_CANFLTR_RULES]);
257 rules_tmp->sff_rules_count = 0;
258 rules_tmp->eff_rules_count = 0;
259 rules_tmp->rules_count = (nla_len(tb[TCA_CANFLTR_RULES]) /
260 sizeof(struct can_filter));
262 rules_tmp->rules_raw = kzalloc(sizeof(struct can_filter) *
263 rules_tmp->rules_count, GFP_KERNEL);
265 if (rules_tmp->rules_raw == NULL)
268 /* We need two for() loops for copying rules into
269 two contiguous areas in rules_raw */
271 /* Process EFF frame rules*/
272 for (i = 0; i < rules_tmp->rules_count; i++) {
273 if ((canfltr_nl_rules[i].can_id & CAN_EFF_FLAG) &&
274 (canfltr_nl_rules[i].can_mask & CAN_EFF_FLAG)) {
275 memcpy(rules_tmp->rules_raw +
276 rules_tmp->eff_rules_count,
277 &canfltr_nl_rules[i],
278 sizeof(struct can_filter));
279 rules_tmp->eff_rules_count++;
285 /* Process SFF frame rules */
286 for (i = 0; i < rules_tmp->rules_count; i++) {
287 if ((canfltr_nl_rules[i].can_id & CAN_EFF_FLAG) &&
288 (canfltr_nl_rules[i].can_mask & CAN_EFF_FLAG)) {
291 memcpy(rules_tmp->rules_raw +
292 rules_tmp->eff_rules_count +
293 rules_tmp->sff_rules_count,
294 &canfltr_nl_rules[i],
295 sizeof(struct can_filter));
296 rules_tmp->sff_rules_count++;
297 canfltr_sff_match_add(rules_tmp,
298 canfltr_nl_rules[i].can_id,
299 canfltr_nl_rules[i].can_mask);
305 /* Setting parameters for newly created filter */
306 if (f->rules == NULL) {
307 rcu_assign_pointer(f->rules, rules_tmp);
308 } else { /* Changing existing filter */
309 struct canfltr_rules *rules_old;
311 rules_old = xchg(&f->rules, rules_tmp);
312 call_rcu(&rules_old->rcu, canfltr_rules_free_rcu);
323 * canfltr_change() - Called for changing properties of an existing filter or
324 * after addition of a new filter to a class (by calling bind_tcf which binds
325 * an instance of a filter to the class).
327 * @tp: Structure representing instance of a filter.
328 * Part of a linked list of all filters.
331 * @tca: Messages passed through the Netlink from userspace.
334 static int canfltr_change(struct tcf_proto *tp, unsigned long base, u32 handle,
335 struct nlattr **tca, unsigned long *arg)
337 struct canfltr_head *head = (struct canfltr_head *)tp->root;
338 struct canfltr_state *f = (struct canfltr_state *)*arg;
339 struct nlattr *tb[TCA_CANFLTR_MAX + 1];
342 if (tca[TCA_OPTIONS] == NULL)
345 /* Parses a stream of attributes and stores a pointer to each
346 attribute in the tb array accessible via the attribute type.
347 Policy may be set to NULL if no validation is required.*/
348 err = nla_parse_nested(tb, TCA_CANFLTR_MAX, tca[TCA_OPTIONS],
352 /* Change existing filter (remove all settings and add
353 them thereafter as if filter was newly created) */
355 if (handle && f->handle != handle)
358 return canfltr_set_parms(tp, f, base, tb, tca[TCA_RATE]);
361 /* Create new filter */
363 f = kzalloc(sizeof(*f), GFP_KERNEL);
367 if (tb[TCA_CANFLTR_CLASSID]) {
368 f->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
369 tcf_bind_filter(tp, &f->res, base);
373 if (handle) /* handle passed from userspace */
376 f->handle = canfltr_gen_handle(tp);
379 /* Configure filter */
380 err = canfltr_set_parms(tp, f, base, tb, tca[TCA_RATE]);
384 /* Add newly created filter to list of all filters */
386 list_add(&f->link, &head->flist);
388 *arg = (unsigned long) f;
393 if (*arg == 0UL && f)
400 static void canfltr_delete_filter(struct tcf_proto *tp,
401 struct canfltr_state *f)
403 tcf_unbind_filter(tp, &f->res);
406 kfree(f->rules->rules_raw);
412 * canfltr_destroy() - Remove whole filter.
414 static void canfltr_destroy(struct tcf_proto *tp)
416 struct canfltr_head *head = tp->root;
417 struct canfltr_state *f, *n;
419 list_for_each_entry_safe(f, n, &head->flist, link) {
421 canfltr_delete_filter(tp, f);
427 * canfltr_delete() - Delete one instance of a filter.
429 static int canfltr_delete(struct tcf_proto *tp, unsigned long arg)
431 struct canfltr_head *head = (struct canfltr_head *)tp->root;
432 struct canfltr_state *t;
433 struct canfltr_state *f = (struct canfltr_state *)arg;
435 rcu_barrier(); /* Wait for completion of call_rcu()'s */
437 list_for_each_entry(t, &head->flist, link)
442 canfltr_delete_filter(tp, t);
451 * canfltr_init() - Initialize filter
453 static int canfltr_init(struct tcf_proto *tp)
455 struct canfltr_head *head;
457 if ((tp->protocol != htons(ETH_P_ALL)) && (tp->protocol != htons(ETH_P_CAN)))
460 /* Work only on CAN frames */
461 if (tp->protocol == htons(ETH_P_ALL))
462 tp->protocol = htons(ETH_P_CAN);
464 head = kzalloc(sizeof(*head), GFP_KERNEL);
468 INIT_LIST_HEAD(&head->flist);
475 * canfltr_walk() - Iterates over all elements of a filter and invokes a
476 * callback function for each of them. This is used to obtain diagnostic data.
478 static void canfltr_walk(struct tcf_proto *tp, struct tcf_walker *arg)
480 struct canfltr_head *head = (struct canfltr_head *) tp->root;
481 struct canfltr_state *f;
483 list_for_each_entry(f, &head->flist, link) {
484 if (arg->count < arg->skip)
487 if (arg->fn(tp, (unsigned long) f, arg) < 0) {
497 * canfltr_dump() - Returns diagnostic data for a filter or one of its elements.
499 static int canfltr_dump(struct tcf_proto *tp, unsigned long fh,
500 struct sk_buff *skb, struct tcmsg *t)
502 struct canfltr_state *f = (struct canfltr_state *) fh;
504 struct canfltr_rules *r;
510 r = rcu_dereference(f->rules);
511 t->tcm_handle = f->handle;
513 nest = nla_nest_start(skb, TCA_OPTIONS);
515 goto nla_put_failure;
518 NLA_PUT_U32(skb, TCA_CANFLTR_CLASSID, f->res.classid);
520 NLA_PUT(skb, TCA_CANFLTR_RULES, r->rules_count *
521 sizeof(struct can_filter), r->rules_raw);
524 nla_nest_end(skb, nest);
530 nla_nest_cancel(skb, nest);
536 static struct tcf_proto_ops cls_canfltr_ops __read_mostly = {
538 .classify = canfltr_classify,
539 .init = canfltr_init,
540 .destroy = canfltr_destroy,
543 .change = canfltr_change,
544 .delete = canfltr_delete,
545 .walk = canfltr_walk,
546 .dump = canfltr_dump,
547 .owner = THIS_MODULE,
550 static int __init init_canfltr(void)
552 pr_debug("canfltr: CAN filter loaded\n");
553 return register_tcf_proto_ops(&cls_canfltr_ops);
556 static void __exit exit_canfltr(void)
558 pr_debug("canfltr: CAN filter removed\n");
559 unregister_tcf_proto_ops(&cls_canfltr_ops);
562 module_init(init_canfltr);
563 module_exit(exit_canfltr);
564 MODULE_LICENSE("GPL");
565 MODULE_AUTHOR("Rostislav Lisovy <lisovy@gmail.cz>");
566 MODULE_DESCRIPTION("Controller Area Network classifier");