blob: 07b27870d87f457de05bee697080d3010cddc82e [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
Denis Vlasenko540a2a12007-04-07 01:14:45 +000026typedef struct 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;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000041} filter_t;
42
43#define filter (*(filter_t*)&bb_common_bufsiz1)
44
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000045
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000046static void print_link_flags(unsigned flags, unsigned mdown)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000047{
Denis Vlasenko7d60fc12008-06-05 06:51:06 +000048 bb_putchar('<');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049 flags &= ~IFF_RUNNING;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000050#define _PF(f) if (flags & IFF_##f) { \
Denis Vlasenkob71c6682007-07-21 15:08:09 +000051 flags &= ~IFF_##f; \
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000052 printf(#f "%s", flags ? "," : ""); }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000053 _PF(LOOPBACK);
54 _PF(BROADCAST);
55 _PF(POINTOPOINT);
56 _PF(MULTICAST);
57 _PF(NOARP);
58#if 0
59 _PF(ALLMULTI);
60 _PF(PROMISC);
61 _PF(MASTER);
62 _PF(SLAVE);
63 _PF(DEBUG);
64 _PF(DYNAMIC);
65 _PF(AUTOMEDIA);
66 _PF(PORTSEL);
67 _PF(NOTRAILERS);
68#endif
69 _PF(UP);
Denis Vlasenko9b6f4aa2008-06-05 14:01:04 +000070 _PF(LOWER_UP);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071#undef _PF
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 Vlasenko229b3d22006-11-27 23:44:57 +000089 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_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
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000100static 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);
103 struct rtattr * tb[IFLA_MAX+1];
104 int len = n->nlmsg_len;
105 unsigned m_flag = 0;
106
107 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
108 return 0;
109
110 len -= NLMSG_LENGTH(sizeof(*ifi));
111 if (len < 0)
112 return -1;
113
114 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
115 return 0;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000116 if (filter.up && !(ifi->ifi_flags & IFF_UP))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000117 return 0;
118
119 memset(tb, 0, sizeof(tb));
120 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
121 if (tb[IFLA_IFNAME] == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 bb_error_msg("nil ifname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000123 return -1;
124 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000125 if (filter.label
126 && (!filter.family || filter.family == AF_PACKET)
127 && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
128 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000129 return 0;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000130 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000131
132 if (n->nlmsg_type == RTM_DELLINK)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000133 printf("Deleted ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000134
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000135 printf("%d: %s", ifi->ifi_index,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
137
138 if (tb[IFLA_LINK]) {
139 SPRINT_BUF(b1);
140 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
141 if (iflink == 0)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000142 printf("@NONE: ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000143 else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000144 printf("@%s: ", ll_idx_n2a(iflink, b1));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145 m_flag = ll_index_to_flags(iflink);
146 m_flag = !(m_flag & IFF_UP);
147 }
148 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000149 printf(": ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000151 print_link_flags(ifi->ifi_flags, m_flag);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000152
153 if (tb[IFLA_MTU])
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000154 printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155 if (tb[IFLA_QDISC])
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000156 printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157#ifdef IFLA_MASTER
158 if (tb[IFLA_MASTER]) {
159 SPRINT_BUF(b1);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000160 printf("master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161 }
162#endif
163 if (filter.showqueue)
164 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000165
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000166 if (!filter.family || filter.family == AF_PACKET) {
167 SPRINT_BUF(b1);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000168 printf("%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000169
170 if (tb[IFLA_ADDRESS]) {
Denis Vlasenko605b20e2007-09-30 16:22:36 +0000171 fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000172 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
173 ifi->ifi_type,
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000174 b1, sizeof(b1)), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000175 }
176 if (tb[IFLA_BROADCAST]) {
Denis Vlasenkodfc07402007-10-29 19:33:26 +0000177 if (ifi->ifi_flags & IFF_POINTOPOINT)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000178 printf(" peer ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000179 else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000180 printf(" brd ");
Denis Vlasenko605b20e2007-09-30 16:22:36 +0000181 fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000182 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
183 ifi->ifi_type,
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000184 b1, sizeof(b1)), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000185 }
186 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000187 bb_putchar('\n');
188 /*fflush(stdout);*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000189 return 0;
190}
191
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000192static int flush_update(void)
193{
194 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000195 bb_perror_msg("failed to send flush request");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000196 return -1;
197 }
198 filter.flushp = 0;
199 return 0;
200}
201
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000202static int print_addrinfo(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
203 struct nlmsghdr *n, void *arg ATTRIBUTE_UNUSED)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000204{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000205 struct ifaddrmsg *ifa = NLMSG_DATA(n);
206 int len = n->nlmsg_len;
207 struct rtattr * rta_tb[IFA_MAX+1];
208 char abuf[256];
209 SPRINT_BUF(b1);
210
211 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
212 return 0;
213 len -= NLMSG_LENGTH(sizeof(*ifa));
214 if (len < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000215 bb_error_msg("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000216 return -1;
217 }
218
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000219 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
220 return 0;
221
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000222 memset(rta_tb, 0, sizeof(rta_tb));
223 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
224
225 if (!rta_tb[IFA_LOCAL])
226 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
227 if (!rta_tb[IFA_ADDRESS])
228 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
229
230 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
231 return 0;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000232 if ((filter.scope ^ ifa->ifa_scope) & filter.scopemask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000233 return 0;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000234 if ((filter.flags ^ ifa->ifa_flags) & filter.flagmask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000235 return 0;
236 if (filter.label) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000237 const char *label;
238 if (rta_tb[IFA_LABEL])
239 label = RTA_DATA(rta_tb[IFA_LABEL]);
240 else
241 label = ll_idx_n2a(ifa->ifa_index, b1);
242 if (fnmatch(filter.label, label, 0) != 0)
243 return 0;
244 }
245 if (filter.pfx.family) {
246 if (rta_tb[IFA_LOCAL]) {
247 inet_prefix dst;
248 memset(&dst, 0, sizeof(dst));
249 dst.family = ifa->ifa_family;
250 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
251 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
252 return 0;
253 }
254 }
255
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000256 if (filter.flushb) {
257 struct nlmsghdr *fn;
258 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
259 if (flush_update())
260 return -1;
261 }
262 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
263 memcpy(fn, n, n->nlmsg_len);
264 fn->nlmsg_type = RTM_DELADDR;
265 fn->nlmsg_flags = NLM_F_REQUEST;
266 fn->nlmsg_seq = ++filter.rth->seq;
267 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000268 filter.flushed = 1;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000269 return 0;
270 }
271
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 if (n->nlmsg_type == RTM_DELADDR)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000273 printf("Deleted ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000274
275 if (filter.oneline)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000276 printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 if (ifa->ifa_family == AF_INET)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000278 printf(" inet ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000279 else if (ifa->ifa_family == AF_INET6)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000280 printf(" inet6 ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281 else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000282 printf(" family %d ", ifa->ifa_family);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000283
284 if (rta_tb[IFA_LOCAL]) {
Denis Vlasenko605b20e2007-09-30 16:22:36 +0000285 fputs(rt_addr_n2a(ifa->ifa_family,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000286 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
287 RTA_DATA(rta_tb[IFA_LOCAL]),
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000288 abuf, sizeof(abuf)), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000289
290 if (rta_tb[IFA_ADDRESS] == NULL ||
291 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000292 printf("/%d ", ifa->ifa_prefixlen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000293 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000294 printf(" peer %s/%d ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000295 rt_addr_n2a(ifa->ifa_family,
296 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
297 RTA_DATA(rta_tb[IFA_ADDRESS]),
298 abuf, sizeof(abuf)),
299 ifa->ifa_prefixlen);
300 }
301 }
302
303 if (rta_tb[IFA_BROADCAST]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000304 printf("brd %s ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000305 rt_addr_n2a(ifa->ifa_family,
306 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
307 RTA_DATA(rta_tb[IFA_BROADCAST]),
308 abuf, sizeof(abuf)));
309 }
310 if (rta_tb[IFA_ANYCAST]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000311 printf("any %s ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000312 rt_addr_n2a(ifa->ifa_family,
313 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
314 RTA_DATA(rta_tb[IFA_ANYCAST]),
315 abuf, sizeof(abuf)));
316 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000317 printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000318 if (ifa->ifa_flags & IFA_F_SECONDARY) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000319 ifa->ifa_flags &= ~IFA_F_SECONDARY;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000320 printf("secondary ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000321 }
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000322 if (ifa->ifa_flags & IFA_F_TENTATIVE) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000323 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000324 printf("tentative ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000325 }
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000326 if (ifa->ifa_flags & IFA_F_DEPRECATED) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000327 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000328 printf("deprecated ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000329 }
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000330 if (!(ifa->ifa_flags & IFA_F_PERMANENT)) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000331 printf("dynamic ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000332 } else
333 ifa->ifa_flags &= ~IFA_F_PERMANENT;
334 if (ifa->ifa_flags)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000335 printf("flags %02x ", ifa->ifa_flags);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000336 if (rta_tb[IFA_LABEL])
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000337 fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000338 if (rta_tb[IFA_CACHEINFO]) {
339 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
340 char buf[128];
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000341 bb_putchar(_SL_);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000342 if (ci->ifa_valid == 0xFFFFFFFFU)
343 sprintf(buf, "valid_lft forever");
344 else
345 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
346 if (ci->ifa_prefered == 0xFFFFFFFFU)
347 sprintf(buf+strlen(buf), " preferred_lft forever");
348 else
349 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000350 printf(" %s", buf);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000351 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000352 bb_putchar('\n');
353 /*fflush(stdout);*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000354 return 0;
355}
356
357
358struct nlmsg_list
359{
360 struct nlmsg_list *next;
361 struct nlmsghdr h;
362};
363
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000364static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000365{
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000366 for (; ainfo; ainfo = ainfo->next) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000367 struct nlmsghdr *n = &ainfo->h;
368 struct ifaddrmsg *ifa = NLMSG_DATA(n);
369
370 if (n->nlmsg_type != RTM_NEWADDR)
371 continue;
372
373 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
374 return -1;
375
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000376 if (ifa->ifa_index != ifindex ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000377 (filter.family && filter.family != ifa->ifa_family))
378 continue;
379
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000380 print_addrinfo(NULL, n, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000381 }
382 return 0;
383}
384
385
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000386static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000387{
388 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
389 struct nlmsg_list *h;
390 struct nlmsg_list **lp;
391
392 h = malloc(n->nlmsg_len+sizeof(void*));
393 if (h == NULL)
394 return -1;
395
396 memcpy(&h->h, n, n->nlmsg_len);
397 h->next = NULL;
398
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000399 for (lp = linfo; *lp; lp = &(*lp)->next)
400 continue;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000401 *lp = h;
402
403 ll_remember_index(who, n, NULL);
404 return 0;
405}
406
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000407static void ipaddr_reset_filter(int _oneline)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000408{
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000409 memset(&filter, 0, sizeof(filter));
410 filter.oneline = _oneline;
411}
412
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000413/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000414int ipaddr_list_or_flush(char **argv, int flush)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000415{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000416 static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000417
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000418 struct nlmsg_list *linfo = NULL;
419 struct nlmsg_list *ainfo = NULL;
420 struct nlmsg_list *l;
421 struct rtnl_handle rth;
422 char *filter_dev = NULL;
423 int no_link = 0;
424
425 ipaddr_reset_filter(oneline);
426 filter.showqueue = 1;
427
428 if (filter.family == AF_UNSPEC)
429 filter.family = preferred_family;
430
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000431 if (flush) {
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000432 if (!*argv) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000433 bb_error_msg_and_die(bb_msg_requires_arg, "flush");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000434 }
435 if (filter.family == AF_PACKET) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000436 bb_error_msg_and_die("cannot flush link addresses");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000437 }
438 }
439
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000440 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000441 const int option_num = index_in_strings(option, *argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000442 switch (option_num) {
443 case 0: /* to */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000444 NEXT_ARG();
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000445 get_prefix(&filter.pfx, *argv, filter.family);
446 if (filter.family == AF_UNSPEC) {
447 filter.family = filter.pfx.family;
448 }
449 break;
450 case 1: /* scope */
451 {
Eric Andersend78aea82006-01-30 18:00:02 +0000452 uint32_t scope = 0;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000453 NEXT_ARG();
454 filter.scopemask = -1;
455 if (rtnl_rtscope_a2n(&scope, *argv)) {
456 if (strcmp(*argv, "all") != 0) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000457 invarg(*argv, "scope");
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000458 }
459 scope = RT_SCOPE_NOWHERE;
460 filter.scopemask = 0;
461 }
462 filter.scope = scope;
463 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000464 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000465 case 2: /* up */
466 filter.up = 1;
467 break;
468 case 3: /* label */
469 NEXT_ARG();
470 filter.label = *argv;
471 break;
472 case 4: /* dev */
473 NEXT_ARG();
474 default:
475 if (filter_dev) {
476 duparg2("dev", *argv);
477 }
478 filter_dev = *argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000479 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000480 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000481 }
482
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000483 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000484
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000485 xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
486 xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000487
488 if (filter_dev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000489 filter.ifindex = xll_name_to_index(filter_dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000490 }
491
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000492 if (flush) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000493 char flushb[4096-512];
494
495 filter.flushb = flushb;
496 filter.flushp = 0;
497 filter.flushe = sizeof(flushb);
498 filter.rth = &rth;
499
500 for (;;) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000501 xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000502 filter.flushed = 0;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000503 xrtnl_dump_filter(&rth, print_addrinfo, NULL);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000504 if (filter.flushed == 0) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000505 return 0;
506 }
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000507 if (flush_update() < 0)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000508 return 1;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000509 }
510 }
511
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000512 if (filter.family != AF_PACKET) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000513 xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
514 xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000515 }
516
517
518 if (filter.family && filter.family != AF_PACKET) {
519 struct nlmsg_list **lp;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000520 lp = &linfo;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000521
522 if (filter.oneline)
523 no_link = 1;
524
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000525 while ((l = *lp) != NULL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000526 int ok = 0;
527 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
528 struct nlmsg_list *a;
529
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000530 for (a = ainfo; a; a = a->next) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000531 struct nlmsghdr *n = &a->h;
532 struct ifaddrmsg *ifa = NLMSG_DATA(n);
533
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000534 if (ifa->ifa_index != ifi->ifi_index ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000535 (filter.family && filter.family != ifa->ifa_family))
536 continue;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000537 if ((filter.scope ^ ifa->ifa_scope) & filter.scopemask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000538 continue;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000539 if ((filter.flags ^ ifa->ifa_flags) & filter.flagmask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000540 continue;
541 if (filter.pfx.family || filter.label) {
542 struct rtattr *tb[IFA_MAX+1];
543 memset(tb, 0, sizeof(tb));
544 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
545 if (!tb[IFA_LOCAL])
546 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
547
548 if (filter.pfx.family && tb[IFA_LOCAL]) {
549 inet_prefix dst;
550 memset(&dst, 0, sizeof(dst));
551 dst.family = ifa->ifa_family;
552 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
553 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
554 continue;
555 }
556 if (filter.label) {
557 SPRINT_BUF(b1);
558 const char *label;
559 if (tb[IFA_LABEL])
560 label = RTA_DATA(tb[IFA_LABEL]);
561 else
562 label = ll_idx_n2a(ifa->ifa_index, b1);
563 if (fnmatch(filter.label, label, 0) != 0)
564 continue;
565 }
566 }
567
568 ok = 1;
569 break;
570 }
571 if (!ok)
572 *lp = l->next;
573 else
574 lp = &l->next;
575 }
576 }
577
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000578 for (l = linfo; l; l = l->next) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000579 if (no_link || print_linkinfo(&l->h) == 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000580 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
581 if (filter.family != AF_PACKET)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000582 print_selected_addrinfo(ifi->ifi_index, ainfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000583 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000584 }
585
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000586 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000587}
588
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000589static int default_scope(inet_prefix *lcl)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000590{
591 if (lcl->family == AF_INET) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000592 if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000593 return RT_SCOPE_HOST;
594 }
595 return 0;
596}
597
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000598/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000599static int ipaddr_modify(int cmd, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000600{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000601 static const char option[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000602 "peer\0""remote\0""broadcast\0""brd\0"
603 "anycast\0""scope\0""dev\0""label\0""local\0";
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000604 struct rtnl_handle rth;
605 struct {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000606 struct nlmsghdr n;
607 struct ifaddrmsg ifa;
608 char buf[256];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000609 } req;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000610 char *d = NULL;
611 char *l = NULL;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000612 inet_prefix lcl;
613 inet_prefix peer;
614 int local_len = 0;
615 int peer_len = 0;
616 int brd_len = 0;
617 int any_len = 0;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000618 bool scoped = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000619
620 memset(&req, 0, sizeof(req));
621
622 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
623 req.n.nlmsg_flags = NLM_F_REQUEST;
624 req.n.nlmsg_type = cmd;
625 req.ifa.ifa_family = preferred_family;
626
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000627 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000628 const int option_num = index_in_strings(option, *argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000629 switch (option_num) {
630 case 0: /* peer */
631 case 1: /* remote */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000632 NEXT_ARG();
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000633
634 if (peer_len) {
635 duparg("peer", *argv);
636 }
637 get_prefix(&peer, *argv, req.ifa.ifa_family);
638 peer_len = peer.bytelen;
639 if (req.ifa.ifa_family == AF_UNSPEC) {
640 req.ifa.ifa_family = peer.family;
641 }
642 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
643 req.ifa.ifa_prefixlen = peer.bitlen;
644 break;
645 case 2: /* broadcast */
646 case 3: /* brd */
647 {
648 inet_prefix addr;
649 NEXT_ARG();
650 if (brd_len) {
651 duparg("broadcast", *argv);
652 }
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000653 if (LONE_CHAR(*argv, '+')) {
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000654 brd_len = -1;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000655 } else if (LONE_DASH(*argv)) {
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000656 brd_len = -2;
657 } else {
658 get_addr(&addr, *argv, req.ifa.ifa_family);
659 if (req.ifa.ifa_family == AF_UNSPEC)
660 req.ifa.ifa_family = addr.family;
661 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
662 brd_len = addr.bytelen;
663 }
664 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000665 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000666 case 4: /* anycast */
667 {
668 inet_prefix addr;
669 NEXT_ARG();
670 if (any_len) {
671 duparg("anycast", *argv);
672 }
673 get_addr(&addr, *argv, req.ifa.ifa_family);
674 if (req.ifa.ifa_family == AF_UNSPEC) {
675 req.ifa.ifa_family = addr.family;
676 }
677 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
678 any_len = addr.bytelen;
679 break;
680 }
681 case 5: /* scope */
682 {
Eric Andersend78aea82006-01-30 18:00:02 +0000683 uint32_t scope = 0;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000684 NEXT_ARG();
685 if (rtnl_rtscope_a2n(&scope, *argv)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000686 invarg(*argv, "scope");
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000687 }
688 req.ifa.ifa_scope = scope;
689 scoped = 1;
690 break;
691 }
692 case 6: /* dev */
693 NEXT_ARG();
694 d = *argv;
695 break;
696 case 7: /* label */
697 NEXT_ARG();
698 l = *argv;
699 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
700 break;
701 case 8: /* local */
702 NEXT_ARG();
703 default:
704 if (local_len) {
705 duparg2("local", *argv);
706 }
707 get_prefix(&lcl, *argv, req.ifa.ifa_family);
708 if (req.ifa.ifa_family == AF_UNSPEC) {
709 req.ifa.ifa_family = lcl.family;
710 }
711 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
712 local_len = lcl.bytelen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000713 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000714 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000715 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000716
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000717 if (d == NULL) {
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000718 bb_error_msg(bb_msg_requires_arg, "\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000719 return -1;
720 }
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000721 if (l && strncmp(d, l, strlen(d)) != 0) {
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000722 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000723 }
724
725 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
726 peer = lcl;
727 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
728 }
729 if (req.ifa.ifa_prefixlen == 0)
730 req.ifa.ifa_prefixlen = lcl.bitlen;
731
732 if (brd_len < 0 && cmd != RTM_DELADDR) {
733 inet_prefix brd;
734 int i;
735 if (req.ifa.ifa_family != AF_INET) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000736 bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000737 }
738 brd = peer;
739 if (brd.bitlen <= 30) {
740 for (i=31; i>=brd.bitlen; i--) {
741 if (brd_len == -1)
742 brd.data[0] |= htonl(1<<(31-i));
743 else
744 brd.data[0] &= ~htonl(1<<(31-i));
745 }
746 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
747 brd_len = brd.bytelen;
748 }
749 }
750 if (!scoped && cmd != RTM_DELADDR)
751 req.ifa.ifa_scope = default_scope(&lcl);
752
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000753 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000754
755 ll_init_map(&rth);
756
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000757 req.ifa.ifa_index = xll_name_to_index(d);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000758
759 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000760 return 2;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000761
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000762 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000763}
764
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000765/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000766int do_ipaddr(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000767{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000768 static const char commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000769 "add\0""delete\0""list\0""show\0""lst\0""flush\0";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000770
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000771 int command_num = 2; /* default command is list */
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000772
773 if (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000774 command_num = index_in_substrings(commands, *argv);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000775 if (command_num < 0 || command_num > 5)
776 bb_error_msg_and_die("unknown command %s", *argv);
777 argv++;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000778 }
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000779 if (command_num == 0) /* add */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000780 return ipaddr_modify(RTM_NEWADDR, argv);
781 if (command_num == 1) /* delete */
782 return ipaddr_modify(RTM_DELADDR, argv);
783 if (command_num == 5) /* flush */
784 return ipaddr_list_or_flush(argv, 1);
785 /* 2 == list, 3 == show, 4 == lst */
786 return ipaddr_list_or_flush(argv, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000787}