blob: 4f83924ae7c359f3515d8b4081900f55dd5f21e1 [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
Rob Landleyecae66a2006-06-02 20:53:38 +000013#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014#include <sys/socket.h>
15#include <sys/ioctl.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000016
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017#include <fnmatch.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000018#include <string.h>
19#include <unistd.h>
20
Eric Andersen8004bb72003-01-14 08:06:07 +000021#include <net/if.h>
22#include <net/if_arp.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000023
24#include "rt_names.h"
25#include "utils.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000026#include "ip_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000027
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000028
Denis Vlasenko540a2a12007-04-07 01:14:45 +000029typedef struct filter_t {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000030 int ifindex;
31 int family;
32 int oneline;
33 int showqueue;
34 inet_prefix pfx;
35 int scope, scopemask;
36 int flags, flagmask;
37 int up;
38 char *label;
Glenn L McGrathd66370c2003-01-13 21:40:38 +000039 int flushed;
40 char *flushb;
41 int flushp;
42 int flushe;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000043 struct rtnl_handle *rth;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000044} filter_t;
45
46#define filter (*(filter_t*)&bb_common_bufsiz1)
47
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000048
Eric Andersen14f5c8d2005-04-16 19:39:00 +000049static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000050{
51 fprintf(fp, "<");
52 flags &= ~IFF_RUNNING;
53#define _PF(f) if (flags&IFF_##f) { \
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000054 flags &= ~IFF_##f ; \
55 fprintf(fp, #f "%s", flags ? "," : ""); }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000056 _PF(LOOPBACK);
57 _PF(BROADCAST);
58 _PF(POINTOPOINT);
59 _PF(MULTICAST);
60 _PF(NOARP);
61#if 0
62 _PF(ALLMULTI);
63 _PF(PROMISC);
64 _PF(MASTER);
65 _PF(SLAVE);
66 _PF(DEBUG);
67 _PF(DYNAMIC);
68 _PF(AUTOMEDIA);
69 _PF(PORTSEL);
70 _PF(NOTRAILERS);
71#endif
72 _PF(UP);
73#undef _PF
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000074 if (flags)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075 fprintf(fp, "%x", flags);
76 if (mdown)
77 fprintf(fp, ",M-DOWN");
78 fprintf(fp, "> ");
79}
80
Glenn L McGrath2626ef62002-12-02 01:40:05 +000081static void print_queuelen(char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000082{
83 struct ifreq ifr;
84 int s;
85
86 s = socket(AF_INET, SOCK_STREAM, 0);
87 if (s < 0)
88 return;
89
90 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko229b3d22006-11-27 23:44:57 +000091 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
Eric Andersenc7bda1c2004-03-15 08:29:22 +000092 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +000093 bb_perror_msg("SIOCGIFXQLEN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094 close(s);
95 return;
96 }
97 close(s);
98
99 if (ifr.ifr_qlen)
100 printf("qlen %d", ifr.ifr_qlen);
101}
102
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +0000103static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000104 const struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105{
106 FILE *fp = (FILE*)arg;
107 struct ifinfomsg *ifi = NLMSG_DATA(n);
108 struct rtattr * tb[IFLA_MAX+1];
109 int len = n->nlmsg_len;
110 unsigned m_flag = 0;
111
112 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
113 return 0;
114
115 len -= NLMSG_LENGTH(sizeof(*ifi));
116 if (len < 0)
117 return -1;
118
119 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
120 return 0;
121 if (filter.up && !(ifi->ifi_flags&IFF_UP))
122 return 0;
123
124 memset(tb, 0, sizeof(tb));
125 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
126 if (tb[IFLA_IFNAME] == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 bb_error_msg("nil ifname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000128 return -1;
129 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000130 if (filter.label
131 && (!filter.family || filter.family == AF_PACKET)
132 && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
133 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000134 return 0;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000135 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136
137 if (n->nlmsg_type == RTM_DELLINK)
138 fprintf(fp, "Deleted ");
139
140 fprintf(fp, "%d: %s", ifi->ifi_index,
141 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
142
143 if (tb[IFLA_LINK]) {
144 SPRINT_BUF(b1);
145 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
146 if (iflink == 0)
147 fprintf(fp, "@NONE: ");
148 else {
149 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
150 m_flag = ll_index_to_flags(iflink);
151 m_flag = !(m_flag & IFF_UP);
152 }
153 } else {
154 fprintf(fp, ": ");
155 }
156 print_link_flags(fp, ifi->ifi_flags, m_flag);
157
158 if (tb[IFLA_MTU])
159 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
160 if (tb[IFLA_QDISC])
161 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
162#ifdef IFLA_MASTER
163 if (tb[IFLA_MASTER]) {
164 SPRINT_BUF(b1);
165 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
166 }
167#endif
168 if (filter.showqueue)
169 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000170
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000171 if (!filter.family || filter.family == AF_PACKET) {
172 SPRINT_BUF(b1);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000173 fprintf(fp, "%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000174
175 if (tb[IFLA_ADDRESS]) {
176 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
177 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
178 ifi->ifi_type,
179 b1, sizeof(b1)));
180 }
181 if (tb[IFLA_BROADCAST]) {
182 if (ifi->ifi_flags&IFF_POINTOPOINT)
183 fprintf(fp, " peer ");
184 else
185 fprintf(fp, " brd ");
186 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
187 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
188 ifi->ifi_type,
189 b1, sizeof(b1)));
190 }
191 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000192 fputc('\n', fp);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000193 fflush(fp);
194 return 0;
195}
196
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000197static int flush_update(void)
198{
199 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000200 bb_perror_msg("failed to send flush request");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000201 return -1;
202 }
203 filter.flushp = 0;
204 return 0;
205}
206
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +0000207static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
208 struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000209{
210 FILE *fp = (FILE*)arg;
211 struct ifaddrmsg *ifa = NLMSG_DATA(n);
212 int len = n->nlmsg_len;
213 struct rtattr * rta_tb[IFA_MAX+1];
214 char abuf[256];
215 SPRINT_BUF(b1);
216
217 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
218 return 0;
219 len -= NLMSG_LENGTH(sizeof(*ifa));
220 if (len < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 bb_error_msg("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000222 return -1;
223 }
224
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000225 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
226 return 0;
227
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000228 memset(rta_tb, 0, sizeof(rta_tb));
229 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
230
231 if (!rta_tb[IFA_LOCAL])
232 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
233 if (!rta_tb[IFA_ADDRESS])
234 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
235
236 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
237 return 0;
238 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
239 return 0;
240 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
241 return 0;
242 if (filter.label) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000243 const char *label;
244 if (rta_tb[IFA_LABEL])
245 label = RTA_DATA(rta_tb[IFA_LABEL]);
246 else
247 label = ll_idx_n2a(ifa->ifa_index, b1);
248 if (fnmatch(filter.label, label, 0) != 0)
249 return 0;
250 }
251 if (filter.pfx.family) {
252 if (rta_tb[IFA_LOCAL]) {
253 inet_prefix dst;
254 memset(&dst, 0, sizeof(dst));
255 dst.family = ifa->ifa_family;
256 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
257 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
258 return 0;
259 }
260 }
261
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000262 if (filter.flushb) {
263 struct nlmsghdr *fn;
264 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
265 if (flush_update())
266 return -1;
267 }
268 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
269 memcpy(fn, n, n->nlmsg_len);
270 fn->nlmsg_type = RTM_DELADDR;
271 fn->nlmsg_flags = NLM_F_REQUEST;
272 fn->nlmsg_seq = ++filter.rth->seq;
273 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
274 filter.flushed++;
275 return 0;
276 }
277
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 if (n->nlmsg_type == RTM_DELADDR)
279 fprintf(fp, "Deleted ");
280
281 if (filter.oneline)
282 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
283 if (ifa->ifa_family == AF_INET)
284 fprintf(fp, " inet ");
285 else if (ifa->ifa_family == AF_INET6)
286 fprintf(fp, " inet6 ");
287 else
288 fprintf(fp, " family %d ", ifa->ifa_family);
289
290 if (rta_tb[IFA_LOCAL]) {
291 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
292 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
293 RTA_DATA(rta_tb[IFA_LOCAL]),
294 abuf, sizeof(abuf)));
295
296 if (rta_tb[IFA_ADDRESS] == NULL ||
297 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
298 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
299 } else {
300 fprintf(fp, " peer %s/%d ",
301 rt_addr_n2a(ifa->ifa_family,
302 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
303 RTA_DATA(rta_tb[IFA_ADDRESS]),
304 abuf, sizeof(abuf)),
305 ifa->ifa_prefixlen);
306 }
307 }
308
309 if (rta_tb[IFA_BROADCAST]) {
310 fprintf(fp, "brd %s ",
311 rt_addr_n2a(ifa->ifa_family,
312 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
313 RTA_DATA(rta_tb[IFA_BROADCAST]),
314 abuf, sizeof(abuf)));
315 }
316 if (rta_tb[IFA_ANYCAST]) {
317 fprintf(fp, "any %s ",
318 rt_addr_n2a(ifa->ifa_family,
319 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
320 RTA_DATA(rta_tb[IFA_ANYCAST]),
321 abuf, sizeof(abuf)));
322 }
323 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
324 if (ifa->ifa_flags&IFA_F_SECONDARY) {
325 ifa->ifa_flags &= ~IFA_F_SECONDARY;
326 fprintf(fp, "secondary ");
327 }
328 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
329 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
330 fprintf(fp, "tentative ");
331 }
332 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
333 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
334 fprintf(fp, "deprecated ");
335 }
336 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
337 fprintf(fp, "dynamic ");
338 } else
339 ifa->ifa_flags &= ~IFA_F_PERMANENT;
340 if (ifa->ifa_flags)
341 fprintf(fp, "flags %02x ", ifa->ifa_flags);
342 if (rta_tb[IFA_LABEL])
343 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
344 if (rta_tb[IFA_CACHEINFO]) {
345 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
346 char buf[128];
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000347 fputc(_SL_, fp);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000348 if (ci->ifa_valid == 0xFFFFFFFFU)
349 sprintf(buf, "valid_lft forever");
350 else
351 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
352 if (ci->ifa_prefered == 0xFFFFFFFFU)
353 sprintf(buf+strlen(buf), " preferred_lft forever");
354 else
355 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
356 fprintf(fp, " %s", buf);
357 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000358 fputc('\n', fp);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000359 fflush(fp);
360 return 0;
361}
362
363
364struct nlmsg_list
365{
366 struct nlmsg_list *next;
367 struct nlmsghdr h;
368};
369
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000370static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000371{
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000372 for (; ainfo; ainfo = ainfo->next) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000373 struct nlmsghdr *n = &ainfo->h;
374 struct ifaddrmsg *ifa = NLMSG_DATA(n);
375
376 if (n->nlmsg_type != RTM_NEWADDR)
377 continue;
378
379 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
380 return -1;
381
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000382 if (ifa->ifa_index != ifindex ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000383 (filter.family && filter.family != ifa->ifa_family))
384 continue;
385
386 print_addrinfo(NULL, n, fp);
387 }
388 return 0;
389}
390
391
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000392static int store_nlmsg(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
398 h = malloc(n->nlmsg_len+sizeof(void*));
399 if (h == NULL)
400 return -1;
401
402 memcpy(&h->h, n, n->nlmsg_len);
403 h->next = NULL;
404
405 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
406 *lp = h;
407
408 ll_remember_index(who, n, NULL);
409 return 0;
410}
411
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000412static void ipaddr_reset_filter(int _oneline)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000413{
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000414 memset(&filter, 0, sizeof(filter));
415 filter.oneline = _oneline;
416}
417
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000418/* Return value becomes exitcode. It's okay to not return at all */
Rob Landleydfba7412006-03-06 20:47:33 +0000419int ipaddr_list_or_flush(int argc, char **argv, int flush)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000420{
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000421 static const char *const option[] = { "to", "scope", "up", "label", "dev", 0 };
422
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000423 struct nlmsg_list *linfo = NULL;
424 struct nlmsg_list *ainfo = NULL;
425 struct nlmsg_list *l;
426 struct rtnl_handle rth;
427 char *filter_dev = NULL;
428 int no_link = 0;
429
430 ipaddr_reset_filter(oneline);
431 filter.showqueue = 1;
432
433 if (filter.family == AF_UNSPEC)
434 filter.family = preferred_family;
435
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000436 if (flush) {
437 if (argc <= 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000438 bb_error_msg_and_die(bb_msg_requires_arg, "flush");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000439 }
440 if (filter.family == AF_PACKET) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000441 bb_error_msg_and_die("cannot flush link addresses");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000442 }
443 }
444
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000445 while (argc > 0) {
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000446 const int option_num = index_in_str_array(option, *argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000447 switch (option_num) {
448 case 0: /* to */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000449 NEXT_ARG();
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000450 get_prefix(&filter.pfx, *argv, filter.family);
451 if (filter.family == AF_UNSPEC) {
452 filter.family = filter.pfx.family;
453 }
454 break;
455 case 1: /* scope */
456 {
Eric Andersend78aea82006-01-30 18:00:02 +0000457 uint32_t scope = 0;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000458 NEXT_ARG();
459 filter.scopemask = -1;
460 if (rtnl_rtscope_a2n(&scope, *argv)) {
461 if (strcmp(*argv, "all") != 0) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000462 invarg(*argv, "scope");
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000463 }
464 scope = RT_SCOPE_NOWHERE;
465 filter.scopemask = 0;
466 }
467 filter.scope = scope;
468 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000469 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000470 case 2: /* up */
471 filter.up = 1;
472 break;
473 case 3: /* label */
474 NEXT_ARG();
475 filter.label = *argv;
476 break;
477 case 4: /* dev */
478 NEXT_ARG();
479 default:
480 if (filter_dev) {
481 duparg2("dev", *argv);
482 }
483 filter_dev = *argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000484 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000485 argv++;
486 argc--;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000487 }
488
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000489 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000490
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000491 xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
492 xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000493
494 if (filter_dev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000495 filter.ifindex = xll_name_to_index(filter_dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000496 }
497
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000498 if (flush) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000499 char flushb[4096-512];
500
501 filter.flushb = flushb;
502 filter.flushp = 0;
503 filter.flushe = sizeof(flushb);
504 filter.rth = &rth;
505
506 for (;;) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000507 xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000508 filter.flushed = 0;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000509 xrtnl_dump_filter(&rth, print_addrinfo, stdout);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000510 if (filter.flushed == 0) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000511 return 0;
512 }
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000513 if (flush_update() < 0)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000514 return 1;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000515 }
516 }
517
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000518 if (filter.family != AF_PACKET) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000519 xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
520 xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000521 }
522
523
524 if (filter.family && filter.family != AF_PACKET) {
525 struct nlmsg_list **lp;
526 lp=&linfo;
527
528 if (filter.oneline)
529 no_link = 1;
530
531 while ((l=*lp)!=NULL) {
532 int ok = 0;
533 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
534 struct nlmsg_list *a;
535
536 for (a=ainfo; a; a=a->next) {
537 struct nlmsghdr *n = &a->h;
538 struct ifaddrmsg *ifa = NLMSG_DATA(n);
539
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000540 if (ifa->ifa_index != ifi->ifi_index ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000541 (filter.family && filter.family != ifa->ifa_family))
542 continue;
543 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
544 continue;
545 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
546 continue;
547 if (filter.pfx.family || filter.label) {
548 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
554 if (filter.pfx.family && tb[IFA_LOCAL]) {
555 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]));
559 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
560 continue;
561 }
562 if (filter.label) {
563 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);
569 if (fnmatch(filter.label, label, 0) != 0)
570 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) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000585 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
586 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
587 if (filter.family != AF_PACKET)
588 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
589 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000590 fflush(stdout); /* why? */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000591 }
592
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000593 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000594}
595
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000596static int default_scope(inet_prefix *lcl)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000597{
598 if (lcl->family == AF_INET) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000599 if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000600 return RT_SCOPE_HOST;
601 }
602 return 0;
603}
604
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000605/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000606static int ipaddr_modify(int cmd, int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000607{
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000608 static const char *const option[] = {
609 "peer", "remote", "broadcast", "brd",
610 "anycast", "scope", "dev", "label", "local", 0
611 };
612
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000613 struct rtnl_handle rth;
614 struct {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000615 struct nlmsghdr n;
616 struct ifaddrmsg ifa;
617 char buf[256];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000618 } req;
619 char *d = NULL;
620 char *l = NULL;
621 inet_prefix lcl;
622 inet_prefix peer;
623 int local_len = 0;
624 int peer_len = 0;
625 int brd_len = 0;
626 int any_len = 0;
627 int scoped = 0;
628
629 memset(&req, 0, sizeof(req));
630
631 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
632 req.n.nlmsg_flags = NLM_F_REQUEST;
633 req.n.nlmsg_type = cmd;
634 req.ifa.ifa_family = preferred_family;
635
636 while (argc > 0) {
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000637 const int option_num = index_in_str_array(option, *argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000638 switch (option_num) {
639 case 0: /* peer */
640 case 1: /* remote */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000641 NEXT_ARG();
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000642
643 if (peer_len) {
644 duparg("peer", *argv);
645 }
646 get_prefix(&peer, *argv, req.ifa.ifa_family);
647 peer_len = peer.bytelen;
648 if (req.ifa.ifa_family == AF_UNSPEC) {
649 req.ifa.ifa_family = peer.family;
650 }
651 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
652 req.ifa.ifa_prefixlen = peer.bitlen;
653 break;
654 case 2: /* broadcast */
655 case 3: /* brd */
656 {
657 inet_prefix addr;
658 NEXT_ARG();
659 if (brd_len) {
660 duparg("broadcast", *argv);
661 }
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000662 if (LONE_CHAR(*argv, '+')) {
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000663 brd_len = -1;
664 }
Denis Vlasenko9f739442006-12-16 23:49:13 +0000665 else if (LONE_DASH(*argv)) {
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000666 brd_len = -2;
667 } else {
668 get_addr(&addr, *argv, req.ifa.ifa_family);
669 if (req.ifa.ifa_family == AF_UNSPEC)
670 req.ifa.ifa_family = addr.family;
671 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
672 brd_len = addr.bytelen;
673 }
674 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000675 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000676 case 4: /* anycast */
677 {
678 inet_prefix addr;
679 NEXT_ARG();
680 if (any_len) {
681 duparg("anycast", *argv);
682 }
683 get_addr(&addr, *argv, req.ifa.ifa_family);
684 if (req.ifa.ifa_family == AF_UNSPEC) {
685 req.ifa.ifa_family = addr.family;
686 }
687 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
688 any_len = addr.bytelen;
689 break;
690 }
691 case 5: /* scope */
692 {
Eric Andersend78aea82006-01-30 18:00:02 +0000693 uint32_t scope = 0;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000694 NEXT_ARG();
695 if (rtnl_rtscope_a2n(&scope, *argv)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000696 invarg(*argv, "scope");
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000697 }
698 req.ifa.ifa_scope = scope;
699 scoped = 1;
700 break;
701 }
702 case 6: /* dev */
703 NEXT_ARG();
704 d = *argv;
705 break;
706 case 7: /* label */
707 NEXT_ARG();
708 l = *argv;
709 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
710 break;
711 case 8: /* local */
712 NEXT_ARG();
713 default:
714 if (local_len) {
715 duparg2("local", *argv);
716 }
717 get_prefix(&lcl, *argv, req.ifa.ifa_family);
718 if (req.ifa.ifa_family == AF_UNSPEC) {
719 req.ifa.ifa_family = lcl.family;
720 }
721 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
722 local_len = lcl.bytelen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000723 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000724 argc--;
725 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000726 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000727
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000728 if (d == NULL) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000729 bb_error_msg(bb_msg_requires_arg,"\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000730 return -1;
731 }
732 if (l && matches(d, l) != 0) {
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000733 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000734 }
735
736 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
737 peer = lcl;
738 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
739 }
740 if (req.ifa.ifa_prefixlen == 0)
741 req.ifa.ifa_prefixlen = lcl.bitlen;
742
743 if (brd_len < 0 && cmd != RTM_DELADDR) {
744 inet_prefix brd;
745 int i;
746 if (req.ifa.ifa_family != AF_INET) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000747 bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000748 }
749 brd = peer;
750 if (brd.bitlen <= 30) {
751 for (i=31; i>=brd.bitlen; i--) {
752 if (brd_len == -1)
753 brd.data[0] |= htonl(1<<(31-i));
754 else
755 brd.data[0] &= ~htonl(1<<(31-i));
756 }
757 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
758 brd_len = brd.bytelen;
759 }
760 }
761 if (!scoped && cmd != RTM_DELADDR)
762 req.ifa.ifa_scope = default_scope(&lcl);
763
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000764 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000765
766 ll_init_map(&rth);
767
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000768 req.ifa.ifa_index = xll_name_to_index(d);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000769
770 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000771 return 2;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000772
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000773 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000774}
775
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000776/* Return value becomes exitcode. It's okay to not return at all */
Rob Landleydfba7412006-03-06 20:47:33 +0000777int do_ipaddr(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000778{
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000779 static const char *const commands[] = {
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000780 "add", "delete", "list", "show", "lst", "flush", 0
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000781 };
782
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000783 int command_num = 2;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000784
785 if (*argv) {
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000786 command_num = index_in_substr_array(commands, *argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000787 }
788 switch (command_num) {
789 case 0: /* add */
790 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000791 case 1: /* delete */
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000792 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000793 case 2: /* list */
794 case 3: /* show */
795 case 4: /* lst */
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000796 return ipaddr_list_or_flush(argc-1, argv+1, 0);
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000797 case 5: /* flush */
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000798 return ipaddr_list_or_flush(argc-1, argv+1, 1);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000799 }
Denis Vlasenko940b2e42006-10-26 00:38:22 +0000800 bb_error_msg_and_die("unknown command %s", *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000801}