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