blob: 054eda29e106e308b13f61b67d6a8cc1458e723c [file] [log] [blame]
Davide Pesaventofe0580c2017-05-12 02:02:10 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5f35f642018-05-10 19:36:03 -04002/*
Davide Pesaventoa599d2a2022-02-16 18:52:43 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Davide Pesaventofe0580c2017-05-12 02:02:10 -04004 * 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 Shi24be0732019-09-23 12:38:30 -060027#include "ethernet-protocol.hpp"
Davide Pesaventofe0580c2017-05-12 02:02:10 -040028
Junxiao Shi7fb90712021-01-27 10:31:16 -070029#include "common/privilege-helper.hpp"
30
Davide Pesaventofe0580c2017-05-12 02:02:10 -040031#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 Pesaventoe422f9e2022-06-03 01:30:23 -040038namespace nfd::face {
Davide Pesaventofe0580c2017-05-12 02:02:10 -040039
40PcapHelper::PcapHelper(const std::string& interfaceName)
41 : m_pcap(nullptr)
42{
43 char errbuf[PCAP_ERRBUF_SIZE] = {};
44 m_pcap = pcap_create(interfaceName.data(), errbuf);
45 if (!m_pcap)
Davide Pesavento19779d82019-02-14 13:40:04 -050046 NDN_THROW(Error("pcap_create: " + std::string(errbuf)));
Davide Pesaventofe0580c2017-05-12 02:02:10 -040047
Davide Pesaventofe0580c2017-05-12 02:02:10 -040048 // Enable "immediate mode", effectively disabling any read buffering in the kernel.
49 // This corresponds to the BIOCIMMEDIATE ioctl on BSD-like systems (including macOS)
50 // where libpcap uses a BPF device. On Linux this forces libpcap not to use TPACKET_V3,
51 // even if the kernel supports it, thus preventing bug #1511.
52 if (pcap_set_immediate_mode(m_pcap, 1) < 0)
Davide Pesavento19779d82019-02-14 13:40:04 -050053 NDN_THROW(Error("pcap_set_immediate_mode failed"));
Junxiao Shi24be0732019-09-23 12:38:30 -060054
55 pcap_set_snaplen(m_pcap, ethernet::HDR_LEN + ndn::MAX_NDN_PACKET_SIZE);
56 pcap_set_buffer_size(m_pcap, 4 * 1024 * 1024);
Davide Pesaventofe0580c2017-05-12 02:02:10 -040057}
58
59PcapHelper::~PcapHelper()
60{
61 close();
62}
63
64void
65PcapHelper::activate(int dlt)
66{
Junxiao Shi7fb90712021-01-27 10:31:16 -070067 PrivilegeHelper::runElevated([this] {
68 int ret = pcap_activate(m_pcap);
69 if (ret < 0)
70 NDN_THROW(Error("pcap_activate: " + std::string(pcap_statustostr(ret))));
71 });
Davide Pesaventofe0580c2017-05-12 02:02:10 -040072
73 if (pcap_set_datalink(m_pcap, dlt) < 0)
Davide Pesavento19779d82019-02-14 13:40:04 -050074 NDN_THROW(Error("pcap_set_datalink: " + getLastError()));
Davide Pesaventofe0580c2017-05-12 02:02:10 -040075
76 if (pcap_setdirection(m_pcap, PCAP_D_IN) < 0)
Davide Pesavento19779d82019-02-14 13:40:04 -050077 NDN_THROW(Error("pcap_setdirection: " + getLastError()));
Davide Pesaventofe0580c2017-05-12 02:02:10 -040078}
79
80void
Davide Pesaventoa599d2a2022-02-16 18:52:43 -050081PcapHelper::close() noexcept
Davide Pesaventofe0580c2017-05-12 02:02:10 -040082{
83 if (m_pcap) {
84 pcap_close(m_pcap);
85 m_pcap = nullptr;
86 }
87}
88
89int
90PcapHelper::getFd() const
91{
92 int fd = pcap_get_selectable_fd(m_pcap);
93 if (fd < 0)
Davide Pesavento19779d82019-02-14 13:40:04 -050094 NDN_THROW(Error("pcap_get_selectable_fd failed"));
Davide Pesaventofe0580c2017-05-12 02:02:10 -040095
96 // we need to duplicate the fd, otherwise both pcap_close() and the
97 // caller may attempt to close the same fd and one of them will fail
98 return ::dup(fd);
99}
100
101std::string
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500102PcapHelper::getLastError() const noexcept
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400103{
104 return pcap_geterr(m_pcap);
105}
106
107size_t
108PcapHelper::getNDropped() const
109{
110 pcap_stat ps{};
111 if (pcap_stats(m_pcap, &ps) < 0)
Davide Pesavento19779d82019-02-14 13:40:04 -0500112 NDN_THROW(Error("pcap_stats: " + getLastError()));
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400113
114 return ps.ps_drop;
115}
116
117void
118PcapHelper::setPacketFilter(const char* filter) const
119{
120 bpf_program prog;
121 if (pcap_compile(m_pcap, &prog, filter, 1, PCAP_NETMASK_UNKNOWN) < 0)
Davide Pesavento19779d82019-02-14 13:40:04 -0500122 NDN_THROW(Error("pcap_compile: " + getLastError()));
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400123
124 int ret = pcap_setfilter(m_pcap, &prog);
125 pcap_freecode(&prog);
126 if (ret < 0)
Davide Pesavento19779d82019-02-14 13:40:04 -0500127 NDN_THROW(Error("pcap_setfilter: " + getLastError()));
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400128}
129
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500130std::tuple<span<const uint8_t>, std::string>
131PcapHelper::readNextPacket() const noexcept
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400132{
133 pcap_pkthdr* header;
134 const uint8_t* packet;
135
136 int ret = pcap_next_ex(m_pcap, &header, &packet);
137 if (ret < 0)
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500138 return {span<uint8_t>{}, getLastError()};
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400139 else if (ret == 0)
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500140 return {span<uint8_t>{}, "timed out"};
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400141 else
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500142 return {{packet, header->caplen}, ""};
Davide Pesaventofe0580c2017-05-12 02:02:10 -0400143}
144
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400145} // namespace nfd::face