1 /*
2  * Copyright (c) 2007, 2008, Andrea Bittau <a.bittau@cs.ucl.ac.uk>
3  * All OS dependent crap should go here.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
12  * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
13  * NON-INFRINGEMENT.  See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18  * MA 02110-1301, USA.
19  *
20  * In addition, as a special exception, the copyright holders give
21  * permission to link the code of portions of this program with the
22  * OpenSSL library under certain conditions as described in each
23  * individual source file, and distribute linked combinations
24  * including the two.
25  * You must obey the GNU General Public License in all respects
26  * for all of the code used other than OpenSSL.  If you modify
27  * file(s) with this exception, you may extend this exception to your
28  * version of the file(s), but you are not obligated to do so.  If you
29  * do not wish to do so, delete this exception statement from your
30  * version.  If you delete this exception statement from all source
31  * files in the program, then also delete it here.
32  */
33 
34 #ifndef __AIRCRACK_NG_OSEDEP_H__
35 #define __AIRCRACK_NG_OSEDEP_H__
36 
37 #include <netinet/in.h>
38 #include <stdint.h>
39 
40 #include "byteorder.h"
41 #include "packed.h"
42 
43 #if defined(__APPLE_CC__) && defined(_XCODE)
44 #include <pcap/bpf.h>
45 #undef LINKTYPE_RADIOTAP_HDR
46 #define LINKTYPE_RADIOTAP_HDR DLT_IEEE802_11_RADIO
47 #undef LINKTYPE_IEEE802_11
48 #define LINKTYPE_IEEE802_11 DLT_IEEE802_11
49 #undef LINKTYPE_PRISM_HEADER
50 #define LINKTYPE_PRISM_HEADER DLT_PRISM_HEADER
51 #undef LINKTYPE_ETHERNET
52 #define LINKTYPE_ETHERNET DLT_ERF_ETH
53 #undef LINKTYPE_PPI_HDR
54 #define LINKTYPE_PPI_HDR DLT_PPI
55 #undef TCPDUMP_MAGIC
56 #define TCPDUMP_MAGIC 0xa1b2c3d4
57 #endif
58 
59 #if defined(_MSC_VER)
60 //  Microsoft
61 #define EXPORT __declspec(dllexport)
62 #define IMPORT __declspec(dllimport)
63 #elif defined(__GNUC__) || defined(__llvm__) || defined(__clang__)             \
64 	|| defined(__INTEL_COMPILER)
65 #define EXPORT __attribute__((visibility("default")))
66 #define IMPORT
67 #else
68 //  do nothing and hope for the best?
69 #define EXPORT
70 #define IMPORT
71 #pragma warning Unknown dynamic link import / export semantics.
72 #endif
73 
74 /* For all structures, when adding new fields, always append them to the end.
75  * This way legacy binary code does not need to be recompiled.  This is
76  * particularly useful for DLLs.  -sorbo
77  */
78 
79 struct tx_info
80 {
81 	uint32_t ti_rate;
82 };
83 
84 struct rx_info
85 {
86 	uint64_t ri_mactime;
87 	int32_t ri_power;
88 	int32_t ri_noise;
89 	uint32_t ri_channel;
90 	uint32_t ri_freq;
91 	uint32_t ri_rate;
92 	uint32_t ri_antenna;
93 } __packed;
94 
95 /* Normal code should not access this directly.  Only osdep.
96  * This structure represents a single interface.  It should be created with
97  * wi_open and destroyed with wi_close.
98  */
99 #define MAX_IFACE_NAME 64
100 struct wif
101 {
102 	int (*wi_read)(struct wif * wi,
103 				   unsigned char * h80211,
104 				   int len,
105 				   struct rx_info * ri);
106 	int (*wi_write)(struct wif * wi,
107 					unsigned char * h80211,
108 					int len,
109 					struct tx_info * ti);
110 	int (*wi_set_ht_channel)(struct wif * wi, int chan, unsigned int htval);
111 	int (*wi_set_channel)(struct wif * wi, int chan);
112 	int (*wi_get_channel)(struct wif * wi);
113 	int (*wi_set_freq)(struct wif * wi, int freq);
114 	int (*wi_get_freq)(struct wif * wi);
115 	void (*wi_close)(struct wif * wi);
116 	int (*wi_fd)(struct wif * wi);
117 	int (*wi_get_mac)(struct wif * wi, unsigned char * mac);
118 	int (*wi_set_mac)(struct wif * wi, unsigned char * mac);
119 	int (*wi_set_rate)(struct wif * wi, int rate);
120 	int (*wi_get_rate)(struct wif * wi);
121 	int (*wi_set_mtu)(struct wif * wi, int mtu);
122 	int (*wi_get_mtu)(struct wif * wi);
123 	int (*wi_get_monitor)(struct wif * wi);
124 
125 	void * wi_priv;
126 	char wi_interface[MAX_IFACE_NAME];
127 };
128 
129 /* Routines to be used by client code */
130 IMPORT struct wif * wi_open(char * iface);
131 IMPORT int
132 wi_read(struct wif * wi, unsigned char * h80211, int len, struct rx_info * ri);
133 IMPORT int
134 wi_write(struct wif * wi, unsigned char * h80211, int len, struct tx_info * ti);
135 IMPORT int wi_set_channel(struct wif * wi, int chan);
136 IMPORT int wi_set_ht_channel(struct wif * wi, int chan, unsigned int htval);
137 IMPORT int wi_get_channel(struct wif * wi);
138 IMPORT int wi_set_freq(struct wif * wi, int freq);
139 IMPORT int wi_get_freq(struct wif * wi);
140 IMPORT void wi_close(struct wif * wi);
141 IMPORT char * wi_get_ifname(struct wif * wi);
142 IMPORT int wi_get_mac(struct wif * wi, unsigned char * mac);
143 IMPORT int wi_set_mac(struct wif * wi, unsigned char * mac);
144 IMPORT int wi_get_rate(struct wif * wi);
145 IMPORT int wi_set_rate(struct wif * wi, int rate);
146 IMPORT int wi_get_monitor(struct wif * wi);
147 IMPORT int wi_get_mtu(struct wif * wi);
148 IMPORT int wi_set_mtu(struct wif * wi, int mtu);
149 
150 /* wi_open_osdep should determine the type of card and setup the wif structure
151  * appropriately.  There is one per OS.  Called by wi_open.
152  */
153 extern struct wif * wi_open_osdep(char * iface);
154 
155 /* This will return the FD used for reading.  This is required for using select
156  * on it.
157  */
158 IMPORT int wi_fd(struct wif * wi);
159 
160 /* Helper routines for osdep code.  */
161 extern struct wif * wi_alloc(int sz);
162 extern void * wi_priv(struct wif * wi);
163 
164 /* Client code can use this to determine the battery state.  One per OS. */
165 IMPORT int get_battery_state(void);
166 
167 /* Client code can create a tap interface */
168 /* XXX we can unify the tap & wi stuff in the future, but for now, lets keep
169  * them separate until we learn something.
170  */
171 struct tif
172 {
173 	int (*ti_read)(struct tif * ti, void * buf, int len);
174 	int (*ti_write)(struct tif * ti, void * buf, int len);
175 	int (*ti_fd)(struct tif * ti);
176 	char * (*ti_name)(struct tif * ti);
177 	int (*ti_set_mtu)(struct tif * ti, int mtu);
178 	int (*ti_get_mtu)(struct tif * ti);
179 	int (*ti_set_ip)(struct tif * ti, struct in_addr * ip);
180 	int (*ti_set_mac)(struct tif * ti, unsigned char * mac);
181 	void (*ti_close)(struct tif * ti);
182 
183 	void * ti_priv;
184 };
185 /* one per OS */
186 IMPORT struct tif * ti_open(char * iface);
187 
188 /* osdep routines */
189 extern struct tif * ti_alloc(int sz);
190 extern void * ti_priv(struct tif * ti);
191 
192 /* client routines */
193 IMPORT char * ti_name(struct tif * ti);
194 IMPORT int ti_set_mtu(struct tif * ti, int mtu);
195 IMPORT int ti_get_mtu(struct tif * ti);
196 IMPORT void ti_close(struct tif * ti);
197 IMPORT int ti_fd(struct tif * ti);
198 IMPORT int ti_read(struct tif * ti, void * buf, int len);
199 IMPORT int ti_write(struct tif * ti, void * buf, int len);
200 IMPORT int ti_set_mac(struct tif * ti, unsigned char * mac);
201 IMPORT int ti_set_ip(struct tif * ti, struct in_addr * ip);
202 
203 #endif /* __AIRCRACK_NG_OSEDEP_H__ */
204