3
0

arp.c 13 KB

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