ipneigh.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. return 0;
  102. parse_rtattr(tb, NDA_MAX, NDA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
  103. if (tb[NDA_DST]) {
  104. if (G_filter.pfx.family) {
  105. inet_prefix dst;
  106. memset(&dst, 0, sizeof(dst));
  107. dst.family = r->ndm_family;
  108. memcpy(&dst.data, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
  109. if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
  110. return 0;
  111. }
  112. }
  113. if (G_filter.unused_only && tb[NDA_CACHEINFO]) {
  114. struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
  115. if (ci->ndm_refcnt)
  116. return 0;
  117. }
  118. if (G_filter.flushb) {
  119. struct nlmsghdr *fn;
  120. if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
  121. if (flush_update())
  122. return -1;
  123. }
  124. fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
  125. memcpy(fn, n, n->nlmsg_len);
  126. fn->nlmsg_type = RTM_DELNEIGH;
  127. fn->nlmsg_flags = NLM_F_REQUEST;
  128. fn->nlmsg_seq = ++(G_filter.rth->seq);
  129. G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
  130. G_filter.flushed++;
  131. if (xshow_stats < 2)
  132. return 0;
  133. }
  134. if (tb[NDA_DST]) {
  135. printf("%s ",
  136. format_host(r->ndm_family,
  137. RTA_PAYLOAD(tb[NDA_DST]),
  138. RTA_DATA(tb[NDA_DST]))
  139. );
  140. }
  141. if (!G_filter.index && r->ndm_ifindex)
  142. printf("dev %s ", ll_index_to_name(r->ndm_ifindex));
  143. if (tb[NDA_LLADDR]) {
  144. SPRINT_BUF(b1);
  145. printf("lladdr %s", ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]),
  146. RTA_PAYLOAD(tb[NDA_LLADDR]),
  147. ARPHRD_ETHER,
  148. b1, sizeof(b1)));
  149. }
  150. if (r->ndm_flags & NTF_ROUTER) {
  151. printf(" router");
  152. }
  153. if (r->ndm_flags & NTF_PROXY) {
  154. printf(" proxy");
  155. }
  156. if (tb[NDA_CACHEINFO] && xshow_stats) {
  157. struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
  158. int hz = get_hz();
  159. if (ci->ndm_refcnt)
  160. printf(" ref %d", ci->ndm_refcnt);
  161. printf(" used %d/%d/%d", ci->ndm_used/hz,
  162. ci->ndm_confirmed/hz, ci->ndm_updated/hz);
  163. }
  164. if (tb[NDA_PROBES] && xshow_stats) {
  165. uint32_t p = rta_getattr_u32(tb[NDA_PROBES]);
  166. printf(" probes %u", p);
  167. }
  168. /*if (r->ndm_state)*/ {
  169. int nud = r->ndm_state;
  170. char c = ' ';
  171. #define PRINT_FLAG(f) \
  172. if (nud & NUD_##f) { \
  173. printf("%c"#f, c); \
  174. c = ','; \
  175. }
  176. PRINT_FLAG(INCOMPLETE);
  177. PRINT_FLAG(REACHABLE);
  178. PRINT_FLAG(STALE);
  179. PRINT_FLAG(DELAY);
  180. PRINT_FLAG(PROBE);
  181. PRINT_FLAG(FAILED);
  182. PRINT_FLAG(NOARP);
  183. PRINT_FLAG(PERMANENT);
  184. #undef PRINT_FLAG
  185. }
  186. bb_putchar('\n');
  187. return 0;
  188. }
  189. static void ipneigh_reset_filter(void)
  190. {
  191. memset(&G_filter, 0, sizeof(G_filter));
  192. G_filter.state = ~0;
  193. }
  194. #define MAX_ROUNDS 10
  195. /* Return value becomes exitcode. It's okay to not return at all */
  196. static int FAST_FUNC ipneigh_list_or_flush(char **argv, int flush)
  197. {
  198. static const char keywords[] ALIGN1 =
  199. /* "ip neigh show/flush" parameters: */
  200. "to\0" "dev\0" "nud\0";
  201. enum {
  202. KW_to, KW_dev, KW_nud,
  203. };
  204. struct rtnl_handle rth;
  205. struct ndmsg ndm = { 0 };
  206. char *filter_dev = NULL;
  207. int state_given = 0;
  208. int arg;
  209. ipneigh_reset_filter();
  210. if (flush && !*argv)
  211. bb_error_msg_and_die(bb_msg_requires_arg, "\"ip neigh flush\"");
  212. if (!G_filter.family)
  213. G_filter.family = preferred_family;
  214. G_filter.state = (flush) ?
  215. ~(NUD_PERMANENT|NUD_NOARP) : 0xFF & ~NUD_NOARP;
  216. while (*argv) {
  217. arg = index_in_substrings(keywords, *argv);
  218. if (arg == KW_dev) {
  219. NEXT_ARG();
  220. filter_dev = *argv;
  221. } else if (arg == KW_nud) {
  222. unsigned state;
  223. NEXT_ARG();
  224. if (!state_given) {
  225. state_given = 1;
  226. G_filter.state = 0;
  227. }
  228. if (strcmp(*argv, "all") == 0) {
  229. state = ~0;
  230. if (flush)
  231. state &= ~NUD_NOARP;
  232. } else {
  233. state = nud_state_a2n(*argv);
  234. }
  235. if (state == 0)
  236. state = 0x100;
  237. G_filter.state |= state;
  238. } else {
  239. if (arg == KW_to) {
  240. NEXT_ARG();
  241. }
  242. get_prefix(&G_filter.pfx, *argv, G_filter.family);
  243. if (G_filter.family == AF_UNSPEC)
  244. G_filter.family = G_filter.pfx.family;
  245. }
  246. argv++;
  247. }
  248. xrtnl_open(&rth);
  249. ll_init_map(&rth);
  250. if (filter_dev) {
  251. G_filter.index = xll_name_to_index(filter_dev);
  252. if (G_filter.index == 0) {
  253. bb_error_msg_and_die("can't find device '%s'", filter_dev);
  254. }
  255. }
  256. if (flush) {
  257. int round = 0;
  258. char flushb[4096-512];
  259. G_filter.flushb = flushb;
  260. G_filter.flushp = 0;
  261. G_filter.flushe = sizeof(flushb);
  262. G_filter.state &= ~NUD_FAILED;
  263. G_filter.rth = &rth;
  264. while (round < MAX_ROUNDS) {
  265. if (xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETNEIGH) < 0) {
  266. bb_perror_msg_and_die("can't send dump request");
  267. }
  268. G_filter.flushed = 0;
  269. if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
  270. bb_perror_msg_and_die("flush terminated");
  271. }
  272. if (G_filter.flushed == 0) {
  273. if (round == 0)
  274. puts("Nothing to flush");
  275. else
  276. printf("*** Flush is complete after %d round(s) ***\n", round);
  277. return 0;
  278. }
  279. round++;
  280. if (flush_update() < 0)
  281. xfunc_die();
  282. printf("\n*** Round %d, deleting %d entries ***\n", round, G_filter.flushed);
  283. }
  284. bb_error_msg_and_die("*** Flush not complete bailing out after %d rounds", MAX_ROUNDS);
  285. }
  286. ndm.ndm_family = G_filter.family;
  287. if (rtnl_dump_request(&rth, RTM_GETNEIGH, &ndm, sizeof(struct ndmsg)) < 0) {
  288. bb_perror_msg_and_die("can't send dump request");
  289. }
  290. if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
  291. bb_error_msg_and_die("dump terminated");
  292. }
  293. return 0;
  294. }
  295. /* Return value becomes exitcode. It's okay to not return at all */
  296. int FAST_FUNC do_ipneigh(char **argv)
  297. {
  298. static const char ip_neigh_commands[] ALIGN1 =
  299. /*0-1*/ "show\0" "flush\0";
  300. int command_num;
  301. INIT_G();
  302. if (!*argv)
  303. return ipneigh_list_or_flush(argv, 0);
  304. command_num = index_in_substrings(ip_neigh_commands, *argv);
  305. switch (command_num) {
  306. case 0: /* show */
  307. return ipneigh_list_or_flush(argv + 1, 0);
  308. case 1: /* flush */
  309. return ipneigh_list_or_flush(argv + 1, 1);
  310. }
  311. invarg_1_to_2(*argv, applet_name);
  312. return 1;
  313. }