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