1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
7 * Ported to Busybox by: Curt Brune <curt@cumulusnetworks.com>
9 #include "ip_common.h" /* #include "libbb.h" is inside */
10 #include "common_bufsiz.h"
13 #include <linux/neighbour.h>
14 #include <net/if_arp.h>
16 //static int xshow_stats = 3;
17 enum { xshow_stats = 3 };
19 static inline uint32_t rta_getattr_u32(const struct rtattr *rta)
21 return *(uint32_t *)RTA_DATA(rta);
25 #define RTAX_RTTVAR RTAX_HOPS
35 /* Misnomer. Does not mean "flushed N something" */
36 /* More like "no_of_flush_commands_constructed_by_print_neigh()" */
38 /* Flush cmd buf. If !NULL, print_neigh() constructs flush commands in it */
42 struct rtnl_handle *rth;
44 typedef struct filter_t filter_t;
46 #define G_filter (*(filter_t*)bb_common_bufsiz1)
47 #define INIT_G() do { setup_common_bufsiz(); } while (0)
49 static int flush_update(void)
51 if (rtnl_send_check(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
52 bb_simple_perror_msg("can't send flush request");
59 static unsigned nud_state_a2n(char *arg)
61 static const char keywords[] ALIGN1 =
62 /* "ip neigh show/flush" parameters: */
63 "permanent\0" "reachable\0" "noarp\0" "none\0"
64 "stale\0" "incomplete\0" "delay\0" "probe\0"
67 static uint8_t nuds[] ALIGN1 = {
68 NUD_PERMANENT,NUD_REACHABLE, NUD_NOARP,NUD_NONE,
69 NUD_STALE, NUD_INCOMPLETE,NUD_DELAY,NUD_PROBE,
75 (NUD_PERMANENT|NUD_REACHABLE| NUD_NOARP|NUD_NONE|
76 NUD_STALE| NUD_INCOMPLETE|NUD_DELAY|NUD_PROBE|
80 id = index_in_substrings(keywords, arg);
82 bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, "nud state");
88 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
92 static int FAST_FUNC print_neigh(const struct sockaddr_nl *who UNUSED_PARAM,
93 struct nlmsghdr *n, void *arg UNUSED_PARAM)
95 struct ndmsg *r = NLMSG_DATA(n);
96 int len = n->nlmsg_len;
97 struct rtattr *tb[NDA_MAX+1];
99 if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH) {
100 bb_error_msg_and_die("not RTM_NEWNEIGH: %08x %08x %08x",
101 n->nlmsg_len, n->nlmsg_type,
104 len -= NLMSG_LENGTH(sizeof(*r));
106 bb_error_msg_and_die("BUG: wrong nlmsg len %d", len);
109 if (G_filter.flushb && n->nlmsg_type != RTM_NEWNEIGH)
112 if (G_filter.family && G_filter.family != r->ndm_family)
114 if (G_filter.index && G_filter.index != r->ndm_ifindex)
116 if (!(G_filter.state&r->ndm_state)
117 && !(r->ndm_flags & NTF_PROXY)
118 && (r->ndm_state || !(G_filter.state & 0x100))
119 && (r->ndm_family != AF_DECnet)
124 parse_rtattr(tb, NDA_MAX, NDA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
127 if (G_filter.pfx.family) {
129 memset(&dst, 0, sizeof(dst));
130 dst.family = r->ndm_family;
131 memcpy(&dst.data, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
132 if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
136 if (G_filter.unused_only && tb[NDA_CACHEINFO]) {
137 struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
142 if (G_filter.flushb) {
144 if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
148 fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
149 memcpy(fn, n, n->nlmsg_len);
150 fn->nlmsg_type = RTM_DELNEIGH;
151 fn->nlmsg_flags = NLM_F_REQUEST;
152 fn->nlmsg_seq = ++(G_filter.rth->seq);
153 G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
161 format_host(r->ndm_family,
162 RTA_PAYLOAD(tb[NDA_DST]),
163 RTA_DATA(tb[NDA_DST]))
166 if (!G_filter.index && r->ndm_ifindex)
167 printf("dev %s ", ll_index_to_name(r->ndm_ifindex));
168 if (tb[NDA_LLADDR]) {
170 printf("lladdr %s", ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]),
171 RTA_PAYLOAD(tb[NDA_LLADDR]),
175 if (r->ndm_flags & NTF_ROUTER) {
178 if (r->ndm_flags & NTF_PROXY) {
181 if (tb[NDA_CACHEINFO] && xshow_stats) {
182 struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
186 printf(" ref %d", ci->ndm_refcnt);
187 printf(" used %d/%d/%d", ci->ndm_used/hz,
188 ci->ndm_confirmed/hz, ci->ndm_updated/hz);
191 if (tb[NDA_PROBES] && xshow_stats) {
192 uint32_t p = rta_getattr_u32(tb[NDA_PROBES]);
193 printf(" probes %u", p);
196 /*if (r->ndm_state)*/ {
197 int nud = r->ndm_state;
199 #define PRINT_FLAG(f) \
200 if (nud & NUD_##f) { \
204 PRINT_FLAG(INCOMPLETE);
205 PRINT_FLAG(REACHABLE);
211 PRINT_FLAG(PERMANENT);
219 static void ipneigh_reset_filter(void)
221 memset(&G_filter, 0, sizeof(G_filter));
225 #define MAX_ROUNDS 10
226 /* Return value becomes exitcode. It's okay to not return at all */
227 static int FAST_FUNC ipneigh_list_or_flush(char **argv, int flush)
229 static const char keywords[] ALIGN1 =
230 /* "ip neigh show/flush" parameters: */
231 "to\0" "dev\0" "nud\0";
233 KW_to, KW_dev, KW_nud,
235 struct rtnl_handle rth;
236 struct ndmsg ndm = { 0 };
237 char *filter_dev = NULL;
241 ipneigh_reset_filter();
244 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip neigh flush\"");
246 if (!G_filter.family)
247 G_filter.family = preferred_family;
249 G_filter.state = (flush) ?
250 ~(NUD_PERMANENT|NUD_NOARP) : 0xFF & ~NUD_NOARP;
253 arg = index_in_substrings(keywords, *argv);
257 } else if (arg == KW_nud) {
264 if (strcmp(*argv, "all") == 0) {
269 state = nud_state_a2n(*argv);
273 G_filter.state |= state;
278 get_prefix(&G_filter.pfx, *argv, G_filter.family);
279 if (G_filter.family == AF_UNSPEC)
280 G_filter.family = G_filter.pfx.family;
289 G_filter.index = xll_name_to_index(filter_dev);
290 if (G_filter.index == 0) {
291 bb_error_msg_and_die("can't find device '%s'", filter_dev);
297 char flushb[4096-512];
298 G_filter.flushb = flushb;
300 G_filter.flushe = sizeof(flushb);
301 G_filter.state &= ~NUD_FAILED;
304 while (round < MAX_ROUNDS) {
305 xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETNEIGH);
306 G_filter.flushed = 0;
307 if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
308 bb_simple_perror_msg_and_die("flush terminated");
310 if (G_filter.flushed == 0) {
312 puts("Nothing to flush");
314 printf("*** Flush is complete after %d round(s) ***\n", round);
318 if (flush_update() < 0)
320 printf("\n*** Round %d, deleting %d entries ***\n", round, G_filter.flushed);
322 bb_error_msg_and_die("*** Flush not complete bailing out after %d rounds", MAX_ROUNDS);
325 ndm.ndm_family = G_filter.family;
327 if (rtnl_dump_request(&rth, RTM_GETNEIGH, &ndm, sizeof(struct ndmsg)) < 0) {
328 bb_simple_perror_msg_and_die("can't send dump request");
331 if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
332 bb_simple_error_msg_and_die("dump terminated");
338 /* Return value becomes exitcode. It's okay to not return at all */
339 int FAST_FUNC do_ipneigh(char **argv)
341 static const char ip_neigh_commands[] ALIGN1 =
342 /*0-1*/ "show\0" "flush\0";
348 return ipneigh_list_or_flush(argv, 0);
350 command_num = index_in_substrings(ip_neigh_commands, *argv);
351 switch (command_num) {
353 return ipneigh_list_or_flush(argv + 1, 0);
355 return ipneigh_list_or_flush(argv + 1, 1);
357 invarg_1_to_2(*argv, applet_name);