blob: 8a5edb9680673cd020ddc3c12d1537f30e160191 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
3 * ipaddress.c "ip address".
4 *
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 *
9 * Changes:
10 * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
11 */
12
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000013#include <fnmatch.h>
Eric Andersen8004bb72003-01-14 08:06:07 +000014#include <net/if.h>
15#include <net/if_arp.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000016
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000017#include "ip_common.h" /* #include "libbb.h" is inside */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018#include "rt_names.h"
19#include "utils.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000020
Denis Vlasenko9b6f4aa2008-06-05 14:01:04 +000021#ifndef IFF_LOWER_UP
22/* from linux/if.h */
23#define IFF_LOWER_UP 0x10000 /* driver signals L1 up*/
24#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000025
Denys Vlasenko36659fd2010-02-05 14:40:23 +010026struct filter_t {
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000027 char *label;
28 char *flushb;
29 struct rtnl_handle *rth;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000030 int scope, scopemask;
31 int flags, flagmask;
Glenn L McGrathd66370c2003-01-13 21:40:38 +000032 int flushp;
33 int flushe;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000034 int ifindex;
35 family_t family;
36 smallint showqueue;
37 smallint oneline;
38 smallint up;
39 smallint flushed;
40 inet_prefix pfx;
Denys Vlasenko36659fd2010-02-05 14:40:23 +010041} FIX_ALIASING;
42typedef struct filter_t filter_t;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000043
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010044#define G_filter (*(filter_t*)&bb_common_bufsiz1)
Denis Vlasenko540a2a12007-04-07 01:14:45 +000045
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000046
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000047static void print_link_flags(unsigned flags, unsigned mdown)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000048{
Denis Vlasenko53354ac2008-06-07 15:10:29 +000049 static const int flag_masks[] = {
50 IFF_LOOPBACK, IFF_BROADCAST, IFF_POINTOPOINT,
51 IFF_MULTICAST, IFF_NOARP, IFF_UP, IFF_LOWER_UP };
52 static const char flag_labels[] ALIGN1 =
53 "LOOPBACK\0""BROADCAST\0""POINTOPOINT\0"
54 "MULTICAST\0""NOARP\0""UP\0""LOWER_UP\0";
55
Denis Vlasenko7d60fc12008-06-05 06:51:06 +000056 bb_putchar('<');
Bernhard Reutner-Fischer49ee8392010-05-25 09:55:42 +020057 if (flags & IFF_UP && !(flags & IFF_RUNNING))
58 printf("NO-CARRIER,");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000059 flags &= ~IFF_RUNNING;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000060#if 0
61 _PF(ALLMULTI);
62 _PF(PROMISC);
63 _PF(MASTER);
64 _PF(SLAVE);
65 _PF(DEBUG);
66 _PF(DYNAMIC);
67 _PF(AUTOMEDIA);
68 _PF(PORTSEL);
69 _PF(NOTRAILERS);
70#endif
Denis Vlasenko53354ac2008-06-07 15:10:29 +000071 flags = print_flags_separated(flag_masks, flag_labels, flags, ",");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000072 if (flags)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000073 printf("%x", flags);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000074 if (mdown)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000075 printf(",M-DOWN");
76 printf("> ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077}
78
Glenn L McGrath2626ef62002-12-02 01:40:05 +000079static void print_queuelen(char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000080{
81 struct ifreq ifr;
82 int s;
83
84 s = socket(AF_INET, SOCK_STREAM, 0);
85 if (s < 0)
86 return;
87
88 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +000089 strncpy_IFNAMSIZ(ifr.ifr_name, name);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000090 if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091 close(s);
92 return;
93 }
94 close(s);
95
96 if (ifr.ifr_qlen)
97 printf("qlen %d", ifr.ifr_qlen);
98}
99
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200100static NOINLINE int print_linkinfo(const struct nlmsghdr *n)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000101{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102 struct ifinfomsg *ifi = NLMSG_DATA(n);
Denys Vlasenko90377872010-01-08 09:07:50 +0100103 struct rtattr *tb[IFLA_MAX+1];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000104 int len = n->nlmsg_len;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105
106 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
107 return 0;
108
109 len -= NLMSG_LENGTH(sizeof(*ifi));
110 if (len < 0)
111 return -1;
112
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100113 if (G_filter.ifindex && ifi->ifi_index != G_filter.ifindex)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000114 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100115 if (G_filter.up && !(ifi->ifi_flags & IFF_UP))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000116 return 0;
117
118 memset(tb, 0, sizeof(tb));
119 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
120 if (tb[IFLA_IFNAME] == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 bb_error_msg("nil ifname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122 return -1;
123 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100124 if (G_filter.label
125 && (!G_filter.family || G_filter.family == AF_PACKET)
126 && fnmatch(G_filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000127 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000128 return 0;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000129 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130
131 if (n->nlmsg_type == RTM_DELLINK)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000132 printf("Deleted ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000134 printf("%d: %s", ifi->ifi_index,
Denys Vlasenko90377872010-01-08 09:07:50 +0100135 /*tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>" - we checked tb[IFLA_IFNAME] above*/
136 (char*)RTA_DATA(tb[IFLA_IFNAME])
137 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138
Denys Vlasenko90377872010-01-08 09:07:50 +0100139 {
140 unsigned m_flag = 0;
141 if (tb[IFLA_LINK]) {
142 SPRINT_BUF(b1);
143 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
144 if (iflink == 0)
145 printf("@NONE: ");
146 else {
147 printf("@%s: ", ll_idx_n2a(iflink, b1));
148 m_flag = ll_index_to_flags(iflink);
149 m_flag = !(m_flag & IFF_UP);
150 }
151 } else {
152 printf(": ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153 }
Denys Vlasenko90377872010-01-08 09:07:50 +0100154 print_link_flags(ifi->ifi_flags, m_flag);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000156
157 if (tb[IFLA_MTU])
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000158 printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000159 if (tb[IFLA_QDISC])
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000160 printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161#ifdef IFLA_MASTER
162 if (tb[IFLA_MASTER]) {
163 SPRINT_BUF(b1);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000164 printf("master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165 }
166#endif
Bernhard Reutner-Fischer49ee8392010-05-25 09:55:42 +0200167 if (tb[IFLA_OPERSTATE]) {
168 static const char operstate_labels[] ALIGN1 =
169 "UNKNOWN\0""NOTPRESENT\0""DOWN\0""LOWERLAYERDOWN\0"
170 "TESTING\0""DORMANT\0""UP\0";
171 printf("state %s ", nth_string(operstate_labels,
172 *(__u8 *)RTA_DATA(tb[IFLA_OPERSTATE])));
173 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100174 if (G_filter.showqueue)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000175 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000176
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100177 if (!G_filter.family || G_filter.family == AF_PACKET) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000178 SPRINT_BUF(b1);
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200179 printf("%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180
181 if (tb[IFLA_ADDRESS]) {
Denis Vlasenko605b20e2007-09-30 16:22:36 +0000182 fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000183 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
184 ifi->ifi_type,
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000185 b1, sizeof(b1)), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000186 }
187 if (tb[IFLA_BROADCAST]) {
Denis Vlasenkodfc07402007-10-29 19:33:26 +0000188 if (ifi->ifi_flags & IFF_POINTOPOINT)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000189 printf(" peer ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000190 else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000191 printf(" brd ");
Denis Vlasenko605b20e2007-09-30 16:22:36 +0000192 fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000193 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
194 ifi->ifi_type,
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000195 b1, sizeof(b1)), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000196 }
197 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000198 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100199 /*fflush_all();*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000200 return 0;
201}
202
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000203static int flush_update(void)
204{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100205 if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100206 bb_perror_msg("can't send flush request");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000207 return -1;
208 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100209 G_filter.flushp = 0;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000210 return 0;
211}
212
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200213static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000214 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000216 struct ifaddrmsg *ifa = NLMSG_DATA(n);
217 int len = n->nlmsg_len;
218 struct rtattr * rta_tb[IFA_MAX+1];
219 char abuf[256];
220 SPRINT_BUF(b1);
221
222 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
223 return 0;
224 len -= NLMSG_LENGTH(sizeof(*ifa));
225 if (len < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000226 bb_error_msg("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000227 return -1;
228 }
229
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100230 if (G_filter.flushb && n->nlmsg_type != RTM_NEWADDR)
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000231 return 0;
232
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000233 memset(rta_tb, 0, sizeof(rta_tb));
234 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
235
236 if (!rta_tb[IFA_LOCAL])
237 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
238 if (!rta_tb[IFA_ADDRESS])
239 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
240
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100241 if (G_filter.ifindex && G_filter.ifindex != ifa->ifa_index)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000242 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100243 if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000244 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100245 if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000246 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100247 if (G_filter.label) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000248 const char *label;
249 if (rta_tb[IFA_LABEL])
250 label = RTA_DATA(rta_tb[IFA_LABEL]);
251 else
252 label = ll_idx_n2a(ifa->ifa_index, b1);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100253 if (fnmatch(G_filter.label, label, 0) != 0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000254 return 0;
255 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100256 if (G_filter.pfx.family) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000257 if (rta_tb[IFA_LOCAL]) {
258 inet_prefix dst;
259 memset(&dst, 0, sizeof(dst));
260 dst.family = ifa->ifa_family;
261 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100262 if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000263 return 0;
264 }
265 }
266
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100267 if (G_filter.flushb) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000268 struct nlmsghdr *fn;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100269 if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000270 if (flush_update())
271 return -1;
272 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100273 fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000274 memcpy(fn, n, n->nlmsg_len);
275 fn->nlmsg_type = RTM_DELADDR;
276 fn->nlmsg_flags = NLM_F_REQUEST;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100277 fn->nlmsg_seq = ++G_filter.rth->seq;
278 G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
279 G_filter.flushed = 1;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000280 return 0;
281 }
282
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000283 if (n->nlmsg_type == RTM_DELADDR)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000284 printf("Deleted ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000285
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100286 if (G_filter.oneline)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000287 printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000288 if (ifa->ifa_family == AF_INET)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000289 printf(" inet ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000290 else if (ifa->ifa_family == AF_INET6)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000291 printf(" inet6 ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000292 else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000293 printf(" family %d ", ifa->ifa_family);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000294
295 if (rta_tb[IFA_LOCAL]) {
Denis Vlasenko605b20e2007-09-30 16:22:36 +0000296 fputs(rt_addr_n2a(ifa->ifa_family,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000297 RTA_DATA(rta_tb[IFA_LOCAL]),
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000298 abuf, sizeof(abuf)), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000299
Denis Vlasenko76140a72009-03-05 09:21:57 +0000300 if (rta_tb[IFA_ADDRESS] == NULL
301 || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0
302 ) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000303 printf("/%d ", ifa->ifa_prefixlen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000304 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000305 printf(" peer %s/%d ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000306 rt_addr_n2a(ifa->ifa_family,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000307 RTA_DATA(rta_tb[IFA_ADDRESS]),
308 abuf, sizeof(abuf)),
309 ifa->ifa_prefixlen);
310 }
311 }
312
313 if (rta_tb[IFA_BROADCAST]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000314 printf("brd %s ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000315 rt_addr_n2a(ifa->ifa_family,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000316 RTA_DATA(rta_tb[IFA_BROADCAST]),
317 abuf, sizeof(abuf)));
318 }
319 if (rta_tb[IFA_ANYCAST]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000320 printf("any %s ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000321 rt_addr_n2a(ifa->ifa_family,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000322 RTA_DATA(rta_tb[IFA_ANYCAST]),
323 abuf, sizeof(abuf)));
324 }
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200325 printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1));
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000326 if (ifa->ifa_flags & IFA_F_SECONDARY) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000327 ifa->ifa_flags &= ~IFA_F_SECONDARY;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000328 printf("secondary ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000329 }
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000330 if (ifa->ifa_flags & IFA_F_TENTATIVE) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000331 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000332 printf("tentative ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000333 }
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000334 if (ifa->ifa_flags & IFA_F_DEPRECATED) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000335 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000336 printf("deprecated ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000337 }
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000338 if (!(ifa->ifa_flags & IFA_F_PERMANENT)) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000339 printf("dynamic ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000340 } else
341 ifa->ifa_flags &= ~IFA_F_PERMANENT;
342 if (ifa->ifa_flags)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000343 printf("flags %02x ", ifa->ifa_flags);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000344 if (rta_tb[IFA_LABEL])
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000345 fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000346 if (rta_tb[IFA_CACHEINFO]) {
347 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
348 char buf[128];
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000349 bb_putchar(_SL_);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000350 if (ci->ifa_valid == 0xFFFFFFFFU)
351 sprintf(buf, "valid_lft forever");
352 else
353 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
354 if (ci->ifa_prefered == 0xFFFFFFFFU)
355 sprintf(buf+strlen(buf), " preferred_lft forever");
356 else
357 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000358 printf(" %s", buf);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000359 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000360 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100361 /*fflush_all();*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000362 return 0;
363}
364
365
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200366struct nlmsg_list {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000367 struct nlmsg_list *next;
368 struct nlmsghdr h;
369};
370
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000371static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000372{
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000373 for (; ainfo; ainfo = ainfo->next) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000374 struct nlmsghdr *n = &ainfo->h;
375 struct ifaddrmsg *ifa = NLMSG_DATA(n);
376
377 if (n->nlmsg_type != RTM_NEWADDR)
378 continue;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000379 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
380 return -1;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100381 if (ifa->ifa_index != ifindex
382 || (G_filter.family && G_filter.family != ifa->ifa_family)
383 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000384 continue;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100385 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000386 print_addrinfo(NULL, n, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000387 }
388 return 0;
389}
390
391
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200392static int FAST_FUNC store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000393{
394 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
395 struct nlmsg_list *h;
396 struct nlmsg_list **lp;
397
Denys Vlasenko90377872010-01-08 09:07:50 +0100398 h = xzalloc(n->nlmsg_len + sizeof(void*));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000399
400 memcpy(&h->h, n, n->nlmsg_len);
Denys Vlasenko90377872010-01-08 09:07:50 +0100401 /*h->next = NULL; - xzalloc did it */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000402
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000403 for (lp = linfo; *lp; lp = &(*lp)->next)
404 continue;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000405 *lp = h;
406
407 ll_remember_index(who, n, NULL);
408 return 0;
409}
410
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000411static void ipaddr_reset_filter(int _oneline)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000412{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100413 memset(&G_filter, 0, sizeof(G_filter));
414 G_filter.oneline = _oneline;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000415}
416
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000417/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000418int ipaddr_list_or_flush(char **argv, int flush)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000419{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000420 static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000421
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000422 struct nlmsg_list *linfo = NULL;
423 struct nlmsg_list *ainfo = NULL;
424 struct nlmsg_list *l;
425 struct rtnl_handle rth;
426 char *filter_dev = NULL;
427 int no_link = 0;
428
429 ipaddr_reset_filter(oneline);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100430 G_filter.showqueue = 1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000431
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100432 if (G_filter.family == AF_UNSPEC)
433 G_filter.family = preferred_family;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000434
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000435 if (flush) {
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000436 if (!*argv) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000437 bb_error_msg_and_die(bb_msg_requires_arg, "flush");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000438 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100439 if (G_filter.family == AF_PACKET) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100440 bb_error_msg_and_die("can't flush link addresses");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000441 }
442 }
443
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000444 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000445 const int option_num = index_in_strings(option, *argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000446 switch (option_num) {
447 case 0: /* to */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000448 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100449 get_prefix(&G_filter.pfx, *argv, G_filter.family);
450 if (G_filter.family == AF_UNSPEC) {
451 G_filter.family = G_filter.pfx.family;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000452 }
453 break;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100454 case 1: { /* scope */
Eric Andersend78aea82006-01-30 18:00:02 +0000455 uint32_t scope = 0;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000456 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100457 G_filter.scopemask = -1;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000458 if (rtnl_rtscope_a2n(&scope, *argv)) {
459 if (strcmp(*argv, "all") != 0) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000460 invarg(*argv, "scope");
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000461 }
462 scope = RT_SCOPE_NOWHERE;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100463 G_filter.scopemask = 0;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000464 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100465 G_filter.scope = scope;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000466 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000467 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000468 case 2: /* up */
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100469 G_filter.up = 1;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000470 break;
471 case 3: /* label */
472 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100473 G_filter.label = *argv;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000474 break;
475 case 4: /* dev */
476 NEXT_ARG();
477 default:
478 if (filter_dev) {
479 duparg2("dev", *argv);
480 }
481 filter_dev = *argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000482 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000483 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000484 }
485
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000486 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000487
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000488 xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
489 xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000490
491 if (filter_dev) {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100492 G_filter.ifindex = xll_name_to_index(filter_dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000493 }
494
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000495 if (flush) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000496 char flushb[4096-512];
497
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100498 G_filter.flushb = flushb;
499 G_filter.flushp = 0;
500 G_filter.flushe = sizeof(flushb);
501 G_filter.rth = &rth;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000502
503 for (;;) {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100504 xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
505 G_filter.flushed = 0;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000506 xrtnl_dump_filter(&rth, print_addrinfo, NULL);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100507 if (G_filter.flushed == 0) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000508 return 0;
509 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100510 if (flush_update() < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000511 return 1;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100512 }
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000513 }
514 }
515
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100516 if (G_filter.family != AF_PACKET) {
517 xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000518 xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000519 }
520
521
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100522 if (G_filter.family && G_filter.family != AF_PACKET) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000523 struct nlmsg_list **lp;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000524 lp = &linfo;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000525
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100526 if (G_filter.oneline)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000527 no_link = 1;
528
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000529 while ((l = *lp) != NULL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000530 int ok = 0;
531 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
532 struct nlmsg_list *a;
533
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000534 for (a = ainfo; a; a = a->next) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000535 struct nlmsghdr *n = &a->h;
536 struct ifaddrmsg *ifa = NLMSG_DATA(n);
537
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100538 if (ifa->ifa_index != ifi->ifi_index
539 || (G_filter.family && G_filter.family != ifa->ifa_family)
540 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000541 continue;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100542 }
543 if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000544 continue;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100545 if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000546 continue;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100547 if (G_filter.pfx.family || G_filter.label) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000548 struct rtattr *tb[IFA_MAX+1];
549 memset(tb, 0, sizeof(tb));
550 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
551 if (!tb[IFA_LOCAL])
552 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
553
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100554 if (G_filter.pfx.family && tb[IFA_LOCAL]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000555 inet_prefix dst;
556 memset(&dst, 0, sizeof(dst));
557 dst.family = ifa->ifa_family;
558 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100559 if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000560 continue;
561 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100562 if (G_filter.label) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000563 SPRINT_BUF(b1);
564 const char *label;
565 if (tb[IFA_LABEL])
566 label = RTA_DATA(tb[IFA_LABEL]);
567 else
568 label = ll_idx_n2a(ifa->ifa_index, b1);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100569 if (fnmatch(G_filter.label, label, 0) != 0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000570 continue;
571 }
572 }
573
574 ok = 1;
575 break;
576 }
577 if (!ok)
578 *lp = l->next;
579 else
580 lp = &l->next;
581 }
582 }
583
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000584 for (l = linfo; l; l = l->next) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000585 if (no_link || print_linkinfo(&l->h) == 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000586 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100587 if (G_filter.family != AF_PACKET)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000588 print_selected_addrinfo(ifi->ifi_index, ainfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000589 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000590 }
591
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000592 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000593}
594
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000595static int default_scope(inet_prefix *lcl)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000596{
597 if (lcl->family == AF_INET) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000598 if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000599 return RT_SCOPE_HOST;
600 }
601 return 0;
602}
603
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000604/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000605static int ipaddr_modify(int cmd, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000606{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000607 static const char option[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000608 "peer\0""remote\0""broadcast\0""brd\0"
609 "anycast\0""scope\0""dev\0""label\0""local\0";
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000610 struct rtnl_handle rth;
611 struct {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000612 struct nlmsghdr n;
613 struct ifaddrmsg ifa;
614 char buf[256];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000615 } req;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000616 char *d = NULL;
617 char *l = NULL;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000618 inet_prefix lcl;
619 inet_prefix peer;
620 int local_len = 0;
621 int peer_len = 0;
622 int brd_len = 0;
623 int any_len = 0;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000624 bool scoped = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000625
626 memset(&req, 0, sizeof(req));
627
628 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
629 req.n.nlmsg_flags = NLM_F_REQUEST;
630 req.n.nlmsg_type = cmd;
631 req.ifa.ifa_family = preferred_family;
632
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000633 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000634 const int option_num = index_in_strings(option, *argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000635 switch (option_num) {
636 case 0: /* peer */
637 case 1: /* remote */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000638 NEXT_ARG();
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000639
640 if (peer_len) {
641 duparg("peer", *argv);
642 }
643 get_prefix(&peer, *argv, req.ifa.ifa_family);
644 peer_len = peer.bytelen;
645 if (req.ifa.ifa_family == AF_UNSPEC) {
646 req.ifa.ifa_family = peer.family;
647 }
648 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
649 req.ifa.ifa_prefixlen = peer.bitlen;
650 break;
651 case 2: /* broadcast */
652 case 3: /* brd */
653 {
654 inet_prefix addr;
655 NEXT_ARG();
656 if (brd_len) {
657 duparg("broadcast", *argv);
658 }
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000659 if (LONE_CHAR(*argv, '+')) {
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000660 brd_len = -1;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000661 } else if (LONE_DASH(*argv)) {
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000662 brd_len = -2;
663 } else {
664 get_addr(&addr, *argv, req.ifa.ifa_family);
665 if (req.ifa.ifa_family == AF_UNSPEC)
666 req.ifa.ifa_family = addr.family;
667 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
668 brd_len = addr.bytelen;
669 }
670 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000671 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000672 case 4: /* anycast */
673 {
674 inet_prefix addr;
675 NEXT_ARG();
676 if (any_len) {
677 duparg("anycast", *argv);
678 }
679 get_addr(&addr, *argv, req.ifa.ifa_family);
680 if (req.ifa.ifa_family == AF_UNSPEC) {
681 req.ifa.ifa_family = addr.family;
682 }
683 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
684 any_len = addr.bytelen;
685 break;
686 }
687 case 5: /* scope */
688 {
Eric Andersend78aea82006-01-30 18:00:02 +0000689 uint32_t scope = 0;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000690 NEXT_ARG();
691 if (rtnl_rtscope_a2n(&scope, *argv)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000692 invarg(*argv, "scope");
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000693 }
694 req.ifa.ifa_scope = scope;
695 scoped = 1;
696 break;
697 }
698 case 6: /* dev */
699 NEXT_ARG();
700 d = *argv;
701 break;
702 case 7: /* label */
703 NEXT_ARG();
704 l = *argv;
705 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
706 break;
707 case 8: /* local */
708 NEXT_ARG();
709 default:
710 if (local_len) {
711 duparg2("local", *argv);
712 }
713 get_prefix(&lcl, *argv, req.ifa.ifa_family);
714 if (req.ifa.ifa_family == AF_UNSPEC) {
715 req.ifa.ifa_family = lcl.family;
716 }
717 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
718 local_len = lcl.bytelen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000719 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000720 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000721 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000722
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000723 if (d == NULL) {
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000724 bb_error_msg(bb_msg_requires_arg, "\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000725 return -1;
726 }
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000727 if (l && strncmp(d, l, strlen(d)) != 0) {
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000728 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000729 }
730
731 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
732 peer = lcl;
733 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
734 }
735 if (req.ifa.ifa_prefixlen == 0)
736 req.ifa.ifa_prefixlen = lcl.bitlen;
737
738 if (brd_len < 0 && cmd != RTM_DELADDR) {
739 inet_prefix brd;
740 int i;
741 if (req.ifa.ifa_family != AF_INET) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000742 bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000743 }
744 brd = peer;
745 if (brd.bitlen <= 30) {
746 for (i=31; i>=brd.bitlen; i--) {
747 if (brd_len == -1)
748 brd.data[0] |= htonl(1<<(31-i));
749 else
750 brd.data[0] &= ~htonl(1<<(31-i));
751 }
752 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
753 brd_len = brd.bytelen;
754 }
755 }
756 if (!scoped && cmd != RTM_DELADDR)
757 req.ifa.ifa_scope = default_scope(&lcl);
758
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000759 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000760
761 ll_init_map(&rth);
762
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000763 req.ifa.ifa_index = xll_name_to_index(d);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000764
765 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000766 return 2;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000767
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000768 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000769}
770
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000771/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000772int do_ipaddr(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000773{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000774 static const char commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000775 "add\0""delete\0""list\0""show\0""lst\0""flush\0";
Bernhard Reutner-Fischercc4493a2010-05-25 17:42:01 +0200776 smalluint cmd = 2;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000777 if (*argv) {
Bernhard Reutner-Fischercc4493a2010-05-25 17:42:01 +0200778 cmd = index_in_substrings(commands, *argv);
779 if (cmd > 5)
780 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000781 argv++;
Bernhard Reutner-Fischercc4493a2010-05-25 17:42:01 +0200782 if (cmd <= 1)
783 return ipaddr_modify((cmd == 0) ? RTM_NEWADDR : RTM_DELADDR, argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000784 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000785 /* 2 == list, 3 == show, 4 == lst */
Bernhard Reutner-Fischercc4493a2010-05-25 17:42:01 +0200786 return ipaddr_list_or_flush(argv, cmd == 5);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000787}