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"
90 #define ARPHRD_IEEE80211 801
91 #define ARPHRD_IEEE80211_PRISM 802
92 #define ARPHRD_IEEE80211_FULL 803
97 #include "wlan/helper_common.h"
98 #include "wlan/loopback_helper.h"
119 static const char * szaDriverTypes[] =
120 { [DT_NULL] = "Unknown", [DT_WLANNG] = "Wlan-NG", [DT_HOSTAP] = "HostAP",
121 [DT_MADWIFI] = "Madwifi", [DT_MADWIFING] = "Madwifi-NG",
122 [DT_BCM43XX] = "BCM43xx", [DT_ORINOCO] = "Orinoco",
123 [DT_ZD1211RW] = "ZD1211RW", [DT_ACX] = "ACX",
124 [DT_MAC80211_RT] = "Mac80211-Radiotap", [DT_AT76USB] = "Atmel 76_usb",
125 [DT_IPW2200] = "ipw2200" };
127 struct Hardware_Infos
130 struct sendbuf *write_pout;
131 int fd_in, arptype_in;
132 int fd_out, arptype_out;
136 DRIVER_TYPE drivertype; /* inited to DT_UNKNOWN on allocation by wi_alloc */
140 struct pcap_file_header pfh_in;
147 char *wlanctlng; /* XXX never set */
153 unsigned char pl_mac[6];
157 //#include "radiotap.h"
161 { 0x13, 0x22, 0x33, 0x44, 0x55, 0x66 };
163 /* wifi bitrate to use in 500kHz units */
165 static const u8 u8aRatesToUse[] =
168 54 * 2, 48 * 2, 36 * 2, 24 * 2, 18 * 2, 12 * 2, 9 * 2, 11 * 2, 11, // 5.5
171 #define OFFSET_FLAGS 0x10
172 #define OFFSET_RATE 0x11
174 // this is where we store a summary of the
175 // information from the radiotap header
183 int m_nRadiotapFlags;
184 }__attribute__((packed)) PENUMBRA_RADIOTAP_DATA;
193 Dump(u8 * pu8, int nLength)
195 char sz[256], szBuf[512], szChar[17], *buf, fFirst = 1;
196 unsigned char baaLast[2][16];
197 uint n, nPos = 0, nStart = 0, nLine = 0, nSameCount = 0;
202 for (n = 0; n < nLength; n++)
204 baaLast[(nLine & 1) ^ 1][n & 0xf] = pu8[n];
205 if ((pu8[n] < 32) || (pu8[n] >= 0x7f))
206 szChar[n & 0xf] = '.';
208 szChar[n & 0xf] = pu8[n];
209 szChar[(n & 0xf) + 1] = '\0';
210 nPos += sprintf(&sz[nPos], "%02X ", baaLast[(nLine & 1) ^ 1][n & 0xf]);
213 if ((memcmp(baaLast[0], baaLast[1], 16) == 0) && (!fFirst))
220 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
221 buf += sprintf(buf, "%04x: %s %s\n", nStart, sz, szChar);
234 buf += sprintf(buf, "(repeated %d times)\n", nSameCount);
236 buf += sprintf(buf, "%04x: %s", nStart, sz);
242 buf += sprintf(buf, " ");
246 buf += sprintf(buf, "%s\n", szChar);
253 printf("Usage: wlan-hwd [options] <interface>\n\nOptions\n"
254 "-f/--fcs Mark as having FCS (CRC) already\n"
255 " (pkt ends with 4 x sacrificial - chars)\n"
257 " echo -n mon0 > /sys/class/ieee80211/phy0/add_iface\n"
258 " iwconfig mon0 mode monitor\n"
259 " ifconfig mon0 up\n"
260 " wlan-hwd mon0 Spam down mon0 with\n"
261 " radiotap header first\n"
267 packet_callback(unsigned char *Args, const struct pcap_pkthdr* Pkthdr,
268 unsigned char *Packet)
270 fprintf(stderr, "+");
275 calc_crc_osdep(unsigned char * buf, int len)
277 unsigned long crc = 0xFFFFFFFF;
279 for (; len > 0; len--, buf++)
280 crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8);
285 /* CRC checksum verification routine */
288 check_crc_buf_osdep(unsigned char *buf, int len)
295 crc = calc_crc_osdep(buf, len);
297 return (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] && ((crc
298 >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3]);
301 /* Search a file recursively */
303 searchInside(const char * dir, const char * filename)
318 len = strlen(filename);
319 lentot = strlen(dir) + 256 + 2;
320 curfile = (char *) calloc(1, lentot);
322 while ((ep = readdir(dp)) != NULL)
325 memset(curfile, 0, lentot);
326 sprintf(curfile, "%s/%s", dir, ep->d_name);
328 //Checking if it's the good file
329 if ((int) strlen(ep->d_name) == len && !strcmp(ep->d_name, filename))
336 //If it's a directory and not a link, try to go inside to search
337 if (S_ISDIR(sb.st_mode) && !S_ISLNK(sb.st_mode))
339 //Check if the directory isn't "." or ".."
340 if (strcmp(".", ep->d_name) && strcmp("..", ep->d_name))
343 ret = searchInside(curfile, filename);
358 /* Search a wireless tool and return its path */
360 wiToolsPath(const char * tool)
364 static const char * paths[] =
365 { "/sbin", "/usr/sbin", "/usr/local/sbin", "/bin", "/usr/bin",
366 "/usr/local/bin", "/tmp" };
368 nbelems = sizeof(paths) / sizeof(char *);
370 for (i = 0; i < nbelems; i++)
372 path = searchInside(paths[i], tool);
381 linux_get_channel(struct Hardware_Infos *dev)
387 memset(&wrq, 0, sizeof(struct iwreq));
390 strncpy(wrq.ifr_name, dev->main_if, IFNAMSIZ );
392 strncpy(wrq.ifr_name, dev->iface, IFNAMSIZ );
395 if (dev->drivertype == DT_IPW2200)
398 if (ioctl(fd, SIOCGIWFREQ, &wrq) < 0)
401 frequency = wrq.u.freq.m;
402 if (frequency > 100000000)
404 else if (frequency > 1000000)
407 if (frequency > 1000)
408 chan = getChannelFromFrequency(frequency);
416 linux_read(struct Hardware_Infos * dev, unsigned char *buf, int count,
419 unsigned char tmpbuf[4096];
421 int caplen, n, got_signal, got_noise, got_channel, fcs_removed;
423 caplen = n = got_signal = got_noise = got_channel = fcs_removed = 0;
425 if ((unsigned) count > sizeof(tmpbuf))
427 caplen = read(dev->fd_in, tmpbuf, count);
433 perror("read failed");
437 memset(buf, 0, sizeof(buf));
441 memset(ri, 0, sizeof(*ri));
443 if (dev->arptype_in == ARPHRD_IEEE80211_PRISM)
445 /* skip the prism header */
446 if (tmpbuf[7] == 0x40)
448 /* prism54 uses a different format */
451 ri->ri_power = tmpbuf[0x33];
452 ri->ri_noise = *(unsigned int *) (tmpbuf + 0x33 + 12);
453 ri->ri_rate = (*(unsigned int *) (tmpbuf + 0x33 + 24)) * 500000;
465 ri->ri_mactime = *(u_int64_t*) (tmpbuf + 0x5C - 48);
466 ri->ri_channel = *(unsigned int *) (tmpbuf + 0x5C - 36);
467 ri->ri_power = *(unsigned int *) (tmpbuf + 0x5C);
468 ri->ri_noise = *(unsigned int *) (tmpbuf + 0x5C + 12);
469 ri->ri_rate = (*(unsigned int *) (tmpbuf + 0x5C + 24)) * 500000;
476 n = *(int *) (tmpbuf + 4);
479 if (n < 8 || n >= caplen)
483 if (dev->arptype_in == ARPHRD_IEEE80211_FULL)
485 struct ieee80211_radiotap_iterator iterator;
486 struct ieee80211_radiotap_header *rthdr;
488 rthdr = (struct ieee80211_radiotap_header *) tmpbuf;
490 if (ieee80211_radiotap_iterator_init(&iterator, rthdr, caplen) < 0)
493 /* go through the radiotap arguments we have been given
497 while (ri && (ieee80211_radiotap_iterator_next(&iterator) >= 0))
500 switch (iterator.this_arg_index)
503 case IEEE80211_RADIOTAP_TSFT:
504 ri->ri_mactime = le64_to_cpu(*((uint64_t*) iterator.this_arg));
507 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
510 if (*iterator.this_arg < 127)
511 ri->ri_power = *iterator.this_arg;
513 ri->ri_power = *iterator.this_arg - 255;
519 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
522 if (*iterator.this_arg < 127)
523 ri->ri_power = *iterator.this_arg;
525 ri->ri_power = *iterator.this_arg - 255;
531 case IEEE80211_RADIOTAP_DBM_ANTNOISE:
534 if (*iterator.this_arg < 127)
535 ri->ri_noise = *iterator.this_arg;
537 ri->ri_noise = *iterator.this_arg - 255;
543 case IEEE80211_RADIOTAP_DB_ANTNOISE:
546 if (*iterator.this_arg < 127)
547 ri->ri_noise = *iterator.this_arg;
549 ri->ri_noise = *iterator.this_arg - 255;
555 case IEEE80211_RADIOTAP_ANTENNA:
556 ri->ri_antenna = *iterator.this_arg;
559 case IEEE80211_RADIOTAP_CHANNEL:
560 ri->ri_channel = *iterator.this_arg;
564 case IEEE80211_RADIOTAP_RATE:
565 ri->ri_rate = (*iterator.this_arg) * 500000;
568 case IEEE80211_RADIOTAP_FLAGS:
569 /* is the CRC visible at the end?
572 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS)
578 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_RX_BADFCS)
586 n = le16_to_cpu(rthdr->it_len);
588 if (n <= 0 || n >= caplen)
594 //detect fcs at the end, even if the flag wasn't set and remove it
595 if (fcs_removed == 0 && check_crc_buf_osdep(tmpbuf + n, caplen - 4) == 1)
600 memcpy(buf, tmpbuf + n, caplen);
602 if (ri && !got_channel)
603 ri->ri_channel = linux_get_channel(dev);
609 linux_write(struct Hardware_Infos * dev, unsigned char *buf, unsigned int count)
611 int ret, usedrtap = 0;
612 unsigned short int *p_rtlen;
614 unsigned char * u8aRadiotap = buf;
616 /* Pointer to the radiotap header length field for later use. */
617 p_rtlen = (unsigned short int*) (u8aRadiotap + 2);
619 ret = write(dev->fd_out, buf, count);
623 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || errno
630 perror("write failed");
634 /* radiotap header length is stored little endian on all systems */
636 ret -= letoh16(*p_rtlen);
640 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || errno
647 perror("write failed");
655 openraw(struct Hardware_Infos * dev, char * iface, int fd, int * arptype,
660 struct packet_mreq mr;
661 struct sockaddr_ll sll;
663 /* find the interface index */
665 memset(&ifr, 0, sizeof(ifr));
666 strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
668 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0)
670 printf("Interface %s: \n", iface);
671 perror("ioctl(SIOCGIFINDEX) failed");
675 memset(&sll, 0, sizeof(sll));
676 sll.sll_family = AF_PACKET;
677 sll.sll_ifindex = ifr.ifr_ifindex;
679 switch (dev->drivertype)
682 sll.sll_protocol = htons(ETH_P_ALL);
686 /* lookup the hardware type */
688 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
690 printf("Interface %s: \n", iface);
691 perror("ioctl(SIOCGIFHWADDR) failed");
696 memset(&wrq, 0, sizeof(struct iwreq));
697 strncpy(wrq.ifr_name, iface, IFNAMSIZ);
699 if (ioctl(fd, SIOCGIWMODE, &wrq) < 0)
701 /* most probably not supported (ie for rtap ipw interface) *
702 * so just assume its correctly set... */
703 wrq.u.mode = IW_MODE_MONITOR;
706 if ((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211 && ifr.ifr_hwaddr.sa_family
707 != ARPHRD_IEEE80211_PRISM && ifr.ifr_hwaddr.sa_family
708 != ARPHRD_IEEE80211_FULL) || (wrq.u.mode != IW_MODE_MONITOR))
710 printf("Error: %s not in monitor mode\n", iface);
714 /* Is interface st to up, broadcast & running ? */
715 if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags)
717 /* Bring interface up*/
718 ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING;
720 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
722 perror("ioctl(SIOCSIFFLAGS) failed");
726 /* bind the raw socket to the interface */
728 if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) < 0)
730 printf("Interface %s: \n", iface);
731 perror("bind(ETH_P_ALL) failed");
735 /* lookup the hardware type */
737 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
739 printf("Interface %s: \n", iface);
740 perror("ioctl(SIOCGIFHWADDR) failed");
744 memcpy(mac, (unsigned char*) ifr.ifr_hwaddr.sa_data, 6);
746 *arptype = ifr.ifr_hwaddr.sa_family;
748 if (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211 && ifr.ifr_hwaddr.sa_family
749 != ARPHRD_IEEE80211_PRISM && ifr.ifr_hwaddr.sa_family
750 != ARPHRD_IEEE80211_FULL)
752 if (ifr.ifr_hwaddr.sa_family == 1)
753 fprintf(stderr, "\nARP linktype is set to 1 (Ethernet) ");
755 fprintf(stderr, "\nUnsupported hardware link type %4d ",
756 ifr.ifr_hwaddr.sa_family);
758 fprintf(stderr, "- expected ARPHRD_IEEE80211,\nARPHRD_IEEE80211_"
759 "FULL or ARPHRD_IEEE80211_PRISM instead. Make\n"
760 "sure RFMON is enabled: run 'airmon-ng start %s"
761 " <#>'\nSysfs injection support was not found "
762 "either.\n\n", iface);
766 /* enable promiscuous mode */
768 memset(&mr, 0, sizeof(mr));
769 mr.mr_ifindex = sll.sll_ifindex;
770 mr.mr_type = PACKET_MR_PROMISC;
772 if (setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0)
774 perror("setsockopt(PACKET_MR_PROMISC) failed");
782 wlaninit(struct Hardware_Infos * dev, char *iface)
787 dev->inject_wlanng = 1;
788 dev->rate = 2; /* default to 1Mbps if nothing is set */
791 dev->fd_in = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
794 perror("socket(PF_PACKET) failed at fd_in");
796 fprintf(stderr, "This program requires root privileges.\n");
800 dev->fd_main = socket(PF_PACKET, SOCK_RAW, htons( ETH_P_ALL ) );
801 if (0 > dev->fd_main)
803 perror("socket(PF_PACKET) failed at fd_main");
805 fprintf(stderr, "This program requires root privileges.\n");
809 /* Check iwpriv existence */
811 iwpriv = wiToolsPath("iwpriv");
812 dev->iwpriv = iwpriv;
813 dev->iwconfig = wiToolsPath("iwconfig");
814 dev->ifconfig = wiToolsPath("ifconfig");
818 fprintf(stderr, "Can't find wireless tools, exiting.\n");
822 dev->fd_out = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
825 perror("socket(PF_PACKET) failed at fd_out");
829 /* figure out device type */
831 /* mac80211 radiotap injection
832 * detected based on interface called mon...
833 * since mac80211 allows multiple virtual interfaces
835 * note though that the virtual interfaces are ultimately using a
836 * single physical radio: that means for example they must all
837 * operate on the same channel
840 /* mac80211 stack detection */
841 memset(strbuf, 0, sizeof(strbuf));
842 snprintf(strbuf, sizeof(strbuf) - 1,
843 "ls /sys/class/net/%s/phy80211/subsystem >/dev/null 2>/dev/null", iface);
845 if (system(strbuf) == 0)
846 dev->drivertype = DT_MAC80211_RT;
850 // At the moment only mac80211 tested
855 fprintf(stderr, "Interface %s -> driver: %s\n", iface,
856 szaDriverTypes[dev->drivertype]);
859 if (openraw(dev, iface, dev->fd_out, &dev->arptype_out, dev->pl_mac) != 0)
864 dev->fd_in = dev->fd_out;
866 dev->arptype_in = dev->arptype_out;
869 close_out: close(dev->fd_out);
870 close_in: close(dev->fd_in);
875 stdin_send_hw(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
877 struct Hardware_Infos * dev = cls;
878 struct sendbuf *write_pout = dev->write_pout;
879 struct Radiotap_Send * header = (struct Radiotap_Send *) &hdr[1];
882 unsigned char u8aRadiotap[] =
883 { 0x00, 0x00, // <-- radiotap version
884 0x0c, 0x00, // <- radiotap header length
885 0x04, 0x80, 0x00, 0x00, // <-- bitmap
887 0x00, // <-- padding for natural alignment
888 0x18, 0x00, // <-- TX flags
891 sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send)
892 - sizeof(struct GNUNET_MessageHeader);
894 if ((sendsize) > MAXLINE * 2)
896 fprintf(stderr, "Function stdin_send: Packet too big for buffer\n");
900 if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type))
902 fprintf(stderr, "Function stdin_send: wrong packet type\n");
906 u8aRadiotap[8] = header->rate;
908 switch (dev->drivertype)
912 memcpy(write_pout->buf, u8aRadiotap, sizeof(u8aRadiotap));
913 memcpy(write_pout->buf + sizeof(u8aRadiotap), write_pout->buf
914 + sizeof(struct Radiotap_Send) + sizeof(struct GNUNET_MessageHeader),
916 sendsize += sizeof(u8aRadiotap);
924 write_pout->size = sendsize;
928 maketest(unsigned char * buf)
930 unsigned char u8aRadiotap[] =
931 { 0x00, 0x00, // <-- radiotap version
932 0x0c, 0x00, // <- radiotap header length
933 0x04, 0x80, 0x00, 0x00, // <-- bitmap
935 0x00, // <-- padding for natural alignment
936 0x18, 0x00, // <-- TX flags
939 static const uint8_t u8aIeeeHeader[] =
940 { 0x08, 0x01, // Frame Control 0x08= 00001000 -> | b1,2 = 0 -> Version 0;
941 // b3,4 = 10 -> Data; b5-8 = 0 -> Normal Data
942 // 0x01 = 00000001 -> | b1 = 1 to DS; b2 = 0 not from DS;
943 0x00, 0x00, // Duration/ID
944 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // mac1 - in this case receiver
945 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, // mac2 - in this case sender
946 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, // mac3 - in this case bssid
947 0x10, 0x86, //Sequence Control
950 static const char txt[] = "Hallo1Hallo2 Hallo3 Hallo4...998877665544332211";
953 memcpy(buf, u8aRadiotap, sizeof(u8aRadiotap));
954 struct ieee80211_frame * ieee = (struct ieee80211_frame *) u8aIeeeHeader;
955 memcpy(buf + sizeof(u8aRadiotap), ieee, sizeof(u8aIeeeHeader));
956 memcpy(buf + sizeof(u8aRadiotap) + sizeof(u8aIeeeHeader), txt, sizeof(txt));
957 return sizeof(u8aRadiotap) + sizeof(u8aIeeeHeader) + sizeof(txt);
962 hardwaremode(int argc, char *argv[])
965 struct Hardware_Infos dev;
967 struct rx_info * rxinfo;
968 uint8_t * mac = dev.pl_mac;
971 signal(SIGINT, &sigfunc_hw);
972 signal(SIGTERM, &sigfunc_hw);
974 if (wlaninit(&dev, argv[1]))
979 printf("Device %s -> Ethernet %02x:%02x:%02x:%02x:%02x:%02x\n",
980 ifreq.ifr_name, (int) mac[0], (int) mac[1], (int) mac[2], (int) mac[3],
981 (int) mac[4], (int) mac[5]);
985 char readbuf[MAXLINE];
987 struct sendbuf write_std;
991 struct sendbuf write_pout;
995 dev.write_pout = &write_pout;
1005 struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst;
1008 fdpout = dev.fd_out;
1010 stdin_mst = GNUNET_SERVER_mst_create(&stdin_send_hw, &dev);
1014 write_std.size = send_mac_to_plugin((char *) &write_std.buf, mac);
1019 retval = select(0, NULL, NULL, NULL, &tv);
1021 while (0 == closeprog)
1024 write_pout.size = maketest(write_pout.buf);
1027 retval = select(0, NULL, NULL, NULL, &tv);
1036 // if output queue is empty
1037 if (0 == write_pout.size)
1039 FD_SET(STDIN_FILENO, &rfds);
1042 if (0 == write_std.size)
1044 //FD_SET(fdpin, &rfds);
1048 // if there is something to write
1049 if (0 < write_std.size)
1051 FD_SET(STDOUT_FILENO, &wfds);
1052 maxfd = MAX(maxfd, STDOUT_FILENO);
1055 if (0 < write_pout.size)
1057 FD_SET(fdpout, &wfds);
1058 maxfd = MAX(maxfd, fdpout);
1061 retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv);
1063 if (-1 == retval && EINTR == errno)
1069 fprintf(stderr, "select failed: %s\n", strerror(errno));
1073 if (FD_ISSET(STDOUT_FILENO, &wfds))
1075 ret = write(STDOUT_FILENO, write_std.buf + write_std.pos,
1076 write_std.size - write_std.pos);
1081 fprintf(stderr, "Write ERROR to STDOUT\n");
1086 write_std.pos += ret;
1087 // check if finished
1088 if (write_std.pos == write_std.size)
1096 if (FD_ISSET(fdpout, &wfds))
1099 ret = linux_write(&dev, write_pout.buf, write_pout.size);
1100 //ret = write(fdpout, write_pout.buf + write_pout.pos, write_pout.size
1101 // - write_pout.pos);
1106 fprintf(stderr, "Write ERROR to fdpout\n");
1110 write_pout.pos += ret;
1111 // check if finished
1112 if (write_pout.pos != write_pout.size && ret != 0)
1115 fprintf(stderr, "Write ERROR packet not in one piece send: %u, %u\n", write_pout.pos, write_pout.size);
1117 else if (write_pout.pos == write_pout.size)
1120 write_pout.size = 0;
1126 if (FD_ISSET(STDIN_FILENO, &rfds))
1128 readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf));
1133 fprintf(stderr, "Read ERROR to STDIN_FILENO\n");
1135 else if (0 < readsize)
1137 GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize,
1138 GNUNET_NO, GNUNET_NO);
1148 if (FD_ISSET(fdpin, &rfds))
1150 rxinfo = (struct rx_info *) (write_pout.buf
1151 + sizeof(struct GNUNET_MessageHeader));
1152 readsize = linux_read(&dev, (unsigned char *) readbuf
1153 + sizeof(struct rx_info) + sizeof(struct GNUNET_MessageHeader),
1154 sizeof(readbuf) - sizeof(struct rx_info)
1155 - sizeof(struct GNUNET_MessageHeader), rxinfo);
1156 //readsize = read(fdpin, readbuf, sizeof(readbuf));
1161 fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno));
1164 else if (0 < readsize)
1182 main(int argc, char *argv[])
1188 "This program must be started with the interface and the operating mode as argument.\n");
1192 if (strstr(argv[2], "1") || strstr(argv[2], "2"))
1195 return testmode(argc, argv);
1199 hardwaremode(argc, argv);
1203 u8 u8aSendBuffer[500];
1204 char szErrbuf[PCAP_ERRBUF_SIZE];
1205 int nCaptureHeaderLength = 0, n80211HeaderLength = 0, nLinkEncap = 0;
1206 int nOrdinal = 0, r, nDelay = 100000;
1207 int nRateIndex = 0, retval, bytes;
1208 pcap_t *ppcap = NULL;
1209 struct bpf_program bpfprogram;
1210 char * szProgram = "", fBrokenSocket = 0;
1212 char szHostname[PATH_MAX];
1214 if (gethostname(szHostname, sizeof (szHostname) - 1))
1216 perror("unable to get hostname");
1218 szHostname[sizeof (szHostname) - 1] = '\0';
1220 printf("Packetspammer (c)2007 Andy Green <andy@warmcat.com> GPL2\n");
1225 static const struct option optiona[] =
1227 { "delay", required_argument, NULL, 'd'},
1228 { "fcs", no_argument, &flagMarkWithFCS, 1},
1229 { "help", no_argument, &flagHelp, 1},
1230 { "verbose", no_argument, &flagVerbose, 1},
1233 int c = getopt_long(argc, argv, "d:hf",
1234 optiona, &nOptionIndex);
1240 case 0: // long option
1247 nDelay = atoi(optarg);
1250 case 'f': // mark as FCS attached
1251 flagMarkWithFCS = 1;
1254 case 'v': //Verbose / readable output to cout
1259 printf("unknown switch %c\n", c);
1268 // open the interface in pcap
1271 ppcap = pcap_open_live(argv[optind], 800, 1, 20, szErrbuf);
1274 printf("Unable to open interface %s in pcap: %s\n",
1275 argv[optind], szErrbuf);
1279 //get mac from interface
1284 sock=socket(PF_INET, SOCK_STREAM, 0);
1286 perror("can not open socket\n");
1290 if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
1291 perror("ioctl(SIOCGIFHWADDR) ");
1294 for (j=0, k=0; j<6; j++) {
1295 k+=snprintf(mac+k, sizeof(mac)-k-1, j ? ":%02X" : "%02X",
1296 (int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
1298 mac[sizeof(mac)-1]='\0';
1302 nLinkEncap = pcap_datalink(ppcap);
1303 nCaptureHeaderLength = 0;
1308 case DLT_PRISM_HEADER:
1309 printf("DLT_PRISM_HEADER Encap\n");
1310 nCaptureHeaderLength = 0x40;
1311 n80211HeaderLength = 0x20; // ieee80211 comes after this
1312 szProgram = "radio[0x4a:4]==0x13223344";
1315 case DLT_IEEE802_11_RADIO:
1316 printf("DLT_IEEE802_11_RADIO Encap\n");
1317 nCaptureHeaderLength = 0x40;
1318 n80211HeaderLength = 0x18; // ieee80211 comes after this
1319 szProgram = "ether[0x0a:4]==0x13223344";
1323 printf("!!! unknown encapsulation on %s !\n", argv[1]);
1328 if (pcap_compile(ppcap, &bpfprogram, szProgram, 1, 0) == -1)
1331 puts(pcap_geterr(ppcap));
1336 if (pcap_setfilter(ppcap, &bpfprogram) == -1)
1339 puts(pcap_geterr(ppcap));
1343 printf("RX Filter applied\n");
1345 pcap_freecode(&bpfprogram);
1348 pcap_setnonblock(ppcap, 1, szErrbuf);
1350 printf(" (delay between packets %dus)\n", nDelay);
1352 memset(u8aSendBuffer, 0, sizeof(u8aSendBuffer));
1354 while (!fBrokenSocket)
1356 u8 * pu8 = u8aSendBuffer;
1357 struct pcap_pkthdr * ppcapPacketHeader = NULL;
1358 struct ieee80211_radiotap_iterator rti;
1359 PENUMBRA_RADIOTAP_DATA prd;
1360 //init of the values
1362 prd.m_nChannel = 255;
1363 prd.m_nAntenna = 255;
1364 prd.m_nRadiotapFlags = 255;
1365 u8 * pu8Payload = u8aSendBuffer;
1370 retval = pcap_next_ex(ppcap, &ppcapPacketHeader,
1371 (const u_char**) &pu8Payload);
1382 u16HeaderLen = (pu8Payload[2] + (pu8Payload[3] << 8));
1385 Dump(pu8Payload, u16HeaderLen);
1387 if (ppcapPacketHeader->len < (u16HeaderLen + n80211HeaderLength))
1390 bytes = ppcapPacketHeader->len - (u16HeaderLen + n80211HeaderLength);
1394 if (ieee80211_radiotap_iterator_init(&rti,
1395 (struct ieee80211_radiotap_header *) pu8Payload, bytes) < 0)
1398 while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0)
1401 switch (rti.this_arg_index)
1403 case IEEE80211_RADIOTAP_RATE:
1404 prd.m_nRate = (*rti.this_arg);
1407 case IEEE80211_RADIOTAP_CHANNEL:
1408 prd.m_nChannel = le16_to_cpu(*((u16 *)rti.this_arg));
1409 prd.m_nChannelFlags = le16_to_cpu(*((u16 *)(rti.this_arg + 2)));
1412 case IEEE80211_RADIOTAP_ANTENNA:
1413 prd.m_nAntenna = (*rti.this_arg) + 1;
1416 case IEEE80211_RADIOTAP_FLAGS:
1417 prd.m_nRadiotapFlags = *rti.this_arg;
1423 pu8Payload += u16HeaderLen + n80211HeaderLength;
1425 if (prd.m_nRadiotapFlags & IEEE80211_RADIOTAP_F_FCS)
1428 printf("RX: Rate: %2d.%dMbps, Freq: %d.%dGHz, "
1429 "Ant: %d, Flags: 0x%X\n", prd.m_nRate / 2, 5 * (prd.m_nRate & 1),
1430 prd.m_nChannel / 1000, prd.m_nChannel - ((prd.m_nChannel / 1000)
1431 * 1000), prd.m_nAntenna, prd.m_nRadiotapFlags);
1433 Dump(pu8Payload, bytes);
1439 memcpy(u8aSendBuffer, u8aRadiotapHeader, sizeof(u8aRadiotapHeader));
1440 if (flagMarkWithFCS)
1441 pu8[OFFSET_FLAGS] |= IEEE80211_RADIOTAP_F_FCS;
1442 nRate = pu8[OFFSET_RATE] = u8aRatesToUse[nRateIndex++];
1443 if (nRateIndex >= sizeof(u8aRatesToUse))
1445 pu8 += sizeof(u8aRadiotapHeader);
1447 memcpy(pu8, u8aIeeeHeader, sizeof(u8aIeeeHeader));
1448 pu8 += sizeof(u8aIeeeHeader);
1450 pu8 += sprintf((char *) u8aSendBuffer, "Packetspammer %02d"
1452 "#%05d -- :-D --%s ----", nRate / 2, nOrdinal++, szHostname);
1453 r = pcap_inject(ppcap, u8aSendBuffer, pu8 - u8aSendBuffer);
1454 if (r != (pu8 - u8aSendBuffer))
1456 perror("Trouble injecting packet");