1 /* $NetBSD: pcap.c,v 1.7 2017/01/24 22:29:28 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the Computer Systems 18 * Engineering Group at Lawrence Berkeley Laboratory. 19 * 4. Neither the name of the University nor of the Laboratory may be used 20 * to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 __RCSID("$NetBSD: pcap.c,v 1.7 2017/01/24 22:29:28 christos Exp $"); 38 39 #ifdef HAVE_CONFIG_H 40 #include "config.h" 41 #endif 42 43 #ifdef _WIN32 44 #include <pcap-stdinc.h> 45 #else /* _WIN32 */ 46 #if HAVE_INTTYPES_H 47 #include <inttypes.h> 48 #elif HAVE_STDINT_H 49 #include <stdint.h> 50 #endif 51 #ifdef HAVE_SYS_BITYPES_H 52 #include <sys/bitypes.h> 53 #endif 54 #include <sys/types.h> 55 #endif /* _WIN32 */ 56 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__MINGW32__) 61 #include <unistd.h> 62 #endif 63 #include <fcntl.h> 64 #include <errno.h> 65 66 #ifdef HAVE_OS_PROTO_H 67 #include "os-proto.h" 68 #endif 69 70 #ifdef MSDOS 71 #include "pcap-dos.h" 72 #endif 73 74 #include "pcap-int.h" 75 76 #ifdef HAVE_DAG_API 77 #include "pcap-dag.h" 78 #endif /* HAVE_DAG_API */ 79 80 #ifdef HAVE_SEPTEL_API 81 #include "pcap-septel.h" 82 #endif /* HAVE_SEPTEL_API */ 83 84 #ifdef HAVE_SNF_API 85 #include "pcap-snf.h" 86 #endif /* HAVE_SNF_API */ 87 88 #ifdef HAVE_TC_API 89 #include "pcap-tc.h" 90 #endif /* HAVE_TC_API */ 91 92 #ifdef PCAP_SUPPORT_USB 93 #include "pcap-usb-linux.h" 94 #endif 95 96 #ifdef PCAP_SUPPORT_BT 97 #include "pcap-bt-linux.h" 98 #endif 99 100 #ifdef PCAP_SUPPORT_BT_MONITOR 101 #include "pcap-bt-monitor-linux.h" 102 #endif 103 104 #ifdef PCAP_SUPPORT_NETFILTER 105 #include "pcap-netfilter-linux.h" 106 #endif 107 108 #ifdef PCAP_SUPPORT_DBUS 109 #include "pcap-dbus.h" 110 #endif 111 112 static int 113 pcap_not_initialized(pcap_t *pcap) 114 { 115 /* in case the caller doesn't check for PCAP_ERROR_NOT_ACTIVATED */ 116 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf), 117 "This handle hasn't been activated yet"); 118 /* this means 'not initialized' */ 119 return (PCAP_ERROR_NOT_ACTIVATED); 120 } 121 122 #ifdef _WIN32 123 static void * 124 pcap_not_initialized_ptr(pcap_t *pcap) 125 { 126 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf), 127 "This handle hasn't been activated yet"); 128 return (NULL); 129 } 130 131 static HANDLE 132 pcap_getevent_not_initialized(pcap_t *pcap) 133 { 134 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf), 135 "This handle hasn't been activated yet"); 136 return (INVALID_HANDLE_VALUE); 137 } 138 139 static u_int 140 pcap_sendqueue_transmit_not_initialized(pcap_t *pcap, pcap_send_queue* queue, int sync) 141 { 142 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf), 143 "This handle hasn't been activated yet"); 144 return (0); 145 } 146 147 static PAirpcapHandle 148 pcap_get_airpcap_handle_not_initialized(pcap_t *pcap) 149 { 150 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf), 151 "This handle hasn't been activated yet"); 152 return (NULL); 153 } 154 #endif 155 156 /* 157 * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't, 158 * a PCAP_ERROR value on an error. 159 */ 160 int 161 pcap_can_set_rfmon(pcap_t *p) 162 { 163 return (p->can_set_rfmon_op(p)); 164 } 165 166 /* 167 * For systems where rfmon mode is never supported. 168 */ 169 static int 170 pcap_cant_set_rfmon(pcap_t *p _U_) 171 { 172 return (0); 173 } 174 175 /* 176 * Sets *tstamp_typesp to point to an array 1 or more supported time stamp 177 * types; the return value is the number of supported time stamp types. 178 * The list should be freed by a call to pcap_free_tstamp_types() when 179 * you're done with it. 180 * 181 * A return value of 0 means "you don't get a choice of time stamp type", 182 * in which case *tstamp_typesp is set to null. 183 * 184 * PCAP_ERROR is returned on error. 185 */ 186 int 187 pcap_list_tstamp_types(pcap_t *p, int **tstamp_typesp) 188 { 189 if (p->tstamp_type_count == 0) { 190 /* 191 * We don't support multiple time stamp types. 192 */ 193 *tstamp_typesp = NULL; 194 } else { 195 *tstamp_typesp = (int*)calloc(sizeof(**tstamp_typesp), 196 p->tstamp_type_count); 197 if (*tstamp_typesp == NULL) { 198 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), 199 "malloc: %s", pcap_strerror(errno)); 200 return (PCAP_ERROR); 201 } 202 (void)memcpy(*tstamp_typesp, p->tstamp_type_list, 203 sizeof(**tstamp_typesp) * p->tstamp_type_count); 204 } 205 return (p->tstamp_type_count); 206 } 207 208 /* 209 * In Windows, you might have a library built with one version of the 210 * C runtime library and an application built with another version of 211 * the C runtime library, which means that the library might use one 212 * version of malloc() and free() and the application might use another 213 * version of malloc() and free(). If so, that means something 214 * allocated by the library cannot be freed by the application, so we 215 * need to have a pcap_free_tstamp_types() routine to free up the list 216 * allocated by pcap_list_tstamp_types(), even though it's just a wrapper 217 * around free(). 218 */ 219 void 220 pcap_free_tstamp_types(int *tstamp_type_list) 221 { 222 free(tstamp_type_list); 223 } 224 225 /* 226 * Default one-shot callback; overridden for capture types where the 227 * packet data cannot be guaranteed to be available after the callback 228 * returns, so that a copy must be made. 229 */ 230 void 231 pcap_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *pkt) 232 { 233 struct oneshot_userdata *sp = (struct oneshot_userdata *)user; 234 235 *sp->hdr = *h; 236 *sp->pkt = pkt; 237 } 238 239 const u_char * 240 pcap_next(pcap_t *p, struct pcap_pkthdr *h) 241 { 242 struct oneshot_userdata s; 243 const u_char *pkt; 244 245 s.hdr = h; 246 s.pkt = &pkt; 247 s.pd = p; 248 if (pcap_dispatch(p, 1, p->oneshot_callback, (u_char *)&s) <= 0) 249 return (0); 250 return (pkt); 251 } 252 253 int 254 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, 255 const u_char **pkt_data) 256 { 257 struct oneshot_userdata s; 258 259 s.hdr = &p->pcap_header; 260 s.pkt = pkt_data; 261 s.pd = p; 262 263 /* Saves a pointer to the packet headers */ 264 *pkt_header= &p->pcap_header; 265 266 if (p->rfile != NULL) { 267 int status; 268 269 /* We are on an offline capture */ 270 status = pcap_offline_read(p, 1, p->oneshot_callback, 271 (u_char *)&s); 272 273 /* 274 * Return codes for pcap_offline_read() are: 275 * - 0: EOF 276 * - -1: error 277 * - >1: OK 278 * The first one ('0') conflicts with the return code of 279 * 0 from pcap_read() meaning "no packets arrived before 280 * the timeout expired", so we map it to -2 so you can 281 * distinguish between an EOF from a savefile and a 282 * "no packets arrived before the timeout expired, try 283 * again" from a live capture. 284 */ 285 if (status == 0) 286 return (-2); 287 else 288 return (status); 289 } 290 291 /* 292 * Return codes for pcap_read() are: 293 * - 0: timeout 294 * - -1: error 295 * - -2: loop was broken out of with pcap_breakloop() 296 * - >1: OK 297 * The first one ('0') conflicts with the return code of 0 from 298 * pcap_offline_read() meaning "end of file". 299 */ 300 return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s)); 301 } 302 303 static struct capture_source_type { 304 int (*findalldevs_op)(pcap_if_t **, char *); 305 pcap_t *(*create_op)(const char *, char *, int *); 306 } capture_source_types[] = { 307 #ifdef HAVE_DAG_API 308 { dag_findalldevs, dag_create }, 309 #endif 310 #ifdef HAVE_SEPTEL_API 311 { septel_findalldevs, septel_create }, 312 #endif 313 #ifdef HAVE_SNF_API 314 { snf_findalldevs, snf_create }, 315 #endif 316 #ifdef HAVE_TC_API 317 { TcFindAllDevs, TcCreate }, 318 #endif 319 #ifdef PCAP_SUPPORT_BT 320 { bt_findalldevs, bt_create }, 321 #endif 322 #ifdef PCAP_SUPPORT_BT_MONITOR 323 { bt_monitor_findalldevs, bt_monitor_create }, 324 #endif 325 #ifdef PCAP_SUPPORT_USB 326 { usb_findalldevs, usb_create }, 327 #endif 328 #ifdef PCAP_SUPPORT_NETFILTER 329 { netfilter_findalldevs, netfilter_create }, 330 #endif 331 #ifdef PCAP_SUPPORT_DBUS 332 { dbus_findalldevs, dbus_create }, 333 #endif 334 { NULL, NULL } 335 }; 336 337 /* 338 * Get a list of all capture sources that are up and that we can open. 339 * Returns -1 on error, 0 otherwise. 340 * The list, as returned through "alldevsp", may be null if no interfaces 341 * were up and could be opened. 342 */ 343 int 344 pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf) 345 { 346 size_t i; 347 348 /* 349 * Find all the local network interfaces on which we 350 * can capture. 351 */ 352 if (pcap_platform_finddevs(alldevsp, errbuf) == -1) 353 return (-1); 354 355 /* 356 * Ask each of the non-local-network-interface capture 357 * source types what interfaces they have. 358 */ 359 for (i = 0; capture_source_types[i].findalldevs_op != NULL; i++) { 360 if (capture_source_types[i].findalldevs_op(alldevsp, errbuf) == -1) { 361 /* 362 * We had an error; free the list we've been 363 * constructing. 364 */ 365 if (*alldevsp != NULL) { 366 pcap_freealldevs(*alldevsp); 367 *alldevsp = NULL; 368 } 369 return (-1); 370 } 371 } 372 373 return (0); 374 } 375 376 pcap_t * 377 pcap_create(const char *device, char *errbuf) 378 { 379 size_t i; 380 int is_theirs; 381 pcap_t *p; 382 char *device_str; 383 384 /* 385 * A null device name is equivalent to the "any" device - 386 * which might not be supported on this platform, but 387 * this means that you'll get a "not supported" error 388 * rather than, say, a crash when we try to dereference 389 * the null pointer. 390 */ 391 if (device == NULL) 392 device_str = strdup("any"); 393 else { 394 #ifdef _WIN32 395 /* 396 * If the string appears to be little-endian UCS-2/UTF-16, 397 * convert it to ASCII. 398 * 399 * XXX - to UTF-8 instead? Or report an error if any 400 * character isn't ASCII? 401 */ 402 if (device[0] != '\0' && device[1] == '\0') { 403 size_t length; 404 405 length = wcslen((wchar_t *)device); 406 device_str = (char *)malloc(length + 1); 407 if (device_str == NULL) { 408 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, 409 "malloc: %s", pcap_strerror(errno)); 410 return (NULL); 411 } 412 413 pcap_snprintf(device_str, length + 1, "%ws", 414 (const wchar_t *)device); 415 } else 416 #endif 417 device_str = strdup(device); 418 } 419 if (device_str == NULL) { 420 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, 421 "malloc: %s", pcap_strerror(errno)); 422 return (NULL); 423 } 424 425 /* 426 * Try each of the non-local-network-interface capture 427 * source types until we find one that works for this 428 * device or run out of types. 429 */ 430 for (i = 0; capture_source_types[i].create_op != NULL; i++) { 431 is_theirs = 0; 432 p = capture_source_types[i].create_op(device_str, errbuf, 433 &is_theirs); 434 if (is_theirs) { 435 /* 436 * The device name refers to a device of the 437 * type in question; either it succeeded, 438 * in which case p refers to a pcap_t to 439 * later activate for the device, or it 440 * failed, in which case p is null and we 441 * should return that to report the failure 442 * to create. 443 */ 444 if (p == NULL) { 445 /* 446 * We assume the caller filled in errbuf. 447 */ 448 free(device_str); 449 return (NULL); 450 } 451 p->opt.device = device_str; 452 return (p); 453 } 454 } 455 456 /* 457 * OK, try it as a regular network interface. 458 */ 459 p = pcap_create_interface(device_str, errbuf); 460 if (p == NULL) { 461 /* 462 * We assume the caller filled in errbuf. 463 */ 464 free(device_str); 465 return (NULL); 466 } 467 p->opt.device = device_str; 468 return (p); 469 } 470 471 static void 472 initialize_ops(pcap_t *p) 473 { 474 /* 475 * Set operation pointers for operations that only work on 476 * an activated pcap_t to point to a routine that returns 477 * a "this isn't activated" error. 478 */ 479 p->read_op = (read_op_t)pcap_not_initialized; 480 p->inject_op = (inject_op_t)pcap_not_initialized; 481 p->setfilter_op = (setfilter_op_t)pcap_not_initialized; 482 p->setdirection_op = (setdirection_op_t)pcap_not_initialized; 483 p->set_datalink_op = (set_datalink_op_t)pcap_not_initialized; 484 p->getnonblock_op = (getnonblock_op_t)pcap_not_initialized; 485 p->setnonblock_op = (setnonblock_op_t)pcap_not_initialized; 486 p->stats_op = (stats_op_t)pcap_not_initialized; 487 #ifdef _WIN32 488 p->stats_ex_op = (stats_ex_op_t)pcap_not_initialized_ptr; 489 p->setbuff_op = (setbuff_op_t)pcap_not_initialized; 490 p->setmode_op = (setmode_op_t)pcap_not_initialized; 491 p->setmintocopy_op = (setmintocopy_op_t)pcap_not_initialized; 492 p->getevent_op = pcap_getevent_not_initialized; 493 p->oid_get_request_op = (oid_get_request_op_t)pcap_not_initialized; 494 p->oid_set_request_op = (oid_set_request_op_t)pcap_not_initialized; 495 p->sendqueue_transmit_op = pcap_sendqueue_transmit_not_initialized; 496 p->setuserbuffer_op = (setuserbuffer_op_t)pcap_not_initialized; 497 p->live_dump_op = (live_dump_op_t)pcap_not_initialized; 498 p->live_dump_ended_op = (live_dump_ended_op_t)pcap_not_initialized; 499 p->get_airpcap_handle_op = pcap_get_airpcap_handle_not_initialized; 500 #endif 501 502 /* 503 * Default cleanup operation - implementations can override 504 * this, but should call pcap_cleanup_live_common() after 505 * doing their own additional cleanup. 506 */ 507 p->cleanup_op = pcap_cleanup_live_common; 508 509 /* 510 * In most cases, the standard one-shot callback can 511 * be used for pcap_next()/pcap_next_ex(). 512 */ 513 p->oneshot_callback = pcap_oneshot; 514 } 515 516 static pcap_t * 517 pcap_alloc_pcap_t(char *ebuf, size_t size) 518 { 519 char *chunk; 520 pcap_t *p; 521 522 /* 523 * Allocate a chunk of memory big enough for a pcap_t 524 * plus a structure following it of size "size". The 525 * structure following it is a private data structure 526 * for the routines that handle this pcap_t. 527 */ 528 chunk = malloc(sizeof (pcap_t) + size); 529 if (chunk == NULL) { 530 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", 531 pcap_strerror(errno)); 532 return (NULL); 533 } 534 memset(chunk, 0, sizeof (pcap_t) + size); 535 536 /* 537 * Get a pointer to the pcap_t at the beginning. 538 */ 539 p = (pcap_t *)chunk; 540 541 #ifndef _WIN32 542 p->fd = -1; /* not opened yet */ 543 p->selectable_fd = -1; 544 #endif 545 546 if (size == 0) { 547 /* No private data was requested. */ 548 p->priv = NULL; 549 } else { 550 /* 551 * Set the pointer to the private data; that's the structure 552 * of size "size" following the pcap_t. 553 */ 554 p->priv = (void *)(chunk + sizeof (pcap_t)); 555 } 556 557 return (p); 558 } 559 560 pcap_t * 561 pcap_create_common(char *ebuf, size_t size) 562 { 563 pcap_t *p; 564 565 p = pcap_alloc_pcap_t(ebuf, size); 566 if (p == NULL) 567 return (NULL); 568 569 /* 570 * Default to "can't set rfmon mode"; if it's supported by 571 * a platform, the create routine that called us can set 572 * the op to its routine to check whether a particular 573 * device supports it. 574 */ 575 p->can_set_rfmon_op = pcap_cant_set_rfmon; 576 577 initialize_ops(p); 578 579 /* put in some defaults*/ 580 p->snapshot = MAXIMUM_SNAPLEN; /* max packet size */ 581 p->opt.timeout = 0; /* no timeout specified */ 582 p->opt.buffer_size = 0; /* use the platform's default */ 583 p->opt.promisc = 0; 584 p->opt.rfmon = 0; 585 p->opt.immediate = 0; 586 p->opt.tstamp_type = -1; /* default to not setting time stamp type */ 587 p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO; 588 589 /* 590 * Start out with no BPF code generation flags set. 591 */ 592 p->bpf_codegen_flags = 0; 593 594 return (p); 595 } 596 597 int 598 pcap_check_activated(pcap_t *p) 599 { 600 if (p->activated) { 601 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform " 602 " operation on activated capture"); 603 return (-1); 604 } 605 return (0); 606 } 607 608 int 609 pcap_set_snaplen(pcap_t *p, int snaplen) 610 { 611 if (pcap_check_activated(p)) 612 return (PCAP_ERROR_ACTIVATED); 613 614 /* 615 * Turn invalid values, or excessively large values, into 616 * the maximum allowed value. 617 * 618 * If some application really *needs* a bigger snapshot 619 * length, we should just increase MAXIMUM_SNAPLEN. 620 */ 621 if (snaplen <= 0 || snaplen > MAXIMUM_SNAPLEN) 622 snaplen = MAXIMUM_SNAPLEN; 623 p->snapshot = snaplen; 624 return (0); 625 } 626 627 int 628 pcap_set_promisc(pcap_t *p, int promisc) 629 { 630 if (pcap_check_activated(p)) 631 return (PCAP_ERROR_ACTIVATED); 632 p->opt.promisc = promisc; 633 return (0); 634 } 635 636 int 637 pcap_set_rfmon(pcap_t *p, int rfmon) 638 { 639 if (pcap_check_activated(p)) 640 return (PCAP_ERROR_ACTIVATED); 641 p->opt.rfmon = rfmon; 642 return (0); 643 } 644 645 int 646 pcap_set_timeout(pcap_t *p, int timeout_ms) 647 { 648 if (pcap_check_activated(p)) 649 return (PCAP_ERROR_ACTIVATED); 650 p->opt.timeout = timeout_ms; 651 return (0); 652 } 653 654 int 655 pcap_set_tstamp_type(pcap_t *p, int tstamp_type) 656 { 657 int i; 658 659 if (pcap_check_activated(p)) 660 return (PCAP_ERROR_ACTIVATED); 661 662 /* 663 * The argument should have been u_int, but that's too late 664 * to change now - it's an API. 665 */ 666 if (tstamp_type < 0) 667 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP); 668 669 /* 670 * If p->tstamp_type_count is 0, we only support PCAP_TSTAMP_HOST; 671 * the default time stamp type is PCAP_TSTAMP_HOST. 672 */ 673 if (p->tstamp_type_count == 0) { 674 if (tstamp_type == PCAP_TSTAMP_HOST) { 675 p->opt.tstamp_type = tstamp_type; 676 return (0); 677 } 678 } else { 679 /* 680 * Check whether we claim to support this type of time stamp. 681 */ 682 for (i = 0; i < p->tstamp_type_count; i++) { 683 if (p->tstamp_type_list[i] == (u_int)tstamp_type) { 684 /* 685 * Yes. 686 */ 687 p->opt.tstamp_type = tstamp_type; 688 return (0); 689 } 690 } 691 } 692 693 /* 694 * We don't support this type of time stamp. 695 */ 696 return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP); 697 } 698 699 int 700 pcap_set_immediate_mode(pcap_t *p, int immediate) 701 { 702 if (pcap_check_activated(p)) 703 return (PCAP_ERROR_ACTIVATED); 704 p->opt.immediate = immediate; 705 return (0); 706 } 707 708 int 709 pcap_set_buffer_size(pcap_t *p, int buffer_size) 710 { 711 if (pcap_check_activated(p)) 712 return (PCAP_ERROR_ACTIVATED); 713 if (buffer_size <= 0) { 714 /* 715 * Silently ignore invalid values. 716 */ 717 return (0); 718 } 719 p->opt.buffer_size = buffer_size; 720 return (0); 721 } 722 723 int 724 pcap_set_tstamp_precision(pcap_t *p, int tstamp_precision) 725 { 726 int i; 727 728 if (pcap_check_activated(p)) 729 return (PCAP_ERROR_ACTIVATED); 730 731 /* 732 * The argument should have been u_int, but that's too late 733 * to change now - it's an API. 734 */ 735 if (tstamp_precision < 0) 736 return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP); 737 738 /* 739 * If p->tstamp_precision_count is 0, we only support setting 740 * the time stamp precision to microsecond precision; every 741 * pcap module *MUST* support microsecond precision, even if 742 * it does so by converting the native precision to 743 * microseconds. 744 */ 745 if (p->tstamp_precision_count == 0) { 746 if (tstamp_precision == PCAP_TSTAMP_PRECISION_MICRO) { 747 p->opt.tstamp_precision = tstamp_precision; 748 return (0); 749 } 750 } else { 751 /* 752 * Check whether we claim to support this precision of 753 * time stamp. 754 */ 755 for (i = 0; i < p->tstamp_precision_count; i++) { 756 if (p->tstamp_precision_list[i] == (u_int)tstamp_precision) { 757 /* 758 * Yes. 759 */ 760 p->opt.tstamp_precision = tstamp_precision; 761 return (0); 762 } 763 } 764 } 765 766 /* 767 * We don't support this time stamp precision. 768 */ 769 return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP); 770 } 771 772 int 773 pcap_get_tstamp_precision(pcap_t *p) 774 { 775 return (p->opt.tstamp_precision); 776 } 777 778 int 779 pcap_activate(pcap_t *p) 780 { 781 int status; 782 783 /* 784 * Catch attempts to re-activate an already-activated 785 * pcap_t; this should, for example, catch code that 786 * calls pcap_open_live() followed by pcap_activate(), 787 * as some code that showed up in a Stack Exchange 788 * question did. 789 */ 790 if (pcap_check_activated(p)) 791 return (PCAP_ERROR_ACTIVATED); 792 status = p->activate_op(p); 793 if (status >= 0) 794 p->activated = 1; 795 else { 796 if (p->errbuf[0] == '\0') { 797 /* 798 * No error message supplied by the activate routine; 799 * for the benefit of programs that don't specially 800 * handle errors other than PCAP_ERROR, return the 801 * error message corresponding to the status. 802 */ 803 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s", 804 pcap_statustostr(status)); 805 } 806 807 /* 808 * Undo any operation pointer setting, etc. done by 809 * the activate operation. 810 */ 811 initialize_ops(p); 812 } 813 return (status); 814 } 815 816 pcap_t * 817 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *errbuf) 818 { 819 pcap_t *p; 820 int status; 821 822 p = pcap_create(device, errbuf); 823 if (p == NULL) 824 return (NULL); 825 status = pcap_set_snaplen(p, snaplen); 826 if (status < 0) 827 goto fail; 828 status = pcap_set_promisc(p, promisc); 829 if (status < 0) 830 goto fail; 831 status = pcap_set_timeout(p, to_ms); 832 if (status < 0) 833 goto fail; 834 /* 835 * Mark this as opened with pcap_open_live(), so that, for 836 * example, we show the full list of DLT_ values, rather 837 * than just the ones that are compatible with capturing 838 * when not in monitor mode. That allows existing applications 839 * to work the way they used to work, but allows new applications 840 * that know about the new open API to, for example, find out the 841 * DLT_ values that they can select without changing whether 842 * the adapter is in monitor mode or not. 843 */ 844 p->oldstyle = 1; 845 status = pcap_activate(p); 846 if (status < 0) 847 goto fail; 848 return (p); 849 fail: 850 if (status == PCAP_ERROR) 851 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device, 852 p->errbuf); 853 else if (status == PCAP_ERROR_NO_SUCH_DEVICE || 854 status == PCAP_ERROR_PERM_DENIED || 855 status == PCAP_ERROR_PROMISC_PERM_DENIED) 856 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)", device, 857 pcap_statustostr(status), p->errbuf); 858 else 859 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device, 860 pcap_statustostr(status)); 861 pcap_close(p); 862 return (NULL); 863 } 864 865 pcap_t * 866 pcap_open_offline_common(char *ebuf, size_t size) 867 { 868 pcap_t *p; 869 870 p = pcap_alloc_pcap_t(ebuf, size); 871 if (p == NULL) 872 return (NULL); 873 874 p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO; 875 876 return (p); 877 } 878 879 int 880 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 881 { 882 return (p->read_op(p, cnt, callback, user)); 883 } 884 885 int 886 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 887 { 888 register int n; 889 890 for (;;) { 891 if (p->rfile != NULL) { 892 /* 893 * 0 means EOF, so don't loop if we get 0. 894 */ 895 n = pcap_offline_read(p, cnt, callback, user); 896 } else { 897 /* 898 * XXX keep reading until we get something 899 * (or an error occurs) 900 */ 901 do { 902 n = p->read_op(p, cnt, callback, user); 903 } while (n == 0); 904 } 905 if (n <= 0) 906 return (n); 907 if (!PACKET_COUNT_IS_UNLIMITED(cnt)) { 908 cnt -= n; 909 if (cnt <= 0) 910 return (0); 911 } 912 } 913 } 914 915 /* 916 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate. 917 */ 918 void 919 pcap_breakloop(pcap_t *p) 920 { 921 p->break_loop = 1; 922 } 923 924 int 925 pcap_datalink(pcap_t *p) 926 { 927 if (!p->activated) 928 return (PCAP_ERROR_NOT_ACTIVATED); 929 return (p->linktype); 930 } 931 932 int 933 pcap_datalink_ext(pcap_t *p) 934 { 935 if (!p->activated) 936 return (PCAP_ERROR_NOT_ACTIVATED); 937 return (p->linktype_ext); 938 } 939 940 int 941 pcap_list_datalinks(pcap_t *p, int **dlt_buffer) 942 { 943 if (!p->activated) 944 return (PCAP_ERROR_NOT_ACTIVATED); 945 if (p->dlt_count == 0) { 946 /* 947 * We couldn't fetch the list of DLTs, which means 948 * this platform doesn't support changing the 949 * DLT for an interface. Return a list of DLTs 950 * containing only the DLT this device supports. 951 */ 952 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer)); 953 if (*dlt_buffer == NULL) { 954 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), 955 "malloc: %s", pcap_strerror(errno)); 956 return (PCAP_ERROR); 957 } 958 **dlt_buffer = p->linktype; 959 return (1); 960 } else { 961 *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count); 962 if (*dlt_buffer == NULL) { 963 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), 964 "malloc: %s", pcap_strerror(errno)); 965 return (PCAP_ERROR); 966 } 967 (void)memcpy(*dlt_buffer, p->dlt_list, 968 sizeof(**dlt_buffer) * p->dlt_count); 969 return (p->dlt_count); 970 } 971 } 972 973 /* 974 * In Windows, you might have a library built with one version of the 975 * C runtime library and an application built with another version of 976 * the C runtime library, which means that the library might use one 977 * version of malloc() and free() and the application might use another 978 * version of malloc() and free(). If so, that means something 979 * allocated by the library cannot be freed by the application, so we 980 * need to have a pcap_free_datalinks() routine to free up the list 981 * allocated by pcap_list_datalinks(), even though it's just a wrapper 982 * around free(). 983 */ 984 void 985 pcap_free_datalinks(int *dlt_list) 986 { 987 free(dlt_list); 988 } 989 990 int 991 pcap_set_datalink(pcap_t *p, int dlt) 992 { 993 int i; 994 const char *dlt_name; 995 996 if (dlt < 0) 997 goto unsupported; 998 999 if (p->dlt_count == 0 || p->set_datalink_op == NULL) { 1000 /* 1001 * We couldn't fetch the list of DLTs, or we don't 1002 * have a "set datalink" operation, which means 1003 * this platform doesn't support changing the 1004 * DLT for an interface. Check whether the new 1005 * DLT is the one this interface supports. 1006 */ 1007 if (p->linktype != dlt) 1008 goto unsupported; 1009 1010 /* 1011 * It is, so there's nothing we need to do here. 1012 */ 1013 return (0); 1014 } 1015 for (i = 0; i < p->dlt_count; i++) 1016 if (p->dlt_list[i] == (u_int)dlt) 1017 break; 1018 if (i >= p->dlt_count) 1019 goto unsupported; 1020 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB && 1021 dlt == DLT_DOCSIS) { 1022 /* 1023 * This is presumably an Ethernet device, as the first 1024 * link-layer type it offers is DLT_EN10MB, and the only 1025 * other type it offers is DLT_DOCSIS. That means that 1026 * we can't tell the driver to supply DOCSIS link-layer 1027 * headers - we're just pretending that's what we're 1028 * getting, as, presumably, we're capturing on a dedicated 1029 * link to a Cisco Cable Modem Termination System, and 1030 * it's putting raw DOCSIS frames on the wire inside low-level 1031 * Ethernet framing. 1032 */ 1033 p->linktype = dlt; 1034 return (0); 1035 } 1036 if (p->set_datalink_op(p, dlt) == -1) 1037 return (-1); 1038 p->linktype = dlt; 1039 return (0); 1040 1041 unsupported: 1042 dlt_name = pcap_datalink_val_to_name(dlt); 1043 if (dlt_name != NULL) { 1044 (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf), 1045 "%s is not one of the DLTs supported by this device", 1046 dlt_name); 1047 } else { 1048 (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf), 1049 "DLT %d is not one of the DLTs supported by this device", 1050 dlt); 1051 } 1052 return (-1); 1053 } 1054 1055 /* 1056 * This array is designed for mapping upper and lower case letter 1057 * together for a case independent comparison. The mappings are 1058 * based upon ascii character sequences. 1059 */ 1060 static const u_char charmap[] = { 1061 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003', 1062 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007', 1063 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013', 1064 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017', 1065 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023', 1066 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027', 1067 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033', 1068 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037', 1069 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043', 1070 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047', 1071 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053', 1072 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057', 1073 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063', 1074 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067', 1075 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073', 1076 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077', 1077 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143', 1078 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 1079 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 1080 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 1081 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 1082 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 1083 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133', 1084 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137', 1085 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143', 1086 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 1087 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 1088 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 1089 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 1090 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 1091 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173', 1092 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177', 1093 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203', 1094 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207', 1095 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213', 1096 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217', 1097 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223', 1098 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227', 1099 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233', 1100 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237', 1101 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243', 1102 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247', 1103 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253', 1104 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257', 1105 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263', 1106 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267', 1107 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273', 1108 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277', 1109 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343', 1110 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 1111 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 1112 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 1113 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 1114 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 1115 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333', 1116 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337', 1117 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343', 1118 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 1119 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 1120 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 1121 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 1122 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 1123 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373', 1124 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377', 1125 }; 1126 1127 int 1128 pcap_strcasecmp(const char *s1, const char *s2) 1129 { 1130 register const u_char *cm = charmap, 1131 *us1 = (const u_char *)s1, 1132 *us2 = (const u_char *)s2; 1133 1134 while (cm[*us1] == cm[*us2++]) 1135 if (*us1++ == '\0') 1136 return(0); 1137 return (cm[*us1] - cm[*--us2]); 1138 } 1139 1140 struct dlt_choice { 1141 const char *name; 1142 const char *description; 1143 int dlt; 1144 }; 1145 1146 #define DLT_CHOICE(code, description) { #code, description, DLT_ ## code } 1147 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 } 1148 1149 static struct dlt_choice dlt_choices[] = { 1150 DLT_CHOICE(NULL, "BSD loopback"), 1151 DLT_CHOICE(EN10MB, "Ethernet"), 1152 DLT_CHOICE(IEEE802, "Token ring"), 1153 DLT_CHOICE(ARCNET, "BSD ARCNET"), 1154 DLT_CHOICE(SLIP, "SLIP"), 1155 DLT_CHOICE(PPP, "PPP"), 1156 DLT_CHOICE(FDDI, "FDDI"), 1157 DLT_CHOICE(ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"), 1158 DLT_CHOICE(RAW, "Raw IP"), 1159 DLT_CHOICE(SLIP_BSDOS, "BSD/OS SLIP"), 1160 DLT_CHOICE(PPP_BSDOS, "BSD/OS PPP"), 1161 DLT_CHOICE(ATM_CLIP, "Linux Classical IP-over-ATM"), 1162 DLT_CHOICE(PPP_SERIAL, "PPP over serial"), 1163 DLT_CHOICE(PPP_ETHER, "PPPoE"), 1164 DLT_CHOICE(SYMANTEC_FIREWALL, "Symantec Firewall"), 1165 DLT_CHOICE(C_HDLC, "Cisco HDLC"), 1166 DLT_CHOICE(IEEE802_11, "802.11"), 1167 DLT_CHOICE(FRELAY, "Frame Relay"), 1168 DLT_CHOICE(LOOP, "OpenBSD loopback"), 1169 DLT_CHOICE(ENC, "OpenBSD encapsulated IP"), 1170 DLT_CHOICE(LINUX_SLL, "Linux cooked"), 1171 DLT_CHOICE(LTALK, "Localtalk"), 1172 DLT_CHOICE(PFLOG, "OpenBSD pflog file"), 1173 DLT_CHOICE(PFSYNC, "Packet filter state syncing"), 1174 DLT_CHOICE(PRISM_HEADER, "802.11 plus Prism header"), 1175 DLT_CHOICE(IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"), 1176 DLT_CHOICE(SUNATM, "Sun raw ATM"), 1177 DLT_CHOICE(IEEE802_11_RADIO, "802.11 plus radiotap header"), 1178 DLT_CHOICE(ARCNET_LINUX, "Linux ARCNET"), 1179 DLT_CHOICE(JUNIPER_MLPPP, "Juniper Multi-Link PPP"), 1180 DLT_CHOICE(JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"), 1181 DLT_CHOICE(JUNIPER_ES, "Juniper Encryption Services PIC"), 1182 DLT_CHOICE(JUNIPER_GGSN, "Juniper GGSN PIC"), 1183 DLT_CHOICE(JUNIPER_MFR, "Juniper FRF.16 Frame Relay"), 1184 DLT_CHOICE(JUNIPER_ATM2, "Juniper ATM2 PIC"), 1185 DLT_CHOICE(JUNIPER_SERVICES, "Juniper Advanced Services PIC"), 1186 DLT_CHOICE(JUNIPER_ATM1, "Juniper ATM1 PIC"), 1187 DLT_CHOICE(APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"), 1188 DLT_CHOICE(MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"), 1189 DLT_CHOICE(MTP2, "SS7 MTP2"), 1190 DLT_CHOICE(MTP3, "SS7 MTP3"), 1191 DLT_CHOICE(SCCP, "SS7 SCCP"), 1192 DLT_CHOICE(DOCSIS, "DOCSIS"), 1193 DLT_CHOICE(LINUX_IRDA, "Linux IrDA"), 1194 DLT_CHOICE(IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"), 1195 DLT_CHOICE(JUNIPER_MONITOR, "Juniper Passive Monitor PIC"), 1196 DLT_CHOICE(BACNET_MS_TP, "BACnet MS/TP"), 1197 DLT_CHOICE(PPP_PPPD, "PPP for pppd, with direction flag"), 1198 DLT_CHOICE(JUNIPER_PPPOE, "Juniper PPPoE"), 1199 DLT_CHOICE(JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"), 1200 DLT_CHOICE(GPRS_LLC, "GPRS LLC"), 1201 DLT_CHOICE(GPF_T, "GPF-T"), 1202 DLT_CHOICE(GPF_F, "GPF-F"), 1203 DLT_CHOICE(JUNIPER_PIC_PEER, "Juniper PIC Peer"), 1204 DLT_CHOICE(ERF_ETH, "Ethernet with Endace ERF header"), 1205 DLT_CHOICE(ERF_POS, "Packet-over-SONET with Endace ERF header"), 1206 DLT_CHOICE(LINUX_LAPD, "Linux vISDN LAPD"), 1207 DLT_CHOICE(JUNIPER_ETHER, "Juniper Ethernet"), 1208 DLT_CHOICE(JUNIPER_PPP, "Juniper PPP"), 1209 DLT_CHOICE(JUNIPER_FRELAY, "Juniper Frame Relay"), 1210 DLT_CHOICE(JUNIPER_CHDLC, "Juniper C-HDLC"), 1211 DLT_CHOICE(MFR, "FRF.16 Frame Relay"), 1212 DLT_CHOICE(JUNIPER_VP, "Juniper Voice PIC"), 1213 DLT_CHOICE(A429, "Arinc 429"), 1214 DLT_CHOICE(A653_ICM, "Arinc 653 Interpartition Communication"), 1215 DLT_CHOICE(USB_FREEBSD, "USB with FreeBSD header"), 1216 DLT_CHOICE(BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"), 1217 DLT_CHOICE(IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"), 1218 DLT_CHOICE(USB_LINUX, "USB with Linux header"), 1219 DLT_CHOICE(CAN20B, "Controller Area Network (CAN) v. 2.0B"), 1220 DLT_CHOICE(IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"), 1221 DLT_CHOICE(PPI, "Per-Packet Information"), 1222 DLT_CHOICE(IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"), 1223 DLT_CHOICE(JUNIPER_ISM, "Juniper Integrated Service Module"), 1224 DLT_CHOICE(IEEE802_15_4, "IEEE 802.15.4 with FCS"), 1225 DLT_CHOICE(SITA, "SITA pseudo-header"), 1226 DLT_CHOICE(ERF, "Endace ERF header"), 1227 DLT_CHOICE(RAIF1, "Ethernet with u10 Networks pseudo-header"), 1228 DLT_CHOICE(IPMB, "IPMB"), 1229 DLT_CHOICE(JUNIPER_ST, "Juniper Secure Tunnel"), 1230 DLT_CHOICE(BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"), 1231 DLT_CHOICE(AX25_KISS, "AX.25 with KISS header"), 1232 DLT_CHOICE(IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"), 1233 DLT_CHOICE(MPLS, "MPLS with label as link-layer header"), 1234 DLT_CHOICE(LINUX_EVDEV, "Linux evdev events"), 1235 DLT_CHOICE(USB_LINUX_MMAPPED, "USB with padded Linux header"), 1236 DLT_CHOICE(DECT, "DECT"), 1237 DLT_CHOICE(AOS, "AOS Space Data Link protocol"), 1238 DLT_CHOICE(WIHART, "Wireless HART"), 1239 DLT_CHOICE(FC_2, "Fibre Channel FC-2"), 1240 DLT_CHOICE(FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"), 1241 DLT_CHOICE(IPNET, "Solaris ipnet"), 1242 DLT_CHOICE(CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"), 1243 DLT_CHOICE(IPV4, "Raw IPv4"), 1244 DLT_CHOICE(IPV6, "Raw IPv6"), 1245 DLT_CHOICE(IEEE802_15_4_NOFCS, "IEEE 802.15.4 without FCS"), 1246 DLT_CHOICE(DBUS, "D-Bus"), 1247 DLT_CHOICE(JUNIPER_VS, "Juniper Virtual Server"), 1248 DLT_CHOICE(JUNIPER_SRX_E2E, "Juniper SRX E2E"), 1249 DLT_CHOICE(JUNIPER_FIBRECHANNEL, "Juniper Fibre Channel"), 1250 DLT_CHOICE(DVB_CI, "DVB-CI"), 1251 DLT_CHOICE(MUX27010, "MUX27010"), 1252 DLT_CHOICE(STANAG_5066_D_PDU, "STANAG 5066 D_PDUs"), 1253 DLT_CHOICE(JUNIPER_ATM_CEMIC, "Juniper ATM CEMIC"), 1254 DLT_CHOICE(NFLOG, "Linux netfilter log messages"), 1255 DLT_CHOICE(NETANALYZER, "Ethernet with Hilscher netANALYZER pseudo-header"), 1256 DLT_CHOICE(NETANALYZER_TRANSPARENT, "Ethernet with Hilscher netANALYZER pseudo-header and with preamble and SFD"), 1257 DLT_CHOICE(IPOIB, "RFC 4391 IP-over-Infiniband"), 1258 DLT_CHOICE(MPEG_2_TS, "MPEG-2 transport stream"), 1259 DLT_CHOICE(NG40, "ng40 protocol tester Iub/Iur"), 1260 DLT_CHOICE(NFC_LLCP, "NFC LLCP PDUs with pseudo-header"), 1261 DLT_CHOICE(INFINIBAND, "InfiniBand"), 1262 DLT_CHOICE(SCTP, "SCTP"), 1263 DLT_CHOICE(USBPCAP, "USB with USBPcap header"), 1264 DLT_CHOICE(RTAC_SERIAL, "Schweitzer Engineering Laboratories RTAC packets"), 1265 DLT_CHOICE(BLUETOOTH_LE_LL, "Bluetooth Low Energy air interface"), 1266 DLT_CHOICE(NETLINK, "Linux netlink"), 1267 DLT_CHOICE(BLUETOOTH_LINUX_MONITOR, "Bluetooth Linux Monitor"), 1268 DLT_CHOICE(BLUETOOTH_BREDR_BB, "Bluetooth Basic Rate/Enhanced Data Rate baseband packets"), 1269 DLT_CHOICE(BLUETOOTH_LE_LL_WITH_PHDR, "Bluetooth Low Energy air interface with pseudo-header"), 1270 DLT_CHOICE(PROFIBUS_DL, "PROFIBUS data link layer"), 1271 DLT_CHOICE(PKTAP, "Apple DLT_PKTAP"), 1272 DLT_CHOICE(EPON, "Ethernet with 802.3 Clause 65 EPON preamble"), 1273 DLT_CHOICE(IPMI_HPM_2, "IPMI trace packets"), 1274 DLT_CHOICE(ZWAVE_R1_R2, "Z-Wave RF profile R1 and R2 packets"), 1275 DLT_CHOICE(ZWAVE_R3, "Z-Wave RF profile R3 packets"), 1276 DLT_CHOICE(WATTSTOPPER_DLM, "WattStopper Digital Lighting Management (DLM) and Legrand Nitoo Open protocol"), 1277 DLT_CHOICE(ISO_14443, "ISO 14443 messages"), 1278 DLT_CHOICE(RDS, "IEC 62106 Radio Data System groups"), 1279 DLT_CHOICE_SENTINEL 1280 }; 1281 1282 int 1283 pcap_datalink_name_to_val(const char *name) 1284 { 1285 int i; 1286 1287 for (i = 0; dlt_choices[i].name != NULL; i++) { 1288 if (pcap_strcasecmp(dlt_choices[i].name, name) == 0) 1289 return (dlt_choices[i].dlt); 1290 } 1291 return (-1); 1292 } 1293 1294 const char * 1295 pcap_datalink_val_to_name(int dlt) 1296 { 1297 int i; 1298 1299 for (i = 0; dlt_choices[i].name != NULL; i++) { 1300 if (dlt_choices[i].dlt == dlt) 1301 return (dlt_choices[i].name); 1302 } 1303 return (NULL); 1304 } 1305 1306 const char * 1307 pcap_datalink_val_to_description(int dlt) 1308 { 1309 int i; 1310 1311 for (i = 0; dlt_choices[i].name != NULL; i++) { 1312 if (dlt_choices[i].dlt == dlt) 1313 return (dlt_choices[i].description); 1314 } 1315 return (NULL); 1316 } 1317 1318 struct tstamp_type_choice { 1319 const char *name; 1320 const char *description; 1321 int type; 1322 }; 1323 1324 static struct tstamp_type_choice tstamp_type_choices[] = { 1325 { "host", "Host", PCAP_TSTAMP_HOST }, 1326 { "host_lowprec", "Host, low precision", PCAP_TSTAMP_HOST_LOWPREC }, 1327 { "host_hiprec", "Host, high precision", PCAP_TSTAMP_HOST_HIPREC }, 1328 { "adapter", "Adapter", PCAP_TSTAMP_ADAPTER }, 1329 { "adapter_unsynced", "Adapter, not synced with system time", PCAP_TSTAMP_ADAPTER_UNSYNCED }, 1330 { NULL, NULL, 0 } 1331 }; 1332 1333 int 1334 pcap_tstamp_type_name_to_val(const char *name) 1335 { 1336 int i; 1337 1338 for (i = 0; tstamp_type_choices[i].name != NULL; i++) { 1339 if (pcap_strcasecmp(tstamp_type_choices[i].name, name) == 0) 1340 return (tstamp_type_choices[i].type); 1341 } 1342 return (PCAP_ERROR); 1343 } 1344 1345 const char * 1346 pcap_tstamp_type_val_to_name(int tstamp_type) 1347 { 1348 int i; 1349 1350 for (i = 0; tstamp_type_choices[i].name != NULL; i++) { 1351 if (tstamp_type_choices[i].type == tstamp_type) 1352 return (tstamp_type_choices[i].name); 1353 } 1354 return (NULL); 1355 } 1356 1357 const char * 1358 pcap_tstamp_type_val_to_description(int tstamp_type) 1359 { 1360 int i; 1361 1362 for (i = 0; tstamp_type_choices[i].name != NULL; i++) { 1363 if (tstamp_type_choices[i].type == tstamp_type) 1364 return (tstamp_type_choices[i].description); 1365 } 1366 return (NULL); 1367 } 1368 1369 int 1370 pcap_snapshot(pcap_t *p) 1371 { 1372 if (!p->activated) 1373 return (PCAP_ERROR_NOT_ACTIVATED); 1374 return (p->snapshot); 1375 } 1376 1377 int 1378 pcap_is_swapped(pcap_t *p) 1379 { 1380 if (!p->activated) 1381 return (PCAP_ERROR_NOT_ACTIVATED); 1382 return (p->swapped); 1383 } 1384 1385 int 1386 pcap_major_version(pcap_t *p) 1387 { 1388 if (!p->activated) 1389 return (PCAP_ERROR_NOT_ACTIVATED); 1390 return (p->version_major); 1391 } 1392 1393 int 1394 pcap_minor_version(pcap_t *p) 1395 { 1396 if (!p->activated) 1397 return (PCAP_ERROR_NOT_ACTIVATED); 1398 return (p->version_minor); 1399 } 1400 1401 FILE * 1402 pcap_file(pcap_t *p) 1403 { 1404 return (p->rfile); 1405 } 1406 1407 int 1408 pcap_fileno(pcap_t *p) 1409 { 1410 #ifndef _WIN32 1411 return (p->fd); 1412 #else 1413 if (p->adapter != NULL) 1414 return ((int)(DWORD)p->adapter->hFile); 1415 else 1416 return (PCAP_ERROR); 1417 #endif 1418 } 1419 1420 #if !defined(_WIN32) && !defined(MSDOS) 1421 int 1422 pcap_get_selectable_fd(pcap_t *p) 1423 { 1424 return (p->selectable_fd); 1425 } 1426 #endif 1427 1428 void 1429 pcap_perror(pcap_t *p, const char *prefix) 1430 { 1431 fprintf(stderr, "%s: %s\n", prefix, p->errbuf); 1432 } 1433 1434 char * 1435 pcap_geterr(pcap_t *p) 1436 { 1437 return (p->errbuf); 1438 } 1439 1440 int 1441 pcap_getnonblock(pcap_t *p, char *errbuf) 1442 { 1443 int ret; 1444 1445 ret = p->getnonblock_op(p, errbuf); 1446 if (ret == -1) { 1447 /* 1448 * In case somebody depended on the bug wherein 1449 * the error message was put into p->errbuf 1450 * by pcap_getnonblock_fd(). 1451 */ 1452 strlcpy(p->errbuf, errbuf, PCAP_ERRBUF_SIZE); 1453 } 1454 return (ret); 1455 } 1456 1457 /* 1458 * Get the current non-blocking mode setting, under the assumption that 1459 * it's just the standard POSIX non-blocking flag. 1460 */ 1461 #if !defined(_WIN32) && !defined(MSDOS) 1462 int 1463 pcap_getnonblock_fd(pcap_t *p, char *errbuf) 1464 { 1465 int fdflags; 1466 1467 fdflags = fcntl(p->fd, F_GETFL, 0); 1468 if (fdflags == -1) { 1469 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 1470 pcap_strerror(errno)); 1471 return (-1); 1472 } 1473 if (fdflags & O_NONBLOCK) 1474 return (1); 1475 else 1476 return (0); 1477 } 1478 #endif 1479 1480 int 1481 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf) 1482 { 1483 int ret; 1484 1485 ret = p->setnonblock_op(p, nonblock, errbuf); 1486 if (ret == -1) { 1487 /* 1488 * In case somebody depended on the bug wherein 1489 * the error message was put into p->errbuf 1490 * by pcap_setnonblock_fd(). 1491 */ 1492 strlcpy(p->errbuf, errbuf, PCAP_ERRBUF_SIZE); 1493 } 1494 return (ret); 1495 } 1496 1497 #if !defined(_WIN32) && !defined(MSDOS) 1498 /* 1499 * Set non-blocking mode, under the assumption that it's just the 1500 * standard POSIX non-blocking flag. (This can be called by the 1501 * per-platform non-blocking-mode routine if that routine also 1502 * needs to do some additional work.) 1503 */ 1504 int 1505 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf) 1506 { 1507 int fdflags; 1508 1509 fdflags = fcntl(p->fd, F_GETFL, 0); 1510 if (fdflags == -1) { 1511 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 1512 pcap_strerror(errno)); 1513 return (-1); 1514 } 1515 if (nonblock) 1516 fdflags |= O_NONBLOCK; 1517 else 1518 fdflags &= ~O_NONBLOCK; 1519 if (fcntl(p->fd, F_SETFL, fdflags) == -1) { 1520 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s", 1521 pcap_strerror(errno)); 1522 return (-1); 1523 } 1524 return (0); 1525 } 1526 #endif 1527 1528 #ifdef _WIN32 1529 /* 1530 * Generate a string for a Win32-specific error (i.e. an error generated when 1531 * calling a Win32 API). 1532 * For errors occurred during standard C calls, we still use pcap_strerror() 1533 */ 1534 void 1535 pcap_win32_err_to_str(DWORD error, char *errbuf) 1536 { 1537 size_t errlen; 1538 char *p; 1539 1540 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf, 1541 PCAP_ERRBUF_SIZE, NULL); 1542 1543 /* 1544 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the 1545 * message. Get rid of it. 1546 */ 1547 errlen = strlen(errbuf); 1548 if (errlen >= 2) { 1549 errbuf[errlen - 1] = '\0'; 1550 errbuf[errlen - 2] = '\0'; 1551 } 1552 p = strchr(errbuf, '\0'); 1553 pcap_snprintf (p, PCAP_ERRBUF_SIZE+1-(p-errbuf), " (%lu)", error); 1554 } 1555 #endif 1556 1557 /* 1558 * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values. 1559 */ 1560 const char * 1561 pcap_statustostr(int errnum) 1562 { 1563 static char ebuf[15+10+1]; 1564 1565 switch (errnum) { 1566 1567 case PCAP_WARNING: 1568 return("Generic warning"); 1569 1570 case PCAP_WARNING_TSTAMP_TYPE_NOTSUP: 1571 return ("That type of time stamp is not supported by that device"); 1572 1573 case PCAP_WARNING_PROMISC_NOTSUP: 1574 return ("That device doesn't support promiscuous mode"); 1575 1576 case PCAP_ERROR: 1577 return("Generic error"); 1578 1579 case PCAP_ERROR_BREAK: 1580 return("Loop terminated by pcap_breakloop"); 1581 1582 case PCAP_ERROR_NOT_ACTIVATED: 1583 return("The pcap_t has not been activated"); 1584 1585 case PCAP_ERROR_ACTIVATED: 1586 return ("The setting can't be changed after the pcap_t is activated"); 1587 1588 case PCAP_ERROR_NO_SUCH_DEVICE: 1589 return ("No such device exists"); 1590 1591 case PCAP_ERROR_RFMON_NOTSUP: 1592 return ("That device doesn't support monitor mode"); 1593 1594 case PCAP_ERROR_NOT_RFMON: 1595 return ("That operation is supported only in monitor mode"); 1596 1597 case PCAP_ERROR_PERM_DENIED: 1598 return ("You don't have permission to capture on that device"); 1599 1600 case PCAP_ERROR_IFACE_NOT_UP: 1601 return ("That device is not up"); 1602 1603 case PCAP_ERROR_CANTSET_TSTAMP_TYPE: 1604 return ("That device doesn't support setting the time stamp type"); 1605 1606 case PCAP_ERROR_PROMISC_PERM_DENIED: 1607 return ("You don't have permission to capture in promiscuous mode on that device"); 1608 1609 case PCAP_ERROR_TSTAMP_PRECISION_NOTSUP: 1610 return ("That device doesn't support that time stamp precision"); 1611 } 1612 (void)pcap_snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum); 1613 return(ebuf); 1614 } 1615 1616 /* 1617 * Not all systems have strerror(). 1618 */ 1619 const char * 1620 pcap_strerror(int errnum) 1621 { 1622 #ifdef HAVE_STRERROR 1623 #ifdef _WIN32 1624 static char errbuf[PCAP_ERRBUF_SIZE]; 1625 errno_t errno; 1626 errno = strerror_s(errbuf, PCAP_ERRBUF_SIZE, errnum); 1627 if (errno != 0) /* errno = 0 if successful */ 1628 strlcpy(errbuf, "strerror_s() error", PCAP_ERRBUF_SIZE); 1629 return (errbuf); 1630 #else 1631 return (strerror(errnum)); 1632 #endif /* _WIN32 */ 1633 #else 1634 extern int sys_nerr; 1635 extern const char *const sys_errlist[]; 1636 static char errbuf[PCAP_ERRBUF_SIZE]; 1637 1638 if ((unsigned int)errnum < sys_nerr) 1639 return ((char *)sys_errlist[errnum]); 1640 (void)pcap_snprintf(errbuf, sizeof errbuf, "Unknown error: %d", errnum); 1641 return (errbuf); 1642 #endif 1643 } 1644 1645 int 1646 pcap_setfilter(pcap_t *p, struct bpf_program *fp) 1647 { 1648 return (p->setfilter_op(p, fp)); 1649 } 1650 1651 /* 1652 * Set direction flag, which controls whether we accept only incoming 1653 * packets, only outgoing packets, or both. 1654 * Note that, depending on the platform, some or all direction arguments 1655 * might not be supported. 1656 */ 1657 int 1658 pcap_setdirection(pcap_t *p, pcap_direction_t d) 1659 { 1660 if (p->setdirection_op == NULL) { 1661 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1662 "Setting direction is not implemented on this platform"); 1663 return (-1); 1664 } else 1665 return (p->setdirection_op(p, d)); 1666 } 1667 1668 int 1669 pcap_stats(pcap_t *p, struct pcap_stat *ps) 1670 { 1671 return (p->stats_op(p, ps)); 1672 } 1673 1674 static int 1675 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_) 1676 { 1677 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1678 "Statistics aren't available from a pcap_open_dead pcap_t"); 1679 return (-1); 1680 } 1681 1682 #ifdef _WIN32 1683 struct pcap_stat * 1684 pcap_stats_ex(pcap_t *p, int *pcap_stat_size) 1685 { 1686 return (p->stats_ex_op(p, pcap_stat_size)); 1687 } 1688 1689 int 1690 pcap_setbuff(pcap_t *p, int dim) 1691 { 1692 return (p->setbuff_op(p, dim)); 1693 } 1694 1695 static int 1696 pcap_setbuff_dead(pcap_t *p, int dim) 1697 { 1698 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1699 "The kernel buffer size cannot be set on a pcap_open_dead pcap_t"); 1700 return (-1); 1701 } 1702 1703 int 1704 pcap_setmode(pcap_t *p, int mode) 1705 { 1706 return (p->setmode_op(p, mode)); 1707 } 1708 1709 static int 1710 pcap_setmode_dead(pcap_t *p, int mode) 1711 { 1712 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1713 "impossible to set mode on a pcap_open_dead pcap_t"); 1714 return (-1); 1715 } 1716 1717 int 1718 pcap_setmintocopy(pcap_t *p, int size) 1719 { 1720 return (p->setmintocopy_op(p, size)); 1721 } 1722 1723 static int 1724 pcap_setmintocopy_dead(pcap_t *p, int size) 1725 { 1726 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1727 "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t"); 1728 return (-1); 1729 } 1730 1731 HANDLE 1732 pcap_getevent(pcap_t *p) 1733 { 1734 return (p->getevent_op(p)); 1735 } 1736 1737 static HANDLE 1738 pcap_getevent_dead(pcap_t *p) 1739 { 1740 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1741 "A pcap_open_dead pcap_t has no event handle"); 1742 return (INVALID_HANDLE_VALUE); 1743 } 1744 1745 int 1746 pcap_oid_get_request(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp) 1747 { 1748 return (p->oid_get_request_op(p, oid, data, lenp)); 1749 } 1750 1751 static int 1752 pcap_oid_get_request_dead(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_, 1753 size_t *lenp _U_) 1754 { 1755 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1756 "An OID get request cannot be performed on a pcap_open_dead pcap_t"); 1757 return (PCAP_ERROR); 1758 } 1759 1760 int 1761 pcap_oid_set_request(pcap_t *p, bpf_u_int32 oid, const void *data, size_t *lenp) 1762 { 1763 return (p->oid_set_request_op(p, oid, data, lenp)); 1764 } 1765 1766 static int 1767 pcap_oid_set_request_dead(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_, 1768 size_t *lenp _U_) 1769 { 1770 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1771 "An OID set request cannot be performed on a pcap_open_dead pcap_t"); 1772 return (PCAP_ERROR); 1773 } 1774 1775 pcap_send_queue * 1776 pcap_sendqueue_alloc(u_int memsize) 1777 { 1778 pcap_send_queue *tqueue; 1779 1780 /* Allocate the queue */ 1781 tqueue = (pcap_send_queue *)malloc(sizeof(pcap_send_queue)); 1782 if (tqueue == NULL){ 1783 return (NULL); 1784 } 1785 1786 /* Allocate the buffer */ 1787 tqueue->buffer = (char *)malloc(memsize); 1788 if (tqueue->buffer == NULL) { 1789 free(tqueue); 1790 return (NULL); 1791 } 1792 1793 tqueue->maxlen = memsize; 1794 tqueue->len = 0; 1795 1796 return (tqueue); 1797 } 1798 1799 void 1800 pcap_sendqueue_destroy(pcap_send_queue *queue) 1801 { 1802 free(queue->buffer); 1803 free(queue); 1804 } 1805 1806 int 1807 pcap_sendqueue_queue(pcap_send_queue *queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data) 1808 { 1809 if (queue->len + sizeof(struct pcap_pkthdr) + pkt_header->caplen > queue->maxlen){ 1810 return (-1); 1811 } 1812 1813 /* Copy the pcap_pkthdr header*/ 1814 memcpy(queue->buffer + queue->len, pkt_header, sizeof(struct pcap_pkthdr)); 1815 queue->len += sizeof(struct pcap_pkthdr); 1816 1817 /* copy the packet */ 1818 memcpy(queue->buffer + queue->len, pkt_data, pkt_header->caplen); 1819 queue->len += pkt_header->caplen; 1820 1821 return (0); 1822 } 1823 1824 u_int 1825 pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue, int sync) 1826 { 1827 return (p->sendqueue_transmit_op(p, queue, sync)); 1828 } 1829 1830 static u_int 1831 pcap_sendqueue_transmit_dead(pcap_t *p, pcap_send_queue *queue, int sync) 1832 { 1833 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1834 "Packets cannot be transmitted on a pcap_open_dead pcap_t"); 1835 return (0); 1836 } 1837 1838 int 1839 pcap_setuserbuffer(pcap_t *p, int size) 1840 { 1841 return (p->setuserbuffer_op(p, size)); 1842 } 1843 1844 static int 1845 pcap_setuserbuffer_dead(pcap_t *p, int size) 1846 { 1847 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1848 "The user buffer cannot be set on a pcap_open_dead pcap_t"); 1849 return (-1); 1850 } 1851 1852 int 1853 pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks) 1854 { 1855 return (p->live_dump_op(p, filename, maxsize, maxpacks)); 1856 } 1857 1858 static int 1859 pcap_live_dump_dead(pcap_t *p, char *filename, int maxsize, int maxpacks) 1860 { 1861 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1862 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t"); 1863 return (-1); 1864 } 1865 1866 int 1867 pcap_live_dump_ended(pcap_t *p, int sync) 1868 { 1869 return (p->live_dump_ended_op(p, sync)); 1870 } 1871 1872 static int 1873 pcap_live_dump_ended_dead(pcap_t *p, int sync) 1874 { 1875 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1876 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t"); 1877 return (-1); 1878 } 1879 1880 PAirpcapHandle 1881 pcap_get_airpcap_handle(pcap_t *p) 1882 { 1883 PAirpcapHandle handle; 1884 1885 handle = p->get_airpcap_handle_op(p); 1886 if (handle == NULL) { 1887 (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), 1888 "This isn't an AirPcap device"); 1889 } 1890 return (handle); 1891 } 1892 1893 static PAirpcapHandle 1894 pcap_get_airpcap_handle_dead(pcap_t *p) 1895 { 1896 return (NULL); 1897 } 1898 #endif 1899 1900 /* 1901 * On some platforms, we need to clean up promiscuous or monitor mode 1902 * when we close a device - and we want that to happen even if the 1903 * application just exits without explicitl closing devices. 1904 * On those platforms, we need to register a "close all the pcaps" 1905 * routine to be called when we exit, and need to maintain a list of 1906 * pcaps that need to be closed to clean up modes. 1907 * 1908 * XXX - not thread-safe. 1909 */ 1910 1911 /* 1912 * List of pcaps on which we've done something that needs to be 1913 * cleaned up. 1914 * If there are any such pcaps, we arrange to call "pcap_close_all()" 1915 * when we exit, and have it close all of them. 1916 */ 1917 static struct pcap *pcaps_to_close; 1918 1919 /* 1920 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to 1921 * be called on exit. 1922 */ 1923 static int did_atexit; 1924 1925 static void 1926 pcap_close_all(void) 1927 { 1928 struct pcap *handle; 1929 1930 while ((handle = pcaps_to_close) != NULL) 1931 pcap_close(handle); 1932 } 1933 1934 int 1935 pcap_do_addexit(pcap_t *p) 1936 { 1937 /* 1938 * If we haven't already done so, arrange to have 1939 * "pcap_close_all()" called when we exit. 1940 */ 1941 if (!did_atexit) { 1942 if (atexit(pcap_close_all) != 0) { 1943 /* 1944 * "atexit()" failed; let our caller know. 1945 */ 1946 strlcpy(p->errbuf, "atexit failed", PCAP_ERRBUF_SIZE); 1947 return (0); 1948 } 1949 did_atexit = 1; 1950 } 1951 return (1); 1952 } 1953 1954 void 1955 pcap_add_to_pcaps_to_close(pcap_t *p) 1956 { 1957 p->next = pcaps_to_close; 1958 pcaps_to_close = p; 1959 } 1960 1961 void 1962 pcap_remove_from_pcaps_to_close(pcap_t *p) 1963 { 1964 pcap_t *pc, *prevpc; 1965 1966 for (pc = pcaps_to_close, prevpc = NULL; pc != NULL; 1967 prevpc = pc, pc = pc->next) { 1968 if (pc == p) { 1969 /* 1970 * Found it. Remove it from the list. 1971 */ 1972 if (prevpc == NULL) { 1973 /* 1974 * It was at the head of the list. 1975 */ 1976 pcaps_to_close = pc->next; 1977 } else { 1978 /* 1979 * It was in the middle of the list. 1980 */ 1981 prevpc->next = pc->next; 1982 } 1983 break; 1984 } 1985 } 1986 } 1987 1988 void 1989 pcap_cleanup_live_common(pcap_t *p) 1990 { 1991 if (p->buffer != NULL) { 1992 free(p->buffer); 1993 p->buffer = NULL; 1994 } 1995 if (p->dlt_list != NULL) { 1996 free(p->dlt_list); 1997 p->dlt_list = NULL; 1998 p->dlt_count = 0; 1999 } 2000 if (p->tstamp_type_list != NULL) { 2001 free(p->tstamp_type_list); 2002 p->tstamp_type_list = NULL; 2003 p->tstamp_type_count = 0; 2004 } 2005 if (p->tstamp_precision_list != NULL) { 2006 free(p->tstamp_precision_list); 2007 p->tstamp_precision_list = NULL; 2008 p->tstamp_precision_count = 0; 2009 } 2010 pcap_freecode(&p->fcode); 2011 #if !defined(_WIN32) && !defined(MSDOS) 2012 if (p->fd >= 0) { 2013 close(p->fd); 2014 p->fd = -1; 2015 } 2016 p->selectable_fd = -1; 2017 #endif 2018 } 2019 2020 static void 2021 pcap_cleanup_dead(pcap_t *p _U_) 2022 { 2023 /* Nothing to do. */ 2024 } 2025 2026 pcap_t * 2027 pcap_open_dead_with_tstamp_precision(int linktype, int snaplen, u_int precision) 2028 { 2029 pcap_t *p; 2030 2031 switch (precision) { 2032 2033 case PCAP_TSTAMP_PRECISION_MICRO: 2034 case PCAP_TSTAMP_PRECISION_NANO: 2035 break; 2036 2037 default: 2038 return NULL; 2039 } 2040 p = malloc(sizeof(*p)); 2041 if (p == NULL) 2042 return NULL; 2043 memset (p, 0, sizeof(*p)); 2044 p->snapshot = snaplen; 2045 p->linktype = linktype; 2046 p->opt.tstamp_precision = precision; 2047 p->stats_op = pcap_stats_dead; 2048 #ifdef _WIN32 2049 p->stats_ex_op = (stats_ex_op_t)pcap_not_initialized_ptr; 2050 p->setbuff_op = pcap_setbuff_dead; 2051 p->setmode_op = pcap_setmode_dead; 2052 p->setmintocopy_op = pcap_setmintocopy_dead; 2053 p->getevent_op = pcap_getevent_dead; 2054 p->oid_get_request_op = pcap_oid_get_request_dead; 2055 p->oid_set_request_op = pcap_oid_set_request_dead; 2056 p->sendqueue_transmit_op = pcap_sendqueue_transmit_dead; 2057 p->setuserbuffer_op = pcap_setuserbuffer_dead; 2058 p->live_dump_op = pcap_live_dump_dead; 2059 p->live_dump_ended_op = pcap_live_dump_ended_dead; 2060 p->get_airpcap_handle_op = pcap_get_airpcap_handle_dead; 2061 #endif 2062 p->cleanup_op = pcap_cleanup_dead; 2063 2064 /* 2065 * A "dead" pcap_t never requires special BPF code generation. 2066 */ 2067 p->bpf_codegen_flags = 0; 2068 2069 p->activated = 1; 2070 return (p); 2071 } 2072 2073 pcap_t * 2074 pcap_open_dead(int linktype, int snaplen) 2075 { 2076 return (pcap_open_dead_with_tstamp_precision(linktype, snaplen, 2077 PCAP_TSTAMP_PRECISION_MICRO)); 2078 } 2079 2080 /* 2081 * API compatible with WinPcap's "send a packet" routine - returns -1 2082 * on error, 0 otherwise. 2083 * 2084 * XXX - what if we get a short write? 2085 */ 2086 int 2087 pcap_sendpacket(pcap_t *p, const u_char *buf, int size) 2088 { 2089 if (p->inject_op(p, buf, size) == -1) 2090 return (-1); 2091 return (0); 2092 } 2093 2094 /* 2095 * API compatible with OpenBSD's "send a packet" routine - returns -1 on 2096 * error, number of bytes written otherwise. 2097 */ 2098 int 2099 pcap_inject(pcap_t *p, const void *buf, size_t size) 2100 { 2101 return (p->inject_op(p, buf, size)); 2102 } 2103 2104 void 2105 pcap_close(pcap_t *p) 2106 { 2107 if (p->opt.device != NULL) 2108 free(p->opt.device); 2109 p->cleanup_op(p); 2110 free(p); 2111 } 2112 2113 /* 2114 * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw 2115 * data for the packet, check whether the packet passes the filter. 2116 * Returns the return value of the filter program, which will be zero if 2117 * the packet doesn't pass and non-zero if the packet does pass. 2118 */ 2119 int 2120 pcap_offline_filter(const struct bpf_program *fp, const struct pcap_pkthdr *h, 2121 const u_char *pkt) 2122 { 2123 const struct bpf_insn *fcode = fp->bf_insns; 2124 2125 if (fcode != NULL) 2126 return (bpf_filter(fcode, pkt, h->len, h->caplen)); 2127 else 2128 return (0); 2129 } 2130 2131 #include "pcap_version.h" 2132 2133 #ifdef _WIN32 2134 2135 static char *full_pcap_version_string; 2136 2137 #ifdef HAVE_VERSION_H 2138 /* 2139 * libpcap being built for Windows, as part of a WinPcap/Npcap source 2140 * tree. Include version.h from that source tree to get the WinPcap/Npcap 2141 * version. 2142 * 2143 * XXX - it'd be nice if we could somehow generate the WinPcap version number 2144 * when building WinPcap. (It'd be nice to do so for the packet.dll version 2145 * number as well.) 2146 */ 2147 #include "../../version.h" 2148 2149 static const char wpcap_version_string[] = WINPCAP_VER_STRING; 2150 static const char pcap_version_string_fmt[] = 2151 WINPCAP_PRODUCT_NAME " version %s, based on %s"; 2152 static const char pcap_version_string_packet_dll_fmt[] = 2153 WINPCAP_PRODUCT_NAME " version %s (packet.dll version %s), based on %s"; 2154 2155 const char * 2156 pcap_lib_version(void) 2157 { 2158 char *packet_version_string; 2159 size_t full_pcap_version_string_len; 2160 2161 if (full_pcap_version_string == NULL) { 2162 /* 2163 * Generate the version string. 2164 */ 2165 packet_version_string = PacketGetVersion(); 2166 if (strcmp(wpcap_version_string, packet_version_string) == 0) { 2167 /* 2168 * WinPcap version string and packet.dll version 2169 * string are the same; just report the WinPcap 2170 * version. 2171 */ 2172 full_pcap_version_string_len = 2173 (sizeof pcap_version_string_fmt - 4) + 2174 strlen(wpcap_version_string) + 2175 strlen(pcap_version_string); 2176 full_pcap_version_string = 2177 malloc(full_pcap_version_string_len); 2178 if (full_pcap_version_string == NULL) 2179 return (NULL); 2180 pcap_snprintf(full_pcap_version_string, 2181 full_pcap_version_string_len, 2182 pcap_version_string_fmt, 2183 wpcap_version_string, 2184 pcap_version_string); 2185 } else { 2186 /* 2187 * WinPcap version string and packet.dll version 2188 * string are different; that shouldn't be the 2189 * case (the two libraries should come from the 2190 * same version of WinPcap), so we report both 2191 * versions. 2192 */ 2193 full_pcap_version_string_len = 2194 (sizeof pcap_version_string_packet_dll_fmt - 6) + 2195 strlen(wpcap_version_string) + 2196 strlen(packet_version_string) + 2197 strlen(pcap_version_string); 2198 full_pcap_version_string = malloc(full_pcap_version_string_len); 2199 if (full_pcap_version_string == NULL) 2200 return (NULL); 2201 pcap_snprintf(full_pcap_version_string, 2202 full_pcap_version_string_len, 2203 pcap_version_string_packet_dll_fmt, 2204 wpcap_version_string, 2205 packet_version_string, 2206 pcap_version_string); 2207 } 2208 } 2209 return (full_pcap_version_string); 2210 } 2211 2212 #else /* HAVE_VERSION_H */ 2213 2214 /* 2215 * libpcap being built for Windows, not as part of a WinPcap/Npcap source 2216 * tree. 2217 */ 2218 static const char pcap_version_string_packet_dll_fmt[] = 2219 "%s (packet.dll version %s)"; 2220 const char * 2221 pcap_lib_version(void) 2222 { 2223 char *packet_version_string; 2224 size_t full_pcap_version_string_len; 2225 2226 if (full_pcap_version_string == NULL) { 2227 /* 2228 * Generate the version string. Report the packet.dll 2229 * version. 2230 */ 2231 packet_version_string = PacketGetVersion(); 2232 full_pcap_version_string_len = 2233 (sizeof pcap_version_string_packet_dll_fmt - 4) + 2234 strlen(pcap_version_string) + 2235 strlen(packet_version_string); 2236 full_pcap_version_string = malloc(full_pcap_version_string_len); 2237 if (full_pcap_version_string == NULL) 2238 return (NULL); 2239 pcap_snprintf(full_pcap_version_string, 2240 full_pcap_version_string_len, 2241 pcap_version_string_packet_dll_fmt, 2242 pcap_version_string, 2243 packet_version_string); 2244 } 2245 return (full_pcap_version_string); 2246 } 2247 2248 #endif /* HAVE_VERSION_H */ 2249 2250 #elif defined(MSDOS) 2251 2252 static char *full_pcap_version_string; 2253 2254 const char * 2255 pcap_lib_version (void) 2256 { 2257 char *packet_version_string; 2258 size_t full_pcap_version_string_len; 2259 static char dospfx[] = "DOS-"; 2260 2261 if (full_pcap_version_string == NULL) { 2262 /* 2263 * Generate the version string. 2264 */ 2265 full_pcap_version_string_len = 2266 sizeof dospfx + strlen(pcap_version_string); 2267 full_pcap_version_string = 2268 malloc(full_pcap_version_string_len); 2269 if (full_pcap_version_string == NULL) 2270 return (NULL); 2271 strcpy(full_pcap_version_string, dospfx); 2272 strcat(full_pcap_version_string, pcap_version_string); 2273 } 2274 return (full_pcap_version_string); 2275 } 2276 2277 #else /* UN*X */ 2278 2279 const char * 2280 pcap_lib_version(void) 2281 { 2282 return (pcap_version_string); 2283 } 2284 #endif 2285 2286 #ifdef YYDEBUG 2287 /* 2288 * Set the internal "debug printout" flag for the filter expression parser. 2289 * The code to print that stuff is present only if YYDEBUG is defined, so 2290 * the flag, and the routine to set it, are defined only if YYDEBUG is 2291 * defined. 2292 * 2293 * This is intended for libpcap developers, not for general use. 2294 * If you want to set these in a program, you'll have to declare this 2295 * routine yourself, with the appropriate DLL import attribute on Windows; 2296 * it's not declared in any header file, and won't be declared in any 2297 * header file provided by libpcap. 2298 */ 2299 PCAP_API void pcap_set_parser_debug(int value); 2300 2301 PCAP_API_DEF void 2302 pcap_set_parser_debug(int value) 2303 { 2304 extern int pcap_debug; 2305 2306 pcap_debug = value; 2307 } 2308 #endif 2309 2310 #ifdef BDEBUG 2311 /* 2312 * Set the internal "debug printout" flag for the filter expression optimizer. 2313 * The code to print that stuff is present only if BDEBUG is defined, so 2314 * the flag, and the routine to set it, are defined only if BDEBUG is 2315 * defined. 2316 * 2317 * This is intended for libpcap developers, not for general use. 2318 * If you want to set these in a program, you'll have to declare this 2319 * routine yourself, with the appropriate DLL import attribute on Windows; 2320 * it's not declared in any header file, and won't be declared in any 2321 * header file provided by libpcap. 2322 */ 2323 PCAP_API void pcap_set_optimizer_debug(int value); 2324 2325 PCAP_API_DEF void 2326 pcap_set_optimizer_debug(int value) 2327 { 2328 extern int pcap_optimizer_debug; 2329 2330 pcap_optimizer_debug = value; 2331 } 2332 #endif 2333