ipneigh.c 8.4 KB

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