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