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