-
Notifications
You must be signed in to change notification settings - Fork 4
/
nflog_common.c
271 lines (227 loc) · 5.07 KB
/
nflog_common.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
#include <arpa/inet.h>
#include <linux/netfilter.h>
#include <libnfnetlink/libnfnetlink.h>
#include <linux/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include "exception.h"
#include "nflog.h"
#include "nflog_common.h"
#include "nflog_version.h"
const char * nflog_bindings_version(void)
{
return NFLOG_BINDINGS_VERSION;
}
int log_open(struct log *self)
{
self->_h = nflog_open();
self->_gh = NULL;
return (self->_h != NULL);
}
void log_close(struct log *self)
{
nflog_close(self->_h);
self->_gh = NULL;
self->_h = NULL;
self->_cb = NULL;
}
int log_bind(struct log *self, int af_family)
{
if (nflog_bind_pf(self->_h, af_family)) {
throw_exception("error during nflog_bind_pf()");
return -1;
}
return 0;
}
int log_unbind(struct log *self, int af_family)
{
if (nflog_unbind_pf(self->_h, af_family)) {
throw_exception("error during nflog_unbind_pf()");
return -1;
}
return 0;
}
int log_create_queue(struct log *self, int queue_num)
{
int ret;
if (self->_cb == NULL) {
throw_exception("Error: no callback set");
return -1;
}
self->_gh = nflog_bind_group(self->_h, queue_num);
if (self->_gh == NULL) {
throw_exception("error during nflog_bind_group()");
return -1;
}
ret = nflog_callback_register(self->_gh, &swig_nflog_callback, (void*)self->_cb);
if (ret != 0) {
throw_exception("error during nflog_callback_register()");
return -1;
}
return 0;
}
int log_fast_open(struct log *self, int queue_num, int af_family)
{
int ret;
if (self->_cb == NULL) {
throw_exception("Error: no callback set");
return -1;
}
ret = log_open(self);
if (!ret)
return -1;
log_unbind(self, af_family);
ret = log_bind(self, af_family);
if (ret < 0) {
log_close(self);
return -1;
}
ret = log_create_queue(self,queue_num);
if (ret < 0) {
log_unbind(self, af_family);
log_close(self);
return -1;
}
return 0;
}
int log_set_bufsiz(struct log *self, int bufsz)
{
int ret;
ret = nflog_set_nlbufsiz(self->_gh, bufsz);
if (ret < 0) {
throw_exception("error during nflog_set_nlbufsiz()\n");
}
return ret;
}
int log_set_qthresh(struct log *self, uint32_t qthresh)
{
int ret;
ret = nflog_set_qthresh(self->_gh, qthresh);
if (ret < 0) {
throw_exception("error during nflog_set_qthresh()\n");
}
return ret;
}
int log_set_timeout(struct log *self, uint32_t timeout)
{
int ret;
ret = nflog_set_timeout(self->_gh, timeout);
if (ret < 0) {
throw_exception("error during nflog_set_timeout()\n");
}
return ret;
}
int log_set_flags(struct log *self, enum CfgFlags flags)
{
int ret;
ret = nflog_set_flags(self->_gh, flags);
if (ret < 0) {
throw_exception("error during nflog_set_flags()\n");
}
return ret;
}
int log_set_mode(struct log *self, enum CopyMode mode, uint32_t range)
{
int ret;
ret = nflog_set_mode(self->_gh, mode, range);
if (ret < 0) {
throw_exception("error during nflog_set_mode()\n");
}
return ret;
}
int log_stop_loop(struct log *self)
{
self->fd = -1;
return 0;
}
int log_prepare(struct log *self)
{
int rv;
int opt = 1;
if (nflog_set_mode(self->_gh, NFULNL_COPY_PACKET, 0xffff) < 0) {
throw_exception("can't set packet_copy mode\n");
exit(1);
}
self->fd = nflog_fd(self->_h);
/* avoid ENOBUFS on read() operation, otherwise the while loop
* in log_loop() is interrupted. */
rv = setsockopt(self->fd, SOL_NETLINK, NETLINK_NO_ENOBUFS, &opt, sizeof(int));
if (rv == -1) {
throw_exception("can't set setsockopt(NETLINK_NO_ENOBUFS)");
exit(1);
}
return 0;
}
uint32_t log_payload_get_nfmark(struct log_payload *self)
{
return nflog_get_nfmark(self->nfad);
}
struct timeval log_payload_get_timestamp(struct log_payload *self)
{
struct timeval tv = {0,0};
if (nflog_get_timestamp(self->nfad, &tv) < 0) {
throw_exception("No such attribute");
return tv;
}
return tv;
}
int log_payload_get_indev(struct log_payload *self)
{
return nflog_get_indev(self->nfad);
}
int log_payload_get_physindev(struct log_payload *self)
{
return nflog_get_physindev(self->nfad);
}
int log_payload_get_outdev(struct log_payload *self)
{
return nflog_get_outdev(self->nfad);
}
int log_payload_get_physoutdev(struct log_payload *self)
{
return nflog_get_physoutdev(self->nfad);
}
uint32_t log_payload_get_uid(struct log_payload *self)
{
u_int32_t i;
if (nflog_get_uid(self->nfad, &i) < 0) {
throw_exception("No such attribute");
return (uint32_t)-1;
}
return i;
}
uint32_t log_payload_get_gid(struct log_payload *self)
{
u_int32_t i;
if (nflog_get_gid(self->nfad, &i) < 0) {
throw_exception("No such attribute");
return (uint32_t)-1;
}
return i;
}
uint32_t log_payload_get_seq(struct log_payload *self)
{
u_int32_t i;
if (nflog_get_seq(self->nfad, &i) < 0) {
throw_exception("No such attribute");
return (uint32_t)-1;
}
return i;
}
uint32_t log_payload_get_seq_global(struct log_payload *self)
{
u_int32_t i;
if (nflog_get_seq_global(self->nfad, &i) < 0) {
throw_exception("No such attribute");
return (uint32_t)-1;
}
return i;
}
const char * log_payload_get_prefix(struct log_payload *self)
{
return nflog_get_prefix(self->nfad);
}
uint16_t log_payload_get_hwtype(struct log_payload *self)
{
return nflog_get_hwtype(self->nfad);
}