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/plugin_creator.h"
25 #include "stg/common.h"
26 #include "stg/raw_ip_packet.h"
30 #include <linux/netfilter.h> /* Defines verdicts (NF_ACCEPT, etc) */
31 #include <libnetfilter_queue/libnetfilter_queue.h>
38 #include <arpa/inet.h> // ntohl
40 #include <unistd.h> // read
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
48 PLUGIN_CREATOR<NFQ_CAP> ncc;
50 int Callback(struct nfq_q_handle * queueHandle, struct nfgenmsg * /*msg*/,
51 struct nfq_data * nfqData, void *data)
55 struct nfqnl_msg_packet_hdr * packetHeader = nfq_get_msg_packet_hdr(nfqData);
56 if (packetHeader == NULL)
59 id = ntohl(packetHeader->packet_id);
61 unsigned char * payload = NULL;
63 if (nfq_get_payload(nfqData, &payload) < 0 || payload == NULL)
68 memcpy(&packet.rawPacket, payload, sizeof(packet.rawPacket));
70 NFQ_CAP * cap = static_cast<NFQ_CAP *>(data);
74 return nfq_set_verdict(queueHandle, id, NF_ACCEPT, 0, NULL);
79 extern "C" PLUGIN * GetPlugin();
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
85 return ncc.GetPlugin();
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
90 std::string NFQ_CAP::GetVersion() const
92 return "cap_nfqueue v.1.0";
94 //-----------------------------------------------------------------------------
104 logger(GetPluginLogger(GetStgLogger(), "cap_nfqueue"))
107 //-----------------------------------------------------------------------------
108 int NFQ_CAP::ParseSettings()
110 for (size_t i = 0; i < settings.moduleParams.size(); i++)
111 if (settings.moduleParams[i].param == "queueNumber")
112 if (str2x(settings.moduleParams[i].param, queueNumber) < 0)
114 errorStr = "Queue number should be a number. Got: '" + settings.moduleParams[i].param + "'";
120 //-----------------------------------------------------------------------------
126 nfqHandle = nfq_open();
127 if (nfqHandle == NULL)
129 errorStr = "Failed to initialize netfilter queue.";
134 if (nfq_unbind_pf(nfqHandle, AF_INET) < 0)
136 errorStr = "Failed to unbind netfilter queue from IP handling.";
141 if (nfq_bind_pf(nfqHandle, AF_INET) < 0)
143 errorStr = "Failed to bind netfilter queue to IP handling.";
148 queueHandle = nfq_create_queue(nfqHandle, queueNumber, &Callback, this);
149 if (queueHandle == NULL)
151 errorStr = "Failed to create queue " + x2str(queueNumber) + ".";
156 if (nfq_set_mode(queueHandle, NFQNL_COPY_PACKET, 0xffFF) < 0)
158 errorStr = "Failed to set queue " + x2str(queueNumber) + " mode.";
165 if (pthread_create(&thread, NULL, Run, this))
167 errorStr = "Cannot create thread.";
168 logger("Cannot create thread.");
169 printfd(__FILE__, "Cannot create thread\n");
175 //-----------------------------------------------------------------------------
183 //5 seconds to thread stops itself
184 for (int i = 0; i < 25 && isRunning; i++)
186 struct timespec ts = {0, 200000000};
187 nanosleep(&ts, NULL);
189 //after 5 seconds waiting thread still running. now killing it
192 if (pthread_kill(thread, SIGUSR1))
194 errorStr = "Cannot kill thread.";
195 logger("Cannot send signal to thread.");
198 for (int i = 0; i < 25 && isRunning; ++i)
200 struct timespec ts = {0, 200000000};
201 nanosleep(&ts, NULL);
205 errorStr = "NFQ_CAP not stopped.";
206 logger("Cannot stop thread.");
207 printfd(__FILE__, "Cannot stop thread\n");
212 pthread_join(thread, NULL);
214 nfq_destroy_queue(queueHandle);
215 nfq_close(nfqHandle);
219 //-----------------------------------------------------------------------------
220 void * NFQ_CAP::Run(void * d)
223 sigfillset(&signalSet);
224 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
226 NFQ_CAP * dc = static_cast<NFQ_CAP *>(d);
227 dc->isRunning = true;
229 int fd = nfq_fd(dc->nfqHandle);
234 if (!WaitPackets(fd))
237 int rv = read(fd, buf, sizeof(buf));
240 dc->errorStr = std::string("Read error: ") + strerror(errno);
241 dc->logger(dc->errorStr);
244 nfq_handle_packet(dc->nfqHandle, buf, rv);
247 dc->isRunning = false;
250 //-----------------------------------------------------------------------------
251 void NFQ_CAP::Process(const RAW_PACKET & packet)
253 traffCnt->Process(packet);