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