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