blob: 1fec4f1469dc0da45a56e9d109e3a5c7b7ddfba6 [file] [log] [blame]
Junxiao Shi2222a612015-06-06 08:01:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento60f8cc12018-05-10 22:05:21 -04002/*
3 * Copyright (c) 2014-2018, Regents of the University of California.
Junxiao Shi3cd47df2015-06-07 20:58:14 -07004 *
5 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
7 *
8 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
Davide Pesavento60f8cc12018-05-10 22:05:21 -040019/*
Junxiao Shi2222a612015-06-06 08:01:38 -070020 * Copyright (c) 2011-2014, Regents of the University of California,
21 *
22 * This file is part of ndndump, the packet capture and analysis tool for Named Data
23 * Networking (NDN).
24 *
25 * ndndump is free software: you can redistribute it and/or modify it under the terms
26 * of the GNU General Public License as published by the Free Software Foundation,
27 * either version 3 of the License, or (at your option) any later version.
28 *
29 * ndndump is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
30 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
31 * PURPOSE. See the GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License along with
34 * ndndump, e.g., in COPYING file. If not, see <http://www.gnu.org/licenses/>.
35 **/
36
37#include "ndndump.hpp"
38
Junxiao Shi2222a612015-06-06 08:01:38 -070039#include "tcpdump/tcpdump-stdinc.h"
40
41namespace ndn {
Junxiao Shi3cd47df2015-06-07 20:58:14 -070042namespace dump {
43// namespace is necessary to prevent clashing with system includes
Junxiao Shi2222a612015-06-06 08:01:38 -070044
45#include "tcpdump/ether.h"
46#include "tcpdump/ip.h"
47#include "tcpdump/udp.h"
48#include "tcpdump/tcp.h"
49
Junxiao Shi3cd47df2015-06-07 20:58:14 -070050} // namespace dump
Vince Lehman277ecf02016-02-10 16:37:48 -060051} // namespace ndn
Junxiao Shi2222a612015-06-06 08:01:38 -070052
Junxiao Shi022bddf2016-11-24 23:15:20 +000053#include <pcap/sll.h>
54
Junxiao Shi2222a612015-06-06 08:01:38 -070055#include <iomanip>
Davide Pesaventoc0702702017-08-24 22:04:00 -040056#include <sstream>
Junxiao Shi2222a612015-06-06 08:01:38 -070057
Vince Lehman277ecf02016-02-10 16:37:48 -060058#include <ndn-cxx/lp/nack.hpp>
59#include <ndn-cxx/lp/packet.hpp>
Junxiao Shi2222a612015-06-06 08:01:38 -070060
61namespace ndn {
Junxiao Shi3cd47df2015-06-07 20:58:14 -070062namespace dump {
Junxiao Shi2222a612015-06-06 08:01:38 -070063
Junxiao Shi2222a612015-06-06 08:01:38 -070064const size_t MAX_SNAPLEN = 65535;
65
Junxiao Shic1c2b832016-07-24 20:45:36 +000066Ndndump::Ndndump()
67 : isVerbose(false)
68 , pcapProgram("(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)")
69 // , isSuccinct(false)
70 // , isMatchInverted(false)
71 // , shouldPrintStructure(false)
72 // , isTcpOnly(false)
73 // , isUdpOnly(false)
74{
75}
76
Junxiao Shi2222a612015-06-06 08:01:38 -070077void
78Ndndump::run()
79{
80 if (inputFile.empty() && interface.empty()) {
81 char errbuf[PCAP_ERRBUF_SIZE];
82 const char* pcapDevice = pcap_lookupdev(errbuf);
83
Junxiao Shic1c2b832016-07-24 20:45:36 +000084 if (pcapDevice == nullptr) {
Junxiao Shic7599632016-07-24 20:46:24 +000085 BOOST_THROW_EXCEPTION(Error(errbuf));
Junxiao Shi2222a612015-06-06 08:01:38 -070086 }
87
88 interface = pcapDevice;
89 }
90
91 if (isVerbose) {
92 if (!interface.empty()) {
93 std::cerr << "ndndump: listening on " << interface << std::endl;
94 }
95 else {
96 std::cerr << "ndndump: reading from " << inputFile << std::endl;
97 }
98
99 if (!nameFilter.empty()) {
100 std::cerr << "ndndump: using name filter " << nameFilter << std::endl;
101 }
102 }
103
104 if (!interface.empty()) {
105 char errbuf[PCAP_ERRBUF_SIZE];
106 m_pcap = pcap_open_live(interface.c_str(), MAX_SNAPLEN, 0, 1000, errbuf);
Junxiao Shic1c2b832016-07-24 20:45:36 +0000107 if (m_pcap == nullptr) {
Junxiao Shic7599632016-07-24 20:46:24 +0000108 BOOST_THROW_EXCEPTION(Error("Cannot open interface " + interface + " (" + errbuf + ")"));
Junxiao Shi2222a612015-06-06 08:01:38 -0700109 }
110 }
111 else {
112 char errbuf[PCAP_ERRBUF_SIZE];
113 m_pcap = pcap_open_offline(inputFile.c_str(), errbuf);
Junxiao Shic1c2b832016-07-24 20:45:36 +0000114 if (m_pcap == nullptr) {
Junxiao Shic7599632016-07-24 20:46:24 +0000115 BOOST_THROW_EXCEPTION(Error("Cannot open file " + inputFile + " for reading (" + errbuf + ")"));
Junxiao Shi2222a612015-06-06 08:01:38 -0700116 }
117 }
118
119 if (!pcapProgram.empty()) {
120 if (isVerbose) {
121 std::cerr << "ndndump: pcap_filter = " << pcapProgram << std::endl;
122 }
123
124 bpf_program program;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000125 int res = pcap_compile(m_pcap, &program, pcapProgram.c_str(), 0, PCAP_NETMASK_UNKNOWN);
Junxiao Shi2222a612015-06-06 08:01:38 -0700126
Junxiao Shic1c2b832016-07-24 20:45:36 +0000127 if (res < 0) {
Junxiao Shic7599632016-07-24 20:46:24 +0000128 BOOST_THROW_EXCEPTION(Error("Cannot parse tcpdump expression '" + pcapProgram +
129 "' (" + pcap_geterr(m_pcap) + ")"));
Junxiao Shi2222a612015-06-06 08:01:38 -0700130 }
131
Junxiao Shic1c2b832016-07-24 20:45:36 +0000132 res = pcap_setfilter(m_pcap, &program);
Junxiao Shi2222a612015-06-06 08:01:38 -0700133 pcap_freecode(&program);
134
Junxiao Shic1c2b832016-07-24 20:45:36 +0000135 if (res < 0) {
Junxiao Shic7599632016-07-24 20:46:24 +0000136 BOOST_THROW_EXCEPTION(Error(std::string("pcap_setfilter failed (") + pcap_geterr(m_pcap) + ")"));
Junxiao Shi2222a612015-06-06 08:01:38 -0700137 }
138 }
139
140 m_dataLinkType = pcap_datalink(m_pcap);
Junxiao Shi022bddf2016-11-24 23:15:20 +0000141 if (m_dataLinkType != DLT_EN10MB && m_dataLinkType != DLT_PPP && m_dataLinkType != DLT_LINUX_SLL) {
Junxiao Shi1c71bcd2016-11-24 04:00:42 +0000142 BOOST_THROW_EXCEPTION(Error("Unsupported pcap format (" + to_string(m_dataLinkType) + ")"));
Junxiao Shic1c2b832016-07-24 20:45:36 +0000143 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700144
145 pcap_loop(m_pcap, -1, &Ndndump::onCapturedPacket, reinterpret_cast<uint8_t*>(this));
146}
147
148
149void
Junxiao Shic1c2b832016-07-24 20:45:36 +0000150Ndndump::onCapturedPacket(const pcap_pkthdr* header, const uint8_t* packet) const
Junxiao Shi2222a612015-06-06 08:01:38 -0700151{
152 std::ostringstream os;
153 printInterceptTime(os, header);
154
155 const uint8_t* payload = packet;
156 ssize_t payloadSize = header->len;
157
158 int frameType = skipDataLinkHeaderAndGetFrameType(payload, payloadSize);
159 if (frameType < 0) {
160 std::cerr << "Unknown frame type" << std::endl;
161 return;
162 }
163
Junxiao Shic1c2b832016-07-24 20:45:36 +0000164 int res = skipAndProcessFrameHeader(frameType, payload, payloadSize, os);
165 if (res < 0) {
Junxiao Shi2222a612015-06-06 08:01:38 -0700166 return;
167 }
168
169 bool isOk = false;
170 Block block;
171 std::tie(isOk, block) = Block::fromBuffer(payload, payloadSize);
172 if (!isOk) {
Vince Lehman277ecf02016-02-10 16:37:48 -0600173 // if packet is incomplete, we will not be able to process it
174 if (payloadSize > 0) {
175 std::cout << os.str() << ", " << "INCOMPLETE-PACKET" << ", size: " << payloadSize << std::endl;
176 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700177 return;
178 }
179
Vince Lehman277ecf02016-02-10 16:37:48 -0600180 lp::Packet lpPacket;
181 Block netPacket;
182
183 if (block.type() == lp::tlv::LpPacket) {
184 lpPacket = lp::Packet(block);
185
186 Buffer::const_iterator begin, end;
187
188 if (lpPacket.has<lp::FragmentField>()) {
189 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
190 }
191 else {
192 std::cout << os.str() << ", " << "NDNLPv2-IDLE" << std::endl;
193 return;
194 }
195
196 bool isOk = false;
197 std::tie(isOk, netPacket) = Block::fromBuffer(&*begin, std::distance(begin, end));
198 if (!isOk) {
199 // if network packet is fragmented, we will not be able to process it
200 std::cout << os.str() << ", " << "NDNLPv2-FRAGMENT" << std::endl;
201 return;
202 }
203 }
204 else {
205 netPacket = block;
206 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700207
208 try {
Vince Lehman277ecf02016-02-10 16:37:48 -0600209 if (netPacket.type() == tlv::Interest) {
210 Interest interest(netPacket);
Junxiao Shi2222a612015-06-06 08:01:38 -0700211 if (matchesFilter(interest.getName())) {
Vince Lehman277ecf02016-02-10 16:37:48 -0600212
213 if (lpPacket.has<lp::NackField>()) {
214 lp::Nack nack(interest);
215 nack.setHeader(lpPacket.get<lp::NackField>());
216
217 std::cout << os.str() << ", " << "NACK: " << nack.getReason() << ", " << interest << std::endl;
218 }
219 else {
220 std::cout << os.str() << ", " << "INTEREST: " << interest << std::endl;
221 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700222 }
223 }
Vince Lehman277ecf02016-02-10 16:37:48 -0600224 else if (netPacket.type() == tlv::Data) {
225 Data data(netPacket);
Junxiao Shi2222a612015-06-06 08:01:38 -0700226 if (matchesFilter(data.getName())) {
227 std::cout << os.str() << ", " << "DATA: " << data.getName() << std::endl;
228 }
229 }
Vince Lehman277ecf02016-02-10 16:37:48 -0600230 else {
231 std::cout << os.str() << ", " << "UNKNOWN-NETWORK-PACKET" << std::endl;
232 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700233 }
Junxiao Shic1c2b832016-07-24 20:45:36 +0000234 catch (const tlv::Error& e) {
Junxiao Shi2222a612015-06-06 08:01:38 -0700235 std::cerr << e.what() << std::endl;
236 }
237}
238
239void
Junxiao Shic1c2b832016-07-24 20:45:36 +0000240Ndndump::printInterceptTime(std::ostream& os, const pcap_pkthdr* header) const
Junxiao Shi2222a612015-06-06 08:01:38 -0700241{
242 os << header->ts.tv_sec
243 << "."
244 << std::setfill('0') << std::setw(6) << header->ts.tv_usec;
245
246 // struct tm* tm;
247 // if (flags.unit_time) {
248 // os << (int) header->ts.tv_sec
249 // << "."
250 // << setfill('0') << setw(6) << (int)header->ts.tv_usec;
Davide Pesaventoc0702702017-08-24 22:04:00 -0400251 // }
252 // else {
Junxiao Shi2222a612015-06-06 08:01:38 -0700253 // tm = localtime(&(header->ts.tv_sec));
254 // os << (int)tm->tm_hour << ":"
255 // << setfill('0') << setw(2) << (int)tm->tm_min<< ":"
256 // << setfill('0') << setw(2) << (int)tm->tm_sec<< "."
257 // << setfill('0') << setw(6) << (int)header->ts.tv_usec;
258 // }
259 os << " ";
260}
261
262int
Junxiao Shic1c2b832016-07-24 20:45:36 +0000263Ndndump::skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize) const
Junxiao Shi2222a612015-06-06 08:01:38 -0700264{
265 int frameType = 0;
266
267 switch (m_dataLinkType) {
Junxiao Shic1c2b832016-07-24 20:45:36 +0000268 case DLT_EN10MB: { // Ethernet frames can have Ethernet or 802.3 encapsulation
Junxiao Shi2222a612015-06-06 08:01:38 -0700269 const ether_header* etherHeader = reinterpret_cast<const ether_header*>(payload);
270
271 if (payloadSize < 0) {
272 std::cerr << "Invalid pcap Ethernet frame" << std::endl;
273 return -1;
274 }
275
276 frameType = ntohs(etherHeader->ether_type);
277 payloadSize -= ETHER_HDRLEN;
278 payload += ETHER_HDRLEN;
279
280 break;
281 }
Junxiao Shic1c2b832016-07-24 20:45:36 +0000282 case DLT_PPP: {
Junxiao Shi2222a612015-06-06 08:01:38 -0700283 frameType = *payload;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000284 --payloadSize;
285 ++payload;
Junxiao Shi2222a612015-06-06 08:01:38 -0700286
287 if (!(frameType & 1)) {
288 frameType = (frameType << 8) | *payload;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000289 --payloadSize;
290 ++payload;
Junxiao Shi2222a612015-06-06 08:01:38 -0700291 }
292
293 if (payloadSize < 0) {
294 std::cerr << "Invalid PPP frame" << std::endl;
295 return -1;
296 }
297
298 break;
299 }
Junxiao Shi022bddf2016-11-24 23:15:20 +0000300 case DLT_LINUX_SLL: {
301 const sll_header* sllHeader = reinterpret_cast<const sll_header*>(payload);
302
303 if (payloadSize < SLL_HDR_LEN) {
304 std::cerr << "Invalid LINUX_SLL frame" << std::endl;
305 return -1;
306 }
307
308 frameType = ntohs(sllHeader->sll_protocol);
309 payloadSize -= SLL_HDR_LEN;
310 payload += SLL_HDR_LEN;
311
312 break;
313 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700314 }
315
316 return frameType;
317}
318
319int
320Ndndump::skipAndProcessFrameHeader(int frameType,
321 const uint8_t*& payload, ssize_t& payloadSize,
Junxiao Shic1c2b832016-07-24 20:45:36 +0000322 std::ostream& os) const
Junxiao Shi2222a612015-06-06 08:01:38 -0700323{
Junxiao Shic1c2b832016-07-24 20:45:36 +0000324 switch (frameType) {
325 case 0x0800: // ETHERTYPE_IP
326 case DLT_EN10MB: { // pcap encapsulation
327 const ip* ipHeader = reinterpret_cast<const ip*>(payload);
328 size_t ipHeaderSize = IP_HL(ipHeader) * 4;
329 if (ipHeaderSize < 20) {
330 std::cerr << "invalid IP header len " << ipHeaderSize << " bytes" << std::endl;
331 return -1;
332 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700333
Junxiao Shic1c2b832016-07-24 20:45:36 +0000334 os << "From: " << inet_ntoa(ipHeader->ip_src) << ", ";
335 os << "To: " << inet_ntoa(ipHeader->ip_dst);
Junxiao Shi2222a612015-06-06 08:01:38 -0700336
Junxiao Shic1c2b832016-07-24 20:45:36 +0000337 payloadSize -= ipHeaderSize;
338 payload += ipHeaderSize;
Junxiao Shi2222a612015-06-06 08:01:38 -0700339
Junxiao Shic1c2b832016-07-24 20:45:36 +0000340 if (payloadSize < 0) {
341 std::cerr << "Invalid pcap IP packet" << std::endl;
342 return -1;
343 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700344
Junxiao Shic1c2b832016-07-24 20:45:36 +0000345 switch (ipHeader->ip_p) {
346 case IPPROTO_UDP: {
347 // if (!flags.udp)
348 // return -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700349
Junxiao Shic1c2b832016-07-24 20:45:36 +0000350 payloadSize -= sizeof(udphdr);
351 payload += sizeof(udphdr);
Junxiao Shi2222a612015-06-06 08:01:38 -0700352
Junxiao Shic1c2b832016-07-24 20:45:36 +0000353 if (payloadSize < 0) {
354 std::cerr << "Invalid pcap UDP/IP packet" << std::endl;
355 return -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700356 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700357
Junxiao Shic1c2b832016-07-24 20:45:36 +0000358 os << ", Tunnel Type: UDP";
359 break;
360 }
361 case IPPROTO_TCP: {
362 // if (!flags.tcp)
363 // return -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700364
Junxiao Shic1c2b832016-07-24 20:45:36 +0000365 const tcphdr* tcpHeader = reinterpret_cast<const tcphdr*>(payload);
366 size_t tcpHeaderSize = TH_OFF(tcpHeader) * 4;
Junxiao Shi2222a612015-06-06 08:01:38 -0700367
Junxiao Shic1c2b832016-07-24 20:45:36 +0000368 if (tcpHeaderSize < 20) {
369 std::cerr << "Invalid TCP Header len: " << tcpHeaderSize <<" bytes" << std::endl;
370 return -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700371 }
Junxiao Shic1c2b832016-07-24 20:45:36 +0000372
373 payloadSize -= tcpHeaderSize;
374 payload += tcpHeaderSize;
375
376 if (payloadSize < 0) {
377 std::cerr << "Invalid pcap TCP/IP packet" << std::endl;
378 return -1;
379 }
380
381 os << ", Tunnel Type: TCP";
382 break;
383 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700384 default:
385 return -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700386 }
Junxiao Shic1c2b832016-07-24 20:45:36 +0000387
388 break;
389 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700390 case /*ETHERTYPE_NDN*/0x7777:
391 os << "Tunnel Type: EthernetFrame";
392 break;
393 case /*ETHERTYPE_NDNLP*/0x8624:
394 os << "Tunnel Type: EthernetFrame";
395 break;
396 case 0x0077: // pcap
397 os << "Tunnel Type: PPP";
398 payloadSize -= 2;
399 payload += 2;
400 break;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000401 default: // do nothing if it is not a recognized type of a packet
Junxiao Shi2222a612015-06-06 08:01:38 -0700402 return -1;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000403 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700404
405 return 0;
406}
407
Junxiao Shic1c2b832016-07-24 20:45:36 +0000408bool
409Ndndump::matchesFilter(const Name& name) const
410{
411 if (nameFilter.empty())
412 return true;
413
414 /// \todo Switch to NDN regular expressions
415
416 return boost::regex_match(name.toUri(), nameFilter);
417}
418
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700419} // namespace dump
Junxiao Shi2222a612015-06-06 08:01:38 -0700420} // namespace ndn