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