2 * em_canid.c Ematch rule to match CAN frames according to their CAN identifiers
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.
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
17 * Documentation: http://rtime.felk.cvut.cz/can/socketcan-qdisc-final.pdf
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
30 #include <linux/can.h>
34 #define EM_CANID_RULES_MAX 400 /* Main reason for this number is Nelink
35 message size limit equal to Single memory page size. When dump()
36 is invoked, there are even some ematch related headers sent from
37 kernel to userspace together with em_canid configuration --
38 400*sizeof(struct can_filter) should fit without any problems */
40 extern struct ematch_util canid_ematch_util
;
42 struct can_filter
*rules_raw
;
43 int rules_capacity
; /* Size of array allocated for rules_raw */
44 int rules_cnt
; /* Actual number of rules stored in rules_raw */
47 static void canid_print_usage(FILE *fd
)
50 "Usage: canid(IDLIST)\n" \
51 "where: IDLIST := IDSPEC [ IDLIST ]\n" \
52 " IDSPEC := { ’sff’ CANID | ’eff’ CANID }\n" \
53 " CANID := ID[:MASK]\n" \
54 " ID, MASK := hexadecimal number (i.e. 0x123)\n" \
55 "Example: canid(sff 0x123 sff 0x124 sff 0x125:0xf)\n");
58 static int canid_parse_rule(struct rules
*rules
, struct bstr
*a
, int iseff
)
60 unsigned int can_id
= 0;
61 unsigned int can_mask
= 0;
63 if (sscanf(a
->data
, "%"SCNx32
":" "%"SCNx32
, &can_id
, &can_mask
) != 2) {
64 if (sscanf(a
->data
, "%"SCNx32
, &can_id
) != 1) {
67 can_mask
= (iseff
) ? CAN_EFF_MASK
: CAN_SFF_MASK
;
71 /* Stretch rules array up to EM_CANID_RULES_MAX if necessary */
72 if (rules
->rules_cnt
== rules
->rules_capacity
) {
73 if (rules
->rules_capacity
<= EM_CANID_RULES_MAX
/2) {
74 rules
->rules_capacity
*= 2;
75 rules
->rules_raw
= realloc(rules
->rules_raw
,
76 sizeof(struct can_filter
) * rules
->rules_capacity
);
82 rules
->rules_raw
[rules
->rules_cnt
].can_id
=
83 can_id
| ((iseff
) ? CAN_EFF_FLAG
: 0);
84 rules
->rules_raw
[rules
->rules_cnt
].can_mask
=
85 can_mask
| CAN_EFF_FLAG
;
92 static int canid_parse_eopt(struct nlmsghdr
*n
, struct tcf_ematch_hdr
*hdr
,
97 struct rules rules
= {
98 .rules_capacity
= 25, /* Denominator of EM_CANID_RULES_MAX
99 Will be multiplied by 2 to calculate the size for realloc() */
103 #define PARSE_ERR(CARG, FMT, ARGS...) \
104 em_parse_error(EINVAL, args, CARG, &canid_ematch_util, FMT, ##ARGS)
107 return PARSE_ERR(args
, "canid: missing arguments");
109 rules
.rules_raw
= calloc(rules
.rules_capacity
,
110 sizeof(struct can_filter
));
113 if (!bstrcmp(args
, "sff")) {
115 } else if (!bstrcmp(args
, "eff")) {
118 ret
= PARSE_ERR(args
, "canid: invalid key");
122 args
= bstr_next(args
);
124 ret
= PARSE_ERR(args
, "canid: missing argument");
128 ret
= canid_parse_rule(&rules
, args
, iseff
);
130 ret
= PARSE_ERR(args
, "canid: Improperly formed CAN ID & mask\n");
132 } else if (ret
== -2) {
133 ret
= PARSE_ERR(args
, "canid: Too many arguments on input\n");
136 } while ((args
= bstr_next(args
)) != NULL
);
138 addraw_l(n
, MAX_MSG
, hdr
, sizeof(*hdr
));
139 addraw_l(n
, MAX_MSG
, rules
.rules_raw
,
140 sizeof(struct can_filter
) * rules
.rules_cnt
);
144 free(rules
.rules_raw
);
148 static int canid_print_eopt(FILE *fd
, struct tcf_ematch_hdr
*hdr
, void *data
,
151 struct can_filter
*conf
= data
; /* Array with rules */
155 rules_count
= data_len
/ sizeof(struct can_filter
);
157 for (i
= 0; i
< rules_count
; i
++) {
158 struct can_filter
*pcfltr
= &conf
[i
];
160 if (pcfltr
->can_id
& CAN_EFF_FLAG
) {
161 if (pcfltr
->can_mask
== (CAN_EFF_FLAG
| CAN_EFF_MASK
))
162 fprintf(fd
, "eff 0x%"PRIX32
,
163 pcfltr
->can_id
& CAN_EFF_MASK
);
165 fprintf(fd
, "eff 0x%"PRIX32
":0x%"PRIX32
,
166 pcfltr
->can_id
& CAN_EFF_MASK
,
167 pcfltr
->can_mask
& CAN_EFF_MASK
);
169 if (pcfltr
->can_mask
== (CAN_EFF_FLAG
| CAN_SFF_MASK
))
170 fprintf(fd
, "sff 0x%"PRIX32
,
171 pcfltr
->can_id
& CAN_SFF_MASK
);
173 fprintf(fd
, "sff 0x%"PRIX32
":0x%"PRIX32
,
174 pcfltr
->can_id
& CAN_SFF_MASK
,
175 pcfltr
->can_mask
& CAN_SFF_MASK
);
178 if ((i
+ 1) < rules_count
)
185 struct ematch_util canid_ematch_util
= {
187 .kind_num
= TCF_EM_CANID
,
188 .parse_eopt
= canid_parse_eopt
,
189 .print_eopt
= canid_print_eopt
,
190 .print_usage
= canid_print_usage