]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/nfqueue/nfqueue.cpp
Added specialization of str2x for doubles.
[stg.git] / projects / stargazer / plugins / capture / nfqueue / nfqueue.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
19 */
20
21 #include "nfqueue.h"
22
23 #include "stg/traffcounter.h"
24 #include "stg/plugin_creator.h"
25 #include "stg/common.h"
26 #include "stg/raw_ip_packet.h"
27
28 extern "C" {
29
30 #include <linux/netfilter.h>  /* Defines verdicts (NF_ACCEPT, etc) */
31 #include <libnetfilter_queue/libnetfilter_queue.h>
32
33 }
34
35 #include <cerrno>
36 #include <csignal>
37
38 #include <arpa/inet.h> // ntohl
39
40 #include <unistd.h> // read
41
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 namespace
46 {
47
48 PLUGIN_CREATOR<NFQ_CAP> ncc;
49
50 int Callback(struct nfq_q_handle * queueHandle, struct nfgenmsg * /*msg*/,
51              struct nfq_data * nfqData, void *data)
52 {
53 int id = 0;
54
55 struct nfqnl_msg_packet_hdr * packetHeader = nfq_get_msg_packet_hdr(nfqData);
56 if (packetHeader == NULL)
57     return 0;
58
59 id = ntohl(packetHeader->packet_id);
60
61 unsigned char * payload = NULL;
62
63 if (nfq_get_payload(nfqData, &payload) < 0 || payload == NULL)
64     return id;
65
66 RAW_PACKET packet;
67
68 memcpy(&packet.rawPacket, payload, sizeof(packet.rawPacket));
69
70 NFQ_CAP * cap = static_cast<NFQ_CAP *>(data);
71
72 cap->Process(packet);
73
74 return nfq_set_verdict(queueHandle, id, NF_ACCEPT, 0, NULL);
75 }
76
77 }
78
79 extern "C" PLUGIN * GetPlugin();
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
83 PLUGIN * GetPlugin()
84 {
85 return ncc.GetPlugin();
86 }
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
90 std::string NFQ_CAP::GetVersion() const
91 {
92 return "cap_nfqueue v.1.0";
93 }
94 //-----------------------------------------------------------------------------
95 NFQ_CAP::NFQ_CAP()
96     : nonstop(false),
97       isRunning(false),
98       queueNumber(0),
99       nfqHandle(NULL),
100       queueHandle(NULL),
101       traffCnt(NULL),
102       logger(GetPluginLogger(GetStgLogger(), "cap_nfqueue"))
103 {
104 }
105 //-----------------------------------------------------------------------------
106 int NFQ_CAP::ParseSettings()
107 {
108 for (size_t i = 0; i < settings.moduleParams.size(); i++)
109     if (settings.moduleParams[i].param == "queueNumber" && !settings.moduleParams[i].value.empty())
110         if (str2x(settings.moduleParams[i].value[0], queueNumber) < 0)
111             {
112             errorStr = "Queue number should be a number. Got: '" + settings.moduleParams[i].param + "'";
113             logger(errorStr);
114             return -1;
115             }
116 return 0;
117 }
118 //-----------------------------------------------------------------------------
119 int NFQ_CAP::Start()
120 {
121 if (isRunning)
122     return 0;
123
124 nfqHandle = nfq_open();
125 if (nfqHandle == NULL)
126     {
127     errorStr = "Failed to initialize netfilter queue.";
128     logger(errorStr);
129     return -1;
130     }
131
132 if (nfq_unbind_pf(nfqHandle, AF_INET) < 0)
133     {
134     errorStr = "Failed to unbind netfilter queue from IP handling.";
135     logger(errorStr);
136     return -1;
137     }
138
139 if (nfq_bind_pf(nfqHandle, AF_INET) < 0)
140     {
141     errorStr = "Failed to bind netfilter queue to IP handling.";
142     logger(errorStr);
143     return -1;
144     }
145
146 queueHandle = nfq_create_queue(nfqHandle, queueNumber, &Callback, this);
147 if (queueHandle == NULL)
148     {
149     errorStr = "Failed to create queue " + x2str(queueNumber) + ".";
150     logger(errorStr);
151     return -1;
152     }
153
154 if (nfq_set_mode(queueHandle, NFQNL_COPY_PACKET, 0xffFF) < 0)
155     {
156     errorStr = "Failed to set queue " + x2str(queueNumber) + " mode.";
157     logger(errorStr);
158     return -1;
159     }
160
161 nonstop = true;
162
163 if (pthread_create(&thread, NULL, Run, this))
164     {
165     errorStr = "Cannot create thread.";
166     logger("Cannot create thread.");
167     printfd(__FILE__, "Cannot create thread\n");
168     return -1;
169     }
170
171 return 0;
172 }
173 //-----------------------------------------------------------------------------
174 int NFQ_CAP::Stop()
175 {
176 if (!isRunning)
177     return 0;
178
179 nonstop = false;
180
181 //5 seconds to thread stops itself
182 for (int i = 0; i < 25 && isRunning; i++)
183     {
184     struct timespec ts = {0, 200000000};
185     nanosleep(&ts, NULL);
186     }
187 //after 5 seconds waiting thread still running. now killing it
188 if (isRunning)
189     {
190     if (pthread_kill(thread, SIGUSR1))
191         {
192         errorStr = "Cannot kill thread.";
193         logger("Cannot send signal to thread.");
194         return -1;
195         }
196     for (int i = 0; i < 25 && isRunning; ++i)
197         {
198         struct timespec ts = {0, 200000000};
199         nanosleep(&ts, NULL);
200         }
201     if (isRunning)
202         {
203         errorStr = "NFQ_CAP not stopped.";
204         logger("Cannot stop thread.");
205         printfd(__FILE__, "Cannot stop thread\n");
206         return -1;
207         }
208     }
209
210 pthread_join(thread, NULL);
211
212 nfq_destroy_queue(queueHandle);
213 nfq_close(nfqHandle);
214
215 return 0;
216 }
217 //-----------------------------------------------------------------------------
218 void * NFQ_CAP::Run(void * d)
219 {
220 sigset_t signalSet;
221 sigfillset(&signalSet);
222 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
223
224 NFQ_CAP * dc = static_cast<NFQ_CAP *>(d);
225 dc->isRunning = true;
226
227 int fd = nfq_fd(dc->nfqHandle);
228 char buf[4096];
229
230 while (dc->nonstop)
231     {
232         if (!WaitPackets(fd))
233             continue;
234
235         int rv = read(fd, buf, sizeof(buf));
236         if (rv < 0)
237             {
238             dc->errorStr = std::string("Read error: ") + strerror(errno);
239             dc->logger(dc->errorStr);
240             break;
241             }
242         nfq_handle_packet(dc->nfqHandle, buf, rv);
243     }
244
245 dc->isRunning = false;
246 return NULL;
247 }
248 //-----------------------------------------------------------------------------
249 void NFQ_CAP::Process(const RAW_PACKET & packet)
250 {
251 traffCnt->Process(packet);
252 }