3 * Fortress Technologies, Inc. All rights reserved.
4 * Charlie Lenahan (clenahan@fortresstech.com)
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
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.
24 static const char rcsid
[] =
25 "@(#) $Header: /tcpdump/master/tcpdump/print-802_11.c,v 1.13 2002-12-12 07:28:35 guy Exp $ (LBL)";
32 #include <tcpdump-stdinc.h>
38 #include "interface.h"
39 #include "addrtoname.h"
40 #include "ethertype.h"
44 #include "ieee802_11.h"
46 #define PRINT_RATES(p) \
49 const char *sep = " ["; \
50 for (z = 0; z < p.rates.length ; z++) { \
51 printf("%s%2.1f", sep, (.5 * (p.rates.rate[z] & 0x7f))); \
54 if (p.rates.length != 0) \
58 static const char *auth_alg_text
[]={"Open System","Shared Key","EAP"};
59 static const char *subtype_text
[]={
77 static const char *status_text
[] = {
79 "Unspecified failure", /* 1 */
88 "Cannot Support all requested capabilities in the Capability Information field", /* 10 */
89 "Reassociation denied due to inability to confirm that association exists", /* 11 */
90 "Association denied due to reason outside the scope of the standard", /* 12 */
91 "Responding station does not support the specified authentication algorithm ", /* 13 */
92 "Received an Authentication frame with authentication transaction " \
93 "sequence number out of expected sequence", /* 14 */
94 "Authentication rejected because of challenge failure", /* 15 */
95 "Authentication rejected due to timeout waiting for next frame in sequence", /* 16 */
96 "Association denied because AP is unable to handle additional associated stations", /* 17 */
97 "Association denied due to requesting station not supporting all of the " \
98 "data rates in BSSBasicRateSet parameter", /* 18 */
102 static const char *reason_text
[] = {
104 "Unspecified reason", /* 1 */
105 "Previous authentication no longer valid", /* 2 */
106 "Deauthenticated because sending station is leaving (or has left) IBSS or ESS", /* 3 */
107 "Disassociated due to inactivity", /* 4 */
108 "Disassociated because AP is unable to handle all currently associated stations", /* 5 */
109 "Class 2 frame receivedfrom nonauthenticated station", /* 6 */
110 "Class 3 frame received from nonassociated station", /* 7 */
111 "Disassociated because sending station is leaving (or has left) BSS", /* 8 */
112 "Station requesting (re)association is not authenticated with responding station", /* 9 */
116 static int wep_print(const u_char
*p
)
122 iv
= EXTRACT_LE_32BITS(p
);
124 printf("Data IV:%3x Pad %x KeyID %x", IV_IV(iv
), IV_PAD(iv
),
131 static int parse_elements(struct mgmt_body_t
*pbody
,const u_char
*p
,int offset
)
134 if (!TTEST2(*(p
+ offset
), 1))
136 switch (*(p
+ offset
)) {
138 if (!TTEST2(*(p
+offset
), 2))
140 memcpy(&(pbody
->ssid
),p
+offset
,2); offset
+= 2;
141 if (pbody
->ssid
.length
> 0)
143 if (!TTEST2(*(p
+offset
), pbody
->ssid
.length
))
145 memcpy(&(pbody
->ssid
.ssid
),p
+offset
,pbody
->ssid
.length
); offset
+= pbody
->ssid
.length
;
146 pbody
->ssid
.ssid
[pbody
->ssid
.length
]='\0';
150 if (!TTEST2(*(p
+offset
), 2))
152 memcpy(&(pbody
->challenge
),p
+offset
,2); offset
+= 2;
153 if (pbody
->challenge
.length
> 0)
155 if (!TTEST2(*(p
+offset
), pbody
->challenge
.length
))
157 memcpy(&(pbody
->challenge
.text
),p
+offset
,pbody
->challenge
.length
); offset
+= pbody
->challenge
.length
;
158 pbody
->challenge
.text
[pbody
->challenge
.length
]='\0';
162 if (!TTEST2(*(p
+offset
), 2))
164 memcpy(&(pbody
->rates
),p
+offset
,2); offset
+= 2;
165 if (pbody
->rates
.length
> 0) {
166 if (!TTEST2(*(p
+offset
), pbody
->rates
.length
))
168 memcpy(&(pbody
->rates
.rate
),p
+offset
,pbody
->rates
.length
); offset
+= pbody
->rates
.length
;
172 if (!TTEST2(*(p
+offset
), 3))
174 memcpy(&(pbody
->ds
),p
+offset
,3); offset
+=3;
177 if (!TTEST2(*(p
+offset
), 8))
179 memcpy(&(pbody
->cf
),p
+offset
,8); offset
+=8;
182 if (!TTEST2(*(p
+offset
), 2))
184 memcpy(&(pbody
->tim
),p
+offset
,2); offset
+=2;
185 if (!TTEST2(*(p
+offset
), 3))
187 memcpy(&(pbody
->tim
.count
),p
+offset
,3); offset
+=3;
189 if ((pbody
->tim
.length
-3) > 0)
191 if (!TTEST2(*(p
+offset
), pbody
->tim
.length
-3))
193 memcpy((pbody
->tim
.bitmap
),p
+(pbody
->tim
.length
-3),(pbody
->tim
.length
-3));
194 offset
+= pbody
->tim
.length
-3;
200 printf("(1) unhandled element_id (%d) ", *(p
+offset
) );
202 offset
+= *(p
+offset
+1) + 2;
209 /*********************************************************************************
210 * Print Handle functions for the management frame types
211 *********************************************************************************/
213 static int handle_beacon(u_int16_t fc
, const u_char
*p
)
215 struct mgmt_body_t pbody
;
218 memset(&pbody
, 0, sizeof(pbody
));
222 memcpy(&pbody
.timestamp
, p
, 8);
224 pbody
.beacon_interval
= EXTRACT_LE_16BITS(p
+offset
);
226 pbody
.capability_info
= EXTRACT_LE_16BITS(p
+offset
);
229 if (!parse_elements(&pbody
,p
,offset
))
232 printf("%s (", subtype_text
[FC_SUBTYPE(fc
)]);
233 fn_print(pbody
.ssid
.ssid
, NULL
);
236 printf(" %s CH: %u%s",
237 CAPABILITY_ESS(pbody
.capability_info
) ? "ESS" : "IBSS",
239 CAPABILITY_PRIVACY(pbody
.capability_info
) ? ", PRIVACY" : "" );
244 static int handle_assoc_request(u_int16_t fc
, const u_char
*p
)
246 struct mgmt_body_t pbody
;
249 memset(&pbody
, 0, sizeof(pbody
));
253 pbody
.capability_info
= EXTRACT_LE_16BITS(p
);
255 pbody
.listen_interval
= EXTRACT_LE_16BITS(p
+offset
);
258 if (!parse_elements(&pbody
,p
,offset
))
261 printf("%s (", subtype_text
[FC_SUBTYPE(fc
)]);
262 fn_print(pbody
.ssid
.ssid
, NULL
);
268 static int handle_assoc_response(u_int16_t fc
, const u_char
*p
)
270 struct mgmt_body_t pbody
;
273 memset(&pbody
, 0, sizeof(pbody
));
277 pbody
.capability_info
= EXTRACT_LE_16BITS(p
);
279 pbody
.status_code
= EXTRACT_LE_16BITS(p
+offset
);
281 pbody
.aid
= EXTRACT_LE_16BITS(p
+offset
);
284 if (!parse_elements(&pbody
,p
,offset
))
287 printf("%s AID(%x) :%s: %s", subtype_text
[FC_SUBTYPE(fc
)],
288 ((u_int16_t
)(pbody
.aid
<< 2 )) >> 2 ,
289 CAPABILITY_PRIVACY(pbody
.capability_info
) ? " PRIVACY " : "",
290 (pbody
.status_code
< 19 ? status_text
[pbody
.status_code
] : "n/a"));
296 static int handle_reassoc_request(u_int16_t fc
, const u_char
*p
)
298 struct mgmt_body_t pbody
;
301 memset(&pbody
, 0, sizeof(pbody
));
305 pbody
.capability_info
= EXTRACT_LE_16BITS(p
);
307 pbody
.listen_interval
= EXTRACT_LE_16BITS(p
+offset
);
309 memcpy(&pbody
.ap
,p
+offset
,6);
312 if (!parse_elements(&pbody
,p
,offset
))
315 printf("%s (", subtype_text
[FC_SUBTYPE(fc
)]);
316 fn_print(pbody
.ssid
.ssid
, NULL
);
317 printf(") AP : %s", etheraddr_string( pbody
.ap
));
322 static int handle_reassoc_response(u_int16_t fc
, const u_char
*p
)
324 /* Same as a Association Reponse */
325 return handle_assoc_response(fc
, p
);
328 static int handle_probe_request(u_int16_t fc
, const u_char
*p
)
330 struct mgmt_body_t pbody
;
333 memset(&pbody
, 0, sizeof(pbody
));
335 if (!parse_elements(&pbody
, p
, offset
))
338 printf("%s (", subtype_text
[FC_SUBTYPE(fc
)]);
339 fn_print(pbody
.ssid
.ssid
, NULL
);
346 static int handle_probe_response(u_int16_t fc
, const u_char
*p
)
348 struct mgmt_body_t pbody
;
351 memset(&pbody
, 0, sizeof(pbody
));
355 memcpy(&pbody
.timestamp
,p
,8);
357 pbody
.beacon_interval
= EXTRACT_LE_16BITS(p
+offset
);
359 pbody
.capability_info
= EXTRACT_LE_16BITS(p
+offset
);
362 if (!parse_elements(&pbody
, p
, offset
))
365 printf("%s (", subtype_text
[FC_SUBTYPE(fc
)]);
366 fn_print(pbody
.ssid
.ssid
, NULL
);
369 printf(" CH: %u%s", pbody
.ds
.channel
,
370 CAPABILITY_PRIVACY(pbody
.capability_info
) ? ", PRIVACY" : "" );
375 static int handle_atim(void)
377 /* the frame body for ATIM is null. */
382 static int handle_disassoc(u_int16_t fc
, const u_char
*p
)
384 struct mgmt_body_t pbody
;
387 memset(&pbody
, 0, sizeof(pbody
));
391 pbody
.reason_code
= EXTRACT_LE_16BITS(p
);
394 printf("%s: %s", subtype_text
[FC_SUBTYPE(fc
)],
395 pbody
.reason_code
< 10 ? reason_text
[pbody
.reason_code
] : "Reserved" );
400 static int handle_auth(u_int16_t fc
, const u_char
*p
)
402 struct mgmt_body_t pbody
;
405 memset(&pbody
, 0, sizeof(pbody
));
409 pbody
.auth_alg
= EXTRACT_LE_16BITS(p
);
411 pbody
.auth_trans_seq_num
= EXTRACT_LE_16BITS(p
+ offset
);
413 pbody
.status_code
= EXTRACT_LE_16BITS(p
+ offset
);
416 if (!parse_elements(&pbody
,p
,offset
))
419 if ((pbody
.auth_alg
== 1) &&
420 ((pbody
.auth_trans_seq_num
== 2) || (pbody
.auth_trans_seq_num
== 3))) {
421 printf("%s (%s)-%x [Challenge Text] %s",
422 subtype_text
[FC_SUBTYPE(fc
)],
423 pbody
.auth_alg
< 4 ? auth_alg_text
[pbody
.auth_alg
] : "Reserved" ,
424 pbody
.auth_trans_seq_num
,
425 ((pbody
.auth_trans_seq_num
% 2) ?
426 (pbody
.status_code
< 19 ? status_text
[pbody
.status_code
] : "n/a") : "" ));
428 printf("%s (%s)-%x: %s",
429 subtype_text
[FC_SUBTYPE(fc
)],
430 pbody
.auth_alg
< 4 ? auth_alg_text
[pbody
.auth_alg
] : "Reserved" ,
431 pbody
.auth_trans_seq_num
,
432 ((pbody
.auth_trans_seq_num
% 2) ? (pbody
.status_code
< 19 ? status_text
[pbody
.status_code
] : "n/a") : ""));
438 static int handle_deauth(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
441 struct mgmt_body_t pbody
;
444 memset(&pbody
, 0, sizeof(pbody
));
448 pbody
.reason_code
= EXTRACT_LE_16BITS(p
);
453 subtype_text
[FC_SUBTYPE(fc
)],
454 pbody
.reason_code
< 10 ? reason_text
[pbody
.reason_code
] : "Reserved" );
456 printf("%s (%s): %s",
457 subtype_text
[FC_SUBTYPE(fc
)], etheraddr_string(pmh
->sa
),
458 pbody
.reason_code
< 10 ? reason_text
[pbody
.reason_code
] : "Reserved" );
465 /*********************************************************************************
467 *********************************************************************************/
470 static int mgmt_body_print(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
473 switch (FC_SUBTYPE(fc
)) {
474 case ST_ASSOC_REQUEST
:
475 return (handle_assoc_request(fc
, p
));
476 case ST_ASSOC_RESPONSE
:
477 return (handle_assoc_response(fc
, p
));
478 case ST_REASSOC_REQUEST
:
479 return (handle_reassoc_request(fc
, p
));
480 case ST_REASSOC_RESPONSE
:
481 return (handle_reassoc_response(fc
, p
));
482 case ST_PROBE_REQUEST
:
483 return (handle_probe_request(fc
, p
));
484 case ST_PROBE_RESPONSE
:
485 return (handle_probe_response(fc
, p
));
487 return (handle_beacon(fc
, p
));
489 return (handle_atim());
491 return (handle_disassoc(fc
, p
));
495 if ((p
[0] == 0 ) && (p
[1] == 0) && (p
[2] == 0)) {
496 printf("Authentication (Shared-Key)-3 ");
497 return (wep_print(p
));
500 return (handle_auth(fc
, p
));
502 return (handle_deauth(fc
, pmh
, p
));
505 printf("Unhandled Managment subtype(%x)",
512 /*********************************************************************************
513 * Handles printing all the control frame types
514 *********************************************************************************/
516 static int ctrl_body_print(u_int16_t fc
, const u_char
*p
)
518 switch (FC_SUBTYPE(fc
)) {
520 if (!TTEST2(*p
, CTRL_PS_POLL_LEN
))
522 printf("Power Save-Poll AID(%x)",
523 EXTRACT_LE_16BITS(&(((const struct ctrl_ps_poll_t
*)p
)->aid
)));
526 if (!TTEST2(*p
, CTRL_RTS_LEN
))
529 printf("Request-To-Send");
531 printf("Request-To-Send TA:%s ",
532 etheraddr_string(((const struct ctrl_rts_t
*)p
)->ta
));
535 if (!TTEST2(*p
, CTRL_CTS_LEN
))
538 printf("Clear-To-Send");
540 printf("Clear-To-Send RA:%s ",
541 etheraddr_string(((const struct ctrl_cts_t
*)p
)->ra
));
544 if (!TTEST2(*p
, CTRL_ACK_LEN
))
547 printf("Acknowledgment");
549 printf("Acknowledgment RA:%s ",
550 etheraddr_string(((const struct ctrl_ack_t
*)p
)->ra
));
553 if (!TTEST2(*p
, CTRL_END_LEN
))
558 printf("CF-End RA:%s ",
559 etheraddr_string(((const struct ctrl_end_t
*)p
)->ra
));
562 if (!TTEST2(*p
, CTRL_END_ACK_LEN
))
565 printf("CF-End+CF-Ack");
567 printf("CF-End+CF-Ack RA:%s ",
568 etheraddr_string(((const struct ctrl_end_ack_t
*)p
)->ra
));
571 printf("(B) Unknown Ctrl Subtype");
583 * Data Frame - Address field contents
585 * To Ds | From DS | Addr 1 | Addr 2 | Addr 3 | Addr 4
586 * 0 | 0 | DA | SA | BSSID | n/a
587 * 0 | 1 | DA | BSSID | SA | n/a
588 * 1 | 0 | BSSID | SA | DA | n/a
589 * 1 | 1 | RA | TA | DA | SA
592 static void data_header_print(u_int16_t fc
, const u_char
*p
)
594 #define ADDR1 (p + 4)
595 #define ADDR2 (p + 10)
596 #define ADDR3 (p + 16)
597 #define ADDR4 (p + 24)
601 printf("DA:%s SA:%s BSSID:%s ",
602 etheraddr_string(ADDR1
), etheraddr_string(ADDR2
),
603 etheraddr_string(ADDR3
));
605 printf("DA:%s BSSID:%s SA:%s ",
606 etheraddr_string(ADDR1
), etheraddr_string(ADDR2
),
607 etheraddr_string(ADDR3
));
610 printf("BSSID:%s SA:%s DA:%s ",
611 etheraddr_string(ADDR1
), etheraddr_string(ADDR2
),
612 etheraddr_string(ADDR3
));
614 printf("RA:%s TA:%s DA:%s SA:%s ",
615 etheraddr_string(ADDR1
), etheraddr_string(ADDR2
),
616 etheraddr_string(ADDR3
), etheraddr_string(ADDR4
));
626 static void mgmt_header_print(const u_char
*p
)
628 const struct mgmt_header_t
*hp
= (const struct mgmt_header_t
*) p
;
630 printf("BSSID:%s DA:%s SA:%s ",
631 etheraddr_string((hp
)->bssid
), etheraddr_string((hp
)->da
),
632 etheraddr_string((hp
)->sa
));
635 static void ctrl_header_print(u_int16_t fc
, const u_char
*p
)
637 switch (FC_SUBTYPE(fc
)) {
639 printf("BSSID:%s TA:%s ",
640 etheraddr_string(((const struct ctrl_ps_poll_t
*)p
)->bssid
),
641 etheraddr_string(((const struct ctrl_ps_poll_t
*)p
)->ta
));
644 printf("RA:%s TA:%s ",
645 etheraddr_string(((const struct ctrl_rts_t
*)p
)->ra
),
646 etheraddr_string(((const struct ctrl_rts_t
*)p
)->ta
));
650 etheraddr_string(((const struct ctrl_cts_t
*)p
)->ra
));
654 etheraddr_string(((const struct ctrl_ack_t
*)p
)->ra
));
657 printf("RA:%s BSSID:%s ",
658 etheraddr_string(((const struct ctrl_end_t
*)p
)->ra
),
659 etheraddr_string(((const struct ctrl_end_t
*)p
)->bssid
));
662 printf("RA:%s BSSID:%s ",
663 etheraddr_string(((const struct ctrl_end_ack_t
*)p
)->ra
),
664 etheraddr_string(((const struct ctrl_end_ack_t
*)p
)->bssid
));
667 printf("(H) Unknown Ctrl Subtype");
671 static int GetHeaderLength(u_int16_t fc
)
675 switch (FC_TYPE(fc
)) {
677 iLength
= MGMT_HEADER_LEN
;
680 switch (FC_SUBTYPE(fc
)) {
682 iLength
= CTRL_PS_POLL_LEN
;
685 iLength
= CTRL_RTS_LEN
;
688 iLength
= CTRL_CTS_LEN
;
691 iLength
= CTRL_ACK_LEN
;
694 iLength
= CTRL_END_LEN
;
697 iLength
= CTRL_END_ACK_LEN
;
705 if (FC_TO_DS(fc
) && FC_FROM_DS(fc
))
711 printf("unknown IEEE802.11 frame type (%d)",
720 * Print the 802.11 MAC header
723 ieee_802_11_hdr_print(u_int16_t fc
, const u_char
*p
)
725 switch (FC_TYPE(fc
)) {
727 mgmt_header_print(p
);
731 ctrl_header_print(fc
, p
);
735 data_header_print(fc
, p
);
739 printf("(header) unknown IEEE802.11 frame type (%d)",
746 ieee802_11_print(const u_char
*p
, u_int length
, u_int caplen
)
750 u_short extracted_ethertype
;
752 fc
= EXTRACT_LE_16BITS(p
);
753 HEADER_LENGTH
= GetHeaderLength(fc
);
755 if (caplen
< HEADER_LENGTH
) {
761 ieee_802_11_hdr_print(fc
, p
);
764 * Some printers want to get back at the ethernet addresses,
765 * and/or check that they're not walking off the end of the packet.
766 * Rather than pass them all the way down, we set these globals.
769 snapend
= p
+ caplen
;
771 length
-= HEADER_LENGTH
;
772 caplen
-= HEADER_LENGTH
;
775 switch (FC_TYPE(fc
)) {
777 if (!mgmt_body_print(fc
, (const struct mgmt_header_t
*)packetp
,
785 if (!ctrl_body_print(fc
, p
- HEADER_LENGTH
)) {
792 /* There may be a problem w/ AP not having this bit set */
799 if (llc_print(p
, length
, caplen
, packetp
+ 10,
800 packetp
+ 4, &extracted_ethertype
) == 0) {
802 * Some kinds of LLC packet we cannot
803 * handle intelligently
806 ieee_802_11_hdr_print(fc
, p
- HEADER_LENGTH
);
807 if (extracted_ethertype
) {
809 etherproto_string(htons(extracted_ethertype
)));
811 if (!xflag
&& !qflag
)
812 default_print(p
, caplen
);
818 printf("(body) unhandled IEEE802.11 frame type (%d)",
824 default_print(p
, caplen
);
828 * This is the top level routine of the printer. 'p' points
829 * to the 802.11 header of the packet, 'h->ts' is the timestamp,
830 * 'h->length' is the length of the packet off the wire, and 'h->caplen'
831 * is the number of bytes actually captured.
834 ieee802_11_if_print(u_char
*user _U_
, const struct pcap_pkthdr
*h
, const u_char
*p
)
836 u_int caplen
= h
->caplen
;
837 u_int length
= h
->len
;
842 if (caplen
< IEEE802_11_FC_LEN
) {
847 ieee802_11_print(p
, length
, caplen
);
856 #define PRISM_HDR_LEN 144
859 * For DLT_PRISM_HEADER; like DLT_IEEE802_11, but with an extra header,
860 * containing information such as radio information, which we
864 prism_if_print(u_char
*user _U_
, const struct pcap_pkthdr
*h
, const u_char
*p
)
866 u_int caplen
= h
->caplen
;
867 u_int length
= h
->len
;
872 if (caplen
< PRISM_HDR_LEN
+ IEEE802_11_FC_LEN
) {
877 ieee802_11_print(p
+ PRISM_HDR_LEN
, length
- PRISM_HDR_LEN
,
878 caplen
- PRISM_HDR_LEN
);
887 #define IEEE802_11_RADIO_HDR_LEN 146
890 * For DLT_IEEE802_11_RADIO; like DLT_IEEE802_11, but with an extra
891 * header, containing information such as radio information, which we
895 ieee802_11_radio_if_print(u_char
*user _U_
, const struct pcap_pkthdr
*h
,
898 u_int caplen
= h
->caplen
;
899 u_int length
= h
->len
;
904 if (caplen
< IEEE802_11_RADIO_HDR_LEN
+ IEEE802_11_FC_LEN
) {
909 ieee802_11_print(p
+ IEEE802_11_RADIO_HDR_LEN
,
910 length
- IEEE802_11_RADIO_HDR_LEN
,
911 caplen
- IEEE802_11_RADIO_HDR_LEN
);