/*
- * $Id$
- */
-
-/*
* cangw.c - manage PF_CAN netlink gateway
*
* Copyright (c) 2010 Volkswagen Group Electronic Research
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
- * Send feedback to <socketcan-users@lists.berlios.de>
+ * Send feedback to <linux-can@vger.kernel.org>
*
*/
{
struct can_filter *filter = (struct can_filter *)data;
- printf("-f %03X:%X ", filter->can_id, filter->can_mask);
+ if (filter->can_id & CAN_INV_FILTER)
+ printf("-f %03X~%X ", (filter->can_id & ~CAN_INV_FILTER), filter->can_mask);
+ else
+ printf("-f %03X:%X ", filter->can_id, filter->can_mask);
}
void printmod(const char *type, const void *data)
cs_xor->result_idx, cs_xor->init_xor_val);
}
+void print_cs_crc8_profile(struct cgw_csum_crc8 *cs_crc8)
+{
+ int i;
+
+ printf("-p %d:", cs_crc8->profile);
+
+ switch (cs_crc8->profile) {
+
+ case CGW_CRC8PRF_1U8:
+
+ printf("%02X", cs_crc8->profile_data[0]);
+ break;
+
+ case CGW_CRC8PRF_16U8:
+
+ for (i = 0; i < 16; i++)
+ printf("%02X", cs_crc8->profile_data[i]);
+ break;
+
+ case CGW_CRC8PRF_SFFID_XOR:
+ break;
+
+ default:
+ printf("<unknown profile #%d>", cs_crc8->profile);
+ }
+
+ printf(" ");
+}
+
void print_cs_crc8(struct cgw_csum_crc8 *cs_crc8)
{
int i;
printf("%02X", cs_crc8->crctab[i]);
printf(" ");
+
+ if (cs_crc8->profile != CGW_CRC8PRF_UNSPEC)
+ print_cs_crc8_profile(cs_crc8);
}
void print_usage(char *prg)
fprintf(stderr, " -d <dst_dev> (destination netdevice)\n");
fprintf(stderr, "Options: -t (preserve src_dev rx timestamp)\n");
fprintf(stderr, " -e (echo sent frames - recommended on vcanx)\n");
+ fprintf(stderr, " -i (allow to route to incoming interface)\n");
fprintf(stderr, " -f <filter> (set CAN filter)\n");
fprintf(stderr, " -m <mod> (set frame modifications)\n");
+ fprintf(stderr, " -x <from_idx>:<to_idx>:<result_idx>:<init_xor_val> (XOR checksum)\n");
+ fprintf(stderr, " -c <from>:<to>:<result>:<init_val>:<xor_val>:<crctab[256]> (CRC8 cs)\n");
+ fprintf(stderr, " -p <profile>:[<profile_data>] (CRC8 checksum profile & parameters)\n");
fprintf(stderr, "\nValues are given and expected in hexadecimal values. Leading 0s can be omitted.\n");
fprintf(stderr, "\n");
- fprintf(stderr, "<filter> is a <value>:<mask> CAN identifier filter\n");
+ fprintf(stderr, "<filter> is a <value><mask> CAN identifier filter\n");
+ fprintf(stderr, " <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)\n");
+ fprintf(stderr, " <can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)\n");
fprintf(stderr, "\n");
fprintf(stderr, "<mod> is a CAN frame modification instruction consisting of\n");
fprintf(stderr, "<instruction>:<can_frame-elements>:<can_id>.<can_dlc>.<can_data>\n");
fprintf(stderr, "Example:\n");
fprintf(stderr, "%s -A -s can0 -d vcan3 -e -f 123:C00007FF -m SET:IL:333.4.1122334455667788\n", prg);
fprintf(stderr, "\n");
+ fprintf(stderr, "Supported CRC 8 profiles:\n");
+ fprintf(stderr, "Profile '%d' (1U8) - add one additional u8 value\n", CGW_CRC8PRF_1U8);
+ fprintf(stderr, "Profile '%d' (16U8) - add u8 value from table[16] indexed by (data[1] & 0xF)\n", CGW_CRC8PRF_16U8);
+ fprintf(stderr, "Profile '%d' (SFFID_XOR) - add u8 value (can_id & 0xFF) ^ (can_id >> 8 & 0xFF)\n", CGW_CRC8PRF_SFFID_XOR);
+ fprintf(stderr, "\n");
}
int b64hex(char *asc, unsigned char *bin, int len)
return 0;
}
+int parse_crc8_profile(char *optarg, struct cgw_csum_crc8 *crc8)
+{
+ int ret = 1;
+ char *ptr;
+
+ if (sscanf(optarg, "%hhd:", &crc8->profile) != 1)
+ return ret;
+
+ switch (crc8->profile) {
+
+ case CGW_CRC8PRF_1U8:
+
+ if (sscanf(optarg, "%hhd:%2hhx", &crc8->profile, &crc8->profile_data[0]) == 2)
+ ret = 0;
+
+ break;
+
+ case CGW_CRC8PRF_16U8:
+
+ ptr = strchr(optarg, ':');
+
+ /* check if length contains 16 base64 hex values */
+ if (ptr != NULL &&
+ strlen(ptr) == strlen(":00112233445566778899AABBCCDDEEFF") &&
+ b64hex(ptr+1, (unsigned char *)&crc8->profile_data[0], 16) == 0)
+ ret = 0;
+
+ break;
+
+ case CGW_CRC8PRF_SFFID_XOR:
+
+ /* no additional parameters needed */
+ ret = 0;
+ break;
+ }
+
+ return ret;
+}
+
int parse_mod(char *optarg, struct modattr *modmsg)
{
char *ptr, *nptr;
ptr++;
}
- if (sscanf(++ptr, "%lx.%hhx.%16s",
- (long unsigned int *)&modmsg->cf.can_id,
+ if (sscanf(++ptr, "%x.%hhx.%16s", &modmsg->cf.can_id,
(unsigned char *)&modmsg->cf.can_dlc, hexdata) != 3)
return 5;
struct nlmsghdr *nlh;
unsigned int src_ifindex = 0;
unsigned int dst_ifindex = 0;
- __u32 handled, dropped;
+ __u32 handled, dropped, deleted;
int rtlen;
handled = 0;
dropped = 0;
+ deleted = 0;
src_ifindex = 0;
dst_ifindex = 0;
dropped = *(__u32 *)RTA_DATA(rta);
break;
+ case CGW_DELETED:
+ deleted = *(__u32 *)RTA_DATA(rta);
+ break;
+
default:
printf("Unknown attribute %d!", rta->rta_type);
return -EINVAL;
if (rtc->flags & CGW_FLAGS_CAN_SRC_TSTAMP)
printf("-t ");
+ if (rtc->flags & CGW_FLAGS_CAN_IIF_TX_OK)
+ printf("-i ");
+
/* second parse for mod attributes */
rta = (struct rtattr *) RTCAN_RTA(rtc);
rtlen = RTCAN_PAYLOAD(nlh);
case CGW_DST_IF:
case CGW_HANDLED:
case CGW_DROPPED:
+ case CGW_DELETED:
break;
default:
}
}
- printf("# %d handled %d dropped\n", handled, dropped); /* end of entry */
+ /* end of entry */
+ printf("# %d handled %d dropped %d deleted\n",
+ handled, dropped, deleted);
/* jump to next NLMSG in the given buffer */
nlh = NLMSG_NEXT(nlh, len);
int i;
memset(&req, 0, sizeof(req));
+ memset(&cs_xor, 0, sizeof(cs_xor));
+ memset(&cs_crc8, 0, sizeof(cs_crc8));
- while ((opt = getopt(argc, argv, "ADFLs:d:tef:c:x:m:?")) != -1) {
+ while ((opt = getopt(argc, argv, "ADFLs:d:teif:c:p:x:m:?")) != -1) {
switch (opt) {
case 'A':
flags |= CGW_FLAGS_CAN_ECHO;
break;
+ case 'i':
+ flags |= CGW_FLAGS_CAN_IIF_TX_OK;
+ break;
+
case 'f':
- if (sscanf(optarg, "%lx:%lx",
- (long unsigned int *)&filter.can_id,
- (long unsigned int *)&filter.can_mask) == 2) {
+ if (sscanf(optarg, "%x:%x", &filter.can_id,
+ &filter.can_mask) == 2) {
+ have_filter = 1;
+ } else if (sscanf(optarg, "%x~%x", &filter.can_id,
+ &filter.can_mask) == 2) {
+ filter.can_id |= CAN_INV_FILTER;
have_filter = 1;
} else {
printf("Bad filter definition '%s'.\n", optarg);
}
break;
+ case 'p':
+ if (parse_crc8_profile(optarg, &cs_crc8)) {
+ printf("Bad CRC8 profile definition '%s'.\n", optarg);
+ exit(1);
+ }
+ break;
+
case 'm':
/* may be triggered by each of the CGW_MOD_FUNCS functions */
if ((modidx < CGW_MOD_FUNCS) && (err = parse_mod(optarg, &modmsg[modidx++]))) {
exit(1);
}
+ if (!modidx && (have_cs_crc8 || have_cs_xor)) {
+ printf("-c or -x can only be used in conjunction with -m\n");
+ exit(1);
+ }
+
s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
switch (cmd) {
}
/* clean netlink receive buffer */
- bzero(rxbuf, sizeof(rxbuf));
+ memset(rxbuf, 0x0, sizeof(rxbuf));
if (cmd != LIST) {