-
Notifications
You must be signed in to change notification settings - Fork 6
/
sflow.c
244 lines (190 loc) · 7.78 KB
/
sflow.c
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
#include <stdlib.h>
#include <arpa/inet.h>
#include "sflow.h"
#include "log.h"
#include "extract.h"
#include "trigger.h"
#include "context.h"
static const apermon_config *_config;
ssize_t parse_sflow(const uint8_t *packet, size_t packet_len, sflow_parsed **output) {
const uint8_t *ptr = packet;
const sflow_sample_common *sample_common;
sflow_parsed *parsed_pkt = (sflow_parsed *) malloc(sizeof(sflow_parsed));
sflow_parsed_samples *last_sample = parsed_pkt->samples = NULL, *parsed_sample;
sflow_parsed_elements *last_element, *parsed_element;
uint32_t n_samples, n_elements, i, j, offset, ver, agent_af, sample_len, element_len;
if (packet_len < sizeof(sflow_common_hdr)) {
log_debug("packet too short (got size %zu)\n", packet_len);
goto parse_err;
}
parsed_pkt->common_hdr = (const sflow_common_hdr *) ptr;
ver = ntohl(parsed_pkt->common_hdr->ver);
agent_af = ntohl(parsed_pkt->common_hdr->agent_af);
if (ver != 5) {
log_warn("unsupported sflow version %u\n", ver);
goto parse_err;
}
if (agent_af == SFLOW_AF_INET) {
if (packet_len < sizeof(sflow_inet_hdr)) {
log_warn("bad sflow header length for inet af - want at least %zu, got %zu\n", sizeof(sflow_inet_hdr), packet_len);
goto parse_err;
}
n_samples = ntohl(parsed_pkt->inet_hdr->n_samples);
ptr += sizeof(sflow_inet_hdr);
} else if (agent_af == SFLOW_AF_INET6) {
if (packet_len < sizeof(sflow_inet6_hdr)) {
log_warn("bad sflow header length for inet af - want at least %zu, got %zu\n", sizeof(sflow_inet6_hdr), packet_len);
goto parse_err;
}
n_samples = ntohl(parsed_pkt->inet6_hdr->n_samples);
ptr += sizeof(sflow_inet6_hdr);
} else {
log_warn("unsupported sflow agent af %u\n", parsed_pkt->common_hdr->agent_af);
goto parse_err;
}
// parse sample
for (i = 0; i < n_samples; ++i) {
sample_common = (const sflow_sample_common *) ptr;
if (htonl(sample_common->tag) != SFLOW_SAMPLETYPE_SAMPLE) {
log_debug("ignored sample with unsupported tag 0x%x\n", htonl(sample_common->tag));
goto parse_err;
}
parsed_sample = (sflow_parsed_samples *) malloc(sizeof(sflow_parsed_samples));
last_element = parsed_sample->elements = NULL;
parsed_sample->next = NULL;
if (last_sample == NULL) {
last_sample = parsed_pkt->samples = parsed_sample;
} else {
last_sample->next = parsed_sample;
last_sample = parsed_sample;
}
if ((packet_len - (ptr - packet)) < sizeof(sflow_sample)) {
log_warn(
"unexpected end of packet when parsing sample %u; expecting %u samples, "
"and min record size should be %zu, but only %zu bytes left in packet.\n",
i, n_samples, sizeof(sflow_sample), packet_len - (ptr - packet)
);
goto parse_err;
}
parsed_sample->sample = (const sflow_sample *) ptr;
sample_len = ntohl(parsed_sample->sample->len);
n_elements = ntohl(parsed_sample->sample->n_elements);
if ((packet_len - (ptr - packet)) < sample_len) {
log_warn(
"unexpected end of packet when parsing sample %u; expecting %u samples, "
"the record has size %u, but only %zu bytes left in packet.\n",
i, n_samples, parsed_sample->sample->len, packet_len - (ptr - packet)
);
goto parse_err;
}
ptr += sizeof(sflow_sample);
for (j = 0, offset = 0; j < n_elements; ++j) {
parsed_element = (sflow_parsed_elements *) malloc(sizeof(sflow_parsed_elements));
parsed_element->next = NULL;
if (last_element == NULL) {
last_element = parsed_sample->elements = parsed_element;
} else {
last_element->next = parsed_element;
last_element = parsed_element;
}
if (sample_len - j < sizeof(sflow_sample_element_common)) {
log_warn(
"unexpected end of sample when parsing element %u in sample %u; expecting %u elements: "
"no space left for element at byte %u of sample (byte %zu of packet) - cannot fit element header. "
"want size %zu, but only %u bytes left.\n",
j, i, n_elements, offset, (size_t) (ptr - packet), sizeof(sflow_sample_element_common), sample_len - offset
);
goto parse_err;
}
parsed_element->common_element_hdr = (const sflow_sample_element_common *) ptr;
offset += sizeof(sflow_sample_element_common);
ptr += sizeof(sflow_sample_element_common);
element_len = ntohl(parsed_element->common_element_hdr->len);
if (sample_len - j < element_len) {
log_warn(
"unexpected end of sample when parsing element %u in sample %u; expecting %u samples: "
"no space left for element at byte %u of sample (byte %zu of packet) - element length is %u, "
"but only %u bytes left.\n",
j, i, n_elements, offset, (size_t) (ptr - packet), element_len, sample_len - offset
);
goto parse_err;
}
ptr += element_len;
offset += element_len;
}
}
*output = parsed_pkt;
return ptr - packet;
parse_err:
free_sflow(parsed_pkt);
return -1;
}
void free_sflow(sflow_parsed *parsed_pkt) {
if (parsed_pkt == NULL) {
return;
}
sflow_parsed_samples *sample = parsed_pkt->samples, *last_sample = NULL;
while (sample != NULL) {
if (last_sample != NULL) {
free(last_sample);
}
sflow_parsed_elements *element = sample->elements, *last_element = NULL;
while (element != NULL) {
if (last_element != NULL) {
free(last_element);
}
last_element = element;
element = element->next;
}
free(last_element);
last_sample = sample;
sample = sample->next;
}
free(last_sample);
free(parsed_pkt);
}
void init_sflow(const apermon_config *config) {
_config = config;
}
ssize_t handle_sflow_packet(const uint8_t *packet, size_t packet_len) {
sflow_parsed *parsed = NULL;
apermon_flows *flows = NULL;
ssize_t ret, pased_len;
apermon_config_triggers *trigger = _config->triggers;
char agent_addr[INET6_ADDRSTRLEN + 1];
const apermon_config_agents *a = NULL;
pased_len = parse_sflow(packet, packet_len, &parsed);
if (pased_len < 0) {
return pased_len;
}
ret = extract_flows(parsed, &flows);
if (ret < 0) {
return ret;
}
if (flows->agent_af == SFLOW_AF_INET) {
a = hash32_find(_config->agents_hash, &flows->agent_inet);
} else if (flows->agent_af == SFLOW_AF_INET6) {
a = hash128_find(_config->agents_hash, flows->agent_inet6);
} else {
log_error("internal error: agent af invalid (%u).\n", flows->agent_af);
}
if (a == NULL) {
if (flows->agent_af == SFLOW_AF_INET) {
inet_ntop(AF_INET, &flows->agent_inet, agent_addr, sizeof(agent_addr));
} else {
inet_ntop(AF_INET6, flows->agent_inet6, agent_addr, sizeof(agent_addr));
}
log_warn("got flow from unknown agent '%s'.\n", agent_addr);
goto skip_packet;
}
flows->agent = a;
log_debug("sflow packet from %s\n", a->name);
while (trigger != NULL) {
run_trigger(trigger, flows);
trigger = trigger->next;
}
skip_packet:
free_apermon_flows(flows);
free_sflow(parsed);
return pased_len;
}