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