xref: /openwifi/user_space/inject_80211/analyze_80211.c (revision a6085186d94dfe08b0e09c18c8d4b1b4fe38ea35)
1 
2 // SPDX-FileCopyrightText: 2020 Michael Tetemke Mehari <[email protected]>
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 /*
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; version 2.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License along
16  *   with this program; if not, write to the Free Software Foundation, Inc.,
17  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include "inject_80211.h"
21 #include "radiotap.h"
22 #include "uthash.h"
23 
24 #define HEADERLEN_80211   24
25 
26 struct RECORD_t
27 {
28 	char		id[16];		// hw_mode-rate-sgi_flag-packet_size
29 	uint16_t	pkt_cnt;	// number of packets received
30 	uint64_t	ts_begin;	// beginning timestamp
31 	uint64_t	ts_end;		// ending timestamp
32 
33 	UT_hash_handle	hh;		// hash function handler
34 };
35 
36 /* 802.11n bitrates x 2 */
37 static const uint8_t rates_11n[] = {13, 26, 39, 52, 78, 104, 117, 130};
38 
39 int main(int argc, char **argv)
40 {
41 	struct pcap_pkthdr pcap_hdr;
42 	const u_char *packet;
43 	char hw_mode, id[16];
44 	int rate, sgi_flag, packet_size;
45 
46 	int n, hdr_len;
47 	struct ieee80211_radiotap_iterator rti;
48 
49 	struct RECORD_t *RECORD_ptr, *tmp_ptr, *hash_ptr = NULL;
50 
51 	if (argc < 2)
52 	{
53 		fprintf(stderr, "Usage: %s <pcap>\n", argv[0]);
54 		exit(1);
55 	}
56 
57 	pcap_t *handle;
58 	char errbuf[PCAP_ERRBUF_SIZE];
59 	handle = pcap_open_offline(argv[1], errbuf);
60 
61 	if (handle == NULL)
62 	{
63 		fprintf(stderr,"Couldn't open pcap file %s: %s\n", argv[1], errbuf);
64 		return(2);
65 	}
66 
67 	while ((packet = pcap_next(handle, &pcap_hdr)))
68 	{
69 		hdr_len = (packet[2] + (packet[3] << 8));
70 		if (pcap_hdr.len < (hdr_len + HEADERLEN_80211))
71 			continue;
72 
73 		packet_size = pcap_hdr.len - (hdr_len + HEADERLEN_80211);
74 		if (packet_size < 0)
75 			continue;
76 
77 		if (ieee80211_radiotap_iterator_init(&rti, (struct ieee80211_radiotap_header *)packet, packet_size) < 0)
78 			continue;
79 
80 		while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0)
81 		{
82 			switch (rti.this_arg_index)
83 			{
84 				case IEEE80211_RADIOTAP_RATE:
85 					rate = (rti.this_arg)[0];
86 					sgi_flag = 0;
87 					hw_mode = 'a';
88 					break;
89 
90 				case IEEE80211_RADIOTAP_MCS:
91 					rate = rates_11n[((rti.this_arg)[2])];
92 					sgi_flag = (rti.this_arg)[1] & 0x40;
93 					hw_mode = 'n';
94 					break;
95 			}
96 		}
97 
98 		// create hash table index
99 		sprintf(id, "%c-%d-%d-%d", hw_mode, rate, sgi_flag, packet_size);
100 
101 		// Hash table implementation for c : https://github.com/troydhanson/uthash
102 		HASH_FIND_STR(hash_ptr, id, RECORD_ptr);
103 		if(RECORD_ptr == NULL)
104 		{
105 			RECORD_ptr = (struct RECORD_t*)malloc(sizeof(struct RECORD_t));
106 			if(RECORD_ptr == NULL)
107 			{
108 				fprintf(stderr, "Unable to create record!\n");
109 				return 1;
110 			}
111 
112 			strcpy(RECORD_ptr->id, id);
113 			RECORD_ptr->pkt_cnt = 1;
114 			RECORD_ptr->ts_begin = 1e6*pcap_hdr.ts.tv_sec + pcap_hdr.ts.tv_usec;
115 
116 			// Add the new record to the hash table
117 			HASH_ADD_STR(hash_ptr, id, RECORD_ptr);
118 		}
119 		else
120 		{
121 			RECORD_ptr->pkt_cnt++;
122 			RECORD_ptr->ts_end = 1e6*pcap_hdr.ts.tv_sec + pcap_hdr.ts.tv_usec;
123 		}
124 	}
125 	pcap_close(handle);
126 
127 
128 	// Iterate through the hash table
129 	printf("HW MODE\tRATE(Mbps)\tSGI\tSIZE(bytes)\tCOUNT\tDELAY(sec)\n");
130 	printf("=======\t==========\t===\t===========\t=====\t=========\n");
131 	HASH_ITER(hh, hash_ptr, RECORD_ptr, tmp_ptr)
132 	{
133 		sscanf(RECORD_ptr->id, "%c-%d-%d-%d", &hw_mode, &rate, &sgi_flag, &packet_size);
134 		printf("802.11%c\t%.1f\t\t%s\t%d\t\t%d\t%.5f\n", hw_mode, rate/2.0, (sgi_flag == 0 ? "OFF" : "ON"), packet_size, RECORD_ptr->pkt_cnt, 1e-6*(RECORD_ptr->ts_end - RECORD_ptr->ts_begin));
135 	}
136 	fflush(stdout);
137 
138 	return 0;
139 }
140