blob: 297506d40a84d33506989f234bbb7961341ddf3f [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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 *
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000011 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -080017#include <inttypes.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000018#include <fcntl.h>
19#include <sys/ioctl.h>
20#include <sys/socket.h>
21#include <sys/ioctl.h>
Strake5bd9dd42012-12-23 08:46:04 -050022#include <errno.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000023#include <netinet/in.h>
24#include <arpa/inet.h>
25#include <string.h>
26#include <fnmatch.h>
27
osdl.org!shemmingere5779fb2004-06-09 22:56:28 +000028#include <linux/netdevice.h>
29#include <linux/if_arp.h>
30#include <linux/sockios.h>
31
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000032#include "rt_names.h"
33#include "utils.h"
34#include "ll_map.h"
35#include "ip_common.h"
36
Pavel Emelyanov81824ac2012-09-11 19:47:00 +040037enum {
38 IPADD_LIST,
39 IPADD_FLUSH,
40 IPADD_SAVE,
41};
Daniel Silverstone7b3d3662007-10-19 13:32:24 +020042
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000043static struct
44{
45 int ifindex;
46 int family;
47 int oneline;
48 int showqueue;
49 inet_prefix pfx;
50 int scope, scopemask;
51 int flags, flagmask;
52 int up;
53 char *label;
54 int flushed;
55 char *flushb;
56 int flushp;
57 int flushe;
Vlad Dogaruf960c922011-02-02 20:23:40 +020058 int group;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000059} filter;
60
61static int do_link;
62
63static void usage(void) __attribute__((noreturn));
64
65static void usage(void)
66{
67 if (do_link) {
68 iplink_usage();
69 }
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +090070 fprintf(stderr, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n");
Brian Haleya1f27792009-12-03 10:39:36 +000071 fprintf(stderr, " [ CONFFLAG-LIST ]\n");
Masahide NAKAMURA35546df2006-11-24 12:26:55 +090072 fprintf(stderr, " ip addr del IFADDR dev STRING\n");
Pavel Emelyanov81824ac2012-09-11 19:47:00 +040073 fprintf(stderr, " ip addr {show|save|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000074 fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n");
Pavel Emelyanov81824ac2012-09-11 19:47:00 +040075 fprintf(stderr, " ip addr {showdump|restore}\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000076 fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n");
77 fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n");
78 fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n");
79 fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
80 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
81 fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n");
Brian Haleya1b9ffc2009-09-14 17:01:43 -040082 fprintf(stderr, " tentative | deprecated | dadfailed | temporary |\n");
Brian Haleya1f27792009-12-03 10:39:36 +000083 fprintf(stderr, " CONFFLAG-LIST ]\n");
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +090084 fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
85 fprintf(stderr, "CONFFLAG := [ home | nodad ]\n");
Masahide NAKAMURA35546df2006-11-24 12:26:55 +090086 fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
87 fprintf(stderr, "LFT := forever | SECONDS\n");
88
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000089 exit(-1);
90}
91
92void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
93{
94 fprintf(fp, "<");
net[shemminger]!shemminger73b49e92005-03-14 18:47:38 +000095 if (flags & IFF_UP && !(flags & IFF_RUNNING))
96 fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000097 flags &= ~IFF_RUNNING;
98#define _PF(f) if (flags&IFF_##f) { \
99 flags &= ~IFF_##f ; \
100 fprintf(fp, #f "%s", flags ? "," : ""); }
101 _PF(LOOPBACK);
102 _PF(BROADCAST);
103 _PF(POINTOPOINT);
104 _PF(MULTICAST);
105 _PF(NOARP);
106 _PF(ALLMULTI);
107 _PF(PROMISC);
108 _PF(MASTER);
109 _PF(SLAVE);
110 _PF(DEBUG);
111 _PF(DYNAMIC);
112 _PF(AUTOMEDIA);
113 _PF(PORTSEL);
114 _PF(NOTRAILERS);
115 _PF(UP);
Thomas Grafdcb283c2007-06-19 16:40:40 -0700116 _PF(LOWER_UP);
117 _PF(DORMANT);
Oliver Hartkopp98f9a1d2009-03-27 11:21:29 -0700118 _PF(ECHO);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000119#undef _PF
120 if (flags)
121 fprintf(fp, "%x", flags);
122 if (mdown)
123 fprintf(fp, ",M-DOWN");
124 fprintf(fp, "> ");
125}
126
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700127static const char *oper_states[] = {
128 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
129 "TESTING", "DORMANT", "UP"
130};
131
132static void print_operstate(FILE *f, __u8 state)
133{
134 if (state >= sizeof(oper_states)/sizeof(oper_states[0]))
135 fprintf(f, "state %#x ", state);
136 else
137 fprintf(f, "state %s ", oper_states[state]);
138}
139
Stephen Hemminger4f2fdd42012-04-05 15:08:57 -0700140int get_operstate(const char *name)
141{
142 int i;
143
144 for (i = 0; i < sizeof(oper_states)/sizeof(oper_states[0]); i++)
145 if (strcasecmp(name, oper_states[i]) == 0)
146 return i;
147 return -1;
148}
149
Eric Dumazetf78e3162009-10-22 18:13:21 +0000150static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1])
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000151{
Eric Dumazetf78e3162009-10-22 18:13:21 +0000152 int qlen;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000153
Eric Dumazetf78e3162009-10-22 18:13:21 +0000154 if (tb[IFLA_TXQLEN])
155 qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]);
156 else {
157 struct ifreq ifr;
158 int s = socket(AF_INET, SOCK_STREAM, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000159
Eric Dumazetf78e3162009-10-22 18:13:21 +0000160 if (s < 0)
161 return;
162
163 memset(&ifr, 0, sizeof(ifr));
Stephen Hemmingerff247462012-04-10 08:47:55 -0700164 strcpy(ifr.ifr_name, rta_getattr_str(tb[IFLA_IFNAME]));
Eric Dumazetf78e3162009-10-22 18:13:21 +0000165 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
Nicolas Dichteld3603512013-01-29 08:46:42 -0800166 fprintf(f, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno));
Eric Dumazetf78e3162009-10-22 18:13:21 +0000167 close(s);
168 return;
169 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000170 close(s);
Eric Dumazetf78e3162009-10-22 18:13:21 +0000171 qlen = ifr.ifr_qlen;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000172 }
Eric Dumazetf78e3162009-10-22 18:13:21 +0000173 if (qlen)
174 fprintf(f, "qlen %d", qlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000175}
176
Stephen Hemminger82499282012-03-19 17:24:43 -0700177static const char *link_modes[] = {
178 "DEFAULT", "DORMANT"
179};
180
181static void print_linkmode(FILE *f, struct rtattr *tb)
182{
183 unsigned int mode = rta_getattr_u8(tb);
184
185 if (mode >= sizeof(link_modes) / sizeof(link_modes[0]))
186 fprintf(f, "mode %d ", mode);
187 else
188 fprintf(f, "mode %s ", link_modes[mode]);
189}
190
Patrick McHardy1d934832007-08-22 10:49:01 -0700191static void print_linktype(FILE *fp, struct rtattr *tb)
192{
193 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
194 struct link_util *lu;
195 char *kind;
196
197 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
198
199 if (!linkinfo[IFLA_INFO_KIND])
200 return;
201 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
202
203 fprintf(fp, "%s", _SL_);
204 fprintf(fp, " %s ", kind);
205
206 lu = get_link_kind(kind);
207 if (!lu || !lu->print_opt)
208 return;
209
210 if (1) {
211 struct rtattr *attr[lu->maxattr+1], **data = NULL;
212
213 if (linkinfo[IFLA_INFO_DATA]) {
214 parse_rtattr_nested(attr, lu->maxattr,
215 linkinfo[IFLA_INFO_DATA]);
216 data = attr;
217 }
218 lu->print_opt(lu, fp, data);
219
220 if (linkinfo[IFLA_INFO_XSTATS] && show_stats &&
221 lu->print_xstats)
222 lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]);
223 }
224}
225
Chris Wright3fd86632010-05-18 00:57:00 -0700226static void print_vfinfo(FILE *fp, struct rtattr *vfinfo)
227{
228 struct ifla_vf_mac *vf_mac;
229 struct ifla_vf_vlan *vf_vlan;
230 struct ifla_vf_tx_rate *vf_tx_rate;
Greg Rose7b8179c2011-10-13 20:31:32 +0000231 struct ifla_vf_spoofchk *vf_spoofchk;
Chris Wright3fd86632010-05-18 00:57:00 -0700232 struct rtattr *vf[IFLA_VF_MAX+1];
Greg Rose7b8179c2011-10-13 20:31:32 +0000233 struct rtattr *tmp;
Chris Wright3fd86632010-05-18 00:57:00 -0700234 SPRINT_BUF(b1);
235
236 if (vfinfo->rta_type != IFLA_VF_INFO) {
237 fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type);
238 return;
239 }
240
241 parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo);
242
243 vf_mac = RTA_DATA(vf[IFLA_VF_MAC]);
244 vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
245 vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]);
246
Greg Rose7b8179c2011-10-13 20:31:32 +0000247 /* Check if the spoof checking vf info type is supported by
248 * this kernel.
249 */
250 tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] +
251 vf[IFLA_VF_TX_RATE]->rta_len);
252
253 if (tmp->rta_type != IFLA_VF_SPOOFCHK)
254 vf_spoofchk = NULL;
255 else
256 vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
257
Chris Wright3fd86632010-05-18 00:57:00 -0700258 fprintf(fp, "\n vf %d MAC %s", vf_mac->vf,
259 ll_addr_n2a((unsigned char *)&vf_mac->mac,
260 ETH_ALEN, 0, b1, sizeof(b1)));
261 if (vf_vlan->vlan)
262 fprintf(fp, ", vlan %d", vf_vlan->vlan);
263 if (vf_vlan->qos)
264 fprintf(fp, ", qos %d", vf_vlan->qos);
265 if (vf_tx_rate->rate)
266 fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate);
Greg Rose7b8179c2011-10-13 20:31:32 +0000267 if (vf_spoofchk && vf_spoofchk->setting != -1) {
268 if (vf_spoofchk->setting)
269 fprintf(fp, ", spoof checking on");
270 else
271 fprintf(fp, ", spoof checking off");
272 }
Chris Wright3fd86632010-05-18 00:57:00 -0700273}
274
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800275static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s) {
276 fprintf(fp, "%s", _SL_);
277 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
278 s->rx_compressed ? "compressed" : "", _SL_);
279 fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
280 (uint64_t)s->rx_bytes,
281 (uint64_t)s->rx_packets,
282 (uint64_t)s->rx_errors,
283 (uint64_t)s->rx_dropped,
284 (uint64_t)s->rx_over_errors,
285 (uint64_t)s->multicast);
286 if (s->rx_compressed)
287 fprintf(fp, " %-7"PRIu64"",
288 (uint64_t)s->rx_compressed);
289 if (show_stats > 1) {
290 fprintf(fp, "%s", _SL_);
291 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
292 fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
293 (uint64_t)s->rx_length_errors,
294 (uint64_t)s->rx_crc_errors,
295 (uint64_t)s->rx_frame_errors,
296 (uint64_t)s->rx_fifo_errors,
297 (uint64_t)s->rx_missed_errors);
298 }
299 fprintf(fp, "%s", _SL_);
300 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
301 (uint64_t)s->tx_compressed ? "compressed" : "", _SL_);
302 fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
303 (uint64_t)s->tx_bytes,
304 (uint64_t)s->tx_packets,
305 (uint64_t)s->tx_errors,
306 (uint64_t)s->tx_dropped,
307 (uint64_t)s->tx_carrier_errors,
308 (uint64_t)s->collisions);
309 if (s->tx_compressed)
310 fprintf(fp, " %-7"PRIu64"",
311 (uint64_t)s->tx_compressed);
312 if (show_stats > 1) {
313 fprintf(fp, "%s", _SL_);
314 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
315 fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
316 (uint64_t)s->tx_aborted_errors,
317 (uint64_t)s->tx_fifo_errors,
318 (uint64_t)s->tx_window_errors,
319 (uint64_t)s->tx_heartbeat_errors);
320 }
321}
322
323static void print_link_stats(FILE *fp, const struct rtnl_link_stats *s)
324{
325 fprintf(fp, "%s", _SL_);
326 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
327 s->rx_compressed ? "compressed" : "", _SL_);
328 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
329 s->rx_bytes, s->rx_packets, s->rx_errors,
330 s->rx_dropped, s->rx_over_errors,
331 s->multicast
332 );
333 if (s->rx_compressed)
334 fprintf(fp, " %-7u", s->rx_compressed);
335 if (show_stats > 1) {
336 fprintf(fp, "%s", _SL_);
337 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
338 fprintf(fp, " %-7u %-7u %-7u %-7u %-7u",
339 s->rx_length_errors,
340 s->rx_crc_errors,
341 s->rx_frame_errors,
342 s->rx_fifo_errors,
343 s->rx_missed_errors
344 );
345 }
346 fprintf(fp, "%s", _SL_);
347 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
348 s->tx_compressed ? "compressed" : "", _SL_);
349 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
350 s->tx_bytes, s->tx_packets, s->tx_errors,
351 s->tx_dropped, s->tx_carrier_errors, s->collisions);
352 if (s->tx_compressed)
353 fprintf(fp, " %-7u", s->tx_compressed);
354 if (show_stats > 1) {
355 fprintf(fp, "%s", _SL_);
356 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
357 fprintf(fp, " %-7u %-7u %-7u %-7u",
358 s->tx_aborted_errors,
359 s->tx_fifo_errors,
360 s->tx_window_errors,
361 s->tx_heartbeat_errors
362 );
363 }
364}
365
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800366int print_linkinfo(const struct sockaddr_nl *who,
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000367 struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000368{
369 FILE *fp = (FILE*)arg;
370 struct ifinfomsg *ifi = NLMSG_DATA(n);
371 struct rtattr * tb[IFLA_MAX+1];
372 int len = n->nlmsg_len;
373 unsigned m_flag = 0;
374
375 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
376 return 0;
377
378 len -= NLMSG_LENGTH(sizeof(*ifi));
379 if (len < 0)
380 return -1;
381
382 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
383 return 0;
384 if (filter.up && !(ifi->ifi_flags&IFF_UP))
385 return 0;
386
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000387 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
388 if (tb[IFLA_IFNAME] == NULL) {
jamal4cd23bd2008-08-08 10:06:17 -0400389 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000390 }
391 if (filter.label &&
392 (!filter.family || filter.family == AF_PACKET) &&
393 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
394 return 0;
395
Vlad Dogaruf960c922011-02-02 20:23:40 +0200396 if (tb[IFLA_GROUP]) {
397 int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
398 if (group != filter.group)
399 return -1;
400 }
401
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000402 if (n->nlmsg_type == RTM_DELLINK)
403 fprintf(fp, "Deleted ");
404
405 fprintf(fp, "%d: %s", ifi->ifi_index,
Stephen Hemmingerff247462012-04-10 08:47:55 -0700406 tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000407
408 if (tb[IFLA_LINK]) {
409 SPRINT_BUF(b1);
410 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
411 if (iflink == 0)
412 fprintf(fp, "@NONE: ");
413 else {
414 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
415 m_flag = ll_index_to_flags(iflink);
416 m_flag = !(m_flag & IFF_UP);
417 }
418 } else {
419 fprintf(fp, ": ");
420 }
421 print_link_flags(fp, ifi->ifi_flags, m_flag);
422
423 if (tb[IFLA_MTU])
424 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
425 if (tb[IFLA_QDISC])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700426 fprintf(fp, "qdisc %s ", rta_getattr_str(tb[IFLA_QDISC]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000427 if (tb[IFLA_MASTER]) {
428 SPRINT_BUF(b1);
429 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
430 }
Stephen Hemminger82499282012-03-19 17:24:43 -0700431
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700432 if (tb[IFLA_OPERSTATE])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700433 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
Stephen Hemminger82499282012-03-19 17:24:43 -0700434
435 if (do_link && tb[IFLA_LINKMODE])
436 print_linkmode(fp, tb[IFLA_LINKMODE]);
437
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000438 if (filter.showqueue)
Eric Dumazetf78e3162009-10-22 18:13:21 +0000439 print_queuelen(fp, tb);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800440
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000441 if (!filter.family || filter.family == AF_PACKET) {
442 SPRINT_BUF(b1);
443 fprintf(fp, "%s", _SL_);
444 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
445
446 if (tb[IFLA_ADDRESS]) {
447 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
448 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
449 ifi->ifi_type,
450 b1, sizeof(b1)));
451 }
452 if (tb[IFLA_BROADCAST]) {
453 if (ifi->ifi_flags&IFF_POINTOPOINT)
454 fprintf(fp, " peer ");
455 else
456 fprintf(fp, " brd ");
457 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
458 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
459 ifi->ifi_type,
460 b1, sizeof(b1)));
461 }
462 }
Patrick McHardy1d934832007-08-22 10:49:01 -0700463
Stephen Hemminger1cb6a112013-02-05 08:16:28 -0800464 if (do_link && tb[IFLA_PROMISCUITY] && show_details)
465 fprintf(fp, " promiscuity %u ",
466 *(int*)RTA_DATA(tb[IFLA_PROMISCUITY]));
467
Patrick McHardy1d934832007-08-22 10:49:01 -0700468 if (do_link && tb[IFLA_LINKINFO] && show_details)
469 print_linktype(fp, tb[IFLA_LINKINFO]);
470
Stephen Hemmingerace9c962009-03-23 10:46:47 -0700471 if (do_link && tb[IFLA_IFALIAS])
472 fprintf(fp,"\n alias %s",
Stephen Hemmingerff247462012-04-10 08:47:55 -0700473 rta_getattr_str(tb[IFLA_IFALIAS]));
Stephen Hemmingerace9c962009-03-23 10:46:47 -0700474
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800475 if (do_link && show_stats) {
476 if (tb[IFLA_STATS64])
477 print_link_stats64(fp, RTA_DATA(tb[IFLA_STATS64]));
478 else if (tb[IFLA_STATS])
479 print_link_stats(fp, RTA_DATA(tb[IFLA_STATS]));
Jan Engelhardt8864ac92010-03-11 10:00:34 +0000480 }
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800481
Chris Wright3fd86632010-05-18 00:57:00 -0700482 if (do_link && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) {
483 struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST];
484 int rem = RTA_PAYLOAD(vflist);
485 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
486 print_vfinfo(fp, i);
Williams, Mitch Aae7229d2010-02-10 01:47:08 +0000487 }
Chris Wright3fd86632010-05-18 00:57:00 -0700488
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000489 fprintf(fp, "\n");
490 fflush(fp);
491 return 0;
492}
493
494static int flush_update(void)
495{
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800496 if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
Stephen Hemminger1fb0a992008-01-26 11:08:31 -0800497 perror("Failed to send flush request");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000498 return -1;
499 }
500 filter.flushp = 0;
501 return 0;
502}
503
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900504static int set_lifetime(unsigned int *lifetime, char *argv)
505{
506 if (strcmp(argv, "forever") == 0)
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900507 *lifetime = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900508 else if (get_u32(lifetime, argv, 0))
509 return -1;
510
511 return 0;
512}
513
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800514int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000515 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000516{
517 FILE *fp = (FILE*)arg;
518 struct ifaddrmsg *ifa = NLMSG_DATA(n);
519 int len = n->nlmsg_len;
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800520 int deprecated = 0;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700521 /* Use local copy of ifa_flags to not interfere with filtering code */
522 unsigned int ifa_flags;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000523 struct rtattr * rta_tb[IFA_MAX+1];
524 char abuf[256];
525 SPRINT_BUF(b1);
526
527 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
528 return 0;
529 len -= NLMSG_LENGTH(sizeof(*ifa));
530 if (len < 0) {
531 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
532 return -1;
533 }
534
535 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
536 return 0;
537
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000538 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
539
540 if (!rta_tb[IFA_LOCAL])
541 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
542 if (!rta_tb[IFA_ADDRESS])
543 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
544
545 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
546 return 0;
547 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
548 return 0;
549 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
550 return 0;
551 if (filter.label) {
552 SPRINT_BUF(b1);
553 const char *label;
554 if (rta_tb[IFA_LABEL])
555 label = RTA_DATA(rta_tb[IFA_LABEL]);
556 else
557 label = ll_idx_n2a(ifa->ifa_index, b1);
558 if (fnmatch(filter.label, label, 0) != 0)
559 return 0;
560 }
561 if (filter.pfx.family) {
562 if (rta_tb[IFA_LOCAL]) {
563 inet_prefix dst;
564 memset(&dst, 0, sizeof(dst));
565 dst.family = ifa->ifa_family;
566 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
567 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
568 return 0;
569 }
570 }
571
net[shemminger]!shemminger3eb17312005-02-07 18:28:31 +0000572 if (filter.family && filter.family != ifa->ifa_family)
573 return 0;
574
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000575 if (filter.flushb) {
576 struct nlmsghdr *fn;
577 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
578 if (flush_update())
579 return -1;
580 }
581 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
582 memcpy(fn, n, n->nlmsg_len);
583 fn->nlmsg_type = RTM_DELADDR;
584 fn->nlmsg_flags = NLM_F_REQUEST;
shemminger351efcd2005-09-01 19:21:50 +0000585 fn->nlmsg_seq = ++rth.seq;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000586 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
587 filter.flushed++;
588 if (show_stats < 2)
589 return 0;
590 }
591
592 if (n->nlmsg_type == RTM_DELADDR)
593 fprintf(fp, "Deleted ");
594
595 if (filter.oneline || filter.flushb)
596 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
597 if (ifa->ifa_family == AF_INET)
598 fprintf(fp, " inet ");
599 else if (ifa->ifa_family == AF_INET6)
600 fprintf(fp, " inet6 ");
601 else if (ifa->ifa_family == AF_DECnet)
602 fprintf(fp, " dnet ");
603 else if (ifa->ifa_family == AF_IPX)
604 fprintf(fp, " ipx ");
605 else
606 fprintf(fp, " family %d ", ifa->ifa_family);
607
608 if (rta_tb[IFA_LOCAL]) {
609 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
610 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
611 RTA_DATA(rta_tb[IFA_LOCAL]),
612 abuf, sizeof(abuf)));
613
614 if (rta_tb[IFA_ADDRESS] == NULL ||
615 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
616 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
617 } else {
618 fprintf(fp, " peer %s/%d ",
619 rt_addr_n2a(ifa->ifa_family,
620 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
621 RTA_DATA(rta_tb[IFA_ADDRESS]),
622 abuf, sizeof(abuf)),
623 ifa->ifa_prefixlen);
624 }
625 }
626
627 if (rta_tb[IFA_BROADCAST]) {
628 fprintf(fp, "brd %s ",
629 rt_addr_n2a(ifa->ifa_family,
630 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
631 RTA_DATA(rta_tb[IFA_BROADCAST]),
632 abuf, sizeof(abuf)));
633 }
634 if (rta_tb[IFA_ANYCAST]) {
635 fprintf(fp, "any %s ",
636 rt_addr_n2a(ifa->ifa_family,
637 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
638 RTA_DATA(rta_tb[IFA_ANYCAST]),
639 abuf, sizeof(abuf)));
640 }
641 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700642 ifa_flags = ifa->ifa_flags;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000643 if (ifa->ifa_flags&IFA_F_SECONDARY) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700644 ifa_flags &= ~IFA_F_SECONDARY;
Brian Haleya1b9ffc2009-09-14 17:01:43 -0400645 if (ifa->ifa_family == AF_INET6)
646 fprintf(fp, "temporary ");
647 else
648 fprintf(fp, "secondary ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000649 }
650 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700651 ifa_flags &= ~IFA_F_TENTATIVE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000652 fprintf(fp, "tentative ");
653 }
654 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700655 ifa_flags &= ~IFA_F_DEPRECATED;
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800656 deprecated = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000657 fprintf(fp, "deprecated ");
658 }
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900659 if (ifa->ifa_flags&IFA_F_HOMEADDRESS) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700660 ifa_flags &= ~IFA_F_HOMEADDRESS;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900661 fprintf(fp, "home ");
662 }
663 if (ifa->ifa_flags&IFA_F_NODAD) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700664 ifa_flags &= ~IFA_F_NODAD;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900665 fprintf(fp, "nodad ");
666 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000667 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
668 fprintf(fp, "dynamic ");
669 } else
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700670 ifa_flags &= ~IFA_F_PERMANENT;
Brian Haleyf4af8512009-12-01 15:58:44 -0800671 if (ifa->ifa_flags&IFA_F_DADFAILED) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700672 ifa_flags &= ~IFA_F_DADFAILED;
Brian Haleyf4af8512009-12-01 15:58:44 -0800673 fprintf(fp, "dadfailed ");
674 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700675 if (ifa_flags)
676 fprintf(fp, "flags %02x ", ifa_flags);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000677 if (rta_tb[IFA_LABEL])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700678 fprintf(fp, "%s", rta_getattr_str(rta_tb[IFA_LABEL]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000679 if (rta_tb[IFA_CACHEINFO]) {
680 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000681 fprintf(fp, "%s", _SL_);
Andreas Schwabf66efad2010-11-05 23:26:29 +0000682 fprintf(fp, " valid_lft ");
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900683 if (ci->ifa_valid == INFINITY_LIFE_TIME)
Andreas Schwabf66efad2010-11-05 23:26:29 +0000684 fprintf(fp, "forever");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000685 else
Andreas Schwabf66efad2010-11-05 23:26:29 +0000686 fprintf(fp, "%usec", ci->ifa_valid);
687 fprintf(fp, " preferred_lft ");
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900688 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
Andreas Schwabf66efad2010-11-05 23:26:29 +0000689 fprintf(fp, "forever");
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800690 else {
691 if (deprecated)
Andreas Schwabf66efad2010-11-05 23:26:29 +0000692 fprintf(fp, "%dsec", ci->ifa_prefered);
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800693 else
Andreas Schwabf66efad2010-11-05 23:26:29 +0000694 fprintf(fp, "%usec", ci->ifa_prefered);
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800695 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000696 }
697 fprintf(fp, "\n");
698 fflush(fp);
699 return 0;
700}
701
Simon Hormanb49240e2009-12-03 12:08:27 +1100702int print_addrinfo_primary(const struct sockaddr_nl *who, struct nlmsghdr *n,
703 void *arg)
704{
705 struct ifaddrmsg *ifa = NLMSG_DATA(n);
706
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700707 if (ifa->ifa_flags & IFA_F_SECONDARY)
Simon Hormanb49240e2009-12-03 12:08:27 +1100708 return 0;
709
710 return print_addrinfo(who, n, arg);
711}
712
713int print_addrinfo_secondary(const struct sockaddr_nl *who, struct nlmsghdr *n,
714 void *arg)
715{
716 struct ifaddrmsg *ifa = NLMSG_DATA(n);
717
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700718 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
Simon Hormanb49240e2009-12-03 12:08:27 +1100719 return 0;
720
721 return print_addrinfo(who, n, arg);
722}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000723
724struct nlmsg_list
725{
726 struct nlmsg_list *next;
727 struct nlmsghdr h;
728};
729
Eric Dumazet62e2e542012-06-09 13:55:55 +0200730struct nlmsg_chain
731{
732 struct nlmsg_list *head;
733 struct nlmsg_list *tail;
734};
735
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700736static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000737{
738 for ( ;ainfo ; ainfo = ainfo->next) {
739 struct nlmsghdr *n = &ainfo->h;
740 struct ifaddrmsg *ifa = NLMSG_DATA(n);
741
742 if (n->nlmsg_type != RTM_NEWADDR)
743 continue;
744
745 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
746 return -1;
747
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800748 if (ifa->ifa_index != ifindex ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000749 (filter.family && filter.family != ifa->ifa_family))
750 continue;
751
752 print_addrinfo(NULL, n, fp);
753 }
754 return 0;
755}
756
757
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800758static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000759 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000760{
Eric Dumazet62e2e542012-06-09 13:55:55 +0200761 struct nlmsg_chain *lchain = (struct nlmsg_chain *)arg;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000762 struct nlmsg_list *h;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000763
764 h = malloc(n->nlmsg_len+sizeof(void*));
765 if (h == NULL)
766 return -1;
767
768 memcpy(&h->h, n, n->nlmsg_len);
769 h->next = NULL;
770
Eric Dumazet62e2e542012-06-09 13:55:55 +0200771 if (lchain->tail)
772 lchain->tail->next = h;
773 else
774 lchain->head = h;
775 lchain->tail = h;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000776
777 ll_remember_index(who, n, NULL);
778 return 0;
779}
780
Pavel Emelyanov81824ac2012-09-11 19:47:00 +0400781static __u32 ipadd_dump_magic = 0x47361222;
782
783static int ipadd_save_prep(void)
784{
785 int ret;
786
787 if (isatty(STDOUT_FILENO)) {
788 fprintf(stderr, "Not sending binary stream to stdout\n");
789 return -1;
790 }
791
792 ret = write(STDOUT_FILENO, &ipadd_dump_magic, sizeof(ipadd_dump_magic));
793 if (ret != sizeof(ipadd_dump_magic)) {
794 fprintf(stderr, "Can't write magic to dump file\n");
795 return -1;
796 }
797
798 return 0;
799}
800
801static int ipadd_dump_check_magic(void)
802{
803 int ret;
804 __u32 magic = 0;
805
806 if (isatty(STDIN_FILENO)) {
807 fprintf(stderr, "Can't restore addr dump from a terminal\n");
808 return -1;
809 }
810
811 ret = fread(&magic, sizeof(magic), 1, stdin);
812 if (magic != ipadd_dump_magic) {
813 fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n", ret, magic);
814 return -1;
815 }
816
817 return 0;
818}
819
820static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
821 void *arg)
822{
823 int ret;
824
825 ret = write(STDOUT_FILENO, n, n->nlmsg_len);
826 if ((ret > 0) && (ret != n->nlmsg_len)) {
827 fprintf(stderr, "Short write while saving nlmsg\n");
828 ret = -EIO;
829 }
830
831 return ret == n->nlmsg_len ? 0 : ret;
832}
833
834static int show_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg)
835{
836 struct ifaddrmsg *ifa = NLMSG_DATA(n);
837
838 printf("if%d:\n", ifa->ifa_index);
839 print_addrinfo(NULL, n, stdout);
840 return 0;
841}
842
843static int ipaddr_showdump(void)
844{
845 if (ipadd_dump_check_magic())
846 exit(-1);
847
848 exit(rtnl_from_file(stdin, &show_handler, NULL));
849}
850
851static int restore_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg)
852{
853 int ret;
854
855 n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
856
857 ll_init_map(&rth);
858
859 ret = rtnl_talk(&rth, n, 0, 0, n);
860 if ((ret < 0) && (errno == EEXIST))
861 ret = 0;
862
863 return ret;
864}
865
866static int ipaddr_restore(void)
867{
868 if (ipadd_dump_check_magic())
869 exit(-1);
870
871 exit(rtnl_from_file(stdin, &restore_handler, NULL));
872}
873
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700874static void free_nlmsg_chain(struct nlmsg_chain *info)
875{
876 struct nlmsg_list *l, *n;
877
878 for (l = info->head; l; l = n) {
879 n = l->next;
880 free(l);
881 }
882}
883
884static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
885{
886 struct nlmsg_list *l, **lp;
887
888 lp = &linfo->head;
889 while ( (l = *lp) != NULL) {
890 int ok = 0;
Petr Písař7f747fd2012-10-03 16:42:41 +0200891 int missing_net_address = 1;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700892 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
893 struct nlmsg_list *a;
894
895 for (a = ainfo->head; a; a = a->next) {
896 struct nlmsghdr *n = &a->h;
897 struct ifaddrmsg *ifa = NLMSG_DATA(n);
898
Petr Písař7f747fd2012-10-03 16:42:41 +0200899 if (ifa->ifa_index != ifi->ifi_index)
900 continue;
901 missing_net_address = 0;
902 if (filter.family && filter.family != ifa->ifa_family)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700903 continue;
904 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
905 continue;
906 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
907 continue;
908 if (filter.pfx.family || filter.label) {
909 struct rtattr *tb[IFA_MAX+1];
910 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
911 if (!tb[IFA_LOCAL])
912 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
913
914 if (filter.pfx.family && tb[IFA_LOCAL]) {
915 inet_prefix dst;
916 memset(&dst, 0, sizeof(dst));
917 dst.family = ifa->ifa_family;
918 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
919 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
920 continue;
921 }
922 if (filter.label) {
923 SPRINT_BUF(b1);
924 const char *label;
925 if (tb[IFA_LABEL])
926 label = RTA_DATA(tb[IFA_LABEL]);
927 else
928 label = ll_idx_n2a(ifa->ifa_index, b1);
929 if (fnmatch(filter.label, label, 0) != 0)
930 continue;
931 }
932 }
933
934 ok = 1;
935 break;
936 }
Petr Písař7f747fd2012-10-03 16:42:41 +0200937 if (missing_net_address &&
938 (filter.family == AF_UNSPEC || filter.family == AF_PACKET))
939 ok = 1;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700940 if (!ok) {
941 *lp = l->next;
942 free(l);
943 } else
944 lp = &l->next;
945 }
946}
947
948static int ipaddr_flush(void)
949{
950 int round = 0;
951 char flushb[4096-512];
952
953 filter.flushb = flushb;
954 filter.flushp = 0;
955 filter.flushe = sizeof(flushb);
956
957 while ((max_flush_loops == 0) || (round < max_flush_loops)) {
958 const struct rtnl_dump_filter_arg a[3] = {
959 {
960 .filter = print_addrinfo_secondary,
961 .arg1 = stdout,
962 },
963 {
964 .filter = print_addrinfo_primary,
965 .arg1 = stdout,
966 },
967 {
968 .filter = NULL,
969 .arg1 = NULL,
970 },
971 };
972 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
973 perror("Cannot send dump request");
974 exit(1);
975 }
976 filter.flushed = 0;
977 if (rtnl_dump_filter_l(&rth, a) < 0) {
978 fprintf(stderr, "Flush terminated\n");
979 exit(1);
980 }
981 if (filter.flushed == 0) {
982 flush_done:
983 if (show_stats) {
984 if (round == 0)
985 printf("Nothing to flush.\n");
986 else
987 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
988 }
989 fflush(stdout);
990 return 0;
991 }
992 round++;
993 if (flush_update() < 0)
994 return 1;
995
996 if (show_stats) {
997 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
998 fflush(stdout);
999 }
1000
1001 /* If we are flushing, and specifying primary, then we
1002 * want to flush only a single round. Otherwise, we'll
1003 * start flushing secondaries that were promoted to
1004 * primaries.
1005 */
1006 if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY))
1007 goto flush_done;
1008 }
1009 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops);
1010 fflush(stderr);
1011 return 1;
1012}
1013
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001014static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001015{
Eric Dumazet62e2e542012-06-09 13:55:55 +02001016 struct nlmsg_chain linfo = { NULL, NULL};
1017 struct nlmsg_chain ainfo = { NULL, NULL};
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001018 struct nlmsg_list *l;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001019 char *filter_dev = NULL;
1020 int no_link = 0;
1021
1022 ipaddr_reset_filter(oneline);
1023 filter.showqueue = 1;
1024
1025 if (filter.family == AF_UNSPEC)
1026 filter.family = preferred_family;
1027
Stephen Hemminger242b8da2011-04-12 14:40:14 -07001028 filter.group = INIT_NETDEV_GROUP;
Vlad Dogaruf960c922011-02-02 20:23:40 +02001029
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001030 if (action == IPADD_FLUSH) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001031 if (argc <= 0) {
1032 fprintf(stderr, "Flush requires arguments.\n");
Vlad Dogaruf960c922011-02-02 20:23:40 +02001033
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001034 return -1;
1035 }
1036 if (filter.family == AF_PACKET) {
1037 fprintf(stderr, "Cannot flush link addresses.\n");
1038 return -1;
1039 }
1040 }
1041
1042 while (argc > 0) {
1043 if (strcmp(*argv, "to") == 0) {
1044 NEXT_ARG();
1045 get_prefix(&filter.pfx, *argv, filter.family);
1046 if (filter.family == AF_UNSPEC)
1047 filter.family = filter.pfx.family;
1048 } else if (strcmp(*argv, "scope") == 0) {
shemmingerf332d162005-07-05 22:37:15 +00001049 unsigned scope = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001050 NEXT_ARG();
1051 filter.scopemask = -1;
1052 if (rtnl_rtscope_a2n(&scope, *argv)) {
1053 if (strcmp(*argv, "all") != 0)
1054 invarg("invalid \"scope\"\n", *argv);
1055 scope = RT_SCOPE_NOWHERE;
1056 filter.scopemask = 0;
1057 }
1058 filter.scope = scope;
1059 } else if (strcmp(*argv, "up") == 0) {
1060 filter.up = 1;
1061 } else if (strcmp(*argv, "dynamic") == 0) {
1062 filter.flags &= ~IFA_F_PERMANENT;
1063 filter.flagmask |= IFA_F_PERMANENT;
1064 } else if (strcmp(*argv, "permanent") == 0) {
1065 filter.flags |= IFA_F_PERMANENT;
1066 filter.flagmask |= IFA_F_PERMANENT;
Brian Haleya1b9ffc2009-09-14 17:01:43 -04001067 } else if (strcmp(*argv, "secondary") == 0 ||
1068 strcmp(*argv, "temporary") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001069 filter.flags |= IFA_F_SECONDARY;
1070 filter.flagmask |= IFA_F_SECONDARY;
1071 } else if (strcmp(*argv, "primary") == 0) {
1072 filter.flags &= ~IFA_F_SECONDARY;
1073 filter.flagmask |= IFA_F_SECONDARY;
1074 } else if (strcmp(*argv, "tentative") == 0) {
1075 filter.flags |= IFA_F_TENTATIVE;
1076 filter.flagmask |= IFA_F_TENTATIVE;
1077 } else if (strcmp(*argv, "deprecated") == 0) {
1078 filter.flags |= IFA_F_DEPRECATED;
1079 filter.flagmask |= IFA_F_DEPRECATED;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001080 } else if (strcmp(*argv, "home") == 0) {
1081 filter.flags |= IFA_F_HOMEADDRESS;
1082 filter.flagmask |= IFA_F_HOMEADDRESS;
1083 } else if (strcmp(*argv, "nodad") == 0) {
1084 filter.flags |= IFA_F_NODAD;
1085 filter.flagmask |= IFA_F_NODAD;
Brian Haleya1f27792009-12-03 10:39:36 +00001086 } else if (strcmp(*argv, "dadfailed") == 0) {
1087 filter.flags |= IFA_F_DADFAILED;
1088 filter.flagmask |= IFA_F_DADFAILED;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001089 } else if (strcmp(*argv, "label") == 0) {
1090 NEXT_ARG();
1091 filter.label = *argv;
Vlad Dogaruf960c922011-02-02 20:23:40 +02001092 } else if (strcmp(*argv, "group") == 0) {
1093 NEXT_ARG();
1094 if (rtnl_group_a2n(&filter.group, *argv))
1095 invarg("Invalid \"group\" value\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001096 } else {
1097 if (strcmp(*argv, "dev") == 0) {
1098 NEXT_ARG();
1099 }
1100 if (matches(*argv, "help") == 0)
1101 usage();
1102 if (filter_dev)
1103 duparg2("dev", *argv);
1104 filter_dev = *argv;
1105 }
1106 argv++; argc--;
1107 }
1108
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001109 if (filter_dev) {
1110 filter.ifindex = ll_name_to_index(filter_dev);
1111 if (filter.ifindex <= 0) {
1112 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
1113 return -1;
1114 }
1115 }
1116
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001117 if (action == IPADD_FLUSH)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001118 return ipaddr_flush();
1119
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001120 if (action == IPADD_SAVE) {
1121 if (ipadd_save_prep())
1122 exit(1);
1123
1124 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETADDR) < 0) {
1125 perror("Cannot send dump request");
1126 exit(1);
1127 }
1128
1129 if (rtnl_dump_filter(&rth, save_nlmsg, stdout) < 0) {
1130 fprintf(stderr, "Save terminated\n");
1131 exit(1);
1132 }
1133
1134 exit(0);
1135 }
1136
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001137 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
1138 perror("Cannot send dump request");
1139 exit(1);
1140 }
1141
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001142 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001143 fprintf(stderr, "Dump terminated\n");
1144 exit(1);
1145 }
1146
Mike Frysingeraf9d4062012-08-13 08:09:52 -07001147 if (filter.family != AF_PACKET) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001148 if (filter.oneline)
1149 no_link = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001150
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001151 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1152 perror("Cannot send dump request");
1153 exit(1);
1154 }
1155
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001156 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001157 fprintf(stderr, "Dump terminated\n");
1158 exit(1);
1159 }
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001160
1161 ipaddr_filter(&linfo, &ainfo);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001162 }
1163
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001164 for (l = linfo.head; l; l = l->next) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001165 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
1166 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
1167 if (filter.family != AF_PACKET)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001168 print_selected_addrinfo(ifi->ifi_index,
1169 ainfo.head, stdout);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001170 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001171 }
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001172 fflush(stdout);
1173
1174 free_nlmsg_chain(&ainfo);
1175 free_nlmsg_chain(&linfo);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001176
shemminger351efcd2005-09-01 19:21:50 +00001177 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001178}
1179
1180int ipaddr_list_link(int argc, char **argv)
1181{
1182 preferred_family = AF_PACKET;
1183 do_link = 1;
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001184 return ipaddr_list_flush_or_save(argc, argv, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001185}
1186
1187void ipaddr_reset_filter(int oneline)
1188{
1189 memset(&filter, 0, sizeof(filter));
1190 filter.oneline = oneline;
1191}
1192
Stephen Hemminger3d866ba2008-03-14 15:30:03 -07001193static int default_scope(inet_prefix *lcl)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001194{
1195 if (lcl->family == AF_INET) {
1196 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
1197 return RT_SCOPE_HOST;
1198 }
1199 return 0;
1200}
1201
Stephen Hemminger3d866ba2008-03-14 15:30:03 -07001202static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001203{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001204 struct {
1205 struct nlmsghdr n;
1206 struct ifaddrmsg ifa;
1207 char buf[256];
1208 } req;
1209 char *d = NULL;
1210 char *l = NULL;
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001211 char *lcl_arg = NULL;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001212 char *valid_lftp = NULL;
1213 char *preferred_lftp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001214 inet_prefix lcl;
1215 inet_prefix peer;
1216 int local_len = 0;
1217 int peer_len = 0;
1218 int brd_len = 0;
1219 int any_len = 0;
1220 int scoped = 0;
Masahide NAKAMURA141bb602006-11-24 12:27:01 +09001221 __u32 preferred_lft = INFINITY_LIFE_TIME;
1222 __u32 valid_lft = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001223 struct ifa_cacheinfo cinfo;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001224
1225 memset(&req, 0, sizeof(req));
1226
1227 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001228 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001229 req.n.nlmsg_type = cmd;
1230 req.ifa.ifa_family = preferred_family;
1231
1232 while (argc > 0) {
1233 if (strcmp(*argv, "peer") == 0 ||
1234 strcmp(*argv, "remote") == 0) {
1235 NEXT_ARG();
1236
1237 if (peer_len)
1238 duparg("peer", *argv);
1239 get_prefix(&peer, *argv, req.ifa.ifa_family);
1240 peer_len = peer.bytelen;
1241 if (req.ifa.ifa_family == AF_UNSPEC)
1242 req.ifa.ifa_family = peer.family;
1243 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
1244 req.ifa.ifa_prefixlen = peer.bitlen;
1245 } else if (matches(*argv, "broadcast") == 0 ||
1246 strcmp(*argv, "brd") == 0) {
1247 inet_prefix addr;
1248 NEXT_ARG();
1249 if (brd_len)
1250 duparg("broadcast", *argv);
1251 if (strcmp(*argv, "+") == 0)
1252 brd_len = -1;
1253 else if (strcmp(*argv, "-") == 0)
1254 brd_len = -2;
1255 else {
1256 get_addr(&addr, *argv, req.ifa.ifa_family);
1257 if (req.ifa.ifa_family == AF_UNSPEC)
1258 req.ifa.ifa_family = addr.family;
1259 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
1260 brd_len = addr.bytelen;
1261 }
1262 } else if (strcmp(*argv, "anycast") == 0) {
1263 inet_prefix addr;
1264 NEXT_ARG();
1265 if (any_len)
1266 duparg("anycast", *argv);
1267 get_addr(&addr, *argv, req.ifa.ifa_family);
1268 if (req.ifa.ifa_family == AF_UNSPEC)
1269 req.ifa.ifa_family = addr.family;
1270 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
1271 any_len = addr.bytelen;
1272 } else if (strcmp(*argv, "scope") == 0) {
shemmingerf332d162005-07-05 22:37:15 +00001273 unsigned scope = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001274 NEXT_ARG();
1275 if (rtnl_rtscope_a2n(&scope, *argv))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +00001276 invarg("invalid scope value.", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001277 req.ifa.ifa_scope = scope;
1278 scoped = 1;
1279 } else if (strcmp(*argv, "dev") == 0) {
1280 NEXT_ARG();
1281 d = *argv;
1282 } else if (strcmp(*argv, "label") == 0) {
1283 NEXT_ARG();
1284 l = *argv;
1285 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001286 } else if (matches(*argv, "valid_lft") == 0) {
1287 if (valid_lftp)
1288 duparg("valid_lft", *argv);
1289 NEXT_ARG();
1290 valid_lftp = *argv;
1291 if (set_lifetime(&valid_lft, *argv))
1292 invarg("valid_lft value", *argv);
1293 } else if (matches(*argv, "preferred_lft") == 0) {
1294 if (preferred_lftp)
1295 duparg("preferred_lft", *argv);
1296 NEXT_ARG();
1297 preferred_lftp = *argv;
1298 if (set_lifetime(&preferred_lft, *argv))
1299 invarg("preferred_lft value", *argv);
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001300 } else if (strcmp(*argv, "home") == 0) {
1301 req.ifa.ifa_flags |= IFA_F_HOMEADDRESS;
1302 } else if (strcmp(*argv, "nodad") == 0) {
1303 req.ifa.ifa_flags |= IFA_F_NODAD;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001304 } else {
1305 if (strcmp(*argv, "local") == 0) {
1306 NEXT_ARG();
1307 }
1308 if (matches(*argv, "help") == 0)
1309 usage();
1310 if (local_len)
1311 duparg2("local", *argv);
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001312 lcl_arg = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001313 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1314 if (req.ifa.ifa_family == AF_UNSPEC)
1315 req.ifa.ifa_family = lcl.family;
1316 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1317 local_len = lcl.bytelen;
1318 }
1319 argc--; argv++;
1320 }
1321 if (d == NULL) {
1322 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1323 return -1;
1324 }
1325 if (l && matches(d, l) != 0) {
1326 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
Michele Petrazzo - Unipex1db61e02010-03-06 08:56:53 +00001327 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001328 }
1329
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001330 if (peer_len == 0 && local_len) {
1331 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
1332 fprintf(stderr,
1333 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1334 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1335 " This special behaviour is likely to disappear in further releases,\n" \
1336 " fix your scripts!\n", lcl_arg, local_len*8);
1337 } else {
1338 peer = lcl;
1339 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
1340 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001341 }
1342 if (req.ifa.ifa_prefixlen == 0)
1343 req.ifa.ifa_prefixlen = lcl.bitlen;
1344
1345 if (brd_len < 0 && cmd != RTM_DELADDR) {
1346 inet_prefix brd;
1347 int i;
1348 if (req.ifa.ifa_family != AF_INET) {
1349 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
1350 return -1;
1351 }
1352 brd = peer;
1353 if (brd.bitlen <= 30) {
1354 for (i=31; i>=brd.bitlen; i--) {
1355 if (brd_len == -1)
1356 brd.data[0] |= htonl(1<<(31-i));
1357 else
1358 brd.data[0] &= ~htonl(1<<(31-i));
1359 }
1360 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
1361 brd_len = brd.bytelen;
1362 }
1363 }
1364 if (!scoped && cmd != RTM_DELADDR)
1365 req.ifa.ifa_scope = default_scope(&lcl);
1366
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001367 ll_init_map(&rth);
1368
1369 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
1370 fprintf(stderr, "Cannot find device \"%s\"\n", d);
1371 return -1;
1372 }
1373
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001374 if (valid_lftp || preferred_lftp) {
1375 if (!valid_lft) {
1376 fprintf(stderr, "valid_lft is zero\n");
1377 return -1;
1378 }
1379 if (valid_lft < preferred_lft) {
1380 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
1381 return -1;
1382 }
1383
1384 memset(&cinfo, 0, sizeof(cinfo));
1385 cinfo.ifa_prefered = preferred_lft;
1386 cinfo.ifa_valid = valid_lft;
1387 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
1388 sizeof(cinfo));
1389 }
1390
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001391 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
Michele Petrazzo - Unipex1db61e02010-03-06 08:56:53 +00001392 return -2;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001393
shemminger351efcd2005-09-01 19:21:50 +00001394 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001395}
1396
1397int do_ipaddr(int argc, char **argv)
1398{
1399 if (argc < 1)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001400 return ipaddr_list_flush_or_save(0, NULL, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001401 if (matches(*argv, "add") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001402 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
1403 if (matches(*argv, "change") == 0 ||
1404 strcmp(*argv, "chg") == 0)
1405 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
1406 if (matches(*argv, "replace") == 0)
1407 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001408 if (matches(*argv, "delete") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001409 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001410 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1411 || matches(*argv, "lst") == 0)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001412 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001413 if (matches(*argv, "flush") == 0)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001414 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_FLUSH);
1415 if (matches(*argv, "save") == 0)
1416 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_SAVE);
1417 if (matches(*argv, "showdump") == 0)
1418 return ipaddr_showdump();
1419 if (matches(*argv, "restore") == 0)
1420 return ipaddr_restore();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001421 if (matches(*argv, "help") == 0)
1422 usage();
Alexander Wirtb096fa52007-10-12 10:56:36 +02001423 fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001424 exit(-1);
1425}
1426