-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathpcapng.cc
More file actions
252 lines (205 loc) · 7.61 KB
/
pcapng.cc
File metadata and controls
252 lines (205 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) 2014-2016, The Regents of the University of California.
// Copyright (c) 2016-2017, Nefeli Networks, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the names of the copyright holders nor the names of their
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#include "pcapng.h"
#include <fcntl.h>
#include <sys/uio.h>
#include <unistd.h>
#include <limits>
#include <glog/logging.h>
#include "../message.h"
#include "../utils/common.h"
#include "../utils/pcapng.h"
#include "../utils/time.h"
using namespace bess::utils::pcapng;
namespace {
// Return `a` rounded up to the nearest multiple of `b`
template <typename T>
T RoundUp(T a, T b) {
return ((a + (b - 1)) / b) * b;
}
// Return the number of bytes of padding to align a buffer long `a` to
// `b` units.
template <typename T>
T PadSize(T a, T b) {
return RoundUp(a, b) - a;
}
// Return the single hex digit representing `nibble`. If it cannot be
// represented, return the char 'X'.
char NibbleToHD(signed char nibble) {
if (nibble >= 0 && nibble <= 9) {
return nibble + '0';
} else if (nibble >= 10 && nibble <= 15) {
return nibble - 10 + 'A';
} else {
return 'X';
}
}
// Represent the buffer `src` (long `len` bytes) as an hex string. Put
// the result into `dst` (which must be at least `len * 2` bytes long).
void BytesToHexDump(const void *src, size_t len, char *dst) {
for (size_t i = 0; i < len; i++) {
char byte = static_cast<const char *>(src)[i];
dst[i * 2] = NibbleToHD((byte >> 4) & 0xF);
dst[i * 2 + 1] = NibbleToHD(byte & 0xF);
}
}
} // namespace
const std::string Pcapng::kName = "PcapNg";
Pcapng::Pcapng()
: bess::GateHook(Pcapng::kName, "pcapng", Pcapng::kPriority),
opener_(),
attrs_(),
attr_template_() {}
// Send the initialization data on the FIFO, once it's open.
bool PcapngOpener::InitFifo(int fd) {
SectionHeaderBlock shb = {
.type = SectionHeaderBlock::kType,
.tot_len = sizeof(shb) + sizeof(uint32_t),
.bom = SectionHeaderBlock::kBom,
.maj_ver = SectionHeaderBlock::kMajVer,
.min_ver = SectionHeaderBlock::kMinVer,
.sec_len = -1,
};
uint32_t shb_tot_len = shb.tot_len;
InterfaceDescriptionBlock idb = {
.type = InterfaceDescriptionBlock::kType,
.tot_len = sizeof(idb) + sizeof(uint32_t),
.link_type = InterfaceDescriptionBlock::kEthernet,
.reserved = 0,
.snap_len = 1518,
};
uint32_t idb_tot_len = idb.tot_len;
struct iovec vec[4] = {{&shb, sizeof(shb)},
{&shb_tot_len, sizeof(shb_tot_len)},
{&idb, sizeof(idb)},
{&idb_tot_len, sizeof(idb_tot_len)}};
// Ideally should check that writev returns the total size,
// but this suffices.
return writev(fd, vec, 4) > 0;
}
CommandResponse Pcapng::Init(const bess::Gate *gate,
const bess::pb::PcapngArg &arg) {
Module *m = gate->module();
std::string tmpl;
size_t i = 0;
for (const auto &it : m->all_attrs()) {
tmpl += it.name + " = ";
size_t tmpl_offset = tmpl.size();
tmpl += std::string(it.size * 2, 'X') + " ";
if (tmpl.size() > std::numeric_limits<uint16_t>::max()) {
// Doesn't fit in the option string.
break;
}
attrs_.emplace_back(Attr{.md_offset = m->attr_offset(i),
.size = it.size,
.tmpl_offset = tmpl_offset});
i++;
}
if (!tmpl.empty() && tmpl.back() == ' ') {
tmpl.pop_back();
}
if (tmpl.size() > std::numeric_limits<uint16_t>::max()) {
tmpl.resize(std::numeric_limits<uint16_t>::max());
}
attr_template_ = std::vector<char>(tmpl.begin(), tmpl.end());
int ret = opener_.Init(arg.fifo(), arg.reconnect());
if (ret < 0) {
return CommandFailure(-errno, "inappropriate reinitialization");
}
ret = arg.defer() ? opener_.OpenInThread() : opener_.OpenNow();
if (ret < 0) {
return CommandFailure(-errno, "Failed to open FIFO");
}
return CommandSuccess();
}
void Pcapng::ProcessBatch(const bess::PacketBatch *batch) {
int fd;
uint32_t gen;
std::tie(fd, gen) = opener_.GetCurrentFd();
if (!opener_.IsValidFd(fd)) {
return;
}
struct timeval tv;
gettimeofday(&tv, nullptr);
uint64_t ts = tv.tv_sec * 1000000 + tv.tv_usec;
uint16_t comment_size = static_cast<uint16_t>(attr_template_.size());
for (int i = 0; i < batch->cnt(); i++) {
bess::Packet *pkt = batch->pkts()[i];
Option opt_comment = {
.code = Option::kComment,
.len = comment_size,
};
for (const Attr &attr : attrs_) {
const char *attr_data = ptr_attr_with_offset<char>(attr.md_offset, pkt);
if (attr_data != nullptr) {
BytesToHexDump(attr_data, attr.size, &attr_template_[attr.tmpl_offset]);
} else {
auto string_it = attr_template_.begin() + attr.tmpl_offset;
std::fill(string_it, string_it + attr.size * 2, 'X');
}
}
Option opt_end = {
.code = Option::kEndOfOpts,
.len = 0,
};
EnhancedPacketBlock epb = {
.type = EnhancedPacketBlock::kType,
.tot_len = static_cast<uint32_t>(
sizeof(epb) + sizeof(uint32_t) +
RoundUp<uint32_t>(pkt->head_len(), 4) + sizeof(opt_comment) +
RoundUp<uint32_t>(comment_size, 4) + sizeof(opt_end)),
.interface_id = 0,
.timestamp_high = static_cast<uint32_t>(ts >> 32),
.timestamp_low = static_cast<uint32_t>(ts),
.captured_len = static_cast<uint32_t>(pkt->head_len()),
.orig_len = static_cast<uint32_t>(pkt->total_len()),
};
uint32_t padding = 0;
uint32_t epb_tot_len = epb.tot_len;
struct iovec vec[8] = {
{&epb, sizeof(epb)},
{pkt->head_data(), static_cast<size_t>(pkt->head_len())},
{&padding, static_cast<size_t>(PadSize(pkt->head_len(), 4))},
{&opt_comment, sizeof(opt_comment)},
{attr_template_.data(), comment_size},
{&padding, static_cast<size_t>(PadSize<uint32_t>(comment_size, 4))},
{&opt_end, sizeof(opt_end)},
{&epb_tot_len, sizeof(epb_tot_len)}};
int ret = writev(fd, vec, 8);
if (ret < 0) {
if (errno == EPIPE) {
DLOG(WARNING) << "Broken pipe: stopping pcapng";
opener_.MarkDead(fd, gen);
}
return;
}
}
}
ADD_GATE_HOOK(Pcapng, "pcapng", "metadata-dump-able packet dump")