forked from CESNET/ipfixprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unirecexporter.cpp
372 lines (316 loc) · 11.3 KB
/
unirecexporter.cpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* \file unirecexporter.cpp
* \brief Flow exporter converting flows to UniRec and sending them to TRAP ifc
* \author Vaclav Bartos <[email protected]>
* \author Jiri Havranek <[email protected]>
* \date 2014
* \date 2015
* \date 2016
*/
/*
* Copyright (C) 2014-2016 CESNET
*
* LICENSE TERMS
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``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 company 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 <config.h>
#ifdef WITH_NEMEA
#include <string>
#include <vector>
#include <algorithm>
#include <libtrap/trap.h>
#include <unirec/unirec.h>
#include "unirecexporter.h"
#include "fields.h"
#include "flowexporter.h"
#include "flowifc.h"
#include "ipfixprobe.h"
using namespace std;
#define BASIC_FLOW_TEMPLATE "SRC_IP,DST_IP,SRC_PORT,DST_PORT,PROTOCOL,PACKETS,BYTES,PACKETS_REV,BYTES_REV,TIME_FIRST,TIME_LAST,TCP_FLAGS,TCP_FLAGS_REV,DIR_BIT_FIELD,SRC_MAC,DST_MAC" /* LINK_BIT_FIELD or ODID will be added at init. */
#define PACKET_TEMPLATE "SRC_MAC,DST_MAC,ETHERTYPE,TIME"
UR_FIELDS (
ipaddr DST_IP,
ipaddr SRC_IP,
uint64 BYTES,
uint64 BYTES_REV,
uint64 LINK_BIT_FIELD,
uint32 ODID,
time TIME_FIRST,
time TIME_LAST,
uint32 PACKETS,
uint32 PACKETS_REV,
uint16 DST_PORT,
uint16 SRC_PORT,
uint8 DIR_BIT_FIELD,
uint8 PROTOCOL,
uint8 TCP_FLAGS,
uint8 TCP_FLAGS_REV,
macaddr SRC_MAC,
macaddr DST_MAC,
uint16 ETHERTYPE
time TIME,
)
/**
* \brief Constructor.
*/
UnirecExporter::UnirecExporter(bool send_eof) : out_ifc_cnt(0), ifc_mapping(NULL),
tmplts(NULL), records(NULL), eof(send_eof), send_odid(false), dir_bit_field(0)
{
flows_seen = 0;
flows_dropped = 0;
}
UnirecExporter::~UnirecExporter()
{
close();
}
/**
* \brief Initialize exporter.
* \param [in] plugins Active plugins.
* \param [in] ifc_cnt Output interface count.
* \param [in] basic_ifc_num Basic output interface number.
* \param [in] link Link bit field value.
* \param [in] dir Direction bit field value.
* \param [in] odid Send ODID field instead of LINK_BIT_FIELD.
* \return 0 on success or negative value when error occur.
*/
int UnirecExporter::init(const vector<FlowCachePlugin *> &plugins, int ifc_cnt, int basic_ifc_number, uint64_t link = 0, uint8_t dir = 0, bool odid = false)
{
string basic_tmplt = BASIC_FLOW_TEMPLATE;
out_ifc_cnt = ifc_cnt;
basic_ifc_num = basic_ifc_number;
link_bit_field = link;
dir_bit_field = dir;
send_odid = odid;
tmplts = new ur_template_t*[out_ifc_cnt];
records = new void*[out_ifc_cnt];
for (int i = 0; i < out_ifc_cnt; i++) {
tmplts[i] = NULL;
records[i] = NULL;
}
if (odid) {
basic_tmplt += ",ODID";
} else {
basic_tmplt += ",LINK_BIT_FIELD";
}
char *error = NULL;
if (basic_ifc_num >= 0) {
tmplts[basic_ifc_num] = ur_create_output_template(basic_ifc_num, basic_tmplt.c_str(), &error);
if (tmplts[basic_ifc_num] == NULL) {
fprintf(stderr, "UnirecExporter: %s\n", error);
free(error);
free_unirec_resources();
return -2;
}
}
ifc_mapping = new int[EXTENSION_CNT];
for (int i = 0; i < EXTENSION_CNT; i++) {
ifc_mapping[i] = -1;
}
string template_str;
for (unsigned int i = 0; i < plugins.size(); i++) {
FlowCachePlugin * const tmp = plugins[i];
const vector<plugin_opt> &opts = tmp->get_options();
int ifc = -1;
for (unsigned int j = 0; j < opts.size(); j++) { // Create plugin extension id -> output interface mapping.
ifc_mapping[opts[j].ext_type] = opts[j].out_ifc_num;
ifc = opts[j].out_ifc_num;
}
if (opts.size() == 0 || ifc < 0) {
continue;
}
// Create unirec templates.
template_str = tmp->get_unirec_field_string() + string(",") + basic_tmplt;
tmplts[ifc] = ur_create_output_template(ifc, template_str.c_str(), &error);
if (tmplts[ifc] == NULL) {
fprintf(stderr, "UnirecExporter: %s\n", error);
free(error);
free_unirec_resources();
return -2;
}
}
for (int i = 0; i < out_ifc_cnt; i++) { // Create unirec records.
if (tmplts[i] != NULL) {
records[i] = ur_create_record(tmplts[i], (i == basic_ifc_num ? 0 : UR_MAX_SIZE));
if (records[i] == NULL) {
free_unirec_resources();
return -3;
}
}
}
return 0;
}
/**
* \brief Close connection and free resources.
*/
void UnirecExporter::close()
{
if (eof) {
for (int i = 0; i < out_ifc_cnt; i++) {
trap_send(i, "", 1);
}
}
trap_finalize();
free_unirec_resources();
basic_ifc_num = -1;
out_ifc_cnt = 0;
}
/**
* \brief Free unirec templates and unirec records.
*/
void UnirecExporter::free_unirec_resources()
{
if (tmplts) {
for (int i = 0; i < out_ifc_cnt; i++) {
if (tmplts[i] != NULL) {
ur_free_template(tmplts[i]);
}
}
delete [] tmplts;
tmplts = NULL;
}
if (records) {
for (int i = 0; i < out_ifc_cnt; i++) {
if (records[i] != NULL) {
ur_free_record(records[i]);
}
}
delete [] records;
records = NULL;
}
if (ifc_mapping) {
delete [] ifc_mapping;
ifc_mapping = NULL;
}
}
int UnirecExporter::export_packet(Packet &pkt)
{
RecordExt *ext = pkt.exts;
ur_template_t *tmplt_ptr = NULL;
void *record_ptr = NULL;
while (ext != NULL) {
flows_seen++;
int ifc_num = ifc_mapping[ext->extType];
if (ifc_num >= 0) {
tmplt_ptr = tmplts[ifc_num];
record_ptr = records[ifc_num];
ur_clear_varlen(tmplt_ptr, record_ptr);
memset(record_ptr, 0, ur_rec_fixlen_size(tmplt_ptr));
fill_packet_fields(pkt, tmplt_ptr, record_ptr);
ext->fillUnirec(tmplt_ptr, record_ptr); /* Add each extension header into unirec record. */
trap_send(ifc_num, record_ptr, ur_rec_fixlen_size(tmplt_ptr) + ur_rec_varlen_size(tmplt_ptr, record_ptr));
}
ext = ext->next;
}
return 0;
}
int UnirecExporter::export_flow(Flow &flow)
{
RecordExt *ext = flow.exts;
ur_template_t *tmplt_ptr = NULL;
void *record_ptr = NULL;
if (basic_ifc_num >= 0) { // Process basic flow.
tmplt_ptr = tmplts[basic_ifc_num];
record_ptr = records[basic_ifc_num];
ur_clear_varlen(tmplt_ptr, record_ptr);
fill_basic_flow(flow, tmplt_ptr, record_ptr);
trap_send(basic_ifc_num, record_ptr, ur_rec_fixlen_size(tmplt_ptr) + ur_rec_varlen_size(tmplt_ptr, record_ptr));
}
while (ext != NULL) {
flows_seen++;
int ifc_num = ifc_mapping[ext->extType];
if (ifc_num >= 0) {
tmplt_ptr = tmplts[ifc_num];
record_ptr = records[ifc_num];
ur_clear_varlen(tmplt_ptr, record_ptr);
memset(record_ptr, 0, ur_rec_fixlen_size(tmplt_ptr));
fill_basic_flow(flow, tmplt_ptr, record_ptr);
ext->fillUnirec(tmplt_ptr, record_ptr); /* Add each extension header into unirec record. */
trap_send(ifc_num, record_ptr, ur_rec_fixlen_size(tmplt_ptr) + ur_rec_varlen_size(tmplt_ptr, record_ptr));
}
ext = ext->next;
}
return 0;
}
/**
* \brief Fill record with basic flow fields.
* \param [in] flow Flow record.
* \param [in] tmplt_ptr Pointer to unirec template.
* \param [out] record_ptr Pointer to unirec record.
*/
void UnirecExporter::fill_basic_flow(Flow &flow, ur_template_t *tmplt_ptr, void *record_ptr)
{
ur_time_t tmp_time;
if (flow.ip_version == 4) {
ur_set(tmplt_ptr, record_ptr, F_SRC_IP, ip_from_4_bytes_be((char *) &flow.src_ip.v4));
ur_set(tmplt_ptr, record_ptr, F_DST_IP, ip_from_4_bytes_be((char *) &flow.dst_ip.v4));
} else {
ur_set(tmplt_ptr, record_ptr, F_SRC_IP, ip_from_16_bytes_be((char *) flow.src_ip.v6));
ur_set(tmplt_ptr, record_ptr, F_DST_IP, ip_from_16_bytes_be((char *) flow.dst_ip.v6));
}
tmp_time = ur_time_from_sec_usec(flow.time_first.tv_sec, flow.time_first.tv_usec);
ur_set(tmplt_ptr, record_ptr, F_TIME_FIRST, tmp_time);
tmp_time = ur_time_from_sec_usec(flow.time_last.tv_sec, flow.time_last.tv_usec);
ur_set(tmplt_ptr, record_ptr, F_TIME_LAST, tmp_time);
if (send_odid) {
ur_set(tmplt_ptr, record_ptr, F_ODID, link_bit_field);
} else {
ur_set(tmplt_ptr, record_ptr, F_LINK_BIT_FIELD, link_bit_field);
}
ur_set(tmplt_ptr, record_ptr, F_DIR_BIT_FIELD, dir_bit_field);
ur_set(tmplt_ptr, record_ptr, F_PROTOCOL, flow.ip_proto);
ur_set(tmplt_ptr, record_ptr, F_SRC_PORT, flow.src_port);
ur_set(tmplt_ptr, record_ptr, F_DST_PORT, flow.dst_port);
ur_set(tmplt_ptr, record_ptr, F_PACKETS, flow.src_pkt_total_cnt);
ur_set(tmplt_ptr, record_ptr, F_BYTES, flow.src_octet_total_length);
ur_set(tmplt_ptr, record_ptr, F_TCP_FLAGS, flow.src_tcp_control_bits);
ur_set(tmplt_ptr, record_ptr, F_PACKETS_REV, flow.dst_pkt_total_cnt);
ur_set(tmplt_ptr, record_ptr, F_BYTES_REV, flow.dst_octet_total_length);
ur_set(tmplt_ptr, record_ptr, F_TCP_FLAGS_REV, flow.dst_tcp_control_bits);
ur_set(tmplt_ptr, record_ptr, F_DST_MAC, mac_from_bytes(flow.dst_mac));
ur_set(tmplt_ptr, record_ptr, F_SRC_MAC, mac_from_bytes(flow.src_mac));
}
/**
* \brief Fill record with basic flow fields.
* \param [in] flow Flow record.
* \param [in] tmplt_ptr Pointer to unirec template.
* \param [out] record_ptr Pointer to unirec record.
*/
void UnirecExporter::fill_packet_fields(Packet &pkt, ur_template_t *tmplt_ptr, void *record_ptr)
{
ur_time_t tmp_time = ur_time_from_sec_usec(pkt.timestamp.tv_sec, pkt.timestamp.tv_usec);
ur_set(tmplt_ptr, record_ptr, F_DST_MAC, mac_from_bytes((uint8_t *) pkt.packet));
ur_set(tmplt_ptr, record_ptr, F_SRC_MAC, mac_from_bytes((uint8_t *) pkt.packet + 6));
ur_set(tmplt_ptr, record_ptr, F_ETHERTYPE, pkt.ethertype);
ur_set(tmplt_ptr, record_ptr, F_TIME, tmp_time);
}
#endif