1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
| #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <string.h> #include <errno.h> #include <pthread.h> #include <ev.h> #include <unistd.h> #include <signal.h> #include <sys/types.h> #include <sys/socket.h> #include <net/if_arp.h> #include <linux/filter.h> #include <linux/if_ether.h> #include <netpacket/packet.h> #include <net/ethernet.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <net/if.h>
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
#define LOG_ERROR(fmt, ...) _log("ERROR", __FILE__, __LINE__, \ __FUNCTION__, fmt, ##__VA_ARGS__)
#define LOG_INFO(fmt, ...) _log("INFO ", __FILE__, __LINE__, \ __FUNCTION__, fmt, ##__VA_ARGS__)
#define LOG_DEBUG(fmt, ...) _log("DEBUG", __FILE__, __LINE__, \ __FUNCTION__, fmt, ##__VA_ARGS__)
struct my_arphdr { unsigned short int ar_hrd; unsigned short int ar_pro; unsigned char ar_hln; unsigned char ar_pln; unsigned short int ar_op;
unsigned char ar_sha[ETH_ALEN]; unsigned char ar_sip[4]; unsigned char ar_tha[ETH_ALEN]; unsigned char ar_tip[4]; };
struct my_arp_response_struct { int fd; struct ev_io io_watcher; unsigned char hwaddr[ETH_ALEN]; };
static volatile int _stop = 0;
static void _log(const char *level, const char *file, int line, const char *func, const char *fmt, ...) { char buf[2048]; va_list list; time_t t; struct tm t_tm = {}; if (time(&t) != (time_t)-1) { localtime_r(&t, &t_tm); }
time(&t);
va_start(list, fmt);
vsnprintf(buf, sizeof(buf), fmt, list);
printf("%4d-%02d-%02d,%02d:%02d:%02d tid:%5lu [%s %s:%d] %s", t_tm.tm_year + 1900, t_tm.tm_mon + 1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec, pthread_self() % 100000, level, file, line, buf); }
static void *thread_loop(void *arg) { struct ev_loop *loop = arg; LOG_INFO("loop start\n"); ev_run(loop, 0); LOG_INFO("loop end\n"); return NULL; }
static void async_callback(struct ev_loop *loop, struct ev_async *w, int revents) { LOG_INFO("in async callback\n"); ev_break(loop, EVBREAK_ONE); }
static void term_handler(int sig) { LOG_INFO("in term handler\n"); _stop = 1; }
static void arp_raw_callback(struct ev_loop *loop, ev_io *w, int revents) { struct my_arp_response_struct *ar; int fd = w->fd; struct sockaddr_ll addr; socklen_t addr_len; char buf[2048]; int r; struct my_arphdr *arph; struct ethhdr *ethh;
ar = w->data;
while (1) { addr_len = sizeof(addr); memset(&addr, '\0', addr_len); r = recvfrom(fd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&addr, &addr_len); if (r == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { break; } else { LOG_ERROR("recv arp packet failed, %s\n", strerror(errno)); break; } } else { LOG_DEBUG("recv arp packet %d bytes\n", r); if (r == sizeof(*arph) + sizeof(*ethh)) { ethh = (struct ethhdr *)buf; arph = (struct my_arphdr *)(buf + sizeof(*ethh)); if (arph->ar_op == htons(ARPOP_REQUEST)) { char src_ip[32]; char dst_ip[32]; unsigned int target_ip = *(unsigned int *)arph->ar_tip; inet_ntop(AF_INET, arph->ar_sip, src_ip, sizeof(src_ip)); inet_ntop(AF_INET, arph->ar_tip, dst_ip, sizeof(dst_ip));
LOG_DEBUG("arp request %s from %s\n", dst_ip, src_ip);
memcpy(ethh->h_dest, ethh->h_source, ETH_ALEN); memcpy(ethh->h_source, ar->hwaddr, ETH_ALEN);
arph->ar_op = htons(ARPOP_REPLY); memcpy(arph->ar_tip, arph->ar_sip, sizeof(arph->ar_sip)); memcpy(arph->ar_tha, arph->ar_sha, sizeof(arph->ar_sha)); memcpy(arph->ar_sip, &target_ip, sizeof(target_ip)); memcpy(arph->ar_sha, ar->hwaddr, sizeof(ar->hwaddr));
r = sendto(fd, ethh, r, 0, (struct sockaddr *)&addr, sizeof(addr)); if (r == -1) { LOG_ERROR("send arp reply failed, %s\n", strerror(errno)); } else { LOG_DEBUG("send arp reply %d bytes\n", r); } } } } } }
static void arp_dgram_callback(struct ev_loop *loop, ev_io *w, int revents) {
struct my_arp_response_struct *ar; int fd = w->fd; struct sockaddr_ll addr; socklen_t addr_len; char buf[2048]; int r; struct my_arphdr *arph;
ar = w->data;
while (1) { addr_len = sizeof(addr); memset(&addr, '\0', addr_len); r = recvfrom(fd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&addr, &addr_len); if (r == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { break; } else { LOG_ERROR("recv arp packet failed, %s\n", strerror(errno)); break; } } else { LOG_DEBUG("recv arp packet %d bytes\n", r); if (r == sizeof(*arph)) { arph = (struct my_arphdr *)buf; if (arph->ar_op == htons(ARPOP_REQUEST)) { char src_ip[32]; char dst_ip[32]; unsigned int target_ip = *(unsigned int *)arph->ar_tip; inet_ntop(AF_INET, arph->ar_sip, src_ip, sizeof(src_ip)); inet_ntop(AF_INET, arph->ar_tip, dst_ip, sizeof(dst_ip));
LOG_DEBUG("arp request %s from %s\n", dst_ip, src_ip);
arph->ar_op = htons(ARPOP_REPLY); memcpy(arph->ar_tip, arph->ar_sip, sizeof(arph->ar_sip)); memcpy(arph->ar_tha, arph->ar_sha, sizeof(arph->ar_sha)); memcpy(arph->ar_sip, &target_ip, sizeof(target_ip)); memcpy(arph->ar_sha, ar->hwaddr, sizeof(ar->hwaddr));
r = sendto(fd, buf, sizeof(*arph), 0, (struct sockaddr *)&addr, sizeof(addr)); if (r == -1) { LOG_ERROR("send arp reply failed, %s\n", strerror(errno)); } else { LOG_DEBUG("send arp reply %d bytes\n", r); } } } } } }
static struct my_arp_response_struct *get_arp_response_struct( int ifindex, struct ev_loop *loop, const char *start, const char *end, const unsigned char hwaddr[ETH_ALEN], int is_dgram) {
struct sockaddr_ll addr; struct my_arp_response_struct *ar; struct sock_fprog bpf; int socket_type; void (*cb)(struct ev_loop *, struct ev_io *, int);
struct sock_filter dgram_filter[] = { { 0x20, 0, 0, 0x00000018 }, { 0x35, 0, 4, 0xac100001 }, { 0x25, 3, 0, 0xac100005 }, { 0x28, 0, 0, 0x00000006 }, { 0x15, 0, 1, 0x00000001 }, { 0x6, 0, 0, 0x00040000 }, { 0x6, 0, 0, 0x00000000 }, };
struct sock_filter raw_filter[] = { { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 6, 0x00000806 }, { 0x20, 0, 0, 0x00000026 }, { 0x35, 0, 4, 0xac100001 }, { 0x25, 3, 0, 0xac100005 }, { 0x28, 0, 0, 0x00000014 }, { 0x15, 0, 1, 0x00000001 }, { 0x6, 0, 0, 0x00040000 }, { 0x6, 0, 0, 0x00000000 }, };
if (is_dgram) {
socket_type = SOCK_DGRAM; cb = arp_dgram_callback;
dgram_filter[1].k = inet_network(start); dgram_filter[2].k = inet_network(end);
bpf.len = ARRAY_SIZE(dgram_filter); bpf.filter = dgram_filter; } else {
socket_type = SOCK_RAW; cb = arp_raw_callback;
raw_filter[3].k = inet_network(start); raw_filter[4].k = inet_network(end);
bpf.len = ARRAY_SIZE(raw_filter); bpf.filter = raw_filter; }
ar = (typeof(ar))malloc(sizeof(*ar)); if (ar == NULL) { LOG_ERROR("out of memory\n"); return NULL; } memcpy(ar->hwaddr, hwaddr, ETH_ALEN);
ar->fd = socket(AF_PACKET, socket_type, htons(ETH_P_ARP)); if (ar->fd == -1) { LOG_ERROR("socket AF_PACKET failed, %s\n", strerror(errno)); free(ar); return NULL; } memset(&addr, '\0', sizeof(addr)); addr.sll_family = AF_PACKET; addr.sll_protocol = htons(ETH_P_ARP); addr.sll_ifindex = ifindex; if (bind(ar->fd, (struct sockaddr *)&addr, sizeof(addr))) { LOG_ERROR("bind failed, %s\n", strerror(errno)); close(ar->fd); free(ar); return NULL; }
if (setsockopt(ar->fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)) != 0) { LOG_ERROR("set bpf filter failed, %s\n", strerror(errno)); close(ar->fd); free(ar); return NULL; }
ev_io_init(&ar->io_watcher, cb, ar->fd, EV_READ); ev_io_start(loop, &ar->io_watcher); ar->io_watcher.data = ar;
return ar; }
static int get_ifindex(const char *if_name) { struct ifreq ifr; int r; int index; int fd;
fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd == -1) { LOG_ERROR("socket failed, %s\n", strerror(errno)); return -1; }
strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
r = ioctl(fd, SIOCGIFINDEX, &ifr); if (r == -1) { LOG_ERROR("get %s ifindex failed, %s\n", if_name, strerror(errno)); close(fd); return -1; } close(fd); index = ifr.ifr_ifindex; return index; }
static int get_hwaddr(const char *if_name, unsigned char buf[6]) { struct ifreq ifr; int r; int fd;
fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd == -1) { LOG_ERROR("socket failed, %s\n", strerror(errno)); return -1; }
strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
r = ioctl(fd, SIOCGIFHWADDR, &ifr); if (r == -1) { LOG_ERROR("get %s hwaddr failed, %s\n", if_name, strerror(errno)); close(fd); return -1; } close(fd); if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) { LOG_ERROR("invalide hwaddr sa_family %u\n", ifr.ifr_hwaddr.sa_family); return -1; } memcpy(buf, ifr.ifr_hwaddr.sa_data, 6); return 0; }
int main(int argc, const char *argv[]) { struct ev_loop *loop = NULL; struct ev_async async_watcher; pthread_t thread; struct sigaction term_action; struct my_arp_response_struct *arp_response = NULL; int ifindex; unsigned char hwaddr[6]; int is_dgram;
if (argc < 4) { printf("usage: %s ifname pseudo_ip_start pseudo_ip_end [dgram]\n" " ifname: 监听arp请求包的网络设备名\n" " pseudo_ip_start: 监听arp请求的ip地址范围的起始地址\n" " pseudo_ip_end: 监听arp请求的ip地址范围的结束地址\n" " dgram: 大于0的数字表示使用SOCK_DGRAM模式," "默认使用SOCK_RAW模式\n" , argv[0]); goto cleanup; }
bzero(&term_action, sizeof(term_action)); term_action.sa_handler = term_handler; if (sigaction(SIGINT, &term_action, NULL) || sigaction(SIGTERM, &term_action, NULL)) { LOG_ERROR("sigaction failed, %s\n", strerror(errno)); goto cleanup; }
loop = ev_loop_new(EVFLAG_AUTO); if (loop == NULL) { LOG_ERROR("ev_loop_new failed\n"); goto cleanup; } ev_async_init(&async_watcher, async_callback); ev_async_start(loop, &async_watcher);
ifindex = get_ifindex(argv[1]); if (ifindex == -1) { LOG_ERROR("get_ifindex failed\n"); goto cleanup; } LOG_INFO("interface %s index %d\n", argv[1], ifindex);
if (get_hwaddr(argv[1], hwaddr)) { LOG_ERROR("get_hwaddr failed\n"); goto cleanup; } LOG_INFO("interface %s hwaddr %02x:%02x:%02x:%02x:%02x:%02x\n", argv[1], hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
if (argc > 4 && atoi(argv[4]) > 0) { is_dgram = 1; } else { is_dgram = 0; } LOG_INFO("socket type: %s\n", is_dgram ? "SOCK_DGRAM" : "SOCK_RAW");
arp_response = get_arp_response_struct(ifindex, loop, argv[2], argv[3], hwaddr, is_dgram); if (arp_response == NULL) { LOG_ERROR("get_arp_response_struct failed\n"); goto cleanup; }
if (pthread_create(&thread, NULL, thread_loop, loop)) { LOG_ERROR("pthread_create failed\n"); goto cleanup; }
while (!_stop) { sleep(1); }
LOG_INFO("before async send\n"); ev_async_send(loop, &async_watcher); LOG_INFO("after async send\n");
pthread_join(thread, NULL);
close(arp_response->fd); free(arp_response); ev_loop_destroy(loop);
LOG_INFO("bye\n"); return 0;
cleanup: if (arp_response) { close(arp_response->fd); free(arp_response); } if (loop) ev_loop_destroy(loop); return -1; }
|