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