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>
55 uint16_t version; // Protocol version
56 uint16_t count; // Flows count
57 uint32_t uptime; // System uptime
58 uint32_t timestamp; // UNIX timestamp
59 uint32_t nsecs; // Residual nanoseconds
60 uint32_t flowSeq; // Sequence counter
61 uint8_t eType; // Engine type
62 uint8_t eID; // Engine ID
63 uint16_t sInterval; // Sampling mode and interval
68 uint32_t srcAddr; // Flow source address
69 uint32_t dstAddr; // Flow destination address
70 uint32_t nextHop; // IP addres on next hop router
71 uint16_t inSNMP; // SNMP index of input iface
72 uint16_t outSNMP; // SNMP index of output iface
73 uint32_t packets; // Packets in flow
74 uint32_t octets; // Total number of bytes in flow
75 uint32_t timeStart; // Uptime on first packet in flow
76 uint32_t timeFinish;// Uptime on last packet in flow
77 uint16_t srcPort; // Flow source port
78 uint16_t dstPort; // Flow destination port
79 uint8_t pad1; // 1-byte padding
80 uint8_t TCPFlags; // Cumulative OR of TCP flags
81 uint8_t proto; // IP protocol type (tcp, udp, etc.)
82 uint8_t tos; // IP Type of Service (ToS)
83 uint16_t srcAS; // Source BGP autonomous system number
84 uint16_t dstAS; // Destination BGP autonomus system number
85 uint8_t srcMask; // Source address mask in "slash" notation
86 uint8_t dstMask; // Destination address mask in "slash" notation
87 uint16_t pad2; // 2-byte padding
90 #define BUF_SIZE (sizeof(NF_HEADER) + 30 * sizeof(NF_DATA))
94 extern "C" STG::Plugin* GetPlugin()
108 logger(PluginLogger::get("cap_nf"))
112 int NF_CAP::ParseSettings()
114 std::vector<ParamValue>::iterator it;
115 for (it = settings.moduleParams.begin(); it != settings.moduleParams.end(); ++it)
117 if (it->param == "TCPPort" && !it->value.empty())
119 if (str2x(it->value[0], portT))
121 errorStr = "Invalid TCPPort value";
122 printfd(__FILE__, "Error: Invalid TCPPort value\n");
127 if (it->param == "UDPPort" && !it->value.empty())
129 if (str2x(it->value[0], portU))
131 errorStr = "Invalid UDPPort value";
132 printfd(__FILE__, "Error: Invalid UDPPort value\n");
137 printfd(__FILE__, "'%s' is not a valid module param\n", it->param.c_str());
150 m_threadUDP = std::jthread([this](auto token){ RunUDP(std::move(token)); });
158 m_threadTCP = std::jthread([this](auto token){ RunTCP(std::move(token)); });
165 m_threadTCP.request_stop();
166 m_threadUDP.request_stop();
167 if (portU && !stoppedUDP)
170 for (int i = 0; i < 25 && !stoppedUDP; ++i)
172 struct timespec ts = {0, 200000000};
173 nanosleep(&ts, NULL);
181 m_threadUDP.detach();
182 printfd(__FILE__, "UDP thread NOT stopped\n");
183 logger("Cannot stop UDP thread.");
186 if (portT && !stoppedTCP)
189 for (int i = 0; i < 25 && !stoppedTCP; ++i)
191 struct timespec ts = {0, 200000000};
192 nanosleep(&ts, NULL);
200 m_threadTCP.detach();
201 printfd(__FILE__, "TCP thread NOT stopped\n");
202 logger("Cannot stop TCP thread.");
208 bool NF_CAP::OpenUDP()
210 struct sockaddr_in sin;
211 sockUDP = socket(PF_INET, SOCK_DGRAM, 0);
214 errorStr = "Error opening UDP socket";
215 logger("Cannot create UDP socket: %s", strerror(errno));
216 printfd(__FILE__, "Error: Error opening UDP socket\n");
219 sin.sin_family = AF_INET;
220 sin.sin_port = htons(portU);
221 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
222 if (bind(sockUDP, reinterpret_cast<const sockaddr*>(&sin), sizeof(sin)))
224 errorStr = "Error binding UDP socket";
225 logger("Cannot bind UDP socket: %s", strerror(errno));
226 printfd(__FILE__, "Error: Error binding UDP socket\n");
232 bool NF_CAP::OpenTCP()
234 struct sockaddr_in sin;
235 sockTCP = socket(PF_INET, SOCK_STREAM, 0);
238 errorStr = "Error opening TCP socket";
239 logger("Cannot create TCP socket: %s", strerror(errno));
240 printfd(__FILE__, "Error: Error opening TCP socket\n");
243 sin.sin_family = AF_INET;
244 sin.sin_port = htons(portT);
245 sin.sin_addr.s_addr = inet_addr("0.0.0.0");
246 if (bind(sockTCP, reinterpret_cast<const sockaddr*>(&sin), sizeof(sin)))
248 errorStr = "Error binding TCP socket";
249 logger("Cannot bind TCP socket: %s", strerror(errno));
250 printfd(__FILE__, "Error: Error binding TCP socket\n");
253 if (listen(sockTCP, 1))
255 errorStr = "Error listening on TCP socket";
256 logger("Cannot listen on TCP socket: %s", strerror(errno));
257 printfd(__FILE__, "Error: Error listening TCP socket\n");
263 void NF_CAP::RunUDP(std::stop_token token) noexcept
266 sigfillset(&signalSet);
267 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
270 while (!token.stop_requested())
272 if (!WaitPackets(sockUDP))
278 struct sockaddr_in sin;
279 socklen_t slen = sizeof(sin);
280 uint8_t buf[BUF_SIZE];
281 ssize_t res = recvfrom(sockUDP, buf, BUF_SIZE, 0, reinterpret_cast<struct sockaddr *>(&sin), &slen);
282 if (token.stop_requested())
287 logger("recvfrom error: %s", strerror(errno));
300 errorStr = "Invalid data received";
301 printfd(__FILE__, "Error: Invalid data received through UDP\n");
306 ParseBuffer(buf, res);
311 void NF_CAP::RunTCP(std::stop_token token) noexcept
314 sigfillset(&signalSet);
315 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
318 while (!token.stop_requested())
320 if (!WaitPackets(sockTCP))
326 struct sockaddr_in sin;
327 socklen_t slen = sizeof(sin);
328 int sd = accept(sockTCP, reinterpret_cast<struct sockaddr *>(&sin), &slen);
329 if (token.stop_requested())
335 logger("accept error: %s", strerror(errno));
339 if (!WaitPackets(sd))
345 uint8_t buf[BUF_SIZE];
346 ssize_t res = recv(sd, buf, BUF_SIZE, MSG_WAITALL);
349 logger("recv error: %s", strerror(errno));
353 if (token.stop_requested())
362 // Need to check actual data length and wait all data to receive
368 ParseBuffer(buf, res);
373 void NF_CAP::ParseBuffer(uint8_t * buf, ssize_t size)
376 NF_HEADER * hdr = reinterpret_cast<NF_HEADER *>(buf);
377 if (htons(hdr->version) != 5)
382 int packets = htons(hdr->count);
384 if (packets < 0 || packets > 30)
389 if (24 + 48 * packets != size)
391 // See 'wrong logic' upper
395 for (int i = 0; i < packets; ++i)
397 NF_DATA * data = reinterpret_cast<NF_DATA *>(buf + 24 + i * 48);
399 ip.rawPacket.header.ipHeader.ip_v = 4;
400 ip.rawPacket.header.ipHeader.ip_hl = 5;
401 ip.rawPacket.header.ipHeader.ip_p = data->proto;
402 ip.dataLen = ntohl(data->octets);
403 ip.rawPacket.header.ipHeader.ip_src.s_addr = data->srcAddr;
404 ip.rawPacket.header.ipHeader.ip_dst.s_addr = data->dstAddr;
405 ip.rawPacket.header.sPort = data->srcPort;
406 ip.rawPacket.header.dPort = data->dstPort;
408 traffCnt->process(ip);