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)
63 STG::RawPacket packet;
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" STG::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 //-----------------------------------------------------------------------------
95 logger(STG::PluginLogger::get("cap_nfqueue"))
98 //-----------------------------------------------------------------------------
99 int NFQ_CAP::ParseSettings()
101 for (size_t i = 0; i < settings.moduleParams.size(); i++)
102 if (settings.moduleParams[i].param == "queueNumber" && !settings.moduleParams[i].value.empty())
103 if (str2x(settings.moduleParams[i].value[0], queueNumber) < 0)
105 errorStr = "Queue number should be a number. Got: '" + settings.moduleParams[i].param + "'";
111 //-----------------------------------------------------------------------------
117 nfqHandle = nfq_open();
118 if (nfqHandle == NULL)
120 errorStr = "Failed to initialize netfilter queue.";
125 if (nfq_unbind_pf(nfqHandle, AF_INET) < 0)
127 errorStr = "Failed to unbind netfilter queue from IP handling.";
132 if (nfq_bind_pf(nfqHandle, AF_INET) < 0)
134 errorStr = "Failed to bind netfilter queue to IP handling.";
139 queueHandle = nfq_create_queue(nfqHandle, queueNumber, &Callback, this);
140 if (queueHandle == NULL)
142 errorStr = "Failed to create queue " + std::to_string(queueNumber) + ".";
147 if (nfq_set_mode(queueHandle, NFQNL_COPY_PACKET, 0xffFF) < 0)
149 errorStr = "Failed to set queue " + std::to_string(queueNumber) + " mode.";
154 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
158 //-----------------------------------------------------------------------------
164 m_thread.request_stop();
166 //5 seconds to thread stops itself
167 for (int i = 0; i < 25 && isRunning; i++)
169 struct timespec ts = {0, 200000000};
170 nanosleep(&ts, NULL);
172 //after 5 seconds waiting thread still running. now killing it
178 nfq_destroy_queue(queueHandle);
179 nfq_close(nfqHandle);
183 //-----------------------------------------------------------------------------
184 void NFQ_CAP::Run(std::stop_token token)
187 sigfillset(&signalSet);
188 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
192 int fd = nfq_fd(nfqHandle);
195 while (!token.stop_requested())
197 if (!WaitPackets(fd))
200 int rv = read(fd, buf, sizeof(buf));
203 errorStr = std::string("Read error: ") + strerror(errno);
207 nfq_handle_packet(nfqHandle, buf, rv);
212 //-----------------------------------------------------------------------------
213 void NFQ_CAP::Process(const STG::RawPacket & packet)
215 traffCnt->process(packet);