blob: e9256d91d271bd4efdd74c1269c874c7edb3d204 [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>
17#include <fcntl.h>
18#include <sys/ioctl.h>
19#include <sys/socket.h>
20#include <sys/ioctl.h>
Stephen Hemminger3d866ba2008-03-14 15:30:03 -070021#include <sys/errno.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000022#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <string.h>
25#include <fnmatch.h>
26
osdl.org!shemmingere5779fb2004-06-09 22:56:28 +000027#include <linux/netdevice.h>
28#include <linux/if_arp.h>
29#include <linux/sockios.h>
30
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000031#include "rt_names.h"
32#include "utils.h"
33#include "ll_map.h"
34#include "ip_common.h"
35
Daniel Silverstone7b3d3662007-10-19 13:32:24 +020036#define MAX_ROUNDS 10
37
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;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000053} filter;
54
55static int do_link;
56
57static void usage(void) __attribute__((noreturn));
58
59static void usage(void)
60{
61 if (do_link) {
62 iplink_usage();
63 }
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +090064 fprintf(stderr, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n");
Brian Haleya1f27792009-12-03 10:39:36 +000065 fprintf(stderr, " [ CONFFLAG-LIST ]\n");
Masahide NAKAMURA35546df2006-11-24 12:26:55 +090066 fprintf(stderr, " ip addr del IFADDR dev STRING\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000067 fprintf(stderr, " ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
68 fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n");
69 fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n");
70 fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n");
71 fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n");
72 fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
73 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
74 fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n");
Brian Haleya1b9ffc2009-09-14 17:01:43 -040075 fprintf(stderr, " tentative | deprecated | dadfailed | temporary |\n");
Brian Haleya1f27792009-12-03 10:39:36 +000076 fprintf(stderr, " CONFFLAG-LIST ]\n");
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +090077 fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
78 fprintf(stderr, "CONFFLAG := [ home | nodad ]\n");
Masahide NAKAMURA35546df2006-11-24 12:26:55 +090079 fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
80 fprintf(stderr, "LFT := forever | SECONDS\n");
81
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000082 exit(-1);
83}
84
85void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
86{
87 fprintf(fp, "<");
net[shemminger]!shemminger73b49e92005-03-14 18:47:38 +000088 if (flags & IFF_UP && !(flags & IFF_RUNNING))
89 fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000090 flags &= ~IFF_RUNNING;
91#define _PF(f) if (flags&IFF_##f) { \
92 flags &= ~IFF_##f ; \
93 fprintf(fp, #f "%s", flags ? "," : ""); }
94 _PF(LOOPBACK);
95 _PF(BROADCAST);
96 _PF(POINTOPOINT);
97 _PF(MULTICAST);
98 _PF(NOARP);
99 _PF(ALLMULTI);
100 _PF(PROMISC);
101 _PF(MASTER);
102 _PF(SLAVE);
103 _PF(DEBUG);
104 _PF(DYNAMIC);
105 _PF(AUTOMEDIA);
106 _PF(PORTSEL);
107 _PF(NOTRAILERS);
108 _PF(UP);
Thomas Grafdcb283c2007-06-19 16:40:40 -0700109 _PF(LOWER_UP);
110 _PF(DORMANT);
Oliver Hartkopp98f9a1d2009-03-27 11:21:29 -0700111 _PF(ECHO);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000112#undef _PF
113 if (flags)
114 fprintf(fp, "%x", flags);
115 if (mdown)
116 fprintf(fp, ",M-DOWN");
117 fprintf(fp, "> ");
118}
119
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700120static const char *oper_states[] = {
121 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
122 "TESTING", "DORMANT", "UP"
123};
124
125static void print_operstate(FILE *f, __u8 state)
126{
127 if (state >= sizeof(oper_states)/sizeof(oper_states[0]))
128 fprintf(f, "state %#x ", state);
129 else
130 fprintf(f, "state %s ", oper_states[state]);
131}
132
133static void print_queuelen(FILE *f, const char *name)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000134{
135 struct ifreq ifr;
136 int s;
137
138 s = socket(AF_INET, SOCK_STREAM, 0);
139 if (s < 0)
140 return;
141
142 memset(&ifr, 0, sizeof(ifr));
143 strcpy(ifr.ifr_name, name);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800144 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700145 fprintf(f, "ioctl(SIOCGIFXQLEN) failed: %s\n", strerror(errno));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000146 close(s);
147 return;
148 }
149 close(s);
150
151 if (ifr.ifr_qlen)
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700152 fprintf(f, "qlen %d", ifr.ifr_qlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000153}
154
Patrick McHardy1d934832007-08-22 10:49:01 -0700155static void print_linktype(FILE *fp, struct rtattr *tb)
156{
157 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
158 struct link_util *lu;
159 char *kind;
160
161 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
162
163 if (!linkinfo[IFLA_INFO_KIND])
164 return;
165 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
166
167 fprintf(fp, "%s", _SL_);
168 fprintf(fp, " %s ", kind);
169
170 lu = get_link_kind(kind);
171 if (!lu || !lu->print_opt)
172 return;
173
174 if (1) {
175 struct rtattr *attr[lu->maxattr+1], **data = NULL;
176
177 if (linkinfo[IFLA_INFO_DATA]) {
178 parse_rtattr_nested(attr, lu->maxattr,
179 linkinfo[IFLA_INFO_DATA]);
180 data = attr;
181 }
182 lu->print_opt(lu, fp, data);
183
184 if (linkinfo[IFLA_INFO_XSTATS] && show_stats &&
185 lu->print_xstats)
186 lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]);
187 }
188}
189
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800190int print_linkinfo(const struct sockaddr_nl *who,
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000191 struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000192{
193 FILE *fp = (FILE*)arg;
194 struct ifinfomsg *ifi = NLMSG_DATA(n);
195 struct rtattr * tb[IFLA_MAX+1];
196 int len = n->nlmsg_len;
197 unsigned m_flag = 0;
198
199 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
200 return 0;
201
202 len -= NLMSG_LENGTH(sizeof(*ifi));
203 if (len < 0)
204 return -1;
205
206 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
207 return 0;
208 if (filter.up && !(ifi->ifi_flags&IFF_UP))
209 return 0;
210
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000211 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
212 if (tb[IFLA_IFNAME] == NULL) {
jamal4cd23bd2008-08-08 10:06:17 -0400213 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000214 }
215 if (filter.label &&
216 (!filter.family || filter.family == AF_PACKET) &&
217 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
218 return 0;
219
220 if (n->nlmsg_type == RTM_DELLINK)
221 fprintf(fp, "Deleted ");
222
223 fprintf(fp, "%d: %s", ifi->ifi_index,
224 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
225
226 if (tb[IFLA_LINK]) {
227 SPRINT_BUF(b1);
228 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
229 if (iflink == 0)
230 fprintf(fp, "@NONE: ");
231 else {
232 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
233 m_flag = ll_index_to_flags(iflink);
234 m_flag = !(m_flag & IFF_UP);
235 }
236 } else {
237 fprintf(fp, ": ");
238 }
239 print_link_flags(fp, ifi->ifi_flags, m_flag);
240
241 if (tb[IFLA_MTU])
242 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
243 if (tb[IFLA_QDISC])
244 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
245#ifdef IFLA_MASTER
246 if (tb[IFLA_MASTER]) {
247 SPRINT_BUF(b1);
248 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
249 }
250#endif
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700251 if (tb[IFLA_OPERSTATE])
252 print_operstate(fp, *(__u8 *)RTA_DATA(tb[IFLA_OPERSTATE]));
253
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000254 if (filter.showqueue)
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700255 print_queuelen(fp, (char*)RTA_DATA(tb[IFLA_IFNAME]));
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800256
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000257 if (!filter.family || filter.family == AF_PACKET) {
258 SPRINT_BUF(b1);
259 fprintf(fp, "%s", _SL_);
260 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
261
262 if (tb[IFLA_ADDRESS]) {
263 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
264 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
265 ifi->ifi_type,
266 b1, sizeof(b1)));
267 }
268 if (tb[IFLA_BROADCAST]) {
269 if (ifi->ifi_flags&IFF_POINTOPOINT)
270 fprintf(fp, " peer ");
271 else
272 fprintf(fp, " brd ");
273 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
274 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
275 ifi->ifi_type,
276 b1, sizeof(b1)));
277 }
278 }
Patrick McHardy1d934832007-08-22 10:49:01 -0700279
280 if (do_link && tb[IFLA_LINKINFO] && show_details)
281 print_linktype(fp, tb[IFLA_LINKINFO]);
282
Stephen Hemmingerace9c962009-03-23 10:46:47 -0700283 if (do_link && tb[IFLA_IFALIAS])
284 fprintf(fp,"\n alias %s",
285 (const char *) RTA_DATA(tb[IFLA_IFALIAS]));
286
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000287 if (do_link && tb[IFLA_STATS] && show_stats) {
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000288 struct rtnl_link_stats slocal;
289 struct rtnl_link_stats *s = RTA_DATA(tb[IFLA_STATS]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000290 if (((unsigned long)s) & (sizeof(unsigned long)-1)) {
291 memcpy(&slocal, s, sizeof(slocal));
292 s = &slocal;
293 }
294 fprintf(fp, "%s", _SL_);
295 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
296 s->rx_compressed ? "compressed" : "", _SL_);
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000297 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000298 s->rx_bytes, s->rx_packets, s->rx_errors,
299 s->rx_dropped, s->rx_over_errors,
300 s->multicast
301 );
302 if (s->rx_compressed)
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000303 fprintf(fp, " %-7u", s->rx_compressed);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000304 if (show_stats > 1) {
305 fprintf(fp, "%s", _SL_);
306 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000307 fprintf(fp, " %-7u %-7u %-7u %-7u %-7u",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000308 s->rx_length_errors,
309 s->rx_crc_errors,
310 s->rx_frame_errors,
311 s->rx_fifo_errors,
312 s->rx_missed_errors
313 );
314 }
315 fprintf(fp, "%s", _SL_);
316 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
317 s->tx_compressed ? "compressed" : "", _SL_);
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000318 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000319 s->tx_bytes, s->tx_packets, s->tx_errors,
320 s->tx_dropped, s->tx_carrier_errors, s->collisions);
321 if (s->tx_compressed)
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000322 fprintf(fp, " %-7u", s->tx_compressed);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000323 if (show_stats > 1) {
324 fprintf(fp, "%s", _SL_);
325 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000326 fprintf(fp, " %-7u %-7u %-7u %-7u",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000327 s->tx_aborted_errors,
328 s->tx_fifo_errors,
329 s->tx_window_errors,
330 s->tx_heartbeat_errors
331 );
332 }
333 }
334 fprintf(fp, "\n");
335 fflush(fp);
336 return 0;
337}
338
339static int flush_update(void)
340{
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800341 if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
Stephen Hemminger1fb0a992008-01-26 11:08:31 -0800342 perror("Failed to send flush request");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000343 return -1;
344 }
345 filter.flushp = 0;
346 return 0;
347}
348
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900349static int set_lifetime(unsigned int *lifetime, char *argv)
350{
351 if (strcmp(argv, "forever") == 0)
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900352 *lifetime = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900353 else if (get_u32(lifetime, argv, 0))
354 return -1;
355
356 return 0;
357}
358
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800359int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000360 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000361{
362 FILE *fp = (FILE*)arg;
363 struct ifaddrmsg *ifa = NLMSG_DATA(n);
364 int len = n->nlmsg_len;
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800365 int deprecated = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000366 struct rtattr * rta_tb[IFA_MAX+1];
367 char abuf[256];
368 SPRINT_BUF(b1);
369
370 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
371 return 0;
372 len -= NLMSG_LENGTH(sizeof(*ifa));
373 if (len < 0) {
374 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
375 return -1;
376 }
377
378 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
379 return 0;
380
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000381 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
382
383 if (!rta_tb[IFA_LOCAL])
384 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
385 if (!rta_tb[IFA_ADDRESS])
386 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
387
388 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
389 return 0;
390 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
391 return 0;
392 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
393 return 0;
394 if (filter.label) {
395 SPRINT_BUF(b1);
396 const char *label;
397 if (rta_tb[IFA_LABEL])
398 label = RTA_DATA(rta_tb[IFA_LABEL]);
399 else
400 label = ll_idx_n2a(ifa->ifa_index, b1);
401 if (fnmatch(filter.label, label, 0) != 0)
402 return 0;
403 }
404 if (filter.pfx.family) {
405 if (rta_tb[IFA_LOCAL]) {
406 inet_prefix dst;
407 memset(&dst, 0, sizeof(dst));
408 dst.family = ifa->ifa_family;
409 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
410 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
411 return 0;
412 }
413 }
414
net[shemminger]!shemminger3eb17312005-02-07 18:28:31 +0000415 if (filter.family && filter.family != ifa->ifa_family)
416 return 0;
417
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000418 if (filter.flushb) {
419 struct nlmsghdr *fn;
420 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
421 if (flush_update())
422 return -1;
423 }
424 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
425 memcpy(fn, n, n->nlmsg_len);
426 fn->nlmsg_type = RTM_DELADDR;
427 fn->nlmsg_flags = NLM_F_REQUEST;
shemminger351efcd2005-09-01 19:21:50 +0000428 fn->nlmsg_seq = ++rth.seq;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000429 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
430 filter.flushed++;
431 if (show_stats < 2)
432 return 0;
433 }
434
435 if (n->nlmsg_type == RTM_DELADDR)
436 fprintf(fp, "Deleted ");
437
438 if (filter.oneline || filter.flushb)
439 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
440 if (ifa->ifa_family == AF_INET)
441 fprintf(fp, " inet ");
442 else if (ifa->ifa_family == AF_INET6)
443 fprintf(fp, " inet6 ");
444 else if (ifa->ifa_family == AF_DECnet)
445 fprintf(fp, " dnet ");
446 else if (ifa->ifa_family == AF_IPX)
447 fprintf(fp, " ipx ");
448 else
449 fprintf(fp, " family %d ", ifa->ifa_family);
450
451 if (rta_tb[IFA_LOCAL]) {
452 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
453 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
454 RTA_DATA(rta_tb[IFA_LOCAL]),
455 abuf, sizeof(abuf)));
456
457 if (rta_tb[IFA_ADDRESS] == NULL ||
458 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
459 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
460 } else {
461 fprintf(fp, " peer %s/%d ",
462 rt_addr_n2a(ifa->ifa_family,
463 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
464 RTA_DATA(rta_tb[IFA_ADDRESS]),
465 abuf, sizeof(abuf)),
466 ifa->ifa_prefixlen);
467 }
468 }
469
470 if (rta_tb[IFA_BROADCAST]) {
471 fprintf(fp, "brd %s ",
472 rt_addr_n2a(ifa->ifa_family,
473 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
474 RTA_DATA(rta_tb[IFA_BROADCAST]),
475 abuf, sizeof(abuf)));
476 }
477 if (rta_tb[IFA_ANYCAST]) {
478 fprintf(fp, "any %s ",
479 rt_addr_n2a(ifa->ifa_family,
480 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
481 RTA_DATA(rta_tb[IFA_ANYCAST]),
482 abuf, sizeof(abuf)));
483 }
484 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
485 if (ifa->ifa_flags&IFA_F_SECONDARY) {
486 ifa->ifa_flags &= ~IFA_F_SECONDARY;
Brian Haleya1b9ffc2009-09-14 17:01:43 -0400487 if (ifa->ifa_family == AF_INET6)
488 fprintf(fp, "temporary ");
489 else
490 fprintf(fp, "secondary ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000491 }
492 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
493 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
494 fprintf(fp, "tentative ");
495 }
496 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
497 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800498 deprecated = 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000499 fprintf(fp, "deprecated ");
500 }
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900501 if (ifa->ifa_flags&IFA_F_HOMEADDRESS) {
502 ifa->ifa_flags &= ~IFA_F_HOMEADDRESS;
503 fprintf(fp, "home ");
504 }
505 if (ifa->ifa_flags&IFA_F_NODAD) {
506 ifa->ifa_flags &= ~IFA_F_NODAD;
507 fprintf(fp, "nodad ");
508 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000509 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
510 fprintf(fp, "dynamic ");
511 } else
512 ifa->ifa_flags &= ~IFA_F_PERMANENT;
Brian Haleyf4af8512009-12-01 15:58:44 -0800513 if (ifa->ifa_flags&IFA_F_DADFAILED) {
514 ifa->ifa_flags &= ~IFA_F_DADFAILED;
515 fprintf(fp, "dadfailed ");
516 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000517 if (ifa->ifa_flags)
518 fprintf(fp, "flags %02x ", ifa->ifa_flags);
519 if (rta_tb[IFA_LABEL])
520 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
521 if (rta_tb[IFA_CACHEINFO]) {
522 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
523 char buf[128];
524 fprintf(fp, "%s", _SL_);
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900525 if (ci->ifa_valid == INFINITY_LIFE_TIME)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000526 sprintf(buf, "valid_lft forever");
527 else
Masahide NAKAMURA0cc5ebf2006-11-24 12:26:53 +0900528 sprintf(buf, "valid_lft %usec", ci->ifa_valid);
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900529 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000530 sprintf(buf+strlen(buf), " preferred_lft forever");
Benedikt Gollatz037d9502009-01-06 19:36:56 -0800531 else {
532 if (deprecated)
533 sprintf(buf+strlen(buf), " preferred_lft %dsec",
534 ci->ifa_prefered);
535 else
536 sprintf(buf+strlen(buf), " preferred_lft %usec",
537 ci->ifa_prefered);
538 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000539 fprintf(fp, " %s", buf);
540 }
541 fprintf(fp, "\n");
542 fflush(fp);
543 return 0;
544}
545
Simon Hormanb49240e2009-12-03 12:08:27 +1100546int print_addrinfo_primary(const struct sockaddr_nl *who, struct nlmsghdr *n,
547 void *arg)
548{
549 struct ifaddrmsg *ifa = NLMSG_DATA(n);
550
551 if (!ifa->ifa_flags & IFA_F_SECONDARY)
552 return 0;
553
554 return print_addrinfo(who, n, arg);
555}
556
557int print_addrinfo_secondary(const struct sockaddr_nl *who, struct nlmsghdr *n,
558 void *arg)
559{
560 struct ifaddrmsg *ifa = NLMSG_DATA(n);
561
562 if (ifa->ifa_flags & IFA_F_SECONDARY)
563 return 0;
564
565 return print_addrinfo(who, n, arg);
566}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000567
568struct nlmsg_list
569{
570 struct nlmsg_list *next;
571 struct nlmsghdr h;
572};
573
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700574static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000575{
576 for ( ;ainfo ; ainfo = ainfo->next) {
577 struct nlmsghdr *n = &ainfo->h;
578 struct ifaddrmsg *ifa = NLMSG_DATA(n);
579
580 if (n->nlmsg_type != RTM_NEWADDR)
581 continue;
582
583 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
584 return -1;
585
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800586 if (ifa->ifa_index != ifindex ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000587 (filter.family && filter.family != ifa->ifa_family))
588 continue;
589
590 print_addrinfo(NULL, n, fp);
591 }
592 return 0;
593}
594
595
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800596static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000597 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000598{
599 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
600 struct nlmsg_list *h;
601 struct nlmsg_list **lp;
602
603 h = malloc(n->nlmsg_len+sizeof(void*));
604 if (h == NULL)
605 return -1;
606
607 memcpy(&h->h, n, n->nlmsg_len);
608 h->next = NULL;
609
610 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
611 *lp = h;
612
613 ll_remember_index(who, n, NULL);
614 return 0;
615}
616
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700617static int ipaddr_list_or_flush(int argc, char **argv, int flush)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000618{
619 struct nlmsg_list *linfo = NULL;
620 struct nlmsg_list *ainfo = NULL;
shemminger8ed63ab2005-09-21 19:33:17 +0000621 struct nlmsg_list *l, *n;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000622 char *filter_dev = NULL;
623 int no_link = 0;
624
625 ipaddr_reset_filter(oneline);
626 filter.showqueue = 1;
627
628 if (filter.family == AF_UNSPEC)
629 filter.family = preferred_family;
630
631 if (flush) {
632 if (argc <= 0) {
633 fprintf(stderr, "Flush requires arguments.\n");
634 return -1;
635 }
636 if (filter.family == AF_PACKET) {
637 fprintf(stderr, "Cannot flush link addresses.\n");
638 return -1;
639 }
640 }
641
642 while (argc > 0) {
643 if (strcmp(*argv, "to") == 0) {
644 NEXT_ARG();
645 get_prefix(&filter.pfx, *argv, filter.family);
646 if (filter.family == AF_UNSPEC)
647 filter.family = filter.pfx.family;
648 } else if (strcmp(*argv, "scope") == 0) {
shemmingerf332d162005-07-05 22:37:15 +0000649 unsigned scope = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000650 NEXT_ARG();
651 filter.scopemask = -1;
652 if (rtnl_rtscope_a2n(&scope, *argv)) {
653 if (strcmp(*argv, "all") != 0)
654 invarg("invalid \"scope\"\n", *argv);
655 scope = RT_SCOPE_NOWHERE;
656 filter.scopemask = 0;
657 }
658 filter.scope = scope;
659 } else if (strcmp(*argv, "up") == 0) {
660 filter.up = 1;
661 } else if (strcmp(*argv, "dynamic") == 0) {
662 filter.flags &= ~IFA_F_PERMANENT;
663 filter.flagmask |= IFA_F_PERMANENT;
664 } else if (strcmp(*argv, "permanent") == 0) {
665 filter.flags |= IFA_F_PERMANENT;
666 filter.flagmask |= IFA_F_PERMANENT;
Brian Haleya1b9ffc2009-09-14 17:01:43 -0400667 } else if (strcmp(*argv, "secondary") == 0 ||
668 strcmp(*argv, "temporary") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000669 filter.flags |= IFA_F_SECONDARY;
670 filter.flagmask |= IFA_F_SECONDARY;
671 } else if (strcmp(*argv, "primary") == 0) {
672 filter.flags &= ~IFA_F_SECONDARY;
673 filter.flagmask |= IFA_F_SECONDARY;
674 } else if (strcmp(*argv, "tentative") == 0) {
675 filter.flags |= IFA_F_TENTATIVE;
676 filter.flagmask |= IFA_F_TENTATIVE;
677 } else if (strcmp(*argv, "deprecated") == 0) {
678 filter.flags |= IFA_F_DEPRECATED;
679 filter.flagmask |= IFA_F_DEPRECATED;
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900680 } else if (strcmp(*argv, "home") == 0) {
681 filter.flags |= IFA_F_HOMEADDRESS;
682 filter.flagmask |= IFA_F_HOMEADDRESS;
683 } else if (strcmp(*argv, "nodad") == 0) {
684 filter.flags |= IFA_F_NODAD;
685 filter.flagmask |= IFA_F_NODAD;
Brian Haleya1f27792009-12-03 10:39:36 +0000686 } else if (strcmp(*argv, "dadfailed") == 0) {
687 filter.flags |= IFA_F_DADFAILED;
688 filter.flagmask |= IFA_F_DADFAILED;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000689 } else if (strcmp(*argv, "label") == 0) {
690 NEXT_ARG();
691 filter.label = *argv;
692 } else {
693 if (strcmp(*argv, "dev") == 0) {
694 NEXT_ARG();
695 }
696 if (matches(*argv, "help") == 0)
697 usage();
698 if (filter_dev)
699 duparg2("dev", *argv);
700 filter_dev = *argv;
701 }
702 argv++; argc--;
703 }
704
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000705 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
706 perror("Cannot send dump request");
707 exit(1);
708 }
709
710 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
711 fprintf(stderr, "Dump terminated\n");
712 exit(1);
713 }
714
715 if (filter_dev) {
716 filter.ifindex = ll_name_to_index(filter_dev);
717 if (filter.ifindex <= 0) {
718 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
719 return -1;
720 }
721 }
722
723 if (flush) {
724 int round = 0;
725 char flushb[4096-512];
726
727 filter.flushb = flushb;
728 filter.flushp = 0;
729 filter.flushe = sizeof(flushb);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000730
Daniel Silverstone7b3d3662007-10-19 13:32:24 +0200731 while (round < MAX_ROUNDS) {
Simon Hormanb49240e2009-12-03 12:08:27 +1100732 const struct rtnl_dump_filter_arg a[3] = {
733 {
734 .filter = print_addrinfo_secondary,
735 .arg1 = stdout,
736 .junk = NULL,
737 .arg2 = NULL
738 },
739 {
740 .filter = print_addrinfo_primary,
741 .arg1 = stdout,
742 .junk = NULL,
743 .arg2 = NULL
744 },
745 {
746 .filter = NULL,
747 .arg1 = NULL,
748 .junk = NULL,
749 .arg2 = NULL
750 },
751 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000752 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
753 perror("Cannot send dump request");
754 exit(1);
755 }
756 filter.flushed = 0;
Simon Hormanb49240e2009-12-03 12:08:27 +1100757 if (rtnl_dump_filter_l(&rth, a) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000758 fprintf(stderr, "Flush terminated\n");
759 exit(1);
760 }
761 if (filter.flushed == 0) {
Andreas Henrikssonf0b34d22008-08-29 19:52:48 +0200762 if (show_stats) {
763 if (round == 0)
764 printf("Nothing to flush.\n");
765 else
766 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
767 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000768 fflush(stdout);
769 return 0;
770 }
771 round++;
772 if (flush_update() < 0)
shemminger351efcd2005-09-01 19:21:50 +0000773 return 1;
774
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000775 if (show_stats) {
776 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
777 fflush(stdout);
778 }
779 }
Daniel Silverstone7b3d3662007-10-19 13:32:24 +0200780 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", MAX_ROUNDS); fflush(stderr);
781 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000782 }
783
784 if (filter.family != AF_PACKET) {
785 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
786 perror("Cannot send dump request");
787 exit(1);
788 }
789
790 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
791 fprintf(stderr, "Dump terminated\n");
792 exit(1);
793 }
794 }
795
796
797 if (filter.family && filter.family != AF_PACKET) {
798 struct nlmsg_list **lp;
799 lp=&linfo;
800
801 if (filter.oneline)
802 no_link = 1;
803
804 while ((l=*lp)!=NULL) {
805 int ok = 0;
806 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
807 struct nlmsg_list *a;
808
809 for (a=ainfo; a; a=a->next) {
810 struct nlmsghdr *n = &a->h;
811 struct ifaddrmsg *ifa = NLMSG_DATA(n);
812
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800813 if (ifa->ifa_index != ifi->ifi_index ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000814 (filter.family && filter.family != ifa->ifa_family))
815 continue;
816 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
817 continue;
818 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
819 continue;
820 if (filter.pfx.family || filter.label) {
821 struct rtattr *tb[IFA_MAX+1];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000822 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
823 if (!tb[IFA_LOCAL])
824 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
825
826 if (filter.pfx.family && tb[IFA_LOCAL]) {
827 inet_prefix dst;
828 memset(&dst, 0, sizeof(dst));
829 dst.family = ifa->ifa_family;
830 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
831 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
832 continue;
833 }
834 if (filter.label) {
835 SPRINT_BUF(b1);
836 const char *label;
837 if (tb[IFA_LABEL])
838 label = RTA_DATA(tb[IFA_LABEL]);
839 else
840 label = ll_idx_n2a(ifa->ifa_index, b1);
841 if (fnmatch(filter.label, label, 0) != 0)
842 continue;
843 }
844 }
845
846 ok = 1;
847 break;
848 }
849 if (!ok)
850 *lp = l->next;
851 else
852 lp = &l->next;
853 }
854 }
855
shemminger8ed63ab2005-09-21 19:33:17 +0000856 for (l=linfo; l; l = n) {
857 n = l->next;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000858 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
859 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
860 if (filter.family != AF_PACKET)
861 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
862 }
863 fflush(stdout);
shemminger8ed63ab2005-09-21 19:33:17 +0000864 free(l);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000865 }
866
shemminger351efcd2005-09-01 19:21:50 +0000867 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000868}
869
870int ipaddr_list_link(int argc, char **argv)
871{
872 preferred_family = AF_PACKET;
873 do_link = 1;
874 return ipaddr_list_or_flush(argc, argv, 0);
875}
876
877void ipaddr_reset_filter(int oneline)
878{
879 memset(&filter, 0, sizeof(filter));
880 filter.oneline = oneline;
881}
882
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700883static int default_scope(inet_prefix *lcl)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000884{
885 if (lcl->family == AF_INET) {
886 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
887 return RT_SCOPE_HOST;
888 }
889 return 0;
890}
891
Stephen Hemminger3d866ba2008-03-14 15:30:03 -0700892static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000893{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000894 struct {
895 struct nlmsghdr n;
896 struct ifaddrmsg ifa;
897 char buf[256];
898 } req;
899 char *d = NULL;
900 char *l = NULL;
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +0000901 char *lcl_arg = NULL;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900902 char *valid_lftp = NULL;
903 char *preferred_lftp = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000904 inet_prefix lcl;
905 inet_prefix peer;
906 int local_len = 0;
907 int peer_len = 0;
908 int brd_len = 0;
909 int any_len = 0;
910 int scoped = 0;
Masahide NAKAMURA141bb602006-11-24 12:27:01 +0900911 __u32 preferred_lft = INFINITY_LIFE_TIME;
912 __u32 valid_lft = INFINITY_LIFE_TIME;
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900913 struct ifa_cacheinfo cinfo;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000914
915 memset(&req, 0, sizeof(req));
916
917 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +0900918 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000919 req.n.nlmsg_type = cmd;
920 req.ifa.ifa_family = preferred_family;
921
922 while (argc > 0) {
923 if (strcmp(*argv, "peer") == 0 ||
924 strcmp(*argv, "remote") == 0) {
925 NEXT_ARG();
926
927 if (peer_len)
928 duparg("peer", *argv);
929 get_prefix(&peer, *argv, req.ifa.ifa_family);
930 peer_len = peer.bytelen;
931 if (req.ifa.ifa_family == AF_UNSPEC)
932 req.ifa.ifa_family = peer.family;
933 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
934 req.ifa.ifa_prefixlen = peer.bitlen;
935 } else if (matches(*argv, "broadcast") == 0 ||
936 strcmp(*argv, "brd") == 0) {
937 inet_prefix addr;
938 NEXT_ARG();
939 if (brd_len)
940 duparg("broadcast", *argv);
941 if (strcmp(*argv, "+") == 0)
942 brd_len = -1;
943 else if (strcmp(*argv, "-") == 0)
944 brd_len = -2;
945 else {
946 get_addr(&addr, *argv, req.ifa.ifa_family);
947 if (req.ifa.ifa_family == AF_UNSPEC)
948 req.ifa.ifa_family = addr.family;
949 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
950 brd_len = addr.bytelen;
951 }
952 } else if (strcmp(*argv, "anycast") == 0) {
953 inet_prefix addr;
954 NEXT_ARG();
955 if (any_len)
956 duparg("anycast", *argv);
957 get_addr(&addr, *argv, req.ifa.ifa_family);
958 if (req.ifa.ifa_family == AF_UNSPEC)
959 req.ifa.ifa_family = addr.family;
960 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
961 any_len = addr.bytelen;
962 } else if (strcmp(*argv, "scope") == 0) {
shemmingerf332d162005-07-05 22:37:15 +0000963 unsigned scope = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000964 NEXT_ARG();
965 if (rtnl_rtscope_a2n(&scope, *argv))
966 invarg(*argv, "invalid scope value.");
967 req.ifa.ifa_scope = scope;
968 scoped = 1;
969 } else if (strcmp(*argv, "dev") == 0) {
970 NEXT_ARG();
971 d = *argv;
972 } else if (strcmp(*argv, "label") == 0) {
973 NEXT_ARG();
974 l = *argv;
975 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
Masahide NAKAMURA35546df2006-11-24 12:26:55 +0900976 } else if (matches(*argv, "valid_lft") == 0) {
977 if (valid_lftp)
978 duparg("valid_lft", *argv);
979 NEXT_ARG();
980 valid_lftp = *argv;
981 if (set_lifetime(&valid_lft, *argv))
982 invarg("valid_lft value", *argv);
983 } else if (matches(*argv, "preferred_lft") == 0) {
984 if (preferred_lftp)
985 duparg("preferred_lft", *argv);
986 NEXT_ARG();
987 preferred_lftp = *argv;
988 if (set_lifetime(&preferred_lft, *argv))
989 invarg("preferred_lft value", *argv);
Noriaki TAKAMIYAbac735c2007-03-08 03:15:43 +0900990 } else if (strcmp(*argv, "home") == 0) {
991 req.ifa.ifa_flags |= IFA_F_HOMEADDRESS;
992 } else if (strcmp(*argv, "nodad") == 0) {
993 req.ifa.ifa_flags |= IFA_F_NODAD;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000994 } else {
995 if (strcmp(*argv, "local") == 0) {
996 NEXT_ARG();
997 }
998 if (matches(*argv, "help") == 0)
999 usage();
1000 if (local_len)
1001 duparg2("local", *argv);
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001002 lcl_arg = *argv;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001003 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1004 if (req.ifa.ifa_family == AF_UNSPEC)
1005 req.ifa.ifa_family = lcl.family;
1006 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1007 local_len = lcl.bytelen;
1008 }
1009 argc--; argv++;
1010 }
1011 if (d == NULL) {
1012 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1013 return -1;
1014 }
1015 if (l && matches(d, l) != 0) {
1016 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
1017 exit(1);
1018 }
1019
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +00001020 if (peer_len == 0 && local_len) {
1021 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
1022 fprintf(stderr,
1023 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1024 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1025 " This special behaviour is likely to disappear in further releases,\n" \
1026 " fix your scripts!\n", lcl_arg, local_len*8);
1027 } else {
1028 peer = lcl;
1029 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
1030 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001031 }
1032 if (req.ifa.ifa_prefixlen == 0)
1033 req.ifa.ifa_prefixlen = lcl.bitlen;
1034
1035 if (brd_len < 0 && cmd != RTM_DELADDR) {
1036 inet_prefix brd;
1037 int i;
1038 if (req.ifa.ifa_family != AF_INET) {
1039 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
1040 return -1;
1041 }
1042 brd = peer;
1043 if (brd.bitlen <= 30) {
1044 for (i=31; i>=brd.bitlen; i--) {
1045 if (brd_len == -1)
1046 brd.data[0] |= htonl(1<<(31-i));
1047 else
1048 brd.data[0] &= ~htonl(1<<(31-i));
1049 }
1050 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
1051 brd_len = brd.bytelen;
1052 }
1053 }
1054 if (!scoped && cmd != RTM_DELADDR)
1055 req.ifa.ifa_scope = default_scope(&lcl);
1056
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001057 ll_init_map(&rth);
1058
1059 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
1060 fprintf(stderr, "Cannot find device \"%s\"\n", d);
1061 return -1;
1062 }
1063
Masahide NAKAMURA35546df2006-11-24 12:26:55 +09001064 if (valid_lftp || preferred_lftp) {
1065 if (!valid_lft) {
1066 fprintf(stderr, "valid_lft is zero\n");
1067 return -1;
1068 }
1069 if (valid_lft < preferred_lft) {
1070 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
1071 return -1;
1072 }
1073
1074 memset(&cinfo, 0, sizeof(cinfo));
1075 cinfo.ifa_prefered = preferred_lft;
1076 cinfo.ifa_valid = valid_lft;
1077 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
1078 sizeof(cinfo));
1079 }
1080
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001081 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
1082 exit(2);
1083
shemminger351efcd2005-09-01 19:21:50 +00001084 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001085}
1086
1087int do_ipaddr(int argc, char **argv)
1088{
1089 if (argc < 1)
1090 return ipaddr_list_or_flush(0, NULL, 0);
1091 if (matches(*argv, "add") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001092 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
1093 if (matches(*argv, "change") == 0 ||
1094 strcmp(*argv, "chg") == 0)
1095 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
1096 if (matches(*argv, "replace") == 0)
1097 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001098 if (matches(*argv, "delete") == 0)
Noriaki TAKAMIYA0aef3662006-11-24 12:26:58 +09001099 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001100 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1101 || matches(*argv, "lst") == 0)
1102 return ipaddr_list_or_flush(argc-1, argv+1, 0);
1103 if (matches(*argv, "flush") == 0)
1104 return ipaddr_list_or_flush(argc-1, argv+1, 1);
1105 if (matches(*argv, "help") == 0)
1106 usage();
Alexander Wirtb096fa52007-10-12 10:56:36 +02001107 fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001108 exit(-1);
1109}
1110