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