RE: Converting raw 802.11 (rfmon) capture file to standard libpcap

From: Chris Eagle (cseagle@redshift.com)
Date: Wed Jan 14 2004 - 00:13:01 EST


Jerry Shenk wrote:
>
> That is exactly what I want...

Ok I cobbled this up. It's rough but it seems to work for me:

# gcc -o wifi2eth wifi2eth.c -lpcap
# ./wifi2eth raw.dat eth.dat

--------------------------------------------------------------
/*
  File: wifi2eth.c

  Copyright (c) 2004 Chris Eagle <cseagle at redshift d0t c0m>

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
"Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS
  IN THE SOFTWARE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pcap.h>

#define FC80211_DATA 0x08
#define FC80211_TYPE_MASK 0x0C

#define PRISM_OFFSET 144

struct p80211_hdr_data {
   unsigned short frame_control __attribute__ ((packed));
   unsigned short duration_id __attribute__ ((packed));
   unsigned char addr1[6] __attribute__ ((packed));
   unsigned char addr2[6] __attribute__ ((packed));
   unsigned char addr3[6] __attribute__ ((packed));
   unsigned short seq_ctrl __attribute__ ((packed));
   unsigned char addr4[6] __attribute__ ((packed));
};

struct p8022_hdr {
   unsigned int snap;
   unsigned short dummy;
   unsigned short ethertype;
   unsigned char data[0];
};

#define TCPDUMP_MAGIC 0xa1b2c3d4

int PKT_OFFSET = PRISM_OFFSET;

void usage(void) {
   fprintf(stderr, "Version: 0.01\nUsage: wifi2eth inputfile outputfile\n");
   exit(1);
}

void *destMac(struct p80211_hdr_data *wh) {
   if (wh->frame_control & 0x0100) {
      return wh->addr3;
   }
   return wh->addr1;
}

void *sourceMac(struct p80211_hdr_data *wh) {
   switch (wh->frame_control & 0x0300) {
      case 0x0000: //AD_HOC
      case 0x0100: //TO_DS
         return wh->addr2;
      case 0x0200: //FROM_DS
         return wh->addr3;
      case 0x0300:
         return wh->addr4;
   }
}

int hdrLen(struct p80211_hdr_data *wh) {
   return ((wh->frame_control & 0x0300) == 0x0300) ? 30 : 24;
}

int usesWep(struct p8022_hdr *eh) {
   return eh->snap != 0x0003AAAA; //only works for little endian!!
}

int isData(struct p80211_hdr_data *wh) {
   return (wh->frame_control & FC80211_TYPE_MASK) == FC80211_DATA;
}

void packetCallback(u_char *user, struct pcap_pkthdr *pph, u_char *pdata){
   struct p80211_hdr_data *wh;
   struct p8022_hdr *eh;
   int start802, outfile;
   outfile = (int) user;
   //the 24 below is a minimum data frame header
   if (pph->len < (PKT_OFFSET + 24)) return; //simple safety check
   wh = (struct p80211_hdr_data*) (pdata + PKT_OFFSET);
   start802 = PKT_OFFSET + hdrLen(wh);
   //if we don't have an 802.2 header then we have no useful data
   if (pph->len < (start802 + sizeof(struct p8022_hdr))) return;
   eh = (struct p8022_hdr*)(pdata + start802);
   switch (wh->frame_control & FC80211_TYPE_MASK) {
      case FC80211_DATA:
         if (!usesWep(eh)) {
            //the 8 here is for the SNAP/OID bytes. The result should be
the
            //length of the IP packet
            int datalen = pph->len - start802 - 8;
            pph->len = datalen + 14;
            pph->caplen = pph->len;
            write(outfile, pph, sizeof(struct pcap_pkthdr));
            write(outfile, destMac(wh), 6);
            write(outfile, sourceMac(wh), 6);
            write(outfile, &eh->ethertype, 2);
            write(outfile, &eh->data, datalen);
         }
         break;
   }
}

int open_dump(char *fname) {
   int fd = -1;
   struct pcap_file_header pfh = {TCPDUMP_MAGIC, PCAP_VERSION_MAJOR,
                                  PCAP_VERSION_MINOR, 8, 0, 1514,
                                  DLT_EN10MB };
   fd = open(fname, O_WRONLY | O_CREAT, 0644);
   write(fd, &pfh, sizeof(pfh));
   return fd;
}

int main (int argc, char * argv[]) {
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t* descr;
    char *dev;
    int outfile, dl;
    if (argc != 3) usage();
    descr = pcap_open_offline(argv[1], errbuf);
    dl = pcap_datalink(descr);
    if (dl == DLT_IEEE802_11) {
       PKT_OFFSET = 0;
    }
    else if (dl != DLT_PRISM_HEADER) {
       pcap_close(descr);
       fprintf(stderr, "%s does not appear to be an 802.11 capture file\n",
               argv[1]);
       exit(1);
    }
    outfile = open_dump(argv[2]);

    pcap_dispatch(descr, -1, (pcap_handler)packetCallback, (u_char
*)outfile);
    pcap_close(descr);
    close(outfile);

    return 0;
}

---------------------------------------------------------------------------
----------------------------------------------------------------------------



This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 10:53:45 EDT