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