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