blob: 1e3f22c5dc547c42e3c69a30e585ef4e158cef6a [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");
Petr Ĺ abata44051232013-03-14 15:10:44 +010074 fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ] [up]\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
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080092static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000093{
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) { \
Stephen Hemminger1124ffb2013-03-14 13:47:49 -070099 flags &= ~IFF_##f ; \
100 fprintf(fp, #f "%s", flags ? "," : ""); }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000101 _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
Stephen Hemminger1124ffb2013-03-14 13:47:49 -0700120 if (flags)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000121 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;
Rony Efraim07fa9c12013-06-13 13:19:12 +0300232 struct ifla_vf_link_state *vf_linkstate;
Chris Wright3fd86632010-05-18 00:57:00 -0700233 struct rtattr *vf[IFLA_VF_MAX+1];
Greg Rose7b8179c2011-10-13 20:31:32 +0000234 struct rtattr *tmp;
Chris Wright3fd86632010-05-18 00:57:00 -0700235 SPRINT_BUF(b1);
236
237 if (vfinfo->rta_type != IFLA_VF_INFO) {
238 fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type);
239 return;
240 }
241
242 parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo);
243
244 vf_mac = RTA_DATA(vf[IFLA_VF_MAC]);
245 vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
246 vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]);
247
Greg Rose7b8179c2011-10-13 20:31:32 +0000248 /* Check if the spoof checking vf info type is supported by
249 * this kernel.
250 */
251 tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] +
252 vf[IFLA_VF_TX_RATE]->rta_len);
253
254 if (tmp->rta_type != IFLA_VF_SPOOFCHK)
255 vf_spoofchk = NULL;
256 else
257 vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
258
Rony Efraim07fa9c12013-06-13 13:19:12 +0300259 if (vf_spoofchk) {
260 /* Check if the link state vf info type is supported by
261 * this kernel.
262 */
263 tmp = (struct rtattr *)((char *)vf[IFLA_VF_SPOOFCHK] +
264 vf[IFLA_VF_SPOOFCHK]->rta_len);
265
266 if (tmp->rta_type != IFLA_VF_LINK_STATE)
267 vf_linkstate = NULL;
268 else
269 vf_linkstate = RTA_DATA(vf[IFLA_VF_LINK_STATE]);
270 } else
271 vf_linkstate = NULL;
272
Chris Wright3fd86632010-05-18 00:57:00 -0700273 fprintf(fp, "\n vf %d MAC %s", vf_mac->vf,
274 ll_addr_n2a((unsigned char *)&vf_mac->mac,
275 ETH_ALEN, 0, b1, sizeof(b1)));
276 if (vf_vlan->vlan)
277 fprintf(fp, ", vlan %d", vf_vlan->vlan);
278 if (vf_vlan->qos)
279 fprintf(fp, ", qos %d", vf_vlan->qos);
280 if (vf_tx_rate->rate)
281 fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate);
Greg Rose7b8179c2011-10-13 20:31:32 +0000282 if (vf_spoofchk && vf_spoofchk->setting != -1) {
283 if (vf_spoofchk->setting)
284 fprintf(fp, ", spoof checking on");
285 else
286 fprintf(fp, ", spoof checking off");
287 }
Rony Efraim07fa9c12013-06-13 13:19:12 +0300288 if (vf_linkstate) {
289 if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_AUTO)
290 fprintf(fp, ", link-state auto");
291 else if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_ENABLE)
292 fprintf(fp, ", link-state enable");
293 else
294 fprintf(fp, ", link-state disable");
295 }
Chris Wright3fd86632010-05-18 00:57:00 -0700296}
297
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800298static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s) {
299 fprintf(fp, "%s", _SL_);
300 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
301 s->rx_compressed ? "compressed" : "", _SL_);
302 fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
303 (uint64_t)s->rx_bytes,
304 (uint64_t)s->rx_packets,
305 (uint64_t)s->rx_errors,
306 (uint64_t)s->rx_dropped,
307 (uint64_t)s->rx_over_errors,
308 (uint64_t)s->multicast);
309 if (s->rx_compressed)
310 fprintf(fp, " %-7"PRIu64"",
311 (uint64_t)s->rx_compressed);
312 if (show_stats > 1) {
313 fprintf(fp, "%s", _SL_);
314 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
315 fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
316 (uint64_t)s->rx_length_errors,
317 (uint64_t)s->rx_crc_errors,
318 (uint64_t)s->rx_frame_errors,
319 (uint64_t)s->rx_fifo_errors,
320 (uint64_t)s->rx_missed_errors);
321 }
322 fprintf(fp, "%s", _SL_);
323 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
324 (uint64_t)s->tx_compressed ? "compressed" : "", _SL_);
325 fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
326 (uint64_t)s->tx_bytes,
327 (uint64_t)s->tx_packets,
328 (uint64_t)s->tx_errors,
329 (uint64_t)s->tx_dropped,
330 (uint64_t)s->tx_carrier_errors,
331 (uint64_t)s->collisions);
332 if (s->tx_compressed)
333 fprintf(fp, " %-7"PRIu64"",
334 (uint64_t)s->tx_compressed);
335 if (show_stats > 1) {
336 fprintf(fp, "%s", _SL_);
337 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
338 fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
339 (uint64_t)s->tx_aborted_errors,
340 (uint64_t)s->tx_fifo_errors,
341 (uint64_t)s->tx_window_errors,
342 (uint64_t)s->tx_heartbeat_errors);
343 }
344}
345
346static void print_link_stats(FILE *fp, const struct rtnl_link_stats *s)
347{
348 fprintf(fp, "%s", _SL_);
349 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
350 s->rx_compressed ? "compressed" : "", _SL_);
351 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
352 s->rx_bytes, s->rx_packets, s->rx_errors,
353 s->rx_dropped, s->rx_over_errors,
354 s->multicast
355 );
356 if (s->rx_compressed)
357 fprintf(fp, " %-7u", s->rx_compressed);
358 if (show_stats > 1) {
359 fprintf(fp, "%s", _SL_);
360 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
361 fprintf(fp, " %-7u %-7u %-7u %-7u %-7u",
362 s->rx_length_errors,
363 s->rx_crc_errors,
364 s->rx_frame_errors,
365 s->rx_fifo_errors,
366 s->rx_missed_errors
367 );
368 }
369 fprintf(fp, "%s", _SL_);
370 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
371 s->tx_compressed ? "compressed" : "", _SL_);
372 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
373 s->tx_bytes, s->tx_packets, s->tx_errors,
374 s->tx_dropped, s->tx_carrier_errors, s->collisions);
375 if (s->tx_compressed)
376 fprintf(fp, " %-7u", s->tx_compressed);
377 if (show_stats > 1) {
378 fprintf(fp, "%s", _SL_);
379 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
380 fprintf(fp, " %-7u %-7u %-7u %-7u",
381 s->tx_aborted_errors,
382 s->tx_fifo_errors,
383 s->tx_window_errors,
384 s->tx_heartbeat_errors
385 );
386 }
387}
388
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800389int print_linkinfo(const struct sockaddr_nl *who,
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000390 struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000391{
392 FILE *fp = (FILE*)arg;
393 struct ifinfomsg *ifi = NLMSG_DATA(n);
394 struct rtattr * tb[IFLA_MAX+1];
395 int len = n->nlmsg_len;
396 unsigned m_flag = 0;
397
398 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
399 return 0;
400
401 len -= NLMSG_LENGTH(sizeof(*ifi));
402 if (len < 0)
403 return -1;
404
405 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
406 return 0;
407 if (filter.up && !(ifi->ifi_flags&IFF_UP))
408 return 0;
409
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000410 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
411 if (tb[IFLA_IFNAME] == NULL) {
jamal4cd23bd2008-08-08 10:06:17 -0400412 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000413 }
414 if (filter.label &&
415 (!filter.family || filter.family == AF_PACKET) &&
416 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
417 return 0;
418
Vlad Dogaruf960c922011-02-02 20:23:40 +0200419 if (tb[IFLA_GROUP]) {
420 int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
Stefan Tomanekc4fdf752013-08-03 14:20:53 +0200421 if (filter.group != -1 && group != filter.group)
Vlad Dogaruf960c922011-02-02 20:23:40 +0200422 return -1;
423 }
424
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000425 if (n->nlmsg_type == RTM_DELLINK)
426 fprintf(fp, "Deleted ");
427
428 fprintf(fp, "%d: %s", ifi->ifi_index,
Stephen Hemmingerff247462012-04-10 08:47:55 -0700429 tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000430
431 if (tb[IFLA_LINK]) {
432 SPRINT_BUF(b1);
433 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
434 if (iflink == 0)
435 fprintf(fp, "@NONE: ");
436 else {
437 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
438 m_flag = ll_index_to_flags(iflink);
439 m_flag = !(m_flag & IFF_UP);
440 }
441 } else {
442 fprintf(fp, ": ");
443 }
444 print_link_flags(fp, ifi->ifi_flags, m_flag);
445
446 if (tb[IFLA_MTU])
447 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
448 if (tb[IFLA_QDISC])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700449 fprintf(fp, "qdisc %s ", rta_getattr_str(tb[IFLA_QDISC]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000450 if (tb[IFLA_MASTER]) {
451 SPRINT_BUF(b1);
452 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
453 }
Stephen Hemminger82499282012-03-19 17:24:43 -0700454
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700455 if (tb[IFLA_OPERSTATE])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700456 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
Stephen Hemminger82499282012-03-19 17:24:43 -0700457
458 if (do_link && tb[IFLA_LINKMODE])
459 print_linkmode(fp, tb[IFLA_LINKMODE]);
460
Stefan Tomanekc4fdf752013-08-03 14:20:53 +0200461 if (tb[IFLA_GROUP]) {
462 SPRINT_BUF(b1);
463 int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
464 fprintf(fp, "group %s ", rtnl_group_n2a(group, b1, sizeof(b1)));
465 }
466
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000467 if (filter.showqueue)
Eric Dumazetf78e3162009-10-22 18:13:21 +0000468 print_queuelen(fp, tb);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800469
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000470 if (!filter.family || filter.family == AF_PACKET) {
471 SPRINT_BUF(b1);
472 fprintf(fp, "%s", _SL_);
473 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
474
475 if (tb[IFLA_ADDRESS]) {
476 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
477 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
478 ifi->ifi_type,
479 b1, sizeof(b1)));
480 }
481 if (tb[IFLA_BROADCAST]) {
482 if (ifi->ifi_flags&IFF_POINTOPOINT)
483 fprintf(fp, " peer ");
484 else
485 fprintf(fp, " brd ");
486 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
487 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
488 ifi->ifi_type,
489 b1, sizeof(b1)));
490 }
491 }
Patrick McHardy1d934832007-08-22 10:49:01 -0700492
Stephen Hemminger1cb6a112013-02-05 08:16:28 -0800493 if (do_link && tb[IFLA_PROMISCUITY] && show_details)
494 fprintf(fp, " promiscuity %u ",
495 *(int*)RTA_DATA(tb[IFLA_PROMISCUITY]));
496
Patrick McHardy1d934832007-08-22 10:49:01 -0700497 if (do_link && tb[IFLA_LINKINFO] && show_details)
498 print_linktype(fp, tb[IFLA_LINKINFO]);
499
roopa263c8942013-03-12 15:04:02 -0700500 if (do_link && tb[IFLA_IFALIAS]) {
Stephen Hemminger1124ffb2013-03-14 13:47:49 -0700501 fprintf(fp, "%s alias %s", _SL_,
Stephen Hemmingerff247462012-04-10 08:47:55 -0700502 rta_getattr_str(tb[IFLA_IFALIAS]));
roopa263c8942013-03-12 15:04:02 -0700503 }
Stephen Hemmingerace9c962009-03-23 10:46:47 -0700504
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800505 if (do_link && show_stats) {
506 if (tb[IFLA_STATS64])
507 print_link_stats64(fp, RTA_DATA(tb[IFLA_STATS64]));
508 else if (tb[IFLA_STATS])
509 print_link_stats(fp, RTA_DATA(tb[IFLA_STATS]));
Jan Engelhardt8864ac92010-03-11 10:00:34 +0000510 }
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800511
Chris Wright3fd86632010-05-18 00:57:00 -0700512 if (do_link && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) {
513 struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST];
514 int rem = RTA_PAYLOAD(vflist);
515 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
516 print_vfinfo(fp, i);
Williams, Mitch Aae7229d2010-02-10 01:47:08 +0000517 }
Chris Wright3fd86632010-05-18 00:57:00 -0700518
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000519 fprintf(fp, "\n");
520 fflush(fp);
521 return 0;
522}
523
524static int flush_update(void)
525{
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800526 if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
Stephen Hemminger1fb0a992008-01-26 11:08:31 -0800527 perror("Failed to send flush request");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000528 return -1;
529 }
530 filter.flushp = 0;
531 return 0;
532}
533
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900534static int set_lifetime(unsigned int *lifetime, char *argv)
535{
536 if (strcmp(argv, "forever") == 0)
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900537 *lifetime = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900538 else if (get_u32(lifetime, argv, 0))
539 return -1;
540
541 return 0;
542}
543
Jiri Pirko37c9b942014-01-06 10:17:09 +0100544static unsigned int get_ifa_flags(struct ifaddrmsg *ifa,
545 struct rtattr *ifa_flags_attr)
546{
547 return ifa_flags_attr ? rta_getattr_u32(ifa_flags_attr) :
548 ifa->ifa_flags;
549}
550
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800551int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000552 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000553{
554 FILE *fp = (FILE*)arg;
555 struct ifaddrmsg *ifa = NLMSG_DATA(n);
556 int len = n->nlmsg_len;
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800557 int deprecated = 0;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700558 /* Use local copy of ifa_flags to not interfere with filtering code */
559 unsigned int ifa_flags;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000560 struct rtattr * rta_tb[IFA_MAX+1];
561 char abuf[256];
562 SPRINT_BUF(b1);
563
564 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
565 return 0;
566 len -= NLMSG_LENGTH(sizeof(*ifa));
567 if (len < 0) {
568 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
569 return -1;
570 }
571
572 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
573 return 0;
574
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000575 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
576
Jiri Pirko37c9b942014-01-06 10:17:09 +0100577 ifa_flags = get_ifa_flags(ifa, rta_tb[IFA_FLAGS]);
578
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000579 if (!rta_tb[IFA_LOCAL])
580 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
581 if (!rta_tb[IFA_ADDRESS])
582 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
583
584 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
585 return 0;
586 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
587 return 0;
Jiri Pirko37c9b942014-01-06 10:17:09 +0100588 if ((filter.flags ^ ifa_flags) & filter.flagmask)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000589 return 0;
590 if (filter.label) {
591 SPRINT_BUF(b1);
592 const char *label;
593 if (rta_tb[IFA_LABEL])
594 label = RTA_DATA(rta_tb[IFA_LABEL]);
595 else
596 label = ll_idx_n2a(ifa->ifa_index, b1);
597 if (fnmatch(filter.label, label, 0) != 0)
598 return 0;
599 }
600 if (filter.pfx.family) {
601 if (rta_tb[IFA_LOCAL]) {
602 inet_prefix dst;
603 memset(&dst, 0, sizeof(dst));
604 dst.family = ifa->ifa_family;
605 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
606 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
607 return 0;
608 }
609 }
610
net[shemminger]!shemminger3eb17312005-02-07 18:28:31 +0000611 if (filter.family && filter.family != ifa->ifa_family)
612 return 0;
613
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000614 if (filter.flushb) {
615 struct nlmsghdr *fn;
616 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
617 if (flush_update())
618 return -1;
619 }
620 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
621 memcpy(fn, n, n->nlmsg_len);
622 fn->nlmsg_type = RTM_DELADDR;
623 fn->nlmsg_flags = NLM_F_REQUEST;
shemminger351efcd2005-09-01 19:21:50 +0000624 fn->nlmsg_seq = ++rth.seq;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000625 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
626 filter.flushed++;
627 if (show_stats < 2)
628 return 0;
629 }
630
631 if (n->nlmsg_type == RTM_DELADDR)
632 fprintf(fp, "Deleted ");
633
634 if (filter.oneline || filter.flushb)
635 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
636 if (ifa->ifa_family == AF_INET)
637 fprintf(fp, " inet ");
638 else if (ifa->ifa_family == AF_INET6)
639 fprintf(fp, " inet6 ");
640 else if (ifa->ifa_family == AF_DECnet)
641 fprintf(fp, " dnet ");
642 else if (ifa->ifa_family == AF_IPX)
643 fprintf(fp, " ipx ");
644 else
645 fprintf(fp, " family %d ", ifa->ifa_family);
646
647 if (rta_tb[IFA_LOCAL]) {
Sami Kerolaffa35d92013-09-30 22:01:48 +0100648 fprintf(fp, "%s", format_host(ifa->ifa_family,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000649 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
650 RTA_DATA(rta_tb[IFA_LOCAL]),
651 abuf, sizeof(abuf)));
652
653 if (rta_tb[IFA_ADDRESS] == NULL ||
Nicolas Dichtel973eb502013-07-16 22:52:48 +0200654 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]),
655 ifa->ifa_family == AF_INET ? 4 : 16) == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000656 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
657 } else {
658 fprintf(fp, " peer %s/%d ",
Sami Kerolaffa35d92013-09-30 22:01:48 +0100659 format_host(ifa->ifa_family,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000660 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
661 RTA_DATA(rta_tb[IFA_ADDRESS]),
662 abuf, sizeof(abuf)),
663 ifa->ifa_prefixlen);
664 }
665 }
666
667 if (rta_tb[IFA_BROADCAST]) {
668 fprintf(fp, "brd %s ",
Sami Kerolaffa35d92013-09-30 22:01:48 +0100669 format_host(ifa->ifa_family,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000670 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
671 RTA_DATA(rta_tb[IFA_BROADCAST]),
672 abuf, sizeof(abuf)));
673 }
674 if (rta_tb[IFA_ANYCAST]) {
675 fprintf(fp, "any %s ",
Sami Kerolaffa35d92013-09-30 22:01:48 +0100676 format_host(ifa->ifa_family,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000677 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
678 RTA_DATA(rta_tb[IFA_ANYCAST]),
679 abuf, sizeof(abuf)));
680 }
681 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
Jiri Pirko37c9b942014-01-06 10:17:09 +0100682 if (ifa_flags & IFA_F_SECONDARY) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700683 ifa_flags &= ~IFA_F_SECONDARY;
Brian Haleya1b9ffc2009-09-14 17:01:43 -0400684 if (ifa->ifa_family == AF_INET6)
685 fprintf(fp, "temporary ");
686 else
687 fprintf(fp, "secondary ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000688 }
Jiri Pirko37c9b942014-01-06 10:17:09 +0100689 if (ifa_flags & IFA_F_TENTATIVE) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700690 ifa_flags &= ~IFA_F_TENTATIVE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000691 fprintf(fp, "tentative ");
692 }
Jiri Pirko37c9b942014-01-06 10:17:09 +0100693 if (ifa_flags & IFA_F_DEPRECATED) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700694 ifa_flags &= ~IFA_F_DEPRECATED;
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800695 deprecated = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000696 fprintf(fp, "deprecated ");
697 }
Jiri Pirko37c9b942014-01-06 10:17:09 +0100698 if (ifa_flags & IFA_F_HOMEADDRESS) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700699 ifa_flags &= ~IFA_F_HOMEADDRESS;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900700 fprintf(fp, "home ");
701 }
Jiri Pirko37c9b942014-01-06 10:17:09 +0100702 if (ifa_flags & IFA_F_NODAD) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700703 ifa_flags &= ~IFA_F_NODAD;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900704 fprintf(fp, "nodad ");
705 }
Jiri Pirko37c9b942014-01-06 10:17:09 +0100706 if (!(ifa_flags & IFA_F_PERMANENT)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000707 fprintf(fp, "dynamic ");
708 } else
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700709 ifa_flags &= ~IFA_F_PERMANENT;
Jiri Pirko37c9b942014-01-06 10:17:09 +0100710 if (ifa_flags & IFA_F_DADFAILED) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700711 ifa_flags &= ~IFA_F_DADFAILED;
Brian Haleyf4af8512009-12-01 15:58:44 -0800712 fprintf(fp, "dadfailed ");
713 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700714 if (ifa_flags)
715 fprintf(fp, "flags %02x ", ifa_flags);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000716 if (rta_tb[IFA_LABEL])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700717 fprintf(fp, "%s", rta_getattr_str(rta_tb[IFA_LABEL]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000718 if (rta_tb[IFA_CACHEINFO]) {
719 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000720 fprintf(fp, "%s", _SL_);
Andreas Schwabf66efad2010-11-05 23:26:29 +0000721 fprintf(fp, " valid_lft ");
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900722 if (ci->ifa_valid == INFINITY_LIFE_TIME)
Andreas Schwabf66efad2010-11-05 23:26:29 +0000723 fprintf(fp, "forever");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000724 else
Andreas Schwabf66efad2010-11-05 23:26:29 +0000725 fprintf(fp, "%usec", ci->ifa_valid);
726 fprintf(fp, " preferred_lft ");
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900727 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
Andreas Schwabf66efad2010-11-05 23:26:29 +0000728 fprintf(fp, "forever");
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800729 else {
730 if (deprecated)
Andreas Schwabf66efad2010-11-05 23:26:29 +0000731 fprintf(fp, "%dsec", ci->ifa_prefered);
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800732 else
Andreas Schwabf66efad2010-11-05 23:26:29 +0000733 fprintf(fp, "%usec", ci->ifa_prefered);
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800734 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000735 }
736 fprintf(fp, "\n");
737 fflush(fp);
738 return 0;
739}
740
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800741static int print_addrinfo_primary(const struct sockaddr_nl *who,
742 struct nlmsghdr *n, void *arg)
Simon Hormanb49240e2009-12-03 12:08:27 +1100743{
744 struct ifaddrmsg *ifa = NLMSG_DATA(n);
745
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700746 if (ifa->ifa_flags & IFA_F_SECONDARY)
Simon Hormanb49240e2009-12-03 12:08:27 +1100747 return 0;
748
749 return print_addrinfo(who, n, arg);
750}
751
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800752static int print_addrinfo_secondary(const struct sockaddr_nl *who,
753 struct nlmsghdr *n, void *arg)
Simon Hormanb49240e2009-12-03 12:08:27 +1100754{
755 struct ifaddrmsg *ifa = NLMSG_DATA(n);
756
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700757 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
Simon Hormanb49240e2009-12-03 12:08:27 +1100758 return 0;
759
760 return print_addrinfo(who, n, arg);
761}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000762
763struct nlmsg_list
764{
765 struct nlmsg_list *next;
766 struct nlmsghdr h;
767};
768
Eric Dumazet62e2e542012-06-09 13:55:55 +0200769struct nlmsg_chain
770{
771 struct nlmsg_list *head;
772 struct nlmsg_list *tail;
773};
774
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700775static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000776{
777 for ( ;ainfo ; ainfo = ainfo->next) {
778 struct nlmsghdr *n = &ainfo->h;
779 struct ifaddrmsg *ifa = NLMSG_DATA(n);
780
781 if (n->nlmsg_type != RTM_NEWADDR)
782 continue;
783
784 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
785 return -1;
786
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800787 if (ifa->ifa_index != ifindex ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000788 (filter.family && filter.family != ifa->ifa_family))
789 continue;
790
791 print_addrinfo(NULL, n, fp);
792 }
793 return 0;
794}
795
796
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800797static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000798 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000799{
Eric Dumazet62e2e542012-06-09 13:55:55 +0200800 struct nlmsg_chain *lchain = (struct nlmsg_chain *)arg;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000801 struct nlmsg_list *h;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000802
803 h = malloc(n->nlmsg_len+sizeof(void*));
804 if (h == NULL)
805 return -1;
806
807 memcpy(&h->h, n, n->nlmsg_len);
808 h->next = NULL;
809
Eric Dumazet62e2e542012-06-09 13:55:55 +0200810 if (lchain->tail)
811 lchain->tail->next = h;
812 else
813 lchain->head = h;
814 lchain->tail = h;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000815
816 ll_remember_index(who, n, NULL);
817 return 0;
818}
819
Pavel Emelyanov81824ac2012-09-11 19:47:00 +0400820static __u32 ipadd_dump_magic = 0x47361222;
821
822static int ipadd_save_prep(void)
823{
824 int ret;
825
826 if (isatty(STDOUT_FILENO)) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000827 fprintf(stderr, "Not sending a binary stream to stdout\n");
Pavel Emelyanov81824ac2012-09-11 19:47:00 +0400828 return -1;
829 }
830
831 ret = write(STDOUT_FILENO, &ipadd_dump_magic, sizeof(ipadd_dump_magic));
832 if (ret != sizeof(ipadd_dump_magic)) {
833 fprintf(stderr, "Can't write magic to dump file\n");
834 return -1;
835 }
836
837 return 0;
838}
839
840static int ipadd_dump_check_magic(void)
841{
842 int ret;
843 __u32 magic = 0;
844
845 if (isatty(STDIN_FILENO)) {
846 fprintf(stderr, "Can't restore addr dump from a terminal\n");
847 return -1;
848 }
849
850 ret = fread(&magic, sizeof(magic), 1, stdin);
851 if (magic != ipadd_dump_magic) {
852 fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n", ret, magic);
853 return -1;
854 }
855
856 return 0;
857}
858
859static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
860 void *arg)
861{
862 int ret;
863
864 ret = write(STDOUT_FILENO, n, n->nlmsg_len);
865 if ((ret > 0) && (ret != n->nlmsg_len)) {
866 fprintf(stderr, "Short write while saving nlmsg\n");
867 ret = -EIO;
868 }
869
870 return ret == n->nlmsg_len ? 0 : ret;
871}
872
873static int show_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg)
874{
875 struct ifaddrmsg *ifa = NLMSG_DATA(n);
876
877 printf("if%d:\n", ifa->ifa_index);
878 print_addrinfo(NULL, n, stdout);
879 return 0;
880}
881
882static int ipaddr_showdump(void)
883{
884 if (ipadd_dump_check_magic())
885 exit(-1);
886
887 exit(rtnl_from_file(stdin, &show_handler, NULL));
888}
889
890static int restore_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg)
891{
892 int ret;
893
894 n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
895
896 ll_init_map(&rth);
897
898 ret = rtnl_talk(&rth, n, 0, 0, n);
899 if ((ret < 0) && (errno == EEXIST))
900 ret = 0;
901
902 return ret;
903}
904
905static int ipaddr_restore(void)
906{
907 if (ipadd_dump_check_magic())
908 exit(-1);
909
910 exit(rtnl_from_file(stdin, &restore_handler, NULL));
911}
912
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700913static void free_nlmsg_chain(struct nlmsg_chain *info)
914{
915 struct nlmsg_list *l, *n;
916
917 for (l = info->head; l; l = n) {
918 n = l->next;
919 free(l);
920 }
921}
922
923static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
924{
925 struct nlmsg_list *l, **lp;
926
927 lp = &linfo->head;
928 while ( (l = *lp) != NULL) {
929 int ok = 0;
Petr Písař7f747fd2012-10-03 16:42:41 +0200930 int missing_net_address = 1;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700931 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
932 struct nlmsg_list *a;
933
934 for (a = ainfo->head; a; a = a->next) {
935 struct nlmsghdr *n = &a->h;
936 struct ifaddrmsg *ifa = NLMSG_DATA(n);
Jiri Pirko37c9b942014-01-06 10:17:09 +0100937 struct rtattr *tb[IFA_MAX + 1];
938 unsigned int ifa_flags;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700939
Petr Písař7f747fd2012-10-03 16:42:41 +0200940 if (ifa->ifa_index != ifi->ifi_index)
941 continue;
942 missing_net_address = 0;
943 if (filter.family && filter.family != ifa->ifa_family)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700944 continue;
945 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
946 continue;
Jiri Pirko37c9b942014-01-06 10:17:09 +0100947
948 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
949 ifa_flags = get_ifa_flags(ifa, tb[IFA_FLAGS]);
950
951 if ((filter.flags ^ ifa_flags) & filter.flagmask)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700952 continue;
953 if (filter.pfx.family || filter.label) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700954 if (!tb[IFA_LOCAL])
955 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
956
957 if (filter.pfx.family && tb[IFA_LOCAL]) {
958 inet_prefix dst;
959 memset(&dst, 0, sizeof(dst));
960 dst.family = ifa->ifa_family;
961 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
962 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
963 continue;
964 }
965 if (filter.label) {
966 SPRINT_BUF(b1);
967 const char *label;
968 if (tb[IFA_LABEL])
969 label = RTA_DATA(tb[IFA_LABEL]);
970 else
971 label = ll_idx_n2a(ifa->ifa_index, b1);
972 if (fnmatch(filter.label, label, 0) != 0)
973 continue;
974 }
975 }
976
977 ok = 1;
978 break;
979 }
Petr Písař7f747fd2012-10-03 16:42:41 +0200980 if (missing_net_address &&
981 (filter.family == AF_UNSPEC || filter.family == AF_PACKET))
982 ok = 1;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -0700983 if (!ok) {
984 *lp = l->next;
985 free(l);
986 } else
987 lp = &l->next;
988 }
989}
990
991static int ipaddr_flush(void)
992{
993 int round = 0;
994 char flushb[4096-512];
995
996 filter.flushb = flushb;
997 filter.flushp = 0;
998 filter.flushe = sizeof(flushb);
999
1000 while ((max_flush_loops == 0) || (round < max_flush_loops)) {
1001 const struct rtnl_dump_filter_arg a[3] = {
1002 {
1003 .filter = print_addrinfo_secondary,
1004 .arg1 = stdout,
1005 },
1006 {
1007 .filter = print_addrinfo_primary,
1008 .arg1 = stdout,
1009 },
1010 {
1011 .filter = NULL,
1012 .arg1 = NULL,
1013 },
1014 };
1015 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1016 perror("Cannot send dump request");
1017 exit(1);
1018 }
1019 filter.flushed = 0;
1020 if (rtnl_dump_filter_l(&rth, a) < 0) {
1021 fprintf(stderr, "Flush terminated\n");
1022 exit(1);
1023 }
1024 if (filter.flushed == 0) {
1025 flush_done:
1026 if (show_stats) {
1027 if (round == 0)
1028 printf("Nothing to flush.\n");
1029 else
1030 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
1031 }
1032 fflush(stdout);
1033 return 0;
1034 }
1035 round++;
1036 if (flush_update() < 0)
1037 return 1;
1038
1039 if (show_stats) {
1040 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
1041 fflush(stdout);
1042 }
1043
1044 /* If we are flushing, and specifying primary, then we
1045 * want to flush only a single round. Otherwise, we'll
1046 * start flushing secondaries that were promoted to
1047 * primaries.
1048 */
1049 if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY))
1050 goto flush_done;
1051 }
1052 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops);
1053 fflush(stderr);
1054 return 1;
1055}
1056
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001057static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001058{
Eric Dumazet62e2e542012-06-09 13:55:55 +02001059 struct nlmsg_chain linfo = { NULL, NULL};
1060 struct nlmsg_chain ainfo = { NULL, NULL};
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001061 struct nlmsg_list *l;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001062 char *filter_dev = NULL;
1063 int no_link = 0;
1064
1065 ipaddr_reset_filter(oneline);
1066 filter.showqueue = 1;
1067
1068 if (filter.family == AF_UNSPEC)
1069 filter.family = preferred_family;
1070
Stefan Tomanekc4fdf752013-08-03 14:20:53 +02001071 filter.group = -1;
Vlad Dogaruf960c922011-02-02 20:23:40 +02001072
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001073 if (action == IPADD_FLUSH) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001074 if (argc <= 0) {
1075 fprintf(stderr, "Flush requires arguments.\n");
Vlad Dogaruf960c922011-02-02 20:23:40 +02001076
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001077 return -1;
1078 }
1079 if (filter.family == AF_PACKET) {
1080 fprintf(stderr, "Cannot flush link addresses.\n");
1081 return -1;
1082 }
1083 }
1084
1085 while (argc > 0) {
1086 if (strcmp(*argv, "to") == 0) {
1087 NEXT_ARG();
1088 get_prefix(&filter.pfx, *argv, filter.family);
1089 if (filter.family == AF_UNSPEC)
1090 filter.family = filter.pfx.family;
1091 } else if (strcmp(*argv, "scope") == 0) {
shemmingerf332d162005-07-05 22:37:15 +00001092 unsigned scope = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001093 NEXT_ARG();
1094 filter.scopemask = -1;
1095 if (rtnl_rtscope_a2n(&scope, *argv)) {
1096 if (strcmp(*argv, "all") != 0)
1097 invarg("invalid \"scope\"\n", *argv);
1098 scope = RT_SCOPE_NOWHERE;
1099 filter.scopemask = 0;
1100 }
1101 filter.scope = scope;
1102 } else if (strcmp(*argv, "up") == 0) {
1103 filter.up = 1;
1104 } else if (strcmp(*argv, "dynamic") == 0) {
1105 filter.flags &= ~IFA_F_PERMANENT;
1106 filter.flagmask |= IFA_F_PERMANENT;
1107 } else if (strcmp(*argv, "permanent") == 0) {
1108 filter.flags |= IFA_F_PERMANENT;
1109 filter.flagmask |= IFA_F_PERMANENT;
Brian Haleya1b9ffc2009-09-14 17:01:43 -04001110 } else if (strcmp(*argv, "secondary") == 0 ||
1111 strcmp(*argv, "temporary") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001112 filter.flags |= IFA_F_SECONDARY;
1113 filter.flagmask |= IFA_F_SECONDARY;
1114 } else if (strcmp(*argv, "primary") == 0) {
1115 filter.flags &= ~IFA_F_SECONDARY;
1116 filter.flagmask |= IFA_F_SECONDARY;
1117 } else if (strcmp(*argv, "tentative") == 0) {
1118 filter.flags |= IFA_F_TENTATIVE;
1119 filter.flagmask |= IFA_F_TENTATIVE;
1120 } else if (strcmp(*argv, "deprecated") == 0) {
1121 filter.flags |= IFA_F_DEPRECATED;
1122 filter.flagmask |= IFA_F_DEPRECATED;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001123 } else if (strcmp(*argv, "home") == 0) {
1124 filter.flags |= IFA_F_HOMEADDRESS;
1125 filter.flagmask |= IFA_F_HOMEADDRESS;
1126 } else if (strcmp(*argv, "nodad") == 0) {
1127 filter.flags |= IFA_F_NODAD;
1128 filter.flagmask |= IFA_F_NODAD;
Brian Haleya1f27792009-12-03 10:39:36 +00001129 } else if (strcmp(*argv, "dadfailed") == 0) {
1130 filter.flags |= IFA_F_DADFAILED;
1131 filter.flagmask |= IFA_F_DADFAILED;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001132 } else if (strcmp(*argv, "label") == 0) {
1133 NEXT_ARG();
1134 filter.label = *argv;
Vlad Dogaruf960c922011-02-02 20:23:40 +02001135 } else if (strcmp(*argv, "group") == 0) {
1136 NEXT_ARG();
1137 if (rtnl_group_a2n(&filter.group, *argv))
1138 invarg("Invalid \"group\" value\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001139 } else {
1140 if (strcmp(*argv, "dev") == 0) {
1141 NEXT_ARG();
1142 }
1143 if (matches(*argv, "help") == 0)
1144 usage();
1145 if (filter_dev)
1146 duparg2("dev", *argv);
1147 filter_dev = *argv;
1148 }
1149 argv++; argc--;
1150 }
1151
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001152 if (filter_dev) {
1153 filter.ifindex = ll_name_to_index(filter_dev);
1154 if (filter.ifindex <= 0) {
1155 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
1156 return -1;
1157 }
1158 }
1159
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001160 if (action == IPADD_FLUSH)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001161 return ipaddr_flush();
1162
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001163 if (action == IPADD_SAVE) {
1164 if (ipadd_save_prep())
1165 exit(1);
1166
1167 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETADDR) < 0) {
1168 perror("Cannot send dump request");
1169 exit(1);
1170 }
1171
1172 if (rtnl_dump_filter(&rth, save_nlmsg, stdout) < 0) {
1173 fprintf(stderr, "Save terminated\n");
1174 exit(1);
1175 }
1176
1177 exit(0);
1178 }
1179
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001180 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
1181 perror("Cannot send dump request");
1182 exit(1);
1183 }
1184
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001185 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001186 fprintf(stderr, "Dump terminated\n");
1187 exit(1);
1188 }
1189
Mike Frysingeraf9d4062012-08-13 08:09:52 -07001190 if (filter.family != AF_PACKET) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001191 if (filter.oneline)
1192 no_link = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001193
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001194 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1195 perror("Cannot send dump request");
1196 exit(1);
1197 }
1198
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001199 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001200 fprintf(stderr, "Dump terminated\n");
1201 exit(1);
1202 }
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001203
1204 ipaddr_filter(&linfo, &ainfo);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001205 }
1206
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001207 for (l = linfo.head; l; l = l->next) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001208 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
1209 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
1210 if (filter.family != AF_PACKET)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001211 print_selected_addrinfo(ifi->ifi_index,
1212 ainfo.head, stdout);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001213 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001214 }
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001215 fflush(stdout);
1216
1217 free_nlmsg_chain(&ainfo);
1218 free_nlmsg_chain(&linfo);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001219
shemminger351efcd2005-09-01 19:21:50 +00001220 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001221}
1222
1223int ipaddr_list_link(int argc, char **argv)
1224{
1225 preferred_family = AF_PACKET;
1226 do_link = 1;
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001227 return ipaddr_list_flush_or_save(argc, argv, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001228}
1229
1230void ipaddr_reset_filter(int oneline)
1231{
1232 memset(&filter, 0, sizeof(filter));
1233 filter.oneline = oneline;
1234}
1235
Stephen Hemminger3d866ba2008-03-14 15:30:03 -07001236static int default_scope(inet_prefix *lcl)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001237{
1238 if (lcl->family == AF_INET) {
1239 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
1240 return RT_SCOPE_HOST;
1241 }
1242 return 0;
1243}
1244
Stephen Hemminger3d866ba2008-03-14 15:30:03 -07001245static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001246{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001247 struct {
1248 struct nlmsghdr n;
1249 struct ifaddrmsg ifa;
1250 char buf[256];
1251 } req;
1252 char *d = NULL;
1253 char *l = NULL;
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001254 char *lcl_arg = NULL;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001255 char *valid_lftp = NULL;
1256 char *preferred_lftp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001257 inet_prefix lcl;
1258 inet_prefix peer;
1259 int local_len = 0;
1260 int peer_len = 0;
1261 int brd_len = 0;
1262 int any_len = 0;
1263 int scoped = 0;
Masahide NAKAMURA141bb602006-11-24 12:27:01 +09001264 __u32 preferred_lft = INFINITY_LIFE_TIME;
1265 __u32 valid_lft = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001266 struct ifa_cacheinfo cinfo;
Jiri Pirko37c9b942014-01-06 10:17:09 +01001267 unsigned int ifa_flags = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001268
1269 memset(&req, 0, sizeof(req));
1270
1271 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001272 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001273 req.n.nlmsg_type = cmd;
1274 req.ifa.ifa_family = preferred_family;
1275
1276 while (argc > 0) {
1277 if (strcmp(*argv, "peer") == 0 ||
1278 strcmp(*argv, "remote") == 0) {
1279 NEXT_ARG();
1280
1281 if (peer_len)
1282 duparg("peer", *argv);
1283 get_prefix(&peer, *argv, req.ifa.ifa_family);
1284 peer_len = peer.bytelen;
1285 if (req.ifa.ifa_family == AF_UNSPEC)
1286 req.ifa.ifa_family = peer.family;
1287 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
1288 req.ifa.ifa_prefixlen = peer.bitlen;
1289 } else if (matches(*argv, "broadcast") == 0 ||
1290 strcmp(*argv, "brd") == 0) {
1291 inet_prefix addr;
1292 NEXT_ARG();
1293 if (brd_len)
1294 duparg("broadcast", *argv);
1295 if (strcmp(*argv, "+") == 0)
1296 brd_len = -1;
1297 else if (strcmp(*argv, "-") == 0)
1298 brd_len = -2;
1299 else {
1300 get_addr(&addr, *argv, req.ifa.ifa_family);
1301 if (req.ifa.ifa_family == AF_UNSPEC)
1302 req.ifa.ifa_family = addr.family;
1303 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
1304 brd_len = addr.bytelen;
1305 }
1306 } else if (strcmp(*argv, "anycast") == 0) {
1307 inet_prefix addr;
1308 NEXT_ARG();
1309 if (any_len)
1310 duparg("anycast", *argv);
1311 get_addr(&addr, *argv, req.ifa.ifa_family);
1312 if (req.ifa.ifa_family == AF_UNSPEC)
1313 req.ifa.ifa_family = addr.family;
1314 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
1315 any_len = addr.bytelen;
1316 } else if (strcmp(*argv, "scope") == 0) {
shemmingerf332d162005-07-05 22:37:15 +00001317 unsigned scope = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001318 NEXT_ARG();
1319 if (rtnl_rtscope_a2n(&scope, *argv))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +00001320 invarg("invalid scope value.", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001321 req.ifa.ifa_scope = scope;
1322 scoped = 1;
1323 } else if (strcmp(*argv, "dev") == 0) {
1324 NEXT_ARG();
1325 d = *argv;
1326 } else if (strcmp(*argv, "label") == 0) {
1327 NEXT_ARG();
1328 l = *argv;
1329 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001330 } else if (matches(*argv, "valid_lft") == 0) {
1331 if (valid_lftp)
1332 duparg("valid_lft", *argv);
1333 NEXT_ARG();
1334 valid_lftp = *argv;
1335 if (set_lifetime(&valid_lft, *argv))
1336 invarg("valid_lft value", *argv);
1337 } else if (matches(*argv, "preferred_lft") == 0) {
1338 if (preferred_lftp)
1339 duparg("preferred_lft", *argv);
1340 NEXT_ARG();
1341 preferred_lftp = *argv;
1342 if (set_lifetime(&preferred_lft, *argv))
1343 invarg("preferred_lft value", *argv);
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001344 } else if (strcmp(*argv, "home") == 0) {
Jiri Pirko37c9b942014-01-06 10:17:09 +01001345 ifa_flags |= IFA_F_HOMEADDRESS;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001346 } else if (strcmp(*argv, "nodad") == 0) {
Jiri Pirko37c9b942014-01-06 10:17:09 +01001347 ifa_flags |= IFA_F_NODAD;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001348 } else {
1349 if (strcmp(*argv, "local") == 0) {
1350 NEXT_ARG();
1351 }
1352 if (matches(*argv, "help") == 0)
1353 usage();
1354 if (local_len)
1355 duparg2("local", *argv);
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001356 lcl_arg = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001357 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1358 if (req.ifa.ifa_family == AF_UNSPEC)
1359 req.ifa.ifa_family = lcl.family;
1360 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1361 local_len = lcl.bytelen;
1362 }
1363 argc--; argv++;
1364 }
Jiri Pirko37c9b942014-01-06 10:17:09 +01001365 req.ifa.ifa_flags = ifa_flags;
1366 addattr32(&req.n, sizeof(req), IFA_FLAGS, ifa_flags);
1367
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001368 if (d == NULL) {
1369 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1370 return -1;
1371 }
1372 if (l && matches(d, l) != 0) {
1373 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
Michele Petrazzo - Unipex1db61e02010-03-06 08:56:53 +00001374 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001375 }
1376
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001377 if (peer_len == 0 && local_len) {
1378 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
1379 fprintf(stderr,
1380 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1381 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1382 " This special behaviour is likely to disappear in further releases,\n" \
1383 " fix your scripts!\n", lcl_arg, local_len*8);
1384 } else {
1385 peer = lcl;
1386 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
1387 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001388 }
1389 if (req.ifa.ifa_prefixlen == 0)
1390 req.ifa.ifa_prefixlen = lcl.bitlen;
1391
1392 if (brd_len < 0 && cmd != RTM_DELADDR) {
1393 inet_prefix brd;
1394 int i;
1395 if (req.ifa.ifa_family != AF_INET) {
1396 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
1397 return -1;
1398 }
1399 brd = peer;
1400 if (brd.bitlen <= 30) {
1401 for (i=31; i>=brd.bitlen; i--) {
1402 if (brd_len == -1)
1403 brd.data[0] |= htonl(1<<(31-i));
1404 else
1405 brd.data[0] &= ~htonl(1<<(31-i));
1406 }
1407 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
1408 brd_len = brd.bytelen;
1409 }
1410 }
1411 if (!scoped && cmd != RTM_DELADDR)
1412 req.ifa.ifa_scope = default_scope(&lcl);
1413
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001414 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
1415 fprintf(stderr, "Cannot find device \"%s\"\n", d);
1416 return -1;
1417 }
1418
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001419 if (valid_lftp || preferred_lftp) {
1420 if (!valid_lft) {
1421 fprintf(stderr, "valid_lft is zero\n");
1422 return -1;
1423 }
1424 if (valid_lft < preferred_lft) {
1425 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
1426 return -1;
1427 }
1428
1429 memset(&cinfo, 0, sizeof(cinfo));
1430 cinfo.ifa_prefered = preferred_lft;
1431 cinfo.ifa_valid = valid_lft;
1432 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
1433 sizeof(cinfo));
1434 }
1435
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001436 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
Michele Petrazzo - Unipex1db61e02010-03-06 08:56:53 +00001437 return -2;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001438
shemminger351efcd2005-09-01 19:21:50 +00001439 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001440}
1441
1442int do_ipaddr(int argc, char **argv)
1443{
1444 if (argc < 1)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001445 return ipaddr_list_flush_or_save(0, NULL, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001446 if (matches(*argv, "add") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001447 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
1448 if (matches(*argv, "change") == 0 ||
1449 strcmp(*argv, "chg") == 0)
1450 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
1451 if (matches(*argv, "replace") == 0)
1452 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001453 if (matches(*argv, "delete") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001454 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001455 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1456 || matches(*argv, "lst") == 0)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001457 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001458 if (matches(*argv, "flush") == 0)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001459 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_FLUSH);
1460 if (matches(*argv, "save") == 0)
1461 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_SAVE);
1462 if (matches(*argv, "showdump") == 0)
1463 return ipaddr_showdump();
1464 if (matches(*argv, "restore") == 0)
1465 return ipaddr_restore();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001466 if (matches(*argv, "help") == 0)
1467 usage();
Alexander Wirtb096fa52007-10-12 10:56:36 +02001468 fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001469 exit(-1);
1470}