-
Notifications
You must be signed in to change notification settings - Fork 8
/
pf_nattrack.c
383 lines (324 loc) · 10 KB
/
pf_nattrack.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
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
372
373
374
375
376
377
378
379
380
381
382
383
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/queue.h>
#include <sys/ioctl.h>
// network libs
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/pfvar.h>
#include <arpa/inet.h>
#include <net/altq/altq.h>
#include <sys/sysctl.h>
#include <netdb.h>
#include "pf_nattrack.h"
#include "hash.h"
u_long pf_hashmask;
static u_long pf_hashsize;
static uint32_t pf_hashseed;
struct pf_nattrack_hash *pfnt_hash;
/*
* hashkey()
*
* create an hash to index the pf states represeting NAT connections
*/
uint32_t hashkey(struct pf_nattrack *nt) {
uint32_t h;
h = jenkins_hash32((uint32_t *)&nt->c,
sizeof(struct conn)/sizeof(uint32_t),
pf_hashseed);
return (h & pf_hashmask);
}
/* initialize()
*
* function used to initialize some data structures of the program
*/
void initialize() {
//TUNABLE_ULONG_FETCH("net.pf.states_hashsize", &pf_hashsize);
if (pf_hashsize == 0 || !powerof2(pf_hashsize))
pf_hashsize = 32768;
pf_hashmask = pf_hashsize - 1;
pf_hashseed = arc4random();
pfnt_hash = (struct pf_nattrack_hash *)calloc(sizeof(struct pf_nattrack_hash), pf_hashsize);
}
/*
* printerror()
*
* function used to print out an error message
*/
static void
printerror(char *s)
{
char *msg;
msg = strerror(errno);
fprintf(stderr, "ERROR: %s: %s\n", s, msg);
return;
}
/*
* print_nattrack()
*
* print out the NAT tuple
*/
void print_nattrack(struct pf_nattrack *nt, int opts) {
char buf[INET_ADDRSTRLEN];
time_t rawtime;
struct tm * timeinfo;
char fmttime[80];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime(fmttime,80,"%Y-%m-%d,%H:%M:%S",timeinfo);
if (!nt) return;
switch (nt->af) {
case AF_INET:
// date/time and protocol
printf("%s proto=%u", fmttime, nt->proto);
// original source address and port
printf(" osrc=");
if (inet_ntop(nt->af, &nt->c.osrc, buf, sizeof(buf)) == NULL)
printf("?");
else
printf("%s", buf);
printf(":%u", nt->c.osport);
// translated source address and port
printf(" tsrc=");
if (inet_ntop(nt->af, &nt->c.tsrc, buf, sizeof(buf)) == NULL)
printf("?");
else
printf("%s", buf);
printf(":%u", nt->c.tsport);
// original destination address and port
printf(" odst=");
if (inet_ntop(nt->af, &nt->c.odst, buf, sizeof(buf)) == NULL)
printf("?");
else
printf("%s", buf);
printf(":%u", nt->c.odport);
// translated destination address and port
printf(" tdst=");
if (inet_ntop(nt->af, &nt->c.tdst, buf, sizeof(buf)) == NULL)
printf("?");
else
printf("%s", buf);
printf(":%u", nt->c.tdport);
printf(" duration=%u", nt->duration);
// TODO: should store interface?
printf("\n");
break;
default:
printf("ERROR: unknown or unsupportted address family\n");
}
}
void free_list(struct pf_nattrack_list **l) {
struct pf_nattrack_list *item;
struct pf_nattrack_hash *pfnth;
while(*l) {
item = *l;
print_nattrack(item->nt, 0);
pfnth = &pfnt_hash[hashkey(item->nt)];
ldel(&pfnth->list, item->ref);
ldel(l, item);
free(item->ref);
free(item->nt);
free(item);
}
}
uint8_t convert_state(struct pfsync_state *state, struct pf_nattrack *node) {
struct pfsync_state_key *orig, *trans;
uint8_t src, dst;
if (state->direction == PF_OUT) {
src = 1; dst = 0;
orig = &state->key[PF_SK_STACK];
trans = &state->key[PF_SK_WIRE];
} else {
src = 0; dst = 1;
orig = &state->key[PF_SK_WIRE];
trans = &state->key[PF_SK_STACK];
}
// check if it is a NAT:
// key_wire == key_stack --> NO NAT
// key_wire != key_stack --> NAT
if (state->af != AF_INET ||
(PF_AEQ(&orig->addr[src], &trans->addr[src], state->af) &&
PF_AEQ(&orig->addr[dst], &trans->addr[dst], state->af) &&
orig->port[src] == trans->port[src] &&
orig->port[dst] == trans->port[dst])) {
//printf("NO_NAT!\n");
return 0;
}
memset(node, 0, sizeof(struct pf_nattrack));
node->c.osrc.v4 = orig->addr[src].v4;
node->c.tsrc.v4 = trans->addr[src].v4;
node->c.odst.v4 = orig->addr[dst].v4;
node->c.tdst.v4 = trans->addr[dst].v4;
node->c.osport = ntohs(orig->port[src]);
node->c.tsport = ntohs(trans->port[src]);
node->c.odport = ntohs(orig->port[dst]);
node->c.tdport = ntohs(trans->port[dst]);
node->af = state->af;
node->proto = state->proto;
node->duration = ntohl(state->creation) + ntohl(state->expire);
return 1;
}
/*
uint8_t pf_getstates(struct pf_nattrack *node) {
}
*/
struct pf_nattrack * read_input(struct pf_nattrack *node) {
char osrc[30], tsrc[30], dst[30], dir[10];
int o_sport, t_sport, dport;
scanf("\n%[^:]:%d (%[^:]:%d) %s %[^:]:%d",osrc, &o_sport, tsrc, &t_sport, dir, dst, &dport);
//printf("osrc=%s o_sport=%d tsrc=%s t_sport=%d dst=%s dport=%d\n", osrc, o_sport, tsrc, t_sport, dst, dport);
memset(node, 0, sizeof(struct pf_nattrack));
// original source address and port
if (!inet_pton(AF_INET, osrc, &node->c.osrc.v4)) {
printf("ERROR: invalid v4 addr (osrc=%s)\n", osrc);
return NULL;
}
node->c.osport = o_sport;
// translated source address and port
if (!inet_pton(AF_INET, tsrc, &node->c.tsrc.v4)) {
printf("ERROR: invalid v4 addr (osrc=%s)\n", tsrc);
return NULL;
}
node->c.tsport = t_sport;
// original destination address and port
// TODO: change to odst
if (!inet_pton(AF_INET, dst, &node->c.odst.v4)) {
printf("ERROR: invalid v4 addr (odst=%s)\n", dst);
return NULL;
}
node->c.odport = dport;
// translated destination address and port
// TODO: change to tdst
if (!inet_pton(AF_INET, dst, &node->c.tdst.v4)) {
printf("ERROR: invalid v4 addr (odst=%s)\n", dst);
return NULL;
}
node->c.tdport = dport;
node->af = AF_INET;
return node;
}
int main() {
struct pf_nattrack_hash *pfnth = NULL;
struct pf_nattrack_list *item, *item2;
struct pf_nattrack_list *lastlist = NULL, *freelist;
struct pf_nattrack node, *nodep;
int i, dev;
initialize();
dev = open("/dev/pf", O_RDWR);
if (dev < 0) {
printerror("open(/dev/pf)");
return 1;
}
do {
//printf("\n\n===================================\n");
//printf("Nova rodada\n");
//printf("===================================\n");
freelist = lastlist;
lastlist = NULL;
struct pfioc_states ps;
struct pfsync_state *p;
char *inbuf = NULL, *newinbuf = NULL;
unsigned int len = 0;
int i, opts = 0;
memset(&ps, 0, sizeof(ps));
for (;;) {
ps.ps_len = len;
if (len) {
newinbuf = realloc(inbuf, len);
if (newinbuf == NULL) {
printerror("error realloc - out of memory?");
goto done;
}
ps.ps_buf = inbuf = newinbuf;
}
if (ioctl(dev, DIOCGETSTATES, &ps) < 0) {
printerror("failed to get states from PF device");
goto done;
}
if (ps.ps_len + sizeof(struct pfioc_states) < len)
break;
if (len == 0 && ps.ps_len == 0)
goto done;
if (len == 0 && ps.ps_len != 0)
len = ps.ps_len;
if (ps.ps_len == 0)
goto done; /* no states */
len *= 2;
}
p = ps.ps_states;
for (i = 0; i < ps.ps_len; i += sizeof(*p), p++) {
if (!convert_state(p, &node)) continue;
pfnth = &pfnt_hash[hashkey(&node)];
item = lfind(pfnth->list, &node);
if (item) {
//printf("Item found! Deleting from freelist\n");
item2 = item->ref;
*(item2->nt) = node;
ldel(&freelist, item2);
} else {
//printf("Not found. Inserting...\n");
nodep = (struct pf_nattrack *)malloc(sizeof(struct pf_nattrack));
*nodep = node;
item = (struct pf_nattrack_list *)malloc(
sizeof(struct pf_nattrack_list));
item->nt = nodep;
item2 = (struct pf_nattrack_list *)malloc(
sizeof(struct pf_nattrack_list));
item2->nt = nodep;
ladd(&pfnth->list, item);
item->ref = item2;
}
ladd(&lastlist, item2);
item2->ref = item;
}
done:
free(inbuf);
free_list(&freelist);
sleep(PFTM_INTERVAL);
} while(1);
/* comentando para trabalhar com o get_states
while ( scanf("\n%d", &i) != EOF && i != 0) {
if (!read_input(&node)) continue;
pfnth = &pfnt_hash[hashkey(&node)];
item = lfind(pfnth->list, &node);
if (item) {
//printf("Item found! Deleting from freelist\n");
item2 = item->ref;
ldel(&freelist, item2);
} else {
//printf("Not found. Inserting...\n");
nodep = (struct pf_nattrack *)malloc(sizeof(struct pf_nattrack));
*nodep = node;
item = (struct pf_nattrack_list *)malloc(
sizeof(struct pf_nattrack_list));
item->nt = nodep;
item2 = (struct pf_nattrack_list *)malloc(
sizeof(struct pf_nattrack_list));
item2->nt = nodep;
ladd(&pfnth->list, item);
item->ref = item2;
}
ladd(&lastlist, item2);
item2->ref = item;
}
//printf("done\n");
//printf("-> removendo itens da freelist\n");
free_list(&freelist);
//printf("-> items armazenados:\n");
//for(i=0; i <= pf_hashmask; i++) {
// for(item=pfnt_hash[i].list; item; item=item->next) {
// print_nattrack(item->nt, 0);
// }
//}
//printf("Nova rodada? (1 = sim) ");
} while(scanf("\n%d", &i) != EOF && i != 0);
*/ // comentando para get_states
free_list(&lastlist);
free(pfnt_hash);
return 0;
}