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