blob: 88e966222e3d82fe37b2f8fba9e5f3171200310f [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>
Kylie McClain110e84a2016-05-22 19:52:02 -040022#include <sys/param.h>
Strake5bd9dd42012-12-23 08:46:04 -050023#include <errno.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000024#include <netinet/in.h>
25#include <arpa/inet.h>
26#include <string.h>
27#include <fnmatch.h>
28
osdl.org!shemmingere5779fb2004-06-09 22:56:28 +000029#include <linux/netdevice.h>
30#include <linux/if_arp.h>
31#include <linux/sockios.h>
Nicolas Dichtelccdcbf32015-02-17 17:30:38 +010032#include <linux/net_namespace.h>
osdl.org!shemmingere5779fb2004-06-09 22:56:28 +000033
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000034#include "rt_names.h"
35#include "utils.h"
36#include "ll_map.h"
37#include "ip_common.h"
Mathias Nymand7bd2db2015-04-28 13:18:21 +030038#include "color.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000039
Pavel Emelyanov81824ac2012-09-11 19:47:00 +040040enum {
41 IPADD_LIST,
42 IPADD_FLUSH,
43 IPADD_SAVE,
44};
Daniel Silverstone7b3d3662007-10-19 13:32:24 +020045
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000046static struct
47{
48 int ifindex;
49 int family;
50 int oneline;
51 int showqueue;
52 inet_prefix pfx;
53 int scope, scopemask;
54 int flags, flagmask;
55 int up;
56 char *label;
57 int flushed;
58 char *flushb;
59 int flushp;
60 int flushe;
Vlad Dogaruf960c922011-02-02 20:23:40 +020061 int group;
Vadim Kochan1b944142014-11-29 11:27:41 -080062 int master;
Vadim Kochan712249d2014-12-10 10:11:29 +020063 char *kind;
Phil Suttere0513802016-06-28 15:07:16 +020064 char *slave_kind;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000065} filter;
66
67static int do_link;
68
69static void usage(void) __attribute__((noreturn));
70
71static void usage(void)
72{
73 if (do_link) {
74 iplink_usage();
75 }
Pavel Šimerda4e972d52015-08-12 22:04:08 +020076 fprintf(stderr, "Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]\n");
Brian Haleya1f27792009-12-03 10:39:36 +000077 fprintf(stderr, " [ CONFFLAG-LIST ]\n");
Pavel Šimerda4e972d52015-08-12 22:04:08 +020078 fprintf(stderr, " ip address del IFADDR dev IFNAME [mngtmpaddr]\n");
Phil Sutter24604eb2016-06-09 19:20:36 +020079 fprintf(stderr, " ip address {save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]\n");
Pavel Šimerda4e972d52015-08-12 22:04:08 +020080 fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]\n");
Phil Sutter24604eb2016-06-09 19:20:36 +020081 fprintf(stderr, " ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ master DEVICE ]\n");
82 fprintf(stderr, " [ type TYPE ] [ to PREFIX ] [ FLAG-LIST ]\n");
David Ahern104444c2016-06-29 11:26:58 -070083 fprintf(stderr, " [ label LABEL ] [up] [ vrf NAME ] ]\n");
Pavel Šimerda4e972d52015-08-12 22:04:08 +020084 fprintf(stderr, " ip address {showdump|restore}\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000085 fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n");
86 fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n");
Pavel Šimerda4e972d52015-08-12 22:04:08 +020087 fprintf(stderr, " [ label IFNAME ] [ scope SCOPE-ID ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000088 fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
89 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
90 fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n");
Heiner Kallweitb5f39b22014-12-22 20:18:43 +010091 fprintf(stderr, " [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n");
Brian Haleya1f27792009-12-03 10:39:36 +000092 fprintf(stderr, " CONFFLAG-LIST ]\n");
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +090093 fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
Madhu Challae31867a2015-03-04 10:30:10 -080094 fprintf(stderr, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n");
Masahide NAKAMURA35546df2006-11-24 12:26:55 +090095 fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
96 fprintf(stderr, "LFT := forever | SECONDS\n");
Phil Sutter24604eb2016-06-09 19:20:36 +020097 fprintf(stderr, "TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | macvtap |\n");
Phil Sutter577cfe02016-06-28 15:07:17 +020098 fprintf(stderr, " bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | lowpan |\n");
99 fprintf(stderr, " gre | gretap | ip6gre | ip6gretap | vti | nlmon | can |\n");
100 fprintf(stderr, " bond_slave | ipvlan | geneve | bridge_slave | vrf | hsr}\n");
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900101
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000102 exit(-1);
103}
104
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700105static void print_link_flags(FILE *fp, unsigned int flags, unsigned int mdown)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000106{
107 fprintf(fp, "<");
net[shemminger]!shemminger73b49e92005-03-14 18:47:38 +0000108 if (flags & IFF_UP && !(flags & IFF_RUNNING))
109 fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000110 flags &= ~IFF_RUNNING;
111#define _PF(f) if (flags&IFF_##f) { \
Stephen Hemminger1124ffb2013-03-14 13:47:49 -0700112 flags &= ~IFF_##f ; \
113 fprintf(fp, #f "%s", flags ? "," : ""); }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000114 _PF(LOOPBACK);
115 _PF(BROADCAST);
116 _PF(POINTOPOINT);
117 _PF(MULTICAST);
118 _PF(NOARP);
119 _PF(ALLMULTI);
120 _PF(PROMISC);
121 _PF(MASTER);
122 _PF(SLAVE);
123 _PF(DEBUG);
124 _PF(DYNAMIC);
125 _PF(AUTOMEDIA);
126 _PF(PORTSEL);
127 _PF(NOTRAILERS);
128 _PF(UP);
Thomas Grafdcb283c2007-06-19 16:40:40 -0700129 _PF(LOWER_UP);
130 _PF(DORMANT);
Oliver Hartkopp98f9a1d2009-03-27 11:21:29 -0700131 _PF(ECHO);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000132#undef _PF
Stephen Hemminger1124ffb2013-03-14 13:47:49 -0700133 if (flags)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000134 fprintf(fp, "%x", flags);
135 if (mdown)
136 fprintf(fp, ",M-DOWN");
137 fprintf(fp, "> ");
138}
139
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700140static const char *oper_states[] = {
Stephen Hemminger06125192014-02-17 10:55:31 -0800141 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700142 "TESTING", "DORMANT", "UP"
143};
144
145static void print_operstate(FILE *f, __u8 state)
146{
Phil Suttera1121aa2016-03-22 19:35:13 +0100147 if (state >= ARRAY_SIZE(oper_states)) {
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700148 fprintf(f, "state %#x ", state);
Phil Suttera1121aa2016-03-22 19:35:13 +0100149 } else if (brief) {
150 color_fprintf(f, oper_state_color(state),
151 "%-14s ", oper_states[state]);
152 } else {
153 fprintf(f, "state ");
154 color_fprintf(f, oper_state_color(state),
155 "%s ", oper_states[state]);
Mathias Nymand7bd2db2015-04-28 13:18:21 +0300156 }
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700157}
158
Stephen Hemminger4f2fdd42012-04-05 15:08:57 -0700159int get_operstate(const char *name)
160{
161 int i;
162
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700163 for (i = 0; i < ARRAY_SIZE(oper_states); i++)
Stephen Hemminger4f2fdd42012-04-05 15:08:57 -0700164 if (strcasecmp(name, oper_states[i]) == 0)
165 return i;
166 return -1;
167}
168
Eric Dumazetf78e3162009-10-22 18:13:21 +0000169static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1])
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000170{
Eric Dumazetf78e3162009-10-22 18:13:21 +0000171 int qlen;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000172
Eric Dumazetf78e3162009-10-22 18:13:21 +0000173 if (tb[IFLA_TXQLEN])
174 qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]);
175 else {
176 struct ifreq ifr;
177 int s = socket(AF_INET, SOCK_STREAM, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000178
Eric Dumazetf78e3162009-10-22 18:13:21 +0000179 if (s < 0)
180 return;
181
182 memset(&ifr, 0, sizeof(ifr));
Stephen Hemmingerff247462012-04-10 08:47:55 -0700183 strcpy(ifr.ifr_name, rta_getattr_str(tb[IFLA_IFNAME]));
Eric Dumazetf78e3162009-10-22 18:13:21 +0000184 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
Nicolas Dichteld3603512013-01-29 08:46:42 -0800185 fprintf(f, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno));
Eric Dumazetf78e3162009-10-22 18:13:21 +0000186 close(s);
187 return;
188 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000189 close(s);
Eric Dumazetf78e3162009-10-22 18:13:21 +0000190 qlen = ifr.ifr_qlen;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000191 }
Eric Dumazetf78e3162009-10-22 18:13:21 +0000192 if (qlen)
193 fprintf(f, "qlen %d", qlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000194}
195
Stephen Hemminger82499282012-03-19 17:24:43 -0700196static const char *link_modes[] = {
197 "DEFAULT", "DORMANT"
198};
199
200static void print_linkmode(FILE *f, struct rtattr *tb)
201{
202 unsigned int mode = rta_getattr_u8(tb);
203
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700204 if (mode >= ARRAY_SIZE(link_modes))
Stephen Hemminger82499282012-03-19 17:24:43 -0700205 fprintf(f, "mode %d ", mode);
206 else
207 fprintf(f, "mode %s ", link_modes[mode]);
208}
209
Phil Suttere0513802016-06-28 15:07:16 +0200210static char *parse_link_kind(struct rtattr *tb, bool slave)
Vadim Kochan712249d2014-12-10 10:11:29 +0200211{
212 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
Phil Suttere0513802016-06-28 15:07:16 +0200213 int attr = slave ? IFLA_INFO_SLAVE_KIND : IFLA_INFO_KIND;
Vadim Kochan712249d2014-12-10 10:11:29 +0200214
215 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
216
Phil Suttere0513802016-06-28 15:07:16 +0200217 if (linkinfo[attr])
218 return RTA_DATA(linkinfo[attr]);
Vadim Kochan712249d2014-12-10 10:11:29 +0200219
220 return "";
221}
222
Phil Sutter03ac85b2016-06-30 16:47:02 +0200223static int match_link_kind(struct rtattr **tb, const char *kind, bool slave)
Phil Suttere0513802016-06-28 15:07:16 +0200224{
225 if (!tb[IFLA_LINKINFO])
226 return -1;
227
228 return strcmp(parse_link_kind(tb[IFLA_LINKINFO], slave), kind);
229}
230
Patrick McHardy1d934832007-08-22 10:49:01 -0700231static void print_linktype(FILE *fp, struct rtattr *tb)
232{
233 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
234 struct link_util *lu;
Jiri Pirkofbea6112014-01-23 17:52:53 +0100235 struct link_util *slave_lu;
Patrick McHardy1d934832007-08-22 10:49:01 -0700236 char *kind;
Jiri Pirkofbea6112014-01-23 17:52:53 +0100237 char *slave_kind;
Patrick McHardy1d934832007-08-22 10:49:01 -0700238
239 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
240
Jiri Pirkofbea6112014-01-23 17:52:53 +0100241 if (linkinfo[IFLA_INFO_KIND]) {
242 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
Patrick McHardy1d934832007-08-22 10:49:01 -0700243
Jiri Pirkofbea6112014-01-23 17:52:53 +0100244 fprintf(fp, "%s", _SL_);
245 fprintf(fp, " %s ", kind);
Patrick McHardy1d934832007-08-22 10:49:01 -0700246
Jiri Pirkofbea6112014-01-23 17:52:53 +0100247 lu = get_link_kind(kind);
248 if (lu && lu->print_opt) {
249 struct rtattr *attr[lu->maxattr+1], **data = NULL;
Patrick McHardy1d934832007-08-22 10:49:01 -0700250
Jiri Pirkofbea6112014-01-23 17:52:53 +0100251 if (linkinfo[IFLA_INFO_DATA]) {
252 parse_rtattr_nested(attr, lu->maxattr,
253 linkinfo[IFLA_INFO_DATA]);
254 data = attr;
255 }
256 lu->print_opt(lu, fp, data);
Patrick McHardy1d934832007-08-22 10:49:01 -0700257
Jiri Pirkofbea6112014-01-23 17:52:53 +0100258 if (linkinfo[IFLA_INFO_XSTATS] && show_stats &&
259 lu->print_xstats)
260 lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]);
Patrick McHardy1d934832007-08-22 10:49:01 -0700261 }
Jiri Pirkofbea6112014-01-23 17:52:53 +0100262 }
Patrick McHardy1d934832007-08-22 10:49:01 -0700263
Jiri Pirkofbea6112014-01-23 17:52:53 +0100264 if (linkinfo[IFLA_INFO_SLAVE_KIND]) {
265 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
266
267 fprintf(fp, "%s", _SL_);
268 fprintf(fp, " %s_slave ", slave_kind);
269
270 slave_lu = get_link_slave_kind(slave_kind);
271 if (slave_lu && slave_lu->print_opt) {
272 struct rtattr *attr[slave_lu->maxattr+1], **data = NULL;
273
274 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
275 parse_rtattr_nested(attr, slave_lu->maxattr,
276 linkinfo[IFLA_INFO_SLAVE_DATA]);
277 data = attr;
278 }
279 slave_lu->print_opt(slave_lu, fp, data);
280 }
Patrick McHardy1d934832007-08-22 10:49:01 -0700281 }
282}
283
Jiri Pirkoee0067a2015-01-08 09:49:55 +0100284static void print_af_spec(FILE *fp, struct rtattr *af_spec_attr)
285{
286 struct rtattr *inet6_attr;
287 struct rtattr *tb[IFLA_INET6_MAX + 1];
288
289 inet6_attr = parse_rtattr_one_nested(AF_INET6, af_spec_attr);
290 if (!inet6_attr)
291 return;
292
293 parse_rtattr_nested(tb, IFLA_INET6_MAX, inet6_attr);
294
295 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
Hannes Frederic Sowa5c5176c2015-12-16 10:52:36 +0100296 __u8 mode = rta_getattr_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700297
Hannes Frederic Sowa5c5176c2015-12-16 10:52:36 +0100298 switch (mode) {
Jiri Pirkoee0067a2015-01-08 09:49:55 +0100299 case IN6_ADDR_GEN_MODE_EUI64:
300 fprintf(fp, "addrgenmode eui64 ");
301 break;
302 case IN6_ADDR_GEN_MODE_NONE:
303 fprintf(fp, "addrgenmode none ");
304 break;
Hannes Frederic Sowa5c5176c2015-12-16 10:52:36 +0100305 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
306 fprintf(fp, "addrgenmode stable_secret ");
307 break;
Bjørn Mork8e12bc02016-01-04 10:58:05 +0100308 case IN6_ADDR_GEN_MODE_RANDOM:
309 fprintf(fp, "addrgenmode random ");
310 break;
Hannes Frederic Sowa5c5176c2015-12-16 10:52:36 +0100311 default:
312 fprintf(fp, "addrgenmode %#.2hhx ", mode);
313 break;
Jiri Pirkoee0067a2015-01-08 09:49:55 +0100314 }
315 }
316}
317
Eran Ben Elishaa1b99712015-06-16 12:13:16 +0300318static void print_vf_stats64(FILE *fp, struct rtattr *vfstats);
319
Chris Wright3fd86632010-05-18 00:57:00 -0700320static void print_vfinfo(FILE *fp, struct rtattr *vfinfo)
321{
322 struct ifla_vf_mac *vf_mac;
323 struct ifla_vf_vlan *vf_vlan;
324 struct ifla_vf_tx_rate *vf_tx_rate;
Greg Rose7b8179c2011-10-13 20:31:32 +0000325 struct ifla_vf_spoofchk *vf_spoofchk;
Rony Efraim07fa9c12013-06-13 13:19:12 +0300326 struct ifla_vf_link_state *vf_linkstate;
Vadim Kochan8c29ae72015-01-09 21:24:31 +0200327 struct rtattr *vf[IFLA_VF_MAX + 1] = {};
Greg Rose7b8179c2011-10-13 20:31:32 +0000328 struct rtattr *tmp;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700329
Chris Wright3fd86632010-05-18 00:57:00 -0700330 SPRINT_BUF(b1);
331
332 if (vfinfo->rta_type != IFLA_VF_INFO) {
333 fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type);
334 return;
335 }
336
337 parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo);
338
339 vf_mac = RTA_DATA(vf[IFLA_VF_MAC]);
340 vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
341 vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]);
342
Greg Rose7b8179c2011-10-13 20:31:32 +0000343 /* Check if the spoof checking vf info type is supported by
344 * this kernel.
345 */
346 tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] +
Stephen Hemmingerbbac6c62016-04-11 22:13:55 +0000347 vf[IFLA_VF_TX_RATE]->rta_len);
Greg Rose7b8179c2011-10-13 20:31:32 +0000348
349 if (tmp->rta_type != IFLA_VF_SPOOFCHK)
350 vf_spoofchk = NULL;
351 else
352 vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
353
Rony Efraim07fa9c12013-06-13 13:19:12 +0300354 if (vf_spoofchk) {
355 /* Check if the link state vf info type is supported by
356 * this kernel.
357 */
358 tmp = (struct rtattr *)((char *)vf[IFLA_VF_SPOOFCHK] +
Stephen Hemmingerbbac6c62016-04-11 22:13:55 +0000359 vf[IFLA_VF_SPOOFCHK]->rta_len);
Rony Efraim07fa9c12013-06-13 13:19:12 +0300360
361 if (tmp->rta_type != IFLA_VF_LINK_STATE)
362 vf_linkstate = NULL;
363 else
364 vf_linkstate = RTA_DATA(vf[IFLA_VF_LINK_STATE]);
365 } else
366 vf_linkstate = NULL;
367
Phil Sutter6f07f3d2015-10-16 12:38:33 +0200368 fprintf(fp, "%s vf %d MAC %s", _SL_, vf_mac->vf,
Chris Wright3fd86632010-05-18 00:57:00 -0700369 ll_addr_n2a((unsigned char *)&vf_mac->mac,
Stephen Hemmingerbbac6c62016-04-11 22:13:55 +0000370 ETH_ALEN, 0, b1, sizeof(b1)));
Chris Wright3fd86632010-05-18 00:57:00 -0700371 if (vf_vlan->vlan)
372 fprintf(fp, ", vlan %d", vf_vlan->vlan);
373 if (vf_vlan->qos)
374 fprintf(fp, ", qos %d", vf_vlan->qos);
375 if (vf_tx_rate->rate)
376 fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate);
Vadim Kochan8c29ae72015-01-09 21:24:31 +0200377
378 if (vf[IFLA_VF_RATE]) {
379 struct ifla_vf_rate *vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
380
381 if (vf_rate->max_tx_rate)
382 fprintf(fp, ", max_tx_rate %dMbps", vf_rate->max_tx_rate);
383 if (vf_rate->min_tx_rate)
384 fprintf(fp, ", min_tx_rate %dMbps", vf_rate->min_tx_rate);
385 }
386
Greg Rose7b8179c2011-10-13 20:31:32 +0000387 if (vf_spoofchk && vf_spoofchk->setting != -1) {
388 if (vf_spoofchk->setting)
389 fprintf(fp, ", spoof checking on");
390 else
391 fprintf(fp, ", spoof checking off");
392 }
Rony Efraim07fa9c12013-06-13 13:19:12 +0300393 if (vf_linkstate) {
394 if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_AUTO)
395 fprintf(fp, ", link-state auto");
396 else if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_ENABLE)
397 fprintf(fp, ", link-state enable");
398 else
399 fprintf(fp, ", link-state disable");
400 }
Phil Sutterfe932272016-03-31 14:43:32 +0200401 if (vf[IFLA_VF_TRUST]) {
402 struct ifla_vf_trust *vf_trust = RTA_DATA(vf[IFLA_VF_TRUST]);
403
404 if (vf_trust->setting != -1)
405 fprintf(fp, ", trust %s",
406 vf_trust->setting ? "on" : "off");
407 }
Eran Ben Elishaa1b99712015-06-16 12:13:16 +0300408 if (vf[IFLA_VF_STATS] && show_stats)
409 print_vf_stats64(fp, vf[IFLA_VF_STATS]);
Chris Wright3fd86632010-05-18 00:57:00 -0700410}
411
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700412static void print_num(FILE *fp, unsigned int width, uint64_t count)
Christian Hesseb68d9832014-10-31 22:33:13 +0100413{
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800414 const char *prefix = "kMGTPE";
415 const unsigned int base = use_iec ? 1024 : 1000;
Christian Hesseb68d9832014-10-31 22:33:13 +0100416 uint64_t powi = 1;
Christian Hesse50ec6652014-11-04 22:17:15 +0100417 uint16_t powj = 1;
418 uint8_t precision = 2;
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800419 char buf[64];
Christian Hesseb68d9832014-10-31 22:33:13 +0100420
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800421 if (!human_readable || count < base) {
Christian Hessef4fe81d2014-11-03 08:21:11 +0100422 fprintf(fp, "%-*"PRIu64" ", width, count);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800423 return;
Christian Hesseb68d9832014-10-31 22:33:13 +0100424 }
425
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800426 /* increase value by a factor of 1000/1024 and print
427 * if result is something a human can read */
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700428 for (;;) {
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800429 powi *= base;
430 if (count / base < powi)
431 break;
Christian Hesseb68d9832014-10-31 22:33:13 +0100432
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800433 if (!prefix[1])
434 break;
435 ++prefix;
Christian Hesseb68d9832014-10-31 22:33:13 +0100436 }
437
Christian Hesse50ec6652014-11-04 22:17:15 +0100438 /* try to guess a good number of digits for precision */
439 for (; precision > 0; precision--) {
440 powj *= 10;
441 if (count / powi < powj)
442 break;
443 }
444
445 snprintf(buf, sizeof(buf), "%.*f%c%s", precision,
446 (double) count / powi, *prefix, use_iec ? "i" : "");
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800447
Christian Hessef4fe81d2014-11-03 08:21:11 +0100448 fprintf(fp, "%-*s ", width, buf);
Christian Hesseb68d9832014-10-31 22:33:13 +0100449}
450
Eran Ben Elishaa1b99712015-06-16 12:13:16 +0300451static void print_vf_stats64(FILE *fp, struct rtattr *vfstats)
452{
453 struct rtattr *vf[IFLA_VF_STATS_MAX + 1] = {};
454
455 if (vfstats->rta_type != IFLA_VF_STATS) {
456 fprintf(stderr, "BUG: rta type is %d\n", vfstats->rta_type);
457 return;
458 }
459
460 parse_rtattr_nested(vf, IFLA_VF_MAX, vfstats);
461
462 /* RX stats */
463 fprintf(fp, "%s", _SL_);
464 fprintf(fp, " RX: bytes packets mcast bcast %s", _SL_);
465 fprintf(fp, " ");
466
467 print_num(fp, 10, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_RX_BYTES]));
468 print_num(fp, 8, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_RX_PACKETS]));
469 print_num(fp, 7, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_MULTICAST]));
470 print_num(fp, 7, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_BROADCAST]));
471
472 /* TX stats */
473 fprintf(fp, "%s", _SL_);
474 fprintf(fp, " TX: bytes packets %s", _SL_);
475 fprintf(fp, " ");
476
477 print_num(fp, 10, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_TX_BYTES]));
478 print_num(fp, 8, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_TX_PACKETS]));
479}
480
david decotigny30b55792014-05-05 20:38:18 -0700481static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s,
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700482 const struct rtattr *carrier_changes)
Stephen Hemminger4b726cb2014-05-09 12:36:46 -0700483{
Christian Hesseb68d9832014-10-31 22:33:13 +0100484 /* RX stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800485 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
486 s->rx_compressed ? "compressed" : "", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800487
488 fprintf(fp, " ");
489 print_num(fp, 10, s->rx_bytes);
490 print_num(fp, 8, s->rx_packets);
491 print_num(fp, 7, s->rx_errors);
492 print_num(fp, 7, s->rx_dropped);
493 print_num(fp, 7, s->rx_over_errors);
494 print_num(fp, 7, s->multicast);
495 if (s->rx_compressed)
496 print_num(fp, 7, s->rx_compressed);
Christian Hesseb68d9832014-10-31 22:33:13 +0100497
498 /* RX error stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800499 if (show_stats > 1) {
500 fprintf(fp, "%s", _SL_);
Stephen Hemminger07ec1832016-02-09 11:13:31 -0800501 fprintf(fp, " RX errors: length crc frame fifo missed%s%s",
502 s->rx_nohandler ? " nohandler" : "", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800503
504 fprintf(fp, " ");
505 print_num(fp, 8, s->rx_length_errors);
506 print_num(fp, 7, s->rx_crc_errors);
507 print_num(fp, 7, s->rx_frame_errors);
508 print_num(fp, 7, s->rx_fifo_errors);
509 print_num(fp, 7, s->rx_missed_errors);
Stephen Hemminger07ec1832016-02-09 11:13:31 -0800510 if (s->rx_nohandler)
511 print_num(fp, 7, s->rx_nohandler);
512
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800513 }
514 fprintf(fp, "%s", _SL_);
Christian Hesseb68d9832014-10-31 22:33:13 +0100515
516 /* TX stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800517 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800518 s->tx_compressed ? "compressed" : "", _SL_);
519
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800520 fprintf(fp, " ");
521 print_num(fp, 10, s->tx_bytes);
522 print_num(fp, 8, s->tx_packets);
523 print_num(fp, 7, s->tx_errors);
524 print_num(fp, 7, s->tx_dropped);
525 print_num(fp, 7, s->tx_carrier_errors);
526 print_num(fp, 7, s->collisions);
527 if (s->tx_compressed)
528 print_num(fp, 7, s->tx_compressed);
Christian Hesseb68d9832014-10-31 22:33:13 +0100529
530 /* TX error stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800531 if (show_stats > 1) {
532 fprintf(fp, "%s", _SL_);
Christian Hesseb68d9832014-10-31 22:33:13 +0100533 fprintf(fp, " TX errors: aborted fifo window heartbeat");
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700534 if (carrier_changes)
david decotigny30b55792014-05-05 20:38:18 -0700535 fprintf(fp, " transns");
Stephen Hemminger4ec0ffd2014-05-29 10:31:30 -0700536 fprintf(fp, "%s", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800537
538 fprintf(fp, " ");
539 print_num(fp, 8, s->tx_aborted_errors);
540 print_num(fp, 7, s->tx_fifo_errors);
541 print_num(fp, 7, s->tx_window_errors);
542 print_num(fp, 7, s->tx_heartbeat_errors);
543 if (carrier_changes)
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700544 print_num(fp, 7, *(uint32_t *)RTA_DATA(carrier_changes));
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800545 }
546}
547
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200548static void print_link_stats32(FILE *fp, const struct rtnl_link_stats *s,
549 const struct rtattr *carrier_changes)
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800550{
Christian Hesseb68d9832014-10-31 22:33:13 +0100551 /* RX stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800552 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
553 s->rx_compressed ? "compressed" : "", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800554
555
556 fprintf(fp, " ");
557 print_num(fp, 10, s->rx_bytes);
558 print_num(fp, 8, s->rx_packets);
559 print_num(fp, 7, s->rx_errors);
560 print_num(fp, 7, s->rx_dropped);
561 print_num(fp, 7, s->rx_over_errors);
562 print_num(fp, 7, s->multicast);
563 if (s->rx_compressed)
564 print_num(fp, 7, s->rx_compressed);
Christian Hesseb68d9832014-10-31 22:33:13 +0100565
566 /* RX error stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800567 if (show_stats > 1) {
568 fprintf(fp, "%s", _SL_);
Stephen Hemminger07ec1832016-02-09 11:13:31 -0800569 fprintf(fp, " RX errors: length crc frame fifo missed%s%s",
570 s->rx_nohandler ? " nohandler" : "", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800571 fprintf(fp, " ");
572 print_num(fp, 8, s->rx_length_errors);
573 print_num(fp, 7, s->rx_crc_errors);
574 print_num(fp, 7, s->rx_frame_errors);
575 print_num(fp, 7, s->rx_fifo_errors);
576 print_num(fp, 7, s->rx_missed_errors);
Stephen Hemminger07ec1832016-02-09 11:13:31 -0800577 if (s->rx_nohandler)
578 print_num(fp, 7, s->rx_nohandler);
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800579 }
580 fprintf(fp, "%s", _SL_);
Christian Hesseb68d9832014-10-31 22:33:13 +0100581
582 /* TX stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800583 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
584 s->tx_compressed ? "compressed" : "", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800585
586 fprintf(fp, " ");
587 print_num(fp, 10, s->tx_bytes);
588 print_num(fp, 8, s->tx_packets);
589 print_num(fp, 7, s->tx_errors);
590 print_num(fp, 7, s->tx_dropped);
591 print_num(fp, 7, s->tx_carrier_errors);
592 print_num(fp, 7, s->collisions);
593 if (s->tx_compressed)
594 print_num(fp, 7, s->tx_compressed);
Christian Hesseb68d9832014-10-31 22:33:13 +0100595
596 /* TX error stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800597 if (show_stats > 1) {
598 fprintf(fp, "%s", _SL_);
Christian Hesseb68d9832014-10-31 22:33:13 +0100599 fprintf(fp, " TX errors: aborted fifo window heartbeat");
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700600 if (carrier_changes)
david decotigny30b55792014-05-05 20:38:18 -0700601 fprintf(fp, " transns");
Stephen Hemminger4ec0ffd2014-05-29 10:31:30 -0700602 fprintf(fp, "%s", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800603
604 fprintf(fp, " ");
605 print_num(fp, 8, s->tx_aborted_errors);
606 print_num(fp, 7, s->tx_fifo_errors);
607 print_num(fp, 7, s->tx_window_errors);
608 print_num(fp, 7, s->tx_heartbeat_errors);
609 if (carrier_changes)
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700610 print_num(fp, 7, *(uint32_t *)RTA_DATA(carrier_changes));
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800611 }
612}
613
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200614static void __print_link_stats(FILE *fp, struct rtattr **tb)
615{
Stephen Hemminger07ec1832016-02-09 11:13:31 -0800616 const struct rtattr *carrier_changes = tb[IFLA_CARRIER_CHANGES];
617
618 if (tb[IFLA_STATS64]) {
619 struct rtnl_link_stats64 stats = { 0 };
620
621 memcpy(&stats, RTA_DATA(tb[IFLA_STATS64]),
622 MIN(RTA_PAYLOAD(tb[IFLA_STATS64]), sizeof(stats)));
623
624 print_link_stats64(fp, &stats, carrier_changes);
625 } else if (tb[IFLA_STATS]) {
626 struct rtnl_link_stats stats = { 0 };
627
628 memcpy(&stats, RTA_DATA(tb[IFLA_STATS]),
629 MIN(RTA_PAYLOAD(tb[IFLA_STATS]), sizeof(stats)));
630
631 print_link_stats32(fp, &stats, carrier_changes);
632 }
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200633}
634
635static void print_link_stats(FILE *fp, struct nlmsghdr *n)
636{
637 struct ifinfomsg *ifi = NLMSG_DATA(n);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700638 struct rtattr *tb[IFLA_MAX+1];
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200639
640 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi),
641 n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
642 __print_link_stats(fp, tb);
643 fprintf(fp, "%s", _SL_);
644}
645
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400646int print_linkinfo_brief(const struct sockaddr_nl *who,
647 struct nlmsghdr *n, void *arg)
648{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700649 FILE *fp = (FILE *)arg;
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400650 struct ifinfomsg *ifi = NLMSG_DATA(n);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700651 struct rtattr *tb[IFLA_MAX+1];
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400652 int len = n->nlmsg_len;
653 char *name;
654 char buf[32] = { 0, };
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700655 unsigned int m_flag = 0;
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400656
657 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
658 return -1;
659
660 len -= NLMSG_LENGTH(sizeof(*ifi));
661 if (len < 0)
662 return -1;
663
664 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
665 return -1;
666 if (filter.up && !(ifi->ifi_flags&IFF_UP))
667 return -1;
668
669 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
670 if (tb[IFLA_IFNAME] == NULL) {
671 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
672 }
673 if (filter.label &&
674 (!filter.family || filter.family == AF_PACKET) &&
675 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
676 return -1;
677
678 if (tb[IFLA_GROUP]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700679 int group = *(int *)RTA_DATA(tb[IFLA_GROUP]);
680
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400681 if (filter.group != -1 && group != filter.group)
682 return -1;
683 }
684
David Ahern84d30af2015-09-23 16:44:56 -0600685 if (tb[IFLA_MASTER]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700686 int master = *(int *)RTA_DATA(tb[IFLA_MASTER]);
687
David Ahern84d30af2015-09-23 16:44:56 -0600688 if (filter.master > 0 && master != filter.master)
689 return -1;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700690 } else if (filter.master > 0)
David Ahern84d30af2015-09-23 16:44:56 -0600691 return -1;
692
Phil Suttere0513802016-06-28 15:07:16 +0200693 if (filter.kind && match_link_kind(tb, filter.kind, 0))
694 return -1;
David Ahern84d30af2015-09-23 16:44:56 -0600695
Phil Suttere0513802016-06-28 15:07:16 +0200696 if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
697 return -1;
David Ahern84d30af2015-09-23 16:44:56 -0600698
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400699 if (n->nlmsg_type == RTM_DELLINK)
700 fprintf(fp, "Deleted ");
701
702 name = (char *)(tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
703
704 if (tb[IFLA_LINK]) {
705 SPRINT_BUF(b1);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700706 int iflink = *(int *)RTA_DATA(tb[IFLA_LINK]);
707
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400708 if (iflink == 0)
709 snprintf(buf, sizeof(buf), "%s@NONE", name);
710 else {
711 snprintf(buf, sizeof(buf),
712 "%s@%s", name, ll_idx_n2a(iflink, b1));
713 m_flag = ll_index_to_flags(iflink);
714 m_flag = !(m_flag & IFF_UP);
715 }
716 } else
717 snprintf(buf, sizeof(buf), "%s", name);
718
719 fprintf(fp, "%-16s ", buf);
720
721 if (tb[IFLA_OPERSTATE])
722 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
723
724 if (filter.family == AF_PACKET) {
725 SPRINT_BUF(b1);
726 if (tb[IFLA_ADDRESS]) {
727 color_fprintf(fp, COLOR_MAC, "%s ",
728 ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
729 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
730 ifi->ifi_type,
731 b1, sizeof(b1)));
732 }
733 }
734
735 if (filter.family == AF_PACKET)
736 print_link_flags(fp, ifi->ifi_flags, m_flag);
737
738 if (filter.family == AF_PACKET)
739 fprintf(fp, "\n");
740 fflush(fp);
741 return 0;
742}
743
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800744int print_linkinfo(const struct sockaddr_nl *who,
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000745 struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000746{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700747 FILE *fp = (FILE *)arg;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000748 struct ifinfomsg *ifi = NLMSG_DATA(n);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700749 struct rtattr *tb[IFLA_MAX+1];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000750 int len = n->nlmsg_len;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700751 unsigned int m_flag = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000752
753 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
754 return 0;
755
756 len -= NLMSG_LENGTH(sizeof(*ifi));
757 if (len < 0)
758 return -1;
759
760 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
761 return 0;
762 if (filter.up && !(ifi->ifi_flags&IFF_UP))
763 return 0;
764
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000765 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
766 if (tb[IFLA_IFNAME] == NULL) {
jamal4cd23bd2008-08-08 10:06:17 -0400767 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000768 }
769 if (filter.label &&
770 (!filter.family || filter.family == AF_PACKET) &&
771 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
772 return 0;
773
Vlad Dogaruf960c922011-02-02 20:23:40 +0200774 if (tb[IFLA_GROUP]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700775 int group = *(int *)RTA_DATA(tb[IFLA_GROUP]);
776
Stefan Tomanekc4fdf752013-08-03 14:20:53 +0200777 if (filter.group != -1 && group != filter.group)
Vlad Dogaruf960c922011-02-02 20:23:40 +0200778 return -1;
779 }
780
Vadim Kochan1b944142014-11-29 11:27:41 -0800781 if (tb[IFLA_MASTER]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700782 int master = *(int *)RTA_DATA(tb[IFLA_MASTER]);
783
Vadim Kochan1b944142014-11-29 11:27:41 -0800784 if (filter.master > 0 && master != filter.master)
785 return -1;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700786 } else if (filter.master > 0)
Vadim Kochan1b944142014-11-29 11:27:41 -0800787 return -1;
788
Phil Suttere0513802016-06-28 15:07:16 +0200789 if (filter.kind && match_link_kind(tb, filter.kind, 0))
790 return -1;
Vadim Kochan712249d2014-12-10 10:11:29 +0200791
Phil Suttere0513802016-06-28 15:07:16 +0200792 if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
793 return -1;
Vadim Kochan712249d2014-12-10 10:11:29 +0200794
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000795 if (n->nlmsg_type == RTM_DELLINK)
796 fprintf(fp, "Deleted ");
797
Mathias Nymand7bd2db2015-04-28 13:18:21 +0300798 fprintf(fp, "%d: ", ifi->ifi_index);
799 color_fprintf(fp, COLOR_IFNAME, "%s",
Stephen Hemminger11522e72016-04-11 22:07:51 +0000800 tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000801
802 if (tb[IFLA_LINK]) {
803 SPRINT_BUF(b1);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700804 int iflink = *(int *)RTA_DATA(tb[IFLA_LINK]);
805
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000806 if (iflink == 0)
807 fprintf(fp, "@NONE: ");
808 else {
Nicolas Dichtelccdcbf32015-02-17 17:30:38 +0100809 if (tb[IFLA_LINK_NETNSID])
810 fprintf(fp, "@if%d: ", iflink);
811 else {
812 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
813 m_flag = ll_index_to_flags(iflink);
814 m_flag = !(m_flag & IFF_UP);
815 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000816 }
817 } else {
818 fprintf(fp, ": ");
819 }
820 print_link_flags(fp, ifi->ifi_flags, m_flag);
821
822 if (tb[IFLA_MTU])
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700823 fprintf(fp, "mtu %u ", *(int *)RTA_DATA(tb[IFLA_MTU]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000824 if (tb[IFLA_QDISC])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700825 fprintf(fp, "qdisc %s ", rta_getattr_str(tb[IFLA_QDISC]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000826 if (tb[IFLA_MASTER]) {
827 SPRINT_BUF(b1);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700828 fprintf(fp, "master %s ", ll_idx_n2a(*(int *)RTA_DATA(tb[IFLA_MASTER]), b1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000829 }
Stephen Hemminger82499282012-03-19 17:24:43 -0700830
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700831 if (tb[IFLA_OPERSTATE])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700832 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
Stephen Hemminger82499282012-03-19 17:24:43 -0700833
834 if (do_link && tb[IFLA_LINKMODE])
835 print_linkmode(fp, tb[IFLA_LINKMODE]);
836
Stefan Tomanekc4fdf752013-08-03 14:20:53 +0200837 if (tb[IFLA_GROUP]) {
838 SPRINT_BUF(b1);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700839 int group = *(int *)RTA_DATA(tb[IFLA_GROUP]);
840
Stefan Tomanekc4fdf752013-08-03 14:20:53 +0200841 fprintf(fp, "group %s ", rtnl_group_n2a(group, b1, sizeof(b1)));
842 }
843
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000844 if (filter.showqueue)
Eric Dumazetf78e3162009-10-22 18:13:21 +0000845 print_queuelen(fp, tb);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800846
Nicolas Dichtel2ec28932014-12-04 17:41:07 +0100847 if (!filter.family || filter.family == AF_PACKET || show_details) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000848 SPRINT_BUF(b1);
849 fprintf(fp, "%s", _SL_);
850 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
851
852 if (tb[IFLA_ADDRESS]) {
Mathias Nymand7bd2db2015-04-28 13:18:21 +0300853 color_fprintf(fp, COLOR_MAC, "%s",
Stephen Hemminger11522e72016-04-11 22:07:51 +0000854 ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
855 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
856 ifi->ifi_type,
857 b1, sizeof(b1)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000858 }
859 if (tb[IFLA_BROADCAST]) {
860 if (ifi->ifi_flags&IFF_POINTOPOINT)
861 fprintf(fp, " peer ");
862 else
863 fprintf(fp, " brd ");
Phil Sutterff9d8f32016-03-22 19:35:14 +0100864 color_fprintf(fp, COLOR_MAC, "%s",
Stephen Hemminger11522e72016-04-11 22:07:51 +0000865 ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
866 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
867 ifi->ifi_type,
868 b1, sizeof(b1)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000869 }
870 }
Patrick McHardy1d934832007-08-22 10:49:01 -0700871
Nicolas Dichtelccdcbf32015-02-17 17:30:38 +0100872 if (tb[IFLA_LINK_NETNSID]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700873 int id = *(int *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
Nicolas Dichtelccdcbf32015-02-17 17:30:38 +0100874
875 if (id >= 0)
876 fprintf(fp, " link-netnsid %d", id);
877 else
878 fprintf(fp, " link-netnsid unknown");
879 }
880
Anuradha Karuppiah18864822015-07-14 13:43:22 -0700881 if (tb[IFLA_PROTO_DOWN]) {
882 if (rta_getattr_u8(tb[IFLA_PROTO_DOWN]))
883 fprintf(fp, " protodown on ");
884 }
885
Stephen Hemminger11522e72016-04-11 22:07:51 +0000886 if (show_details) {
887 if (tb[IFLA_PROMISCUITY])
888 fprintf(fp, " promiscuity %u ",
889 *(int *)RTA_DATA(tb[IFLA_PROMISCUITY]));
Stephen Hemminger1cb6a112013-02-05 08:16:28 -0800890
Stephen Hemminger11522e72016-04-11 22:07:51 +0000891 if (tb[IFLA_LINKINFO])
892 print_linktype(fp, tb[IFLA_LINKINFO]);
Patrick McHardy1d934832007-08-22 10:49:01 -0700893
Stephen Hemminger11522e72016-04-11 22:07:51 +0000894 if (do_link && tb[IFLA_AF_SPEC])
895 print_af_spec(fp, tb[IFLA_AF_SPEC]);
Jiri Pirkoee0067a2015-01-08 09:49:55 +0100896
Stephen Hemminger11522e72016-04-11 22:07:51 +0000897 if (tb[IFLA_NUM_TX_QUEUES])
898 fprintf(fp, "numtxqueues %u ",
899 rta_getattr_u32(tb[IFLA_NUM_TX_QUEUES]));
Eric Dumazetf1c656e2016-04-07 16:11:39 -0700900
Stephen Hemminger11522e72016-04-11 22:07:51 +0000901 if (tb[IFLA_NUM_RX_QUEUES])
902 fprintf(fp, "numrxqueues %u ",
903 rta_getattr_u32(tb[IFLA_NUM_RX_QUEUES]));
904
905 if (tb[IFLA_PHYS_PORT_NAME])
906 fprintf(fp, "portname %s ",
907 rta_getattr_str(tb[IFLA_PHYS_PORT_NAME]));
908
909 if (tb[IFLA_PHYS_PORT_ID]) {
910 SPRINT_BUF(b1);
911 fprintf(fp, "portid %s ",
912 hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_PORT_ID]),
913 RTA_PAYLOAD(tb[IFLA_PHYS_PORT_ID]),
914 b1, sizeof(b1)));
915 }
916
917 if (tb[IFLA_PHYS_SWITCH_ID]) {
918 SPRINT_BUF(b1);
919 fprintf(fp, "switchid %s ",
920 hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_SWITCH_ID]),
921 RTA_PAYLOAD(tb[IFLA_PHYS_SWITCH_ID]),
922 b1, sizeof(b1)));
923 }
924 }
925
Eric Dumazetf1c656e2016-04-07 16:11:39 -0700926
Nicolas Dichtel2ec28932014-12-04 17:41:07 +0100927 if ((do_link || show_details) && tb[IFLA_IFALIAS]) {
Stephen Hemminger1124ffb2013-03-14 13:47:49 -0700928 fprintf(fp, "%s alias %s", _SL_,
Stephen Hemmingerff247462012-04-10 08:47:55 -0700929 rta_getattr_str(tb[IFLA_IFALIAS]));
roopa263c8942013-03-12 15:04:02 -0700930 }
Stephen Hemmingerace9c962009-03-23 10:46:47 -0700931
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800932 if (do_link && show_stats) {
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200933 fprintf(fp, "%s", _SL_);
934 __print_link_stats(fp, tb);
Jan Engelhardt8864ac92010-03-11 10:00:34 +0000935 }
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800936
Nicolas Dichtel2ec28932014-12-04 17:41:07 +0100937 if ((do_link || show_details) && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) {
Chris Wright3fd86632010-05-18 00:57:00 -0700938 struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST];
939 int rem = RTA_PAYLOAD(vflist);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700940
Chris Wright3fd86632010-05-18 00:57:00 -0700941 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
942 print_vfinfo(fp, i);
Williams, Mitch Aae7229d2010-02-10 01:47:08 +0000943 }
Chris Wright3fd86632010-05-18 00:57:00 -0700944
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000945 fprintf(fp, "\n");
946 fflush(fp);
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200947 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000948}
949
950static int flush_update(void)
951{
Neil Hormane149d4e2015-11-05 14:54:17 -0500952
953 /*
954 * Note that the kernel may delete multiple addresses for one
955 * delete request (e.g. if ipv4 address promotion is disabled).
956 * Since a flush operation is really a series of delete requests
957 * its possible that we may request an address delete that has
958 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
959 * errors returned from a flush request
960 */
961 if ((rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) &&
962 (errno != EADDRNOTAVAIL)) {
Stephen Hemminger1fb0a992008-01-26 11:08:31 -0800963 perror("Failed to send flush request");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000964 return -1;
965 }
966 filter.flushp = 0;
967 return 0;
968}
969
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900970static int set_lifetime(unsigned int *lifetime, char *argv)
971{
972 if (strcmp(argv, "forever") == 0)
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900973 *lifetime = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900974 else if (get_u32(lifetime, argv, 0))
975 return -1;
976
977 return 0;
978}
979
Jiri Pirko37c9b942014-01-06 10:17:09 +0100980static unsigned int get_ifa_flags(struct ifaddrmsg *ifa,
981 struct rtattr *ifa_flags_attr)
982{
983 return ifa_flags_attr ? rta_getattr_u32(ifa_flags_attr) :
984 ifa->ifa_flags;
985}
986
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800987int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000988 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000989{
Stephen Hemminger4b726cb2014-05-09 12:36:46 -0700990 FILE *fp = arg;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000991 struct ifaddrmsg *ifa = NLMSG_DATA(n);
992 int len = n->nlmsg_len;
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800993 int deprecated = 0;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700994 /* Use local copy of ifa_flags to not interfere with filtering code */
995 unsigned int ifa_flags;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700996 struct rtattr *rta_tb[IFA_MAX+1];
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700997
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000998 SPRINT_BUF(b1);
999
1000 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
1001 return 0;
1002 len -= NLMSG_LENGTH(sizeof(*ifa));
1003 if (len < 0) {
1004 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
1005 return -1;
1006 }
1007
1008 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
1009 return 0;
1010
Stephen Hemminger4b726cb2014-05-09 12:36:46 -07001011 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa),
1012 n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001013
Jiri Pirko37c9b942014-01-06 10:17:09 +01001014 ifa_flags = get_ifa_flags(ifa, rta_tb[IFA_FLAGS]);
1015
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001016 if (!rta_tb[IFA_LOCAL])
1017 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
1018 if (!rta_tb[IFA_ADDRESS])
1019 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
1020
1021 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
1022 return 0;
1023 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
1024 return 0;
Jiri Pirko37c9b942014-01-06 10:17:09 +01001025 if ((filter.flags ^ ifa_flags) & filter.flagmask)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001026 return 0;
1027 if (filter.label) {
1028 SPRINT_BUF(b1);
1029 const char *label;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001030
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001031 if (rta_tb[IFA_LABEL])
1032 label = RTA_DATA(rta_tb[IFA_LABEL]);
1033 else
1034 label = ll_idx_n2a(ifa->ifa_index, b1);
1035 if (fnmatch(filter.label, label, 0) != 0)
1036 return 0;
1037 }
1038 if (filter.pfx.family) {
1039 if (rta_tb[IFA_LOCAL]) {
1040 inet_prefix dst;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001041
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001042 memset(&dst, 0, sizeof(dst));
1043 dst.family = ifa->ifa_family;
1044 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
1045 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
1046 return 0;
1047 }
1048 }
1049
net[shemminger]!shemminger3eb17312005-02-07 18:28:31 +00001050 if (filter.family && filter.family != ifa->ifa_family)
1051 return 0;
1052
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001053 if (filter.flushb) {
1054 struct nlmsghdr *fn;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001055
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001056 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
1057 if (flush_update())
1058 return -1;
1059 }
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001060 fn = (struct nlmsghdr *)(filter.flushb + NLMSG_ALIGN(filter.flushp));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001061 memcpy(fn, n, n->nlmsg_len);
1062 fn->nlmsg_type = RTM_DELADDR;
1063 fn->nlmsg_flags = NLM_F_REQUEST;
shemminger351efcd2005-09-01 19:21:50 +00001064 fn->nlmsg_seq = ++rth.seq;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001065 filter.flushp = (((char *)fn) + n->nlmsg_len) - filter.flushb;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001066 filter.flushed++;
1067 if (show_stats < 2)
1068 return 0;
1069 }
1070
1071 if (n->nlmsg_type == RTM_DELADDR)
1072 fprintf(fp, "Deleted ");
1073
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001074 if (!brief) {
1075 if (filter.oneline || filter.flushb)
1076 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
1077 if (ifa->ifa_family == AF_INET)
1078 fprintf(fp, " inet ");
1079 else if (ifa->ifa_family == AF_INET6)
1080 fprintf(fp, " inet6 ");
1081 else if (ifa->ifa_family == AF_DECnet)
1082 fprintf(fp, " dnet ");
1083 else if (ifa->ifa_family == AF_IPX)
1084 fprintf(fp, " ipx ");
1085 else
1086 fprintf(fp, " family %d ", ifa->ifa_family);
1087 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001088
1089 if (rta_tb[IFA_LOCAL]) {
Phil Suttera1121aa2016-03-22 19:35:13 +01001090 color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s",
Phil Sutterd49f9342016-03-22 19:35:17 +01001091 format_host_rta(ifa->ifa_family,
1092 rta_tb[IFA_LOCAL]));
Phil Sutterff9d8f32016-03-22 19:35:14 +01001093 if (rta_tb[IFA_ADDRESS] &&
1094 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]),
1095 RTA_DATA(rta_tb[IFA_LOCAL]),
1096 ifa->ifa_family == AF_INET ? 4 : 16)) {
1097 fprintf(fp, " peer ");
1098 color_fprintf(fp, ifa_family_color(ifa->ifa_family),
Phil Sutterd49f9342016-03-22 19:35:17 +01001099 "%s", format_host_rta(ifa->ifa_family,
1100 rta_tb[IFA_ADDRESS]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001101 }
Phil Sutterff9d8f32016-03-22 19:35:14 +01001102 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001103 }
1104
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001105 if (brief)
1106 goto brief_exit;
1107
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001108 if (rta_tb[IFA_BROADCAST]) {
Phil Sutterff9d8f32016-03-22 19:35:14 +01001109 fprintf(fp, "brd ");
1110 color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
Phil Sutterd49f9342016-03-22 19:35:17 +01001111 format_host_rta(ifa->ifa_family,
1112 rta_tb[IFA_BROADCAST]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001113 }
1114 if (rta_tb[IFA_ANYCAST]) {
Phil Sutterff9d8f32016-03-22 19:35:14 +01001115 fprintf(fp, "any ");
1116 color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
Phil Sutterd49f9342016-03-22 19:35:17 +01001117 format_host_rta(ifa->ifa_family,
1118 rta_tb[IFA_ANYCAST]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001119 }
1120 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
Jiri Pirko37c9b942014-01-06 10:17:09 +01001121 if (ifa_flags & IFA_F_SECONDARY) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001122 ifa_flags &= ~IFA_F_SECONDARY;
Brian Haleya1b9ffc2009-09-14 17:01:43 -04001123 if (ifa->ifa_family == AF_INET6)
1124 fprintf(fp, "temporary ");
1125 else
1126 fprintf(fp, "secondary ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001127 }
Jiri Pirko37c9b942014-01-06 10:17:09 +01001128 if (ifa_flags & IFA_F_TENTATIVE) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001129 ifa_flags &= ~IFA_F_TENTATIVE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001130 fprintf(fp, "tentative ");
1131 }
Jiri Pirko37c9b942014-01-06 10:17:09 +01001132 if (ifa_flags & IFA_F_DEPRECATED) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001133 ifa_flags &= ~IFA_F_DEPRECATED;
Benedikt Gollatz037d9502009-01-06 19:36:56 -08001134 deprecated = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001135 fprintf(fp, "deprecated ");
1136 }
Jiri Pirko37c9b942014-01-06 10:17:09 +01001137 if (ifa_flags & IFA_F_HOMEADDRESS) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001138 ifa_flags &= ~IFA_F_HOMEADDRESS;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001139 fprintf(fp, "home ");
1140 }
Jiri Pirko37c9b942014-01-06 10:17:09 +01001141 if (ifa_flags & IFA_F_NODAD) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001142 ifa_flags &= ~IFA_F_NODAD;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001143 fprintf(fp, "nodad ");
1144 }
Jiri Pirko5b7e21c2014-01-06 10:17:10 +01001145 if (ifa_flags & IFA_F_MANAGETEMPADDR) {
1146 ifa_flags &= ~IFA_F_MANAGETEMPADDR;
1147 fprintf(fp, "mngtmpaddr ");
1148 }
Thomas Haller58c69b22014-01-07 21:46:28 +01001149 if (ifa_flags & IFA_F_NOPREFIXROUTE) {
1150 ifa_flags &= ~IFA_F_NOPREFIXROUTE;
1151 fprintf(fp, "noprefixroute ");
1152 }
Madhu Challae31867a2015-03-04 10:30:10 -08001153 if (ifa_flags & IFA_F_MCAUTOJOIN) {
1154 ifa_flags &= ~IFA_F_MCAUTOJOIN;
1155 fprintf(fp, "autojoin ");
1156 }
Jiri Pirko37c9b942014-01-06 10:17:09 +01001157 if (!(ifa_flags & IFA_F_PERMANENT)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001158 fprintf(fp, "dynamic ");
1159 } else
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001160 ifa_flags &= ~IFA_F_PERMANENT;
Jiri Pirko37c9b942014-01-06 10:17:09 +01001161 if (ifa_flags & IFA_F_DADFAILED) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001162 ifa_flags &= ~IFA_F_DADFAILED;
Brian Haleyf4af8512009-12-01 15:58:44 -08001163 fprintf(fp, "dadfailed ");
1164 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001165 if (ifa_flags)
1166 fprintf(fp, "flags %02x ", ifa_flags);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001167 if (rta_tb[IFA_LABEL])
Stephen Hemmingerff247462012-04-10 08:47:55 -07001168 fprintf(fp, "%s", rta_getattr_str(rta_tb[IFA_LABEL]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001169 if (rta_tb[IFA_CACHEINFO]) {
1170 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001171
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001172 fprintf(fp, "%s", _SL_);
Andreas Schwabf66efad2010-11-05 23:26:29 +00001173 fprintf(fp, " valid_lft ");
Masahide NAKAMURA141bb602006-11-24 12:27:01 +09001174 if (ci->ifa_valid == INFINITY_LIFE_TIME)
Andreas Schwabf66efad2010-11-05 23:26:29 +00001175 fprintf(fp, "forever");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001176 else
Andreas Schwabf66efad2010-11-05 23:26:29 +00001177 fprintf(fp, "%usec", ci->ifa_valid);
1178 fprintf(fp, " preferred_lft ");
Masahide NAKAMURA141bb602006-11-24 12:27:01 +09001179 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
Andreas Schwabf66efad2010-11-05 23:26:29 +00001180 fprintf(fp, "forever");
Benedikt Gollatz037d9502009-01-06 19:36:56 -08001181 else {
1182 if (deprecated)
Andreas Schwabf66efad2010-11-05 23:26:29 +00001183 fprintf(fp, "%dsec", ci->ifa_prefered);
Benedikt Gollatz037d9502009-01-06 19:36:56 -08001184 else
Andreas Schwabf66efad2010-11-05 23:26:29 +00001185 fprintf(fp, "%usec", ci->ifa_prefered);
Benedikt Gollatz037d9502009-01-06 19:36:56 -08001186 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001187 }
1188 fprintf(fp, "\n");
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001189brief_exit:
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001190 fflush(fp);
1191 return 0;
1192}
1193
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001194struct nlmsg_list {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001195 struct nlmsg_list *next;
1196 struct nlmsghdr h;
1197};
1198
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001199struct nlmsg_chain {
Eric Dumazet62e2e542012-06-09 13:55:55 +02001200 struct nlmsg_list *head;
1201 struct nlmsg_list *tail;
1202};
1203
Andreas Henriksson5e5055b2015-01-23 13:10:33 +01001204static int print_selected_addrinfo(struct ifinfomsg *ifi,
1205 struct nlmsg_list *ainfo, FILE *fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001206{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001207 for ( ; ainfo ; ainfo = ainfo->next) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001208 struct nlmsghdr *n = &ainfo->h;
1209 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1210
1211 if (n->nlmsg_type != RTM_NEWADDR)
1212 continue;
1213
1214 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
1215 return -1;
1216
Andreas Henriksson5e5055b2015-01-23 13:10:33 +01001217 if (ifa->ifa_index != ifi->ifi_index ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001218 (filter.family && filter.family != ifa->ifa_family))
1219 continue;
1220
Andreas Henriksson5e5055b2015-01-23 13:10:33 +01001221 if (filter.up && !(ifi->ifi_flags&IFF_UP))
1222 continue;
1223
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001224 print_addrinfo(NULL, n, fp);
1225 }
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001226 if (brief) {
1227 fprintf(fp, "\n");
1228 fflush(fp);
1229 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001230 return 0;
1231}
1232
1233
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001234static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +00001235 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001236{
Eric Dumazet62e2e542012-06-09 13:55:55 +02001237 struct nlmsg_chain *lchain = (struct nlmsg_chain *)arg;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001238 struct nlmsg_list *h;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001239
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001240 h = malloc(n->nlmsg_len+sizeof(void *));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001241 if (h == NULL)
1242 return -1;
1243
1244 memcpy(&h->h, n, n->nlmsg_len);
1245 h->next = NULL;
1246
Eric Dumazet62e2e542012-06-09 13:55:55 +02001247 if (lchain->tail)
1248 lchain->tail->next = h;
1249 else
1250 lchain->head = h;
1251 lchain->tail = h;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001252
1253 ll_remember_index(who, n, NULL);
1254 return 0;
1255}
1256
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001257static __u32 ipadd_dump_magic = 0x47361222;
1258
1259static int ipadd_save_prep(void)
1260{
1261 int ret;
1262
1263 if (isatty(STDOUT_FILENO)) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +00001264 fprintf(stderr, "Not sending a binary stream to stdout\n");
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001265 return -1;
1266 }
1267
1268 ret = write(STDOUT_FILENO, &ipadd_dump_magic, sizeof(ipadd_dump_magic));
1269 if (ret != sizeof(ipadd_dump_magic)) {
1270 fprintf(stderr, "Can't write magic to dump file\n");
1271 return -1;
1272 }
1273
1274 return 0;
1275}
1276
1277static int ipadd_dump_check_magic(void)
1278{
1279 int ret;
1280 __u32 magic = 0;
1281
1282 if (isatty(STDIN_FILENO)) {
Pavel Šimerda4e972d52015-08-12 22:04:08 +02001283 fprintf(stderr, "Can't restore address dump from a terminal\n");
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001284 return -1;
1285 }
1286
1287 ret = fread(&magic, sizeof(magic), 1, stdin);
1288 if (magic != ipadd_dump_magic) {
1289 fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n", ret, magic);
1290 return -1;
1291 }
1292
1293 return 0;
1294}
1295
1296static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
1297 void *arg)
1298{
1299 int ret;
1300
1301 ret = write(STDOUT_FILENO, n, n->nlmsg_len);
1302 if ((ret > 0) && (ret != n->nlmsg_len)) {
1303 fprintf(stderr, "Short write while saving nlmsg\n");
1304 ret = -EIO;
1305 }
1306
1307 return ret == n->nlmsg_len ? 0 : ret;
1308}
1309
Nicolas Dichtel0628cdd2015-05-20 16:19:58 +02001310static int show_handler(const struct sockaddr_nl *nl,
1311 struct rtnl_ctrl_data *ctrl,
1312 struct nlmsghdr *n, void *arg)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001313{
1314 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1315
1316 printf("if%d:\n", ifa->ifa_index);
1317 print_addrinfo(NULL, n, stdout);
1318 return 0;
1319}
1320
1321static int ipaddr_showdump(void)
1322{
1323 if (ipadd_dump_check_magic())
1324 exit(-1);
1325
1326 exit(rtnl_from_file(stdin, &show_handler, NULL));
1327}
1328
Nicolas Dichtel0628cdd2015-05-20 16:19:58 +02001329static int restore_handler(const struct sockaddr_nl *nl,
1330 struct rtnl_ctrl_data *ctrl,
1331 struct nlmsghdr *n, void *arg)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001332{
1333 int ret;
1334
1335 n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
1336
1337 ll_init_map(&rth);
1338
Stephen Hemmingerc079e122015-05-27 12:26:14 -07001339 ret = rtnl_talk(&rth, n, n, sizeof(*n));
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001340 if ((ret < 0) && (errno == EEXIST))
1341 ret = 0;
1342
1343 return ret;
1344}
1345
1346static int ipaddr_restore(void)
1347{
1348 if (ipadd_dump_check_magic())
1349 exit(-1);
1350
1351 exit(rtnl_from_file(stdin, &restore_handler, NULL));
1352}
1353
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001354static void free_nlmsg_chain(struct nlmsg_chain *info)
1355{
1356 struct nlmsg_list *l, *n;
1357
1358 for (l = info->head; l; l = n) {
1359 n = l->next;
1360 free(l);
1361 }
1362}
1363
1364static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
1365{
1366 struct nlmsg_list *l, **lp;
1367
1368 lp = &linfo->head;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001369 while ((l = *lp) != NULL) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001370 int ok = 0;
Petr Písař7f747fd2012-10-03 16:42:41 +02001371 int missing_net_address = 1;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001372 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
1373 struct nlmsg_list *a;
1374
1375 for (a = ainfo->head; a; a = a->next) {
1376 struct nlmsghdr *n = &a->h;
1377 struct ifaddrmsg *ifa = NLMSG_DATA(n);
Jiri Pirko37c9b942014-01-06 10:17:09 +01001378 struct rtattr *tb[IFA_MAX + 1];
1379 unsigned int ifa_flags;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001380
Petr Písař7f747fd2012-10-03 16:42:41 +02001381 if (ifa->ifa_index != ifi->ifi_index)
1382 continue;
1383 missing_net_address = 0;
1384 if (filter.family && filter.family != ifa->ifa_family)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001385 continue;
1386 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
1387 continue;
Jiri Pirko37c9b942014-01-06 10:17:09 +01001388
1389 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
1390 ifa_flags = get_ifa_flags(ifa, tb[IFA_FLAGS]);
1391
1392 if ((filter.flags ^ ifa_flags) & filter.flagmask)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001393 continue;
1394 if (filter.pfx.family || filter.label) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001395 if (!tb[IFA_LOCAL])
1396 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1397
1398 if (filter.pfx.family && tb[IFA_LOCAL]) {
1399 inet_prefix dst;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001400
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001401 memset(&dst, 0, sizeof(dst));
1402 dst.family = ifa->ifa_family;
1403 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
1404 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
1405 continue;
1406 }
1407 if (filter.label) {
1408 SPRINT_BUF(b1);
1409 const char *label;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001410
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001411 if (tb[IFA_LABEL])
1412 label = RTA_DATA(tb[IFA_LABEL]);
1413 else
1414 label = ll_idx_n2a(ifa->ifa_index, b1);
1415 if (fnmatch(filter.label, label, 0) != 0)
1416 continue;
1417 }
1418 }
1419
1420 ok = 1;
1421 break;
1422 }
Petr Písař7f747fd2012-10-03 16:42:41 +02001423 if (missing_net_address &&
1424 (filter.family == AF_UNSPEC || filter.family == AF_PACKET))
1425 ok = 1;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001426 if (!ok) {
1427 *lp = l->next;
1428 free(l);
1429 } else
1430 lp = &l->next;
1431 }
1432}
1433
1434static int ipaddr_flush(void)
1435{
1436 int round = 0;
1437 char flushb[4096-512];
1438
1439 filter.flushb = flushb;
1440 filter.flushp = 0;
1441 filter.flushe = sizeof(flushb);
1442
1443 while ((max_flush_loops == 0) || (round < max_flush_loops)) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001444 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1445 perror("Cannot send dump request");
1446 exit(1);
1447 }
1448 filter.flushed = 0;
Phil Sutterd25ec032015-11-24 15:31:02 +01001449 if (rtnl_dump_filter_nc(&rth, print_addrinfo,
1450 stdout, NLM_F_DUMP_INTR) < 0) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001451 fprintf(stderr, "Flush terminated\n");
1452 exit(1);
1453 }
1454 if (filter.flushed == 0) {
1455 flush_done:
1456 if (show_stats) {
1457 if (round == 0)
1458 printf("Nothing to flush.\n");
1459 else
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001460 printf("*** Flush is complete after %d round%s ***\n", round, round > 1?"s":"");
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001461 }
1462 fflush(stdout);
1463 return 0;
1464 }
1465 round++;
1466 if (flush_update() < 0)
1467 return 1;
1468
1469 if (show_stats) {
1470 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
1471 fflush(stdout);
1472 }
1473
1474 /* If we are flushing, and specifying primary, then we
1475 * want to flush only a single round. Otherwise, we'll
1476 * start flushing secondaries that were promoted to
1477 * primaries.
1478 */
1479 if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY))
1480 goto flush_done;
1481 }
1482 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops);
1483 fflush(stderr);
1484 return 1;
1485}
1486
David Ahernb0a4ce62016-05-11 06:51:58 -07001487static int iplink_filter_req(struct nlmsghdr *nlh, int reqlen)
1488{
1489 int err;
1490
1491 err = addattr32(nlh, reqlen, IFLA_EXT_MASK, RTEXT_FILTER_VF);
1492 if (err)
1493 return err;
1494
1495 if (filter.master) {
1496 err = addattr32(nlh, reqlen, IFLA_MASTER, filter.master);
1497 if (err)
1498 return err;
1499 }
1500
1501 if (filter.kind) {
1502 struct rtattr *linkinfo;
1503
1504 linkinfo = addattr_nest(nlh, reqlen, IFLA_LINKINFO);
1505
1506 err = addattr_l(nlh, reqlen, IFLA_INFO_KIND, filter.kind,
1507 strlen(filter.kind));
1508 if (err)
1509 return err;
1510
1511 addattr_nest_end(nlh, linkinfo);
1512 }
1513
1514 return 0;
1515}
1516
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001517static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001518{
Eric Dumazet62e2e542012-06-09 13:55:55 +02001519 struct nlmsg_chain linfo = { NULL, NULL};
1520 struct nlmsg_chain ainfo = { NULL, NULL};
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001521 struct nlmsg_list *l;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001522 char *filter_dev = NULL;
1523 int no_link = 0;
1524
vadimk093b7642014-10-20 12:25:17 +03001525 ipaddr_reset_filter(oneline, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001526 filter.showqueue = 1;
Phil Sutter906dfe42015-11-24 15:31:03 +01001527 filter.family = preferred_family;
Stefan Tomanekc4fdf752013-08-03 14:20:53 +02001528 filter.group = -1;
Vlad Dogaruf960c922011-02-02 20:23:40 +02001529
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001530 if (action == IPADD_FLUSH) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001531 if (argc <= 0) {
1532 fprintf(stderr, "Flush requires arguments.\n");
Vlad Dogaruf960c922011-02-02 20:23:40 +02001533
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001534 return -1;
1535 }
1536 if (filter.family == AF_PACKET) {
1537 fprintf(stderr, "Cannot flush link addresses.\n");
1538 return -1;
1539 }
1540 }
1541
1542 while (argc > 0) {
1543 if (strcmp(*argv, "to") == 0) {
1544 NEXT_ARG();
1545 get_prefix(&filter.pfx, *argv, filter.family);
1546 if (filter.family == AF_UNSPEC)
1547 filter.family = filter.pfx.family;
1548 } else if (strcmp(*argv, "scope") == 0) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001549 unsigned int scope = 0;
1550
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001551 NEXT_ARG();
1552 filter.scopemask = -1;
1553 if (rtnl_rtscope_a2n(&scope, *argv)) {
1554 if (strcmp(*argv, "all") != 0)
1555 invarg("invalid \"scope\"\n", *argv);
1556 scope = RT_SCOPE_NOWHERE;
1557 filter.scopemask = 0;
1558 }
1559 filter.scope = scope;
1560 } else if (strcmp(*argv, "up") == 0) {
1561 filter.up = 1;
1562 } else if (strcmp(*argv, "dynamic") == 0) {
1563 filter.flags &= ~IFA_F_PERMANENT;
1564 filter.flagmask |= IFA_F_PERMANENT;
1565 } else if (strcmp(*argv, "permanent") == 0) {
1566 filter.flags |= IFA_F_PERMANENT;
1567 filter.flagmask |= IFA_F_PERMANENT;
Brian Haleya1b9ffc2009-09-14 17:01:43 -04001568 } else if (strcmp(*argv, "secondary") == 0 ||
1569 strcmp(*argv, "temporary") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001570 filter.flags |= IFA_F_SECONDARY;
1571 filter.flagmask |= IFA_F_SECONDARY;
1572 } else if (strcmp(*argv, "primary") == 0) {
1573 filter.flags &= ~IFA_F_SECONDARY;
1574 filter.flagmask |= IFA_F_SECONDARY;
1575 } else if (strcmp(*argv, "tentative") == 0) {
1576 filter.flags |= IFA_F_TENTATIVE;
1577 filter.flagmask |= IFA_F_TENTATIVE;
Heiner Kallweitb5f39b22014-12-22 20:18:43 +01001578 } else if (strcmp(*argv, "-tentative") == 0) {
1579 filter.flags &= ~IFA_F_TENTATIVE;
1580 filter.flagmask |= IFA_F_TENTATIVE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001581 } else if (strcmp(*argv, "deprecated") == 0) {
1582 filter.flags |= IFA_F_DEPRECATED;
1583 filter.flagmask |= IFA_F_DEPRECATED;
Heiner Kallweitb5f39b22014-12-22 20:18:43 +01001584 } else if (strcmp(*argv, "-deprecated") == 0) {
1585 filter.flags &= ~IFA_F_DEPRECATED;
1586 filter.flagmask |= IFA_F_DEPRECATED;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001587 } else if (strcmp(*argv, "home") == 0) {
1588 filter.flags |= IFA_F_HOMEADDRESS;
1589 filter.flagmask |= IFA_F_HOMEADDRESS;
1590 } else if (strcmp(*argv, "nodad") == 0) {
1591 filter.flags |= IFA_F_NODAD;
1592 filter.flagmask |= IFA_F_NODAD;
Jiri Pirko5b7e21c2014-01-06 10:17:10 +01001593 } else if (strcmp(*argv, "mngtmpaddr") == 0) {
1594 filter.flags |= IFA_F_MANAGETEMPADDR;
1595 filter.flagmask |= IFA_F_MANAGETEMPADDR;
Thomas Haller58c69b22014-01-07 21:46:28 +01001596 } else if (strcmp(*argv, "noprefixroute") == 0) {
1597 filter.flags |= IFA_F_NOPREFIXROUTE;
1598 filter.flagmask |= IFA_F_NOPREFIXROUTE;
Madhu Challae31867a2015-03-04 10:30:10 -08001599 } else if (strcmp(*argv, "autojoin") == 0) {
1600 filter.flags |= IFA_F_MCAUTOJOIN;
1601 filter.flagmask |= IFA_F_MCAUTOJOIN;
Brian Haleya1f27792009-12-03 10:39:36 +00001602 } else if (strcmp(*argv, "dadfailed") == 0) {
1603 filter.flags |= IFA_F_DADFAILED;
1604 filter.flagmask |= IFA_F_DADFAILED;
Heiner Kallweitb5f39b22014-12-22 20:18:43 +01001605 } else if (strcmp(*argv, "-dadfailed") == 0) {
1606 filter.flags &= ~IFA_F_DADFAILED;
1607 filter.flagmask |= IFA_F_DADFAILED;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001608 } else if (strcmp(*argv, "label") == 0) {
1609 NEXT_ARG();
1610 filter.label = *argv;
Vlad Dogaruf960c922011-02-02 20:23:40 +02001611 } else if (strcmp(*argv, "group") == 0) {
1612 NEXT_ARG();
1613 if (rtnl_group_a2n(&filter.group, *argv))
1614 invarg("Invalid \"group\" value\n", *argv);
Vadim Kochan1b944142014-11-29 11:27:41 -08001615 } else if (strcmp(*argv, "master") == 0) {
1616 int ifindex;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001617
Vadim Kochan1b944142014-11-29 11:27:41 -08001618 NEXT_ARG();
1619 ifindex = ll_name_to_index(*argv);
1620 if (!ifindex)
1621 invarg("Device does not exist\n", *argv);
1622 filter.master = ifindex;
David Ahern104444c2016-06-29 11:26:58 -07001623 } else if (strcmp(*argv, "vrf") == 0) {
1624 int ifindex;
1625
1626 NEXT_ARG();
1627 ifindex = ll_name_to_index(*argv);
1628 if (!ifindex)
1629 invarg("Not a valid VRF name\n", *argv);
1630 if (!name_is_vrf(*argv))
1631 invarg("Not a valid VRF name\n", *argv);
1632 filter.master = ifindex;
Phil Sutter24604eb2016-06-09 19:20:36 +02001633 } else if (strcmp(*argv, "type") == 0) {
Phil Suttere0513802016-06-28 15:07:16 +02001634 int soff;
1635
Vadim Kochan712249d2014-12-10 10:11:29 +02001636 NEXT_ARG();
Phil Suttere0513802016-06-28 15:07:16 +02001637 soff = strlen(*argv) - strlen("_slave");
1638 if (!strcmp(*argv + soff, "_slave")) {
1639 (*argv)[soff] = '\0';
1640 filter.slave_kind = *argv;
1641 } else {
1642 filter.kind = *argv;
1643 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001644 } else {
1645 if (strcmp(*argv, "dev") == 0) {
1646 NEXT_ARG();
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001647 } else if (matches(*argv, "help") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001648 usage();
1649 if (filter_dev)
1650 duparg2("dev", *argv);
1651 filter_dev = *argv;
1652 }
1653 argv++; argc--;
1654 }
1655
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001656 if (filter_dev) {
1657 filter.ifindex = ll_name_to_index(filter_dev);
1658 if (filter.ifindex <= 0) {
1659 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
1660 return -1;
1661 }
1662 }
1663
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001664 if (action == IPADD_FLUSH)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001665 return ipaddr_flush();
1666
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001667 if (action == IPADD_SAVE) {
1668 if (ipadd_save_prep())
1669 exit(1);
1670
1671 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETADDR) < 0) {
1672 perror("Cannot send dump request");
1673 exit(1);
1674 }
1675
1676 if (rtnl_dump_filter(&rth, save_nlmsg, stdout) < 0) {
1677 fprintf(stderr, "Save terminated\n");
1678 exit(1);
1679 }
1680
1681 exit(0);
1682 }
1683
Roopa Prabhu50b99502014-07-16 07:13:25 -07001684 /*
1685 * If only filter_dev present and none of the other
1686 * link filters are present, use RTM_GETLINK to get
1687 * the link device
1688 */
1689 if (filter_dev && filter.group == -1 && do_link == 1) {
1690 if (iplink_get(0, filter_dev, RTEXT_FILTER_VF) < 0) {
1691 perror("Cannot send link get request");
1692 exit(1);
1693 }
1694 exit(0);
1695 }
1696
David Ahernb0a4ce62016-05-11 06:51:58 -07001697 if (rtnl_wilddump_req_filter_fn(&rth, preferred_family, RTM_GETLINK,
1698 iplink_filter_req) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001699 perror("Cannot send dump request");
1700 exit(1);
1701 }
1702
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001703 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001704 fprintf(stderr, "Dump terminated\n");
1705 exit(1);
1706 }
1707
Mike Frysingeraf9d4062012-08-13 08:09:52 -07001708 if (filter.family != AF_PACKET) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001709 if (filter.oneline)
1710 no_link = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001711
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001712 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1713 perror("Cannot send dump request");
1714 exit(1);
1715 }
1716
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001717 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001718 fprintf(stderr, "Dump terminated\n");
1719 exit(1);
1720 }
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001721
1722 ipaddr_filter(&linfo, &ainfo);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001723 }
1724
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001725 for (l = linfo.head; l; l = l->next) {
Jiri Benc5d5cf1b2014-10-03 15:25:02 +02001726 int res = 0;
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001727 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
Jiri Benc5d5cf1b2014-10-03 15:25:02 +02001728
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001729 if (brief) {
1730 if (print_linkinfo_brief(NULL, &l->h, stdout) == 0)
1731 if (filter.family != AF_PACKET)
1732 print_selected_addrinfo(ifi,
1733 ainfo.head,
1734 stdout);
1735 } else if (no_link ||
1736 (res = print_linkinfo(NULL, &l->h, stdout)) >= 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001737 if (filter.family != AF_PACKET)
Andreas Henriksson5e5055b2015-01-23 13:10:33 +01001738 print_selected_addrinfo(ifi,
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001739 ainfo.head, stdout);
Jiri Benc5d5cf1b2014-10-03 15:25:02 +02001740 if (res > 0 && !do_link && show_stats)
1741 print_link_stats(stdout, &l->h);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001742 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001743 }
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001744 fflush(stdout);
1745
1746 free_nlmsg_chain(&ainfo);
1747 free_nlmsg_chain(&linfo);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001748
shemminger351efcd2005-09-01 19:21:50 +00001749 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001750}
1751
Sucheta Chakrabortyf89a2a02014-05-22 09:59:37 -04001752static void
1753ipaddr_loop_each_vf(struct rtattr *tb[], int vfnum, int *min, int *max)
1754{
1755 struct rtattr *vflist = tb[IFLA_VFINFO_LIST];
1756 struct rtattr *i, *vf[IFLA_VF_MAX+1];
1757 struct ifla_vf_rate *vf_rate;
1758 int rem;
1759
1760 rem = RTA_PAYLOAD(vflist);
1761
1762 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
1763 parse_rtattr_nested(vf, IFLA_VF_MAX, i);
1764 vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
1765 if (vf_rate->vf == vfnum) {
1766 *min = vf_rate->min_tx_rate;
1767 *max = vf_rate->max_tx_rate;
1768 return;
1769 }
1770 }
1771 fprintf(stderr, "Cannot find VF %d\n", vfnum);
1772 exit(1);
1773}
1774
1775void ipaddr_get_vf_rate(int vfnum, int *min, int *max, int idx)
1776{
1777 struct nlmsg_chain linfo = { NULL, NULL};
1778 struct rtattr *tb[IFLA_MAX+1];
1779 struct ifinfomsg *ifi;
1780 struct nlmsg_list *l;
1781 struct nlmsghdr *n;
1782 int len;
1783
1784 if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) {
1785 perror("Cannot send dump request");
1786 exit(1);
1787 }
1788 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
1789 fprintf(stderr, "Dump terminated\n");
1790 exit(1);
1791 }
1792 for (l = linfo.head; l; l = l->next) {
1793 n = &l->h;
1794 ifi = NLMSG_DATA(n);
1795
1796 len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
Stephen Hemminger1199c4f2014-07-14 12:06:52 -07001797 if (len < 0 || (idx && idx != ifi->ifi_index))
Sucheta Chakrabortyf89a2a02014-05-22 09:59:37 -04001798 continue;
1799
1800 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1801
1802 if ((tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF])) {
1803 ipaddr_loop_each_vf(tb, vfnum, min, max);
1804 return;
1805 }
1806 }
1807}
1808
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001809int ipaddr_list_link(int argc, char **argv)
1810{
1811 preferred_family = AF_PACKET;
1812 do_link = 1;
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001813 return ipaddr_list_flush_or_save(argc, argv, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001814}
1815
vadimk093b7642014-10-20 12:25:17 +03001816void ipaddr_reset_filter(int oneline, int ifindex)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001817{
1818 memset(&filter, 0, sizeof(filter));
1819 filter.oneline = oneline;
vadimk093b7642014-10-20 12:25:17 +03001820 filter.ifindex = ifindex;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001821}
1822
Stephen Hemminger3d866ba2008-03-14 15:30:03 -07001823static int default_scope(inet_prefix *lcl)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001824{
1825 if (lcl->family == AF_INET) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001826 if (lcl->bytelen >= 1 && *(__u8 *)&lcl->data == 127)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001827 return RT_SCOPE_HOST;
1828 }
1829 return 0;
1830}
1831
Madhu Challae31867a2015-03-04 10:30:10 -08001832static bool ipaddr_is_multicast(inet_prefix *a)
1833{
1834 if (a->family == AF_INET)
1835 return IN_MULTICAST(ntohl(a->data[0]));
1836 else if (a->family == AF_INET6)
1837 return IN6_IS_ADDR_MULTICAST(a->data);
1838 else
1839 return false;
1840}
1841
Stephen Hemminger3d866ba2008-03-14 15:30:03 -07001842static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001843{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001844 struct {
Stephen Hemminger48068672014-02-17 10:56:31 -08001845 struct nlmsghdr n;
1846 struct ifaddrmsg ifa;
Stephen Hemminger4b726cb2014-05-09 12:36:46 -07001847 char buf[256];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001848 } req;
1849 char *d = NULL;
1850 char *l = NULL;
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001851 char *lcl_arg = NULL;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001852 char *valid_lftp = NULL;
1853 char *preferred_lftp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001854 inet_prefix lcl;
1855 inet_prefix peer;
1856 int local_len = 0;
1857 int peer_len = 0;
1858 int brd_len = 0;
1859 int any_len = 0;
1860 int scoped = 0;
Masahide NAKAMURA141bb602006-11-24 12:27:01 +09001861 __u32 preferred_lft = INFINITY_LIFE_TIME;
1862 __u32 valid_lft = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001863 struct ifa_cacheinfo cinfo;
Jiri Pirko37c9b942014-01-06 10:17:09 +01001864 unsigned int ifa_flags = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001865
1866 memset(&req, 0, sizeof(req));
1867
1868 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001869 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001870 req.n.nlmsg_type = cmd;
1871 req.ifa.ifa_family = preferred_family;
1872
1873 while (argc > 0) {
1874 if (strcmp(*argv, "peer") == 0 ||
1875 strcmp(*argv, "remote") == 0) {
1876 NEXT_ARG();
1877
1878 if (peer_len)
1879 duparg("peer", *argv);
1880 get_prefix(&peer, *argv, req.ifa.ifa_family);
1881 peer_len = peer.bytelen;
1882 if (req.ifa.ifa_family == AF_UNSPEC)
1883 req.ifa.ifa_family = peer.family;
1884 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
1885 req.ifa.ifa_prefixlen = peer.bitlen;
1886 } else if (matches(*argv, "broadcast") == 0 ||
1887 strcmp(*argv, "brd") == 0) {
1888 inet_prefix addr;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001889
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001890 NEXT_ARG();
1891 if (brd_len)
1892 duparg("broadcast", *argv);
1893 if (strcmp(*argv, "+") == 0)
1894 brd_len = -1;
1895 else if (strcmp(*argv, "-") == 0)
1896 brd_len = -2;
1897 else {
1898 get_addr(&addr, *argv, req.ifa.ifa_family);
1899 if (req.ifa.ifa_family == AF_UNSPEC)
1900 req.ifa.ifa_family = addr.family;
1901 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
1902 brd_len = addr.bytelen;
1903 }
1904 } else if (strcmp(*argv, "anycast") == 0) {
1905 inet_prefix addr;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001906
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001907 NEXT_ARG();
1908 if (any_len)
1909 duparg("anycast", *argv);
1910 get_addr(&addr, *argv, req.ifa.ifa_family);
1911 if (req.ifa.ifa_family == AF_UNSPEC)
1912 req.ifa.ifa_family = addr.family;
1913 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
1914 any_len = addr.bytelen;
1915 } else if (strcmp(*argv, "scope") == 0) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001916 unsigned int scope = 0;
1917
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001918 NEXT_ARG();
1919 if (rtnl_rtscope_a2n(&scope, *argv))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +00001920 invarg("invalid scope value.", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001921 req.ifa.ifa_scope = scope;
1922 scoped = 1;
1923 } else if (strcmp(*argv, "dev") == 0) {
1924 NEXT_ARG();
1925 d = *argv;
1926 } else if (strcmp(*argv, "label") == 0) {
1927 NEXT_ARG();
1928 l = *argv;
1929 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001930 } else if (matches(*argv, "valid_lft") == 0) {
1931 if (valid_lftp)
1932 duparg("valid_lft", *argv);
1933 NEXT_ARG();
1934 valid_lftp = *argv;
1935 if (set_lifetime(&valid_lft, *argv))
1936 invarg("valid_lft value", *argv);
1937 } else if (matches(*argv, "preferred_lft") == 0) {
1938 if (preferred_lftp)
1939 duparg("preferred_lft", *argv);
1940 NEXT_ARG();
1941 preferred_lftp = *argv;
1942 if (set_lifetime(&preferred_lft, *argv))
1943 invarg("preferred_lft value", *argv);
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001944 } else if (strcmp(*argv, "home") == 0) {
Jiri Pirko37c9b942014-01-06 10:17:09 +01001945 ifa_flags |= IFA_F_HOMEADDRESS;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001946 } else if (strcmp(*argv, "nodad") == 0) {
Jiri Pirko37c9b942014-01-06 10:17:09 +01001947 ifa_flags |= IFA_F_NODAD;
Jiri Pirko5b7e21c2014-01-06 10:17:10 +01001948 } else if (strcmp(*argv, "mngtmpaddr") == 0) {
1949 ifa_flags |= IFA_F_MANAGETEMPADDR;
Thomas Haller58c69b22014-01-07 21:46:28 +01001950 } else if (strcmp(*argv, "noprefixroute") == 0) {
1951 ifa_flags |= IFA_F_NOPREFIXROUTE;
Madhu Challae31867a2015-03-04 10:30:10 -08001952 } else if (strcmp(*argv, "autojoin") == 0) {
1953 ifa_flags |= IFA_F_MCAUTOJOIN;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001954 } else {
1955 if (strcmp(*argv, "local") == 0) {
1956 NEXT_ARG();
1957 }
1958 if (matches(*argv, "help") == 0)
1959 usage();
1960 if (local_len)
1961 duparg2("local", *argv);
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001962 lcl_arg = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001963 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1964 if (req.ifa.ifa_family == AF_UNSPEC)
1965 req.ifa.ifa_family = lcl.family;
1966 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1967 local_len = lcl.bytelen;
1968 }
1969 argc--; argv++;
1970 }
WANG Cong8b21f882014-04-04 10:07:20 -07001971 if (ifa_flags <= 0xff)
1972 req.ifa.ifa_flags = ifa_flags;
1973 else
1974 addattr32(&req.n, sizeof(req), IFA_FLAGS, ifa_flags);
Jiri Pirko37c9b942014-01-06 10:17:09 +01001975
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001976 if (d == NULL) {
1977 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1978 return -1;
1979 }
1980 if (l && matches(d, l) != 0) {
1981 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
Michele Petrazzo - Unipex1db61e02010-03-06 08:56:53 +00001982 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001983 }
1984
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001985 if (peer_len == 0 && local_len) {
1986 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
1987 fprintf(stderr,
1988 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1989 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1990 " This special behaviour is likely to disappear in further releases,\n" \
1991 " fix your scripts!\n", lcl_arg, local_len*8);
1992 } else {
1993 peer = lcl;
1994 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
1995 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001996 }
1997 if (req.ifa.ifa_prefixlen == 0)
1998 req.ifa.ifa_prefixlen = lcl.bitlen;
1999
2000 if (brd_len < 0 && cmd != RTM_DELADDR) {
2001 inet_prefix brd;
2002 int i;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07002003
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002004 if (req.ifa.ifa_family != AF_INET) {
2005 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
2006 return -1;
2007 }
2008 brd = peer;
2009 if (brd.bitlen <= 30) {
Stephen Hemminger4b726cb2014-05-09 12:36:46 -07002010 for (i = 31; i >= brd.bitlen; i--) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002011 if (brd_len == -1)
2012 brd.data[0] |= htonl(1<<(31-i));
2013 else
2014 brd.data[0] &= ~htonl(1<<(31-i));
2015 }
2016 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
2017 brd_len = brd.bytelen;
2018 }
2019 }
2020 if (!scoped && cmd != RTM_DELADDR)
2021 req.ifa.ifa_scope = default_scope(&lcl);
2022
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002023 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
2024 fprintf(stderr, "Cannot find device \"%s\"\n", d);
2025 return -1;
2026 }
2027
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09002028 if (valid_lftp || preferred_lftp) {
2029 if (!valid_lft) {
2030 fprintf(stderr, "valid_lft is zero\n");
2031 return -1;
2032 }
2033 if (valid_lft < preferred_lft) {
2034 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
2035 return -1;
2036 }
2037
2038 memset(&cinfo, 0, sizeof(cinfo));
2039 cinfo.ifa_prefered = preferred_lft;
2040 cinfo.ifa_valid = valid_lft;
2041 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
2042 sizeof(cinfo));
2043 }
2044
Madhu Challae31867a2015-03-04 10:30:10 -08002045 if ((ifa_flags & IFA_F_MCAUTOJOIN) && !ipaddr_is_multicast(&lcl)) {
2046 fprintf(stderr, "autojoin needs multicast address\n");
2047 return -1;
2048 }
2049
Stephen Hemmingerc079e122015-05-27 12:26:14 -07002050 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
Michele Petrazzo - Unipex1db61e02010-03-06 08:56:53 +00002051 return -2;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002052
shemminger351efcd2005-09-01 19:21:50 +00002053 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002054}
2055
2056int do_ipaddr(int argc, char **argv)
2057{
2058 if (argc < 1)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04002059 return ipaddr_list_flush_or_save(0, NULL, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002060 if (matches(*argv, "add") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09002061 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
2062 if (matches(*argv, "change") == 0 ||
2063 strcmp(*argv, "chg") == 0)
2064 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
2065 if (matches(*argv, "replace") == 0)
2066 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002067 if (matches(*argv, "delete") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09002068 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002069 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
2070 || matches(*argv, "lst") == 0)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04002071 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002072 if (matches(*argv, "flush") == 0)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04002073 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_FLUSH);
2074 if (matches(*argv, "save") == 0)
2075 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_SAVE);
2076 if (matches(*argv, "showdump") == 0)
2077 return ipaddr_showdump();
2078 if (matches(*argv, "restore") == 0)
2079 return ipaddr_restore();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002080 if (matches(*argv, "help") == 0)
2081 usage();
Pavel Šimerda4e972d52015-08-12 22:04:08 +02002082 fprintf(stderr, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002083 exit(-1);
2084}