]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - can-utils/cangw.c
Complete rework of CAN netlink gateway.
[socketcan-devel.git] / can-utils / cangw.c
1 /*
2  *  $Id$
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 <linux/can/gw.h>
59
60 enum {
61         UNSPEC,
62         ADD,
63         DEL,
64         FLUSH,
65         LIST
66 };
67
68 struct modattr {
69         struct can_frame cf;
70         __u8 modtype;
71         __u8 instruction;
72 } __attribute__((packed));
73
74
75 #define RTCAN_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct rtcanmsg))))
76 #define RTCAN_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct rtcanmsg))
77
78 /* some netlink helpers stolen from iproute2 package */
79 #define NLMSG_TAIL(nmsg) \
80         ((struct rtattr *)(((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
81
82 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
83               int alen)
84 {
85         int len = RTA_LENGTH(alen);
86         struct rtattr *rta;
87
88         if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
89                 fprintf(stderr, "addattr_l: message exceeded bound of %d\n",
90                         maxlen);
91                 return -1;
92         }
93         rta = NLMSG_TAIL(n);
94         rta->rta_type = type;
95         rta->rta_len = len;
96         memcpy(RTA_DATA(rta), data, alen);
97         n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
98         return 0;
99 }
100
101 void printfilter(const void *data)
102 {
103         struct can_filter *filter = (struct can_filter *)data;
104
105         printf("-f %03X:%X ", filter->can_id, filter->can_mask);
106 }
107
108 void printmod(const char *type, const void *data)
109 {
110         struct modattr mod;
111         int i;
112
113         memcpy (&mod, data, CGW_MODATTR_LEN);
114
115         printf("-m %s:", type);
116
117         if (mod.modtype & CGW_MOD_ID)
118                 printf("I");
119
120         if (mod.modtype & CGW_MOD_DLC)
121                 printf("L");
122
123         if (mod.modtype & CGW_MOD_DATA)
124                 printf("D");
125
126         printf(":%03X.%X.", mod.cf.can_id, mod.cf.can_dlc);
127
128         for (i = 0; i < 8; i++)
129                 printf("%02X", mod.cf.data[i]);
130
131         printf(" ");
132 }
133
134 void print_cs_xor(struct cgw_csum_xor *cs_xor)
135 {
136         printf("-x %d:%d:%d:%02X ",
137                cs_xor->from_idx, cs_xor->to_idx,
138                cs_xor->result_idx, cs_xor->prefix_value);
139 }
140
141 void print_cs_crc8(struct cgw_csum_crc8 *cs_crc8)
142 {
143         int i;
144
145         printf("-c %d:%d:%d:",
146                cs_crc8->from_idx, cs_crc8->to_idx,
147                cs_crc8->result_idx);
148
149         for (i = 0; i < 256; i++)
150                 printf("%02X", cs_crc8->crctab[i]);
151
152         printf(" ");
153 }
154
155 void print_usage(char *prg)
156 {
157         fprintf(stderr, "\nUsage: %s [options]\n\n", prg);
158         fprintf(stderr, "Commands:  -A (add a new rule)\n");
159         fprintf(stderr, "           -D (delete a rule)\n");
160         fprintf(stderr, "           -F (flush / delete all rules)\n");
161         fprintf(stderr, "           -L (list all rules)\n");
162         fprintf(stderr, "Mandatory: -s <src_dev>  (source netdevice)\n");
163         fprintf(stderr, "           -d <dst_dev>  (destination netdevice)\n");
164         fprintf(stderr, "Options:   -t (preserve src_dev rx timestamp)\n");
165         fprintf(stderr, "           -e (echo sent frames - recommended on vcanx)\n");
166         fprintf(stderr, "           -f <filter> (set CAN filter)\n");
167         fprintf(stderr, "           -m <mod> (set frame modifications)\n");
168         fprintf(stderr, "\nValues are given and expected in hexadecimal values. Leading 0s can be omitted.\n");
169         fprintf(stderr, "\n");
170         fprintf(stderr, "<filter> is a <value>:<mask> CAN identifier filter\n");
171         fprintf(stderr, "\n");
172         fprintf(stderr, "<mod> is a CAN frame modification instruction consisting of\n");
173         fprintf(stderr, "<instruction>:<can_frame-elements>:<can_id>.<can_dlc>.<can_data>\n");
174         fprintf(stderr, " - <instruction> is one of 'AND' 'OR' 'XOR' 'SET'\n");
175         fprintf(stderr, " - <can_frame-elements> is _one_ or _more_ of 'I'dentifier 'L'ength 'D'ata\n");
176         fprintf(stderr, " - <can_id> is an u32 value containing the CAN Identifier\n");
177         fprintf(stderr, " - <can_dlc> is an u8 value containing the data length code (0 .. 8)\n");
178         fprintf(stderr, " - <can_data> is always eight(!) u8 values containing the CAN frames data\n");
179         fprintf(stderr, "The max. four modifications are performed in the order AND -> OR -> XOR -> SET\n");
180         fprintf(stderr, "\n");
181         fprintf(stderr, "Example:\n");
182         fprintf(stderr, "%s -A -s can0 -d vcan3 -e -f 123:C00007FF -m SET:IL:333.4.1122334455667788\n", prg);
183         fprintf(stderr, "\n");
184 }
185
186 int b64hex(char *asc, unsigned char *bin, int len)
187 {
188         int i;
189
190         for (i = 0; i < len; i++) {
191                 if (!sscanf(asc+(i*2), "%2hhx", bin+i))
192                         return 1;       
193         }
194         return 0;
195 }
196
197 int parse_mod(char *optarg, struct modattr *modmsg)
198 {
199         char *ptr, *nptr;
200         char hexdata[17] = {0};
201
202         ptr = optarg;
203         nptr = strchr(ptr, ':');
204
205         if ((nptr - ptr > 3) || (nptr - ptr == 0))
206                 return 1;
207
208         if (!strncmp(ptr, "AND", 3))
209                 modmsg->instruction = CGW_MOD_AND;
210         else if (!strncmp(ptr, "OR", 2))
211                 modmsg->instruction = CGW_MOD_OR;
212         else if (!strncmp(ptr, "XOR", 3))
213                 modmsg->instruction = CGW_MOD_XOR;
214         else if (!strncmp(ptr, "SET", 3))
215                 modmsg->instruction = CGW_MOD_SET;
216         else
217                 return 2;
218
219         ptr = nptr+1;
220         nptr = strchr(ptr, ':');
221
222         if ((nptr - ptr > 3) || (nptr - ptr == 0))
223                 return 3;
224
225         modmsg->modtype = 0;
226
227         while (*ptr != ':') {
228
229                 switch (*ptr) {
230
231                 case 'I':
232                         modmsg->modtype |= CGW_MOD_ID;
233                         break;
234
235                 case 'L':
236                         modmsg->modtype |= CGW_MOD_DLC;
237                         break;
238
239                 case 'D':
240                         modmsg->modtype |= CGW_MOD_DATA;
241                         break;
242
243                 default:
244                         return 4;
245                 }
246                 ptr++;
247         }
248
249         if ((sscanf(++ptr, "%lx.%hhd.%16s",
250                     (long unsigned int *)&modmsg->cf.can_id,
251                     (unsigned char *)&modmsg->cf.can_dlc, hexdata) != 3) ||
252             (modmsg->cf.can_dlc > 8))
253                 return 5;
254
255         if (strlen(hexdata) != 16)
256                 return 6;
257
258         if (b64hex(hexdata, &modmsg->cf.data[0], 8))
259                 return 7;
260
261         return 0; /* ok */
262 }
263
264 int parse_rtlist(char *prgname, unsigned char *rxbuf, int len)
265 {
266         char ifname[IF_NAMESIZE]; /* internface name for if_indextoname() */
267         struct rtcanmsg *rtc;
268         struct rtattr *rta;
269         struct nlmsghdr *nlh;
270         unsigned int src_ifindex = 0;
271         unsigned int dst_ifindex = 0;
272         __u32 handled, dropped;
273         int rtlen;
274
275
276         nlh = (struct nlmsghdr *)rxbuf;
277
278         while (1) {
279                 if (!NLMSG_OK(nlh, len))
280                         return 0;
281
282                 if (nlh->nlmsg_type == NLMSG_ERROR) {
283                         printf("NLMSG_ERROR\n");
284                         return 1;
285                 }
286
287                 if (nlh->nlmsg_type == NLMSG_DONE) {
288                         //printf("NLMSG_DONE\n");
289                         return 1;
290                 }
291
292                 rtc = (struct rtcanmsg *)NLMSG_DATA(nlh);
293                 if (rtc->can_family != AF_CAN) {
294                         printf("received msg from unknown family %d\n", rtc->can_family);
295                         return -EINVAL;
296                 }
297
298                 if (rtc->gwtype != CGW_TYPE_CAN_CAN) {
299                         printf("received msg with unknown gwtype %d\n", rtc->gwtype);
300                         return -EINVAL;
301                 }
302
303                 /*
304                  * print list in a representation that
305                  * can be used directly for start scripts.
306                  *
307                  * To order the mandatory and optional parameters in the
308                  * output string, the NLMSG is parsed twice.
309                  */
310
311                 handled = 0;
312                 dropped = 0;
313                 src_ifindex = 0;
314                 dst_ifindex = 0;
315
316                 printf("%s -A ", basename(prgname));
317
318                 /* first parse for mandatory options */
319                 rta = (struct rtattr *) RTCAN_RTA(rtc);
320                 rtlen = RTCAN_PAYLOAD(nlh);
321                 for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
322                 {
323                         //printf("(A-%d)", rta->rta_type);
324                         switch(rta->rta_type) {
325
326                         case CGW_FILTER:
327                         case CGW_MOD_AND:
328                         case CGW_MOD_OR:
329                         case CGW_MOD_XOR:
330                         case CGW_MOD_SET:
331                         case CGW_CS_XOR:
332                         case CGW_CS_CRC8:
333                                 break;
334
335                         case CGW_SRC_IF:
336                                 src_ifindex = *(__u32 *)RTA_DATA(rta);
337                                 break;
338
339                         case CGW_DST_IF:
340                                 dst_ifindex = *(__u32 *)RTA_DATA(rta);
341                                 break;
342
343                         case CGW_HANDLED:
344                                 handled = *(__u32 *)RTA_DATA(rta);
345                                 break;
346
347                         case CGW_DROPPED:
348                                 dropped = *(__u32 *)RTA_DATA(rta);
349                                 break;
350
351                         default:
352                                 printf("Unknown attribute %d!", rta->rta_type);
353                                 return -EINVAL;
354                                 break;
355                         }
356                 }
357
358
359                 printf("-s %s ", if_indextoname(src_ifindex, ifname));
360                 printf("-d %s ", if_indextoname(dst_ifindex, ifname));
361
362                 if (rtc->flags & CGW_FLAGS_CAN_ECHO)
363                         printf("-e ");
364
365                 if (rtc->flags & CGW_FLAGS_CAN_SRC_TSTAMP)
366                         printf("-t ");
367
368                 /* second parse for mod attributes */
369                 rta = (struct rtattr *) RTCAN_RTA(rtc);
370                 rtlen = RTCAN_PAYLOAD(nlh);
371                 for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
372                 {
373                         //printf("(B-%d)", rta->rta_type);
374                         switch(rta->rta_type) {
375
376                         case CGW_FILTER:
377                                 printfilter(RTA_DATA(rta));
378                                 break;
379
380                         case CGW_MOD_AND:
381                                 printmod("AND", RTA_DATA(rta));
382                                 break;
383
384                         case CGW_MOD_OR:
385                                 printmod("OR", RTA_DATA(rta));
386                                 break;
387
388                         case CGW_MOD_XOR:
389                                 printmod("XOR", RTA_DATA(rta));
390                                 break;
391
392                         case CGW_MOD_SET:
393                                 printmod("SET", RTA_DATA(rta));
394                                 break;
395
396                         case CGW_CS_XOR:
397                                 print_cs_xor((struct cgw_csum_xor *)RTA_DATA(rta));
398                                 break;
399
400                         case CGW_CS_CRC8:
401                                 print_cs_crc8((struct cgw_csum_crc8 *)RTA_DATA(rta));
402                                 break;
403
404                         case CGW_SRC_IF:
405                         case CGW_DST_IF:
406                         case CGW_HANDLED:
407                         case CGW_DROPPED:
408                                 break;
409
410                         default:
411                                 printf("Unknown attribute %d!", rta->rta_type);
412                                 return -EINVAL;
413                                 break;
414                         }
415                 }
416
417                 printf("# %d handled %d dropped\n", handled, dropped); /* end of entry */
418
419                 /* jump to next NLMSG in the given buffer */
420                 nlh = NLMSG_NEXT(nlh, len);
421         }
422 }
423
424 int main(int argc, char **argv)
425 {
426         int s;
427         int err = 0;
428
429         int opt;
430         extern int optind, opterr, optopt;
431
432         int cmd = UNSPEC;
433         int have_filter = 0;
434         int have_cs_xor = 0;
435         int have_cs_crc8 = 0;
436
437         struct {
438                 struct nlmsghdr nh;
439                 struct rtcanmsg rtcan;
440                 char buf[600];
441
442         } req;
443
444         unsigned char rxbuf[8192]; /* netlink receive buffer */
445         struct nlmsghdr *nlh;
446         struct nlmsgerr *rte;
447         unsigned int src_ifindex = 0;
448         unsigned int dst_ifindex = 0;
449         __u16 flags = 0;
450         int len;
451
452         struct can_filter filter;
453         struct sockaddr_nl nladdr;
454
455         struct cgw_csum_xor cs_xor;
456         struct cgw_csum_crc8 cs_crc8;
457         char crc8tab[513] = {0};
458
459         struct modattr modmsg[CGW_MOD_FUNCS];
460         int modidx = 0;
461         int i;
462
463         memset(&req, 0, sizeof(req));
464
465         while ((opt = getopt(argc, argv, "ADFLs:d:tef:c:x:m:?")) != -1) {
466                 switch (opt) {
467
468                 case 'A':
469                         if (cmd == UNSPEC)
470                                 cmd = ADD;
471                         break;
472
473                 case 'D':
474                         if (cmd == UNSPEC)
475                                 cmd = DEL;
476                         break;
477
478                 case 'F':
479                         if (cmd == UNSPEC)
480                                 cmd = FLUSH;
481                         break;
482
483                 case 'L':
484                         if (cmd == UNSPEC)
485                                 cmd = LIST;
486                         break;
487
488                 case 's':
489                         src_ifindex = if_nametoindex(optarg);
490                         break;
491
492                 case 'd':
493                         dst_ifindex = if_nametoindex(optarg);
494                         break;
495
496                 case 't':
497                         flags |= CGW_FLAGS_CAN_SRC_TSTAMP;
498                         break;
499
500                 case 'e':
501                         flags |= CGW_FLAGS_CAN_ECHO;
502                         break;
503
504                 case 'f':
505                         if (sscanf(optarg, "%lx:%lx",
506                                    (long unsigned int *)&filter.can_id, 
507                                    (long unsigned int *)&filter.can_mask) == 2) {
508                                 have_filter = 1;
509                         } else {
510                                 printf("Bad filter definition '%s'.\n", optarg);
511                                 exit(1);
512                         }
513                         break;
514
515                 case 'x':
516                         if (sscanf(optarg, "%hhd:%hhd:%hhd:%hhx",
517                                    &cs_xor.from_idx, &cs_xor.to_idx,
518                                    &cs_xor.result_idx, &cs_xor.prefix_value) == 4) {
519                                 have_cs_xor = 1;
520                         } else {
521                                 printf("Bad XOR checksum definition '%s'.\n", optarg);
522                                 exit(1);
523                         }
524                         break;
525
526                 case 'c':
527                         if ((sscanf(optarg, "%hhd:%hhd:%hhd:%512s",
528                                     &cs_crc8.from_idx, &cs_crc8.to_idx,
529                                     &cs_crc8.result_idx, crc8tab) == 4) &&
530                             (strlen(crc8tab) == 512) &&
531                             (b64hex(crc8tab, (unsigned char *)&cs_crc8.crctab, 256) == 0)) {
532                                 have_cs_crc8 = 1;
533                         } else {
534                                 printf("Bad CRC8 checksum definition '%s'.\n", optarg);
535                                 exit(1);
536                         }
537                         break;
538
539                 case 'm':
540                         /* may be triggered by each of the CGW_MOD_FUNCS functions */
541                         if ((modidx < CGW_MOD_FUNCS) && (err = parse_mod(optarg, &modmsg[modidx++]))) {
542                                 printf("Problem %d with modification definition '%s'.\n", err, optarg);
543                                 exit(1);
544                         }
545                         break;
546
547                 case '?':
548                         print_usage(basename(argv[0]));
549                         exit(0);
550                         break;
551
552                 default:
553                         fprintf(stderr, "Unknown option %c\n", opt);
554                         print_usage(basename(argv[0]));
555                         exit(1);
556                         break;
557                 }
558         }
559
560         if ((argc - optind != 0) || (cmd == UNSPEC)) {
561                 print_usage(basename(argv[0]));
562                 exit(1);
563         }
564
565         if ((cmd == ADD || cmd == DEL) &&
566             ((!src_ifindex) || (!dst_ifindex))) {
567                 print_usage(basename(argv[0]));
568                 exit(1);
569         }
570
571         s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
572
573         switch (cmd) {
574
575         case ADD:
576                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
577                 req.nh.nlmsg_type  = RTM_NEWROUTE;
578                 break;
579
580         case DEL:
581                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
582                 req.nh.nlmsg_type  = RTM_DELROUTE;
583                 break;
584
585         case FLUSH:
586                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
587                 req.nh.nlmsg_type  = RTM_DELROUTE;
588                 /* if_index set to 0 => remove all entries */
589                 src_ifindex  = 0;
590                 dst_ifindex  = 0;
591                 break;
592
593         case LIST:
594                 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
595                 req.nh.nlmsg_type  = RTM_GETROUTE;
596                 break;
597
598         default:
599                 printf("This function is not yet implemented.\n");
600                 exit(1);
601                 break;
602         }
603
604         req.nh.nlmsg_len   = NLMSG_LENGTH(sizeof(struct rtcanmsg));
605         req.nh.nlmsg_seq   = 0;
606
607         req.rtcan.can_family  = AF_CAN;
608         req.rtcan.gwtype = CGW_TYPE_CAN_CAN;
609         req.rtcan.flags = flags;
610
611         addattr_l(&req.nh, sizeof(req), CGW_SRC_IF, &src_ifindex, sizeof(src_ifindex));
612         addattr_l(&req.nh, sizeof(req), CGW_DST_IF, &dst_ifindex, sizeof(dst_ifindex));
613
614         /* add new attributes here */
615
616         if (have_filter)
617                 addattr_l(&req.nh, sizeof(req), CGW_FILTER, &filter, sizeof(filter));
618
619         if (have_cs_xor)
620                 addattr_l(&req.nh, sizeof(req), CGW_CS_XOR, &cs_xor, sizeof(cs_xor));
621
622         if (have_cs_crc8)
623                 addattr_l(&req.nh, sizeof(req), CGW_CS_CRC8, &cs_crc8, sizeof(cs_crc8));
624
625         /*
626          * a better example code
627          * modmsg.modtype = CGW_MOD_ID;
628          * addattr_l(&req.n, sizeof(req), CGW_MOD_SET, &modmsg, CGW_MODATTR_LEN);
629          */
630
631         /* add up to CGW_MOD_FUNCS modification definitions */
632         for (i = 0; i < modidx; i++)
633                 addattr_l(&req.nh, sizeof(req), modmsg[i].instruction, &modmsg[i], CGW_MODATTR_LEN);
634
635         memset(&nladdr, 0, sizeof(nladdr));
636         nladdr.nl_family = AF_NETLINK;
637         nladdr.nl_pid    = 0;
638         nladdr.nl_groups = 0;
639
640         err = sendto(s, &req, req.nh.nlmsg_len, 0,
641                      (struct sockaddr*)&nladdr, sizeof(nladdr));
642         if (err < 0) {
643                 perror("netlink sendto");
644                 return err;
645         }
646
647         /* clean netlink receive buffer */
648         bzero(rxbuf, sizeof(rxbuf));
649
650         if (cmd != LIST) {
651
652                 /*
653                  * cmd == ADD || cmd == DEL || cmd == FLUSH
654                  *
655                  * Parse the requested netlink acknowledge return values.
656                  */
657
658                 err = recv(s, &rxbuf, sizeof(rxbuf), 0);
659                 if (err < 0) {
660                         perror("netlink recv");
661                         return err;
662                 }
663                 nlh = (struct nlmsghdr *)rxbuf;
664                 if (nlh->nlmsg_type != NLMSG_ERROR) {
665                         fprintf(stderr, "unexpected netlink answer of type %d\n", nlh->nlmsg_type);
666                         return -EINVAL;
667                 }
668                 rte = (struct nlmsgerr *)NLMSG_DATA(nlh);
669                 err = rte->error;
670                 if (err < 0)
671                         fprintf(stderr, "netlink error %d (%s)\n", err, strerror(abs(err)));
672
673         } else {
674
675                 /* cmd == LIST */
676
677                 while (1) {
678                         len = recv(s, &rxbuf, sizeof(rxbuf), 0);
679                         if (len < 0) {
680                                 perror("netlink recv");
681                                 return len;
682                         }
683 #if 0
684                         printf("received msg len %d\n", len);
685
686                         for (i = 0; i < len; i++)
687                                 printf("%02X ", rxbuf[i]);
688
689                         printf("\n");
690 #endif
691                         /* leave on errors or NLMSG_DONE */
692                         if (parse_rtlist(argv[0], rxbuf, len))
693                                 break;
694                 }
695         }
696
697         close(s);
698
699         return err;
700 }
701