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
39 struct rtnl_handle *rth;
41 typedef struct filter_t filter_t;
43 #define G_filter (*(filter_t*)bb_common_bufsiz1)
44 #define INIT_G() do { setup_common_bufsiz(); } while (0)
46 static int flush_update(void)
48 if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
49 bb_perror_msg("can't send flush request");
56 static unsigned nud_state_a2n(char *arg)
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"
64 static uint8_t nuds[] ALIGN1 = {
65 NUD_PERMANENT,NUD_REACHABLE, NUD_NOARP,NUD_NONE,
66 NUD_STALE, NUD_INCOMPLETE,NUD_DELAY,NUD_PROBE,
72 (NUD_PERMANENT|NUD_REACHABLE| NUD_NOARP|NUD_NONE|
73 NUD_STALE| NUD_INCOMPLETE|NUD_DELAY|NUD_PROBE|
77 id = index_in_substrings(keywords, arg);
79 bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, "nud state");
85 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
89 static int FAST_FUNC print_neigh(const struct sockaddr_nl *who UNUSED_PARAM,
90 struct nlmsghdr *n, void *arg UNUSED_PARAM)
92 struct ndmsg *r = NLMSG_DATA(n);
93 int len = n->nlmsg_len;
94 struct rtattr *tb[NDA_MAX+1];
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,
101 len -= NLMSG_LENGTH(sizeof(*r));
103 bb_error_msg_and_die("BUG: wrong nlmsg len %d", len);
106 if (G_filter.flushb && n->nlmsg_type != RTM_NEWNEIGH)
109 if (G_filter.family && G_filter.family != r->ndm_family)
111 if (G_filter.index && G_filter.index != r->ndm_ifindex)
113 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)
121 parse_rtattr(tb, NDA_MAX, NDA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
124 if (G_filter.pfx.family) {
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))
133 if (G_filter.unused_only && tb[NDA_CACHEINFO]) {
134 struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
139 if (G_filter.flushb) {
141 if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
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;
158 format_host(r->ndm_family,
159 RTA_PAYLOAD(tb[NDA_DST]),
160 RTA_DATA(tb[NDA_DST]))
163 if (!G_filter.index && r->ndm_ifindex)
164 printf("dev %s ", ll_index_to_name(r->ndm_ifindex));
165 if (tb[NDA_LLADDR]) {
167 printf("lladdr %s", ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]),
168 RTA_PAYLOAD(tb[NDA_LLADDR]),
172 if (r->ndm_flags & NTF_ROUTER) {
175 if (r->ndm_flags & NTF_PROXY) {
178 if (tb[NDA_CACHEINFO] && xshow_stats) {
179 struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
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);
188 if (tb[NDA_PROBES] && xshow_stats) {
189 uint32_t p = rta_getattr_u32(tb[NDA_PROBES]);
190 printf(" probes %u", p);
193 /*if (r->ndm_state)*/ {
194 int nud = r->ndm_state;
196 #define PRINT_FLAG(f) \
197 if (nud & NUD_##f) { \
201 PRINT_FLAG(INCOMPLETE);
202 PRINT_FLAG(REACHABLE);
208 PRINT_FLAG(PERMANENT);
216 static void ipneigh_reset_filter(void)
218 memset(&G_filter, 0, sizeof(G_filter));
222 #define MAX_ROUNDS 10
223 /* Return value becomes exitcode. It's okay to not return at all */
224 static int FAST_FUNC ipneigh_list_or_flush(char **argv, int flush)
226 static const char keywords[] ALIGN1 =
227 /* "ip neigh show/flush" parameters: */
228 "to\0" "dev\0" "nud\0";
230 KW_to, KW_dev, KW_nud,
232 struct rtnl_handle rth;
233 struct ndmsg ndm = { 0 };
234 char *filter_dev = NULL;
238 ipneigh_reset_filter();
241 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip neigh flush\"");
243 if (!G_filter.family)
244 G_filter.family = preferred_family;
246 G_filter.state = (flush) ?
247 ~(NUD_PERMANENT|NUD_NOARP) : 0xFF & ~NUD_NOARP;
250 arg = index_in_substrings(keywords, *argv);
254 } else if (arg == KW_nud) {
261 if (strcmp(*argv, "all") == 0) {
266 state = nud_state_a2n(*argv);
270 G_filter.state |= state;
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;
286 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);
294 char flushb[4096-512];
295 G_filter.flushb = flushb;
297 G_filter.flushe = sizeof(flushb);
298 G_filter.state &= ~NUD_FAILED;
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");
305 G_filter.flushed = 0;
306 if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
307 bb_perror_msg_and_die("flush terminated");
309 if (G_filter.flushed == 0) {
311 puts("Nothing to flush");
313 printf("*** Flush is complete after %d round(s) ***\n", round);
317 if (flush_update() < 0)
319 printf("\n*** Round %d, deleting %d entries ***\n", round, G_filter.flushed);
321 bb_error_msg_and_die("*** Flush not complete bailing out after %d rounds", MAX_ROUNDS);
324 ndm.ndm_family = G_filter.family;
326 if (rtnl_dump_request(&rth, RTM_GETNEIGH, &ndm, sizeof(struct ndmsg)) < 0) {
327 bb_perror_msg_and_die("can't send dump request");
330 if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
331 bb_error_msg_and_die("dump terminated");
337 /* Return value becomes exitcode. It's okay to not return at all */
338 int FAST_FUNC do_ipneigh(char **argv)
340 static const char ip_neigh_commands[] ALIGN1 =
341 /*0-1*/ "show\0" "flush\0";
347 return ipneigh_list_or_flush(argv, 0);
349 command_num = index_in_substrings(ip_neigh_commands, *argv);
350 switch (command_num) {
352 return ipneigh_list_or_flush(argv + 1, 0);
354 return ipneigh_list_or_flush(argv + 1, 1);
356 invarg_1_to_2(*argv, applet_name);