arp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * arp.c - Manipulate the system ARP cache
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * Author: Fred N. van Kempen, <waltje at uwalt.nl.mugnet.org>
  11. * Busybox port: Paul van Gool <pvangool at mimotech.com>
  12. *
  13. * modified for getopt32 by Arne Bernin <arne [at] alamut.de>
  14. */
  15. //config:config ARP
  16. //config: bool "arp (11 kb)"
  17. //config: default y
  18. //config: select PLATFORM_LINUX
  19. //config: help
  20. //config: Manipulate the system ARP cache.
  21. //applet:IF_ARP(APPLET(arp, BB_DIR_SBIN, BB_SUID_DROP))
  22. //kbuild:lib-$(CONFIG_ARP) += arp.o interface.o
  23. //usage:#define arp_trivial_usage
  24. //usage: "\n[-vn] [-H HWTYPE] [-i IF] -a [HOSTNAME]"
  25. //usage: "\n[-v] [-i IF] -d HOSTNAME [pub]"
  26. //usage: "\n[-v] [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [temp]"
  27. //usage: "\n[-v] [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [netmask MASK] pub"
  28. //usage: "\n[-v] [-H HWTYPE] [-i IF] -Ds HOSTNAME IFACE [netmask MASK] pub"
  29. //usage:#define arp_full_usage "\n\n"
  30. //usage: "Manipulate ARP cache\n"
  31. //usage: "\n -a Display (all) hosts"
  32. //usage: "\n -d Delete ARP entry"
  33. //usage: "\n -s Set new entry"
  34. //usage: "\n -v Verbose"
  35. //usage: "\n -n Don't resolve names"
  36. //usage: "\n -i IF Network interface"
  37. //usage: "\n -D Read HWADDR from IFACE"
  38. //usage: "\n -A,-p AF Protocol family"
  39. //usage: "\n -H HWTYPE Hardware address type"
  40. #include "libbb.h"
  41. #include "common_bufsiz.h"
  42. #include "inet_common.h"
  43. #include <arpa/inet.h>
  44. #include <net/if.h>
  45. #include <net/if_arp.h>
  46. #include <netinet/ether.h>
  47. #include <netpacket/packet.h>
  48. #define DEBUG 0
  49. #define DFLT_AF "inet"
  50. #define DFLT_HW "ether"
  51. enum {
  52. ARP_OPT_A = (1 << 0),
  53. ARP_OPT_p = (1 << 1),
  54. ARP_OPT_H = (1 << 2),
  55. ARP_OPT_t = (1 << 3),
  56. ARP_OPT_i = (1 << 4),
  57. ARP_OPT_a = (1 << 5),
  58. ARP_OPT_d = (1 << 6),
  59. ARP_OPT_n = (1 << 7), /* do not resolve addresses */
  60. ARP_OPT_D = (1 << 8), /* HW-address is devicename */
  61. ARP_OPT_s = (1 << 9),
  62. ARP_OPT_v = (1 << 10) * DEBUG, /* debugging output flag */
  63. };
  64. enum {
  65. sockfd = 3, /* active socket descriptor */
  66. };
  67. struct globals {
  68. const struct aftype *ap; /* current address family */
  69. const struct hwtype *hw; /* current hardware type */
  70. const char *device; /* current device */
  71. smallint hw_set; /* flag if hw-type was set (-H) */
  72. } FIX_ALIASING;
  73. #define G (*(struct globals*)bb_common_bufsiz1)
  74. #define ap (G.ap )
  75. #define hw (G.hw )
  76. #define device (G.device )
  77. #define hw_set (G.hw_set )
  78. #define INIT_G() do { \
  79. setup_common_bufsiz(); \
  80. device = ""; \
  81. } while (0)
  82. static const char options[] ALIGN1 =
  83. "pub\0"
  84. "priv\0"
  85. "temp\0"
  86. "trail\0"
  87. "dontpub\0"
  88. "auto\0"
  89. "dev\0"
  90. "netmask\0";
  91. /* Delete an entry from the ARP cache. */
  92. /* Called only from main, once */
  93. static int arp_del(char **args)
  94. {
  95. char *host;
  96. struct arpreq req;
  97. struct sockaddr sa;
  98. int flags = 0;
  99. int err;
  100. memset(&req, 0, sizeof(req));
  101. /* Resolve the host name. */
  102. host = *args;
  103. if (ap->input(host, &sa) < 0) {
  104. bb_herror_msg_and_die("%s", host);
  105. }
  106. /* If a host has more than one address, use the correct one! */
  107. memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
  108. if (hw_set)
  109. req.arp_ha.sa_family = hw->type;
  110. req.arp_flags = ATF_PERM;
  111. args++;
  112. while (*args != NULL) {
  113. switch (index_in_strings(options, *args)) {
  114. case 0: /* "pub" */
  115. flags |= 1;
  116. args++;
  117. break;
  118. case 1: /* "priv" */
  119. flags |= 2;
  120. args++;
  121. break;
  122. case 2: /* "temp" */
  123. req.arp_flags &= ~ATF_PERM;
  124. args++;
  125. break;
  126. case 3: /* "trail" */
  127. req.arp_flags |= ATF_USETRAILERS;
  128. args++;
  129. break;
  130. case 4: /* "dontpub" */
  131. #ifdef HAVE_ATF_DONTPUB
  132. req.arp_flags |= ATF_DONTPUB;
  133. #else
  134. bb_error_msg("feature ATF_DONTPUB is not supported");
  135. #endif
  136. args++;
  137. break;
  138. case 5: /* "auto" */
  139. #ifdef HAVE_ATF_MAGIC
  140. req.arp_flags |= ATF_MAGIC;
  141. #else
  142. bb_error_msg("feature ATF_MAGIC is not supported");
  143. #endif
  144. args++;
  145. break;
  146. case 6: /* "dev" */
  147. if (*++args == NULL)
  148. bb_show_usage();
  149. device = *args;
  150. args++;
  151. break;
  152. case 7: /* "netmask" */
  153. if (*++args == NULL)
  154. bb_show_usage();
  155. if (strcmp(*args, "255.255.255.255") != 0) {
  156. host = *args;
  157. if (ap->input(host, &sa) < 0) {
  158. bb_herror_msg_and_die("%s", host);
  159. }
  160. memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
  161. req.arp_flags |= ATF_NETMASK;
  162. }
  163. args++;
  164. break;
  165. default:
  166. bb_show_usage();
  167. break;
  168. }
  169. }
  170. if (flags == 0)
  171. flags = 3;
  172. strncpy_IFNAMSIZ(req.arp_dev, device);
  173. err = -1;
  174. /* Call the kernel. */
  175. if (flags & 2) {
  176. if (option_mask32 & ARP_OPT_v)
  177. bb_error_msg("SIOCDARP(nopub)");
  178. err = ioctl(sockfd, SIOCDARP, &req);
  179. if (err < 0) {
  180. if (errno == ENXIO) {
  181. if (flags & 1)
  182. goto nopub;
  183. printf("No ARP entry for %s\n", host);
  184. return -1;
  185. }
  186. bb_perror_msg_and_die("SIOCDARP(priv)");
  187. }
  188. }
  189. if ((flags & 1) && err) {
  190. nopub:
  191. req.arp_flags |= ATF_PUBL;
  192. if (option_mask32 & ARP_OPT_v)
  193. bb_error_msg("SIOCDARP(pub)");
  194. if (ioctl(sockfd, SIOCDARP, &req) < 0) {
  195. if (errno == ENXIO) {
  196. printf("No ARP entry for %s\n", host);
  197. return -1;
  198. }
  199. bb_perror_msg_and_die("SIOCDARP(pub)");
  200. }
  201. }
  202. return 0;
  203. }
  204. /* Get the hardware address to a specified interface name */
  205. static void arp_getdevhw(char *ifname, struct sockaddr *sa)
  206. {
  207. struct ifreq ifr;
  208. const struct hwtype *xhw;
  209. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  210. ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
  211. "can't get HW-Address for '%s'", ifname);
  212. if (hw_set && (ifr.ifr_hwaddr.sa_family != hw->type)) {
  213. bb_error_msg_and_die("protocol type mismatch");
  214. }
  215. memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
  216. if (option_mask32 & ARP_OPT_v) {
  217. xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
  218. if (!xhw || !xhw->print) {
  219. xhw = get_hwntype(-1);
  220. }
  221. bb_error_msg("device '%s' has HW address %s '%s'",
  222. ifname, xhw->name,
  223. xhw->print((unsigned char *) &ifr.ifr_hwaddr.sa_data));
  224. }
  225. }
  226. /* Set an entry in the ARP cache. */
  227. /* Called only from main, once */
  228. static int arp_set(char **args)
  229. {
  230. char *host;
  231. struct arpreq req;
  232. struct sockaddr sa;
  233. int flags;
  234. memset(&req, 0, sizeof(req));
  235. host = *args++;
  236. if (ap->input(host, &sa) < 0) {
  237. bb_herror_msg_and_die("%s", host);
  238. }
  239. /* If a host has more than one address, use the correct one! */
  240. memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
  241. /* Fetch the hardware address. */
  242. if (*args == NULL) {
  243. bb_error_msg_and_die("need hardware address");
  244. }
  245. if (option_mask32 & ARP_OPT_D) {
  246. arp_getdevhw(*args++, &req.arp_ha);
  247. } else {
  248. if (hw->input(*args++, &req.arp_ha) < 0) {
  249. bb_error_msg_and_die("invalid hardware address");
  250. }
  251. }
  252. /* Check out any modifiers. */
  253. flags = ATF_PERM | ATF_COM;
  254. while (*args != NULL) {
  255. switch (index_in_strings(options, *args)) {
  256. case 0: /* "pub" */
  257. flags |= ATF_PUBL;
  258. args++;
  259. break;
  260. case 1: /* "priv" */
  261. flags &= ~ATF_PUBL;
  262. args++;
  263. break;
  264. case 2: /* "temp" */
  265. flags &= ~ATF_PERM;
  266. args++;
  267. break;
  268. case 3: /* "trail" */
  269. flags |= ATF_USETRAILERS;
  270. args++;
  271. break;
  272. case 4: /* "dontpub" */
  273. #ifdef HAVE_ATF_DONTPUB
  274. flags |= ATF_DONTPUB;
  275. #else
  276. bb_error_msg("feature ATF_DONTPUB is not supported");
  277. #endif
  278. args++;
  279. break;
  280. case 5: /* "auto" */
  281. #ifdef HAVE_ATF_MAGIC
  282. flags |= ATF_MAGIC;
  283. #else
  284. bb_error_msg("feature ATF_MAGIC is not supported");
  285. #endif
  286. args++;
  287. break;
  288. case 6: /* "dev" */
  289. if (*++args == NULL)
  290. bb_show_usage();
  291. device = *args;
  292. args++;
  293. break;
  294. case 7: /* "netmask" */
  295. if (*++args == NULL)
  296. bb_show_usage();
  297. if (strcmp(*args, "255.255.255.255") != 0) {
  298. host = *args;
  299. if (ap->input(host, &sa) < 0) {
  300. bb_herror_msg_and_die("%s", host);
  301. }
  302. memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
  303. flags |= ATF_NETMASK;
  304. }
  305. args++;
  306. break;
  307. default:
  308. bb_show_usage();
  309. break;
  310. }
  311. }
  312. /* Fill in the remainder of the request. */
  313. req.arp_flags = flags;
  314. strncpy_IFNAMSIZ(req.arp_dev, device);
  315. /* Call the kernel. */
  316. if (option_mask32 & ARP_OPT_v)
  317. bb_error_msg("SIOCSARP()");
  318. xioctl(sockfd, SIOCSARP, &req);
  319. return 0;
  320. }
  321. /* Print the contents of an ARP request block. */
  322. static void
  323. arp_disp(const char *name, char *ip, int type, int arp_flags,
  324. char *hwa, char *mask, char *dev)
  325. {
  326. static const int arp_masks[] = {
  327. ATF_PERM, ATF_PUBL,
  328. #ifdef HAVE_ATF_MAGIC
  329. ATF_MAGIC,
  330. #endif
  331. #ifdef HAVE_ATF_DONTPUB
  332. ATF_DONTPUB,
  333. #endif
  334. ATF_USETRAILERS,
  335. };
  336. static const char arp_labels[] ALIGN1 = "PERM\0""PUP\0"
  337. #ifdef HAVE_ATF_MAGIC
  338. "AUTO\0"
  339. #endif
  340. #ifdef HAVE_ATF_DONTPUB
  341. "DONTPUB\0"
  342. #endif
  343. "TRAIL\0"
  344. ;
  345. const struct hwtype *xhw;
  346. xhw = get_hwntype(type);
  347. if (xhw == NULL)
  348. xhw = get_hwtype(DFLT_HW);
  349. printf("%s (%s) at ", name, ip);
  350. if (!(arp_flags & ATF_COM)) {
  351. if (arp_flags & ATF_PUBL)
  352. printf("* ");
  353. else
  354. printf("<incomplete> ");
  355. } else {
  356. printf("%s [%s] ", hwa, xhw->name);
  357. }
  358. if (arp_flags & ATF_NETMASK)
  359. printf("netmask %s ", mask);
  360. print_flags_separated(arp_masks, arp_labels, arp_flags, " ");
  361. printf(" on %s\n", dev);
  362. }
  363. /* Display the contents of the ARP cache in the kernel. */
  364. /* Called only from main, once */
  365. static int arp_show(char *name)
  366. {
  367. const char *host;
  368. const char *hostname;
  369. FILE *fp;
  370. struct sockaddr sa;
  371. int type, flags;
  372. int num;
  373. unsigned entries = 0, shown = 0;
  374. char ip[128];
  375. char hwa[128];
  376. char mask[128];
  377. char line[128];
  378. char dev[128];
  379. host = NULL;
  380. if (name != NULL) {
  381. /* Resolve the host name. */
  382. if (ap->input(name, &sa) < 0) {
  383. bb_herror_msg_and_die("%s", name);
  384. }
  385. host = xstrdup(ap->sprint(&sa, 1));
  386. }
  387. fp = xfopen_for_read("/proc/net/arp");
  388. /* Bypass header -- read one line */
  389. fgets(line, sizeof(line), fp);
  390. /* Read the ARP cache entries. */
  391. while (fgets(line, sizeof(line), fp)) {
  392. mask[0] = '-'; mask[1] = '\0';
  393. dev[0] = '-'; dev[1] = '\0';
  394. /* All these strings can't overflow
  395. * because fgets above reads limited amount of data */
  396. num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
  397. ip, &type, &flags, hwa, mask, dev);
  398. if (num < 4)
  399. break;
  400. entries++;
  401. /* if the user specified hw-type differs, skip it */
  402. if (hw_set && (type != hw->type))
  403. continue;
  404. /* if the user specified address differs, skip it */
  405. if (host && strcmp(ip, host) != 0)
  406. continue;
  407. /* if the user specified device differs, skip it */
  408. if (device[0] && strcmp(dev, device) != 0)
  409. continue;
  410. shown++;
  411. /* This IS ugly but it works -be */
  412. hostname = "?";
  413. if (!(option_mask32 & ARP_OPT_n)) {
  414. if (ap->input(ip, &sa) < 0)
  415. hostname = ip;
  416. else
  417. hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
  418. if (strcmp(hostname, ip) == 0)
  419. hostname = "?";
  420. }
  421. arp_disp(hostname, ip, type, flags, hwa, mask, dev);
  422. }
  423. if (option_mask32 & ARP_OPT_v)
  424. printf("Entries: %u\tSkipped: %u\tFound: %u\n",
  425. entries, entries - shown, shown);
  426. if (!shown) {
  427. if (hw_set || host || device[0])
  428. printf("No match found in %u entries\n", entries);
  429. }
  430. if (ENABLE_FEATURE_CLEAN_UP) {
  431. free((char*)host);
  432. fclose(fp);
  433. }
  434. return 0;
  435. }
  436. int arp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  437. int arp_main(int argc UNUSED_PARAM, char **argv)
  438. {
  439. const char *hw_type;
  440. const char *protocol;
  441. unsigned opts;
  442. INIT_G();
  443. xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sockfd);
  444. ap = get_aftype(DFLT_AF);
  445. /* Defaults are always supported */
  446. //if (!ap)
  447. // bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
  448. hw = get_hwtype(DFLT_HW);
  449. //if (!hw)
  450. // bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
  451. opts = getopt32(argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
  452. &hw_type, &hw_type, &device);
  453. argv += optind;
  454. if (opts & (ARP_OPT_A | ARP_OPT_p)) {
  455. ap = get_aftype(protocol);
  456. if (!ap)
  457. bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
  458. }
  459. if (opts & (ARP_OPT_H | ARP_OPT_t)) {
  460. hw = get_hwtype(hw_type);
  461. if (!hw)
  462. bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
  463. hw_set = 1;
  464. }
  465. //if (opts & ARP_OPT_i)... -i
  466. if (ap->af != AF_INET) {
  467. bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
  468. }
  469. if (hw->alen <= 0) {
  470. bb_error_msg_and_die("%s: %s without ARP support",
  471. hw->name, "hardware type");
  472. }
  473. /* Now see what we have to do here... */
  474. if (opts & (ARP_OPT_d | ARP_OPT_s)) {
  475. if (argv[0] == NULL)
  476. bb_error_msg_and_die("need host name");
  477. if (opts & ARP_OPT_s)
  478. return arp_set(argv);
  479. return arp_del(argv);
  480. }
  481. //if (opts & ARP_OPT_a) - default
  482. return arp_show(argv[0]);
  483. }