blob: 91fabb1fd3d390a6a038ee677acc64b514c4fee7 [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/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00004 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
7 * Changes:
Denys Vlasenkofb132e42010-10-29 11:46:52 +02008 * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00009 */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000010#include <fnmatch.h>
Eric Andersen8004bb72003-01-14 08:06:07 +000011#include <net/if.h>
12#include <net/if_arp.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000013
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000014#include "ip_common.h" /* #include "libbb.h" is inside */
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020015#include "common_bufsiz.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000016#include "rt_names.h"
17#include "utils.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018
Denis Vlasenko9b6f4aa2008-06-05 14:01:04 +000019#ifndef IFF_LOWER_UP
20/* from linux/if.h */
Denys Vlasenkofb132e42010-10-29 11:46:52 +020021#define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
Denis Vlasenko9b6f4aa2008-06-05 14:01:04 +000022#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000023
Denys Vlasenko77a51a22020-12-29 16:53:11 +010024#ifndef IFA_F_NOPREFIXROUTE
25# define IFA_FLAGS 8
26/* ifa_flags */
27# define IFA_F_NOPREFIXROUTE 0x200
28#endif
29
Denys Vlasenko36659fd2010-02-05 14:40:23 +010030struct filter_t {
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000031 char *label;
Denys Vlasenko028c5aa2019-05-22 13:54:46 +020032 /* Flush cmd buf. If !NULL, print_addrinfo() constructs flush commands in it */
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000033 char *flushb;
34 struct rtnl_handle *rth;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000035 int scope, scopemask;
36 int flags, flagmask;
Glenn L McGrathd66370c2003-01-13 21:40:38 +000037 int flushp;
38 int flushe;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000039 int ifindex;
40 family_t family;
41 smallint showqueue;
42 smallint oneline;
43 smallint up;
Denys Vlasenko028c5aa2019-05-22 13:54:46 +020044 /* Misnomer. Does not mean "flushed something" */
45 /* More like "flush commands were constructed by print_addrinfo()" */
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000046 smallint flushed;
47 inet_prefix pfx;
Denys Vlasenko36659fd2010-02-05 14:40:23 +010048} FIX_ALIASING;
49typedef struct filter_t filter_t;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000050
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020051#define G_filter (*(filter_t*)bb_common_bufsiz1)
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020052#define INIT_G() do { setup_common_bufsiz(); } while (0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000053
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000054static void print_link_flags(unsigned flags, unsigned mdown)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000055{
Denis Vlasenko53354ac2008-06-07 15:10:29 +000056 static const int flag_masks[] = {
57 IFF_LOOPBACK, IFF_BROADCAST, IFF_POINTOPOINT,
58 IFF_MULTICAST, IFF_NOARP, IFF_UP, IFF_LOWER_UP };
59 static const char flag_labels[] ALIGN1 =
60 "LOOPBACK\0""BROADCAST\0""POINTOPOINT\0"
61 "MULTICAST\0""NOARP\0""UP\0""LOWER_UP\0";
62
Denis Vlasenko7d60fc12008-06-05 06:51:06 +000063 bb_putchar('<');
Bernhard Reutner-Fischer49ee8392010-05-25 09:55:42 +020064 if (flags & IFF_UP && !(flags & IFF_RUNNING))
65 printf("NO-CARRIER,");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066 flags &= ~IFF_RUNNING;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000067#if 0
68 _PF(ALLMULTI);
69 _PF(PROMISC);
70 _PF(MASTER);
71 _PF(SLAVE);
72 _PF(DEBUG);
73 _PF(DYNAMIC);
74 _PF(AUTOMEDIA);
75 _PF(PORTSEL);
76 _PF(NOTRAILERS);
77#endif
Denis Vlasenko53354ac2008-06-07 15:10:29 +000078 flags = print_flags_separated(flag_masks, flag_labels, flags, ",");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000079 if (flags)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000080 printf("%x", flags);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000081 if (mdown)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000082 printf(",M-DOWN");
83 printf("> ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000084}
85
Glenn L McGrath2626ef62002-12-02 01:40:05 +000086static void print_queuelen(char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000087{
88 struct ifreq ifr;
89 int s;
90
91 s = socket(AF_INET, SOCK_STREAM, 0);
92 if (s < 0)
93 return;
94
95 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +000096 strncpy_IFNAMSIZ(ifr.ifr_name, name);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000097 if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000098 close(s);
99 return;
100 }
101 close(s);
102
103 if (ifr.ifr_qlen)
104 printf("qlen %d", ifr.ifr_qlen);
105}
106
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200107static NOINLINE int print_linkinfo(const struct nlmsghdr *n)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000108{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000109 struct ifinfomsg *ifi = NLMSG_DATA(n);
Denys Vlasenko90377872010-01-08 09:07:50 +0100110 struct rtattr *tb[IFLA_MAX+1];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000111 int len = n->nlmsg_len;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000112
113 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
114 return 0;
115
116 len -= NLMSG_LENGTH(sizeof(*ifi));
117 if (len < 0)
118 return -1;
119
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100120 if (G_filter.ifindex && ifi->ifi_index != G_filter.ifindex)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000121 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100122 if (G_filter.up && !(ifi->ifi_flags & IFF_UP))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000123 return 0;
124
Denys Vlasenko68ae5422018-02-08 08:42:37 +0100125 //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000126 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
127 if (tb[IFLA_IFNAME] == NULL) {
James Byrne69374872019-07-02 11:35:03 +0200128 bb_simple_error_msg("nil ifname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000129 return -1;
130 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100131 if (G_filter.label
132 && (!G_filter.family || G_filter.family == AF_PACKET)
133 && fnmatch(G_filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000134 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000135 return 0;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000136 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000137
138 if (n->nlmsg_type == RTM_DELLINK)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000139 printf("Deleted ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000140
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000141 printf("%d: %s", ifi->ifi_index,
Denys Vlasenko90377872010-01-08 09:07:50 +0100142 /*tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>" - we checked tb[IFLA_IFNAME] above*/
143 (char*)RTA_DATA(tb[IFLA_IFNAME])
144 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145
Denys Vlasenko90377872010-01-08 09:07:50 +0100146 {
147 unsigned m_flag = 0;
148 if (tb[IFLA_LINK]) {
Denys Vlasenko90377872010-01-08 09:07:50 +0100149 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
150 if (iflink == 0)
151 printf("@NONE: ");
152 else {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200153 printf("@%s: ", ll_index_to_name(iflink));
Denys Vlasenko90377872010-01-08 09:07:50 +0100154 m_flag = ll_index_to_flags(iflink);
155 m_flag = !(m_flag & IFF_UP);
156 }
157 } else {
158 printf(": ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000159 }
Denys Vlasenko90377872010-01-08 09:07:50 +0100160 print_link_flags(ifi->ifi_flags, m_flag);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000162
163 if (tb[IFLA_MTU])
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000164 printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165 if (tb[IFLA_QDISC])
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000166 printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167#ifdef IFLA_MASTER
168 if (tb[IFLA_MASTER]) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200169 printf("master %s ", ll_index_to_name(*(int*)RTA_DATA(tb[IFLA_MASTER])));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170 }
171#endif
Stefan Seyfriedda4441c2011-02-21 17:29:59 +0100172/* IFLA_OPERSTATE was added to kernel with the same commit as IFF_DORMANT */
173#ifdef IFF_DORMANT
Bernhard Reutner-Fischer49ee8392010-05-25 09:55:42 +0200174 if (tb[IFLA_OPERSTATE]) {
175 static const char operstate_labels[] ALIGN1 =
176 "UNKNOWN\0""NOTPRESENT\0""DOWN\0""LOWERLAYERDOWN\0"
177 "TESTING\0""DORMANT\0""UP\0";
178 printf("state %s ", nth_string(operstate_labels,
Denys Vlasenkoeb29e912010-05-27 13:35:04 +0200179 *(uint8_t *)RTA_DATA(tb[IFLA_OPERSTATE])));
Bernhard Reutner-Fischer49ee8392010-05-25 09:55:42 +0200180 }
Stefan Seyfriedda4441c2011-02-21 17:29:59 +0100181#endif
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100182 if (G_filter.showqueue)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000183 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000184
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100185 if (!G_filter.family || G_filter.family == AF_PACKET) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000186 SPRINT_BUF(b1);
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200187 printf("%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000188
189 if (tb[IFLA_ADDRESS]) {
Denis Vlasenko605b20e2007-09-30 16:22:36 +0000190 fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000191 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
192 ifi->ifi_type,
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000193 b1, sizeof(b1)), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000194 }
195 if (tb[IFLA_BROADCAST]) {
Denis Vlasenkodfc07402007-10-29 19:33:26 +0000196 if (ifi->ifi_flags & IFF_POINTOPOINT)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000197 printf(" peer ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000198 else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000199 printf(" brd ");
Denis Vlasenko605b20e2007-09-30 16:22:36 +0000200 fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000201 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
202 ifi->ifi_type,
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000203 b1, sizeof(b1)), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000204 }
205 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000206 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100207 /*fflush_all();*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000208 return 0;
209}
210
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000211static int flush_update(void)
212{
Denys Vlasenko028c5aa2019-05-22 13:54:46 +0200213 if (rtnl_send_check(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
James Byrne69374872019-07-02 11:35:03 +0200214 bb_simple_perror_msg("can't send flush request");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000215 return -1;
216 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100217 G_filter.flushp = 0;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000218 return 0;
219}
220
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200221static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000222 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000223{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224 struct ifaddrmsg *ifa = NLMSG_DATA(n);
225 int len = n->nlmsg_len;
Christian Eggers31d34f32020-06-29 17:57:25 +0200226 unsigned int ifa_flags;
Denys Vlasenko926d8012015-10-14 13:56:42 +0200227 struct rtattr *rta_tb[IFA_MAX+1];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000228
229 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
230 return 0;
231 len -= NLMSG_LENGTH(sizeof(*ifa));
232 if (len < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 bb_error_msg("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 return -1;
235 }
236
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100237 if (G_filter.flushb && n->nlmsg_type != RTM_NEWADDR)
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000238 return 0;
239
Denys Vlasenko68ae5422018-02-08 08:42:37 +0100240 //memset(rta_tb, 0, sizeof(rta_tb)); - parse_rtattr does this
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000241 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
242
Christian Eggers31d34f32020-06-29 17:57:25 +0200243 ifa_flags = rta_tb[IFA_FLAGS] ? *(__u32*)RTA_DATA(rta_tb[IFA_FLAGS]) : ifa->ifa_flags;
244
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000245 if (!rta_tb[IFA_LOCAL])
246 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
247 if (!rta_tb[IFA_ADDRESS])
248 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
249
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100250 if (G_filter.ifindex && G_filter.ifindex != ifa->ifa_index)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000251 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100252 if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000253 return 0;
Christian Eggers31d34f32020-06-29 17:57:25 +0200254 if ((G_filter.flags ^ ifa_flags) & G_filter.flagmask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100256 if (G_filter.label) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000257 const char *label;
258 if (rta_tb[IFA_LABEL])
259 label = RTA_DATA(rta_tb[IFA_LABEL]);
260 else
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200261 label = ll_index_to_name(ifa->ifa_index);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100262 if (fnmatch(G_filter.label, label, 0) != 0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000263 return 0;
264 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100265 if (G_filter.pfx.family) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266 if (rta_tb[IFA_LOCAL]) {
267 inet_prefix dst;
268 memset(&dst, 0, sizeof(dst));
269 dst.family = ifa->ifa_family;
270 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100271 if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 return 0;
273 }
274 }
275
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100276 if (G_filter.flushb) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000277 struct nlmsghdr *fn;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100278 if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000279 if (flush_update())
280 return -1;
281 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100282 fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000283 memcpy(fn, n, n->nlmsg_len);
284 fn->nlmsg_type = RTM_DELADDR;
285 fn->nlmsg_flags = NLM_F_REQUEST;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100286 fn->nlmsg_seq = ++G_filter.rth->seq;
287 G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
288 G_filter.flushed = 1;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000289 return 0;
290 }
291
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000292 if (n->nlmsg_type == RTM_DELADDR)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000293 printf("Deleted ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000294
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100295 if (G_filter.oneline)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000296 printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000297 if (ifa->ifa_family == AF_INET)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000298 printf(" inet ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000299 else if (ifa->ifa_family == AF_INET6)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000300 printf(" inet6 ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000301 else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000302 printf(" family %d ", ifa->ifa_family);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000303
304 if (rta_tb[IFA_LOCAL]) {
Denys Vlasenko926d8012015-10-14 13:56:42 +0200305 fputs(rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_LOCAL])),
306 stdout
307 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000308
Denis Vlasenko76140a72009-03-05 09:21:57 +0000309 if (rta_tb[IFA_ADDRESS] == NULL
310 || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0
311 ) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000312 printf("/%d ", ifa->ifa_prefixlen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000313 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000314 printf(" peer %s/%d ",
Denys Vlasenko926d8012015-10-14 13:56:42 +0200315 rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ADDRESS])),
316 ifa->ifa_prefixlen
317 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000318 }
319 }
320
321 if (rta_tb[IFA_BROADCAST]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000322 printf("brd %s ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000323 rt_addr_n2a(ifa->ifa_family,
Denys Vlasenko926d8012015-10-14 13:56:42 +0200324 RTA_DATA(rta_tb[IFA_BROADCAST]))
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100325 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000326 }
327 if (rta_tb[IFA_ANYCAST]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000328 printf("any %s ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000329 rt_addr_n2a(ifa->ifa_family,
Denys Vlasenko926d8012015-10-14 13:56:42 +0200330 RTA_DATA(rta_tb[IFA_ANYCAST]))
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100331 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000332 }
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200333 printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope));
Christian Eggers31d34f32020-06-29 17:57:25 +0200334 if (ifa_flags & IFA_F_SECONDARY) {
335 ifa_flags &= ~IFA_F_SECONDARY;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000336 printf("secondary ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000337 }
Christian Eggers31d34f32020-06-29 17:57:25 +0200338 if (ifa_flags & IFA_F_TENTATIVE) {
339 ifa_flags &= ~IFA_F_TENTATIVE;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000340 printf("tentative ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000341 }
Christian Eggers31d34f32020-06-29 17:57:25 +0200342 if (ifa_flags & IFA_F_DADFAILED) {
343 ifa_flags &= ~IFA_F_DADFAILED;
Kaarle Ritvanen1c952ba2018-12-31 19:52:32 +0200344 printf("dadfailed ");
345 }
Christian Eggers31d34f32020-06-29 17:57:25 +0200346 if (ifa_flags & IFA_F_DEPRECATED) {
347 ifa_flags &= ~IFA_F_DEPRECATED;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000348 printf("deprecated ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000349 }
Christian Eggers31d34f32020-06-29 17:57:25 +0200350 if (!(ifa_flags & IFA_F_PERMANENT)) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000351 printf("dynamic ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000352 } else
Christian Eggers31d34f32020-06-29 17:57:25 +0200353 ifa_flags &= ~IFA_F_PERMANENT;
354 if (ifa_flags & IFA_F_NOPREFIXROUTE) {
355 ifa_flags &= ~IFA_F_NOPREFIXROUTE;
356 printf("noprefixroute ");
357 }
358 if (ifa_flags)
359 printf("flags %02x ", ifa_flags);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000360 if (rta_tb[IFA_LABEL])
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000361 fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000362 if (rta_tb[IFA_CACHEINFO]) {
363 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
364 char buf[128];
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000365 bb_putchar(_SL_);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000366 if (ci->ifa_valid == 0xFFFFFFFFU)
367 sprintf(buf, "valid_lft forever");
368 else
369 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
370 if (ci->ifa_prefered == 0xFFFFFFFFU)
371 sprintf(buf+strlen(buf), " preferred_lft forever");
372 else
373 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000374 printf(" %s", buf);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000375 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000376 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100377 /*fflush_all();*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000378 return 0;
379}
380
381
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200382struct nlmsg_list {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000383 struct nlmsg_list *next;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200384 struct nlmsghdr h;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000385};
386
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000387static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000388{
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000389 for (; ainfo; ainfo = ainfo->next) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000390 struct nlmsghdr *n = &ainfo->h;
391 struct ifaddrmsg *ifa = NLMSG_DATA(n);
392
393 if (n->nlmsg_type != RTM_NEWADDR)
394 continue;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000395 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
396 return -1;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100397 if (ifa->ifa_index != ifindex
398 || (G_filter.family && G_filter.family != ifa->ifa_family)
399 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000400 continue;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100401 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000402 print_addrinfo(NULL, n, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000403 }
404 return 0;
405}
406
407
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200408static int FAST_FUNC store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000409{
410 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
411 struct nlmsg_list *h;
412 struct nlmsg_list **lp;
413
Denys Vlasenko90377872010-01-08 09:07:50 +0100414 h = xzalloc(n->nlmsg_len + sizeof(void*));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000415
416 memcpy(&h->h, n, n->nlmsg_len);
Denys Vlasenko90377872010-01-08 09:07:50 +0100417 /*h->next = NULL; - xzalloc did it */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000418
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000419 for (lp = linfo; *lp; lp = &(*lp)->next)
420 continue;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000421 *lp = h;
422
423 ll_remember_index(who, n, NULL);
424 return 0;
425}
426
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000427static void ipaddr_reset_filter(int _oneline)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000428{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100429 memset(&G_filter, 0, sizeof(G_filter));
430 G_filter.oneline = _oneline;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000431}
432
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000433/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200434int FAST_FUNC ipaddr_list_or_flush(char **argv, int flush)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000435{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000436 static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000437
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000438 struct nlmsg_list *linfo = NULL;
439 struct nlmsg_list *ainfo = NULL;
440 struct nlmsg_list *l;
441 struct rtnl_handle rth;
442 char *filter_dev = NULL;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000443
444 ipaddr_reset_filter(oneline);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100445 G_filter.showqueue = 1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000446
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100447 if (G_filter.family == AF_UNSPEC)
448 G_filter.family = preferred_family;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000449
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000450 if (flush) {
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000451 if (!*argv) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000452 bb_error_msg_and_die(bb_msg_requires_arg, "flush");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000453 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100454 if (G_filter.family == AF_PACKET) {
James Byrne69374872019-07-02 11:35:03 +0200455 bb_simple_error_msg_and_die("can't flush link addresses");
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000456 }
457 }
458
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000459 while (*argv) {
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200460 const smalluint key = index_in_strings(option, *argv);
461 if (key == 0) { /* to */
Denys Vlasenkofd744512010-07-04 03:55:43 +0200462 NEXT_ARG();
463 get_prefix(&G_filter.pfx, *argv, G_filter.family);
464 if (G_filter.family == AF_UNSPEC) {
465 G_filter.family = G_filter.pfx.family;
466 }
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200467 } else if (key == 1) { /* scope */
Denys Vlasenkofd744512010-07-04 03:55:43 +0200468 uint32_t scope = 0;
469 NEXT_ARG();
470 G_filter.scopemask = -1;
471 if (rtnl_rtscope_a2n(&scope, *argv)) {
472 if (strcmp(*argv, "all") != 0) {
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200473 invarg_1_to_2(*argv, "scope");
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000474 }
Denys Vlasenkofd744512010-07-04 03:55:43 +0200475 scope = RT_SCOPE_NOWHERE;
476 G_filter.scopemask = 0;
477 }
478 G_filter.scope = scope;
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200479 } else if (key == 2) { /* up */
Denys Vlasenkofd744512010-07-04 03:55:43 +0200480 G_filter.up = 1;
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200481 } else if (key == 3) { /* label */
Denys Vlasenkofd744512010-07-04 03:55:43 +0200482 NEXT_ARG();
483 G_filter.label = *argv;
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200484 } else {
485 if (key == 4) /* dev */
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000486 NEXT_ARG();
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200487 if (filter_dev)
488 duparg2("dev", *argv);
489 filter_dev = *argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000490 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000491 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000492 }
493
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000494 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000495
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000496 xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
497 xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000498
499 if (filter_dev) {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100500 G_filter.ifindex = xll_name_to_index(filter_dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000501 }
502
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000503 if (flush) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000504 char flushb[4096-512];
505
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100506 G_filter.flushb = flushb;
507 G_filter.flushp = 0;
508 G_filter.flushe = sizeof(flushb);
509 G_filter.rth = &rth;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000510
511 for (;;) {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100512 xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
513 G_filter.flushed = 0;
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000514 xrtnl_dump_filter(&rth, print_addrinfo, NULL);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100515 if (G_filter.flushed == 0) {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000516 return 0;
517 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100518 if (flush_update() < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000519 return 1;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100520 }
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000521 }
522 }
523
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100524 if (G_filter.family != AF_PACKET) {
525 xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000526 xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000527 }
528
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100529 if (G_filter.family && G_filter.family != AF_PACKET) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000530 struct nlmsg_list **lp;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000531 lp = &linfo;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000532
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000533 while ((l = *lp) != NULL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000534 int ok = 0;
535 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
536 struct nlmsg_list *a;
537
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000538 for (a = ainfo; a; a = a->next) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000539 struct nlmsghdr *n = &a->h;
540 struct ifaddrmsg *ifa = NLMSG_DATA(n);
541
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100542 if (ifa->ifa_index != ifi->ifi_index
543 || (G_filter.family && G_filter.family != ifa->ifa_family)
544 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000545 continue;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100546 }
547 if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000548 continue;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100549 if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000550 continue;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100551 if (G_filter.pfx.family || G_filter.label) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000552 struct rtattr *tb[IFA_MAX+1];
Denys Vlasenko68ae5422018-02-08 08:42:37 +0100553 //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000554 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
555 if (!tb[IFA_LOCAL])
556 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
557
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100558 if (G_filter.pfx.family && tb[IFA_LOCAL]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000559 inet_prefix dst;
560 memset(&dst, 0, sizeof(dst));
561 dst.family = ifa->ifa_family;
562 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100563 if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000564 continue;
565 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100566 if (G_filter.label) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000567 const char *label;
568 if (tb[IFA_LABEL])
569 label = RTA_DATA(tb[IFA_LABEL]);
570 else
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200571 label = ll_index_to_name(ifa->ifa_index);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100572 if (fnmatch(G_filter.label, label, 0) != 0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000573 continue;
574 }
575 }
576
577 ok = 1;
578 break;
579 }
580 if (!ok)
581 *lp = l->next;
582 else
583 lp = &l->next;
584 }
585 }
586
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000587 for (l = linfo; l; l = l->next) {
Stefan Sørensenbb3a9532019-03-30 18:24:46 +0100588 if ((oneline && G_filter.family != AF_PACKET)
Denys Vlasenkodb169f22018-03-08 15:55:07 +0100589 /* ^^^^^^^^^ "ip -oneline a" does not print link info */
Denys Vlasenko13f42042019-04-29 00:34:07 +0200590 || (print_linkinfo(&l->h) == 0)
Denys Vlasenkodb169f22018-03-08 15:55:07 +0100591 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000592 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100593 if (G_filter.family != AF_PACKET)
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000594 print_selected_addrinfo(ifi->ifi_index, ainfo);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000595 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000596 }
597
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000598 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000599}
600
Christian Eggers8a485b02020-06-29 17:57:26 +0200601static void set_lifetime(unsigned int *lifetime, char *argv, const char *errmsg)
602{
603 if (strcmp(argv, "forever") == 0)
604 *lifetime = INFINITY_LIFE_TIME;
605 else
606 *lifetime = get_u32(argv, errmsg);
607}
608
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000609static int default_scope(inet_prefix *lcl)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000610{
611 if (lcl->family == AF_INET) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000612 if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000613 return RT_SCOPE_HOST;
614 }
615 return 0;
616}
617
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000618/* Return value becomes exitcode. It's okay to not return at all */
Michael Tokarev6a7cd3d2015-05-20 16:27:44 +0300619static int ipaddr_modify(int cmd, int flags, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000620{
Denys Vlasenko4eaa0f72017-04-07 18:14:46 +0200621 /* If you add stuff here, update ipaddr_full_usage */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000622 static const char option[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000623 "peer\0""remote\0""broadcast\0""brd\0"
Christian Eggers8a485b02020-06-29 17:57:26 +0200624 "anycast\0""valid_lft\0""preferred_lft\0"
625 "scope\0""dev\0""label\0""noprefixroute\0""local\0";
Denys Vlasenko4eaa0f72017-04-07 18:14:46 +0200626#define option_peer option
627#define option_broadcast (option + sizeof("peer") + sizeof("remote"))
628#define option_anycast (option_broadcast + sizeof("broadcast") + sizeof("brd"))
Christian Eggers8a485b02020-06-29 17:57:26 +0200629#define option_valid_lft (option_anycast + sizeof("anycast"))
630#define option_pref_lft (option_valid_lft + sizeof("valid_lft"))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000631 struct rtnl_handle rth;
632 struct {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000633 struct nlmsghdr n;
634 struct ifaddrmsg ifa;
635 char buf[256];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000636 } req;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000637 char *d = NULL;
638 char *l = NULL;
Christian Eggers8a485b02020-06-29 17:57:26 +0200639 char *valid_lftp = NULL;
640 char *preferred_lftp = NULL;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000641 inet_prefix lcl;
642 inet_prefix peer;
643 int local_len = 0;
644 int peer_len = 0;
645 int brd_len = 0;
646 int any_len = 0;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000647 bool scoped = 0;
Christian Eggers8a485b02020-06-29 17:57:26 +0200648 __u32 valid_lft = INFINITY_LIFE_TIME;
649 __u32 preferred_lft = INFINITY_LIFE_TIME;
Christian Eggers31d34f32020-06-29 17:57:25 +0200650 unsigned int ifa_flags = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000651
652 memset(&req, 0, sizeof(req));
653
654 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
Michael Tokarev6a7cd3d2015-05-20 16:27:44 +0300655 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000656 req.n.nlmsg_type = cmd;
657 req.ifa.ifa_family = preferred_family;
658
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000659 while (*argv) {
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200660 unsigned arg = index_in_strings(option, *argv);
661 /* if search fails, "local" is assumed */
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000662
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200663 if (arg <= 1) { /* peer, remote */
Christian Eggers8a485b02020-06-29 17:57:26 +0200664 NEXT_ARG();
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200665 if (peer_len) {
Denys Vlasenko4eaa0f72017-04-07 18:14:46 +0200666 duparg(option_peer, *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000667 }
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200668 get_prefix(&peer, *argv, req.ifa.ifa_family);
669 peer_len = peer.bytelen;
670 if (req.ifa.ifa_family == AF_UNSPEC) {
671 req.ifa.ifa_family = peer.family;
672 }
673 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
674 req.ifa.ifa_prefixlen = peer.bitlen;
675 } else if (arg <= 3) { /* broadcast, brd */
676 inet_prefix addr;
Christian Eggers8a485b02020-06-29 17:57:26 +0200677 NEXT_ARG();
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200678 if (brd_len) {
Denys Vlasenko4eaa0f72017-04-07 18:14:46 +0200679 duparg(option_broadcast, *argv);
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200680 }
681 if (LONE_CHAR(*argv, '+')) {
682 brd_len = -1;
683 } else if (LONE_DASH(*argv)) {
684 brd_len = -2;
685 } else {
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000686 get_addr(&addr, *argv, req.ifa.ifa_family);
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200687 if (req.ifa.ifa_family == AF_UNSPEC)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000688 req.ifa.ifa_family = addr.family;
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200689 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
690 brd_len = addr.bytelen;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000691 }
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200692 } else if (arg == 4) { /* anycast */
693 inet_prefix addr;
Christian Eggers8a485b02020-06-29 17:57:26 +0200694 NEXT_ARG();
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200695 if (any_len) {
Denys Vlasenko4eaa0f72017-04-07 18:14:46 +0200696 duparg(option_anycast, *argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000697 }
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200698 get_addr(&addr, *argv, req.ifa.ifa_family);
699 if (req.ifa.ifa_family == AF_UNSPEC) {
700 req.ifa.ifa_family = addr.family;
701 }
702 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
703 any_len = addr.bytelen;
Christian Eggers8a485b02020-06-29 17:57:26 +0200704 } else if (arg == 5) { /* valid_lft */
705 if (valid_lftp)
706 duparg(option_valid_lft, *argv);
707 NEXT_ARG();
708 valid_lftp = *argv;
709 set_lifetime(&valid_lft, *argv, option_valid_lft);
710 } else if (arg == 6) { /* preferred_lft */
711 if (preferred_lftp)
712 duparg(option_pref_lft, *argv);
713 NEXT_ARG();
714 preferred_lftp = *argv;
715 set_lifetime(&preferred_lft, *argv, option_pref_lft);
716 } else if (arg == 7) { /* scope */
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200717 uint32_t scope = 0;
Christian Eggers8a485b02020-06-29 17:57:26 +0200718 NEXT_ARG();
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200719 if (rtnl_rtscope_a2n(&scope, *argv)) {
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200720 invarg_1_to_2(*argv, "scope");
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200721 }
722 req.ifa.ifa_scope = scope;
723 scoped = 1;
Christian Eggers8a485b02020-06-29 17:57:26 +0200724 } else if (arg == 8) { /* dev */
725 NEXT_ARG();
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200726 d = *argv;
Christian Eggers8a485b02020-06-29 17:57:26 +0200727 } else if (arg == 9) { /* label */
728 NEXT_ARG();
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200729 l = *argv;
Denys Vlasenkofd744512010-07-04 03:55:43 +0200730 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l) + 1);
Christian Eggers8a485b02020-06-29 17:57:26 +0200731 } else if (arg == 10) { /* noprefixroute */
Christian Eggers31d34f32020-06-29 17:57:25 +0200732 ifa_flags |= IFA_F_NOPREFIXROUTE;
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200733 } else {
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200734 /* local (specified or assumed) */
Christian Eggers8a485b02020-06-29 17:57:26 +0200735 if ((int)arg >= 0)
736 NEXT_ARG();
Bernhard Reutner-Fischerc5f30c02010-05-25 18:46:09 +0200737 if (local_len) {
738 duparg2("local", *argv);
739 }
740 get_prefix(&lcl, *argv, req.ifa.ifa_family);
741 if (req.ifa.ifa_family == AF_UNSPEC) {
742 req.ifa.ifa_family = lcl.family;
743 }
744 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
745 local_len = lcl.bytelen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000746 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000747 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000748 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000749
Christian Eggers31d34f32020-06-29 17:57:25 +0200750 if (ifa_flags <= 0xff)
751 req.ifa.ifa_flags = ifa_flags;
752 else
753 addattr32(&req.n, sizeof(req), IFA_FLAGS, ifa_flags);
754
Denys Vlasenkofd744512010-07-04 03:55:43 +0200755 if (!d) {
756 /* There was no "dev IFACE", but we need that */
James Byrne69374872019-07-02 11:35:03 +0200757 bb_simple_error_msg_and_die("need \"dev IFACE\"");
Denys Vlasenkofd744512010-07-04 03:55:43 +0200758 }
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100759 if (l && !is_prefixed_with(l, d)) {
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000760 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000761 }
762
763 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
764 peer = lcl;
765 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
766 }
767 if (req.ifa.ifa_prefixlen == 0)
768 req.ifa.ifa_prefixlen = lcl.bitlen;
769
770 if (brd_len < 0 && cmd != RTM_DELADDR) {
771 inet_prefix brd;
772 int i;
773 if (req.ifa.ifa_family != AF_INET) {
James Byrne69374872019-07-02 11:35:03 +0200774 bb_simple_error_msg_and_die("broadcast can be set only for IPv4 addresses");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000775 }
776 brd = peer;
777 if (brd.bitlen <= 30) {
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200778 for (i = 31; i >= brd.bitlen; i--) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000779 if (brd_len == -1)
780 brd.data[0] |= htonl(1<<(31-i));
781 else
782 brd.data[0] &= ~htonl(1<<(31-i));
783 }
784 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
785 brd_len = brd.bytelen;
786 }
787 }
788 if (!scoped && cmd != RTM_DELADDR)
789 req.ifa.ifa_scope = default_scope(&lcl);
790
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000791 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000792
793 ll_init_map(&rth);
794
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000795 req.ifa.ifa_index = xll_name_to_index(d);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000796
Christian Eggers8a485b02020-06-29 17:57:26 +0200797 if (valid_lftp || preferred_lftp) {
798 struct ifa_cacheinfo cinfo = {};
799
800 if (!valid_lft) {
801 fprintf(stderr, "valid_lft is zero\n");
802 return 1;
803 }
804 if (valid_lft < preferred_lft) {
805 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
806 return 1;
807 }
808
809 cinfo.ifa_prefered = preferred_lft;
810 cinfo.ifa_valid = valid_lft;
811 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
812 sizeof(cinfo));
813 }
814
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000815 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000816 return 2;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000817
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000818 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000819}
820
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000821/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200822int FAST_FUNC do_ipaddr(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000823{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000824 static const char commands[] ALIGN1 =
Michael Tokarev6a7cd3d2015-05-20 16:27:44 +0300825 /* 0 1 2 3 4 5 6 7 8 */
826 "add\0""change\0""chg\0""replace\0""delete\0""list\0""show\0""lst\0""flush\0";
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200827 int cmd = 2;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200828
829 INIT_G();
830
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000831 if (*argv) {
Bernhard Reutner-Fischercc4493a2010-05-25 17:42:01 +0200832 cmd = index_in_substrings(commands, *argv);
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200833 if (cmd < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200834 invarg_1_to_2(*argv, applet_name);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000835 argv++;
Denys Vlasenkodb31c632015-07-01 18:36:06 +0200836 if (cmd <= 4) {
837 return ipaddr_modify(
838 /*cmd:*/ cmd == 4 ? RTM_DELADDR : RTM_NEWADDR,
839 /*flags:*/
840 cmd == 0 ? NLM_F_CREATE|NLM_F_EXCL : /* add */
841 cmd == 1 || cmd == 2 ? NLM_F_REPLACE : /* change */
842 cmd == 3 ? NLM_F_CREATE|NLM_F_REPLACE : /* replace */
843 0 /* delete */
844 , argv);
845 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000846 }
Michael Tokarev6a7cd3d2015-05-20 16:27:44 +0300847 return ipaddr_list_or_flush(argv, cmd == 8);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000848}