| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* em_canid.c Ematch rule to match CAN frames according to their CAN identifiers |
| 4 |
* |
| 5 |
* Idea: Oliver Hartkopp <[email protected]> |
| 6 |
* Copyright: (c) 2011 Czech Technical University in Prague |
| 7 |
* (c) 2011 Volkswagen Group Research |
| 8 |
* Authors: Michal Sojka <[email protected]> |
| 9 |
* Pavel Pisa <[email protected]> |
| 10 |
* Rostislav Lisovy <[email protected]> |
| 11 |
* Funded by: Volkswagen Group Research |
| 12 |
* |
| 13 |
* Documentation: http://rtime.felk.cvut.cz/can/socketcan-qdisc-final.pdf |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <unistd.h> |
| 19 |
#include <fcntl.h> |
| 20 |
#include <sys/socket.h> |
| 21 |
#include <netinet/in.h> |
| 22 |
#include <arpa/inet.h> |
| 23 |
#include <string.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <linux/can.h> |
| 26 |
#include <inttypes.h> |
| 27 |
#include "m_ematch.h" |
| 28 |
|
| 29 |
#define EM_CANID_RULES_MAX 400 /* Main reason for this number is Netlink |
| 30 |
message size limit equal to Single memory page size. When dump() |
| 31 |
is invoked, there are even some ematch related headers sent from |
| 32 |
kernel to userspace together with em_canid configuration -- |
| 33 |
400*sizeof(struct can_filter) should fit without any problems */ |
| 34 |
|
| 35 |
extern struct ematch_util canid_ematch_util; |
| 36 |
struct rules { |
| 37 |
struct can_filter *rules_raw; |
| 38 |
int rules_capacity; /* Size of array allocated for rules_raw */ |
| 39 |
int rules_cnt; /* Actual number of rules stored in rules_raw */ |
| 40 |
}; |
| 41 |
|
| 42 |
static void canid_print_usage(FILE *fd) |
| 43 |
{ |
| 44 |
fprintf(fd, |
| 45 |
"Usage: canid(IDLIST)\n" \ |
| 46 |
"where: IDLIST := IDSPEC [ IDLIST ]\n" \ |
| 47 |
" IDSPEC := { ’sff’ CANID | ’eff’ CANID }\n" \ |
| 48 |
" CANID := ID[:MASK]\n" \ |
| 49 |
" ID, MASK := hexadecimal number (i.e. 0x123)\n" \ |
| 50 |
"Example: canid(sff 0x123 sff 0x124 sff 0x125:0xf)\n"); |
| 51 |
} |
| 52 |
|
| 53 |
static int canid_parse_rule(struct rules *rules, struct bstr *a, int iseff) |
| 54 |
{ |
| 55 |
unsigned int can_id = 0; |
| 56 |
unsigned int can_mask = 0; |
| 57 |
|
| 58 |
if (sscanf(a->data, "%"SCNx32 ":" "%"SCNx32, &can_id, &can_mask) != 2) { |
| 59 |
if (sscanf(a->data, "%"SCNx32, &can_id) != 1) { |
| 60 |
return -1; |
| 61 |
} else { |
| 62 |
can_mask = (iseff) ? CAN_EFF_MASK : CAN_SFF_MASK; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/* Stretch rules array up to EM_CANID_RULES_MAX if necessary */ |
| 67 |
if (rules->rules_cnt == rules->rules_capacity) { |
| 68 |
if (rules->rules_capacity <= EM_CANID_RULES_MAX/2) { |
| 69 |
rules->rules_capacity *= 2; |
| 70 |
rules->rules_raw = realloc(rules->rules_raw, |
| 71 |
sizeof(struct can_filter) * rules->rules_capacity); |
| 72 |
} else { |
| 73 |
return -2; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
rules->rules_raw[rules->rules_cnt].can_id = |
| 78 |
can_id | ((iseff) ? CAN_EFF_FLAG : 0); |
| 79 |
rules->rules_raw[rules->rules_cnt].can_mask = |
| 80 |
can_mask | CAN_EFF_FLAG; |
| 81 |
|
| 82 |
rules->rules_cnt++; |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
static int canid_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr, |
| 88 |
struct bstr *args) |
| 89 |
{ |
| 90 |
int iseff = 0; |
| 91 |
int ret = 0; |
| 92 |
struct rules rules = { |
| 93 |
.rules_capacity = 25, /* Denominator of EM_CANID_RULES_MAX |
| 94 |
Will be multiplied by 2 to calculate the size for realloc() */ |
| 95 |
.rules_cnt = 0 |
| 96 |
}; |
| 97 |
|
| 98 |
#define PARSE_ERR(CARG, FMT, ARGS...) \ |
| 99 |
em_parse_error(EINVAL, args, CARG, &canid_ematch_util, FMT, ##ARGS) |
| 100 |
|
| 101 |
if (args == NULL) |
| 102 |
return PARSE_ERR(args, "canid: missing arguments"); |
| 103 |
|
| 104 |
rules.rules_raw = calloc(rules.rules_capacity, |
| 105 |
sizeof(struct can_filter)); |
| 106 |
|
| 107 |
do { |
| 108 |
if (!bstrcmp(args, "sff")) { |
| 109 |
iseff = 0; |
| 110 |
} else if (!bstrcmp(args, "eff")) { |
| 111 |
iseff = 1; |
| 112 |
} else { |
| 113 |
ret = PARSE_ERR(args, "canid: invalid key"); |
| 114 |
goto exit; |
| 115 |
} |
| 116 |
|
| 117 |
args = bstr_next(args); |
| 118 |
if (args == NULL) { |
| 119 |
ret = PARSE_ERR(args, "canid: missing argument"); |
| 120 |
goto exit; |
| 121 |
} |
| 122 |
|
| 123 |
ret = canid_parse_rule(&rules, args, iseff); |
| 124 |
if (ret == -1) { |
| 125 |
ret = PARSE_ERR(args, "canid: Improperly formed CAN ID & mask\n"); |
| 126 |
goto exit; |
| 127 |
} else if (ret == -2) { |
| 128 |
ret = PARSE_ERR(args, "canid: Too many arguments on input\n"); |
| 129 |
goto exit; |
| 130 |
} |
| 131 |
} while ((args = bstr_next(args)) != NULL); |
| 132 |
|
| 133 |
addraw_l(n, MAX_MSG, hdr, sizeof(*hdr)); |
| 134 |
addraw_l(n, MAX_MSG, rules.rules_raw, |
| 135 |
sizeof(struct can_filter) * rules.rules_cnt); |
| 136 |
|
| 137 |
#undef PARSE_ERR |
| 138 |
exit: |
| 139 |
free(rules.rules_raw); |
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
static int canid_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data, |
| 144 |
int data_len) |
| 145 |
{ |
| 146 |
struct can_filter *conf = data; /* Array with rules */ |
| 147 |
int rules_count; |
| 148 |
int i; |
| 149 |
|
| 150 |
rules_count = data_len / sizeof(struct can_filter); |
| 151 |
|
| 152 |
for (i = 0; i < rules_count; i++) { |
| 153 |
struct can_filter *pcfltr = &conf[i]; |
| 154 |
|
| 155 |
if (pcfltr->can_id & CAN_EFF_FLAG) { |
| 156 |
if (pcfltr->can_mask == (CAN_EFF_FLAG | CAN_EFF_MASK)) |
| 157 |
fprintf(fd, "eff 0x%"PRIX32, |
| 158 |
pcfltr->can_id & CAN_EFF_MASK); |
| 159 |
else |
| 160 |
fprintf(fd, "eff 0x%"PRIX32":0x%"PRIX32, |
| 161 |
pcfltr->can_id & CAN_EFF_MASK, |
| 162 |
pcfltr->can_mask & CAN_EFF_MASK); |
| 163 |
} else { |
| 164 |
if (pcfltr->can_mask == (CAN_EFF_FLAG | CAN_SFF_MASK)) |
| 165 |
fprintf(fd, "sff 0x%"PRIX32, |
| 166 |
pcfltr->can_id & CAN_SFF_MASK); |
| 167 |
else |
| 168 |
fprintf(fd, "sff 0x%"PRIX32":0x%"PRIX32, |
| 169 |
pcfltr->can_id & CAN_SFF_MASK, |
| 170 |
pcfltr->can_mask & CAN_SFF_MASK); |
| 171 |
} |
| 172 |
|
| 173 |
if ((i + 1) < rules_count) |
| 174 |
fprintf(fd, " "); |
| 175 |
} |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
struct ematch_util canid_ematch_util = { |
| 181 |
.kind = "canid", |
| 182 |
.kind_num = TCF_EM_CANID, |
| 183 |
.parse_eopt = canid_parse_eopt, |
| 184 |
.print_eopt = canid_print_eopt, |
| 185 |
.print_usage = canid_print_usage |
| 186 |
}; |