]> rtime.felk.cvut.cz Git - can-utils.git/blob - cangw.c
candump: Enable HW timestamping before using it
[can-utils.git] / cangw.c
1 /*
2  * cangw.c - manage PF_CAN netlink gateway
3  *
4  * Copyright (c) 2010 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of Volkswagen nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * Alternatively, provided that this notice is retained in full, this
20  * software may be distributed under the terms of the GNU General
21  * Public License ("GPL") version 2, in which case the provisions of the
22  * GPL apply INSTEAD OF those given above.
23  *
24  * The provided data structures and external interfaces from this code
25  * are not restricted to be used by modules with a GPL compatible license.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  *
40  * Send feedback to <linux-can@vger.kernel.org>
41  *
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <libgen.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <errno.h>
50 #include <sys/socket.h>
51 #include <net/if.h>
52 #include <linux/netlink.h>
53 #include <linux/rtnetlink.h>
54 #include <linux/can/gw.h>
55
56 enum {
57         UNSPEC,
58         ADD,
59         DEL,
60         FLUSH,
61         LIST
62 };
63
64 struct modattr {
65         struct can_frame cf;
66         __u8 modtype;
67         __u8 instruction;
68 } __attribute__((packed));
69
70
71 #define RTCAN_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct rtcanmsg))))
72 #define RTCAN_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct rtcanmsg))
73
74 /* some netlink helpers stolen from iproute2 package */
75 #define NLMSG_TAIL(nmsg) \
76         ((struct rtattr *)(((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
77
78 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
79               int alen)
80 {
81         int len = RTA_LENGTH(alen);
82         struct rtattr *rta;
83
84         if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
85                 fprintf(stderr, "addattr_l: message exceeded bound of %d\n",
86                         maxlen);
87                 return -1;
88         }
89         rta = NLMSG_TAIL(n);
90         rta->rta_type = type;
91         rta->rta_len = len;
92         memcpy(RTA_DATA(rta), data, alen);
93         n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
94         return 0;
95 }
96
97 void printfilter(const void *data)
98 {
99         struct can_filter *filter = (struct can_filter *)data;
100
101         if (filter->can_id & CAN_INV_FILTER)
102                 printf("-f %03X~%X ", (filter->can_id & ~CAN_INV_FILTER), filter->can_mask);
103         else
104                 printf("-f %03X:%X ", filter->can_id, filter->can_mask);
105 }
106
107 void printmod(const char *type, const void *data)
108 {
109         struct modattr mod;
110         int i;
111
112         memcpy (&mod, data, CGW_MODATTR_LEN);
113
114         printf("-m %s:", type);
115
116         if (mod.modtype & CGW_MOD_ID)
117                 printf("I");
118
119         if (mod.modtype & CGW_MOD_DLC)
120                 printf("L");
121
122         if (mod.modtype & CGW_MOD_DATA)
123                 printf("D");
124
125         printf(":%03X.%X.", mod.cf.can_id, mod.cf.can_dlc);
126
127         for (i = 0; i < 8; i++)
128                 printf("%02X", mod.cf.data[i]);
129
130         printf(" ");
131 }
132
133 void print_cs_xor(struct cgw_csum_xor *cs_xor)
134 {
135         printf("-x %d:%d:%d:%02X ",
136                cs_xor->from_idx, cs_xor->to_idx,
137                cs_xor->result_idx, cs_xor->init_xor_val);
138 }
139
140 void print_cs_crc8_profile(struct cgw_csum_crc8 *cs_crc8)
141 {
142         int i;
143
144         printf("-p %d:", cs_crc8->profile);
145
146         switch (cs_crc8->profile) {
147
148         case  CGW_CRC8PRF_1U8:
149
150                 printf("%02X", cs_crc8->profile_data[0]);
151                 break;
152
153         case  CGW_CRC8PRF_16U8:
154
155                 for (i = 0; i < 16; i++)
156                         printf("%02X", cs_crc8->profile_data[i]);
157                 break;
158
159         case  CGW_CRC8PRF_SFFID_XOR:
160                 break;
161
162         default:
163                 printf("<unknown profile #%d>", cs_crc8->profile);
164         }
165
166         printf(" ");
167 }
168
169 void print_cs_crc8(struct cgw_csum_crc8 *cs_crc8)
170 {
171         int i;
172
173         printf("-c %d:%d:%d:%02X:%02X:",
174                cs_crc8->from_idx, cs_crc8->to_idx,
175                cs_crc8->result_idx, cs_crc8->init_crc_val,
176                cs_crc8->final_xor_val);
177
178         for (i = 0; i < 256; i++)
179                 printf("%02X", cs_crc8->crctab[i]);
180
181         printf(" ");
182
183         if (cs_crc8->profile != CGW_CRC8PRF_UNSPEC)
184                 print_cs_crc8_profile(cs_crc8);
185 }
186
187 void print_usage(char *prg)
188 {
189         fprintf(stderr, "\nUsage: %s [options]\n\n", prg);
190         fprintf(stderr, "Commands:  -A (add a new rule)\n");
191         fprintf(stderr, "           -D (delete a rule)\n");
192         fprintf(stderr, "           -F (flush / delete all rules)\n");
193         fprintf(stderr, "           -L (list all rules)\n");
194         fprintf(stderr, "Mandatory: -s <src_dev>  (source netdevice)\n");
195         fprintf(stderr, "           -d <dst_dev>  (destination netdevice)\n");
196         fprintf(stderr, "Options:   -t (preserve src_dev rx timestamp)\n");
197         fprintf(stderr, "           -e (echo sent frames - recommended on vcanx)\n");
198         fprintf(stderr, "           -i (allow to route to incoming interface)\n");
199         fprintf(stderr, "           -u <uid> (user defined modification identifier)\n");
200         fprintf(stderr, "           -l <hops> (limit the number of frame hops / routings)\n");
201         fprintf(stderr, "           -f <filter> (set CAN filter)\n");
202         fprintf(stderr, "           -m <mod> (set frame modifications)\n");
203         fprintf(stderr, "           -x <from_idx>:<to_idx>:<result_idx>:<init_xor_val> (XOR checksum)\n");
204         fprintf(stderr, "           -c <from>:<to>:<result>:<init_val>:<xor_val>:<crctab[256]> (CRC8 cs)\n");
205         fprintf(stderr, "           -p <profile>:[<profile_data>] (CRC8 checksum profile & parameters)\n");
206         fprintf(stderr, "\nValues are given and expected in hexadecimal values. Leading 0s can be omitted.\n");
207         fprintf(stderr, "\n");
208         fprintf(stderr, "<filter> is a <value><mask> CAN identifier filter\n");
209         fprintf(stderr, "   <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)\n");
210         fprintf(stderr, "   <can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)\n");
211         fprintf(stderr, "\n");
212         fprintf(stderr, "<mod> is a CAN frame modification instruction consisting of\n");
213         fprintf(stderr, "<instruction>:<can_frame-elements>:<can_id>.<can_dlc>.<can_data>\n");
214         fprintf(stderr, " - <instruction> is one of 'AND' 'OR' 'XOR' 'SET'\n");
215         fprintf(stderr, " - <can_frame-elements> is _one_ or _more_ of 'I'dentifier 'L'ength 'D'ata\n");
216         fprintf(stderr, " - <can_id> is an u32 value containing the CAN Identifier\n");
217         fprintf(stderr, " - <can_dlc> is an u8 value containing the data length code (0 .. 8)\n");
218         fprintf(stderr, " - <can_data> is always eight(!) u8 values containing the CAN frames data\n");
219         fprintf(stderr, "The max. four modifications are performed in the order AND -> OR -> XOR -> SET\n");
220         fprintf(stderr, "\n");
221         fprintf(stderr, "Example:\n");
222         fprintf(stderr, "%s -A -s can0 -d vcan3 -e -f 123:C00007FF -m SET:IL:333.4.1122334455667788\n", prg);
223         fprintf(stderr, "\n");
224         fprintf(stderr, "Supported CRC 8 profiles:\n");
225         fprintf(stderr, "Profile '%d' (1U8)       - add one additional u8 value\n", CGW_CRC8PRF_1U8);
226         fprintf(stderr, "Profile '%d' (16U8)      - add u8 value from table[16] indexed by (data[1] & 0xF)\n", CGW_CRC8PRF_16U8);
227         fprintf(stderr, "Profile '%d' (SFFID_XOR) - add u8 value (can_id & 0xFF) ^ (can_id >> 8 & 0xFF)\n", CGW_CRC8PRF_SFFID_XOR);
228         fprintf(stderr, "\n");
229 }
230
231 int b64hex(char *asc, unsigned char *bin, int len)
232 {
233         int i;
234
235         for (i = 0; i < len; i++) {
236                 if (!sscanf(asc+(i*2), "%2hhx", bin+i))
237                         return 1;       
238         }
239         return 0;
240 }
241
242 int parse_crc8_profile(char *optarg, struct cgw_csum_crc8 *crc8)
243 {
244         int ret = 1;
245         char *ptr;
246
247         if (sscanf(optarg, "%hhd:", &crc8->profile) != 1)
248                 return ret;
249
250         switch (crc8->profile) {
251
252         case  CGW_CRC8PRF_1U8:
253
254                 if (sscanf(optarg, "%hhd:%2hhx", &crc8->profile, &crc8->profile_data[0]) == 2)
255                         ret = 0;
256
257                 break;
258
259         case  CGW_CRC8PRF_16U8:
260
261                 ptr = strchr(optarg, ':');
262
263                 /* check if length contains 16 base64 hex values */
264                 if (ptr != NULL &&
265                     strlen(ptr) == strlen(":00112233445566778899AABBCCDDEEFF") &&
266                     b64hex(ptr+1, (unsigned char *)&crc8->profile_data[0], 16) == 0)
267                         ret = 0;
268
269                 break;
270
271         case  CGW_CRC8PRF_SFFID_XOR:
272
273                 /* no additional parameters needed */
274                 ret = 0;
275                 break;
276         }
277
278         return ret;
279 }
280
281 int parse_mod(char *optarg, struct modattr *modmsg)
282 {
283         char *ptr, *nptr;
284         char hexdata[17] = {0};
285
286         ptr = optarg;
287         nptr = strchr(ptr, ':');
288
289         if ((nptr - ptr > 3) || (nptr - ptr == 0))
290                 return 1;
291
292         if (!strncmp(ptr, "AND", 3))
293                 modmsg->instruction = CGW_MOD_AND;
294         else if (!strncmp(ptr, "OR", 2))
295                 modmsg->instruction = CGW_MOD_OR;
296         else if (!strncmp(ptr, "XOR", 3))
297                 modmsg->instruction = CGW_MOD_XOR;
298         else if (!strncmp(ptr, "SET", 3))
299                 modmsg->instruction = CGW_MOD_SET;
300         else
301                 return 2;
302
303         ptr = nptr+1;
304         nptr = strchr(ptr, ':');
305
306         if ((nptr - ptr > 3) || (nptr - ptr == 0))
307                 return 3;
308
309         modmsg->modtype = 0;
310
311         while (*ptr != ':') {
312
313                 switch (*ptr) {
314
315                 case 'I':
316                         modmsg->modtype |= CGW_MOD_ID;
317                         break;
318
319                 case 'L':
320                         modmsg->modtype |= CGW_MOD_DLC;
321                         break;
322
323                 case 'D':
324                         modmsg->modtype |= CGW_MOD_DATA;
325                         break;
326
327                 default:
328                         return 4;
329                 }
330                 ptr++;
331         }
332
333         if (sscanf(++ptr, "%x.%hhx.%16s", &modmsg->cf.can_id,
334                    (unsigned char *)&modmsg->cf.can_dlc, hexdata) != 3)
335                 return 5;
336
337         /* 4-bit masks can have values from 0 to 0xF */ 
338         if (modmsg->cf.can_dlc > 0xF)
339                 return 6;
340
341         /* but when setting CAN_DLC the value has to be limited to 8 */
342         if (modmsg->instruction == CGW_MOD_SET && modmsg->cf.can_dlc > 8)
343                 return 7;
344
345         if (strlen(hexdata) != 16)
346                 return 8;
347
348         if (b64hex(hexdata, &modmsg->cf.data[0], 8))
349                 return 9;
350
351         return 0; /* ok */
352 }
353
354 int parse_rtlist(char *prgname, unsigned char *rxbuf, int len)
355 {
356         char ifname[IF_NAMESIZE]; /* internface name for if_indextoname() */
357         struct rtcanmsg *rtc;
358         struct rtattr *rta;
359         struct nlmsghdr *nlh;
360         unsigned int src_ifindex = 0;
361         unsigned int dst_ifindex = 0;
362         __u32 handled, dropped, deleted;
363         int rtlen;
364
365
366         nlh = (struct nlmsghdr *)rxbuf;
367
368         while (1) {
369                 if (!NLMSG_OK(nlh, len))
370                         return 0;
371
372                 if (nlh->nlmsg_type == NLMSG_ERROR) {
373                         printf("NLMSG_ERROR\n");
374                         return 1;
375                 }
376
377                 if (nlh->nlmsg_type == NLMSG_DONE) {
378                         //printf("NLMSG_DONE\n");
379                         return 1;
380                 }
381
382                 rtc = (struct rtcanmsg *)NLMSG_DATA(nlh);
383                 if (rtc->can_family != AF_CAN) {
384                         printf("received msg from unknown family %d\n", rtc->can_family);
385                         return -EINVAL;
386                 }
387
388                 if (rtc->gwtype != CGW_TYPE_CAN_CAN) {
389                         printf("received msg with unknown gwtype %d\n", rtc->gwtype);
390                         return -EINVAL;
391                 }
392
393                 /*
394                  * print list in a representation that
395                  * can be used directly for start scripts.
396                  *
397                  * To order the mandatory and optional parameters in the
398                  * output string, the NLMSG is parsed twice.
399                  */
400
401                 handled = 0;
402                 dropped = 0;
403                 deleted = 0;
404                 src_ifindex = 0;
405                 dst_ifindex = 0;
406
407                 printf("%s -A ", basename(prgname));
408
409                 /* first parse for mandatory options */
410                 rta = (struct rtattr *) RTCAN_RTA(rtc);
411                 rtlen = RTCAN_PAYLOAD(nlh);
412                 for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
413                 {
414                         //printf("(A-%d)", rta->rta_type);
415                         switch(rta->rta_type) {
416
417                         case CGW_FILTER:
418                         case CGW_MOD_AND:
419                         case CGW_MOD_OR:
420                         case CGW_MOD_XOR:
421                         case CGW_MOD_SET:
422                         case CGW_MOD_UID:
423                         case CGW_LIM_HOPS:
424                         case CGW_CS_XOR:
425                         case CGW_CS_CRC8:
426                                 break;
427
428                         case CGW_SRC_IF:
429                                 src_ifindex = *(__u32 *)RTA_DATA(rta);
430                                 break;
431
432                         case CGW_DST_IF:
433                                 dst_ifindex = *(__u32 *)RTA_DATA(rta);
434                                 break;
435
436                         case CGW_HANDLED:
437                                 handled = *(__u32 *)RTA_DATA(rta);
438                                 break;
439
440                         case CGW_DROPPED:
441                                 dropped = *(__u32 *)RTA_DATA(rta);
442                                 break;
443
444                         case CGW_DELETED:
445                                 deleted = *(__u32 *)RTA_DATA(rta);
446                                 break;
447
448                         default:
449                                 printf("Unknown attribute %d!", rta->rta_type);
450                                 return -EINVAL;
451                                 break;
452                         }
453                 }
454
455
456                 printf("-s %s ", if_indextoname(src_ifindex, ifname));
457                 printf("-d %s ", if_indextoname(dst_ifindex, ifname));
458
459                 if (rtc->flags & CGW_FLAGS_CAN_ECHO)
460                         printf("-e ");
461
462                 if (rtc->flags & CGW_FLAGS_CAN_SRC_TSTAMP)
463                         printf("-t ");
464
465                 if (rtc->flags & CGW_FLAGS_CAN_IIF_TX_OK)
466                         printf("-i ");
467
468                 /* second parse for mod attributes */
469                 rta = (struct rtattr *) RTCAN_RTA(rtc);
470                 rtlen = RTCAN_PAYLOAD(nlh);
471                 for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
472                 {
473                         //printf("(B-%d)", rta->rta_type);
474                         switch(rta->rta_type) {
475
476                         case CGW_FILTER:
477                                 printfilter(RTA_DATA(rta));
478                                 break;
479
480                         case CGW_MOD_AND:
481                                 printmod("AND", RTA_DATA(rta));
482                                 break;
483
484                         case CGW_MOD_OR:
485                                 printmod("OR", RTA_DATA(rta));
486                                 break;
487
488                         case CGW_MOD_XOR:
489                                 printmod("XOR", RTA_DATA(rta));
490                                 break;
491
492                         case CGW_MOD_SET:
493                                 printmod("SET", RTA_DATA(rta));
494                                 break;
495
496                         case CGW_MOD_UID:
497                                 printf("-u %X ", *(__u32 *)RTA_DATA(rta));
498                                 break;
499
500                         case CGW_LIM_HOPS:
501                                 printf("-l %d ", *(__u8 *)RTA_DATA(rta));
502                                 break;
503
504                         case CGW_CS_XOR:
505                                 print_cs_xor((struct cgw_csum_xor *)RTA_DATA(rta));
506                                 break;
507
508                         case CGW_CS_CRC8:
509                                 print_cs_crc8((struct cgw_csum_crc8 *)RTA_DATA(rta));
510                                 break;
511
512                         case CGW_SRC_IF:
513                         case CGW_DST_IF:
514                         case CGW_HANDLED:
515                         case CGW_DROPPED:
516                         case CGW_DELETED:
517                                 break;
518
519                         default:
520                                 printf("Unknown attribute %d!", rta->rta_type);
521                                 return -EINVAL;
522                                 break;
523                         }
524                 }
525
526                 /* end of entry */
527                 printf("# %d handled %d dropped %d deleted\n",
528                        handled, dropped, deleted);
529
530                 /* jump to next NLMSG in the given buffer */
531                 nlh = NLMSG_NEXT(nlh, len);
532         }
533 }
534
535 int main(int argc, char **argv)
536 {
537         int s;
538         int err = 0;
539
540         int opt;
541         extern int optind, opterr, optopt;
542
543         int cmd = UNSPEC;
544         int have_filter = 0;
545         int have_cs_xor = 0;
546         int have_cs_crc8 = 0;
547
548         struct {
549                 struct nlmsghdr nh;
550                 struct rtcanmsg rtcan;
551                 char buf[600];
552
553         } req;
554
555         unsigned char rxbuf[8192]; /* netlink receive buffer */
556         struct nlmsghdr *nlh;
557         struct nlmsgerr *rte;
558         unsigned int src_ifindex = 0;
559         unsigned int dst_ifindex = 0;
560         __u32 uid = 0;
561         __u8 limit_hops = 0;
562         __u16 flags = 0;
563         int len;
564
565         struct can_filter filter;
566         struct sockaddr_nl nladdr;
567
568         struct cgw_csum_xor cs_xor;
569         struct cgw_csum_crc8 cs_crc8;
570         char crc8tab[513] = {0};
571
572         struct modattr modmsg[CGW_MOD_FUNCS];
573         int modidx = 0;
574         int i;
575
576         memset(&req, 0, sizeof(req));
577         memset(&cs_xor, 0, sizeof(cs_xor));
578         memset(&cs_crc8, 0, sizeof(cs_crc8));
579
580         while ((opt = getopt(argc, argv, "ADFLs:d:teiu:l:f:c:p:x:m:?")) != -1) {
581                 switch (opt) {
582
583                 case 'A':
584                         if (cmd == UNSPEC)
585                                 cmd = ADD;
586                         break;
587
588                 case 'D':
589                         if (cmd == UNSPEC)
590                                 cmd = DEL;
591                         break;
592
593                 case 'F':
594                         if (cmd == UNSPEC)
595                                 cmd = FLUSH;
596                         break;
597
598                 case 'L':
599                         if (cmd == UNSPEC)
600                                 cmd = LIST;
601                         break;
602
603                 case 's':
604                         src_ifindex = if_nametoindex(optarg);
605                         if (src_ifindex == 0) {
606                                 perror(optarg);
607                                 return 1;
608                         }
609                         break;
610
611                 case 'd':
612                         dst_ifindex = if_nametoindex(optarg);
613                         if (dst_ifindex == 0) {
614                                 perror(optarg);
615                                 return 1;
616                         }
617                         break;
618
619                 case 't':
620                         flags |= CGW_FLAGS_CAN_SRC_TSTAMP;
621                         break;
622
623                 case 'e':
624                         flags |= CGW_FLAGS_CAN_ECHO;
625                         break;
626
627                 case 'i':
628                         flags |= CGW_FLAGS_CAN_IIF_TX_OK;
629                         break;
630
631                 case 'u':
632                         uid = strtoul(optarg, (char **)NULL, 16);
633                         break;
634
635                 case 'l':
636                         if (sscanf(optarg, "%hhd", &limit_hops) != 1 || !(limit_hops)) {
637                                 printf("Bad hop limit definition '%s'.\n", optarg);
638                                 exit(1);
639                         }
640                         break;
641
642                 case 'f':
643                         if (sscanf(optarg, "%x:%x", &filter.can_id,
644                                    &filter.can_mask) == 2) {
645                                 have_filter = 1;
646                         } else if (sscanf(optarg, "%x~%x", &filter.can_id,
647                                           &filter.can_mask) == 2) {
648                                 filter.can_id |= CAN_INV_FILTER;
649                                 have_filter = 1;
650                         } else {
651                                 printf("Bad filter definition '%s'.\n", optarg);
652                                 exit(1);
653                         }
654                         break;
655
656                 case 'x':
657                         if (sscanf(optarg, "%hhd:%hhd:%hhd:%hhx",
658                                    &cs_xor.from_idx, &cs_xor.to_idx,
659                                    &cs_xor.result_idx, &cs_xor.init_xor_val) == 4) {
660                                 have_cs_xor = 1;
661                         } else {
662                                 printf("Bad XOR checksum definition '%s'.\n", optarg);
663                                 exit(1);
664                         }
665                         break;
666
667                 case 'c':
668                         if ((sscanf(optarg, "%hhd:%hhd:%hhd:%hhx:%hhx:%512s",
669                                     &cs_crc8.from_idx, &cs_crc8.to_idx,
670                                     &cs_crc8.result_idx, &cs_crc8.init_crc_val,
671                                     &cs_crc8.final_xor_val, crc8tab) == 6) &&
672                             (strlen(crc8tab) == 512) &&
673                             (b64hex(crc8tab, (unsigned char *)&cs_crc8.crctab, 256) == 0)) {
674                                 have_cs_crc8 = 1;
675                         } else {
676                                 printf("Bad CRC8 checksum definition '%s'.\n", optarg);
677                                 exit(1);
678                         }
679                         break;
680
681                 case 'p':
682                         if (parse_crc8_profile(optarg, &cs_crc8)) {
683                                 printf("Bad CRC8 profile definition '%s'.\n", optarg);
684                                 exit(1);
685                         }
686                         break;
687
688                 case 'm':
689                         /* may be triggered by each of the CGW_MOD_FUNCS functions */
690                         if ((modidx < CGW_MOD_FUNCS) && (err = parse_mod(optarg, &modmsg[modidx++]))) {
691                                 printf("Problem %d with modification definition '%s'.\n", err, optarg);
692                                 exit(1);
693                         }
694                         break;
695
696                 case '?':
697                         print_usage(basename(argv[0]));
698                         exit(0);
699                         break;
700
701                 default:
702                         fprintf(stderr, "Unknown option %c\n", opt);
703                         print_usage(basename(argv[0]));
704                         exit(1);
705                         break;
706                 }
707         }
708
709         if ((argc - optind != 0) || (cmd == UNSPEC)) {
710                 print_usage(basename(argv[0]));
711                 exit(1);
712         }
713
714         if ((cmd == ADD || cmd == DEL) &&
715             ((!src_ifindex) || (!dst_ifindex))) {
716                 print_usage(basename(argv[0]));
717                 exit(1);
718         }
719
720         if (!modidx && (have_cs_crc8 || have_cs_xor)) {
721                 printf("-c or -x can only be used in conjunction with -m\n");
722                 exit(1);
723         }
724
725         s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
726
727         switch (cmd) {
728
729         case ADD:
730                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
731                 req.nh.nlmsg_type  = RTM_NEWROUTE;
732                 break;
733
734         case DEL:
735                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
736                 req.nh.nlmsg_type  = RTM_DELROUTE;
737                 break;
738
739         case FLUSH:
740                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
741                 req.nh.nlmsg_type  = RTM_DELROUTE;
742                 /* if_index set to 0 => remove all entries */
743                 src_ifindex  = 0;
744                 dst_ifindex  = 0;
745                 break;
746
747         case LIST:
748                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
749                 req.nh.nlmsg_type  = RTM_GETROUTE;
750                 break;
751
752         default:
753                 printf("This function is not yet implemented.\n");
754                 exit(1);
755                 break;
756         }
757
758         req.nh.nlmsg_len   = NLMSG_LENGTH(sizeof(struct rtcanmsg));
759         req.nh.nlmsg_seq   = 0;
760
761         req.rtcan.can_family  = AF_CAN;
762         req.rtcan.gwtype = CGW_TYPE_CAN_CAN;
763         req.rtcan.flags = flags;
764
765         addattr_l(&req.nh, sizeof(req), CGW_SRC_IF, &src_ifindex, sizeof(src_ifindex));
766         addattr_l(&req.nh, sizeof(req), CGW_DST_IF, &dst_ifindex, sizeof(dst_ifindex));
767
768         /* add new attributes here */
769
770         if (have_filter)
771                 addattr_l(&req.nh, sizeof(req), CGW_FILTER, &filter, sizeof(filter));
772
773         if (have_cs_crc8)
774                 addattr_l(&req.nh, sizeof(req), CGW_CS_CRC8, &cs_crc8, sizeof(cs_crc8));
775
776         if (have_cs_xor)
777                 addattr_l(&req.nh, sizeof(req), CGW_CS_XOR, &cs_xor, sizeof(cs_xor));
778
779         if (uid)
780                 addattr_l(&req.nh, sizeof(req), CGW_MOD_UID, &uid, sizeof(__u32));
781
782         if (limit_hops)
783                 addattr_l(&req.nh, sizeof(req), CGW_LIM_HOPS, &limit_hops, sizeof(__u8));
784
785         /*
786          * a better example code
787          * modmsg.modtype = CGW_MOD_ID;
788          * addattr_l(&req.n, sizeof(req), CGW_MOD_SET, &modmsg, CGW_MODATTR_LEN);
789          */
790
791         /* add up to CGW_MOD_FUNCS modification definitions */
792         for (i = 0; i < modidx; i++)
793                 addattr_l(&req.nh, sizeof(req), modmsg[i].instruction, &modmsg[i], CGW_MODATTR_LEN);
794
795         memset(&nladdr, 0, sizeof(nladdr));
796         nladdr.nl_family = AF_NETLINK;
797         nladdr.nl_pid    = 0;
798         nladdr.nl_groups = 0;
799
800         err = sendto(s, &req, req.nh.nlmsg_len, 0,
801                      (struct sockaddr*)&nladdr, sizeof(nladdr));
802         if (err < 0) {
803                 perror("netlink sendto");
804                 return err;
805         }
806
807         /* clean netlink receive buffer */
808         memset(rxbuf, 0x0, sizeof(rxbuf));
809
810         if (cmd != LIST) {
811
812                 /*
813                  * cmd == ADD || cmd == DEL || cmd == FLUSH
814                  *
815                  * Parse the requested netlink acknowledge return values.
816                  */
817
818                 err = recv(s, &rxbuf, sizeof(rxbuf), 0);
819                 if (err < 0) {
820                         perror("netlink recv");
821                         return err;
822                 }
823                 nlh = (struct nlmsghdr *)rxbuf;
824                 if (nlh->nlmsg_type != NLMSG_ERROR) {
825                         fprintf(stderr, "unexpected netlink answer of type %d\n", nlh->nlmsg_type);
826                         return -EINVAL;
827                 }
828                 rte = (struct nlmsgerr *)NLMSG_DATA(nlh);
829                 err = rte->error;
830                 if (err < 0)
831                         fprintf(stderr, "netlink error %d (%s)\n", err, strerror(abs(err)));
832
833         } else {
834
835                 /* cmd == LIST */
836
837                 while (1) {
838                         len = recv(s, &rxbuf, sizeof(rxbuf), 0);
839                         if (len < 0) {
840                                 perror("netlink recv");
841                                 return len;
842                         }
843 #if 0
844                         printf("received msg len %d\n", len);
845
846                         for (i = 0; i < len; i++)
847                                 printf("%02X ", rxbuf[i]);
848
849                         printf("\n");
850 #endif
851                         /* leave on errors or NLMSG_DONE */
852                         if (parse_rtlist(argv[0], rxbuf, len))
853                                 break;
854                 }
855         }
856
857         close(s);
858
859         return err;
860 }
861