]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - ip/ipaddress.c
Add support for IFALIAS
[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 <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_IFALIAS])
285                 fprintf(fp,"\n    alias %s", 
286                         (const char *) RTA_DATA(tb[IFLA_IFALIAS]));
287
288         if (do_link && tb[IFLA_STATS] && show_stats) {
289                 struct rtnl_link_stats slocal;
290                 struct rtnl_link_stats *s = RTA_DATA(tb[IFLA_STATS]);
291                 if (((unsigned long)s) & (sizeof(unsigned long)-1)) {
292                         memcpy(&slocal, s, sizeof(slocal));
293                         s = &slocal;
294                 }
295                 fprintf(fp, "%s", _SL_);
296                 fprintf(fp, "    RX: bytes  packets  errors  dropped overrun mcast   %s%s",
297                         s->rx_compressed ? "compressed" : "", _SL_);
298                 fprintf(fp, "    %-10u %-8u %-7u %-7u %-7u %-7u",
299                         s->rx_bytes, s->rx_packets, s->rx_errors,
300                         s->rx_dropped, s->rx_over_errors,
301                         s->multicast
302                         );
303                 if (s->rx_compressed)
304                         fprintf(fp, " %-7u", s->rx_compressed);
305                 if (show_stats > 1) {
306                         fprintf(fp, "%s", _SL_);
307                         fprintf(fp, "    RX errors: length  crc     frame   fifo    missed%s", _SL_);
308                         fprintf(fp, "               %-7u  %-7u %-7u %-7u %-7u",
309                                 s->rx_length_errors,
310                                 s->rx_crc_errors,
311                                 s->rx_frame_errors,
312                                 s->rx_fifo_errors,
313                                 s->rx_missed_errors
314                                 );
315                 }
316                 fprintf(fp, "%s", _SL_);
317                 fprintf(fp, "    TX: bytes  packets  errors  dropped carrier collsns %s%s",
318                         s->tx_compressed ? "compressed" : "", _SL_);
319                 fprintf(fp, "    %-10u %-8u %-7u %-7u %-7u %-7u",
320                         s->tx_bytes, s->tx_packets, s->tx_errors,
321                         s->tx_dropped, s->tx_carrier_errors, s->collisions);
322                 if (s->tx_compressed)
323                         fprintf(fp, " %-7u", s->tx_compressed);
324                 if (show_stats > 1) {
325                         fprintf(fp, "%s", _SL_);
326                         fprintf(fp, "    TX errors: aborted fifo    window  heartbeat%s", _SL_);
327                         fprintf(fp, "               %-7u  %-7u %-7u %-7u",
328                                 s->tx_aborted_errors,
329                                 s->tx_fifo_errors,
330                                 s->tx_window_errors,
331                                 s->tx_heartbeat_errors
332                                 );
333                 }
334         }
335         fprintf(fp, "\n");
336         fflush(fp);
337         return 0;
338 }
339
340 static int flush_update(void)
341 {
342         if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
343                 perror("Failed to send flush request");
344                 return -1;
345         }
346         filter.flushp = 0;
347         return 0;
348 }
349
350 static int set_lifetime(unsigned int *lifetime, char *argv)
351 {
352         if (strcmp(argv, "forever") == 0)
353                 *lifetime = INFINITY_LIFE_TIME;
354         else if (get_u32(lifetime, argv, 0))
355                 return -1;
356
357         return 0;
358 }
359
360 int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
361                    void *arg)
362 {
363         FILE *fp = (FILE*)arg;
364         struct ifaddrmsg *ifa = NLMSG_DATA(n);
365         int len = n->nlmsg_len;
366         int deprecated = 0;
367         struct rtattr * rta_tb[IFA_MAX+1];
368         char abuf[256];
369         SPRINT_BUF(b1);
370
371         if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
372                 return 0;
373         len -= NLMSG_LENGTH(sizeof(*ifa));
374         if (len < 0) {
375                 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
376                 return -1;
377         }
378
379         if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
380                 return 0;
381
382         parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
383
384         if (!rta_tb[IFA_LOCAL])
385                 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
386         if (!rta_tb[IFA_ADDRESS])
387                 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
388
389         if (filter.ifindex && filter.ifindex != ifa->ifa_index)
390                 return 0;
391         if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
392                 return 0;
393         if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
394                 return 0;
395         if (filter.label) {
396                 SPRINT_BUF(b1);
397                 const char *label;
398                 if (rta_tb[IFA_LABEL])
399                         label = RTA_DATA(rta_tb[IFA_LABEL]);
400                 else
401                         label = ll_idx_n2a(ifa->ifa_index, b1);
402                 if (fnmatch(filter.label, label, 0) != 0)
403                         return 0;
404         }
405         if (filter.pfx.family) {
406                 if (rta_tb[IFA_LOCAL]) {
407                         inet_prefix dst;
408                         memset(&dst, 0, sizeof(dst));
409                         dst.family = ifa->ifa_family;
410                         memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
411                         if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
412                                 return 0;
413                 }
414         }
415
416         if (filter.family && filter.family != ifa->ifa_family)
417                 return 0;
418
419         if (filter.flushb) {
420                 struct nlmsghdr *fn;
421                 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
422                         if (flush_update())
423                                 return -1;
424                 }
425                 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
426                 memcpy(fn, n, n->nlmsg_len);
427                 fn->nlmsg_type = RTM_DELADDR;
428                 fn->nlmsg_flags = NLM_F_REQUEST;
429                 fn->nlmsg_seq = ++rth.seq;
430                 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
431                 filter.flushed++;
432                 if (show_stats < 2)
433                         return 0;
434         }
435
436         if (n->nlmsg_type == RTM_DELADDR)
437                 fprintf(fp, "Deleted ");
438
439         if (filter.oneline || filter.flushb)
440                 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
441         if (ifa->ifa_family == AF_INET)
442                 fprintf(fp, "    inet ");
443         else if (ifa->ifa_family == AF_INET6)
444                 fprintf(fp, "    inet6 ");
445         else if (ifa->ifa_family == AF_DECnet)
446                 fprintf(fp, "    dnet ");
447         else if (ifa->ifa_family == AF_IPX)
448                 fprintf(fp, "     ipx ");
449         else
450                 fprintf(fp, "    family %d ", ifa->ifa_family);
451
452         if (rta_tb[IFA_LOCAL]) {
453                 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
454                                               RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
455                                               RTA_DATA(rta_tb[IFA_LOCAL]),
456                                               abuf, sizeof(abuf)));
457
458                 if (rta_tb[IFA_ADDRESS] == NULL ||
459                     memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
460                         fprintf(fp, "/%d ", ifa->ifa_prefixlen);
461                 } else {
462                         fprintf(fp, " peer %s/%d ",
463                                 rt_addr_n2a(ifa->ifa_family,
464                                             RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
465                                             RTA_DATA(rta_tb[IFA_ADDRESS]),
466                                             abuf, sizeof(abuf)),
467                                 ifa->ifa_prefixlen);
468                 }
469         }
470
471         if (rta_tb[IFA_BROADCAST]) {
472                 fprintf(fp, "brd %s ",
473                         rt_addr_n2a(ifa->ifa_family,
474                                     RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
475                                     RTA_DATA(rta_tb[IFA_BROADCAST]),
476                                     abuf, sizeof(abuf)));
477         }
478         if (rta_tb[IFA_ANYCAST]) {
479                 fprintf(fp, "any %s ",
480                         rt_addr_n2a(ifa->ifa_family,
481                                     RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
482                                     RTA_DATA(rta_tb[IFA_ANYCAST]),
483                                     abuf, sizeof(abuf)));
484         }
485         fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
486         if (ifa->ifa_flags&IFA_F_SECONDARY) {
487                 ifa->ifa_flags &= ~IFA_F_SECONDARY;
488                 fprintf(fp, "secondary ");
489         }
490         if (ifa->ifa_flags&IFA_F_TENTATIVE) {
491                 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
492                 fprintf(fp, "tentative ");
493         }
494         if (ifa->ifa_flags&IFA_F_DEPRECATED) {
495                 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
496                 deprecated = 1;
497                 fprintf(fp, "deprecated ");
498         }
499         if (ifa->ifa_flags&IFA_F_HOMEADDRESS) {
500                 ifa->ifa_flags &= ~IFA_F_HOMEADDRESS;
501                 fprintf(fp, "home ");
502         }
503         if (ifa->ifa_flags&IFA_F_NODAD) {
504                 ifa->ifa_flags &= ~IFA_F_NODAD;
505                 fprintf(fp, "nodad ");
506         }
507         if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
508                 fprintf(fp, "dynamic ");
509         } else
510                 ifa->ifa_flags &= ~IFA_F_PERMANENT;
511         if (ifa->ifa_flags)
512                 fprintf(fp, "flags %02x ", ifa->ifa_flags);
513         if (rta_tb[IFA_LABEL])
514                 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
515         if (rta_tb[IFA_CACHEINFO]) {
516                 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
517                 char buf[128];
518                 fprintf(fp, "%s", _SL_);
519                 if (ci->ifa_valid == INFINITY_LIFE_TIME)
520                         sprintf(buf, "valid_lft forever");
521                 else
522                         sprintf(buf, "valid_lft %usec", ci->ifa_valid);
523                 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
524                         sprintf(buf+strlen(buf), " preferred_lft forever");
525                 else {
526                         if (deprecated)
527                                 sprintf(buf+strlen(buf), " preferred_lft %dsec",
528                                         ci->ifa_prefered);
529                         else
530                                 sprintf(buf+strlen(buf), " preferred_lft %usec",
531                                         ci->ifa_prefered);
532                 }
533                 fprintf(fp, "       %s", buf);
534         }
535         fprintf(fp, "\n");
536         fflush(fp);
537         return 0;
538 }
539
540
541 struct nlmsg_list
542 {
543         struct nlmsg_list *next;
544         struct nlmsghdr   h;
545 };
546
547 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
548 {
549         for ( ;ainfo ;  ainfo = ainfo->next) {
550                 struct nlmsghdr *n = &ainfo->h;
551                 struct ifaddrmsg *ifa = NLMSG_DATA(n);
552
553                 if (n->nlmsg_type != RTM_NEWADDR)
554                         continue;
555
556                 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
557                         return -1;
558
559                 if (ifa->ifa_index != ifindex ||
560                     (filter.family && filter.family != ifa->ifa_family))
561                         continue;
562
563                 print_addrinfo(NULL, n, fp);
564         }
565         return 0;
566 }
567
568
569 static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
570                        void *arg)
571 {
572         struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
573         struct nlmsg_list *h;
574         struct nlmsg_list **lp;
575
576         h = malloc(n->nlmsg_len+sizeof(void*));
577         if (h == NULL)
578                 return -1;
579
580         memcpy(&h->h, n, n->nlmsg_len);
581         h->next = NULL;
582
583         for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
584         *lp = h;
585
586         ll_remember_index(who, n, NULL);
587         return 0;
588 }
589
590 static int ipaddr_list_or_flush(int argc, char **argv, int flush)
591 {
592         struct nlmsg_list *linfo = NULL;
593         struct nlmsg_list *ainfo = NULL;
594         struct nlmsg_list *l, *n;
595         char *filter_dev = NULL;
596         int no_link = 0;
597
598         ipaddr_reset_filter(oneline);
599         filter.showqueue = 1;
600
601         if (filter.family == AF_UNSPEC)
602                 filter.family = preferred_family;
603
604         if (flush) {
605                 if (argc <= 0) {
606                         fprintf(stderr, "Flush requires arguments.\n");
607                         return -1;
608                 }
609                 if (filter.family == AF_PACKET) {
610                         fprintf(stderr, "Cannot flush link addresses.\n");
611                         return -1;
612                 }
613         }
614
615         while (argc > 0) {
616                 if (strcmp(*argv, "to") == 0) {
617                         NEXT_ARG();
618                         get_prefix(&filter.pfx, *argv, filter.family);
619                         if (filter.family == AF_UNSPEC)
620                                 filter.family = filter.pfx.family;
621                 } else if (strcmp(*argv, "scope") == 0) {
622                         unsigned scope = 0;
623                         NEXT_ARG();
624                         filter.scopemask = -1;
625                         if (rtnl_rtscope_a2n(&scope, *argv)) {
626                                 if (strcmp(*argv, "all") != 0)
627                                         invarg("invalid \"scope\"\n", *argv);
628                                 scope = RT_SCOPE_NOWHERE;
629                                 filter.scopemask = 0;
630                         }
631                         filter.scope = scope;
632                 } else if (strcmp(*argv, "up") == 0) {
633                         filter.up = 1;
634                 } else if (strcmp(*argv, "dynamic") == 0) {
635                         filter.flags &= ~IFA_F_PERMANENT;
636                         filter.flagmask |= IFA_F_PERMANENT;
637                 } else if (strcmp(*argv, "permanent") == 0) {
638                         filter.flags |= IFA_F_PERMANENT;
639                         filter.flagmask |= IFA_F_PERMANENT;
640                 } else if (strcmp(*argv, "secondary") == 0) {
641                         filter.flags |= IFA_F_SECONDARY;
642                         filter.flagmask |= IFA_F_SECONDARY;
643                 } else if (strcmp(*argv, "primary") == 0) {
644                         filter.flags &= ~IFA_F_SECONDARY;
645                         filter.flagmask |= IFA_F_SECONDARY;
646                 } else if (strcmp(*argv, "tentative") == 0) {
647                         filter.flags |= IFA_F_TENTATIVE;
648                         filter.flagmask |= IFA_F_TENTATIVE;
649                 } else if (strcmp(*argv, "deprecated") == 0) {
650                         filter.flags |= IFA_F_DEPRECATED;
651                         filter.flagmask |= IFA_F_DEPRECATED;
652                 } else if (strcmp(*argv, "home") == 0) {
653                         filter.flags |= IFA_F_HOMEADDRESS;
654                         filter.flagmask |= IFA_F_HOMEADDRESS;
655                 } else if (strcmp(*argv, "nodad") == 0) {
656                         filter.flags |= IFA_F_NODAD;
657                         filter.flagmask |= IFA_F_NODAD;
658                 } else if (strcmp(*argv, "label") == 0) {
659                         NEXT_ARG();
660                         filter.label = *argv;
661                 } else {
662                         if (strcmp(*argv, "dev") == 0) {
663                                 NEXT_ARG();
664                         }
665                         if (matches(*argv, "help") == 0)
666                                 usage();
667                         if (filter_dev)
668                                 duparg2("dev", *argv);
669                         filter_dev = *argv;
670                 }
671                 argv++; argc--;
672         }
673
674         if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
675                 perror("Cannot send dump request");
676                 exit(1);
677         }
678
679         if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
680                 fprintf(stderr, "Dump terminated\n");
681                 exit(1);
682         }
683
684         if (filter_dev) {
685                 filter.ifindex = ll_name_to_index(filter_dev);
686                 if (filter.ifindex <= 0) {
687                         fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
688                         return -1;
689                 }
690         }
691
692         if (flush) {
693                 int round = 0;
694                 char flushb[4096-512];
695
696                 filter.flushb = flushb;
697                 filter.flushp = 0;
698                 filter.flushe = sizeof(flushb);
699
700                 while (round < MAX_ROUNDS) {
701                         if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
702                                 perror("Cannot send dump request");
703                                 exit(1);
704                         }
705                         filter.flushed = 0;
706                         if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
707                                 fprintf(stderr, "Flush terminated\n");
708                                 exit(1);
709                         }
710                         if (filter.flushed == 0) {
711                                 if (show_stats) {
712                                         if (round == 0)
713                                                 printf("Nothing to flush.\n");
714                                         else 
715                                                 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
716                                 }
717                                 fflush(stdout);
718                                 return 0;
719                         }
720                         round++;
721                         if (flush_update() < 0)
722                                 return 1;
723
724                         if (show_stats) {
725                                 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
726                                 fflush(stdout);
727                         }
728                 }
729                 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", MAX_ROUNDS); fflush(stderr);
730                 return 1;
731         }
732
733         if (filter.family != AF_PACKET) {
734                 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
735                         perror("Cannot send dump request");
736                         exit(1);
737                 }
738
739                 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
740                         fprintf(stderr, "Dump terminated\n");
741                         exit(1);
742                 }
743         }
744
745
746         if (filter.family && filter.family != AF_PACKET) {
747                 struct nlmsg_list **lp;
748                 lp=&linfo;
749
750                 if (filter.oneline)
751                         no_link = 1;
752
753                 while ((l=*lp)!=NULL) {
754                         int ok = 0;
755                         struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
756                         struct nlmsg_list *a;
757
758                         for (a=ainfo; a; a=a->next) {
759                                 struct nlmsghdr *n = &a->h;
760                                 struct ifaddrmsg *ifa = NLMSG_DATA(n);
761
762                                 if (ifa->ifa_index != ifi->ifi_index ||
763                                     (filter.family && filter.family != ifa->ifa_family))
764                                         continue;
765                                 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
766                                         continue;
767                                 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
768                                         continue;
769                                 if (filter.pfx.family || filter.label) {
770                                         struct rtattr *tb[IFA_MAX+1];
771                                         parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
772                                         if (!tb[IFA_LOCAL])
773                                                 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
774
775                                         if (filter.pfx.family && tb[IFA_LOCAL]) {
776                                                 inet_prefix dst;
777                                                 memset(&dst, 0, sizeof(dst));
778                                                 dst.family = ifa->ifa_family;
779                                                 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
780                                                 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
781                                                         continue;
782                                         }
783                                         if (filter.label) {
784                                                 SPRINT_BUF(b1);
785                                                 const char *label;
786                                                 if (tb[IFA_LABEL])
787                                                         label = RTA_DATA(tb[IFA_LABEL]);
788                                                 else
789                                                         label = ll_idx_n2a(ifa->ifa_index, b1);
790                                                 if (fnmatch(filter.label, label, 0) != 0)
791                                                         continue;
792                                         }
793                                 }
794
795                                 ok = 1;
796                                 break;
797                         }
798                         if (!ok)
799                                 *lp = l->next;
800                         else
801                                 lp = &l->next;
802                 }
803         }
804
805         for (l=linfo; l; l = n) {
806                 n = l->next;
807                 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
808                         struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
809                         if (filter.family != AF_PACKET)
810                                 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
811                 }
812                 fflush(stdout);
813                 free(l);
814         }
815
816         return 0;
817 }
818
819 int ipaddr_list_link(int argc, char **argv)
820 {
821         preferred_family = AF_PACKET;
822         do_link = 1;
823         return ipaddr_list_or_flush(argc, argv, 0);
824 }
825
826 void ipaddr_reset_filter(int oneline)
827 {
828         memset(&filter, 0, sizeof(filter));
829         filter.oneline = oneline;
830 }
831
832 static int default_scope(inet_prefix *lcl)
833 {
834         if (lcl->family == AF_INET) {
835                 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
836                         return RT_SCOPE_HOST;
837         }
838         return 0;
839 }
840
841 static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
842 {
843         struct {
844                 struct nlmsghdr         n;
845                 struct ifaddrmsg        ifa;
846                 char                    buf[256];
847         } req;
848         char  *d = NULL;
849         char  *l = NULL;
850         char  *lcl_arg = NULL;
851         char  *valid_lftp = NULL;
852         char  *preferred_lftp = NULL;
853         inet_prefix lcl;
854         inet_prefix peer;
855         int local_len = 0;
856         int peer_len = 0;
857         int brd_len = 0;
858         int any_len = 0;
859         int scoped = 0;
860         __u32 preferred_lft = INFINITY_LIFE_TIME;
861         __u32 valid_lft = INFINITY_LIFE_TIME;
862         struct ifa_cacheinfo cinfo;
863
864         memset(&req, 0, sizeof(req));
865
866         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
867         req.n.nlmsg_flags = NLM_F_REQUEST | flags;
868         req.n.nlmsg_type = cmd;
869         req.ifa.ifa_family = preferred_family;
870
871         while (argc > 0) {
872                 if (strcmp(*argv, "peer") == 0 ||
873                     strcmp(*argv, "remote") == 0) {
874                         NEXT_ARG();
875
876                         if (peer_len)
877                                 duparg("peer", *argv);
878                         get_prefix(&peer, *argv, req.ifa.ifa_family);
879                         peer_len = peer.bytelen;
880                         if (req.ifa.ifa_family == AF_UNSPEC)
881                                 req.ifa.ifa_family = peer.family;
882                         addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
883                         req.ifa.ifa_prefixlen = peer.bitlen;
884                 } else if (matches(*argv, "broadcast") == 0 ||
885                            strcmp(*argv, "brd") == 0) {
886                         inet_prefix addr;
887                         NEXT_ARG();
888                         if (brd_len)
889                                 duparg("broadcast", *argv);
890                         if (strcmp(*argv, "+") == 0)
891                                 brd_len = -1;
892                         else if (strcmp(*argv, "-") == 0)
893                                 brd_len = -2;
894                         else {
895                                 get_addr(&addr, *argv, req.ifa.ifa_family);
896                                 if (req.ifa.ifa_family == AF_UNSPEC)
897                                         req.ifa.ifa_family = addr.family;
898                                 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
899                                 brd_len = addr.bytelen;
900                         }
901                 } else if (strcmp(*argv, "anycast") == 0) {
902                         inet_prefix addr;
903                         NEXT_ARG();
904                         if (any_len)
905                                 duparg("anycast", *argv);
906                         get_addr(&addr, *argv, req.ifa.ifa_family);
907                         if (req.ifa.ifa_family == AF_UNSPEC)
908                                 req.ifa.ifa_family = addr.family;
909                         addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
910                         any_len = addr.bytelen;
911                 } else if (strcmp(*argv, "scope") == 0) {
912                         unsigned scope = 0;
913                         NEXT_ARG();
914                         if (rtnl_rtscope_a2n(&scope, *argv))
915                                 invarg(*argv, "invalid scope value.");
916                         req.ifa.ifa_scope = scope;
917                         scoped = 1;
918                 } else if (strcmp(*argv, "dev") == 0) {
919                         NEXT_ARG();
920                         d = *argv;
921                 } else if (strcmp(*argv, "label") == 0) {
922                         NEXT_ARG();
923                         l = *argv;
924                         addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
925                 } else if (matches(*argv, "valid_lft") == 0) {
926                         if (valid_lftp)
927                                 duparg("valid_lft", *argv);
928                         NEXT_ARG();
929                         valid_lftp = *argv;
930                         if (set_lifetime(&valid_lft, *argv))
931                                 invarg("valid_lft value", *argv);
932                 } else if (matches(*argv, "preferred_lft") == 0) {
933                         if (preferred_lftp)
934                                 duparg("preferred_lft", *argv);
935                         NEXT_ARG();
936                         preferred_lftp = *argv;
937                         if (set_lifetime(&preferred_lft, *argv))
938                                 invarg("preferred_lft value", *argv);
939                 } else if (strcmp(*argv, "home") == 0) {
940                         req.ifa.ifa_flags |= IFA_F_HOMEADDRESS;
941                 } else if (strcmp(*argv, "nodad") == 0) {
942                         req.ifa.ifa_flags |= IFA_F_NODAD;
943                 } else {
944                         if (strcmp(*argv, "local") == 0) {
945                                 NEXT_ARG();
946                         }
947                         if (matches(*argv, "help") == 0)
948                                 usage();
949                         if (local_len)
950                                 duparg2("local", *argv);
951                         lcl_arg = *argv;
952                         get_prefix(&lcl, *argv, req.ifa.ifa_family);
953                         if (req.ifa.ifa_family == AF_UNSPEC)
954                                 req.ifa.ifa_family = lcl.family;
955                         addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
956                         local_len = lcl.bytelen;
957                 }
958                 argc--; argv++;
959         }
960         if (d == NULL) {
961                 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
962                 return -1;
963         }
964         if (l && matches(d, l) != 0) {
965                 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
966                 exit(1);
967         }
968
969         if (peer_len == 0 && local_len) {
970                 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
971                         fprintf(stderr,
972                             "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
973                             "         Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
974                             "         This special behaviour is likely to disappear in further releases,\n" \
975                             "         fix your scripts!\n", lcl_arg, local_len*8);
976                 } else {
977                         peer = lcl;
978                         addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
979                 }
980         }
981         if (req.ifa.ifa_prefixlen == 0)
982                 req.ifa.ifa_prefixlen = lcl.bitlen;
983
984         if (brd_len < 0 && cmd != RTM_DELADDR) {
985                 inet_prefix brd;
986                 int i;
987                 if (req.ifa.ifa_family != AF_INET) {
988                         fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
989                         return -1;
990                 }
991                 brd = peer;
992                 if (brd.bitlen <= 30) {
993                         for (i=31; i>=brd.bitlen; i--) {
994                                 if (brd_len == -1)
995                                         brd.data[0] |= htonl(1<<(31-i));
996                                 else
997                                         brd.data[0] &= ~htonl(1<<(31-i));
998                         }
999                         addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
1000                         brd_len = brd.bytelen;
1001                 }
1002         }
1003         if (!scoped && cmd != RTM_DELADDR)
1004                 req.ifa.ifa_scope = default_scope(&lcl);
1005
1006         ll_init_map(&rth);
1007
1008         if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
1009                 fprintf(stderr, "Cannot find device \"%s\"\n", d);
1010                 return -1;
1011         }
1012
1013         if (valid_lftp || preferred_lftp) {
1014                 if (!valid_lft) {
1015                         fprintf(stderr, "valid_lft is zero\n");
1016                         return -1;
1017                 }
1018                 if (valid_lft < preferred_lft) {
1019                         fprintf(stderr, "preferred_lft is greater than valid_lft\n");
1020                         return -1;
1021                 }
1022
1023                 memset(&cinfo, 0, sizeof(cinfo));
1024                 cinfo.ifa_prefered = preferred_lft;
1025                 cinfo.ifa_valid = valid_lft;
1026                 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
1027                           sizeof(cinfo));
1028         }
1029
1030         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
1031                 exit(2);
1032
1033         return 0;
1034 }
1035
1036 int do_ipaddr(int argc, char **argv)
1037 {
1038         if (argc < 1)
1039                 return ipaddr_list_or_flush(0, NULL, 0);
1040         if (matches(*argv, "add") == 0)
1041                 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
1042         if (matches(*argv, "change") == 0 ||
1043                 strcmp(*argv, "chg") == 0)
1044                 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
1045         if (matches(*argv, "replace") == 0)
1046                 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
1047         if (matches(*argv, "delete") == 0)
1048                 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
1049         if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1050             || matches(*argv, "lst") == 0)
1051                 return ipaddr_list_or_flush(argc-1, argv+1, 0);
1052         if (matches(*argv, "flush") == 0)
1053                 return ipaddr_list_or_flush(argc-1, argv+1, 1);
1054         if (matches(*argv, "help") == 0)
1055                 usage();
1056         fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv);
1057         exit(-1);
1058 }
1059