blob: f572414e9ace3ac1c0ee858773a14966d838c572 [file] [log] [blame]
Curt Brune69934702015-10-14 12:53:47 +02001/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 *
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 *
7 * Ported to Busybox by: Curt Brune <curt@cumulusnetworks.com>
8 */
Curt Brune69934702015-10-14 12:53:47 +02009#include "ip_common.h" /* #include "libbb.h" is inside */
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020010#include "common_bufsiz.h"
Curt Brune69934702015-10-14 12:53:47 +020011#include "rt_names.h"
12#include "utils.h"
13#include <linux/neighbour.h>
14#include <net/if_arp.h>
15
16//static int xshow_stats = 3;
17enum { xshow_stats = 3 };
18
19static inline uint32_t rta_getattr_u32(const struct rtattr *rta)
20{
21 return *(uint32_t *)RTA_DATA(rta);
22}
23
24#ifndef RTAX_RTTVAR
25#define RTAX_RTTVAR RTAX_HOPS
26#endif
27
28
29struct filter_t {
30 int family;
31 int index;
32 int state;
33 int unused_only;
34 inet_prefix pfx;
35 int flushed;
36 char *flushb;
37 int flushp;
38 int flushe;
39 struct rtnl_handle *rth;
40} FIX_ALIASING;
41typedef struct filter_t filter_t;
42
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020043#define G_filter (*(filter_t*)bb_common_bufsiz1)
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020044#define INIT_G() do { setup_common_bufsiz(); } while (0)
Curt Brune69934702015-10-14 12:53:47 +020045
46static int flush_update(void)
47{
48 if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
49 bb_perror_msg("can't send flush request");
50 return -1;
51 }
52 G_filter.flushp = 0;
53 return 0;
54}
55
56static unsigned nud_state_a2n(char *arg)
57{
58 static const char keywords[] ALIGN1 =
59 /* "ip neigh show/flush" parameters: */
60 "permanent\0" "reachable\0" "noarp\0" "none\0"
61 "stale\0" "incomplete\0" "delay\0" "probe\0"
62 "failed\0"
63 ;
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020064 static uint8_t nuds[] ALIGN1 = {
Curt Brune69934702015-10-14 12:53:47 +020065 NUD_PERMANENT,NUD_REACHABLE, NUD_NOARP,NUD_NONE,
66 NUD_STALE, NUD_INCOMPLETE,NUD_DELAY,NUD_PROBE,
67 NUD_FAILED
68 };
69 int id;
70
71 BUILD_BUG_ON(
72 (NUD_PERMANENT|NUD_REACHABLE| NUD_NOARP|NUD_NONE|
73 NUD_STALE| NUD_INCOMPLETE|NUD_DELAY|NUD_PROBE|
74 NUD_FAILED) > 0xff
75 );
76
77 id = index_in_substrings(keywords, arg);
78 if (id < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +020079 bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, "nud state");
Curt Brune69934702015-10-14 12:53:47 +020080 return nuds[id];
81}
82
83#ifndef NDA_RTA
84#define NDA_RTA(r) \
85 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
86#endif
87
88
89static int FAST_FUNC print_neigh(const struct sockaddr_nl *who UNUSED_PARAM,
90 struct nlmsghdr *n, void *arg UNUSED_PARAM)
91{
92 struct ndmsg *r = NLMSG_DATA(n);
93 int len = n->nlmsg_len;
94 struct rtattr *tb[NDA_MAX+1];
Curt Brune69934702015-10-14 12:53:47 +020095
96 if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH) {
97 bb_error_msg_and_die("not RTM_NEWNEIGH: %08x %08x %08x",
98 n->nlmsg_len, n->nlmsg_type,
99 n->nlmsg_flags);
100 }
101 len -= NLMSG_LENGTH(sizeof(*r));
102 if (len < 0) {
103 bb_error_msg_and_die("BUG: wrong nlmsg len %d", len);
104 }
105
106 if (G_filter.flushb && n->nlmsg_type != RTM_NEWNEIGH)
107 return 0;
108
109 if (G_filter.family && G_filter.family != r->ndm_family)
110 return 0;
111 if (G_filter.index && G_filter.index != r->ndm_ifindex)
112 return 0;
Denys Vlasenko68ae5422018-02-08 08:42:37 +0100113 if (!(G_filter.state&r->ndm_state)
114 && !(r->ndm_flags & NTF_PROXY)
115 && (r->ndm_state || !(G_filter.state & 0x100))
116 && (r->ndm_family != AF_DECnet)
117 ) {
Curt Brune69934702015-10-14 12:53:47 +0200118 return 0;
Denys Vlasenko68ae5422018-02-08 08:42:37 +0100119 }
Curt Brune69934702015-10-14 12:53:47 +0200120
121 parse_rtattr(tb, NDA_MAX, NDA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
122
123 if (tb[NDA_DST]) {
124 if (G_filter.pfx.family) {
125 inet_prefix dst;
126 memset(&dst, 0, sizeof(dst));
127 dst.family = r->ndm_family;
128 memcpy(&dst.data, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
129 if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
130 return 0;
131 }
132 }
133 if (G_filter.unused_only && tb[NDA_CACHEINFO]) {
134 struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
135 if (ci->ndm_refcnt)
136 return 0;
137 }
138
139 if (G_filter.flushb) {
140 struct nlmsghdr *fn;
141 if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
142 if (flush_update())
143 return -1;
144 }
145 fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
146 memcpy(fn, n, n->nlmsg_len);
147 fn->nlmsg_type = RTM_DELNEIGH;
148 fn->nlmsg_flags = NLM_F_REQUEST;
149 fn->nlmsg_seq = ++(G_filter.rth->seq);
150 G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
151 G_filter.flushed++;
152 if (xshow_stats < 2)
153 return 0;
154 }
155
156 if (tb[NDA_DST]) {
157 printf("%s ",
158 format_host(r->ndm_family,
159 RTA_PAYLOAD(tb[NDA_DST]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200160 RTA_DATA(tb[NDA_DST]))
161 );
Curt Brune69934702015-10-14 12:53:47 +0200162 }
163 if (!G_filter.index && r->ndm_ifindex)
164 printf("dev %s ", ll_index_to_name(r->ndm_ifindex));
165 if (tb[NDA_LLADDR]) {
166 SPRINT_BUF(b1);
167 printf("lladdr %s", ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]),
168 RTA_PAYLOAD(tb[NDA_LLADDR]),
169 ARPHRD_ETHER,
170 b1, sizeof(b1)));
171 }
172 if (r->ndm_flags & NTF_ROUTER) {
173 printf(" router");
174 }
175 if (r->ndm_flags & NTF_PROXY) {
176 printf(" proxy");
177 }
178 if (tb[NDA_CACHEINFO] && xshow_stats) {
179 struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
180 int hz = get_hz();
181
182 if (ci->ndm_refcnt)
183 printf(" ref %d", ci->ndm_refcnt);
184 printf(" used %d/%d/%d", ci->ndm_used/hz,
185 ci->ndm_confirmed/hz, ci->ndm_updated/hz);
186 }
187
188 if (tb[NDA_PROBES] && xshow_stats) {
189 uint32_t p = rta_getattr_u32(tb[NDA_PROBES]);
190 printf(" probes %u", p);
191 }
192
193 /*if (r->ndm_state)*/ {
194 int nud = r->ndm_state;
195 char c = ' ';
196#define PRINT_FLAG(f) \
197 if (nud & NUD_##f) { \
198 printf("%c"#f, c); \
199 c = ','; \
200 }
201 PRINT_FLAG(INCOMPLETE);
202 PRINT_FLAG(REACHABLE);
203 PRINT_FLAG(STALE);
204 PRINT_FLAG(DELAY);
205 PRINT_FLAG(PROBE);
206 PRINT_FLAG(FAILED);
207 PRINT_FLAG(NOARP);
208 PRINT_FLAG(PERMANENT);
209#undef PRINT_FLAG
210 }
211 bb_putchar('\n');
212
213 return 0;
214}
215
216static void ipneigh_reset_filter(void)
217{
218 memset(&G_filter, 0, sizeof(G_filter));
219 G_filter.state = ~0;
220}
221
222#define MAX_ROUNDS 10
223/* Return value becomes exitcode. It's okay to not return at all */
224static int FAST_FUNC ipneigh_list_or_flush(char **argv, int flush)
225{
226 static const char keywords[] ALIGN1 =
227 /* "ip neigh show/flush" parameters: */
228 "to\0" "dev\0" "nud\0";
229 enum {
230 KW_to, KW_dev, KW_nud,
231 };
232 struct rtnl_handle rth;
233 struct ndmsg ndm = { 0 };
234 char *filter_dev = NULL;
235 int state_given = 0;
236 int arg;
237
238 ipneigh_reset_filter();
239
240 if (flush && !*argv)
241 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip neigh flush\"");
242
243 if (!G_filter.family)
244 G_filter.family = preferred_family;
245
246 G_filter.state = (flush) ?
247 ~(NUD_PERMANENT|NUD_NOARP) : 0xFF & ~NUD_NOARP;
248
249 while (*argv) {
250 arg = index_in_substrings(keywords, *argv);
251 if (arg == KW_dev) {
252 NEXT_ARG();
253 filter_dev = *argv;
254 } else if (arg == KW_nud) {
255 unsigned state;
256 NEXT_ARG();
257 if (!state_given) {
258 state_given = 1;
259 G_filter.state = 0;
260 }
261 if (strcmp(*argv, "all") == 0) {
262 state = ~0;
263 if (flush)
264 state &= ~NUD_NOARP;
265 } else {
266 state = nud_state_a2n(*argv);
267 }
268 if (state == 0)
269 state = 0x100;
270 G_filter.state |= state;
271 } else {
272 if (arg == KW_to) {
273 NEXT_ARG();
274 }
275 get_prefix(&G_filter.pfx, *argv, G_filter.family);
276 if (G_filter.family == AF_UNSPEC)
277 G_filter.family = G_filter.pfx.family;
278 }
279 argv++;
280 }
281
282 xrtnl_open(&rth);
283 ll_init_map(&rth);
284
285 if (filter_dev) {
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200286 G_filter.index = xll_name_to_index(filter_dev);
287 if (G_filter.index == 0) {
288 bb_error_msg_and_die("can't find device '%s'", filter_dev);
Curt Brune69934702015-10-14 12:53:47 +0200289 }
290 }
291
292 if (flush) {
293 int round = 0;
294 char flushb[4096-512];
295 G_filter.flushb = flushb;
296 G_filter.flushp = 0;
297 G_filter.flushe = sizeof(flushb);
298 G_filter.state &= ~NUD_FAILED;
299 G_filter.rth = &rth;
300
301 while (round < MAX_ROUNDS) {
302 if (xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETNEIGH) < 0) {
303 bb_perror_msg_and_die("can't send dump request");
304 }
305 G_filter.flushed = 0;
306 if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
307 bb_perror_msg_and_die("flush terminated");
308 }
309 if (G_filter.flushed == 0) {
310 if (round == 0)
311 puts("Nothing to flush");
312 else
313 printf("*** Flush is complete after %d round(s) ***\n", round);
314 return 0;
315 }
316 round++;
317 if (flush_update() < 0)
318 xfunc_die();
319 printf("\n*** Round %d, deleting %d entries ***\n", round, G_filter.flushed);
320 }
321 bb_error_msg_and_die("*** Flush not complete bailing out after %d rounds", MAX_ROUNDS);
322 }
323
324 ndm.ndm_family = G_filter.family;
325
326 if (rtnl_dump_request(&rth, RTM_GETNEIGH, &ndm, sizeof(struct ndmsg)) < 0) {
327 bb_perror_msg_and_die("can't send dump request");
328 }
329
330 if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
331 bb_error_msg_and_die("dump terminated");
332 }
333
334 return 0;
335}
336
337/* Return value becomes exitcode. It's okay to not return at all */
338int FAST_FUNC do_ipneigh(char **argv)
339{
340 static const char ip_neigh_commands[] ALIGN1 =
341 /*0-1*/ "show\0" "flush\0";
342 int command_num;
343
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200344 INIT_G();
345
Curt Brune69934702015-10-14 12:53:47 +0200346 if (!*argv)
347 return ipneigh_list_or_flush(argv, 0);
348
349 command_num = index_in_substrings(ip_neigh_commands, *argv);
350 switch (command_num) {
351 case 0: /* show */
352 return ipneigh_list_or_flush(argv + 1, 0);
353 case 1: /* flush */
354 return ipneigh_list_or_flush(argv + 1, 1);
355 }
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200356 invarg_1_to_2(*argv, applet_name);
Curt Brune69934702015-10-14 12:53:47 +0200357 return 1;
358}