]> The Tcpdump Group git mirrors - tcpdump/blob - print-802_11.c
Fix typo.
[tcpdump] / print-802_11.c
1 /*
2 * Copyright (c) 2001
3 * Fortress Technologies, Inc. All rights reserved.
4 * Charlie Lenahan (clenahan@fortresstech.com)
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
17 * written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 */
22
23 #ifndef lint
24 static const char rcsid[] _U_ =
25 "@(#) $Header: /tcpdump/master/tcpdump/print-802_11.c,v 1.49 2007-12-29 23:25:02 guy Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <tcpdump-stdinc.h>
33
34 #include <stdio.h>
35 #include <pcap.h>
36 #include <string.h>
37
38 #include "interface.h"
39 #include "addrtoname.h"
40 #include "ethertype.h"
41
42 #include "extract.h"
43
44 #include "cpack.h"
45
46 #include "ieee802_11.h"
47 #include "ieee802_11_radio.h"
48
49 #define PRINT_SSID(p) \
50 switch (p.ssid_status) { \
51 case TRUNCATED: \
52 return 0; \
53 case PRESENT: \
54 printf(" ("); \
55 fn_print(p.ssid.ssid, NULL); \
56 printf(")"); \
57 break; \
58 case NOT_PRESENT: \
59 break; \
60 }
61
62 #define PRINT_RATE(_sep, _r, _suf) \
63 printf("%s%2.1f%s", _sep, (.5 * ((_r) & 0x7f)), _suf)
64 #define PRINT_RATES(p) \
65 switch (p.rates_status) { \
66 case TRUNCATED: \
67 return 0; \
68 case PRESENT: \
69 do { \
70 int z; \
71 const char *sep = " ["; \
72 for (z = 0; z < p.rates.length ; z++) { \
73 PRINT_RATE(sep, p.rates.rate[z], \
74 (p.rates.rate[z] & 0x80 ? "*" : "")); \
75 sep = " "; \
76 } \
77 if (p.rates.length != 0) \
78 printf(" Mbit]"); \
79 } while (0); \
80 break; \
81 case NOT_PRESENT: \
82 break; \
83 }
84
85 #define PRINT_DS_CHANNEL(p) \
86 switch (p.ds_status) { \
87 case TRUNCATED: \
88 return 0; \
89 case PRESENT: \
90 printf(" CH: %u", p.ds.channel); \
91 break; \
92 case NOT_PRESENT: \
93 break; \
94 } \
95 printf("%s", \
96 CAPABILITY_PRIVACY(p.capability_info) ? ", PRIVACY" : "" );
97
98 static const int ieee80211_htrates[16] = {
99 13, /* IFM_IEEE80211_MCS0 */
100 26, /* IFM_IEEE80211_MCS1 */
101 39, /* IFM_IEEE80211_MCS2 */
102 52, /* IFM_IEEE80211_MCS3 */
103 78, /* IFM_IEEE80211_MCS4 */
104 104, /* IFM_IEEE80211_MCS5 */
105 117, /* IFM_IEEE80211_MCS6 */
106 130, /* IFM_IEEE80211_MCS7 */
107 26, /* IFM_IEEE80211_MCS8 */
108 52, /* IFM_IEEE80211_MCS9 */
109 78, /* IFM_IEEE80211_MCS10 */
110 104, /* IFM_IEEE80211_MCS11 */
111 156, /* IFM_IEEE80211_MCS12 */
112 208, /* IFM_IEEE80211_MCS13 */
113 234, /* IFM_IEEE80211_MCS14 */
114 260, /* IFM_IEEE80211_MCS15 */
115 };
116 #define PRINT_HT_RATE(_sep, _r, _suf) \
117 printf("%s%.1f%s", _sep, (.5 * ieee80211_htrates[(_r) & 0xf]), _suf)
118
119 static const char *auth_alg_text[]={"Open System","Shared Key","EAP"};
120 #define NUM_AUTH_ALGS (sizeof auth_alg_text / sizeof auth_alg_text[0])
121
122 static const char *status_text[] = {
123 "Succesful", /* 0 */
124 "Unspecified failure", /* 1 */
125 "Reserved", /* 2 */
126 "Reserved", /* 3 */
127 "Reserved", /* 4 */
128 "Reserved", /* 5 */
129 "Reserved", /* 6 */
130 "Reserved", /* 7 */
131 "Reserved", /* 8 */
132 "Reserved", /* 9 */
133 "Cannot Support all requested capabilities in the Capability "
134 "Information field", /* 10 */
135 "Reassociation denied due to inability to confirm that association "
136 "exists", /* 11 */
137 "Association denied due to reason outside the scope of the "
138 "standard", /* 12 */
139 "Responding station does not support the specified authentication "
140 "algorithm ", /* 13 */
141 "Received an Authentication frame with authentication transaction "
142 "sequence number out of expected sequence", /* 14 */
143 "Authentication rejected because of challenge failure", /* 15 */
144 "Authentication rejected due to timeout waiting for next frame in "
145 "sequence", /* 16 */
146 "Association denied because AP is unable to handle additional"
147 "associated stations", /* 17 */
148 "Association denied due to requesting station not supporting all of "
149 "the data rates in BSSBasicRateSet parameter", /* 18 */
150 "Association denied due to requesting station not supporting "
151 "short preamble operation", /* 19 */
152 "Association denied due to requesting station not supporting "
153 "PBCC encoding", /* 20 */
154 "Association denied due to requesting station not supporting "
155 "channel agility", /* 21 */
156 "Association request rejected because Spectrum Management "
157 "capability is required", /* 22 */
158 "Association request rejected because the information in the "
159 "Power Capability element is unacceptable", /* 23 */
160 "Association request rejected because the information in the "
161 "Supported Channels element is unacceptable", /* 24 */
162 "Association denied due to requesting station not supporting "
163 "short slot operation", /* 25 */
164 "Association denied due to requesting station not supporting "
165 "DSSS-OFDM operation", /* 26 */
166 "Association denied because the requested STA does not support HT "
167 "features", /* 27 */
168 "Reserved", /* 28 */
169 "Association denied because the requested STA does not support "
170 "the PCO transition time required by the AP", /* 29 */
171 "Reserved", /* 30 */
172 "Reserved", /* 31 */
173 "Unspecified, QoS-related failure", /* 32 */
174 "Association denied due to QAP having insufficient bandwidth "
175 "to handle another QSTA", /* 33 */
176 "Association denied due to excessive frame loss rates and/or "
177 "poor conditions on current operating channel", /* 34 */
178 "Association (with QBSS) denied due to requesting station not "
179 "supporting the QoS facility", /* 35 */
180 "Association denied due to requesting station not supporting "
181 "Block Ack", /* 36 */
182 "The request has been declined", /* 37 */
183 "The request has not been successful as one or more parameters "
184 "have invalid values", /* 38 */
185 "The TS has not been created because the request cannot be honored. "
186 "However, a suggested TSPEC is provided so that the initiating QSTA"
187 "may attempt to set another TS with the suggested changes to the "
188 "TSPEC", /* 39 */
189 "Invalid Information Element", /* 40 */
190 "Group Cipher is not valid", /* 41 */
191 "Pairwise Cipher is not valid", /* 42 */
192 "AKMP is not valid", /* 43 */
193 "Unsupported RSN IE version", /* 44 */
194 "Invalid RSN IE Capabilities", /* 45 */
195 "Cipher suite is rejected per security policy", /* 46 */
196 "The TS has not been created. However, the HC may be capable of "
197 "creating a TS, in response to a request, after the time indicated "
198 "in the TS Delay element", /* 47 */
199 "Direct Link is not allowed in the BSS by policy", /* 48 */
200 "Destination STA is not present within this QBSS.", /* 49 */
201 "The Destination STA is not a QSTA.", /* 50 */
202
203 };
204 #define NUM_STATUSES (sizeof status_text / sizeof status_text[0])
205
206 static const char *reason_text[] = {
207 "Reserved", /* 0 */
208 "Unspecified reason", /* 1 */
209 "Previous authentication no longer valid", /* 2 */
210 "Deauthenticated because sending station is leaving (or has left) "
211 "IBSS or ESS", /* 3 */
212 "Disassociated due to inactivity", /* 4 */
213 "Disassociated because AP is unable to handle all currently "
214 " associated stations", /* 5 */
215 "Class 2 frame received from nonauthenticated station", /* 6 */
216 "Class 3 frame received from nonassociated station", /* 7 */
217 "Disassociated because sending station is leaving "
218 "(or has left) BSS", /* 8 */
219 "Station requesting (re)association is not authenticated with "
220 "responding station", /* 9 */
221 "Disassociated because the information in the Power Capability "
222 "element is unacceptable", /* 10 */
223 "Disassociated because the information in the SupportedChannels "
224 "element is unacceptable", /* 11 */
225 "Invalid Information Element", /* 12 */
226 "Reserved", /* 13 */
227 "Michael MIC failure", /* 14 */
228 "4-Way Handshake timeout", /* 15 */
229 "Group key update timeout", /* 16 */
230 "Information element in 4-Way Handshake different from (Re)Association"
231 "Request/Probe Response/Beacon", /* 17 */
232 "Group Cipher is not valid", /* 18 */
233 "AKMP is not valid", /* 20 */
234 "Unsupported RSN IE version", /* 21 */
235 "Invalid RSN IE Capabilities", /* 22 */
236 "IEEE 802.1X Authentication failed", /* 23 */
237 "Cipher suite is rejected per security policy", /* 24 */
238 "Reserved", /* 25 */
239 "Reserved", /* 26 */
240 "Reserved", /* 27 */
241 "Reserved", /* 28 */
242 "Reserved", /* 29 */
243 "Reserved", /* 30 */
244 "TS deleted because QoS AP lacks sufficient bandwidth for this "
245 "QoS STA due to a change in BSS service characteristics or "
246 "operational mode (e.g. an HT BSS change from 40 MHz channel "
247 "to 20 MHz channel)", /* 31 */
248 "Disassociated for unspecified, QoS-related reason", /* 32 */
249 "Disassociated because QoS AP lacks sufficient bandwidth for this "
250 "QoS STA", /* 33 */
251 "Disassociated because of excessive number of frames that need to be "
252 "acknowledged, but are not acknowledged for AP transmissions "
253 "and/or poor channel conditions", /* 34 */
254 "Disassociated because STA is transmitting outside the limits "
255 "of its TXOPs", /* 35 */
256 "Requested from peer STA as the STA is leaving the BSS "
257 "(or resetting)", /* 36 */
258 "Requested from peer STA as it does not want to use the "
259 "mechanism", /* 37 */
260 "Requested from peer STA as the STA received frames using the "
261 "mechanism for which a set up is required", /* 38 */
262 "Requested from peer STA due to time out", /* 39 */
263 "Reserved", /* 40 */
264 "Reserved", /* 41 */
265 "Reserved", /* 42 */
266 "Reserved", /* 43 */
267 "Reserved", /* 44 */
268 "Peer STA does not support the requested cipher suite", /* 45 */
269 "Association denied due to requesting STA not supporting HT "
270 "features", /* 46 */
271 };
272 #define NUM_REASONS (sizeof reason_text / sizeof reason_text[0])
273
274 static int
275 wep_print(const u_char *p)
276 {
277 u_int32_t iv;
278
279 if (!TTEST2(*p, IEEE802_11_IV_LEN + IEEE802_11_KID_LEN))
280 return 0;
281 iv = EXTRACT_LE_32BITS(p);
282
283 printf("Data IV:%3x Pad %x KeyID %x", IV_IV(iv), IV_PAD(iv),
284 IV_KEYID(iv));
285
286 return 1;
287 }
288
289 static void
290 parse_elements(struct mgmt_body_t *pbody, const u_char *p, int offset)
291 {
292 /*
293 * We haven't seen any elements yet.
294 */
295 pbody->challenge_status = NOT_PRESENT;
296 pbody->ssid_status = NOT_PRESENT;
297 pbody->rates_status = NOT_PRESENT;
298 pbody->ds_status = NOT_PRESENT;
299 pbody->cf_status = NOT_PRESENT;
300 pbody->tim_status = NOT_PRESENT;
301
302 for (;;) {
303 if (!TTEST2(*(p + offset), 1))
304 return;
305 switch (*(p + offset)) {
306 case E_SSID:
307 /* Present, possibly truncated */
308 pbody->ssid_status = TRUNCATED;
309 if (!TTEST2(*(p + offset), 2))
310 return;
311 memcpy(&pbody->ssid, p + offset, 2);
312 offset += 2;
313 if (pbody->ssid.length != 0) {
314 if (pbody->ssid.length >
315 sizeof(pbody->ssid.ssid) - 1)
316 return;
317 if (!TTEST2(*(p + offset), pbody->ssid.length))
318 return;
319 memcpy(&pbody->ssid.ssid, p + offset,
320 pbody->ssid.length);
321 offset += pbody->ssid.length;
322 }
323 pbody->ssid.ssid[pbody->ssid.length] = '\0';
324 /* Present and not truncated */
325 pbody->ssid_status = PRESENT;
326 break;
327 case E_CHALLENGE:
328 /* Present, possibly truncated */
329 pbody->challenge_status = TRUNCATED;
330 if (!TTEST2(*(p + offset), 2))
331 return;
332 memcpy(&pbody->challenge, p + offset, 2);
333 offset += 2;
334 if (pbody->challenge.length != 0) {
335 if (pbody->challenge.length >
336 sizeof(pbody->challenge.text) - 1)
337 return;
338 if (!TTEST2(*(p + offset), pbody->challenge.length))
339 return;
340 memcpy(&pbody->challenge.text, p + offset,
341 pbody->challenge.length);
342 offset += pbody->challenge.length;
343 }
344 pbody->challenge.text[pbody->challenge.length] = '\0';
345 /* Present and not truncated */
346 pbody->challenge_status = PRESENT;
347 break;
348 case E_RATES:
349 /* Present, possibly truncated */
350 pbody->rates_status = TRUNCATED;
351 if (!TTEST2(*(p + offset), 2))
352 return;
353 memcpy(&(pbody->rates), p + offset, 2);
354 offset += 2;
355 if (pbody->rates.length != 0) {
356 if (pbody->rates.length > sizeof pbody->rates.rate)
357 return;
358 if (!TTEST2(*(p + offset), pbody->rates.length))
359 return;
360 memcpy(&pbody->rates.rate, p + offset,
361 pbody->rates.length);
362 offset += pbody->rates.length;
363 }
364 /* Present and not truncated */
365 pbody->rates_status = PRESENT;
366 break;
367 case E_DS:
368 /* Present, possibly truncated */
369 pbody->ds_status = TRUNCATED;
370 if (!TTEST2(*(p + offset), 3))
371 return;
372 memcpy(&pbody->ds, p + offset, 3);
373 offset += 3;
374 /* Present and not truncated */
375 pbody->ds_status = PRESENT;
376 break;
377 case E_CF:
378 /* Present, possibly truncated */
379 pbody->cf_status = TRUNCATED;
380 if (!TTEST2(*(p + offset), 8))
381 return;
382 memcpy(&pbody->cf, p + offset, 8);
383 offset += 8;
384 /* Present and not truncated */
385 pbody->cf_status = PRESENT;
386 break;
387 case E_TIM:
388 /* Present, possibly truncated */
389 pbody->tim_status = TRUNCATED;
390 if (!TTEST2(*(p + offset), 2))
391 return;
392 memcpy(&pbody->tim, p + offset, 2);
393 offset += 2;
394 if (!TTEST2(*(p + offset), 3))
395 return;
396 memcpy(&pbody->tim.count, p + offset, 3);
397 offset += 3;
398
399 if (pbody->tim.length <= 3)
400 break;
401 if (pbody->tim.length - 3 > (int)sizeof pbody->tim.bitmap)
402 return;
403 if (!TTEST2(*(p + offset), pbody->tim.length - 3))
404 return;
405 memcpy(pbody->tim.bitmap, p + (pbody->tim.length - 3),
406 (pbody->tim.length - 3));
407 offset += pbody->tim.length - 3;
408 /* Present and not truncated */
409 pbody->tim_status = PRESENT;
410 break;
411 default:
412 #if 0
413 printf("(1) unhandled element_id (%d) ",
414 *(p + offset) );
415 #endif
416 if (!TTEST2(*(p + offset), 2))
417 return;
418 if (!TTEST2(*(p + offset + 2), *(p + offset + 1)))
419 return;
420 offset += *(p + offset + 1) + 2;
421 break;
422 }
423 }
424 }
425
426 /*********************************************************************************
427 * Print Handle functions for the management frame types
428 *********************************************************************************/
429
430 static int
431 handle_beacon(const u_char *p)
432 {
433 struct mgmt_body_t pbody;
434 int offset = 0;
435
436 memset(&pbody, 0, sizeof(pbody));
437
438 if (!TTEST2(*p, IEEE802_11_TSTAMP_LEN + IEEE802_11_BCNINT_LEN +
439 IEEE802_11_CAPINFO_LEN))
440 return 0;
441 memcpy(&pbody.timestamp, p, IEEE802_11_TSTAMP_LEN);
442 offset += IEEE802_11_TSTAMP_LEN;
443 pbody.beacon_interval = EXTRACT_LE_16BITS(p+offset);
444 offset += IEEE802_11_BCNINT_LEN;
445 pbody.capability_info = EXTRACT_LE_16BITS(p+offset);
446 offset += IEEE802_11_CAPINFO_LEN;
447
448 parse_elements(&pbody, p, offset);
449
450 PRINT_SSID(pbody);
451 PRINT_RATES(pbody);
452 printf(" %s",
453 CAPABILITY_ESS(pbody.capability_info) ? "ESS" : "IBSS");
454 PRINT_DS_CHANNEL(pbody);
455
456 return 1;
457 }
458
459 static int
460 handle_assoc_request(const u_char *p)
461 {
462 struct mgmt_body_t pbody;
463 int offset = 0;
464
465 memset(&pbody, 0, sizeof(pbody));
466
467 if (!TTEST2(*p, IEEE802_11_CAPINFO_LEN + IEEE802_11_LISTENINT_LEN))
468 return 0;
469 pbody.capability_info = EXTRACT_LE_16BITS(p);
470 offset += IEEE802_11_CAPINFO_LEN;
471 pbody.listen_interval = EXTRACT_LE_16BITS(p+offset);
472 offset += IEEE802_11_LISTENINT_LEN;
473
474 parse_elements(&pbody, p, offset);
475
476 PRINT_SSID(pbody);
477 PRINT_RATES(pbody);
478 return 1;
479 }
480
481 static int
482 handle_assoc_response(const u_char *p)
483 {
484 struct mgmt_body_t pbody;
485 int offset = 0;
486
487 memset(&pbody, 0, sizeof(pbody));
488
489 if (!TTEST2(*p, IEEE802_11_CAPINFO_LEN + IEEE802_11_STATUS_LEN +
490 IEEE802_11_AID_LEN))
491 return 0;
492 pbody.capability_info = EXTRACT_LE_16BITS(p);
493 offset += IEEE802_11_CAPINFO_LEN;
494 pbody.status_code = EXTRACT_LE_16BITS(p+offset);
495 offset += IEEE802_11_STATUS_LEN;
496 pbody.aid = EXTRACT_LE_16BITS(p+offset);
497 offset += IEEE802_11_AID_LEN;
498
499 parse_elements(&pbody, p, offset);
500
501 printf(" AID(%x) :%s: %s", ((u_int16_t)(pbody.aid << 2 )) >> 2 ,
502 CAPABILITY_PRIVACY(pbody.capability_info) ? " PRIVACY " : "",
503 (pbody.status_code < NUM_STATUSES
504 ? status_text[pbody.status_code]
505 : "n/a"));
506
507 return 1;
508 }
509
510 static int
511 handle_reassoc_request(const u_char *p)
512 {
513 struct mgmt_body_t pbody;
514 int offset = 0;
515
516 memset(&pbody, 0, sizeof(pbody));
517
518 if (!TTEST2(*p, IEEE802_11_CAPINFO_LEN + IEEE802_11_LISTENINT_LEN +
519 IEEE802_11_AP_LEN))
520 return 0;
521 pbody.capability_info = EXTRACT_LE_16BITS(p);
522 offset += IEEE802_11_CAPINFO_LEN;
523 pbody.listen_interval = EXTRACT_LE_16BITS(p+offset);
524 offset += IEEE802_11_LISTENINT_LEN;
525 memcpy(&pbody.ap, p+offset, IEEE802_11_AP_LEN);
526 offset += IEEE802_11_AP_LEN;
527
528 parse_elements(&pbody, p, offset);
529
530 PRINT_SSID(pbody);
531 printf(" AP : %s", etheraddr_string( pbody.ap ));
532
533 return 1;
534 }
535
536 static int
537 handle_reassoc_response(const u_char *p)
538 {
539 /* Same as a Association Reponse */
540 return handle_assoc_response(p);
541 }
542
543 static int
544 handle_probe_request(const u_char *p)
545 {
546 struct mgmt_body_t pbody;
547 int offset = 0;
548
549 memset(&pbody, 0, sizeof(pbody));
550
551 parse_elements(&pbody, p, offset);
552
553 PRINT_SSID(pbody);
554 PRINT_RATES(pbody);
555
556 return 1;
557 }
558
559 static int
560 handle_probe_response(const u_char *p)
561 {
562 struct mgmt_body_t pbody;
563 int offset = 0;
564
565 memset(&pbody, 0, sizeof(pbody));
566
567 if (!TTEST2(*p, IEEE802_11_TSTAMP_LEN + IEEE802_11_BCNINT_LEN +
568 IEEE802_11_CAPINFO_LEN))
569 return 0;
570
571 memcpy(&pbody.timestamp, p, IEEE802_11_TSTAMP_LEN);
572 offset += IEEE802_11_TSTAMP_LEN;
573 pbody.beacon_interval = EXTRACT_LE_16BITS(p+offset);
574 offset += IEEE802_11_BCNINT_LEN;
575 pbody.capability_info = EXTRACT_LE_16BITS(p+offset);
576 offset += IEEE802_11_CAPINFO_LEN;
577
578 parse_elements(&pbody, p, offset);
579
580 PRINT_SSID(pbody);
581 PRINT_RATES(pbody);
582 PRINT_DS_CHANNEL(pbody);
583
584 return 1;
585 }
586
587 static int
588 handle_atim(void)
589 {
590 /* the frame body for ATIM is null. */
591 return 1;
592 }
593
594 static int
595 handle_disassoc(const u_char *p)
596 {
597 struct mgmt_body_t pbody;
598
599 memset(&pbody, 0, sizeof(pbody));
600
601 if (!TTEST2(*p, IEEE802_11_REASON_LEN))
602 return 0;
603 pbody.reason_code = EXTRACT_LE_16BITS(p);
604
605 printf(": %s",
606 (pbody.reason_code < NUM_REASONS)
607 ? reason_text[pbody.reason_code]
608 : "Reserved" );
609
610 return 1;
611 }
612
613 static int
614 handle_auth(const u_char *p)
615 {
616 struct mgmt_body_t pbody;
617 int offset = 0;
618
619 memset(&pbody, 0, sizeof(pbody));
620
621 if (!TTEST2(*p, 6))
622 return 0;
623 pbody.auth_alg = EXTRACT_LE_16BITS(p);
624 offset += 2;
625 pbody.auth_trans_seq_num = EXTRACT_LE_16BITS(p + offset);
626 offset += 2;
627 pbody.status_code = EXTRACT_LE_16BITS(p + offset);
628 offset += 2;
629
630 parse_elements(&pbody, p, offset);
631
632 if ((pbody.auth_alg == 1) &&
633 ((pbody.auth_trans_seq_num == 2) ||
634 (pbody.auth_trans_seq_num == 3))) {
635 printf(" (%s)-%x [Challenge Text] %s",
636 (pbody.auth_alg < NUM_AUTH_ALGS)
637 ? auth_alg_text[pbody.auth_alg]
638 : "Reserved",
639 pbody.auth_trans_seq_num,
640 ((pbody.auth_trans_seq_num % 2)
641 ? ((pbody.status_code < NUM_STATUSES)
642 ? status_text[pbody.status_code]
643 : "n/a") : ""));
644 return 1;
645 }
646 printf(" (%s)-%x: %s",
647 (pbody.auth_alg < NUM_AUTH_ALGS)
648 ? auth_alg_text[pbody.auth_alg]
649 : "Reserved",
650 pbody.auth_trans_seq_num,
651 (pbody.auth_trans_seq_num % 2)
652 ? ((pbody.status_code < NUM_STATUSES)
653 ? status_text[pbody.status_code]
654 : "n/a")
655 : "");
656
657 return 1;
658 }
659
660 static int
661 handle_deauth(const struct mgmt_header_t *pmh, const u_char *p)
662 {
663 struct mgmt_body_t pbody;
664 int offset = 0;
665 const char *reason = NULL;
666
667 memset(&pbody, 0, sizeof(pbody));
668
669 if (!TTEST2(*p, IEEE802_11_REASON_LEN))
670 return 0;
671 pbody.reason_code = EXTRACT_LE_16BITS(p);
672 offset += IEEE802_11_REASON_LEN;
673
674 reason = (pbody.reason_code < NUM_REASONS)
675 ? reason_text[pbody.reason_code]
676 : "Reserved";
677
678 if (eflag) {
679 printf(": %s", reason);
680 } else {
681 printf(" (%s): %s", etheraddr_string(pmh->sa), reason);
682 }
683 return 1;
684 }
685
686 #define PRINT_HT_ACTION(v) (\
687 (v) == 0 ? printf("TxChWidth") : \
688 (v) == 1 ? printf("MIMOPwrSave") : \
689 printf("Act#%d", (v)) \
690 )
691 #define PRINT_BA_ACTION(v) (\
692 (v) == 0 ? printf("ADDBA Request") : \
693 (v) == 1 ? printf("ADDBA Response") : \
694 (v) == 2 ? printf("DELBA") : \
695 printf("Act#%d", (v)) \
696 )
697 #define PRINT_MESHLINK_ACTION(v) (\
698 (v) == 0 ? printf("Request") : \
699 (v) == 1 ? printf("Report") : \
700 printf("Act#%d", (v)) \
701 )
702 #define PRINT_MESHPEERING_ACTION(v) (\
703 (v) == 0 ? printf("Open") : \
704 (v) == 1 ? printf("Confirm") : \
705 (v) == 2 ? printf("Close") : \
706 printf("Act#%d", (v)) \
707 )
708 #define PRINT_MESHPATH_ACTION(v) (\
709 (v) == 0 ? printf("Request") : \
710 (v) == 1 ? printf("Report") : \
711 (v) == 2 ? printf("Error") : \
712 (v) == 3 ? printf("RootAnnouncement") : \
713 printf("Act#%d", (v)) \
714 )
715
716 static int
717 handle_action(const struct mgmt_header_t *pmh, const u_char *p)
718 {
719 if (!TTEST2(*p, 2))
720 return 0;
721 if (eflag) {
722 printf(": ");
723 } else {
724 printf(" (%s): ", etheraddr_string(pmh->sa));
725 }
726 switch (p[0]) {
727 case 0: printf("Spectrum Management Act#%d", p[1]); break;
728 case 1: printf("QoS Act#%d", p[1]); break;
729 case 2: printf("DLS Act#%d", p[1]); break;
730 case 3: printf("BA "); PRINT_BA_ACTION(p[1]); break;
731 case 7: printf("HT "); PRINT_HT_ACTION(p[1]); break;
732 case 13: printf("MeshLMetric "); PRINT_MESHLINK_ACTION(p[1]); break;
733 case 15: printf("Interwork Act#%d", p[1]); break;
734 case 16: printf("Resource Act#%d", p[1]); break;
735 case 17: printf("Proxy Act#%d", p[1]); break;
736 case 30: printf("MeshPeering "); PRINT_MESHPEERING_ACTION(p[1]); break;
737 case 32: printf("MeshPath "); PRINT_MESHPATH_ACTION(p[1]); break;
738 case 127: printf("Vendor Act#%d", p[1]); break;
739 default:
740 printf("Reserved(%d) Act#%d", p[0], p[1]);
741 break;
742 }
743 return 1;
744 }
745
746
747 /*********************************************************************************
748 * Print Body funcs
749 *********************************************************************************/
750
751
752 static int
753 mgmt_body_print(u_int16_t fc, const struct mgmt_header_t *pmh,
754 const u_char *p)
755 {
756 switch (FC_SUBTYPE(fc)) {
757 case ST_ASSOC_REQUEST:
758 printf("Assoc Request");
759 return handle_assoc_request(p);
760 case ST_ASSOC_RESPONSE:
761 printf("Assoc Response");
762 return handle_assoc_response(p);
763 case ST_REASSOC_REQUEST:
764 printf("ReAssoc Request");
765 return handle_reassoc_request(p);
766 case ST_REASSOC_RESPONSE:
767 printf("ReAssoc Response");
768 return handle_reassoc_response(p);
769 case ST_PROBE_REQUEST:
770 printf("Probe Request");
771 return handle_probe_request(p);
772 case ST_PROBE_RESPONSE:
773 printf("Probe Response");
774 return handle_probe_response(p);
775 case ST_BEACON:
776 printf("Beacon");
777 return handle_beacon(p);
778 case ST_ATIM:
779 printf("ATIM");
780 return handle_atim();
781 case ST_DISASSOC:
782 printf("Disassociation");
783 return handle_disassoc(p);
784 case ST_AUTH:
785 printf("Authentication");
786 if (!TTEST2(*p, 3))
787 return 0;
788 if ((p[0] == 0 ) && (p[1] == 0) && (p[2] == 0)) {
789 printf("Authentication (Shared-Key)-3 ");
790 return wep_print(p);
791 }
792 return handle_auth(p);
793 case ST_DEAUTH:
794 printf("DeAuthentication");
795 return handle_deauth(pmh, p);
796 break;
797 case ST_ACTION:
798 printf("Action");
799 return handle_action(pmh, p);
800 break;
801 default:
802 printf("Unhandled Management subtype(%x)",
803 FC_SUBTYPE(fc));
804 return 1;
805 }
806 }
807
808
809 /*********************************************************************************
810 * Handles printing all the control frame types
811 *********************************************************************************/
812
813 static int
814 ctrl_body_print(u_int16_t fc, const u_char *p)
815 {
816 switch (FC_SUBTYPE(fc)) {
817 case CTRL_CONTROL_WRAPPER:
818 printf("Control Wrapper");
819 /* XXX - requires special handling */
820 break;
821 case CTRL_BAR:
822 printf("BAR");
823 if (!TTEST2(*p, CTRL_BAR_HDRLEN))
824 return 0;
825 if (!eflag)
826 printf(" RA:%s TA:%s CTL(%x) SEQ(%u) ",
827 etheraddr_string(((const struct ctrl_bar_t *)p)->ra),
828 etheraddr_string(((const struct ctrl_bar_t *)p)->ta),
829 EXTRACT_LE_16BITS(&(((const struct ctrl_bar_t *)p)->ctl)),
830 EXTRACT_LE_16BITS(&(((const struct ctrl_bar_t *)p)->seq)));
831 break;
832 case CTRL_BA:
833 printf("BA");
834 if (!TTEST2(*p, CTRL_BA_HDRLEN))
835 return 0;
836 if (!eflag)
837 printf(" RA:%s ",
838 etheraddr_string(((const struct ctrl_ba_t *)p)->ra));
839 break;
840 case CTRL_PS_POLL:
841 printf("Power Save-Poll");
842 if (!TTEST2(*p, CTRL_PS_POLL_HDRLEN))
843 return 0;
844 printf(" AID(%x)",
845 EXTRACT_LE_16BITS(&(((const struct ctrl_ps_poll_t *)p)->aid)));
846 break;
847 case CTRL_RTS:
848 printf("Request-To-Send");
849 if (!TTEST2(*p, CTRL_RTS_HDRLEN))
850 return 0;
851 if (!eflag)
852 printf(" TA:%s ",
853 etheraddr_string(((const struct ctrl_rts_t *)p)->ta));
854 break;
855 case CTRL_CTS:
856 printf("Clear-To-Send");
857 if (!TTEST2(*p, CTRL_CTS_HDRLEN))
858 return 0;
859 if (!eflag)
860 printf(" RA:%s ",
861 etheraddr_string(((const struct ctrl_cts_t *)p)->ra));
862 break;
863 case CTRL_ACK:
864 printf("Acknowledgment");
865 if (!TTEST2(*p, CTRL_ACK_HDRLEN))
866 return 0;
867 if (!eflag)
868 printf(" RA:%s ",
869 etheraddr_string(((const struct ctrl_ack_t *)p)->ra));
870 break;
871 case CTRL_CF_END:
872 printf("CF-End");
873 if (!TTEST2(*p, CTRL_END_HDRLEN))
874 return 0;
875 if (!eflag)
876 printf(" RA:%s ",
877 etheraddr_string(((const struct ctrl_end_t *)p)->ra));
878 break;
879 case CTRL_END_ACK:
880 printf("CF-End+CF-Ack");
881 if (!TTEST2(*p, CTRL_END_ACK_HDRLEN))
882 return 0;
883 if (!eflag)
884 printf(" RA:%s ",
885 etheraddr_string(((const struct ctrl_end_ack_t *)p)->ra));
886 break;
887 default:
888 printf("Unknown Ctrl Subtype");
889 }
890 return 1;
891 }
892
893 /*
894 * Print Header funcs
895 */
896
897 /*
898 * Data Frame - Address field contents
899 *
900 * To Ds | From DS | Addr 1 | Addr 2 | Addr 3 | Addr 4
901 * 0 | 0 | DA | SA | BSSID | n/a
902 * 0 | 1 | DA | BSSID | SA | n/a
903 * 1 | 0 | BSSID | SA | DA | n/a
904 * 1 | 1 | RA | TA | DA | SA
905 */
906
907 static void
908 data_header_print(u_int16_t fc, const u_char *p, const u_int8_t **srcp,
909 const u_int8_t **dstp)
910 {
911 u_int subtype = FC_SUBTYPE(fc);
912
913 if (DATA_FRAME_IS_CF_ACK(subtype) || DATA_FRAME_IS_CF_POLL(subtype) ||
914 DATA_FRAME_IS_QOS(subtype)) {
915 printf("CF ");
916 if (DATA_FRAME_IS_CF_ACK(subtype)) {
917 if (DATA_FRAME_IS_CF_POLL(subtype))
918 printf("Ack/Poll");
919 else
920 printf("Ack");
921 } else {
922 if (DATA_FRAME_IS_CF_POLL(subtype))
923 printf("Poll");
924 }
925 if (DATA_FRAME_IS_QOS(subtype))
926 printf("+QoS");
927 printf(" ");
928 }
929
930 #define ADDR1 (p + 4)
931 #define ADDR2 (p + 10)
932 #define ADDR3 (p + 16)
933 #define ADDR4 (p + 24)
934
935 if (!FC_TO_DS(fc) && !FC_FROM_DS(fc)) {
936 if (srcp != NULL)
937 *srcp = ADDR2;
938 if (dstp != NULL)
939 *dstp = ADDR1;
940 if (!eflag)
941 return;
942 printf("DA:%s SA:%s BSSID:%s ",
943 etheraddr_string(ADDR1), etheraddr_string(ADDR2),
944 etheraddr_string(ADDR3));
945 } else if (!FC_TO_DS(fc) && FC_FROM_DS(fc)) {
946 if (srcp != NULL)
947 *srcp = ADDR3;
948 if (dstp != NULL)
949 *dstp = ADDR1;
950 if (!eflag)
951 return;
952 printf("DA:%s BSSID:%s SA:%s ",
953 etheraddr_string(ADDR1), etheraddr_string(ADDR2),
954 etheraddr_string(ADDR3));
955 } else if (FC_TO_DS(fc) && !FC_FROM_DS(fc)) {
956 if (srcp != NULL)
957 *srcp = ADDR2;
958 if (dstp != NULL)
959 *dstp = ADDR3;
960 if (!eflag)
961 return;
962 printf("BSSID:%s SA:%s DA:%s ",
963 etheraddr_string(ADDR1), etheraddr_string(ADDR2),
964 etheraddr_string(ADDR3));
965 } else if (FC_TO_DS(fc) && FC_FROM_DS(fc)) {
966 if (srcp != NULL)
967 *srcp = ADDR4;
968 if (dstp != NULL)
969 *dstp = ADDR3;
970 if (!eflag)
971 return;
972 printf("RA:%s TA:%s DA:%s SA:%s ",
973 etheraddr_string(ADDR1), etheraddr_string(ADDR2),
974 etheraddr_string(ADDR3), etheraddr_string(ADDR4));
975 }
976
977 #undef ADDR1
978 #undef ADDR2
979 #undef ADDR3
980 #undef ADDR4
981 }
982
983 static void
984 mgmt_header_print(const u_char *p, const u_int8_t **srcp,
985 const u_int8_t **dstp)
986 {
987 const struct mgmt_header_t *hp = (const struct mgmt_header_t *) p;
988
989 if (srcp != NULL)
990 *srcp = hp->sa;
991 if (dstp != NULL)
992 *dstp = hp->da;
993 if (!eflag)
994 return;
995
996 printf("BSSID:%s DA:%s SA:%s ",
997 etheraddr_string((hp)->bssid), etheraddr_string((hp)->da),
998 etheraddr_string((hp)->sa));
999 }
1000
1001 static void
1002 ctrl_header_print(u_int16_t fc, const u_char *p, const u_int8_t **srcp,
1003 const u_int8_t **dstp)
1004 {
1005 if (srcp != NULL)
1006 *srcp = NULL;
1007 if (dstp != NULL)
1008 *dstp = NULL;
1009 if (!eflag)
1010 return;
1011
1012 switch (FC_SUBTYPE(fc)) {
1013 case CTRL_BAR:
1014 printf(" RA:%s TA:%s CTL(%x) SEQ(%u) ",
1015 etheraddr_string(((const struct ctrl_bar_t *)p)->ra),
1016 etheraddr_string(((const struct ctrl_bar_t *)p)->ta),
1017 EXTRACT_LE_16BITS(&(((const struct ctrl_bar_t *)p)->ctl)),
1018 EXTRACT_LE_16BITS(&(((const struct ctrl_bar_t *)p)->seq)));
1019 break;
1020 case CTRL_BA:
1021 printf("RA:%s ",
1022 etheraddr_string(((const struct ctrl_ba_t *)p)->ra));
1023 break;
1024 case CTRL_PS_POLL:
1025 printf("BSSID:%s TA:%s ",
1026 etheraddr_string(((const struct ctrl_ps_poll_t *)p)->bssid),
1027 etheraddr_string(((const struct ctrl_ps_poll_t *)p)->ta));
1028 break;
1029 case CTRL_RTS:
1030 printf("RA:%s TA:%s ",
1031 etheraddr_string(((const struct ctrl_rts_t *)p)->ra),
1032 etheraddr_string(((const struct ctrl_rts_t *)p)->ta));
1033 break;
1034 case CTRL_CTS:
1035 printf("RA:%s ",
1036 etheraddr_string(((const struct ctrl_cts_t *)p)->ra));
1037 break;
1038 case CTRL_ACK:
1039 printf("RA:%s ",
1040 etheraddr_string(((const struct ctrl_ack_t *)p)->ra));
1041 break;
1042 case CTRL_CF_END:
1043 printf("RA:%s BSSID:%s ",
1044 etheraddr_string(((const struct ctrl_end_t *)p)->ra),
1045 etheraddr_string(((const struct ctrl_end_t *)p)->bssid));
1046 break;
1047 case CTRL_END_ACK:
1048 printf("RA:%s BSSID:%s ",
1049 etheraddr_string(((const struct ctrl_end_ack_t *)p)->ra),
1050 etheraddr_string(((const struct ctrl_end_ack_t *)p)->bssid));
1051 break;
1052 default:
1053 printf("(H) Unknown Ctrl Subtype");
1054 break;
1055 }
1056 }
1057
1058 static int
1059 extract_header_length(u_int16_t fc)
1060 {
1061 int len;
1062
1063 switch (FC_TYPE(fc)) {
1064 case T_MGMT:
1065 return MGMT_HDRLEN;
1066 case T_CTRL:
1067 switch (FC_SUBTYPE(fc)) {
1068 case CTRL_BAR:
1069 return CTRL_BAR_HDRLEN;
1070 case CTRL_PS_POLL:
1071 return CTRL_PS_POLL_HDRLEN;
1072 case CTRL_RTS:
1073 return CTRL_RTS_HDRLEN;
1074 case CTRL_CTS:
1075 return CTRL_CTS_HDRLEN;
1076 case CTRL_ACK:
1077 return CTRL_ACK_HDRLEN;
1078 case CTRL_CF_END:
1079 return CTRL_END_HDRLEN;
1080 case CTRL_END_ACK:
1081 return CTRL_END_ACK_HDRLEN;
1082 default:
1083 return 0;
1084 }
1085 case T_DATA:
1086 len = (FC_TO_DS(fc) && FC_FROM_DS(fc)) ? 30 : 24;
1087 if (DATA_FRAME_IS_QOS(FC_SUBTYPE(fc)))
1088 len += 2;
1089 return len;
1090 default:
1091 printf("unknown IEEE802.11 frame type (%d)", FC_TYPE(fc));
1092 return 0;
1093 }
1094 }
1095
1096 static int
1097 extract_mesh_header_length(const u_char *p)
1098 {
1099 return (p[0] &~ 3) ? 0 : 6*(1 + (p[0] & 3));
1100 }
1101
1102 /*
1103 * Print the 802.11 MAC header if eflag is set, and set "*srcp" and "*dstp"
1104 * to point to the source and destination MAC addresses in any case if
1105 * "srcp" and "dstp" aren't null.
1106 */
1107 static void
1108 ieee_802_11_hdr_print(u_int16_t fc, const u_char *p, u_int hdrlen,
1109 u_int meshdrlen, const u_int8_t **srcp, const u_int8_t **dstp)
1110 {
1111 if (vflag) {
1112 if (FC_MORE_DATA(fc))
1113 printf("More Data ");
1114 if (FC_MORE_FLAG(fc))
1115 printf("More Fragments ");
1116 if (FC_POWER_MGMT(fc))
1117 printf("Pwr Mgmt ");
1118 if (FC_RETRY(fc))
1119 printf("Retry ");
1120 if (FC_ORDER(fc))
1121 printf("Strictly Ordered ");
1122 if (FC_WEP(fc))
1123 printf("WEP Encrypted ");
1124 if (FC_TYPE(fc) != T_CTRL || FC_SUBTYPE(fc) != CTRL_PS_POLL)
1125 printf("%dus ",
1126 EXTRACT_LE_16BITS(
1127 &((const struct mgmt_header_t *)p)->duration));
1128 }
1129 if (meshdrlen != 0) {
1130 const struct meshcntl_t *mc =
1131 (const struct meshcntl_t *)&p[hdrlen - meshdrlen];
1132 int ae = mc->flags & 3;
1133
1134 printf("MeshData (AE %d TTL %u seq %u", ae, mc->ttl,
1135 EXTRACT_LE_32BITS(mc->seq));
1136 if (ae > 0)
1137 printf(" A4:%s", etheraddr_string(mc->addr4));
1138 if (ae > 1)
1139 printf(" A5:%s", etheraddr_string(mc->addr5));
1140 if (ae > 2)
1141 printf(" A6:%s", etheraddr_string(mc->addr6));
1142 printf(") ");
1143 }
1144
1145 switch (FC_TYPE(fc)) {
1146 case T_MGMT:
1147 mgmt_header_print(p, srcp, dstp);
1148 break;
1149 case T_CTRL:
1150 ctrl_header_print(fc, p, srcp, dstp);
1151 break;
1152 case T_DATA:
1153 data_header_print(fc, p, srcp, dstp);
1154 break;
1155 default:
1156 printf("(header) unknown IEEE802.11 frame type (%d)",
1157 FC_TYPE(fc));
1158 *srcp = NULL;
1159 *dstp = NULL;
1160 break;
1161 }
1162 }
1163
1164 #ifndef roundup2
1165 #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
1166 #endif
1167
1168 static u_int
1169 ieee802_11_print(const u_char *p, u_int length, u_int orig_caplen, int pad,
1170 u_int fcslen)
1171 {
1172 u_int16_t fc;
1173 u_int caplen, hdrlen, meshdrlen;
1174 const u_int8_t *src, *dst;
1175 u_short extracted_ethertype;
1176
1177 caplen = orig_caplen;
1178 /* Remove FCS, if present */
1179 if (length < fcslen) {
1180 printf("[|802.11]");
1181 return caplen;
1182 }
1183 length -= fcslen;
1184 if (caplen > length) {
1185 /* Amount of FCS in actual packet data, if any */
1186 fcslen = caplen - length;
1187 caplen -= fcslen;
1188 snapend -= fcslen;
1189 }
1190
1191 if (caplen < IEEE802_11_FC_LEN) {
1192 printf("[|802.11]");
1193 return orig_caplen;
1194 }
1195
1196 fc = EXTRACT_LE_16BITS(p);
1197 hdrlen = extract_header_length(fc);
1198 if (pad)
1199 hdrlen = roundup2(hdrlen, 4);
1200 if (FC_TYPE(fc) == T_DATA && DATA_FRAME_IS_QOS(FC_SUBTYPE(fc))) {
1201 meshdrlen = extract_mesh_header_length(p+hdrlen);
1202 hdrlen += meshdrlen;
1203 } else
1204 meshdrlen = 0;
1205
1206
1207 if (caplen < hdrlen) {
1208 printf("[|802.11]");
1209 return hdrlen;
1210 }
1211
1212 ieee_802_11_hdr_print(fc, p, hdrlen, meshdrlen, &src, &dst);
1213
1214 /*
1215 * Go past the 802.11 header.
1216 */
1217 length -= hdrlen;
1218 caplen -= hdrlen;
1219 p += hdrlen;
1220
1221 switch (FC_TYPE(fc)) {
1222 case T_MGMT:
1223 if (!mgmt_body_print(fc,
1224 (const struct mgmt_header_t *)(p - hdrlen), p)) {
1225 printf("[|802.11]");
1226 return hdrlen;
1227 }
1228 break;
1229 case T_CTRL:
1230 if (!ctrl_body_print(fc, p - hdrlen)) {
1231 printf("[|802.11]");
1232 return hdrlen;
1233 }
1234 break;
1235 case T_DATA:
1236 if (DATA_FRAME_IS_NULL(FC_SUBTYPE(fc)))
1237 return hdrlen; /* no-data frame */
1238 /* There may be a problem w/ AP not having this bit set */
1239 if (FC_WEP(fc)) {
1240 if (!wep_print(p)) {
1241 printf("[|802.11]");
1242 return hdrlen;
1243 }
1244 } else if (llc_print(p, length, caplen, dst, src,
1245 &extracted_ethertype) == 0) {
1246 /*
1247 * Some kinds of LLC packet we cannot
1248 * handle intelligently
1249 */
1250 if (!eflag)
1251 ieee_802_11_hdr_print(fc, p - hdrlen, hdrlen,
1252 meshdrlen, NULL, NULL);
1253 if (extracted_ethertype)
1254 printf("(LLC %s) ",
1255 etherproto_string(
1256 htons(extracted_ethertype)));
1257 if (!suppress_default_print)
1258 default_print(p, caplen);
1259 }
1260 break;
1261 default:
1262 printf("unknown 802.11 frame type (%d)", FC_TYPE(fc));
1263 break;
1264 }
1265
1266 return hdrlen;
1267 }
1268
1269 /*
1270 * This is the top level routine of the printer. 'p' points
1271 * to the 802.11 header of the packet, 'h->ts' is the timestamp,
1272 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
1273 * is the number of bytes actually captured.
1274 */
1275 u_int
1276 ieee802_11_if_print(const struct pcap_pkthdr *h, const u_char *p)
1277 {
1278 return ieee802_11_print(p, h->len, h->caplen, 0, 0);
1279 }
1280
1281 #define IEEE80211_CHAN_FHSS \
1282 (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_GFSK)
1283 #define IEEE80211_CHAN_A \
1284 (IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_OFDM)
1285 #define IEEE80211_CHAN_B \
1286 (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_CCK)
1287 #define IEEE80211_CHAN_PUREG \
1288 (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_OFDM)
1289 #define IEEE80211_CHAN_G \
1290 (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_DYN)
1291
1292 #define IS_CHAN_FHSS(flags) \
1293 ((flags & IEEE80211_CHAN_FHSS) == IEEE80211_CHAN_FHSS)
1294 #define IS_CHAN_A(flags) \
1295 ((flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)
1296 #define IS_CHAN_B(flags) \
1297 ((flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)
1298 #define IS_CHAN_PUREG(flags) \
1299 ((flags & IEEE80211_CHAN_PUREG) == IEEE80211_CHAN_PUREG)
1300 #define IS_CHAN_G(flags) \
1301 ((flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)
1302 #define IS_CHAN_ANYG(flags) \
1303 (IS_CHAN_PUREG(flags) || IS_CHAN_G(flags))
1304
1305 static void
1306 print_chaninfo(int freq, int flags)
1307 {
1308 printf("%u MHz", freq);
1309 if (IS_CHAN_FHSS(flags))
1310 printf(" FHSS");
1311 if (IS_CHAN_A(flags)) {
1312 if (flags & IEEE80211_CHAN_HALF)
1313 printf(" 11a/10Mhz");
1314 else if (flags & IEEE80211_CHAN_QUARTER)
1315 printf(" 11a/5Mhz");
1316 else
1317 printf(" 11a");
1318 }
1319 if (IS_CHAN_ANYG(flags)) {
1320 if (flags & IEEE80211_CHAN_HALF)
1321 printf(" 11g/10Mhz");
1322 else if (flags & IEEE80211_CHAN_QUARTER)
1323 printf(" 11g/5Mhz");
1324 else
1325 printf(" 11g");
1326 } else if (IS_CHAN_B(flags))
1327 printf(" 11b");
1328 if (flags & IEEE80211_CHAN_TURBO)
1329 printf(" Turbo");
1330 if (flags & IEEE80211_CHAN_HT20)
1331 printf(" ht/20");
1332 else if (flags & IEEE80211_CHAN_HT40D)
1333 printf(" ht/40-");
1334 else if (flags & IEEE80211_CHAN_HT40U)
1335 printf(" ht/40+");
1336 printf(" ");
1337 }
1338
1339 static int
1340 print_radiotap_field(struct cpack_state *s, u_int32_t bit, u_int8_t *flags)
1341 {
1342 union {
1343 int8_t i8;
1344 u_int8_t u8;
1345 int16_t i16;
1346 u_int16_t u16;
1347 u_int32_t u32;
1348 u_int64_t u64;
1349 } u, u2, u3, u4;
1350 int rc;
1351
1352 switch (bit) {
1353 case IEEE80211_RADIOTAP_FLAGS:
1354 rc = cpack_uint8(s, &u.u8);
1355 *flags = u.u8;
1356 break;
1357 case IEEE80211_RADIOTAP_RATE:
1358 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1359 case IEEE80211_RADIOTAP_DB_ANTNOISE:
1360 case IEEE80211_RADIOTAP_ANTENNA:
1361 rc = cpack_uint8(s, &u.u8);
1362 break;
1363 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
1364 case IEEE80211_RADIOTAP_DBM_ANTNOISE:
1365 rc = cpack_int8(s, &u.i8);
1366 break;
1367 case IEEE80211_RADIOTAP_CHANNEL:
1368 rc = cpack_uint16(s, &u.u16);
1369 if (rc != 0)
1370 break;
1371 rc = cpack_uint16(s, &u2.u16);
1372 break;
1373 case IEEE80211_RADIOTAP_FHSS:
1374 case IEEE80211_RADIOTAP_LOCK_QUALITY:
1375 case IEEE80211_RADIOTAP_TX_ATTENUATION:
1376 rc = cpack_uint16(s, &u.u16);
1377 break;
1378 case IEEE80211_RADIOTAP_DB_TX_ATTENUATION:
1379 rc = cpack_uint8(s, &u.u8);
1380 break;
1381 case IEEE80211_RADIOTAP_DBM_TX_POWER:
1382 rc = cpack_int8(s, &u.i8);
1383 break;
1384 case IEEE80211_RADIOTAP_TSFT:
1385 rc = cpack_uint64(s, &u.u64);
1386 break;
1387 case IEEE80211_RADIOTAP_XCHANNEL:
1388 rc = cpack_uint32(s, &u.u32);
1389 if (rc != 0)
1390 break;
1391 rc = cpack_uint16(s, &u2.u16);
1392 if (rc != 0)
1393 break;
1394 rc = cpack_uint8(s, &u3.u8);
1395 if (rc != 0)
1396 break;
1397 rc = cpack_uint8(s, &u4.u8);
1398 break;
1399 default:
1400 /* this bit indicates a field whose
1401 * size we do not know, so we cannot
1402 * proceed.
1403 */
1404 printf("[0x%08x] ", bit);
1405 return -1;
1406 }
1407
1408 if (rc != 0) {
1409 printf("[|802.11]");
1410 return rc;
1411 }
1412
1413 switch (bit) {
1414 case IEEE80211_RADIOTAP_CHANNEL:
1415 print_chaninfo(u.u16, u2.u16);
1416 break;
1417 case IEEE80211_RADIOTAP_FHSS:
1418 printf("fhset %d fhpat %d ", u.u16 & 0xff, (u.u16 >> 8) & 0xff);
1419 break;
1420 case IEEE80211_RADIOTAP_RATE:
1421 if (u.u8 & 0x80)
1422 PRINT_HT_RATE("", u.u8, " Mb/s ");
1423 else
1424 PRINT_RATE("", u.u8, " Mb/s ");
1425 break;
1426 case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
1427 printf("%ddB signal ", u.i8);
1428 break;
1429 case IEEE80211_RADIOTAP_DBM_ANTNOISE:
1430 printf("%ddB noise ", u.i8);
1431 break;
1432 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1433 printf("%ddB signal ", u.u8);
1434 break;
1435 case IEEE80211_RADIOTAP_DB_ANTNOISE:
1436 printf("%ddB noise ", u.u8);
1437 break;
1438 case IEEE80211_RADIOTAP_LOCK_QUALITY:
1439 printf("%u sq ", u.u16);
1440 break;
1441 case IEEE80211_RADIOTAP_TX_ATTENUATION:
1442 printf("%d tx power ", -(int)u.u16);
1443 break;
1444 case IEEE80211_RADIOTAP_DB_TX_ATTENUATION:
1445 printf("%ddB tx power ", -(int)u.u8);
1446 break;
1447 case IEEE80211_RADIOTAP_DBM_TX_POWER:
1448 printf("%ddBm tx power ", u.i8);
1449 break;
1450 case IEEE80211_RADIOTAP_FLAGS:
1451 if (u.u8 & IEEE80211_RADIOTAP_F_CFP)
1452 printf("cfp ");
1453 if (u.u8 & IEEE80211_RADIOTAP_F_SHORTPRE)
1454 printf("short preamble ");
1455 if (u.u8 & IEEE80211_RADIOTAP_F_WEP)
1456 printf("wep ");
1457 if (u.u8 & IEEE80211_RADIOTAP_F_FRAG)
1458 printf("fragmented ");
1459 if (u.u8 & IEEE80211_RADIOTAP_F_BADFCS)
1460 printf("bad-fcs ");
1461 break;
1462 case IEEE80211_RADIOTAP_ANTENNA:
1463 printf("antenna %d ", u.u8);
1464 break;
1465 case IEEE80211_RADIOTAP_TSFT:
1466 printf("%" PRIu64 "us tsft ", u.u64);
1467 break;
1468 case IEEE80211_RADIOTAP_XCHANNEL:
1469 print_chaninfo(u2.u16, u.u32);
1470 break;
1471 }
1472 return 0;
1473 }
1474
1475 static u_int
1476 ieee802_11_radio_print(const u_char *p, u_int length, u_int caplen)
1477 {
1478 #define BITNO_32(x) (((x) >> 16) ? 16 + BITNO_16((x) >> 16) : BITNO_16((x)))
1479 #define BITNO_16(x) (((x) >> 8) ? 8 + BITNO_8((x) >> 8) : BITNO_8((x)))
1480 #define BITNO_8(x) (((x) >> 4) ? 4 + BITNO_4((x) >> 4) : BITNO_4((x)))
1481 #define BITNO_4(x) (((x) >> 2) ? 2 + BITNO_2((x) >> 2) : BITNO_2((x)))
1482 #define BITNO_2(x) (((x) & 2) ? 1 : 0)
1483 #define BIT(n) (1U << n)
1484 #define IS_EXTENDED(__p) \
1485 (EXTRACT_LE_32BITS(__p) & BIT(IEEE80211_RADIOTAP_EXT)) != 0
1486
1487 struct cpack_state cpacker;
1488 struct ieee80211_radiotap_header *hdr;
1489 u_int32_t present, next_present;
1490 u_int32_t *presentp, *last_presentp;
1491 enum ieee80211_radiotap_type bit;
1492 int bit0;
1493 const u_char *iter;
1494 u_int len;
1495 u_int8_t flags;
1496 int pad;
1497 u_int fcslen;
1498
1499 if (caplen < sizeof(*hdr)) {
1500 printf("[|802.11]");
1501 return caplen;
1502 }
1503
1504 hdr = (struct ieee80211_radiotap_header *)p;
1505
1506 len = EXTRACT_LE_16BITS(&hdr->it_len);
1507
1508 if (caplen < len) {
1509 printf("[|802.11]");
1510 return caplen;
1511 }
1512 for (last_presentp = &hdr->it_present;
1513 IS_EXTENDED(last_presentp) &&
1514 (u_char*)(last_presentp + 1) <= p + len;
1515 last_presentp++);
1516
1517 /* are there more bitmap extensions than bytes in header? */
1518 if (IS_EXTENDED(last_presentp)) {
1519 printf("[|802.11]");
1520 return caplen;
1521 }
1522
1523 iter = (u_char*)(last_presentp + 1);
1524
1525 if (cpack_init(&cpacker, (u_int8_t*)iter, len - (iter - p)) != 0) {
1526 /* XXX */
1527 printf("[|802.11]");
1528 return caplen;
1529 }
1530
1531 /* Assume no flags */
1532 flags = 0;
1533 /* Assume no Atheros padding between 802.11 header and body */
1534 pad = 0;
1535 /* Assume no FCS at end of frame */
1536 fcslen = 0;
1537 for (bit0 = 0, presentp = &hdr->it_present; presentp <= last_presentp;
1538 presentp++, bit0 += 32) {
1539 for (present = EXTRACT_LE_32BITS(presentp); present;
1540 present = next_present) {
1541 /* clear the least significant bit that is set */
1542 next_present = present & (present - 1);
1543
1544 /* extract the least significant bit that is set */
1545 bit = (enum ieee80211_radiotap_type)
1546 (bit0 + BITNO_32(present ^ next_present));
1547
1548 if (print_radiotap_field(&cpacker, bit, &flags) != 0)
1549 goto out;
1550 }
1551 }
1552
1553 if (flags & IEEE80211_RADIOTAP_F_DATAPAD)
1554 pad = 1; /* Atheros padding */
1555 if (flags & IEEE80211_RADIOTAP_F_FCS)
1556 fcslen = 4; /* FCS at end of packet */
1557 out:
1558 return len + ieee802_11_print(p + len, length - len, caplen - len, pad,
1559 fcslen);
1560 #undef BITNO_32
1561 #undef BITNO_16
1562 #undef BITNO_8
1563 #undef BITNO_4
1564 #undef BITNO_2
1565 #undef BIT
1566 }
1567
1568 static u_int
1569 ieee802_11_avs_radio_print(const u_char *p, u_int length, u_int caplen)
1570 {
1571 u_int32_t caphdr_len;
1572
1573 if (caplen < 8) {
1574 printf("[|802.11]");
1575 return caplen;
1576 }
1577
1578 caphdr_len = EXTRACT_32BITS(p + 4);
1579 if (caphdr_len < 8) {
1580 /*
1581 * Yow! The capture header length is claimed not
1582 * to be large enough to include even the version
1583 * cookie or capture header length!
1584 */
1585 printf("[|802.11]");
1586 return caplen;
1587 }
1588
1589 if (caplen < caphdr_len) {
1590 printf("[|802.11]");
1591 return caplen;
1592 }
1593
1594 return caphdr_len + ieee802_11_print(p + caphdr_len,
1595 length - caphdr_len, caplen - caphdr_len, 0, 0);
1596 }
1597
1598 #define PRISM_HDR_LEN 144
1599
1600 #define WLANCAP_MAGIC_COOKIE_BASE 0x80211000
1601 #define WLANCAP_MAGIC_COOKIE_V1 0x80211001
1602 #define WLANCAP_MAGIC_COOKIE_V2 0x80211002
1603
1604 /*
1605 * For DLT_PRISM_HEADER; like DLT_IEEE802_11, but with an extra header,
1606 * containing information such as radio information, which we
1607 * currently ignore.
1608 *
1609 * If, however, the packet begins with WLANCAP_MAGIC_COOKIE_V1 or
1610 * WLANCAP_MAGIC_COOKIE_V2, it's really DLT_IEEE802_11_RADIO_AVS
1611 * (currently, on Linux, there's no ARPHRD_ type for
1612 * DLT_IEEE802_11_RADIO_AVS, as there is a ARPHRD_IEEE80211_PRISM
1613 * for DLT_PRISM_HEADER, so ARPHRD_IEEE80211_PRISM is used for
1614 * the AVS header, and the first 4 bytes of the header are used to
1615 * indicate whether it's a Prism header or an AVS header).
1616 */
1617 u_int
1618 prism_if_print(const struct pcap_pkthdr *h, const u_char *p)
1619 {
1620 u_int caplen = h->caplen;
1621 u_int length = h->len;
1622 u_int32_t msgcode;
1623
1624 if (caplen < 4) {
1625 printf("[|802.11]");
1626 return caplen;
1627 }
1628
1629 msgcode = EXTRACT_32BITS(p);
1630 if (msgcode == WLANCAP_MAGIC_COOKIE_V1 ||
1631 msgcode == WLANCAP_MAGIC_COOKIE_V2)
1632 return ieee802_11_avs_radio_print(p, length, caplen);
1633
1634 if (caplen < PRISM_HDR_LEN) {
1635 printf("[|802.11]");
1636 return caplen;
1637 }
1638
1639 return PRISM_HDR_LEN + ieee802_11_print(p + PRISM_HDR_LEN,
1640 length - PRISM_HDR_LEN, caplen - PRISM_HDR_LEN, 0, 0);
1641 }
1642
1643 /*
1644 * For DLT_IEEE802_11_RADIO; like DLT_IEEE802_11, but with an extra
1645 * header, containing information such as radio information.
1646 */
1647 u_int
1648 ieee802_11_radio_if_print(const struct pcap_pkthdr *h, const u_char *p)
1649 {
1650 return ieee802_11_radio_print(p, h->len, h->caplen);
1651 }
1652
1653 /*
1654 * For DLT_IEEE802_11_RADIO_AVS; like DLT_IEEE802_11, but with an
1655 * extra header, containing information such as radio information,
1656 * which we currently ignore.
1657 */
1658 u_int
1659 ieee802_11_radio_avs_if_print(const struct pcap_pkthdr *h, const u_char *p)
1660 {
1661 return ieee802_11_avs_radio_print(p, h->len, h->caplen);
1662 }