diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 219 |
1 files changed, 94 insertions, 125 deletions
diff --git a/src/main.c b/src/main.c index b74907d..8eeb21f 100644 --- a/src/main.c +++ b/src/main.c | |||
@@ -14,10 +14,12 @@ | |||
14 | 14 | ||
15 | #include "addr_list.h" | 15 | #include "addr_list.h" |
16 | #include "beacon_data.h" | 16 | #include "beacon_data.h" |
17 | #include "retransmit.h" | ||
18 | #include "transmit.h" | ||
17 | 19 | ||
18 | static unsigned char kBroadcastAddress[] = "\xFF\xFF\xFF\xFF\xFF\xFF"; | 20 | #define CHANNEL_NUM 13 |
19 | 21 | ||
20 | static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER; | 22 | static unsigned char kBroadcastAddress[] = "\xFF\xFF\xFF\xFF\xFF\xFF"; |
21 | 23 | ||
22 | static pthread_mutex_t seq_mutex = PTHREAD_MUTEX_INITIALIZER; | 24 | static pthread_mutex_t seq_mutex = PTHREAD_MUTEX_INITIALIZER; |
23 | static int seq_number = 0; | 25 | static int seq_number = 0; |
@@ -50,7 +52,8 @@ int get_interface_index(int sock, const char *interface, int *index) | |||
50 | return 0; | 52 | return 0; |
51 | } | 53 | } |
52 | 54 | ||
53 | int get_next_seq_number() { | 55 | int get_next_seq_number() |
56 | { | ||
54 | pthread_mutex_lock(&seq_mutex); | 57 | pthread_mutex_lock(&seq_mutex); |
55 | int next = seq_number++; | 58 | int next = seq_number++; |
56 | pthread_mutex_unlock(&seq_mutex); | 59 | pthread_mutex_unlock(&seq_mutex); |
@@ -62,7 +65,7 @@ int make_beacon_frame(int index, const unsigned char tx_addr[6], int seq_number, | |||
62 | { | 65 | { |
63 | struct libwifi_beacon beacon = {0}; | 66 | struct libwifi_beacon beacon = {0}; |
64 | 67 | ||
65 | if (libwifi_create_beacon(&beacon, kBroadcastAddress, tx_addr, tx_addr, NULL, 13)) | 68 | if (libwifi_create_beacon(&beacon, kBroadcastAddress, tx_addr, tx_addr, NULL, CHANNEL_NUM)) |
66 | { | 69 | { |
67 | printf("Could not create beacon frame.\n"); | 70 | printf("Could not create beacon frame.\n"); |
68 | return -3; | 71 | return -3; |
@@ -80,7 +83,8 @@ int make_beacon_frame(int index, const unsigned char tx_addr[6], int seq_number, | |||
80 | } | 83 | } |
81 | 84 | ||
82 | static const unsigned char tim_bitmap[] = {0x01, 0x02, 0x00, 0x00}; | 85 | static const unsigned char tim_bitmap[] = {0x01, 0x02, 0x00, 0x00}; |
83 | if (libwifi_quick_add_tag(&beacon.tags, TAG_TIM, tim_bitmap, 4)) { | 86 | if (libwifi_quick_add_tag(&beacon.tags, TAG_TIM, tim_bitmap, 4)) |
87 | { | ||
84 | printf("Could not add TIM tag.\n"); | 88 | printf("Could not add TIM tag.\n"); |
85 | return -7; | 89 | return -7; |
86 | } | 90 | } |
@@ -134,88 +138,17 @@ int make_beacon_frame(int index, const unsigned char tx_addr[6], int seq_number, | |||
134 | return beacon_size; | 138 | return beacon_size; |
135 | } | 139 | } |
136 | 140 | ||
137 | int prepend_radiotap(const unsigned char *input, int input_size, unsigned char **output) | ||
138 | { | ||
139 | static const unsigned char radiotap[] = "\x00\x00\x08\x00\x00\x00\x00\x00"; | ||
140 | static const int radiotap_size = 8; | ||
141 | |||
142 | int output_size = input_size + radiotap_size; | ||
143 | |||
144 | unsigned char *buf = (unsigned char *)malloc(output_size); | ||
145 | if (buf == NULL) | ||
146 | { | ||
147 | return -1; | ||
148 | } | ||
149 | |||
150 | memcpy(buf, radiotap, radiotap_size); | ||
151 | memcpy(buf + radiotap_size, input, input_size); | ||
152 | |||
153 | *output = buf; | ||
154 | |||
155 | return output_size; | ||
156 | } | ||
157 | |||
158 | int send_packet(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size) | ||
159 | { | ||
160 | struct sockaddr_ll socket_address; | ||
161 | socket_address.sll_ifindex = device_index; | ||
162 | socket_address.sll_halen = ETH_ALEN; | ||
163 | memcpy(socket_address.sll_addr, dst_addr, 6); | ||
164 | |||
165 | pthread_mutex_lock(&socket_mutex); | ||
166 | |||
167 | int ret = 0; | ||
168 | if (sendto(sock, packet, packet_size, 0, (struct sockaddr *)&socket_address, sizeof(struct sockaddr_ll)) < 0) | ||
169 | { | ||
170 | printf("Could not send packet: %d\n", errno); | ||
171 | ret = errno; | ||
172 | } | ||
173 | |||
174 | pthread_mutex_unlock(&socket_mutex); | ||
175 | |||
176 | return ret; | ||
177 | } | ||
178 | |||
179 | int send_packet_with_radiotap(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size) | ||
180 | { | ||
181 | struct libwifi_radiotap_info radiotap_info = {0}; | ||
182 | radiotap_info.present = (1 << IEEE80211_RADIOTAP_FLAGS) | (1 << IEEE80211_RADIOTAP_RATE); | ||
183 | radiotap_info.flags = IEEE80211_RADIOTAP_F_FCS | IEEE80211_RADIOTAP_F_SHORTPRE; | ||
184 | radiotap_info.rate_raw = 4; | ||
185 | |||
186 | unsigned char radiotap_buffer[256]; | ||
187 | int radiotap_size = libwifi_create_radiotap(&radiotap_info, radiotap_buffer); | ||
188 | |||
189 | uint32_t fcs = libwifi_calculate_fcs(packet, packet_size); | ||
190 | |||
191 | int buffer_size = radiotap_size + packet_size + sizeof(uint32_t); | ||
192 | unsigned char *buffer = malloc(buffer_size); | ||
193 | if (buffer == NULL) | ||
194 | { | ||
195 | printf("Could not prepend radiotap.\n"); | ||
196 | return -2; | ||
197 | } | ||
198 | |||
199 | memcpy(buffer, radiotap_buffer, radiotap_size); | ||
200 | memcpy(buffer + radiotap_size, packet, packet_size); | ||
201 | memcpy(buffer + radiotap_size + packet_size, (unsigned char*)&fcs, sizeof(uint32_t)); | ||
202 | |||
203 | int ret = send_packet(sock, device_index, dst_addr, buffer, buffer_size); | ||
204 | free(buffer); | ||
205 | |||
206 | return ret; | ||
207 | } | ||
208 | |||
209 | int send_ack(int sock, int device_index, const unsigned char dst_addr[6]) | 141 | int send_ack(int sock, int device_index, const unsigned char dst_addr[6]) |
210 | { | 142 | { |
211 | struct libwifi_cts ack_frame = {0}; | 143 | struct libwifi_cts ack_frame = {0}; |
212 | if (libwifi_create_cts(&ack_frame, dst_addr, 0)) { | 144 | if (libwifi_create_cts(&ack_frame, dst_addr, 0)) |
145 | { | ||
213 | printf("Could not create ack packet.\n"); | 146 | printf("Could not create ack packet.\n"); |
214 | } | 147 | } |
215 | 148 | ||
216 | ack_frame.frame_header.frame_control.subtype = SUBTYPE_ACK; | 149 | ack_frame.frame_header.frame_control.subtype = SUBTYPE_ACK; |
217 | 150 | ||
218 | int ret = send_packet_with_radiotap(sock, device_index, dst_addr, (unsigned char*)&ack_frame, sizeof(struct libwifi_cts)); | 151 | int ret = send_packet_with_radiotap(sock, device_index, dst_addr, (unsigned char *)&ack_frame, sizeof(struct libwifi_cts)); |
219 | 152 | ||
220 | return ret; | 153 | return ret; |
221 | } | 154 | } |
@@ -232,10 +165,22 @@ struct thread_info | |||
232 | int sock; | 165 | int sock; |
233 | unsigned char tx_addr[6]; | 166 | unsigned char tx_addr[6]; |
234 | int device_index; | 167 | int device_index; |
168 | struct retransmitter *retransmitter; | ||
235 | 169 | ||
236 | struct addr_list authenticated; | 170 | struct addr_list authenticated; |
237 | }; | 171 | }; |
238 | 172 | ||
173 | enum auth_state | ||
174 | { | ||
175 | kAuthStateAuthenticated, | ||
176 | kAuthStateAssociated, | ||
177 | }; | ||
178 | |||
179 | struct auth_info | ||
180 | { | ||
181 | enum auth_state auth_state; | ||
182 | }; | ||
183 | |||
239 | void handle_incoming_packet(u_char *userdata, const struct pcap_pkthdr *h, const u_char *bytes) | 184 | void handle_incoming_packet(u_char *userdata, const struct pcap_pkthdr *h, const u_char *bytes) |
240 | { | 185 | { |
241 | struct thread_info *tinfo = (struct thread_info *)userdata; | 186 | struct thread_info *tinfo = (struct thread_info *)userdata; |
@@ -249,7 +194,8 @@ void handle_incoming_packet(u_char *userdata, const struct pcap_pkthdr *h, const | |||
249 | 194 | ||
250 | if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_AUTH) | 195 | if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_AUTH) |
251 | { | 196 | { |
252 | if (strncmp(frame.header.data.addr1, tinfo->tx_addr, 6)) { | 197 | if (strncmp(frame.header.data.addr1, tinfo->tx_addr, 6)) |
198 | { | ||
253 | // This is not for me. Misdelivery. | 199 | // This is not for me. Misdelivery. |
254 | return; | 200 | return; |
255 | } | 201 | } |
@@ -271,11 +217,14 @@ void handle_incoming_packet(u_char *userdata, const struct pcap_pkthdr *h, const | |||
271 | return; | 217 | return; |
272 | } | 218 | } |
273 | 219 | ||
274 | //usleep(frame.header.ctrl.duration / 2 * 100); | 220 | // usleep(frame.header.ctrl.duration / 2 * 100); |
275 | 221 | ||
276 | send_ack(tinfo->sock, tinfo->device_index, frame.header.data.addr2); | 222 | send_ack(tinfo->sock, tinfo->device_index, frame.header.data.addr2); |
277 | 223 | ||
278 | addr_list_add(&tinfo->authenticated, frame.header.data.addr2); | 224 | struct auth_info *ainfo = (struct auth_info *)malloc(sizeof(struct auth_info)); |
225 | ainfo->auth_state = kAuthStateAuthenticated; | ||
226 | |||
227 | addr_list_add(&tinfo->authenticated, frame.header.data.addr2, ainfo); | ||
279 | 228 | ||
280 | struct libwifi_auth auth_resp = {0}; | 229 | struct libwifi_auth auth_resp = {0}; |
281 | if (libwifi_create_auth(&auth_resp, frame.header.data.addr2, frame.header.data.addr1, frame.header.data.addr1, AUTH_OPEN, 2, STATUS_SUCCESS)) | 230 | if (libwifi_create_auth(&auth_resp, frame.header.data.addr2, frame.header.data.addr1, frame.header.data.addr1, AUTH_OPEN, 2, STATUS_SUCCESS)) |
@@ -287,8 +236,8 @@ void handle_incoming_packet(u_char *userdata, const struct pcap_pkthdr *h, const | |||
287 | auth_resp.frame_header.duration = frame.header.ctrl.duration; | 236 | auth_resp.frame_header.duration = frame.header.ctrl.duration; |
288 | auth_resp.frame_header.seq_control.sequence_number = get_next_seq_number(); | 237 | auth_resp.frame_header.seq_control.sequence_number = get_next_seq_number(); |
289 | 238 | ||
290 | //static const unsigned char broadcom_tag_value[] = {0x00, 0x10, 0x18, 0x02, 0x00, 0xf0, 0x00, 0x00, 0x00}; | 239 | // static const unsigned char broadcom_tag_value[] = {0x00, 0x10, 0x18, 0x02, 0x00, 0xf0, 0x00, 0x00, 0x00}; |
291 | //libwifi_quick_add_tag(&auth_resp.tags, TAG_VENDOR_SPECIFIC, broadcom_tag_value, 9); | 240 | // libwifi_quick_add_tag(&auth_resp.tags, TAG_VENDOR_SPECIFIC, broadcom_tag_value, 9); |
292 | 241 | ||
293 | size_t auth_resp_size = libwifi_get_auth_length(&auth_resp); | 242 | size_t auth_resp_size = libwifi_get_auth_length(&auth_resp); |
294 | unsigned char *auth_resp_output = (unsigned char *)malloc(auth_resp_size); | 243 | unsigned char *auth_resp_output = (unsigned char *)malloc(auth_resp_size); |
@@ -306,23 +255,73 @@ void handle_incoming_packet(u_char *userdata, const struct pcap_pkthdr *h, const | |||
306 | 255 | ||
307 | libwifi_free_auth(&auth_resp); | 256 | libwifi_free_auth(&auth_resp); |
308 | 257 | ||
309 | //usleep(frame.header.ctrl.duration * 100); | 258 | // usleep(frame.header.ctrl.duration * 100); |
259 | |||
260 | send_until_acked(tinfo->retransmitter, frame.header.data.addr2, auth_resp_output, auth_resp_size); | ||
310 | 261 | ||
311 | if (send_packet_with_radiotap(tinfo->sock, tinfo->device_index, frame.header.data.addr2, auth_resp_output, auth_resp_size)) | 262 | printf("Successfully authenticated %s!\n", in_addr); |
263 | } | ||
264 | else if (frame.frame_control.type == TYPE_CONTROL && frame.frame_control.subtype == SUBTYPE_ACK) | ||
265 | { | ||
266 | if (strncmp(frame.body, tinfo->tx_addr, 6)) | ||
312 | { | 267 | { |
313 | printf("Could not send auth response.\n"); | 268 | // This is not for me. Misdelivery. |
314 | return; | 269 | return; |
315 | } | 270 | } |
316 | 271 | ||
317 | usleep(5000); | 272 | handle_ack(tinfo->retransmitter); |
273 | } | ||
274 | else if (frame.frame_control.type == TYPE_MANAGEMENT && frame.frame_control.subtype == SUBTYPE_ASSOC_REQ) | ||
275 | { | ||
276 | if (strncmp(frame.header.data.addr1, tinfo->tx_addr, 6)) | ||
277 | { | ||
278 | // This is not for me. Misdelivery. | ||
279 | return; | ||
280 | } | ||
318 | 281 | ||
319 | if (send_packet_with_radiotap(tinfo->sock, tinfo->device_index, frame.header.data.addr2, auth_resp_output, auth_resp_size)) | 282 | unsigned char in_addr[18]; |
283 | format_mac_address(frame.header.data.addr2, in_addr); | ||
284 | printf("Association request from %s\n", in_addr); | ||
285 | |||
286 | if (!addr_list_contains(&tinfo->authenticated, frame.header.data.addr2)) | ||
320 | { | 287 | { |
321 | printf("Could not send auth response.\n"); | 288 | // Not authenticated. |
322 | return; | 289 | return; |
323 | } | 290 | } |
324 | 291 | ||
325 | printf("Successfully authenticated %s!\n", in_addr); | 292 | struct auth_info *ainfo = addr_list_get(&tinfo->authenticated, frame.header.data.addr2); |
293 | if (ainfo->auth_state != kAuthStateAuthenticated) | ||
294 | { | ||
295 | // Already associated. | ||
296 | return; | ||
297 | } | ||
298 | |||
299 | send_ack(tinfo->sock, tinfo->device_index, frame.header.data.addr2); | ||
300 | |||
301 | ainfo->auth_state = kAuthStateAssociated; | ||
302 | |||
303 | struct libwifi_assoc_resp assoc_resp = {0}; | ||
304 | if (libwifi_create_assoc_resp(&assoc_resp, frame.header.data.addr2, tinfo->tx_addr, tinfo->tx_addr, CHANNEL_NUM)) | ||
305 | { | ||
306 | printf("Could not create assoc response.\n"); | ||
307 | return; | ||
308 | } | ||
309 | |||
310 | assoc_resp.frame_header.seq_control.sequence_number = get_next_seq_number(); | ||
311 | |||
312 | size_t assoc_resp_length = libwifi_get_assoc_resp_length(&assoc_resp); | ||
313 | unsigned char *assoc_resp_buffer = malloc(assoc_resp_length); | ||
314 | if (libwifi_dump_assoc_resp(&assoc_resp, assoc_resp_buffer, assoc_resp_length) < 0) | ||
315 | { | ||
316 | printf("Could not dump assoc response.\n"); | ||
317 | return; | ||
318 | } | ||
319 | |||
320 | libwifi_free_assoc_resp(&assoc_resp); | ||
321 | |||
322 | send_until_acked(tinfo->retransmitter, frame.header.data.addr2, assoc_resp_buffer, assoc_resp_length); | ||
323 | |||
324 | printf("Successfully associated with %s!\n", in_addr); | ||
326 | } | 325 | } |
327 | } | 326 | } |
328 | 327 | ||
@@ -330,31 +329,6 @@ void *polling_thread(void *arg) | |||
330 | { | 329 | { |
331 | struct thread_info *tinfo = (struct thread_info *)arg; | 330 | struct thread_info *tinfo = (struct thread_info *)arg; |
332 | 331 | ||
333 | /*for (;;) { | ||
334 | struct pollfd pfd; | ||
335 | pfd.fd = tinfo->sock; | ||
336 | pfd.events = POLLIN; | ||
337 | |||
338 | if (poll(&pfd, 1, -1) < 0) { | ||
339 | printf("Error polling.\n"); | ||
340 | } else { | ||
341 | if (pfd.revents & POLLIN) { | ||
342 | unsigned char message[1024]; | ||
343 | int msglen = read(tinfo->sock, message, 1024); | ||
344 | if (msglen < 0) { | ||
345 | printf("Could not read from socket.\n"); | ||
346 | } else { | ||
347 | struct libwifi_frame frame = {0}; | ||
348 | if (libwifi_get_wifi_frame(&frame, message, msglen, true)) { | ||
349 | printf("Could not parse frame.\n"); | ||
350 | } else { | ||
351 | printf("Frame type: %d:%d\n", frame.frame_control.type, frame.frame_control.subtype); | ||
352 | } | ||
353 | } | ||
354 | } | ||
355 | } | ||
356 | }*/ | ||
357 | |||
358 | char errbuf[PCAP_ERRBUF_SIZE]; | 332 | char errbuf[PCAP_ERRBUF_SIZE]; |
359 | pcap_t *sniffer = pcap_create("mon0", errbuf); | 333 | pcap_t *sniffer = pcap_create("mon0", errbuf); |
360 | if (sniffer == NULL) | 334 | if (sniffer == NULL) |
@@ -396,21 +370,16 @@ int main(int argc, char **argv) | |||
396 | return 2; | 370 | return 2; |
397 | } | 371 | } |
398 | 372 | ||
373 | struct retransmitter *retransmitter = start_retransmit_thread(sock, device_index); | ||
374 | |||
399 | struct thread_info *tinfo = (struct thread_info *)malloc(sizeof(struct thread_info *)); | 375 | struct thread_info *tinfo = (struct thread_info *)malloc(sizeof(struct thread_info *)); |
400 | tinfo->sock = sock; | 376 | tinfo->sock = sock; |
401 | memcpy(tinfo->tx_addr, tx_addr, 6); | 377 | memcpy(tinfo->tx_addr, tx_addr, 6); |
402 | tinfo->device_index = device_index; | 378 | tinfo->device_index = device_index; |
379 | tinfo->retransmitter = retransmitter; | ||
403 | addr_list_init(&tinfo->authenticated); | 380 | addr_list_init(&tinfo->authenticated); |
404 | 381 | ||
405 | pthread_attr_t thread_attr; | 382 | pthread_create(&tinfo->thread_id, NULL, &polling_thread, tinfo); |
406 | if (pthread_attr_init(&thread_attr)) | ||
407 | { | ||
408 | printf("Could not initialize thread attr.\n"); | ||
409 | return 6; | ||
410 | } | ||
411 | |||
412 | pthread_create(&tinfo->thread_id, &thread_attr, &polling_thread, tinfo); | ||
413 | pthread_attr_destroy(&thread_attr); | ||
414 | 383 | ||
415 | int beacon_index = 0; | 384 | int beacon_index = 0; |
416 | for (;;) | 385 | for (;;) |