blob: 50897e6c8e783f58446cdcc32257132517408d6e [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");
Davide Carattifd4df5b2016-07-26 11:03:19 +0200100 fprintf(stderr, " bond_slave | ipvlan | geneve | bridge_slave | vrf | hsr | macsec }\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),
Stephen Hemminger468fa022016-11-14 09:28:09 +0100151 "%-14s ", oper_states[state]);
Phil Suttera1121aa2016-03-22 19:35:13 +0100152 } else {
153 fprintf(f, "state ");
154 color_fprintf(f, oper_state_color(state),
Stephen Hemminger468fa022016-11-14 09:28:09 +0100155 "%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 {
Phil Sutterd17b1362016-07-18 16:48:42 +0200176 struct ifreq ifr = {};
Eric Dumazetf78e3162009-10-22 18:13:21 +0000177 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
Stephen Hemmingerff247462012-04-10 08:47:55 -0700182 strcpy(ifr.ifr_name, rta_getattr_str(tb[IFLA_IFNAME]));
Eric Dumazetf78e3162009-10-22 18:13:21 +0000183 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
Nicolas Dichteld3603512013-01-29 08:46:42 -0800184 fprintf(f, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno));
Eric Dumazetf78e3162009-10-22 18:13:21 +0000185 close(s);
186 return;
187 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000188 close(s);
Eric Dumazetf78e3162009-10-22 18:13:21 +0000189 qlen = ifr.ifr_qlen;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000190 }
Eric Dumazetf78e3162009-10-22 18:13:21 +0000191 if (qlen)
192 fprintf(f, "qlen %d", qlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000193}
194
Stephen Hemminger82499282012-03-19 17:24:43 -0700195static const char *link_modes[] = {
196 "DEFAULT", "DORMANT"
197};
198
199static void print_linkmode(FILE *f, struct rtattr *tb)
200{
201 unsigned int mode = rta_getattr_u8(tb);
202
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700203 if (mode >= ARRAY_SIZE(link_modes))
Stephen Hemminger82499282012-03-19 17:24:43 -0700204 fprintf(f, "mode %d ", mode);
205 else
206 fprintf(f, "mode %s ", link_modes[mode]);
207}
208
Phil Suttere0513802016-06-28 15:07:16 +0200209static char *parse_link_kind(struct rtattr *tb, bool slave)
Vadim Kochan712249d2014-12-10 10:11:29 +0200210{
211 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
Phil Suttere0513802016-06-28 15:07:16 +0200212 int attr = slave ? IFLA_INFO_SLAVE_KIND : IFLA_INFO_KIND;
Vadim Kochan712249d2014-12-10 10:11:29 +0200213
214 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
215
Phil Suttere0513802016-06-28 15:07:16 +0200216 if (linkinfo[attr])
217 return RTA_DATA(linkinfo[attr]);
Vadim Kochan712249d2014-12-10 10:11:29 +0200218
219 return "";
220}
221
Phil Sutter03ac85b2016-06-30 16:47:02 +0200222static int match_link_kind(struct rtattr **tb, const char *kind, bool slave)
Phil Suttere0513802016-06-28 15:07:16 +0200223{
224 if (!tb[IFLA_LINKINFO])
225 return -1;
226
227 return strcmp(parse_link_kind(tb[IFLA_LINKINFO], slave), kind);
228}
229
Patrick McHardy1d934832007-08-22 10:49:01 -0700230static void print_linktype(FILE *fp, struct rtattr *tb)
231{
232 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
233 struct link_util *lu;
Jiri Pirkofbea6112014-01-23 17:52:53 +0100234 struct link_util *slave_lu;
Hangbin Liu22a84712016-09-20 18:02:12 +0800235 char slave[32];
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);
Hangbin Liu22a84712016-09-20 18:02:12 +0800269 snprintf(slave, sizeof(slave), "%s_slave", slave_kind);
Jiri Pirkofbea6112014-01-23 17:52:53 +0100270
Hangbin Liu22a84712016-09-20 18:02:12 +0800271 slave_lu = get_link_kind(slave);
Jiri Pirkofbea6112014-01-23 17:52:53 +0100272 if (slave_lu && slave_lu->print_opt) {
273 struct rtattr *attr[slave_lu->maxattr+1], **data = NULL;
274
275 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
276 parse_rtattr_nested(attr, slave_lu->maxattr,
277 linkinfo[IFLA_INFO_SLAVE_DATA]);
278 data = attr;
279 }
280 slave_lu->print_opt(slave_lu, fp, data);
281 }
Patrick McHardy1d934832007-08-22 10:49:01 -0700282 }
283}
284
Jiri Pirkoee0067a2015-01-08 09:49:55 +0100285static void print_af_spec(FILE *fp, struct rtattr *af_spec_attr)
286{
287 struct rtattr *inet6_attr;
288 struct rtattr *tb[IFLA_INET6_MAX + 1];
289
290 inet6_attr = parse_rtattr_one_nested(AF_INET6, af_spec_attr);
291 if (!inet6_attr)
292 return;
293
294 parse_rtattr_nested(tb, IFLA_INET6_MAX, inet6_attr);
295
296 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
Hannes Frederic Sowa5c5176c2015-12-16 10:52:36 +0100297 __u8 mode = rta_getattr_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700298
Hannes Frederic Sowa5c5176c2015-12-16 10:52:36 +0100299 switch (mode) {
Jiri Pirkoee0067a2015-01-08 09:49:55 +0100300 case IN6_ADDR_GEN_MODE_EUI64:
301 fprintf(fp, "addrgenmode eui64 ");
302 break;
303 case IN6_ADDR_GEN_MODE_NONE:
304 fprintf(fp, "addrgenmode none ");
305 break;
Hannes Frederic Sowa5c5176c2015-12-16 10:52:36 +0100306 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
307 fprintf(fp, "addrgenmode stable_secret ");
308 break;
Bjørn Mork8e12bc02016-01-04 10:58:05 +0100309 case IN6_ADDR_GEN_MODE_RANDOM:
310 fprintf(fp, "addrgenmode random ");
311 break;
Hannes Frederic Sowa5c5176c2015-12-16 10:52:36 +0100312 default:
313 fprintf(fp, "addrgenmode %#.2hhx ", mode);
314 break;
Jiri Pirkoee0067a2015-01-08 09:49:55 +0100315 }
316 }
317}
318
Eran Ben Elishaa1b99712015-06-16 12:13:16 +0300319static void print_vf_stats64(FILE *fp, struct rtattr *vfstats);
320
Chris Wright3fd86632010-05-18 00:57:00 -0700321static void print_vfinfo(FILE *fp, struct rtattr *vfinfo)
322{
323 struct ifla_vf_mac *vf_mac;
Chris Wright3fd86632010-05-18 00:57:00 -0700324 struct ifla_vf_tx_rate *vf_tx_rate;
Vadim Kochan8c29ae72015-01-09 21:24:31 +0200325 struct rtattr *vf[IFLA_VF_MAX + 1] = {};
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700326
Chris Wright3fd86632010-05-18 00:57:00 -0700327 SPRINT_BUF(b1);
328
329 if (vfinfo->rta_type != IFLA_VF_INFO) {
330 fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type);
331 return;
332 }
333
334 parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo);
335
336 vf_mac = RTA_DATA(vf[IFLA_VF_MAC]);
Chris Wright3fd86632010-05-18 00:57:00 -0700337 vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]);
338
Phil Sutter6f07f3d2015-10-16 12:38:33 +0200339 fprintf(fp, "%s vf %d MAC %s", _SL_, vf_mac->vf,
Chris Wright3fd86632010-05-18 00:57:00 -0700340 ll_addr_n2a((unsigned char *)&vf_mac->mac,
Stephen Hemmingerbbac6c62016-04-11 22:13:55 +0000341 ETH_ALEN, 0, b1, sizeof(b1)));
Moshe Shemesh56e9f0a2016-09-28 10:58:59 +0300342 if (vf[IFLA_VF_VLAN_LIST]) {
343 struct rtattr *i, *vfvlanlist = vf[IFLA_VF_VLAN_LIST];
344 int rem = RTA_PAYLOAD(vfvlanlist);
345
346 for (i = RTA_DATA(vfvlanlist);
347 RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
348 struct ifla_vf_vlan_info *vf_vlan_info =
349 RTA_DATA(i);
350 SPRINT_BUF(b2);
351
352 if (vf_vlan_info->vlan)
353 fprintf(fp, ", vlan %d", vf_vlan_info->vlan);
354 if (vf_vlan_info->qos)
355 fprintf(fp, ", qos %d", vf_vlan_info->qos);
356 if (vf_vlan_info->vlan_proto &&
357 vf_vlan_info->vlan_proto != htons(ETH_P_8021Q))
358 fprintf(fp, ", vlan protocol %s",
359 ll_proto_n2a(vf_vlan_info->vlan_proto,
360 b2, sizeof(b2)));
361
362 }
363 } else {
364 struct ifla_vf_vlan *vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
365
366 if (vf_vlan->vlan)
367 fprintf(fp, ", vlan %d", vf_vlan->vlan);
368 if (vf_vlan->qos)
369 fprintf(fp, ", qos %d", vf_vlan->qos);
370 }
Chris Wright3fd86632010-05-18 00:57:00 -0700371 if (vf_tx_rate->rate)
372 fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate);
Vadim Kochan8c29ae72015-01-09 21:24:31 +0200373
374 if (vf[IFLA_VF_RATE]) {
375 struct ifla_vf_rate *vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
376
377 if (vf_rate->max_tx_rate)
378 fprintf(fp, ", max_tx_rate %dMbps", vf_rate->max_tx_rate);
379 if (vf_rate->min_tx_rate)
380 fprintf(fp, ", min_tx_rate %dMbps", vf_rate->min_tx_rate);
381 }
Phil Sutterff9463e2016-11-08 22:29:11 +0100382 if (vf[IFLA_VF_SPOOFCHK]) {
383 struct ifla_vf_spoofchk *vf_spoofchk =
384 RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
Vadim Kochan8c29ae72015-01-09 21:24:31 +0200385
Phil Sutterff9463e2016-11-08 22:29:11 +0100386 if (vf_spoofchk->setting != -1)
387 fprintf(fp, ", spoof checking %s",
Stephen Hemminger468fa022016-11-14 09:28:09 +0100388 vf_spoofchk->setting ? "on" : "off");
Greg Rose7b8179c2011-10-13 20:31:32 +0000389 }
Phil Sutterff9463e2016-11-08 22:29:11 +0100390 if (vf[IFLA_VF_LINK_STATE]) {
391 struct ifla_vf_link_state *vf_linkstate =
392 RTA_DATA(vf[IFLA_VF_LINK_STATE]);
393
Rony Efraim07fa9c12013-06-13 13:19:12 +0300394 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",
Stephen Hemminger468fa022016-11-14 09:28:09 +0100406 vf_trust->setting ? "on" : "off");
Phil Sutterfe932272016-03-31 14:43:32 +0200407 }
Phil Sutter4fb4a102016-11-14 09:29:54 +0100408 if (vf[IFLA_VF_RSS_QUERY_EN]) {
409 struct ifla_vf_rss_query_en *rss_query =
410 RTA_DATA(vf[IFLA_VF_RSS_QUERY_EN]);
411
412 if (rss_query->setting != -1)
413 fprintf(fp, ", query_rss %s",
414 rss_query->setting ? "on" : "off");
415 }
416 if (vf[IFLA_VF_RSS_QUERY_EN]) {
417 struct ifla_vf_rss_query_en *rss_query =
418 RTA_DATA(vf[IFLA_VF_RSS_QUERY_EN]);
419
420 if (rss_query->setting != -1)
421 fprintf(fp, ", query_rss %s",
422 rss_query->setting ? "on" : "off");
423 }
Eran Ben Elishaa1b99712015-06-16 12:13:16 +0300424 if (vf[IFLA_VF_STATS] && show_stats)
425 print_vf_stats64(fp, vf[IFLA_VF_STATS]);
Chris Wright3fd86632010-05-18 00:57:00 -0700426}
427
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700428static void print_num(FILE *fp, unsigned int width, uint64_t count)
Christian Hesseb68d9832014-10-31 22:33:13 +0100429{
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800430 const char *prefix = "kMGTPE";
431 const unsigned int base = use_iec ? 1024 : 1000;
Christian Hesseb68d9832014-10-31 22:33:13 +0100432 uint64_t powi = 1;
Christian Hesse50ec6652014-11-04 22:17:15 +0100433 uint16_t powj = 1;
434 uint8_t precision = 2;
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800435 char buf[64];
Christian Hesseb68d9832014-10-31 22:33:13 +0100436
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800437 if (!human_readable || count < base) {
Christian Hessef4fe81d2014-11-03 08:21:11 +0100438 fprintf(fp, "%-*"PRIu64" ", width, count);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800439 return;
Christian Hesseb68d9832014-10-31 22:33:13 +0100440 }
441
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800442 /* increase value by a factor of 1000/1024 and print
Stephen Hemminger468fa022016-11-14 09:28:09 +0100443 * if result is something a human can read
444 */
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700445 for (;;) {
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800446 powi *= base;
447 if (count / base < powi)
448 break;
Christian Hesseb68d9832014-10-31 22:33:13 +0100449
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800450 if (!prefix[1])
451 break;
452 ++prefix;
Christian Hesseb68d9832014-10-31 22:33:13 +0100453 }
454
Christian Hesse50ec6652014-11-04 22:17:15 +0100455 /* try to guess a good number of digits for precision */
456 for (; precision > 0; precision--) {
457 powj *= 10;
458 if (count / powi < powj)
459 break;
460 }
461
462 snprintf(buf, sizeof(buf), "%.*f%c%s", precision,
463 (double) count / powi, *prefix, use_iec ? "i" : "");
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800464
Christian Hessef4fe81d2014-11-03 08:21:11 +0100465 fprintf(fp, "%-*s ", width, buf);
Christian Hesseb68d9832014-10-31 22:33:13 +0100466}
467
Eran Ben Elishaa1b99712015-06-16 12:13:16 +0300468static void print_vf_stats64(FILE *fp, struct rtattr *vfstats)
469{
Phil Sutter30a88422016-07-18 16:48:44 +0200470 struct rtattr *vf[IFLA_VF_STATS_MAX + 1];
Eran Ben Elishaa1b99712015-06-16 12:13:16 +0300471
472 if (vfstats->rta_type != IFLA_VF_STATS) {
473 fprintf(stderr, "BUG: rta type is %d\n", vfstats->rta_type);
474 return;
475 }
476
477 parse_rtattr_nested(vf, IFLA_VF_MAX, vfstats);
478
479 /* RX stats */
480 fprintf(fp, "%s", _SL_);
481 fprintf(fp, " RX: bytes packets mcast bcast %s", _SL_);
482 fprintf(fp, " ");
483
484 print_num(fp, 10, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_RX_BYTES]));
485 print_num(fp, 8, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_RX_PACKETS]));
486 print_num(fp, 7, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_MULTICAST]));
487 print_num(fp, 7, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_BROADCAST]));
488
489 /* TX stats */
490 fprintf(fp, "%s", _SL_);
491 fprintf(fp, " TX: bytes packets %s", _SL_);
492 fprintf(fp, " ");
493
494 print_num(fp, 10, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_TX_BYTES]));
495 print_num(fp, 8, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_TX_PACKETS]));
496}
497
david decotigny30b55792014-05-05 20:38:18 -0700498static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s,
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700499 const struct rtattr *carrier_changes)
Stephen Hemminger4b726cb2014-05-09 12:36:46 -0700500{
Christian Hesseb68d9832014-10-31 22:33:13 +0100501 /* RX stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800502 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
503 s->rx_compressed ? "compressed" : "", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800504
505 fprintf(fp, " ");
506 print_num(fp, 10, s->rx_bytes);
507 print_num(fp, 8, s->rx_packets);
508 print_num(fp, 7, s->rx_errors);
509 print_num(fp, 7, s->rx_dropped);
510 print_num(fp, 7, s->rx_over_errors);
511 print_num(fp, 7, s->multicast);
512 if (s->rx_compressed)
513 print_num(fp, 7, s->rx_compressed);
Christian Hesseb68d9832014-10-31 22:33:13 +0100514
515 /* RX error stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800516 if (show_stats > 1) {
517 fprintf(fp, "%s", _SL_);
Stephen Hemminger07ec1832016-02-09 11:13:31 -0800518 fprintf(fp, " RX errors: length crc frame fifo missed%s%s",
519 s->rx_nohandler ? " nohandler" : "", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800520
521 fprintf(fp, " ");
522 print_num(fp, 8, s->rx_length_errors);
523 print_num(fp, 7, s->rx_crc_errors);
524 print_num(fp, 7, s->rx_frame_errors);
525 print_num(fp, 7, s->rx_fifo_errors);
526 print_num(fp, 7, s->rx_missed_errors);
Stephen Hemminger07ec1832016-02-09 11:13:31 -0800527 if (s->rx_nohandler)
528 print_num(fp, 7, s->rx_nohandler);
529
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800530 }
531 fprintf(fp, "%s", _SL_);
Christian Hesseb68d9832014-10-31 22:33:13 +0100532
533 /* TX stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800534 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800535 s->tx_compressed ? "compressed" : "", _SL_);
536
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800537 fprintf(fp, " ");
538 print_num(fp, 10, s->tx_bytes);
539 print_num(fp, 8, s->tx_packets);
540 print_num(fp, 7, s->tx_errors);
541 print_num(fp, 7, s->tx_dropped);
542 print_num(fp, 7, s->tx_carrier_errors);
543 print_num(fp, 7, s->collisions);
544 if (s->tx_compressed)
545 print_num(fp, 7, s->tx_compressed);
Christian Hesseb68d9832014-10-31 22:33:13 +0100546
547 /* TX error stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800548 if (show_stats > 1) {
549 fprintf(fp, "%s", _SL_);
Christian Hesseb68d9832014-10-31 22:33:13 +0100550 fprintf(fp, " TX errors: aborted fifo window heartbeat");
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700551 if (carrier_changes)
david decotigny30b55792014-05-05 20:38:18 -0700552 fprintf(fp, " transns");
Stephen Hemminger4ec0ffd2014-05-29 10:31:30 -0700553 fprintf(fp, "%s", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800554
555 fprintf(fp, " ");
556 print_num(fp, 8, s->tx_aborted_errors);
557 print_num(fp, 7, s->tx_fifo_errors);
558 print_num(fp, 7, s->tx_window_errors);
559 print_num(fp, 7, s->tx_heartbeat_errors);
560 if (carrier_changes)
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700561 print_num(fp, 7, *(uint32_t *)RTA_DATA(carrier_changes));
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800562 }
563}
564
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200565static void print_link_stats32(FILE *fp, const struct rtnl_link_stats *s,
566 const struct rtattr *carrier_changes)
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800567{
Christian Hesseb68d9832014-10-31 22:33:13 +0100568 /* RX stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800569 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
570 s->rx_compressed ? "compressed" : "", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800571
572
573 fprintf(fp, " ");
574 print_num(fp, 10, s->rx_bytes);
575 print_num(fp, 8, s->rx_packets);
576 print_num(fp, 7, s->rx_errors);
577 print_num(fp, 7, s->rx_dropped);
578 print_num(fp, 7, s->rx_over_errors);
579 print_num(fp, 7, s->multicast);
580 if (s->rx_compressed)
581 print_num(fp, 7, s->rx_compressed);
Christian Hesseb68d9832014-10-31 22:33:13 +0100582
583 /* RX error stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800584 if (show_stats > 1) {
585 fprintf(fp, "%s", _SL_);
Stephen Hemminger07ec1832016-02-09 11:13:31 -0800586 fprintf(fp, " RX errors: length crc frame fifo missed%s%s",
587 s->rx_nohandler ? " nohandler" : "", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800588 fprintf(fp, " ");
589 print_num(fp, 8, s->rx_length_errors);
590 print_num(fp, 7, s->rx_crc_errors);
591 print_num(fp, 7, s->rx_frame_errors);
592 print_num(fp, 7, s->rx_fifo_errors);
593 print_num(fp, 7, s->rx_missed_errors);
Stephen Hemminger07ec1832016-02-09 11:13:31 -0800594 if (s->rx_nohandler)
595 print_num(fp, 7, s->rx_nohandler);
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800596 }
597 fprintf(fp, "%s", _SL_);
Christian Hesseb68d9832014-10-31 22:33:13 +0100598
599 /* TX stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800600 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
601 s->tx_compressed ? "compressed" : "", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800602
603 fprintf(fp, " ");
604 print_num(fp, 10, s->tx_bytes);
605 print_num(fp, 8, s->tx_packets);
606 print_num(fp, 7, s->tx_errors);
607 print_num(fp, 7, s->tx_dropped);
608 print_num(fp, 7, s->tx_carrier_errors);
609 print_num(fp, 7, s->collisions);
610 if (s->tx_compressed)
611 print_num(fp, 7, s->tx_compressed);
Christian Hesseb68d9832014-10-31 22:33:13 +0100612
613 /* TX error stats */
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800614 if (show_stats > 1) {
615 fprintf(fp, "%s", _SL_);
Christian Hesseb68d9832014-10-31 22:33:13 +0100616 fprintf(fp, " TX errors: aborted fifo window heartbeat");
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700617 if (carrier_changes)
david decotigny30b55792014-05-05 20:38:18 -0700618 fprintf(fp, " transns");
Stephen Hemminger4ec0ffd2014-05-29 10:31:30 -0700619 fprintf(fp, "%s", _SL_);
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800620
621 fprintf(fp, " ");
622 print_num(fp, 8, s->tx_aborted_errors);
623 print_num(fp, 7, s->tx_fifo_errors);
624 print_num(fp, 7, s->tx_window_errors);
625 print_num(fp, 7, s->tx_heartbeat_errors);
626 if (carrier_changes)
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700627 print_num(fp, 7, *(uint32_t *)RTA_DATA(carrier_changes));
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800628 }
629}
630
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200631static void __print_link_stats(FILE *fp, struct rtattr **tb)
632{
Stephen Hemminger07ec1832016-02-09 11:13:31 -0800633 const struct rtattr *carrier_changes = tb[IFLA_CARRIER_CHANGES];
634
635 if (tb[IFLA_STATS64]) {
636 struct rtnl_link_stats64 stats = { 0 };
637
638 memcpy(&stats, RTA_DATA(tb[IFLA_STATS64]),
639 MIN(RTA_PAYLOAD(tb[IFLA_STATS64]), sizeof(stats)));
640
641 print_link_stats64(fp, &stats, carrier_changes);
642 } else if (tb[IFLA_STATS]) {
643 struct rtnl_link_stats stats = { 0 };
644
645 memcpy(&stats, RTA_DATA(tb[IFLA_STATS]),
646 MIN(RTA_PAYLOAD(tb[IFLA_STATS]), sizeof(stats)));
647
648 print_link_stats32(fp, &stats, carrier_changes);
649 }
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200650}
651
652static void print_link_stats(FILE *fp, struct nlmsghdr *n)
653{
654 struct ifinfomsg *ifi = NLMSG_DATA(n);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700655 struct rtattr *tb[IFLA_MAX+1];
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200656
657 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi),
658 n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
659 __print_link_stats(fp, tb);
660 fprintf(fp, "%s", _SL_);
661}
662
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400663int print_linkinfo_brief(const struct sockaddr_nl *who,
664 struct nlmsghdr *n, void *arg)
665{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700666 FILE *fp = (FILE *)arg;
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400667 struct ifinfomsg *ifi = NLMSG_DATA(n);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700668 struct rtattr *tb[IFLA_MAX+1];
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400669 int len = n->nlmsg_len;
670 char *name;
671 char buf[32] = { 0, };
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700672 unsigned int m_flag = 0;
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400673
674 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
675 return -1;
676
677 len -= NLMSG_LENGTH(sizeof(*ifi));
678 if (len < 0)
679 return -1;
680
681 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
682 return -1;
683 if (filter.up && !(ifi->ifi_flags&IFF_UP))
684 return -1;
685
686 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
Stephen Hemminger468fa022016-11-14 09:28:09 +0100687 if (tb[IFLA_IFNAME] == NULL)
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400688 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
Stephen Hemminger468fa022016-11-14 09:28:09 +0100689
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400690 if (filter.label &&
691 (!filter.family || filter.family == AF_PACKET) &&
692 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
693 return -1;
694
695 if (tb[IFLA_GROUP]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700696 int group = *(int *)RTA_DATA(tb[IFLA_GROUP]);
697
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400698 if (filter.group != -1 && group != filter.group)
699 return -1;
700 }
701
David Ahern84d30af2015-09-23 16:44:56 -0600702 if (tb[IFLA_MASTER]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700703 int master = *(int *)RTA_DATA(tb[IFLA_MASTER]);
704
David Ahern84d30af2015-09-23 16:44:56 -0600705 if (filter.master > 0 && master != filter.master)
706 return -1;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700707 } else if (filter.master > 0)
David Ahern84d30af2015-09-23 16:44:56 -0600708 return -1;
709
Phil Suttere0513802016-06-28 15:07:16 +0200710 if (filter.kind && match_link_kind(tb, filter.kind, 0))
711 return -1;
David Ahern84d30af2015-09-23 16:44:56 -0600712
Phil Suttere0513802016-06-28 15:07:16 +0200713 if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
714 return -1;
David Ahern84d30af2015-09-23 16:44:56 -0600715
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400716 if (n->nlmsg_type == RTM_DELLINK)
717 fprintf(fp, "Deleted ");
718
719 name = (char *)(tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
720
721 if (tb[IFLA_LINK]) {
722 SPRINT_BUF(b1);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700723 int iflink = *(int *)RTA_DATA(tb[IFLA_LINK]);
724
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400725 if (iflink == 0)
726 snprintf(buf, sizeof(buf), "%s@NONE", name);
727 else {
728 snprintf(buf, sizeof(buf),
729 "%s@%s", name, ll_idx_n2a(iflink, b1));
730 m_flag = ll_index_to_flags(iflink);
731 m_flag = !(m_flag & IFF_UP);
732 }
733 } else
734 snprintf(buf, sizeof(buf), "%s", name);
735
736 fprintf(fp, "%-16s ", buf);
737
738 if (tb[IFLA_OPERSTATE])
739 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
740
741 if (filter.family == AF_PACKET) {
742 SPRINT_BUF(b1);
743 if (tb[IFLA_ADDRESS]) {
744 color_fprintf(fp, COLOR_MAC, "%s ",
745 ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
746 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
747 ifi->ifi_type,
748 b1, sizeof(b1)));
749 }
750 }
751
752 if (filter.family == AF_PACKET)
753 print_link_flags(fp, ifi->ifi_flags, m_flag);
754
755 if (filter.family == AF_PACKET)
756 fprintf(fp, "\n");
757 fflush(fp);
758 return 0;
759}
760
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800761int print_linkinfo(const struct sockaddr_nl *who,
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000762 struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000763{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700764 FILE *fp = (FILE *)arg;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000765 struct ifinfomsg *ifi = NLMSG_DATA(n);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700766 struct rtattr *tb[IFLA_MAX+1];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000767 int len = n->nlmsg_len;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700768 unsigned int m_flag = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000769
770 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
771 return 0;
772
773 len -= NLMSG_LENGTH(sizeof(*ifi));
774 if (len < 0)
775 return -1;
776
777 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
778 return 0;
779 if (filter.up && !(ifi->ifi_flags&IFF_UP))
780 return 0;
781
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000782 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
Stephen Hemminger468fa022016-11-14 09:28:09 +0100783 if (tb[IFLA_IFNAME] == NULL)
jamal4cd23bd2008-08-08 10:06:17 -0400784 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
Stephen Hemminger468fa022016-11-14 09:28:09 +0100785
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000786 if (filter.label &&
787 (!filter.family || filter.family == AF_PACKET) &&
788 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
789 return 0;
790
Vlad Dogaruf960c922011-02-02 20:23:40 +0200791 if (tb[IFLA_GROUP]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700792 int group = *(int *)RTA_DATA(tb[IFLA_GROUP]);
793
Stefan Tomanekc4fdf752013-08-03 14:20:53 +0200794 if (filter.group != -1 && group != filter.group)
Vlad Dogaruf960c922011-02-02 20:23:40 +0200795 return -1;
796 }
797
Vadim Kochan1b944142014-11-29 11:27:41 -0800798 if (tb[IFLA_MASTER]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700799 int master = *(int *)RTA_DATA(tb[IFLA_MASTER]);
800
Vadim Kochan1b944142014-11-29 11:27:41 -0800801 if (filter.master > 0 && master != filter.master)
802 return -1;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700803 } else if (filter.master > 0)
Vadim Kochan1b944142014-11-29 11:27:41 -0800804 return -1;
805
Phil Suttere0513802016-06-28 15:07:16 +0200806 if (filter.kind && match_link_kind(tb, filter.kind, 0))
807 return -1;
Vadim Kochan712249d2014-12-10 10:11:29 +0200808
Phil Suttere0513802016-06-28 15:07:16 +0200809 if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
810 return -1;
Vadim Kochan712249d2014-12-10 10:11:29 +0200811
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000812 if (n->nlmsg_type == RTM_DELLINK)
813 fprintf(fp, "Deleted ");
814
Mathias Nymand7bd2db2015-04-28 13:18:21 +0300815 fprintf(fp, "%d: ", ifi->ifi_index);
816 color_fprintf(fp, COLOR_IFNAME, "%s",
Stephen Hemminger11522e72016-04-11 22:07:51 +0000817 tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000818
819 if (tb[IFLA_LINK]) {
820 SPRINT_BUF(b1);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700821 int iflink = *(int *)RTA_DATA(tb[IFLA_LINK]);
822
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000823 if (iflink == 0)
824 fprintf(fp, "@NONE: ");
825 else {
Nicolas Dichtelccdcbf32015-02-17 17:30:38 +0100826 if (tb[IFLA_LINK_NETNSID])
827 fprintf(fp, "@if%d: ", iflink);
828 else {
829 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
830 m_flag = ll_index_to_flags(iflink);
831 m_flag = !(m_flag & IFF_UP);
832 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000833 }
834 } else {
835 fprintf(fp, ": ");
836 }
837 print_link_flags(fp, ifi->ifi_flags, m_flag);
838
839 if (tb[IFLA_MTU])
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700840 fprintf(fp, "mtu %u ", *(int *)RTA_DATA(tb[IFLA_MTU]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000841 if (tb[IFLA_QDISC])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700842 fprintf(fp, "qdisc %s ", rta_getattr_str(tb[IFLA_QDISC]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000843 if (tb[IFLA_MASTER]) {
844 SPRINT_BUF(b1);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700845 fprintf(fp, "master %s ", ll_idx_n2a(*(int *)RTA_DATA(tb[IFLA_MASTER]), b1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000846 }
Stephen Hemminger82499282012-03-19 17:24:43 -0700847
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700848 if (tb[IFLA_OPERSTATE])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700849 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
Stephen Hemminger82499282012-03-19 17:24:43 -0700850
851 if (do_link && tb[IFLA_LINKMODE])
852 print_linkmode(fp, tb[IFLA_LINKMODE]);
853
Stefan Tomanekc4fdf752013-08-03 14:20:53 +0200854 if (tb[IFLA_GROUP]) {
855 SPRINT_BUF(b1);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700856 int group = *(int *)RTA_DATA(tb[IFLA_GROUP]);
857
Stefan Tomanekc4fdf752013-08-03 14:20:53 +0200858 fprintf(fp, "group %s ", rtnl_group_n2a(group, b1, sizeof(b1)));
859 }
860
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000861 if (filter.showqueue)
Eric Dumazetf78e3162009-10-22 18:13:21 +0000862 print_queuelen(fp, tb);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800863
Nicolas Dichtel2ec28932014-12-04 17:41:07 +0100864 if (!filter.family || filter.family == AF_PACKET || show_details) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000865 SPRINT_BUF(b1);
866 fprintf(fp, "%s", _SL_);
867 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
868
869 if (tb[IFLA_ADDRESS]) {
Mathias Nymand7bd2db2015-04-28 13:18:21 +0300870 color_fprintf(fp, COLOR_MAC, "%s",
Stephen Hemminger11522e72016-04-11 22:07:51 +0000871 ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
872 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
873 ifi->ifi_type,
874 b1, sizeof(b1)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000875 }
876 if (tb[IFLA_BROADCAST]) {
877 if (ifi->ifi_flags&IFF_POINTOPOINT)
878 fprintf(fp, " peer ");
879 else
880 fprintf(fp, " brd ");
Phil Sutterff9d8f32016-03-22 19:35:14 +0100881 color_fprintf(fp, COLOR_MAC, "%s",
Stephen Hemminger11522e72016-04-11 22:07:51 +0000882 ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
883 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
884 ifi->ifi_type,
885 b1, sizeof(b1)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000886 }
887 }
Patrick McHardy1d934832007-08-22 10:49:01 -0700888
Nicolas Dichtelccdcbf32015-02-17 17:30:38 +0100889 if (tb[IFLA_LINK_NETNSID]) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700890 int id = *(int *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
Nicolas Dichtelccdcbf32015-02-17 17:30:38 +0100891
892 if (id >= 0)
893 fprintf(fp, " link-netnsid %d", id);
894 else
895 fprintf(fp, " link-netnsid unknown");
896 }
897
Anuradha Karuppiah18864822015-07-14 13:43:22 -0700898 if (tb[IFLA_PROTO_DOWN]) {
899 if (rta_getattr_u8(tb[IFLA_PROTO_DOWN]))
900 fprintf(fp, " protodown on ");
901 }
902
Stephen Hemminger11522e72016-04-11 22:07:51 +0000903 if (show_details) {
904 if (tb[IFLA_PROMISCUITY])
905 fprintf(fp, " promiscuity %u ",
906 *(int *)RTA_DATA(tb[IFLA_PROMISCUITY]));
Stephen Hemminger1cb6a112013-02-05 08:16:28 -0800907
Stephen Hemminger11522e72016-04-11 22:07:51 +0000908 if (tb[IFLA_LINKINFO])
909 print_linktype(fp, tb[IFLA_LINKINFO]);
Patrick McHardy1d934832007-08-22 10:49:01 -0700910
Stephen Hemminger11522e72016-04-11 22:07:51 +0000911 if (do_link && tb[IFLA_AF_SPEC])
912 print_af_spec(fp, tb[IFLA_AF_SPEC]);
Jiri Pirkoee0067a2015-01-08 09:49:55 +0100913
Stephen Hemminger11522e72016-04-11 22:07:51 +0000914 if (tb[IFLA_NUM_TX_QUEUES])
915 fprintf(fp, "numtxqueues %u ",
916 rta_getattr_u32(tb[IFLA_NUM_TX_QUEUES]));
Eric Dumazetf1c656e2016-04-07 16:11:39 -0700917
Stephen Hemminger11522e72016-04-11 22:07:51 +0000918 if (tb[IFLA_NUM_RX_QUEUES])
919 fprintf(fp, "numrxqueues %u ",
920 rta_getattr_u32(tb[IFLA_NUM_RX_QUEUES]));
921
Eric Dumazet1acd2082016-08-17 14:39:21 -0700922 if (tb[IFLA_GSO_MAX_SIZE])
923 fprintf(fp, "gso_max_size %u ",
924 rta_getattr_u32(tb[IFLA_GSO_MAX_SIZE]));
925
926 if (tb[IFLA_GSO_MAX_SEGS])
927 fprintf(fp, "gso_max_segs %u ",
928 rta_getattr_u32(tb[IFLA_GSO_MAX_SEGS]));
929
Stephen Hemminger11522e72016-04-11 22:07:51 +0000930 if (tb[IFLA_PHYS_PORT_NAME])
931 fprintf(fp, "portname %s ",
932 rta_getattr_str(tb[IFLA_PHYS_PORT_NAME]));
933
934 if (tb[IFLA_PHYS_PORT_ID]) {
935 SPRINT_BUF(b1);
936 fprintf(fp, "portid %s ",
937 hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_PORT_ID]),
938 RTA_PAYLOAD(tb[IFLA_PHYS_PORT_ID]),
939 b1, sizeof(b1)));
940 }
941
942 if (tb[IFLA_PHYS_SWITCH_ID]) {
943 SPRINT_BUF(b1);
944 fprintf(fp, "switchid %s ",
945 hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_SWITCH_ID]),
946 RTA_PAYLOAD(tb[IFLA_PHYS_SWITCH_ID]),
947 b1, sizeof(b1)));
948 }
949 }
950
Eric Dumazetf1c656e2016-04-07 16:11:39 -0700951
Nicolas Dichtel2ec28932014-12-04 17:41:07 +0100952 if ((do_link || show_details) && tb[IFLA_IFALIAS]) {
Stephen Hemminger1124ffb2013-03-14 13:47:49 -0700953 fprintf(fp, "%s alias %s", _SL_,
Stephen Hemmingerff247462012-04-10 08:47:55 -0700954 rta_getattr_str(tb[IFLA_IFALIAS]));
roopa263c8942013-03-12 15:04:02 -0700955 }
Stephen Hemmingerace9c962009-03-23 10:46:47 -0700956
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800957 if (do_link && show_stats) {
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200958 fprintf(fp, "%s", _SL_);
959 __print_link_stats(fp, tb);
Jan Engelhardt8864ac92010-03-11 10:00:34 +0000960 }
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800961
Nicolas Dichtel2ec28932014-12-04 17:41:07 +0100962 if ((do_link || show_details) && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) {
Chris Wright3fd86632010-05-18 00:57:00 -0700963 struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST];
964 int rem = RTA_PAYLOAD(vflist);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700965
Chris Wright3fd86632010-05-18 00:57:00 -0700966 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
967 print_vfinfo(fp, i);
Williams, Mitch Aae7229d2010-02-10 01:47:08 +0000968 }
Chris Wright3fd86632010-05-18 00:57:00 -0700969
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000970 fprintf(fp, "\n");
971 fflush(fp);
Jiri Benc5d5cf1b2014-10-03 15:25:02 +0200972 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000973}
974
975static int flush_update(void)
976{
Neil Hormane149d4e2015-11-05 14:54:17 -0500977
978 /*
979 * Note that the kernel may delete multiple addresses for one
980 * delete request (e.g. if ipv4 address promotion is disabled).
981 * Since a flush operation is really a series of delete requests
982 * its possible that we may request an address delete that has
983 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
984 * errors returned from a flush request
985 */
986 if ((rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) &&
987 (errno != EADDRNOTAVAIL)) {
Stephen Hemminger1fb0a992008-01-26 11:08:31 -0800988 perror("Failed to send flush request");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000989 return -1;
990 }
991 filter.flushp = 0;
992 return 0;
993}
994
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900995static int set_lifetime(unsigned int *lifetime, char *argv)
996{
997 if (strcmp(argv, "forever") == 0)
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900998 *lifetime = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900999 else if (get_u32(lifetime, argv, 0))
1000 return -1;
1001
1002 return 0;
1003}
1004
Jiri Pirko37c9b942014-01-06 10:17:09 +01001005static unsigned int get_ifa_flags(struct ifaddrmsg *ifa,
1006 struct rtattr *ifa_flags_attr)
1007{
1008 return ifa_flags_attr ? rta_getattr_u32(ifa_flags_attr) :
1009 ifa->ifa_flags;
1010}
1011
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001012int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +00001013 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001014{
Stephen Hemminger4b726cb2014-05-09 12:36:46 -07001015 FILE *fp = arg;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001016 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1017 int len = n->nlmsg_len;
Benedikt Gollatz037d9502009-01-06 19:36:56 -08001018 int deprecated = 0;
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001019 /* Use local copy of ifa_flags to not interfere with filtering code */
1020 unsigned int ifa_flags;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001021 struct rtattr *rta_tb[IFA_MAX+1];
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001022
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001023 SPRINT_BUF(b1);
1024
1025 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
1026 return 0;
1027 len -= NLMSG_LENGTH(sizeof(*ifa));
1028 if (len < 0) {
1029 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
1030 return -1;
1031 }
1032
1033 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
1034 return 0;
1035
Stephen Hemminger4b726cb2014-05-09 12:36:46 -07001036 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa),
1037 n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001038
Jiri Pirko37c9b942014-01-06 10:17:09 +01001039 ifa_flags = get_ifa_flags(ifa, rta_tb[IFA_FLAGS]);
1040
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001041 if (!rta_tb[IFA_LOCAL])
1042 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
1043 if (!rta_tb[IFA_ADDRESS])
1044 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
1045
1046 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
1047 return 0;
1048 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
1049 return 0;
Jiri Pirko37c9b942014-01-06 10:17:09 +01001050 if ((filter.flags ^ ifa_flags) & filter.flagmask)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001051 return 0;
1052 if (filter.label) {
1053 SPRINT_BUF(b1);
1054 const char *label;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001055
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001056 if (rta_tb[IFA_LABEL])
1057 label = RTA_DATA(rta_tb[IFA_LABEL]);
1058 else
1059 label = ll_idx_n2a(ifa->ifa_index, b1);
1060 if (fnmatch(filter.label, label, 0) != 0)
1061 return 0;
1062 }
1063 if (filter.pfx.family) {
1064 if (rta_tb[IFA_LOCAL]) {
Phil Sutterd17b1362016-07-18 16:48:42 +02001065 inet_prefix dst = { .family = ifa->ifa_family };
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001066
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001067 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
1068 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
1069 return 0;
1070 }
1071 }
1072
net[shemminger]!shemminger3eb17312005-02-07 18:28:31 +00001073 if (filter.family && filter.family != ifa->ifa_family)
1074 return 0;
1075
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001076 if (filter.flushb) {
1077 struct nlmsghdr *fn;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001078
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001079 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
1080 if (flush_update())
1081 return -1;
1082 }
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001083 fn = (struct nlmsghdr *)(filter.flushb + NLMSG_ALIGN(filter.flushp));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001084 memcpy(fn, n, n->nlmsg_len);
1085 fn->nlmsg_type = RTM_DELADDR;
1086 fn->nlmsg_flags = NLM_F_REQUEST;
shemminger351efcd2005-09-01 19:21:50 +00001087 fn->nlmsg_seq = ++rth.seq;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001088 filter.flushp = (((char *)fn) + n->nlmsg_len) - filter.flushb;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001089 filter.flushed++;
1090 if (show_stats < 2)
1091 return 0;
1092 }
1093
1094 if (n->nlmsg_type == RTM_DELADDR)
1095 fprintf(fp, "Deleted ");
1096
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001097 if (!brief) {
1098 if (filter.oneline || filter.flushb)
1099 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
1100 if (ifa->ifa_family == AF_INET)
1101 fprintf(fp, " inet ");
1102 else if (ifa->ifa_family == AF_INET6)
1103 fprintf(fp, " inet6 ");
1104 else if (ifa->ifa_family == AF_DECnet)
1105 fprintf(fp, " dnet ");
1106 else if (ifa->ifa_family == AF_IPX)
1107 fprintf(fp, " ipx ");
1108 else
1109 fprintf(fp, " family %d ", ifa->ifa_family);
1110 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001111
1112 if (rta_tb[IFA_LOCAL]) {
Phil Suttera1121aa2016-03-22 19:35:13 +01001113 color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s",
Stephen Hemminger468fa022016-11-14 09:28:09 +01001114 format_host_rta(ifa->ifa_family,
1115 rta_tb[IFA_LOCAL]));
Phil Sutterff9d8f32016-03-22 19:35:14 +01001116 if (rta_tb[IFA_ADDRESS] &&
1117 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]),
Stephen Hemminger468fa022016-11-14 09:28:09 +01001118 RTA_DATA(rta_tb[IFA_LOCAL]),
1119 ifa->ifa_family == AF_INET ? 4 : 16)) {
Phil Sutterff9d8f32016-03-22 19:35:14 +01001120 fprintf(fp, " peer ");
1121 color_fprintf(fp, ifa_family_color(ifa->ifa_family),
Stephen Hemminger468fa022016-11-14 09:28:09 +01001122 "%s", format_host_rta(ifa->ifa_family,
1123 rta_tb[IFA_ADDRESS]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001124 }
Phil Sutterff9d8f32016-03-22 19:35:14 +01001125 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001126 }
1127
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001128 if (brief)
1129 goto brief_exit;
1130
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001131 if (rta_tb[IFA_BROADCAST]) {
Phil Sutterff9d8f32016-03-22 19:35:14 +01001132 fprintf(fp, "brd ");
1133 color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
Stephen Hemminger468fa022016-11-14 09:28:09 +01001134 format_host_rta(ifa->ifa_family,
1135 rta_tb[IFA_BROADCAST]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001136 }
1137 if (rta_tb[IFA_ANYCAST]) {
Phil Sutterff9d8f32016-03-22 19:35:14 +01001138 fprintf(fp, "any ");
1139 color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
Stephen Hemminger468fa022016-11-14 09:28:09 +01001140 format_host_rta(ifa->ifa_family,
1141 rta_tb[IFA_ANYCAST]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001142 }
1143 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
Jiri Pirko37c9b942014-01-06 10:17:09 +01001144 if (ifa_flags & IFA_F_SECONDARY) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001145 ifa_flags &= ~IFA_F_SECONDARY;
Brian Haleya1b9ffc2009-09-14 17:01:43 -04001146 if (ifa->ifa_family == AF_INET6)
1147 fprintf(fp, "temporary ");
1148 else
1149 fprintf(fp, "secondary ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001150 }
Jiri Pirko37c9b942014-01-06 10:17:09 +01001151 if (ifa_flags & IFA_F_TENTATIVE) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001152 ifa_flags &= ~IFA_F_TENTATIVE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001153 fprintf(fp, "tentative ");
1154 }
Jiri Pirko37c9b942014-01-06 10:17:09 +01001155 if (ifa_flags & IFA_F_DEPRECATED) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001156 ifa_flags &= ~IFA_F_DEPRECATED;
Benedikt Gollatz037d9502009-01-06 19:36:56 -08001157 deprecated = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001158 fprintf(fp, "deprecated ");
1159 }
Jiri Pirko37c9b942014-01-06 10:17:09 +01001160 if (ifa_flags & IFA_F_HOMEADDRESS) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001161 ifa_flags &= ~IFA_F_HOMEADDRESS;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001162 fprintf(fp, "home ");
1163 }
Jiri Pirko37c9b942014-01-06 10:17:09 +01001164 if (ifa_flags & IFA_F_NODAD) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001165 ifa_flags &= ~IFA_F_NODAD;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001166 fprintf(fp, "nodad ");
1167 }
Jiri Pirko5b7e21c2014-01-06 10:17:10 +01001168 if (ifa_flags & IFA_F_MANAGETEMPADDR) {
1169 ifa_flags &= ~IFA_F_MANAGETEMPADDR;
1170 fprintf(fp, "mngtmpaddr ");
1171 }
Thomas Haller58c69b22014-01-07 21:46:28 +01001172 if (ifa_flags & IFA_F_NOPREFIXROUTE) {
1173 ifa_flags &= ~IFA_F_NOPREFIXROUTE;
1174 fprintf(fp, "noprefixroute ");
1175 }
Madhu Challae31867a2015-03-04 10:30:10 -08001176 if (ifa_flags & IFA_F_MCAUTOJOIN) {
1177 ifa_flags &= ~IFA_F_MCAUTOJOIN;
1178 fprintf(fp, "autojoin ");
1179 }
Stephen Hemminger468fa022016-11-14 09:28:09 +01001180 if (!(ifa_flags & IFA_F_PERMANENT))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001181 fprintf(fp, "dynamic ");
Stephen Hemminger468fa022016-11-14 09:28:09 +01001182 else
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001183 ifa_flags &= ~IFA_F_PERMANENT;
Jiri Pirko37c9b942014-01-06 10:17:09 +01001184 if (ifa_flags & IFA_F_DADFAILED) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001185 ifa_flags &= ~IFA_F_DADFAILED;
Brian Haleyf4af8512009-12-01 15:58:44 -08001186 fprintf(fp, "dadfailed ");
1187 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -07001188 if (ifa_flags)
1189 fprintf(fp, "flags %02x ", ifa_flags);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001190 if (rta_tb[IFA_LABEL])
Stephen Hemmingerff247462012-04-10 08:47:55 -07001191 fprintf(fp, "%s", rta_getattr_str(rta_tb[IFA_LABEL]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001192 if (rta_tb[IFA_CACHEINFO]) {
1193 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001194
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001195 fprintf(fp, "%s", _SL_);
Andreas Schwabf66efad2010-11-05 23:26:29 +00001196 fprintf(fp, " valid_lft ");
Masahide NAKAMURA141bb602006-11-24 12:27:01 +09001197 if (ci->ifa_valid == INFINITY_LIFE_TIME)
Andreas Schwabf66efad2010-11-05 23:26:29 +00001198 fprintf(fp, "forever");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001199 else
Andreas Schwabf66efad2010-11-05 23:26:29 +00001200 fprintf(fp, "%usec", ci->ifa_valid);
1201 fprintf(fp, " preferred_lft ");
Masahide NAKAMURA141bb602006-11-24 12:27:01 +09001202 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
Andreas Schwabf66efad2010-11-05 23:26:29 +00001203 fprintf(fp, "forever");
Benedikt Gollatz037d9502009-01-06 19:36:56 -08001204 else {
1205 if (deprecated)
Andreas Schwabf66efad2010-11-05 23:26:29 +00001206 fprintf(fp, "%dsec", ci->ifa_prefered);
Benedikt Gollatz037d9502009-01-06 19:36:56 -08001207 else
Andreas Schwabf66efad2010-11-05 23:26:29 +00001208 fprintf(fp, "%usec", ci->ifa_prefered);
Benedikt Gollatz037d9502009-01-06 19:36:56 -08001209 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001210 }
1211 fprintf(fp, "\n");
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001212brief_exit:
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001213 fflush(fp);
1214 return 0;
1215}
1216
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001217struct nlmsg_list {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001218 struct nlmsg_list *next;
1219 struct nlmsghdr h;
1220};
1221
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001222struct nlmsg_chain {
Eric Dumazet62e2e542012-06-09 13:55:55 +02001223 struct nlmsg_list *head;
1224 struct nlmsg_list *tail;
1225};
1226
Andreas Henriksson5e5055b2015-01-23 13:10:33 +01001227static int print_selected_addrinfo(struct ifinfomsg *ifi,
1228 struct nlmsg_list *ainfo, FILE *fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001229{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001230 for ( ; ainfo ; ainfo = ainfo->next) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001231 struct nlmsghdr *n = &ainfo->h;
1232 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1233
1234 if (n->nlmsg_type != RTM_NEWADDR)
1235 continue;
1236
Igor Ryzhov6cf26092016-10-04 13:16:55 +03001237 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifa)))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001238 return -1;
1239
Andreas Henriksson5e5055b2015-01-23 13:10:33 +01001240 if (ifa->ifa_index != ifi->ifi_index ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001241 (filter.family && filter.family != ifa->ifa_family))
1242 continue;
1243
Andreas Henriksson5e5055b2015-01-23 13:10:33 +01001244 if (filter.up && !(ifi->ifi_flags&IFF_UP))
1245 continue;
1246
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001247 print_addrinfo(NULL, n, fp);
1248 }
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001249 if (brief) {
1250 fprintf(fp, "\n");
1251 fflush(fp);
1252 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001253 return 0;
1254}
1255
1256
Stephen Hemmingerae665a52006-12-05 10:10:22 -08001257static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +00001258 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001259{
Eric Dumazet62e2e542012-06-09 13:55:55 +02001260 struct nlmsg_chain *lchain = (struct nlmsg_chain *)arg;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001261 struct nlmsg_list *h;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001262
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001263 h = malloc(n->nlmsg_len+sizeof(void *));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001264 if (h == NULL)
1265 return -1;
1266
1267 memcpy(&h->h, n, n->nlmsg_len);
1268 h->next = NULL;
1269
Eric Dumazet62e2e542012-06-09 13:55:55 +02001270 if (lchain->tail)
1271 lchain->tail->next = h;
1272 else
1273 lchain->head = h;
1274 lchain->tail = h;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001275
1276 ll_remember_index(who, n, NULL);
1277 return 0;
1278}
1279
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001280static __u32 ipadd_dump_magic = 0x47361222;
1281
1282static int ipadd_save_prep(void)
1283{
1284 int ret;
1285
1286 if (isatty(STDOUT_FILENO)) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +00001287 fprintf(stderr, "Not sending a binary stream to stdout\n");
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001288 return -1;
1289 }
1290
1291 ret = write(STDOUT_FILENO, &ipadd_dump_magic, sizeof(ipadd_dump_magic));
1292 if (ret != sizeof(ipadd_dump_magic)) {
1293 fprintf(stderr, "Can't write magic to dump file\n");
1294 return -1;
1295 }
1296
1297 return 0;
1298}
1299
1300static int ipadd_dump_check_magic(void)
1301{
1302 int ret;
1303 __u32 magic = 0;
1304
1305 if (isatty(STDIN_FILENO)) {
Pavel Šimerda4e972d52015-08-12 22:04:08 +02001306 fprintf(stderr, "Can't restore address dump from a terminal\n");
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001307 return -1;
1308 }
1309
1310 ret = fread(&magic, sizeof(magic), 1, stdin);
1311 if (magic != ipadd_dump_magic) {
1312 fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n", ret, magic);
1313 return -1;
1314 }
1315
1316 return 0;
1317}
1318
1319static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
1320 void *arg)
1321{
1322 int ret;
1323
1324 ret = write(STDOUT_FILENO, n, n->nlmsg_len);
1325 if ((ret > 0) && (ret != n->nlmsg_len)) {
1326 fprintf(stderr, "Short write while saving nlmsg\n");
1327 ret = -EIO;
1328 }
1329
1330 return ret == n->nlmsg_len ? 0 : ret;
1331}
1332
Nicolas Dichtel0628cdd2015-05-20 16:19:58 +02001333static int show_handler(const struct sockaddr_nl *nl,
1334 struct rtnl_ctrl_data *ctrl,
1335 struct nlmsghdr *n, void *arg)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001336{
1337 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1338
1339 printf("if%d:\n", ifa->ifa_index);
1340 print_addrinfo(NULL, n, stdout);
1341 return 0;
1342}
1343
1344static int ipaddr_showdump(void)
1345{
1346 if (ipadd_dump_check_magic())
1347 exit(-1);
1348
1349 exit(rtnl_from_file(stdin, &show_handler, NULL));
1350}
1351
Nicolas Dichtel0628cdd2015-05-20 16:19:58 +02001352static int restore_handler(const struct sockaddr_nl *nl,
1353 struct rtnl_ctrl_data *ctrl,
1354 struct nlmsghdr *n, void *arg)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001355{
1356 int ret;
1357
1358 n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
1359
1360 ll_init_map(&rth);
1361
Stephen Hemmingerc079e122015-05-27 12:26:14 -07001362 ret = rtnl_talk(&rth, n, n, sizeof(*n));
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001363 if ((ret < 0) && (errno == EEXIST))
1364 ret = 0;
1365
1366 return ret;
1367}
1368
1369static int ipaddr_restore(void)
1370{
1371 if (ipadd_dump_check_magic())
1372 exit(-1);
1373
1374 exit(rtnl_from_file(stdin, &restore_handler, NULL));
1375}
1376
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001377static void free_nlmsg_chain(struct nlmsg_chain *info)
1378{
1379 struct nlmsg_list *l, *n;
1380
1381 for (l = info->head; l; l = n) {
1382 n = l->next;
1383 free(l);
1384 }
1385}
1386
1387static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
1388{
1389 struct nlmsg_list *l, **lp;
1390
1391 lp = &linfo->head;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001392 while ((l = *lp) != NULL) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001393 int ok = 0;
Petr Písař7f747fd2012-10-03 16:42:41 +02001394 int missing_net_address = 1;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001395 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
1396 struct nlmsg_list *a;
1397
1398 for (a = ainfo->head; a; a = a->next) {
1399 struct nlmsghdr *n = &a->h;
1400 struct ifaddrmsg *ifa = NLMSG_DATA(n);
Jiri Pirko37c9b942014-01-06 10:17:09 +01001401 struct rtattr *tb[IFA_MAX + 1];
1402 unsigned int ifa_flags;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001403
Petr Písař7f747fd2012-10-03 16:42:41 +02001404 if (ifa->ifa_index != ifi->ifi_index)
1405 continue;
1406 missing_net_address = 0;
1407 if (filter.family && filter.family != ifa->ifa_family)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001408 continue;
1409 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
1410 continue;
Jiri Pirko37c9b942014-01-06 10:17:09 +01001411
1412 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
1413 ifa_flags = get_ifa_flags(ifa, tb[IFA_FLAGS]);
1414
1415 if ((filter.flags ^ ifa_flags) & filter.flagmask)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001416 continue;
1417 if (filter.pfx.family || filter.label) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001418 if (!tb[IFA_LOCAL])
1419 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1420
1421 if (filter.pfx.family && tb[IFA_LOCAL]) {
Phil Sutterd17b1362016-07-18 16:48:42 +02001422 inet_prefix dst = {
1423 .family = ifa->ifa_family
1424 };
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001425
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001426 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
1427 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
1428 continue;
1429 }
1430 if (filter.label) {
1431 SPRINT_BUF(b1);
1432 const char *label;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001433
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001434 if (tb[IFA_LABEL])
1435 label = RTA_DATA(tb[IFA_LABEL]);
1436 else
1437 label = ll_idx_n2a(ifa->ifa_index, b1);
1438 if (fnmatch(filter.label, label, 0) != 0)
1439 continue;
1440 }
1441 }
1442
1443 ok = 1;
1444 break;
1445 }
Petr Písař7f747fd2012-10-03 16:42:41 +02001446 if (missing_net_address &&
1447 (filter.family == AF_UNSPEC || filter.family == AF_PACKET))
1448 ok = 1;
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001449 if (!ok) {
1450 *lp = l->next;
1451 free(l);
1452 } else
1453 lp = &l->next;
1454 }
1455}
1456
1457static int ipaddr_flush(void)
1458{
1459 int round = 0;
1460 char flushb[4096-512];
1461
1462 filter.flushb = flushb;
1463 filter.flushp = 0;
1464 filter.flushe = sizeof(flushb);
1465
1466 while ((max_flush_loops == 0) || (round < max_flush_loops)) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001467 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1468 perror("Cannot send dump request");
1469 exit(1);
1470 }
1471 filter.flushed = 0;
Phil Sutterd25ec032015-11-24 15:31:02 +01001472 if (rtnl_dump_filter_nc(&rth, print_addrinfo,
1473 stdout, NLM_F_DUMP_INTR) < 0) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001474 fprintf(stderr, "Flush terminated\n");
1475 exit(1);
1476 }
1477 if (filter.flushed == 0) {
1478 flush_done:
1479 if (show_stats) {
1480 if (round == 0)
1481 printf("Nothing to flush.\n");
1482 else
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001483 printf("*** Flush is complete after %d round%s ***\n", round, round > 1?"s":"");
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001484 }
1485 fflush(stdout);
1486 return 0;
1487 }
1488 round++;
1489 if (flush_update() < 0)
1490 return 1;
1491
1492 if (show_stats) {
1493 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
1494 fflush(stdout);
1495 }
1496
1497 /* If we are flushing, and specifying primary, then we
1498 * want to flush only a single round. Otherwise, we'll
1499 * start flushing secondaries that were promoted to
1500 * primaries.
1501 */
1502 if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY))
1503 goto flush_done;
1504 }
1505 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops);
1506 fflush(stderr);
1507 return 1;
1508}
1509
David Ahernb0a4ce62016-05-11 06:51:58 -07001510static int iplink_filter_req(struct nlmsghdr *nlh, int reqlen)
1511{
1512 int err;
1513
1514 err = addattr32(nlh, reqlen, IFLA_EXT_MASK, RTEXT_FILTER_VF);
1515 if (err)
1516 return err;
1517
1518 if (filter.master) {
1519 err = addattr32(nlh, reqlen, IFLA_MASTER, filter.master);
1520 if (err)
1521 return err;
1522 }
1523
1524 if (filter.kind) {
1525 struct rtattr *linkinfo;
1526
1527 linkinfo = addattr_nest(nlh, reqlen, IFLA_LINKINFO);
1528
1529 err = addattr_l(nlh, reqlen, IFLA_INFO_KIND, filter.kind,
1530 strlen(filter.kind));
1531 if (err)
1532 return err;
1533
1534 addattr_nest_end(nlh, linkinfo);
1535 }
1536
1537 return 0;
1538}
1539
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001540static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001541{
Eric Dumazet62e2e542012-06-09 13:55:55 +02001542 struct nlmsg_chain linfo = { NULL, NULL};
1543 struct nlmsg_chain ainfo = { NULL, NULL};
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001544 struct nlmsg_list *l;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001545 char *filter_dev = NULL;
1546 int no_link = 0;
1547
vadimk093b7642014-10-20 12:25:17 +03001548 ipaddr_reset_filter(oneline, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001549 filter.showqueue = 1;
Phil Sutter906dfe42015-11-24 15:31:03 +01001550 filter.family = preferred_family;
Stefan Tomanekc4fdf752013-08-03 14:20:53 +02001551 filter.group = -1;
Vlad Dogaruf960c922011-02-02 20:23:40 +02001552
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001553 if (action == IPADD_FLUSH) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001554 if (argc <= 0) {
1555 fprintf(stderr, "Flush requires arguments.\n");
Vlad Dogaruf960c922011-02-02 20:23:40 +02001556
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001557 return -1;
1558 }
1559 if (filter.family == AF_PACKET) {
1560 fprintf(stderr, "Cannot flush link addresses.\n");
1561 return -1;
1562 }
1563 }
1564
1565 while (argc > 0) {
1566 if (strcmp(*argv, "to") == 0) {
1567 NEXT_ARG();
1568 get_prefix(&filter.pfx, *argv, filter.family);
1569 if (filter.family == AF_UNSPEC)
1570 filter.family = filter.pfx.family;
1571 } else if (strcmp(*argv, "scope") == 0) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001572 unsigned int scope = 0;
1573
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001574 NEXT_ARG();
1575 filter.scopemask = -1;
1576 if (rtnl_rtscope_a2n(&scope, *argv)) {
1577 if (strcmp(*argv, "all") != 0)
1578 invarg("invalid \"scope\"\n", *argv);
1579 scope = RT_SCOPE_NOWHERE;
1580 filter.scopemask = 0;
1581 }
1582 filter.scope = scope;
1583 } else if (strcmp(*argv, "up") == 0) {
1584 filter.up = 1;
1585 } else if (strcmp(*argv, "dynamic") == 0) {
1586 filter.flags &= ~IFA_F_PERMANENT;
1587 filter.flagmask |= IFA_F_PERMANENT;
1588 } else if (strcmp(*argv, "permanent") == 0) {
1589 filter.flags |= IFA_F_PERMANENT;
1590 filter.flagmask |= IFA_F_PERMANENT;
Brian Haleya1b9ffc2009-09-14 17:01:43 -04001591 } else if (strcmp(*argv, "secondary") == 0 ||
1592 strcmp(*argv, "temporary") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001593 filter.flags |= IFA_F_SECONDARY;
1594 filter.flagmask |= IFA_F_SECONDARY;
1595 } else if (strcmp(*argv, "primary") == 0) {
1596 filter.flags &= ~IFA_F_SECONDARY;
1597 filter.flagmask |= IFA_F_SECONDARY;
1598 } else if (strcmp(*argv, "tentative") == 0) {
1599 filter.flags |= IFA_F_TENTATIVE;
1600 filter.flagmask |= IFA_F_TENTATIVE;
Heiner Kallweitb5f39b22014-12-22 20:18:43 +01001601 } else if (strcmp(*argv, "-tentative") == 0) {
1602 filter.flags &= ~IFA_F_TENTATIVE;
1603 filter.flagmask |= IFA_F_TENTATIVE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001604 } else if (strcmp(*argv, "deprecated") == 0) {
1605 filter.flags |= IFA_F_DEPRECATED;
1606 filter.flagmask |= IFA_F_DEPRECATED;
Heiner Kallweitb5f39b22014-12-22 20:18:43 +01001607 } else if (strcmp(*argv, "-deprecated") == 0) {
1608 filter.flags &= ~IFA_F_DEPRECATED;
1609 filter.flagmask |= IFA_F_DEPRECATED;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001610 } else if (strcmp(*argv, "home") == 0) {
1611 filter.flags |= IFA_F_HOMEADDRESS;
1612 filter.flagmask |= IFA_F_HOMEADDRESS;
1613 } else if (strcmp(*argv, "nodad") == 0) {
1614 filter.flags |= IFA_F_NODAD;
1615 filter.flagmask |= IFA_F_NODAD;
Jiri Pirko5b7e21c2014-01-06 10:17:10 +01001616 } else if (strcmp(*argv, "mngtmpaddr") == 0) {
1617 filter.flags |= IFA_F_MANAGETEMPADDR;
1618 filter.flagmask |= IFA_F_MANAGETEMPADDR;
Thomas Haller58c69b22014-01-07 21:46:28 +01001619 } else if (strcmp(*argv, "noprefixroute") == 0) {
1620 filter.flags |= IFA_F_NOPREFIXROUTE;
1621 filter.flagmask |= IFA_F_NOPREFIXROUTE;
Madhu Challae31867a2015-03-04 10:30:10 -08001622 } else if (strcmp(*argv, "autojoin") == 0) {
1623 filter.flags |= IFA_F_MCAUTOJOIN;
1624 filter.flagmask |= IFA_F_MCAUTOJOIN;
Brian Haleya1f27792009-12-03 10:39:36 +00001625 } else if (strcmp(*argv, "dadfailed") == 0) {
1626 filter.flags |= IFA_F_DADFAILED;
1627 filter.flagmask |= IFA_F_DADFAILED;
Heiner Kallweitb5f39b22014-12-22 20:18:43 +01001628 } else if (strcmp(*argv, "-dadfailed") == 0) {
1629 filter.flags &= ~IFA_F_DADFAILED;
1630 filter.flagmask |= IFA_F_DADFAILED;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001631 } else if (strcmp(*argv, "label") == 0) {
1632 NEXT_ARG();
1633 filter.label = *argv;
Vlad Dogaruf960c922011-02-02 20:23:40 +02001634 } else if (strcmp(*argv, "group") == 0) {
1635 NEXT_ARG();
1636 if (rtnl_group_a2n(&filter.group, *argv))
1637 invarg("Invalid \"group\" value\n", *argv);
Vadim Kochan1b944142014-11-29 11:27:41 -08001638 } else if (strcmp(*argv, "master") == 0) {
1639 int ifindex;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001640
Vadim Kochan1b944142014-11-29 11:27:41 -08001641 NEXT_ARG();
1642 ifindex = ll_name_to_index(*argv);
1643 if (!ifindex)
1644 invarg("Device does not exist\n", *argv);
1645 filter.master = ifindex;
David Ahern104444c2016-06-29 11:26:58 -07001646 } else if (strcmp(*argv, "vrf") == 0) {
1647 int ifindex;
1648
1649 NEXT_ARG();
1650 ifindex = ll_name_to_index(*argv);
1651 if (!ifindex)
1652 invarg("Not a valid VRF name\n", *argv);
1653 if (!name_is_vrf(*argv))
1654 invarg("Not a valid VRF name\n", *argv);
1655 filter.master = ifindex;
Phil Sutter24604eb2016-06-09 19:20:36 +02001656 } else if (strcmp(*argv, "type") == 0) {
Phil Suttere0513802016-06-28 15:07:16 +02001657 int soff;
1658
Vadim Kochan712249d2014-12-10 10:11:29 +02001659 NEXT_ARG();
Phil Suttere0513802016-06-28 15:07:16 +02001660 soff = strlen(*argv) - strlen("_slave");
1661 if (!strcmp(*argv + soff, "_slave")) {
1662 (*argv)[soff] = '\0';
1663 filter.slave_kind = *argv;
1664 } else {
1665 filter.kind = *argv;
1666 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001667 } else {
Stephen Hemminger468fa022016-11-14 09:28:09 +01001668 if (strcmp(*argv, "dev") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001669 NEXT_ARG();
Stephen Hemminger468fa022016-11-14 09:28:09 +01001670 else if (matches(*argv, "help") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001671 usage();
1672 if (filter_dev)
1673 duparg2("dev", *argv);
1674 filter_dev = *argv;
1675 }
1676 argv++; argc--;
1677 }
1678
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001679 if (filter_dev) {
1680 filter.ifindex = ll_name_to_index(filter_dev);
1681 if (filter.ifindex <= 0) {
1682 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
1683 return -1;
1684 }
1685 }
1686
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001687 if (action == IPADD_FLUSH)
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001688 return ipaddr_flush();
1689
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001690 if (action == IPADD_SAVE) {
1691 if (ipadd_save_prep())
1692 exit(1);
1693
1694 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETADDR) < 0) {
1695 perror("Cannot send dump request");
1696 exit(1);
1697 }
1698
1699 if (rtnl_dump_filter(&rth, save_nlmsg, stdout) < 0) {
1700 fprintf(stderr, "Save terminated\n");
1701 exit(1);
1702 }
1703
1704 exit(0);
1705 }
1706
Roopa Prabhu50b99502014-07-16 07:13:25 -07001707 /*
1708 * If only filter_dev present and none of the other
1709 * link filters are present, use RTM_GETLINK to get
1710 * the link device
1711 */
1712 if (filter_dev && filter.group == -1 && do_link == 1) {
1713 if (iplink_get(0, filter_dev, RTEXT_FILTER_VF) < 0) {
1714 perror("Cannot send link get request");
1715 exit(1);
1716 }
1717 exit(0);
1718 }
1719
David Ahernb0a4ce62016-05-11 06:51:58 -07001720 if (rtnl_wilddump_req_filter_fn(&rth, preferred_family, RTM_GETLINK,
1721 iplink_filter_req) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001722 perror("Cannot send dump request");
1723 exit(1);
1724 }
1725
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001726 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001727 fprintf(stderr, "Dump terminated\n");
1728 exit(1);
1729 }
1730
Mike Frysingeraf9d4062012-08-13 08:09:52 -07001731 if (filter.family != AF_PACKET) {
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001732 if (filter.oneline)
1733 no_link = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001734
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001735 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1736 perror("Cannot send dump request");
1737 exit(1);
1738 }
1739
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001740 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001741 fprintf(stderr, "Dump terminated\n");
1742 exit(1);
1743 }
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001744
1745 ipaddr_filter(&linfo, &ainfo);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001746 }
1747
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001748 for (l = linfo.head; l; l = l->next) {
Jiri Benc5d5cf1b2014-10-03 15:25:02 +02001749 int res = 0;
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001750 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
Jiri Benc5d5cf1b2014-10-03 15:25:02 +02001751
Andy Gospodarek5d295bb2015-08-28 13:45:41 -04001752 if (brief) {
1753 if (print_linkinfo_brief(NULL, &l->h, stdout) == 0)
1754 if (filter.family != AF_PACKET)
1755 print_selected_addrinfo(ifi,
1756 ainfo.head,
1757 stdout);
1758 } else if (no_link ||
1759 (res = print_linkinfo(NULL, &l->h, stdout)) >= 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001760 if (filter.family != AF_PACKET)
Andreas Henriksson5e5055b2015-01-23 13:10:33 +01001761 print_selected_addrinfo(ifi,
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001762 ainfo.head, stdout);
Jiri Benc5d5cf1b2014-10-03 15:25:02 +02001763 if (res > 0 && !do_link && show_stats)
1764 print_link_stats(stdout, &l->h);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001765 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001766 }
Stephen Hemminger8d07e5f2012-07-13 13:36:07 -07001767 fflush(stdout);
1768
1769 free_nlmsg_chain(&ainfo);
1770 free_nlmsg_chain(&linfo);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001771
shemminger351efcd2005-09-01 19:21:50 +00001772 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001773}
1774
Sucheta Chakrabortyf89a2a02014-05-22 09:59:37 -04001775static void
1776ipaddr_loop_each_vf(struct rtattr *tb[], int vfnum, int *min, int *max)
1777{
1778 struct rtattr *vflist = tb[IFLA_VFINFO_LIST];
1779 struct rtattr *i, *vf[IFLA_VF_MAX+1];
1780 struct ifla_vf_rate *vf_rate;
1781 int rem;
1782
1783 rem = RTA_PAYLOAD(vflist);
1784
1785 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
1786 parse_rtattr_nested(vf, IFLA_VF_MAX, i);
1787 vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
1788 if (vf_rate->vf == vfnum) {
1789 *min = vf_rate->min_tx_rate;
1790 *max = vf_rate->max_tx_rate;
1791 return;
1792 }
1793 }
1794 fprintf(stderr, "Cannot find VF %d\n", vfnum);
1795 exit(1);
1796}
1797
1798void ipaddr_get_vf_rate(int vfnum, int *min, int *max, int idx)
1799{
1800 struct nlmsg_chain linfo = { NULL, NULL};
1801 struct rtattr *tb[IFLA_MAX+1];
1802 struct ifinfomsg *ifi;
1803 struct nlmsg_list *l;
1804 struct nlmsghdr *n;
1805 int len;
1806
1807 if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) {
1808 perror("Cannot send dump request");
1809 exit(1);
1810 }
1811 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
1812 fprintf(stderr, "Dump terminated\n");
1813 exit(1);
1814 }
1815 for (l = linfo.head; l; l = l->next) {
1816 n = &l->h;
1817 ifi = NLMSG_DATA(n);
1818
1819 len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
Stephen Hemminger1199c4f2014-07-14 12:06:52 -07001820 if (len < 0 || (idx && idx != ifi->ifi_index))
Sucheta Chakrabortyf89a2a02014-05-22 09:59:37 -04001821 continue;
1822
1823 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1824
1825 if ((tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF])) {
1826 ipaddr_loop_each_vf(tb, vfnum, min, max);
1827 return;
1828 }
1829 }
1830}
1831
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001832int ipaddr_list_link(int argc, char **argv)
1833{
1834 preferred_family = AF_PACKET;
1835 do_link = 1;
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04001836 return ipaddr_list_flush_or_save(argc, argv, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001837}
1838
vadimk093b7642014-10-20 12:25:17 +03001839void ipaddr_reset_filter(int oneline, int ifindex)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001840{
1841 memset(&filter, 0, sizeof(filter));
1842 filter.oneline = oneline;
vadimk093b7642014-10-20 12:25:17 +03001843 filter.ifindex = ifindex;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001844}
1845
Stephen Hemminger3d866ba2008-03-14 15:30:03 -07001846static int default_scope(inet_prefix *lcl)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001847{
1848 if (lcl->family == AF_INET) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001849 if (lcl->bytelen >= 1 && *(__u8 *)&lcl->data == 127)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001850 return RT_SCOPE_HOST;
1851 }
1852 return 0;
1853}
1854
Madhu Challae31867a2015-03-04 10:30:10 -08001855static bool ipaddr_is_multicast(inet_prefix *a)
1856{
1857 if (a->family == AF_INET)
1858 return IN_MULTICAST(ntohl(a->data[0]));
1859 else if (a->family == AF_INET6)
1860 return IN6_IS_ADDR_MULTICAST(a->data);
1861 else
1862 return false;
1863}
1864
Stephen Hemminger3d866ba2008-03-14 15:30:03 -07001865static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001866{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001867 struct {
Stephen Hemminger48068672014-02-17 10:56:31 -08001868 struct nlmsghdr n;
1869 struct ifaddrmsg ifa;
Stephen Hemminger4b726cb2014-05-09 12:36:46 -07001870 char buf[256];
Phil Sutterd17b1362016-07-18 16:48:42 +02001871 } req = {
1872 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
1873 .n.nlmsg_flags = NLM_F_REQUEST | flags,
1874 .n.nlmsg_type = cmd,
1875 .ifa.ifa_family = preferred_family,
1876 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001877 char *d = NULL;
1878 char *l = NULL;
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001879 char *lcl_arg = NULL;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001880 char *valid_lftp = NULL;
1881 char *preferred_lftp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001882 inet_prefix lcl;
1883 inet_prefix peer;
1884 int local_len = 0;
1885 int peer_len = 0;
1886 int brd_len = 0;
1887 int any_len = 0;
1888 int scoped = 0;
Masahide NAKAMURA141bb602006-11-24 12:27:01 +09001889 __u32 preferred_lft = INFINITY_LIFE_TIME;
1890 __u32 valid_lft = INFINITY_LIFE_TIME;
Jiri Pirko37c9b942014-01-06 10:17:09 +01001891 unsigned int ifa_flags = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001892
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001893 while (argc > 0) {
1894 if (strcmp(*argv, "peer") == 0 ||
1895 strcmp(*argv, "remote") == 0) {
1896 NEXT_ARG();
1897
1898 if (peer_len)
1899 duparg("peer", *argv);
1900 get_prefix(&peer, *argv, req.ifa.ifa_family);
1901 peer_len = peer.bytelen;
1902 if (req.ifa.ifa_family == AF_UNSPEC)
1903 req.ifa.ifa_family = peer.family;
1904 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
1905 req.ifa.ifa_prefixlen = peer.bitlen;
1906 } else if (matches(*argv, "broadcast") == 0 ||
1907 strcmp(*argv, "brd") == 0) {
1908 inet_prefix addr;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001909
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001910 NEXT_ARG();
1911 if (brd_len)
1912 duparg("broadcast", *argv);
1913 if (strcmp(*argv, "+") == 0)
1914 brd_len = -1;
1915 else if (strcmp(*argv, "-") == 0)
1916 brd_len = -2;
1917 else {
1918 get_addr(&addr, *argv, req.ifa.ifa_family);
1919 if (req.ifa.ifa_family == AF_UNSPEC)
1920 req.ifa.ifa_family = addr.family;
1921 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
1922 brd_len = addr.bytelen;
1923 }
1924 } else if (strcmp(*argv, "anycast") == 0) {
1925 inet_prefix addr;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001926
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001927 NEXT_ARG();
1928 if (any_len)
1929 duparg("anycast", *argv);
1930 get_addr(&addr, *argv, req.ifa.ifa_family);
1931 if (req.ifa.ifa_family == AF_UNSPEC)
1932 req.ifa.ifa_family = addr.family;
1933 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
1934 any_len = addr.bytelen;
1935 } else if (strcmp(*argv, "scope") == 0) {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07001936 unsigned int scope = 0;
1937
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001938 NEXT_ARG();
1939 if (rtnl_rtscope_a2n(&scope, *argv))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +00001940 invarg("invalid scope value.", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001941 req.ifa.ifa_scope = scope;
1942 scoped = 1;
1943 } else if (strcmp(*argv, "dev") == 0) {
1944 NEXT_ARG();
1945 d = *argv;
1946 } else if (strcmp(*argv, "label") == 0) {
1947 NEXT_ARG();
1948 l = *argv;
1949 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001950 } else if (matches(*argv, "valid_lft") == 0) {
1951 if (valid_lftp)
1952 duparg("valid_lft", *argv);
1953 NEXT_ARG();
1954 valid_lftp = *argv;
1955 if (set_lifetime(&valid_lft, *argv))
1956 invarg("valid_lft value", *argv);
1957 } else if (matches(*argv, "preferred_lft") == 0) {
1958 if (preferred_lftp)
1959 duparg("preferred_lft", *argv);
1960 NEXT_ARG();
1961 preferred_lftp = *argv;
1962 if (set_lifetime(&preferred_lft, *argv))
1963 invarg("preferred_lft value", *argv);
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001964 } else if (strcmp(*argv, "home") == 0) {
Jiri Pirko37c9b942014-01-06 10:17:09 +01001965 ifa_flags |= IFA_F_HOMEADDRESS;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001966 } else if (strcmp(*argv, "nodad") == 0) {
Jiri Pirko37c9b942014-01-06 10:17:09 +01001967 ifa_flags |= IFA_F_NODAD;
Jiri Pirko5b7e21c2014-01-06 10:17:10 +01001968 } else if (strcmp(*argv, "mngtmpaddr") == 0) {
1969 ifa_flags |= IFA_F_MANAGETEMPADDR;
Thomas Haller58c69b22014-01-07 21:46:28 +01001970 } else if (strcmp(*argv, "noprefixroute") == 0) {
1971 ifa_flags |= IFA_F_NOPREFIXROUTE;
Madhu Challae31867a2015-03-04 10:30:10 -08001972 } else if (strcmp(*argv, "autojoin") == 0) {
1973 ifa_flags |= IFA_F_MCAUTOJOIN;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001974 } else {
Stephen Hemminger468fa022016-11-14 09:28:09 +01001975 if (strcmp(*argv, "local") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001976 NEXT_ARG();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001977 if (matches(*argv, "help") == 0)
1978 usage();
1979 if (local_len)
1980 duparg2("local", *argv);
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001981 lcl_arg = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001982 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1983 if (req.ifa.ifa_family == AF_UNSPEC)
1984 req.ifa.ifa_family = lcl.family;
1985 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1986 local_len = lcl.bytelen;
1987 }
1988 argc--; argv++;
1989 }
WANG Cong8b21f882014-04-04 10:07:20 -07001990 if (ifa_flags <= 0xff)
1991 req.ifa.ifa_flags = ifa_flags;
1992 else
1993 addattr32(&req.n, sizeof(req), IFA_FLAGS, ifa_flags);
Jiri Pirko37c9b942014-01-06 10:17:09 +01001994
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001995 if (d == NULL) {
1996 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1997 return -1;
1998 }
1999 if (l && matches(d, l) != 0) {
2000 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
Michele Petrazzo - Unipex1db61e02010-03-06 08:56:53 +00002001 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002002 }
2003
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00002004 if (peer_len == 0 && local_len) {
2005 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
2006 fprintf(stderr,
Stephen Hemminger468fa022016-11-14 09:28:09 +01002007 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n"
2008 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n"
2009 " This special behaviour is likely to disappear in further releases,\n"
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00002010 " fix your scripts!\n", lcl_arg, local_len*8);
2011 } else {
2012 peer = lcl;
2013 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
2014 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002015 }
2016 if (req.ifa.ifa_prefixlen == 0)
2017 req.ifa.ifa_prefixlen = lcl.bitlen;
2018
2019 if (brd_len < 0 && cmd != RTM_DELADDR) {
2020 inet_prefix brd;
2021 int i;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -07002022
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002023 if (req.ifa.ifa_family != AF_INET) {
2024 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
2025 return -1;
2026 }
2027 brd = peer;
2028 if (brd.bitlen <= 30) {
Stephen Hemminger4b726cb2014-05-09 12:36:46 -07002029 for (i = 31; i >= brd.bitlen; i--) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002030 if (brd_len == -1)
2031 brd.data[0] |= htonl(1<<(31-i));
2032 else
2033 brd.data[0] &= ~htonl(1<<(31-i));
2034 }
2035 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
2036 brd_len = brd.bytelen;
2037 }
2038 }
2039 if (!scoped && cmd != RTM_DELADDR)
2040 req.ifa.ifa_scope = default_scope(&lcl);
2041
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002042 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
2043 fprintf(stderr, "Cannot find device \"%s\"\n", d);
2044 return -1;
2045 }
2046
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09002047 if (valid_lftp || preferred_lftp) {
Phil Sutterd17b1362016-07-18 16:48:42 +02002048 struct ifa_cacheinfo cinfo = {};
2049
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09002050 if (!valid_lft) {
2051 fprintf(stderr, "valid_lft is zero\n");
2052 return -1;
2053 }
2054 if (valid_lft < preferred_lft) {
2055 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
2056 return -1;
2057 }
2058
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09002059 cinfo.ifa_prefered = preferred_lft;
2060 cinfo.ifa_valid = valid_lft;
2061 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
2062 sizeof(cinfo));
2063 }
2064
Madhu Challae31867a2015-03-04 10:30:10 -08002065 if ((ifa_flags & IFA_F_MCAUTOJOIN) && !ipaddr_is_multicast(&lcl)) {
2066 fprintf(stderr, "autojoin needs multicast address\n");
2067 return -1;
2068 }
2069
Stephen Hemmingerc079e122015-05-27 12:26:14 -07002070 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
Michele Petrazzo - Unipex1db61e02010-03-06 08:56:53 +00002071 return -2;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002072
shemminger351efcd2005-09-01 19:21:50 +00002073 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002074}
2075
2076int do_ipaddr(int argc, char **argv)
2077{
2078 if (argc < 1)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04002079 return ipaddr_list_flush_or_save(0, NULL, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002080 if (matches(*argv, "add") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09002081 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
2082 if (matches(*argv, "change") == 0 ||
2083 strcmp(*argv, "chg") == 0)
2084 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
2085 if (matches(*argv, "replace") == 0)
2086 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002087 if (matches(*argv, "delete") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09002088 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002089 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
2090 || matches(*argv, "lst") == 0)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04002091 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_LIST);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002092 if (matches(*argv, "flush") == 0)
Pavel Emelyanov81824ac2012-09-11 19:47:00 +04002093 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_FLUSH);
2094 if (matches(*argv, "save") == 0)
2095 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_SAVE);
2096 if (matches(*argv, "showdump") == 0)
2097 return ipaddr_showdump();
2098 if (matches(*argv, "restore") == 0)
2099 return ipaddr_restore();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002100 if (matches(*argv, "help") == 0)
2101 usage();
Pavel Šimerda4e972d52015-08-12 22:04:08 +02002102 fprintf(stderr, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00002103 exit(-1);
2104}