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>
50 //#include <sys/utsname.h>
51 #include <sys/param.h>
60 //#include "platform.h"
61 #include "gnunet_constants.h"
62 #include "gnunet_os_lib.h"
63 #include "gnunet_transport_plugin.h"
64 #include "transport.h"
65 #include "gnunet_util_lib.h"
66 #include "plugin_transport_wlan.h"
67 #include "gnunet_common.h"
68 #include "gnunet-transport-wlan-helper.h"
69 #include "gnunet_crypto_lib.h"
76 #include "wlan/radiotap-parser.h"
77 /* radiotap-parser defines types like u8 that
78 * ieee80211_radiotap.h needs
80 * we use our local copy of ieee80211_radiotap.h
82 * - since we can't support extensions we don't understand
83 * - since linux does not include it in userspace headers
85 #include "wlan/ieee80211_radiotap.h"
86 #include "wlan/crctable_osdep.h"
87 #include "wlan/loopback_helper.h"
89 #define ARPHRD_IEEE80211 801
90 #define ARPHRD_IEEE80211_PRISM 802
91 #define ARPHRD_IEEE80211_FULL 803
96 #include "wlan/helper_common.h"
97 #include "wlan/loopback_helper.h"
118 static const char * szaDriverTypes[] =
119 { [DT_NULL] = "Unknown", [DT_WLANNG] = "Wlan-NG", [DT_HOSTAP] = "HostAP",
120 [DT_MADWIFI] = "Madwifi", [DT_MADWIFING] = "Madwifi-NG",
121 [DT_BCM43XX] = "BCM43xx", [DT_ORINOCO] = "Orinoco",
122 [DT_ZD1211RW] = "ZD1211RW", [DT_ACX] = "ACX",
123 [DT_MAC80211_RT] = "Mac80211-Radiotap", [DT_AT76USB] = "Atmel 76_usb",
124 [DT_IPW2200] = "ipw2200" };
126 struct Hardware_Infos
129 struct sendbuf *write_pout;
130 int fd_in, arptype_in;
131 int fd_out, arptype_out;
135 DRIVER_TYPE drivertype; /* inited to DT_UNKNOWN on allocation by wi_alloc */
139 struct pcap_file_header pfh_in;
146 char *wlanctlng; /* XXX never set */
152 unsigned char pl_mac[6];
156 //#include "radiotap.h"
160 { 0x13, 0x22, 0x33, 0x44, 0x55, 0x66 };
162 /* wifi bitrate to use in 500kHz units */
164 static const u8 u8aRatesToUse[] =
167 54 * 2, 48 * 2, 36 * 2, 24 * 2, 18 * 2, 12 * 2, 9 * 2, 11 * 2, 11, // 5.5
170 #define OFFSET_FLAGS 0x10
171 #define OFFSET_RATE 0x11
173 // this is where we store a summary of the
174 // information from the radiotap header
182 int m_nRadiotapFlags;
183 }__attribute__((packed)) PENUMBRA_RADIOTAP_DATA;
192 Dump(u8 * pu8, int nLength)
194 char sz[256], szBuf[512], szChar[17], *buf, fFirst = 1;
195 unsigned char baaLast[2][16];
196 uint n, nPos = 0, nStart = 0, nLine = 0, nSameCount = 0;
201 for (n = 0; n < nLength; n++)
203 baaLast[(nLine & 1) ^ 1][n & 0xf] = pu8[n];
204 if ((pu8[n] < 32) || (pu8[n] >= 0x7f))
205 szChar[n & 0xf] = '.';
207 szChar[n & 0xf] = pu8[n];
208 szChar[(n & 0xf) + 1] = '\0';
209 nPos += sprintf(&sz[nPos], "%02X ", baaLast[(nLine & 1) ^ 1][n & 0xf]);
212 if ((memcmp(baaLast[0], baaLast[1], 16) == 0) && (!fFirst))
219 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
220 buf += sprintf(buf, "%04x: %s %s\n", nStart, sz, szChar);
233 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
235 buf += sprintf(buf, "%04x: %s", nStart, sz);
241 buf += sprintf(buf, " ");
245 buf += sprintf(buf, "%s\n", szChar);
252 printf("Usage: wlan-hwd [options] <interface>\n\nOptions\n"
253 "-f/--fcs Mark as having FCS (CRC) already\n"
254 " (pkt ends with 4 x sacrificial - chars)\n"
256 " echo -n mon0 > /sys/class/ieee80211/phy0/add_iface\n"
257 " iwconfig mon0 mode monitor\n"
258 " ifconfig mon0 up\n"
259 " wlan-hwd mon0 Spam down mon0 with\n"
260 " radiotap header first\n"
266 packet_callback(unsigned char *Args, const struct pcap_pkthdr* Pkthdr,
267 unsigned char *Packet)
269 fprintf(stderr, "+");
274 calc_crc_osdep(unsigned char * buf, int len)
276 unsigned long crc = 0xFFFFFFFF;
278 for (; len > 0; len--, buf++)
279 crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8);
284 /* CRC checksum verification routine */
287 check_crc_buf_osdep(unsigned char *buf, int len)
294 crc = calc_crc_osdep(buf, len);
296 return (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] && ((crc
297 >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]);
300 /* Search a file recursively */
302 searchInside(const char * dir, const char * filename)
317 len = strlen(filename);
318 lentot = strlen(dir) + 256 + 2;
319 curfile = (char *) calloc(1, lentot);
321 while ((ep = readdir(dp)) != NULL)
324 memset(curfile, 0, lentot);
325 sprintf(curfile, "%s/%s", dir, ep->d_name);
327 //Checking if it's the good file
328 if ((int) strlen(ep->d_name) == len && !strcmp(ep->d_name, filename))
335 //If it's a directory and not a link, try to go inside to search
336 if (S_ISDIR(sb.st_mode) && !S_ISLNK(sb.st_mode))
338 //Check if the directory isn't "." or ".."
339 if (strcmp(".", ep->d_name) && strcmp("..", ep->d_name))
342 ret = searchInside(curfile, filename);
357 /* Search a wireless tool and return its path */
359 wiToolsPath(const char * tool)
363 static const char * paths[] =
364 { "/sbin", "/usr/sbin", "/usr/local/sbin", "/bin", "/usr/bin",
365 "/usr/local/bin", "/tmp" };
367 nbelems = sizeof(paths) / sizeof(char *);
369 for (i = 0; i < nbelems; i++)
371 path = searchInside(paths[i], tool);
380 linux_get_channel(struct Hardware_Infos *dev)
386 memset(&wrq, 0, sizeof(struct iwreq));
389 strncpy(wrq.ifr_name, dev->main_if, IFNAMSIZ );
391 strncpy(wrq.ifr_name, dev->iface, IFNAMSIZ );
394 if (dev->drivertype == DT_IPW2200)
397 if (ioctl(fd, SIOCGIWFREQ, &wrq) < 0)
400 frequency = wrq.u.freq.m;
401 if (frequency > 100000000)
403 else if (frequency > 1000000)
406 if (frequency > 1000)
407 chan = getChannelFromFrequency(frequency);
415 linux_read(struct Hardware_Infos * dev, unsigned char *buf, int count,
418 unsigned char tmpbuf[4096];
420 int caplen, n, got_signal, got_noise, got_channel, fcs_removed;
422 caplen = n = got_signal = got_noise = got_channel = fcs_removed = 0;
424 if ((unsigned) count > sizeof(tmpbuf))
426 caplen = read(dev->fd_in, tmpbuf, count);
432 perror("read failed");
436 memset(buf, 0, sizeof(buf));
440 memset(ri, 0, sizeof(*ri));
442 if (dev->arptype_in == ARPHRD_IEEE80211_PRISM)
444 /* skip the prism header */
445 if (tmpbuf[7] == 0x40)
447 /* prism54 uses a different format */
450 ri->ri_power = tmpbuf[0x33];
451 ri->ri_noise = *(unsigned int *) (tmpbuf + 0x33 + 12);
452 ri->ri_rate = (*(unsigned int *) (tmpbuf + 0x33 + 24)) * 500000;
464 ri->ri_mactime = *(u_int64_t*) (tmpbuf + 0x5C - 48);
465 ri->ri_channel = *(unsigned int *) (tmpbuf + 0x5C - 36);
466 ri->ri_power = *(unsigned int *) (tmpbuf + 0x5C);
467 ri->ri_noise = *(unsigned int *) (tmpbuf + 0x5C + 12);
468 ri->ri_rate = (*(unsigned int *) (tmpbuf + 0x5C + 24)) * 500000;
475 n = *(int *) (tmpbuf + 4);
478 if (n < 8 || n >= caplen)
482 if (dev->arptype_in == ARPHRD_IEEE80211_FULL)
484 struct ieee80211_radiotap_iterator iterator;
485 struct ieee80211_radiotap_header *rthdr;
487 rthdr = (struct ieee80211_radiotap_header *) tmpbuf;
489 if (ieee80211_radiotap_iterator_init(&iterator, rthdr, caplen) < 0)
492 /* go through the radiotap arguments we have been given
496 while (ri && (ieee80211_radiotap_iterator_next(&iterator) >= 0))
499 switch (iterator.this_arg_index)
502 case IEEE80211_RADIOTAP_TSFT:
503 ri->ri_mactime = le64_to_cpu(*((uint64_t*) iterator.this_arg));
506 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
509 if (*iterator.this_arg < 127)
510 ri->ri_power = *iterator.this_arg;
512 ri->ri_power = *iterator.this_arg - 255;
518 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
521 if (*iterator.this_arg < 127)
522 ri->ri_power = *iterator.this_arg;
524 ri->ri_power = *iterator.this_arg - 255;
530 case IEEE80211_RADIOTAP_DBM_ANTNOISE:
533 if (*iterator.this_arg < 127)
534 ri->ri_noise = *iterator.this_arg;
536 ri->ri_noise = *iterator.this_arg - 255;
542 case IEEE80211_RADIOTAP_DB_ANTNOISE:
545 if (*iterator.this_arg < 127)
546 ri->ri_noise = *iterator.this_arg;
548 ri->ri_noise = *iterator.this_arg - 255;
554 case IEEE80211_RADIOTAP_ANTENNA:
555 ri->ri_antenna = *iterator.this_arg;
558 case IEEE80211_RADIOTAP_CHANNEL:
559 ri->ri_channel = *iterator.this_arg;
563 case IEEE80211_RADIOTAP_RATE:
564 ri->ri_rate = (*iterator.this_arg) * 500000;
567 case IEEE80211_RADIOTAP_FLAGS:
568 /* is the CRC visible at the end?
571 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS)
577 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_RX_BADFCS)
585 n = le16_to_cpu(rthdr->it_len);
587 if (n <= 0 || n >= caplen)
593 //detect fcs at the end, even if the flag wasn't set and remove it
594 if (fcs_removed == 0 && check_crc_buf_osdep(tmpbuf + n, caplen - 4) == 1)
599 memcpy(buf, tmpbuf + n, caplen);
601 if (ri && !got_channel)
602 ri->ri_channel = linux_get_channel(dev);
608 linux_write(struct Hardware_Infos * dev, unsigned char *buf, unsigned int count)
610 int ret, usedrtap = 0;
611 unsigned short int *p_rtlen;
613 unsigned char * u8aRadiotap = buf;
615 /* Pointer to the radiotap header length field for later use. */
616 p_rtlen = (unsigned short int*) (u8aRadiotap + 2);
618 ret = write(dev->fd_out, buf, count);
622 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || errno
629 perror("write failed");
633 /* radiotap header length is stored little endian on all systems */
635 ret -= letoh16(*p_rtlen);
639 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || errno
646 perror("write failed");
654 openraw(struct Hardware_Infos * dev, char * iface, int fd, int * arptype,
659 struct packet_mreq mr;
660 struct sockaddr_ll sll;
662 /* find the interface index */
664 memset(&ifr, 0, sizeof(ifr));
665 strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
667 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0)
669 printf("Interface %s: \n", iface);
670 perror("ioctl(SIOCGIFINDEX) failed");
674 memset(&sll, 0, sizeof(sll));
675 sll.sll_family = AF_PACKET;
676 sll.sll_ifindex = ifr.ifr_ifindex;
678 switch (dev->drivertype)
681 sll.sll_protocol = htons(ETH_P_ALL);
685 /* lookup the hardware type */
687 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
689 printf("Interface %s: \n", iface);
690 perror("ioctl(SIOCGIFHWADDR) failed");
695 memset(&wrq, 0, sizeof(struct iwreq));
696 strncpy(wrq.ifr_name, iface, IFNAMSIZ);
698 if (ioctl(fd, SIOCGIWMODE, &wrq) < 0)
700 /* most probably not supported (ie for rtap ipw interface) *
701 * so just assume its correctly set... */
702 wrq.u.mode = IW_MODE_MONITOR;
705 if ((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211 && ifr.ifr_hwaddr.sa_family
706 != ARPHRD_IEEE80211_PRISM && ifr.ifr_hwaddr.sa_family
707 != ARPHRD_IEEE80211_FULL) || (wrq.u.mode != IW_MODE_MONITOR))
709 printf("Error: %s not in monitor mode\n", iface);
713 /* Is interface st to up, broadcast & running ? */
714 if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags)
716 /* Bring interface up*/
717 ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING;
719 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
721 perror("ioctl(SIOCSIFFLAGS) failed");
725 /* bind the raw socket to the interface */
727 if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) < 0)
729 printf("Interface %s: \n", iface);
730 perror("bind(ETH_P_ALL) failed");
734 /* lookup the hardware type */
736 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
738 printf("Interface %s: \n", iface);
739 perror("ioctl(SIOCGIFHWADDR) failed");
743 memcpy(mac, (unsigned char*) ifr.ifr_hwaddr.sa_data, 6);
745 *arptype = ifr.ifr_hwaddr.sa_family;
747 if (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211 && ifr.ifr_hwaddr.sa_family
748 != ARPHRD_IEEE80211_PRISM && ifr.ifr_hwaddr.sa_family
749 != ARPHRD_IEEE80211_FULL)
751 if (ifr.ifr_hwaddr.sa_family == 1)
752 fprintf(stderr, "\nARP linktype is set to 1 (Ethernet) ");
754 fprintf(stderr, "\nUnsupported hardware link type %4d ",
755 ifr.ifr_hwaddr.sa_family);
757 fprintf(stderr, "- expected ARPHRD_IEEE80211,\nARPHRD_IEEE80211_"
758 "FULL or ARPHRD_IEEE80211_PRISM instead. Make\n"
759 "sure RFMON is enabled: run 'airmon-ng start %s"
760 " <#>'\nSysfs injection support was not found "
761 "either.\n\n", iface);
765 /* enable promiscuous mode */
767 memset(&mr, 0, sizeof(mr));
768 mr.mr_ifindex = sll.sll_ifindex;
769 mr.mr_type = PACKET_MR_PROMISC;
771 if (setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0)
773 perror("setsockopt(PACKET_MR_PROMISC) failed");
781 wlaninit(struct Hardware_Infos * dev, char *iface)
786 dev->inject_wlanng = 1;
787 dev->rate = 2; /* default to 1Mbps if nothing is set */
790 dev->fd_in = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
793 perror("socket(PF_PACKET) failed at fd_in");
795 fprintf(stderr, "This program requires root privileges.\n");
799 dev->fd_main = socket(PF_PACKET, SOCK_RAW, htons( ETH_P_ALL ) );
800 if (0 > dev->fd_main)
802 perror("socket(PF_PACKET) failed at fd_main");
804 fprintf(stderr, "This program requires root privileges.\n");
808 /* Check iwpriv existence */
810 iwpriv = wiToolsPath("iwpriv");
811 dev->iwpriv = iwpriv;
812 dev->iwconfig = wiToolsPath("iwconfig");
813 dev->ifconfig = wiToolsPath("ifconfig");
817 fprintf(stderr, "Can't find wireless tools, exiting.\n");
821 dev->fd_out = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
824 perror("socket(PF_PACKET) failed at fd_out");
828 /* figure out device type */
830 /* mac80211 radiotap injection
831 * detected based on interface called mon...
832 * since mac80211 allows multiple virtual interfaces
834 * note though that the virtual interfaces are ultimately using a
835 * single physical radio: that means for example they must all
836 * operate on the same channel
839 /* mac80211 stack detection */
840 memset(strbuf, 0, sizeof(strbuf));
841 snprintf(strbuf, sizeof(strbuf) - 1,
842 "ls /sys/class/net/%s/phy80211/subsystem >/dev/null 2>/dev/null", iface);
844 if (system(strbuf) == 0)
845 dev->drivertype = DT_MAC80211_RT;
849 // At the moment only mac80211 tested
854 fprintf(stderr, "Interface %s -> driver: %s\n", iface,
855 szaDriverTypes[dev->drivertype]);
858 if (openraw(dev, iface, dev->fd_out, &dev->arptype_out, dev->pl_mac) != 0)
863 dev->fd_in = dev->fd_out;
865 dev->arptype_in = dev->arptype_out;
868 close_out: close(dev->fd_out);
869 close_in: close(dev->fd_in);
874 stdin_send_hw(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
876 struct Hardware_Infos * dev = cls;
877 struct sendbuf *write_pout = dev->write_pout;
878 struct Radiotap_Send * header = (struct Radiotap_Send *) &hdr[1];
881 unsigned char u8aRadiotap[] =
882 { 0x00, 0x00, // <-- radiotap version
883 0x0c, 0x00, // <- radiotap header length
884 0x04, 0x80, 0x00, 0x00, // <-- bitmap
886 0x00, // <-- padding for natural alignment
887 0x18, 0x00, // <-- TX flags
890 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send)
891 - sizeof(struct GNUNET_MessageHeader);
893 if ((sendsize) > MAXLINE * 2)
895 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n");
899 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
901 fprintf(stderr, "Function stdin_send: wrong packet type\n");
905 u8aRadiotap[8] = header->rate;
907 switch (dev->drivertype)
911 memcpy(write_pout->buf, u8aRadiotap, sizeof(u8aRadiotap));
912 memcpy(write_pout->buf + sizeof(u8aRadiotap), write_pout->buf
913 + sizeof(struct Radiotap_Send) + sizeof(struct GNUNET_MessageHeader),
915 sendsize += sizeof(u8aRadiotap);
923 write_pout->size = sendsize;
927 maketest(unsigned char * buf)
929 unsigned char u8aRadiotap[] =
930 { 0x00, 0x00, // <-- radiotap version
931 0x0c, 0x00, // <- radiotap header length
932 0x04, 0x80, 0x00, 0x00, // <-- bitmap
934 0x00, // <-- padding for natural alignment
935 0x18, 0x00, // <-- TX flags
938 static const uint8_t u8aIeeeHeader[] =
939 { 0x08, 0x01, // Frame Control 0x08= 00001000 -> | b1,2 = 0 -> Version 0;
940 // b3,4 = 10 -> Data; b5-8 = 0 -> Normal Data
941 // 0x01 = 00000001 -> | b1 = 1 to DS; b2 = 0 not from DS;
942 0x00, 0x00, // Duration/ID
943 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // mac1 - in this case receiver
944 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, // mac2 - in this case sender
945 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, // mac3 - in this case bssid
946 0x10, 0x86, //Sequence Control
949 static const char txt[] = "Hallo1Hallo2 Hallo3 Hallo4...998877665544332211";
952 memcpy(buf, u8aRadiotap, sizeof(u8aRadiotap));
953 struct ieee80211_frame * ieee = (struct ieee80211_frame *) u8aIeeeHeader;
954 memcpy(buf + sizeof(u8aRadiotap), ieee, sizeof(u8aIeeeHeader));
955 memcpy(buf + sizeof(u8aRadiotap) + sizeof(u8aIeeeHeader), txt, sizeof(txt));
956 return sizeof(u8aRadiotap) + sizeof(u8aIeeeHeader) + sizeof(txt);
961 hardwaremode(int argc, char *argv[])
964 struct Hardware_Infos dev;
966 struct rx_info * rxinfo;
967 uint8_t * mac = dev.pl_mac;
970 signal(SIGINT, &sigfunc_hw);
971 signal(SIGTERM, &sigfunc_hw);
973 if (wlaninit(&dev, argv[1]))
978 printf("Device %s -> Ethernet %02x:%02x:%02x:%02x:%02x:%02x\n",
979 ifreq.ifr_name, (int) mac[0], (int) mac[1], (int) mac[2], (int) mac[3],
980 (int) mac[4], (int) mac[5]);
984 char readbuf[MAXLINE];
986 struct sendbuf write_std;
990 struct sendbuf write_pout;
994 dev.write_pout = &write_pout;
1004 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
1007 fdpout = dev.fd_out;
1009 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send_hw, &dev);
1013 write_std.size = send_mac_to_plugin((char *) &write_std.buf, mac);
1018 retval = select(0, NULL, NULL, NULL, &tv);
1020 while (0 == closeprog)
1023 write_pout.size = maketest(write_pout.buf);
1026 retval = select(0, NULL, NULL, NULL, &tv);
1035 // if output queue is empty
1036 if (0 == write_pout.size)
1038 FD_SET(STDIN_FILENO, &rfds);
1041 if (0 == write_std.size)
1043 //FD_SET(fdpin, &rfds);
1047 // if there is something to write
1048 if (0 < write_std.size)
1050 FD_SET(STDOUT_FILENO, &wfds);
1051 maxfd = MAX(maxfd, STDOUT_FILENO);
1054 if (0 < write_pout.size)
1056 FD_SET(fdpout, &wfds);
1057 maxfd = MAX(maxfd, fdpout);
1060 retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv);
1062 if (-1 == retval && EINTR == errno)
1068 fprintf(stderr, "select failed: %s\n", strerror(errno));
1072 if (FD_ISSET(STDOUT_FILENO, &wfds))
1074 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos,
1075 write_std.size - write_std.pos);
1080 fprintf(stderr, "Write ERROR to STDOUT\n");
1085 write_std.pos += ret;
1086 // check if finished
1087 if (write_std.pos == write_std.size)
1095 if (FD_ISSET(fdpout, &wfds))
1098 ret = linux_write(&dev, write_pout.buf, write_pout.size);
1099 //ret = write(fdpout, write_pout.buf + write_pout.pos, write_pout.size
1100 // - write_pout.pos);
1105 fprintf(stderr, "Write ERROR to fdpout\n");
1109 write_pout.pos += ret;
1110 // check if finished
1111 if (write_pout.pos != write_pout.size && ret != 0)
1114 fprintf(stderr, "Write ERROR packet not in one piece send: %u, %u\n", write_pout.pos, write_pout.size);
1116 else if (write_pout.pos == write_pout.size)
1119 write_pout.size = 0;
1125 if (FD_ISSET(STDIN_FILENO, &rfds))
1127 readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf));
1132 fprintf(stderr, "Read ERROR to STDIN_FILENO\n");
1134 else if (0 < readsize)
1136 GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize,
1137 GNUNET_NO, GNUNET_NO);
1147 if (FD_ISSET(fdpin, &rfds))
1149 rxinfo = (struct rx_info *) (write_pout.buf
1150 + sizeof(struct GNUNET_MessageHeader));
1151 readsize = linux_read(&dev, (unsigned char *) readbuf
1152 + sizeof(struct rx_info) + sizeof(struct GNUNET_MessageHeader),
1153 sizeof(readbuf) - sizeof(struct rx_info)
1154 - sizeof(struct GNUNET_MessageHeader), rxinfo);
1155 //readsize = read(fdpin, readbuf, sizeof(readbuf));
1160 fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno));
1163 else if (0 < readsize)
1181 main(int argc, char *argv[])
1187 "This program must be started with the interface and the operating mode as argument.\n");
1191 if (strstr(argv[2], "1") || strstr(argv[2], "2"))
1194 return testmode(argc, argv);
1198 hardwaremode(argc, argv);
1202 u8 u8aSendBuffer[500];
1203 char szErrbuf[PCAP_ERRBUF_SIZE];
1204 int nCaptureHeaderLength = 0, n80211HeaderLength = 0, nLinkEncap = 0;
1205 int nOrdinal = 0, r, nDelay = 100000;
1206 int nRateIndex = 0, retval, bytes;
1207 pcap_t *ppcap = NULL;
1208 struct bpf_program bpfprogram;
1209 char * szProgram = "", fBrokenSocket = 0;
1211 char szHostname[PATH_MAX];
1213 if (gethostname(szHostname, sizeof (szHostname) - 1))
1215 perror("unable to get hostname");
1217 szHostname[sizeof (szHostname) - 1] = '\0';
1219 printf("Packetspammer (c)2007 Andy Green <andy@warmcat.com> GPL2\n");
1224 static const struct option optiona[] =
1226 { "delay", required_argument, NULL, 'd'},
1227 { "fcs", no_argument, &flagMarkWithFCS, 1},
1228 { "help", no_argument, &flagHelp, 1},
1229 { "verbose", no_argument, &flagVerbose, 1},
1232 int c = getopt_long(argc, argv, "d:hf",
1233 optiona, &nOptionIndex);
1239 case 0: // long option
1246 nDelay = atoi(optarg);
1249 case 'f': // mark as FCS attached
1250 flagMarkWithFCS = 1;
1253 case 'v': //Verbose / readable output to cout
1258 printf("unknown switch %c\n", c);
1267 // open the interface in pcap
1270 ppcap = pcap_open_live(argv[optind], 800, 1, 20, szErrbuf);
1273 printf("Unable to open interface %s in pcap: %s\n",
1274 argv[optind], szErrbuf);
1278 //get mac from interface
1283 sock=socket(PF_INET, SOCK_STREAM, 0);
1285 perror("can not open socket\n");
1289 if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
1290 perror("ioctl(SIOCGIFHWADDR) ");
1293 for (j=0, k=0; j<6; j++) {
1294 k+=snprintf(mac+k, sizeof(mac)-k-1, j ? ":%02X" : "%02X",
1295 (int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
1297 mac[sizeof(mac)-1]='\0';
1301 nLinkEncap = pcap_datalink(ppcap);
1302 nCaptureHeaderLength = 0;
1307 case DLT_PRISM_HEADER:
1308 printf("DLT_PRISM_HEADER Encap\n");
1309 nCaptureHeaderLength = 0x40;
1310 n80211HeaderLength = 0x20; // ieee80211 comes after this
1311 szProgram = "radio[0x4a:4]==0x13223344";
1314 case DLT_IEEE802_11_RADIO:
1315 printf("DLT_IEEE802_11_RADIO Encap\n");
1316 nCaptureHeaderLength = 0x40;
1317 n80211HeaderLength = 0x18; // ieee80211 comes after this
1318 szProgram = "ether[0x0a:4]==0x13223344";
1322 printf("!!! unknown encapsulation on %s !\n", argv[1]);
1327 if (pcap_compile(ppcap, &bpfprogram, szProgram, 1, 0) == -1)
1330 puts(pcap_geterr(ppcap));
1335 if (pcap_setfilter(ppcap, &bpfprogram) == -1)
1338 puts(pcap_geterr(ppcap));
1342 printf("RX Filter applied\n");
1344 pcap_freecode(&bpfprogram);
1347 pcap_setnonblock(ppcap, 1, szErrbuf);
1349 printf(" (delay between packets %dus)\n", nDelay);
1351 memset(u8aSendBuffer, 0, sizeof(u8aSendBuffer));
1353 while (!fBrokenSocket)
1355 u8 * pu8 = u8aSendBuffer;
1356 struct pcap_pkthdr * ppcapPacketHeader = NULL;
1357 struct ieee80211_radiotap_iterator rti;
1358 PENUMBRA_RADIOTAP_DATA prd;
1359 //init of the values
1361 prd.m_nChannel = 255;
1362 prd.m_nAntenna = 255;
1363 prd.m_nRadiotapFlags = 255;
1364 u8 * pu8Payload = u8aSendBuffer;
1369 retval = pcap_next_ex(ppcap, &ppcapPacketHeader,
1370 (const u_char**) &pu8Payload);
1381 u16HeaderLen = (pu8Payload[2] + (pu8Payload[3] << 8));
1384 Dump(pu8Payload, u16HeaderLen);
1386 if (ppcapPacketHeader->len < (u16HeaderLen + n80211HeaderLength))
1389 bytes = ppcapPacketHeader->len - (u16HeaderLen + n80211HeaderLength);
1393 if (ieee80211_radiotap_iterator_init(&rti,
1394 (struct ieee80211_radiotap_header *) pu8Payload, bytes) < 0)
1397 while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0)
1400 switch (rti.this_arg_index)
1402 case IEEE80211_RADIOTAP_RATE:
1403 prd.m_nRate = (*rti.this_arg);
1406 case IEEE80211_RADIOTAP_CHANNEL:
1407 prd.m_nChannel = le16_to_cpu(*((u16 *)rti.this_arg));
1408 prd.m_nChannelFlags = le16_to_cpu(*((u16 *)(rti.this_arg + 2)));
1411 case IEEE80211_RADIOTAP_ANTENNA:
1412 prd.m_nAntenna = (*rti.this_arg) + 1;
1415 case IEEE80211_RADIOTAP_FLAGS:
1416 prd.m_nRadiotapFlags = *rti.this_arg;
1422 pu8Payload += u16HeaderLen + n80211HeaderLength;
1424 if (prd.m_nRadiotapFlags & IEEE80211_RADIOTAP_F_FCS)
1427 printf("RX: Rate: %2d.%dMbps, Freq: %d.%dGHz, "
1428 "Ant: %d, Flags: 0x%X\n", prd.m_nRate / 2, 5 * (prd.m_nRate & 1),
1429 prd.m_nChannel / 1000, prd.m_nChannel - ((prd.m_nChannel / 1000)
1430 * 1000), prd.m_nAntenna, prd.m_nRadiotapFlags);
1432 Dump(pu8Payload, bytes);
1438 memcpy(u8aSendBuffer, u8aRadiotapHeader, sizeof(u8aRadiotapHeader));
1439 if (flagMarkWithFCS)
1440 pu8[OFFSET_FLAGS] |= IEEE80211_RADIOTAP_F_FCS;
1441 nRate = pu8[OFFSET_RATE] = u8aRatesToUse[nRateIndex++];
1442 if (nRateIndex >= sizeof(u8aRatesToUse))
1444 pu8 += sizeof(u8aRadiotapHeader);
1446 memcpy(pu8, u8aIeeeHeader, sizeof(u8aIeeeHeader));
1447 pu8 += sizeof(u8aIeeeHeader);
1449 pu8 += sprintf((char *) u8aSendBuffer, "Packetspammer %02d"
1451 "#%05d -- :-D --%s ----", nRate / 2, nOrdinal++, szHostname);
1452 r = pcap_inject(ppcap, u8aSendBuffer, pu8 - u8aSendBuffer);
1453 if (r != (pu8 - u8aSendBuffer))
1455 perror("Trouble injecting packet");