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
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
37 #include <netpacket/packet.h>
38 #include <linux/if_ether.h>
40 #include <linux/wireless.h>
41 #include <netinet/in.h>
42 #include <linux/if_tun.h>
51 //#include <sys/utsname.h>
52 #include <sys/param.h>
61 //#include "platform.h"
62 #include "gnunet_constants.h"
63 #include "gnunet_os_lib.h"
64 #include "gnunet_transport_plugin.h"
65 #include "transport.h"
66 #include "gnunet_util_lib.h"
67 #include "plugin_transport_wlan.h"
68 #include "gnunet_common.h"
69 #include "gnunet-transport-wlan-helper.h"
70 #include "gnunet_crypto_lib.h"
77 #include "wlan/radiotap-parser.h"
78 /* radiotap-parser defines types like u8 that
79 * ieee80211_radiotap.h needs
81 * we use our local copy of ieee80211_radiotap.h
83 * - since we can't support extensions we don't understand
84 * - since linux does not include it in userspace headers
86 #include "wlan/ieee80211_radiotap.h"
87 #include "wlan/crctable_osdep.h"
88 #include "wlan/loopback_helper.h"
89 #include "wlan/ieee80211.h"
91 #define ARPHRD_IEEE80211 801
92 #define ARPHRD_IEEE80211_PRISM 802
93 #define ARPHRD_IEEE80211_FULL 803
98 #include "wlan/helper_common.h"
99 #include "wlan/loopback_helper.h"
120 static const char * szaDriverTypes[] =
121 { [DT_NULL] = "Unknown", [DT_WLANNG] = "Wlan-NG", [DT_HOSTAP] = "HostAP",
122 [DT_MADWIFI] = "Madwifi", [DT_MADWIFING] = "Madwifi-NG",
123 [DT_BCM43XX] = "BCM43xx", [DT_ORINOCO] = "Orinoco",
124 [DT_ZD1211RW] = "ZD1211RW", [DT_ACX] = "ACX",
125 [DT_MAC80211_RT] = "Mac80211-Radiotap", [DT_AT76USB] = "Atmel 76_usb",
126 [DT_IPW2200] = "ipw2200" };
128 struct Hardware_Infos
131 struct sendbuf *write_pout;
132 int fd_in, arptype_in;
133 int fd_out, arptype_out;
137 DRIVER_TYPE drivertype; /* inited to DT_UNKNOWN on allocation by wi_alloc */
141 struct pcap_file_header pfh_in;
148 char *wlanctlng; /* XXX never set */
154 unsigned char pl_mac[6];
158 //#include "radiotap.h"
162 { 0x13, 0x22, 0x33, 0x44, 0x55, 0x66 };
164 /* wifi bitrate to use in 500kHz units */
166 static const u8 u8aRatesToUse[] =
169 54 * 2, 48 * 2, 36 * 2, 24 * 2, 18 * 2, 12 * 2, 9 * 2, 11 * 2, 11, // 5.5
172 #define OFFSET_FLAGS 0x10
173 #define OFFSET_RATE 0x11
175 // this is where we store a summary of the
176 // information from the radiotap header
184 int m_nRadiotapFlags;
185 }__attribute__((packed)) PENUMBRA_RADIOTAP_DATA;
194 Dump(u8 * pu8, int nLength)
196 char sz[256], szBuf[512], szChar[17], *buf, fFirst = 1;
197 unsigned char baaLast[2][16];
198 uint n, nPos = 0, nStart = 0, nLine = 0, nSameCount = 0;
203 for (n = 0; n < nLength; n++)
205 baaLast[(nLine & 1) ^ 1][n & 0xf] = pu8[n];
206 if ((pu8[n] < 32) || (pu8[n] >= 0x7f))
207 szChar[n & 0xf] = '.';
209 szChar[n & 0xf] = pu8[n];
210 szChar[(n & 0xf) + 1] = '\0';
211 nPos += sprintf(&sz[nPos], "%02X ", baaLast[(nLine & 1) ^ 1][n & 0xf]);
214 if ((memcmp(baaLast[0], baaLast[1], 16) == 0) && (!fFirst))
221 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
222 buf += sprintf(buf, "%04x: %s %s\n", nStart, sz, szChar);
235 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
237 buf += sprintf(buf, "%04x: %s", nStart, sz);
243 buf += sprintf(buf, " ");
247 buf += sprintf(buf, "%s\n", szChar);
254 printf("Usage: interface-name optins\n"
255 "options: 0 = with hardware\n"
256 "1 = first loopback file\n"
257 "2 = second loopback file\n"
263 packet_callback(unsigned char *Args, const struct pcap_pkthdr* Pkthdr,
264 unsigned char *Packet)
266 fprintf(stderr, "+");
271 calc_crc_osdep(unsigned char * buf, int len)
273 unsigned long crc = 0xFFFFFFFF;
275 for (; len > 0; len--, buf++)
276 crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8);
281 /* CRC checksum verification routine */
284 check_crc_buf_osdep(unsigned char *buf, int len)
291 crc = calc_crc_osdep(buf, len);
293 return (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] && ((crc
294 >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]);
297 /* Search a file recursively */
299 searchInside(const char * dir, const char * filename)
314 len = strlen(filename);
315 lentot = strlen(dir) + 256 + 2;
316 curfile = (char *) calloc(1, lentot);
318 while ((ep = readdir(dp)) != NULL)
321 memset(curfile, 0, lentot);
322 sprintf(curfile, "%s/%s", dir, ep->d_name);
324 //Checking if it's the good file
325 if ((int) strlen(ep->d_name) == len && !strcmp(ep->d_name, filename))
332 //If it's a directory and not a link, try to go inside to search
333 if (S_ISDIR(sb.st_mode) && !S_ISLNK(sb.st_mode))
335 //Check if the directory isn't "." or ".."
336 if (strcmp(".", ep->d_name) && strcmp("..", ep->d_name))
339 ret = searchInside(curfile, filename);
354 /* Search a wireless tool and return its path */
356 wiToolsPath(const char * tool)
360 static const char * paths[] =
361 { "/sbin", "/usr/sbin", "/usr/local/sbin", "/bin", "/usr/bin",
362 "/usr/local/bin", "/tmp" };
364 nbelems = sizeof(paths) / sizeof(char *);
366 for (i = 0; i < nbelems; i++)
368 path = searchInside(paths[i], tool);
377 linux_get_channel(struct Hardware_Infos *dev)
383 memset(&wrq, 0, sizeof(struct iwreq));
386 strncpy(wrq.ifr_name, dev->main_if, IFNAMSIZ );
388 strncpy(wrq.ifr_name, dev->iface, IFNAMSIZ );
391 if (dev->drivertype == DT_IPW2200)
394 if (ioctl(fd, SIOCGIWFREQ, &wrq) < 0)
397 frequency = wrq.u.freq.m;
398 if (frequency > 100000000)
400 else if (frequency > 1000000)
403 if (frequency > 1000)
404 chan = getChannelFromFrequency(frequency);
412 linux_read(struct Hardware_Infos * dev, unsigned char *buf, int count,
415 unsigned char tmpbuf[4096];
417 int caplen, n, got_signal, got_noise, got_channel, fcs_removed;
419 caplen = n = got_signal = got_noise = got_channel = fcs_removed = 0;
421 if ((unsigned) count > sizeof(tmpbuf))
423 caplen = read(dev->fd_in, tmpbuf, count);
429 perror("read failed");
433 memset(buf, 0, sizeof(buf));
437 memset(ri, 0, sizeof(*ri));
439 if (dev->arptype_in == ARPHRD_IEEE80211_PRISM)
441 /* skip the prism header */
442 if (tmpbuf[7] == 0x40)
444 /* prism54 uses a different format */
447 ri->ri_power = tmpbuf[0x33];
448 ri->ri_noise = *(unsigned int *) (tmpbuf + 0x33 + 12);
449 ri->ri_rate = (*(unsigned int *) (tmpbuf + 0x33 + 24)) * 500000;
461 ri->ri_mactime = *(u_int64_t*) (tmpbuf + 0x5C - 48);
462 ri->ri_channel = *(unsigned int *) (tmpbuf + 0x5C - 36);
463 ri->ri_power = *(unsigned int *) (tmpbuf + 0x5C);
464 ri->ri_noise = *(unsigned int *) (tmpbuf + 0x5C + 12);
465 ri->ri_rate = (*(unsigned int *) (tmpbuf + 0x5C + 24)) * 500000;
472 n = *(int *) (tmpbuf + 4);
475 if (n < 8 || n >= caplen)
479 if (dev->arptype_in == ARPHRD_IEEE80211_FULL)
481 struct ieee80211_radiotap_iterator iterator;
482 struct ieee80211_radiotap_header *rthdr;
484 rthdr = (struct ieee80211_radiotap_header *) tmpbuf;
486 if (ieee80211_radiotap_iterator_init(&iterator, rthdr, caplen) < 0)
489 /* go through the radiotap arguments we have been given
493 while (ri && (ieee80211_radiotap_iterator_next(&iterator) >= 0))
496 switch (iterator.this_arg_index)
499 case IEEE80211_RADIOTAP_TSFT:
500 ri->ri_mactime = le64_to_cpu(*((uint64_t*) iterator.this_arg));
503 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
506 if (*iterator.this_arg < 127)
507 ri->ri_power = *iterator.this_arg;
509 ri->ri_power = *iterator.this_arg - 255;
515 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
518 if (*iterator.this_arg < 127)
519 ri->ri_power = *iterator.this_arg;
521 ri->ri_power = *iterator.this_arg - 255;
527 case IEEE80211_RADIOTAP_DBM_ANTNOISE:
530 if (*iterator.this_arg < 127)
531 ri->ri_noise = *iterator.this_arg;
533 ri->ri_noise = *iterator.this_arg - 255;
539 case IEEE80211_RADIOTAP_DB_ANTNOISE:
542 if (*iterator.this_arg < 127)
543 ri->ri_noise = *iterator.this_arg;
545 ri->ri_noise = *iterator.this_arg - 255;
551 case IEEE80211_RADIOTAP_ANTENNA:
552 ri->ri_antenna = *iterator.this_arg;
555 case IEEE80211_RADIOTAP_CHANNEL:
556 ri->ri_channel = *iterator.this_arg;
560 case IEEE80211_RADIOTAP_RATE:
561 ri->ri_rate = (*iterator.this_arg) * 500000;
564 case IEEE80211_RADIOTAP_FLAGS:
565 /* is the CRC visible at the end?
568 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS)
574 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_RX_BADFCS)
582 n = le16_to_cpu(rthdr->it_len);
584 if (n <= 0 || n >= caplen)
590 //detect fcs at the end, even if the flag wasn't set and remove it
591 if (fcs_removed == 0 && check_crc_buf_osdep(tmpbuf + n, caplen - 4) == 1)
596 memcpy(buf, tmpbuf + n, caplen);
598 if (ri && !got_channel)
599 ri->ri_channel = linux_get_channel(dev);
605 linux_write(struct Hardware_Infos * dev, unsigned char *buf, unsigned int count)
607 int ret, usedrtap = 0;
608 unsigned short int *p_rtlen;
610 unsigned char * u8aRadiotap = buf;
612 /* Pointer to the radiotap header length field for later use. */
613 p_rtlen = (unsigned short int*) (u8aRadiotap + 2);
615 ret = write(dev->fd_out, buf, count);
619 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || errno
626 perror("write failed");
630 /* radiotap header length is stored little endian on all systems */
632 ret -= letoh16(*p_rtlen);
636 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || errno
643 perror("write failed");
651 openraw(struct Hardware_Infos * dev, char * iface, int fd, int * arptype,
656 struct packet_mreq mr;
657 struct sockaddr_ll sll;
659 /* find the interface index */
661 memset(&ifr, 0, sizeof(ifr));
662 strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
664 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0)
666 printf("Interface %s: \n", iface);
667 perror("ioctl(SIOCGIFINDEX) failed");
671 memset(&sll, 0, sizeof(sll));
672 sll.sll_family = AF_PACKET;
673 sll.sll_ifindex = ifr.ifr_ifindex;
675 switch (dev->drivertype)
678 sll.sll_protocol = htons(ETH_P_ALL);
682 /* lookup the hardware type */
684 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
686 printf("Interface %s: \n", iface);
687 perror("ioctl(SIOCGIFHWADDR) failed");
692 memset(&wrq, 0, sizeof(struct iwreq));
693 strncpy(wrq.ifr_name, iface, IFNAMSIZ);
695 if (ioctl(fd, SIOCGIWMODE, &wrq) < 0)
697 /* most probably not supported (ie for rtap ipw interface) *
698 * so just assume its correctly set... */
699 wrq.u.mode = IW_MODE_MONITOR;
702 if ((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211 && ifr.ifr_hwaddr.sa_family
703 != ARPHRD_IEEE80211_PRISM && ifr.ifr_hwaddr.sa_family
704 != ARPHRD_IEEE80211_FULL) || (wrq.u.mode != IW_MODE_MONITOR))
706 printf("Error: %s not in monitor mode\n", iface);
710 /* Is interface st to up, broadcast & running ? */
711 if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags)
713 /* Bring interface up*/
714 ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING;
716 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
718 perror("ioctl(SIOCSIFFLAGS) failed");
722 /* bind the raw socket to the interface */
724 if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) < 0)
726 printf("Interface %s: \n", iface);
727 perror("bind(ETH_P_ALL) failed");
731 /* lookup the hardware type */
733 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
735 printf("Interface %s: \n", iface);
736 perror("ioctl(SIOCGIFHWADDR) failed");
740 memcpy(mac, (unsigned char*) ifr.ifr_hwaddr.sa_data, 6);
742 *arptype = ifr.ifr_hwaddr.sa_family;
744 if (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211 && ifr.ifr_hwaddr.sa_family
745 != ARPHRD_IEEE80211_PRISM && ifr.ifr_hwaddr.sa_family
746 != ARPHRD_IEEE80211_FULL)
748 if (ifr.ifr_hwaddr.sa_family == 1)
749 fprintf(stderr, "\nARP linktype is set to 1 (Ethernet) ");
751 fprintf(stderr, "\nUnsupported hardware link type %4d ",
752 ifr.ifr_hwaddr.sa_family);
754 fprintf(stderr, "- expected ARPHRD_IEEE80211,\nARPHRD_IEEE80211_"
755 "FULL or ARPHRD_IEEE80211_PRISM instead. Make\n"
756 "sure RFMON is enabled: run 'airmon-ng start %s"
757 " <#>'\nSysfs injection support was not found "
758 "either.\n\n", iface);
762 /* enable promiscuous mode */
764 memset(&mr, 0, sizeof(mr));
765 mr.mr_ifindex = sll.sll_ifindex;
766 mr.mr_type = PACKET_MR_PROMISC;
768 if (setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0)
770 perror("setsockopt(PACKET_MR_PROMISC) failed");
778 wlaninit(struct Hardware_Infos * dev, char *iface)
783 dev->inject_wlanng = 1;
784 dev->rate = 2; /* default to 1Mbps if nothing is set */
787 dev->fd_in = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
790 perror("socket(PF_PACKET) failed at fd_in");
792 fprintf(stderr, "This program requires root privileges.\n");
796 dev->fd_main = socket(PF_PACKET, SOCK_RAW, htons( ETH_P_ALL ) );
797 if (0 > dev->fd_main)
799 perror("socket(PF_PACKET) failed at fd_main");
801 fprintf(stderr, "This program requires root privileges.\n");
805 /* Check iwpriv existence */
807 iwpriv = wiToolsPath("iwpriv");
808 dev->iwpriv = iwpriv;
809 dev->iwconfig = wiToolsPath("iwconfig");
810 dev->ifconfig = wiToolsPath("ifconfig");
814 fprintf(stderr, "Can't find wireless tools, exiting.\n");
818 dev->fd_out = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
821 perror("socket(PF_PACKET) failed at fd_out");
825 /* figure out device type */
827 /* mac80211 radiotap injection
828 * detected based on interface called mon...
829 * since mac80211 allows multiple virtual interfaces
831 * note though that the virtual interfaces are ultimately using a
832 * single physical radio: that means for example they must all
833 * operate on the same channel
836 /* mac80211 stack detection */
837 memset(strbuf, 0, sizeof(strbuf));
838 snprintf(strbuf, sizeof(strbuf) - 1,
839 "ls /sys/class/net/%s/phy80211/subsystem >/dev/null 2>/dev/null", iface);
841 if (system(strbuf) == 0)
842 dev->drivertype = DT_MAC80211_RT;
846 // At the moment only mac80211 tested
851 fprintf(stderr, "Interface %s -> driver: %s\n", iface,
852 szaDriverTypes[dev->drivertype]);
855 if (openraw(dev, iface, dev->fd_out, &dev->arptype_out, dev->pl_mac) != 0)
860 dev->fd_in = dev->fd_out;
862 dev->arptype_in = dev->arptype_out;
865 close_out: close(dev->fd_out);
866 close_in: close(dev->fd_in);
871 stdin_send_hw(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
873 struct Hardware_Infos * dev = cls;
874 struct sendbuf *write_pout = dev->write_pout;
875 struct Radiotap_Send * header = (struct Radiotap_Send *) &hdr[1];
878 unsigned char u8aRadiotap[] =
879 { 0x00, 0x00, // <-- radiotap version
880 0x0c, 0x00, // <- radiotap header length
881 0x04, 0x80, 0x00, 0x00, // <-- bitmap
883 0x00, // <-- padding for natural alignment
884 0x18, 0x00, // <-- TX flags
887 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send)
888 - sizeof(struct GNUNET_MessageHeader);
890 if ((sendsize) > MAXLINE * 2)
892 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n");
896 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
898 fprintf(stderr, "Function stdin_send: wrong packet type\n");
902 u8aRadiotap[8] = header->rate;
904 switch (dev->drivertype)
908 memcpy(write_pout->buf, u8aRadiotap, sizeof(u8aRadiotap));
909 memcpy(write_pout->buf + sizeof(u8aRadiotap), write_pout->buf
910 + sizeof(struct Radiotap_Send) + sizeof(struct GNUNET_MessageHeader),
912 sendsize += sizeof(u8aRadiotap);
920 write_pout->size = sendsize;
924 maketest(unsigned char * buf, struct Hardware_Infos * dev)
927 static uint16_t seqenz = 0;
928 static int first = 0;
930 const int rate = 11000000;
933 "Hallo1Hallo2 Hallo3 Hallo4...998877665544332211Hallo1Hallo2 Hallo3 Hallo4...998877665544332211";
935 unsigned char u8aRadiotap[] =
936 { 0x00, 0x00, // <-- radiotap version
937 0x00, 0x00, // <- radiotap header length
938 0x04, 0x80, 0x02, 0x00, // <-- bitmap
940 0x00, // <-- padding for natural alignment
941 0x10, 0x00, // <-- TX flags
945 /*uint8_t u8aRadiotap[] =
947 0x00, 0x00, // <-- radiotap version
948 0x19, 0x00, // <- radiotap header length
949 0x6f, 0x08, 0x00, 0x00, // <-- bitmap
950 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // <-- timestamp
951 0x00, // <-- flags (Offset +0x10)
952 0x6c, // <-- rate (0ffset +0x11)
953 0x71, 0x09, 0xc0, 0x00, // <-- channel
954 0xde, // <-- antsignal
955 0x00, // <-- antnoise
959 u8aRadiotap[8] = (rate/500000);
960 u8aRadiotap[2] = htole16(sizeof(u8aRadiotap));
962 static struct ieee80211_frame u8aIeeeHeader;
964 uint8_t u8aIeeeHeader_def[] =
965 { 0x08, 0x00, // Frame Control 0x08= 00001000 -> | b1,2 = 0 -> Version 0;
966 // b3,4 = 10 -> Data; b5-8 = 0 -> Normal Data
967 // 0x01 = 00000001 -> | b1 = 1 to DS; b2 = 0 not from DS;
968 0x00, 0x00, // Duration/ID
970 //0x00, 0x1f, 0x3f, 0xd1, 0x8e, 0xe6, // mac1 - in this case receiver
971 0x00, 0x1d, 0xe0, 0xb0, 0x17, 0xdf, // mac1 - in this case receiver
972 0xC0, 0x3F, 0x0E, 0x44, 0x2D, 0x51, // mac2 - in this case sender
973 0x02, 0x1d, 0xe0, 0x00, 0x01, 0xc4,
974 //0x13, 0x22, 0x33, 0x44, 0x55, 0x66, // mac3 - in this case bssid
975 0x10, 0x86, //Sequence Control
979 memcpy(&u8aIeeeHeader, u8aIeeeHeader_def, sizeof(struct ieee80211_frame));
980 memcpy(u8aIeeeHeader.i_addr2, dev->pl_mac, 6);
984 tmp16 = (uint16_t*) u8aIeeeHeader.i_dur;
985 *tmp16 = (uint16_t) htole16((sizeof(txt) + sizeof(struct ieee80211_frame) * 1000000) / rate + 290);
986 tmp16 = (uint16_t*) u8aIeeeHeader.i_seq;
987 *tmp16 = (*tmp16 & IEEE80211_SEQ_FRAG_MASK) | (htole16(seqenz)
988 << IEEE80211_SEQ_SEQ_SHIFT);
991 memcpy(buf, u8aRadiotap, sizeof(u8aRadiotap));
992 memcpy(buf + sizeof(u8aRadiotap), &u8aIeeeHeader, sizeof(u8aIeeeHeader));
993 memcpy(buf + sizeof(u8aRadiotap) + sizeof(u8aIeeeHeader), txt, sizeof(txt));
994 return sizeof(u8aRadiotap) + sizeof(u8aIeeeHeader) + sizeof(txt);
999 hardwaremode(int argc, char *argv[])
1002 struct Hardware_Infos dev;
1004 struct rx_info * rxinfo;
1005 uint8_t * mac = dev.pl_mac;
1008 signal(SIGINT, &sigfunc_hw);
1009 signal(SIGTERM, &sigfunc_hw);
1011 if (wlaninit(&dev, argv[1]))
1016 printf("Device %s -> Ethernet %02x:%02x:%02x:%02x:%02x:%02x\n",
1017 ifreq.ifr_name, (int) mac[0], (int) mac[1], (int) mac[2], (int) mac[3],
1018 (int) mac[4], (int) mac[5]);
1022 char readbuf[MAXLINE];
1024 struct sendbuf write_std;
1028 struct sendbuf write_pout;
1029 write_pout.size = 0;
1032 dev.write_pout = &write_pout;
1042 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
1045 fdpout = dev.fd_out;
1047 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send_hw, &dev);
1051 write_std.size = send_mac_to_plugin((char *) &write_std.buf, mac);
1056 retval = select(0, NULL, NULL, NULL, &tv);
1058 while (0 == closeprog)
1061 write_pout.size = maketest(write_pout.buf, &dev);
1064 retval = select(0, NULL, NULL, NULL, &tv);
1073 // if output queue is empty
1074 if (0 == write_pout.size)
1076 FD_SET(STDIN_FILENO, &rfds);
1079 if (0 == write_std.size)
1081 //FD_SET(fdpin, &rfds);
1085 // if there is something to write
1086 if (0 < write_std.size)
1088 FD_SET(STDOUT_FILENO, &wfds);
1089 maxfd = MAX(maxfd, STDOUT_FILENO);
1092 if (0 < write_pout.size)
1094 FD_SET(fdpout, &wfds);
1095 maxfd = MAX(maxfd, fdpout);
1098 retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv);
1100 if (-1 == retval && EINTR == errno)
1106 fprintf(stderr, "select failed: %s\n", strerror(errno));
1110 if (FD_ISSET(STDOUT_FILENO, &wfds))
1112 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos,
1113 write_std.size - write_std.pos);
1118 fprintf(stderr, "Write ERROR to STDOUT\n");
1123 write_std.pos += ret;
1124 // check if finished
1125 if (write_std.pos == write_std.size)
1133 if (FD_ISSET(fdpout, &wfds))
1136 ret = linux_write(&dev, write_pout.buf, write_pout.size);
1137 //ret = write(fdpout, write_pout.buf + write_pout.pos, write_pout.size
1138 // - write_pout.pos);
1143 fprintf(stderr, "Write ERROR to fdpout\n");
1147 write_pout.pos += ret;
1148 // check if finished
1149 if (write_pout.pos != write_pout.size && ret != 0)
1153 "Write ERROR packet not in one piece send: %u, %u\n",
1154 write_pout.pos, write_pout.size);
1156 else if (write_pout.pos == write_pout.size)
1159 write_pout.size = 0;
1165 if (FD_ISSET(STDIN_FILENO, &rfds))
1167 readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf));
1172 fprintf(stderr, "Read ERROR to STDIN_FILENO\n");
1174 else if (0 < readsize)
1176 GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize,
1177 GNUNET_NO, GNUNET_NO);
1187 if (FD_ISSET(fdpin, &rfds))
1189 rxinfo = (struct rx_info *) (write_pout.buf
1190 + sizeof(struct GNUNET_MessageHeader));
1191 readsize = linux_read(&dev, (unsigned char *) readbuf
1192 + sizeof(struct rx_info) + sizeof(struct GNUNET_MessageHeader),
1193 sizeof(readbuf) - sizeof(struct rx_info)
1194 - sizeof(struct GNUNET_MessageHeader), rxinfo);
1195 //readsize = read(fdpin, readbuf, sizeof(readbuf));
1200 fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno));
1203 else if (0 < readsize)
1221 main(int argc, char *argv[])
1227 "This program must be started with the interface and the operating mode as argument.\n");
1232 if (strstr(argv[2], "1") || strstr(argv[2], "2"))
1235 return testmode(argc, argv);
1239 hardwaremode(argc, argv);
1243 u8 u8aSendBuffer[500];
1244 char szErrbuf[PCAP_ERRBUF_SIZE];
1245 int nCaptureHeaderLength = 0, n80211HeaderLength = 0, nLinkEncap = 0;
1246 int nOrdinal = 0, r, nDelay = 100000;
1247 int nRateIndex = 0, retval, bytes;
1248 pcap_t *ppcap = NULL;
1249 struct bpf_program bpfprogram;
1250 char * szProgram = "", fBrokenSocket = 0;
1252 char szHostname[PATH_MAX];
1254 if (gethostname(szHostname, sizeof (szHostname) - 1))
1256 perror("unable to get hostname");
1258 szHostname[sizeof (szHostname) - 1] = '\0';
1260 printf("Packetspammer (c)2007 Andy Green <andy@warmcat.com> GPL2\n");
1265 static const struct option optiona[] =
1267 { "delay", required_argument, NULL, 'd'},
1268 { "fcs", no_argument, &flagMarkWithFCS, 1},
1269 { "help", no_argument, &flagHelp, 1},
1270 { "verbose", no_argument, &flagVerbose, 1},
1273 int c = getopt_long(argc, argv, "d:hf",
1274 optiona, &nOptionIndex);
1280 case 0: // long option
1287 nDelay = atoi(optarg);
1290 case 'f': // mark as FCS attached
1291 flagMarkWithFCS = 1;
1294 case 'v': //Verbose / readable output to cout
1299 printf("unknown switch %c\n", c);
1308 // open the interface in pcap
1311 ppcap = pcap_open_live(argv[optind], 800, 1, 20, szErrbuf);
1314 printf("Unable to open interface %s in pcap: %s\n",
1315 argv[optind], szErrbuf);
1319 //get mac from interface
1324 sock=socket(PF_INET, SOCK_STREAM, 0);
1326 perror("can not open socket\n");
1330 if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
1331 perror("ioctl(SIOCGIFHWADDR) ");
1334 for (j=0, k=0; j<6; j++) {
1335 k+=snprintf(mac+k, sizeof(mac)-k-1, j ? ":%02X" : "%02X",
1336 (int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
1338 mac[sizeof(mac)-1]='\0';
1342 nLinkEncap = pcap_datalink(ppcap);
1343 nCaptureHeaderLength = 0;
1348 case DLT_PRISM_HEADER:
1349 printf("DLT_PRISM_HEADER Encap\n");
1350 nCaptureHeaderLength = 0x40;
1351 n80211HeaderLength = 0x20; // ieee80211 comes after this
1352 szProgram = "radio[0x4a:4]==0x13223344";
1355 case DLT_IEEE802_11_RADIO:
1356 printf("DLT_IEEE802_11_RADIO Encap\n");
1357 nCaptureHeaderLength = 0x40;
1358 n80211HeaderLength = 0x18; // ieee80211 comes after this
1359 szProgram = "ether[0x0a:4]==0x13223344";
1363 printf("!!! unknown encapsulation on %s !\n", argv[1]);
1368 if (pcap_compile(ppcap, &bpfprogram, szProgram, 1, 0) == -1)
1371 puts(pcap_geterr(ppcap));
1376 if (pcap_setfilter(ppcap, &bpfprogram) == -1)
1379 puts(pcap_geterr(ppcap));
1383 printf("RX Filter applied\n");
1385 pcap_freecode(&bpfprogram);
1388 pcap_setnonblock(ppcap, 1, szErrbuf);
1390 printf(" (delay between packets %dus)\n", nDelay);
1392 memset(u8aSendBuffer, 0, sizeof(u8aSendBuffer));
1394 while (!fBrokenSocket)
1396 u8 * pu8 = u8aSendBuffer;
1397 struct pcap_pkthdr * ppcapPacketHeader = NULL;
1398 struct ieee80211_radiotap_iterator rti;
1399 PENUMBRA_RADIOTAP_DATA prd;
1400 //init of the values
1402 prd.m_nChannel = 255;
1403 prd.m_nAntenna = 255;
1404 prd.m_nRadiotapFlags = 255;
1405 u8 * pu8Payload = u8aSendBuffer;
1410 retval = pcap_next_ex(ppcap, &ppcapPacketHeader,
1411 (const u_char**) &pu8Payload);
1422 u16HeaderLen = (pu8Payload[2] + (pu8Payload[3] << 8));
1425 Dump(pu8Payload, u16HeaderLen);
1427 if (ppcapPacketHeader->len < (u16HeaderLen + n80211HeaderLength))
1430 bytes = ppcapPacketHeader->len - (u16HeaderLen + n80211HeaderLength);
1434 if (ieee80211_radiotap_iterator_init(&rti,
1435 (struct ieee80211_radiotap_header *) pu8Payload, bytes) < 0)
1438 while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0)
1441 switch (rti.this_arg_index)
1443 case IEEE80211_RADIOTAP_RATE:
1444 prd.m_nRate = (*rti.this_arg);
1447 case IEEE80211_RADIOTAP_CHANNEL:
1448 prd.m_nChannel = le16_to_cpu(*((u16 *)rti.this_arg));
1449 prd.m_nChannelFlags = le16_to_cpu(*((u16 *)(rti.this_arg + 2)));
1452 case IEEE80211_RADIOTAP_ANTENNA:
1453 prd.m_nAntenna = (*rti.this_arg) + 1;
1456 case IEEE80211_RADIOTAP_FLAGS:
1457 prd.m_nRadiotapFlags = *rti.this_arg;
1463 pu8Payload += u16HeaderLen + n80211HeaderLength;
1465 if (prd.m_nRadiotapFlags & IEEE80211_RADIOTAP_F_FCS)
1468 printf("RX: Rate: %2d.%dMbps, Freq: %d.%dGHz, "
1469 "Ant: %d, Flags: 0x%X\n", prd.m_nRate / 2, 5 * (prd.m_nRate & 1),
1470 prd.m_nChannel / 1000, prd.m_nChannel - ((prd.m_nChannel / 1000)
1471 * 1000), prd.m_nAntenna, prd.m_nRadiotapFlags);
1473 Dump(pu8Payload, bytes);
1479 memcpy(u8aSendBuffer, u8aRadiotapHeader, sizeof(u8aRadiotapHeader));
1480 if (flagMarkWithFCS)
1481 pu8[OFFSET_FLAGS] |= IEEE80211_RADIOTAP_F_FCS;
1482 nRate = pu8[OFFSET_RATE] = u8aRatesToUse[nRateIndex++];
1483 if (nRateIndex >= sizeof(u8aRatesToUse))
1485 pu8 += sizeof(u8aRadiotapHeader);
1487 memcpy(pu8, u8aIeeeHeader, sizeof(u8aIeeeHeader));
1488 pu8 += sizeof(u8aIeeeHeader);
1490 pu8 += sprintf((char *) u8aSendBuffer, "Packetspammer %02d"
1492 "#%05d -- :-D --%s ----", nRate / 2, nOrdinal++, szHostname);
1493 r = pcap_inject(ppcap, u8aSendBuffer, pu8 - u8aSendBuffer);
1494 if (r != (pu8 - u8aSendBuffer))
1496 perror("Trouble injecting packet");