6 * cangw.c - manage PF_CAN netlink gateway
8 * Copyright (c) 2010 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Volkswagen nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * Alternatively, provided that this notice is retained in full, this
24 * software may be distributed under the terms of the GNU General
25 * Public License ("GPL") version 2, in which case the provisions of the
26 * GPL apply INSTEAD OF those given above.
28 * The provided data structures and external interfaces from this code
29 * are not restricted to be used by modules with a GPL compatible license.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
44 * Send feedback to <socketcan-users@lists.berlios.de>
54 #include <sys/socket.h>
56 #include <linux/netlink.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/can/gw.h>
72 } __attribute__((packed));
75 /* some netlink helpers stolen from iproute2 package */
76 #define NLMSG_TAIL(nmsg) \
77 ((struct rtattr *)(((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
79 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
82 int len = RTA_LENGTH(alen);
85 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
86 fprintf(stderr, "addattr_l: message exceeded bound of %d\n",
93 memcpy(RTA_DATA(rta), data, alen);
94 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
98 void printfilter(const void *data)
100 struct can_filter *filter = (struct can_filter *)data;
102 printf("-f %03X:%X ", filter->can_id, filter->can_mask);
105 void printmod(const char *type, const void *data)
110 memcpy (&mod, data, CGW_MODATTR_LEN);
112 printf("-m %s:", type);
114 if (mod.modtype & CGW_MOD_ID)
117 if (mod.modtype & CGW_MOD_DLC)
120 if (mod.modtype & CGW_MOD_DATA)
123 printf(":%03X.%X.", mod.cf.can_id, mod.cf.can_dlc);
125 for (i = 0; i < 8; i++)
126 printf("%02X", mod.cf.data[i]);
132 void print_usage(char *prg)
134 fprintf(stderr, "\nUsage: %s [options]\n\n", prg);
135 fprintf(stderr, "Commands: -A (add a new rule)\n");
136 fprintf(stderr, " -D (delete a rule)\n");
137 fprintf(stderr, " -F (flush - delete all rules) [not yet implemented]\n");
138 fprintf(stderr, " -L (list all rules)\n");
139 fprintf(stderr, "Mandatory: -s <src_dev> (source netdevice)\n");
140 fprintf(stderr, " -d <dst_dev> (destination netdevice)\n");
141 fprintf(stderr, "Options: -t (preserve src_dev rx timestamp)\n");
142 fprintf(stderr, " -e (echo sent frames - recommended on vcanx)\n");
143 fprintf(stderr, " -f <filter> (set CAN filter)\n");
144 fprintf(stderr, " -m <mod> (set frame modifications)\n");
145 fprintf(stderr, "\nValues are given and expected in hexadecimal values. Leading 0s can be omitted.\n");
146 fprintf(stderr, "\n");
147 fprintf(stderr, "<filter> is a <value>:<mask> CAN identifier filter\n");
148 fprintf(stderr, "\n");
149 fprintf(stderr, "<mod> is a CAN frame modification instruction consisting of\n");
150 fprintf(stderr, "<instruction>:<can_frame-elements>:<can_id>.<can_dlc>.<can_data>\n");
151 fprintf(stderr, " - <instruction> is one of 'AND' 'OR' 'XOR' 'SET'\n");
152 fprintf(stderr, " - <can_frame-elements> is _one_ or _more_ of 'I'dentifier 'L'ength 'D'ata\n");
153 fprintf(stderr, " - <can_id> is an u32 value containing the CAN Identifier\n");
154 fprintf(stderr, " - <can_dlc> is an u8 value containing the data length code (0 .. 8)\n");
155 fprintf(stderr, " - <can_data> is always eight(!) u8 values containing the CAN frames data\n");
156 fprintf(stderr, "The instructions are performed in the order 'AND' -> 'OR' -> 'XOR' -> 'SET'\n");
157 fprintf(stderr, "\n");
158 fprintf(stderr, "Example:\n");
159 fprintf(stderr, "%s -A -s can0 -d vcan3 -f 123:C00007FF -m SET:IL:80000333.4.1122334455667788\n", prg);
160 fprintf(stderr, "\n");
163 int parse_mod(char *optarg, struct modattr *modmsg)
168 char hexdata[17] = {0};
171 nptr = strchr(ptr, ':');
173 if ((nptr - ptr > 3) || (nptr - ptr == 0))
176 if (!strncmp(ptr, "AND", 3))
177 modmsg->instruction = CGW_MOD_AND;
178 else if (!strncmp(ptr, "OR", 2))
179 modmsg->instruction = CGW_MOD_OR;
180 else if (!strncmp(ptr, "XOR", 3))
181 modmsg->instruction = CGW_MOD_XOR;
182 else if (!strncmp(ptr, "SET", 3))
183 modmsg->instruction = CGW_MOD_SET;
188 nptr = strchr(ptr, ':');
190 if ((nptr - ptr > 3) || (nptr - ptr == 0))
195 while (*ptr != ':') {
200 modmsg->modtype |= CGW_MOD_ID;
204 modmsg->modtype |= CGW_MOD_DLC;
208 modmsg->modtype |= CGW_MOD_DATA;
217 if ((sscanf(++ptr, "%lx.%hhd.%16s",
218 (long unsigned int *)&modmsg->cf.can_id,
219 (unsigned char *)&modmsg->cf.can_dlc, hexdata) != 3) ||
220 (modmsg->cf.can_dlc > 8))
223 if (strlen(hexdata) != 16)
226 for (i = 0; i < 8; i++) {
227 if (!sscanf(&hexdata[i*2], "%2hhx", &modmsg->cf.data[i]))
234 int main(int argc, char **argv)
240 extern int optind, opterr, optopt;
247 struct rtcanmsg rtcan;
252 char rxbuf[8192]; /* netlink receive buffer */
253 char ifname[IF_NAMESIZE]; /* internface name for if_indextoname() */
254 struct nlmsghdr *nlh;
255 struct nlmsgerr *rte;
256 struct rtcanmsg *rtc;
263 struct can_filter filter;
264 struct sockaddr_nl nladdr;
266 struct modattr modmsg[CGW_MOD_FUNCS];
270 memset(&req, 0, sizeof(req));
272 while ((opt = getopt(argc, argv, "ADFLs:d:tef:m:?")) != -1) {
296 req.rtcan.src_ifindex = if_nametoindex(optarg);
300 req.rtcan.dst_ifindex = if_nametoindex(optarg);
304 req.rtcan.can_txflags |= CAN_GW_TXFLAGS_SRC_TSTAMP;
308 req.rtcan.can_txflags |= CAN_GW_TXFLAGS_ECHO;
312 if (sscanf(optarg, "%lx:%lx",
313 (long unsigned int *)&filter.can_id,
314 (long unsigned int *)&filter.can_mask) == 2) {
317 printf("Bad filter definition '%s'.\n", optarg);
323 /* may be triggered by each of the CGW_MOD_FUNCS functions */
324 if ((modidx < CGW_MOD_FUNCS) && (err = parse_mod(optarg, &modmsg[modidx++]))) {
325 printf("Problem %d with modification definition '%s'.\n", err, optarg);
331 print_usage(basename(argv[0]));
336 fprintf(stderr, "Unknown option %c\n", opt);
337 print_usage(basename(argv[0]));
343 if ((argc - optind != 0) || (cmd == UNSPEC)) {
344 print_usage(basename(argv[0]));
348 if ((cmd == ADD || cmd == DEL) &&
349 ((!req.rtcan.src_ifindex) || (!req.rtcan.dst_ifindex))) {
350 print_usage(basename(argv[0]));
354 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
359 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
360 req.nh.nlmsg_type = RTM_NEWROUTE;
364 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
365 req.nh.nlmsg_type = RTM_DELROUTE;
369 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
370 req.nh.nlmsg_type = RTM_GETROUTE;
374 printf("This function is not yet implemented.\n");
379 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtcanmsg));
380 req.nh.nlmsg_seq = 0;
382 req.rtcan.can_family = AF_CAN;
384 /* add new attributes here */
387 addattr_l(&req.nh, sizeof(req), CGW_FILTER, &filter, sizeof(filter));
390 * a better example code
391 * modmsg.modtype = CGW_MOD_ID;
392 * addattr_l(&req.n, sizeof(req), CGW_MOD_SET, &modmsg, CGW_MODATTR_LEN);
395 /* add up to CGW_MOD_FUNCS modification definitions */
396 for (i = 0; i < modidx; i++)
397 addattr_l(&req.nh, sizeof(req), modmsg[i].instruction, &modmsg[i], CGW_MODATTR_LEN);
399 memset(&nladdr, 0, sizeof(nladdr));
400 nladdr.nl_family = AF_NETLINK;
402 nladdr.nl_groups = 0;
404 err = sendto(s, &req, req.nh.nlmsg_len, 0,
405 (struct sockaddr*)&nladdr, sizeof(nladdr));
407 perror("netlink sendto");
411 /* clean netlink receive buffer */
412 bzero(rxbuf, sizeof(rxbuf));
414 if (cmd == ADD || cmd == DEL) {
416 err = recv(s, &rxbuf, sizeof(rxbuf), 0);
418 perror("netlink recv");
421 nlh = (struct nlmsghdr *)rxbuf;
422 if (nlh->nlmsg_type != NLMSG_ERROR) {
423 fprintf(stderr, "unexpected netlink answer of type %d\n", nlh->nlmsg_type);
426 rte = (struct nlmsgerr *)NLMSG_DATA(nlh);
429 fprintf(stderr, "netlink error %d (%s)\n", err, strerror(abs(err)));
433 /* cmd == LIST (for now) */
436 len = recv(s, &rxbuf, sizeof(rxbuf), 0);
438 perror("netlink recv");
441 nlh = (struct nlmsghdr *)rxbuf;
442 if (nlh->nlmsg_type == NLMSG_DONE)
445 rtc = (struct rtcanmsg *)NLMSG_DATA(nlh);
446 if (rtc->can_family != AF_CAN) {
447 printf("received msg from unknown family %d\n", rtc->can_family);
452 * print list in a representation that
453 * can be used directly for start scripts
456 printf("%s -A ", basename(argv[0]));
457 printf("-s %s ", if_indextoname(rtc->src_ifindex, ifname));
458 printf("-d %s ", if_indextoname(rtc->dst_ifindex, ifname));
460 if (rtc->can_txflags & CAN_GW_TXFLAGS_ECHO)
463 if (rtc->can_txflags & CAN_GW_TXFLAGS_SRC_TSTAMP)
466 /* check for attributes */
467 rta = (struct rtattr *) RTM_RTA(rtc);
468 rtlen = RTM_PAYLOAD(nlh);
469 for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
471 switch(rta->rta_type) {
474 printfilter(RTA_DATA(rta));
478 printmod("AND", RTA_DATA(rta));
482 printmod("OR", RTA_DATA(rta));
486 printmod("XOR", RTA_DATA(rta));
490 printmod("SET", RTA_DATA(rta));
494 handled = *(__u32 *)RTA_DATA(rta);
498 dropped = *(__u32 *)RTA_DATA(rta);
502 printf("Unknown attribute %d!", rta->rta_type);
508 printf("# %d handled %d dropped\n", handled, dropped); /* end of entry */