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.9 2002-08-01 08:52:59 risso 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) \
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
,u_int length
)
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 struct mgmt_header_t
*pmh
,
216 struct mgmt_body_t pbody
;
219 memset(&pbody
, 0, sizeof(pbody
));
223 memcpy(&pbody
.timestamp
, p
, 8);
225 pbody
.beacon_interval
= EXTRACT_LE_16BITS(p
+offset
);
227 pbody
.capability_info
= EXTRACT_LE_16BITS(p
+offset
);
230 if (!parse_elements(&pbody
,p
,offset
))
233 printf("%s (", subtype_text
[FC_SUBTYPE(fc
)]);
234 fn_print(pbody
.ssid
.ssid
, NULL
);
237 printf(" %s CH: %u %s",
238 CAPABILITY_ESS(pbody
.capability_info
) ? "ESS" : "IBSS",
240 CAPABILITY_PRIVACY(pbody
.capability_info
) ? ", PRIVACY" : "" );
245 static int handle_assoc_request(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
248 struct mgmt_body_t pbody
;
251 memset(&pbody
, 0, sizeof(pbody
));
255 pbody
.capability_info
= EXTRACT_LE_16BITS(p
);
257 pbody
.listen_interval
= EXTRACT_LE_16BITS(p
+offset
);
260 if (!parse_elements(&pbody
,p
,offset
))
263 printf("%s (", subtype_text
[FC_SUBTYPE(fc
)]);
264 fn_print(pbody
.ssid
.ssid
, NULL
);
270 static int handle_assoc_response(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
273 struct mgmt_body_t pbody
;
276 memset(&pbody
, 0, sizeof(pbody
));
280 pbody
.capability_info
= EXTRACT_LE_16BITS(p
);
282 pbody
.status_code
= EXTRACT_LE_16BITS(p
+offset
);
284 pbody
.aid
= EXTRACT_LE_16BITS(p
+offset
);
287 if (!parse_elements(&pbody
,p
,offset
))
290 printf("%s AID(%x) :%s: %s", subtype_text
[FC_SUBTYPE(fc
)],
291 ((u_int16_t
)(pbody
.aid
<< 2 )) >> 2 ,
292 CAPABILITY_PRIVACY(pbody
.capability_info
) ? " PRIVACY " : "",
293 (pbody
.status_code
< 19 ? status_text
[pbody
.status_code
] : "n/a"));
299 static int handle_reassoc_request(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
302 struct mgmt_body_t pbody
;
305 memset(&pbody
, 0, sizeof(pbody
));
309 pbody
.capability_info
= EXTRACT_LE_16BITS(p
);
311 pbody
.listen_interval
= EXTRACT_LE_16BITS(p
+offset
);
313 memcpy(&pbody
.ap
,p
+offset
,6);
316 if (!parse_elements(&pbody
,p
,offset
))
319 printf("%s (", subtype_text
[FC_SUBTYPE(fc
)]);
320 fn_print(pbody
.ssid
.ssid
, NULL
);
321 printf(") AP : %s", etheraddr_string( pbody
.ap
));
326 static int handle_reassoc_response(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
329 /* Same as a Association Reponse */
330 return handle_assoc_response(fc
,pmh
,p
);
333 static int handle_probe_request(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
336 struct mgmt_body_t pbody
;
339 memset(&pbody
, 0, sizeof(pbody
));
341 if (!parse_elements(&pbody
, p
, offset
))
344 printf("%s (", subtype_text
[FC_SUBTYPE(fc
)]);
345 fn_print(pbody
.ssid
.ssid
, NULL
);
352 static int handle_probe_response(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
355 struct mgmt_body_t pbody
;
358 memset(&pbody
, 0, sizeof(pbody
));
362 memcpy(&pbody
.timestamp
,p
,8);
364 pbody
.beacon_interval
= EXTRACT_LE_16BITS(p
+offset
);
366 pbody
.capability_info
= EXTRACT_LE_16BITS(p
+offset
);
369 if (!parse_elements(&pbody
, p
, offset
))
372 printf("%s (", subtype_text
[FC_SUBTYPE(fc
)]);
373 fn_print(pbody
.ssid
.ssid
, NULL
);
376 printf(" CH: %u%s", pbody
.ds
.channel
,
377 CAPABILITY_PRIVACY(pbody
.capability_info
) ? ", PRIVACY" : "" );
382 static int handle_atim(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
385 /* the frame body for ATIM is null. */
390 static int handle_disassoc(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
393 struct mgmt_body_t pbody
;
396 memset(&pbody
, 0, sizeof(pbody
));
400 pbody
.reason_code
= EXTRACT_LE_16BITS(p
);
403 printf("%s: %s", subtype_text
[FC_SUBTYPE(fc
)],
404 pbody
.reason_code
< 10 ? reason_text
[pbody
.reason_code
] : "Reserved" );
409 static int handle_auth(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
412 struct mgmt_body_t pbody
;
415 memset(&pbody
, 0, sizeof(pbody
));
419 pbody
.auth_alg
= EXTRACT_LE_16BITS(p
);
421 pbody
.auth_trans_seq_num
= EXTRACT_LE_16BITS(p
+ offset
);
423 pbody
.status_code
= EXTRACT_LE_16BITS(p
+ offset
);
426 if (!parse_elements(&pbody
,p
,offset
))
429 if ((pbody
.auth_alg
== 1) &&
430 ((pbody
.auth_trans_seq_num
== 2) || (pbody
.auth_trans_seq_num
== 3))) {
431 printf("%s (%s)-%x [Challenge Text] %s",
432 subtype_text
[FC_SUBTYPE(fc
)],
433 pbody
.auth_alg
< 4 ? auth_alg_text
[pbody
.auth_alg
] : "Reserved" ,
434 pbody
.auth_trans_seq_num
,
435 ((pbody
.auth_trans_seq_num
% 2) ?
436 (pbody
.status_code
< 19 ? status_text
[pbody
.status_code
] : "n/a") : "" ));
438 printf("%s (%s)-%x: %s",
439 subtype_text
[FC_SUBTYPE(fc
)],
440 pbody
.auth_alg
< 4 ? auth_alg_text
[pbody
.auth_alg
] : "Reserved" ,
441 pbody
.auth_trans_seq_num
,
442 ((pbody
.auth_trans_seq_num
% 2) ? (pbody
.status_code
< 19 ? status_text
[pbody
.status_code
] : "n/a") : ""));
448 static int handle_deauth(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
451 struct mgmt_body_t pbody
;
454 memset(&pbody
, 0, sizeof(pbody
));
458 pbody
.reason_code
= EXTRACT_LE_16BITS(p
);
463 subtype_text
[FC_SUBTYPE(fc
)],
464 pbody
.reason_code
< 10 ? reason_text
[pbody
.reason_code
] : "Reserved" );
466 printf("%s (%s): %s",
467 subtype_text
[FC_SUBTYPE(fc
)], etheraddr_string(pmh
->sa
),
468 pbody
.reason_code
< 10 ? reason_text
[pbody
.reason_code
] : "Reserved" );
475 /*********************************************************************************
477 *********************************************************************************/
480 static int mgmt_body_print(u_int16_t fc
, const struct mgmt_header_t
*pmh
,
481 const u_char
*p
, u_int length
)
483 switch (FC_SUBTYPE(fc
)) {
484 case ST_ASSOC_REQUEST
:
485 return (handle_assoc_request(fc
, pmh
, p
));
486 case ST_ASSOC_RESPONSE
:
487 return (handle_assoc_response(fc
, pmh
, p
));
488 case ST_REASSOC_REQUEST
:
489 return (handle_reassoc_request(fc
, pmh
, p
));
490 case ST_REASSOC_RESPONSE
:
491 return (handle_reassoc_response(fc
, pmh
, p
));
492 case ST_PROBE_REQUEST
:
493 return (handle_probe_request(fc
, pmh
, p
));
494 case ST_PROBE_RESPONSE
:
495 return (handle_probe_response(fc
, pmh
, p
));
497 return (handle_beacon(fc
, pmh
, p
));
499 return (handle_atim(fc
, pmh
, p
));
501 return (handle_disassoc(fc
, pmh
, p
));
505 if ((p
[0] == 0 ) && (p
[1] == 0) && (p
[2] == 0)) {
506 printf("Authentication (Shared-Key)-3 ");
507 return (wep_print(p
, length
));
510 return (handle_auth(fc
, pmh
, p
));
512 return (handle_deauth(fc
, pmh
, p
));
515 printf("Unhandled Managment subtype(%x)",
522 /*********************************************************************************
523 * Handles printing all the control frame types
524 *********************************************************************************/
526 static int ctrl_body_print(u_int16_t fc
,const u_char
*p
, u_int length
)
528 switch (FC_SUBTYPE(fc
)) {
530 if (!TTEST2(*p
, CTRL_PS_POLL_LEN
))
532 printf("Power Save-Poll AID(%x)",
533 EXTRACT_LE_16BITS(&(((const struct ctrl_ps_poll_t
*)p
)->aid
)));
536 if (!TTEST2(*p
, CTRL_RTS_LEN
))
539 printf("Request-To-Send");
541 printf("Request-To-Send TA:%s ",
542 etheraddr_string(((const struct ctrl_rts_t
*)p
)->ta
));
545 if (!TTEST2(*p
, CTRL_CTS_LEN
))
548 printf("Clear-To-Send");
550 printf("Clear-To-Send RA:%s ",
551 etheraddr_string(((const struct ctrl_cts_t
*)p
)->ra
));
554 if (!TTEST2(*p
, CTRL_ACK_LEN
))
557 printf("Acknowledgment");
559 printf("Acknowledgment RA:%s ",
560 etheraddr_string(((const struct ctrl_ack_t
*)p
)->ra
));
563 if (!TTEST2(*p
, CTRL_END_LEN
))
568 printf("CF-End RA:%s ",
569 etheraddr_string(((const struct ctrl_end_t
*)p
)->ra
));
572 if (!TTEST2(*p
, CTRL_END_ACK_LEN
))
575 printf("CF-End+CF-Ack");
577 printf("CF-End+CF-Ack RA:%s ",
578 etheraddr_string(((const struct ctrl_end_ack_t
*)p
)->ra
));
581 printf("(B) Unknown Ctrl Subtype");
593 * Data Frame - Address field contents
595 * To Ds | From DS | Addr 1 | Addr 2 | Addr 3 | Addr 4
596 * 0 | 0 | DA | SA | BSSID | n/a
597 * 0 | 1 | DA | BSSID | SA | n/a
598 * 1 | 0 | BSSID | SA | DA | n/a
599 * 1 | 1 | RA | TA | DA | SA
602 static void data_header_print(u_int16_t fc
,const u_char
*p
, u_int length
)
604 #define ADDR1 (p + 4)
605 #define ADDR2 (p + 10)
606 #define ADDR3 (p + 16)
607 #define ADDR4 (p + 24)
611 printf("DA:%s SA:%s BSSID:%s ",
612 etheraddr_string(ADDR1
), etheraddr_string(ADDR2
),
613 etheraddr_string(ADDR3
));
615 printf("DA:%s BSSID:%s SA:%s ",
616 etheraddr_string(ADDR1
), etheraddr_string(ADDR2
),
617 etheraddr_string(ADDR3
));
620 printf("BSSID:%s SA:%s DA:%s ",
621 etheraddr_string(ADDR1
), etheraddr_string(ADDR2
),
622 etheraddr_string(ADDR3
));
624 printf("RA:%s TA:%s DA:%s SA:%s ",
625 etheraddr_string(ADDR1
), etheraddr_string(ADDR2
),
626 etheraddr_string(ADDR3
), etheraddr_string(ADDR4
));
636 static void mgmt_header_print(const u_char
*p
, u_int length
)
638 const struct mgmt_header_t
*hp
= (const struct mgmt_header_t
*) p
;
640 printf("BSSID:%s DA:%s SA:%s ",
641 etheraddr_string((hp
)->bssid
), etheraddr_string((hp
)->da
),
642 etheraddr_string((hp
)->sa
));
645 static void ctrl_header_print(u_int16_t fc
,const u_char
*p
, u_int length
)
647 switch (FC_SUBTYPE(fc
)) {
649 printf("BSSID:%s TA:%s ",
650 etheraddr_string(((const struct ctrl_ps_poll_t
*)p
)->bssid
),
651 etheraddr_string(((const struct ctrl_ps_poll_t
*)p
)->ta
));
654 printf("RA:%s TA:%s ",
655 etheraddr_string(((const struct ctrl_rts_t
*)p
)->ra
),
656 etheraddr_string(((const struct ctrl_rts_t
*)p
)->ta
));
660 etheraddr_string(((const struct ctrl_cts_t
*)p
)->ra
));
664 etheraddr_string(((const struct ctrl_ack_t
*)p
)->ra
));
667 printf("RA:%s BSSID:%s ",
668 etheraddr_string(((const struct ctrl_end_t
*)p
)->ra
),
669 etheraddr_string(((const struct ctrl_end_t
*)p
)->bssid
));
672 printf("RA:%s BSSID:%s ",
673 etheraddr_string(((const struct ctrl_end_ack_t
*)p
)->ra
),
674 etheraddr_string(((const struct ctrl_end_ack_t
*)p
)->bssid
));
677 printf("(H) Unknown Ctrl Subtype");
681 static int GetHeaderLength(u_int16_t fc
)
685 switch (FC_TYPE(fc
)) {
687 iLength
= MGMT_HEADER_LEN
;
690 switch (FC_SUBTYPE(fc
)) {
692 iLength
= CTRL_PS_POLL_LEN
;
695 iLength
= CTRL_RTS_LEN
;
698 iLength
= CTRL_CTS_LEN
;
701 iLength
= CTRL_ACK_LEN
;
704 iLength
= CTRL_END_LEN
;
707 iLength
= CTRL_END_ACK_LEN
;
715 if (FC_TO_DS(fc
) && FC_FROM_DS(fc
))
721 printf("unknown IEEE802.11 frame type (%d)",
730 * Print the 802.11 MAC header
733 ieee_802_11_print(u_int16_t fc
, const u_char
*p
, u_int length
)
735 switch (FC_TYPE(fc
)) {
737 mgmt_header_print(p
, length
);
741 ctrl_header_print(fc
, p
, length
);
745 data_header_print(fc
, p
, length
);
749 printf("(header) unknown IEEE802.11 frame type (%d)",
756 * This is the top level routine of the printer. 'p' is the points
757 * to the ether header of the packet, 'h->tv' is the timestamp,
758 * 'h->length' is the length of the packet off the wire, and 'h->caplen'
759 * is the number of bytes actually captured.
762 ieee802_11_if_print(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*p
)
764 u_int caplen
= h
->caplen
;
765 u_int length
= h
->len
;
768 u_short extracted_ethertype
;
773 if (caplen
< IEEE802_11_FC_LEN
) {
778 fc
=EXTRACT_LE_16BITS(p
);
781 ieee_802_11_print(fc
, p
, length
);
784 * Some printers want to get back at the ethernet addresses,
785 * and/or check that they're not walking off the end of the packet.
786 * Rather than pass them all the way down, we set these globals.
789 snapend
= p
+ caplen
;
791 HEADER_LENGTH
=GetHeaderLength(fc
);
793 length
-= HEADER_LENGTH
;
794 caplen
-= HEADER_LENGTH
;
797 switch (FC_TYPE(fc
)) {
799 if (!mgmt_body_print(fc
, (const struct mgmt_header_t
*)packetp
,
807 if (!ctrl_body_print(fc
, p
- HEADER_LENGTH
,
808 length
+ HEADER_LENGTH
)) {
815 /* There may be a problem w/ AP not having this bit set */
817 if (!wep_print(p
,length
)) {
822 if (llc_print(p
, length
, caplen
, packetp
+ 10,
823 packetp
+ 4, &extracted_ethertype
) == 0) {
825 * Some kinds of LLC packet we cannot
826 * handle intelligently
829 ieee_802_11_print(fc
, p
- HEADER_LENGTH
,
830 length
+ HEADER_LENGTH
);
831 if (extracted_ethertype
) {
833 etherproto_string(htons(extracted_ethertype
)));
835 if (!xflag
&& !qflag
)
836 default_print(p
, caplen
);
842 printf("(body) unhandled IEEE802.11 frame type (%d)",
848 default_print(p
, caplen
);