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 : Boris Mikhailenko <stg34@stg.dp.ua>
27 $Date: 2009/03/24 11:20:15 $
31 #include "ether_cap.h"
33 #include "stg/common.h"
34 #include "stg/raw_ip_packet.h"
35 #include "stg/traffcounter.h"
43 #include <sys/types.h>
45 #include <sys/socket.h>
47 #include <sys/ioctl.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
58 extern "C" STG::Plugin* GetPlugin()
60 static BPF_CAP plugin;
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 int BPF_CAP_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
68 iface.erase(iface.begin(), iface.end());
70 if (s.moduleParams.empty())
72 errorStr = "Parameter \'iface\' not found.";
73 printfd(__FILE__, "Parameter 'iface' not found\n");
77 for (unsigned i = 0; i < s.moduleParams.size(); i++)
79 if (s.moduleParams[i].param != "iface")
81 errorStr = "Parameter \'" + s.moduleParams[i].param + "\' unrecognized.";
82 printfd(__FILE__, "Invalid parameter: '%s'\n", s.moduleParams[i].param.c_str());
85 for (unsigned j = 0; j < s.moduleParams[i].value.size(); j++)
87 iface.push_back(s.moduleParams[i].value[j]);
93 //-----------------------------------------------------------------------------
94 std::string BPF_CAP_SETTINGS::GetIface(unsigned int num)
96 if (num >= iface.size())
102 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
105 std::string BPF_CAP::GetVersion() const
107 return "cap_bpf v.1.0";
109 //-----------------------------------------------------------------------------
114 logger(STG::PluginLogger::get("cap_bpf"))
117 //-----------------------------------------------------------------------------
118 int BPF_CAP::ParseSettings()
120 int ret = capSettings.ParseSettings(settings);
123 errorStr = capSettings.GetStrError();
128 //-----------------------------------------------------------------------------
134 if (BPFCapOpen() < 0)
136 //errorStr = "Cannot open bpf device!";
140 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
144 //-----------------------------------------------------------------------------
152 m_thread.request_stop();
154 //5 seconds to thread stops itself
156 for (i = 0; i < 25; i++)
161 struct timespec ts = {0, 200000000};
162 nanosleep(&ts, NULL);
165 //after 5 seconds waiting thread still running. now killing it
173 //-----------------------------------------------------------------------------
174 void BPF_CAP::Run(std::stop_token token)
177 sigfillset(&signalSet);
178 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
182 uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96
184 STG::RawPacket * rpp = (STG::RawPacket *)&hdr[14];
185 memset(hdr, 0, sizeof(hdr));
190 while (!token.stop_requested())
192 if (BPFCapRead((char*)&hdr, 68 + 14, &iface))
195 if (!(hdr[12] == 0x8 && hdr[13] == 0x0))
198 traffCnt->process(*rpp);
203 //-----------------------------------------------------------------------------
204 int BPF_CAP::BPFCapOpen()
210 while ((bd.iface = capSettings.GetIface(i)) != "")
212 bpfData.push_back(bd);
213 if (BPFCapOpen(&bpfData[i]) < 0)
219 pd.fd = bpfData[i].fd;
226 //-----------------------------------------------------------------------------
227 int BPF_CAP::BPFCapOpen(BPF_DATA * bd)
237 sprintf(devbpf, "/dev/bpf%d", i);
239 bd->fd = open(devbpf, O_RDONLY);
240 } while(bd->fd < 0 && errno == EBUSY);
244 errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error.";
245 logger("Cannot open device for interface '%s': %s", bd->iface.c_str(), strerror(errno));
246 printfd(__FILE__, "Cannot open BPF device\n");
250 strncpy(ifr.ifr_name, bd->iface.c_str(), sizeof(ifr.ifr_name));
252 if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0)
254 errorStr = bd->iface + " BIOCSBLEN " + std::string(strerror(errno));
255 logger("ioctl (BIOCSBLEN) error for interface '%s': %s", bd->iface.c_str(), strerror(errno));
256 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
260 if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0)
262 errorStr = bd->iface + " BIOCSETIF " + std::string(strerror(errno));
263 logger("ioctl (BIOCSETIF) error for interface '%s': %s", bd->iface.c_str(), strerror(errno));
264 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
268 if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0)
270 errorStr = bd->iface + " BIOCIMMEDIATE " + std::string(strerror(errno));
271 logger("ioctl (BIOCIMMEDIATE) error for interface '%s': %s", bd->iface.c_str(), strerror(errno));
272 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
278 //-----------------------------------------------------------------------------
279 int BPF_CAP::BPFCapClose()
281 for (unsigned int i = 0; i < bpfData.size(); i++)
282 close(bpfData[i].fd);
285 //-----------------------------------------------------------------------------
286 int BPF_CAP::BPFCapRead(char * buffer, int blen, char ** capIface)
288 poll(&polld[0], polld.size(), -1);
290 for (unsigned int i = 0; i < polld.size(); i++)
292 if (polld[i].revents & POLLIN)
294 if (BPFCapRead(buffer, blen, capIface, &bpfData[i]))
296 polld[i].revents = 0;
299 polld[i].revents = 0;
305 //-----------------------------------------------------------------------------
306 int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd)
310 bd->r = read(bd->fd, bd->buffer, BUFF_LEN);
313 logger("read error: %s", strerror(errno));
314 struct timespec ts = {0, 20000000};
315 nanosleep(&ts, NULL);
320 bd->bh = (struct bpf_hdr*)bd->p;
326 memcpy(buffer, (char*)(bd->p) + bd->bh->bh_hdrlen, blen);
328 bd->sum += BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
329 bd->p = bd->p + BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
330 bd->bh = (struct bpf_hdr*)bd->p;
341 //-----------------------------------------------------------------------------