-
Notifications
You must be signed in to change notification settings - Fork 6
/
condition.c
189 lines (152 loc) · 6.27 KB
/
condition.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
#include <string.h>
#include <stdlib.h>
#include "prefix-list.h"
#include "condition.h"
#include "log.h"
static apermon_context *_ctx;
int cond_list(const apermon_flow_record* record, const void* arg /* apermon_cond_list* */) {
const apermon_cond_list *cl = (apermon_cond_list *) arg;
const apermon_cond_func_list *f = cl->funcs;
if (cl->type == APERMON_COND_AND) {
while (f != NULL) {
if (!f->func(record, f->arg)) {
return 0;
}
f = f->next;
}
return 1;
} else if (cl->type == APERMON_COND_OR) {
while (f != NULL) {
if (f->func(record, f->arg)) {
return 1;
}
f = f->next;
}
return 0;
} else if (cl->type == APERMON_COND_NOT) {
while (f != NULL) {
if (f->func(record, f->arg)) {
return 0;
}
f = f->next;
}
return 1;
} else {
log_error("unknown cond type %d.\n", cl->type);
}
return 0;
}
int cond_in_interface(const apermon_flow_record* record, const void* arg /* apermon_config_interfaces** */) {
apermon_config_interfaces *iface = *(apermon_config_interfaces **) arg;
apermon_config_ifindexes *ifindex = iface->ifindexes;
while (ifindex != NULL) {
if (ifindex->agent != _ctx->current_flows->agent) {
ifindex = ifindex->next;
continue;
}
if (ifindex->ifindex == record->in_ifindex) {
return 1;
}
ifindex = ifindex->next;
}
return 0;
}
int cond_out_interface(const apermon_flow_record* record, const void* arg /* apermon_config_interfaces** */) {
apermon_config_interfaces *iface = *(apermon_config_interfaces **) arg;
apermon_config_ifindexes *ifindex = iface->ifindexes;
while (ifindex != NULL) {
if (ifindex->agent != _ctx->current_flows->agent) {
ifindex = ifindex->next;
continue;
}
if (ifindex->ifindex == record->out_ifindex) {
return 1;
}
ifindex = ifindex->next;
}
return 0;
}
int cond_src(const apermon_flow_record* record, const void* arg /* apermon_config_prefix_list_elements** */) {
const apermon_config_prefix_list_elements *l = *(apermon_config_prefix_list_elements **) arg;
if (record->flow_af == SFLOW_AF_INET) {
return apermon_prefix_list_match_inet(l, record->src_inet) != NULL;
} else if (record->flow_af == SFLOW_AF_INET6) {
return apermon_prefix_list_match_inet6(l, record->src_inet6) != NULL;
}
log_error("unknown flow address family %d.\n", record->flow_af);
return 0;
}
int cond_dst(const apermon_flow_record* record, const void* arg /* apermon_config_prefix_list_elements** */) {
const apermon_config_prefix_list_elements *l = *(apermon_config_prefix_list_elements **) arg;
if (record->flow_af == SFLOW_AF_INET) {
return apermon_prefix_list_match_inet(l, record->dst_inet) != NULL;
} else if (record->flow_af == SFLOW_AF_INET6) {
return apermon_prefix_list_match_inet6(l, record->dst_inet6) != NULL;
}
log_error("unknown flow address family %d.\n", record->flow_af);
return 0;
}
int cond_proto(const apermon_flow_record* record, const void* arg /* uint8_t* */) {
return (* (uint8_t *) arg) == record->l3_proto;
}
int cond_src_port(const apermon_flow_record* record, const void* arg /* uint16_t* */) {
return (* (uint16_t *) arg) == record->src_port;
}
int cond_dst_port(const apermon_flow_record* record, const void* arg /* uint16_t* */) {
return (* (uint16_t *) arg) == record->dst_port;
}
int cond_is_fragment(const apermon_flow_record* record, const void* arg /* unused */) {
(void) arg;
if (record->flow_af == SFLOW_AF_INET6) {
return 0;
}
return record->frag_off != 0 || record->mf_bit;
}
void cond_begin(apermon_context *ctx) {
_ctx = ctx;
}
void select_flow(const apermon_flow_record *flow) {
const apermon_config_triggers *t = _ctx->trigger_config;
const apermon_config_prefix_lists_set *ls = t->networks;
const apermon_config_prefix_list_elements *ps;
const apermon_prefix *pfx;
while (ls != NULL) {
ps = ls->prefix_list->elements;
if (flow->flow_af == SFLOW_AF_INET) {
if ((t->flags & APERMON_TRIGGER_CHECK_INGRESS) && (pfx = apermon_prefix_list_match_inet(ps, flow->dst_inet))) {
_ctx->flow_directions[_ctx->n_selected] = FLOW_INGRESS;
_ctx->selected_flows[_ctx->n_selected] = flow;
_ctx->selected_prefixes[_ctx->n_selected] = pfx;
_ctx->selected_prefix_lists[_ctx->n_selected] = ls->prefix_list;
_ctx->n_selected++;
break;
} else if ((t->flags & APERMON_TRIGGER_CHECK_EGRESS) && (pfx = apermon_prefix_list_match_inet(ps, flow->src_inet))) {
_ctx->flow_directions[_ctx->n_selected] = FLOW_EGRESS;
_ctx->selected_flows[_ctx->n_selected] = flow;
_ctx->selected_prefixes[_ctx->n_selected] = pfx;
_ctx->selected_prefix_lists[_ctx->n_selected] = ls->prefix_list;
_ctx->n_selected++;
break;
}
} else if (flow->flow_af == SFLOW_AF_INET6) {
if ((t->flags & APERMON_TRIGGER_CHECK_INGRESS) && (pfx = apermon_prefix_list_match_inet6(ps, flow->dst_inet6))) {
_ctx->flow_directions[_ctx->n_selected] = FLOW_INGRESS;
_ctx->selected_flows[_ctx->n_selected] = flow;
_ctx->selected_prefixes[_ctx->n_selected] = pfx;
_ctx->selected_prefix_lists[_ctx->n_selected] = ls->prefix_list;
_ctx->n_selected++;
break;
} else if ((t->flags & APERMON_TRIGGER_CHECK_EGRESS) && (pfx = apermon_prefix_list_match_inet6(ps, flow->src_inet6))) {
_ctx->flow_directions[_ctx->n_selected] = FLOW_EGRESS;
_ctx->selected_flows[_ctx->n_selected] = flow;
_ctx->selected_prefixes[_ctx->n_selected] = pfx;
_ctx->selected_prefix_lists[_ctx->n_selected] = ls->prefix_list;
_ctx->n_selected++;
break;
}
} else {
log_error("bad af: %d\n", flow->flow_af);
}
ls = ls->next;
}
}