3
0

arp.c 12 KB

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