osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ipaddress.c "ip address". |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
| 16 | #include <syslog.h> |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 17 | #include <inttypes.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <sys/ioctl.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/ioctl.h> |
Strake | 5bd9dd4 | 2012-12-23 08:46:04 -0500 | [diff] [blame] | 22 | #include <errno.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 23 | #include <netinet/in.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include <string.h> |
| 26 | #include <fnmatch.h> |
| 27 | |
osdl.org!shemminger | e5779fb | 2004-06-09 22:56:28 +0000 | [diff] [blame] | 28 | #include <linux/netdevice.h> |
| 29 | #include <linux/if_arp.h> |
| 30 | #include <linux/sockios.h> |
Nicolas Dichtel | ccdcbf3 | 2015-02-17 17:30:38 +0100 | [diff] [blame] | 31 | #include <linux/net_namespace.h> |
osdl.org!shemminger | e5779fb | 2004-06-09 22:56:28 +0000 | [diff] [blame] | 32 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 33 | #include "rt_names.h" |
| 34 | #include "utils.h" |
| 35 | #include "ll_map.h" |
| 36 | #include "ip_common.h" |
| 37 | |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 38 | enum { |
| 39 | IPADD_LIST, |
| 40 | IPADD_FLUSH, |
| 41 | IPADD_SAVE, |
| 42 | }; |
Daniel Silverstone | 7b3d366 | 2007-10-19 13:32:24 +0200 | [diff] [blame] | 43 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 44 | static struct |
| 45 | { |
| 46 | int ifindex; |
| 47 | int family; |
| 48 | int oneline; |
| 49 | int showqueue; |
| 50 | inet_prefix pfx; |
| 51 | int scope, scopemask; |
| 52 | int flags, flagmask; |
| 53 | int up; |
| 54 | char *label; |
| 55 | int flushed; |
| 56 | char *flushb; |
| 57 | int flushp; |
| 58 | int flushe; |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 59 | int group; |
Vadim Kochan | 1b94414 | 2014-11-29 11:27:41 -0800 | [diff] [blame] | 60 | int master; |
Vadim Kochan | 712249d | 2014-12-10 10:11:29 +0200 | [diff] [blame] | 61 | char *kind; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 62 | } filter; |
| 63 | |
| 64 | static int do_link; |
| 65 | |
| 66 | static void usage(void) __attribute__((noreturn)); |
| 67 | |
| 68 | static void usage(void) |
| 69 | { |
| 70 | if (do_link) { |
| 71 | iplink_usage(); |
| 72 | } |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 73 | fprintf(stderr, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n"); |
Brian Haley | a1f2779 | 2009-12-03 10:39:36 +0000 | [diff] [blame] | 74 | fprintf(stderr, " [ CONFFLAG-LIST ]\n"); |
Heiner Kallweit | a424c39 | 2014-04-10 21:16:45 +0200 | [diff] [blame] | 75 | fprintf(stderr, " ip addr del IFADDR dev STRING [mngtmpaddr]\n"); |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 76 | fprintf(stderr, " ip addr {show|save|flush} [ dev STRING ] [ scope SCOPE-ID ]\n"); |
Petr Ĺ abata | 4405123 | 2013-03-14 15:10:44 +0100 | [diff] [blame] | 77 | fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ] [up]\n"); |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 78 | fprintf(stderr, " ip addr {showdump|restore}\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 79 | fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n"); |
| 80 | fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n"); |
| 81 | fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n"); |
| 82 | fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n"); |
| 83 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); |
| 84 | fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n"); |
Heiner Kallweit | b5f39b2 | 2014-12-22 20:18:43 +0100 | [diff] [blame] | 85 | fprintf(stderr, " [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n"); |
Brian Haley | a1f2779 | 2009-12-03 10:39:36 +0000 | [diff] [blame] | 86 | fprintf(stderr, " CONFFLAG-LIST ]\n"); |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 87 | fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n"); |
Madhu Challa | e31867a | 2015-03-04 10:30:10 -0800 | [diff] [blame^] | 88 | fprintf(stderr, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n"); |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 89 | fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n"); |
| 90 | fprintf(stderr, "LFT := forever | SECONDS\n"); |
| 91 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 92 | exit(-1); |
| 93 | } |
| 94 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 95 | static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 96 | { |
| 97 | fprintf(fp, "<"); |
net[shemminger]!shemminger | 73b49e9 | 2005-03-14 18:47:38 +0000 | [diff] [blame] | 98 | if (flags & IFF_UP && !(flags & IFF_RUNNING)) |
| 99 | fprintf(fp, "NO-CARRIER%s", flags ? "," : ""); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 100 | flags &= ~IFF_RUNNING; |
| 101 | #define _PF(f) if (flags&IFF_##f) { \ |
Stephen Hemminger | 1124ffb | 2013-03-14 13:47:49 -0700 | [diff] [blame] | 102 | flags &= ~IFF_##f ; \ |
| 103 | fprintf(fp, #f "%s", flags ? "," : ""); } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 104 | _PF(LOOPBACK); |
| 105 | _PF(BROADCAST); |
| 106 | _PF(POINTOPOINT); |
| 107 | _PF(MULTICAST); |
| 108 | _PF(NOARP); |
| 109 | _PF(ALLMULTI); |
| 110 | _PF(PROMISC); |
| 111 | _PF(MASTER); |
| 112 | _PF(SLAVE); |
| 113 | _PF(DEBUG); |
| 114 | _PF(DYNAMIC); |
| 115 | _PF(AUTOMEDIA); |
| 116 | _PF(PORTSEL); |
| 117 | _PF(NOTRAILERS); |
| 118 | _PF(UP); |
Thomas Graf | dcb283c | 2007-06-19 16:40:40 -0700 | [diff] [blame] | 119 | _PF(LOWER_UP); |
| 120 | _PF(DORMANT); |
Oliver Hartkopp | 98f9a1d | 2009-03-27 11:21:29 -0700 | [diff] [blame] | 121 | _PF(ECHO); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 122 | #undef _PF |
Stephen Hemminger | 1124ffb | 2013-03-14 13:47:49 -0700 | [diff] [blame] | 123 | if (flags) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 124 | fprintf(fp, "%x", flags); |
| 125 | if (mdown) |
| 126 | fprintf(fp, ",M-DOWN"); |
| 127 | fprintf(fp, "> "); |
| 128 | } |
| 129 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 130 | static const char *oper_states[] = { |
Stephen Hemminger | 0612519 | 2014-02-17 10:55:31 -0800 | [diff] [blame] | 131 | "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN", |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 132 | "TESTING", "DORMANT", "UP" |
| 133 | }; |
| 134 | |
| 135 | static void print_operstate(FILE *f, __u8 state) |
| 136 | { |
| 137 | if (state >= sizeof(oper_states)/sizeof(oper_states[0])) |
| 138 | fprintf(f, "state %#x ", state); |
| 139 | else |
| 140 | fprintf(f, "state %s ", oper_states[state]); |
| 141 | } |
| 142 | |
Stephen Hemminger | 4f2fdd4 | 2012-04-05 15:08:57 -0700 | [diff] [blame] | 143 | int get_operstate(const char *name) |
| 144 | { |
| 145 | int i; |
| 146 | |
| 147 | for (i = 0; i < sizeof(oper_states)/sizeof(oper_states[0]); i++) |
| 148 | if (strcasecmp(name, oper_states[i]) == 0) |
| 149 | return i; |
| 150 | return -1; |
| 151 | } |
| 152 | |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 153 | static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1]) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 154 | { |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 155 | int qlen; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 156 | |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 157 | if (tb[IFLA_TXQLEN]) |
| 158 | qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]); |
| 159 | else { |
| 160 | struct ifreq ifr; |
| 161 | int s = socket(AF_INET, SOCK_STREAM, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 162 | |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 163 | if (s < 0) |
| 164 | return; |
| 165 | |
| 166 | memset(&ifr, 0, sizeof(ifr)); |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 167 | strcpy(ifr.ifr_name, rta_getattr_str(tb[IFLA_IFNAME])); |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 168 | if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { |
Nicolas Dichtel | d360351 | 2013-01-29 08:46:42 -0800 | [diff] [blame] | 169 | fprintf(f, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno)); |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 170 | close(s); |
| 171 | return; |
| 172 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 173 | close(s); |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 174 | qlen = ifr.ifr_qlen; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 175 | } |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 176 | if (qlen) |
| 177 | fprintf(f, "qlen %d", qlen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Stephen Hemminger | 8249928 | 2012-03-19 17:24:43 -0700 | [diff] [blame] | 180 | static const char *link_modes[] = { |
| 181 | "DEFAULT", "DORMANT" |
| 182 | }; |
| 183 | |
| 184 | static void print_linkmode(FILE *f, struct rtattr *tb) |
| 185 | { |
| 186 | unsigned int mode = rta_getattr_u8(tb); |
| 187 | |
| 188 | if (mode >= sizeof(link_modes) / sizeof(link_modes[0])) |
| 189 | fprintf(f, "mode %d ", mode); |
| 190 | else |
| 191 | fprintf(f, "mode %s ", link_modes[mode]); |
| 192 | } |
| 193 | |
Vadim Kochan | 712249d | 2014-12-10 10:11:29 +0200 | [diff] [blame] | 194 | static char *parse_link_kind(struct rtattr *tb) |
| 195 | { |
| 196 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; |
| 197 | |
| 198 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); |
| 199 | |
| 200 | if (linkinfo[IFLA_INFO_KIND]) |
| 201 | return RTA_DATA(linkinfo[IFLA_INFO_KIND]); |
| 202 | |
| 203 | return ""; |
| 204 | } |
| 205 | |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 206 | static void print_linktype(FILE *fp, struct rtattr *tb) |
| 207 | { |
| 208 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; |
| 209 | struct link_util *lu; |
Jiri Pirko | fbea611 | 2014-01-23 17:52:53 +0100 | [diff] [blame] | 210 | struct link_util *slave_lu; |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 211 | char *kind; |
Jiri Pirko | fbea611 | 2014-01-23 17:52:53 +0100 | [diff] [blame] | 212 | char *slave_kind; |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 213 | |
| 214 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); |
| 215 | |
Jiri Pirko | fbea611 | 2014-01-23 17:52:53 +0100 | [diff] [blame] | 216 | if (linkinfo[IFLA_INFO_KIND]) { |
| 217 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 218 | |
Jiri Pirko | fbea611 | 2014-01-23 17:52:53 +0100 | [diff] [blame] | 219 | fprintf(fp, "%s", _SL_); |
| 220 | fprintf(fp, " %s ", kind); |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 221 | |
Jiri Pirko | fbea611 | 2014-01-23 17:52:53 +0100 | [diff] [blame] | 222 | lu = get_link_kind(kind); |
| 223 | if (lu && lu->print_opt) { |
| 224 | struct rtattr *attr[lu->maxattr+1], **data = NULL; |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 225 | |
Jiri Pirko | fbea611 | 2014-01-23 17:52:53 +0100 | [diff] [blame] | 226 | if (linkinfo[IFLA_INFO_DATA]) { |
| 227 | parse_rtattr_nested(attr, lu->maxattr, |
| 228 | linkinfo[IFLA_INFO_DATA]); |
| 229 | data = attr; |
| 230 | } |
| 231 | lu->print_opt(lu, fp, data); |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 232 | |
Jiri Pirko | fbea611 | 2014-01-23 17:52:53 +0100 | [diff] [blame] | 233 | if (linkinfo[IFLA_INFO_XSTATS] && show_stats && |
| 234 | lu->print_xstats) |
| 235 | lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]); |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 236 | } |
Jiri Pirko | fbea611 | 2014-01-23 17:52:53 +0100 | [diff] [blame] | 237 | } |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 238 | |
Jiri Pirko | fbea611 | 2014-01-23 17:52:53 +0100 | [diff] [blame] | 239 | if (linkinfo[IFLA_INFO_SLAVE_KIND]) { |
| 240 | slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]); |
| 241 | |
| 242 | fprintf(fp, "%s", _SL_); |
| 243 | fprintf(fp, " %s_slave ", slave_kind); |
| 244 | |
| 245 | slave_lu = get_link_slave_kind(slave_kind); |
| 246 | if (slave_lu && slave_lu->print_opt) { |
| 247 | struct rtattr *attr[slave_lu->maxattr+1], **data = NULL; |
| 248 | |
| 249 | if (linkinfo[IFLA_INFO_SLAVE_DATA]) { |
| 250 | parse_rtattr_nested(attr, slave_lu->maxattr, |
| 251 | linkinfo[IFLA_INFO_SLAVE_DATA]); |
| 252 | data = attr; |
| 253 | } |
| 254 | slave_lu->print_opt(slave_lu, fp, data); |
| 255 | } |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Jiri Pirko | ee0067a | 2015-01-08 09:49:55 +0100 | [diff] [blame] | 259 | static void print_af_spec(FILE *fp, struct rtattr *af_spec_attr) |
| 260 | { |
| 261 | struct rtattr *inet6_attr; |
| 262 | struct rtattr *tb[IFLA_INET6_MAX + 1]; |
| 263 | |
| 264 | inet6_attr = parse_rtattr_one_nested(AF_INET6, af_spec_attr); |
| 265 | if (!inet6_attr) |
| 266 | return; |
| 267 | |
| 268 | parse_rtattr_nested(tb, IFLA_INET6_MAX, inet6_attr); |
| 269 | |
| 270 | if (tb[IFLA_INET6_ADDR_GEN_MODE]) { |
| 271 | switch (rta_getattr_u8(tb[IFLA_INET6_ADDR_GEN_MODE])) { |
| 272 | case IN6_ADDR_GEN_MODE_EUI64: |
| 273 | fprintf(fp, "addrgenmode eui64 "); |
| 274 | break; |
| 275 | case IN6_ADDR_GEN_MODE_NONE: |
| 276 | fprintf(fp, "addrgenmode none "); |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 282 | static void print_vfinfo(FILE *fp, struct rtattr *vfinfo) |
| 283 | { |
| 284 | struct ifla_vf_mac *vf_mac; |
| 285 | struct ifla_vf_vlan *vf_vlan; |
| 286 | struct ifla_vf_tx_rate *vf_tx_rate; |
Greg Rose | 7b8179c | 2011-10-13 20:31:32 +0000 | [diff] [blame] | 287 | struct ifla_vf_spoofchk *vf_spoofchk; |
Rony Efraim | 07fa9c1 | 2013-06-13 13:19:12 +0300 | [diff] [blame] | 288 | struct ifla_vf_link_state *vf_linkstate; |
Vadim Kochan | 8c29ae7 | 2015-01-09 21:24:31 +0200 | [diff] [blame] | 289 | struct rtattr *vf[IFLA_VF_MAX + 1] = {}; |
Greg Rose | 7b8179c | 2011-10-13 20:31:32 +0000 | [diff] [blame] | 290 | struct rtattr *tmp; |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 291 | SPRINT_BUF(b1); |
| 292 | |
| 293 | if (vfinfo->rta_type != IFLA_VF_INFO) { |
| 294 | fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo); |
| 299 | |
| 300 | vf_mac = RTA_DATA(vf[IFLA_VF_MAC]); |
| 301 | vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]); |
| 302 | vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]); |
| 303 | |
Greg Rose | 7b8179c | 2011-10-13 20:31:32 +0000 | [diff] [blame] | 304 | /* Check if the spoof checking vf info type is supported by |
| 305 | * this kernel. |
| 306 | */ |
| 307 | tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] + |
| 308 | vf[IFLA_VF_TX_RATE]->rta_len); |
| 309 | |
| 310 | if (tmp->rta_type != IFLA_VF_SPOOFCHK) |
| 311 | vf_spoofchk = NULL; |
| 312 | else |
| 313 | vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]); |
| 314 | |
Rony Efraim | 07fa9c1 | 2013-06-13 13:19:12 +0300 | [diff] [blame] | 315 | if (vf_spoofchk) { |
| 316 | /* Check if the link state vf info type is supported by |
| 317 | * this kernel. |
| 318 | */ |
| 319 | tmp = (struct rtattr *)((char *)vf[IFLA_VF_SPOOFCHK] + |
| 320 | vf[IFLA_VF_SPOOFCHK]->rta_len); |
| 321 | |
| 322 | if (tmp->rta_type != IFLA_VF_LINK_STATE) |
| 323 | vf_linkstate = NULL; |
| 324 | else |
| 325 | vf_linkstate = RTA_DATA(vf[IFLA_VF_LINK_STATE]); |
| 326 | } else |
| 327 | vf_linkstate = NULL; |
| 328 | |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 329 | fprintf(fp, "\n vf %d MAC %s", vf_mac->vf, |
| 330 | ll_addr_n2a((unsigned char *)&vf_mac->mac, |
| 331 | ETH_ALEN, 0, b1, sizeof(b1))); |
| 332 | if (vf_vlan->vlan) |
| 333 | fprintf(fp, ", vlan %d", vf_vlan->vlan); |
| 334 | if (vf_vlan->qos) |
| 335 | fprintf(fp, ", qos %d", vf_vlan->qos); |
| 336 | if (vf_tx_rate->rate) |
| 337 | fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate); |
Vadim Kochan | 8c29ae7 | 2015-01-09 21:24:31 +0200 | [diff] [blame] | 338 | |
| 339 | if (vf[IFLA_VF_RATE]) { |
| 340 | struct ifla_vf_rate *vf_rate = RTA_DATA(vf[IFLA_VF_RATE]); |
| 341 | |
| 342 | if (vf_rate->max_tx_rate) |
| 343 | fprintf(fp, ", max_tx_rate %dMbps", vf_rate->max_tx_rate); |
| 344 | if (vf_rate->min_tx_rate) |
| 345 | fprintf(fp, ", min_tx_rate %dMbps", vf_rate->min_tx_rate); |
| 346 | } |
| 347 | |
Greg Rose | 7b8179c | 2011-10-13 20:31:32 +0000 | [diff] [blame] | 348 | if (vf_spoofchk && vf_spoofchk->setting != -1) { |
| 349 | if (vf_spoofchk->setting) |
| 350 | fprintf(fp, ", spoof checking on"); |
| 351 | else |
| 352 | fprintf(fp, ", spoof checking off"); |
| 353 | } |
Rony Efraim | 07fa9c1 | 2013-06-13 13:19:12 +0300 | [diff] [blame] | 354 | if (vf_linkstate) { |
| 355 | if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_AUTO) |
| 356 | fprintf(fp, ", link-state auto"); |
| 357 | else if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_ENABLE) |
| 358 | fprintf(fp, ", link-state enable"); |
| 359 | else |
| 360 | fprintf(fp, ", link-state disable"); |
| 361 | } |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 364 | static void print_num(FILE *fp, unsigned width, uint64_t count) |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 365 | { |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 366 | const char *prefix = "kMGTPE"; |
| 367 | const unsigned int base = use_iec ? 1024 : 1000; |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 368 | uint64_t powi = 1; |
Christian Hesse | 50ec665 | 2014-11-04 22:17:15 +0100 | [diff] [blame] | 369 | uint16_t powj = 1; |
| 370 | uint8_t precision = 2; |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 371 | char buf[64]; |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 372 | |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 373 | if (!human_readable || count < base) { |
Christian Hesse | f4fe81d | 2014-11-03 08:21:11 +0100 | [diff] [blame] | 374 | fprintf(fp, "%-*"PRIu64" ", width, count); |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 375 | return; |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 376 | } |
| 377 | |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 378 | /* increase value by a factor of 1000/1024 and print |
| 379 | * if result is something a human can read */ |
| 380 | for(;;) { |
| 381 | powi *= base; |
| 382 | if (count / base < powi) |
| 383 | break; |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 384 | |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 385 | if (!prefix[1]) |
| 386 | break; |
| 387 | ++prefix; |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Christian Hesse | 50ec665 | 2014-11-04 22:17:15 +0100 | [diff] [blame] | 390 | /* try to guess a good number of digits for precision */ |
| 391 | for (; precision > 0; precision--) { |
| 392 | powj *= 10; |
| 393 | if (count / powi < powj) |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | snprintf(buf, sizeof(buf), "%.*f%c%s", precision, |
| 398 | (double) count / powi, *prefix, use_iec ? "i" : ""); |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 399 | |
Christian Hesse | f4fe81d | 2014-11-03 08:21:11 +0100 | [diff] [blame] | 400 | fprintf(fp, "%-*s ", width, buf); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 401 | } |
| 402 | |
david decotigny | 30b5579 | 2014-05-05 20:38:18 -0700 | [diff] [blame] | 403 | static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s, |
Stephen Hemminger | 4b726cb | 2014-05-09 12:36:46 -0700 | [diff] [blame] | 404 | const struct rtattr *carrier_changes) |
| 405 | { |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 406 | /* RX stats */ |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 407 | fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s", |
| 408 | s->rx_compressed ? "compressed" : "", _SL_); |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 409 | |
| 410 | fprintf(fp, " "); |
| 411 | print_num(fp, 10, s->rx_bytes); |
| 412 | print_num(fp, 8, s->rx_packets); |
| 413 | print_num(fp, 7, s->rx_errors); |
| 414 | print_num(fp, 7, s->rx_dropped); |
| 415 | print_num(fp, 7, s->rx_over_errors); |
| 416 | print_num(fp, 7, s->multicast); |
| 417 | if (s->rx_compressed) |
| 418 | print_num(fp, 7, s->rx_compressed); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 419 | |
| 420 | /* RX error stats */ |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 421 | if (show_stats > 1) { |
| 422 | fprintf(fp, "%s", _SL_); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 423 | fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_); |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 424 | |
| 425 | fprintf(fp, " "); |
| 426 | print_num(fp, 8, s->rx_length_errors); |
| 427 | print_num(fp, 7, s->rx_crc_errors); |
| 428 | print_num(fp, 7, s->rx_frame_errors); |
| 429 | print_num(fp, 7, s->rx_fifo_errors); |
| 430 | print_num(fp, 7, s->rx_missed_errors); |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 431 | } |
| 432 | fprintf(fp, "%s", _SL_); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 433 | |
| 434 | /* TX stats */ |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 435 | fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s", |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 436 | s->tx_compressed ? "compressed" : "", _SL_); |
| 437 | |
| 438 | |
| 439 | fprintf(fp, " "); |
| 440 | print_num(fp, 10, s->tx_bytes); |
| 441 | print_num(fp, 8, s->tx_packets); |
| 442 | print_num(fp, 7, s->tx_errors); |
| 443 | print_num(fp, 7, s->tx_dropped); |
| 444 | print_num(fp, 7, s->tx_carrier_errors); |
| 445 | print_num(fp, 7, s->collisions); |
| 446 | if (s->tx_compressed) |
| 447 | print_num(fp, 7, s->tx_compressed); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 448 | |
| 449 | /* TX error stats */ |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 450 | if (show_stats > 1) { |
| 451 | fprintf(fp, "%s", _SL_); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 452 | fprintf(fp, " TX errors: aborted fifo window heartbeat"); |
david decotigny | 30b5579 | 2014-05-05 20:38:18 -0700 | [diff] [blame] | 453 | if (carrier_changes) |
| 454 | fprintf(fp, " transns"); |
Stephen Hemminger | 4ec0ffd | 2014-05-29 10:31:30 -0700 | [diff] [blame] | 455 | fprintf(fp, "%s", _SL_); |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 456 | |
| 457 | fprintf(fp, " "); |
| 458 | print_num(fp, 8, s->tx_aborted_errors); |
| 459 | print_num(fp, 7, s->tx_fifo_errors); |
| 460 | print_num(fp, 7, s->tx_window_errors); |
| 461 | print_num(fp, 7, s->tx_heartbeat_errors); |
| 462 | if (carrier_changes) |
| 463 | print_num(fp, 7, *(uint32_t*)RTA_DATA(carrier_changes)); |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Jiri Benc | 5d5cf1b | 2014-10-03 15:25:02 +0200 | [diff] [blame] | 467 | static void print_link_stats32(FILE *fp, const struct rtnl_link_stats *s, |
| 468 | const struct rtattr *carrier_changes) |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 469 | { |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 470 | /* RX stats */ |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 471 | fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s", |
| 472 | s->rx_compressed ? "compressed" : "", _SL_); |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 473 | |
| 474 | |
| 475 | fprintf(fp, " "); |
| 476 | print_num(fp, 10, s->rx_bytes); |
| 477 | print_num(fp, 8, s->rx_packets); |
| 478 | print_num(fp, 7, s->rx_errors); |
| 479 | print_num(fp, 7, s->rx_dropped); |
| 480 | print_num(fp, 7, s->rx_over_errors); |
| 481 | print_num(fp, 7, s->multicast); |
| 482 | if (s->rx_compressed) |
| 483 | print_num(fp, 7, s->rx_compressed); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 484 | |
| 485 | /* RX error stats */ |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 486 | if (show_stats > 1) { |
| 487 | fprintf(fp, "%s", _SL_); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 488 | fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_); |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 489 | fprintf(fp, " "); |
| 490 | print_num(fp, 8, s->rx_length_errors); |
| 491 | print_num(fp, 7, s->rx_crc_errors); |
| 492 | print_num(fp, 7, s->rx_frame_errors); |
| 493 | print_num(fp, 7, s->rx_fifo_errors); |
| 494 | print_num(fp, 7, s->rx_missed_errors); |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 495 | } |
| 496 | fprintf(fp, "%s", _SL_); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 497 | |
| 498 | /* TX stats */ |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 499 | fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s", |
| 500 | s->tx_compressed ? "compressed" : "", _SL_); |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 501 | |
| 502 | fprintf(fp, " "); |
| 503 | print_num(fp, 10, s->tx_bytes); |
| 504 | print_num(fp, 8, s->tx_packets); |
| 505 | print_num(fp, 7, s->tx_errors); |
| 506 | print_num(fp, 7, s->tx_dropped); |
| 507 | print_num(fp, 7, s->tx_carrier_errors); |
| 508 | print_num(fp, 7, s->collisions); |
| 509 | if (s->tx_compressed) |
| 510 | print_num(fp, 7, s->tx_compressed); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 511 | |
| 512 | /* TX error stats */ |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 513 | if (show_stats > 1) { |
| 514 | fprintf(fp, "%s", _SL_); |
Christian Hesse | b68d983 | 2014-10-31 22:33:13 +0100 | [diff] [blame] | 515 | fprintf(fp, " TX errors: aborted fifo window heartbeat"); |
david decotigny | 30b5579 | 2014-05-05 20:38:18 -0700 | [diff] [blame] | 516 | if (carrier_changes) |
| 517 | fprintf(fp, " transns"); |
Stephen Hemminger | 4ec0ffd | 2014-05-29 10:31:30 -0700 | [diff] [blame] | 518 | fprintf(fp, "%s", _SL_); |
Stephen Hemminger | 1e264ab | 2014-11-02 12:49:19 -0800 | [diff] [blame] | 519 | |
| 520 | fprintf(fp, " "); |
| 521 | print_num(fp, 8, s->tx_aborted_errors); |
| 522 | print_num(fp, 7, s->tx_fifo_errors); |
| 523 | print_num(fp, 7, s->tx_window_errors); |
| 524 | print_num(fp, 7, s->tx_heartbeat_errors); |
| 525 | if (carrier_changes) |
| 526 | print_num(fp, 7, *(uint32_t*)RTA_DATA(carrier_changes)); |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Jiri Benc | 5d5cf1b | 2014-10-03 15:25:02 +0200 | [diff] [blame] | 530 | static void __print_link_stats(FILE *fp, struct rtattr **tb) |
| 531 | { |
| 532 | if (tb[IFLA_STATS64]) |
| 533 | print_link_stats64(fp, RTA_DATA(tb[IFLA_STATS64]), |
| 534 | tb[IFLA_CARRIER_CHANGES]); |
| 535 | else if (tb[IFLA_STATS]) |
| 536 | print_link_stats32(fp, RTA_DATA(tb[IFLA_STATS]), |
| 537 | tb[IFLA_CARRIER_CHANGES]); |
| 538 | } |
| 539 | |
| 540 | static void print_link_stats(FILE *fp, struct nlmsghdr *n) |
| 541 | { |
| 542 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
| 543 | struct rtattr * tb[IFLA_MAX+1]; |
| 544 | |
| 545 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), |
| 546 | n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi))); |
| 547 | __print_link_stats(fp, tb); |
| 548 | fprintf(fp, "%s", _SL_); |
| 549 | } |
| 550 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 551 | int print_linkinfo(const struct sockaddr_nl *who, |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 552 | struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 553 | { |
| 554 | FILE *fp = (FILE*)arg; |
| 555 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
| 556 | struct rtattr * tb[IFLA_MAX+1]; |
| 557 | int len = n->nlmsg_len; |
| 558 | unsigned m_flag = 0; |
| 559 | |
| 560 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) |
| 561 | return 0; |
| 562 | |
| 563 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 564 | if (len < 0) |
| 565 | return -1; |
| 566 | |
| 567 | if (filter.ifindex && ifi->ifi_index != filter.ifindex) |
| 568 | return 0; |
| 569 | if (filter.up && !(ifi->ifi_flags&IFF_UP)) |
| 570 | return 0; |
| 571 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 572 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 573 | if (tb[IFLA_IFNAME] == NULL) { |
jamal | 4cd23bd | 2008-08-08 10:06:17 -0400 | [diff] [blame] | 574 | fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 575 | } |
| 576 | if (filter.label && |
| 577 | (!filter.family || filter.family == AF_PACKET) && |
| 578 | fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) |
| 579 | return 0; |
| 580 | |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 581 | if (tb[IFLA_GROUP]) { |
| 582 | int group = *(int*)RTA_DATA(tb[IFLA_GROUP]); |
Stefan Tomanek | c4fdf75 | 2013-08-03 14:20:53 +0200 | [diff] [blame] | 583 | if (filter.group != -1 && group != filter.group) |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 584 | return -1; |
| 585 | } |
| 586 | |
Vadim Kochan | 1b94414 | 2014-11-29 11:27:41 -0800 | [diff] [blame] | 587 | if (tb[IFLA_MASTER]) { |
| 588 | int master = *(int*)RTA_DATA(tb[IFLA_MASTER]); |
| 589 | if (filter.master > 0 && master != filter.master) |
| 590 | return -1; |
| 591 | } |
| 592 | else if (filter.master > 0) |
| 593 | return -1; |
| 594 | |
Vadim Kochan | 712249d | 2014-12-10 10:11:29 +0200 | [diff] [blame] | 595 | if (filter.kind) { |
| 596 | if (tb[IFLA_LINKINFO]) { |
| 597 | char *kind = parse_link_kind(tb[IFLA_LINKINFO]); |
| 598 | |
| 599 | if (strcmp(kind, filter.kind)) |
| 600 | return -1; |
| 601 | } else { |
| 602 | return -1; |
| 603 | } |
| 604 | } |
| 605 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 606 | if (n->nlmsg_type == RTM_DELLINK) |
| 607 | fprintf(fp, "Deleted "); |
| 608 | |
| 609 | fprintf(fp, "%d: %s", ifi->ifi_index, |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 610 | tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 611 | |
| 612 | if (tb[IFLA_LINK]) { |
| 613 | SPRINT_BUF(b1); |
| 614 | int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]); |
| 615 | if (iflink == 0) |
| 616 | fprintf(fp, "@NONE: "); |
| 617 | else { |
Nicolas Dichtel | ccdcbf3 | 2015-02-17 17:30:38 +0100 | [diff] [blame] | 618 | if (tb[IFLA_LINK_NETNSID]) |
| 619 | fprintf(fp, "@if%d: ", iflink); |
| 620 | else { |
| 621 | fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1)); |
| 622 | m_flag = ll_index_to_flags(iflink); |
| 623 | m_flag = !(m_flag & IFF_UP); |
| 624 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 625 | } |
| 626 | } else { |
| 627 | fprintf(fp, ": "); |
| 628 | } |
| 629 | print_link_flags(fp, ifi->ifi_flags, m_flag); |
| 630 | |
| 631 | if (tb[IFLA_MTU]) |
| 632 | fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); |
| 633 | if (tb[IFLA_QDISC]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 634 | fprintf(fp, "qdisc %s ", rta_getattr_str(tb[IFLA_QDISC])); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 635 | if (tb[IFLA_MASTER]) { |
| 636 | SPRINT_BUF(b1); |
| 637 | fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); |
| 638 | } |
Stephen Hemminger | 8249928 | 2012-03-19 17:24:43 -0700 | [diff] [blame] | 639 | |
Jiri Pirko | c897067 | 2014-05-15 15:10:22 +0200 | [diff] [blame] | 640 | if (tb[IFLA_PHYS_PORT_ID]) { |
| 641 | SPRINT_BUF(b1); |
| 642 | fprintf(fp, "portid %s ", |
| 643 | hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_PORT_ID]), |
| 644 | RTA_PAYLOAD(tb[IFLA_PHYS_PORT_ID]), |
| 645 | b1, sizeof(b1))); |
| 646 | } |
| 647 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 648 | if (tb[IFLA_OPERSTATE]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 649 | print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE])); |
Stephen Hemminger | 8249928 | 2012-03-19 17:24:43 -0700 | [diff] [blame] | 650 | |
| 651 | if (do_link && tb[IFLA_LINKMODE]) |
| 652 | print_linkmode(fp, tb[IFLA_LINKMODE]); |
| 653 | |
Stefan Tomanek | c4fdf75 | 2013-08-03 14:20:53 +0200 | [diff] [blame] | 654 | if (tb[IFLA_GROUP]) { |
| 655 | SPRINT_BUF(b1); |
| 656 | int group = *(int*)RTA_DATA(tb[IFLA_GROUP]); |
| 657 | fprintf(fp, "group %s ", rtnl_group_n2a(group, b1, sizeof(b1))); |
| 658 | } |
| 659 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 660 | if (filter.showqueue) |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 661 | print_queuelen(fp, tb); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 662 | |
Nicolas Dichtel | 2ec2893 | 2014-12-04 17:41:07 +0100 | [diff] [blame] | 663 | if (!filter.family || filter.family == AF_PACKET || show_details) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 664 | SPRINT_BUF(b1); |
| 665 | fprintf(fp, "%s", _SL_); |
| 666 | fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); |
| 667 | |
| 668 | if (tb[IFLA_ADDRESS]) { |
| 669 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), |
| 670 | RTA_PAYLOAD(tb[IFLA_ADDRESS]), |
| 671 | ifi->ifi_type, |
| 672 | b1, sizeof(b1))); |
| 673 | } |
| 674 | if (tb[IFLA_BROADCAST]) { |
| 675 | if (ifi->ifi_flags&IFF_POINTOPOINT) |
| 676 | fprintf(fp, " peer "); |
| 677 | else |
| 678 | fprintf(fp, " brd "); |
| 679 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), |
| 680 | RTA_PAYLOAD(tb[IFLA_BROADCAST]), |
| 681 | ifi->ifi_type, |
| 682 | b1, sizeof(b1))); |
| 683 | } |
| 684 | } |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 685 | |
Nicolas Dichtel | ccdcbf3 | 2015-02-17 17:30:38 +0100 | [diff] [blame] | 686 | if (tb[IFLA_LINK_NETNSID]) { |
| 687 | int id = *(int*)RTA_DATA(tb[IFLA_LINK_NETNSID]); |
| 688 | |
| 689 | if (id >= 0) |
| 690 | fprintf(fp, " link-netnsid %d", id); |
| 691 | else |
| 692 | fprintf(fp, " link-netnsid unknown"); |
| 693 | } |
| 694 | |
Nicolas Dichtel | 2ec2893 | 2014-12-04 17:41:07 +0100 | [diff] [blame] | 695 | if (tb[IFLA_PROMISCUITY] && show_details) |
Stephen Hemminger | 1cb6a11 | 2013-02-05 08:16:28 -0800 | [diff] [blame] | 696 | fprintf(fp, " promiscuity %u ", |
| 697 | *(int*)RTA_DATA(tb[IFLA_PROMISCUITY])); |
| 698 | |
Nicolas Dichtel | 2ec2893 | 2014-12-04 17:41:07 +0100 | [diff] [blame] | 699 | if (tb[IFLA_LINKINFO] && show_details) |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 700 | print_linktype(fp, tb[IFLA_LINKINFO]); |
| 701 | |
Jiri Pirko | ee0067a | 2015-01-08 09:49:55 +0100 | [diff] [blame] | 702 | if (do_link && tb[IFLA_AF_SPEC] && show_details) |
| 703 | print_af_spec(fp, tb[IFLA_AF_SPEC]); |
| 704 | |
Nicolas Dichtel | 2ec2893 | 2014-12-04 17:41:07 +0100 | [diff] [blame] | 705 | if ((do_link || show_details) && tb[IFLA_IFALIAS]) { |
Stephen Hemminger | 1124ffb | 2013-03-14 13:47:49 -0700 | [diff] [blame] | 706 | fprintf(fp, "%s alias %s", _SL_, |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 707 | rta_getattr_str(tb[IFLA_IFALIAS])); |
roopa | 263c894 | 2013-03-12 15:04:02 -0700 | [diff] [blame] | 708 | } |
Stephen Hemminger | ace9c96 | 2009-03-23 10:46:47 -0700 | [diff] [blame] | 709 | |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 710 | if (do_link && show_stats) { |
Jiri Benc | 5d5cf1b | 2014-10-03 15:25:02 +0200 | [diff] [blame] | 711 | fprintf(fp, "%s", _SL_); |
| 712 | __print_link_stats(fp, tb); |
Jan Engelhardt | 8864ac9 | 2010-03-11 10:00:34 +0000 | [diff] [blame] | 713 | } |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 714 | |
Nicolas Dichtel | 2ec2893 | 2014-12-04 17:41:07 +0100 | [diff] [blame] | 715 | if ((do_link || show_details) && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) { |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 716 | struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST]; |
| 717 | int rem = RTA_PAYLOAD(vflist); |
| 718 | for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) |
| 719 | print_vfinfo(fp, i); |
Williams, Mitch A | ae7229d | 2010-02-10 01:47:08 +0000 | [diff] [blame] | 720 | } |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 721 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 722 | fprintf(fp, "\n"); |
| 723 | fflush(fp); |
Jiri Benc | 5d5cf1b | 2014-10-03 15:25:02 +0200 | [diff] [blame] | 724 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | static int flush_update(void) |
| 728 | { |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 729 | if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) { |
Stephen Hemminger | 1fb0a99 | 2008-01-26 11:08:31 -0800 | [diff] [blame] | 730 | perror("Failed to send flush request"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 731 | return -1; |
| 732 | } |
| 733 | filter.flushp = 0; |
| 734 | return 0; |
| 735 | } |
| 736 | |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 737 | static int set_lifetime(unsigned int *lifetime, char *argv) |
| 738 | { |
| 739 | if (strcmp(argv, "forever") == 0) |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 740 | *lifetime = INFINITY_LIFE_TIME; |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 741 | else if (get_u32(lifetime, argv, 0)) |
| 742 | return -1; |
| 743 | |
| 744 | return 0; |
| 745 | } |
| 746 | |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 747 | static unsigned int get_ifa_flags(struct ifaddrmsg *ifa, |
| 748 | struct rtattr *ifa_flags_attr) |
| 749 | { |
| 750 | return ifa_flags_attr ? rta_getattr_u32(ifa_flags_attr) : |
| 751 | ifa->ifa_flags; |
| 752 | } |
| 753 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 754 | int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 755 | void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 756 | { |
Stephen Hemminger | 4b726cb | 2014-05-09 12:36:46 -0700 | [diff] [blame] | 757 | FILE *fp = arg; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 758 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 759 | int len = n->nlmsg_len; |
Benedikt Gollatz | 037d950 | 2009-01-06 19:36:56 -0800 | [diff] [blame] | 760 | int deprecated = 0; |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 761 | /* Use local copy of ifa_flags to not interfere with filtering code */ |
| 762 | unsigned int ifa_flags; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 763 | struct rtattr * rta_tb[IFA_MAX+1]; |
| 764 | char abuf[256]; |
| 765 | SPRINT_BUF(b1); |
| 766 | |
| 767 | if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR) |
| 768 | return 0; |
| 769 | len -= NLMSG_LENGTH(sizeof(*ifa)); |
| 770 | if (len < 0) { |
| 771 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 772 | return -1; |
| 773 | } |
| 774 | |
| 775 | if (filter.flushb && n->nlmsg_type != RTM_NEWADDR) |
| 776 | return 0; |
| 777 | |
Stephen Hemminger | 4b726cb | 2014-05-09 12:36:46 -0700 | [diff] [blame] | 778 | parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), |
| 779 | n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 780 | |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 781 | ifa_flags = get_ifa_flags(ifa, rta_tb[IFA_FLAGS]); |
| 782 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 783 | if (!rta_tb[IFA_LOCAL]) |
| 784 | rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; |
| 785 | if (!rta_tb[IFA_ADDRESS]) |
| 786 | rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; |
| 787 | |
| 788 | if (filter.ifindex && filter.ifindex != ifa->ifa_index) |
| 789 | return 0; |
| 790 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 791 | return 0; |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 792 | if ((filter.flags ^ ifa_flags) & filter.flagmask) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 793 | return 0; |
| 794 | if (filter.label) { |
| 795 | SPRINT_BUF(b1); |
| 796 | const char *label; |
| 797 | if (rta_tb[IFA_LABEL]) |
| 798 | label = RTA_DATA(rta_tb[IFA_LABEL]); |
| 799 | else |
| 800 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 801 | if (fnmatch(filter.label, label, 0) != 0) |
| 802 | return 0; |
| 803 | } |
| 804 | if (filter.pfx.family) { |
| 805 | if (rta_tb[IFA_LOCAL]) { |
| 806 | inet_prefix dst; |
| 807 | memset(&dst, 0, sizeof(dst)); |
| 808 | dst.family = ifa->ifa_family; |
| 809 | memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL])); |
| 810 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 811 | return 0; |
| 812 | } |
| 813 | } |
| 814 | |
net[shemminger]!shemminger | 3eb1731 | 2005-02-07 18:28:31 +0000 | [diff] [blame] | 815 | if (filter.family && filter.family != ifa->ifa_family) |
| 816 | return 0; |
| 817 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 818 | if (filter.flushb) { |
| 819 | struct nlmsghdr *fn; |
| 820 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { |
| 821 | if (flush_update()) |
| 822 | return -1; |
| 823 | } |
| 824 | fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); |
| 825 | memcpy(fn, n, n->nlmsg_len); |
| 826 | fn->nlmsg_type = RTM_DELADDR; |
| 827 | fn->nlmsg_flags = NLM_F_REQUEST; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 828 | fn->nlmsg_seq = ++rth.seq; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 829 | filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; |
| 830 | filter.flushed++; |
| 831 | if (show_stats < 2) |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | if (n->nlmsg_type == RTM_DELADDR) |
| 836 | fprintf(fp, "Deleted "); |
| 837 | |
| 838 | if (filter.oneline || filter.flushb) |
| 839 | fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); |
| 840 | if (ifa->ifa_family == AF_INET) |
| 841 | fprintf(fp, " inet "); |
| 842 | else if (ifa->ifa_family == AF_INET6) |
| 843 | fprintf(fp, " inet6 "); |
| 844 | else if (ifa->ifa_family == AF_DECnet) |
| 845 | fprintf(fp, " dnet "); |
| 846 | else if (ifa->ifa_family == AF_IPX) |
| 847 | fprintf(fp, " ipx "); |
| 848 | else |
| 849 | fprintf(fp, " family %d ", ifa->ifa_family); |
| 850 | |
| 851 | if (rta_tb[IFA_LOCAL]) { |
Sami Kerola | ffa35d9 | 2013-09-30 22:01:48 +0100 | [diff] [blame] | 852 | fprintf(fp, "%s", format_host(ifa->ifa_family, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 853 | RTA_PAYLOAD(rta_tb[IFA_LOCAL]), |
| 854 | RTA_DATA(rta_tb[IFA_LOCAL]), |
| 855 | abuf, sizeof(abuf))); |
| 856 | |
| 857 | if (rta_tb[IFA_ADDRESS] == NULL || |
Nicolas Dichtel | 973eb50 | 2013-07-16 22:52:48 +0200 | [diff] [blame] | 858 | memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), |
| 859 | ifa->ifa_family == AF_INET ? 4 : 16) == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 860 | fprintf(fp, "/%d ", ifa->ifa_prefixlen); |
| 861 | } else { |
| 862 | fprintf(fp, " peer %s/%d ", |
Sami Kerola | ffa35d9 | 2013-09-30 22:01:48 +0100 | [diff] [blame] | 863 | format_host(ifa->ifa_family, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 864 | RTA_PAYLOAD(rta_tb[IFA_ADDRESS]), |
| 865 | RTA_DATA(rta_tb[IFA_ADDRESS]), |
| 866 | abuf, sizeof(abuf)), |
| 867 | ifa->ifa_prefixlen); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | if (rta_tb[IFA_BROADCAST]) { |
| 872 | fprintf(fp, "brd %s ", |
Sami Kerola | ffa35d9 | 2013-09-30 22:01:48 +0100 | [diff] [blame] | 873 | format_host(ifa->ifa_family, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 874 | RTA_PAYLOAD(rta_tb[IFA_BROADCAST]), |
| 875 | RTA_DATA(rta_tb[IFA_BROADCAST]), |
| 876 | abuf, sizeof(abuf))); |
| 877 | } |
| 878 | if (rta_tb[IFA_ANYCAST]) { |
| 879 | fprintf(fp, "any %s ", |
Sami Kerola | ffa35d9 | 2013-09-30 22:01:48 +0100 | [diff] [blame] | 880 | format_host(ifa->ifa_family, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 881 | RTA_PAYLOAD(rta_tb[IFA_ANYCAST]), |
| 882 | RTA_DATA(rta_tb[IFA_ANYCAST]), |
| 883 | abuf, sizeof(abuf))); |
| 884 | } |
| 885 | fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 886 | if (ifa_flags & IFA_F_SECONDARY) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 887 | ifa_flags &= ~IFA_F_SECONDARY; |
Brian Haley | a1b9ffc | 2009-09-14 17:01:43 -0400 | [diff] [blame] | 888 | if (ifa->ifa_family == AF_INET6) |
| 889 | fprintf(fp, "temporary "); |
| 890 | else |
| 891 | fprintf(fp, "secondary "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 892 | } |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 893 | if (ifa_flags & IFA_F_TENTATIVE) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 894 | ifa_flags &= ~IFA_F_TENTATIVE; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 895 | fprintf(fp, "tentative "); |
| 896 | } |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 897 | if (ifa_flags & IFA_F_DEPRECATED) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 898 | ifa_flags &= ~IFA_F_DEPRECATED; |
Benedikt Gollatz | 037d950 | 2009-01-06 19:36:56 -0800 | [diff] [blame] | 899 | deprecated = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 900 | fprintf(fp, "deprecated "); |
| 901 | } |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 902 | if (ifa_flags & IFA_F_HOMEADDRESS) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 903 | ifa_flags &= ~IFA_F_HOMEADDRESS; |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 904 | fprintf(fp, "home "); |
| 905 | } |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 906 | if (ifa_flags & IFA_F_NODAD) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 907 | ifa_flags &= ~IFA_F_NODAD; |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 908 | fprintf(fp, "nodad "); |
| 909 | } |
Jiri Pirko | 5b7e21c | 2014-01-06 10:17:10 +0100 | [diff] [blame] | 910 | if (ifa_flags & IFA_F_MANAGETEMPADDR) { |
| 911 | ifa_flags &= ~IFA_F_MANAGETEMPADDR; |
| 912 | fprintf(fp, "mngtmpaddr "); |
| 913 | } |
Thomas Haller | 58c69b2 | 2014-01-07 21:46:28 +0100 | [diff] [blame] | 914 | if (ifa_flags & IFA_F_NOPREFIXROUTE) { |
| 915 | ifa_flags &= ~IFA_F_NOPREFIXROUTE; |
| 916 | fprintf(fp, "noprefixroute "); |
| 917 | } |
Madhu Challa | e31867a | 2015-03-04 10:30:10 -0800 | [diff] [blame^] | 918 | if (ifa_flags & IFA_F_MCAUTOJOIN) { |
| 919 | ifa_flags &= ~IFA_F_MCAUTOJOIN; |
| 920 | fprintf(fp, "autojoin "); |
| 921 | } |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 922 | if (!(ifa_flags & IFA_F_PERMANENT)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 923 | fprintf(fp, "dynamic "); |
| 924 | } else |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 925 | ifa_flags &= ~IFA_F_PERMANENT; |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 926 | if (ifa_flags & IFA_F_DADFAILED) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 927 | ifa_flags &= ~IFA_F_DADFAILED; |
Brian Haley | f4af851 | 2009-12-01 15:58:44 -0800 | [diff] [blame] | 928 | fprintf(fp, "dadfailed "); |
| 929 | } |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 930 | if (ifa_flags) |
| 931 | fprintf(fp, "flags %02x ", ifa_flags); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 932 | if (rta_tb[IFA_LABEL]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 933 | fprintf(fp, "%s", rta_getattr_str(rta_tb[IFA_LABEL])); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 934 | if (rta_tb[IFA_CACHEINFO]) { |
| 935 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 936 | fprintf(fp, "%s", _SL_); |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 937 | fprintf(fp, " valid_lft "); |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 938 | if (ci->ifa_valid == INFINITY_LIFE_TIME) |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 939 | fprintf(fp, "forever"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 940 | else |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 941 | fprintf(fp, "%usec", ci->ifa_valid); |
| 942 | fprintf(fp, " preferred_lft "); |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 943 | if (ci->ifa_prefered == INFINITY_LIFE_TIME) |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 944 | fprintf(fp, "forever"); |
Benedikt Gollatz | 037d950 | 2009-01-06 19:36:56 -0800 | [diff] [blame] | 945 | else { |
| 946 | if (deprecated) |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 947 | fprintf(fp, "%dsec", ci->ifa_prefered); |
Benedikt Gollatz | 037d950 | 2009-01-06 19:36:56 -0800 | [diff] [blame] | 948 | else |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 949 | fprintf(fp, "%usec", ci->ifa_prefered); |
Benedikt Gollatz | 037d950 | 2009-01-06 19:36:56 -0800 | [diff] [blame] | 950 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 951 | } |
| 952 | fprintf(fp, "\n"); |
| 953 | fflush(fp); |
| 954 | return 0; |
| 955 | } |
| 956 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 957 | static int print_addrinfo_primary(const struct sockaddr_nl *who, |
| 958 | struct nlmsghdr *n, void *arg) |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 959 | { |
| 960 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 961 | |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 962 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 963 | return 0; |
| 964 | |
| 965 | return print_addrinfo(who, n, arg); |
| 966 | } |
| 967 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 968 | static int print_addrinfo_secondary(const struct sockaddr_nl *who, |
| 969 | struct nlmsghdr *n, void *arg) |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 970 | { |
| 971 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 972 | |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 973 | if (!(ifa->ifa_flags & IFA_F_SECONDARY)) |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 974 | return 0; |
| 975 | |
| 976 | return print_addrinfo(who, n, arg); |
| 977 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 978 | |
| 979 | struct nlmsg_list |
| 980 | { |
| 981 | struct nlmsg_list *next; |
| 982 | struct nlmsghdr h; |
| 983 | }; |
| 984 | |
Eric Dumazet | 62e2e54 | 2012-06-09 13:55:55 +0200 | [diff] [blame] | 985 | struct nlmsg_chain |
| 986 | { |
| 987 | struct nlmsg_list *head; |
| 988 | struct nlmsg_list *tail; |
| 989 | }; |
| 990 | |
Andreas Henriksson | 5e5055b | 2015-01-23 13:10:33 +0100 | [diff] [blame] | 991 | static int print_selected_addrinfo(struct ifinfomsg *ifi, |
| 992 | struct nlmsg_list *ainfo, FILE *fp) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 993 | { |
| 994 | for ( ;ainfo ; ainfo = ainfo->next) { |
| 995 | struct nlmsghdr *n = &ainfo->h; |
| 996 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 997 | |
| 998 | if (n->nlmsg_type != RTM_NEWADDR) |
| 999 | continue; |
| 1000 | |
| 1001 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa))) |
| 1002 | return -1; |
| 1003 | |
Andreas Henriksson | 5e5055b | 2015-01-23 13:10:33 +0100 | [diff] [blame] | 1004 | if (ifa->ifa_index != ifi->ifi_index || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1005 | (filter.family && filter.family != ifa->ifa_family)) |
| 1006 | continue; |
| 1007 | |
Andreas Henriksson | 5e5055b | 2015-01-23 13:10:33 +0100 | [diff] [blame] | 1008 | if (filter.up && !(ifi->ifi_flags&IFF_UP)) |
| 1009 | continue; |
| 1010 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1011 | print_addrinfo(NULL, n, fp); |
| 1012 | } |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 1017 | static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 1018 | void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1019 | { |
Eric Dumazet | 62e2e54 | 2012-06-09 13:55:55 +0200 | [diff] [blame] | 1020 | struct nlmsg_chain *lchain = (struct nlmsg_chain *)arg; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1021 | struct nlmsg_list *h; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1022 | |
| 1023 | h = malloc(n->nlmsg_len+sizeof(void*)); |
| 1024 | if (h == NULL) |
| 1025 | return -1; |
| 1026 | |
| 1027 | memcpy(&h->h, n, n->nlmsg_len); |
| 1028 | h->next = NULL; |
| 1029 | |
Eric Dumazet | 62e2e54 | 2012-06-09 13:55:55 +0200 | [diff] [blame] | 1030 | if (lchain->tail) |
| 1031 | lchain->tail->next = h; |
| 1032 | else |
| 1033 | lchain->head = h; |
| 1034 | lchain->tail = h; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1035 | |
| 1036 | ll_remember_index(who, n, NULL); |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 1040 | static __u32 ipadd_dump_magic = 0x47361222; |
| 1041 | |
| 1042 | static int ipadd_save_prep(void) |
| 1043 | { |
| 1044 | int ret; |
| 1045 | |
| 1046 | if (isatty(STDOUT_FILENO)) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 1047 | fprintf(stderr, "Not sending a binary stream to stdout\n"); |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 1048 | return -1; |
| 1049 | } |
| 1050 | |
| 1051 | ret = write(STDOUT_FILENO, &ipadd_dump_magic, sizeof(ipadd_dump_magic)); |
| 1052 | if (ret != sizeof(ipadd_dump_magic)) { |
| 1053 | fprintf(stderr, "Can't write magic to dump file\n"); |
| 1054 | return -1; |
| 1055 | } |
| 1056 | |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | static int ipadd_dump_check_magic(void) |
| 1061 | { |
| 1062 | int ret; |
| 1063 | __u32 magic = 0; |
| 1064 | |
| 1065 | if (isatty(STDIN_FILENO)) { |
| 1066 | fprintf(stderr, "Can't restore addr dump from a terminal\n"); |
| 1067 | return -1; |
| 1068 | } |
| 1069 | |
| 1070 | ret = fread(&magic, sizeof(magic), 1, stdin); |
| 1071 | if (magic != ipadd_dump_magic) { |
| 1072 | fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n", ret, magic); |
| 1073 | return -1; |
| 1074 | } |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, |
| 1080 | void *arg) |
| 1081 | { |
| 1082 | int ret; |
| 1083 | |
| 1084 | ret = write(STDOUT_FILENO, n, n->nlmsg_len); |
| 1085 | if ((ret > 0) && (ret != n->nlmsg_len)) { |
| 1086 | fprintf(stderr, "Short write while saving nlmsg\n"); |
| 1087 | ret = -EIO; |
| 1088 | } |
| 1089 | |
| 1090 | return ret == n->nlmsg_len ? 0 : ret; |
| 1091 | } |
| 1092 | |
| 1093 | static int show_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg) |
| 1094 | { |
| 1095 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 1096 | |
| 1097 | printf("if%d:\n", ifa->ifa_index); |
| 1098 | print_addrinfo(NULL, n, stdout); |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static int ipaddr_showdump(void) |
| 1103 | { |
| 1104 | if (ipadd_dump_check_magic()) |
| 1105 | exit(-1); |
| 1106 | |
| 1107 | exit(rtnl_from_file(stdin, &show_handler, NULL)); |
| 1108 | } |
| 1109 | |
| 1110 | static int restore_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg) |
| 1111 | { |
| 1112 | int ret; |
| 1113 | |
| 1114 | n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK; |
| 1115 | |
| 1116 | ll_init_map(&rth); |
| 1117 | |
| 1118 | ret = rtnl_talk(&rth, n, 0, 0, n); |
| 1119 | if ((ret < 0) && (errno == EEXIST)) |
| 1120 | ret = 0; |
| 1121 | |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
| 1125 | static int ipaddr_restore(void) |
| 1126 | { |
| 1127 | if (ipadd_dump_check_magic()) |
| 1128 | exit(-1); |
| 1129 | |
| 1130 | exit(rtnl_from_file(stdin, &restore_handler, NULL)); |
| 1131 | } |
| 1132 | |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1133 | static void free_nlmsg_chain(struct nlmsg_chain *info) |
| 1134 | { |
| 1135 | struct nlmsg_list *l, *n; |
| 1136 | |
| 1137 | for (l = info->head; l; l = n) { |
| 1138 | n = l->next; |
| 1139 | free(l); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo) |
| 1144 | { |
| 1145 | struct nlmsg_list *l, **lp; |
| 1146 | |
| 1147 | lp = &linfo->head; |
| 1148 | while ( (l = *lp) != NULL) { |
| 1149 | int ok = 0; |
Petr PĂsaĹ™ | 7f747fd | 2012-10-03 16:42:41 +0200 | [diff] [blame] | 1150 | int missing_net_address = 1; |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1151 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 1152 | struct nlmsg_list *a; |
| 1153 | |
| 1154 | for (a = ainfo->head; a; a = a->next) { |
| 1155 | struct nlmsghdr *n = &a->h; |
| 1156 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 1157 | struct rtattr *tb[IFA_MAX + 1]; |
| 1158 | unsigned int ifa_flags; |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1159 | |
Petr PĂsaĹ™ | 7f747fd | 2012-10-03 16:42:41 +0200 | [diff] [blame] | 1160 | if (ifa->ifa_index != ifi->ifi_index) |
| 1161 | continue; |
| 1162 | missing_net_address = 0; |
| 1163 | if (filter.family && filter.family != ifa->ifa_family) |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1164 | continue; |
| 1165 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 1166 | continue; |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 1167 | |
| 1168 | parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n)); |
| 1169 | ifa_flags = get_ifa_flags(ifa, tb[IFA_FLAGS]); |
| 1170 | |
| 1171 | if ((filter.flags ^ ifa_flags) & filter.flagmask) |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1172 | continue; |
| 1173 | if (filter.pfx.family || filter.label) { |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1174 | if (!tb[IFA_LOCAL]) |
| 1175 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; |
| 1176 | |
| 1177 | if (filter.pfx.family && tb[IFA_LOCAL]) { |
| 1178 | inet_prefix dst; |
| 1179 | memset(&dst, 0, sizeof(dst)); |
| 1180 | dst.family = ifa->ifa_family; |
| 1181 | memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL])); |
| 1182 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 1183 | continue; |
| 1184 | } |
| 1185 | if (filter.label) { |
| 1186 | SPRINT_BUF(b1); |
| 1187 | const char *label; |
| 1188 | if (tb[IFA_LABEL]) |
| 1189 | label = RTA_DATA(tb[IFA_LABEL]); |
| 1190 | else |
| 1191 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 1192 | if (fnmatch(filter.label, label, 0) != 0) |
| 1193 | continue; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | ok = 1; |
| 1198 | break; |
| 1199 | } |
Petr PĂsaĹ™ | 7f747fd | 2012-10-03 16:42:41 +0200 | [diff] [blame] | 1200 | if (missing_net_address && |
| 1201 | (filter.family == AF_UNSPEC || filter.family == AF_PACKET)) |
| 1202 | ok = 1; |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1203 | if (!ok) { |
| 1204 | *lp = l->next; |
| 1205 | free(l); |
| 1206 | } else |
| 1207 | lp = &l->next; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | static int ipaddr_flush(void) |
| 1212 | { |
| 1213 | int round = 0; |
| 1214 | char flushb[4096-512]; |
| 1215 | |
| 1216 | filter.flushb = flushb; |
| 1217 | filter.flushp = 0; |
| 1218 | filter.flushe = sizeof(flushb); |
| 1219 | |
| 1220 | while ((max_flush_loops == 0) || (round < max_flush_loops)) { |
| 1221 | const struct rtnl_dump_filter_arg a[3] = { |
| 1222 | { |
| 1223 | .filter = print_addrinfo_secondary, |
| 1224 | .arg1 = stdout, |
| 1225 | }, |
| 1226 | { |
| 1227 | .filter = print_addrinfo_primary, |
| 1228 | .arg1 = stdout, |
| 1229 | }, |
| 1230 | { |
| 1231 | .filter = NULL, |
| 1232 | .arg1 = NULL, |
| 1233 | }, |
| 1234 | }; |
| 1235 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
| 1236 | perror("Cannot send dump request"); |
| 1237 | exit(1); |
| 1238 | } |
| 1239 | filter.flushed = 0; |
| 1240 | if (rtnl_dump_filter_l(&rth, a) < 0) { |
| 1241 | fprintf(stderr, "Flush terminated\n"); |
| 1242 | exit(1); |
| 1243 | } |
| 1244 | if (filter.flushed == 0) { |
| 1245 | flush_done: |
| 1246 | if (show_stats) { |
| 1247 | if (round == 0) |
| 1248 | printf("Nothing to flush.\n"); |
| 1249 | else |
| 1250 | printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":""); |
| 1251 | } |
| 1252 | fflush(stdout); |
| 1253 | return 0; |
| 1254 | } |
| 1255 | round++; |
| 1256 | if (flush_update() < 0) |
| 1257 | return 1; |
| 1258 | |
| 1259 | if (show_stats) { |
| 1260 | printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed); |
| 1261 | fflush(stdout); |
| 1262 | } |
| 1263 | |
| 1264 | /* If we are flushing, and specifying primary, then we |
| 1265 | * want to flush only a single round. Otherwise, we'll |
| 1266 | * start flushing secondaries that were promoted to |
| 1267 | * primaries. |
| 1268 | */ |
| 1269 | if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY)) |
| 1270 | goto flush_done; |
| 1271 | } |
| 1272 | fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops); |
| 1273 | fflush(stderr); |
| 1274 | return 1; |
| 1275 | } |
| 1276 | |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 1277 | static int ipaddr_list_flush_or_save(int argc, char **argv, int action) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1278 | { |
Eric Dumazet | 62e2e54 | 2012-06-09 13:55:55 +0200 | [diff] [blame] | 1279 | struct nlmsg_chain linfo = { NULL, NULL}; |
| 1280 | struct nlmsg_chain ainfo = { NULL, NULL}; |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1281 | struct nlmsg_list *l; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1282 | char *filter_dev = NULL; |
| 1283 | int no_link = 0; |
| 1284 | |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 1285 | ipaddr_reset_filter(oneline, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1286 | filter.showqueue = 1; |
| 1287 | |
| 1288 | if (filter.family == AF_UNSPEC) |
| 1289 | filter.family = preferred_family; |
| 1290 | |
Stefan Tomanek | c4fdf75 | 2013-08-03 14:20:53 +0200 | [diff] [blame] | 1291 | filter.group = -1; |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 1292 | |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 1293 | if (action == IPADD_FLUSH) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1294 | if (argc <= 0) { |
| 1295 | fprintf(stderr, "Flush requires arguments.\n"); |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 1296 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1297 | return -1; |
| 1298 | } |
| 1299 | if (filter.family == AF_PACKET) { |
| 1300 | fprintf(stderr, "Cannot flush link addresses.\n"); |
| 1301 | return -1; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | while (argc > 0) { |
| 1306 | if (strcmp(*argv, "to") == 0) { |
| 1307 | NEXT_ARG(); |
| 1308 | get_prefix(&filter.pfx, *argv, filter.family); |
| 1309 | if (filter.family == AF_UNSPEC) |
| 1310 | filter.family = filter.pfx.family; |
| 1311 | } else if (strcmp(*argv, "scope") == 0) { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 1312 | unsigned scope = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1313 | NEXT_ARG(); |
| 1314 | filter.scopemask = -1; |
| 1315 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 1316 | if (strcmp(*argv, "all") != 0) |
| 1317 | invarg("invalid \"scope\"\n", *argv); |
| 1318 | scope = RT_SCOPE_NOWHERE; |
| 1319 | filter.scopemask = 0; |
| 1320 | } |
| 1321 | filter.scope = scope; |
| 1322 | } else if (strcmp(*argv, "up") == 0) { |
| 1323 | filter.up = 1; |
| 1324 | } else if (strcmp(*argv, "dynamic") == 0) { |
| 1325 | filter.flags &= ~IFA_F_PERMANENT; |
| 1326 | filter.flagmask |= IFA_F_PERMANENT; |
| 1327 | } else if (strcmp(*argv, "permanent") == 0) { |
| 1328 | filter.flags |= IFA_F_PERMANENT; |
| 1329 | filter.flagmask |= IFA_F_PERMANENT; |
Brian Haley | a1b9ffc | 2009-09-14 17:01:43 -0400 | [diff] [blame] | 1330 | } else if (strcmp(*argv, "secondary") == 0 || |
| 1331 | strcmp(*argv, "temporary") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1332 | filter.flags |= IFA_F_SECONDARY; |
| 1333 | filter.flagmask |= IFA_F_SECONDARY; |
| 1334 | } else if (strcmp(*argv, "primary") == 0) { |
| 1335 | filter.flags &= ~IFA_F_SECONDARY; |
| 1336 | filter.flagmask |= IFA_F_SECONDARY; |
| 1337 | } else if (strcmp(*argv, "tentative") == 0) { |
| 1338 | filter.flags |= IFA_F_TENTATIVE; |
| 1339 | filter.flagmask |= IFA_F_TENTATIVE; |
Heiner Kallweit | b5f39b2 | 2014-12-22 20:18:43 +0100 | [diff] [blame] | 1340 | } else if (strcmp(*argv, "-tentative") == 0) { |
| 1341 | filter.flags &= ~IFA_F_TENTATIVE; |
| 1342 | filter.flagmask |= IFA_F_TENTATIVE; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1343 | } else if (strcmp(*argv, "deprecated") == 0) { |
| 1344 | filter.flags |= IFA_F_DEPRECATED; |
| 1345 | filter.flagmask |= IFA_F_DEPRECATED; |
Heiner Kallweit | b5f39b2 | 2014-12-22 20:18:43 +0100 | [diff] [blame] | 1346 | } else if (strcmp(*argv, "-deprecated") == 0) { |
| 1347 | filter.flags &= ~IFA_F_DEPRECATED; |
| 1348 | filter.flagmask |= IFA_F_DEPRECATED; |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 1349 | } else if (strcmp(*argv, "home") == 0) { |
| 1350 | filter.flags |= IFA_F_HOMEADDRESS; |
| 1351 | filter.flagmask |= IFA_F_HOMEADDRESS; |
| 1352 | } else if (strcmp(*argv, "nodad") == 0) { |
| 1353 | filter.flags |= IFA_F_NODAD; |
| 1354 | filter.flagmask |= IFA_F_NODAD; |
Jiri Pirko | 5b7e21c | 2014-01-06 10:17:10 +0100 | [diff] [blame] | 1355 | } else if (strcmp(*argv, "mngtmpaddr") == 0) { |
| 1356 | filter.flags |= IFA_F_MANAGETEMPADDR; |
| 1357 | filter.flagmask |= IFA_F_MANAGETEMPADDR; |
Thomas Haller | 58c69b2 | 2014-01-07 21:46:28 +0100 | [diff] [blame] | 1358 | } else if (strcmp(*argv, "noprefixroute") == 0) { |
| 1359 | filter.flags |= IFA_F_NOPREFIXROUTE; |
| 1360 | filter.flagmask |= IFA_F_NOPREFIXROUTE; |
Madhu Challa | e31867a | 2015-03-04 10:30:10 -0800 | [diff] [blame^] | 1361 | } else if (strcmp(*argv, "autojoin") == 0) { |
| 1362 | filter.flags |= IFA_F_MCAUTOJOIN; |
| 1363 | filter.flagmask |= IFA_F_MCAUTOJOIN; |
Brian Haley | a1f2779 | 2009-12-03 10:39:36 +0000 | [diff] [blame] | 1364 | } else if (strcmp(*argv, "dadfailed") == 0) { |
| 1365 | filter.flags |= IFA_F_DADFAILED; |
| 1366 | filter.flagmask |= IFA_F_DADFAILED; |
Heiner Kallweit | b5f39b2 | 2014-12-22 20:18:43 +0100 | [diff] [blame] | 1367 | } else if (strcmp(*argv, "-dadfailed") == 0) { |
| 1368 | filter.flags &= ~IFA_F_DADFAILED; |
| 1369 | filter.flagmask |= IFA_F_DADFAILED; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1370 | } else if (strcmp(*argv, "label") == 0) { |
| 1371 | NEXT_ARG(); |
| 1372 | filter.label = *argv; |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 1373 | } else if (strcmp(*argv, "group") == 0) { |
| 1374 | NEXT_ARG(); |
| 1375 | if (rtnl_group_a2n(&filter.group, *argv)) |
| 1376 | invarg("Invalid \"group\" value\n", *argv); |
Vadim Kochan | 1b94414 | 2014-11-29 11:27:41 -0800 | [diff] [blame] | 1377 | } else if (strcmp(*argv, "master") == 0) { |
| 1378 | int ifindex; |
| 1379 | NEXT_ARG(); |
| 1380 | ifindex = ll_name_to_index(*argv); |
| 1381 | if (!ifindex) |
| 1382 | invarg("Device does not exist\n", *argv); |
| 1383 | filter.master = ifindex; |
Vadim Kochan | 712249d | 2014-12-10 10:11:29 +0200 | [diff] [blame] | 1384 | } else if (do_link && strcmp(*argv, "type") == 0) { |
| 1385 | NEXT_ARG(); |
| 1386 | filter.kind = *argv; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1387 | } else { |
| 1388 | if (strcmp(*argv, "dev") == 0) { |
| 1389 | NEXT_ARG(); |
| 1390 | } |
| 1391 | if (matches(*argv, "help") == 0) |
| 1392 | usage(); |
| 1393 | if (filter_dev) |
| 1394 | duparg2("dev", *argv); |
| 1395 | filter_dev = *argv; |
| 1396 | } |
| 1397 | argv++; argc--; |
| 1398 | } |
| 1399 | |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1400 | if (filter_dev) { |
| 1401 | filter.ifindex = ll_name_to_index(filter_dev); |
| 1402 | if (filter.ifindex <= 0) { |
| 1403 | fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev); |
| 1404 | return -1; |
| 1405 | } |
| 1406 | } |
| 1407 | |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 1408 | if (action == IPADD_FLUSH) |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1409 | return ipaddr_flush(); |
| 1410 | |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 1411 | if (action == IPADD_SAVE) { |
| 1412 | if (ipadd_save_prep()) |
| 1413 | exit(1); |
| 1414 | |
| 1415 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETADDR) < 0) { |
| 1416 | perror("Cannot send dump request"); |
| 1417 | exit(1); |
| 1418 | } |
| 1419 | |
| 1420 | if (rtnl_dump_filter(&rth, save_nlmsg, stdout) < 0) { |
| 1421 | fprintf(stderr, "Save terminated\n"); |
| 1422 | exit(1); |
| 1423 | } |
| 1424 | |
| 1425 | exit(0); |
| 1426 | } |
| 1427 | |
Roopa Prabhu | 50b9950 | 2014-07-16 07:13:25 -0700 | [diff] [blame] | 1428 | /* |
| 1429 | * If only filter_dev present and none of the other |
| 1430 | * link filters are present, use RTM_GETLINK to get |
| 1431 | * the link device |
| 1432 | */ |
| 1433 | if (filter_dev && filter.group == -1 && do_link == 1) { |
| 1434 | if (iplink_get(0, filter_dev, RTEXT_FILTER_VF) < 0) { |
| 1435 | perror("Cannot send link get request"); |
| 1436 | exit(1); |
| 1437 | } |
| 1438 | exit(0); |
| 1439 | } |
| 1440 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1441 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) { |
| 1442 | perror("Cannot send dump request"); |
| 1443 | exit(1); |
| 1444 | } |
| 1445 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 1446 | if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1447 | fprintf(stderr, "Dump terminated\n"); |
| 1448 | exit(1); |
| 1449 | } |
| 1450 | |
Mike Frysinger | af9d406 | 2012-08-13 08:09:52 -0700 | [diff] [blame] | 1451 | if (filter.family != AF_PACKET) { |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1452 | if (filter.oneline) |
| 1453 | no_link = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1454 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1455 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
| 1456 | perror("Cannot send dump request"); |
| 1457 | exit(1); |
| 1458 | } |
| 1459 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 1460 | if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1461 | fprintf(stderr, "Dump terminated\n"); |
| 1462 | exit(1); |
| 1463 | } |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1464 | |
| 1465 | ipaddr_filter(&linfo, &ainfo); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1468 | for (l = linfo.head; l; l = l->next) { |
Jiri Benc | 5d5cf1b | 2014-10-03 15:25:02 +0200 | [diff] [blame] | 1469 | int res = 0; |
| 1470 | |
| 1471 | if (no_link || (res = print_linkinfo(NULL, &l->h, stdout)) >= 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1472 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 1473 | if (filter.family != AF_PACKET) |
Andreas Henriksson | 5e5055b | 2015-01-23 13:10:33 +0100 | [diff] [blame] | 1474 | print_selected_addrinfo(ifi, |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1475 | ainfo.head, stdout); |
Jiri Benc | 5d5cf1b | 2014-10-03 15:25:02 +0200 | [diff] [blame] | 1476 | if (res > 0 && !do_link && show_stats) |
| 1477 | print_link_stats(stdout, &l->h); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1478 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1479 | } |
Stephen Hemminger | 8d07e5f | 2012-07-13 13:36:07 -0700 | [diff] [blame] | 1480 | fflush(stdout); |
| 1481 | |
| 1482 | free_nlmsg_chain(&ainfo); |
| 1483 | free_nlmsg_chain(&linfo); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1484 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1485 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Sucheta Chakraborty | f89a2a0 | 2014-05-22 09:59:37 -0400 | [diff] [blame] | 1488 | static void |
| 1489 | ipaddr_loop_each_vf(struct rtattr *tb[], int vfnum, int *min, int *max) |
| 1490 | { |
| 1491 | struct rtattr *vflist = tb[IFLA_VFINFO_LIST]; |
| 1492 | struct rtattr *i, *vf[IFLA_VF_MAX+1]; |
| 1493 | struct ifla_vf_rate *vf_rate; |
| 1494 | int rem; |
| 1495 | |
| 1496 | rem = RTA_PAYLOAD(vflist); |
| 1497 | |
| 1498 | for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { |
| 1499 | parse_rtattr_nested(vf, IFLA_VF_MAX, i); |
| 1500 | vf_rate = RTA_DATA(vf[IFLA_VF_RATE]); |
| 1501 | if (vf_rate->vf == vfnum) { |
| 1502 | *min = vf_rate->min_tx_rate; |
| 1503 | *max = vf_rate->max_tx_rate; |
| 1504 | return; |
| 1505 | } |
| 1506 | } |
| 1507 | fprintf(stderr, "Cannot find VF %d\n", vfnum); |
| 1508 | exit(1); |
| 1509 | } |
| 1510 | |
| 1511 | void ipaddr_get_vf_rate(int vfnum, int *min, int *max, int idx) |
| 1512 | { |
| 1513 | struct nlmsg_chain linfo = { NULL, NULL}; |
| 1514 | struct rtattr *tb[IFLA_MAX+1]; |
| 1515 | struct ifinfomsg *ifi; |
| 1516 | struct nlmsg_list *l; |
| 1517 | struct nlmsghdr *n; |
| 1518 | int len; |
| 1519 | |
| 1520 | if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) { |
| 1521 | perror("Cannot send dump request"); |
| 1522 | exit(1); |
| 1523 | } |
| 1524 | if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) { |
| 1525 | fprintf(stderr, "Dump terminated\n"); |
| 1526 | exit(1); |
| 1527 | } |
| 1528 | for (l = linfo.head; l; l = l->next) { |
| 1529 | n = &l->h; |
| 1530 | ifi = NLMSG_DATA(n); |
| 1531 | |
| 1532 | len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)); |
Stephen Hemminger | 1199c4f | 2014-07-14 12:06:52 -0700 | [diff] [blame] | 1533 | if (len < 0 || (idx && idx != ifi->ifi_index)) |
Sucheta Chakraborty | f89a2a0 | 2014-05-22 09:59:37 -0400 | [diff] [blame] | 1534 | continue; |
| 1535 | |
| 1536 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 1537 | |
| 1538 | if ((tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF])) { |
| 1539 | ipaddr_loop_each_vf(tb, vfnum, min, max); |
| 1540 | return; |
| 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1545 | int ipaddr_list_link(int argc, char **argv) |
| 1546 | { |
| 1547 | preferred_family = AF_PACKET; |
| 1548 | do_link = 1; |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 1549 | return ipaddr_list_flush_or_save(argc, argv, IPADD_LIST); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 1552 | void ipaddr_reset_filter(int oneline, int ifindex) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1553 | { |
| 1554 | memset(&filter, 0, sizeof(filter)); |
| 1555 | filter.oneline = oneline; |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 1556 | filter.ifindex = ifindex; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 1559 | static int default_scope(inet_prefix *lcl) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1560 | { |
| 1561 | if (lcl->family == AF_INET) { |
| 1562 | if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127) |
| 1563 | return RT_SCOPE_HOST; |
| 1564 | } |
| 1565 | return 0; |
| 1566 | } |
| 1567 | |
Madhu Challa | e31867a | 2015-03-04 10:30:10 -0800 | [diff] [blame^] | 1568 | static bool ipaddr_is_multicast(inet_prefix *a) |
| 1569 | { |
| 1570 | if (a->family == AF_INET) |
| 1571 | return IN_MULTICAST(ntohl(a->data[0])); |
| 1572 | else if (a->family == AF_INET6) |
| 1573 | return IN6_IS_ADDR_MULTICAST(a->data); |
| 1574 | else |
| 1575 | return false; |
| 1576 | } |
| 1577 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 1578 | static int ipaddr_modify(int cmd, int flags, int argc, char **argv) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1579 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1580 | struct { |
Stephen Hemminger | 4806867 | 2014-02-17 10:56:31 -0800 | [diff] [blame] | 1581 | struct nlmsghdr n; |
| 1582 | struct ifaddrmsg ifa; |
Stephen Hemminger | 4b726cb | 2014-05-09 12:36:46 -0700 | [diff] [blame] | 1583 | char buf[256]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1584 | } req; |
| 1585 | char *d = NULL; |
| 1586 | char *l = NULL; |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 1587 | char *lcl_arg = NULL; |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 1588 | char *valid_lftp = NULL; |
| 1589 | char *preferred_lftp = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1590 | inet_prefix lcl; |
| 1591 | inet_prefix peer; |
| 1592 | int local_len = 0; |
| 1593 | int peer_len = 0; |
| 1594 | int brd_len = 0; |
| 1595 | int any_len = 0; |
| 1596 | int scoped = 0; |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 1597 | __u32 preferred_lft = INFINITY_LIFE_TIME; |
| 1598 | __u32 valid_lft = INFINITY_LIFE_TIME; |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 1599 | struct ifa_cacheinfo cinfo; |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 1600 | unsigned int ifa_flags = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1601 | |
| 1602 | memset(&req, 0, sizeof(req)); |
| 1603 | |
| 1604 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 1605 | req.n.nlmsg_flags = NLM_F_REQUEST | flags; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1606 | req.n.nlmsg_type = cmd; |
| 1607 | req.ifa.ifa_family = preferred_family; |
| 1608 | |
| 1609 | while (argc > 0) { |
| 1610 | if (strcmp(*argv, "peer") == 0 || |
| 1611 | strcmp(*argv, "remote") == 0) { |
| 1612 | NEXT_ARG(); |
| 1613 | |
| 1614 | if (peer_len) |
| 1615 | duparg("peer", *argv); |
| 1616 | get_prefix(&peer, *argv, req.ifa.ifa_family); |
| 1617 | peer_len = peer.bytelen; |
| 1618 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 1619 | req.ifa.ifa_family = peer.family; |
| 1620 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen); |
| 1621 | req.ifa.ifa_prefixlen = peer.bitlen; |
| 1622 | } else if (matches(*argv, "broadcast") == 0 || |
| 1623 | strcmp(*argv, "brd") == 0) { |
| 1624 | inet_prefix addr; |
| 1625 | NEXT_ARG(); |
| 1626 | if (brd_len) |
| 1627 | duparg("broadcast", *argv); |
| 1628 | if (strcmp(*argv, "+") == 0) |
| 1629 | brd_len = -1; |
| 1630 | else if (strcmp(*argv, "-") == 0) |
| 1631 | brd_len = -2; |
| 1632 | else { |
| 1633 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 1634 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 1635 | req.ifa.ifa_family = addr.family; |
| 1636 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen); |
| 1637 | brd_len = addr.bytelen; |
| 1638 | } |
| 1639 | } else if (strcmp(*argv, "anycast") == 0) { |
| 1640 | inet_prefix addr; |
| 1641 | NEXT_ARG(); |
| 1642 | if (any_len) |
| 1643 | duparg("anycast", *argv); |
| 1644 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 1645 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 1646 | req.ifa.ifa_family = addr.family; |
| 1647 | addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen); |
| 1648 | any_len = addr.bytelen; |
| 1649 | } else if (strcmp(*argv, "scope") == 0) { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 1650 | unsigned scope = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1651 | NEXT_ARG(); |
| 1652 | if (rtnl_rtscope_a2n(&scope, *argv)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame] | 1653 | invarg("invalid scope value.", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1654 | req.ifa.ifa_scope = scope; |
| 1655 | scoped = 1; |
| 1656 | } else if (strcmp(*argv, "dev") == 0) { |
| 1657 | NEXT_ARG(); |
| 1658 | d = *argv; |
| 1659 | } else if (strcmp(*argv, "label") == 0) { |
| 1660 | NEXT_ARG(); |
| 1661 | l = *argv; |
| 1662 | addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1); |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 1663 | } else if (matches(*argv, "valid_lft") == 0) { |
| 1664 | if (valid_lftp) |
| 1665 | duparg("valid_lft", *argv); |
| 1666 | NEXT_ARG(); |
| 1667 | valid_lftp = *argv; |
| 1668 | if (set_lifetime(&valid_lft, *argv)) |
| 1669 | invarg("valid_lft value", *argv); |
| 1670 | } else if (matches(*argv, "preferred_lft") == 0) { |
| 1671 | if (preferred_lftp) |
| 1672 | duparg("preferred_lft", *argv); |
| 1673 | NEXT_ARG(); |
| 1674 | preferred_lftp = *argv; |
| 1675 | if (set_lifetime(&preferred_lft, *argv)) |
| 1676 | invarg("preferred_lft value", *argv); |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 1677 | } else if (strcmp(*argv, "home") == 0) { |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 1678 | ifa_flags |= IFA_F_HOMEADDRESS; |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 1679 | } else if (strcmp(*argv, "nodad") == 0) { |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 1680 | ifa_flags |= IFA_F_NODAD; |
Jiri Pirko | 5b7e21c | 2014-01-06 10:17:10 +0100 | [diff] [blame] | 1681 | } else if (strcmp(*argv, "mngtmpaddr") == 0) { |
| 1682 | ifa_flags |= IFA_F_MANAGETEMPADDR; |
Thomas Haller | 58c69b2 | 2014-01-07 21:46:28 +0100 | [diff] [blame] | 1683 | } else if (strcmp(*argv, "noprefixroute") == 0) { |
| 1684 | ifa_flags |= IFA_F_NOPREFIXROUTE; |
Madhu Challa | e31867a | 2015-03-04 10:30:10 -0800 | [diff] [blame^] | 1685 | } else if (strcmp(*argv, "autojoin") == 0) { |
| 1686 | ifa_flags |= IFA_F_MCAUTOJOIN; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1687 | } else { |
| 1688 | if (strcmp(*argv, "local") == 0) { |
| 1689 | NEXT_ARG(); |
| 1690 | } |
| 1691 | if (matches(*argv, "help") == 0) |
| 1692 | usage(); |
| 1693 | if (local_len) |
| 1694 | duparg2("local", *argv); |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 1695 | lcl_arg = *argv; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1696 | get_prefix(&lcl, *argv, req.ifa.ifa_family); |
| 1697 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 1698 | req.ifa.ifa_family = lcl.family; |
| 1699 | addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen); |
| 1700 | local_len = lcl.bytelen; |
| 1701 | } |
| 1702 | argc--; argv++; |
| 1703 | } |
WANG Cong | 8b21f88 | 2014-04-04 10:07:20 -0700 | [diff] [blame] | 1704 | if (ifa_flags <= 0xff) |
| 1705 | req.ifa.ifa_flags = ifa_flags; |
| 1706 | else |
| 1707 | addattr32(&req.n, sizeof(req), IFA_FLAGS, ifa_flags); |
Jiri Pirko | 37c9b94 | 2014-01-06 10:17:09 +0100 | [diff] [blame] | 1708 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1709 | if (d == NULL) { |
| 1710 | fprintf(stderr, "Not enough information: \"dev\" argument is required.\n"); |
| 1711 | return -1; |
| 1712 | } |
| 1713 | if (l && matches(d, l) != 0) { |
| 1714 | fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l); |
Michele Petrazzo - Unipex | 1db61e0 | 2010-03-06 08:56:53 +0000 | [diff] [blame] | 1715 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 1718 | if (peer_len == 0 && local_len) { |
| 1719 | if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) { |
| 1720 | fprintf(stderr, |
| 1721 | "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \ |
| 1722 | " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \ |
| 1723 | " This special behaviour is likely to disappear in further releases,\n" \ |
| 1724 | " fix your scripts!\n", lcl_arg, local_len*8); |
| 1725 | } else { |
| 1726 | peer = lcl; |
| 1727 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen); |
| 1728 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1729 | } |
| 1730 | if (req.ifa.ifa_prefixlen == 0) |
| 1731 | req.ifa.ifa_prefixlen = lcl.bitlen; |
| 1732 | |
| 1733 | if (brd_len < 0 && cmd != RTM_DELADDR) { |
| 1734 | inet_prefix brd; |
| 1735 | int i; |
| 1736 | if (req.ifa.ifa_family != AF_INET) { |
| 1737 | fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n"); |
| 1738 | return -1; |
| 1739 | } |
| 1740 | brd = peer; |
| 1741 | if (brd.bitlen <= 30) { |
Stephen Hemminger | 4b726cb | 2014-05-09 12:36:46 -0700 | [diff] [blame] | 1742 | for (i = 31; i >= brd.bitlen; i--) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1743 | if (brd_len == -1) |
| 1744 | brd.data[0] |= htonl(1<<(31-i)); |
| 1745 | else |
| 1746 | brd.data[0] &= ~htonl(1<<(31-i)); |
| 1747 | } |
| 1748 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen); |
| 1749 | brd_len = brd.bytelen; |
| 1750 | } |
| 1751 | } |
| 1752 | if (!scoped && cmd != RTM_DELADDR) |
| 1753 | req.ifa.ifa_scope = default_scope(&lcl); |
| 1754 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1755 | if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { |
| 1756 | fprintf(stderr, "Cannot find device \"%s\"\n", d); |
| 1757 | return -1; |
| 1758 | } |
| 1759 | |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 1760 | if (valid_lftp || preferred_lftp) { |
| 1761 | if (!valid_lft) { |
| 1762 | fprintf(stderr, "valid_lft is zero\n"); |
| 1763 | return -1; |
| 1764 | } |
| 1765 | if (valid_lft < preferred_lft) { |
| 1766 | fprintf(stderr, "preferred_lft is greater than valid_lft\n"); |
| 1767 | return -1; |
| 1768 | } |
| 1769 | |
| 1770 | memset(&cinfo, 0, sizeof(cinfo)); |
| 1771 | cinfo.ifa_prefered = preferred_lft; |
| 1772 | cinfo.ifa_valid = valid_lft; |
| 1773 | addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo, |
| 1774 | sizeof(cinfo)); |
| 1775 | } |
| 1776 | |
Madhu Challa | e31867a | 2015-03-04 10:30:10 -0800 | [diff] [blame^] | 1777 | if ((ifa_flags & IFA_F_MCAUTOJOIN) && !ipaddr_is_multicast(&lcl)) { |
| 1778 | fprintf(stderr, "autojoin needs multicast address\n"); |
| 1779 | return -1; |
| 1780 | } |
| 1781 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 1782 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) |
Michele Petrazzo - Unipex | 1db61e0 | 2010-03-06 08:56:53 +0000 | [diff] [blame] | 1783 | return -2; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1784 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1785 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | int do_ipaddr(int argc, char **argv) |
| 1789 | { |
| 1790 | if (argc < 1) |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 1791 | return ipaddr_list_flush_or_save(0, NULL, IPADD_LIST); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1792 | if (matches(*argv, "add") == 0) |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 1793 | return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); |
| 1794 | if (matches(*argv, "change") == 0 || |
| 1795 | strcmp(*argv, "chg") == 0) |
| 1796 | return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1); |
| 1797 | if (matches(*argv, "replace") == 0) |
| 1798 | return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1799 | if (matches(*argv, "delete") == 0) |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 1800 | return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1801 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 1802 | || matches(*argv, "lst") == 0) |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 1803 | return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_LIST); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1804 | if (matches(*argv, "flush") == 0) |
Pavel Emelyanov | 81824ac | 2012-09-11 19:47:00 +0400 | [diff] [blame] | 1805 | return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_FLUSH); |
| 1806 | if (matches(*argv, "save") == 0) |
| 1807 | return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_SAVE); |
| 1808 | if (matches(*argv, "showdump") == 0) |
| 1809 | return ipaddr_showdump(); |
| 1810 | if (matches(*argv, "restore") == 0) |
| 1811 | return ipaddr_restore(); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1812 | if (matches(*argv, "help") == 0) |
| 1813 | usage(); |
Alexander Wirt | b096fa5 | 2007-10-12 10:56:36 +0200 | [diff] [blame] | 1814 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1815 | exit(-1); |
| 1816 | } |