blob: d23bf9adfcab83624b46c4ff6176da564e7a4f2e [file] [log] [blame]
Junxiao Shi2222a612015-06-06 08:01:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman277ecf02016-02-10 16:37:48 -06003 * Copyright (c) 2014-2016, 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 */
19/**
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
53#include <boost/lexical_cast.hpp>
54
55#include <iomanip>
56
57#include <ndn-cxx/interest.hpp>
58#include <ndn-cxx/data.hpp>
Vince Lehman277ecf02016-02-10 16:37:48 -060059#include <ndn-cxx/lp/nack.hpp>
60#include <ndn-cxx/lp/packet.hpp>
Junxiao Shi2222a612015-06-06 08:01:38 -070061
62namespace ndn {
Junxiao Shi3cd47df2015-06-07 20:58:14 -070063namespace dump {
Junxiao Shi2222a612015-06-06 08:01:38 -070064
Junxiao Shi2222a612015-06-06 08:01:38 -070065const size_t MAX_SNAPLEN = 65535;
66
Junxiao Shic1c2b832016-07-24 20:45:36 +000067Ndndump::Ndndump()
68 : isVerbose(false)
69 , pcapProgram("(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)")
70 // , isSuccinct(false)
71 // , isMatchInverted(false)
72 // , shouldPrintStructure(false)
73 // , isTcpOnly(false)
74 // , isUdpOnly(false)
75{
76}
77
Junxiao Shi2222a612015-06-06 08:01:38 -070078void
79Ndndump::run()
80{
81 if (inputFile.empty() && interface.empty()) {
82 char errbuf[PCAP_ERRBUF_SIZE];
83 const char* pcapDevice = pcap_lookupdev(errbuf);
84
Junxiao Shic1c2b832016-07-24 20:45:36 +000085 if (pcapDevice == nullptr) {
Junxiao Shic7599632016-07-24 20:46:24 +000086 BOOST_THROW_EXCEPTION(Error(errbuf));
Junxiao Shi2222a612015-06-06 08:01:38 -070087 }
88
89 interface = pcapDevice;
90 }
91
92 if (isVerbose) {
93 if (!interface.empty()) {
94 std::cerr << "ndndump: listening on " << interface << std::endl;
95 }
96 else {
97 std::cerr << "ndndump: reading from " << inputFile << std::endl;
98 }
99
100 if (!nameFilter.empty()) {
101 std::cerr << "ndndump: using name filter " << nameFilter << std::endl;
102 }
103 }
104
105 if (!interface.empty()) {
106 char errbuf[PCAP_ERRBUF_SIZE];
107 m_pcap = pcap_open_live(interface.c_str(), MAX_SNAPLEN, 0, 1000, errbuf);
Junxiao Shic1c2b832016-07-24 20:45:36 +0000108 if (m_pcap == nullptr) {
Junxiao Shic7599632016-07-24 20:46:24 +0000109 BOOST_THROW_EXCEPTION(Error("Cannot open interface " + interface + " (" + errbuf + ")"));
Junxiao Shi2222a612015-06-06 08:01:38 -0700110 }
111 }
112 else {
113 char errbuf[PCAP_ERRBUF_SIZE];
114 m_pcap = pcap_open_offline(inputFile.c_str(), errbuf);
Junxiao Shic1c2b832016-07-24 20:45:36 +0000115 if (m_pcap == nullptr) {
Junxiao Shic7599632016-07-24 20:46:24 +0000116 BOOST_THROW_EXCEPTION(Error("Cannot open file " + inputFile + " for reading (" + errbuf + ")"));
Junxiao Shi2222a612015-06-06 08:01:38 -0700117 }
118 }
119
120 if (!pcapProgram.empty()) {
121 if (isVerbose) {
122 std::cerr << "ndndump: pcap_filter = " << pcapProgram << std::endl;
123 }
124
125 bpf_program program;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000126 int res = pcap_compile(m_pcap, &program, pcapProgram.c_str(), 0, PCAP_NETMASK_UNKNOWN);
Junxiao Shi2222a612015-06-06 08:01:38 -0700127
Junxiao Shic1c2b832016-07-24 20:45:36 +0000128 if (res < 0) {
Junxiao Shic7599632016-07-24 20:46:24 +0000129 BOOST_THROW_EXCEPTION(Error("Cannot parse tcpdump expression '" + pcapProgram +
130 "' (" + pcap_geterr(m_pcap) + ")"));
Junxiao Shi2222a612015-06-06 08:01:38 -0700131 }
132
Junxiao Shic1c2b832016-07-24 20:45:36 +0000133 res = pcap_setfilter(m_pcap, &program);
Junxiao Shi2222a612015-06-06 08:01:38 -0700134 pcap_freecode(&program);
135
Junxiao Shic1c2b832016-07-24 20:45:36 +0000136 if (res < 0) {
Junxiao Shic7599632016-07-24 20:46:24 +0000137 BOOST_THROW_EXCEPTION(Error(std::string("pcap_setfilter failed (") + pcap_geterr(m_pcap) + ")"));
Junxiao Shi2222a612015-06-06 08:01:38 -0700138 }
139 }
140
141 m_dataLinkType = pcap_datalink(m_pcap);
Junxiao Shic1c2b832016-07-24 20:45:36 +0000142 if (m_dataLinkType != DLT_EN10MB && m_dataLinkType != DLT_PPP) {
Junxiao Shi1c71bcd2016-11-24 04:00:42 +0000143 BOOST_THROW_EXCEPTION(Error("Unsupported pcap format (" + to_string(m_dataLinkType) + ")"));
Junxiao Shic1c2b832016-07-24 20:45:36 +0000144 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700145
146 pcap_loop(m_pcap, -1, &Ndndump::onCapturedPacket, reinterpret_cast<uint8_t*>(this));
147}
148
149
150void
Junxiao Shic1c2b832016-07-24 20:45:36 +0000151Ndndump::onCapturedPacket(const pcap_pkthdr* header, const uint8_t* packet) const
Junxiao Shi2222a612015-06-06 08:01:38 -0700152{
153 std::ostringstream os;
154 printInterceptTime(os, header);
155
156 const uint8_t* payload = packet;
157 ssize_t payloadSize = header->len;
158
159 int frameType = skipDataLinkHeaderAndGetFrameType(payload, payloadSize);
160 if (frameType < 0) {
161 std::cerr << "Unknown frame type" << std::endl;
162 return;
163 }
164
Junxiao Shic1c2b832016-07-24 20:45:36 +0000165 int res = skipAndProcessFrameHeader(frameType, payload, payloadSize, os);
166 if (res < 0) {
Junxiao Shi2222a612015-06-06 08:01:38 -0700167 return;
168 }
169
170 bool isOk = false;
171 Block block;
172 std::tie(isOk, block) = Block::fromBuffer(payload, payloadSize);
173 if (!isOk) {
Vince Lehman277ecf02016-02-10 16:37:48 -0600174 // if packet is incomplete, we will not be able to process it
175 if (payloadSize > 0) {
176 std::cout << os.str() << ", " << "INCOMPLETE-PACKET" << ", size: " << payloadSize << std::endl;
177 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700178 return;
179 }
180
Vince Lehman277ecf02016-02-10 16:37:48 -0600181 lp::Packet lpPacket;
182 Block netPacket;
183
184 if (block.type() == lp::tlv::LpPacket) {
185 lpPacket = lp::Packet(block);
186
187 Buffer::const_iterator begin, end;
188
189 if (lpPacket.has<lp::FragmentField>()) {
190 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
191 }
192 else {
193 std::cout << os.str() << ", " << "NDNLPv2-IDLE" << std::endl;
194 return;
195 }
196
197 bool isOk = false;
198 std::tie(isOk, netPacket) = Block::fromBuffer(&*begin, std::distance(begin, end));
199 if (!isOk) {
200 // if network packet is fragmented, we will not be able to process it
201 std::cout << os.str() << ", " << "NDNLPv2-FRAGMENT" << std::endl;
202 return;
203 }
204 }
205 else {
206 netPacket = block;
207 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700208
209 try {
Vince Lehman277ecf02016-02-10 16:37:48 -0600210 if (netPacket.type() == tlv::Interest) {
211 Interest interest(netPacket);
Junxiao Shi2222a612015-06-06 08:01:38 -0700212 if (matchesFilter(interest.getName())) {
Vince Lehman277ecf02016-02-10 16:37:48 -0600213
214 if (lpPacket.has<lp::NackField>()) {
215 lp::Nack nack(interest);
216 nack.setHeader(lpPacket.get<lp::NackField>());
217
218 std::cout << os.str() << ", " << "NACK: " << nack.getReason() << ", " << interest << std::endl;
219 }
220 else {
221 std::cout << os.str() << ", " << "INTEREST: " << interest << std::endl;
222 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700223 }
224 }
Vince Lehman277ecf02016-02-10 16:37:48 -0600225 else if (netPacket.type() == tlv::Data) {
226 Data data(netPacket);
Junxiao Shi2222a612015-06-06 08:01:38 -0700227 if (matchesFilter(data.getName())) {
228 std::cout << os.str() << ", " << "DATA: " << data.getName() << std::endl;
229 }
230 }
Vince Lehman277ecf02016-02-10 16:37:48 -0600231 else {
232 std::cout << os.str() << ", " << "UNKNOWN-NETWORK-PACKET" << std::endl;
233 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700234 }
Junxiao Shic1c2b832016-07-24 20:45:36 +0000235 catch (const tlv::Error& e) {
Junxiao Shi2222a612015-06-06 08:01:38 -0700236 std::cerr << e.what() << std::endl;
237 }
238}
239
240void
Junxiao Shic1c2b832016-07-24 20:45:36 +0000241Ndndump::printInterceptTime(std::ostream& os, const pcap_pkthdr* header) const
Junxiao Shi2222a612015-06-06 08:01:38 -0700242{
243 os << header->ts.tv_sec
244 << "."
245 << std::setfill('0') << std::setw(6) << header->ts.tv_usec;
246
247 // struct tm* tm;
248 // if (flags.unit_time) {
249 // os << (int) header->ts.tv_sec
250 // << "."
251 // << setfill('0') << setw(6) << (int)header->ts.tv_usec;
252 // } else {
253 // 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 }
300 }
301
302 return frameType;
303}
304
305int
306Ndndump::skipAndProcessFrameHeader(int frameType,
307 const uint8_t*& payload, ssize_t& payloadSize,
Junxiao Shic1c2b832016-07-24 20:45:36 +0000308 std::ostream& os) const
Junxiao Shi2222a612015-06-06 08:01:38 -0700309{
Junxiao Shic1c2b832016-07-24 20:45:36 +0000310 switch (frameType) {
311 case 0x0800: // ETHERTYPE_IP
312 case DLT_EN10MB: { // pcap encapsulation
313 const ip* ipHeader = reinterpret_cast<const ip*>(payload);
314 size_t ipHeaderSize = IP_HL(ipHeader) * 4;
315 if (ipHeaderSize < 20) {
316 std::cerr << "invalid IP header len " << ipHeaderSize << " bytes" << std::endl;
317 return -1;
318 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700319
Junxiao Shic1c2b832016-07-24 20:45:36 +0000320 os << "From: " << inet_ntoa(ipHeader->ip_src) << ", ";
321 os << "To: " << inet_ntoa(ipHeader->ip_dst);
Junxiao Shi2222a612015-06-06 08:01:38 -0700322
Junxiao Shic1c2b832016-07-24 20:45:36 +0000323 payloadSize -= ipHeaderSize;
324 payload += ipHeaderSize;
Junxiao Shi2222a612015-06-06 08:01:38 -0700325
Junxiao Shic1c2b832016-07-24 20:45:36 +0000326 if (payloadSize < 0) {
327 std::cerr << "Invalid pcap IP packet" << std::endl;
328 return -1;
329 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700330
Junxiao Shic1c2b832016-07-24 20:45:36 +0000331 switch (ipHeader->ip_p) {
332 case IPPROTO_UDP: {
333 // if (!flags.udp)
334 // return -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700335
Junxiao Shic1c2b832016-07-24 20:45:36 +0000336 payloadSize -= sizeof(udphdr);
337 payload += sizeof(udphdr);
Junxiao Shi2222a612015-06-06 08:01:38 -0700338
Junxiao Shic1c2b832016-07-24 20:45:36 +0000339 if (payloadSize < 0) {
340 std::cerr << "Invalid pcap UDP/IP packet" << std::endl;
341 return -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700342 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700343
Junxiao Shic1c2b832016-07-24 20:45:36 +0000344 os << ", Tunnel Type: UDP";
345 break;
346 }
347 case IPPROTO_TCP: {
348 // if (!flags.tcp)
349 // return -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700350
Junxiao Shic1c2b832016-07-24 20:45:36 +0000351 const tcphdr* tcpHeader = reinterpret_cast<const tcphdr*>(payload);
352 size_t tcpHeaderSize = TH_OFF(tcpHeader) * 4;
Junxiao Shi2222a612015-06-06 08:01:38 -0700353
Junxiao Shic1c2b832016-07-24 20:45:36 +0000354 if (tcpHeaderSize < 20) {
355 std::cerr << "Invalid TCP Header len: " << tcpHeaderSize <<" bytes" << std::endl;
356 return -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700357 }
Junxiao Shic1c2b832016-07-24 20:45:36 +0000358
359 payloadSize -= tcpHeaderSize;
360 payload += tcpHeaderSize;
361
362 if (payloadSize < 0) {
363 std::cerr << "Invalid pcap TCP/IP packet" << std::endl;
364 return -1;
365 }
366
367 os << ", Tunnel Type: TCP";
368 break;
369 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700370 default:
371 return -1;
Junxiao Shi2222a612015-06-06 08:01:38 -0700372 }
Junxiao Shic1c2b832016-07-24 20:45:36 +0000373
374 break;
375 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700376 case /*ETHERTYPE_NDN*/0x7777:
377 os << "Tunnel Type: EthernetFrame";
378 break;
379 case /*ETHERTYPE_NDNLP*/0x8624:
380 os << "Tunnel Type: EthernetFrame";
381 break;
382 case 0x0077: // pcap
383 os << "Tunnel Type: PPP";
384 payloadSize -= 2;
385 payload += 2;
386 break;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000387 default: // do nothing if it is not a recognized type of a packet
Junxiao Shi2222a612015-06-06 08:01:38 -0700388 return -1;
Junxiao Shic1c2b832016-07-24 20:45:36 +0000389 }
Junxiao Shi2222a612015-06-06 08:01:38 -0700390
391 return 0;
392}
393
Junxiao Shic1c2b832016-07-24 20:45:36 +0000394bool
395Ndndump::matchesFilter(const Name& name) const
396{
397 if (nameFilter.empty())
398 return true;
399
400 /// \todo Switch to NDN regular expressions
401
402 return boost::regex_match(name.toUri(), nameFilter);
403}
404
Junxiao Shi3cd47df2015-06-07 20:58:14 -0700405} // namespace dump
Junxiao Shi2222a612015-06-06 08:01:38 -0700406} // namespace ndn