1/* $NetBSD: pcap-sita.c,v 1.7 2023/08/17 15:18:12 christos Exp $ */ 2 3/* 4 * pcap-sita.c: Packet capture interface additions for SITA ACN devices 5 * 6 * Copyright (c) 2007 Fulko Hew, SITA INC Canada, Inc <fulko.hew@sita.aero> 7 * 8 * License: BSD 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 3. The names of the authors may not be used to endorse or promote 21 * products derived from this software without specific prior 22 * written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 27 */ 28 29#include <sys/cdefs.h> 30__RCSID("$NetBSD: pcap-sita.c,v 1.7 2023/08/17 15:18:12 christos Exp $"); 31 32#ifdef HAVE_CONFIG_H 33#include <config.h> 34#endif 35 36#include <stdio.h> 37#include <string.h> 38#include <stdlib.h> 39#include <unistd.h> 40#include <fcntl.h> 41#include <errno.h> 42#include <sys/time.h> 43#include <sys/socket.h> 44#include <netinet/in.h> 45#include <arpa/inet.h> 46#include "pcap-int.h" 47 48#include "pcap-sita.h" 49 50 /* non-configureable manifests follow */ 51 52#define IOP_SNIFFER_PORT 49152 /* TCP port on the IOP used for 'distributed pcap' usage */ 53#define MAX_LINE_SIZE 255 /* max size of a buffer/line in /etc/hosts we allow */ 54#define MAX_CHASSIS 8 /* number of chassis in an ACN site */ 55#define MAX_GEOSLOT 8 /* max number of access units in an ACN site */ 56 57#define FIND 0 58#define LIVE 1 59 60typedef struct iface { 61 struct iface *next; /* a pointer to the next interface */ 62 char *name; /* this interface's name */ 63 char *IOPname; /* this interface's name on an IOP */ 64 uint32_t iftype; /* the type of interface (DLT values) */ 65} iface_t; 66 67typedef struct unit { 68 char *ip; /* this unit's IP address (as extracted from /etc/hosts) */ 69 int fd; /* the connection to this unit (if it exists) */ 70 int find_fd; /* a big kludge to avoid my programming limitations since I could have this unit open for findalldevs purposes */ 71 int first_time; /* 0 = just opened via acn_open_live(), ie. the first time, NZ = nth time */ 72 struct sockaddr_in *serv_addr; /* the address control block for comms to this unit */ 73 int chassis; 74 int geoslot; 75 iface_t *iface; /* a pointer to a linked list of interface structures */ 76 char *imsg; /* a pointer to an inbound message */ 77 int len; /* the current size of the inbound message */ 78} unit_t; 79 80/* 81 * Private data. 82 * Currently contains nothing. 83 */ 84struct pcap_sita { 85 int dummy; 86}; 87 88static unit_t units[MAX_CHASSIS+1][MAX_GEOSLOT+1]; /* we use indexes of 1 through 8, but we reserve/waste index 0 */ 89static fd_set readfds; /* a place to store the file descriptors for the connections to the IOPs */ 90static int max_fs; 91 92pcap_if_t *acn_if_list; /* pcap's list of available interfaces */ 93 94static void dump_interface_list(void) { 95 pcap_if_t *iff; 96 pcap_addr_t *addr; 97 int longest_name_len = 0; 98 char *n, *d, *f; 99 int if_number = 0; 100 101 iff = acn_if_list; 102 while (iff) { 103 if (iff->name && (strlen(iff->name) > longest_name_len)) longest_name_len = strlen(iff->name); 104 iff = iff->next; 105 } 106 iff = acn_if_list; 107 printf("Interface List:\n"); 108 while (iff) { 109 n = (iff->name) ? iff->name : ""; 110 d = (iff->description) ? iff->description : ""; 111 f = (iff->flags == PCAP_IF_LOOPBACK) ? "L" : ""; 112 printf("%3d: %*s %s '%s'\n", if_number++, longest_name_len, n, f, d); 113 addr = iff->addresses; 114 while (addr) { 115 printf("%*s ", (5 + longest_name_len), ""); /* add some indentation */ 116 printf("%15s ", (addr->addr) ? inet_ntoa(((struct sockaddr_in *)addr->addr)->sin_addr) : ""); 117 printf("%15s ", (addr->netmask) ? inet_ntoa(((struct sockaddr_in *)addr->netmask)->sin_addr) : ""); 118 printf("%15s ", (addr->broadaddr) ? inet_ntoa(((struct sockaddr_in *)addr->broadaddr)->sin_addr) : ""); 119 printf("%15s ", (addr->dstaddr) ? inet_ntoa(((struct sockaddr_in *)addr->dstaddr)->sin_addr) : ""); 120 printf("\n"); 121 addr = addr->next; 122 } 123 iff = iff->next; 124 } 125} 126 127static void dump(unsigned char *ptr, int i, int indent) { 128 fprintf(stderr, "%*s", indent, " "); 129 for (; i > 0; i--) { 130 fprintf(stderr, "%2.2x ", *ptr++); 131 } 132 fprintf(stderr, "\n"); 133} 134 135static void dump_interface_list_p(void) { 136 pcap_if_t *iff; 137 pcap_addr_t *addr; 138 int if_number = 0; 139 140 iff = acn_if_list; 141 printf("Interface Pointer @ %p is %p:\n", &acn_if_list, iff); 142 while (iff) { 143 printf("%3d: %p %p next: %p\n", if_number++, iff->name, iff->description, iff->next); 144 dump((unsigned char *)iff, sizeof(pcap_if_t), 5); 145 addr = iff->addresses; 146 while (addr) { 147 printf(" %p %p %p %p, next: %p\n", addr->addr, addr->netmask, addr->broadaddr, addr->dstaddr, addr->next); 148 dump((unsigned char *)addr, sizeof(pcap_addr_t), 10); 149 addr = addr->next; 150 } 151 iff = iff->next; 152 } 153} 154 155static void dump_unit_table(void) { 156 int chassis, geoslot; 157 iface_t *p; 158 159 printf("%c:%c %s %s\n", 'C', 'S', "fd", "IP Address"); 160 for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { 161 for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) { 162 if (units[chassis][geoslot].ip != NULL) 163 printf("%d:%d %2d %s\n", chassis, geoslot, units[chassis][geoslot].fd, units[chassis][geoslot].ip); 164 p = units[chassis][geoslot].iface; 165 while (p) { 166 char *n = (p->name) ? p->name : ""; 167 char *i = (p->IOPname) ? p->IOPname : ""; 168 p = p->next; 169 printf(" %12s -> %12s\n", i, n); 170 } 171 } 172 } 173} 174 175static int find_unit_by_fd(int fd, int *chassis, int *geoslot, unit_t **unit_ptr) { 176 int c, s; 177 178 for (c = 0; c <= MAX_CHASSIS; c++) { 179 for (s = 0; s <= MAX_GEOSLOT; s++) { 180 if (units[c][s].fd == fd || units[c][s].find_fd == fd) { 181 if (chassis) *chassis = c; 182 if (geoslot) *geoslot = s; 183 if (unit_ptr) *unit_ptr = &units[c][s]; 184 return 1; 185 } 186 } 187 } 188 return 0; 189} 190 191static int read_client_nbytes(int fd, int count, unsigned char *buf) { 192 unit_t *u; 193 int chassis, geoslot; 194 int len; 195 196 find_unit_by_fd(fd, &chassis, &geoslot, &u); 197 while (count) { 198 if ((len = recv(fd, buf, count, 0)) <= 0) return -1; /* read in whatever data was sent to us */ 199 count -= len; 200 buf += len; 201 } /* till we have everything we are looking for */ 202 return 0; 203} 204 205static void empty_unit_iface(unit_t *u) { 206 iface_t *p, *cur; 207 208 cur = u->iface; 209 while (cur) { /* loop over all the interface entries */ 210 if (cur->name) free(cur->name); /* throwing away the contents if they exist */ 211 if (cur->IOPname) free(cur->IOPname); 212 p = cur->next; 213 free(cur); /* then throw away the structure itself */ 214 cur = p; 215 } 216 u->iface = 0; /* and finally remember that there are no remaining structure */ 217} 218 219static void empty_unit(int chassis, int geoslot) { 220 unit_t *u = &units[chassis][geoslot]; 221 222 empty_unit_iface(u); 223 if (u->imsg) { /* then if an inbound message buffer exists */ 224 void *bigger_buffer; 225 226 bigger_buffer = (char *)realloc(u->imsg, 1); /* and re-allocate the old large buffer into a new small one */ 227 if (bigger_buffer == NULL) { /* oops, realloc call failed */ 228 fprintf(stderr, "Warning...call to realloc() failed, value of errno is %d\n", errno); 229 return; 230 } 231 u->imsg = bigger_buffer; 232 } 233} 234 235static void empty_unit_table(void) { 236 int chassis, geoslot; 237 238 for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { 239 for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) { 240 if (units[chassis][geoslot].ip != NULL) { 241 free(units[chassis][geoslot].ip); /* get rid of the malloc'ed space that holds the IP address */ 242 units[chassis][geoslot].ip = 0; /* then set the pointer to NULL */ 243 } 244 empty_unit(chassis, geoslot); 245 } 246 } 247} 248 249static char *find_nth_interface_name(int n) { 250 int chassis, geoslot; 251 iface_t *p; 252 char *last_name = 0; 253 254 if (n < 0) n = 0; /* ensure we are working with a valid number */ 255 for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { /* scan the table... */ 256 for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) { 257 if (units[chassis][geoslot].ip != NULL) { 258 p = units[chassis][geoslot].iface; 259 while (p) { /* and all interfaces... */ 260 if (p->IOPname) last_name = p->name; /* remembering the last name found */ 261 if (n-- == 0) return last_name; /* and if we hit the instance requested */ 262 p = p->next; 263 } 264 } 265 } 266 } 267 /* if we couldn't fine the selected entry */ 268 if (last_name) return last_name; /* ... but we did have at least one entry... return the last entry found */ 269 return ""; /* ... but if there wasn't any entry... return an empty string instead */ 270} 271 272int acn_parse_hosts_file(char *errbuf) { /* returns: -1 = error, 0 = OK */ 273 FILE *fp; 274 char buf[MAX_LINE_SIZE]; 275 char *ptr, *ptr2; 276 int pos; 277 int chassis, geoslot; 278 unit_t *u; 279 280 empty_unit_table(); 281 if ((fp = fopen("/etc/hosts", "r")) == NULL) { /* try to open the hosts file and if it fails */ 282 snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot open '/etc/hosts' for reading."); /* return the nohostsfile error response */ 283 return -1; 284 } 285 while (fgets(buf, MAX_LINE_SIZE-1, fp)) { /* while looping over the file */ 286 287 pos = strcspn(buf, "#\n\r"); /* find the first comment character or EOL */ 288 *(buf + pos) = '\0'; /* and clobber it and anything that follows it */ 289 290 pos = strspn(buf, " \t"); /* then find the first non-white space */ 291 if (pos == strlen(buf)) /* if there is nothing but white space on the line */ 292 continue; /* ignore that empty line */ 293 ptr = buf + pos; /* and skip over any of that leading whitespace */ 294 295 if ((ptr2 = strstr(ptr, "_I_")) == NULL) /* skip any lines that don't have names that look like they belong to IOPs */ 296 continue; 297 if (*(ptr2 + 4) != '_') /* and skip other lines that have names that don't look like ACN components */ 298 continue; 299 *(ptr + strcspn(ptr, " \t")) = '\0'; /* null terminate the IP address so its a standalone string */ 300 301 chassis = *(ptr2 + 3) - '0'; /* extract the chassis number */ 302 geoslot = *(ptr2 + 5) - '0'; /* and geo-slot number */ 303 if (chassis < 1 || chassis > MAX_CHASSIS || 304 geoslot < 1 || geoslot > MAX_GEOSLOT) { /* if the chassis and/or slot numbers appear to be bad... */ 305 snprintf(errbuf, PCAP_ERRBUF_SIZE, "Invalid ACN name in '/etc/hosts'."); /* warn the user */ 306 continue; /* and ignore the entry */ 307 } 308 ptr2 = strdup(ptr); /* copy the IP address into our malloc'ed memory */ 309 if (ptr2 == NULL) { 310 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, 311 errno, "malloc"); 312 continue; 313 } 314 u = &units[chassis][geoslot]; 315 u->ip = ptr2; /* and remember the whole shebang */ 316 u->chassis = chassis; 317 u->geoslot = geoslot; 318 } 319 fclose(fp); 320 if (*errbuf) return -1; 321 else return 0; 322} 323 324static int open_with_IOP(unit_t *u, int flag) { 325 int sockfd; 326 char *ip; 327 328 if (u->serv_addr == NULL) { 329 u->serv_addr = malloc(sizeof(struct sockaddr_in)); 330 331 /* since we called malloc(), lets check to see if we actually got the memory */ 332 if (u->serv_addr == NULL) { /* oops, we didn't get the memory requested */ 333 fprintf(stderr, "malloc() request for u->serv_addr failed, value of errno is: %d\n", errno); 334 return 0; 335 } 336 337 } 338 ip = u->ip; 339 /* bzero() is deprecated, replaced with memset() */ 340 memset((char *)u->serv_addr, 0, sizeof(struct sockaddr_in)); 341 u->serv_addr->sin_family = AF_INET; 342 u->serv_addr->sin_addr.s_addr = inet_addr(ip); 343 u->serv_addr->sin_port = htons(IOP_SNIFFER_PORT); 344 345 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 346 fprintf(stderr, "pcap can't open a socket for connecting to IOP at %s\n", ip); 347 return 0; 348 } 349 if (connect(sockfd, (struct sockaddr *)u->serv_addr, sizeof(struct sockaddr_in)) < 0) { 350 fprintf(stderr, "pcap can't connect to IOP at %s\n", ip); 351 return 0; 352 } 353 if (flag == LIVE) u->fd = sockfd; 354 else u->find_fd = sockfd; 355 u->first_time = 0; 356 return sockfd; /* return the non-zero file descriptor as a 'success' indicator */ 357} 358 359static void close_with_IOP(int chassis, int geoslot, int flag) { 360 int *id; 361 362 if (flag == LIVE) id = &units[chassis][geoslot].fd; 363 else id = &units[chassis][geoslot].find_fd; 364 365 if (*id) { /* this was the last time, so... if we are connected... */ 366 close(*id); /* disconnect us */ 367 *id = 0; /* and forget that the descriptor exists because we are not open */ 368 } 369} 370 371static void pcap_cleanup_acn(pcap_t *handle) { 372 int chassis, geoslot; 373 unit_t *u; 374 375 if (find_unit_by_fd(handle->fd, &chassis, &geoslot, &u) == 0) 376 return; 377 close_with_IOP(chassis, geoslot, LIVE); 378 if (u) 379 u->first_time = 0; 380 pcap_cleanup_live_common(handle); 381} 382 383static void send_to_fd(int fd, int len, unsigned char *str) { 384 int nwritten; 385 int chassis, geoslot; 386 387 while (len > 0) { 388 if ((nwritten = write(fd, str, len)) <= 0) { 389 find_unit_by_fd(fd, &chassis, &geoslot, NULL); 390 if (units[chassis][geoslot].fd == fd) close_with_IOP(chassis, geoslot, LIVE); 391 else if (units[chassis][geoslot].find_fd == fd) close_with_IOP(chassis, geoslot, FIND); 392 empty_unit(chassis, geoslot); 393 return; 394 } 395 len -= nwritten; 396 str += nwritten; 397 } 398} 399 400static void acn_freealldevs(void) { 401 402 pcap_if_t *iff, *next_iff; 403 pcap_addr_t *addr, *next_addr; 404 405 for (iff = acn_if_list; iff != NULL; iff = next_iff) { 406 next_iff = iff->next; 407 for (addr = iff->addresses; addr != NULL; addr = next_addr) { 408 next_addr = addr->next; 409 if (addr->addr) free(addr->addr); 410 if (addr->netmask) free(addr->netmask); 411 if (addr->broadaddr) free(addr->broadaddr); 412 if (addr->dstaddr) free(addr->dstaddr); 413 free(addr); 414 } 415 if (iff->name) free(iff->name); 416 if (iff->description) free(iff->description); 417 free(iff); 418 } 419} 420 421static void nonUnified_IOP_port_name(char *buf, size_t bufsize, const char *proto, unit_t *u) { 422 423 snprintf(buf, bufsize, "%s_%d_%d", proto, u->chassis, u->geoslot); 424} 425 426static void unified_IOP_port_name(char *buf, size_t bufsize, const char *proto, unit_t *u, int IOPportnum) { 427 int portnum; 428 429 portnum = ((u->chassis - 1) * 64) + ((u->geoslot - 1) * 8) + IOPportnum + 1; 430 snprintf(buf, bufsize, "%s_%d", proto, portnum); 431} 432 433static char *translate_IOP_to_pcap_name(unit_t *u, char *IOPname, bpf_u_int32 iftype) { 434 iface_t *iface_ptr, *iface; 435 char buf[32]; 436 char *proto; 437 char *port; 438 int IOPportnum = 0; 439 440 iface = malloc(sizeof(iface_t)); /* get memory for a structure */ 441 if (iface == NULL) { /* oops, we didn't get the memory requested */ 442 fprintf(stderr, "Error...couldn't allocate memory for interface structure...value of errno is: %d\n", errno); 443 return NULL; 444 } 445 memset((char *)iface, 0, sizeof(iface_t)); /* bzero is deprecated(), replaced with memset() */ 446 447 iface->iftype = iftype; /* remember the interface type of this interface */ 448 449 iface->IOPname = strdup(IOPname); /* copy it and stick it into the structure */ 450 if (iface->IOPname == NULL) { /* oops, we didn't get the memory requested */ 451 fprintf(stderr, "Error...couldn't allocate memory for IOPname...value of errno is: %d\n", errno); 452 return NULL; 453 } 454 455 if (strncmp(IOPname, "lo", 2) == 0) { 456 IOPportnum = atoi(&IOPname[2]); 457 switch (iftype) { 458 case DLT_EN10MB: 459 nonUnified_IOP_port_name(buf, sizeof buf, "lo", u); 460 break; 461 default: 462 unified_IOP_port_name(buf, sizeof buf, "???", u, IOPportnum); 463 break; 464 } 465 } else if (strncmp(IOPname, "eth", 3) == 0) { 466 IOPportnum = atoi(&IOPname[3]); 467 switch (iftype) { 468 case DLT_EN10MB: 469 nonUnified_IOP_port_name(buf, sizeof buf, "eth", u); 470 break; 471 default: 472 unified_IOP_port_name(buf, sizeof buf, "???", u, IOPportnum); 473 break; 474 } 475 } else if (strncmp(IOPname, "wan", 3) == 0) { 476 IOPportnum = atoi(&IOPname[3]); 477 switch (iftype) { 478 case DLT_SITA: 479 unified_IOP_port_name(buf, sizeof buf, "wan", u, IOPportnum); 480 break; 481 default: 482 unified_IOP_port_name(buf, sizeof buf, "???", u, IOPportnum); 483 break; 484 } 485 } else { 486 fprintf(stderr, "Error... invalid IOP name %s\n", IOPname); 487 return NULL; 488 } 489 490 iface->name = strdup(buf); /* make a copy and stick it into the structure */ 491 if (iface->name == NULL) { /* oops, we didn't get the memory requested */ 492 fprintf(stderr, "Error...couldn't allocate memory for IOP port name...value of errno is: %d\n", errno); 493 return NULL; 494 } 495 496 if (u->iface == 0) { /* if this is the first name */ 497 u->iface = iface; /* stick this entry at the head of the list */ 498 } else { 499 iface_ptr = u->iface; 500 while (iface_ptr->next) { /* otherwise scan the list */ 501 iface_ptr = iface_ptr->next; /* till we're at the last entry */ 502 } 503 iface_ptr->next = iface; /* then tack this entry on the end of the list */ 504 } 505 return iface->name; 506} 507 508static int if_sort(char *s1, char *s2) { 509 char *s1_p2, *s2_p2; 510 char str1[MAX_LINE_SIZE], str2[MAX_LINE_SIZE]; 511 int s1_p1_len, s2_p1_len; 512 int retval; 513 514 if ((s1_p2 = strchr(s1, '_'))) { /* if an underscore is found... */ 515 s1_p1_len = s1_p2 - s1; /* the prefix length is the difference in pointers */ 516 s1_p2++; /* the suffix actually starts _after_ the underscore */ 517 } else { /* otherwise... */ 518 s1_p1_len = strlen(s1); /* the prefix length is the length of the string itself */ 519 s1_p2 = 0; /* and there is no suffix */ 520 } 521 if ((s2_p2 = strchr(s2, '_'))) { /* now do the same for the second string */ 522 s2_p1_len = s2_p2 - s2; 523 s2_p2++; 524 } else { 525 s2_p1_len = strlen(s2); 526 s2_p2 = 0; 527 } 528 strncpy(str1, s1, (s1_p1_len > sizeof(str1)) ? s1_p1_len : sizeof(str1)); *(str1 + s1_p1_len) = 0; 529 strncpy(str2, s2, (s2_p1_len > sizeof(str2)) ? s2_p1_len : sizeof(str2)); *(str2 + s2_p1_len) = 0; 530 retval = strcmp(str1, str2); 531 if (retval != 0) return retval; /* if they are not identical, then we can quit now and return the indication */ 532 return strcmp(s1_p2, s2_p2); /* otherwise we return the result of comparing the 2nd half of the string */ 533} 534 535static void sort_if_table(void) { 536 pcap_if_t *p1, *p2, *prev, *temp; 537 int has_swapped; 538 539 if (!acn_if_list) return; /* nothing to do if the list is empty */ 540 541 while (1) { 542 p1 = acn_if_list; /* start at the head of the list */ 543 prev = 0; 544 has_swapped = 0; 545 while ((p2 = p1->next)) { 546 if (if_sort(p1->name, p2->name) > 0) { 547 if (prev) { /* we are swapping things that are _not_ at the head of the list */ 548 temp = p2->next; 549 prev->next = p2; 550 p2->next = p1; 551 p1->next = temp; 552 } else { /* special treatment if we are swapping with the head of the list */ 553 temp = p2->next; 554 acn_if_list= p2; 555 p2->next = p1; 556 p1->next = temp; 557 } 558 p1 = p2; 559 prev = p1; 560 has_swapped = 1; 561 } 562 prev = p1; 563 p1 = p1->next; 564 } 565 if (has_swapped == 0) 566 return; 567 } 568 return; 569} 570 571static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 = OK */ 572 int chassis, geoslot; 573 unit_t *u; 574 pcap_if_t *iff, *prev_iff; 575 pcap_addr_t *addr, *prev_addr; 576 char *ptr; 577 int address_count; 578 struct sockaddr_in *s; 579 char *newname; 580 bpf_u_int32 interfaceType; 581 unsigned char flags; 582 void *bigger_buffer; 583 584 prev_iff = 0; 585 for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { 586 for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) { /* now loop over all the devices */ 587 u = &units[chassis][geoslot]; 588 empty_unit_iface(u); 589 ptr = u->imsg; /* point to the start of the msg for this IOP */ 590 while (ptr < (u->imsg + u->len)) { 591 if ((iff = malloc(sizeof(pcap_if_t))) == NULL) { 592 pcap_fmt_errmsg_for_errno(errbuf, 593 PCAP_ERRBUF_SIZE, errno, "malloc"); 594 return -1; 595 } 596 memset((char *)iff, 0, sizeof(pcap_if_t)); /* bzero() is deprecated, replaced with memset() */ 597 if (acn_if_list == 0) acn_if_list = iff; /* remember the head of the list */ 598 if (prev_iff) prev_iff->next = iff; /* insert a forward link */ 599 600 if (*ptr) { /* if there is a count for the name */ 601 if ((iff->name = malloc(*ptr + 1)) == NULL) { /* get that amount of space */ 602 pcap_fmt_errmsg_for_errno(errbuf, 603 PCAP_ERRBUF_SIZE, errno, 604 "malloc"); 605 return -1; 606 } 607 memcpy(iff->name, (ptr + 1), *ptr); /* copy the name into the malloc'ed space */ 608 *(iff->name + *ptr) = 0; /* and null terminate the string */ 609 ptr += *ptr; /* now move the pointer forwards by the length of the count plus the length of the string */ 610 } 611 ptr++; 612 613 if (*ptr) { /* if there is a count for the description */ 614 if ((iff->description = malloc(*ptr + 1)) == NULL) { /* get that amount of space */ 615 pcap_fmt_errmsg_for_errno(errbuf, 616 PCAP_ERRBUF_SIZE, errno, 617 "malloc"); 618 return -1; 619 } 620 memcpy(iff->description, (ptr + 1), *ptr); /* copy the name into the malloc'ed space */ 621 *(iff->description + *ptr) = 0; /* and null terminate the string */ 622 ptr += *ptr; /* now move the pointer forwards by the length of the count plus the length of the string */ 623 } 624 ptr++; 625 626 interfaceType = ntohl(*(bpf_u_int32 *)ptr); 627 ptr += 4; /* skip over the interface type */ 628 629 flags = *ptr++; 630 if (flags) iff->flags = PCAP_IF_LOOPBACK; /* if this is a loopback style interface, lets mark it as such */ 631 632 address_count = *ptr++; 633 634 prev_addr = 0; 635 while (address_count--) { 636 if ((addr = malloc(sizeof(pcap_addr_t))) == NULL) { 637 pcap_fmt_errmsg_for_errno(errbuf, 638 PCAP_ERRBUF_SIZE, errno, 639 "malloc"); 640 return -1; 641 } 642 memset((char *)addr, 0, sizeof(pcap_addr_t)); /* bzero() is deprecated, replaced with memset() */ 643 if (iff->addresses == 0) iff->addresses = addr; 644 if (prev_addr) prev_addr->next = addr; /* insert a forward link */ 645 if (*ptr) { /* if there is a count for the address */ 646 if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { /* get that amount of space */ 647 pcap_fmt_errmsg_for_errno(errbuf, 648 PCAP_ERRBUF_SIZE, 649 errno, "malloc"); 650 return -1; 651 } 652 memset((char *)s, 0, sizeof(struct sockaddr_in)); /* bzero() is deprecated, replaced with memset() */ 653 addr->addr = (struct sockaddr *)s; 654 s->sin_family = AF_INET; 655 s->sin_addr.s_addr = *(bpf_u_int32 *)(ptr + 1); /* copy the address in */ 656 ptr += *ptr; /* now move the pointer forwards according to the specified length of the address */ 657 } 658 ptr++; /* then forwards one more for the 'length of the address' field */ 659 if (*ptr) { /* process any netmask */ 660 if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { 661 pcap_fmt_errmsg_for_errno(errbuf, 662 PCAP_ERRBUF_SIZE, 663 errno, "malloc"); 664 return -1; 665 } 666 /* bzero() is deprecated, replaced with memset() */ 667 memset((char *)s, 0, sizeof(struct sockaddr_in)); 668 669 addr->netmask = (struct sockaddr *)s; 670 s->sin_family = AF_INET; 671 s->sin_addr.s_addr = *(bpf_u_int32*)(ptr + 1); 672 ptr += *ptr; 673 } 674 ptr++; 675 if (*ptr) { /* process any broadcast address */ 676 if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { 677 pcap_fmt_errmsg_for_errno(errbuf, 678 PCAP_ERRBUF_SIZE, 679 errno, "malloc"); 680 return -1; 681 } 682 /* bzero() is deprecated, replaced with memset() */ 683 memset((char *)s, 0, sizeof(struct sockaddr_in)); 684 685 addr->broadaddr = (struct sockaddr *)s; 686 s->sin_family = AF_INET; 687 s->sin_addr.s_addr = *(bpf_u_int32*)(ptr + 1); 688 ptr += *ptr; 689 } 690 ptr++; 691 if (*ptr) { /* process any destination address */ 692 if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { 693 pcap_fmt_errmsg_for_errno(errbuf, 694 PCAP_ERRBUF_SIZE, 695 errno, "malloc"); 696 return -1; 697 } 698 /* bzero() is deprecated, replaced with memset() */ 699 memset((char *)s, 0, sizeof(struct sockaddr_in)); 700 701 addr->dstaddr = (struct sockaddr *)s; 702 s->sin_family = AF_INET; 703 s->sin_addr.s_addr = *(bpf_u_int32*)(ptr + 1); 704 ptr += *ptr; 705 } 706 ptr++; 707 prev_addr = addr; 708 } 709 prev_iff = iff; 710 711 newname = translate_IOP_to_pcap_name(u, iff->name, interfaceType); /* add a translation entry and get a point to the mangled name */ 712 bigger_buffer = realloc(iff->name, strlen(newname) + 1); 713 if (bigger_buffer == NULL) { /* we now re-write the name stored in the interface list */ 714 pcap_fmt_errmsg_for_errno(errbuf, 715 PCAP_ERRBUF_SIZE, errno, "realloc"); 716 return -1; 717 } 718 iff->name = bigger_buffer; 719 strcpy(iff->name, newname); /* to this new name */ 720 } 721 } 722 } 723 return 0; 724} 725 726static int read_client_data (int fd) { 727 unsigned char buf[256]; 728 int chassis, geoslot; 729 unit_t *u; 730 int len; 731 732 find_unit_by_fd(fd, &chassis, &geoslot, &u); 733 734 if ((len = recv(fd, buf, sizeof(buf), 0)) <= 0) return 0; /* read in whatever data was sent to us */ 735 736 if ((u->imsg = realloc(u->imsg, (u->len + len))) == NULL) /* extend the buffer for the new data */ 737 return 0; 738 memcpy((u->imsg + u->len), buf, len); /* append the new data */ 739 u->len += len; 740 return 1; 741} 742 743static void wait_for_all_answers(void) { 744 int retval; 745 struct timeval tv; 746 int fd; 747 int chassis, geoslot; 748 749 tv.tv_sec = 2; 750 tv.tv_usec = 0; 751 752 while (1) { 753 int flag = 0; 754 fd_set working_set; 755 756 for (fd = 0; fd <= max_fs; fd++) { /* scan the list of descriptors we may be listening to */ 757 if (FD_ISSET(fd, &readfds)) flag = 1; /* and see if there are any still set */ 758 } 759 if (flag == 0) return; /* we are done, when they are all gone */ 760 761 memcpy(&working_set, &readfds, sizeof(readfds)); /* otherwise, we still have to listen for more stuff, till we timeout */ 762 retval = select(max_fs + 1, &working_set, NULL, NULL, &tv); 763 if (retval == -1) { /* an error occurred !!!!! */ 764 return; 765 } else if (retval == 0) { /* timeout occurred, so process what we've got sofar and return */ 766 printf("timeout\n"); 767 return; 768 } else { 769 for (fd = 0; fd <= max_fs; fd++) { /* scan the list of things to do, and do them */ 770 if (FD_ISSET(fd, &working_set)) { 771 if (read_client_data(fd) == 0) { /* if the socket has closed */ 772 FD_CLR(fd, &readfds); /* and descriptors we listen to for errors */ 773 find_unit_by_fd(fd, &chassis, &geoslot, NULL); 774 close_with_IOP(chassis, geoslot, FIND); /* and close out connection to him */ 775 } 776 } 777 } 778 } 779 } 780} 781 782static char *get_error_response(int fd, char *errbuf) { /* return a pointer on error, NULL on no error */ 783 char byte; 784 int len = 0; 785 786 while (1) { 787 recv(fd, &byte, 1, 0); /* read another byte in */ 788 if (errbuf && (len++ < PCAP_ERRBUF_SIZE)) { /* and if there is still room in the buffer */ 789 *errbuf++ = byte; /* stick it in */ 790 *errbuf = '\0'; /* ensure the string is null terminated just in case we might exceed the buffer's size */ 791 } 792 if (byte == '\0') { 793 if (len > 1) { return errbuf; } 794 else { return NULL; } 795 } 796 } 797} 798 799int acn_findalldevs(char *errbuf) { /* returns: -1 = error, 0 = OK */ 800 int chassis, geoslot; 801 unit_t *u; 802 803 FD_ZERO(&readfds); 804 max_fs = 0; 805 for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { 806 for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) { 807 u = &units[chassis][geoslot]; 808 if (u->ip && (open_with_IOP(u, FIND))) { /* connect to the remote IOP */ 809 send_to_fd(u->find_fd, 1, (unsigned char *)"\0"); 810 if (get_error_response(u->find_fd, errbuf)) 811 close_with_IOP(chassis, geoslot, FIND); 812 else { 813 if (u->find_fd > max_fs) 814 max_fs = u->find_fd; /* remember the highest number currently in use */ 815 FD_SET(u->find_fd, &readfds); /* we are going to want to read this guy's response to */ 816 u->len = 0; 817 send_to_fd(u->find_fd, 1, (unsigned char *)"Q"); /* this interface query request */ 818 } 819 } 820 } 821 } 822 wait_for_all_answers(); 823 if (process_client_data(errbuf)) 824 return -1; 825 sort_if_table(); 826 return 0; 827} 828 829static int pcap_stats_acn(pcap_t *handle, struct pcap_stat *ps) { 830 unsigned char buf[12]; 831 832 send_to_fd(handle->fd, 1, (unsigned char *)"S"); /* send the get_stats command to the IOP */ 833 834 if (read_client_nbytes(handle->fd, sizeof(buf), buf) == -1) return -1; /* try reading the required bytes */ 835 836 ps->ps_recv = ntohl(*(uint32_t *)&buf[0]); /* break the buffer into its three 32 bit components */ 837 ps->ps_drop = ntohl(*(uint32_t *)&buf[4]); 838 ps->ps_ifdrop = ntohl(*(uint32_t *)&buf[8]); 839 840 return 0; 841} 842 843static int acn_open_live(const char *name, char *errbuf, int *linktype) { /* returns 0 on error, else returns the file descriptor */ 844 int chassis, geoslot; 845 unit_t *u; 846 iface_t *p; 847 pcap_if_list_t devlist; 848 849 pcap_platform_finddevs(&devlist, errbuf); 850 for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { /* scan the table... */ 851 for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) { 852 u = &units[chassis][geoslot]; 853 if (u->ip != NULL) { 854 p = u->iface; 855 while (p) { /* and all interfaces... */ 856 if (p->IOPname && p->name && (strcmp(p->name, name) == 0)) { /* and if we found the interface we want... */ 857 *linktype = p->iftype; 858 open_with_IOP(u, LIVE); /* start a connection with that IOP */ 859 send_to_fd(u->fd, strlen(p->IOPname)+1, (unsigned char *)p->IOPname); /* send the IOP's interface name, and a terminating null */ 860 if (get_error_response(u->fd, errbuf)) { 861 return -1; 862 } 863 return u->fd; /* and return that open descriptor */ 864 } 865 p = p->next; 866 } 867 } 868 } 869 } 870 return -1; /* if the interface wasn't found, return an error */ 871} 872 873static void acn_start_monitor(int fd, int snaplen, int timeout, int promiscuous, int direction) { 874 unsigned char buf[8]; 875 unit_t *u; 876 877 //printf("acn_start_monitor()\n"); // fulko 878 find_unit_by_fd(fd, NULL, NULL, &u); 879 if (u->first_time == 0) { 880 buf[0] = 'M'; 881 *(uint32_t *)&buf[1] = htonl(snaplen); 882 buf[5] = timeout; 883 buf[6] = promiscuous; 884 buf[7] = direction; 885 //printf("acn_start_monitor() first time\n"); // fulko 886 send_to_fd(fd, 8, buf); /* send the start monitor command with its parameters to the IOP */ 887 u->first_time = 1; 888 } 889 //printf("acn_start_monitor() complete\n"); // fulko 890} 891 892static int pcap_inject_acn(pcap_t *p, const void *buf _U_, int size _U_) { 893 pcap_strlcpy(p->errbuf, "Sending packets isn't supported on ACN adapters", 894 PCAP_ERRBUF_SIZE); 895 return (-1); 896} 897 898static int pcap_setfilter_acn(pcap_t *handle, struct bpf_program *bpf) { 899 int fd = handle->fd; 900 int count; 901 struct bpf_insn *p; 902 uint16_t shortInt; 903 uint32_t longInt; 904 905 send_to_fd(fd, 1, (unsigned char *)"F"); /* BPF filter follows command */ 906 count = bpf->bf_len; 907 longInt = htonl(count); 908 send_to_fd(fd, 4, (unsigned char *)&longInt); /* send the instruction sequence count */ 909 p = bpf->bf_insns; 910 while (count--) { /* followed by the list of instructions */ 911 shortInt = htons(p->code); 912 longInt = htonl(p->k); 913 send_to_fd(fd, 2, (unsigned char *)&shortInt); 914 send_to_fd(fd, 1, (unsigned char *)&p->jt); 915 send_to_fd(fd, 1, (unsigned char *)&p->jf); 916 send_to_fd(fd, 4, (unsigned char *)&longInt); 917 p++; 918 } 919 if (get_error_response(fd, NULL)) 920 return -1; 921 return 0; 922} 923 924static int acn_read_n_bytes_with_timeout(pcap_t *handle, int count) { 925 struct timeval tv; 926 int retval, fd; 927 fd_set r_fds; 928 fd_set w_fds; 929 u_char *bp; 930 int len = 0; 931 int offset = 0; 932 933 tv.tv_sec = 5; 934 tv.tv_usec = 0; 935 936 fd = handle->fd; 937 FD_ZERO(&r_fds); 938 FD_SET(fd, &r_fds); 939 memcpy(&w_fds, &r_fds, sizeof(r_fds)); 940 bp = handle->bp; 941 while (count) { 942 retval = select(fd + 1, &w_fds, NULL, NULL, &tv); 943 if (retval == -1) { /* an error occurred !!!!! */ 944// fprintf(stderr, "error during packet data read\n"); 945 return -1; /* but we need to return a good indication to prevent unnecessary popups */ 946 } else if (retval == 0) { /* timeout occurred, so process what we've got sofar and return */ 947// fprintf(stderr, "timeout during packet data read\n"); 948 return -1; 949 } else { 950 if ((len = recv(fd, (bp + offset), count, 0)) <= 0) { 951// fprintf(stderr, "premature exit during packet data rx\n"); 952 return -1; 953 } 954 count -= len; 955 offset += len; 956 } 957 } 958 return 0; 959} 960 961static int pcap_read_acn(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) { 962 #define HEADER_SIZE (4 * 4) 963 unsigned char packet_header[HEADER_SIZE]; 964 struct pcap_pkthdr pcap_header; 965 966 //printf("pcap_read_acn()\n"); // fulko 967 acn_start_monitor(handle->fd, handle->snapshot, handle->opt.timeout, handle->opt.promisc, handle->direction); /* maybe tell him to start monitoring */ 968 //printf("pcap_read_acn() after start monitor\n"); // fulko 969 970 handle->bp = packet_header; 971 if (acn_read_n_bytes_with_timeout(handle, HEADER_SIZE) == -1) return 0; /* try to read a packet header in so we can get the sizeof the packet data */ 972 973 pcap_header.ts.tv_sec = ntohl(*(uint32_t *)&packet_header[0]); /* tv_sec */ 974 pcap_header.ts.tv_usec = ntohl(*(uint32_t *)&packet_header[4]); /* tv_usec */ 975 pcap_header.caplen = ntohl(*(uint32_t *)&packet_header[8]); /* caplen */ 976 pcap_header.len = ntohl(*(uint32_t *)&packet_header[12]); /* len */ 977 978 handle->bp = (u_char *)handle->buffer + handle->offset; /* start off the receive pointer at the right spot */ 979 if (acn_read_n_bytes_with_timeout(handle, pcap_header.caplen) == -1) return 0; /* then try to read in the rest of the data */ 980 981 callback(user, &pcap_header, handle->bp); /* call the user supplied callback function */ 982 return 1; 983} 984 985static int pcap_activate_sita(pcap_t *handle) { 986 int fd; 987 988 if (handle->opt.rfmon) { 989 /* 990 * No monitor mode on SITA devices (they're not Wi-Fi 991 * devices). 992 */ 993 return PCAP_ERROR_RFMON_NOTSUP; 994 } 995 996 /* Initialize some components of the pcap structure. */ 997 998 handle->inject_op = pcap_inject_acn; 999 handle->setfilter_op = pcap_setfilter_acn; 1000 handle->setdirection_op = NULL; /* Not implemented */ 1001 handle->set_datalink_op = NULL; /* can't change data link type */ 1002 handle->getnonblock_op = pcap_getnonblock_fd; 1003 handle->setnonblock_op = pcap_setnonblock_fd; 1004 handle->cleanup_op = pcap_cleanup_acn; 1005 handle->read_op = pcap_read_acn; 1006 handle->stats_op = pcap_stats_acn; 1007 1008 fd = acn_open_live(handle->opt.device, handle->errbuf, 1009 &handle->linktype); 1010 if (fd == -1) 1011 return PCAP_ERROR; 1012 1013 /* 1014 * Turn a negative snapshot value (invalid), a snapshot value of 1015 * 0 (unspecified), or a value bigger than the normal maximum 1016 * value, into the maximum allowed value. 1017 * 1018 * If some application really *needs* a bigger snapshot 1019 * length, we should just increase MAXIMUM_SNAPLEN. 1020 */ 1021 if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN) 1022 handle->snapshot = MAXIMUM_SNAPLEN; 1023 1024 handle->fd = fd; 1025 handle->bufsize = handle->snapshot; 1026 1027 /* Allocate the buffer */ 1028 1029 handle->buffer = malloc(handle->bufsize + handle->offset); 1030 if (!handle->buffer) { 1031 pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, 1032 errno, "malloc"); 1033 pcap_cleanup_acn(handle); 1034 return PCAP_ERROR; 1035 } 1036 1037 /* 1038 * "handle->fd" is a socket, so "select()" and "poll()" 1039 * should work on it. 1040 */ 1041 handle->selectable_fd = handle->fd; 1042 1043 return 0; 1044} 1045 1046pcap_t *pcap_create_interface(const char *device _U_, char *ebuf) { 1047 pcap_t *p; 1048 1049 p = PCAP_CREATE_COMMON(ebuf, struct pcap_sita); 1050 if (p == NULL) 1051 return (NULL); 1052 1053 p->activate_op = pcap_activate_sita; 1054 return (p); 1055} 1056 1057int pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { 1058 1059 //printf("pcap_findalldevs()\n"); // fulko 1060 1061 *alldevsp = 0; /* initialize the returned variables before we do anything */ 1062 strcpy(errbuf, ""); 1063 if (acn_parse_hosts_file(errbuf)) /* scan the hosts file for potential IOPs */ 1064 { 1065 //printf("pcap_findalldevs() returning BAD after parsehosts\n"); // fulko 1066 return -1; 1067 } 1068 //printf("pcap_findalldevs() got hostlist now finding devs\n"); // fulko 1069 if (acn_findalldevs(errbuf)) /* then ask the IOPs for their monitorable devices */ 1070 { 1071 //printf("pcap_findalldevs() returning BAD after findalldevs\n"); // fulko 1072 return -1; 1073 } 1074 devlistp->beginning = acn_if_list; 1075 acn_if_list = 0; /* then forget our list head, because someone will call pcap_freealldevs() to empty the malloc'ed stuff */ 1076 //printf("pcap_findalldevs() returning ZERO OK\n"); // fulko 1077 return 0; 1078} 1079 1080/* 1081 * Libpcap version string. 1082 */ 1083const char * 1084pcap_lib_version(void) 1085{ 1086 return PCAP_VERSION_STRING " (SITA-only)"; 1087} 1088