-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflowtable.c
170 lines (147 loc) · 5.1 KB
/
flowtable.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
/* Copyright 2014 Andrew Bates
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "nf_time.h"
#include "flowtable.h"
#include "netflow.h"
flowrecord *flow_retrieve(flowtable *cache, flowtable *table, ipv4_tuple *tuple, unsigned int table_len) {
unsigned int table_index = hash(tuple, 0xebebebeb) % table_len;
flow_entry *entry = table[table_index].head;
flowrecord *fr = NULL;
while (entry != NULL) {
if (memcmp(&entry->record->tuple, tuple, sizeof(ipv4_tuple)) == 0) {
break;
}
entry = entry->next;
}
/**
* if a matching flow record was not found
* then create a new one
*/
if (entry == NULL) {
fr = malloc(sizeof(flowrecord));
if (fr == NULL) {
fprintf(stderr, "Memory allocation falure\n");
exit(-1);
}
memset(&fr->nf_record, 0, sizeof(nf_v5_record_t));
memcpy(&fr->tuple, tuple, sizeof(ipv4_tuple));
fr->table_id = table_index;
fr->flow_table.next = NULL;
fr->flow_table.previous = NULL;
fr->flow_table.record = fr;
fr->flow_cache.next = NULL;
fr->flow_cache.previous = NULL;
fr->flow_cache.record = fr;
fr->nf_record.first = 0;
fr->nf_record.last = 0;
fr->nf_record.source_ip = tuple->source_ip;
fr->nf_record.destination_ip = tuple->destination_ip;
fr->nf_record.source_port = tuple->prot.port.source_port;
fr->nf_record.destination_port = tuple->prot.port.destination_port;
fr->nf_record.protocol = htons(tuple->protocol);
flow_insert(cache, &table[table_index], fr);
} else {
fr = entry->record;
}
return fr;
}
void flow_insert(flowtable *cache, flowtable *table, flowrecord *flowrecord) {
flow_entry *cache_entry = &flowrecord->flow_cache;
flow_entry *table_entry = &flowrecord->flow_table;
if (table->head == NULL) {
table->head = table_entry;
table->tail = table_entry;
} else {
table_entry->previous = table->tail;
table->tail->next = table_entry;
table->tail = table_entry;
}
if (cache->head == NULL) {
cache->head = cache_entry;
cache->tail = cache_entry;
} else {
cache->head->previous = cache_entry;
cache_entry->next = cache->head;
cache->head = cache_entry;
}
}
void flow_refresh(flowtable *cache, flowrecord *flowrecord) {
struct flow_entry *flow_entry = &flowrecord->flow_cache;
/* do nothing if the previous pointer is null since the
* entry is already at the top
*/
if (flow_entry->previous != NULL) {
/* Remove the entry from the bottom of the list */
if (flow_entry->next == NULL) {
flow_entry->previous->next = NULL;
cache->tail = flow_entry->previous;
/* Remove the entry from the middle of the list */
} else {
flow_entry->previous->next = flow_entry->next;
flow_entry->next->previous = flow_entry->previous;
}
/* Move it to the top of the stack */
flow_entry->previous = NULL;
flow_entry->next = cache->head;
cache->head->previous = flow_entry;
cache->head = flow_entry;
}
}
void flow_expire(nf_peer_t *nf_peer, flowtable *cache, flowtable *table, unsigned long expiration) {
flow_entry *export_cache;
flow_entry *export_table;
nf_v5_packet_t nf_packet;
flowrecord *record;
unsigned int num_records = 0;
while(cache->tail != NULL && cache->tail->record->nf_record.last < expiration) {
export_cache = cache->tail;
export_table = &export_cache->record->flow_table;
record = export_cache->record;
memcpy(&nf_packet.records[num_records], &record->nf_record, sizeof(nf_v5_record_t));
num_records++;
if (num_records == 30) {
nf_export(nf_peer, &nf_packet, num_records);
num_records = 0;
}
/* Remove the entry from the bottom of the stack */
cache->tail = export_cache->previous;
if (cache->tail != NULL) {
cache->tail->next = NULL;
}
/* Remove the entry from the flow table */
if (export_table->next == NULL && export_table->previous == NULL) {
table[record->table_id].head = NULL;
table[record->table_id].tail = NULL;
} else if (export_table->previous == NULL) {
export_table->next->previous = NULL;
table[record->table_id].head = export_table->next;
} else if (export_table->next == NULL) {
export_table->previous->next = NULL;
table[record->table_id].tail = export_table->previous;
} else {
export_table->previous->next = export_table->next;
export_table->next->previous = export_table->previous;
}
/* Free the previously allocated record */
free(record);
}
if (num_records > 0) {
nf_export(nf_peer, &nf_packet, num_records);
}
}