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