Skip to content

Commit 0900de6

Browse files
committed
Introduce mpls plugin
1 parent 88aa6cc commit 0900de6

File tree

3 files changed

+197
-1
lines changed

3 files changed

+197
-1
lines changed

Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ ipfixprobe_process_src=\
134134
process/nettisa.hpp \
135135
process/nettisa.cpp \
136136
process/flow_hash.hpp \
137-
process/flow_hash.cpp
137+
process/flow_hash.cpp \
138+
process/mpls.hpp \
139+
process/mpls.cpp
138140

139141
if WITH_QUIC
140142
ipfixprobe_process_src+=\

process/mpls.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* \file mpls.cpp
3+
* \brief Plugin for parsing mpls traffic.
4+
* \author Jakub Antonín Štigler [email protected]
5+
* \date 2023
6+
*/
7+
/*
8+
* Copyright (C) 2023 CESNET
9+
*
10+
* LICENSE TERMS
11+
*
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions
14+
* are met:
15+
* 1. Redistributions of source code must retain the above copyright
16+
* notice, this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright
18+
* notice, this list of conditions and the following disclaimer in
19+
* the documentation and/or other materials provided with the
20+
* distribution.
21+
* 3. Neither the name of the Company nor the names of its contributors
22+
* may be used to endorse or promote products derived from this
23+
* software without specific prior written permission.
24+
*
25+
*
26+
*
27+
*/
28+
29+
#include <iostream>
30+
31+
#include "mpls.hpp"
32+
33+
namespace ipxp {
34+
35+
int RecordExtMPLS::REGISTERED_ID = -1;
36+
37+
__attribute__((constructor)) static void register_this_plugin()
38+
{
39+
static PluginRecord rec = PluginRecord("mpls", [](){return new MPLSPlugin();});
40+
register_plugin(&rec);
41+
RecordExtMPLS::REGISTERED_ID = register_extension();
42+
}
43+
44+
ProcessPlugin *MPLSPlugin::copy()
45+
{
46+
return new MPLSPlugin(*this);
47+
}
48+
49+
int MPLSPlugin::post_create(Flow &rec, const Packet &pkt)
50+
{
51+
if (pkt.mplsTop == 0) {
52+
return 0;
53+
}
54+
55+
auto ext = new RecordExtMPLS();
56+
ext->mpls = pkt.mplsTop;
57+
58+
rec.add_extension(ext);
59+
return 0;
60+
}
61+
62+
}
63+

process/mpls.hpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* \file mpls.hpp
3+
* \brief Plugin for parsing mpls traffic.
4+
* \author Jakub Antonín Štigler [email protected]
5+
* \date 2023
6+
*/
7+
/*
8+
* Copyright (C) 2023 CESNET
9+
*
10+
* LICENSE TERMS
11+
*
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions
14+
* are met:
15+
* 1. Redistributions of source code must retain the above copyright
16+
* notice, this list of conditions and the following disclaimer.
17+
* 2. Redistributions in binary form must reproduce the above copyright
18+
* notice, this list of conditions and the following disclaimer in
19+
* the documentation and/or other materials provided with the
20+
* distribution.
21+
* 3. Neither the name of the Company nor the names of its contributors
22+
* may be used to endorse or promote products derived from this
23+
* software without specific prior written permission.
24+
*
25+
*
26+
*
27+
*/
28+
29+
#ifndef IPXP_PROCESS_MPLS_HPP
30+
#define IPXP_PROCESS_MPLS_HPP
31+
32+
#include <cstring>
33+
#include <string>
34+
#include <sstream>
35+
36+
#ifdef WITH_NEMEA
37+
#include "fields.h"
38+
#endif
39+
40+
#include <ipfixprobe/process.hpp>
41+
#include <ipfixprobe/flowifc.hpp>
42+
#include <ipfixprobe/packet.hpp>
43+
#include <ipfixprobe/ipfix-elements.hpp>
44+
45+
namespace ipxp {
46+
47+
#define MPLS_LABEL_SECTION_LENGTH 3
48+
49+
#define MPLS_UNIREC_TEMPLATE "MPLS_TOP_LABEL_STACK_SECTION"
50+
51+
UR_FIELDS (
52+
bytes MPLS_TOP_LABEL_STACK_SECTION
53+
)
54+
55+
/**
56+
* \brief Flow record extension header for storing parsed MPLS data.
57+
*/
58+
struct RecordExtMPLS : public RecordExt {
59+
static int REGISTERED_ID;
60+
61+
// Contents are (from MSb to LSb):
62+
// 20-bit - Label,
63+
// 3-bit - Traffic class / EXP,
64+
// 1-bit - Bottom of stack (true if last),
65+
// 8-bit - TTL (Time To Live)
66+
uint32_t mpls;
67+
68+
RecordExtMPLS() : RecordExt(REGISTERED_ID), mpls(0) { }
69+
70+
#ifdef WITH_NEMEA
71+
void fill_unirec(ur_template_t *tmplt, void *record) override
72+
{
73+
auto v = htonl(mpls);
74+
auto arr = reinterpret_cast<uint8_t *>(&v);
75+
ur_set_var(tmplt, record, F_MPLS_TOP_LABEL_STACK_SECTION, arr, MPLS_LABEL_SECTION_LENGTH);
76+
}
77+
78+
const char *get_unirec_tmplt() const
79+
{
80+
return MPLS_UNIREC_TEMPLATE;
81+
}
82+
#endif
83+
84+
int fill_ipfix(uint8_t *buffer, int size) override
85+
{
86+
if (size < MPLS_LABEL_SECTION_LENGTH + 1) {
87+
return -1;
88+
}
89+
90+
buffer[0] = MPLS_LABEL_SECTION_LENGTH;
91+
auto v = htonl(mpls);
92+
auto arr = reinterpret_cast<uint8_t *>(&v);
93+
memcpy(buffer + 2, arr, MPLS_LABEL_SECTION_LENGTH);
94+
95+
return MPLS_LABEL_SECTION_LENGTH + 1;
96+
}
97+
98+
const char **get_ipfix_tmplt() const
99+
{
100+
static const char *ipfix_template[] = {
101+
IPFIX_MPLS_TEMPLATE(IPFIX_FIELD_NAMES)
102+
NULL
103+
};
104+
return ipfix_template;
105+
}
106+
107+
std::string get_text() const
108+
{
109+
std::ostringstream out;
110+
out << "mpls_label_1=\"" << (mpls >> 8) << '"';
111+
return out.str();
112+
}
113+
};
114+
115+
/**
116+
* \brief Process plugin for parsing MPLS packets.
117+
*/
118+
class MPLSPlugin : public ProcessPlugin
119+
{
120+
public:
121+
OptionsParser *get_parser() const { return new OptionsParser("mpls", "Parse MPLS traffic"); }
122+
std::string get_name() const { return "mpls"; }
123+
RecordExt *get_ext() const { return new RecordExtMPLS(); }
124+
ProcessPlugin *copy();
125+
126+
int post_create(Flow &rec, const Packet &pkt);
127+
};
128+
129+
}
130+
#endif /* IPXP_PROCESS_MPLS_HPP */
131+

0 commit comments

Comments
 (0)