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
22 * Author : Maxim Mamontov <faust@stg.dp.ua>
27 $Date: 2010/09/10 06:41:06 $
33 #include "stg/common.h"
34 #include "stg/raw_ip_packet.h"
35 #include "stg/traffcounter.h"
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
52 uint16_t version; // Protocol version
53 uint16_t count; // Flows count
54 uint32_t uptime; // System uptime
55 uint32_t timestamp; // UNIX timestamp
56 uint32_t nsecs; // Residual nanoseconds
57 uint32_t flowSeq; // Sequence counter
58 uint8_t eType; // Engine type
59 uint8_t eID; // Engine ID
60 uint16_t sInterval; // Sampling mode and interval
64 uint32_t srcAddr; // Flow source address
65 uint32_t dstAddr; // Flow destination address
66 uint32_t nextHop; // IP addres on next hop router
67 uint16_t inSNMP; // SNMP index of input iface
68 uint16_t outSNMP; // SNMP index of output iface
69 uint32_t packets; // Packets in flow
70 uint32_t octets; // Total number of bytes in flow
71 uint32_t timeStart; // Uptime on first packet in flow
72 uint32_t timeFinish;// Uptime on last packet in flow
73 uint16_t srcPort; // Flow source port
74 uint16_t dstPort; // Flow destination port
75 uint8_t pad1; // 1-byte padding
76 uint8_t TCPFlags; // Cumulative OR of TCP flags
77 uint8_t proto; // IP protocol type (tcp, udp, etc.)
78 uint8_t tos; // IP Type of Service (ToS)
79 uint16_t srcAS; // Source BGP autonomous system number
80 uint16_t dstAS; // Destination BGP autonomus system number
81 uint8_t srcMask; // Source address mask in "slash" notation
82 uint8_t dstMask; // Destination address mask in "slash" notation
83 uint16_t pad2; // 2-byte padding
86 #define BUF_SIZE (sizeof(NF_HEADER) + 30 * sizeof(NF_DATA))
90 extern "C" STG::Plugin* GetPlugin()
106 logger(STG::PluginLogger::get("cap_nf"))
110 int NF_CAP::ParseSettings()
112 std::vector<STG::ParamValue>::iterator it;
113 for (it = settings.moduleParams.begin(); it != settings.moduleParams.end(); ++it)
115 if (it->param == "TCPPort" && !it->value.empty())
117 if (str2x(it->value[0], portT))
119 errorStr = "Invalid TCPPort value";
120 printfd(__FILE__, "Error: Invalid TCPPort value\n");
125 if (it->param == "UDPPort" && !it->value.empty())
127 if (str2x(it->value[0], portU))
129 errorStr = "Invalid UDPPort value";
130 printfd(__FILE__, "Error: Invalid UDPPort value\n");
135 printfd(__FILE__, "'%s' is not a valid module param\n", it->param.c_str());
149 if (pthread_create(&tidUDP, NULL, RunUDP, this))
153 errorStr = "Cannot create UDP thread";
154 logger("Cannot create UDP thread.");
155 printfd(__FILE__, "Error: Cannot create UDP thread\n");
166 if (pthread_create(&tidTCP, NULL, RunTCP, this))
170 logger("Cannot create TCP thread.");
171 errorStr = "Cannot create TCP thread";
172 printfd(__FILE__, "Error: Cannot create TCP thread\n");
181 runningTCP = runningUDP = false;
182 if (portU && !stoppedUDP)
185 for (int i = 0; i < 25 && !stoppedUDP; ++i)
187 struct timespec ts = {0, 200000000};
188 nanosleep(&ts, NULL);
192 pthread_join(tidUDP, NULL);
196 if (pthread_kill(tidUDP, SIGUSR1))
198 errorStr = "Error sending signal to UDP thread";
199 logger("Error sending sugnal to UDP thread.");
200 printfd(__FILE__, "Error: Error sending signal to UDP thread\n");
203 printfd(__FILE__, "UDP thread NOT stopped\n");
204 logger("Cannot stop UDP thread.");
207 if (portT && !stoppedTCP)
210 for (int i = 0; i < 25 && !stoppedTCP; ++i)
212 struct timespec ts = {0, 200000000};
213 nanosleep(&ts, NULL);
217 pthread_join(tidTCP, NULL);
221 if (pthread_kill(tidTCP, SIGUSR1))
223 errorStr = "Error sending signal to TCP thread";
224 logger("Error sending signal to TCP thread.");
225 printfd(__FILE__, "Error: Error sending signal to TCP thread\n");
228 printfd(__FILE__, "TCP thread NOT stopped\n");
229 logger("Cannot stop TCP thread.");
235 bool NF_CAP::OpenUDP()
237 struct sockaddr_in sin;
238 sockUDP = socket(PF_INET, SOCK_DGRAM, 0);
241 errorStr = "Error opening UDP socket";
242 logger("Cannot create UDP socket: %s", strerror(errno));
243 printfd(__FILE__, "Error: Error opening UDP socket\n");
246 sin.sin_family = AF_INET;
247 sin.sin_port = htons(portU);
248 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
249 if (bind(sockUDP, reinterpret_cast<const sockaddr*>(&sin), sizeof(sin)))
251 errorStr = "Error binding UDP socket";
252 logger("Cannot bind UDP socket: %s", strerror(errno));
253 printfd(__FILE__, "Error: Error binding UDP socket\n");
259 bool NF_CAP::OpenTCP()
261 struct sockaddr_in sin;
262 sockTCP = socket(PF_INET, SOCK_STREAM, 0);
265 errorStr = "Error opening TCP socket";
266 logger("Cannot create TCP socket: %s", strerror(errno));
267 printfd(__FILE__, "Error: Error opening TCP socket\n");
270 sin.sin_family = AF_INET;
271 sin.sin_port = htons(portT);
272 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
273 if (bind(sockTCP, reinterpret_cast<const sockaddr*>(&sin), sizeof(sin)))
275 errorStr = "Error binding TCP socket";
276 logger("Cannot bind TCP socket: %s", strerror(errno));
277 printfd(__FILE__, "Error: Error binding TCP socket\n");
280 if (listen(sockTCP, 1))
282 errorStr = "Error listening on TCP socket";
283 logger("Cannot listen on TCP socket: %s", strerror(errno));
284 printfd(__FILE__, "Error: Error listening TCP socket\n");
290 void * NF_CAP::RunUDP(void * c)
293 sigfillset(&signalSet);
294 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
296 NF_CAP * cap = static_cast<NF_CAP *>(c);
297 cap->stoppedUDP = false;
298 while (cap->runningUDP)
300 if (!WaitPackets(cap->sockUDP))
306 struct sockaddr_in sin;
307 socklen_t slen = sizeof(sin);
308 uint8_t buf[BUF_SIZE];
309 ssize_t res = recvfrom(cap->sockUDP, buf, BUF_SIZE, 0, reinterpret_cast<struct sockaddr *>(&sin), &slen);
310 if (!cap->runningUDP)
315 cap->logger("recvfrom error: %s", strerror(errno));
328 cap->errorStr = "Invalid data received";
329 printfd(__FILE__, "Error: Invalid data received through UDP\n");
334 cap->ParseBuffer(buf, res);
336 cap->stoppedUDP = true;
340 void * NF_CAP::RunTCP(void * c)
343 sigfillset(&signalSet);
344 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
346 NF_CAP * cap = static_cast<NF_CAP *>(c);
347 cap->stoppedTCP = false;
348 while (cap->runningTCP)
350 if (!WaitPackets(cap->sockTCP))
356 struct sockaddr_in sin;
357 socklen_t slen = sizeof(sin);
358 int sd = accept(cap->sockTCP, reinterpret_cast<struct sockaddr *>(&sin), &slen);
359 if (!cap->runningTCP)
365 cap->logger("accept error: %s", strerror(errno));
369 if (!WaitPackets(sd))
375 uint8_t buf[BUF_SIZE];
376 ssize_t res = recv(sd, buf, BUF_SIZE, MSG_WAITALL);
379 cap->logger("recv error: %s", strerror(errno));
383 if (!cap->runningTCP)
392 // Need to check actual data length and wait all data to receive
398 cap->ParseBuffer(buf, res);
400 cap->stoppedTCP = true;
404 void NF_CAP::ParseBuffer(uint8_t * buf, ssize_t size)
407 NF_HEADER * hdr = reinterpret_cast<NF_HEADER *>(buf);
408 if (htons(hdr->version) != 5)
413 int packets = htons(hdr->count);
415 if (packets < 0 || packets > 30)
420 if (24 + 48 * packets != size)
422 // See 'wrong logic' upper
426 for (int i = 0; i < packets; ++i)
428 NF_DATA * data = reinterpret_cast<NF_DATA *>(buf + 24 + i * 48);
430 ip.rawPacket.header.ipHeader.ip_v = 4;
431 ip.rawPacket.header.ipHeader.ip_hl = 5;
432 ip.rawPacket.header.ipHeader.ip_p = data->proto;
433 ip.dataLen = ntohl(data->octets);
434 ip.rawPacket.header.ipHeader.ip_src.s_addr = data->srcAddr;
435 ip.rawPacket.header.ipHeader.ip_dst.s_addr = data->dstAddr;
436 ip.rawPacket.header.sPort = data->srcPort;
437 ip.rawPacket.header.dPort = data->dstPort;
439 traffCnt->process(ip);