Tagging module iproute2 - iproute2-2.6.16-2
[iproute2.git] / ip / ipaddress.c
1 /*
2  * ipaddress.c          "ip address".
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  * Changes:
12  *      Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <syslog.h>
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <string.h>
26 #include <fnmatch.h>
27
28 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/sockios.h>
31
32 #include "rt_names.h"
33 #include "utils.h"
34 #include "ll_map.h"
35 #include "ip_common.h"
36
37 static struct
38 {
39         int ifindex;
40         int family;
41         int oneline;
42         int showqueue;
43         inet_prefix pfx;
44         int scope, scopemask;
45         int flags, flagmask;
46         int up;
47         char *label;
48         int flushed;
49         char *flushb;
50         int flushp;
51         int flushe;
52 } filter;
53
54 static int do_link;
55
56 static void usage(void) __attribute__((noreturn));
57
58 static void usage(void)
59 {
60         if (do_link) {
61                 iplink_usage();
62         }
63         fprintf(stderr, "Usage: ip addr {add|del} IFADDR dev STRING\n");
64         fprintf(stderr, "       ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
65         fprintf(stderr, "                            [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n");
66         fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n");
67         fprintf(stderr, "          [ broadcast ADDR ] [ anycast ADDR ]\n");
68         fprintf(stderr, "          [ label STRING ] [ scope SCOPE-ID ]\n");
69         fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
70         fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
71         fprintf(stderr, "FLAG  := [ permanent | dynamic | secondary | primary |\n");
72         fprintf(stderr, "           tentative | deprecated ]\n");
73         exit(-1);
74 }
75
76 void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
77 {
78         fprintf(fp, "<");
79         if (flags & IFF_UP && !(flags & IFF_RUNNING))
80                 fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
81         flags &= ~IFF_RUNNING;
82 #define _PF(f) if (flags&IFF_##f) { \
83                   flags &= ~IFF_##f ; \
84                   fprintf(fp, #f "%s", flags ? "," : ""); }
85         _PF(LOOPBACK);
86         _PF(BROADCAST);
87         _PF(POINTOPOINT);
88         _PF(MULTICAST);
89         _PF(NOARP);
90         _PF(ALLMULTI);
91         _PF(PROMISC);
92         _PF(MASTER);
93         _PF(SLAVE);
94         _PF(DEBUG);
95         _PF(DYNAMIC);
96         _PF(AUTOMEDIA);
97         _PF(PORTSEL);
98         _PF(NOTRAILERS);
99         _PF(UP);
100 #undef _PF
101         if (flags)
102                 fprintf(fp, "%x", flags);
103         if (mdown)
104                 fprintf(fp, ",M-DOWN");
105         fprintf(fp, "> ");
106 }
107
108 void print_queuelen(char *name)
109 {
110         struct ifreq ifr;
111         int s;
112
113         s = socket(AF_INET, SOCK_STREAM, 0);
114         if (s < 0)
115                 return;
116
117         memset(&ifr, 0, sizeof(ifr));
118         strcpy(ifr.ifr_name, name);
119         if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { 
120                 perror("SIOCGIFXQLEN");
121                 close(s);
122                 return;
123         }
124         close(s);
125
126         if (ifr.ifr_qlen)
127                 printf("qlen %d", ifr.ifr_qlen);
128 }
129
130 int print_linkinfo(const struct sockaddr_nl *who, 
131                    struct nlmsghdr *n, void *arg)
132 {
133         FILE *fp = (FILE*)arg;
134         struct ifinfomsg *ifi = NLMSG_DATA(n);
135         struct rtattr * tb[IFLA_MAX+1];
136         int len = n->nlmsg_len;
137         unsigned m_flag = 0;
138
139         if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
140                 return 0;
141
142         len -= NLMSG_LENGTH(sizeof(*ifi));
143         if (len < 0)
144                 return -1;
145
146         if (filter.ifindex && ifi->ifi_index != filter.ifindex)
147                 return 0;
148         if (filter.up && !(ifi->ifi_flags&IFF_UP))
149                 return 0;
150
151         parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
152         if (tb[IFLA_IFNAME] == NULL) {
153                 fprintf(stderr, "BUG: nil ifname\n");
154                 return -1;
155         }
156         if (filter.label &&
157             (!filter.family || filter.family == AF_PACKET) &&
158             fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
159                 return 0;
160
161         if (n->nlmsg_type == RTM_DELLINK)
162                 fprintf(fp, "Deleted ");
163
164         fprintf(fp, "%d: %s", ifi->ifi_index,
165                 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
166
167         if (tb[IFLA_LINK]) {
168                 SPRINT_BUF(b1);
169                 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
170                 if (iflink == 0)
171                         fprintf(fp, "@NONE: ");
172                 else {
173                         fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
174                         m_flag = ll_index_to_flags(iflink);
175                         m_flag = !(m_flag & IFF_UP);
176                 }
177         } else {
178                 fprintf(fp, ": ");
179         }
180         print_link_flags(fp, ifi->ifi_flags, m_flag);
181
182         if (tb[IFLA_MTU])
183                 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
184         if (tb[IFLA_QDISC])
185                 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
186 #ifdef IFLA_MASTER
187         if (tb[IFLA_MASTER]) {
188                 SPRINT_BUF(b1);
189                 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
190         }
191 #endif
192         if (filter.showqueue)
193                 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
194         
195         if (!filter.family || filter.family == AF_PACKET) {
196                 SPRINT_BUF(b1);
197                 fprintf(fp, "%s", _SL_);
198                 fprintf(fp, "    link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
199
200                 if (tb[IFLA_ADDRESS]) {
201                         fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
202                                                       RTA_PAYLOAD(tb[IFLA_ADDRESS]),
203                                                       ifi->ifi_type,
204                                                       b1, sizeof(b1)));
205                 }
206                 if (tb[IFLA_BROADCAST]) {
207                         if (ifi->ifi_flags&IFF_POINTOPOINT)
208                                 fprintf(fp, " peer ");
209                         else
210                                 fprintf(fp, " brd ");
211                         fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
212                                                       RTA_PAYLOAD(tb[IFLA_BROADCAST]),
213                                                       ifi->ifi_type,
214                                                       b1, sizeof(b1)));
215                 }
216         }
217         if (do_link && tb[IFLA_STATS] && show_stats) {
218                 struct rtnl_link_stats slocal;
219                 struct rtnl_link_stats *s = RTA_DATA(tb[IFLA_STATS]);
220                 if (((unsigned long)s) & (sizeof(unsigned long)-1)) {
221                         memcpy(&slocal, s, sizeof(slocal));
222                         s = &slocal;
223                 }
224                 fprintf(fp, "%s", _SL_);
225                 fprintf(fp, "    RX: bytes  packets  errors  dropped overrun mcast   %s%s",
226                         s->rx_compressed ? "compressed" : "", _SL_);
227                 fprintf(fp, "    %-10u %-8u %-7u %-7u %-7u %-7u",
228                         s->rx_bytes, s->rx_packets, s->rx_errors,
229                         s->rx_dropped, s->rx_over_errors,
230                         s->multicast
231                         );
232                 if (s->rx_compressed)
233                         fprintf(fp, " %-7u", s->rx_compressed);
234                 if (show_stats > 1) {
235                         fprintf(fp, "%s", _SL_);
236                         fprintf(fp, "    RX errors: length  crc     frame   fifo    missed%s", _SL_);
237                         fprintf(fp, "               %-7u  %-7u %-7u %-7u %-7u",
238                                 s->rx_length_errors,
239                                 s->rx_crc_errors,
240                                 s->rx_frame_errors,
241                                 s->rx_fifo_errors,
242                                 s->rx_missed_errors
243                                 );
244                 }
245                 fprintf(fp, "%s", _SL_);
246                 fprintf(fp, "    TX: bytes  packets  errors  dropped carrier collsns %s%s",
247                         s->tx_compressed ? "compressed" : "", _SL_);
248                 fprintf(fp, "    %-10u %-8u %-7u %-7u %-7u %-7u",
249                         s->tx_bytes, s->tx_packets, s->tx_errors,
250                         s->tx_dropped, s->tx_carrier_errors, s->collisions);
251                 if (s->tx_compressed)
252                         fprintf(fp, " %-7u", s->tx_compressed);
253                 if (show_stats > 1) {
254                         fprintf(fp, "%s", _SL_);
255                         fprintf(fp, "    TX errors: aborted fifo    window  heartbeat%s", _SL_);
256                         fprintf(fp, "               %-7u  %-7u %-7u %-7u",
257                                 s->tx_aborted_errors,
258                                 s->tx_fifo_errors,
259                                 s->tx_window_errors,
260                                 s->tx_heartbeat_errors
261                                 );
262                 }
263         }
264         fprintf(fp, "\n");
265         fflush(fp);
266         return 0;
267 }
268
269 static int flush_update(void)
270 {
271         if (rtnl_send(&rth, filter.flushb, filter.flushp) < 0) {
272                 perror("Failed to send flush request\n");
273                 return -1;
274         }
275         filter.flushp = 0;
276         return 0;
277 }
278
279 int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, 
280                    void *arg)
281 {
282         FILE *fp = (FILE*)arg;
283         struct ifaddrmsg *ifa = NLMSG_DATA(n);
284         int len = n->nlmsg_len;
285         struct rtattr * rta_tb[IFA_MAX+1];
286         char abuf[256];
287         SPRINT_BUF(b1);
288
289         if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
290                 return 0;
291         len -= NLMSG_LENGTH(sizeof(*ifa));
292         if (len < 0) {
293                 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
294                 return -1;
295         }
296
297         if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
298                 return 0;
299
300         parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
301
302         if (!rta_tb[IFA_LOCAL])
303                 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
304         if (!rta_tb[IFA_ADDRESS])
305                 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
306
307         if (filter.ifindex && filter.ifindex != ifa->ifa_index)
308                 return 0;
309         if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
310                 return 0;
311         if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
312                 return 0;
313         if (filter.label) {
314                 SPRINT_BUF(b1);
315                 const char *label;
316                 if (rta_tb[IFA_LABEL])
317                         label = RTA_DATA(rta_tb[IFA_LABEL]);
318                 else
319                         label = ll_idx_n2a(ifa->ifa_index, b1);
320                 if (fnmatch(filter.label, label, 0) != 0)
321                         return 0;
322         }
323         if (filter.pfx.family) {
324                 if (rta_tb[IFA_LOCAL]) {
325                         inet_prefix dst;
326                         memset(&dst, 0, sizeof(dst));
327                         dst.family = ifa->ifa_family;
328                         memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
329                         if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
330                                 return 0;
331                 }
332         }
333
334         if (filter.family && filter.family != ifa->ifa_family)
335                 return 0;
336
337         if (filter.flushb) {
338                 struct nlmsghdr *fn;
339                 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
340                         if (flush_update())
341                                 return -1;
342                 }
343                 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
344                 memcpy(fn, n, n->nlmsg_len);
345                 fn->nlmsg_type = RTM_DELADDR;
346                 fn->nlmsg_flags = NLM_F_REQUEST;
347                 fn->nlmsg_seq = ++rth.seq;
348                 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
349                 filter.flushed++;
350                 if (show_stats < 2)
351                         return 0;
352         }
353
354         if (n->nlmsg_type == RTM_DELADDR)
355                 fprintf(fp, "Deleted ");
356
357         if (filter.oneline || filter.flushb)
358                 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
359         if (ifa->ifa_family == AF_INET)
360                 fprintf(fp, "    inet ");
361         else if (ifa->ifa_family == AF_INET6)
362                 fprintf(fp, "    inet6 ");
363         else if (ifa->ifa_family == AF_DECnet)
364                 fprintf(fp, "    dnet ");
365         else if (ifa->ifa_family == AF_IPX)
366                 fprintf(fp, "     ipx ");
367         else
368                 fprintf(fp, "    family %d ", ifa->ifa_family);
369
370         if (rta_tb[IFA_LOCAL]) {
371                 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
372                                               RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
373                                               RTA_DATA(rta_tb[IFA_LOCAL]),
374                                               abuf, sizeof(abuf)));
375
376                 if (rta_tb[IFA_ADDRESS] == NULL ||
377                     memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
378                         fprintf(fp, "/%d ", ifa->ifa_prefixlen);
379                 } else {
380                         fprintf(fp, " peer %s/%d ",
381                                 rt_addr_n2a(ifa->ifa_family,
382                                             RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
383                                             RTA_DATA(rta_tb[IFA_ADDRESS]),
384                                             abuf, sizeof(abuf)),
385                                 ifa->ifa_prefixlen);
386                 }
387         }
388
389         if (rta_tb[IFA_BROADCAST]) {
390                 fprintf(fp, "brd %s ",
391                         rt_addr_n2a(ifa->ifa_family,
392                                     RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
393                                     RTA_DATA(rta_tb[IFA_BROADCAST]),
394                                     abuf, sizeof(abuf)));
395         }
396         if (rta_tb[IFA_ANYCAST]) {
397                 fprintf(fp, "any %s ",
398                         rt_addr_n2a(ifa->ifa_family,
399                                     RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
400                                     RTA_DATA(rta_tb[IFA_ANYCAST]),
401                                     abuf, sizeof(abuf)));
402         }
403         fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
404         if (ifa->ifa_flags&IFA_F_SECONDARY) {
405                 ifa->ifa_flags &= ~IFA_F_SECONDARY;
406                 fprintf(fp, "secondary ");
407         }
408         if (ifa->ifa_flags&IFA_F_TENTATIVE) {
409                 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
410                 fprintf(fp, "tentative ");
411         }
412         if (ifa->ifa_flags&IFA_F_DEPRECATED) {
413                 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
414                 fprintf(fp, "deprecated ");
415         }
416         if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
417                 fprintf(fp, "dynamic ");
418         } else
419                 ifa->ifa_flags &= ~IFA_F_PERMANENT;
420         if (ifa->ifa_flags)
421                 fprintf(fp, "flags %02x ", ifa->ifa_flags);
422         if (rta_tb[IFA_LABEL])
423                 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
424         if (rta_tb[IFA_CACHEINFO]) {
425                 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
426                 char buf[128];
427                 fprintf(fp, "%s", _SL_);
428                 if (ci->ifa_valid == 0xFFFFFFFFU)
429                         sprintf(buf, "valid_lft forever");
430                 else
431                         sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
432                 if (ci->ifa_prefered == 0xFFFFFFFFU)
433                         sprintf(buf+strlen(buf), " preferred_lft forever");
434                 else
435                         sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
436                 fprintf(fp, "       %s", buf);
437         }
438         fprintf(fp, "\n");
439         fflush(fp);
440         return 0;
441 }
442
443
444 struct nlmsg_list
445 {
446         struct nlmsg_list *next;
447         struct nlmsghdr   h;
448 };
449
450 int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
451 {
452         for ( ;ainfo ;  ainfo = ainfo->next) {
453                 struct nlmsghdr *n = &ainfo->h;
454                 struct ifaddrmsg *ifa = NLMSG_DATA(n);
455
456                 if (n->nlmsg_type != RTM_NEWADDR)
457                         continue;
458
459                 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
460                         return -1;
461
462                 if (ifa->ifa_index != ifindex || 
463                     (filter.family && filter.family != ifa->ifa_family))
464                         continue;
465
466                 print_addrinfo(NULL, n, fp);
467         }
468         return 0;
469 }
470
471
472 static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, 
473                        void *arg)
474 {
475         struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
476         struct nlmsg_list *h;
477         struct nlmsg_list **lp;
478
479         h = malloc(n->nlmsg_len+sizeof(void*));
480         if (h == NULL)
481                 return -1;
482
483         memcpy(&h->h, n, n->nlmsg_len);
484         h->next = NULL;
485
486         for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
487         *lp = h;
488
489         ll_remember_index(who, n, NULL);
490         return 0;
491 }
492
493 int ipaddr_list_or_flush(int argc, char **argv, int flush)
494 {
495         struct nlmsg_list *linfo = NULL;
496         struct nlmsg_list *ainfo = NULL;
497         struct nlmsg_list *l, *n;
498         char *filter_dev = NULL;
499         int no_link = 0;
500
501         ipaddr_reset_filter(oneline);
502         filter.showqueue = 1;
503
504         if (filter.family == AF_UNSPEC)
505                 filter.family = preferred_family;
506
507         if (flush) {
508                 if (argc <= 0) {
509                         fprintf(stderr, "Flush requires arguments.\n");
510                         return -1;
511                 }
512                 if (filter.family == AF_PACKET) {
513                         fprintf(stderr, "Cannot flush link addresses.\n");
514                         return -1;
515                 }
516         }
517
518         while (argc > 0) {
519                 if (strcmp(*argv, "to") == 0) {
520                         NEXT_ARG();
521                         get_prefix(&filter.pfx, *argv, filter.family);
522                         if (filter.family == AF_UNSPEC)
523                                 filter.family = filter.pfx.family;
524                 } else if (strcmp(*argv, "scope") == 0) {
525                         unsigned scope = 0;
526                         NEXT_ARG();
527                         filter.scopemask = -1;
528                         if (rtnl_rtscope_a2n(&scope, *argv)) {
529                                 if (strcmp(*argv, "all") != 0)
530                                         invarg("invalid \"scope\"\n", *argv);
531                                 scope = RT_SCOPE_NOWHERE;
532                                 filter.scopemask = 0;
533                         }
534                         filter.scope = scope;
535                 } else if (strcmp(*argv, "up") == 0) {
536                         filter.up = 1;
537                 } else if (strcmp(*argv, "dynamic") == 0) {
538                         filter.flags &= ~IFA_F_PERMANENT;
539                         filter.flagmask |= IFA_F_PERMANENT;
540                 } else if (strcmp(*argv, "permanent") == 0) {
541                         filter.flags |= IFA_F_PERMANENT;
542                         filter.flagmask |= IFA_F_PERMANENT;
543                 } else if (strcmp(*argv, "secondary") == 0) {
544                         filter.flags |= IFA_F_SECONDARY;
545                         filter.flagmask |= IFA_F_SECONDARY;
546                 } else if (strcmp(*argv, "primary") == 0) {
547                         filter.flags &= ~IFA_F_SECONDARY;
548                         filter.flagmask |= IFA_F_SECONDARY;
549                 } else if (strcmp(*argv, "tentative") == 0) {
550                         filter.flags |= IFA_F_TENTATIVE;
551                         filter.flagmask |= IFA_F_TENTATIVE;
552                 } else if (strcmp(*argv, "deprecated") == 0) {
553                         filter.flags |= IFA_F_DEPRECATED;
554                         filter.flagmask |= IFA_F_DEPRECATED;
555                 } else if (strcmp(*argv, "label") == 0) {
556                         NEXT_ARG();
557                         filter.label = *argv;
558                 } else {
559                         if (strcmp(*argv, "dev") == 0) {
560                                 NEXT_ARG();
561                         }
562                         if (matches(*argv, "help") == 0)
563                                 usage();
564                         if (filter_dev)
565                                 duparg2("dev", *argv);
566                         filter_dev = *argv;
567                 }
568                 argv++; argc--;
569         }
570
571         if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
572                 perror("Cannot send dump request");
573                 exit(1);
574         }
575
576         if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
577                 fprintf(stderr, "Dump terminated\n");
578                 exit(1);
579         }
580
581         if (filter_dev) {
582                 filter.ifindex = ll_name_to_index(filter_dev);
583                 if (filter.ifindex <= 0) {
584                         fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
585                         return -1;
586                 }
587         }
588
589         if (flush) {
590                 int round = 0;
591                 char flushb[4096-512];
592
593                 filter.flushb = flushb;
594                 filter.flushp = 0;
595                 filter.flushe = sizeof(flushb);
596
597                 for (;;) {
598                         if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
599                                 perror("Cannot send dump request");
600                                 exit(1);
601                         }
602                         filter.flushed = 0;
603                         if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
604                                 fprintf(stderr, "Flush terminated\n");
605                                 exit(1);
606                         }
607                         if (filter.flushed == 0) {
608                                 if (round == 0) {
609                                         fprintf(stderr, "Nothing to flush.\n");
610                                 } else if (show_stats)
611                                         printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
612                                 fflush(stdout);
613                                 return 0;
614                         }
615                         round++;
616                         if (flush_update() < 0)
617                                 return 1;
618
619                         if (show_stats) {
620                                 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
621                                 fflush(stdout);
622                         }
623                 }
624         }
625
626         if (filter.family != AF_PACKET) {
627                 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
628                         perror("Cannot send dump request");
629                         exit(1);
630                 }
631
632                 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
633                         fprintf(stderr, "Dump terminated\n");
634                         exit(1);
635                 }
636         }
637
638
639         if (filter.family && filter.family != AF_PACKET) {
640                 struct nlmsg_list **lp;
641                 lp=&linfo;
642
643                 if (filter.oneline)
644                         no_link = 1;
645
646                 while ((l=*lp)!=NULL) {
647                         int ok = 0;
648                         struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
649                         struct nlmsg_list *a;
650
651                         for (a=ainfo; a; a=a->next) {
652                                 struct nlmsghdr *n = &a->h;
653                                 struct ifaddrmsg *ifa = NLMSG_DATA(n);
654
655                                 if (ifa->ifa_index != ifi->ifi_index || 
656                                     (filter.family && filter.family != ifa->ifa_family))
657                                         continue;
658                                 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
659                                         continue;
660                                 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
661                                         continue;
662                                 if (filter.pfx.family || filter.label) {
663                                         struct rtattr *tb[IFA_MAX+1];
664                                         parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
665                                         if (!tb[IFA_LOCAL])
666                                                 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
667
668                                         if (filter.pfx.family && tb[IFA_LOCAL]) {
669                                                 inet_prefix dst;
670                                                 memset(&dst, 0, sizeof(dst));
671                                                 dst.family = ifa->ifa_family;
672                                                 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
673                                                 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
674                                                         continue;
675                                         }
676                                         if (filter.label) {
677                                                 SPRINT_BUF(b1);
678                                                 const char *label;
679                                                 if (tb[IFA_LABEL])
680                                                         label = RTA_DATA(tb[IFA_LABEL]);
681                                                 else
682                                                         label = ll_idx_n2a(ifa->ifa_index, b1);
683                                                 if (fnmatch(filter.label, label, 0) != 0)
684                                                         continue;
685                                         }
686                                 }
687
688                                 ok = 1;
689                                 break;
690                         }
691                         if (!ok)
692                                 *lp = l->next;
693                         else
694                                 lp = &l->next;
695                 }
696         }
697
698         for (l=linfo; l; l = n) {
699                 n = l->next;
700                 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
701                         struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
702                         if (filter.family != AF_PACKET)
703                                 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
704                 }
705                 fflush(stdout);
706                 free(l);
707         }
708
709         return 0;
710 }
711
712 int ipaddr_list_link(int argc, char **argv)
713 {
714         preferred_family = AF_PACKET;
715         do_link = 1;
716         return ipaddr_list_or_flush(argc, argv, 0);
717 }
718
719 void ipaddr_reset_filter(int oneline)
720 {
721         memset(&filter, 0, sizeof(filter));
722         filter.oneline = oneline;
723 }
724
725 int default_scope(inet_prefix *lcl)
726 {
727         if (lcl->family == AF_INET) {
728                 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
729                         return RT_SCOPE_HOST;
730         }
731         return 0;
732 }
733
734 int ipaddr_modify(int cmd, int argc, char **argv)
735 {
736         struct {
737                 struct nlmsghdr         n;
738                 struct ifaddrmsg        ifa;
739                 char                    buf[256];
740         } req;
741         char  *d = NULL;
742         char  *l = NULL;
743         char  *lcl_arg = NULL;
744         inet_prefix lcl;
745         inet_prefix peer;
746         int local_len = 0;
747         int peer_len = 0;
748         int brd_len = 0;
749         int any_len = 0;
750         int scoped = 0;
751
752         memset(&req, 0, sizeof(req));
753
754         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
755         req.n.nlmsg_flags = NLM_F_REQUEST;
756         req.n.nlmsg_type = cmd;
757         req.ifa.ifa_family = preferred_family;
758
759         while (argc > 0) {
760                 if (strcmp(*argv, "peer") == 0 ||
761                     strcmp(*argv, "remote") == 0) {
762                         NEXT_ARG();
763
764                         if (peer_len)
765                                 duparg("peer", *argv);
766                         get_prefix(&peer, *argv, req.ifa.ifa_family);
767                         peer_len = peer.bytelen;
768                         if (req.ifa.ifa_family == AF_UNSPEC)
769                                 req.ifa.ifa_family = peer.family;
770                         addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
771                         req.ifa.ifa_prefixlen = peer.bitlen;
772                 } else if (matches(*argv, "broadcast") == 0 ||
773                            strcmp(*argv, "brd") == 0) {
774                         inet_prefix addr;
775                         NEXT_ARG();
776                         if (brd_len)
777                                 duparg("broadcast", *argv);
778                         if (strcmp(*argv, "+") == 0)
779                                 brd_len = -1;
780                         else if (strcmp(*argv, "-") == 0)
781                                 brd_len = -2;
782                         else {
783                                 get_addr(&addr, *argv, req.ifa.ifa_family);
784                                 if (req.ifa.ifa_family == AF_UNSPEC)
785                                         req.ifa.ifa_family = addr.family;
786                                 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
787                                 brd_len = addr.bytelen;
788                         }
789                 } else if (strcmp(*argv, "anycast") == 0) {
790                         inet_prefix addr;
791                         NEXT_ARG();
792                         if (any_len)
793                                 duparg("anycast", *argv);
794                         get_addr(&addr, *argv, req.ifa.ifa_family);
795                         if (req.ifa.ifa_family == AF_UNSPEC)
796                                 req.ifa.ifa_family = addr.family;
797                         addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
798                         any_len = addr.bytelen;
799                 } else if (strcmp(*argv, "scope") == 0) {
800                         unsigned scope = 0;
801                         NEXT_ARG();
802                         if (rtnl_rtscope_a2n(&scope, *argv))
803                                 invarg(*argv, "invalid scope value.");
804                         req.ifa.ifa_scope = scope;
805                         scoped = 1;
806                 } else if (strcmp(*argv, "dev") == 0) {
807                         NEXT_ARG();
808                         d = *argv;
809                 } else if (strcmp(*argv, "label") == 0) {
810                         NEXT_ARG();
811                         l = *argv;
812                         addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
813                 } else {
814                         if (strcmp(*argv, "local") == 0) {
815                                 NEXT_ARG();
816                         }
817                         if (matches(*argv, "help") == 0)
818                                 usage();
819                         if (local_len)
820                                 duparg2("local", *argv);
821                         lcl_arg = *argv;
822                         get_prefix(&lcl, *argv, req.ifa.ifa_family);
823                         if (req.ifa.ifa_family == AF_UNSPEC)
824                                 req.ifa.ifa_family = lcl.family;
825                         addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
826                         local_len = lcl.bytelen;
827                 }
828                 argc--; argv++;
829         }
830         if (d == NULL) {
831                 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
832                 return -1;
833         }
834         if (l && matches(d, l) != 0) {
835                 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
836                 exit(1);
837         }
838
839         if (peer_len == 0 && local_len) {
840                 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
841                         fprintf(stderr,
842                             "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
843                             "         Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
844                             "         This special behaviour is likely to disappear in further releases,\n" \
845                             "         fix your scripts!\n", lcl_arg, local_len*8);
846                 } else {
847                         peer = lcl;
848                         addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
849                 }
850         }
851         if (req.ifa.ifa_prefixlen == 0)
852                 req.ifa.ifa_prefixlen = lcl.bitlen;
853
854         if (brd_len < 0 && cmd != RTM_DELADDR) {
855                 inet_prefix brd;
856                 int i;
857                 if (req.ifa.ifa_family != AF_INET) {
858                         fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
859                         return -1;
860                 }
861                 brd = peer;
862                 if (brd.bitlen <= 30) {
863                         for (i=31; i>=brd.bitlen; i--) {
864                                 if (brd_len == -1)
865                                         brd.data[0] |= htonl(1<<(31-i));
866                                 else
867                                         brd.data[0] &= ~htonl(1<<(31-i));
868                         }
869                         addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
870                         brd_len = brd.bytelen;
871                 }
872         }
873         if (!scoped && cmd != RTM_DELADDR)
874                 req.ifa.ifa_scope = default_scope(&lcl);
875
876         ll_init_map(&rth);
877
878         if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
879                 fprintf(stderr, "Cannot find device \"%s\"\n", d);
880                 return -1;
881         }
882
883         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
884                 exit(2);
885
886         return 0;
887 }
888
889 int do_ipaddr(int argc, char **argv)
890 {
891         if (argc < 1)
892                 return ipaddr_list_or_flush(0, NULL, 0);
893         if (matches(*argv, "add") == 0)
894                 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
895         if (matches(*argv, "delete") == 0)
896                 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
897         if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
898             || matches(*argv, "lst") == 0)
899                 return ipaddr_list_or_flush(argc-1, argv+1, 0);
900         if (matches(*argv, "flush") == 0)
901                 return ipaddr_list_or_flush(argc-1, argv+1, 1);
902         if (matches(*argv, "help") == 0)
903                 usage();
904         fprintf(stderr, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv);
905         exit(-1);
906 }
907