2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
27 * This program serves as the mediator between the wlan interface and
33 #include "gnunet_constants.h"
34 #include "gnunet_os_lib.h"
35 #include "gnunet_transport_plugin.h"
36 #include "transport.h"
37 #include "gnunet_util_lib.h"
38 #include "plugin_transport_wlan.h"
39 #include "gnunet_common.h"
40 #include "gnunet-transport-wlan-helper.h"
41 #include "ieee80211_radiotap.h"
51 //#include "radiotap.h"
55 { 0x13, 0x22, 0x33, 0x44, 0x55, 0x66 };
57 /* wifi bitrate to use in 500kHz units */
59 static const u8 u8aRatesToUse[] = {
74 #define OFFSET_FLAGS 0x10
75 #define OFFSET_RATE 0x11
77 // this is where we store a summary of the
78 // information from the radiotap header
86 } __attribute__((packed)) PENUMBRA_RADIOTAP_DATA;
89 Dump(u8 * pu8, int nLength)
91 char sz[256], szBuf[512], szChar[17], *buf, fFirst = 1;
92 unsigned char baaLast[2][16];
93 uint n, nPos = 0, nStart = 0, nLine = 0, nSameCount = 0;
98 for (n = 0; n < nLength; n++) {
99 baaLast[(nLine&1)^1][n&0xf] = pu8[n];
100 if ((pu8[n] < 32) || (pu8[n] >= 0x7f))
103 szChar[n&0xf] = pu8[n];
104 szChar[(n&0xf)+1] = '\0';
105 nPos += sprintf(&sz[nPos], "%02X ",
106 baaLast[(nLine&1)^1][n&0xf]);
109 if ((memcmp(baaLast[0], baaLast[1], 16) == 0) && (!fFirst)) {
113 buf += sprintf(buf, "(repeated %d times)\n",
115 buf += sprintf(buf, "%04x: %s %s\n",
121 nPos = 0; nStart = n+1; nLine++;
122 fFirst = 0; sz[0] = '\0'; szChar[0] = '\0';
125 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
127 buf += sprintf(buf, "%04x: %s", nStart, sz);
131 buf += sprintf(buf, " ");
135 buf += sprintf(buf, "%s\n", szChar);
144 "Usage: wlan-hwd [options] <interface>\n\nOptions\n"
145 "-f/--fcs Mark as having FCS (CRC) already\n"
146 " (pkt ends with 4 x sacrificial - chars)\n"
148 " echo -n mon0 > /sys/class/ieee80211/phy0/add_iface\n"
149 " iwconfig mon0 mode monitor\n"
150 " ifconfig mon0 up\n"
151 " wlan-hwd mon0 Spam down mon0 with\n"
152 " radiotap header first\n"
157 int flagHelp = 0, flagMarkWithFCS = 0;
164 * Copyright 2007 Andy Green <andy@warmcat.com>
168 * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
169 * @param iterator: radiotap_iterator to initialize
170 * @param radiotap_header: radiotap header to parse
171 * @param max_length: total length we can parse into (eg, whole packet length)
173 * @return 0 or a negative error code if there is a problem.
175 * This function initializes an opaque iterator struct which can then
176 * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
177 * argument which is present in the header. It knows about extended
178 * present headers and handles them.
181 * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
182 * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
183 * checking for a good 0 return code. Then loop calling
184 * __ieee80211_radiotap_iterator_next()... it returns either 0,
185 * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
186 * The iterator's this_arg member points to the start of the argument
187 * associated with the current argument index that is present, which can be
188 * found in the iterator's this_arg_index member. This arg index corresponds
189 * to the IEEE80211_RADIOTAP_... defines.
191 * Radiotap header length:
192 * You can find the CPU-endian total radiotap header length in
193 * iterator->max_length after executing ieee80211_radiotap_iterator_init()
197 * See Documentation/networking/radiotap-headers.txt
200 int ieee80211_radiotap_iterator_init(
201 struct ieee80211_radiotap_iterator *iterator,
202 struct ieee80211_radiotap_header *radiotap_header,
205 /* Linux only supports version 0 radiotap format */
206 if (radiotap_header->it_version)
209 /* sanity check for allowed length and radiotap length field */
210 if (max_length < le16_to_cpu(radiotap_header->it_len))
213 iterator->rtheader = radiotap_header;
214 iterator->max_length = le16_to_cpu(radiotap_header->it_len);
215 iterator->arg_index = 0;
216 iterator->bitmap_shifter = le32_to_cpu(radiotap_header->it_present);
217 iterator->arg = (u8 *)radiotap_header + sizeof(*radiotap_header);
218 iterator->this_arg = 0;
220 /* find payload start allowing for extended bitmap(s) */
222 if (unlikely(iterator->bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT))) {
223 while (le32_to_cpu(*((u32 *)iterator->arg)) &
224 (1<<IEEE80211_RADIOTAP_EXT)) {
225 iterator->arg += sizeof(u32);
228 * check for insanity where the present bitmaps
229 * keep claiming to extend up to or even beyond the
230 * stated radiotap header length
233 if (((ulong)iterator->arg -
234 (ulong)iterator->rtheader) > iterator->max_length)
238 iterator->arg += sizeof(u32);
241 * no need to check again for blowing past stated radiotap
242 * header length, because ieee80211_radiotap_iterator_next
243 * checks it before it is dereferenced
247 /* we are all initialized happily */
254 * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
255 * @param iterator: radiotap_iterator to move to next arg (if any)
257 * @return 0 if there is an argument to handle,
258 * -ENOENT if there are no more args or -EINVAL
259 * if there is something else wrong.
261 * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
262 * in this_arg_index and sets this_arg to point to the
263 * payload for the field. It takes care of alignment handling and extended
264 * present fields. this_arg can be changed by the caller (eg,
265 * incremented to move inside a compound argument like
266 * IEEE80211_RADIOTAP_CHANNEL). The args pointed to are in
267 * little-endian format whatever the endianess of your CPU.
270 int ieee80211_radiotap_iterator_next(
271 struct ieee80211_radiotap_iterator *iterator)
275 * small length lookup table for all radiotap types we heard of
276 * starting from b0 in the bitmap, so we can walk the payload
277 * area of the radiotap header
279 * There is a requirement to pad args, so that args
280 * of a given length must begin at a boundary of that length
281 * -- but note that compound args are allowed (eg, 2 x u16
282 * for IEEE80211_RADIOTAP_CHANNEL) so total arg length is not
283 * a reliable indicator of alignment requirement.
285 * upper nybble: content alignment for arg
286 * lower nybble: content length for arg
289 static const u8 rt_sizes[] = {
290 [IEEE80211_RADIOTAP_TSFT] = 0x88,
291 [IEEE80211_RADIOTAP_FLAGS] = 0x11,
292 [IEEE80211_RADIOTAP_RATE] = 0x11,
293 [IEEE80211_RADIOTAP_CHANNEL] = 0x24,
294 [IEEE80211_RADIOTAP_FHSS] = 0x22,
295 [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = 0x11,
296 [IEEE80211_RADIOTAP_DBM_ANTNOISE] = 0x11,
297 [IEEE80211_RADIOTAP_LOCK_QUALITY] = 0x22,
298 [IEEE80211_RADIOTAP_TX_ATTENUATION] = 0x22,
299 [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = 0x22,
300 [IEEE80211_RADIOTAP_DBM_TX_POWER] = 0x11,
301 [IEEE80211_RADIOTAP_ANTENNA] = 0x11,
302 [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = 0x11,
303 [IEEE80211_RADIOTAP_DB_ANTNOISE] = 0x11
305 * add more here as they are defined in
306 * include/net/ieee80211_radiotap.h
311 * for every radiotap entry we can at
312 * least skip (by knowing the length)...
315 while (iterator->arg_index < sizeof(rt_sizes)) {
319 if (!(iterator->bitmap_shifter & 1))
320 goto next_entry; /* arg not present */
323 * arg is present, account for alignment padding
324 * 8-bit args can be at any alignment
325 * 16-bit args must start on 16-bit boundary
326 * 32-bit args must start on 32-bit boundary
327 * 64-bit args must start on 64-bit boundary
329 * note that total arg size can differ from alignment of
330 * elements inside arg, so we use upper nybble of length
331 * table to base alignment on
333 * also note: these alignments are ** relative to the
334 * start of the radiotap header **. There is no guarantee
335 * that the radiotap header itself is aligned on any
339 pad = (((ulong)iterator->arg) -
340 ((ulong)iterator->rtheader)) &
341 ((rt_sizes[iterator->arg_index] >> 4) - 1);
344 iterator->arg_index +=
345 (rt_sizes[iterator->arg_index] >> 4) - pad;
348 * this is what we will return to user, but we need to
349 * move on first so next call has something fresh to test
351 iterator->this_arg_index = iterator->arg_index;
352 iterator->this_arg = iterator->arg;
355 /* internally move on the size of this arg */
356 iterator->arg += rt_sizes[iterator->arg_index] & 0x0f;
359 * check for insanity where we are given a bitmap that
360 * claims to have more arg content than the length of the
361 * radiotap section. We will normally end up equalling this
362 * max_length on the last arg, never exceeding it.
365 if (((ulong)iterator->arg - (ulong)iterator->rtheader) >
366 iterator->max_length)
370 iterator->arg_index++;
371 if (unlikely((iterator->arg_index & 31) == 0)) {
372 /* completed current u32 bitmap */
373 if (iterator->bitmap_shifter & 1) {
374 /* b31 was set, there is more */
375 /* move to next u32 bitmap */
376 iterator->bitmap_shifter =
377 le32_to_cpu(*iterator->next_bitmap);
378 iterator->next_bitmap++;
380 /* no more bitmaps: end */
381 iterator->arg_index = sizeof(rt_sizes);
383 } else { /* just try the next bit */
384 iterator->bitmap_shifter >>= 1;
387 /* if we found a valid arg earlier, return it now */
392 /* we don't know how to handle any more args, we're done */
396 #define FIFO_FILE1 "/tmp/MYFIFOin"
397 #define FIFO_FILE2 "/tmp/MYFIFOout"
403 void sigfunc(int sig)
406 if(sig != SIGINT || sig != SIGTERM || sig != SIGKILL)
421 testmode(int argc, char *argv[])
433 //make the fifos if needed
434 if (stat(FIFO_FILE1, &st) != 0)
436 if (stat(FIFO_FILE2, &st) == 0)
438 perror("FIFO 2 exists, but FIFO 1 not");
443 erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0);
444 erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0);
446 if ((fpin = fopen(FIFO_FILE1, "r")) == NULL)
451 if ((fpout = fopen(FIFO_FILE2, "w")) == NULL)
460 if (stat(FIFO_FILE2, &st) != 0)
462 perror("FIFO 1 exists, but FIFO 2 not");
465 if ((fpout = fopen(FIFO_FILE1, "w")) == NULL)
470 if ((fpin = fopen(FIFO_FILE2, "r")) == NULL)
480 fd[0] = STDIN_FILENO;
481 fd[1] = STDOUT_FILENO;
483 if ((pid = fork()) < 0)
485 perror("FORK ERROR");
497 else if (pid == 0) // CHILD PROCESS
500 signal(SIGINT, sigfunc);
501 signal(SIGTERM, sigfunc);
502 signal(SIGKILL, sigfunc);
509 while (closeprog == 0)
513 //while (readc < sizeof( struct RadiotapHeader) + sizeof(struct GNUNET_MessageHeader)){
514 if ((rv = read(fd[0], line, MAXLINE)) < 0)
516 perror("READ ERROR FROM STDIN");
528 perror("writing blub");
529 //fwrite(&line[pos], 1, sizeof(struct GNUNET_MessageHeader), fpout);
531 //pos += sizeof(struct GNUNET_Mes#include "gnunet_util_lib.h"sageHeader);
533 //do not send radiotap header
534 //pos += sizeof( struct RadiotapHeader);
538 ret = fwrite(&line[pos], 1, readc - pos, fpout);
542 perror("Write ERROR FROM fpout");
556 else // PARENT PROCESS
558 signal(SIGINT, sigfunc);
559 signal(SIGTERM, sigfunc);
560 signal(SIGKILL, sigfunc);
565 struct Wlan_Helper_Control_Message macmsg;
567 //Send random mac address
568 macmsg.mac.mac[0] = 0x13;
569 macmsg.mac.mac[1] = 0x22;
570 macmsg.mac.mac[2] = 0x33;
571 macmsg.mac.mac[3] = 0x44;
572 macmsg.mac.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
573 macmsg.mac.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
574 macmsg.hdr.size = sizeof(struct Wlan_Helper_Control_Message);
575 macmsg.hdr.type = GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL;
579 while (pos < sizeof(struct Wlan_Helper_Control_Message))
581 pos += write(fd[1], &macmsg + pos, sizeof(struct Wlan_Helper_Control_Message) - pos);
584 while (closeprog == 0)
586 if ((rv = fread(line, 1, MAXLINE, fpin)) < 0)
588 perror("READ ERROR FROM fpin");
597 ret= write(fd[1], &line[pos], rv - pos);
601 perror("Write ERROR FROM STDout");
622 // Write the input to the output
629 main(int argc, char *argv[])
631 if ((argc==2) && (strstr(argv[1],"1")))
633 return testmode(argc, argv);
636 u8 u8aSendBuffer[500];
637 char szErrbuf[PCAP_ERRBUF_SIZE];
638 int nCaptureHeaderLength = 0, n80211HeaderLength = 0, nLinkEncap = 0;
639 int nOrdinal = 0, r, nDelay = 100000;
640 int nRateIndex = 0, retval, bytes;
641 pcap_t *ppcap = NULL;
642 struct bpf_program bpfprogram;
643 char * szProgram = "", fBrokenSocket = 0;
645 char szHostname[PATH_MAX];
647 if (gethostname(szHostname, sizeof (szHostname) - 1)) {
648 perror("unable to get hostname");
650 szHostname[sizeof (szHostname) - 1] = '\0';
653 printf("Packetspammer (c)2007 Andy Green <andy@warmcat.com> GPL2\n");
657 static const struct option optiona[] = {
658 { "delay", required_argument, NULL, 'd' },
659 { "fcs", no_argument, &flagMarkWithFCS, 1 },
660 { "help", no_argument, &flagHelp, 1 },
661 { "verbose", no_argument, &flagVerbose, 1},
664 int c = getopt_long(argc, argv, "d:hf",
665 optiona, &nOptionIndex);
670 case 0: // long option
677 nDelay = atoi(optarg);
680 case 'f': // mark as FCS attached
684 case 'v': //Verbose / readable output to cout
689 printf("unknown switch %c\n", c);
699 // open the interface in pcap
702 ppcap = pcap_open_live(argv[optind], 800, 1, 20, szErrbuf);
704 printf("Unable to open interface %s in pcap: %s\n",
705 argv[optind], szErrbuf);
709 //get mac from interface
714 sock=socket(PF_INET, SOCK_STREAM, 0);
716 perror("can not open socket\n");
720 if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
721 perror("ioctl(SIOCGIFHWADDR) ");
724 for (j=0, k=0; j<6; j++) {
725 k+=snprintf(mac+k, sizeof(mac)-k-1, j ? ":%02X" : "%02X",
726 (int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
728 mac[sizeof(mac)-1]='\0';
732 nLinkEncap = pcap_datalink(ppcap);
733 nCaptureHeaderLength = 0;home/mwachs/gnb/bin/
735 switch (nLinkEncap) {
737 case DLT_PRISM_HEADER:
738 printf("DLT_PRISM_HEADER Encap\n");
739 nCaptureHeaderLength = 0x40;
740 n80211HeaderLength = 0x20; // ieee80211 comes after this
741 szProgram = "radio[0x4a:4]==0x13223344";
744 case DLT_IEEE802_11_RADIO:
745 printf("DLT_IEEE802_11_RADIO Encap\n");
746 nCaptureHeaderLength = 0x40;
747 n80211HeaderLength = 0x18; // ieee80211 comes after this
748 szProgram = "ether[0x0a:4]==0x13223344";
752 printf("!!! unknown encapsulation on %s !\n", argv[1]);
757 if (pcap_compile(ppcap, &bpfprogram, szProgram, 1, 0) == -1) {
759 puts(pcap_geterr(ppcap));
762 if (pcap_setfilter(ppcap, &bpfprogram) == -1) {
764 puts(pcap_geterr(ppcap));
766 printf("RX Filter applied\n");
768 pcap_freecode(&bpfprogram);
771 pcap_setnonblock(ppcap, 1, szErrbuf);
773 printf(" (delay between packets %dus)\n", nDelay);
775 memset(u8aSendBuffer, 0, sizeof (u8aSendBuffer));
777 while (!fBrokenSocket) {
778 u8 * pu8 = u8aSendBuffer;
779 struct pcap_pkthdr * ppcapPacketHeader = NULL;
780 struct ieee80211_radiotap_iterator rti;
781 PENUMBRA_RADIOTAP_DATA prd;
784 prd.m_nChannel = 255;
785 prd.m_nAntenna = 255;
786 prd.m_nRadiotapFlags = 255;
787 u8 * pu8Payload = u8aSendBuffer;
792 retval = pcap_next_ex(ppcap, &ppcapPacketHeader,
793 (const u_char**)&pu8Payload);
803 u16HeaderLen = (pu8Payload[2] + (pu8Payload[3] << 8));
806 Dump(pu8Payload, u16HeaderLen);
808 if (ppcapPacketHeader->len <
809 (u16HeaderLen + n80211HeaderLength))
812 bytes = ppcapPacketHeader->len -
813 (u16HeaderLen + n80211HeaderLength);
817 if (ieee80211_radiotap_iterator_init(&rti,
818 (struct ieee80211_radiotap_header *)pu8Payload,
822 while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0) {
824 switch (rti.this_arg_index) {
825 case IEEE80211_RADIOTAP_RATE:
826 prd.m_nRate = (*rti.this_arg);
829 case IEEE80211_RADIOTAP_CHANNEL:
831 le16_to_cpu(*((u16 *)rti.this_arg));
832 prd.m_nChannelFlags =
833 le16_to_cpu(*((u16 *)(rti.this_arg + 2)));
836 case IEEE80211_RADIOTAP_ANTENNA:
837 prd.m_nAntenna = (*rti.this_arg) + 1;
840 case IEEE80211_RADIOTAP_FLAGS:
841 prd.m_nRadiotapFlags = *rti.this_arg;
847 pu8Payload += u16HeaderLen + n80211HeaderLength;
849 if (prd.m_nRadiotapFlags & IEEE80211_RADIOTAP_F_FCS)
852 printf("RX: Rate: %2d.%dMbps, Freq: %d.%dGHz, "
853 "Ant: %d, Flags: 0x%X\n",
854 prd.m_nRate / 2, 5 * (prd.m_nRate & 1),
855 prd.m_nChannel / 1000,
856 prd.m_nChannel - ((prd.m_nChannel / 1000) * 1000),
858 prd.m_nRadiotapFlags);
860 Dump(pu8Payload, bytes);
866 memcpy(u8aSendBuffer, u8aRadiotapHeader,
867 sizeof (u8aRadiotapHeader));
869 pu8[OFFSET_FLAGS] |= IEEE80211_RADIOTAP_F_FCS;
870 nRate = pu8[OFFSET_RATE] = u8aRatesToUse[nRateIndex++];
871 if (nRateIndex >= sizeof (u8aRatesToUse))
873 pu8 += sizeof (u8aRadiotapHeader);
875 memcpy(pu8, u8aIeeeHeader, sizeof (u8aIeeeHeader));
876 pu8 += sizeof (u8aIeeeHeader);
878 pu8 += sprintf((char *)u8aSendBuffer,
881 "#%05d -- :-D --%s ----",
882 nRate/2, nOrdinal++, szHostname);
883 r = pcap_inject(ppcap, u8aSendBuffer, pu8 - u8aSendBuffer);
884 if (r != (pu8-u8aSendBuffer)) {
885 perror("Trouble injecting packet");