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