blob: 8123da12f7e0a9459e0fdbcbf76b1fb447ecc2be [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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!shemmingeraba5acd2004-04-15 20:56:59 +000011 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -080017#include <inttypes.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000018#include <fcntl.h>
19#include <sys/ioctl.h>
20#include <sys/socket.h>
21#include <sys/ioctl.h>
Stephen Hemminger3d866ba2008-03-14 15:30:03 -070022#include <sys/errno.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000023#include <netinet/in.h>
24#include <arpa/inet.h>
25#include <string.h>
26#include <fnmatch.h>
27
osdl.org!shemmingere5779fb2004-06-09 22:56:28 +000028#include <linux/netdevice.h>
29#include <linux/if_arp.h>
30#include <linux/sockios.h>
31
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000032#include "rt_names.h"
33#include "utils.h"
34#include "ll_map.h"
35#include "ip_common.h"
36
Daniel Silverstone7b3d3662007-10-19 13:32:24 +020037
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000038static struct
39{
40 int ifindex;
41 int family;
42 int oneline;
43 int showqueue;
44 inet_prefix pfx;
45 int scope, scopemask;
46 int flags, flagmask;
47 int up;
48 char *label;
49 int flushed;
50 char *flushb;
51 int flushp;
52 int flushe;
Vlad Dogaruf960c922011-02-02 20:23:40 +020053 int group;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000054} filter;
55
56static int do_link;
57
58static void usage(void) __attribute__((noreturn));
59
60static void usage(void)
61{
62 if (do_link) {
63 iplink_usage();
64 }
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +090065 fprintf(stderr, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n");
Brian Haleya1f27792009-12-03 10:39:36 +000066 fprintf(stderr, " [ CONFFLAG-LIST ]\n");
Masahide NAKAMURA35546df2006-11-24 12:26:55 +090067 fprintf(stderr, " ip addr del IFADDR dev STRING\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000068 fprintf(stderr, " ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
69 fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n");
70 fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n");
71 fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n");
72 fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n");
73 fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
74 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
75 fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n");
Brian Haleya1b9ffc2009-09-14 17:01:43 -040076 fprintf(stderr, " tentative | deprecated | dadfailed | temporary |\n");
Brian Haleya1f27792009-12-03 10:39:36 +000077 fprintf(stderr, " CONFFLAG-LIST ]\n");
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +090078 fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
79 fprintf(stderr, "CONFFLAG := [ home | nodad ]\n");
Masahide NAKAMURA35546df2006-11-24 12:26:55 +090080 fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
81 fprintf(stderr, "LFT := forever | SECONDS\n");
82
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000083 exit(-1);
84}
85
86void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
87{
88 fprintf(fp, "<");
net[shemminger]!shemminger73b49e92005-03-14 18:47:38 +000089 if (flags & IFF_UP && !(flags & IFF_RUNNING))
90 fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000091 flags &= ~IFF_RUNNING;
92#define _PF(f) if (flags&IFF_##f) { \
93 flags &= ~IFF_##f ; \
94 fprintf(fp, #f "%s", flags ? "," : ""); }
95 _PF(LOOPBACK);
96 _PF(BROADCAST);
97 _PF(POINTOPOINT);
98 _PF(MULTICAST);
99 _PF(NOARP);
100 _PF(ALLMULTI);
101 _PF(PROMISC);
102 _PF(MASTER);
103 _PF(SLAVE);
104 _PF(DEBUG);
105 _PF(DYNAMIC);
106 _PF(AUTOMEDIA);
107 _PF(PORTSEL);
108 _PF(NOTRAILERS);
109 _PF(UP);
Thomas Grafdcb283c2007-06-19 16:40:40 -0700110 _PF(LOWER_UP);
111 _PF(DORMANT);
Oliver Hartkopp98f9a1d2009-03-27 11:21:29 -0700112 _PF(ECHO);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000113#undef _PF
114 if (flags)
115 fprintf(fp, "%x", flags);
116 if (mdown)
117 fprintf(fp, ",M-DOWN");
118 fprintf(fp, "> ");
119}
120
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700121static const char *oper_states[] = {
122 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
123 "TESTING", "DORMANT", "UP"
124};
125
126static void print_operstate(FILE *f, __u8 state)
127{
128 if (state >= sizeof(oper_states)/sizeof(oper_states[0]))
129 fprintf(f, "state %#x ", state);
130 else
131 fprintf(f, "state %s ", oper_states[state]);
132}
133
Eric Dumazetf78e3162009-10-22 18:13:21 +0000134static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1])
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000135{
Eric Dumazetf78e3162009-10-22 18:13:21 +0000136 int qlen;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000137
Eric Dumazetf78e3162009-10-22 18:13:21 +0000138 if (tb[IFLA_TXQLEN])
139 qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]);
140 else {
141 struct ifreq ifr;
142 int s = socket(AF_INET, SOCK_STREAM, 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000143
Eric Dumazetf78e3162009-10-22 18:13:21 +0000144 if (s < 0)
145 return;
146
147 memset(&ifr, 0, sizeof(ifr));
148 strcpy(ifr.ifr_name, (char *)RTA_DATA(tb[IFLA_IFNAME]));
149 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
150 fprintf(f, "ioctl(SIOCGIFXQLEN) failed: %s\n", strerror(errno));
151 close(s);
152 return;
153 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000154 close(s);
Eric Dumazetf78e3162009-10-22 18:13:21 +0000155 qlen = ifr.ifr_qlen;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000156 }
Eric Dumazetf78e3162009-10-22 18:13:21 +0000157 if (qlen)
158 fprintf(f, "qlen %d", qlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000159}
160
Patrick McHardy1d934832007-08-22 10:49:01 -0700161static void print_linktype(FILE *fp, struct rtattr *tb)
162{
163 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
164 struct link_util *lu;
165 char *kind;
166
167 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
168
169 if (!linkinfo[IFLA_INFO_KIND])
170 return;
171 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
172
173 fprintf(fp, "%s", _SL_);
174 fprintf(fp, " %s ", kind);
175
176 lu = get_link_kind(kind);
177 if (!lu || !lu->print_opt)
178 return;
179
180 if (1) {
181 struct rtattr *attr[lu->maxattr+1], **data = NULL;
182
183 if (linkinfo[IFLA_INFO_DATA]) {
184 parse_rtattr_nested(attr, lu->maxattr,
185 linkinfo[IFLA_INFO_DATA]);
186 data = attr;
187 }
188 lu->print_opt(lu, fp, data);
189
190 if (linkinfo[IFLA_INFO_XSTATS] && show_stats &&
191 lu->print_xstats)
192 lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]);
193 }
194}
195
Chris Wright3fd86632010-05-18 00:57:00 -0700196static void print_vfinfo(FILE *fp, struct rtattr *vfinfo)
197{
198 struct ifla_vf_mac *vf_mac;
199 struct ifla_vf_vlan *vf_vlan;
200 struct ifla_vf_tx_rate *vf_tx_rate;
Greg Rose7b8179c2011-10-13 20:31:32 +0000201 struct ifla_vf_spoofchk *vf_spoofchk;
Chris Wright3fd86632010-05-18 00:57:00 -0700202 struct rtattr *vf[IFLA_VF_MAX+1];
Greg Rose7b8179c2011-10-13 20:31:32 +0000203 struct rtattr *tmp;
Chris Wright3fd86632010-05-18 00:57:00 -0700204 SPRINT_BUF(b1);
205
206 if (vfinfo->rta_type != IFLA_VF_INFO) {
207 fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type);
208 return;
209 }
210
211 parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo);
212
213 vf_mac = RTA_DATA(vf[IFLA_VF_MAC]);
214 vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
215 vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]);
216
Greg Rose7b8179c2011-10-13 20:31:32 +0000217 /* Check if the spoof checking vf info type is supported by
218 * this kernel.
219 */
220 tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] +
221 vf[IFLA_VF_TX_RATE]->rta_len);
222
223 if (tmp->rta_type != IFLA_VF_SPOOFCHK)
224 vf_spoofchk = NULL;
225 else
226 vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
227
Chris Wright3fd86632010-05-18 00:57:00 -0700228 fprintf(fp, "\n vf %d MAC %s", vf_mac->vf,
229 ll_addr_n2a((unsigned char *)&vf_mac->mac,
230 ETH_ALEN, 0, b1, sizeof(b1)));
231 if (vf_vlan->vlan)
232 fprintf(fp, ", vlan %d", vf_vlan->vlan);
233 if (vf_vlan->qos)
234 fprintf(fp, ", qos %d", vf_vlan->qos);
235 if (vf_tx_rate->rate)
236 fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate);
Greg Rose7b8179c2011-10-13 20:31:32 +0000237 if (vf_spoofchk && vf_spoofchk->setting != -1) {
238 if (vf_spoofchk->setting)
239 fprintf(fp, ", spoof checking on");
240 else
241 fprintf(fp, ", spoof checking off");
242 }
Chris Wright3fd86632010-05-18 00:57:00 -0700243}
244
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800245static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s) {
246 fprintf(fp, "%s", _SL_);
247 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
248 s->rx_compressed ? "compressed" : "", _SL_);
249 fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
250 (uint64_t)s->rx_bytes,
251 (uint64_t)s->rx_packets,
252 (uint64_t)s->rx_errors,
253 (uint64_t)s->rx_dropped,
254 (uint64_t)s->rx_over_errors,
255 (uint64_t)s->multicast);
256 if (s->rx_compressed)
257 fprintf(fp, " %-7"PRIu64"",
258 (uint64_t)s->rx_compressed);
259 if (show_stats > 1) {
260 fprintf(fp, "%s", _SL_);
261 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
262 fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
263 (uint64_t)s->rx_length_errors,
264 (uint64_t)s->rx_crc_errors,
265 (uint64_t)s->rx_frame_errors,
266 (uint64_t)s->rx_fifo_errors,
267 (uint64_t)s->rx_missed_errors);
268 }
269 fprintf(fp, "%s", _SL_);
270 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
271 (uint64_t)s->tx_compressed ? "compressed" : "", _SL_);
272 fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
273 (uint64_t)s->tx_bytes,
274 (uint64_t)s->tx_packets,
275 (uint64_t)s->tx_errors,
276 (uint64_t)s->tx_dropped,
277 (uint64_t)s->tx_carrier_errors,
278 (uint64_t)s->collisions);
279 if (s->tx_compressed)
280 fprintf(fp, " %-7"PRIu64"",
281 (uint64_t)s->tx_compressed);
282 if (show_stats > 1) {
283 fprintf(fp, "%s", _SL_);
284 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
285 fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
286 (uint64_t)s->tx_aborted_errors,
287 (uint64_t)s->tx_fifo_errors,
288 (uint64_t)s->tx_window_errors,
289 (uint64_t)s->tx_heartbeat_errors);
290 }
291}
292
293static void print_link_stats(FILE *fp, const struct rtnl_link_stats *s)
294{
295 fprintf(fp, "%s", _SL_);
296 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
297 s->rx_compressed ? "compressed" : "", _SL_);
298 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
299 s->rx_bytes, s->rx_packets, s->rx_errors,
300 s->rx_dropped, s->rx_over_errors,
301 s->multicast
302 );
303 if (s->rx_compressed)
304 fprintf(fp, " %-7u", s->rx_compressed);
305 if (show_stats > 1) {
306 fprintf(fp, "%s", _SL_);
307 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
308 fprintf(fp, " %-7u %-7u %-7u %-7u %-7u",
309 s->rx_length_errors,
310 s->rx_crc_errors,
311 s->rx_frame_errors,
312 s->rx_fifo_errors,
313 s->rx_missed_errors
314 );
315 }
316 fprintf(fp, "%s", _SL_);
317 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
318 s->tx_compressed ? "compressed" : "", _SL_);
319 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
320 s->tx_bytes, s->tx_packets, s->tx_errors,
321 s->tx_dropped, s->tx_carrier_errors, s->collisions);
322 if (s->tx_compressed)
323 fprintf(fp, " %-7u", s->tx_compressed);
324 if (show_stats > 1) {
325 fprintf(fp, "%s", _SL_);
326 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
327 fprintf(fp, " %-7u %-7u %-7u %-7u",
328 s->tx_aborted_errors,
329 s->tx_fifo_errors,
330 s->tx_window_errors,
331 s->tx_heartbeat_errors
332 );
333 }
334}
335
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800336int print_linkinfo(const struct sockaddr_nl *who,
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000337 struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000338{
339 FILE *fp = (FILE*)arg;
340 struct ifinfomsg *ifi = NLMSG_DATA(n);
341 struct rtattr * tb[IFLA_MAX+1];
342 int len = n->nlmsg_len;
343 unsigned m_flag = 0;
344
345 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
346 return 0;
347
348 len -= NLMSG_LENGTH(sizeof(*ifi));
349 if (len < 0)
350 return -1;
351
352 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
353 return 0;
354 if (filter.up && !(ifi->ifi_flags&IFF_UP))
355 return 0;
356
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000357 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
358 if (tb[IFLA_IFNAME] == NULL) {
jamal4cd23bd2008-08-08 10:06:17 -0400359 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000360 }
361 if (filter.label &&
362 (!filter.family || filter.family == AF_PACKET) &&
363 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
364 return 0;
365
Vlad Dogaruf960c922011-02-02 20:23:40 +0200366 if (tb[IFLA_GROUP]) {
367 int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
368 if (group != filter.group)
369 return -1;
370 }
371
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000372 if (n->nlmsg_type == RTM_DELLINK)
373 fprintf(fp, "Deleted ");
374
375 fprintf(fp, "%d: %s", ifi->ifi_index,
376 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
377
378 if (tb[IFLA_LINK]) {
379 SPRINT_BUF(b1);
380 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
381 if (iflink == 0)
382 fprintf(fp, "@NONE: ");
383 else {
384 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
385 m_flag = ll_index_to_flags(iflink);
386 m_flag = !(m_flag & IFF_UP);
387 }
388 } else {
389 fprintf(fp, ": ");
390 }
391 print_link_flags(fp, ifi->ifi_flags, m_flag);
392
393 if (tb[IFLA_MTU])
394 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
395 if (tb[IFLA_QDISC])
396 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000397 if (tb[IFLA_MASTER]) {
398 SPRINT_BUF(b1);
399 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
400 }
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700401 if (tb[IFLA_OPERSTATE])
402 print_operstate(fp, *(__u8 *)RTA_DATA(tb[IFLA_OPERSTATE]));
403
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000404 if (filter.showqueue)
Eric Dumazetf78e3162009-10-22 18:13:21 +0000405 print_queuelen(fp, tb);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800406
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000407 if (!filter.family || filter.family == AF_PACKET) {
408 SPRINT_BUF(b1);
409 fprintf(fp, "%s", _SL_);
410 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
411
412 if (tb[IFLA_ADDRESS]) {
413 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
414 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
415 ifi->ifi_type,
416 b1, sizeof(b1)));
417 }
418 if (tb[IFLA_BROADCAST]) {
419 if (ifi->ifi_flags&IFF_POINTOPOINT)
420 fprintf(fp, " peer ");
421 else
422 fprintf(fp, " brd ");
423 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
424 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
425 ifi->ifi_type,
426 b1, sizeof(b1)));
427 }
428 }
Patrick McHardy1d934832007-08-22 10:49:01 -0700429
430 if (do_link && tb[IFLA_LINKINFO] && show_details)
431 print_linktype(fp, tb[IFLA_LINKINFO]);
432
Stephen Hemmingerace9c962009-03-23 10:46:47 -0700433 if (do_link && tb[IFLA_IFALIAS])
434 fprintf(fp,"\n alias %s",
435 (const char *) RTA_DATA(tb[IFLA_IFALIAS]));
436
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800437 if (do_link && show_stats) {
438 if (tb[IFLA_STATS64])
439 print_link_stats64(fp, RTA_DATA(tb[IFLA_STATS64]));
440 else if (tb[IFLA_STATS])
441 print_link_stats(fp, RTA_DATA(tb[IFLA_STATS]));
Jan Engelhardt8864ac92010-03-11 10:00:34 +0000442 }
Stephen Hemmingere6e6fb52012-02-21 17:18:59 -0800443
Chris Wright3fd86632010-05-18 00:57:00 -0700444 if (do_link && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) {
445 struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST];
446 int rem = RTA_PAYLOAD(vflist);
447 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
448 print_vfinfo(fp, i);
Williams, Mitch Aae7229d2010-02-10 01:47:08 +0000449 }
Chris Wright3fd86632010-05-18 00:57:00 -0700450
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000451 fprintf(fp, "\n");
452 fflush(fp);
453 return 0;
454}
455
456static int flush_update(void)
457{
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800458 if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
Stephen Hemminger1fb0a992008-01-26 11:08:31 -0800459 perror("Failed to send flush request");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000460 return -1;
461 }
462 filter.flushp = 0;
463 return 0;
464}
465
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900466static int set_lifetime(unsigned int *lifetime, char *argv)
467{
468 if (strcmp(argv, "forever") == 0)
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900469 *lifetime = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900470 else if (get_u32(lifetime, argv, 0))
471 return -1;
472
473 return 0;
474}
475
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800476int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000477 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000478{
479 FILE *fp = (FILE*)arg;
480 struct ifaddrmsg *ifa = NLMSG_DATA(n);
481 int len = n->nlmsg_len;
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800482 int deprecated = 0;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700483 /* Use local copy of ifa_flags to not interfere with filtering code */
484 unsigned int ifa_flags;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000485 struct rtattr * rta_tb[IFA_MAX+1];
486 char abuf[256];
487 SPRINT_BUF(b1);
488
489 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
490 return 0;
491 len -= NLMSG_LENGTH(sizeof(*ifa));
492 if (len < 0) {
493 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
494 return -1;
495 }
496
497 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
498 return 0;
499
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000500 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
501
502 if (!rta_tb[IFA_LOCAL])
503 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
504 if (!rta_tb[IFA_ADDRESS])
505 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
506
507 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
508 return 0;
509 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
510 return 0;
511 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
512 return 0;
513 if (filter.label) {
514 SPRINT_BUF(b1);
515 const char *label;
516 if (rta_tb[IFA_LABEL])
517 label = RTA_DATA(rta_tb[IFA_LABEL]);
518 else
519 label = ll_idx_n2a(ifa->ifa_index, b1);
520 if (fnmatch(filter.label, label, 0) != 0)
521 return 0;
522 }
523 if (filter.pfx.family) {
524 if (rta_tb[IFA_LOCAL]) {
525 inet_prefix dst;
526 memset(&dst, 0, sizeof(dst));
527 dst.family = ifa->ifa_family;
528 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
529 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
530 return 0;
531 }
532 }
533
net[shemminger]!shemminger3eb17312005-02-07 18:28:31 +0000534 if (filter.family && filter.family != ifa->ifa_family)
535 return 0;
536
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000537 if (filter.flushb) {
538 struct nlmsghdr *fn;
539 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
540 if (flush_update())
541 return -1;
542 }
543 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
544 memcpy(fn, n, n->nlmsg_len);
545 fn->nlmsg_type = RTM_DELADDR;
546 fn->nlmsg_flags = NLM_F_REQUEST;
shemminger351efcd2005-09-01 19:21:50 +0000547 fn->nlmsg_seq = ++rth.seq;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000548 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
549 filter.flushed++;
550 if (show_stats < 2)
551 return 0;
552 }
553
554 if (n->nlmsg_type == RTM_DELADDR)
555 fprintf(fp, "Deleted ");
556
557 if (filter.oneline || filter.flushb)
558 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
559 if (ifa->ifa_family == AF_INET)
560 fprintf(fp, " inet ");
561 else if (ifa->ifa_family == AF_INET6)
562 fprintf(fp, " inet6 ");
563 else if (ifa->ifa_family == AF_DECnet)
564 fprintf(fp, " dnet ");
565 else if (ifa->ifa_family == AF_IPX)
566 fprintf(fp, " ipx ");
567 else
568 fprintf(fp, " family %d ", ifa->ifa_family);
569
570 if (rta_tb[IFA_LOCAL]) {
571 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
572 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
573 RTA_DATA(rta_tb[IFA_LOCAL]),
574 abuf, sizeof(abuf)));
575
576 if (rta_tb[IFA_ADDRESS] == NULL ||
577 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
578 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
579 } else {
580 fprintf(fp, " peer %s/%d ",
581 rt_addr_n2a(ifa->ifa_family,
582 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
583 RTA_DATA(rta_tb[IFA_ADDRESS]),
584 abuf, sizeof(abuf)),
585 ifa->ifa_prefixlen);
586 }
587 }
588
589 if (rta_tb[IFA_BROADCAST]) {
590 fprintf(fp, "brd %s ",
591 rt_addr_n2a(ifa->ifa_family,
592 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
593 RTA_DATA(rta_tb[IFA_BROADCAST]),
594 abuf, sizeof(abuf)));
595 }
596 if (rta_tb[IFA_ANYCAST]) {
597 fprintf(fp, "any %s ",
598 rt_addr_n2a(ifa->ifa_family,
599 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
600 RTA_DATA(rta_tb[IFA_ANYCAST]),
601 abuf, sizeof(abuf)));
602 }
603 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700604 ifa_flags = ifa->ifa_flags;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000605 if (ifa->ifa_flags&IFA_F_SECONDARY) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700606 ifa_flags &= ~IFA_F_SECONDARY;
Brian Haleya1b9ffc2009-09-14 17:01:43 -0400607 if (ifa->ifa_family == AF_INET6)
608 fprintf(fp, "temporary ");
609 else
610 fprintf(fp, "secondary ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000611 }
612 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700613 ifa_flags &= ~IFA_F_TENTATIVE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000614 fprintf(fp, "tentative ");
615 }
616 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700617 ifa_flags &= ~IFA_F_DEPRECATED;
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800618 deprecated = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000619 fprintf(fp, "deprecated ");
620 }
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900621 if (ifa->ifa_flags&IFA_F_HOMEADDRESS) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700622 ifa_flags &= ~IFA_F_HOMEADDRESS;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900623 fprintf(fp, "home ");
624 }
625 if (ifa->ifa_flags&IFA_F_NODAD) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700626 ifa_flags &= ~IFA_F_NODAD;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900627 fprintf(fp, "nodad ");
628 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000629 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
630 fprintf(fp, "dynamic ");
631 } else
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700632 ifa_flags &= ~IFA_F_PERMANENT;
Brian Haleyf4af8512009-12-01 15:58:44 -0800633 if (ifa->ifa_flags&IFA_F_DADFAILED) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700634 ifa_flags &= ~IFA_F_DADFAILED;
Brian Haleyf4af8512009-12-01 15:58:44 -0800635 fprintf(fp, "dadfailed ");
636 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700637 if (ifa_flags)
638 fprintf(fp, "flags %02x ", ifa_flags);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000639 if (rta_tb[IFA_LABEL])
640 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
641 if (rta_tb[IFA_CACHEINFO]) {
642 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000643 fprintf(fp, "%s", _SL_);
Andreas Schwabf66efad2010-11-05 23:26:29 +0000644 fprintf(fp, " valid_lft ");
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900645 if (ci->ifa_valid == INFINITY_LIFE_TIME)
Andreas Schwabf66efad2010-11-05 23:26:29 +0000646 fprintf(fp, "forever");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000647 else
Andreas Schwabf66efad2010-11-05 23:26:29 +0000648 fprintf(fp, "%usec", ci->ifa_valid);
649 fprintf(fp, " preferred_lft ");
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900650 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
Andreas Schwabf66efad2010-11-05 23:26:29 +0000651 fprintf(fp, "forever");
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800652 else {
653 if (deprecated)
Andreas Schwabf66efad2010-11-05 23:26:29 +0000654 fprintf(fp, "%dsec", ci->ifa_prefered);
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800655 else
Andreas Schwabf66efad2010-11-05 23:26:29 +0000656 fprintf(fp, "%usec", ci->ifa_prefered);
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800657 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000658 }
659 fprintf(fp, "\n");
660 fflush(fp);
661 return 0;
662}
663
Simon Hormanb49240e2009-12-03 12:08:27 +1100664int print_addrinfo_primary(const struct sockaddr_nl *who, struct nlmsghdr *n,
665 void *arg)
666{
667 struct ifaddrmsg *ifa = NLMSG_DATA(n);
668
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700669 if (ifa->ifa_flags & IFA_F_SECONDARY)
Simon Hormanb49240e2009-12-03 12:08:27 +1100670 return 0;
671
672 return print_addrinfo(who, n, arg);
673}
674
675int print_addrinfo_secondary(const struct sockaddr_nl *who, struct nlmsghdr *n,
676 void *arg)
677{
678 struct ifaddrmsg *ifa = NLMSG_DATA(n);
679
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700680 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
Simon Hormanb49240e2009-12-03 12:08:27 +1100681 return 0;
682
683 return print_addrinfo(who, n, arg);
684}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000685
686struct nlmsg_list
687{
688 struct nlmsg_list *next;
689 struct nlmsghdr h;
690};
691
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700692static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000693{
694 for ( ;ainfo ; ainfo = ainfo->next) {
695 struct nlmsghdr *n = &ainfo->h;
696 struct ifaddrmsg *ifa = NLMSG_DATA(n);
697
698 if (n->nlmsg_type != RTM_NEWADDR)
699 continue;
700
701 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
702 return -1;
703
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800704 if (ifa->ifa_index != ifindex ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000705 (filter.family && filter.family != ifa->ifa_family))
706 continue;
707
708 print_addrinfo(NULL, n, fp);
709 }
710 return 0;
711}
712
713
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800714static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000715 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000716{
717 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
718 struct nlmsg_list *h;
719 struct nlmsg_list **lp;
720
721 h = malloc(n->nlmsg_len+sizeof(void*));
722 if (h == NULL)
723 return -1;
724
725 memcpy(&h->h, n, n->nlmsg_len);
726 h->next = NULL;
727
728 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
729 *lp = h;
730
731 ll_remember_index(who, n, NULL);
732 return 0;
733}
734
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700735static int ipaddr_list_or_flush(int argc, char **argv, int flush)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000736{
737 struct nlmsg_list *linfo = NULL;
738 struct nlmsg_list *ainfo = NULL;
shemminger8ed63ab2005-09-21 19:33:17 +0000739 struct nlmsg_list *l, *n;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000740 char *filter_dev = NULL;
741 int no_link = 0;
742
743 ipaddr_reset_filter(oneline);
744 filter.showqueue = 1;
745
746 if (filter.family == AF_UNSPEC)
747 filter.family = preferred_family;
748
Stephen Hemminger242b8da2011-04-12 14:40:14 -0700749 filter.group = INIT_NETDEV_GROUP;
Vlad Dogaruf960c922011-02-02 20:23:40 +0200750
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000751 if (flush) {
752 if (argc <= 0) {
753 fprintf(stderr, "Flush requires arguments.\n");
Vlad Dogaruf960c922011-02-02 20:23:40 +0200754
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000755 return -1;
756 }
757 if (filter.family == AF_PACKET) {
758 fprintf(stderr, "Cannot flush link addresses.\n");
759 return -1;
760 }
761 }
762
763 while (argc > 0) {
764 if (strcmp(*argv, "to") == 0) {
765 NEXT_ARG();
766 get_prefix(&filter.pfx, *argv, filter.family);
767 if (filter.family == AF_UNSPEC)
768 filter.family = filter.pfx.family;
769 } else if (strcmp(*argv, "scope") == 0) {
shemmingerf332d162005-07-05 22:37:15 +0000770 unsigned scope = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000771 NEXT_ARG();
772 filter.scopemask = -1;
773 if (rtnl_rtscope_a2n(&scope, *argv)) {
774 if (strcmp(*argv, "all") != 0)
775 invarg("invalid \"scope\"\n", *argv);
776 scope = RT_SCOPE_NOWHERE;
777 filter.scopemask = 0;
778 }
779 filter.scope = scope;
780 } else if (strcmp(*argv, "up") == 0) {
781 filter.up = 1;
782 } else if (strcmp(*argv, "dynamic") == 0) {
783 filter.flags &= ~IFA_F_PERMANENT;
784 filter.flagmask |= IFA_F_PERMANENT;
785 } else if (strcmp(*argv, "permanent") == 0) {
786 filter.flags |= IFA_F_PERMANENT;
787 filter.flagmask |= IFA_F_PERMANENT;
Brian Haleya1b9ffc2009-09-14 17:01:43 -0400788 } else if (strcmp(*argv, "secondary") == 0 ||
789 strcmp(*argv, "temporary") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000790 filter.flags |= IFA_F_SECONDARY;
791 filter.flagmask |= IFA_F_SECONDARY;
792 } else if (strcmp(*argv, "primary") == 0) {
793 filter.flags &= ~IFA_F_SECONDARY;
794 filter.flagmask |= IFA_F_SECONDARY;
795 } else if (strcmp(*argv, "tentative") == 0) {
796 filter.flags |= IFA_F_TENTATIVE;
797 filter.flagmask |= IFA_F_TENTATIVE;
798 } else if (strcmp(*argv, "deprecated") == 0) {
799 filter.flags |= IFA_F_DEPRECATED;
800 filter.flagmask |= IFA_F_DEPRECATED;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900801 } else if (strcmp(*argv, "home") == 0) {
802 filter.flags |= IFA_F_HOMEADDRESS;
803 filter.flagmask |= IFA_F_HOMEADDRESS;
804 } else if (strcmp(*argv, "nodad") == 0) {
805 filter.flags |= IFA_F_NODAD;
806 filter.flagmask |= IFA_F_NODAD;
Brian Haleya1f27792009-12-03 10:39:36 +0000807 } else if (strcmp(*argv, "dadfailed") == 0) {
808 filter.flags |= IFA_F_DADFAILED;
809 filter.flagmask |= IFA_F_DADFAILED;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000810 } else if (strcmp(*argv, "label") == 0) {
811 NEXT_ARG();
812 filter.label = *argv;
Vlad Dogaruf960c922011-02-02 20:23:40 +0200813 } else if (strcmp(*argv, "group") == 0) {
814 NEXT_ARG();
815 if (rtnl_group_a2n(&filter.group, *argv))
816 invarg("Invalid \"group\" value\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000817 } else {
818 if (strcmp(*argv, "dev") == 0) {
819 NEXT_ARG();
820 }
821 if (matches(*argv, "help") == 0)
822 usage();
823 if (filter_dev)
824 duparg2("dev", *argv);
825 filter_dev = *argv;
826 }
827 argv++; argc--;
828 }
829
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000830 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
831 perror("Cannot send dump request");
832 exit(1);
833 }
834
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800835 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000836 fprintf(stderr, "Dump terminated\n");
837 exit(1);
838 }
839
840 if (filter_dev) {
841 filter.ifindex = ll_name_to_index(filter_dev);
842 if (filter.ifindex <= 0) {
843 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
844 return -1;
845 }
846 }
847
848 if (flush) {
849 int round = 0;
850 char flushb[4096-512];
851
852 filter.flushb = flushb;
853 filter.flushp = 0;
854 filter.flushe = sizeof(flushb);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000855
Ben Greear64c79562010-12-01 11:13:51 -0800856 while ((max_flush_loops == 0) || (round < max_flush_loops)) {
Simon Hormanb49240e2009-12-03 12:08:27 +1100857 const struct rtnl_dump_filter_arg a[3] = {
858 {
859 .filter = print_addrinfo_secondary,
860 .arg1 = stdout,
Simon Hormanb49240e2009-12-03 12:08:27 +1100861 },
862 {
863 .filter = print_addrinfo_primary,
864 .arg1 = stdout,
Simon Hormanb49240e2009-12-03 12:08:27 +1100865 },
866 {
867 .filter = NULL,
868 .arg1 = NULL,
Simon Hormanb49240e2009-12-03 12:08:27 +1100869 },
870 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000871 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
872 perror("Cannot send dump request");
873 exit(1);
874 }
875 filter.flushed = 0;
Simon Hormanb49240e2009-12-03 12:08:27 +1100876 if (rtnl_dump_filter_l(&rth, a) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000877 fprintf(stderr, "Flush terminated\n");
878 exit(1);
879 }
880 if (filter.flushed == 0) {
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700881flush_done:
Andreas Henrikssonf0b34d22008-08-29 19:52:48 +0200882 if (show_stats) {
883 if (round == 0)
884 printf("Nothing to flush.\n");
885 else
886 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
887 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000888 fflush(stdout);
889 return 0;
890 }
891 round++;
892 if (flush_update() < 0)
shemminger351efcd2005-09-01 19:21:50 +0000893 return 1;
894
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000895 if (show_stats) {
896 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
897 fflush(stdout);
898 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700899
900 /* If we are flushing, and specifying primary, then we
901 * want to flush only a single round. Otherwise, we'll
902 * start flushing secondaries that were promoted to
903 * primaries.
904 */
905 if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY))
906 goto flush_done;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000907 }
Ben Greear64c79562010-12-01 11:13:51 -0800908 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops);
909 fflush(stderr);
Daniel Silverstone7b3d3662007-10-19 13:32:24 +0200910 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000911 }
912
913 if (filter.family != AF_PACKET) {
914 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
915 perror("Cannot send dump request");
916 exit(1);
917 }
918
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800919 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000920 fprintf(stderr, "Dump terminated\n");
921 exit(1);
922 }
923 }
924
925
926 if (filter.family && filter.family != AF_PACKET) {
927 struct nlmsg_list **lp;
928 lp=&linfo;
929
930 if (filter.oneline)
931 no_link = 1;
932
933 while ((l=*lp)!=NULL) {
934 int ok = 0;
935 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
936 struct nlmsg_list *a;
937
938 for (a=ainfo; a; a=a->next) {
939 struct nlmsghdr *n = &a->h;
940 struct ifaddrmsg *ifa = NLMSG_DATA(n);
941
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800942 if (ifa->ifa_index != ifi->ifi_index ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000943 (filter.family && filter.family != ifa->ifa_family))
944 continue;
945 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
946 continue;
947 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
948 continue;
949 if (filter.pfx.family || filter.label) {
950 struct rtattr *tb[IFA_MAX+1];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000951 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
952 if (!tb[IFA_LOCAL])
953 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
954
955 if (filter.pfx.family && tb[IFA_LOCAL]) {
956 inet_prefix dst;
957 memset(&dst, 0, sizeof(dst));
958 dst.family = ifa->ifa_family;
959 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
960 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
961 continue;
962 }
963 if (filter.label) {
964 SPRINT_BUF(b1);
965 const char *label;
966 if (tb[IFA_LABEL])
967 label = RTA_DATA(tb[IFA_LABEL]);
968 else
969 label = ll_idx_n2a(ifa->ifa_index, b1);
970 if (fnmatch(filter.label, label, 0) != 0)
971 continue;
972 }
973 }
974
975 ok = 1;
976 break;
977 }
978 if (!ok)
979 *lp = l->next;
980 else
981 lp = &l->next;
982 }
983 }
984
shemminger8ed63ab2005-09-21 19:33:17 +0000985 for (l=linfo; l; l = n) {
986 n = l->next;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000987 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
988 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
989 if (filter.family != AF_PACKET)
990 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
991 }
992 fflush(stdout);
shemminger8ed63ab2005-09-21 19:33:17 +0000993 free(l);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000994 }
995
shemminger351efcd2005-09-01 19:21:50 +0000996 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000997}
998
999int ipaddr_list_link(int argc, char **argv)
1000{
1001 preferred_family = AF_PACKET;
1002 do_link = 1;
1003 return ipaddr_list_or_flush(argc, argv, 0);
1004}
1005
1006void ipaddr_reset_filter(int oneline)
1007{
1008 memset(&filter, 0, sizeof(filter));
1009 filter.oneline = oneline;
1010}
1011
Stephen Hemminger3d866ba2008-03-14 15:30:03 -07001012static int default_scope(inet_prefix *lcl)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001013{
1014 if (lcl->family == AF_INET) {
1015 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
1016 return RT_SCOPE_HOST;
1017 }
1018 return 0;
1019}
1020
Stephen Hemminger3d866ba2008-03-14 15:30:03 -07001021static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001022{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001023 struct {
1024 struct nlmsghdr n;
1025 struct ifaddrmsg ifa;
1026 char buf[256];
1027 } req;
1028 char *d = NULL;
1029 char *l = NULL;
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001030 char *lcl_arg = NULL;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001031 char *valid_lftp = NULL;
1032 char *preferred_lftp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001033 inet_prefix lcl;
1034 inet_prefix peer;
1035 int local_len = 0;
1036 int peer_len = 0;
1037 int brd_len = 0;
1038 int any_len = 0;
1039 int scoped = 0;
Masahide NAKAMURA141bb602006-11-24 12:27:01 +09001040 __u32 preferred_lft = INFINITY_LIFE_TIME;
1041 __u32 valid_lft = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001042 struct ifa_cacheinfo cinfo;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001043
1044 memset(&req, 0, sizeof(req));
1045
1046 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001047 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001048 req.n.nlmsg_type = cmd;
1049 req.ifa.ifa_family = preferred_family;
1050
1051 while (argc > 0) {
1052 if (strcmp(*argv, "peer") == 0 ||
1053 strcmp(*argv, "remote") == 0) {
1054 NEXT_ARG();
1055
1056 if (peer_len)
1057 duparg("peer", *argv);
1058 get_prefix(&peer, *argv, req.ifa.ifa_family);
1059 peer_len = peer.bytelen;
1060 if (req.ifa.ifa_family == AF_UNSPEC)
1061 req.ifa.ifa_family = peer.family;
1062 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
1063 req.ifa.ifa_prefixlen = peer.bitlen;
1064 } else if (matches(*argv, "broadcast") == 0 ||
1065 strcmp(*argv, "brd") == 0) {
1066 inet_prefix addr;
1067 NEXT_ARG();
1068 if (brd_len)
1069 duparg("broadcast", *argv);
1070 if (strcmp(*argv, "+") == 0)
1071 brd_len = -1;
1072 else if (strcmp(*argv, "-") == 0)
1073 brd_len = -2;
1074 else {
1075 get_addr(&addr, *argv, req.ifa.ifa_family);
1076 if (req.ifa.ifa_family == AF_UNSPEC)
1077 req.ifa.ifa_family = addr.family;
1078 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
1079 brd_len = addr.bytelen;
1080 }
1081 } else if (strcmp(*argv, "anycast") == 0) {
1082 inet_prefix addr;
1083 NEXT_ARG();
1084 if (any_len)
1085 duparg("anycast", *argv);
1086 get_addr(&addr, *argv, req.ifa.ifa_family);
1087 if (req.ifa.ifa_family == AF_UNSPEC)
1088 req.ifa.ifa_family = addr.family;
1089 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
1090 any_len = addr.bytelen;
1091 } else if (strcmp(*argv, "scope") == 0) {
shemmingerf332d162005-07-05 22:37:15 +00001092 unsigned scope = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001093 NEXT_ARG();
1094 if (rtnl_rtscope_a2n(&scope, *argv))
1095 invarg(*argv, "invalid scope value.");
1096 req.ifa.ifa_scope = scope;
1097 scoped = 1;
1098 } else if (strcmp(*argv, "dev") == 0) {
1099 NEXT_ARG();
1100 d = *argv;
1101 } else if (strcmp(*argv, "label") == 0) {
1102 NEXT_ARG();
1103 l = *argv;
1104 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001105 } else if (matches(*argv, "valid_lft") == 0) {
1106 if (valid_lftp)
1107 duparg("valid_lft", *argv);
1108 NEXT_ARG();
1109 valid_lftp = *argv;
1110 if (set_lifetime(&valid_lft, *argv))
1111 invarg("valid_lft value", *argv);
1112 } else if (matches(*argv, "preferred_lft") == 0) {
1113 if (preferred_lftp)
1114 duparg("preferred_lft", *argv);
1115 NEXT_ARG();
1116 preferred_lftp = *argv;
1117 if (set_lifetime(&preferred_lft, *argv))
1118 invarg("preferred_lft value", *argv);
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +09001119 } else if (strcmp(*argv, "home") == 0) {
1120 req.ifa.ifa_flags |= IFA_F_HOMEADDRESS;
1121 } else if (strcmp(*argv, "nodad") == 0) {
1122 req.ifa.ifa_flags |= IFA_F_NODAD;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001123 } else {
1124 if (strcmp(*argv, "local") == 0) {
1125 NEXT_ARG();
1126 }
1127 if (matches(*argv, "help") == 0)
1128 usage();
1129 if (local_len)
1130 duparg2("local", *argv);
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001131 lcl_arg = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001132 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1133 if (req.ifa.ifa_family == AF_UNSPEC)
1134 req.ifa.ifa_family = lcl.family;
1135 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1136 local_len = lcl.bytelen;
1137 }
1138 argc--; argv++;
1139 }
1140 if (d == NULL) {
1141 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1142 return -1;
1143 }
1144 if (l && matches(d, l) != 0) {
1145 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
Michele Petrazzo - Unipex1db61e02010-03-06 08:56:53 +00001146 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001147 }
1148
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001149 if (peer_len == 0 && local_len) {
1150 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
1151 fprintf(stderr,
1152 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1153 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1154 " This special behaviour is likely to disappear in further releases,\n" \
1155 " fix your scripts!\n", lcl_arg, local_len*8);
1156 } else {
1157 peer = lcl;
1158 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
1159 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001160 }
1161 if (req.ifa.ifa_prefixlen == 0)
1162 req.ifa.ifa_prefixlen = lcl.bitlen;
1163
1164 if (brd_len < 0 && cmd != RTM_DELADDR) {
1165 inet_prefix brd;
1166 int i;
1167 if (req.ifa.ifa_family != AF_INET) {
1168 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
1169 return -1;
1170 }
1171 brd = peer;
1172 if (brd.bitlen <= 30) {
1173 for (i=31; i>=brd.bitlen; i--) {
1174 if (brd_len == -1)
1175 brd.data[0] |= htonl(1<<(31-i));
1176 else
1177 brd.data[0] &= ~htonl(1<<(31-i));
1178 }
1179 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
1180 brd_len = brd.bytelen;
1181 }
1182 }
1183 if (!scoped && cmd != RTM_DELADDR)
1184 req.ifa.ifa_scope = default_scope(&lcl);
1185
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001186 ll_init_map(&rth);
1187
1188 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
1189 fprintf(stderr, "Cannot find device \"%s\"\n", d);
1190 return -1;
1191 }
1192
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001193 if (valid_lftp || preferred_lftp) {
1194 if (!valid_lft) {
1195 fprintf(stderr, "valid_lft is zero\n");
1196 return -1;
1197 }
1198 if (valid_lft < preferred_lft) {
1199 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
1200 return -1;
1201 }
1202
1203 memset(&cinfo, 0, sizeof(cinfo));
1204 cinfo.ifa_prefered = preferred_lft;
1205 cinfo.ifa_valid = valid_lft;
1206 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
1207 sizeof(cinfo));
1208 }
1209
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -08001210 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
Michele Petrazzo - Unipex1db61e02010-03-06 08:56:53 +00001211 return -2;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001212
shemminger351efcd2005-09-01 19:21:50 +00001213 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001214}
1215
1216int do_ipaddr(int argc, char **argv)
1217{
1218 if (argc < 1)
1219 return ipaddr_list_or_flush(0, NULL, 0);
1220 if (matches(*argv, "add") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001221 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
1222 if (matches(*argv, "change") == 0 ||
1223 strcmp(*argv, "chg") == 0)
1224 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
1225 if (matches(*argv, "replace") == 0)
1226 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001227 if (matches(*argv, "delete") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001228 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001229 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1230 || matches(*argv, "lst") == 0)
1231 return ipaddr_list_or_flush(argc-1, argv+1, 0);
1232 if (matches(*argv, "flush") == 0)
1233 return ipaddr_list_or_flush(argc-1, argv+1, 1);
1234 if (matches(*argv, "help") == 0)
1235 usage();
Alexander Wirtb096fa52007-10-12 10:56:36 +02001236 fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001237 exit(-1);
1238}
1239