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 <sys/types.h>
33 #include <sys/socket.h>
35 #include <sys/ioctl.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
51 #include "stg/common.h"
52 #include "stg/raw_ip_packet.h"
53 #include "stg/traffcounter.h"
54 #include "stg/plugin_creator.h"
56 #include "ether_cap.h"
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 PLUGIN_CREATOR<BPF_CAP> bcc;
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
69 return bcc.GetPlugin();
71 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 int BPF_CAP_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
76 iface.erase(iface.begin(), iface.end());
78 if (s.moduleParams.empty())
80 errorStr = "Parameter \'iface\' not found.";
81 printfd(__FILE__, "Parameter 'iface' not found\n");
85 for (unsigned i = 0; i < s.moduleParams.size(); i++)
87 if (s.moduleParams[i].param != "iface")
89 errorStr = "Parameter \'" + s.moduleParams[i].param + "\' unrecognized.";
90 printfd(__FILE__, "Invalid parameter: '%s'\n", s.moduleParams[i].param.c_str());
93 for (unsigned j = 0; j < s.moduleParams[i].value.size(); j++)
95 iface.push_back(s.moduleParams[i].value[j]);
101 //-----------------------------------------------------------------------------
102 std::string BPF_CAP_SETTINGS::GetIface(unsigned int num)
104 if (num >= iface.size())
110 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
113 const std::string BPF_CAP::GetVersion() const
115 return "bpf_cap v.1.0";
117 //-----------------------------------------------------------------------------
129 logger(GetPluginLogger(GetStgLogger(), "cap_ether"))
132 //-----------------------------------------------------------------------------
133 int BPF_CAP::ParseSettings()
135 int ret = capSettings.ParseSettings(settings);
138 errorStr = capSettings.GetStrError();
143 //-----------------------------------------------------------------------------
149 if (BPFCapOpen() < 0)
151 //errorStr = "Cannot open bpf device!";
157 if (pthread_create(&thread, NULL, Run, this))
159 errorStr = "Cannot create thread.";
160 logger("Cannot create thread.");
161 printfd(__FILE__, "Cannot create thread\n");
167 //-----------------------------------------------------------------------------
177 //5 seconds to thread stops itself
179 for (i = 0; i < 25; i++)
184 struct timespec ts = {0, 200000000};
185 nanosleep(&ts, NULL);
188 //after 5 seconds waiting thread still running. now killing it
191 //TODO pthread_cancel()
192 if (pthread_kill(thread, SIGINT))
194 errorStr = "Cannot kill thread.";
195 logger("Cannot send signal to thread.");
196 printfd(__FILE__, "Cannot kill thread\n");
203 //-----------------------------------------------------------------------------
204 void * BPF_CAP::Run(void * d)
207 sigfillset(&signalSet);
208 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
210 BPF_CAP * dc = static_cast<BPF_CAP *>(d);
211 dc->isRunning = true;
213 uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96
215 RAW_PACKET * rpp = (RAW_PACKET *)&hdr[14];
216 memset(hdr, 0, sizeof(hdr));
223 if (dc->BPFCapRead((char*)&hdr, 68 + 14, &iface))
226 if (!(hdr[12] == 0x8 && hdr[13] == 0x0))
229 dc->traffCnt->Process(*rpp);
232 dc->isRunning = false;
235 //-----------------------------------------------------------------------------
236 int BPF_CAP::BPFCapOpen()
242 while ((bd.iface = capSettings.GetIface(i)) != "")
244 bpfData.push_back(bd);
245 if (BPFCapOpen(&bpfData[i]) < 0)
251 pd.fd = bpfData[i].fd;
258 //-----------------------------------------------------------------------------
259 int BPF_CAP::BPFCapOpen(BPF_DATA * bd)
269 sprintf(devbpf, "/dev/bpf%d", i);
271 bd->fd = open(devbpf, O_RDONLY);
272 } while(bd->fd < 0 && errno == EBUSY);
276 errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error.";
277 logger("Cannot open device for interface '%s': %s", bd->iface, strerror(errno));
278 printfd(__FILE__, "Cannot open BPF device\n");
282 strncpy(ifr.ifr_name, bd->iface.c_str(), sizeof(ifr.ifr_name));
284 if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0)
286 errorStr = bd->iface + " BIOCSBLEN " + std::string(strerror(errno));
287 logger("ioctl (BIOCSBLEN) error for interface '%s': %s", db->iface, strerror(errno));
288 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
292 if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0)
294 errorStr = bd->iface + " BIOCSETIF " + std::string(strerror(errno));
295 logger("ioctl (BIOCSETIF) error for interface '%s': %s", db->iface, strerror(errno));
296 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
300 if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0)
302 errorStr = bd->iface + " BIOCIMMEDIATE " + std::string(strerror(errno));
303 logger("ioctl (BIOCIMMEDIATE) error for interface '%s': %s", db->iface, strerror(errno));
304 printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
310 //-----------------------------------------------------------------------------
311 int BPF_CAP::BPFCapClose()
313 for (unsigned int i = 0; i < bpfData.size(); i++)
314 close(bpfData[i].fd);
317 //-----------------------------------------------------------------------------
318 int BPF_CAP::BPFCapRead(char * buffer, int blen, char ** capIface)
320 poll(&polld[0], polld.size(), -1);
322 for (unsigned int i = 0; i < polld.size(); i++)
324 if (polld[i].revents & POLLIN)
326 if (BPFCapRead(buffer, blen, capIface, &bpfData[i]))
328 polld[i].revents = 0;
331 polld[i].revents = 0;
337 //-----------------------------------------------------------------------------
338 int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd)
342 bd->r = read(bd->fd, bd->buffer, BUFF_LEN);
345 logger("read error: %s", strerror(errno));
346 struct timespec ts = {0, 20000000};
347 nanosleep(&ts, NULL);
352 bd->bh = (struct bpf_hdr*)bd->p;
358 memcpy(buffer, (char*)(bd->p) + bd->bh->bh_hdrlen, blen);
360 bd->sum += BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
361 bd->p = bd->p + BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
362 bd->bh = (struct bpf_hdr*)bd->p;
373 //-----------------------------------------------------------------------------