Documantation and small fixes.
[oweals/gnunet.git] / src / transport / gnunet-transport-wlan-helper.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010, 2011 Christian Grothoff (and other contributing authors)
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 3, or (at your
8  option) any later version.
9
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file src/transport/gnunet-transport-wlan-helper.c
23  * @brief wlan layer two server; must run as root (SUID will do)
24  *        This code will work under GNU/Linux only.
25  * @author David Brodski
26  *
27  * This program serves as the mediator between the wlan interface and
28  * gnunet
29  */
30
31 /**
32  * parts taken from aircrack-ng, parts changend.
33  */
34
35 #define _GNU_SOURCE
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <sys/wait.h>
41 #include <sys/time.h>
42 #include <sys/stat.h>
43 #include <netpacket/packet.h>
44 #include <linux/if_ether.h>
45 #include <linux/if.h>
46 #include <linux/wireless.h>
47 #include <netinet/in.h>
48 #include <linux/if_tun.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <stdarg.h>
53 #include <fcntl.h>
54 #include <errno.h>
55 #include <dirent.h>
56 //#include <sys/utsname.h>
57 #include <sys/param.h>
58
59 /*
60  //#include <resolv.h>
61  #include <string.h>
62  #include <utime.h>
63  #include <getopt.h>
64  */
65 //#include "platform.h"
66 #include "gnunet_constants.h"
67 #include "gnunet_os_lib.h"
68 #include "gnunet_transport_plugin.h"
69 #include "transport.h"
70 #include "gnunet_util_lib.h"
71 #include "plugin_transport_wlan.h"
72 #include "gnunet_common.h"
73 #include "gnunet-transport-wlan-helper.h"
74 #include "gnunet_crypto_lib.h"
75
76 #include "wlan/radiotap-parser.h"
77 /* radiotap-parser defines types like u8 that
78  * ieee80211_radiotap.h needs
79  *
80  * we use our local copy of ieee80211_radiotap.h
81  *
82  * - since we can't support extensions we don't understand
83  * - since linux does not include it in userspace headers
84  */
85 #include "wlan/ieee80211_radiotap.h"
86 #include "wlan/crctable_osdep.h"
87 //#include "wlan/loopback_helper.h"
88 //#include "wlan/ieee80211.h"
89 #include "wlan/helper_common.h"
90
91 #define ARPHRD_IEEE80211        801
92 #define ARPHRD_IEEE80211_PRISM  802
93 #define ARPHRD_IEEE80211_FULL   803
94
95 #define DEBUG 1
96
97 #define MAC_ADDR_SIZE 6
98
99
100 #define IEEE80211_ADDR_LEN      6       /* size of 802.11 address */
101
102 /*
103  * generic definitions for IEEE 802.11 frames
104  */
105 struct ieee80211_frame
106 {
107   u_int8_t i_fc[2];
108   u_int8_t i_dur[2];
109   u_int8_t i_addr1[IEEE80211_ADDR_LEN];
110   u_int8_t i_addr2[IEEE80211_ADDR_LEN];
111   u_int8_t i_addr3[IEEE80211_ADDR_LEN];
112   u_int8_t i_seq[2];
113   /* possibly followed by addr4[IEEE80211_ADDR_LEN]; */
114   /* see below */
115 } GNUNET_PACKED;
116
117 /**
118  * struct for storing the information of the hardware
119  */
120 struct Hardware_Infos
121 {
122
123   /**
124   * send buffer
125   */
126   struct sendbuf write_pout;
127   /**
128    * file descriptor for the raw socket
129    */
130   int fd_raw;
131
132   int arptype_in;
133
134   /**
135    * Name of the interface, not necessarily 0-terminated (!).
136    */
137   char iface[IFNAMSIZ];
138   unsigned char pl_mac[MAC_ADDR_SIZE];
139 };
140
141 struct RadioTapheader
142 {
143   struct ieee80211_radiotap_header header;
144   u8 rate;
145   u8 pad1;
146   u16 txflags;
147 };
148
149 // FIXME: inline?
150 int
151 getChannelFromFrequency (int frequency);
152
153 // FIXME: make nice...
154 /**
155  * function to calculate the crc, the start of the calculation
156  * @param buf buffer to calc the crc
157  * @param len len of the buffer
158  * @return crc sum
159  */
160 static unsigned long
161 calc_crc_osdep (unsigned char *buf, int len)
162 {
163   unsigned long crc = 0xFFFFFFFF;
164
165   for (; len > 0; len--, buf++)
166     crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8);
167
168   return (~crc);
169 }
170
171 /* CRC checksum verification routine */
172
173 // FIXME: make nice...
174 /**
175  * Function to check crc of the wlan packet
176  * @param buf buffer of the packet
177  * @param len len of the data
178  * @return crc sum of the data
179  */
180 static int
181 check_crc_buf_osdep (unsigned char *buf, int len)
182 {
183   unsigned long crc;
184
185   if (0 > len)
186     return 0;
187
188   crc = calc_crc_osdep (buf, len);
189   buf += len;
190   return (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] &&
191           ((crc >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]);
192 }
193
194
195 // FIXME: make nice...
196 /**
197  * function to get the channel of a specific wlan card
198  * @param dev pointer to the dev struct of the card
199  * @return channel number
200  */
201 static int
202 linux_get_channel (struct Hardware_Infos *dev)
203 {
204   struct iwreq wrq;
205   int fd, frequency;
206   int chan = 0;
207
208   memset (&wrq, 0, sizeof (struct iwreq));
209
210   strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ);
211
212   fd = dev->fd_raw;
213   if (0 > ioctl (fd, SIOCGIWFREQ, &wrq))
214     return (-1);
215
216   frequency = wrq.u.freq.m;
217   if (100000000 < frequency)
218     frequency /= 100000;
219   else if (1000000 < frequency)
220     frequency /= 1000;
221
222   if (1000 < frequency)
223     chan = getChannelFromFrequency (frequency);
224   else
225     chan = frequency;
226
227   return chan;
228 }
229
230
231 // FIXME: make nice...
232 /**
233  * function to read from a wlan card
234  * @param dev pointer to the struct of the wlan card
235  * @param buf buffer to read to
236  * @param buf_size size of the buffer
237  * @param ri radiotap_rx info
238  * @return size read from the buffer
239  */
240 static ssize_t
241 linux_read (struct Hardware_Infos *dev, unsigned char *buf,     /* FIXME: void*? */
242             size_t buf_size, struct Radiotap_rx *ri)
243 {
244   unsigned char tmpbuf[buf_size];
245   ssize_t caplen;
246   int n, got_signal, got_noise, got_channel, fcs_removed;
247
248   n = got_signal = got_noise = got_channel = fcs_removed = 0;
249
250   caplen = read (dev->fd_raw, tmpbuf, buf_size);
251   if (0 > caplen)
252   {
253     if (EAGAIN == errno)
254       return 0;
255     fprintf (stderr, "Failed to read from RAW socket: %s\n", strerror (errno));
256     return -1;
257   }
258
259   memset (buf, 0, buf_size);
260   memset (ri, 0, sizeof (*ri));
261
262   switch (dev->arptype_in)
263   {
264   case ARPHRD_IEEE80211_PRISM:
265   {
266     /* skip the prism header */
267     if (tmpbuf[7] == 0x40)
268     {
269       /* prism54 uses a different format */
270       ri->ri_power = tmpbuf[0x33];
271       ri->ri_noise = *(unsigned int *) (tmpbuf + 0x33 + 12);
272       ri->ri_rate = (*(unsigned int *) (tmpbuf + 0x33 + 24)) * 500000;
273       got_signal = 1;
274       got_noise = 1;
275       n = 0x40;
276     }
277     else
278     {
279       ri->ri_mactime = *(u_int64_t *) (tmpbuf + 0x5C - 48);
280       ri->ri_channel = *(unsigned int *) (tmpbuf + 0x5C - 36);
281       ri->ri_power = *(unsigned int *) (tmpbuf + 0x5C);
282       ri->ri_noise = *(unsigned int *) (tmpbuf + 0x5C + 12);
283       ri->ri_rate = (*(unsigned int *) (tmpbuf + 0x5C + 24)) * 500000;
284       got_channel = 1;
285       got_signal = 1;
286       got_noise = 1;
287       n = *(int *) (tmpbuf + 4);
288     }
289
290     if (n < 8 || n >= caplen)
291       return (0);
292   }
293     break;
294
295   case ARPHRD_IEEE80211_FULL:
296   {
297     struct ieee80211_radiotap_iterator iterator;
298     struct ieee80211_radiotap_header *rthdr;
299
300     rthdr = (struct ieee80211_radiotap_header *) tmpbuf;
301
302     if (ieee80211_radiotap_iterator_init (&iterator, rthdr, caplen) < 0)
303       return (0);
304
305     /* go through the radiotap arguments we have been given
306      * by the driver
307      */
308
309     while (ieee80211_radiotap_iterator_next (&iterator) >= 0)
310     {
311
312       switch (iterator.this_arg_index)
313       {
314
315       case IEEE80211_RADIOTAP_TSFT:
316         ri->ri_mactime = le64_to_cpu (*((uint64_t *) iterator.this_arg));
317         break;
318
319       case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
320         if (!got_signal)
321         {
322           if (*iterator.this_arg < 127)
323             ri->ri_power = *iterator.this_arg;
324           else
325             ri->ri_power = *iterator.this_arg - 255;
326
327           got_signal = 1;
328         }
329         break;
330
331       case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
332         if (!got_signal)
333         {
334           if (*iterator.this_arg < 127)
335             ri->ri_power = *iterator.this_arg;
336           else
337             ri->ri_power = *iterator.this_arg - 255;
338
339           got_signal = 1;
340         }
341         break;
342
343       case IEEE80211_RADIOTAP_DBM_ANTNOISE:
344         if (!got_noise)
345         {
346           if (*iterator.this_arg < 127)
347             ri->ri_noise = *iterator.this_arg;
348           else
349             ri->ri_noise = *iterator.this_arg - 255;
350
351           got_noise = 1;
352         }
353         break;
354
355       case IEEE80211_RADIOTAP_DB_ANTNOISE:
356         if (!got_noise)
357         {
358           if (*iterator.this_arg < 127)
359             ri->ri_noise = *iterator.this_arg;
360           else
361             ri->ri_noise = *iterator.this_arg - 255;
362
363           got_noise = 1;
364         }
365         break;
366
367       case IEEE80211_RADIOTAP_ANTENNA:
368         ri->ri_antenna = *iterator.this_arg;
369         break;
370
371       case IEEE80211_RADIOTAP_CHANNEL:
372         ri->ri_channel = *iterator.this_arg;
373         got_channel = 1;
374         break;
375
376       case IEEE80211_RADIOTAP_RATE:
377         ri->ri_rate = (*iterator.this_arg) * 500000;
378         break;
379
380       case IEEE80211_RADIOTAP_FLAGS:
381         /* is the CRC visible at the end?
382          * remove
383          */
384         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS)
385         {
386           fcs_removed = 1;
387           caplen -= 4;
388         }
389
390         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_RX_BADFCS)
391           return (0);
392
393         break;
394       }
395     }
396     n = le16_to_cpu (rthdr->it_len);
397     if (n <= 0 || n >= caplen)
398       return 0;
399   }
400     break;
401   case ARPHRD_IEEE80211:
402     /* do nothing? */
403     break;
404   default:
405     errno = ENOTSUP;
406     return -1;
407   }
408
409   caplen -= n;
410
411   //detect fcs at the end, even if the flag wasn't set and remove it
412   if ((0 == fcs_removed) && (1 == check_crc_buf_osdep (tmpbuf + n, caplen - 4)))
413   {
414     caplen -= 4;
415   }
416   memcpy (buf, tmpbuf + n, caplen);
417   if (!got_channel)
418     ri->ri_channel = linux_get_channel (dev);
419
420   return caplen;
421 }
422
423 /**
424  * function to open the device for read/write
425  * @param dev pointer to the device struct
426  * @return 0 on success
427  */
428 static int
429 openraw (struct Hardware_Infos *dev)
430 {
431   struct ifreq ifr;
432   struct iwreq wrq;
433   struct packet_mreq mr;
434   struct sockaddr_ll sll;
435
436   /* find the interface index */
437   memset (&ifr, 0, sizeof (ifr));
438   strncpy (ifr.ifr_name, dev->iface, IFNAMSIZ);
439   if (-1 == ioctl (dev->fd_raw, SIOCGIFINDEX, &ifr))
440   {
441     fprintf (stderr,
442              "Line: 381 ioctl(SIOCGIFINDEX) on interface `%.*s' failed: %s\n",
443              IFNAMSIZ, dev->iface, strerror (errno));
444     return 1;
445   }
446
447   /* lookup the hardware type */
448   memset (&sll, 0, sizeof (sll));
449   sll.sll_family = AF_PACKET;
450   sll.sll_ifindex = ifr.ifr_ifindex;
451   sll.sll_protocol = htons (ETH_P_ALL);
452   if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
453   {
454     fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
455              IFNAMSIZ, dev->iface, strerror (errno));
456     return 1;
457   }
458
459   /* lookup iw mode */
460   memset (&wrq, 0, sizeof (struct iwreq));
461   strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ);
462   if (-1 == ioctl (dev->fd_raw, SIOCGIWMODE, &wrq))
463   {
464     /* most probably not supported (ie for rtap ipw interface) *
465      * so just assume its correctly set...                     */
466     wrq.u.mode = IW_MODE_MONITOR;
467   }
468
469   if (((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) &&
470        (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
471        (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)) ||
472       (wrq.u.mode != IW_MODE_MONITOR))
473   {
474     fprintf (stderr, "Error: interface `%.*s' is not in monitor mode\n",
475              IFNAMSIZ, dev->iface);
476     return 1;
477   }
478
479   /* Is interface st to up, broadcast & running ? */
480   if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags)
481   {
482     /* Bring interface up */
483     ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING;
484
485     if (-1 == ioctl (dev->fd_raw, SIOCSIFFLAGS, &ifr))
486     {
487       fprintf (stderr,
488                "Line: 434 ioctl(SIOCSIFFLAGS) on interface `%.*s' failed: %s\n",
489                IFNAMSIZ, dev->iface, strerror (errno));
490       return 1;
491     }
492   }
493
494   /* bind the raw socket to the interface */
495   if (-1 == bind (dev->fd_raw, (struct sockaddr *) &sll, sizeof (sll)))
496   {
497     fprintf (stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ,
498              dev->iface, strerror (errno));
499     return 1;
500   }
501
502   /* lookup the hardware type */
503   if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
504   {
505     fprintf (stderr,
506              "Line: 457 ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
507              IFNAMSIZ, dev->iface, strerror (errno));
508     return 1;
509   }
510
511   memcpy (dev->pl_mac, ifr.ifr_hwaddr.sa_data, MAC_ADDR_SIZE);
512   dev->arptype_in = ifr.ifr_hwaddr.sa_family;
513   if ((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) &&
514       (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
515       (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL))
516   {
517     fprintf (stderr, "Unsupported hardware link type %d on interface `%.*s'\n",
518              ifr.ifr_hwaddr.sa_family, IFNAMSIZ, dev->iface);
519     return 1;
520   }
521
522   /* enable promiscuous mode */
523   memset (&mr, 0, sizeof (mr));
524   mr.mr_ifindex = sll.sll_ifindex;
525   mr.mr_type = PACKET_MR_PROMISC;
526   if (0 !=
527       setsockopt (dev->fd_raw, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr,
528                   sizeof (mr)))
529   {
530     fprintf (stderr, "Failed to enable promiscuous mode on interface `%.*s'\n",
531              IFNAMSIZ, dev->iface);
532     return 1;
533   }
534
535   return 0;
536 }
537
538 /**
539  * function to prepare the helper, e.g. sockets, device...
540  * @param dev struct for the device
541  * @param iface name of the interface
542  * @return 0 on success
543  */
544 static int
545 wlaninit (struct Hardware_Infos *dev, const char *iface)
546 {
547   char strbuf[512];
548   struct stat sbuf;
549   int ret;
550
551   dev->fd_raw = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
552   if (0 > dev->fd_raw)
553   {
554     fprintf (stderr, "Failed to create raw socket: %s\n", strerror (errno));
555     return 1;
556   }
557   if (dev->fd_raw >= FD_SETSIZE)
558   {
559     fprintf (stderr, "File descriptor too large for select (%d > %d)\n",
560              dev->fd_raw, FD_SETSIZE);
561     close (dev->fd_raw);
562     return 1;
563   }
564
565   /* mac80211 stack detection */
566   ret =
567       snprintf (strbuf, sizeof (strbuf), "/sys/class/net/%s/phy80211/subsystem",
568                 iface);
569   if ((ret < 0) || (ret >= sizeof (strbuf)) || (0 != stat (strbuf, &sbuf)))
570   {
571     fprintf (stderr, "Did not find 802.11 interface `%s'. Exiting.\n", iface);
572     close (dev->fd_raw);
573     return 1;
574   }
575   strncpy (dev->iface, iface, IFNAMSIZ);
576   if (0 != openraw (dev))
577   {
578     close (dev->fd_raw);
579     return 1;
580   }
581   return 0;
582 }
583
584
585 /**
586  * Function to test incoming packets mac for being our own.
587  *
588  * @param u8aIeeeHeader buffer of the packet
589  * @param dev the Hardware_Infos struct
590  * @return 0 if mac belongs to us, 1 if mac is for another target
591  */
592 static int
593 mac_test (const struct ieee80211_frame *u8aIeeeHeader,
594           const struct Hardware_Infos *dev)
595 {
596   if (0 != memcmp (u8aIeeeHeader->i_addr3, &mac_bssid, MAC_ADDR_SIZE))
597     return 1;
598   if (0 == memcmp (u8aIeeeHeader->i_addr1, dev->pl_mac, MAC_ADDR_SIZE))
599     return 0;
600   if (0 == memcmp (u8aIeeeHeader->i_addr1, &bc_all_mac, MAC_ADDR_SIZE))
601     return 0;
602   return 1;
603 }
604
605
606 /**
607  * function to set the wlan header to make attacks more difficult
608  * @param u8aIeeeHeader pointer to the header of the packet
609  * @param dev pointer to the Hardware_Infos struct
610  */
611 static void
612 mac_set (struct ieee80211_frame *u8aIeeeHeader,
613          const struct Hardware_Infos *dev)
614 {
615   u8aIeeeHeader->i_fc[0] = 0x08;
616   u8aIeeeHeader->i_fc[1] = 0x00;
617   memcpy (u8aIeeeHeader->i_addr2, dev->pl_mac, MAC_ADDR_SIZE);
618   memcpy (u8aIeeeHeader->i_addr3, &mac_bssid, MAC_ADDR_SIZE);
619
620 }
621
622 /**
623  * function to process the data from the stdin
624  * @param cls pointer to the device struct
625  * @param client not used
626  * @param hdr pointer to the start of the packet
627  */
628 static void
629 stdin_send_hw (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
630 {
631   struct Hardware_Infos *dev = cls;
632   struct sendbuf *write_pout = &dev->write_pout;
633   struct Radiotap_Send *header = (struct Radiotap_Send *) &hdr[1];
634   struct ieee80211_frame *wlanheader;
635   size_t sendsize;
636
637   // struct? // FIXME: make nice...
638   struct RadioTapheader rtheader;
639
640   rtheader.header.it_version = 0;
641   rtheader.header.it_len = htole16 (0x0c);
642   rtheader.header.it_present = htole32 (0x00008004);
643   rtheader.rate = 0x00;
644   rtheader.txflags =
645       htole16 (IEEE80211_RADIOTAP_F_TX_NOACK | IEEE80211_RADIOTAP_F_TX_NOSEQ);
646
647   /*  { 0x00, 0x00, <-- radiotap version
648    * 0x0c, 0x00, <- radiotap header length
649    * 0x04, 0x80, 0x00, 0x00,  <-- bitmap
650    * 0x00,  <-- rate
651    * 0x00,  <-- padding for natural alignment
652    * 0x18, 0x00,  <-- TX flags
653    * }; */
654
655   sendsize = ntohs (hdr->size);
656   if (sendsize <
657       sizeof (struct Radiotap_Send) + sizeof (struct GNUNET_MessageHeader))
658   {
659     fprintf (stderr, "Function stdin_send_hw: malformed packet (too small)\n");
660     exit (1);
661   }
662   sendsize -=
663       sizeof (struct Radiotap_Send) + sizeof (struct GNUNET_MessageHeader);
664
665   if (MAXLINE < sendsize)
666   {
667     fprintf (stderr, "Function stdin_send_hw: Packet too big for buffer\n");
668     exit (1);
669   }
670   if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs (hdr->type))
671   {
672     fprintf (stderr, "Function stdin_send: wrong packet type\n");
673     exit (1);
674   }
675
676   rtheader.header.it_len = htole16 (sizeof (rtheader));
677   rtheader.rate = header->rate;
678   memcpy (write_pout->buf, &rtheader, sizeof (rtheader));
679   memcpy (write_pout->buf + sizeof (rtheader), &header[1], sendsize);
680   /* payload contains MAC address, but we don't trust it, so we'll
681    * overwrite it with OUR MAC address again to prevent mischief */
682   wlanheader = (struct ieee80211_frame *) (write_pout->buf + sizeof (rtheader));
683   mac_set (wlanheader, dev);
684   write_pout->size = sendsize + sizeof (rtheader);
685 }
686
687 #if 0
688 /**
689  * Function to make test packets with special options
690  * @param buf buffer to write the data to
691  * @param dev device to send the data from
692  * @return size of packet (what should be send)
693  */
694 static int
695 maketest (unsigned char *buf, struct Hardware_Infos *dev)
696 {
697   uint16_t *tmp16;
698   static uint16_t seqenz = 0;
699   static int first = 0;
700
701   const int rate = 11000000;
702   static const char txt[] =
703       "Hallo1Hallo2 Hallo3 Hallo4...998877665544332211Hallo1Hallo2 Hallo3 Hallo4...998877665544332211";
704
705   unsigned char u8aRadiotap[] = { 0x00, 0x00,   // <-- radiotap version
706     0x00, 0x00,                 // <- radiotap header length
707     0x04, 0x80, 0x02, 0x00,     // <-- bitmap
708     0x00,                       // <-- rate
709     0x00,                       // <-- padding for natural alignment
710     0x10, 0x00,                 // <-- TX flags
711     0x04                        //retries
712   };
713
714   /*uint8_t u8aRadiotap[] =
715    * {
716    * 0x00, 0x00, // <-- radiotap version
717    * 0x19, 0x00, // <- radiotap header length
718    * 0x6f, 0x08, 0x00, 0x00, // <-- bitmap
719    * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // <-- timestamp
720    * 0x00, // <-- flags (Offset +0x10)
721    * 0x6c, // <-- rate (0ffset +0x11)
722    * 0x71, 0x09, 0xc0, 0x00, // <-- channel
723    * 0xde, // <-- antsignal
724    * 0x00, // <-- antnoise
725    * 0x01, // <-- antenna
726    * }; */
727
728   u8aRadiotap[8] = (rate / 500000);
729   u8aRadiotap[2] = htole16 (sizeof (u8aRadiotap));
730
731   static struct ieee80211_frame u8aIeeeHeader;
732
733   uint8_t u8aIeeeHeader_def[] = { 0x08, 0x00,   // Frame Control 0x08= 00001000 -> | b1,2 = 0 -> Version 0;
734     //      b3,4 = 10 -> Data; b5-8 = 0 -> Normal Data
735     //      0x01 = 00000001 -> | b1 = 1 to DS; b2 = 0 not from DS;
736     0x00, 0x00,                 // Duration/ID
737
738     //0x00, 0x1f, 0x3f, 0xd1, 0x8e, 0xe6, // mac1 - in this case receiver
739     0x00, 0x1d, 0xe0, 0xb0, 0x17, 0xdf, // mac1 - in this case receiver
740     0xC0, 0x3F, 0x0E, 0x44, 0x2D, 0x51, // mac2 - in this case sender
741     //0x02, 0x1d, 0xe0, 0x00, 0x01, 0xc4,
742     0x13, 0x22, 0x33, 0x44, 0x55, 0x66, // mac3 - in this case bssid
743     0x10, 0x86,                 //Sequence Control
744   };
745   if (0 == first)
746   {
747     memcpy (&u8aIeeeHeader, u8aIeeeHeader_def, sizeof (struct ieee80211_frame));
748     memcpy (u8aIeeeHeader.i_addr2, dev->pl_mac, MAC_ADDR_SIZE);
749     first = 1;
750   }
751
752   tmp16 = (uint16_t *) u8aIeeeHeader.i_dur;
753   *tmp16 =
754       (uint16_t)
755       htole16 ((sizeof (txt) +
756                 sizeof (struct ieee80211_frame) * 1000000) / rate + 290);
757   tmp16 = (uint16_t *) u8aIeeeHeader.i_seq;
758   *tmp16 =
759       (*tmp16 & IEEE80211_SEQ_FRAG_MASK) | (htole16 (seqenz) <<
760                                             IEEE80211_SEQ_SEQ_SHIFT);
761   seqenz++;
762
763   memcpy (buf, u8aRadiotap, sizeof (u8aRadiotap));
764   memcpy (buf + sizeof (u8aRadiotap), &u8aIeeeHeader, sizeof (u8aIeeeHeader));
765   memcpy (buf + sizeof (u8aRadiotap) + sizeof (u8aIeeeHeader), txt,
766           sizeof (txt));
767   return sizeof (u8aRadiotap) + sizeof (u8aIeeeHeader) + sizeof (txt);
768
769 }
770 #endif
771
772
773 /**
774  * Function to start the hardware for the wlan helper
775  * @param argc number of arguments
776  * @param argv arguments
777  * @return returns one on error
778  */
779 static int
780 hardwaremode (int argc, char *argv[])
781 {
782   uid_t uid;
783   struct Hardware_Infos dev;
784   char readbuf[MAXLINE];
785   struct sendbuf write_std;
786   ssize_t ret;
787   int maxfd;
788   fd_set rfds;
789   fd_set wfds;
790   int retval;
791   int stdin_open;
792   struct GNUNET_SERVER_MessageStreamTokenizer *stdin_mst;
793
794   if (0 != wlaninit (&dev, argv[1]))
795     return 1;
796   uid = getuid ();
797   if (0 != setresuid (uid, uid, uid))
798   {
799     fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
800     /* not critical, continue anyway */
801   }
802
803   dev.write_pout.size = 0;
804   dev.write_pout.pos = 0;
805   stdin_mst = GNUNET_SERVER_mst_create (&stdin_send_hw, &dev);
806
807   /* send mac to STDOUT first */
808   write_std.pos = 0;
809   write_std.size = send_mac_to_plugin ((char *) &write_std.buf, dev.pl_mac);
810   stdin_open = 1;
811
812   while (1)
813   {
814     maxfd = -1;
815     FD_ZERO (&rfds);
816     if ((0 == dev.write_pout.size) && (1 == stdin_open))
817     {
818       FD_SET (STDIN_FILENO, &rfds);
819       maxfd = MAX (maxfd, STDIN_FILENO);
820     }
821     if (0 == write_std.size)
822     {
823       FD_SET (dev.fd_raw, &rfds);
824       maxfd = MAX (maxfd, dev.fd_raw);
825     }
826     FD_ZERO (&wfds);
827     if (0 < write_std.size)
828     {
829       FD_SET (STDOUT_FILENO, &wfds);
830       maxfd = MAX (maxfd, STDOUT_FILENO);
831     }
832     if (0 < dev.write_pout.size)
833     {
834       FD_SET (dev.fd_raw, &wfds);
835       maxfd = MAX (maxfd, dev.fd_raw);
836     }
837     retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL);
838     if ((-1 == retval) && (EINTR == errno))
839       continue;
840     if (0 > retval)
841     {
842       fprintf (stderr, "select failed: %s\n", strerror (errno));
843       break;
844     }
845
846     if (FD_ISSET (STDOUT_FILENO, &wfds))
847     {
848       ret =
849           write (STDOUT_FILENO, write_std.buf + write_std.pos,
850                  write_std.size - write_std.pos);
851       if (0 > ret)
852       {
853         fprintf (stderr, "Failed to write to STDOUT: %s\n", strerror (errno));
854         break;
855       }
856       write_std.pos += ret;
857       if (write_std.pos == write_std.size)
858       {
859         write_std.pos = 0;
860         write_std.size = 0;
861       }
862     }
863
864     if (FD_ISSET (dev.fd_raw, &wfds))
865     {
866       ret = write (dev.fd_raw, dev.write_pout.buf, dev.write_pout.size);
867       if (0 > ret)
868       {
869         fprintf (stderr,
870                  "Line %u: Failed to write to WLAN device: %s, Message-Size: %u\n",
871                  __LINE__, strerror (errno), dev.write_pout.size);
872         break;
873       }
874       dev.write_pout.pos += ret;
875       if ((dev.write_pout.pos != dev.write_pout.size) && (ret != 0))
876       {
877         fprintf (stderr, "Line %u: Write error, partial send: %u/%u\n",
878                  __LINE__, dev.write_pout.pos, dev.write_pout.size);
879         break;
880       }
881       if (dev.write_pout.pos == dev.write_pout.size)
882       {
883         dev.write_pout.pos = 0;
884         dev.write_pout.size = 0;
885       }
886     }
887
888     if (FD_ISSET (STDIN_FILENO, &rfds))
889     {
890       ret = read (STDIN_FILENO, readbuf, sizeof (readbuf));
891       if (0 > ret)
892       {
893         fprintf (stderr, "Read error from STDIN: %s\n", strerror (errno));
894         break;
895       }
896       if (0 == ret)
897       {
898         /* stop reading... */
899         stdin_open = 0;
900       }
901       GNUNET_SERVER_mst_receive (stdin_mst, NULL, readbuf, ret, GNUNET_NO,
902                                  GNUNET_NO);
903     }
904
905     if (FD_ISSET (dev.fd_raw, &rfds))
906     {
907       struct GNUNET_MessageHeader *header;
908       struct Radiotap_rx *rxinfo;
909       struct ieee80211_frame *datastart;
910
911       header = (struct GNUNET_MessageHeader *) write_std.buf;
912       rxinfo = (struct Radiotap_rx *) &header[1];
913       datastart = (struct ieee80211_frame *) &rxinfo[1];
914       ret =
915           linux_read (&dev, (unsigned char *) datastart,
916                       sizeof (write_std.buf) - sizeof (struct Radiotap_rx) -
917                       sizeof (struct GNUNET_MessageHeader), rxinfo);
918       if (0 > ret)
919       {
920         fprintf (stderr, "Read error from raw socket: %s\n", strerror (errno));
921         break;
922       }
923       if ((0 < ret) && (0 == mac_test (datastart, &dev)))
924       {
925         write_std.size =
926             ret + sizeof (struct GNUNET_MessageHeader) +
927             sizeof (struct Radiotap_rx);
928         header->size = htons (write_std.size);
929         header->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
930       }
931     }
932
933   }
934   /* Error handling, try to clean up a bit at least */
935   GNUNET_SERVER_mst_destroy (stdin_mst);
936   close (dev.fd_raw);
937   return 1;
938 }
939
940 /**
941  * main function of the helper
942  * @param argc number of arguments
943  * @param argv arguments
944  * @return 0 on success, 1 on error
945  */
946 int
947 main (int argc, char *argv[])
948 {
949   if (2 != argc)
950   {
951     fprintf (stderr,
952              "This program must be started with the interface as argument.\nThis program was compiled at ----- %s ----\n",
953              __TIMESTAMP__);
954     fprintf (stderr, "Usage: interface-name\n" "\n");
955     return 1;
956   }
957   return hardwaremode (argc, argv);
958 }
959
960 /*
961    *  Copyright (c) 2008, Thomas d'Otreppe
962    *
963    *  Common OSdep stuff
964    *
965    *  This program is free software; you can redistribute it and/or modify
966    *  it under the terms of the GNU General Public License as published by
967    *  the Free Software Foundation; either version 2 of the License, or
968    *  (at your option) any later version.
969    *
970    *  This program is distributed in the hope that it will be useful,
971    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
972    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
973    *  GNU General Public License for more details.
974    *
975    *  You should have received a copy of the GNU General Public License
976    *  along with this program; if not, write to the Free Software
977    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
978    */
979
980 /**
981  * Return the frequency in Mhz from a channel number
982  * @param channel number of the channel
983  * @return frequency of the channel
984  */
985 int
986 getFrequencyFromChannel (int channel)
987 {
988   static int frequencies[] = {
989     -1,                         // No channel 0
990     2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467,
991     2472, 2484,
992     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // Nothing from channel 15 to 34 (exclusive)
993     5170, 5175, 5180, 5185, 5190, 5195, 5200, 5205, 5210, 5215, 5220, 5225,
994     5230, 5235, 5240, 5245,
995     5250, 5255, 5260, 5265, 5270, 5275, 5280, 5285, 5290, 5295, 5300, 5305,
996     5310, 5315, 5320, 5325,
997     5330, 5335, 5340, 5345, 5350, 5355, 5360, 5365, 5370, 5375, 5380, 5385,
998     5390, 5395, 5400, 5405,
999     5410, 5415, 5420, 5425, 5430, 5435, 5440, 5445, 5450, 5455, 5460, 5465,
1000     5470, 5475, 5480, 5485,
1001     5490, 5495, 5500, 5505, 5510, 5515, 5520, 5525, 5530, 5535, 5540, 5545,
1002     5550, 5555, 5560, 5565,
1003     5570, 5575, 5580, 5585, 5590, 5595, 5600, 5605, 5610, 5615, 5620, 5625,
1004     5630, 5635, 5640, 5645,
1005     5650, 5655, 5660, 5665, 5670, 5675, 5680, 5685, 5690, 5695, 5700, 5705,
1006     5710, 5715, 5720, 5725,
1007     5730, 5735, 5740, 5745, 5750, 5755, 5760, 5765, 5770, 5775, 5780, 5785,
1008     5790, 5795, 5800, 5805,
1009     5810, 5815, 5820, 5825, 5830, 5835, 5840, 5845, 5850, 5855, 5860, 5865,
1010     5870, 5875, 5880, 5885,
1011     5890, 5895, 5900, 5905, 5910, 5915, 5920, 5925, 5930, 5935, 5940, 5945,
1012     5950, 5955, 5960, 5965,
1013     5970, 5975, 5980, 5985, 5990, 5995, 6000, 6005, 6010, 6015, 6020, 6025,
1014     6030, 6035, 6040, 6045,
1015     6050, 6055, 6060, 6065, 6070, 6075, 6080, 6085, 6090, 6095, 6100
1016   };
1017
1018   return (channel > 0 && channel <= 221) ? frequencies[channel] : -1;
1019 }
1020
1021 /**
1022  * Return the channel from the frequency (in Mhz)
1023  * @param frequency of the channel
1024  * @return number of the channel
1025  */
1026 int
1027 getChannelFromFrequency (int frequency)
1028 {
1029   if (frequency >= 2412 && frequency <= 2472)
1030     return (frequency - 2407) / 5;
1031   else if (frequency == 2484)
1032     return 14;
1033   else if (frequency >= 5000 && frequency <= 6100)
1034     return (frequency - 5000) / 5;
1035   else
1036     return -1;
1037 }