summaryrefslogtreecommitdiff
path: root/noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc
Unidiff
Diffstat (limited to 'noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc451
1 files changed, 0 insertions, 451 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc b/noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc
deleted file mode 100644
index 4f187c0..0000000
--- a/noncore/net/wellenreiter/libwellenreiter/source/cardmode.cc
+++ b/dev/null
@@ -1,451 +0,0 @@
1/*
2 * Set card modes for sniffing
3 *
4 * $Id$
5 */
6
7#include "cardmode.hh"
8#include "wl_log.hh"
9pcap_t *handletopcap;
10/* main card into monitor function */
11int card_into_monitormode (const char *device, int cardtype)
12{
13 char CiscoRFMON[35] = "/proc/driver/aironet/";
14 FILE *CISCO_CONFIG_FILE;
15
16 /* Checks if we have a device to sniff on */
17 if(device == NULL)
18 {
19 wl_logerr("No device given");
20 return 0;
21 }
22
23 /* Setting the promiscous and up flag to the interface */
24 if (!card_set_promisc_up(device))
25 {
26 wl_logerr("Cannot set interface to promisc mode");
27 return 0;
28 }
29 wl_loginfo("Interface set to promisc mode");
30
31 /* Check the cardtype and executes the commands to go into monitor mode */
32 if (cardtype == CARD_TYPE_CISCO)
33 {
34 /* bring the sniffer into rfmon mode */
35 snprintf(CiscoRFMON, sizeof(CiscoRFMON) - 1, DEFAULT_PATH, device);
36 if((CISCO_CONFIG_FILE = fopen(CiscoRFMON,"w")) == NULL)
37 {
38 wl_logerr("Cannot open config file: %s", strerror(errno));
39 return 0;
40 }
41 fputs ("Mode: r",CISCO_CONFIG_FILE);
42 fputs ("Mode: y",CISCO_CONFIG_FILE);
43 fputs ("XmitPower: 1",CISCO_CONFIG_FILE);
44 fclose(CISCO_CONFIG_FILE);
45 }
46 else if (cardtype == CARD_TYPE_NG)
47 {
48 char wlanngcmd[80];
49 snprintf(wlanngcmd, sizeof(wlanngcmd) - 1, "$(which wlanctl-ng) %s lnxreq_wlansniff channel=%d enable=true", device, 1);
50 if (system(wlanngcmd) != 0)
51 {
52 wl_logerr("Could not set %s in raw mode, check cardtype", device);
53 return 0;
54 }
55 }
56 else if (cardtype == CARD_TYPE_HOSTAP)
57 {
58#if WIRELESS_EXT > 14
59 // IW_MODE_MONITOR was implemented in Wireless Extensions Version 15
60 int skfd;
61 skfd = socket(AF_INET, SOCK_STREAM, 0);
62 struct iwreq wrq;
63 wrq.u.mode = IW_MODE_MONITOR;
64
65 if(iw_set_ext(skfd,(char *) device,SIOCSIWMODE,&wrq)<0)
66 {
67 wl_logerr("Could not set hostap card %s to raw mode, check cardtype", device);
68 return 0;
69 }
70 else
71 {
72 wl_loginfo("Successfully set hostap card %s into raw mode",device);
73 return 1;
74 }
75 return 1;
76#else
77 // Wireless Extensions < Version 15 need iwpriv commandos for monitoring
78 int fd;
79 //Wireless tools structure for the iocalls
80 struct iwreq ireq;
81 int *ptr;
82 /* Socket needed to use the iocall to */
83 fd = socket(AF_INET, SOCK_STREAM, 0);
84
85 if ( fd == -1 ) {
86 return -1;
87 }
88
89 ptr = (int *) ireq.u.name;
90 // This is the monitor mode for 802.11 non-prism header
91 ptr[0] = 2;
92 strcpy(ireq.ifr_ifrn.ifrn_name, device);
93 if (ioctl( fd, SIOCDEVPRIVATE, &ireq)==0)
94 {
95 /* All was fine... */
96 close(fd);
97 wl_loginfo("Set hostap card %s into monitormode",device);
98 return 1;
99 }
100 else
101 {
102 /* iocall does not work */
103 close(fd);
104 wl_logerr("Could not set hostap card %s into monitormode, check cardtype",device);
105 return 0;
106 }
107#endif
108 }
109 else if (cardtype == CARD_TYPE_ORINOCCO )
110 {
111 if (!card_set_channel (device, 1, CARD_TYPE_ORINOCCO))
112 {
113 wl_logerr("Could not set %s in raw mode, check cardtype", device);
114 return 0;
115 }
116 else
117 {
118 wl_loginfo("Successfully set %s into raw mode",device);
119 }
120 }
121
122 /* Setting the promiscous and up flag to the interface */
123 if (!card_check_rfmon_datalink(device))
124 {
125 wl_logerr("Cannot set interface to rfmon mode");
126 return 0;
127 }
128 else
129 {
130 wl_loginfo("Interface set to rfmon mode");
131 }
132 return 1;
133}
134
135/* Check card is in the rfmon mode */
136int card_check_rfmon_datalink (const char *device)
137{
138 int datalinktype=0;
139 pcap_t *phandle;
140 phandle = pcap_open_live((char *)device, 65,0,0,NULL);
141 datalinktype = pcap_datalink (phandle);
142 pcap_close(phandle);
143
144 if (datalinktype != DLT_IEEE802_11) /* Rawmode is IEEE802_11 */
145 {
146 return 0;
147 }
148 else
149 {
150 wl_loginfo("Your successfully listen on %s in 802.11 raw mode", device);
151 return 1;
152 }
153}
154
155/* Ipaq running familiar does not have a loopback device, we need one */
156int check_loopback()
157{
158 /* Checking for a loopback interface with 127.0.0.1, otherwise the other stuff seems to fail on
159 familiar linux on ipaq's */
160 int err;
161 /* First generate a socket to use with iocalls */
162 int fd = socket(AF_INET, SOCK_DGRAM, 0);
163 if (fd < 0)
164 {
165 /* In case of an error */
166 wl_logerr("check_loopback, generation of a socket failed, cannot continue");
167 return 0;
168 }
169 /* Fill an empty an interface structure with the right flags (UP and Promsic) */
170 struct ifreq ifr;
171 strncpy(ifr.ifr_name, "lo",3);
172
173 /* Get the interface flags, loopback interfaces can be detected that way */
174 err = ioctl(fd, SIOCGIFFLAGS, &ifr);
175 if (err < 0)
176 {
177 wl_logerr("check_loopback, could not get the flags of lo, check if you got a lo loopback interface, cannot continue");
178 close(fd);
179 return 0;
180 }
181 /* Checking the flags for IFF_LOOPBACK flags */
182 if(ifr.ifr_flags && IFF_LOOPBACK)
183 {
184 /* Yes, we do have a loopback interface....sup! */
185 close(fd);
186 wl_loginfo ("check_loopback, check for loopback interface lo successful");
187 return 1;
188 }
189 else
190 {
191 wl_logerr("check_loopback, did not found an interface lo with the IFF_LOOPBACK flag set, cannot continue");
192 close(fd);
193 return 0;
194 }
195 /* Should never be reached */
196 return 0;
197} /*check_loopback */
198
199
200/* Set card into promisc mode */
201int card_set_promisc_up (const char *device)
202{
203 int err;
204 /* First generate a socket to use with iocalls */
205 int fd = socket(AF_INET, SOCK_DGRAM, 0);
206 if (fd < 0)
207 {
208 /* In case of an error */
209 perror("socket");
210 return 0;
211 }
212
213 /* Fill an empty an interface structure with the right flags (UP and Promsic) */
214 struct ifreq ifr;
215 strncpy(ifr.ifr_name, device,10);
216 ifr.ifr_flags = IFF_UP + IFF_PROMISC;
217 err = ioctl(fd, SIOCSIFFLAGS, &ifr);
218 if (err < 0)
219 {
220 perror("Could not access the interface, ");
221 close(fd);
222 return 0;
223 }
224
225 /* Get the informations back from the interface to check if the flags are correct */
226 strncpy(ifr.ifr_name, device,10);
227 err = ioctl(fd, SIOCGIFFLAGS, &ifr);
228 if (err < 0)
229 {
230 perror("Could not access the interface, ");
231 close(fd);
232 return 0;
233 }
234
235 if(ifr.ifr_flags && IFF_UP)
236 {
237 close(fd);
238 return 1;
239 }
240 else
241 {
242 wl_logerr("Could not set promisc flag on %d", device);
243 close(fd);
244 return 0;
245 }
246}
247
248/* Remove card from promisc mode */
249int card_remove_promisc (const char *device)
250{
251 int err;
252 /* First generate a socket to use with iocalls */
253 int fd = socket(AF_INET, SOCK_DGRAM, 0);
254 if (fd < 0)
255 {
256 /* In case of an error */
257 perror("socket");
258 return 0;
259 }
260
261 /* Fill an empty an interface structure with the right flags (UP and Promsic) */
262 struct ifreq ifr;
263 /* Get the flags from the interface*/
264 strncpy(ifr.ifr_name, device,10);
265 err = ioctl(fd, SIOCGIFFLAGS, &ifr);
266 if (err < 0)
267 {
268 perror("Could not access the interface, ");
269 close(fd);
270 return 0;
271 }
272 /* Remove the IFF_PROMISC flag */
273 ifr.ifr_flags = ifr.ifr_flags - IFF_PROMISC;
274 /*Set the new flags to the interface*/
275 err = ioctl(fd, SIOCSIFFLAGS, &ifr);
276 if (err < 0)
277 {
278 perror("Could not access the interface, ");
279 close(fd);
280 return 0;
281 }
282
283 /* Get the flags again to check if IFF_PROMISC is removed */
284 err = ioctl(fd, SIOCGIFFLAGS, &ifr);
285 if (err < 0)
286 {
287 perror("Could not access the interface, ");
288 close(fd);
289 return 0;
290 }
291 if(ifr.ifr_flags && IFF_PROMISC)
292 {
293 wl_logerr("Could not remove the promisc flag on %d", device);
294 close(fd);
295 return 0;
296 }
297 else
298 {
299 /* Successfully removed the promisc flags */
300 close(fd);
301 return 1;
302 }
303}
304
305
306
307
308/* Set channel (Wireless frequency) of the device */
309int card_set_channel (const char *device, int channel, int cardtype)
310{
311 if (cardtype == CARD_TYPE_CISCO || cardtype == CARD_TYPE_NG)
312 {
313 /* Cisco and wlan-ng drivers don't need channelswitching */
314 return 1;
315 }
316 /* If it is a lucent orinocco card */
317 else if (cardtype == CARD_TYPE_ORINOCCO)
318 {
319 int fd;
320 //Wireless tools structure for the iocalls
321 struct iwreq ireq;
322 int *ptr;
323 /* Socket needed to use the iocall to */
324 fd = socket(AF_INET, SOCK_STREAM, 0);
325
326 if ( fd == -1 ) {
327 return -1;
328 }
329
330 ptr = (int *) ireq.u.name;
331 // This is the monitor mode for 802.11 non-prism header
332 ptr[0] = 2;
333 ptr[1] = channel;
334 strcpy(ireq.ifr_ifrn.ifrn_name, device);
335 if (ioctl( fd, SIOCIWFIRSTPRIV + 0x8, &ireq)==0)
336 {
337 /* All was fine... */
338 close(fd);
339 wl_loginfo("Set channel %d on interface %s",channel, device);
340 return 1;
341 }
342 else
343 {
344 /* iocall does not work */
345 close(fd);
346 wl_logerr("Could not set channel %d on %s, check cardtype",channel, device);
347 return 0;
348 }
349 }
350 /* when it is an hostap card you need another iocall for channel switching */
351 else if (cardtype == CARD_TYPE_HOSTAP)
352 {
353 int skfd;
354 skfd = socket(AF_INET, SOCK_STREAM, 0);
355 struct iwreq wrq;
356 iw_float2freq((double) channel, &wrq.u.freq);
357
358 if(iw_set_ext(skfd,(char *) device,SIOCSIWFREQ,&wrq)<0)
359 {
360 wl_logerr("Could not set hostap card %s to channel %d", device, channel);
361 return 0;
362 }
363 else
364 {
365 wl_loginfo("Successfully set hostap card %s to channel %d", device, channel);
366 return 1;
367 }
368 }
369 /* For undefined situations */
370 return 0;
371}
372
373
374int card_detect_channels (char * device)
375{
376 int skfd;
377 skfd = socket(AF_INET, SOCK_STREAM, 0);
378 struct iw_rangerange;
379 /* Get list of frequencies / channels */
380 printf ("\n hostap card get the frequencies");
381 /* Get list of frequencies / channels */
382 if(iw_get_range_info(skfd, device, &range) < 0)
383 { /* We are failsave if something went wrong, asume only 11 channels */
384 return 11;
385 }
386 else
387 {
388 if(range.num_frequency > 0)
389 {
390 return range.num_frequency;
391 }
392 else
393 { /* We are failsave asume only 11 channels */
394 return 11;
395 }
396 }
397
398} /* End of card_detect_channels */
399
400int iw_get_range_info(int skfd, const char * ifname, iw_range * range)
401{
402 struct iwreq wrq2;
403 char buffer[sizeof(iw_range) * 2];/* Large enough */
404
405 /* Cleanup */
406 memset(buffer, 0, sizeof(buffer));
407 wrq2.u.data.pointer = (caddr_t) buffer;
408 wrq2.u.data.length = sizeof(buffer);
409 wrq2.u.data.flags = 0;
410
411 if(iw_get_ext(skfd, (char *)ifname, SIOCGIWRANGE, &wrq2) < 0)
412 {
413 wl_logerr("Could not get the range from the interface");
414 return(-1);
415 }
416 else
417 {
418 /* Copy stuff at the right place, ignore extra */
419 memcpy((char *) range, buffer, sizeof(iw_range));
420 return 0;
421 }
422}
423
424/*------------------------------------------------------------------*/
425/*
426 * Convert our internal representation of frequencies to a floating point.
427 */
428double iw_freq2float(iw_freq * in)
429{
430 int i;
431 double res = (double) in->m;
432 for(i = 0; i < in->e; i++)
433 res *= 10;
434 return(res);
435}
436
437
438 void iw_float2freq(double in, iw_freq *out)
439{
440 /* Version without libm : slower */
441 out->e = 0;
442 while(in > 1e9)
443 {
444 in /= 10;
445 out->e++;
446 }
447 out->m = (long) in;
448}
449
450
451