Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5f35f64 | 2018-05-10 19:36:03 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "pcap-helper.hpp" |
Junxiao Shi | 24be073 | 2019-09-23 12:38:30 -0600 | [diff] [blame] | 27 | #include "ethernet-protocol.hpp" |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 28 | |
Junxiao Shi | 7fb9071 | 2021-01-27 10:31:16 -0700 | [diff] [blame] | 29 | #include "common/privilege-helper.hpp" |
| 30 | |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 31 | #include <pcap/pcap.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #if !defined(PCAP_NETMASK_UNKNOWN) |
| 35 | #define PCAP_NETMASK_UNKNOWN 0xffffffff |
| 36 | #endif |
| 37 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 38 | namespace nfd::face { |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 39 | |
| 40 | PcapHelper::PcapHelper(const std::string& interfaceName) |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 41 | { |
| 42 | char errbuf[PCAP_ERRBUF_SIZE] = {}; |
| 43 | m_pcap = pcap_create(interfaceName.data(), errbuf); |
| 44 | if (!m_pcap) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 45 | NDN_THROW(Error("pcap_create: " + std::string(errbuf))); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 46 | |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 47 | // Enable "immediate mode", effectively disabling any read buffering in the kernel. |
| 48 | // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including macOS) |
| 49 | // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3, |
| 50 | // even if the kernel supports it, thus preventing bug #1511. |
| 51 | if (pcap_set_immediate_mode(m_pcap, 1) < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 52 | NDN_THROW(Error("pcap_set_immediate_mode failed")); |
Junxiao Shi | 24be073 | 2019-09-23 12:38:30 -0600 | [diff] [blame] | 53 | |
| 54 | pcap_set_snaplen(m_pcap, ethernet::HDR_LEN + ndn::MAX_NDN_PACKET_SIZE); |
| 55 | pcap_set_buffer_size(m_pcap, 4 * 1024 * 1024); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 58 | PcapHelper::~PcapHelper() noexcept |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 59 | { |
| 60 | close(); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | PcapHelper::activate(int dlt) |
| 65 | { |
Junxiao Shi | 7fb9071 | 2021-01-27 10:31:16 -0700 | [diff] [blame] | 66 | PrivilegeHelper::runElevated([this] { |
| 67 | int ret = pcap_activate(m_pcap); |
| 68 | if (ret < 0) |
| 69 | NDN_THROW(Error("pcap_activate: " + std::string(pcap_statustostr(ret)))); |
| 70 | }); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 71 | |
Davide Pesavento | 0b01568 | 2022-12-12 21:00:04 -0500 | [diff] [blame] | 72 | char errbuf[PCAP_ERRBUF_SIZE] = {}; |
| 73 | if (pcap_setnonblock(m_pcap, 1, errbuf) < 0) |
| 74 | NDN_THROW(Error("pcap_setnonblock: " + std::string(errbuf))); |
| 75 | |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 76 | if (pcap_set_datalink(m_pcap, dlt) < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 77 | NDN_THROW(Error("pcap_set_datalink: " + getLastError())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 78 | |
| 79 | if (pcap_setdirection(m_pcap, PCAP_D_IN) < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 80 | NDN_THROW(Error("pcap_setdirection: " + getLastError())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 84 | PcapHelper::close() noexcept |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 85 | { |
| 86 | if (m_pcap) { |
| 87 | pcap_close(m_pcap); |
| 88 | m_pcap = nullptr; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | PcapHelper::getFd() const |
| 94 | { |
| 95 | int fd = pcap_get_selectable_fd(m_pcap); |
| 96 | if (fd < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 97 | NDN_THROW(Error("pcap_get_selectable_fd failed")); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 98 | |
| 99 | // we need to duplicate the fd, otherwise both pcap_close() and the |
| 100 | // caller may attempt to close the same fd and one of them will fail |
| 101 | return ::dup(fd); |
| 102 | } |
| 103 | |
| 104 | std::string |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 105 | PcapHelper::getLastError() const noexcept |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 106 | { |
| 107 | return pcap_geterr(m_pcap); |
| 108 | } |
| 109 | |
| 110 | size_t |
| 111 | PcapHelper::getNDropped() const |
| 112 | { |
| 113 | pcap_stat ps{}; |
| 114 | if (pcap_stats(m_pcap, &ps) < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 115 | NDN_THROW(Error("pcap_stats: " + getLastError())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 116 | |
| 117 | return ps.ps_drop; |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | PcapHelper::setPacketFilter(const char* filter) const |
| 122 | { |
| 123 | bpf_program prog; |
| 124 | if (pcap_compile(m_pcap, &prog, filter, 1, PCAP_NETMASK_UNKNOWN) < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 125 | NDN_THROW(Error("pcap_compile: " + getLastError())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 126 | |
| 127 | int ret = pcap_setfilter(m_pcap, &prog); |
| 128 | pcap_freecode(&prog); |
| 129 | if (ret < 0) |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 130 | NDN_THROW(Error("pcap_setfilter: " + getLastError())); |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 133 | std::tuple<span<const uint8_t>, std::string> |
| 134 | PcapHelper::readNextPacket() const noexcept |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 135 | { |
| 136 | pcap_pkthdr* header; |
| 137 | const uint8_t* packet; |
| 138 | |
| 139 | int ret = pcap_next_ex(m_pcap, &header, &packet); |
| 140 | if (ret < 0) |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 141 | return {span<uint8_t>{}, getLastError()}; |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 142 | else if (ret == 0) |
Davide Pesavento | 0b01568 | 2022-12-12 21:00:04 -0500 | [diff] [blame] | 143 | return {span<uint8_t>{}, "Nothing to read"}; |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 144 | else |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 145 | return {{packet, header->caplen}, ""}; |
Davide Pesavento | fe0580c | 2017-05-12 02:02:10 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 148 | } // namespace nfd::face |