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