-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNFQ.cpp
469 lines (402 loc) · 13.5 KB
/
NFQ.cpp
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// based on libnetfilter_queue/utils/nfqnl_test.c
// documentation based on Brad Fisher's post titled "Re: libnetfilter_queue man
// page" to [email protected] on 2006-02-08
// WARNING: not thread-safe, partly because libnetfilter_queue isn't thread-safe
#include <cerrno>
#include <cstring>
#include <cassert>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include "NFQ.hpp"
namespace NFQ
{
NfqException::NfqException(const std::string& d)
: runtime_error(d)
{}
NfqSocket::NfqSocket()
: isConnected(false), queueNum(), copyMode(CopyMode::META), nfqHandle(NULL), queueHandle(NULL), pkt(nullptr)
{}
NfqSocket::NfqSocket(QueueNum num) THROW((NfqException))
: isConnected(false), queueNum(), copyMode(CopyMode::META), nfqHandle(NULL), queueHandle(NULL), pkt(nullptr)
{
connect(num);
}
NfqSocket::~NfqSocket()
{
try {
close();
} catch (const NfqException& e) {
}
}
// not thread-safe
void
NfqSocket::connect(QueueNum num) THROW((NfqException))
{
if (isConnected)
throw NfqException("Socket already connected");
// open library handle
nfqHandle = nfq_open();
if (!nfqHandle)
throw NfqException("Error opening NFQ handle");
// unbind existing nf_queue handler for AF_INET (if any)
if (nfq_unbind_pf(nfqHandle, AF_INET) < 0)
throw NfqException("Error unbinding existing queue handler");
// bind nfnetlink_queue as nf_queue handler for AF_INET
if (nfq_bind_pf(nfqHandle, AF_INET) < 0)
throw NfqException("Error binding queue handler");
// bind this socket to the specified queue
queueHandle = nfq_create_queue(nfqHandle, num, NfqPacket::createPacket, &pkt);
if (!queueHandle)
throw NfqException("Error creating queue");
// set default copy mode
setCopyMode(CopyMode::META, 0);
isConnected = true;
queueNum = num;
return;
}
void
NfqSocket::setCopyMode(CopyMode mode, int range) THROW((NfqException))
{
if (nfq_set_mode(queueHandle, u_int8_t(mode), range) < 0)
throw NfqException("Error setting copy mode");
}
NfqPacket::ptr
NfqSocket::recvPacket(bool noblock) THROW((NfqException))
{
// NOTE: this may drop messages if more than one message is queued.
// Can that even happen?
// get a message header
struct nlmsghdr nlh;
struct sockaddr_nl peer;
int fd = nfq_fd(nfqHandle);
socklen_t addrlen = sizeof(peer);
int flags = MSG_PEEK | (noblock ? MSG_DONTWAIT : 0);
ssize_t status = recvfrom(fd, reinterpret_cast<void*>(&nlh), sizeof(nlh), flags, reinterpret_cast<struct sockaddr*>(&peer), &addrlen);
if (status <= 0) {
throw NfqException(strerror(errno));
}
if ((status != sizeof(nlh)) || (addrlen != sizeof(peer)) || (peer.nl_pid != 0) || (nlh.nlmsg_pid != 0)) {
throw NfqException(strerror(EPROTO));
}
// read packet
std::unique_ptr<char[]> buf(new char[nlh.nlmsg_len]);
flags = noblock ? MSG_DONTWAIT : MSG_WAITALL;
status = recvfrom(fd, reinterpret_cast<void*>(buf.get()), nlh.nlmsg_len, flags, reinterpret_cast<struct sockaddr*>(&peer), &addrlen);
if (status <= 0) {
throw NfqException(strerror(errno));
}
if ((static_cast<unsigned>(status) != (nlh.nlmsg_len)) || (addrlen != sizeof(peer)) || (peer.nl_pid != 0) || (nlh.nlmsg_flags & MSG_TRUNC)) {
throw NfqException(strerror(EPROTO));
}
if (nfq_handle_packet(nfqHandle, buf.get(), nlh.nlmsg_len) < 0) {
throw NfqException("Error parsing packet");
}
return std::move(pkt);
}
void
NfqSocket::waitForPacket() THROW((NfqException))
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(nfq_fd(nfqHandle), &fds);
int ret = select(nfq_fd(nfqHandle)+1, &fds, NULL, NULL, NULL);
if (ret != 1) {
throw NfqException(std::string("Error waiting for packet: ") + std::strerror(errno));
}
return;
}
void
NfqSocket::waitForPacket(int func_fd, boost::function<void()> func)
{
fd_set fds;
int ret;
int max_fd = (func_fd > nfq_fd(nfqHandle)) ? func_fd : nfq_fd(nfqHandle);
while (1) {
FD_ZERO(&fds);
FD_SET(nfq_fd(nfqHandle), &fds);
FD_SET(func_fd, &fds);
do {
ret = select(max_fd+1, &fds, NULL, NULL, NULL);
} while ((ret == -1) && (errno == EINTR));
if (ret == -1) {
throw NfqException(std::string("Error waiting for packet: ") + std::strerror(errno));
} else if (FD_ISSET(func_fd, &fds)) {
func();
if (ret == 2) {
return;
}
} else {
return;
}
}
}
void
NfqSocket::sendResponse(NfqPacket::ptr pkt) THROW((NfqException))
{
// make sure that a response hasn't already been sent
if (pkt->responseSent == false) {
if (!pkt->verdictSet)
throw NfqException("Verdict not set");
int status;
if (pkt->markSet)
status = nfq_set_verdict2(queueHandle, pkt->getId(), pkt->nfVerdict, pkt->nfMark, 0, NULL);
else
status = nfq_set_verdict(queueHandle, pkt->getId(), pkt->nfVerdict, 0, NULL);
if (status < 0)
throw NfqException(strerror(errno));
pkt->responseSent = true;
}
}
//TODO not thread-safe
void
NfqSocket::close() THROW((NfqException))
{
if (isConnected) {
// unbind from queue
if (nfq_destroy_queue(queueHandle) < 0)
throw NfqException("Error disconnecting from queue");
// close library handle
if (nfq_close(nfqHandle) < 0)
throw NfqException("Error closing NFQ handle");
isConnected = false;
}
return;
}
// create a NfqPacket, store a pointer in *data
int
NfqPacket::createPacket(struct nfq_q_handle*, struct nfgenmsg*, struct nfq_data *nfa, void *data)
{
// get the payload and determine what it is
std::uint8_t* payload;
//TODO: handle an error return
std::size_t psize = static_cast<std::size_t>(nfq_get_payload(nfa, &payload));
NfqPacket** pkt = reinterpret_cast<NfqPacket**>(data);
if ((psize >= sizeof(struct iphdr)) && (reinterpret_cast<struct iphdr*>(payload)->version == 4)) {
// assume IPv4 with full header present
struct iphdr* iph = reinterpret_cast<struct iphdr*>(payload);
if ((psize >= (iph->ihl*4 + sizeof(struct tcphdr))) && (iph->protocol == IPPROTO_TCP)) {
// assume TCP with full header present
*pkt = new NfqTcpPacket(nfa, payload, psize);
} else if ((psize >= (iph->ihl*4 + sizeof(struct udphdr))) && (iph->protocol == IPPROTO_UDP)) {
// assume UDP with full header present
*pkt = new NfqUdpPacket(nfa, payload, psize);
} else {
// some other IP protocol
*pkt = new NfqIpPacket(nfa, payload, psize);
}
} else {
// not able to determine the type of the payload
*pkt = new NfqPacket(nfa, payload, psize);
}
return 0;
}
NfqPacket::NfqPacket(struct nfq_data* nfa, std::uint8_t* payload, std::size_t psize)
: packet(new std::uint8_t[psize]), packetLen(psize), nfInfo(), nfMark(0), timestamp(), indev(0), physIndev(0), outdev(0), physOutdev(0), hwSource(), nfVerdict(0), verdictSet(false), markSet(false), responseSent(false)
{
// copy message contents into this object
std::memcpy(&nfInfo, nfq_get_msg_packet_hdr(nfa), sizeof(nfInfo));
nfMark = nfq_get_nfmark(nfa);
int ret = nfq_get_timestamp(nfa, ×tamp);
if (ret != 0) {
timestamp.tv_sec = 0;
timestamp.tv_usec = 0;
}
indev = nfq_get_indev(nfa);
physIndev = nfq_get_physindev(nfa);
outdev = nfq_get_outdev(nfa);
physOutdev = nfq_get_physoutdev(nfa);
struct nfqnl_msg_packet_hw* shw;
shw = nfq_get_packet_hw(nfa);
if (shw != NULL) {
std::memcpy(&hwSource, shw, sizeof(hwSource));
} else {
std::memset(&hwSource, 0, sizeof(hwSource));
}
if ((psize > 0) && (nullptr != payload)) {
std::memcpy(packet.get(), payload, psize);
}
}
NfqPacket::~NfqPacket()
{
assert(responseSent == true);
}
// returns the ID assigned to the packet by Netfilter
std::uint32_t
NfqPacket::getId() const
{
return ntohl(nfInfo.packet_id);
}
// returns the level 3 protocol number??
std::uint16_t
NfqPacket::getHwProtocol() const
{
return ntohs(nfInfo.hw_protocol);
}
// returns the Netfilter hook number
// see NF_IP_LOCAL_IN, etc. in <linux/netfilter_ipv4.h>?
std::uint8_t
NfqPacket::getNfHook() const
{
return nfInfo.hook;
}
// returns the current Netfilter mark on this packet, or 0 if not known
std::uint32_t
NfqPacket::getNfMark() const
{
return nfMark;
}
// returns the time when the packet arrived, or 0 if not known
const struct timeval&
NfqPacket::getTimestamp() const
{
return timestamp;
}
// returns the index of the device on which the packet was received. If the
// index is 0, the packet was locally generated or the input interface is no
// longer known (ie. POSTROUTING?)
std::uint32_t
NfqPacket::getIndev() const
{
return indev;
}
// returns the index of the physical device on which the packet was received.
// If the index is 0, the packet was locally generated or the input interface is
// no longer known (ie. POSTROUTING?)
std::uint32_t
NfqPacket::getPhysIndev() const
{
return physIndev;
}
// returns the index of the device on which the packet will be sent. If the
// index is 0, the packet is destined for localhost or the output interface is
// not yet known (ie. PREROUTING?)
std::uint32_t
NfqPacket::getOutdev() const
{
return outdev;
}
// returns the index of the physical device on which the packet will be sent.
// If the index is 0, the packet is destined for localhost or the output
// interface is not yet known (ie. PREROUTING?)
std::uint32_t
NfqPacket::getPhysOutdev() const
{
return physOutdev;
}
// returns the source hardware address (such as an ethernet MAC address) of the
// packet, or all zeroes if not known. The destination hardware address will
// not be known until after POSTROUTING and a successful ARP request.
// (The return type is a const reference to uint8_t[8].)
const std::uint8_t (&NfqPacket::getHwSource(std::uint16_t& addrlen) const)[8]
{
addrlen = ntohs(hwSource.hw_addrlen);
return hwSource.hw_addr;
}
// returns the packet contents. The amount of data returned depends on the copy
// mode: NONE - a null pointer will be returned
// META - only packet headers will be returned???
// PACKET - the entire packet will be returned
shared_ptr<const std::uint8_t[]>
NfqPacket::getPacket(std::size_t& size) const
{
size = packetLen;
return { shared_from_this(), packet.get() };
}
void
NfqPacket::setVerdict(Verdict v)
{
nfVerdict = u_int32_t(v);
verdictSet = true;
}
void
NfqPacket::setNfMark(std::uint32_t mark)
{
nfMark = mark;
markSet = true;
}
NfqIpPacket::NfqIpPacket(struct nfq_data* nfa, std::uint8_t* payload, std::size_t psize)
: NfqPacket(nfa, payload, psize)
{
assert((nullptr != packet) && (0 != packetLen));
}
shared_ptr<const struct iphdr>
NfqIpPacket::getIpHeader(std::size_t& size) const
{
std::size_t offset = getIpHeaderOffset();
size = getIpPayloadOffset() - offset;
return { shared_from_this(), reinterpret_cast<struct iphdr*>(packet.get() + offset) };
}
shared_ptr<const std::uint8_t[]>
NfqIpPacket::getIpPayload(std::size_t& size) const
{
std::size_t offset = getIpPayloadOffset();
size = packetLen - offset;
return { shared_from_this(), packet.get() + offset };
}
std::uint32_t
NfqIpPacket::getIpSource() const
{
return ntohl((reinterpret_cast<struct iphdr*>(packet.get()+getIpHeaderOffset()))->saddr);
}
std::uint32_t
NfqIpPacket::getIpDest() const
{
return ntohl((reinterpret_cast<struct iphdr*>(packet.get()+getIpHeaderOffset()))->daddr);
}
NfqTcpPacket::NfqTcpPacket(struct nfq_data* nfa, std::uint8_t* payload, std::size_t psize)
: NfqIpPacket(nfa, payload, psize)
{}
shared_ptr<const struct tcphdr>
NfqTcpPacket::getTcpHeader(std::size_t& size) const
{
std::size_t offset = getTcpHeaderOffset();
size = getTcpPayloadOffset() - offset;
return { shared_from_this(), reinterpret_cast<struct tcphdr*>(packet.get() + offset) };
}
shared_ptr<const std::uint8_t[]>
NfqTcpPacket::getTcpPayload(std::size_t& size) const
{
std::size_t offset = getTcpPayloadOffset();
size = packetLen - offset;
return { shared_from_this(), packet.get() + offset };
}
std::uint16_t
NfqTcpPacket::getTcpSource() const
{
return ntohs((reinterpret_cast<struct tcphdr*>(packet.get()+getTcpHeaderOffset()))->source);
}
std::uint16_t
NfqTcpPacket::getTcpDest() const
{
return ntohs((reinterpret_cast<struct tcphdr*>(packet.get()+getTcpHeaderOffset()))->dest);
}
NfqUdpPacket::NfqUdpPacket(struct nfq_data* nfa, std::uint8_t* payload, std::size_t psize)
: NfqIpPacket(nfa, payload, psize)
{}
shared_ptr<const struct udphdr>
NfqUdpPacket::getUdpHeader(std::size_t& size) const
{
std::size_t offset = getUdpHeaderOffset();
size = getUdpPayloadOffset() - offset;
return { shared_from_this(), reinterpret_cast<struct udphdr*>(packet.get() + offset) };
}
shared_ptr<const std::uint8_t[]>
NfqUdpPacket::getUdpPayload(std::size_t& size) const
{
std::size_t offset = getUdpPayloadOffset();
size = packetLen - offset;
return { shared_from_this(), packet.get() + offset };
}
std::uint16_t
NfqUdpPacket::getUdpSource() const
{
return ntohs((reinterpret_cast<struct udphdr*>(packet.get()+getUdpHeaderOffset()))->source);
}
std::uint16_t
NfqUdpPacket::getUdpDest() const
{
return ntohs((reinterpret_cast<struct udphdr*>(packet.get()+getUdpHeaderOffset()))->dest);
}
} // namespace NFQ