1 /*-
2  * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * FreeBSD: src/tools/tools/net80211/wlantxtime/wlantxtime.c,v 1.2 2009/02/19 05:36:07 sam Exp $
26  */
27 
28 /*
29  * IEEE 802.11 PHY-related support.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 
35 #include <net/if_llc.h>
36 
37 #include <netproto/802_11/_ieee80211.h>
38 #include <netproto/802_11/ieee80211.h>
39 
40 #define	IEEE80211_F_SHPREAMBLE	0x00040000	/* STATUS: use short preamble */
41 
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <strings.h>
47 #include <unistd.h>
48 
49 struct ieee80211_rate_table {
50 	int		rateCount;		/* NB: for proper padding */
51 	uint8_t		rateCodeToIndex[256];	/* back mapping */
52 	struct {
53 		uint8_t		phy;		/* CCK/OFDM/TURBO */
54 		uint32_t	rateKbps;	/* transfer rate in kbs */
55 		uint8_t		shortPreamble;	/* mask for enabling short
56 						 * preamble in CCK rate code */
57 		uint8_t		dot11Rate;	/* value for supported rates
58 						 * info element of MLME */
59 		uint8_t		ctlRateIndex;	/* index of next lower basic
60 						 * rate; used for dur. calcs */
61 		uint16_t	lpAckDuration;	/* long preamble ACK dur. */
62 		uint16_t	spAckDuration;	/* short preamble ACK dur. */
63 	} info[32];
64 };
65 
66 uint16_t
67 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
68 	uint32_t frameLen, uint16_t rate, int isShortPreamble);
69 
70 #define	KASSERT(c, msg) do {			\
71 	if (!(c)) {				\
72 		printf msg;			\
73 		putchar('\n');			\
74 		exit(-1);			\
75 	}					\
76 } while (0)
77 
78 static void
79 panic(const char *fmt, ...)
80 {
81 	va_list ap;
82 
83 	va_start(ap, fmt);
84 	vprintf(fmt, ap);
85 	va_end(ap);
86 	exit(-1);
87 }
88 
89 /* shorthands to compact tables for readability */
90 #define	OFDM	IEEE80211_T_OFDM
91 #define	CCK	IEEE80211_T_CCK
92 #define	TURBO	IEEE80211_T_TURBO
93 #define	HALF	IEEE80211_T_OFDM_HALF
94 #define	QUART	IEEE80211_T_OFDM_QUARTER
95 #define	PBCC	(IEEE80211_T_OFDM_QUARTER+1)		/* XXX */
96 #define	B(r)	(0x80 | r)
97 #define	Mb(x)	(x*1000)
98 
99 static struct ieee80211_rate_table ieee80211_11b_table = {
100     .rateCount = 4,		/* XXX no PBCC */
101     .info = {
102 /*                                   short            ctrl  */
103 /*                                Preamble  dot11Rate Rate */
104      [0] = { .phy = CCK,     1000,    0x00,      B(2),   0 },/*   1 Mb */
105      [1] = { .phy = CCK,     2000,    0x04,      B(4),   1 },/*   2 Mb */
106      [2] = { .phy = CCK,     5500,    0x04,     B(11),   1 },/* 5.5 Mb */
107      [3] = { .phy = CCK,    11000,    0x04,     B(22),   1 },/*  11 Mb */
108      [4] = { .phy = PBCC,   22000,    0x04,        44,   3 } /*  22 Mb */
109     },
110 };
111 
112 static struct ieee80211_rate_table ieee80211_11g_table = {
113     .rateCount = 12,
114     .info = {
115 /*                                   short            ctrl  */
116 /*                                Preamble  dot11Rate Rate */
117      [0] = { .phy = CCK,     1000,    0x00,      B(2),   0 },
118      [1] = { .phy = CCK,     2000,    0x04,      B(4),   1 },
119      [2] = { .phy = CCK,     5500,    0x04,     B(11),   2 },
120      [3] = { .phy = CCK,    11000,    0x04,     B(22),   3 },
121      [4] = { .phy = OFDM,    6000,    0x00,        12,   4 },
122      [5] = { .phy = OFDM,    9000,    0x00,        18,   4 },
123      [6] = { .phy = OFDM,   12000,    0x00,        24,   6 },
124      [7] = { .phy = OFDM,   18000,    0x00,        36,   6 },
125      [8] = { .phy = OFDM,   24000,    0x00,        48,   8 },
126      [9] = { .phy = OFDM,   36000,    0x00,        72,   8 },
127     [10] = { .phy = OFDM,   48000,    0x00,        96,   8 },
128     [11] = { .phy = OFDM,   54000,    0x00,       108,   8 }
129     },
130 };
131 
132 static struct ieee80211_rate_table ieee80211_11a_table = {
133     .rateCount = 8,
134     .info = {
135 /*                                   short            ctrl  */
136 /*                                Preamble  dot11Rate Rate */
137      [0] = { .phy = OFDM,    6000,    0x00,     B(12),   0 },
138      [1] = { .phy = OFDM,    9000,    0x00,        18,   0 },
139      [2] = { .phy = OFDM,   12000,    0x00,     B(24),   2 },
140      [3] = { .phy = OFDM,   18000,    0x00,        36,   2 },
141      [4] = { .phy = OFDM,   24000,    0x00,     B(48),   4 },
142      [5] = { .phy = OFDM,   36000,    0x00,        72,   4 },
143      [6] = { .phy = OFDM,   48000,    0x00,        96,   4 },
144      [7] = { .phy = OFDM,   54000,    0x00,       108,   4 }
145     },
146 };
147 
148 static struct ieee80211_rate_table ieee80211_half_table = {
149     .rateCount = 8,
150     .info = {
151 /*                                   short            ctrl  */
152 /*                                Preamble  dot11Rate Rate */
153      [0] = { .phy = HALF,    3000,    0x00,      B(6),   0 },
154      [1] = { .phy = HALF,    4500,    0x00,         9,   0 },
155      [2] = { .phy = HALF,    6000,    0x00,     B(12),   2 },
156      [3] = { .phy = HALF,    9000,    0x00,        18,   2 },
157      [4] = { .phy = HALF,   12000,    0x00,     B(24),   4 },
158      [5] = { .phy = HALF,   18000,    0x00,        36,   4 },
159      [6] = { .phy = HALF,   24000,    0x00,        48,   4 },
160      [7] = { .phy = HALF,   27000,    0x00,        54,   4 }
161     },
162 };
163 
164 static struct ieee80211_rate_table ieee80211_quarter_table = {
165     .rateCount = 8,
166     .info = {
167 /*                                   short            ctrl  */
168 /*                                Preamble  dot11Rate Rate */
169      [0] = { .phy = QUART,   1500,    0x00,      B(3),   0 },
170      [1] = { .phy = QUART,   2250,    0x00,         4,   0 },
171      [2] = { .phy = QUART,   3000,    0x00,      B(9),   2 },
172      [3] = { .phy = QUART,   4500,    0x00,         9,   2 },
173      [4] = { .phy = QUART,   6000,    0x00,     B(12),   4 },
174      [5] = { .phy = QUART,   9000,    0x00,        18,   4 },
175      [6] = { .phy = QUART,  12000,    0x00,        24,   4 },
176      [7] = { .phy = QUART,  13500,    0x00,        27,   4 }
177     },
178 };
179 
180 static struct ieee80211_rate_table ieee80211_turbog_table = {
181     .rateCount = 7,
182     .info = {
183 /*                                   short            ctrl  */
184 /*                                Preamble  dot11Rate Rate */
185      [0] = { .phy = TURBO,   12000,   0x00,     B(12),   0 },
186      [1] = { .phy = TURBO,   24000,   0x00,     B(24),   1 },
187      [2] = { .phy = TURBO,   36000,   0x00,        36,   1 },
188      [3] = { .phy = TURBO,   48000,   0x00,     B(48),   3 },
189      [4] = { .phy = TURBO,   72000,   0x00,        72,   3 },
190      [5] = { .phy = TURBO,   96000,   0x00,        96,   3 },
191      [6] = { .phy = TURBO,  108000,   0x00,       108,   3 }
192     },
193 };
194 
195 static struct ieee80211_rate_table ieee80211_turboa_table = {
196     .rateCount = 8,
197     .info = {
198 /*                                   short            ctrl  */
199 /*                                Preamble  dot11Rate Rate */
200      [0] = { .phy = TURBO,   12000,   0x00,     B(12),   0 },
201      [1] = { .phy = TURBO,   18000,   0x00,        18,   0 },
202      [2] = { .phy = TURBO,   24000,   0x00,     B(24),   2 },
203      [3] = { .phy = TURBO,   36000,   0x00,        36,   2 },
204      [4] = { .phy = TURBO,   48000,   0x00,     B(48),   4 },
205      [5] = { .phy = TURBO,   72000,   0x00,        72,   4 },
206      [6] = { .phy = TURBO,   96000,   0x00,        96,   4 },
207      [7] = { .phy = TURBO,  108000,   0x00,       108,   4 }
208     },
209 };
210 
211 #undef	Mb
212 #undef	B
213 #undef	OFDM
214 #undef	CCK
215 #undef	TURBO
216 #undef	XR
217 
218 /*
219  * Setup a rate table's reverse lookup table and fill in
220  * ack durations.  The reverse lookup tables are assumed
221  * to be initialized to zero (or at least the first entry).
222  * We use this as a key that indicates whether or not
223  * we've previously setup the reverse lookup table.
224  *
225  * XXX not reentrant, but shouldn't matter
226  */
227 static void
228 ieee80211_setup_ratetable(struct ieee80211_rate_table *rt)
229 {
230 #define	N(a)	(sizeof(a)/sizeof(a[0]))
231 #define	WLAN_CTRL_FRAME_SIZE \
232 	(sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
233 
234 	int i;
235 
236 	for (i = 0; i < N(rt->rateCodeToIndex); i++)
237 		rt->rateCodeToIndex[i] = (uint8_t) -1;
238 	for (i = 0; i < rt->rateCount; i++) {
239 		uint8_t code = rt->info[i].dot11Rate;
240 		uint8_t cix = rt->info[i].ctlRateIndex;
241 		uint8_t ctl_rate = rt->info[cix].dot11Rate;
242 
243 		rt->rateCodeToIndex[code] = i;
244 		if (code & IEEE80211_RATE_BASIC) {
245 			/*
246 			 * Map w/o basic rate bit too.
247 			 */
248 			code &= IEEE80211_RATE_VAL;
249 			rt->rateCodeToIndex[code] = i;
250 		}
251 
252 		/*
253 		 * XXX for 11g the control rate to use for 5.5 and 11 Mb/s
254 		 *     depends on whether they are marked as basic rates;
255 		 *     the static tables are setup with an 11b-compatible
256 		 *     2Mb/s rate which will work but is suboptimal
257 		 *
258 		 * NB: Control rate is always less than or equal to the
259 		 *     current rate, so control rate's reverse lookup entry
260 		 *     has been installed and following call is safe.
261 		 */
262 		rt->info[i].lpAckDuration = ieee80211_compute_duration(rt,
263 			WLAN_CTRL_FRAME_SIZE, ctl_rate, 0);
264 		rt->info[i].spAckDuration = ieee80211_compute_duration(rt,
265 			WLAN_CTRL_FRAME_SIZE, ctl_rate, IEEE80211_F_SHPREAMBLE);
266 	}
267 
268 #undef WLAN_CTRL_FRAME_SIZE
269 #undef N
270 }
271 
272 /* Setup all rate tables */
273 static void
274 ieee80211_phy_init(void)
275 {
276 #define N(arr)	(int)(sizeof(arr) / sizeof(arr[0]))
277 	static struct ieee80211_rate_table * const ratetables[] = {
278 		&ieee80211_half_table,
279 		&ieee80211_quarter_table,
280 		&ieee80211_11a_table,
281 		&ieee80211_11g_table,
282 		&ieee80211_turbog_table,
283 		&ieee80211_turboa_table,
284 		&ieee80211_turboa_table,
285 		&ieee80211_11a_table,
286 		&ieee80211_11g_table,
287 		&ieee80211_11b_table
288 	};
289 	int i;
290 
291 	for (i = 0; i < N(ratetables); ++i)
292 		ieee80211_setup_ratetable(ratetables[i]);
293 
294 #undef N
295 }
296 #define CCK_SIFS_TIME		10
297 #define CCK_PREAMBLE_BITS	144
298 #define CCK_PLCP_BITS		48
299 
300 #define OFDM_SIFS_TIME		16
301 #define OFDM_PREAMBLE_TIME	20
302 #define OFDM_PLCP_BITS		22
303 #define OFDM_SYMBOL_TIME	4
304 
305 #define OFDM_HALF_SIFS_TIME	32
306 #define OFDM_HALF_PREAMBLE_TIME	40
307 #define OFDM_HALF_PLCP_BITS	22
308 #define OFDM_HALF_SYMBOL_TIME	8
309 
310 #define OFDM_QUARTER_SIFS_TIME 		64
311 #define OFDM_QUARTER_PREAMBLE_TIME	80
312 #define OFDM_QUARTER_PLCP_BITS		22
313 #define OFDM_QUARTER_SYMBOL_TIME	16
314 
315 #define TURBO_SIFS_TIME		8
316 #define TURBO_PREAMBLE_TIME	14
317 #define TURBO_PLCP_BITS		22
318 #define TURBO_SYMBOL_TIME	4
319 
320 #define	HT_L_STF	8
321 #define	HT_L_LTF	8
322 #define	HT_L_SIG	4
323 #define	HT_SIG		8
324 #define	HT_STF		4
325 #define	HT_LTF(n)	((n) * 4)
326 
327 /*
328  * Compute the time to transmit a frame of length frameLen bytes
329  * using the specified rate, phy, and short preamble setting.
330  * SIFS is included.
331  */
332 uint16_t
333 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
334 	uint32_t frameLen, uint16_t rate, int isShortPreamble)
335 {
336 	uint8_t rix = rt->rateCodeToIndex[rate];
337 	uint32_t bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
338 	uint32_t kbps;
339 
340 	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
341 	kbps = rt->info[rix].rateKbps;
342 	if (kbps == 0)			/* XXX bandaid for channel changes */
343 		return 0;
344 
345 	switch (rt->info[rix].phy) {
346 	case IEEE80211_T_CCK:
347 		phyTime		= CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
348 		if (isShortPreamble && rt->info[rix].shortPreamble)
349 			phyTime >>= 1;
350 		numBits		= frameLen << 3;
351 		txTime		= CCK_SIFS_TIME + phyTime
352 				+ ((numBits * 1000)/kbps);
353 		break;
354 	case IEEE80211_T_OFDM:
355 		bitsPerSymbol	= (kbps * OFDM_SYMBOL_TIME) / 1000;
356 		KASSERT(bitsPerSymbol != 0, ("full rate bps"));
357 
358 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
359 		numSymbols	= howmany(numBits, bitsPerSymbol);
360 		txTime		= OFDM_SIFS_TIME
361 				+ OFDM_PREAMBLE_TIME
362 				+ (numSymbols * OFDM_SYMBOL_TIME);
363 		break;
364 	case IEEE80211_T_OFDM_HALF:
365 		bitsPerSymbol	= (kbps * OFDM_HALF_SYMBOL_TIME) / 1000;
366 		KASSERT(bitsPerSymbol != 0, ("1/4 rate bps"));
367 
368 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
369 		numSymbols	= howmany(numBits, bitsPerSymbol);
370 		txTime		= OFDM_HALF_SIFS_TIME
371 				+ OFDM_HALF_PREAMBLE_TIME
372 				+ (numSymbols * OFDM_HALF_SYMBOL_TIME);
373 		break;
374 	case IEEE80211_T_OFDM_QUARTER:
375 		bitsPerSymbol	= (kbps * OFDM_QUARTER_SYMBOL_TIME) / 1000;
376 		KASSERT(bitsPerSymbol != 0, ("1/2 rate bps"));
377 
378 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
379 		numSymbols	= howmany(numBits, bitsPerSymbol);
380 		txTime		= OFDM_QUARTER_SIFS_TIME
381 				+ OFDM_QUARTER_PREAMBLE_TIME
382 				+ (numSymbols * OFDM_QUARTER_SYMBOL_TIME);
383 		break;
384 	case IEEE80211_T_TURBO:
385 		/* we still save OFDM rates in kbps - so double them */
386 		bitsPerSymbol = ((kbps << 1) * TURBO_SYMBOL_TIME) / 1000;
387 		KASSERT(bitsPerSymbol != 0, ("turbo bps"));
388 
389 		numBits       = TURBO_PLCP_BITS + (frameLen << 3);
390 		numSymbols    = howmany(numBits, bitsPerSymbol);
391 		txTime        = TURBO_SIFS_TIME + TURBO_PREAMBLE_TIME
392 			      + (numSymbols * TURBO_SYMBOL_TIME);
393 		break;
394 	default:
395 		panic("%s: unknown phy %u (rate %u)\n", __func__,
396 		      rt->info[rix].phy, rate);
397 		break;
398 	}
399 	return txTime;
400 }
401 
402 uint32_t
403 ieee80211_compute_duration_ht(const struct ieee80211_rate_table *rt,
404 	uint32_t frameLen, uint16_t rate,
405 	int streams, int isht40, int isShortGI)
406 {
407 	static const uint16_t ht20_bps[16] = {
408 	    26, 52, 78, 104, 156, 208, 234, 260,
409 	    52, 104, 156, 208, 312, 416, 468, 520
410 	};
411 	static const uint16_t ht40_bps[16] = {
412 	    54, 108, 162, 216, 324, 432, 486, 540,
413 	    108, 216, 324, 432, 648, 864, 972, 1080,
414 	};
415 	uint32_t bitsPerSymbol, numBits, numSymbols, txTime;
416 
417 	KASSERT(rate & IEEE80211_RATE_MCS, ("not mcs %d", rate));
418 	KASSERT((rate &~ IEEE80211_RATE_MCS) < 16, ("bad mcs 0x%x", rate));
419 
420 	if (isht40)
421 		bitsPerSymbol = ht40_bps[rate & 0xf];
422 	else
423 		bitsPerSymbol = ht20_bps[rate & 0xf];
424 	numBits = OFDM_PLCP_BITS + (frameLen << 3);
425 	numSymbols = howmany(numBits, bitsPerSymbol);
426 	if (isShortGI)
427 		txTime = ((numSymbols * 18) + 4) / 5;	/* 3.6us */
428 	else
429 		txTime = numSymbols * 4;		/* 4us */
430 	return txTime + HT_L_STF + HT_L_LTF +
431 	    HT_L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
432 }
433 
434 static const struct ieee80211_rate_table *
435 mode2table(const char *mode)
436 {
437 	if (strcasecmp(mode, "half") == 0)
438 		return &ieee80211_half_table;
439 	else if (strcasecmp(mode, "quarter") == 0)
440 		return &ieee80211_quarter_table;
441 	else if (strcasecmp(mode, "hta") == 0)
442 		return &ieee80211_11a_table;	/* XXX */
443 	else if (strcasecmp(mode, "htg") == 0)
444 		return &ieee80211_11g_table;	/* XXX */
445 	else if (strcasecmp(mode, "108g") == 0)
446 		return &ieee80211_turbog_table;
447 	else if (strcasecmp(mode, "sturbo") == 0)
448 		return &ieee80211_turboa_table;
449 	else if (strcasecmp(mode, "turbo") == 0)
450 		return &ieee80211_turboa_table;
451 	else if (strcasecmp(mode, "11a") == 0)
452 		return &ieee80211_11a_table;
453 	else if (strcasecmp(mode, "11g") == 0)
454 		return &ieee80211_11g_table;
455 	else if (strcasecmp(mode, "11b") == 0)
456 		return &ieee80211_11b_table;
457 	else
458 		return NULL;
459 }
460 
461 const char *
462 srate(int rate)
463 {
464 	static char buf[32];
465 	if (rate & 1)
466 		snprintf(buf, sizeof(buf), "%u.5", rate/2);
467 	else
468 		snprintf(buf, sizeof(buf), "%u", rate/2);
469 	return buf;
470 }
471 
472 static int
473 checkpreamble(const struct ieee80211_rate_table *rt, uint8_t rix,
474 	int isShortPreamble, int verbose)
475 {
476 	if (isShortPreamble) {
477 		if (rt->info[rix].phy != IEEE80211_T_CCK) {
478 			if (verbose)
479 				warnx("short preamble not meaningful, ignored");
480 			isShortPreamble = 0;
481 		} else if (!rt->info[rix].shortPreamble) {
482 			if (verbose)
483 				warnx("short preamble not meaningful with "
484 				    "rate %s, ignored",
485 				    srate(rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC));
486 			isShortPreamble = 0;
487 		}
488 	}
489 	return isShortPreamble;
490 }
491 
492 static void
493 usage(const char *progname)
494 {
495 	fprintf(stderr, "usage: %s [-a] [-l framelen] [-m mode] [-r rate] [-s]\n",
496 	    progname);
497 	fprintf(stderr, "-a             display calculations for all possible rates\n");
498 	fprintf(stderr, "-l framelen    length in bytes of 802.11 payload (default 1536)\n");
499 	fprintf(stderr, "-m 11a         calculate for 11a channel\n");
500 	fprintf(stderr, "-m 11b         calculate for 11b channel\n");
501 	fprintf(stderr, "-m 11g         calculate for 11g channel (default)\n");
502 	fprintf(stderr, "-m half        calculate for 1/2 width channel\n");
503 	fprintf(stderr, "-m quarter     calculate for 1/4 width channel\n");
504 	fprintf(stderr, "-m 108g        calculate for dynamic turbo 11g channel\n");
505 	fprintf(stderr, "-m sturbo      calculate for static turbo channel\n");
506 	fprintf(stderr, "-m turbo       calculate for dynamic turbo 11a channel\n");
507 	fprintf(stderr, "-r rate        IEEE rate code (default 54)\n");
508 	fprintf(stderr, "-s             short preamble (default long)\n");
509 	exit(0);
510 }
511 
512 int
513 main(int argc, char *argv[])
514 {
515 	const struct ieee80211_rate_table *rt;
516 	const char *mode;
517 	uint32_t frameLen;
518 	uint16_t rate;
519 	uint16_t time;
520 	uint8_t rix;
521 	int ch, allrates, isShortPreamble, isShort;
522 	float frate;
523 
524 	ieee80211_phy_init();
525 
526 	mode = "11g";
527 	isShortPreamble = 0;
528 	frameLen = 1500
529 		 + sizeof(struct ieee80211_frame)
530 		 + LLC_SNAPFRAMELEN
531 		 + IEEE80211_CRC_LEN
532 		 ;
533 	rate = 2*54;
534 	allrates = 0;
535 	while ((ch = getopt(argc, argv, "al:m:r:s")) != -1) {
536 		switch (ch) {
537 		case 'a':
538 			allrates = 1;
539 			break;
540 		case 'l':
541 			frameLen = strtoul(optarg, NULL, 0);
542 			break;
543 		case 'm':
544 			mode = optarg;
545 			break;
546 		case 'r':
547 			frate = atof(optarg);
548 			rate = (int) 2*frate;
549 			break;
550 		case 's':
551 			isShortPreamble = 1;
552 			break;
553 		default:
554 			usage(argv[0]);
555 			break;
556 		}
557 	}
558 	rt = mode2table(mode);
559 	if (rt == NULL)
560 		errx(-1, "unknown mode %s", mode);
561 	if (!allrates) {
562 		rix = rt->rateCodeToIndex[rate];
563 		if (rix == (uint8_t) -1)
564 			errx(-1, "rate %s not valid for mode %s", srate(rate), mode);
565 		isShort = checkpreamble(rt, rix, isShortPreamble, 1);
566 
567 		time = ieee80211_compute_duration(rt, frameLen, rate, isShort);
568 		printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
569 		    time, frameLen, srate(rate),
570 		    isShort ? "short" : "long");
571 	} else {
572 		for (rix = 0; rix < rt->rateCount; rix++) {
573 			rate = rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC;
574 			isShort = checkpreamble(rt, rix, isShortPreamble, 0);
575 			time = ieee80211_compute_duration(rt, frameLen, rate,
576 			    isShort);
577 			printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
578 			    time, frameLen, srate(rate),
579 			    isShort ? "short" : "long");
580 		}
581 	}
582 	return 0;
583 }
584