2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
23 #include "stg/traffcounter.h"
24 #include "stg/common.h"
25 #include "stg/raw_ip_packet.h"
29 #include <linux/netfilter.h> /* Defines verdicts (NF_ACCEPT, etc) */
30 #include <libnetfilter_queue/libnetfilter_queue.h>
37 #include <arpa/inet.h> // ntohl
39 #include <unistd.h> // read
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
47 int Callback(struct nfq_q_handle * queueHandle, struct nfgenmsg * /*msg*/,
48 struct nfq_data * nfqData, void *data)
52 struct nfqnl_msg_packet_hdr * packetHeader = nfq_get_msg_packet_hdr(nfqData);
53 if (packetHeader == NULL)
56 id = ntohl(packetHeader->packet_id);
58 unsigned char * payload = NULL;
60 if (nfq_get_payload(nfqData, &payload) < 0 || payload == NULL)
65 memcpy(&packet.rawPacket, payload, sizeof(packet.rawPacket));
67 NFQ_CAP * cap = static_cast<NFQ_CAP *>(data);
71 return nfq_set_verdict(queueHandle, id, NF_ACCEPT, 0, NULL);
76 extern "C" Plugin* GetPlugin()
78 static NFQ_CAP plugin;
81 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 std::string NFQ_CAP::GetVersion() const
86 return "cap_nfqueue v.1.0";
88 //-----------------------------------------------------------------------------
96 logger(PluginLogger::get("cap_nfqueue"))
99 //-----------------------------------------------------------------------------
100 int NFQ_CAP::ParseSettings()
102 for (size_t i = 0; i < settings.moduleParams.size(); i++)
103 if (settings.moduleParams[i].param == "queueNumber" && !settings.moduleParams[i].value.empty())
104 if (str2x(settings.moduleParams[i].value[0], queueNumber) < 0)
106 errorStr = "Queue number should be a number. Got: '" + settings.moduleParams[i].param + "'";
112 //-----------------------------------------------------------------------------
118 nfqHandle = nfq_open();
119 if (nfqHandle == NULL)
121 errorStr = "Failed to initialize netfilter queue.";
126 if (nfq_unbind_pf(nfqHandle, AF_INET) < 0)
128 errorStr = "Failed to unbind netfilter queue from IP handling.";
133 if (nfq_bind_pf(nfqHandle, AF_INET) < 0)
135 errorStr = "Failed to bind netfilter queue to IP handling.";
140 queueHandle = nfq_create_queue(nfqHandle, queueNumber, &Callback, this);
141 if (queueHandle == NULL)
143 errorStr = "Failed to create queue " + std::to_string(queueNumber) + ".";
148 if (nfq_set_mode(queueHandle, NFQNL_COPY_PACKET, 0xffFF) < 0)
150 errorStr = "Failed to set queue " + std::to_string(queueNumber) + " mode.";
157 if (pthread_create(&thread, NULL, Run, this))
159 errorStr = "Cannot create thread.";
160 logger("Cannot create thread.");
161 printfd(__FILE__, "Cannot create thread\n");
167 //-----------------------------------------------------------------------------
175 //5 seconds to thread stops itself
176 for (int i = 0; i < 25 && isRunning; i++)
178 struct timespec ts = {0, 200000000};
179 nanosleep(&ts, NULL);
181 //after 5 seconds waiting thread still running. now killing it
184 if (pthread_kill(thread, SIGUSR1))
186 errorStr = "Cannot kill thread.";
187 logger("Cannot send signal to thread.");
190 for (int i = 0; i < 25 && isRunning; ++i)
192 struct timespec ts = {0, 200000000};
193 nanosleep(&ts, NULL);
197 errorStr = "NFQ_CAP not stopped.";
198 logger("Cannot stop thread.");
199 printfd(__FILE__, "Cannot stop thread\n");
204 pthread_join(thread, NULL);
206 nfq_destroy_queue(queueHandle);
207 nfq_close(nfqHandle);
211 //-----------------------------------------------------------------------------
212 void * NFQ_CAP::Run(void * d)
215 sigfillset(&signalSet);
216 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
218 NFQ_CAP * dc = static_cast<NFQ_CAP *>(d);
219 dc->isRunning = true;
221 int fd = nfq_fd(dc->nfqHandle);
226 if (!WaitPackets(fd))
229 int rv = read(fd, buf, sizeof(buf));
232 dc->errorStr = std::string("Read error: ") + strerror(errno);
233 dc->logger(dc->errorStr);
236 nfq_handle_packet(dc->nfqHandle, buf, rv);
239 dc->isRunning = false;
242 //-----------------------------------------------------------------------------
243 void NFQ_CAP::Process(const RAW_PACKET & packet)
245 traffCnt->Process(packet);