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