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