summaryrefslogtreecommitdiff
path: root/athsta.c
blob: 178d2cc37b740194f62e1b8444be161aefb62389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/sockio.h>

#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/if_media.h>
#include <net/route.h>

#include <net80211/ieee80211.h>
#include <net80211/ieee80211_crypto.h>
#include <net80211/ieee80211_ioctl.h>

#include <ifaddrs.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static char name[IFNAMSIZ];	/* name of interface */
static int verbose = 0;
static int only_remote = 0;
static uint8_t localaddr[6] = { 0, };
static struct ether_addr remaddr;

static u_int
ieee80211_mhz2ieee(u_int freq)
{
	if (freq == 2484)
		return 14;
	if (freq < 2484)
		return (freq - 2407) / 5;
	if (freq < 5000)
		return 15 + ((freq - 2512) / 20);
	return (freq - 5000) / 5;
}

static const char *
getcaps(int capinfo)
{
	static char capstring[32];
	char *cp = capstring;

	if (capinfo & IEEE80211_CAPINFO_ESS)
		*cp++ = 'E';
	if (capinfo & IEEE80211_CAPINFO_IBSS)
		*cp++ = 'I';
	if (capinfo & IEEE80211_CAPINFO_CF_POLLABLE)
		*cp++ = 'c';
	if (capinfo & IEEE80211_CAPINFO_CF_POLLREQ)
		*cp++ = 'C';
	if (capinfo & IEEE80211_CAPINFO_PRIVACY)
		*cp++ = 'P';
	if (capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
		*cp++ = 'S';
	if (capinfo & IEEE80211_CAPINFO_PBCC)
		*cp++ = 'B';
	if (capinfo & IEEE80211_CAPINFO_CHNL_AGILITY)
		*cp++ = 'A';
	if (capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
		*cp++ = 's';
	if (capinfo & IEEE80211_CAPINFO_RSN)
		*cp++ = 'R';
	if (capinfo & IEEE80211_CAPINFO_DSSSOFDM)
		*cp++ = 'D';
	*cp = '\0';
	return capstring;
}

static void
printie(const char* tag, const uint8_t *ie, size_t ielen, int maxlen)
{
	printf("%s", tag);
	if (verbose) {
		maxlen -= strlen(tag)+2;
		if (2*ielen > maxlen)
			maxlen--;
		printf("<");
		for (; ielen > 0; ie++, ielen--) {
			if (maxlen-- <= 0)
				break;
			printf("%02x", *ie);
		}
		if (ielen != 0)
			printf("-");
		printf(">");
	}
}

/*
 * Copy the ssid string contents into buf, truncating to fit.  If the
 * ssid is entirely printable then just copy intact.  Otherwise convert
 * to hexadecimal.  If the result is truncated then replace the last
 * three characters with "...".
 */
static int
copy_essid(char buf[], size_t bufsize, const u_int8_t *essid, size_t essid_len)
{
	const u_int8_t *p;
	size_t maxlen;
	int i;

	if (essid_len > bufsize)
		maxlen = bufsize;
	else
		maxlen = essid_len;
	/* determine printable or not */
	for (i = 0, p = essid; i < maxlen; i++, p++) {
		if (*p < ' ' || *p > 0x7e)
			break;
	}
	if (i != maxlen) {		/* not printable, print as hex */
		if (bufsize < 3)
			return 0;
		strlcpy(buf, "0x", bufsize);
		bufsize -= 2;
		p = essid;
		for (i = 0; i < maxlen && bufsize >= 2; i++) {
			sprintf(&buf[2+2*i], "%02x", p[i]);
			bufsize -= 2;
		}
		if (i != essid_len)
			memcpy(&buf[2+2*i-3], "...", 3);
	} else {			/* printable, truncate as needed */
		memcpy(buf, essid, maxlen);
		if (maxlen != essid_len)
			memcpy(&buf[maxlen-3], "...", 3);
	}
	return maxlen;
}

/* unaligned little endian access */
#define LE_READ_4(p)					\
	((u_int32_t)					\
	 ((((const u_int8_t *)(p))[0]      ) |		\
	  (((const u_int8_t *)(p))[1] <<  8) |		\
	  (((const u_int8_t *)(p))[2] << 16) |		\
	  (((const u_int8_t *)(p))[3] << 24)))

static int __inline
iswpaoui(const u_int8_t *frm)
{
	return frm[1] > 3 && LE_READ_4(frm+2) == ((WPA_OUI_TYPE<<24)|WPA_OUI);
}

static int __inline
iswmeoui(const u_int8_t *frm)
{
	return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
}

static int __inline
isatherosoui(const u_int8_t *frm)
{
	return frm[1] > 3 && LE_READ_4(frm+2) == ((ATH_OUI_TYPE<<24)|ATH_OUI);
}

static void
printies(const u_int8_t *vp, int ielen, int maxcols)
{
	while (ielen > 0) {
		switch (vp[0]) {
		case IEEE80211_ELEMID_VENDOR:
			if (iswpaoui(vp))
				printie(" WPA", vp, 2+vp[1], maxcols);
			else if (iswmeoui(vp))
				printie(" WME", vp, 2+vp[1], maxcols);
			else if (isatherosoui(vp))
				printie(" ATH", vp, 2+vp[1], maxcols);
			else
				printie(" VEN", vp, 2+vp[1], maxcols);
			break;
		case IEEE80211_ELEMID_RSN:
			printie(" RSN", vp, 2+vp[1], maxcols);
			break;
		default:
			printie(" ???", vp, 2+vp[1], maxcols);
			break;
		}
		ielen -= 2+vp[1];
		vp += 2+vp[1];
	}
}

static void
list_stations(int s, int head)
{
	uint8_t buf[24*1024];
	struct ieee80211req ireq;
	struct ieee80211req_sta_info *si, *ni;
	uint8_t *vp;
	uint8_t *cp;
	int len, print;

	(void) memset(&ireq, 0, sizeof(ireq));
	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
	ireq.i_type = IEEE80211_IOC_STA_INFO;
	ireq.i_data = buf;
	ireq.i_len = sizeof(buf);
	if (ioctl(s, SIOCG80211, &ireq) < 0)
		err(1, "unable to get station information");
	len = ireq.i_len;
	if (len < sizeof(struct ieee80211req_sta_info))
		return;

	if (head)
		printf("%-17.17s %4s %4s %4s %4s %4s %6s %6s %4s %3s\n"
			, "ADDR"
			, "AID"
			, "CHAN"
			, "RATE"
			, "RSSI"
			, "IDLE"
			, "TXSEQ"
			, "RXSEQ"
			, "CAPS"
			, "ERP"
		);

	cp = buf;
	ni = (struct ieee80211req_sta_info *)cp;

	do {

		si = ni;
		vp = (u_int8_t *)(si + 1);

		cp += si->isi_len;
		len -= si->isi_len;
		ni = (struct ieee80211req_sta_info *)cp;

		if (len < sizeof(struct ieee80211req_sta_info))
			ni = NULL;

		print = 0;

		if (memcmp (localaddr, si->isi_macaddr, sizeof (localaddr)) != 0 &&
		    (!ni || memcmp (ni->isi_macaddr, si->isi_macaddr, sizeof (ni->isi_macaddr)) != 0))
			print = 1;

		if (only_remote)
			print = (memcmp(&remaddr, si->isi_macaddr, sizeof (remaddr)) == 0);

		if (print) {
			printf("%s %4u %4d %3dM %4d %4d %6d %6d %-4.4s %3x"
				, ether_ntoa((const struct ether_addr*) si->isi_macaddr)
				, IEEE80211_AID(si->isi_associd)
				, ieee80211_mhz2ieee(si->isi_freq)
				, (si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL)/2
				, si->isi_rssi
				, si->isi_inact
				, si->isi_txseqs[0]
				, si->isi_rxseqs[0]
				, getcaps(si->isi_capinfo)
				, si->isi_erp
			);
			printies(vp, si->isi_ie_len, 24);
			printf("\n");
		}

	} while (ni);
}

static void
load_lladdr (int s)
{
	struct ifaddrs *ifap, *p;

    if (getifaddrs(&ifap) != 0)
		err(1, "couldn't get interface addresses");

    for (p = ifap; p; p = p->ifa_next) {
		if (p->ifa_addr->sa_family == AF_LINK &&
 			strcmp(name, p->ifa_name) == 0) {
			struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr;
			memcpy(localaddr, sdp->sdl_data + sdp->sdl_nlen, 6);
			break;
		}
	}

	freeifaddrs(ifap);
}

int
main (int argc, char* argv[])
{
	struct ether_addr *eth;
	int s, i;

	if (argc < 2 || argc > 3) {
		fprintf (stderr, "usage: athsta <interface> [macaddr]\n");
		exit (2);
	}

	strncpy (name, argv[1], sizeof (name));
	name[sizeof (name) - 1] = 0;

	if (argc == 3) {
		eth = ether_aton (argv[2]);
		if (!eth)
			err (1, "invalid mac address: %s", argv[2]);
		memcpy (&remaddr, eth, sizeof (remaddr));
		only_remote = 1;
	}

	if ((s = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
		err(1, "creating socket failed");

	load_lladdr (s);

	for (i = 0; ; i++) {
		list_stations (s, !(i % 10));
		fflush(stdout);
		sleep (1);
	}

	return 0;
}