ipneigh.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* 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. */
  9. #include "ip_common.h" /* #include "libbb.h" is inside */
  10. #include "common_bufsiz.h"
  11. #include "rt_names.h"
  12. #include "utils.h"
  13. #include <linux/neighbour.h>
  14. #include <net/if_arp.h>
  15. //static int xshow_stats = 3;
  16. enum { xshow_stats = 3 };
  17. static inline uint32_t rta_getattr_u32(const struct rtattr *rta)
  18. {
  19. return *(uint32_t *)RTA_DATA(rta);
  20. }
  21. #ifndef RTAX_RTTVAR
  22. #define RTAX_RTTVAR RTAX_HOPS
  23. #endif
  24. struct filter_t {
  25. int family;
  26. int index;
  27. int state;
  28. int unused_only;
  29. inet_prefix pfx;
  30. /* Misnomer. Does not mean "flushed N something" */
  31. /* More like "no_of_flush_commands_constructed_by_print_neigh()" */
  32. int flushed;
  33. /* Flush cmd buf. If !NULL, print_neigh() constructs flush commands in it */
  34. char *flushb;
  35. int flushp;
  36. int flushe;
  37. struct rtnl_handle *rth;
  38. } FIX_ALIASING;
  39. typedef struct filter_t filter_t;
  40. #define G_filter (*(filter_t*)bb_common_bufsiz1)
  41. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  42. static int flush_update(void)
  43. {
  44. if (rtnl_send_check(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
  45. bb_simple_perror_msg("can't send flush request");
  46. return -1;
  47. }
  48. G_filter.flushp = 0;
  49. return 0;
  50. }
  51. static unsigned nud_state_a2n(char *arg)
  52. {
  53. static const char keywords[] ALIGN1 =
  54. /* "ip neigh show/flush" parameters: */
  55. "permanent\0" "reachable\0" "noarp\0" "none\0"
  56. "stale\0" "incomplete\0" "delay\0" "probe\0"
  57. "failed\0"
  58. ;
  59. static uint8_t nuds[] ALIGN1 = {
  60. NUD_PERMANENT,NUD_REACHABLE, NUD_NOARP,NUD_NONE,
  61. NUD_STALE, NUD_INCOMPLETE,NUD_DELAY,NUD_PROBE,
  62. NUD_FAILED
  63. };
  64. int id;
  65. BUILD_BUG_ON(
  66. (NUD_PERMANENT|NUD_REACHABLE| NUD_NOARP|NUD_NONE|
  67. NUD_STALE| NUD_INCOMPLETE|NUD_DELAY|NUD_PROBE|
  68. NUD_FAILED) > 0xff
  69. );
  70. id = index_in_substrings(keywords, arg);
  71. if (id < 0)
  72. bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, "nud state");
  73. return nuds[id];
  74. }
  75. #ifndef NDA_RTA
  76. #define NDA_RTA(r) \
  77. ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
  78. #endif
  79. static int FAST_FUNC print_neigh(const struct sockaddr_nl *who UNUSED_PARAM,
  80. struct nlmsghdr *n, void *arg UNUSED_PARAM)
  81. {
  82. struct ndmsg *r = NLMSG_DATA(n);
  83. int len = n->nlmsg_len;
  84. struct rtattr *tb[NDA_MAX+1];
  85. if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH) {
  86. bb_error_msg_and_die("not RTM_NEWNEIGH: %08x %08x %08x",
  87. n->nlmsg_len, n->nlmsg_type,
  88. n->nlmsg_flags);
  89. }
  90. len -= NLMSG_LENGTH(sizeof(*r));
  91. if (len < 0) {
  92. bb_error_msg_and_die("BUG: wrong nlmsg len %d", len);
  93. }
  94. if (G_filter.flushb && n->nlmsg_type != RTM_NEWNEIGH)
  95. return 0;
  96. if (G_filter.family && G_filter.family != r->ndm_family)
  97. return 0;
  98. if (G_filter.index && G_filter.index != r->ndm_ifindex)
  99. return 0;
  100. if (!(G_filter.state&r->ndm_state)
  101. && !(r->ndm_flags & NTF_PROXY)
  102. && (r->ndm_state || !(G_filter.state & 0x100))
  103. && (r->ndm_family != AF_DECnet)
  104. ) {
  105. return 0;
  106. }
  107. parse_rtattr(tb, NDA_MAX, NDA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
  108. if (tb[NDA_DST]) {
  109. if (G_filter.pfx.family) {
  110. inet_prefix dst;
  111. memset(&dst, 0, sizeof(dst));
  112. dst.family = r->ndm_family;
  113. memcpy(&dst.data, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
  114. if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
  115. return 0;
  116. }
  117. }
  118. if (G_filter.unused_only && tb[NDA_CACHEINFO]) {
  119. struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
  120. if (ci->ndm_refcnt)
  121. return 0;
  122. }
  123. if (G_filter.flushb) {
  124. struct nlmsghdr *fn;
  125. if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
  126. if (flush_update())
  127. return -1;
  128. }
  129. fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
  130. memcpy(fn, n, n->nlmsg_len);
  131. fn->nlmsg_type = RTM_DELNEIGH;
  132. fn->nlmsg_flags = NLM_F_REQUEST;
  133. fn->nlmsg_seq = ++(G_filter.rth->seq);
  134. G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
  135. G_filter.flushed++;
  136. if (xshow_stats < 2)
  137. return 0;
  138. }
  139. if (tb[NDA_DST]) {
  140. printf("%s ",
  141. format_host(r->ndm_family,
  142. RTA_PAYLOAD(tb[NDA_DST]),
  143. RTA_DATA(tb[NDA_DST]))
  144. );
  145. }
  146. if (!G_filter.index && r->ndm_ifindex)
  147. printf("dev %s ", ll_index_to_name(r->ndm_ifindex));
  148. if (tb[NDA_LLADDR]) {
  149. SPRINT_BUF(b1);
  150. printf("lladdr %s", ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]),
  151. RTA_PAYLOAD(tb[NDA_LLADDR]),
  152. ARPHRD_ETHER,
  153. b1, sizeof(b1)));
  154. }
  155. if (r->ndm_flags & NTF_ROUTER) {
  156. printf(" router");
  157. }
  158. if (r->ndm_flags & NTF_PROXY) {
  159. printf(" proxy");
  160. }
  161. if (tb[NDA_CACHEINFO] && xshow_stats) {
  162. struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
  163. int hz = get_hz();
  164. if (ci->ndm_refcnt)
  165. printf(" ref %d", ci->ndm_refcnt);
  166. printf(" used %d/%d/%d", ci->ndm_used/hz,
  167. ci->ndm_confirmed/hz, ci->ndm_updated/hz);
  168. }
  169. if (tb[NDA_PROBES] && xshow_stats) {
  170. uint32_t p = rta_getattr_u32(tb[NDA_PROBES]);
  171. printf(" probes %u", p);
  172. }
  173. /*if (r->ndm_state)*/ {
  174. int nud = r->ndm_state;
  175. char c = ' ';
  176. #define PRINT_FLAG(f) \
  177. if (nud & NUD_##f) { \
  178. printf("%c"#f, c); \
  179. c = ','; \
  180. }
  181. PRINT_FLAG(INCOMPLETE);
  182. PRINT_FLAG(REACHABLE);
  183. PRINT_FLAG(STALE);
  184. PRINT_FLAG(DELAY);
  185. PRINT_FLAG(PROBE);
  186. PRINT_FLAG(FAILED);
  187. PRINT_FLAG(NOARP);
  188. PRINT_FLAG(PERMANENT);
  189. #undef PRINT_FLAG
  190. }
  191. bb_putchar('\n');
  192. return 0;
  193. }
  194. static void ipneigh_reset_filter(void)
  195. {
  196. memset(&G_filter, 0, sizeof(G_filter));
  197. G_filter.state = ~0;
  198. }
  199. #define MAX_ROUNDS 10
  200. /* Return value becomes exitcode. It's okay to not return at all */
  201. static int FAST_FUNC ipneigh_list_or_flush(char **argv, int flush)
  202. {
  203. static const char keywords[] ALIGN1 =
  204. /* "ip neigh show/flush" parameters: */
  205. "to\0" "dev\0" "nud\0";
  206. enum {
  207. KW_to, KW_dev, KW_nud,
  208. };
  209. struct rtnl_handle rth;
  210. struct ndmsg ndm = { 0 };
  211. char *filter_dev = NULL;
  212. int state_given = 0;
  213. int arg;
  214. ipneigh_reset_filter();
  215. if (flush && !*argv)
  216. bb_error_msg_and_die(bb_msg_requires_arg, "\"ip neigh flush\"");
  217. if (!G_filter.family)
  218. G_filter.family = preferred_family;
  219. G_filter.state = (flush) ?
  220. ~(NUD_PERMANENT|NUD_NOARP) : 0xFF & ~NUD_NOARP;
  221. while (*argv) {
  222. arg = index_in_substrings(keywords, *argv);
  223. if (arg == KW_dev) {
  224. NEXT_ARG();
  225. filter_dev = *argv;
  226. } else if (arg == KW_nud) {
  227. unsigned state;
  228. NEXT_ARG();
  229. if (!state_given) {
  230. state_given = 1;
  231. G_filter.state = 0;
  232. }
  233. if (strcmp(*argv, "all") == 0) {
  234. state = ~0;
  235. if (flush)
  236. state &= ~NUD_NOARP;
  237. } else {
  238. state = nud_state_a2n(*argv);
  239. }
  240. if (state == 0)
  241. state = 0x100;
  242. G_filter.state |= state;
  243. } else {
  244. if (arg == KW_to) {
  245. NEXT_ARG();
  246. }
  247. get_prefix(&G_filter.pfx, *argv, G_filter.family);
  248. if (G_filter.family == AF_UNSPEC)
  249. G_filter.family = G_filter.pfx.family;
  250. }
  251. argv++;
  252. }
  253. xrtnl_open(&rth);
  254. ll_init_map(&rth);
  255. if (filter_dev) {
  256. G_filter.index = xll_name_to_index(filter_dev);
  257. if (G_filter.index == 0) {
  258. bb_error_msg_and_die("can't find device '%s'", filter_dev);
  259. }
  260. }
  261. if (flush) {
  262. int round = 0;
  263. char flushb[4096-512];
  264. G_filter.flushb = flushb;
  265. G_filter.flushp = 0;
  266. G_filter.flushe = sizeof(flushb);
  267. G_filter.state &= ~NUD_FAILED;
  268. G_filter.rth = &rth;
  269. while (round < MAX_ROUNDS) {
  270. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETNEIGH);
  271. G_filter.flushed = 0;
  272. if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
  273. bb_simple_perror_msg_and_die("flush terminated");
  274. }
  275. if (G_filter.flushed == 0) {
  276. if (round == 0)
  277. puts("Nothing to flush");
  278. else
  279. printf("*** Flush is complete after %d round(s) ***\n", round);
  280. return 0;
  281. }
  282. round++;
  283. if (flush_update() < 0)
  284. xfunc_die();
  285. printf("\n*** Round %d, deleting %d entries ***\n", round, G_filter.flushed);
  286. }
  287. bb_error_msg_and_die("*** Flush not complete bailing out after %d rounds", MAX_ROUNDS);
  288. }
  289. ndm.ndm_family = G_filter.family;
  290. if (rtnl_dump_request(&rth, RTM_GETNEIGH, &ndm, sizeof(struct ndmsg)) < 0) {
  291. bb_simple_perror_msg_and_die("can't send dump request");
  292. }
  293. if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
  294. bb_simple_error_msg_and_die("dump terminated");
  295. }
  296. return 0;
  297. }
  298. /* Return value becomes exitcode. It's okay to not return at all */
  299. int FAST_FUNC do_ipneigh(char **argv)
  300. {
  301. static const char ip_neigh_commands[] ALIGN1 =
  302. /*0-1*/ "show\0" "flush\0";
  303. int command_num;
  304. INIT_G();
  305. if (!*argv)
  306. return ipneigh_list_or_flush(argv, 0);
  307. command_num = index_in_substrings(ip_neigh_commands, *argv);
  308. switch (command_num) {
  309. case 0: /* show */
  310. return ipneigh_list_or_flush(argv + 1, 0);
  311. case 1: /* flush */
  312. return ipneigh_list_or_flush(argv + 1, 1);
  313. }
  314. invarg_1_to_2(*argv, applet_name);
  315. return 1;
  316. }