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