3
0

arp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 -s Set new ARP entry"
  25. //usage: "\n -d Delete a specified 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 given device"
  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. const struct hwtype *hwt)
  197. {
  198. struct ifreq ifr;
  199. const struct hwtype *xhw;
  200. strcpy(ifr.ifr_name, ifname);
  201. ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
  202. "cant get HW-Address for '%s'", ifname);
  203. if (hwt && (ifr.ifr_hwaddr.sa_family != hw->type)) {
  204. bb_error_msg_and_die("protocol type mismatch");
  205. }
  206. memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
  207. if (option_mask32 & ARP_OPT_v) {
  208. xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
  209. if (!xhw || !xhw->print) {
  210. xhw = get_hwntype(-1);
  211. }
  212. bb_error_msg("device '%s' has HW address %s '%s'",
  213. ifname, xhw->name,
  214. xhw->print((unsigned char *) &ifr.ifr_hwaddr.sa_data));
  215. }
  216. }
  217. /* Set an entry in the ARP cache. */
  218. /* Called only from main, once */
  219. static int arp_set(char **args)
  220. {
  221. char *host;
  222. struct arpreq req;
  223. struct sockaddr sa;
  224. int flags;
  225. memset(&req, 0, sizeof(req));
  226. host = *args++;
  227. if (ap->input(host, &sa) < 0) {
  228. bb_herror_msg_and_die("%s", host);
  229. }
  230. /* If a host has more than one address, use the correct one! */
  231. memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
  232. /* Fetch the hardware address. */
  233. if (*args == NULL) {
  234. bb_error_msg_and_die("need hardware address");
  235. }
  236. if (option_mask32 & ARP_OPT_D) {
  237. arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL);
  238. } else {
  239. if (hw->input(*args++, &req.arp_ha) < 0) {
  240. bb_error_msg_and_die("invalid hardware address");
  241. }
  242. }
  243. /* Check out any modifiers. */
  244. flags = ATF_PERM | ATF_COM;
  245. while (*args != NULL) {
  246. switch (index_in_strings(options, *args)) {
  247. case 0: /* "pub" */
  248. flags |= ATF_PUBL;
  249. args++;
  250. break;
  251. case 1: /* "priv" */
  252. flags &= ~ATF_PUBL;
  253. args++;
  254. break;
  255. case 2: /* "temp" */
  256. flags &= ~ATF_PERM;
  257. args++;
  258. break;
  259. case 3: /* "trail" */
  260. flags |= ATF_USETRAILERS;
  261. args++;
  262. break;
  263. case 4: /* "dontpub" */
  264. #ifdef HAVE_ATF_DONTPUB
  265. flags |= ATF_DONTPUB;
  266. #else
  267. bb_error_msg("feature ATF_DONTPUB is not supported");
  268. #endif
  269. args++;
  270. break;
  271. case 5: /* "auto" */
  272. #ifdef HAVE_ATF_MAGIC
  273. flags |= ATF_MAGIC;
  274. #else
  275. bb_error_msg("feature ATF_MAGIC is not supported");
  276. #endif
  277. args++;
  278. break;
  279. case 6: /* "dev" */
  280. if (*++args == NULL)
  281. bb_show_usage();
  282. device = *args;
  283. args++;
  284. break;
  285. case 7: /* "netmask" */
  286. if (*++args == NULL)
  287. bb_show_usage();
  288. if (strcmp(*args, "255.255.255.255") != 0) {
  289. host = *args;
  290. if (ap->input(host, &sa) < 0) {
  291. bb_herror_msg_and_die("%s", host);
  292. }
  293. memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
  294. flags |= ATF_NETMASK;
  295. }
  296. args++;
  297. break;
  298. default:
  299. bb_show_usage();
  300. break;
  301. }
  302. }
  303. /* Fill in the remainder of the request. */
  304. req.arp_flags = flags;
  305. strncpy(req.arp_dev, device, sizeof(req.arp_dev));
  306. /* Call the kernel. */
  307. if (option_mask32 & ARP_OPT_v)
  308. bb_error_msg("SIOCSARP()");
  309. xioctl(sockfd, SIOCSARP, &req);
  310. return 0;
  311. }
  312. /* Print the contents of an ARP request block. */
  313. static void
  314. arp_disp(const char *name, char *ip, int type, int arp_flags,
  315. char *hwa, char *mask, char *dev)
  316. {
  317. static const int arp_masks[] = {
  318. ATF_PERM, ATF_PUBL,
  319. #ifdef HAVE_ATF_MAGIC
  320. ATF_MAGIC,
  321. #endif
  322. #ifdef HAVE_ATF_DONTPUB
  323. ATF_DONTPUB,
  324. #endif
  325. ATF_USETRAILERS,
  326. };
  327. static const char arp_labels[] ALIGN1 = "PERM\0""PUP\0"
  328. #ifdef HAVE_ATF_MAGIC
  329. "AUTO\0"
  330. #endif
  331. #ifdef HAVE_ATF_DONTPUB
  332. "DONTPUB\0"
  333. #endif
  334. "TRAIL\0"
  335. ;
  336. const struct hwtype *xhw;
  337. xhw = get_hwntype(type);
  338. if (xhw == NULL)
  339. xhw = get_hwtype(DFLT_HW);
  340. printf("%s (%s) at ", name, ip);
  341. if (!(arp_flags & ATF_COM)) {
  342. if (arp_flags & ATF_PUBL)
  343. printf("* ");
  344. else
  345. printf("<incomplete> ");
  346. } else {
  347. printf("%s [%s] ", hwa, xhw->name);
  348. }
  349. if (arp_flags & ATF_NETMASK)
  350. printf("netmask %s ", mask);
  351. print_flags_separated(arp_masks, arp_labels, arp_flags, " ");
  352. printf(" on %s\n", dev);
  353. }
  354. /* Display the contents of the ARP cache in the kernel. */
  355. /* Called only from main, once */
  356. static int arp_show(char *name)
  357. {
  358. const char *host;
  359. const char *hostname;
  360. FILE *fp;
  361. struct sockaddr sa;
  362. int type, flags;
  363. int num;
  364. unsigned entries = 0, shown = 0;
  365. char ip[128];
  366. char hwa[128];
  367. char mask[128];
  368. char line[128];
  369. char dev[128];
  370. host = NULL;
  371. if (name != NULL) {
  372. /* Resolve the host name. */
  373. if (ap->input(name, &sa) < 0) {
  374. bb_herror_msg_and_die("%s", name);
  375. }
  376. host = xstrdup(ap->sprint(&sa, 1));
  377. }
  378. fp = xfopen_for_read("/proc/net/arp");
  379. /* Bypass header -- read one line */
  380. fgets(line, sizeof(line), fp);
  381. /* Read the ARP cache entries. */
  382. while (fgets(line, sizeof(line), fp)) {
  383. mask[0] = '-'; mask[1] = '\0';
  384. dev[0] = '-'; dev[1] = '\0';
  385. /* All these strings can't overflow
  386. * because fgets above reads limited amount of data */
  387. num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
  388. ip, &type, &flags, hwa, mask, dev);
  389. if (num < 4)
  390. break;
  391. entries++;
  392. /* if the user specified hw-type differs, skip it */
  393. if (hw_set && (type != hw->type))
  394. continue;
  395. /* if the user specified address differs, skip it */
  396. if (host && strcmp(ip, host) != 0)
  397. continue;
  398. /* if the user specified device differs, skip it */
  399. if (device[0] && strcmp(dev, device) != 0)
  400. continue;
  401. shown++;
  402. /* This IS ugly but it works -be */
  403. hostname = "?";
  404. if (!(option_mask32 & ARP_OPT_n)) {
  405. if (ap->input(ip, &sa) < 0)
  406. hostname = ip;
  407. else
  408. hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
  409. if (strcmp(hostname, ip) == 0)
  410. hostname = "?";
  411. }
  412. arp_disp(hostname, ip, type, flags, hwa, mask, dev);
  413. }
  414. if (option_mask32 & ARP_OPT_v)
  415. printf("Entries: %d\tSkipped: %d\tFound: %d\n",
  416. entries, entries - shown, shown);
  417. if (!shown) {
  418. if (hw_set || host || device[0])
  419. printf("No match found in %d entries\n", entries);
  420. }
  421. if (ENABLE_FEATURE_CLEAN_UP) {
  422. free((char*)host);
  423. fclose(fp);
  424. }
  425. return 0;
  426. }
  427. int arp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  428. int arp_main(int argc UNUSED_PARAM, char **argv)
  429. {
  430. const char *hw_type = "ether";
  431. const char *protocol;
  432. unsigned opts;
  433. INIT_G();
  434. xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sockfd);
  435. ap = get_aftype(DFLT_AF);
  436. if (!ap)
  437. bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
  438. opts = getopt32(argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
  439. &hw_type, &hw_type, &device);
  440. argv += optind;
  441. if (opts & (ARP_OPT_A | ARP_OPT_p)) {
  442. ap = get_aftype(protocol);
  443. if (ap == NULL)
  444. bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
  445. }
  446. if (opts & (ARP_OPT_A | ARP_OPT_p)) {
  447. hw = get_hwtype(hw_type);
  448. if (hw == NULL)
  449. bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
  450. hw_set = 1;
  451. }
  452. //if (opts & ARP_OPT_i)... -i
  453. if (ap->af != AF_INET) {
  454. bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
  455. }
  456. /* If no hw type specified get default */
  457. if (!hw) {
  458. hw = get_hwtype(DFLT_HW);
  459. if (!hw)
  460. bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
  461. }
  462. if (hw->alen <= 0) {
  463. bb_error_msg_and_die("%s: %s without ARP support",
  464. hw->name, "hardware type");
  465. }
  466. /* Now see what we have to do here... */
  467. if (opts & (ARP_OPT_d | ARP_OPT_s)) {
  468. if (argv[0] == NULL)
  469. bb_error_msg_and_die("need host name");
  470. if (opts & ARP_OPT_s)
  471. return arp_set(argv);
  472. return arp_del(argv);
  473. }
  474. //if (opts & ARP_OPT_a) - default
  475. return arp_show(argv[0]);
  476. }