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