Lines Matching +full:no +full:- +full:can +full:- +full:fd

2  *  pcap-linux.c: Packet capture interface to the Linux kernel
5 * Sebastian Krahmer <krahmer@cs.uni-potsdam.de>
36 * Monitor-mode support for mac80211 includes code taken from the iw
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
99 #include "pcap-int.h"
100 #include "pcap-util.h"
105 #include "diag-control.h"
124 * least some people are doing cross-builds for MIPS with older versions
135 (__atomic_load_n(&pkt->tp_status, __ATOMIC_ACQUIRE) != TP_STATUS_KERNEL)
137 (__atomic_store_n(&pkt->tp_status, TP_STATUS_KERNEL, __ATOMIC_RELEASE))
139 (__atomic_load_n(&pkt->hdr.bh1.block_status, __ATOMIC_ACQUIRE) != TP_STATUS_KERNEL)
141 (__atomic_store_n(&pkt->hdr.bh1.block_status, TP_STATUS_KERNEL, __ATOMIC_RELEASE))
199 u_char *mmapbuf; /* memory-mapped region pointer */
201 int vlan_offset; /* offset at which to insert vlan tags; if -1, don't insert */
210 int poll_breakloop_fd; /* fd to an eventfd to break from blocking operations */
217 #define MUST_DELETE_MONIF 0x00000002 /* delete monitor-mode interface */
244 #define RING_GET_FRAME_AT(h, offset) (((u_char **)h->buffer)[(offset)])
245 #define RING_GET_CURRENT_FRAME(h) RING_GET_FRAME_AT(h, h->offset)
260 * In pre-3.0 kernels, the tp_vlan_tci field is set to whatever the
261 * vlan_tci field in the skbuff is. 0 can either mean "not on a VLAN"
262 * or "on VLAN 0". There is no flag set in the tp_status field to
270 * With a pre-3.0 kernel, we cannot distinguish between packets with no
272 * there's nothing we can do about that.
280 * continues the old unfixably-imperfect behavior on pre-3.0 kernels.
286 #define VLAN_VALID(hdr, hv) ((hv)->tp_vlan_tci != 0 || ((hdr)->tp_status & TP_STATUS_VLAN_VALID))
291 * we can test it if we're running on a system that has it. (If we're
296 #define VLAN_VALID(hdr, hv) ((hv)->tp_vlan_tci != 0 || ((hdr)->tp_status & 0x10))
300 # define VLAN_TPID(hdr, hv) (((hv)->tp_vlan_tpid || ((hdr)->tp_status & TP_STATUS_VLAN_TPID_VALID))…
307 * indication - 1 millisecond.
316 static int iface_get_id(int fd, const char *device, char *ebuf);
317 static int iface_get_mtu(int fd, const char *device, char *ebuf);
318 static int iface_get_arptype(int fd, const char *device, char *ebuf);
319 static int iface_bind(int fd, int ifindex, char *ebuf, int protocol);
347 handle->activate_op = pcap_activate_linux; in pcapint_create_interface()
348 handle->can_set_rfmon_op = pcap_can_set_rfmon_linux; in pcapint_create_interface()
353 if (iface_get_ts_types(device, handle, ebuf) == -1) { in pcapint_create_interface()
362 * XXX - with adapter-supplied time stamps, can we choose in pcapint_create_interface()
366 handle->tstamp_precision_list = malloc(2 * sizeof(u_int)); in pcapint_create_interface()
367 if (handle->tstamp_precision_list == NULL) { in pcapint_create_interface()
373 handle->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; in pcapint_create_interface()
374 handle->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; in pcapint_create_interface()
375 handle->tstamp_precision_count = 2; in pcapint_create_interface()
381 struct pcap_linux *handlep = handle->priv; in pcapint_create_interface()
382 handlep->poll_breakloop_fd = -1; in pcapint_create_interface()
393 * On Fedora 9, with a 2.6.26.3-29 kernel, my Zydas stick, at
396 * "tcpdump -D" output. Capturing on the wmaster0 device
399 * airmon-ng searches through /sys/class/net for devices named
411 * mode and configures it up. Otherwise, you can't do monitor mode.
415 * place, so, given a wmaster, wlan, or mon device, you can
424 * or by sending {monif_name}, with no NL, down
430 * value of -ENFILE. (Return values are negative errnos.) We
433 * Yes, you can have multiple monitor devices for a given
439 * return 1; if not, return 0. On an error, fill in handle->errbuf and
452 if (asprintf(&pathstr, "/sys/class/net/%s/phy80211", device) == -1) { in get_mac80211_phydev()
453 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in get_mac80211_phydev()
454 "%s: Can't generate path name string for /sys/class/net device", in get_mac80211_phydev()
459 if (bytes_read == -1) { in get_mac80211_phydev()
468 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in get_mac80211_phydev()
469 errno, "%s: Can't readlink %s", device, pathstr); in get_mac80211_phydev()
489 state->nl_sock = nl_socket_alloc(); in nl80211_init()
490 if (!state->nl_sock) { in nl80211_init()
491 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in nl80211_init()
496 if (genl_connect(state->nl_sock)) { in nl80211_init()
497 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in nl80211_init()
502 err = genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache); in nl80211_init()
504 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in nl80211_init()
506 device, nl_geterror(-err)); in nl80211_init()
510 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211"); in nl80211_init()
511 if (!state->nl80211) { in nl80211_init()
512 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in nl80211_init()
520 nl_cache_free(state->nl_cache); in nl80211_init()
522 nl_socket_free(state->nl_sock); in nl80211_init()
529 genl_family_put(state->nl80211); in nl80211_cleanup()
530 nl_cache_free(state->nl_cache); in nl80211_cleanup()
531 nl_socket_free(state->nl_sock); in nl80211_cleanup()
542 struct pcap_linux *handlep = handle->priv; in add_mon_if()
547 ifindex = iface_get_id(sock_fd, device, handle->errbuf); in add_mon_if()
548 if (ifindex == -1) in add_mon_if()
553 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in add_mon_if()
558 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, in add_mon_if()
566 err = nl_send_auto_complete(state->nl_sock, msg); in add_mon_if()
568 if (err == -NLE_FAILURE) { in add_mon_if()
572 * NLE_FAILURE; it can also map other errors in add_mon_if()
573 * to that, but there's not much we can do in add_mon_if()
583 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in add_mon_if()
585 device, mondevice, nl_geterror(-err)); in add_mon_if()
590 err = nl_wait_for_ack(state->nl_sock); in add_mon_if()
592 if (err == -NLE_FAILURE) { in add_mon_if()
596 * NLE_FAILURE; it can also map other errors in add_mon_if()
597 * to that, but there's not much we can do in add_mon_if()
607 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in add_mon_if()
609 device, mondevice, nl_geterror(-err)); in add_mon_if()
623 handlep->mondevice = strdup(mondevice); in add_mon_if()
624 if (handlep->mondevice == NULL) { in add_mon_if()
625 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in add_mon_if()
636 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in add_mon_if()
651 ifindex = iface_get_id(sock_fd, mondevice, handle->errbuf); in del_mon_if()
652 if (ifindex == -1) in del_mon_if()
657 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in del_mon_if()
662 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, in del_mon_if()
666 err = nl_send_auto_complete(state->nl_sock, msg); in del_mon_if()
668 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in del_mon_if()
670 device, mondevice, nl_geterror(-err)); in del_mon_if()
674 err = nl_wait_for_ack(state->nl_sock); in del_mon_if()
676 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in del_mon_if()
678 device, mondevice, nl_geterror(-err)); in del_mon_if()
690 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in del_mon_if()
702 protocol = handle->opt.protocol; in pcap_protocol()
717 if (strcmp(handle->opt.device, "any") == 0) { in pcap_can_set_rfmon_linux()
719 * Monitor mode makes no sense on the "any" device. in pcap_can_set_rfmon_linux()
732 ret = get_mac80211_phydev(handle, handle->opt.device, phydev_path, in pcap_can_set_rfmon_linux()
749 * The author has found no straightforward way to check for support.
754 int fd; in linux_get_stat() local
758 fd = open(buffer, O_RDONLY); in linux_get_stat()
759 if (fd == -1) in linux_get_stat()
762 bytes_read = read(fd, buffer, sizeof(buffer) - 1); in linux_get_stat()
763 close(fd); in linux_get_stat()
764 if (bytes_read == -1) in linux_get_stat()
782 * interface before exiting. The problem can't really be solved without
792 struct pcap_linux *handlep = handle->priv; in pcap_cleanup_linux()
798 if (handlep->must_do_on_close != 0) { in pcap_cleanup_linux()
804 if (handlep->must_do_on_close & MUST_DELETE_MONIF) { in pcap_cleanup_linux()
805 ret = nl80211_init(handle, &nlstate, handlep->device); in pcap_cleanup_linux()
807 ret = del_mon_if(handle, handle->fd, &nlstate, in pcap_cleanup_linux()
808 handlep->device, handlep->mondevice); in pcap_cleanup_linux()
813 "Can't delete monitor interface %s (%s).\n" in pcap_cleanup_linux()
815 handlep->mondevice, handle->errbuf); in pcap_cleanup_linux()
827 if (handle->fd != -1) { in pcap_cleanup_linux()
835 if (handlep->oneshot_buffer != NULL) { in pcap_cleanup_linux()
836 free(handlep->oneshot_buffer); in pcap_cleanup_linux()
837 handlep->oneshot_buffer = NULL; in pcap_cleanup_linux()
840 if (handlep->mondevice != NULL) { in pcap_cleanup_linux()
841 free(handlep->mondevice); in pcap_cleanup_linux()
842 handlep->mondevice = NULL; in pcap_cleanup_linux()
844 if (handlep->device != NULL) { in pcap_cleanup_linux()
845 free(handlep->device); in pcap_cleanup_linux()
846 handlep->device = NULL; in pcap_cleanup_linux()
849 if (handlep->poll_breakloop_fd != -1) { in pcap_cleanup_linux()
850 close(handlep->poll_breakloop_fd); in pcap_cleanup_linux()
851 handlep->poll_breakloop_fd = -1; in pcap_cleanup_linux()
864 * it has *no* version.
873 /* No version information, assume broken. */ in has_broken_tpacket_v3()
874 if (uname(&utsname) == -1) in has_broken_tpacket_v3()
895 * Set the timeout to be used in poll() with memory-mapped packet capture.
903 if (handlep->timeout == 0) { in set_poll_timeout()
906 * XXX - due to a set of (mis)features in the TPACKET_V3 in set_poll_timeout()
908 * with a TPACKET_V3 socket can, if few packets are in set_poll_timeout()
917 if (handlep->tp_version == TPACKET_V3 && broken_tpacket_v3) in set_poll_timeout()
918 handlep->poll_timeout = 1; /* don't block for very long */ in set_poll_timeout()
921 handlep->poll_timeout = -1; /* block forever */ in set_poll_timeout()
922 } else if (handlep->timeout > 0) { in set_poll_timeout()
929 if (handlep->tp_version == TPACKET_V3 && !broken_tpacket_v3) in set_poll_timeout()
930 handlep->poll_timeout = -1; /* block forever, let TPACKET_V3 wake us up */ in set_poll_timeout()
933 handlep->poll_timeout = handlep->timeout; /* block for that amount of time */ in set_poll_timeout()
936 * Non-blocking mode; we call poll() to pick up error in set_poll_timeout()
940 handlep->poll_timeout = 0; in set_poll_timeout()
947 struct pcap_linux *handlep = handle->priv; in pcap_breakloop_linux()
951 if (handlep->poll_breakloop_fd != -1) { in pcap_breakloop_linux()
953 * XXX - pcap_breakloop() doesn't have a return value, in pcap_breakloop_linux()
954 * so we can't indicate an error. in pcap_breakloop_linux()
957 (void)write(handlep->poll_breakloop_fd, &value, sizeof(value)); in pcap_breakloop_linux()
969 struct pcap_linux *handlep = handle->priv; in set_vlan_offset()
971 switch (handle->linktype) { in set_vlan_offset()
978 handlep->vlan_offset = 2 * ETH_ALEN; in set_vlan_offset()
986 handlep->vlan_offset = SLL_HDR_LEN - 2; in set_vlan_offset()
990 handlep->vlan_offset = -1; /* unknown */ in set_vlan_offset()
996 * Get a handle for a live capture from the given device. You can
1001 * modification of that values -- Torsten).
1006 struct pcap_linux *handlep = handle->priv; in pcap_activate_linux()
1013 device = handle->opt.device; in pcap_activate_linux()
1016 * Start out assuming no warnings. in pcap_activate_linux()
1022 * might perform on the device; if not, return a "No such device" in pcap_activate_linux()
1036 handle->errbuf[0] = '\0'; in pcap_activate_linux()
1049 if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN) in pcap_activate_linux()
1050 handle->snapshot = MAXIMUM_SNAPLEN; in pcap_activate_linux()
1052 handlep->device = strdup(device); in pcap_activate_linux()
1053 if (handlep->device == NULL) { in pcap_activate_linux()
1054 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in pcap_activate_linux()
1067 if (handle->opt.promisc) { in pcap_activate_linux()
1068 handle->opt.promisc = 0; in pcap_activate_linux()
1070 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in pcap_activate_linux()
1077 handlep->timeout = handle->opt.timeout; in pcap_activate_linux()
1085 if (handle->opt.promisc) in pcap_activate_linux()
1086 handlep->sysfs_dropped = linux_if_drops(handlep->device); in pcap_activate_linux()
1096 * and handle->errbuf has been set to an appropriate in pcap_activate_linux()
1104 * We got a warning; return that, as handle->errbuf in pcap_activate_linux()
1113 * First, try to allocate an event FD for breakloop, if in pcap_activate_linux()
1114 * we're not going to start in non-blocking mode. in pcap_activate_linux()
1116 if (!handle->opt.nonblock) { in pcap_activate_linux()
1117 handlep->poll_breakloop_fd = eventfd(0, EFD_NONBLOCK); in pcap_activate_linux()
1118 if (handlep->poll_breakloop_fd == -1) { in pcap_activate_linux()
1122 pcapint_fmt_errmsg_for_errno(handle->errbuf, in pcap_activate_linux()
1131 * Try to set up memory-mapped access. in pcap_activate_linux()
1140 * PCAP_ERROR, handle->errbuf contains in pcap_activate_linux()
1148 * We got a warning; return that, as handle->errbuf in pcap_activate_linux()
1159 * Now that we have activated the mmap ring, we can in pcap_activate_linux()
1162 if ((ret = iface_bind(handle->fd, handlep->ifindex, in pcap_activate_linux()
1163 handle->errbuf, pcap_protocol(handle))) != 0) { in pcap_activate_linux()
1168 handle->inject_op = pcap_inject_linux; in pcap_activate_linux()
1169 handle->setfilter_op = pcap_setfilter_linux; in pcap_activate_linux()
1170 handle->setdirection_op = pcap_setdirection_linux; in pcap_activate_linux()
1171 handle->set_datalink_op = pcap_set_datalink_linux; in pcap_activate_linux()
1172 handle->setnonblock_op = pcap_setnonblock_linux; in pcap_activate_linux()
1173 handle->getnonblock_op = pcap_getnonblock_linux; in pcap_activate_linux()
1174 handle->cleanup_op = pcap_cleanup_linux; in pcap_activate_linux()
1175 handle->stats_op = pcap_stats_linux; in pcap_activate_linux()
1176 handle->breakloop_op = pcap_breakloop_linux; in pcap_activate_linux()
1178 switch (handlep->tp_version) { in pcap_activate_linux()
1181 handle->read_op = pcap_read_linux_mmap_v2; in pcap_activate_linux()
1185 handle->read_op = pcap_read_linux_mmap_v3; in pcap_activate_linux()
1189 handle->oneshot_callback = pcapint_oneshot_linux; in pcap_activate_linux()
1190 handle->selectable_fd = handle->fd; in pcap_activate_linux()
1202 handle->linktype = dlt; in pcap_set_datalink_linux()
1206 * new link-layer type. in pcap_set_datalink_linux()
1221 struct pcap_linux *handlep = handle->priv; in linux_check_direction()
1223 if (sll->sll_pkttype == PACKET_OUTGOING) { in linux_check_direction()
1230 if (sll->sll_ifindex == handlep->lo_ifindex) in linux_check_direction()
1234 * If this is an outgoing CAN or CAN FD frame, and in linux_check_direction()
1236 * reject it; CAN devices and drivers, and the CAN in linux_check_direction()
1239 * We don't want duplicate packets, and we can't in linux_check_direction()
1240 * easily distinguish packets looped back by the CAN in linux_check_direction()
1241 * layer than those received by the CAN layer, so we in linux_check_direction()
1244 * We check whether this is a CAN or CAN FD frame in linux_check_direction()
1248 if (sll->sll_hatype == ARPHRD_CAN && in linux_check_direction()
1249 handle->direction != PCAP_D_OUT) in linux_check_direction()
1255 if (handle->direction == PCAP_D_IN) in linux_check_direction()
1262 if (handle->direction == PCAP_D_OUT) in linux_check_direction()
1271 * whether the ifindex in the address is -1, meaning "that device is gone",
1277 struct pcap_linux *handlep = handle->priv; in device_still_exists()
1282 * If handlep->ifindex is -1, the socket isn't bound, meaning in device_still_exists()
1287 if (handlep->ifindex == -1) in device_still_exists()
1294 if (getsockname(handle->fd, (struct sockaddr *) &addr, &addr_len) == -1) { in device_still_exists()
1296 * Error - report an error and return -1. in device_still_exists()
1298 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in device_still_exists()
1300 return (-1); in device_still_exists()
1302 if (addr.sll_ifindex == -1) { in device_still_exists()
1318 struct pcap_linux *handlep = handle->priv; in pcap_inject_linux()
1321 if (handlep->ifindex == -1) { in pcap_inject_linux()
1325 pcapint_strlcpy(handle->errbuf, in pcap_inject_linux()
1328 return (-1); in pcap_inject_linux()
1331 if (handlep->cooked) { in pcap_inject_linux()
1333 * We don't support sending on cooked-mode sockets. in pcap_inject_linux()
1335 * XXX - how do you send on a bound cooked-mode in pcap_inject_linux()
1339 pcapint_strlcpy(handle->errbuf, in pcap_inject_linux()
1342 return (-1); in pcap_inject_linux()
1345 ret = (int)send(handle->fd, buf, size, 0); in pcap_inject_linux()
1346 if (ret == -1) { in pcap_inject_linux()
1347 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in pcap_inject_linux()
1349 return (-1); in pcap_inject_linux()
1360 struct pcap_linux *handlep = handle->priv; in pcap_stats_linux()
1371 * XXX - it's probably OK, in fact, to just use a in pcap_stats_linux()
1388 if (handle->opt.promisc) in pcap_stats_linux()
1391 * XXX - is there any reason to do this by remembering in pcap_stats_linux()
1405 * if the kernel maintains the counts as 64-bit even in pcap_stats_linux()
1406 * on 32-bit platforms, we can handle the real count. in pcap_stats_linux()
1408 * Unfortunately, we can't report 64-bit counts; we in pcap_stats_linux()
1412 * counts are 64-bit, 2) it's easier to add new statistics in pcap_stats_linux()
1418 if_dropped = handlep->sysfs_dropped; in pcap_stats_linux()
1419 handlep->sysfs_dropped = linux_if_drops(handlep->device); in pcap_stats_linux()
1420 handlep->stat.ps_ifdrop += (u_int)(handlep->sysfs_dropped - if_dropped); in pcap_stats_linux()
1426 if (getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS, in pcap_stats_linux()
1427 &kstats, &len) > -1) { in pcap_stats_linux()
1456 * there wasn't room on the socket buffer - but not in pcap_stats_linux()
1463 * We can't make "pcap_stats()" work the same on both in pcap_stats_linux()
1469 * getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS, .... in pcap_stats_linux()
1472 handlep->stat.ps_recv += kstats.tp_packets; in pcap_stats_linux()
1473 handlep->stat.ps_drop += kstats.tp_drops; in pcap_stats_linux()
1474 *stats = handlep->stat; in pcap_stats_linux()
1478 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, errno, in pcap_stats_linux()
1480 return -1; in pcap_stats_linux()
1484 * A PF_PACKET socket can be bound to any network interface.
1498 int fd; in get_if_ioctl_socket() local
1506 * kernel (can it be configured out if you have any networking in get_if_ioctl_socket()
1509 * recent - that feature was introduced in Linux 4.6. in get_if_ioctl_socket()
1511 * AF_UNIX will work *if* you have UNIX-domain sockets configured in get_if_ioctl_socket()
1513 * allow them - some SELinux systems don't allow you create them. in get_if_ioctl_socket()
1532 * asking "why isn't it showing XXX" - or, worse, if you don't in get_if_ioctl_socket()
1534 * capture on them, "why do no interfaces show up?" - when the in get_if_ioctl_socket()
1536 * type require a lot more back-and-forth to debug, as evidenced in get_if_ioctl_socket()
1543 * pre-4.6 kernels are sufficiently rare, that will probably in get_if_ioctl_socket()
1547 * likely to be configured out on a networking-capable system in get_if_ioctl_socket()
1554 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); in get_if_ioctl_socket()
1555 if (fd != -1) { in get_if_ioctl_socket()
1557 * OK, let's make sure we can do an SIOCGIFNAME in get_if_ioctl_socket()
1563 if (ioctl(fd, SIOCGIFNAME, &ifr) == 0 || in get_if_ioctl_socket()
1571 return (fd); in get_if_ioctl_socket()
1579 close(fd); in get_if_ioctl_socket()
1585 fd = socket(AF_UNIX, SOCK_RAW, 0); in get_if_ioctl_socket()
1586 if (fd != -1) { in get_if_ioctl_socket()
1590 return (fd); in get_if_ioctl_socket()
1596 fd = socket(AF_INET6, SOCK_DGRAM, 0); in get_if_ioctl_socket()
1597 if (fd != -1) { in get_if_ioctl_socket()
1598 return (fd); in get_if_ioctl_socket()
1604 * XXX - if that fails, is there anything else we should try? in get_if_ioctl_socket()
1607 * types popular in non-Internet embedded systems? in get_if_ioctl_socket()
1634 if (sock == -1) { in get_if_flags()
1636 "Can't create socket to get ethtool information for %s", in get_if_flags()
1638 return -1; in get_if_flags()
1647 * Wi-Fi, hence wireless. in get_if_flags()
1653 * (We don't use that for Wi-Fi, as it'll report in get_if_flags()
1654 * "Ethernet", i.e. ARPHRD_ETHER, for non-monitor- in get_if_flags()
1659 if (asprintf(&pathstr, "/sys/class/net/%s/type", name) == -1) { in get_if_flags()
1661 "%s: Can't generate path name string for /sys/class/net device", in get_if_flags()
1664 return -1; in get_if_flags()
1681 * XXX - add other types? in get_if_flags()
1718 * XXX - while Valgrind handles SIOCETHTOOL and knows that in get_if_flags()
1725 * in question is fixed, we can remove this. in get_if_flags()
1729 if (ioctl(sock, SIOCETHTOOL, &ifr) == -1) { in get_if_flags()
1739 * XXX - distinguish between "this doesn't in get_if_flags()
1751 * OK, no such device. in get_if_flags()
1768 return -1; in get_if_flags()
1799 get_if_flags) == -1) in pcapint_platform_finddevs()
1800 return (-1); /* failure */ in pcapint_platform_finddevs()
1806 return (-1); in pcapint_platform_finddevs()
1822 handle->direction = d; in pcap_setdirection_linux()
1836 if (asprintf(&pathstr, "/sys/class/net/%s/wireless", device) == -1) { in is_wifi()
1855 * constant, as arguments, and sets "handle->linktype" to the
1856 * appropriate DLT_XXX constant and sets "handle->offset" to
1857 * the appropriate value (to make "handle->offset" plus link-layer
1858 * header length be a multiple of 4, so that the link-layer payload
1859 * will be aligned on a 4-byte boundary when capturing packets).
1863 * If "cooked_ok" is non-zero, we can use DLT_LINUX_SLL and capture
1864 * in cooked mode; otherwise, we can't use cooked mode, so we have
1867 * Sets the link type to -1 if unable to map the type.
1881 * software, some versions of Android give the mobile- in map_arphrd_to_dlt()
1882 * phone-network interface an ARPHRD_ value of in map_arphrd_to_dlt()
1884 * that interface have no link-layer header, and begin in map_arphrd_to_dlt()
1891 if (strncmp(device, cdma_rmnet, sizeof cdma_rmnet - 1) == 0) { in map_arphrd_to_dlt()
1892 handle->linktype = DLT_RAW; in map_arphrd_to_dlt()
1898 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so in map_arphrd_to_dlt()
1899 * that an application can let you choose it, in case you're in map_arphrd_to_dlt()
1903 * DOCSIS frames out on the wire inside the low-level in map_arphrd_to_dlt()
1906 * XXX - are there any other sorts of "fake Ethernet" that in map_arphrd_to_dlt()
1918 * This is not a Wi-Fi device but it could be in map_arphrd_to_dlt()
1932 handle->offset = 2; in map_arphrd_to_dlt()
1937 * It's not a Wi-Fi device; offer DOCSIS. in map_arphrd_to_dlt()
1939 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); in map_arphrd_to_dlt()
1940 if (handle->dlt_list == NULL) { in map_arphrd_to_dlt()
1941 pcapint_fmt_errmsg_for_errno(handle->errbuf, in map_arphrd_to_dlt()
1945 handle->dlt_list[0] = DLT_EN10MB; in map_arphrd_to_dlt()
1946 handle->dlt_list[1] = DLT_DOCSIS; in map_arphrd_to_dlt()
1947 handle->dlt_count = 2; in map_arphrd_to_dlt()
1953 handle->linktype = DLT_EN10MB; in map_arphrd_to_dlt()
1954 handle->offset = 2; in map_arphrd_to_dlt()
1958 handle->linktype = DLT_EN3MB; in map_arphrd_to_dlt()
1962 handle->linktype = DLT_AX25_KISS; in map_arphrd_to_dlt()
1966 handle->linktype = DLT_PRONET; in map_arphrd_to_dlt()
1970 handle->linktype = DLT_CHAOS; in map_arphrd_to_dlt()
1976 handle->linktype = DLT_CAN_SOCKETCAN; in map_arphrd_to_dlt()
1984 handle->linktype = DLT_IEEE802; in map_arphrd_to_dlt()
1985 handle->offset = 2; in map_arphrd_to_dlt()
1989 handle->linktype = DLT_ARCNET_LINUX; in map_arphrd_to_dlt()
1996 handle->linktype = DLT_FDDI; in map_arphrd_to_dlt()
1997 handle->offset = 3; in map_arphrd_to_dlt()
2011 * layer protocols, and no header is prepended to packets. in map_arphrd_to_dlt()
2014 * you can't use the ARPHRD_ type to find out whether in map_arphrd_to_dlt()
2017 * type, there's no ioctl to *get* the encapsulation type. in map_arphrd_to_dlt()
2024 * the frame as LLC-encapsulated or as raw IP (I in map_arphrd_to_dlt()
2034 * Both of those are a nuisance - and, at least on systems in map_arphrd_to_dlt()
2036 * up with those nuisances; instead, we can just capture in map_arphrd_to_dlt()
2037 * in cooked mode. That's what we'll do, if we can. in map_arphrd_to_dlt()
2041 handle->linktype = DLT_LINUX_SLL; in map_arphrd_to_dlt()
2043 handle->linktype = -1; in map_arphrd_to_dlt()
2050 handle->linktype = DLT_IEEE802_11; in map_arphrd_to_dlt()
2057 handle->linktype = DLT_PRISM_HEADER; in map_arphrd_to_dlt()
2064 handle->linktype = DLT_IEEE802_11_RADIO; in map_arphrd_to_dlt()
2069 * Some PPP code in the kernel supplies no link-layer in map_arphrd_to_dlt()
2071 * code supplies PPP link-layer headers ("syncppp.c"); in map_arphrd_to_dlt()
2072 * some PPP code might supply random link-layer in map_arphrd_to_dlt()
2073 * headers (PPP over ISDN - there's code in Ethereal, in map_arphrd_to_dlt()
2074 * for example, to cope with PPP-over-ISDN captures in map_arphrd_to_dlt()
2077 * oddball link-layer headers particular packets have). in map_arphrd_to_dlt()
2080 * in cooked mode, if we can; otherwise, we just treat in map_arphrd_to_dlt()
2081 * it as DLT_RAW, for now - if somebody needs to capture, in map_arphrd_to_dlt()
2083 * link-layer header, they'll have to add code here to in map_arphrd_to_dlt()
2088 handle->linktype = DLT_LINUX_SLL; in map_arphrd_to_dlt()
2091 * XXX - handle ISDN types here? We can't fall in map_arphrd_to_dlt()
2094 * link-layer encapsulation it's using, and map in map_arphrd_to_dlt()
2097 * supply raw IP packets with no link-layer in map_arphrd_to_dlt()
2100 * a link-layer header. in map_arphrd_to_dlt()
2103 * in the link-layer header when capturing on in map_arphrd_to_dlt()
2106 handle->linktype = DLT_RAW; in map_arphrd_to_dlt()
2114 handle->linktype = DLT_C_HDLC; in map_arphrd_to_dlt()
2138 * XXX - should some of those be mapped to DLT_LINUX_SLL in map_arphrd_to_dlt()
2141 handle->linktype = DLT_RAW; in map_arphrd_to_dlt()
2148 handle->linktype = DLT_FRELAY; in map_arphrd_to_dlt()
2152 handle->linktype = DLT_LTALK; in map_arphrd_to_dlt()
2163 * supports RFC 4338-style IP-over-FC, it should define in map_arphrd_to_dlt()
2168 * should Linux ever officially support RFC 4338-style in map_arphrd_to_dlt()
2169 * IP-over-FC. in map_arphrd_to_dlt()
2171 handle->linktype = DLT_IP_OVER_FC; in map_arphrd_to_dlt()
2192 * IP-over-FC: in map_arphrd_to_dlt()
2194 * https://www.mail-archive.com/tcpdump-workers@sandelman.ottawa.on.ca/msg01043.html in map_arphrd_to_dlt()
2199 * on the ethereal-users list) on how to get that DLT_ in map_arphrd_to_dlt()
2208 * one of the ARPHRD_FCxxx types, in "fcLINUXfcp.c" - in map_arphrd_to_dlt()
2209 * change it to set "dev->type" to ARPHRD_FCFABRIC, for in map_arphrd_to_dlt()
2210 * example (the exact value doesn't matter, it can be in map_arphrd_to_dlt()
2222 * any of the ARPHRD_FC* values for IP-over-FC, and in map_arphrd_to_dlt()
2224 * ARP hardware" are supposed to mean (link-layer in map_arphrd_to_dlt()
2233 * IP-over-FC on which somebody wants to capture in map_arphrd_to_dlt()
2236 handle->linktype = DLT_FC_2; in map_arphrd_to_dlt()
2237 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 3); in map_arphrd_to_dlt()
2238 if (handle->dlt_list == NULL) { in map_arphrd_to_dlt()
2239 pcapint_fmt_errmsg_for_errno(handle->errbuf, in map_arphrd_to_dlt()
2243 handle->dlt_list[0] = DLT_FC_2; in map_arphrd_to_dlt()
2244 handle->dlt_list[1] = DLT_FC_2_WITH_FRAME_DELIMS; in map_arphrd_to_dlt()
2245 handle->dlt_list[2] = DLT_IP_OVER_FC; in map_arphrd_to_dlt()
2246 handle->dlt_count = 3; in map_arphrd_to_dlt()
2254 handle->linktype = DLT_LINUX_IRDA; in map_arphrd_to_dlt()
2256 * so let's use "Linux-cooked" mode. Jean II in map_arphrd_to_dlt()
2258 * XXX - this is handled in setup_socket(). */ in map_arphrd_to_dlt()
2259 /* handlep->cooked = 1; */ in map_arphrd_to_dlt()
2269 handle->linktype = DLT_LINUX_LAPD; in map_arphrd_to_dlt()
2277 * No link-layer header; packets are just IP in map_arphrd_to_dlt()
2280 handle->linktype = DLT_RAW; in map_arphrd_to_dlt()
2287 handle->linktype = DLT_IEEE802_15_4_NOFCS; in map_arphrd_to_dlt()
2294 handle->linktype = DLT_NETLINK; in map_arphrd_to_dlt()
2300 * XXX - this is handled in setup_socket(). in map_arphrd_to_dlt()
2302 /* handlep->cooked = 1; */ in map_arphrd_to_dlt()
2309 handle->linktype = DLT_VSOCK; in map_arphrd_to_dlt()
2313 handle->linktype = -1; in map_arphrd_to_dlt()
2327 struct pcap_linux *handlep = handle->priv; in setup_socket()
2328 const char *device = handle->opt.device; in setup_socket()
2344 * The protocol is set to 0. This means we will receive no in setup_socket()
2345 * packets until we "bind" the socket with a non-zero in setup_socket()
2353 if (sock_fd == -1) { in setup_socket()
2360 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in setup_socket()
2361 "Attempt to create packet socket failed - CAP_NET_RAW may be required"); in setup_socket()
2370 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in setup_socket()
2371 "PF_PACKET sockets not supported - is this WSL1?"); in setup_socket()
2378 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in setup_socket()
2386 * "handlep->lo_ifindex" to -1. in setup_socket()
2388 * XXX - can there be more than one device that loops in setup_socket()
2394 handlep->lo_ifindex = iface_get_id(sock_fd, "lo", handle->errbuf); in setup_socket()
2397 * Default value for offset to align link-layer payload in setup_socket()
2398 * on a 4-byte boundary. in setup_socket()
2400 handle->offset = 0; in setup_socket()
2409 handlep->cooked = 0; in setup_socket()
2411 if (handle->opt.rfmon) { in setup_socket()
2414 * Do so before we get the link-layer type, in setup_socket()
2416 * the link-layer type. in setup_socket()
2440 if (handlep->mondevice != NULL) in setup_socket()
2441 device = handlep->mondevice; in setup_socket()
2443 arptype = iface_get_arptype(sock_fd, device, handle->errbuf); in setup_socket()
2453 if (handle->linktype == -1 || in setup_socket()
2454 handle->linktype == DLT_LINUX_SLL || in setup_socket()
2455 handle->linktype == DLT_LINUX_IRDA || in setup_socket()
2456 handle->linktype == DLT_LINUX_LAPD || in setup_socket()
2457 handle->linktype == DLT_NETLINK || in setup_socket()
2458 (handle->linktype == DLT_EN10MB && in setup_socket()
2462 * Unknown interface type (-1), or a in setup_socket()
2465 * or an ISDN device (whose link-layer in setup_socket()
2466 * type we can only determine by using in setup_socket()
2468 * kernels) - reopen in cooked mode. in setup_socket()
2474 if (close(sock_fd) == -1) { in setup_socket()
2475 pcapint_fmt_errmsg_for_errno(handle->errbuf, in setup_socket()
2489 pcapint_fmt_errmsg_for_errno(handle->errbuf, in setup_socket()
2493 handlep->cooked = 1; in setup_socket()
2496 * Get rid of any link-layer type list in setup_socket()
2497 * we allocated - this only supports cooked in setup_socket()
2500 if (handle->dlt_list != NULL) { in setup_socket()
2501 free(handle->dlt_list); in setup_socket()
2502 handle->dlt_list = NULL; in setup_socket()
2503 handle->dlt_count = 0; in setup_socket()
2506 if (handle->linktype == -1) { in setup_socket()
2513 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in setup_socket()
2515 "supported by libpcap - " in setup_socket()
2527 if (handle->linktype != DLT_LINUX_IRDA && in setup_socket()
2528 handle->linktype != DLT_LINUX_LAPD && in setup_socket()
2529 handle->linktype != DLT_NETLINK) in setup_socket()
2530 handle->linktype = DLT_LINUX_SLL; in setup_socket()
2533 handlep->ifindex = iface_get_id(sock_fd, device, in setup_socket()
2534 handle->errbuf); in setup_socket()
2535 if (handlep->ifindex == -1) { in setup_socket()
2540 if ((err = iface_bind(sock_fd, handlep->ifindex, in setup_socket()
2541 handle->errbuf, 0)) != 0) { in setup_socket()
2549 if (handle->opt.rfmon) { in setup_socket()
2561 handlep->cooked = 1; in setup_socket()
2562 handle->linktype = DLT_LINUX_SLL; in setup_socket()
2563 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); in setup_socket()
2564 if (handle->dlt_list == NULL) { in setup_socket()
2565 pcapint_fmt_errmsg_for_errno(handle->errbuf, in setup_socket()
2569 handle->dlt_list[0] = DLT_LINUX_SLL; in setup_socket()
2570 handle->dlt_list[1] = DLT_LINUX_SLL2; in setup_socket()
2571 handle->dlt_count = 2; in setup_socket()
2576 * that we can't transmit; stop doing that only in setup_socket()
2580 handlep->ifindex = -1; in setup_socket()
2587 * promiscuous mode - on some devices (e.g., Orinoco in setup_socket()
2591 * card as a normal networking interface, and on no in setup_socket()
2592 * other platform I know of does starting a non- in setup_socket()
2598 * Hmm, how can we set promiscuous mode on all interfaces? in setup_socket()
2605 if (!is_any_device && handle->opt.promisc) { in setup_socket()
2607 mr.mr_ifindex = handlep->ifindex; in setup_socket()
2610 &mr, sizeof(mr)) == -1) { in setup_socket()
2611 pcapint_fmt_errmsg_for_errno(handle->errbuf, in setup_socket()
2622 * XXX - is enabling auxiliary data necessary, now that we in setup_socket()
2623 * only support memory-mapped capture? The kernel's memory-mapped in setup_socket()
2629 sizeof(val)) == -1 && errno != ENOPROTOOPT) { in setup_socket()
2630 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in setup_socket()
2635 handle->offset += VLAN_TAG_LEN; in setup_socket()
2642 * XXX - we don't know whether this will be DLT_LINUX_SLL in setup_socket()
2647 if (handlep->cooked) { in setup_socket()
2648 if (handle->snapshot < SLL2_HDR_LEN + 1) in setup_socket()
2649 handle->snapshot = SLL2_HDR_LEN + 1; in setup_socket()
2651 handle->bufsize = handle->snapshot; in setup_socket()
2658 if (handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) { in setup_socket()
2662 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "setsockopt: unable to set SO_TIMESTAMPNS"); in setup_socket()
2669 * We've succeeded. Save the socket FD in the pcap structure. in setup_socket()
2671 handle->fd = sock_fd; in setup_socket()
2675 * Can we generate special code for VLAN checks? in setup_socket()
2676 * (XXX - what if we need the special code but it's not supported in setup_socket()
2683 * Yes, we can. Request that we do so. in setup_socket()
2685 handle->bpf_codegen_flags |= BPF_SPECIAL_VLAN_HANDLING; in setup_socket()
2694 * Attempt to setup memory-mapped access.
2696 * On success, returns 0 if there are no warnings or a PCAP_WARNING_ code
2700 * sets handle->errbuf to the appropriate message.
2705 struct pcap_linux *handlep = handle->priv; in setup_mmapped()
2712 handlep->oneshot_buffer = malloc(handle->snapshot); in setup_mmapped()
2713 if (handlep->oneshot_buffer == NULL) { in setup_mmapped()
2714 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in setup_mmapped()
2715 errno, "can't allocate oneshot buffer"); in setup_mmapped()
2719 if (handle->opt.buffer_size == 0) { in setup_mmapped()
2721 handle->opt.buffer_size = 2*1024*1024; in setup_mmapped()
2724 if (status == -1) { in setup_mmapped()
2725 free(handlep->oneshot_buffer); in setup_mmapped()
2726 handlep->oneshot_buffer = NULL; in setup_mmapped()
2732 * Error attempting to enable memory-mapped capture; in setup_mmapped()
2735 free(handlep->oneshot_buffer); in setup_mmapped()
2736 handlep->oneshot_buffer = NULL; in setup_mmapped()
2741 * Success. status has been set either to 0 if there are no in setup_mmapped()
2744 * handle->offset is used to get the current position into the rx ring. in setup_mmapped()
2745 * handle->cc is used to store the ring size. in setup_mmapped()
2757 * Attempt to set the socket to the specified version of the memory-mapped
2761 * supported; return -1 on any other error, and set handle->errbuf.
2766 struct pcap_linux *handlep = handle->priv; in init_tpacket()
2777 if (getsockopt(handle->fd, SOL_PACKET, PACKET_HDRLEN, &val, &len) < 0) { in init_tpacket()
2781 * is not supported. Tell the caller they can try in init_tpacket()
2795 * that memory-mapped capture isn't supported. in init_tpacket()
2798 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in init_tpacket()
2799 …"Kernel doesn't support memory-mapped capture; a 2.6.27 or later 2.x kernel is required, with CONF… in init_tpacket()
2804 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in init_tpacket()
2805 errno, "can't get %s header len on packet socket", in init_tpacket()
2808 return -1; in init_tpacket()
2810 handlep->tp_hdrlen = val; in init_tpacket()
2813 if (setsockopt(handle->fd, SOL_PACKET, PACKET_VERSION, &val, in init_tpacket()
2815 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in init_tpacket()
2816 errno, "can't activate %s on packet socket", version_str); in init_tpacket()
2817 return -1; in init_tpacket()
2819 handlep->tp_version = version; in init_tpacket()
2825 * Attempt to set the socket to version 3 of the memory-mapped header and,
2829 * Return 0 if we succeed and -1 on any other error, and set handle->errbuf.
2848 if (!handle->opt.immediate) { in prepare_tpacket_socket()
2856 if (ret == -1) { in prepare_tpacket_socket()
2861 return -1; in prepare_tpacket_socket()
2884 * OK, the kernel supports memory-mapped capture, but in prepare_tpacket_socket()
2887 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in prepare_tpacket_socket()
2894 return -1; in prepare_tpacket_socket()
2900 * Attempt to set up memory-mapped access.
2902 * On success, returns 0 if there are no warnings or to a PCAP_WARNING_ code
2906 * sets handle->errbuf to the appropriate message.
2911 struct pcap_linux *handlep = handle->priv; in create_ring()
2929 * Start out assuming no warnings. in create_ring()
2942 * that can be changed on an open device, so we reserve in create_ring()
2945 * XXX - we assume that the kernel is still adding in create_ring()
2948 * (Are they doing that for DLT_LINUX_SLL, the link- in create_ring()
2951 * XXX - should we use TPACKET_ALIGN(SLL2_HDR_LEN - 16)? in create_ring()
2953 if (handlep->cooked) in create_ring()
2954 tp_reserve += SLL2_HDR_LEN - 16; in create_ring()
2961 if (setsockopt(handle->fd, SOL_PACKET, PACKET_RESERVE, in create_ring()
2963 pcapint_fmt_errmsg_for_errno(handle->errbuf, in create_ring()
2969 switch (handlep->tp_version) { in create_ring()
2974 * TShark, dumpcap or 64K, the value that "-s 0" has given for in create_ring()
2985 * maximum radiotap header length is device-dependent. in create_ring()
2988 * there's no metadata header, and the link-layer header is in create_ring()
2989 * fixed length. We can get the maximum packet size by in create_ring()
2997 * of packets that we can receive.) in create_ring()
3000 * enabled, we can get reassembled/aggregated packets larger in create_ring()
3003 frame_size = handle->snapshot; in create_ring()
3004 if (handle->linktype == DLT_EN10MB) { in create_ring()
3009 mtu = iface_get_mtu(handle->fd, handle->opt.device, in create_ring()
3010 handle->errbuf); in create_ring()
3011 if (mtu == -1) in create_ring()
3014 if (offload == -1) in create_ring()
3027 * in linux-2.6/net/packet/af_packet.c in create_ring()
3030 if (getsockopt(handle->fd, SOL_SOCKET, SO_TYPE, &sk_type, in create_ring()
3032 pcapint_fmt_errmsg_for_errno(handle->errbuf, in create_ring()
3039 * in: packet_snd() in linux-2.6/net/packet/af_packet.c in create_ring()
3040 * then packet_alloc_skb() in linux-2.6/net/packet/af_packet.c in create_ring()
3041 * then sock_alloc_send_pskb() in linux-2.6/net/core/sock.c in create_ring()
3042 * but I see no way to get those sizes in userspace, in create_ring()
3045 * the kernel part of linux-2.6/include/linux/netdevice.h in create_ring()
3046 * which goes up to 128+48=176; since pcap-linux.c in create_ring()
3051 tp_hdrlen = TPACKET_ALIGN(handlep->tp_hdrlen) + sizeof(struct sockaddr_ll) ; in create_ring()
3055 * linux-2.6/Documentation/networking/packet_mmap.txt in create_ring()
3057 * "- Gap, chosen so that packet data (Start+tp_net) in create_ring()
3060 /* NOTE: in linux-2.6/include/linux/skbuff.h: in create_ring()
3064 macoff = netoff - maclen; in create_ring()
3073 req.tp_frame_nr = (handle->opt.buffer_size + req.tp_frame_size - 1)/req.tp_frame_size; in create_ring()
3079 * contain multiple variable-sized frames. in create_ring()
3082 * enough room for at least one reasonably-sized packet in create_ring()
3092 req.tp_frame_nr = (handle->opt.buffer_size + req.tp_frame_size - 1)/req.tp_frame_size; in create_ring()
3096 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in create_ring()
3098 handlep->tp_version); in create_ring()
3104 * The max block size allowed by the kernel is arch-dependent and in create_ring()
3133 if (handle->opt.tstamp_type == PCAP_TSTAMP_ADAPTER || in create_ring()
3134 handle->opt.tstamp_type == PCAP_TSTAMP_ADAPTER_UNSYNCED) { in create_ring()
3148 pcapint_strlcpy(ifr.ifr_name, handle->opt.device, sizeof(ifr.ifr_name)); in create_ring()
3154 if (ioctl(handle->fd, SIOCSHWTSTAMP, &ifr) < 0) { in create_ring()
3161 * with the appropriate privileges - in create_ring()
3162 * and, if they can't, shouldn't in create_ring()
3165 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in create_ring()
3166 "Attempt to set hardware timestamp failed - CAP_NET_ADMIN may be required"); in create_ring()
3189 pcapint_fmt_errmsg_for_errno(handle->errbuf, in create_ring()
3200 if (handle->opt.tstamp_type == PCAP_TSTAMP_ADAPTER) { in create_ring()
3208 * PCAP_TSTAMP_ADAPTER_UNSYNCED - hardware in create_ring()
3214 if (setsockopt(handle->fd, SOL_PACKET, PACKET_TIMESTAMP, in create_ring()
3216 pcapint_fmt_errmsg_for_errno(handle->errbuf, in create_ring()
3218 "can't set PACKET_TIMESTAMP"); in create_ring()
3233 /* timeout value to retire block - use the configured buffering timeout, or default if <0. */ in create_ring()
3234 if (handlep->timeout > 0) { in create_ring()
3236 req.tp_retire_blk_tov = handlep->timeout; in create_ring()
3237 } else if (handlep->timeout == 0) { in create_ring()
3249 * XXX - this is not valid; use 0, meaning "have the in create_ring()
3256 /* Rx ring - feature request bits - none (rxhash will not be filled) */ in create_ring()
3260 if (setsockopt(handle->fd, SOL_PACKET, PACKET_RX_RING, in create_ring()
3267 * We used to reduce this by half -- do 5% instead. in create_ring()
3273 req.tp_frame_nr -= 1; in create_ring()
3275 req.tp_frame_nr -= req.tp_frame_nr/20; in create_ring()
3278 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in create_ring()
3279 errno, "can't create rx ring on packet socket"); in create_ring()
3284 handlep->mmapbuflen = req.tp_block_nr * req.tp_block_size; in create_ring()
3285 handlep->mmapbuf = mmap(0, handlep->mmapbuflen, in create_ring()
3286 PROT_READ|PROT_WRITE, MAP_SHARED, handle->fd, 0); in create_ring()
3287 if (handlep->mmapbuf == MAP_FAILED) { in create_ring()
3288 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in create_ring()
3289 errno, "can't mmap rx ring"); in create_ring()
3297 handle->cc = req.tp_frame_nr; in create_ring()
3298 handle->buffer = malloc(handle->cc * sizeof(union thdr *)); in create_ring()
3299 if (!handle->buffer) { in create_ring()
3300 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in create_ring()
3301 errno, "can't allocate ring of frame headers"); in create_ring()
3308 handle->offset = 0; in create_ring()
3310 u_char *base = &handlep->mmapbuf[i*req.tp_block_size]; in create_ring()
3311 for (j=0; j<frames_per_block; ++j, ++handle->offset) { in create_ring()
3317 handle->bufsize = req.tp_frame_size; in create_ring()
3318 handle->offset = 0; in create_ring()
3326 struct pcap_linux *handlep = handle->priv; in destroy_ring()
3330 * We don't check for setsockopt failure, as 1) we can't recover in destroy_ring()
3336 (void)setsockopt(handle->fd, SOL_PACKET, PACKET_RX_RING, in destroy_ring()
3340 if (handlep->mmapbuf) { in destroy_ring()
3341 /* do not test for mmap failure, as we can't recover from any error */ in destroy_ring()
3342 (void)munmap(handlep->mmapbuf, handlep->mmapbuflen); in destroy_ring()
3343 handlep->mmapbuf = NULL; in destroy_ring()
3348 * Special one-shot callback, used for pcap_next() and pcap_next_ex(),
3369 pcap_t *handle = sp->pd; in pcapint_oneshot_linux()
3370 struct pcap_linux *handlep = handle->priv; in pcapint_oneshot_linux()
3372 *sp->hdr = *h; in pcapint_oneshot_linux()
3373 memcpy(handlep->oneshot_buffer, bytes, h->caplen); in pcapint_oneshot_linux()
3374 *sp->pkt = handlep->oneshot_buffer; in pcapint_oneshot_linux()
3380 struct pcap_linux *handlep = handle->priv; in pcap_getnonblock_linux()
3383 return (handlep->timeout<0); in pcap_getnonblock_linux()
3389 struct pcap_linux *handlep = handle->priv; in pcap_setnonblock_linux()
3395 if (pcapint_setnonblock_fd(handle, nonblock) == -1) in pcap_setnonblock_linux()
3396 return -1; in pcap_setnonblock_linux()
3404 * We're setting the mode to non-blocking mode. in pcap_setnonblock_linux()
3406 if (handlep->timeout >= 0) { in pcap_setnonblock_linux()
3409 * non-blocking mode. in pcap_setnonblock_linux()
3411 handlep->timeout = ~handlep->timeout; in pcap_setnonblock_linux()
3413 if (handlep->poll_breakloop_fd != -1) { in pcap_setnonblock_linux()
3415 close(handlep->poll_breakloop_fd); in pcap_setnonblock_linux()
3416 handlep->poll_breakloop_fd = -1; in pcap_setnonblock_linux()
3422 if (handlep->poll_breakloop_fd == -1) { in pcap_setnonblock_linux()
3424 if ( ( handlep->poll_breakloop_fd = eventfd(0, EFD_NONBLOCK) ) == -1 ) { in pcap_setnonblock_linux()
3425 pcapint_fmt_errmsg_for_errno(handle->errbuf, in pcap_setnonblock_linux()
3428 return -1; in pcap_setnonblock_linux()
3431 if (handlep->timeout < 0) { in pcap_setnonblock_linux()
3432 handlep->timeout = ~handlep->timeout; in pcap_setnonblock_linux()
3446 struct pcap_linux *handlep = handle->priv; in pcap_get_ring_frame_status()
3450 switch (handlep->tp_version) { in pcap_get_ring_frame_status()
3452 return __atomic_load_n(&h.h2->tp_status, __ATOMIC_ACQUIRE); in pcap_get_ring_frame_status()
3456 return __atomic_load_n(&h.h3->hdr.bh1.block_status, __ATOMIC_ACQUIRE); in pcap_get_ring_frame_status()
3469 struct pcap_linux *handlep = handle->priv; in pcap_wait_for_frames_mmap()
3475 pollinfo[0].fd = handle->fd; in pcap_wait_for_frames_mmap()
3477 if ( handlep->poll_breakloop_fd == -1 ) { in pcap_wait_for_frames_mmap()
3488 pollinfo[1].fd = handlep->poll_breakloop_fd; in pcap_wait_for_frames_mmap()
3498 * In non-blocking mode, we must still do one poll() to catch in pcap_wait_for_frames_mmap()
3505 * configured down. Unfortunately, there's no guarantee that in pcap_wait_for_frames_mmap()
3513 * to -1, and the second indication won't cause a wakeup (because in pcap_wait_for_frames_mmap()
3516 * interface index to -1; in pcap_wait_for_frames_mmap()
3520 * set to -1 after the wakeup, so there's a small but non-zero in pcap_wait_for_frames_mmap()
3523 * gets set to -1, so it'll get the old interface index. in pcap_wait_for_frames_mmap()
3534 * Yes, we do this even in non-blocking mode, as it's in pcap_wait_for_frames_mmap()
3538 * The timeout is 0 in non-blocking mode, so poll() in pcap_wait_for_frames_mmap()
3541 timeout = handlep->poll_timeout; in pcap_wait_for_frames_mmap()
3547 * gone away or not, do a poll() with a 1-millisecond timeout, in pcap_wait_for_frames_mmap()
3552 if (handlep->netdown) { in pcap_wait_for_frames_mmap()
3562 pcapint_fmt_errmsg_for_errno(handle->errbuf, in pcap_wait_for_frames_mmap()
3564 "can't poll on packet socket"); in pcap_wait_for_frames_mmap()
3572 if (handle->break_loop) { in pcap_wait_for_frames_mmap()
3573 handle->break_loop = 0; in pcap_wait_for_frames_mmap()
3594 * "you can read on this descriptor" on in pcap_wait_for_frames_mmap()
3598 snprintf(handle->errbuf, in pcap_wait_for_frames_mmap()
3604 snprintf(handle->errbuf, in pcap_wait_for_frames_mmap()
3617 if (getsockopt(handle->fd, SOL_SOCKET, in pcap_wait_for_frames_mmap()
3618 SO_ERROR, &err, &errlen) == -1) { in pcap_wait_for_frames_mmap()
3648 * they can call us and we in pcap_wait_for_frames_mmap()
3649 * can do the checks. in pcap_wait_for_frames_mmap()
3651 handlep->netdown = 1; in pcap_wait_for_frames_mmap()
3652 handle->required_select_timeout = &netdown_timeout; in pcap_wait_for_frames_mmap()
3659 snprintf(handle->errbuf, in pcap_wait_for_frames_mmap()
3664 pcapint_fmt_errmsg_for_errno(handle->errbuf, in pcap_wait_for_frames_mmap()
3683 nread = read(handlep->poll_breakloop_fd, &value, in pcap_wait_for_frames_mmap()
3685 if (nread == -1) { in pcap_wait_for_frames_mmap()
3686 pcapint_fmt_errmsg_for_errno(handle->errbuf, in pcap_wait_for_frames_mmap()
3689 "Error reading from event FD"); in pcap_wait_for_frames_mmap()
3696 * 2^31-1 bytes, so the return value is in pcap_wait_for_frames_mmap()
3697 * either -1 or a value between 0 in pcap_wait_for_frames_mmap()
3698 * and 2^31-1, so it's non-negative. in pcap_wait_for_frames_mmap()
3703 * humans reading the code. :-) in pcap_wait_for_frames_mmap()
3711 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in pcap_wait_for_frames_mmap()
3712 "Short read from event FD: expected %zu, got %zd", in pcap_wait_for_frames_mmap()
3722 if (handle->break_loop) { in pcap_wait_for_frames_mmap()
3723 handle->break_loop = 0; in pcap_wait_for_frames_mmap()
3734 * are no packets waiting to read in pcap_wait_for_frames_mmap()
3740 * are no packets waiting to read in pcap_wait_for_frames_mmap()
3752 if (handlep->netdown) { in pcap_wait_for_frames_mmap()
3758 * XXX - we should really return an in pcap_wait_for_frames_mmap()
3764 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in pcap_wait_for_frames_mmap()
3773 pcapint_strlcpy(ifr.ifr_name, handlep->device, in pcap_wait_for_frames_mmap()
3775 if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) { in pcap_wait_for_frames_mmap()
3780 * XXX - see above comment. in pcap_wait_for_frames_mmap()
3782 snprintf(handle->errbuf, in pcap_wait_for_frames_mmap()
3787 pcapint_fmt_errmsg_for_errno(handle->errbuf, in pcap_wait_for_frames_mmap()
3789 "%s: Can't get flags", in pcap_wait_for_frames_mmap()
3790 handlep->device); in pcap_wait_for_frames_mmap()
3797 * Cancel the ENETDOWN indication - we in pcap_wait_for_frames_mmap()
3800 * away - and revert to "no required select in pcap_wait_for_frames_mmap()
3803 handlep->netdown = 0; in pcap_wait_for_frames_mmap()
3804 handle->required_select_timeout = NULL; in pcap_wait_for_frames_mmap()
3809 * If we're in non-blocking mode, just quit now, rather in pcap_wait_for_frames_mmap()
3811 * time out if there's no indication on any descriptor. in pcap_wait_for_frames_mmap()
3813 if (handlep->poll_timeout == 0) in pcap_wait_for_frames_mmap()
3834 struct pcap_linux *handlep = handle->priv; in pcap_handle_packet_mmap()
3842 if (tp_mac + tp_snaplen > handle->bufsize) { in pcap_handle_packet_mmap()
3846 if (uname(&utsname) != -1) { in pcap_handle_packet_mmap()
3847 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in pcap_handle_packet_mmap()
3851 tp_mac, tp_snaplen, handle->bufsize, in pcap_handle_packet_mmap()
3855 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in pcap_handle_packet_mmap()
3858 tp_mac, tp_snaplen, handle->bufsize); in pcap_handle_packet_mmap()
3860 return -1; in pcap_handle_packet_mmap()
3870 * the filter when the ring became empty, but it can possibly in pcap_handle_packet_mmap()
3875 sll = (void *)(frame + TPACKET_ALIGN(handlep->tp_hdrlen)); in pcap_handle_packet_mmap()
3876 if (handlep->cooked) { in pcap_handle_packet_mmap()
3877 if (handle->linktype == DLT_LINUX_SLL2) { in pcap_handle_packet_mmap()
3887 bp -= SLL2_HDR_LEN; in pcap_handle_packet_mmap()
3897 TPACKET_ALIGN(handlep->tp_hdrlen) + in pcap_handle_packet_mmap()
3899 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in pcap_handle_packet_mmap()
3900 "cooked-mode frame doesn't have room for sll header"); in pcap_handle_packet_mmap()
3901 return -1; in pcap_handle_packet_mmap()
3908 hdrp->sll2_protocol = sll->sll_protocol; in pcap_handle_packet_mmap()
3909 hdrp->sll2_reserved_mbz = 0; in pcap_handle_packet_mmap()
3910 hdrp->sll2_if_index = htonl(sll->sll_ifindex); in pcap_handle_packet_mmap()
3911 hdrp->sll2_hatype = htons(sll->sll_hatype); in pcap_handle_packet_mmap()
3912 hdrp->sll2_pkttype = sll->sll_pkttype; in pcap_handle_packet_mmap()
3913 hdrp->sll2_halen = sll->sll_halen; in pcap_handle_packet_mmap()
3914 memcpy(hdrp->sll2_addr, sll->sll_addr, SLL_ADDRLEN); in pcap_handle_packet_mmap()
3927 bp -= SLL_HDR_LEN; in pcap_handle_packet_mmap()
3937 TPACKET_ALIGN(handlep->tp_hdrlen) + in pcap_handle_packet_mmap()
3939 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in pcap_handle_packet_mmap()
3940 "cooked-mode frame doesn't have room for sll header"); in pcap_handle_packet_mmap()
3941 return -1; in pcap_handle_packet_mmap()
3948 hdrp->sll_pkttype = htons(sll->sll_pkttype); in pcap_handle_packet_mmap()
3949 hdrp->sll_hatype = htons(sll->sll_hatype); in pcap_handle_packet_mmap()
3950 hdrp->sll_halen = htons(sll->sll_halen); in pcap_handle_packet_mmap()
3951 memcpy(hdrp->sll_addr, sll->sll_addr, SLL_ADDRLEN); in pcap_handle_packet_mmap()
3952 hdrp->sll_protocol = sll->sll_protocol; in pcap_handle_packet_mmap()
3958 * If this is a packet from a CAN device, so that in pcap_handle_packet_mmap()
3959 * sll->sll_hatype is ARPHRD_CAN, then, as we're in pcap_handle_packet_mmap()
3960 * not capturing in cooked mode, its link-layer in pcap_handle_packet_mmap()
3965 if (sll->sll_hatype == ARPHRD_CAN) { in pcap_handle_packet_mmap()
3967 uint16_t protocol = ntohs(sll->sll_protocol); in pcap_handle_packet_mmap()
3971 * If it's one of the known CAN protocol types, in pcap_handle_packet_mmap()
3973 * that a program can tell what type of frame in pcap_handle_packet_mmap()
3979 * of the CAN classic/CAN FD header; in pcap_handle_packet_mmap()
3982 * of the CAN XL header, which overlaps in pcap_handle_packet_mmap()
3983 * the payload_length field of the CAN in pcap_handle_packet_mmap()
3984 * classic/CAN FD header. in pcap_handle_packet_mmap()
3990 * CAN classic. in pcap_handle_packet_mmap()
4001 * will appear to be a CAN classic frame. in pcap_handle_packet_mmap()
4003 canhdr->payload_length &= ~CANXL_XLF; in pcap_handle_packet_mmap()
4004 canhdr->fd_flags = 0; in pcap_handle_packet_mmap()
4005 canhdr->reserved1 = 0; in pcap_handle_packet_mmap()
4006 canhdr->reserved2 = 0; in pcap_handle_packet_mmap()
4014 * will appear to be a CAN FD frame. in pcap_handle_packet_mmap()
4016 canhdr->payload_length &= ~CANXL_XLF; in pcap_handle_packet_mmap()
4017 canhdr->fd_flags |= CANFD_FDF; in pcap_handle_packet_mmap()
4022 * a program reading this can assume that in pcap_handle_packet_mmap()
4034 * the reserved fields for FD frames. in pcap_handle_packet_mmap()
4036 canhdr->fd_flags &= (CANFD_FDF|CANFD_ESI|CANFD_BRS); in pcap_handle_packet_mmap()
4037 canhdr->reserved1 = 0; in pcap_handle_packet_mmap()
4038 canhdr->reserved2 = 0; in pcap_handle_packet_mmap()
4043 * CAN XL frame. in pcap_handle_packet_mmap()
4048 * CAN XL frame. in pcap_handle_packet_mmap()
4050 canhdr->payload_length |= CANXL_XLF; in pcap_handle_packet_mmap()
4055 * Put multi-byte header fields in a byte-order in pcap_handle_packet_mmap()
4056 *-independent format. in pcap_handle_packet_mmap()
4058 if (canhdr->payload_length & CANXL_XLF) { in pcap_handle_packet_mmap()
4060 * This is a CAN XL frame. in pcap_handle_packet_mmap()
4063 * the Priority ID/VCID field in big-- in pcap_handle_packet_mmap()
4065 * and Acceptance Field in little-endian byte in pcap_handle_packet_mmap()
4066 * order. but capturing on a CAN device in pcap_handle_packet_mmap()
4072 * into big-endian byte order is that in pcap_handle_packet_mmap()
4074 * CAN XL, treated it as the CAN ID in pcap_handle_packet_mmap()
4075 * field and put it into big-endian in pcap_handle_packet_mmap()
4077 * break code that understands CAN XL in pcap_handle_packet_mmap()
4079 * being big-endian. in pcap_handle_packet_mmap()
4081 * The other fields are put in little- in pcap_handle_packet_mmap()
4083 * libpcap code, ignorant of CAN XL, in pcap_handle_packet_mmap()
4085 * processors on which the CAN XL in pcap_handle_packet_mmap()
4087 * to be little-endian processors. in pcap_handle_packet_mmap()
4093 * We're capturing on a little-endian in pcap_handle_packet_mmap()
4095 * field into big-endian byte order, and in pcap_handle_packet_mmap()
4097 * field in little-endian byte order. in pcap_handle_packet_mmap()
4099 /* Byte-swap priority/VCID. */ in pcap_handle_packet_mmap()
4100 canxl_hdr->priority_vcid = SWAPLONG(canxl_hdr->priority_vcid); in pcap_handle_packet_mmap()
4103 * We're capturing on a big-endian in pcap_handle_packet_mmap()
4105 * priority/VCID field alone, and byte-swap in pcap_handle_packet_mmap()
4107 * fields to little-endian. in pcap_handle_packet_mmap()
4109 /* Byte-swap the payload length */ in pcap_handle_packet_mmap()
4110 canxl_hdr->payload_length = SWAPSHORT(canxl_hdr->payload_length); in pcap_handle_packet_mmap()
4113 * Byte-swap the acceptance field. in pcap_handle_packet_mmap()
4115 * XXX - is it just a 4-octet string, in pcap_handle_packet_mmap()
4118 canxl_hdr->acceptance_field = SWAPLONG(canxl_hdr->acceptance_field); in pcap_handle_packet_mmap()
4124 * CAN or CAN FD frame. in pcap_handle_packet_mmap()
4127 * the CAN ID and flags in network byte in pcap_handle_packet_mmap()
4128 * order, but capturing on a CAN device in pcap_handle_packet_mmap()
4132 canhdr->can_id = htonl(canhdr->can_id); in pcap_handle_packet_mmap()
4137 if (handlep->filter_in_userland && handle->fcode.bf_insns) { in pcap_handle_packet_mmap()
4143 if (pcapint_filter_with_aux_data(handle->fcode.bf_insns, in pcap_handle_packet_mmap()
4161 if (handlep->cooked) { in pcap_handle_packet_mmap()
4163 if (handle->linktype == DLT_LINUX_SLL2) { in pcap_handle_packet_mmap()
4173 handlep->vlan_offset != -1 && in pcap_handle_packet_mmap()
4174 tp_snaplen >= (unsigned int) handlep->vlan_offset) in pcap_handle_packet_mmap()
4183 bp -= VLAN_TAG_LEN; in pcap_handle_packet_mmap()
4184 memmove(bp, bp + VLAN_TAG_LEN, handlep->vlan_offset); in pcap_handle_packet_mmap()
4189 tag = (struct vlan_tag *)(bp + handlep->vlan_offset); in pcap_handle_packet_mmap()
4190 tag->vlan_tpid = htons(tp_vlan_tpid); in pcap_handle_packet_mmap()
4191 tag->vlan_tci = htons(tp_vlan_tci); in pcap_handle_packet_mmap()
4203 * if there's no filter program, the kernel won't cut in pcap_handle_packet_mmap()
4206 * Trim the snapshot length to be no longer than the in pcap_handle_packet_mmap()
4209 * XXX - an alternative is to put a filter, consisting in pcap_handle_packet_mmap()
4214 * the memory-mapped buffer. in pcap_handle_packet_mmap()
4216 if (pcaphdr.caplen > (bpf_u_int32)handle->snapshot) in pcap_handle_packet_mmap()
4217 pcaphdr.caplen = handle->snapshot; in pcap_handle_packet_mmap()
4229 struct pcap_linux *handlep = handle->priv; in pcap_read_linux_mmap_v2()
4248 * This can conceivably process more than INT_MAX packets, in pcap_read_linux_mmap_v2()
4253 * return a too-low count. in pcap_read_linux_mmap_v2()
4276 h.h2->tp_len, in pcap_read_linux_mmap_v2()
4277 h.h2->tp_mac, in pcap_read_linux_mmap_v2()
4278 h.h2->tp_snaplen, in pcap_read_linux_mmap_v2()
4279 h.h2->tp_sec, in pcap_read_linux_mmap_v2()
4280 … handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO ? h.h2->tp_nsec : h.h2->tp_nsec / 1000, in pcap_read_linux_mmap_v2()
4282 h.h2->tp_vlan_tci, in pcap_read_linux_mmap_v2()
4297 if (handlep->blocks_to_filter_in_userland > 0) { in pcap_read_linux_mmap_v2()
4298 handlep->blocks_to_filter_in_userland--; in pcap_read_linux_mmap_v2()
4299 if (handlep->blocks_to_filter_in_userland == 0) { in pcap_read_linux_mmap_v2()
4301 * No more blocks need to be filtered in pcap_read_linux_mmap_v2()
4304 handlep->filter_in_userland = 0; in pcap_read_linux_mmap_v2()
4309 if (++handle->offset >= handle->cc) in pcap_read_linux_mmap_v2()
4310 handle->offset = 0; in pcap_read_linux_mmap_v2()
4313 if (handle->break_loop) { in pcap_read_linux_mmap_v2()
4314 handle->break_loop = 0; in pcap_read_linux_mmap_v2()
4326 struct pcap_linux *handlep = handle->priv; in pcap_read_linux_mmap_v3()
4332 if (handlep->current_packet == NULL) { in pcap_read_linux_mmap_v3()
4348 if (pkts == 0 && handlep->timeout == 0) { in pcap_read_linux_mmap_v3()
4356 * This can conceivably process more than INT_MAX packets, in pcap_read_linux_mmap_v3()
4361 * return a too-low count. in pcap_read_linux_mmap_v3()
4373 if (handlep->current_packet == NULL) { in pcap_read_linux_mmap_v3()
4378 handlep->current_packet = h.raw + h.h3->hdr.bh1.offset_to_first_pkt; in pcap_read_linux_mmap_v3()
4379 handlep->packets_left = h.h3->hdr.bh1.num_pkts; in pcap_read_linux_mmap_v3()
4381 packets_to_read = handlep->packets_left; in pcap_read_linux_mmap_v3()
4383 if (packets_to_read > (max_packets - pkts)) { in pcap_read_linux_mmap_v3()
4391 packets_to_read = max_packets - pkts; in pcap_read_linux_mmap_v3()
4394 while (packets_to_read-- && !handle->break_loop) { in pcap_read_linux_mmap_v3()
4395 struct tpacket3_hdr* tp3_hdr = (struct tpacket3_hdr*) handlep->current_packet; in pcap_read_linux_mmap_v3()
4400 handlep->current_packet, in pcap_read_linux_mmap_v3()
4401 tp3_hdr->tp_len, in pcap_read_linux_mmap_v3()
4402 tp3_hdr->tp_mac, in pcap_read_linux_mmap_v3()
4403 tp3_hdr->tp_snaplen, in pcap_read_linux_mmap_v3()
4404 tp3_hdr->tp_sec, in pcap_read_linux_mmap_v3()
4405 …handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO ? tp3_hdr->tp_nsec : tp3_hdr->tp_nsec /… in pcap_read_linux_mmap_v3()
4406 VLAN_VALID(tp3_hdr, &tp3_hdr->hv1), in pcap_read_linux_mmap_v3()
4407 tp3_hdr->hv1.tp_vlan_tci, in pcap_read_linux_mmap_v3()
4408 VLAN_TPID(tp3_hdr, &tp3_hdr->hv1)); in pcap_read_linux_mmap_v3()
4412 handlep->current_packet = NULL; in pcap_read_linux_mmap_v3()
4415 handlep->current_packet += tp3_hdr->tp_next_offset; in pcap_read_linux_mmap_v3()
4416 handlep->packets_left--; in pcap_read_linux_mmap_v3()
4419 if (handlep->packets_left <= 0) { in pcap_read_linux_mmap_v3()
4428 if (handlep->blocks_to_filter_in_userland > 0) { in pcap_read_linux_mmap_v3()
4429 handlep->blocks_to_filter_in_userland--; in pcap_read_linux_mmap_v3()
4430 if (handlep->blocks_to_filter_in_userland == 0) { in pcap_read_linux_mmap_v3()
4432 * No more blocks need to be filtered in pcap_read_linux_mmap_v3()
4435 handlep->filter_in_userland = 0; in pcap_read_linux_mmap_v3()
4440 if (++handle->offset >= handle->cc) in pcap_read_linux_mmap_v3()
4441 handle->offset = 0; in pcap_read_linux_mmap_v3()
4443 handlep->current_packet = NULL; in pcap_read_linux_mmap_v3()
4447 if (handle->break_loop) { in pcap_read_linux_mmap_v3()
4448 handle->break_loop = 0; in pcap_read_linux_mmap_v3()
4452 if (pkts == 0 && handlep->timeout == 0) { in pcap_read_linux_mmap_v3()
4473 return -1; in pcap_setfilter_linux()
4475 pcapint_strlcpy(handle->errbuf, "setfilter: No filter specified", in pcap_setfilter_linux()
4477 return -1; in pcap_setfilter_linux()
4480 handlep = handle->priv; in pcap_setfilter_linux()
4486 return -1; in pcap_setfilter_linux()
4492 handlep->filter_in_userland = 1; in pcap_setfilter_linux()
4497 if (handle->fcode.bf_len > USHRT_MAX) { in pcap_setfilter_linux()
4500 * I have yet to see BPF-Code with that much in pcap_setfilter_linux()
4517 * instructions with non-zero operands have MAXIMUM_SNAPLEN in pcap_setfilter_linux()
4518 * as the operand if we're not capturing in memory-mapped in pcap_setfilter_linux()
4519 * mode, and so that, if we're in cooked mode, all memory- in pcap_setfilter_linux()
4521 * references to the link-layer header and assume that the in pcap_setfilter_linux()
4522 * link-layer payload begins at 0; "fix_program()" will do in pcap_setfilter_linux()
4527 case -1: in pcap_setfilter_linux()
4532 * return -1 for that reason.) in pcap_setfilter_linux()
4534 return -1; in pcap_setfilter_linux()
4538 * The program performed checks that we can't make in pcap_setfilter_linux()
4566 * If there are no other fields, then: in pcap_setfilter_linux()
4581 * Installation succeeded - using kernel filter, in pcap_setfilter_linux()
4584 handlep->filter_in_userland = 0; in pcap_setfilter_linux()
4586 else if (err == -1) /* Non-fatal error */ in pcap_setfilter_linux()
4620 if (handlep->filter_in_userland) { in pcap_setfilter_linux()
4621 if (reset_kernel_filter(handle) == -1) { in pcap_setfilter_linux()
4622 pcapint_fmt_errmsg_for_errno(handle->errbuf, in pcap_setfilter_linux()
4624 "can't remove kernel filter"); in pcap_setfilter_linux()
4625 err = -2; /* fatal error */ in pcap_setfilter_linux()
4635 if (err == -2) in pcap_setfilter_linux()
4637 return -1; in pcap_setfilter_linux()
4643 if (handlep->filter_in_userland) in pcap_setfilter_linux()
4655 offset = handle->offset; in pcap_setfilter_linux()
4656 if (--offset < 0) in pcap_setfilter_linux()
4657 offset = handle->cc - 1; in pcap_setfilter_linux()
4658 for (n=0; n < handle->cc; ++n) { in pcap_setfilter_linux()
4659 if (--offset < 0) in pcap_setfilter_linux()
4660 offset = handle->cc - 1; in pcap_setfilter_linux()
4672 * XXX - could there be more than one block added in in pcap_setfilter_linux()
4675 * XXX - is there a way to avoid that race, e.g. somehow in pcap_setfilter_linux()
4680 n--; in pcap_setfilter_linux()
4688 * not to be zero - n, above, couldn't be set to a in pcap_setfilter_linux()
4689 * value > handle->cc, and if it were equal to in pcap_setfilter_linux()
4690 * handle->cc, it wouldn't be zero, and thus would in pcap_setfilter_linux()
4691 * be decremented to handle->cc - 1.) in pcap_setfilter_linux()
4693 handlep->blocks_to_filter_in_userland = handle->cc - n; in pcap_setfilter_linux()
4694 handlep->filter_in_userland = 1; in pcap_setfilter_linux()
4701 * -1 on failure.
4704 iface_get_id(int fd, const char *device, char *ebuf) in iface_get_id() argument
4711 if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) { in iface_get_id()
4714 return -1; in iface_get_id()
4721 * Bind the socket associated with FD to the given device.
4725 iface_bind(int fd, int ifindex, char *ebuf, int protocol) in iface_bind() argument
4736 if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) { in iface_bind()
4740 * the application can report that rather than in iface_bind()
4764 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { in iface_bind()
4773 * the application can report that rather than in iface_bind()
4790 * If we have libnl, try to create a new monitor-mode device and
4797 struct pcap_linux *handlep = handle->priv; in enter_rfmon_mode()
4811 return 0; /* no error, but not mac80211 device */ in enter_rfmon_mode()
4814 * XXX - is this already a monN device? in enter_rfmon_mode()
4842 * Hard failure. Just return ret; handle->errbuf in enter_rfmon_mode()
4850 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in enter_rfmon_mode()
4851 "%s: No free monN interfaces", device); in enter_rfmon_mode()
4876 handlep->mondevice); in enter_rfmon_mode()
4885 pcapint_strlcpy(ifr.ifr_name, handlep->mondevice, sizeof(ifr.ifr_name)); in enter_rfmon_mode()
4886 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) { in enter_rfmon_mode()
4887 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in enter_rfmon_mode()
4888 errno, "%s: Can't get flags for %s", device, in enter_rfmon_mode()
4889 handlep->mondevice); in enter_rfmon_mode()
4891 handlep->mondevice); in enter_rfmon_mode()
4896 if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) { in enter_rfmon_mode()
4897 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in enter_rfmon_mode()
4898 errno, "%s: Can't set flags for %s", device, in enter_rfmon_mode()
4899 handlep->mondevice); in enter_rfmon_mode()
4901 handlep->mondevice); in enter_rfmon_mode()
4915 handlep->must_do_on_close |= MUST_DELETE_MONIF; in enter_rfmon_mode()
4929 * We don't have libnl, so we can't do monitor mode. in enter_rfmon_mode()
4957 handle->tstamp_type_list = malloc(NUM_SOF_TIMESTAMPING_TYPES * sizeof(u_int)); in iface_set_all_ts_types()
4958 if (handle->tstamp_type_list == NULL) { in iface_set_all_ts_types()
4961 return -1; in iface_set_all_ts_types()
4964 handle->tstamp_type_list[i] = sof_ts_type_map[i].pcap_tstamp_val; in iface_set_all_ts_types()
4965 handle->tstamp_type_count = NUM_SOF_TIMESTAMPING_TYPES; in iface_set_all_ts_types()
4976 int fd; in iface_get_ts_types() local
4983 * This doesn't apply to the "any" device; you can't say "turn on in iface_get_ts_types()
4990 handle->tstamp_type_list = NULL; in iface_get_ts_types()
4997 fd = get_if_ioctl_socket(); in iface_get_ts_types()
4998 if (fd < 0) { in iface_get_ts_types()
5001 return -1; in iface_get_ts_types()
5009 if (ioctl(fd, SIOCETHTOOL, &ifr) == -1) { in iface_get_ts_types()
5012 close(fd); in iface_get_ts_types()
5022 if (iface_set_all_ts_types(handle, ebuf) == -1) in iface_get_ts_types()
5023 return -1; in iface_get_ts_types()
5028 * OK, no such device. in iface_get_ts_types()
5033 handle->tstamp_type_list = NULL; in iface_get_ts_types()
5044 return -1; in iface_get_ts_types()
5047 close(fd); in iface_get_ts_types()
5054 * No, so don't report any time stamp types. in iface_get_ts_types()
5056 * XXX - some devices either don't report in iface_get_ts_types()
5060 * http://marc.info/?l=linux-netdev&m=146318183529571&w=2 in iface_get_ts_types()
5064 handle->tstamp_type_list = NULL; in iface_get_ts_types()
5074 handle->tstamp_type_list = malloc(num_ts_types * sizeof(u_int)); in iface_get_ts_types()
5075 if (handle->tstamp_type_list == NULL) { in iface_get_ts_types()
5078 return -1; in iface_get_ts_types()
5082 handle->tstamp_type_list[j] = sof_ts_type_map[i].pcap_tstamp_val; in iface_get_ts_types()
5086 handle->tstamp_type_count = num_ts_types; in iface_get_ts_types()
5088 handle->tstamp_type_list = NULL; in iface_get_ts_types()
5097 * This doesn't apply to the "any" device; you can't say "turn on in iface_get_ts_types()
5104 handle->tstamp_type_list = NULL; in iface_get_ts_types()
5112 if (iface_set_all_ts_types(handle, ebuf) == -1) in iface_get_ts_types()
5113 return -1; in iface_get_ts_types()
5133 * of the types of offloading, there's nothing we can do to check, so
5134 * we just say "no, we don't".
5157 pcapint_strlcpy(ifr.ifr_name, handle->opt.device, sizeof(ifr.ifr_name)); in iface_ethtool_flag_ioctl()
5161 if (ioctl(handle->fd, SIOCETHTOOL, &ifr) == -1) { in iface_ethtool_flag_ioctl()
5166 * case, either means "no, what we're asking in iface_ethtool_flag_ioctl()
5172 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in iface_ethtool_flag_ioctl()
5174 handle->opt.device, cmdname); in iface_ethtool_flag_ioctl()
5175 return -1; in iface_ethtool_flag_ioctl()
5181 * XXX - it's annoying that we have to check for offloading at all, but,
5184 * offloading may be added - and, worse, may not be checkable with
5187 * opaque (defined in a non-uapi header), and there doesn't seem to
5189 * are - at best, you can ask for a set of strings(!) to get *names*
5192 * by name and set flags by name, with the names having no semantics
5202 if (ret == -1) in iface_get_offload()
5203 return -1; in iface_get_offload()
5210 * XXX - will this cause large unsegmented packets to be in iface_get_offload()
5215 if (ret == -1) in iface_get_offload()
5216 return -1; in iface_get_offload()
5223 if (ret == -1) in iface_get_offload()
5224 return -1; in iface_get_offload()
5231 * XXX - will this cause large reassembled packets to be in iface_get_offload()
5236 if (ret == -1) in iface_get_offload()
5237 return -1; in iface_get_offload()
5250 if (ret == -1) in iface_get_offload()
5251 return -1; in iface_get_offload()
5263 * XXX - do we need to get this information if we don't in iface_get_offload()
5281 { "brcm-prepend", DLT_DSA_TAG_BRCM_PREPEND },
5300 int fd; in iface_dsa_get_proto_info() local
5302 fd = asprintf(&pathstr, "/sys/class/net/%s/dsa/tagging", device); in iface_dsa_get_proto_info()
5303 if (fd < 0) { in iface_dsa_get_proto_info()
5304 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in iface_dsa_get_proto_info()
5305 fd, "asprintf"); in iface_dsa_get_proto_info()
5309 fd = open(pathstr, O_RDONLY); in iface_dsa_get_proto_info()
5314 if (fd < 0) in iface_dsa_get_proto_info()
5317 r = read(fd, buf, sizeof(buf) - 1); in iface_dsa_get_proto_info()
5319 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in iface_dsa_get_proto_info()
5321 close(fd); in iface_dsa_get_proto_info()
5324 close(fd); in iface_dsa_get_proto_info()
5329 if (buf[r - 1] == '\n') in iface_dsa_get_proto_info()
5330 r--; in iface_dsa_get_proto_info()
5336 handle->linktype = dsa_protos[i].linktype; in iface_dsa_get_proto_info()
5346 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, in iface_dsa_get_proto_info()
5356 iface_get_mtu(int fd, const char *device, char *ebuf) in iface_get_mtu() argument
5366 if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) { in iface_get_mtu()
5369 return -1; in iface_get_mtu()
5379 iface_get_arptype(int fd, const char *device, char *ebuf) in iface_get_arptype() argument
5387 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) { in iface_get_arptype()
5390 * No such device. in iface_get_arptype()
5411 struct pcap_linux *handlep = handle->priv; in fix_program()
5422 prog_size = sizeof(*handle->fcode.bf_insns) * handle->fcode.bf_len; in fix_program()
5423 len = handle->fcode.bf_len; in fix_program()
5426 pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, in fix_program()
5428 return -1; in fix_program()
5430 memcpy(f, handle->fcode.bf_insns, prog_size); in fix_program()
5431 fcode->len = len; in fix_program()
5432 fcode->filter = (struct sock_filter *) f; in fix_program()
5439 switch (BPF_CLASS(p->code)) { in fix_program()
5447 switch (BPF_MODE(p->code)) { in fix_program()
5455 if (handlep->cooked) { in fix_program()
5483 * Note that SKF_AD_OFF is negative, but p->k is unsigned, so in fix_offset()
5486 if (p->k >= (bpf_u_int32)SKF_AD_OFF) in fix_offset()
5488 if (handle->linktype == DLT_LINUX_SLL2) { in fix_offset()
5492 if (p->k >= SLL2_HDR_LEN) { in fix_offset()
5494 * It's within the link-layer payload; that starts in fix_offset()
5497 * the link-layer header. in fix_offset()
5499 p->k -= SLL2_HDR_LEN; in fix_offset()
5500 } else if (p->k == 0) { in fix_offset()
5505 p->k = SKF_AD_OFF + SKF_AD_PROTOCOL; in fix_offset()
5506 } else if (p->k == 4) { in fix_offset()
5511 p->k = SKF_AD_OFF + SKF_AD_IFINDEX; in fix_offset()
5512 } else if (p->k == 10) { in fix_offset()
5517 p->k = SKF_AD_OFF + SKF_AD_PKTTYPE; in fix_offset()
5518 } else if ((bpf_int32)(p->k) > 0) { in fix_offset()
5521 * those fields; we can't do that in the kernel, in fix_offset()
5524 return -1; in fix_offset()
5530 if (p->k >= SLL_HDR_LEN) { in fix_offset()
5532 * It's within the link-layer payload; that starts in fix_offset()
5535 * the link-layer header. in fix_offset()
5537 p->k -= SLL_HDR_LEN; in fix_offset()
5538 } else if (p->k == 0) { in fix_offset()
5543 p->k = SKF_AD_OFF + SKF_AD_PKTTYPE; in fix_offset()
5544 } else if (p->k == 14) { in fix_offset()
5549 p->k = SKF_AD_OFF + SKF_AD_PROTOCOL; in fix_offset()
5550 } else if ((bpf_int32)(p->k) > 0) { in fix_offset()
5553 * those fields; we can't do that in the kernel, in fix_offset()
5556 return -1; in fix_offset()
5586 * To flush those packets, we put the socket in read-only mode, in set_kernel_filter()
5587 * and read packets from the socket until there are no more to in set_kernel_filter()
5590 * In order to keep that from being an infinite loop - i.e., in set_kernel_filter()
5592 * the queue - we put the "total filter", which is a filter in set_kernel_filter()
5601 if (setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER, in set_kernel_filter()
5612 * non-blocking mode; we drain it by reading packets in set_kernel_filter()
5616 save_mode = fcntl(handle->fd, F_GETFL, 0); in set_kernel_filter()
5617 if (save_mode == -1) { in set_kernel_filter()
5618 pcapint_fmt_errmsg_for_errno(handle->errbuf, in set_kernel_filter()
5620 "can't get FD flags when changing filter"); in set_kernel_filter()
5621 return -2; in set_kernel_filter()
5623 if (fcntl(handle->fd, F_SETFL, save_mode | O_NONBLOCK) < 0) { in set_kernel_filter()
5624 pcapint_fmt_errmsg_for_errno(handle->errbuf, in set_kernel_filter()
5626 "can't set nonblocking mode when changing filter"); in set_kernel_filter()
5627 return -2; in set_kernel_filter()
5629 while (recv(handle->fd, &drain, sizeof drain, MSG_TRUNC) >= 0) in set_kernel_filter()
5636 * If we can't restore the mode or reset the in set_kernel_filter()
5637 * kernel filter, there's nothing we can do. in set_kernel_filter()
5639 (void)fcntl(handle->fd, F_SETFL, save_mode); in set_kernel_filter()
5641 pcapint_fmt_errmsg_for_errno(handle->errbuf, in set_kernel_filter()
5644 return -2; in set_kernel_filter()
5646 if (fcntl(handle->fd, F_SETFL, save_mode) == -1) { in set_kernel_filter()
5647 pcapint_fmt_errmsg_for_errno(handle->errbuf, in set_kernel_filter()
5649 "can't restore FD flags when changing filter"); in set_kernel_filter()
5650 return -2; in set_kernel_filter()
5657 ret = setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER, in set_kernel_filter()
5659 if (ret == -1 && total_filter_on) { in set_kernel_filter()
5677 if (reset_kernel_filter(handle) == -1) { in set_kernel_filter()
5678 pcapint_fmt_errmsg_for_errno(handle->errbuf, in set_kernel_filter()
5680 "can't remove kernel total filter"); in set_kernel_filter()
5681 return -2; /* fatal error */ in set_kernel_filter()
5696 * as it has no idea that setsockopt() ignores its in reset_kernel_filter()
5701 ret = setsockopt(handle->fd, SOL_SOCKET, SO_DETACH_FILTER, in reset_kernel_filter()
5704 * Ignore ENOENT - it means "we don't have a filter", so there in reset_kernel_filter()
5705 * was no filter to remove, and there's still no filter. in reset_kernel_filter()
5710 if (ret == -1 && errno != ENOENT && errno != ENONET) in reset_kernel_filter()
5711 return -1; in reset_kernel_filter()
5720 p->opt.protocol = protocol; in pcap_set_protocol_linux()