]> rtime.felk.cvut.cz Git - can-utils.git/blob - cangw.c
add 'v' command response
[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, "           -l <hops> (limit the number of frame hops / routings)\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, deleted;
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                 deleted = 0;
403                 src_ifindex = 0;
404                 dst_ifindex = 0;
405
406                 printf("%s -A ", basename(prgname));
407
408                 /* first parse for mandatory options */
409                 rta = (struct rtattr *) RTCAN_RTA(rtc);
410                 rtlen = RTCAN_PAYLOAD(nlh);
411                 for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
412                 {
413                         //printf("(A-%d)", rta->rta_type);
414                         switch(rta->rta_type) {
415
416                         case CGW_FILTER:
417                         case CGW_MOD_AND:
418                         case CGW_MOD_OR:
419                         case CGW_MOD_XOR:
420                         case CGW_MOD_SET:
421                         case CGW_LIM_HOPS:
422                         case CGW_CS_XOR:
423                         case CGW_CS_CRC8:
424                                 break;
425
426                         case CGW_SRC_IF:
427                                 src_ifindex = *(__u32 *)RTA_DATA(rta);
428                                 break;
429
430                         case CGW_DST_IF:
431                                 dst_ifindex = *(__u32 *)RTA_DATA(rta);
432                                 break;
433
434                         case CGW_HANDLED:
435                                 handled = *(__u32 *)RTA_DATA(rta);
436                                 break;
437
438                         case CGW_DROPPED:
439                                 dropped = *(__u32 *)RTA_DATA(rta);
440                                 break;
441
442                         case CGW_DELETED:
443                                 deleted = *(__u32 *)RTA_DATA(rta);
444                                 break;
445
446                         default:
447                                 printf("Unknown attribute %d!", rta->rta_type);
448                                 return -EINVAL;
449                                 break;
450                         }
451                 }
452
453
454                 printf("-s %s ", if_indextoname(src_ifindex, ifname));
455                 printf("-d %s ", if_indextoname(dst_ifindex, ifname));
456
457                 if (rtc->flags & CGW_FLAGS_CAN_ECHO)
458                         printf("-e ");
459
460                 if (rtc->flags & CGW_FLAGS_CAN_SRC_TSTAMP)
461                         printf("-t ");
462
463                 if (rtc->flags & CGW_FLAGS_CAN_IIF_TX_OK)
464                         printf("-i ");
465
466                 /* second parse for mod attributes */
467                 rta = (struct rtattr *) RTCAN_RTA(rtc);
468                 rtlen = RTCAN_PAYLOAD(nlh);
469                 for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
470                 {
471                         //printf("(B-%d)", rta->rta_type);
472                         switch(rta->rta_type) {
473
474                         case CGW_FILTER:
475                                 printfilter(RTA_DATA(rta));
476                                 break;
477
478                         case CGW_MOD_AND:
479                                 printmod("AND", RTA_DATA(rta));
480                                 break;
481
482                         case CGW_MOD_OR:
483                                 printmod("OR", RTA_DATA(rta));
484                                 break;
485
486                         case CGW_MOD_XOR:
487                                 printmod("XOR", RTA_DATA(rta));
488                                 break;
489
490                         case CGW_MOD_SET:
491                                 printmod("SET", RTA_DATA(rta));
492                                 break;
493
494                         case CGW_LIM_HOPS:
495                                 printf("-l %d ", *(__u8 *)RTA_DATA(rta));
496                                 break;
497
498                         case CGW_CS_XOR:
499                                 print_cs_xor((struct cgw_csum_xor *)RTA_DATA(rta));
500                                 break;
501
502                         case CGW_CS_CRC8:
503                                 print_cs_crc8((struct cgw_csum_crc8 *)RTA_DATA(rta));
504                                 break;
505
506                         case CGW_SRC_IF:
507                         case CGW_DST_IF:
508                         case CGW_HANDLED:
509                         case CGW_DROPPED:
510                         case CGW_DELETED:
511                                 break;
512
513                         default:
514                                 printf("Unknown attribute %d!", rta->rta_type);
515                                 return -EINVAL;
516                                 break;
517                         }
518                 }
519
520                 /* end of entry */
521                 printf("# %d handled %d dropped %d deleted\n",
522                        handled, dropped, deleted);
523
524                 /* jump to next NLMSG in the given buffer */
525                 nlh = NLMSG_NEXT(nlh, len);
526         }
527 }
528
529 int main(int argc, char **argv)
530 {
531         int s;
532         int err = 0;
533
534         int opt;
535         extern int optind, opterr, optopt;
536
537         int cmd = UNSPEC;
538         int have_filter = 0;
539         int have_cs_xor = 0;
540         int have_cs_crc8 = 0;
541
542         struct {
543                 struct nlmsghdr nh;
544                 struct rtcanmsg rtcan;
545                 char buf[600];
546
547         } req;
548
549         unsigned char rxbuf[8192]; /* netlink receive buffer */
550         struct nlmsghdr *nlh;
551         struct nlmsgerr *rte;
552         unsigned int src_ifindex = 0;
553         unsigned int dst_ifindex = 0;
554         __u8 limit_hops = 0;
555         __u16 flags = 0;
556         int len;
557
558         struct can_filter filter;
559         struct sockaddr_nl nladdr;
560
561         struct cgw_csum_xor cs_xor;
562         struct cgw_csum_crc8 cs_crc8;
563         char crc8tab[513] = {0};
564
565         struct modattr modmsg[CGW_MOD_FUNCS];
566         int modidx = 0;
567         int i;
568
569         memset(&req, 0, sizeof(req));
570         memset(&cs_xor, 0, sizeof(cs_xor));
571         memset(&cs_crc8, 0, sizeof(cs_crc8));
572
573         while ((opt = getopt(argc, argv, "ADFLs:d:teil:f:c:p:x:m:?")) != -1) {
574                 switch (opt) {
575
576                 case 'A':
577                         if (cmd == UNSPEC)
578                                 cmd = ADD;
579                         break;
580
581                 case 'D':
582                         if (cmd == UNSPEC)
583                                 cmd = DEL;
584                         break;
585
586                 case 'F':
587                         if (cmd == UNSPEC)
588                                 cmd = FLUSH;
589                         break;
590
591                 case 'L':
592                         if (cmd == UNSPEC)
593                                 cmd = LIST;
594                         break;
595
596                 case 's':
597                         src_ifindex = if_nametoindex(optarg);
598                         break;
599
600                 case 'd':
601                         dst_ifindex = if_nametoindex(optarg);
602                         break;
603
604                 case 't':
605                         flags |= CGW_FLAGS_CAN_SRC_TSTAMP;
606                         break;
607
608                 case 'e':
609                         flags |= CGW_FLAGS_CAN_ECHO;
610                         break;
611
612                 case 'i':
613                         flags |= CGW_FLAGS_CAN_IIF_TX_OK;
614                         break;
615
616                 case 'l':
617                         if (sscanf(optarg, "%hhd", &limit_hops) != 1 || !(limit_hops)) {
618                                 printf("Bad hop limit definition '%s'.\n", optarg);
619                                 exit(1);
620                         }
621                         break;
622
623                 case 'f':
624                         if (sscanf(optarg, "%x:%x", &filter.can_id,
625                                    &filter.can_mask) == 2) {
626                                 have_filter = 1;
627                         } else if (sscanf(optarg, "%x~%x", &filter.can_id,
628                                           &filter.can_mask) == 2) {
629                                 filter.can_id |= CAN_INV_FILTER;
630                                 have_filter = 1;
631                         } else {
632                                 printf("Bad filter definition '%s'.\n", optarg);
633                                 exit(1);
634                         }
635                         break;
636
637                 case 'x':
638                         if (sscanf(optarg, "%hhd:%hhd:%hhd:%hhx",
639                                    &cs_xor.from_idx, &cs_xor.to_idx,
640                                    &cs_xor.result_idx, &cs_xor.init_xor_val) == 4) {
641                                 have_cs_xor = 1;
642                         } else {
643                                 printf("Bad XOR checksum definition '%s'.\n", optarg);
644                                 exit(1);
645                         }
646                         break;
647
648                 case 'c':
649                         if ((sscanf(optarg, "%hhd:%hhd:%hhd:%hhx:%hhx:%512s",
650                                     &cs_crc8.from_idx, &cs_crc8.to_idx,
651                                     &cs_crc8.result_idx, &cs_crc8.init_crc_val,
652                                     &cs_crc8.final_xor_val, crc8tab) == 6) &&
653                             (strlen(crc8tab) == 512) &&
654                             (b64hex(crc8tab, (unsigned char *)&cs_crc8.crctab, 256) == 0)) {
655                                 have_cs_crc8 = 1;
656                         } else {
657                                 printf("Bad CRC8 checksum definition '%s'.\n", optarg);
658                                 exit(1);
659                         }
660                         break;
661
662                 case 'p':
663                         if (parse_crc8_profile(optarg, &cs_crc8)) {
664                                 printf("Bad CRC8 profile definition '%s'.\n", optarg);
665                                 exit(1);
666                         }
667                         break;
668
669                 case 'm':
670                         /* may be triggered by each of the CGW_MOD_FUNCS functions */
671                         if ((modidx < CGW_MOD_FUNCS) && (err = parse_mod(optarg, &modmsg[modidx++]))) {
672                                 printf("Problem %d with modification definition '%s'.\n", err, optarg);
673                                 exit(1);
674                         }
675                         break;
676
677                 case '?':
678                         print_usage(basename(argv[0]));
679                         exit(0);
680                         break;
681
682                 default:
683                         fprintf(stderr, "Unknown option %c\n", opt);
684                         print_usage(basename(argv[0]));
685                         exit(1);
686                         break;
687                 }
688         }
689
690         if ((argc - optind != 0) || (cmd == UNSPEC)) {
691                 print_usage(basename(argv[0]));
692                 exit(1);
693         }
694
695         if ((cmd == ADD || cmd == DEL) &&
696             ((!src_ifindex) || (!dst_ifindex))) {
697                 print_usage(basename(argv[0]));
698                 exit(1);
699         }
700
701         if (!modidx && (have_cs_crc8 || have_cs_xor)) {
702                 printf("-c or -x can only be used in conjunction with -m\n");
703                 exit(1);
704         }
705
706         s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
707
708         switch (cmd) {
709
710         case ADD:
711                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
712                 req.nh.nlmsg_type  = RTM_NEWROUTE;
713                 break;
714
715         case DEL:
716                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
717                 req.nh.nlmsg_type  = RTM_DELROUTE;
718                 break;
719
720         case FLUSH:
721                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
722                 req.nh.nlmsg_type  = RTM_DELROUTE;
723                 /* if_index set to 0 => remove all entries */
724                 src_ifindex  = 0;
725                 dst_ifindex  = 0;
726                 break;
727
728         case LIST:
729                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
730                 req.nh.nlmsg_type  = RTM_GETROUTE;
731                 break;
732
733         default:
734                 printf("This function is not yet implemented.\n");
735                 exit(1);
736                 break;
737         }
738
739         req.nh.nlmsg_len   = NLMSG_LENGTH(sizeof(struct rtcanmsg));
740         req.nh.nlmsg_seq   = 0;
741
742         req.rtcan.can_family  = AF_CAN;
743         req.rtcan.gwtype = CGW_TYPE_CAN_CAN;
744         req.rtcan.flags = flags;
745
746         addattr_l(&req.nh, sizeof(req), CGW_SRC_IF, &src_ifindex, sizeof(src_ifindex));
747         addattr_l(&req.nh, sizeof(req), CGW_DST_IF, &dst_ifindex, sizeof(dst_ifindex));
748
749         /* add new attributes here */
750
751         if (have_filter)
752                 addattr_l(&req.nh, sizeof(req), CGW_FILTER, &filter, sizeof(filter));
753
754         if (have_cs_crc8)
755                 addattr_l(&req.nh, sizeof(req), CGW_CS_CRC8, &cs_crc8, sizeof(cs_crc8));
756
757         if (have_cs_xor)
758                 addattr_l(&req.nh, sizeof(req), CGW_CS_XOR, &cs_xor, sizeof(cs_xor));
759
760         if (limit_hops)
761                 addattr_l(&req.nh, sizeof(req), CGW_LIM_HOPS, &limit_hops, sizeof(__u8));
762
763         /*
764          * a better example code
765          * modmsg.modtype = CGW_MOD_ID;
766          * addattr_l(&req.n, sizeof(req), CGW_MOD_SET, &modmsg, CGW_MODATTR_LEN);
767          */
768
769         /* add up to CGW_MOD_FUNCS modification definitions */
770         for (i = 0; i < modidx; i++)
771                 addattr_l(&req.nh, sizeof(req), modmsg[i].instruction, &modmsg[i], CGW_MODATTR_LEN);
772
773         memset(&nladdr, 0, sizeof(nladdr));
774         nladdr.nl_family = AF_NETLINK;
775         nladdr.nl_pid    = 0;
776         nladdr.nl_groups = 0;
777
778         err = sendto(s, &req, req.nh.nlmsg_len, 0,
779                      (struct sockaddr*)&nladdr, sizeof(nladdr));
780         if (err < 0) {
781                 perror("netlink sendto");
782                 return err;
783         }
784
785         /* clean netlink receive buffer */
786         memset(rxbuf, 0x0, sizeof(rxbuf));
787
788         if (cmd != LIST) {
789
790                 /*
791                  * cmd == ADD || cmd == DEL || cmd == FLUSH
792                  *
793                  * Parse the requested netlink acknowledge return values.
794                  */
795
796                 err = recv(s, &rxbuf, sizeof(rxbuf), 0);
797                 if (err < 0) {
798                         perror("netlink recv");
799                         return err;
800                 }
801                 nlh = (struct nlmsghdr *)rxbuf;
802                 if (nlh->nlmsg_type != NLMSG_ERROR) {
803                         fprintf(stderr, "unexpected netlink answer of type %d\n", nlh->nlmsg_type);
804                         return -EINVAL;
805                 }
806                 rte = (struct nlmsgerr *)NLMSG_DATA(nlh);
807                 err = rte->error;
808                 if (err < 0)
809                         fprintf(stderr, "netlink error %d (%s)\n", err, strerror(abs(err)));
810
811         } else {
812
813                 /* cmd == LIST */
814
815                 while (1) {
816                         len = recv(s, &rxbuf, sizeof(rxbuf), 0);
817                         if (len < 0) {
818                                 perror("netlink recv");
819                                 return len;
820                         }
821 #if 0
822                         printf("received msg len %d\n", len);
823
824                         for (i = 0; i < len; i++)
825                                 printf("%02X ", rxbuf[i]);
826
827                         printf("\n");
828 #endif
829                         /* leave on errors or NLMSG_DONE */
830                         if (parse_rtlist(argv[0], rxbuf, len))
831                                 break;
832                 }
833         }
834
835         close(s);
836
837         return err;
838 }
839