arp.c 11 KB

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