]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp
Fix BPF capture plugin compilation errors
[stg.git] / projects / stargazer / plugins / capture / ether_freebsd / ether_cap.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 Date: 18.09.2002
19 */
20
21 /*
22 * Author : Boris Mikhailenko <stg34@stg.dp.ua>
23 */
24
25 /*
26 $Revision: 1.19 $
27 $Date: 2009/03/24 11:20:15 $
28 $Author: faust $
29 */
30
31 #include <sys/types.h>
32 #include <sys/uio.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
35 #include <sys/ioctl.h>
36 #include <sys/poll.h>
37
38 #include <net/bpf.h>
39 #include <net/if.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <signal.h>
49 #include <unistd.h>
50
51 #include "ether_cap.h"
52 #include "stg/common.h"
53 #include "stg/raw_ip_packet.h"
54 #include "stg/traffcounter.h"
55
56 //#define CAP_DEBUG 1
57 //-----------------------------------------------------------------------------
58
59 //-----------------------------------------------------------------------------
60 class BPF_CAP_CREATOR {
61 private:
62     BPF_CAP * bpfc;
63
64 public:
65     BPF_CAP_CREATOR()
66         : bpfc(new BPF_CAP())
67         {
68         }
69     ~BPF_CAP_CREATOR()
70         {
71         delete bpfc;
72         }
73
74     BPF_CAP * GetCapturer()
75     {
76     return bpfc;
77     }
78 };
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 BPF_CAP_CREATOR bcc;
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
86 PLUGIN * GetPlugin()
87 {
88 return bcc.GetCapturer();
89 }
90 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
93 int BPF_CAP_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
94 {
95 std::string ifaces;
96
97 iface.erase(iface.begin(), iface.end());
98
99 if (s.moduleParams.empty())
100     {
101     errorStr = "Parameter \'iface\' not found.";
102     printfd(__FILE__, "Parameter 'iface' not found\n");
103     return -1;
104     }
105
106 for (unsigned i = 0; i < s.moduleParams.size(); i++)
107     {
108     if (s.moduleParams[i].param != "iface")
109         {
110         errorStr = "Parameter \'" + s.moduleParams[i].param + "\' unrecognized.";
111         printfd(__FILE__, "Invalid parameter: '%s'\n", s.moduleParams[i].param.c_str());
112         return -1;
113         }
114     for (unsigned j = 0; j < s.moduleParams[i].value.size(); j++)
115         {
116         iface.push_back(s.moduleParams[i].value[j]);
117         }
118     }
119
120 return 0;
121 }
122 //-----------------------------------------------------------------------------
123 std::string BPF_CAP_SETTINGS::GetIface(unsigned int num)
124 {
125 if (num >= iface.size())
126     {
127     return "";
128     }
129 return iface[num];
130 }
131 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
134 const std::string BPF_CAP::GetVersion() const
135 {
136 return "bpf_cap v.1.0";
137 }
138 //-----------------------------------------------------------------------------
139 BPF_CAP::BPF_CAP()
140     : nonstop(false),
141       isRunning(false),
142       capSock(-1),
143       traffCnt(NULL)
144 {
145 }
146 //-----------------------------------------------------------------------------
147 int BPF_CAP::ParseSettings()
148 {
149 int ret = capSettings.ParseSettings(settings);
150 if (ret)
151     {
152     errorStr = capSettings.GetStrError();
153     return ret;
154     }
155 return 0;
156 }
157 //-----------------------------------------------------------------------------
158 int BPF_CAP::Start()
159 {
160 if (isRunning)
161     return 0;
162
163 if (BPFCapOpen() < 0)
164     {
165     //errorStr = "Cannot open bpf device!";
166     return -1;
167     }
168
169 nonstop = true;
170
171 if (pthread_create(&thread, NULL, Run, this) == 0)
172     {
173     return 0;
174     }
175
176 errorStr = "Cannot create thread.";
177 printfd(__FILE__, "Cannot create thread\n");
178 return -1;
179 }
180 //-----------------------------------------------------------------------------
181 int BPF_CAP::Stop()
182 {
183 if (!isRunning)
184     return 0;
185
186 BPFCapClose();
187
188 nonstop = false;
189
190 //5 seconds to thread stops itself
191 int i;
192 for (i = 0; i < 25; i++)
193     {
194     if (!isRunning)
195         break;
196
197     usleep(200000);
198     }
199
200 //after 5 seconds waiting thread still running. now killing it
201 if (isRunning)
202     {
203     //TODO pthread_cancel()
204     if (pthread_kill(thread, SIGINT))
205         {
206         errorStr = "Cannot kill thread.";
207         printfd(__FILE__, "Cannot kill thread\n");
208         return -1;
209         }
210     }
211
212 return 0;
213 }
214 //-----------------------------------------------------------------------------
215 void * BPF_CAP::Run(void * d)
216 {
217 BPF_CAP * dc = (BPF_CAP *)d;
218 dc->isRunning = true;
219
220 uint8_t hdr[96]; //68 + 14 + 4(size) + 9(SYS_IFACE) + 1(align to 4) = 96
221
222 RAW_PACKET *  rpp = (RAW_PACKET *)&hdr[14];
223 memset(hdr, 0, sizeof(hdr));
224
225 rpp->dataLen = -1;
226 char * iface;
227
228 while (dc->nonstop)
229     {
230     dc->BPFCapRead((char*)&hdr, 68 + 14, &iface);
231
232     if (!(hdr[12] == 0x8 && hdr[13] == 0x0))
233     {
234         continue;
235     }
236
237     dc->traffCnt->Process(*rpp);
238     }
239
240 dc->isRunning = false;
241 return NULL;
242 }
243 //-----------------------------------------------------------------------------
244 int BPF_CAP::BPFCapOpen()
245 {
246 int i = 0;
247 BPF_DATA bd;
248 pollfd pd;
249
250 while ((bd.iface = capSettings.GetIface(i)) != "")
251     {
252     bpfData.push_back(bd);
253     if (BPFCapOpen(&bpfData[i]) < 0)
254         {
255         return -1;
256         }
257
258     pd.events = POLLIN;
259     pd.fd = bpfData[i].fd;
260     polld.push_back(pd);
261     i++;
262     }
263
264 return 0;
265 }
266 //-----------------------------------------------------------------------------
267 int BPF_CAP::BPFCapOpen(BPF_DATA * bd)
268 {
269 char devbpf[20];
270 int i = 0;
271 int l = BUFF_LEN;
272 int im = 1;
273 struct ifreq ifr;
274
275 do
276     {
277     sprintf(devbpf, "/dev/bpf%d", i);
278     i++;
279     bd->fd = open(devbpf, O_RDONLY);
280     } while(bd->fd < 0 && errno == EBUSY);
281
282 if (bd->fd < 0)
283     {
284     errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error.";
285     printfd(__FILE__, "Cannot open BPF device\n");
286     return -1;
287     }
288
289 strncpy(ifr.ifr_name, bd->iface.c_str(), sizeof(ifr.ifr_name));
290
291 if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0)
292     {
293     errorStr = bd->iface + " BIOCSBLEN " + std::string(strerror(errno));
294     printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
295     return -1;
296     }
297
298 if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0)
299     {
300     errorStr = bd->iface + " BIOCSETIF " + std::string(strerror(errno));
301     printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
302     return -1;
303     }
304
305 if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0)
306     {
307     errorStr = bd->iface + " BIOCIMMEDIATE " + std::string(strerror(errno));
308     printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str());
309     return -1;
310     }
311
312 return bd->fd;
313 }
314 //-----------------------------------------------------------------------------
315 int BPF_CAP::BPFCapClose()
316 {
317 for (unsigned int i = 0; i < bpfData.size(); i++)
318     close(bpfData[i].fd);
319 return 0;
320 }
321 //-----------------------------------------------------------------------------
322 int BPF_CAP::BPFCapRead(char * buffer, int blen, char ** capIface)
323 {
324 poll(&polld[0], polld.size(), -1);
325
326 for (unsigned int i = 0; i < polld.size(); i++)
327     {
328     if (polld[i].revents & POLLIN)
329         {
330         BPFCapRead(buffer, blen, capIface, &bpfData[i]);
331         polld[i].revents = 0;
332         return 0;
333         }
334     }
335 return 0;
336 }
337 //-----------------------------------------------------------------------------
338 int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd)
339 {
340 if (bd->canRead)
341     {
342     bd->r = read(bd->fd, bd->buffer, BUFF_LEN);
343     if (bd->r < 0)
344         {
345         //printfd(__FILE__, " error read\n");
346         usleep(20000);
347         }
348
349     bd->p = bd->buffer;
350     bd->bh = (struct bpf_hdr*)bd->p;
351     bd->canRead = 0;
352     }
353
354 if(bd->r > bd->sum)
355     {
356     memcpy(buffer, (char*)(bd->p) + bd->bh->bh_hdrlen, blen);
357
358     bd->sum += BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
359     bd->p = bd->p + BPF_WORDALIGN(bd->bh->bh_hdrlen + bd->bh->bh_caplen);
360     bd->bh = (struct bpf_hdr*)bd->p;
361     }
362
363 if(bd->r <= bd->sum)
364     {
365     bd->canRead = 1;
366     bd->sum = 0;
367     }
368
369 return 0;
370 }
371 //-----------------------------------------------------------------------------