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-wlan.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 "plugin_transport_wlan.h"
38 #include "gnunet_common.h"
39 #include "gnunet-transport-wlan-helper.h"
40 #include "ieee80211_radiotap.h"
50 //#include "radiotap.h"
54 { 0x13, 0x22, 0x33, 0x44, 0x55, 0x66 };
56 /* wifi bitrate to use in 500kHz units */
58 static const u8 u8aRatesToUse[] = {
73 #define OFFSET_FLAGS 0x10
74 #define OFFSET_RATE 0x11
76 // this is where we store a summary of the
77 // information from the radiotap header
85 } __attribute__((packed)) PENUMBRA_RADIOTAP_DATA;
88 Dump(u8 * pu8, int nLength)
90 char sz[256], szBuf[512], szChar[17], *buf, fFirst = 1;
91 unsigned char baaLast[2][16];
92 uint n, nPos = 0, nStart = 0, nLine = 0, nSameCount = 0;
97 for (n = 0; n < nLength; n++) {
98 baaLast[(nLine&1)^1][n&0xf] = pu8[n];
99 if ((pu8[n] < 32) || (pu8[n] >= 0x7f))
102 szChar[n&0xf] = pu8[n];
103 szChar[(n&0xf)+1] = '\0';
104 nPos += sprintf(&sz[nPos], "%02X ",
105 baaLast[(nLine&1)^1][n&0xf]);
108 if ((memcmp(baaLast[0], baaLast[1], 16) == 0) && (!fFirst)) {
112 buf += sprintf(buf, "(repeated %d times)\n",
114 buf += sprintf(buf, "%04x: %s %s\n",
120 nPos = 0; nStart = n+1; nLine++;
121 fFirst = 0; sz[0] = '\0'; szChar[0] = '\0';
124 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
126 buf += sprintf(buf, "%04x: %s", nStart, sz);
130 buf += sprintf(buf, " ");
134 buf += sprintf(buf, "%s\n", szChar);
143 "Usage: wlan-hwd [options] <interface>\n\nOptions\n"
144 "-f/--fcs Mark as having FCS (CRC) already\n"
145 " (pkt ends with 4 x sacrificial - chars)\n"
147 " echo -n mon0 > /sys/class/ieee80211/phy0/add_iface\n"
148 " iwconfig mon0 mode monitor\n"
149 " ifconfig mon0 up\n"
150 " wlan-hwd mon0 Spam down mon0 with\n"
151 " radiotap header first\n"
156 int flagHelp = 0, flagMarkWithFCS = 0;
163 * Copyright 2007 Andy Green <andy@warmcat.com>
167 * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
168 * @param iterator: radiotap_iterator to initialize
169 * @param radiotap_header: radiotap header to parse
170 * @param max_length: total length we can parse into (eg, whole packet length)
172 * @return 0 or a negative error code if there is a problem.
174 * This function initializes an opaque iterator struct which can then
175 * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
176 * argument which is present in the header. It knows about extended
177 * present headers and handles them.
180 * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
181 * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
182 * checking for a good 0 return code. Then loop calling
183 * __ieee80211_radiotap_iterator_next()... it returns either 0,
184 * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
185 * The iterator's this_arg member points to the start of the argument
186 * associated with the current argument index that is present, which can be
187 * found in the iterator's this_arg_index member. This arg index corresponds
188 * to the IEEE80211_RADIOTAP_... defines.
190 * Radiotap header length:
191 * You can find the CPU-endian total radiotap header length in
192 * iterator->max_length after executing ieee80211_radiotap_iterator_init()
196 * See Documentation/networking/radiotap-headers.txt
199 int ieee80211_radiotap_iterator_init(
200 struct ieee80211_radiotap_iterator *iterator,
201 struct ieee80211_radiotap_header *radiotap_header,
204 /* Linux only supports version 0 radiotap format */
205 if (radiotap_header->it_version)
208 /* sanity check for allowed length and radiotap length field */
209 if (max_length < le16_to_cpu(radiotap_header->it_len))
212 iterator->rtheader = radiotap_header;
213 iterator->max_length = le16_to_cpu(radiotap_header->it_len);
214 iterator->arg_index = 0;
215 iterator->bitmap_shifter = le32_to_cpu(radiotap_header->it_present);
216 iterator->arg = (u8 *)radiotap_header + sizeof(*radiotap_header);
217 iterator->this_arg = 0;
219 /* find payload start allowing for extended bitmap(s) */
221 if (unlikely(iterator->bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT))) {
222 while (le32_to_cpu(*((u32 *)iterator->arg)) &
223 (1<<IEEE80211_RADIOTAP_EXT)) {
224 iterator->arg += sizeof(u32);
227 * check for insanity where the present bitmaps
228 * keep claiming to extend up to or even beyond the
229 * stated radiotap header length
232 if (((ulong)iterator->arg -
233 (ulong)iterator->rtheader) > iterator->max_length)
237 iterator->arg += sizeof(u32);
240 * no need to check again for blowing past stated radiotap
241 * header length, because ieee80211_radiotap_iterator_next
242 * checks it before it is dereferenced
246 /* we are all initialized happily */
253 * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
254 * @param iterator: radiotap_iterator to move to next arg (if any)
256 * @return 0 if there is an argument to handle,
257 * -ENOENT if there are no more args or -EINVAL
258 * if there is something else wrong.
260 * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
261 * in this_arg_index and sets this_arg to point to the
262 * payload for the field. It takes care of alignment handling and extended
263 * present fields. this_arg can be changed by the caller (eg,
264 * incremented to move inside a compound argument like
265 * IEEE80211_RADIOTAP_CHANNEL). The args pointed to are in
266 * little-endian format whatever the endianess of your CPU.
269 int ieee80211_radiotap_iterator_next(
270 struct ieee80211_radiotap_iterator *iterator)
274 * small length lookup table for all radiotap types we heard of
275 * starting from b0 in the bitmap, so we can walk the payload
276 * area of the radiotap header
278 * There is a requirement to pad args, so that args
279 * of a given length must begin at a boundary of that length
280 * -- but note that compound args are allowed (eg, 2 x u16
281 * for IEEE80211_RADIOTAP_CHANNEL) so total arg length is not
282 * a reliable indicator of alignment requirement.
284 * upper nybble: content alignment for arg
285 * lower nybble: content length for arg
288 static const u8 rt_sizes[] = {
289 [IEEE80211_RADIOTAP_TSFT] = 0x88,
290 [IEEE80211_RADIOTAP_FLAGS] = 0x11,
291 [IEEE80211_RADIOTAP_RATE] = 0x11,
292 [IEEE80211_RADIOTAP_CHANNEL] = 0x24,
293 [IEEE80211_RADIOTAP_FHSS] = 0x22,
294 [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = 0x11,
295 [IEEE80211_RADIOTAP_DBM_ANTNOISE] = 0x11,
296 [IEEE80211_RADIOTAP_LOCK_QUALITY] = 0x22,
297 [IEEE80211_RADIOTAP_TX_ATTENUATION] = 0x22,
298 [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = 0x22,
299 [IEEE80211_RADIOTAP_DBM_TX_POWER] = 0x11,
300 [IEEE80211_RADIOTAP_ANTENNA] = 0x11,
301 [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = 0x11,
302 [IEEE80211_RADIOTAP_DB_ANTNOISE] = 0x11
304 * add more here as they are defined in
305 * include/net/ieee80211_radiotap.h
310 * for every radiotap entry we can at
311 * least skip (by knowing the length)...
314 while (iterator->arg_index < sizeof(rt_sizes)) {
318 if (!(iterator->bitmap_shifter & 1))
319 goto next_entry; /* arg not present */
322 * arg is present, account for alignment padding
323 * 8-bit args can be at any alignment
324 * 16-bit args must start on 16-bit boundary
325 * 32-bit args must start on 32-bit boundary
326 * 64-bit args must start on 64-bit boundary
328 * note that total arg size can differ from alignment of
329 * elements inside arg, so we use upper nybble of length
330 * table to base alignment on
332 * also note: these alignments are ** relative to the
333 * start of the radiotap header **. There is no guarantee
334 * that the radiotap header itself is aligned on any
338 pad = (((ulong)iterator->arg) -
339 ((ulong)iterator->rtheader)) &
340 ((rt_sizes[iterator->arg_index] >> 4) - 1);
343 iterator->arg_index +=
344 (rt_sizes[iterator->arg_index] >> 4) - pad;
347 * this is what we will return to user, but we need to
348 * move on first so next call has something fresh to test
350 iterator->this_arg_index = iterator->arg_index;
351 iterator->this_arg = iterator->arg;
354 /* internally move on the size of this arg */
355 iterator->arg += rt_sizes[iterator->arg_index] & 0x0f;
358 * check for insanity where we are given a bitmap that
359 * claims to have more arg content than the length of the
360 * radiotap section. We will normally end up equalling this
361 * max_length on the last arg, never exceeding it.
364 if (((ulong)iterator->arg - (ulong)iterator->rtheader) >
365 iterator->max_length)
369 iterator->arg_index++;
370 if (unlikely((iterator->arg_index & 31) == 0)) {
371 /* completed current u32 bitmap */
372 if (iterator->bitmap_shifter & 1) {
373 /* b31 was set, there is more */
374 /* move to next u32 bitmap */
375 iterator->bitmap_shifter =
376 le32_to_cpu(*iterator->next_bitmap);
377 iterator->next_bitmap++;
379 /* no more bitmaps: end */
380 iterator->arg_index = sizeof(rt_sizes);
382 } else { /* just try the next bit */
383 iterator->bitmap_shifter >>= 1;
386 /* if we found a valid arg earlier, return it now */
391 /* we don't know how to handle any more args, we're done */
395 #define FIFO_FILE1 "/tmp/MYFIFOin"
396 #define FIFO_FILE2 "/tmp/MYFIFOout"
402 void sigfunc(int sig)
405 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);
508 while (closeprog == 0)
512 while (readc < sizeof( struct RadiotapHeader) + sizeof(struct GNUNET_MessageHeader)){
513 if ((rv = read(fd[0], line, MAXLINE)) < 0)
515 perror("READ ERROR FROM STDIN");
522 fwrite(&line[pos], 1, sizeof(struct GNUNET_MessageHeader), fpout);
524 pos += sizeof(struct GNUNET_MessageHeader);
526 //do not send radiotap header
527 pos += sizeof( struct RadiotapHeader);
531 pos += fwrite(&line[pos], 1, readc - pos, fpout);
539 else // PARENT PROCESS
541 signal(SIGINT, sigfunc);
542 signal(SIGTERM, sigfunc);
543 signal(SIGKILL, sigfunc);
547 struct Wlan_Helper_Control_Message macmsg;
549 //Send random mac address
550 macmsg.mac.mac[0] = 0x13;
551 macmsg.mac.mac[1] = 0x22;
552 macmsg.mac.mac[2] = 0x33;
553 macmsg.mac.mac[3] = 0x44;
554 macmsg.mac.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
555 macmsg.mac.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255);
556 macmsg.hdr.size = sizeof(struct Wlan_Helper_Control_Message);
559 while (pos < sizeof(struct Wlan_Helper_Control_Message))
561 pos += write(fd[1], &macmsg + pos, sizeof(struct Wlan_Helper_Control_Message) - pos);
564 while (closeprog == 0)
566 if ((rv = fread(line, 1, MAXLINE, fpin)) < 0)
568 perror("READ ERROR FROM fpin");
574 pos += write(fd[1], &line[pos], rv - pos);
589 // Write the input to the output
596 main(int argc, char *argv[])
598 if ((argc==3) && (strstr(argv[2],"1")))
600 return testmode(argc, argv);
603 u8 u8aSendBuffer[500];
604 char szErrbuf[PCAP_ERRBUF_SIZE];
605 int nCaptureHeaderLength = 0, n80211HeaderLength = 0, nLinkEncap = 0;
606 int nOrdinal = 0, r, nDelay = 100000;
607 int nRateIndex = 0, retval, bytes;
608 pcap_t *ppcap = NULL;
609 struct bpf_program bpfprogram;
610 char * szProgram = "", fBrokenSocket = 0;
612 char szHostname[PATH_MAX];
614 if (gethostname(szHostname, sizeof (szHostname) - 1)) {
615 perror("unable to get hostname");
617 szHostname[sizeof (szHostname) - 1] = '\0';
620 printf("Packetspammer (c)2007 Andy Green <andy@warmcat.com> GPL2\n");
624 static const struct option optiona[] = {
625 { "delay", required_argument, NULL, 'd' },
626 { "fcs", no_argument, &flagMarkWithFCS, 1 },
627 { "help", no_argument, &flagHelp, 1 },
628 { "verbose", no_argument, &flagVerbose, 1},
631 int c = getopt_long(argc, argv, "d:hf",
632 optiona, &nOptionIndex);
637 case 0: // long option
644 nDelay = atoi(optarg);
647 case 'f': // mark as FCS attached
651 case 'v': //Verbose / readable output to cout
656 printf("unknown switch %c\n", c);
666 // open the interface in pcap
669 ppcap = pcap_open_live(argv[optind], 800, 1, 20, szErrbuf);
671 printf("Unable to open interface %s in pcap: %s\n",
672 argv[optind], szErrbuf);
676 //get mac from interface
681 sock=socket(PF_INET, SOCK_STREAM, 0);
683 perror("can not open socket\n");
687 if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
688 perror("ioctl(SIOCGIFHWADDR) ");
691 for (j=0, k=0; j<6; j++) {
692 k+=snprintf(mac+k, sizeof(mac)-k-1, j ? ":%02X" : "%02X",
693 (int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
695 mac[sizeof(mac)-1]='\0';
699 nLinkEncap = pcap_datalink(ppcap);
700 nCaptureHeaderLength = 0;
702 switch (nLinkEncap) {
704 case DLT_PRISM_HEADER:
705 printf("DLT_PRISM_HEADER Encap\n");
706 nCaptureHeaderLength = 0x40;
707 n80211HeaderLength = 0x20; // ieee80211 comes after this
708 szProgram = "radio[0x4a:4]==0x13223344";
711 case DLT_IEEE802_11_RADIO:
712 printf("DLT_IEEE802_11_RADIO Encap\n");
713 nCaptureHeaderLength = 0x40;
714 n80211HeaderLength = 0x18; // ieee80211 comes after this
715 szProgram = "ether[0x0a:4]==0x13223344";
719 printf("!!! unknown encapsulation on %s !\n", argv[1]);
724 if (pcap_compile(ppcap, &bpfprogram, szProgram, 1, 0) == -1) {
726 puts(pcap_geterr(ppcap));
729 if (pcap_setfilter(ppcap, &bpfprogram) == -1) {
731 puts(pcap_geterr(ppcap));
733 printf("RX Filter applied\n");
735 pcap_freecode(&bpfprogram);
738 pcap_setnonblock(ppcap, 1, szErrbuf);
740 printf(" (delay between packets %dus)\n", nDelay);
742 memset(u8aSendBuffer, 0, sizeof (u8aSendBuffer));
744 while (!fBrokenSocket) {
745 u8 * pu8 = u8aSendBuffer;
746 struct pcap_pkthdr * ppcapPacketHeader = NULL;
747 struct ieee80211_radiotap_iterator rti;
748 PENUMBRA_RADIOTAP_DATA prd;
751 prd.m_nChannel = 255;
752 prd.m_nAntenna = 255;
753 prd.m_nRadiotapFlags = 255;
754 u8 * pu8Payload = u8aSendBuffer;
759 retval = pcap_next_ex(ppcap, &ppcapPacketHeader,
760 (const u_char**)&pu8Payload);
770 u16HeaderLen = (pu8Payload[2] + (pu8Payload[3] << 8));
773 Dump(pu8Payload, u16HeaderLen);
775 if (ppcapPacketHeader->len <
776 (u16HeaderLen + n80211HeaderLength))
779 bytes = ppcapPacketHeader->len -
780 (u16HeaderLen + n80211HeaderLength);
784 if (ieee80211_radiotap_iterator_init(&rti,
785 (struct ieee80211_radiotap_header *)pu8Payload,
789 while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0) {
791 switch (rti.this_arg_index) {
792 case IEEE80211_RADIOTAP_RATE:
793 prd.m_nRate = (*rti.this_arg);
796 case IEEE80211_RADIOTAP_CHANNEL:
798 le16_to_cpu(*((u16 *)rti.this_arg));
799 prd.m_nChannelFlags =
800 le16_to_cpu(*((u16 *)(rti.this_arg + 2)));
803 case IEEE80211_RADIOTAP_ANTENNA:
804 prd.m_nAntenna = (*rti.this_arg) + 1;
807 case IEEE80211_RADIOTAP_FLAGS:
808 prd.m_nRadiotapFlags = *rti.this_arg;
814 pu8Payload += u16HeaderLen + n80211HeaderLength;
816 if (prd.m_nRadiotapFlags & IEEE80211_RADIOTAP_F_FCS)
819 printf("RX: Rate: %2d.%dMbps, Freq: %d.%dGHz, "
820 "Ant: %d, Flags: 0x%X\n",
821 prd.m_nRate / 2, 5 * (prd.m_nRate & 1),
822 prd.m_nChannel / 1000,
823 prd.m_nChannel - ((prd.m_nChannel / 1000) * 1000),
825 prd.m_nRadiotapFlags);
827 Dump(pu8Payload, bytes);
833 memcpy(u8aSendBuffer, u8aRadiotapHeader,
834 sizeof (u8aRadiotapHeader));
836 pu8[OFFSET_FLAGS] |= IEEE80211_RADIOTAP_F_FCS;
837 nRate = pu8[OFFSET_RATE] = u8aRatesToUse[nRateIndex++];
838 if (nRateIndex >= sizeof (u8aRatesToUse))
840 pu8 += sizeof (u8aRadiotapHeader);
842 memcpy(pu8, u8aIeeeHeader, sizeof (u8aIeeeHeader));
843 pu8 += sizeof (u8aIeeeHeader);
845 pu8 += sprintf((char *)u8aSendBuffer,
848 "#%05d -- :-D --%s ----",
849 nRate/2, nOrdinal++, szHostname);
850 r = pcap_inject(ppcap, u8aSendBuffer, pu8 - u8aSendBuffer);
851 if (r != (pu8-u8aSendBuffer)) {
852 perror("Trouble injecting packet");