fix
[oweals/gnunet.git] / src / transport / gnunet-transport-wlan-helper.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file transport/gnunet-wlan.h
23  * @brief common internal definitions for the wlan mediator
24  * @author David Brodski
25  */
26  
27 #ifndef gnunet_wlan_H
28 #define gnunet_wlan_H
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <resolv.h>
34 #include <string.h>
35 #include <utime.h>
36 #include <unistd.h>
37 #include <getopt.h>
38 #include <pcap.h>
39 #include <endian.h>
40 #include <pcap.h>
41
42
43 typedef unsigned int u32;
44 typedef unsigned short u16;
45 typedef unsigned char u8;
46 typedef u32 __le32;
47
48 #if __BYTE_ORDER == __LITTLE_ENDIAN
49 #define le16_to_cpu(x) (x)
50 #define le32_to_cpu(x) (x)
51 #else
52 #define le16_to_cpu(x) ((((x)&0xff)<<8)|(((x)&0xff00)>>8))
53 #define le32_to_cpu(x) \
54 ((((x)&0xff)<<24)|(((x)&0xff00)<<8)|(((x)&0xff0000)>>8)|(((x)&0xff000000)>>24))
55 #endif
56 #define unlikely(x) (x)
57
58
59 #endif /* gnunet_wlan_H */
60
61
62 /*
63  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
64  *
65  * Redistribution and use in source and binary forms, with or without
66  * modification, are permitted provided that the following conditions
67  * are met:
68  * 1. Redistributions of source code must retain the above copyright
69  *    notice, this list of conditions and the following disclaimer.
70  * 2. Redistributions in binary form must reproduce the above copyright
71  *    notice, this list of conditions and the following disclaimer in the
72  *    documentation and/or other materials provided with the distribution.
73  * 3. The name of David Young may not be used to endorse or promote
74  *    products derived from this software without specific prior
75  *    written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
78  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
79  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
80  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DAVID
81  * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
82  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
83  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
85  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
86  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
88  * OF SUCH DAMAGE.
89  */
90
91 /*
92  * Modifications to fit into the linux IEEE 802.11 stack,
93  * Mike Kershaw (dragorn@kismetwireless.net)
94  */
95
96 #ifndef IEEE80211RADIOTAP_H
97 #define IEEE80211RADIOTAP_H
98
99 #include <linux/if_ether.h>
100 #include <linux/kernel.h>
101 //#include <asm/unaligned.h>
102
103 /* Base version of the radiotap packet header data */
104 #define PKTHDR_RADIOTAP_VERSION         0
105
106 /* A generic radio capture format is desirable. There is one for
107  * Linux, but it is neither rigidly defined (there were not even
108  * units given for some fields) nor easily extensible.
109  *
110  * I suggest the following extensible radio capture format. It is
111  * based on a bitmap indicating which fields are present.
112  *
113  * I am trying to describe precisely what the application programmer
114  * should expect in the following, and for that reason I tell the
115  * units and origin of each measurement (where it applies), or else I
116  * use sufficiently weaselly language ("is a monotonically nondecreasing
117  * function of...") that I cannot set false expectations for lawyerly
118  * readers.
119  */
120
121 /*
122  * The radio capture header precedes the 802.11 header.
123  * All data in the header is little endian on all platforms.
124  */
125 struct ieee80211_radiotap_header {
126         u8 it_version;          /* Version 0. Only increases
127                                  * for drastic changes,
128                                  * introduction of compatible
129                                  * new fields does not count.
130                                  */
131         u8 it_pad;
132         __le16 it_len;          /* length of the whole
133                                  * header in bytes, including
134                                  * it_version, it_pad,
135                                  * it_len, and data fields.
136                                  */
137         __le32 it_present;      /* A bitmap telling which
138                                  * fields are present. Set bit 31
139                                  * (0x80000000) to extend the
140                                  * bitmap by another 32 bits.
141                                  * Additional extensions are made
142                                  * by setting bit 31.
143                                  */
144 } __packed;
145
146 /* Name                                 Data type    Units
147  * ----                                 ---------    -----
148  *
149  * IEEE80211_RADIOTAP_TSFT              __le64       microseconds
150  *
151  *      Value in microseconds of the MAC's 64-bit 802.11 Time
152  *      Synchronization Function timer when the first bit of the
153  *      MPDU arrived at the MAC. For received frames, only.
154  *
155  * IEEE80211_RADIOTAP_CHANNEL           2 x __le16   MHz, bitmap
156  *
157  *      Tx/Rx frequency in MHz, followed by flags (see below).
158  *
159  * IEEE80211_RADIOTAP_FHSS              __le16       see below
160  *
161  *      For frequency-hopping radios, the hop set (first byte)
162  *      and pattern (second byte).
163  *
164  * IEEE80211_RADIOTAP_RATE              u8           500kb/s
165  *
166  *      Tx/Rx data rate
167  *
168  * IEEE80211_RADIOTAP_DBM_ANTSIGNAL     s8           decibels from
169  *                                                   one milliwatt (dBm)
170  *
171  *      RF signal power at the antenna, decibel difference from
172  *      one milliwatt.
173  *
174  * IEEE80211_RADIOTAP_DBM_ANTNOISE      s8           decibels from
175  *                                                   one milliwatt (dBm)
176  *
177  *      RF noise power at the antenna, decibel difference from one
178  *      milliwatt.
179  *
180  * IEEE80211_RADIOTAP_DB_ANTSIGNAL      u8           decibel (dB)
181  *
182  *      RF signal power at the antenna, decibel difference from an
183  *      arbitrary, fixed reference.
184  *
185  * IEEE80211_RADIOTAP_DB_ANTNOISE       u8           decibel (dB)
186  *
187  *      RF noise power at the antenna, decibel difference from an
188  *      arbitrary, fixed reference point.
189  *
190  * IEEE80211_RADIOTAP_LOCK_QUALITY      __le16       unitless
191  *
192  *      Quality of Barker code lock. Unitless. Monotonically
193  *      nondecreasing with "better" lock strength. Called "Signal
194  *      Quality" in datasheets.  (Is there a standard way to measure
195  *      this?)
196  *
197  * IEEE80211_RADIOTAP_TX_ATTENUATION    __le16       unitless
198  *
199  *      Transmit power expressed as unitless distance from max
200  *      power set at factory calibration.  0 is max power.
201  *      Monotonically nondecreasing with lower power levels.
202  *
203  * IEEE80211_RADIOTAP_DB_TX_ATTENUATION __le16       decibels (dB)
204  *
205  *      Transmit power expressed as decibel distance from max power
206  *      set at factory calibration.  0 is max power.  Monotonically
207  *      nondecreasing with lower power levels.
208  *
209  * IEEE80211_RADIOTAP_DBM_TX_POWER      s8           decibels from
210  *                                                   one milliwatt (dBm)
211  *
212  *      Transmit power expressed as dBm (decibels from a 1 milliwatt
213  *      reference). This is the absolute power level measured at
214  *      the antenna port.
215  *
216  * IEEE80211_RADIOTAP_FLAGS             u8           bitmap
217  *
218  *      Properties of transmitted and received frames. See flags
219  *      defined below.
220  *
221  * IEEE80211_RADIOTAP_ANTENNA           u8           antenna index
222  *
223  *      Unitless indication of the Rx/Tx antenna for this packet.
224  *      The first antenna is antenna 0.
225  *
226  * IEEE80211_RADIOTAP_RX_FLAGS          __le16       bitmap
227  *
228  *     Properties of received frames. See flags defined below.
229  *
230  * IEEE80211_RADIOTAP_TX_FLAGS          __le16       bitmap
231  *
232  *     Properties of transmitted frames. See flags defined below.
233  *
234  * IEEE80211_RADIOTAP_RTS_RETRIES       u8           data
235  *
236  *     Number of rts retries a transmitted frame used.
237  *
238  * IEEE80211_RADIOTAP_DATA_RETRIES      u8           data
239  *
240  *     Number of unicast retries a transmitted frame used.
241  *
242  */
243 enum ieee80211_radiotap_type {
244         IEEE80211_RADIOTAP_TSFT = 0,
245         IEEE80211_RADIOTAP_FLAGS = 1,
246         IEEE80211_RADIOTAP_RATE = 2,
247         IEEE80211_RADIOTAP_CHANNEL = 3,
248         IEEE80211_RADIOTAP_FHSS = 4,
249         IEEE80211_RADIOTAP_DBM_ANTSIGNAL = 5,
250         IEEE80211_RADIOTAP_DBM_ANTNOISE = 6,
251         IEEE80211_RADIOTAP_LOCK_QUALITY = 7,
252         IEEE80211_RADIOTAP_TX_ATTENUATION = 8,
253         IEEE80211_RADIOTAP_DB_TX_ATTENUATION = 9,
254         IEEE80211_RADIOTAP_DBM_TX_POWER = 10,
255         IEEE80211_RADIOTAP_ANTENNA = 11,
256         IEEE80211_RADIOTAP_DB_ANTSIGNAL = 12,
257         IEEE80211_RADIOTAP_DB_ANTNOISE = 13,
258         IEEE80211_RADIOTAP_RX_FLAGS = 14,
259         IEEE80211_RADIOTAP_TX_FLAGS = 15,
260         IEEE80211_RADIOTAP_RTS_RETRIES = 16,
261         IEEE80211_RADIOTAP_DATA_RETRIES = 17,
262
263         /* valid in every it_present bitmap, even vendor namespaces */
264         IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE = 29,
265         IEEE80211_RADIOTAP_VENDOR_NAMESPACE = 30,
266         IEEE80211_RADIOTAP_EXT = 31
267 };
268
269 /* Channel flags. */
270 #define IEEE80211_CHAN_TURBO    0x0010  /* Turbo channel */
271 #define IEEE80211_CHAN_CCK      0x0020  /* CCK channel */
272 #define IEEE80211_CHAN_OFDM     0x0040  /* OFDM channel */
273 #define IEEE80211_CHAN_2GHZ     0x0080  /* 2 GHz spectrum channel. */
274 #define IEEE80211_CHAN_5GHZ     0x0100  /* 5 GHz spectrum channel */
275 #define IEEE80211_CHAN_PASSIVE  0x0200  /* Only passive scan allowed */
276 #define IEEE80211_CHAN_DYN      0x0400  /* Dynamic CCK-OFDM channel */
277 #define IEEE80211_CHAN_GFSK     0x0800  /* GFSK channel (FHSS PHY) */
278
279 /* For IEEE80211_RADIOTAP_FLAGS */
280 #define IEEE80211_RADIOTAP_F_CFP        0x01    /* sent/received
281                                                  * during CFP
282                                                  */
283 #define IEEE80211_RADIOTAP_F_SHORTPRE   0x02    /* sent/received
284                                                  * with short
285                                                  * preamble
286                                                  */
287 #define IEEE80211_RADIOTAP_F_WEP        0x04    /* sent/received
288                                                  * with WEP encryption
289                                                  */
290 #define IEEE80211_RADIOTAP_F_FRAG       0x08    /* sent/received
291                                                  * with fragmentation
292                                                  */
293 #define IEEE80211_RADIOTAP_F_FCS        0x10    /* frame includes FCS */
294 #define IEEE80211_RADIOTAP_F_DATAPAD    0x20    /* frame has padding between
295                                                  * 802.11 header and payload
296                                                  * (to 32-bit boundary)
297                                                  */
298 #define IEEE80211_RADIOTAP_F_BADFCS     0x40    /* bad FCS */
299
300 /* For IEEE80211_RADIOTAP_RX_FLAGS */
301 #define IEEE80211_RADIOTAP_F_RX_BADPLCP 0x0002  /* frame has bad PLCP */
302
303 /* For IEEE80211_RADIOTAP_TX_FLAGS */
304 #define IEEE80211_RADIOTAP_F_TX_FAIL    0x0001  /* failed due to excessive
305                                                  * retries */
306 #define IEEE80211_RADIOTAP_F_TX_CTS     0x0002  /* used cts 'protection' */
307 #define IEEE80211_RADIOTAP_F_TX_RTS     0x0004  /* used rts/cts handshake */
308
309 /* Ugly macro to convert literal channel numbers into their mhz equivalents
310  * There are certianly some conditions that will break this (like feeding it '30')
311  * but they shouldn't arise since nothing talks on channel 30. */
312 #define ieee80211chan2mhz(x) \
313         (((x) <= 14) ? \
314         (((x) == 14) ? 2484 : ((x) * 5) + 2407) : \
315         ((x) + 1000) * 5)
316
317 #if BROKEN
318 /* helpers */
319 static inline int ieee80211_get_radiotap_len(unsigned char *data)
320 {
321         struct ieee80211_radiotap_header *hdr =
322                 (struct ieee80211_radiotap_header *)data;
323
324         return get_unaligned_le16(&hdr->it_len);
325 }
326 #endif
327 #endif                          /* IEEE80211_RADIOTAP_H */
328
329
330
331 /* Radiotap header iteration
332  *   implemented in net/wireless/radiotap.c
333  *   docs in Documentation/networking/radiotap-headers.txt
334  */
335 /**
336  * struct ieee80211_radiotap_iterator - tracks walk thru present radiotap args
337  * @rtheader: pointer to the radiotap header we are walking through
338  * @max_length: length of radiotap header in cpu byte ordering
339  * @this_arg_index: IEEE80211_RADIOTAP_... index of current arg
340  * @this_arg: pointer to current radiotap arg
341  * @arg_index: internal next argument index
342  * @arg: internal next argument pointer
343  * @next_bitmap: internal pointer to next present u32
344  * @bitmap_shifter: internal shifter for curr u32 bitmap, 23344";
345 b0 set == arg present
346  */
347
348 struct ieee80211_radiotap_iterator {
349         struct ieee80211_radiotap_header *rtheader;
350         int max_length;
351         int this_arg_index;
352         u8 *this_arg;
353
354         int arg_index;
355         u8 *arg;
356         __le32 *next_bitmap;
357         u32 bitmap_shifter;
358 };
359
360 extern int ieee80211_radiotap_iterator_init(
361    struct ieee80211_radiotap_iterator *iterator,
362    struct ieee80211_radiotap_header *radiotap_header,
363    int max_length);
364
365 extern int ieee80211_radiotap_iterator_next(
366    struct ieee80211_radiotap_iterator *iterator);