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