]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/nfqueue/nfqueue.cpp
Lots of stylistic fixes.
[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     : errorStr(),
97       thread(),
98       nonstop(false),
99       isRunning(false),
100       queueNumber(0),
101       nfqHandle(NULL),
102       queueHandle(NULL),
103       traffCnt(NULL),
104       logger(GetPluginLogger(GetStgLogger(), "cap_nfqueue"))
105 {
106 }
107 //-----------------------------------------------------------------------------
108 int NFQ_CAP::ParseSettings()
109 {
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)
113             {
114             errorStr = "Queue number should be a number. Got: '" + settings.moduleParams[i].param + "'";
115             logger(errorStr);
116             return -1;
117             }
118 return 0;
119 }
120 //-----------------------------------------------------------------------------
121 int NFQ_CAP::Start()
122 {
123 if (isRunning)
124     return 0;
125
126 nfqHandle = nfq_open();
127 if (nfqHandle == NULL)
128     {
129     errorStr = "Failed to initialize netfilter queue.";
130     logger(errorStr);
131     return -1;
132     }
133
134 if (nfq_unbind_pf(nfqHandle, AF_INET) < 0)
135     {
136     errorStr = "Failed to unbind netfilter queue from IP handling.";
137     logger(errorStr);
138     return -1;
139     }
140
141 if (nfq_bind_pf(nfqHandle, AF_INET) < 0)
142     {
143     errorStr = "Failed to bind netfilter queue to IP handling.";
144     logger(errorStr);
145     return -1;
146     }
147
148 queueHandle = nfq_create_queue(nfqHandle, queueNumber, &Callback, this);
149 if (queueHandle == NULL)
150     {
151     errorStr = "Failed to create queue " + x2str(queueNumber) + ".";
152     logger(errorStr);
153     return -1;
154     }
155
156 if (nfq_set_mode(queueHandle, NFQNL_COPY_PACKET, 0xffFF) < 0)
157     {
158     errorStr = "Failed to set queue " + x2str(queueNumber) + " mode.";
159     logger(errorStr);
160     return -1;
161     }
162
163 nonstop = true;
164
165 if (pthread_create(&thread, NULL, Run, this))
166     {
167     errorStr = "Cannot create thread.";
168     logger("Cannot create thread.");
169     printfd(__FILE__, "Cannot create thread\n");
170     return -1;
171     }
172
173 return 0;
174 }
175 //-----------------------------------------------------------------------------
176 int NFQ_CAP::Stop()
177 {
178 if (!isRunning)
179     return 0;
180
181 nonstop = false;
182
183 //5 seconds to thread stops itself
184 for (int i = 0; i < 25 && isRunning; i++)
185     {
186     struct timespec ts = {0, 200000000};
187     nanosleep(&ts, NULL);
188     }
189 //after 5 seconds waiting thread still running. now killing it
190 if (isRunning)
191     {
192     if (pthread_kill(thread, SIGUSR1))
193         {
194         errorStr = "Cannot kill thread.";
195         logger("Cannot send signal to thread.");
196         return -1;
197         }
198     for (int i = 0; i < 25 && isRunning; ++i)
199         {
200         struct timespec ts = {0, 200000000};
201         nanosleep(&ts, NULL);
202         }
203     if (isRunning)
204         {
205         errorStr = "NFQ_CAP not stopped.";
206         logger("Cannot stop thread.");
207         printfd(__FILE__, "Cannot stop thread\n");
208         return -1;
209         }
210     }
211
212 pthread_join(thread, NULL);
213
214 nfq_destroy_queue(queueHandle);
215 nfq_close(nfqHandle);
216
217 return 0;
218 }
219 //-----------------------------------------------------------------------------
220 void * NFQ_CAP::Run(void * d)
221 {
222 sigset_t signalSet;
223 sigfillset(&signalSet);
224 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
225
226 NFQ_CAP * dc = static_cast<NFQ_CAP *>(d);
227 dc->isRunning = true;
228
229 int fd = nfq_fd(dc->nfqHandle);
230 char buf[4096];
231
232 while (dc->nonstop)
233     {
234         if (!WaitPackets(fd))
235             continue;
236
237         int rv = read(fd, buf, sizeof(buf));
238         if (rv < 0)
239             {
240             dc->errorStr = std::string("Read error: ") + strerror(errno);
241             dc->logger(dc->errorStr);
242             break;
243             }
244         nfq_handle_packet(dc->nfqHandle, buf, rv);
245     }
246
247 dc->isRunning = false;
248 return NULL;
249 }
250 //-----------------------------------------------------------------------------
251 void NFQ_CAP::Process(const RAW_PACKET & packet)
252 {
253 traffCnt->Process(packet);
254 }