em_canid.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * em_canid.c Ematch rule to match CAN frames according to their CAN IDs
  3. *
  4. * This program is free software; you can distribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Idea: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
  10. * Copyright: (c) 2011 Czech Technical University in Prague
  11. * (c) 2011 Volkswagen Group Research
  12. * Authors: Michal Sojka <sojkam1@fel.cvut.cz>
  13. * Pavel Pisa <pisa@cmp.felk.cvut.cz>
  14. * Rostislav Lisovy <lisovy@gmail.cz>
  15. * Funded by: Volkswagen Group Research
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22. #include <linux/skbuff.h>
  23. #include <net/pkt_cls.h>
  24. #include <linux/can.h>
  25. #define EM_CAN_RULES_MAX 500
  26. struct canid_match {
  27. /* For each SFF CAN ID (11 bit) there is one record in this bitfield */
  28. DECLARE_BITMAP(match_sff, (1 << CAN_SFF_ID_BITS));
  29. int rules_count;
  30. int sff_rules_count;
  31. int eff_rules_count;
  32. /*
  33. * Raw rules copied from netlink message; Used for sending
  34. * information to userspace (when 'tc filter show' is invoked)
  35. * AND when matching EFF frames
  36. */
  37. struct can_filter rules_raw[];
  38. };
  39. /**
  40. * em_canid_get_id() - Extracts Can ID out of the sk_buff structure.
  41. */
  42. static canid_t em_canid_get_id(struct sk_buff *skb)
  43. {
  44. /* CAN ID is stored within the data field */
  45. struct can_frame *cf = (struct can_frame *)skb->data;
  46. return cf->can_id;
  47. }
  48. static void em_canid_sff_match_add(struct canid_match *cm, u32 can_id,
  49. u32 can_mask)
  50. {
  51. int i;
  52. /*
  53. * Limit can_mask and can_id to SFF range to
  54. * protect against write after end of array
  55. */
  56. can_mask &= CAN_SFF_MASK;
  57. can_id &= can_mask;
  58. /* Single frame */
  59. if (can_mask == CAN_SFF_MASK) {
  60. set_bit(can_id, cm->match_sff);
  61. return;
  62. }
  63. /* All frames */
  64. if (can_mask == 0) {
  65. bitmap_fill(cm->match_sff, (1 << CAN_SFF_ID_BITS));
  66. return;
  67. }
  68. /*
  69. * Individual frame filter.
  70. * Add record (set bit to 1) for each ID that
  71. * conforms particular rule
  72. */
  73. for (i = 0; i < (1 << CAN_SFF_ID_BITS); i++) {
  74. if ((i & can_mask) == can_id)
  75. set_bit(i, cm->match_sff);
  76. }
  77. }
  78. static inline struct canid_match *em_canid_priv(struct tcf_ematch *m)
  79. {
  80. return (struct canid_match *)m->data;
  81. }
  82. static int em_canid_match(struct sk_buff *skb, struct tcf_ematch *m,
  83. struct tcf_pkt_info *info)
  84. {
  85. struct canid_match *cm = em_canid_priv(m);
  86. canid_t can_id;
  87. int match = 0;
  88. int i;
  89. const struct can_filter *lp;
  90. can_id = em_canid_get_id(skb);
  91. if (can_id & CAN_EFF_FLAG) {
  92. for (i = 0, lp = cm->rules_raw;
  93. i < cm->eff_rules_count; i++, lp++) {
  94. if (!(((lp->can_id ^ can_id) & lp->can_mask))) {
  95. match = 1;
  96. break;
  97. }
  98. }
  99. } else { /* SFF */
  100. can_id &= CAN_SFF_MASK;
  101. match = (test_bit(can_id, cm->match_sff) ? 1 : 0);
  102. }
  103. return match;
  104. }
  105. static int em_canid_change(struct net *net, void *data, int len,
  106. struct tcf_ematch *m)
  107. {
  108. struct can_filter *conf = data; /* Array with rules */
  109. struct canid_match *cm;
  110. int i;
  111. if (!len)
  112. return -EINVAL;
  113. if (len % sizeof(struct can_filter))
  114. return -EINVAL;
  115. if (len > sizeof(struct can_filter) * EM_CAN_RULES_MAX)
  116. return -EINVAL;
  117. cm = kzalloc(sizeof(struct canid_match) + len, GFP_KERNEL);
  118. if (!cm)
  119. return -ENOMEM;
  120. cm->rules_count = len / sizeof(struct can_filter);
  121. /*
  122. * We need two for() loops for copying rules into two contiguous
  123. * areas in rules_raw to process all eff rules with a simple loop.
  124. * NB: The configuration interface supports sff and eff rules.
  125. * We do not support filters here that match for the same can_id
  126. * provided in a SFF and EFF frame (e.g. 0x123 / 0x80000123).
  127. * For this (unusual case) two filters have to be specified. The
  128. * SFF/EFF separation is done with the CAN_EFF_FLAG in the can_id.
  129. */
  130. /* Fill rules_raw with EFF rules first */
  131. for (i = 0; i < cm->rules_count; i++) {
  132. if (conf[i].can_id & CAN_EFF_FLAG) {
  133. memcpy(cm->rules_raw + cm->eff_rules_count,
  134. &conf[i],
  135. sizeof(struct can_filter));
  136. cm->eff_rules_count++;
  137. }
  138. }
  139. /* append SFF frame rules */
  140. for (i = 0; i < cm->rules_count; i++) {
  141. if (!(conf[i].can_id & CAN_EFF_FLAG)) {
  142. memcpy(cm->rules_raw
  143. + cm->eff_rules_count
  144. + cm->sff_rules_count,
  145. &conf[i], sizeof(struct can_filter));
  146. cm->sff_rules_count++;
  147. em_canid_sff_match_add(cm,
  148. conf[i].can_id, conf[i].can_mask);
  149. }
  150. }
  151. m->datalen = sizeof(struct canid_match) + len;
  152. m->data = (unsigned long)cm;
  153. return 0;
  154. }
  155. static void em_canid_destroy(struct tcf_ematch *m)
  156. {
  157. struct canid_match *cm = em_canid_priv(m);
  158. kfree(cm);
  159. }
  160. static int em_canid_dump(struct sk_buff *skb, struct tcf_ematch *m)
  161. {
  162. struct canid_match *cm = em_canid_priv(m);
  163. /*
  164. * When configuring this ematch 'rules_count' is set not to exceed
  165. * 'rules_raw' array size
  166. */
  167. if (nla_put_nohdr(skb, sizeof(struct can_filter) * cm->rules_count,
  168. &cm->rules_raw) < 0)
  169. return -EMSGSIZE;
  170. return 0;
  171. }
  172. static struct tcf_ematch_ops em_canid_ops = {
  173. .kind = TCF_EM_CANID,
  174. .change = em_canid_change,
  175. .match = em_canid_match,
  176. .destroy = em_canid_destroy,
  177. .dump = em_canid_dump,
  178. .owner = THIS_MODULE,
  179. .link = LIST_HEAD_INIT(em_canid_ops.link)
  180. };
  181. static int __init init_em_canid(void)
  182. {
  183. return tcf_em_register(&em_canid_ops);
  184. }
  185. static void __exit exit_em_canid(void)
  186. {
  187. tcf_em_unregister(&em_canid_ops);
  188. }
  189. MODULE_LICENSE("GPL");
  190. module_init(init_em_canid);
  191. module_exit(exit_em_canid);
  192. MODULE_ALIAS_TCF_EMATCH(TCF_EM_CANID);