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