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